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