LLVM 23.0.0git
MCAssembler.h
Go to the documentation of this file.
1//===- MCAssembler.h - Object File Generation -------------------*- 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_MCASSEMBLER_H
10#define LLVM_MC_MCASSEMBLER_H
11
12#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/iterator.h"
18#include "llvm/MC/MCDwarf.h"
19#include "llvm/MC/MCSymbol.h"
21#include "llvm/Support/SMLoc.h"
22#include <cassert>
23#include <cstddef>
24#include <cstdint>
25#include <memory>
26#include <string>
27#include <utility>
28
29namespace llvm {
30
34class MCFragment;
35class MCFixup;
36class MCSymbolRefExpr;
37class raw_ostream;
38class MCAsmBackend;
39class MCContext;
40class MCCodeEmitter;
41class MCFragment;
42class MCObjectWriter;
43class MCSection;
44class MCValue;
45
47public:
48 friend class MCObjectWriter;
51
52private:
53 MCContext &Context;
54
55 std::unique_ptr<MCAsmBackend> Backend;
56 std::unique_ptr<MCCodeEmitter> Emitter;
57 std::unique_ptr<MCObjectWriter> Writer;
58
59 bool HasLayout = false;
60 bool HasFinalLayout = false;
61 bool RelaxAll = false;
62
63 // Cumulative upstream size change during `relaxOnce`. Used to compensate
64 // forward-reference displacements in `evaluateFixup`.
65 int64_t Stretch = 0;
66
67 SectionListType Sections;
68
70
71 struct RelocDirective {
72 const MCExpr &Offset;
73 const MCExpr *Expr;
74 uint32_t Kind;
75 };
76 SmallVector<RelocDirective, 0> relocDirectives;
77
78 mutable SmallVector<std::pair<SMLoc, std::string>, 0> PendingErrors;
79
81
82 /// The set of function symbols for which a .thumb_func directive has
83 /// been seen.
84 //
85 // FIXME: We really would like this in target specific code rather than
86 // here. Maybe when the relocation stuff moves to target specific,
87 // this can go with it? The streamer would need some target specific
88 // refactoring too.
89 mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
90
91 /// Evaluate a fixup to a relocatable expression and the value which should be
92 /// placed into the fixup.
93 ///
94 /// \param F The fragment the fixup is inside.
95 /// \param Fixup The fixup to evaluate.
96 /// \param Target [out] On return, the relocatable expression the fixup
97 /// evaluates to.
98 /// \param Value [out] On return, the value of the fixup as currently laid
99 /// out.
100 /// \param RecordReloc Record relocation if needed.
101 /// relocation.
102 bool evaluateFixup(const MCFragment &F, MCFixup &Fixup, MCValue &Target,
103 uint64_t &Value, bool RecordReloc, uint8_t *Data) const;
104
105 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
106 /// (increased in size, in order to hold its value correctly).
107 bool fixupNeedsRelaxation(const MCFragment &, const MCFixup &) const;
108
109 void layoutSection(MCSection &Sec);
110 /// Perform one layout iteration and return the index of the first stable
111 /// section for subsequent optimization.
112 unsigned relaxOnce(unsigned FirstStable);
113
114 /// Perform relaxation on a single fragment.
115 void relaxFragment(MCFragment &F);
116 void relaxAlign(MCFragment &F);
117 void relaxPrefAlign(MCFragment &F);
118 void relaxInstruction(MCFragment &F);
119 void relaxLEB(MCFragment &F);
120 void relaxBoundaryAlign(MCBoundaryAlignFragment &BF);
121 void relaxDwarfLineAddr(MCFragment &F);
122 void relaxDwarfCallFrameFragment(MCFragment &F);
123 void relaxSFrameFragment(MCFragment &DF);
124
125public:
126 /// Construct a new assembler instance.
127 //
128 // FIXME: How are we going to parameterize this? Two obvious options are stay
129 // concrete and require clients to pass in a target like object. The other
130 // option is to make this abstract, and have targets provide concrete
131 // implementations as we do with AsmParser.
133 std::unique_ptr<MCAsmBackend> Backend,
134 std::unique_ptr<MCCodeEmitter> Emitter,
135 std::unique_ptr<MCObjectWriter> Writer);
136 MCAssembler(const MCAssembler &) = delete;
138
139 /// Compute the effective fragment size.
141
142 // Get the offset of the given fragment inside its containing section.
143 uint64_t getFragmentOffset(const MCFragment &F) const { return F.Offset; }
144
147
148 // Get the offset of the given symbol, as computed in the current
149 // layout.
150 // \return True on success.
151 LLVM_ABI bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const;
152
153 // Variant that reports a fatal error if the offset is not computable.
155
156 // If this symbol is equivalent to A + Constant, return A.
157 LLVM_ABI const MCSymbol *getBaseSymbol(const MCSymbol &Symbol) const;
158
159 /// Emit the section contents to \p OS.
161 const MCSection *Section) const;
162
163 /// Check whether a given symbol has been flagged with .thumb_func.
164 LLVM_ABI bool isThumbFunc(const MCSymbol *Func) const;
165
166 /// Flag a function symbol as the target of a .thumb_func directive.
167 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
168
169 /// Reuse an assembler instance
170 ///
171 LLVM_ABI void reset();
172
173 MCContext &getContext() const { return Context; }
174
175 MCAsmBackend *getBackendPtr() const { return Backend.get(); }
176
177 MCCodeEmitter *getEmitterPtr() const { return Emitter.get(); }
178
179 MCAsmBackend &getBackend() const { return *Backend; }
180
181 MCCodeEmitter &getEmitter() const { return *Emitter; }
182
183 MCObjectWriter &getWriter() const { return *Writer; }
184
186
187 /// Finish - Do final processing and write the object to the output stream.
188 /// \p Writer is used for custom object writer (as the MCJIT does),
189 /// if not specified it is automatically created from backend.
190 LLVM_ABI void Finish();
191
192 // Layout all section and prepare them for emission.
193 LLVM_ABI void layout();
194
195 bool hasLayout() const { return HasLayout; }
196 bool hasFinalLayout() const { return HasFinalLayout; }
197 bool getRelaxAll() const { return RelaxAll; }
198 void setRelaxAll(bool Value) { RelaxAll = Value; }
199 int64_t getStretch() const { return Stretch; }
200
201 const_iterator begin() const { return Sections.begin(); }
202 const_iterator end() const { return Sections.end(); }
203
207 symbols() const {
208 return make_pointee_range(Symbols);
209 }
210
211 LLVM_ABI bool registerSection(MCSection &Section);
212 LLVM_ABI bool registerSymbol(const MCSymbol &Symbol);
213 LLVM_ABI void addRelocDirective(RelocDirective RD);
214
215 LLVM_ABI void reportError(SMLoc L, const Twine &Msg) const;
216 // Record pending errors during layout iteration, as they may go away once the
217 // layout is finalized.
218 LLVM_ABI void recordError(SMLoc L, const Twine &Msg) const;
219 LLVM_ABI void flushPendingErrors() const;
220
221 LLVM_ABI void dump() const;
222};
223
224} // end namespace llvm
225
226#endif // LLVM_MC_MCASSEMBLER_H
#define LLVM_ABI
Definition Compiler.h:213
dxil DXContainer Global Emitter
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
#define F(x, y, z)
Definition MD5.cpp:54
PowerPC TLS Dynamic Call Fixup
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
Generic interface to target specific assembler backends.
const_iterator begin() const
MCContext & getContext() const
LLVM_ABI bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
LLVM_ABI uint64_t getSectionAddressSize(const MCSection &Sec) const
MCAssembler & operator=(const MCAssembler &)=delete
LLVM_ABI void Finish()
Finish - Do final processing and write the object to the output stream.
SmallVector< MCSection *, 0 > SectionListType
Definition MCAssembler.h:49
LLVM_ABI void reportError(SMLoc L, const Twine &Msg) const
void setIsThumbFunc(const MCSymbol *Func)
Flag a function symbol as the target of a .thumb_func directive.
LLVM_ABI void writeSectionData(raw_ostream &OS, const MCSection *Section) const
Emit the section contents to OS.
iterator_range< pointee_iterator< SmallVector< const MCSymbol *, 0 >::const_iterator > > symbols() const
int64_t getStretch() const
LLVM_ABI void dump() const
LLVM_ABI void layout()
MCObjectWriter & getWriter() const
bool hasFinalLayout() const
MCCodeEmitter * getEmitterPtr() const
LLVM_ABI void addRelocDirective(RelocDirective RD)
SmallVectorImpl< const MCSymbol * > & getSymbols()
bool getRelaxAll() const
MCAssembler(const MCAssembler &)=delete
MCCodeEmitter & getEmitter() const
LLVM_ABI void recordError(SMLoc L, const Twine &Msg) const
LLVM_ABI MCAssembler(MCContext &Context, std::unique_ptr< MCAsmBackend > Backend, std::unique_ptr< MCCodeEmitter > Emitter, std::unique_ptr< MCObjectWriter > Writer)
Construct a new assembler instance.
LLVM_ABI bool isThumbFunc(const MCSymbol *Func) const
Check whether a given symbol has been flagged with .thumb_func.
const_iterator end() const
MCAsmBackend & getBackend() const
LLVM_ABI bool registerSection(MCSection &Section)
LLVM_ABI void flushPendingErrors() const
LLVM_ABI uint64_t computeFragmentSize(const MCFragment &F) const
Compute the effective fragment size.
LLVM_ABI const MCSymbol * getBaseSymbol(const MCSymbol &Symbol) const
pointee_iterator< SectionListType::const_iterator > const_iterator
Definition MCAssembler.h:50
MCAsmBackend * getBackendPtr() const
LLVM_ABI uint64_t getSectionFileSize(const MCSection &Sec) const
friend class MCObjectWriter
Definition MCAssembler.h:48
LLVM_ABI void reset()
Reuse an assembler instance.
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
bool hasLayout() const
uint64_t getFragmentOffset(const MCFragment &F) const
MCDwarfLineTableParams getDWARFLinetableParams() const
void setRelaxAll(bool Value)
Represents required padding such that a particular other set of fragments does not cross a particular...
Definition MCSection.h:535
Fragment representing the .cv_def_range directive.
Definition MCSection.h:506
Fragment representing the binary annotations produced by the .cv_inline_linetable directive.
Definition MCSection.h:478
MCCodeEmitter - Generic instruction encoding interface.
Context object for machine code objects.
Definition MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:61
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:569
Represent a reference to a symbol from inside an expression.
Definition MCExpr.h:190
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Represents a location in source code.
Definition SMLoc.h:22
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM Value Representation.
Definition Value.h:75
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
iterator_range< pointee_iterator< WrappedIteratorT > > make_pointee_range(RangeT &&Range)
Definition iterator.h:341
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
An iterator type that allows iterating over the pointees via some other iterator.
Definition iterator.h:329