LLVM 19.0.0git
MCObjectStreamer.h
Go to the documentation of this file.
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"
14#include "llvm/MC/MCFixup.h"
15#include "llvm/MC/MCFragment.h"
16#include "llvm/MC/MCSection.h"
17#include "llvm/MC/MCStreamer.h"
18
19namespace llvm {
20class MCContext;
21class MCInst;
22class MCObjectWriter;
23class MCSymbol;
24struct MCDwarfFrameInfo;
25class MCAssembler;
26class MCCodeEmitter;
27class MCSubtargetInfo;
28class MCExpr;
29class MCFragment;
30class MCDataFragment;
31class MCAsmBackend;
32class raw_ostream;
33class raw_pwrite_stream;
34
35/// Streaming object file generation interface.
36///
37/// This class provides an implementation of the MCStreamer interface which is
38/// suitable for use with the assembler backend. Specific object file formats
39/// are expected to subclass this interface to implement directives specific
40/// to that file format or custom semantics expected by the object writer
41/// implementation.
43 std::unique_ptr<MCAssembler> Assembler;
44 bool EmitEHFrame;
45 bool EmitDebugFrame;
46 SmallVector<MCSymbol *, 2> PendingLabels;
47 SmallSetVector<MCSection *, 4> PendingLabelSections;
48 unsigned CurSubsectionIdx = 0;
49 struct PendingMCFixup {
50 const MCSymbol *Sym;
53 PendingMCFixup(const MCSymbol *McSym, MCDataFragment *F, MCFixup McFixup)
54 : Sym(McSym), Fixup(McFixup), DF(F) {}
55 };
57
58 struct PendingAssignment {
59 MCSymbol *Symbol;
60 const MCExpr *Value;
61 };
62
63 /// A list of conditional assignments we may need to emit if the target
64 /// symbol is later emitted.
66 pendingAssignments;
67
68 virtual void emitInstToData(const MCInst &Inst, const MCSubtargetInfo&) = 0;
69 void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
70 void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
71 MCSymbol *emitCFILabel() override;
72 void emitInstructionImpl(const MCInst &Inst, const MCSubtargetInfo &STI);
73 void resolvePendingFixups();
74
75protected:
76 MCObjectStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
77 std::unique_ptr<MCObjectWriter> OW,
78 std::unique_ptr<MCCodeEmitter> Emitter);
80
81public:
82 /// state management
83 void reset() override;
84
85 /// Object streamers require the integrated assembler.
86 bool isIntegratedAssemblerRequired() const override { return true; }
87
88 void emitFrames(MCAsmBackend *MAB);
89 void emitCFISections(bool EH, bool Debug) override;
90
92
95 MCSection *CurSection = getCurrentSectionOnly();
96 CurSection->addFragment(*F);
97 F->setParent(CurSection);
98 }
99
100 /// Get a data fragment to write into, creating a new one if the current
101 /// fragment is not a data fragment.
102 /// Optionally a \p STI can be passed in so that a new fragment is created
103 /// if the Subtarget differs from the current fragment.
105
106protected:
107 bool changeSectionImpl(MCSection *Section, const MCExpr *Subsection);
108
109 /// Assign a label to the current Section and Subsection even though a
110 /// fragment is not yet present. Use flushPendingLabels(F) to associate
111 /// a fragment with this label.
112 void addPendingLabel(MCSymbol* label);
113
114 /// If any labels have been emitted but not assigned fragments in the current
115 /// Section and Subsection, ensure that they get assigned to fragment F.
116 /// Optionally, one can provide an offset \p FOffset as a symbol offset within
117 /// the fragment.
118 void flushPendingLabels(MCFragment *F, uint64_t FOffset = 0);
119
120public:
121 void visitUsedSymbol(const MCSymbol &Sym) override;
122
123 /// Create a data fragment for any pending labels across all Sections
124 /// and Subsections.
125 void flushPendingLabels();
126
127 MCAssembler &getAssembler() { return *Assembler; }
128 MCAssembler *getAssemblerPtr() override;
129 /// \name MCStreamer Interface
130 /// @{
131
132 void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
133 virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment *F,
135 void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
137 const MCExpr *Value) override;
138 void emitValueImpl(const MCExpr *Value, unsigned Size,
139 SMLoc Loc = SMLoc()) override;
140 void emitULEB128Value(const MCExpr *Value) override;
141 void emitSLEB128Value(const MCExpr *Value) override;
142 void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override;
143 void changeSection(MCSection *Section, const MCExpr *Subsection) override;
144 void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
145
146 /// Emit an instruction to a special fragment, because this instruction
147 /// can change its size during relaxation.
148 virtual void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);
149
150 void emitBundleAlignMode(Align Alignment) override;
151 void emitBundleLock(bool AlignToEnd) override;
152 void emitBundleUnlock() override;
153 void emitBytes(StringRef Data) override;
154 void emitValueToAlignment(Align Alignment, int64_t Value = 0,
155 unsigned ValueSize = 1,
156 unsigned MaxBytesToEmit = 0) override;
157 void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo *STI,
158 unsigned MaxBytesToEmit = 0) override;
159 void emitValueToOffset(const MCExpr *Offset, unsigned char Value,
160 SMLoc Loc) override;
161 void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column,
162 unsigned Flags, unsigned Isa,
163 unsigned Discriminator,
164 StringRef FileName) override;
165 void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel,
166 const MCSymbol *Label,
167 unsigned PointerSize) override;
168 void emitDwarfLineEndEntry(MCSection *Section, MCSymbol *LastLabel) override;
169 void emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
170 const MCSymbol *Label, SMLoc Loc);
171 void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line,
172 unsigned Column, bool PrologueEnd, bool IsStmt,
173 StringRef FileName, SMLoc Loc) override;
174 void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin,
175 const MCSymbol *End) override;
176 void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
177 unsigned SourceFileId,
178 unsigned SourceLineNum,
179 const MCSymbol *FnStartSym,
180 const MCSymbol *FnEndSym) override;
182 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
183 StringRef FixedSizePortion) override;
184 void emitCVStringTableDirective() override;
185 void emitCVFileChecksumsDirective() override;
186 void emitCVFileChecksumOffsetDirective(unsigned FileNo) override;
187 void emitDTPRel32Value(const MCExpr *Value) override;
188 void emitDTPRel64Value(const MCExpr *Value) override;
189 void emitTPRel32Value(const MCExpr *Value) override;
190 void emitTPRel64Value(const MCExpr *Value) override;
191 void emitGPRel32Value(const MCExpr *Value) override;
192 void emitGPRel64Value(const MCExpr *Value) override;
193 std::optional<std::pair<bool, std::string>>
195 SMLoc Loc, const MCSubtargetInfo &STI) override;
197 void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
198 SMLoc Loc = SMLoc()) override;
199 void emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
200 SMLoc Loc = SMLoc()) override;
201 void emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc,
202 const MCSubtargetInfo &STI) override;
203 void emitFileDirective(StringRef Filename) override;
204 void emitFileDirective(StringRef Filename, StringRef CompilerVersion,
205 StringRef TimeStamp, StringRef Description) override;
206
207 void emitAddrsig() override;
208 void emitAddrsigSym(const MCSymbol *Sym) override;
209
210 void finishImpl() override;
211
212 /// Emit the absolute difference between two symbols if possible.
213 ///
214 /// Emit the absolute difference between \c Hi and \c Lo, as long as we can
215 /// compute it. Currently, that requires that both symbols are in the same
216 /// data fragment and that the target has not specified that diff expressions
217 /// require relocations to be emitted. Otherwise, do nothing and return
218 /// \c false.
219 ///
220 /// \pre Offset of \c Hi is greater than the offset \c Lo.
221 void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
222 unsigned Size) override;
223
225 const MCSymbol *Lo) override;
226
227 bool mayHaveInstructions(MCSection &Sec) const override;
228
229 /// Emits pending conditional assignments that depend on \p Symbol
230 /// being emitted.
231 void emitPendingAssignments(MCSymbol *Symbol);
232};
233
234} // end namespace llvm
235
236#endif
dxil DXContainer Global Emitter
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
PowerPC TLS Dynamic Call Fixup
bool Debug
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:43
Context object for machine code objects.
Definition: MCContext.h:82
Fragment for data and encoded instructions.
Definition: MCFragment.h:235
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Streaming object file generation interface.
void reset() override
state management
void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc=SMLoc()) override
Emit Size bytes worth of the value specified by FillValue.
void emitDwarfLineEndEntry(MCSection *Section, MCSymbol *LastLabel) override
Emit the debug line end entry.
void emitValueToAlignment(Align Alignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override
Emit an assignment of Value to Symbol.
void emitULEB128Value(const MCExpr *Value) override
void emitSLEB128Value(const MCExpr *Value) override
void emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc, const MCSubtargetInfo &STI) override
MCDataFragment * getOrCreateDataFragment(const MCSubtargetInfo *STI=nullptr)
Get a data fragment to write into, creating a new one if the current fragment is not a data fragment.
void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) override
This implements the CodeView '.cv_inline_linetable' assembler directive.
void emitCVStringTableDirective() override
This implements the CodeView '.cv_stringtable' assembler directive.
void emitBundleAlignMode(Align Alignment) override
Set the bundle alignment mode from now on in the section.
MCAssembler & getAssembler()
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override
Emit an weak reference from Alias to Symbol.
void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel, const MCSymbol *Label, unsigned PointerSize) override
If targets does not support representing debug line section by .loc/.file directives in assembly outp...
void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line, unsigned Column, bool PrologueEnd, bool IsStmt, StringRef FileName, SMLoc Loc) override
This implements the CodeView '.cv_loc' assembler directive.
void emitDTPRel32Value(const MCExpr *Value) override
Emit the expression Value into the output as a dtprel (32-bit DTP relative) value.
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
void emitBundleLock(bool AlignToEnd) override
The following instructions are a bundle-locked group.
void emitPendingAssignments(MCSymbol *Symbol)
Emits pending conditional assignments that depend on Symbol being emitted.
void emitFileDirective(StringRef Filename) override
Switch to a new logical file.
void emitCVDefRangeDirective(ArrayRef< std::pair< const MCSymbol *, const MCSymbol * > > Ranges, StringRef FixedSizePortion) override
This implements the CodeView '.cv_def_range' assembler directive.
void emitGPRel64Value(const MCExpr *Value) override
Emit the expression Value into the output as a gprel64 (64-bit GP relative) value.
MCAssembler * getAssemblerPtr() override
void flushPendingLabels()
Create a data fragment for any pending labels across all Sections and Subsections.
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment *F, uint64_t Offset)
std::optional< std::pair< bool, std::string > > emitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr, SMLoc Loc, const MCSubtargetInfo &STI) override
Record a relocation described by the .reloc directive.
void emitAddrsigSym(const MCSymbol *Sym) override
bool isIntegratedAssemblerRequired() const override
Object streamers require the integrated assembler.
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
void visitUsedSymbol(const MCSymbol &Sym) override
void emitGPRel32Value(const MCExpr *Value) override
Emit the expression Value into the output as a gprel32 (32-bit GP relative) value.
void insert(MCFragment *F)
virtual void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &)
Emit an instruction to a special fragment, because this instruction can change its size during relaxa...
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
void finishImpl() override
Streamer specific finalization.
void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi, const MCSymbol *Lo) override
Emit the absolute difference between two symbols encoded with ULEB128.
void emitBundleUnlock() override
Ends a bundle-locked group.
bool changeSectionImpl(MCSection *Section, const MCExpr *Subsection)
void emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, const MCSymbol *Label, SMLoc Loc)
void emitCFISections(bool EH, bool Debug) override
void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column, unsigned Flags, unsigned Isa, unsigned Discriminator, StringRef FileName) override
This implements the DWARF2 '.loc fileno lineno ...' assembler directive.
void addPendingLabel(MCSymbol *label)
Assign a label to the current Section and Subsection even though a fragment is not yet present.
bool mayHaveInstructions(MCSection &Sec) const override
MCFragment * getCurrentFragment() const
void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo *STI, unsigned MaxBytesToEmit=0) override
Emit nops until the byte alignment ByteAlignment is reached.
void emitFrames(MCAsmBackend *MAB)
void changeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
void emitTPRel64Value(const MCExpr *Value) override
Emit the expression Value into the output as a tprel (64-bit TP relative) value.
void emitCVFileChecksumOffsetDirective(unsigned FileNo) override
This implements the CodeView '.cv_filechecksumoffset' assembler directive.
void emitTPRel32Value(const MCExpr *Value) override
Emit the expression Value into the output as a tprel (32-bit TP relative) value.
void emitConditionalAssignment(MCSymbol *Symbol, const MCExpr *Value) override
Emit an assignment of Value to Symbol, but only if Value is also emitted.
void emitCVFileChecksumsDirective() override
This implements the CodeView '.cv_filechecksums' assembler directive.
void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin, const MCSymbol *End) override
This implements the CodeView '.cv_linetable' assembler directive.
void emitDTPRel64Value(const MCExpr *Value) override
Emit the expression Value into the output as a dtprel (64-bit DTP relative) value.
void emitValueToOffset(const MCExpr *Offset, unsigned char Value, SMLoc Loc) override
Emit some number of copies of Value until the byte offset Offset is reached.
void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) override
Emit the absolute difference between two symbols if possible.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:35
void addFragment(MCFragment &F)
Definition: MCSection.h:195
Streaming machine code generation interface.
Definition: MCStreamer.h:212
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:395
void emitFill(uint64_t NumBytes, uint8_t FillValue)
Emit NumBytes bytes worth of the value specified by FillValue.
Definition: MCStreamer.cpp:221
Generic base class for all target subtargets.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
Represents a location in source code.
Definition: SMLoc.h:23
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:370
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
LLVM Value Representation.
Definition: Value.h:74
This class represents a function that is read from a sample profile.
Definition: FunctionId.h:36
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39