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 relaxInstruction(MCFragment &F);
118 void relaxLEB(MCFragment &F);
119 void relaxBoundaryAlign(MCBoundaryAlignFragment &BF);
120 void relaxDwarfLineAddr(MCFragment &F);
121 void relaxDwarfCallFrameFragment(MCFragment &F);
122 void relaxSFrameFragment(MCFragment &DF);
123
124public:
125 /// Construct a new assembler instance.
126 //
127 // FIXME: How are we going to parameterize this? Two obvious options are stay
128 // concrete and require clients to pass in a target like object. The other
129 // option is to make this abstract, and have targets provide concrete
130 // implementations as we do with AsmParser.
132 std::unique_ptr<MCAsmBackend> Backend,
133 std::unique_ptr<MCCodeEmitter> Emitter,
134 std::unique_ptr<MCObjectWriter> Writer);
135 MCAssembler(const MCAssembler &) = delete;
137
138 /// Compute the effective fragment size.
140
141 // Get the offset of the given fragment inside its containing section.
142 uint64_t getFragmentOffset(const MCFragment &F) const { return F.Offset; }
143
146
147 // Get the offset of the given symbol, as computed in the current
148 // layout.
149 // \return True on success.
150 LLVM_ABI bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const;
151
152 // Variant that reports a fatal error if the offset is not computable.
154
155 // If this symbol is equivalent to A + Constant, return A.
156 LLVM_ABI const MCSymbol *getBaseSymbol(const MCSymbol &Symbol) const;
157
158 /// Emit the section contents to \p OS.
160 const MCSection *Section) const;
161
162 /// Check whether a given symbol has been flagged with .thumb_func.
163 LLVM_ABI bool isThumbFunc(const MCSymbol *Func) const;
164
165 /// Flag a function symbol as the target of a .thumb_func directive.
166 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
167
168 /// Reuse an assembler instance
169 ///
170 LLVM_ABI void reset();
171
172 MCContext &getContext() const { return Context; }
173
174 MCAsmBackend *getBackendPtr() const { return Backend.get(); }
175
176 MCCodeEmitter *getEmitterPtr() const { return Emitter.get(); }
177
178 MCAsmBackend &getBackend() const { return *Backend; }
179
180 MCCodeEmitter &getEmitter() const { return *Emitter; }
181
182 MCObjectWriter &getWriter() const { return *Writer; }
183
185
186 /// Finish - Do final processing and write the object to the output stream.
187 /// \p Writer is used for custom object writer (as the MCJIT does),
188 /// if not specified it is automatically created from backend.
189 LLVM_ABI void Finish();
190
191 // Layout all section and prepare them for emission.
192 LLVM_ABI void layout();
193
194 bool hasLayout() const { return HasLayout; }
195 bool hasFinalLayout() const { return HasFinalLayout; }
196 bool getRelaxAll() const { return RelaxAll; }
197 void setRelaxAll(bool Value) { RelaxAll = Value; }
198
199 const_iterator begin() const { return Sections.begin(); }
200 const_iterator end() const { return Sections.end(); }
201
205 symbols() const {
206 return make_pointee_range(Symbols);
207 }
208
209 LLVM_ABI bool registerSection(MCSection &Section);
210 LLVM_ABI bool registerSymbol(const MCSymbol &Symbol);
211 LLVM_ABI void addRelocDirective(RelocDirective RD);
212
213 LLVM_ABI void reportError(SMLoc L, const Twine &Msg) const;
214 // Record pending errors during layout iteration, as they may go away once the
215 // layout is finalized.
216 LLVM_ABI void recordError(SMLoc L, const Twine &Msg) const;
217 LLVM_ABI void flushPendingErrors() const;
218
219 LLVM_ABI void dump() const;
220};
221
222} // end namespace llvm
223
224#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
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:482
Fragment representing the .cv_def_range directive.
Definition MCSection.h:453
Fragment representing the binary annotations produced by the .cv_inline_linetable directive.
Definition MCSection.h:425
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:516
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.
Definition Types.h:26
@ 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:189
An iterator type that allows iterating over the pointees via some other iterator.
Definition iterator.h:329