xref: /freebsd/contrib/llvm-project/llvm/lib/DWARFLinker/Parallel/DWARFEmitterImpl.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- DwarfEmitterImpl.h ---------------------------------------*- 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 
9 #ifndef LLVM_LIB_DWARFLINKER_PARALLEL_DWARFEMITTERIMPL_H
10 #define LLVM_LIB_DWARFLINKER_PARALLEL_DWARFEMITTERIMPL_H
11 
12 #include "DWARFLinkerCompileUnit.h"
13 #include "llvm/BinaryFormat/Swift.h"
14 #include "llvm/CodeGen/AccelTable.h"
15 #include "llvm/CodeGen/AsmPrinter.h"
16 #include "llvm/DWARFLinker/Parallel/DWARFLinker.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCInstPrinter.h"
20 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/MC/MCObjectFileInfo.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/Target/TargetMachine.h"
26 
27 namespace llvm {
28 
29 ///   User of DwarfEmitterImpl should call initialization code
30 ///   for AsmPrinter:
31 ///
32 ///   InitializeAllTargetInfos();
33 ///   InitializeAllTargetMCs();
34 ///   InitializeAllTargets();
35 ///   InitializeAllAsmPrinters();
36 
37 template <typename DataT> class AccelTable;
38 class MCCodeEmitter;
39 
40 namespace dwarf_linker {
41 namespace parallel {
42 
43 using DebugNamesUnitsOffsets = std::vector<std::variant<MCSymbol *, uint64_t>>;
44 using CompUnitIDToIdx = DenseMap<unsigned, unsigned>;
45 
46 /// This class emits DWARF data to the output stream. It emits already
47 /// generated section data and specific data, which could not be generated
48 /// by CompileUnit.
49 class DwarfEmitterImpl {
50 public:
DwarfEmitterImpl(DWARFLinker::OutputFileType OutFileType,raw_pwrite_stream & OutFile)51   DwarfEmitterImpl(DWARFLinker::OutputFileType OutFileType,
52                    raw_pwrite_stream &OutFile)
53       : OutFile(OutFile), OutFileType(OutFileType) {}
54 
55   /// Initialize AsmPrinter data.
56   Error init(Triple TheTriple, StringRef Swift5ReflectionSegmentName);
57 
58   /// Returns triple of output stream.
getTargetTriple()59   const Triple &getTargetTriple() { return MC->getTargetTriple(); }
60 
61   /// Dump the file to the disk.
finish()62   void finish() { MS->finish(); }
63 
64   /// Emit abbreviations.
65   void emitAbbrevs(const SmallVector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
66                    unsigned DwarfVersion);
67 
68   /// Emit compile unit header.
69   void emitCompileUnitHeader(DwarfUnit &Unit);
70 
71   /// Emit DIE recursively.
72   void emitDIE(DIE &Die);
73 
74   /// Returns size of generated .debug_info section.
getDebugInfoSectionSize()75   uint64_t getDebugInfoSectionSize() const { return DebugInfoSectionSize; }
76 
77   /// Emits .debug_names section according to the specified \p Table.
78   void emitDebugNames(DWARF5AccelTable &Table,
79                       DebugNamesUnitsOffsets &CUOffsets,
80                       CompUnitIDToIdx &UnitIDToIdxMap);
81 
82   /// Emits .apple_names section according to the specified \p Table.
83   void emitAppleNames(AccelTable<AppleAccelTableStaticOffsetData> &Table);
84 
85   /// Emits .apple_namespaces section according to the specified \p Table.
86   void emitAppleNamespaces(AccelTable<AppleAccelTableStaticOffsetData> &Table);
87 
88   /// Emits .apple_objc section according to the specified \p Table.
89   void emitAppleObjc(AccelTable<AppleAccelTableStaticOffsetData> &Table);
90 
91   /// Emits .apple_types section according to the specified \p Table.
92   void emitAppleTypes(AccelTable<AppleAccelTableStaticTypeData> &Table);
93 
94 private:
95   // Enumerate all string patches and write them into the destination section.
96   // Order of patches is the same as in original input file. To avoid emitting
97   // the same string twice we accumulate NextOffset value. Thus if string
98   // offset smaller than NextOffset value then the patch is skipped (as that
99   // string was emitted earlier).
100   template <typename PatchTy>
101   void emitStringsImpl(ArrayList<PatchTy> &StringPatches,
102                        const StringEntryToDwarfStringPoolEntryMap &Strings,
103                        uint64_t &NextOffset, MCSection *OutSection);
104 
105   /// \defgroup MCObjects MC layer objects constructed by the streamer
106   /// @{
107   std::unique_ptr<MCRegisterInfo> MRI;
108   std::unique_ptr<MCAsmInfo> MAI;
109   std::unique_ptr<MCObjectFileInfo> MOFI;
110   std::unique_ptr<MCContext> MC;
111   MCAsmBackend *MAB; // Owned by MCStreamer
112   std::unique_ptr<MCInstrInfo> MII;
113   std::unique_ptr<MCSubtargetInfo> MSTI;
114   std::unique_ptr<MCInstPrinter> MIP; // Owned by AsmPrinter
115   MCCodeEmitter *MCE; // Owned by MCStreamer
116   MCStreamer *MS;     // Owned by AsmPrinter
117   std::unique_ptr<TargetMachine> TM;
118   std::unique_ptr<AsmPrinter> Asm;
119   /// @}
120 
121   /// The output file we stream the linked Dwarf to.
122   raw_pwrite_stream &OutFile;
123   DWARFLinkerBase::OutputFileType OutFileType =
124       DWARFLinkerBase::OutputFileType::Object;
125 
126   uint64_t DebugInfoSectionSize = 0;
127 };
128 
129 } // end of namespace parallel
130 } // end of namespace dwarf_linker
131 } // end of namespace llvm
132 
133 #endif // LLVM_LIB_DWARFLINKER_PARALLEL_DWARFEMITTERIMPL_H
134