1 //===- MCObjectStreamer.h - MCStreamer Object File Interface ----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_MC_MCOBJECTSTREAMER_H 11 #define LLVM_MC_MCOBJECTSTREAMER_H 12 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 struct PendingMCFixup { 43 const MCSymbol *Sym; 44 MCFixup Fixup; 45 MCDataFragment *DF; PendingMCFixupPendingMCFixup46 PendingMCFixup(const MCSymbol *McSym, MCDataFragment *F, MCFixup McFixup) 47 : Sym(McSym), Fixup(McFixup), DF(F) {} 48 }; 49 SmallVector<PendingMCFixup, 2> PendingFixups; 50 51 virtual void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo&) = 0; 52 void EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override; 53 void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override; 54 MCSymbol *EmitCFILabel() override; 55 void EmitInstructionImpl(const MCInst &Inst, const MCSubtargetInfo &STI); 56 void resolvePendingFixups(); 57 58 protected: 59 MCObjectStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB, 60 std::unique_ptr<MCObjectWriter> OW, 61 std::unique_ptr<MCCodeEmitter> Emitter); 62 ~MCObjectStreamer(); 63 64 public: 65 /// state management 66 void reset() override; 67 68 /// Object streamers require the integrated assembler. isIntegratedAssemblerRequired()69 bool isIntegratedAssemblerRequired() const override { return true; } 70 71 void EmitFrames(MCAsmBackend *MAB); 72 void EmitCFISections(bool EH, bool Debug) override; 73 74 MCFragment *getCurrentFragment() const; 75 insert(MCFragment * F)76 void insert(MCFragment *F) { 77 flushPendingLabels(F); 78 MCSection *CurSection = getCurrentSectionOnly(); 79 CurSection->getFragmentList().insert(CurInsertionPoint, F); 80 F->setParent(CurSection); 81 } 82 83 /// Get a data fragment to write into, creating a new one if the current 84 /// fragment is not a data fragment. 85 /// Optionally a \p STI can be passed in so that a new fragment is created 86 /// if the Subtarget differs from the current fragment. 87 MCDataFragment *getOrCreateDataFragment(const MCSubtargetInfo* STI = nullptr); 88 MCPaddingFragment *getOrCreatePaddingFragment(); 89 90 protected: 91 bool changeSectionImpl(MCSection *Section, const MCExpr *Subsection); 92 93 /// If any labels have been emitted but not assigned fragments, ensure that 94 /// they get assigned, either to F if possible or to a new data fragment. 95 /// Optionally, it is also possible to provide an offset \p FOffset, which 96 /// will be used as a symbol offset within the fragment. 97 void flushPendingLabels(MCFragment *F, uint64_t FOffset = 0); 98 99 public: 100 void visitUsedSymbol(const MCSymbol &Sym) override; 101 102 /// Create a dummy fragment to assign any pending labels. flushPendingLabels()103 void flushPendingLabels() { flushPendingLabels(nullptr); } 104 getAssembler()105 MCAssembler &getAssembler() { return *Assembler; } 106 MCAssembler *getAssemblerPtr() override; 107 /// \name MCStreamer Interface 108 /// @{ 109 110 void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override; 111 virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc, MCFragment *F); 112 void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override; 113 void EmitValueImpl(const MCExpr *Value, unsigned Size, 114 SMLoc Loc = SMLoc()) override; 115 void EmitULEB128Value(const MCExpr *Value) override; 116 void EmitSLEB128Value(const MCExpr *Value) override; 117 void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override; 118 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override; 119 void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, 120 bool = false) override; 121 122 /// Emit an instruction to a special fragment, because this instruction 123 /// can change its size during relaxation. 124 virtual void EmitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &); 125 126 void EmitBundleAlignMode(unsigned AlignPow2) override; 127 void EmitBundleLock(bool AlignToEnd) override; 128 void EmitBundleUnlock() override; 129 void EmitBytes(StringRef Data) override; 130 void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, 131 unsigned ValueSize = 1, 132 unsigned MaxBytesToEmit = 0) override; 133 void EmitCodeAlignment(unsigned ByteAlignment, 134 unsigned MaxBytesToEmit = 0) override; 135 void emitValueToOffset(const MCExpr *Offset, unsigned char Value, 136 SMLoc Loc) override; 137 void 138 EmitCodePaddingBasicBlockStart(const MCCodePaddingContext &Context) override; 139 void 140 EmitCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context) override; 141 void EmitDwarfLocDirective(unsigned FileNo, unsigned Line, 142 unsigned Column, unsigned Flags, 143 unsigned Isa, unsigned Discriminator, 144 StringRef FileName) override; 145 void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel, 146 const MCSymbol *Label, 147 unsigned PointerSize); 148 void EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, 149 const MCSymbol *Label); 150 void EmitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line, 151 unsigned Column, bool PrologueEnd, bool IsStmt, 152 StringRef FileName, SMLoc Loc) override; 153 void EmitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin, 154 const MCSymbol *End) override; 155 void EmitCVInlineLinetableDirective(unsigned PrimaryFunctionId, 156 unsigned SourceFileId, 157 unsigned SourceLineNum, 158 const MCSymbol *FnStartSym, 159 const MCSymbol *FnEndSym) override; 160 void EmitCVDefRangeDirective( 161 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, 162 StringRef FixedSizePortion) override; 163 void EmitCVStringTableDirective() override; 164 void EmitCVFileChecksumsDirective() override; 165 void EmitCVFileChecksumOffsetDirective(unsigned FileNo) override; 166 void EmitDTPRel32Value(const MCExpr *Value) override; 167 void EmitDTPRel64Value(const MCExpr *Value) override; 168 void EmitTPRel32Value(const MCExpr *Value) override; 169 void EmitTPRel64Value(const MCExpr *Value) override; 170 void EmitGPRel32Value(const MCExpr *Value) override; 171 void EmitGPRel64Value(const MCExpr *Value) override; 172 bool EmitRelocDirective(const MCExpr &Offset, StringRef Name, 173 const MCExpr *Expr, SMLoc Loc, 174 const MCSubtargetInfo &STI) override; 175 using MCStreamer::emitFill; 176 void emitFill(const MCExpr &NumBytes, uint64_t FillValue, 177 SMLoc Loc = SMLoc()) override; 178 void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr, 179 SMLoc Loc = SMLoc()) override; 180 void EmitFileDirective(StringRef Filename) override; 181 182 void EmitAddrsig() override; 183 void EmitAddrsigSym(const MCSymbol *Sym) override; 184 185 void FinishImpl() override; 186 187 /// Emit the absolute difference between two symbols if possible. 188 /// 189 /// Emit the absolute difference between \c Hi and \c Lo, as long as we can 190 /// compute it. Currently, that requires that both symbols are in the same 191 /// data fragment and that the target has not specified that diff expressions 192 /// require relocations to be emitted. Otherwise, do nothing and return 193 /// \c false. 194 /// 195 /// \pre Offset of \c Hi is greater than the offset \c Lo. 196 void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, 197 unsigned Size) override; 198 199 void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi, 200 const MCSymbol *Lo) override; 201 202 bool mayHaveInstructions(MCSection &Sec) const override; 203 }; 204 205 } // end namespace llvm 206 207 #endif 208