1 //===- MCObjectStreamer.h - MCStreamer Object File Interface ----*- 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_MC_MCOBJECTSTREAMER_H
10 #define LLVM_MC_MCOBJECTSTREAMER_H
11 
12 #include "llvm/ADT/SetVector.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCSection.h"
16 #include "llvm/MC/MCStreamer.h"
17 
18 namespace llvm {
19 class MCAssembler;
20 class MCCodeEmitter;
21 class MCSubtargetInfo;
22 class MCExpr;
23 class MCFragment;
24 class MCDataFragment;
25 class MCAsmBackend;
26 class raw_ostream;
27 class raw_pwrite_stream;
28 
29 /// Streaming object file generation interface.
30 ///
31 /// This class provides an implementation of the MCStreamer interface which is
32 /// suitable for use with the assembler backend. Specific object file formats
33 /// are expected to subclass this interface to implement directives specific
34 /// to that file format or custom semantics expected by the object writer
35 /// implementation.
36 class MCObjectStreamer : public MCStreamer {
37   std::unique_ptr<MCAssembler> Assembler;
38   MCSection::iterator CurInsertionPoint;
39   bool EmitEHFrame;
40   bool EmitDebugFrame;
41   SmallVector<MCSymbol *, 2> PendingLabels;
42   SmallSetVector<MCSection *, 4> PendingLabelSections;
43   unsigned CurSubsectionIdx;
44   struct PendingMCFixup {
45     const MCSymbol *Sym;
46     MCFixup Fixup;
47     MCDataFragment *DF;
48     PendingMCFixup(const MCSymbol *McSym, MCDataFragment *F, MCFixup McFixup)
49         : Sym(McSym), Fixup(McFixup), DF(F) {}
50   };
51   SmallVector<PendingMCFixup, 2> PendingFixups;
52 
53   struct PendingAssignment {
54     MCSymbol *Symbol;
55     const MCExpr *Value;
56   };
57 
58   /// A list of conditional assignments we may need to emit if the target
59   /// symbol is later emitted.
60   DenseMap<const MCSymbol *, SmallVector<PendingAssignment, 1>>
61       pendingAssignments;
62 
63   virtual void emitInstToData(const MCInst &Inst, const MCSubtargetInfo&) = 0;
64   void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
65   void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
66   MCSymbol *emitCFILabel() override;
67   void emitInstructionImpl(const MCInst &Inst, const MCSubtargetInfo &STI);
68   void resolvePendingFixups();
69 
70 protected:
71   MCObjectStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
72                    std::unique_ptr<MCObjectWriter> OW,
73                    std::unique_ptr<MCCodeEmitter> Emitter);
74   ~MCObjectStreamer();
75 
76 public:
77   /// state management
78   void reset() override;
79 
80   /// Object streamers require the integrated assembler.
81   bool isIntegratedAssemblerRequired() const override { return true; }
82 
83   void emitFrames(MCAsmBackend *MAB);
84   void emitCFISections(bool EH, bool Debug) override;
85 
86   MCFragment *getCurrentFragment() const;
87 
88   void insert(MCFragment *F) {
89     flushPendingLabels(F);
90     MCSection *CurSection = getCurrentSectionOnly();
91     CurSection->getFragmentList().insert(CurInsertionPoint, F);
92     F->setParent(CurSection);
93   }
94 
95   /// Get a data fragment to write into, creating a new one if the current
96   /// fragment is not a data fragment.
97   /// Optionally a \p STI can be passed in so that a new fragment is created
98   /// if the Subtarget differs from the current fragment.
99   MCDataFragment *getOrCreateDataFragment(const MCSubtargetInfo* STI = nullptr);
100 
101 protected:
102   bool changeSectionImpl(MCSection *Section, const MCExpr *Subsection);
103 
104   /// Assign a label to the current Section and Subsection even though a
105   /// fragment is not yet present. Use flushPendingLabels(F) to associate
106   /// a fragment with this label.
107   void addPendingLabel(MCSymbol* label);
108 
109   /// If any labels have been emitted but not assigned fragments in the current
110   /// Section and Subsection, ensure that they get assigned, either to fragment
111   /// F if possible or to a new data fragment. Optionally, one can provide an
112   /// offset \p FOffset as a symbol offset within the fragment.
113   void flushPendingLabels(MCFragment *F, uint64_t FOffset = 0);
114 
115 public:
116   void visitUsedSymbol(const MCSymbol &Sym) override;
117 
118   /// Create a data fragment for any pending labels across all Sections
119   /// and Subsections.
120   void flushPendingLabels();
121 
122   MCAssembler &getAssembler() { return *Assembler; }
123   MCAssembler *getAssemblerPtr() override;
124   /// \name MCStreamer Interface
125   /// @{
126 
127   void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
128   virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment *F,
129                               uint64_t Offset);
130   void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
131   void emitConditionalAssignment(MCSymbol *Symbol,
132                                  const MCExpr *Value) override;
133   void emitValueImpl(const MCExpr *Value, unsigned Size,
134                      SMLoc Loc = SMLoc()) override;
135   void emitULEB128Value(const MCExpr *Value) override;
136   void emitSLEB128Value(const MCExpr *Value) override;
137   void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
138   void changeSection(MCSection *Section, const MCExpr *Subsection) override;
139   void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
140 
141   /// Emit an instruction to a special fragment, because this instruction
142   /// can change its size during relaxation.
143   virtual void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
144 
145   void emitBundleAlignMode(unsigned AlignPow2) override;
146   void emitBundleLock(bool AlignToEnd) override;
147   void emitBundleUnlock() override;
148   void emitBytes(StringRef Data) override;
149   void emitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
150                             unsigned ValueSize = 1,
151                             unsigned MaxBytesToEmit = 0) override;
152   void emitCodeAlignment(unsigned ByteAlignment, const MCSubtargetInfo *STI,
153                          unsigned MaxBytesToEmit = 0) override;
154   void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
155                          SMLoc Loc) override;
156   void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column,
157                              unsigned Flags, unsigned Isa,
158                              unsigned Discriminator,
159                              StringRef FileName) override;
160   void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
161                                 const MCSymbol *Label,
162                                 unsigned PointerSize) override;
163   void emitDwarfLineEndEntry(MCSection *Section, MCSymbol *LastLabel) override;
164   void emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
165                                  const MCSymbol *Label);
166   void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
167                           unsigned Column, bool PrologueEnd, bool IsStmt,
168                           StringRef FileName, SMLoc Loc) override;
169   void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin,
170                                 const MCSymbol *End) override;
171   void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
172                                       unsigned SourceFileId,
173                                       unsigned SourceLineNum,
174                                       const MCSymbol *FnStartSym,
175                                       const MCSymbol *FnEndSym) override;
176   void emitCVDefRangeDirective(
177       ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
178       StringRef FixedSizePortion) override;
179   void emitCVStringTableDirective() override;
180   void emitCVFileChecksumsDirective() override;
181   void emitCVFileChecksumOffsetDirective(unsigned FileNo) override;
182   void emitDTPRel32Value(const MCExpr *Value) override;
183   void emitDTPRel64Value(const MCExpr *Value) override;
184   void emitTPRel32Value(const MCExpr *Value) override;
185   void emitTPRel64Value(const MCExpr *Value) override;
186   void emitGPRel32Value(const MCExpr *Value) override;
187   void emitGPRel64Value(const MCExpr *Value) override;
188   Optional<std::pair<bool, std::string>>
189   emitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr,
190                      SMLoc Loc, const MCSubtargetInfo &STI) override;
191   using MCStreamer::emitFill;
192   void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
193                 SMLoc Loc = SMLoc()) override;
194   void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
195                 SMLoc Loc = SMLoc()) override;
196   void emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc,
197                 const MCSubtargetInfo &STI) override;
198   void emitFileDirective(StringRef Filename) override;
199   void emitFileDirective(StringRef Filename, StringRef CompilerVerion,
200                          StringRef TimeStamp, StringRef Description) override;
201 
202   void emitAddrsig() override;
203   void emitAddrsigSym(const MCSymbol *Sym) override;
204 
205   void finishImpl() override;
206 
207   /// Emit the absolute difference between two symbols if possible.
208   ///
209   /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
210   /// compute it.  Currently, that requires that both symbols are in the same
211   /// data fragment and that the target has not specified that diff expressions
212   /// require relocations to be emitted. Otherwise, do nothing and return
213   /// \c false.
214   ///
215   /// \pre Offset of \c Hi is greater than the offset \c Lo.
216   void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
217                               unsigned Size) override;
218 
219   void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
220                                        const MCSymbol *Lo) override;
221 
222   bool mayHaveInstructions(MCSection &Sec) const override;
223 
224   /// Emits pending conditional assignments that depend on \p Symbol
225   /// being emitted.
226   void emitPendingAssignments(MCSymbol *Symbol);
227 };
228 
229 } // end namespace llvm
230 
231 #endif
232