DWARFStreamer.h 8.7 KB

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