DWARFStreamer.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DwarfStreamer.h ------------------------------------------*- 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_DWARFLINKER_DWARFSTREAMER_H
  14. #define LLVM_DWARFLINKER_DWARFSTREAMER_H
  15. #include "llvm/BinaryFormat/Swift.h"
  16. #include "llvm/CodeGen/AccelTable.h"
  17. #include "llvm/CodeGen/AsmPrinter.h"
  18. #include "llvm/DWARFLinker/DWARFLinker.h"
  19. #include "llvm/MC/MCAsmInfo.h"
  20. #include "llvm/MC/MCContext.h"
  21. #include "llvm/MC/MCInstrInfo.h"
  22. #include "llvm/MC/MCObjectFileInfo.h"
  23. #include "llvm/MC/MCRegisterInfo.h"
  24. #include "llvm/Target/TargetMachine.h"
  25. namespace llvm {
  26. enum class OutputFileType {
  27. Object,
  28. Assembly,
  29. };
  30. /// User of DwarfStreamer should call initialization code
  31. /// for AsmPrinter:
  32. ///
  33. /// InitializeAllTargetInfos();
  34. /// InitializeAllTargetMCs();
  35. /// InitializeAllTargets();
  36. /// InitializeAllAsmPrinters();
  37. class MCCodeEmitter;
  38. /// The Dwarf streaming logic.
  39. ///
  40. /// All interactions with the MC layer that is used to build the debug
  41. /// information binary representation are handled in this class.
  42. class DwarfStreamer : public DwarfEmitter {
  43. public:
  44. DwarfStreamer(OutputFileType OutFileType, raw_pwrite_stream &OutFile,
  45. std::function<StringRef(StringRef Input)> Translator,
  46. messageHandler Error, messageHandler Warning)
  47. : OutFile(OutFile), OutFileType(OutFileType), Translator(Translator),
  48. ErrorHandler(Error), WarningHandler(Warning) {}
  49. bool init(Triple TheTriple, StringRef Swift5ReflectionSegmentName);
  50. /// Dump the file to the disk.
  51. void finish();
  52. AsmPrinter &getAsmPrinter() const { return *Asm; }
  53. /// Set the current output section to debug_info and change
  54. /// the MC Dwarf version to \p DwarfVersion.
  55. void switchToDebugInfoSection(unsigned DwarfVersion);
  56. /// Emit the compilation unit header for \p Unit in the
  57. /// debug_info section.
  58. ///
  59. /// As a side effect, this also switches the current Dwarf version
  60. /// of the MC layer to the one of U.getOrigUnit().
  61. void emitCompileUnitHeader(CompileUnit &Unit, unsigned DwarfVersion) override;
  62. /// Recursively emit the DIE tree rooted at \p Die.
  63. void emitDIE(DIE &Die) override;
  64. /// Emit the abbreviation table \p Abbrevs to the debug_abbrev section.
  65. void emitAbbrevs(const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
  66. unsigned DwarfVersion) override;
  67. /// Emit DIE containing warnings.
  68. void emitPaperTrailWarningsDie(DIE &Die) override;
  69. /// Emit contents of section SecName From Obj.
  70. void emitSectionContents(StringRef SecData, StringRef SecName) override;
  71. /// Emit the string table described by \p Pool.
  72. void emitStrings(const NonRelocatableStringpool &Pool) override;
  73. /// Emit the swift_ast section stored in \p Buffer.
  74. void emitSwiftAST(StringRef Buffer);
  75. /// Emit the swift reflection section stored in \p Buffer.
  76. void emitSwiftReflectionSection(
  77. llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind,
  78. StringRef Buffer, uint32_t Alignment, uint32_t Size);
  79. /// Emit debug_ranges for \p FuncRange by translating the
  80. /// original \p Entries.
  81. void emitRangesEntries(
  82. int64_t UnitPcOffset, uint64_t OrigLowPc,
  83. const FunctionIntervals::const_iterator &FuncRange,
  84. const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
  85. unsigned AddressSize) override;
  86. /// Emit debug_aranges entries for \p Unit and if \p DoRangesSection is true,
  87. /// also emit the debug_ranges entries for the DW_TAG_compile_unit's
  88. /// DW_AT_ranges attribute.
  89. void emitUnitRangesEntries(CompileUnit &Unit, bool DoRangesSection) override;
  90. uint64_t getRangesSectionSize() const override { return RangesSectionSize; }
  91. /// Emit the debug_loc contribution for \p Unit by copying the entries from
  92. /// \p Dwarf and offsetting them. Update the location attributes to point to
  93. /// the new entries.
  94. void emitLocationsForUnit(
  95. const CompileUnit &Unit, DWARFContext &Dwarf,
  96. std::function<void(StringRef, SmallVectorImpl<uint8_t> &)> ProcessExpr)
  97. override;
  98. /// Emit the line table described in \p Rows into the debug_line section.
  99. void emitLineTableForUnit(MCDwarfLineTableParams Params,
  100. StringRef PrologueBytes, unsigned MinInstLength,
  101. std::vector<DWARFDebugLine::Row> &Rows,
  102. unsigned AdddressSize) override;
  103. /// Copy the debug_line over to the updated binary while unobfuscating the
  104. /// file names and directories.
  105. void translateLineTable(DataExtractor LineData, uint64_t Offset) override;
  106. uint64_t getLineSectionSize() const override { return LineSectionSize; }
  107. /// Emit the .debug_pubnames contribution for \p Unit.
  108. void emitPubNamesForUnit(const CompileUnit &Unit) override;
  109. /// Emit the .debug_pubtypes contribution for \p Unit.
  110. void emitPubTypesForUnit(const CompileUnit &Unit) override;
  111. /// Emit a CIE.
  112. void emitCIE(StringRef CIEBytes) override;
  113. /// Emit an FDE with data \p Bytes.
  114. void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address,
  115. StringRef Bytes) override;
  116. /// Emit DWARF debug names.
  117. void emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table) override;
  118. /// Emit Apple namespaces accelerator table.
  119. void emitAppleNamespaces(
  120. AccelTable<AppleAccelTableStaticOffsetData> &Table) override;
  121. /// Emit Apple names accelerator table.
  122. void
  123. emitAppleNames(AccelTable<AppleAccelTableStaticOffsetData> &Table) override;
  124. /// Emit Apple Objective-C accelerator table.
  125. void
  126. emitAppleObjc(AccelTable<AppleAccelTableStaticOffsetData> &Table) override;
  127. /// Emit Apple type accelerator table.
  128. void
  129. emitAppleTypes(AccelTable<AppleAccelTableStaticTypeData> &Table) override;
  130. uint64_t getFrameSectionSize() const override { return FrameSectionSize; }
  131. uint64_t getDebugInfoSectionSize() const override {
  132. return DebugInfoSectionSize;
  133. }
  134. private:
  135. inline void error(const Twine &Error, StringRef Context = "") {
  136. if (ErrorHandler)
  137. ErrorHandler(Error, Context, nullptr);
  138. }
  139. inline void warn(const Twine &Warning, StringRef Context = "") {
  140. if (WarningHandler)
  141. WarningHandler(Warning, Context, nullptr);
  142. }
  143. /// \defgroup MCObjects MC layer objects constructed by the streamer
  144. /// @{
  145. std::unique_ptr<MCRegisterInfo> MRI;
  146. std::unique_ptr<MCAsmInfo> MAI;
  147. std::unique_ptr<MCObjectFileInfo> MOFI;
  148. std::unique_ptr<MCContext> MC;
  149. MCAsmBackend *MAB; // Owned by MCStreamer
  150. std::unique_ptr<MCInstrInfo> MII;
  151. std::unique_ptr<MCSubtargetInfo> MSTI;
  152. MCInstPrinter *MIP; // Owned by AsmPrinter
  153. MCCodeEmitter *MCE; // Owned by MCStreamer
  154. MCStreamer *MS; // Owned by AsmPrinter
  155. std::unique_ptr<TargetMachine> TM;
  156. std::unique_ptr<AsmPrinter> Asm;
  157. /// @}
  158. /// The output file we stream the linked Dwarf to.
  159. raw_pwrite_stream &OutFile;
  160. OutputFileType OutFileType = OutputFileType::Object;
  161. std::function<StringRef(StringRef Input)> Translator;
  162. uint64_t RangesSectionSize = 0;
  163. uint64_t LocSectionSize = 0;
  164. uint64_t LineSectionSize = 0;
  165. uint64_t FrameSectionSize = 0;
  166. uint64_t DebugInfoSectionSize = 0;
  167. /// Keep track of emitted CUs and their Unique ID.
  168. struct EmittedUnit {
  169. unsigned ID;
  170. MCSymbol *LabelBegin;
  171. };
  172. std::vector<EmittedUnit> EmittedUnits;
  173. /// Emit the pubnames or pubtypes section contribution for \p
  174. /// Unit into \p Sec. The data is provided in \p Names.
  175. void emitPubSectionForUnit(MCSection *Sec, StringRef Name,
  176. const CompileUnit &Unit,
  177. const std::vector<CompileUnit::AccelInfo> &Names);
  178. messageHandler ErrorHandler = nullptr;
  179. messageHandler WarningHandler = nullptr;
  180. };
  181. } // end namespace llvm
  182. #endif // LLVM_DWARFLINKER_DWARFSTREAMER_H
  183. #ifdef __GNUC__
  184. #pragma GCC diagnostic pop
  185. #endif