LLVM 22.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 <algorithm>
23#include <cassert>
24#include <cstddef>
25#include <cstdint>
26#include <memory>
27#include <string>
28#include <tuple>
29#include <utility>
30#include <vector>
31
32namespace llvm {
33
34class MCBoundaryAlignFragment;
35class MCCVDefRangeFragment;
36class MCCVInlineLineTableFragment;
37class MCFragment;
38class MCFixup;
39class MCSymbolRefExpr;
40class raw_ostream;
41class MCAsmBackend;
42class MCContext;
43class MCCodeEmitter;
44class MCFragment;
45class MCObjectWriter;
46class MCSection;
47class MCValue;
48
50public:
51 friend class MCObjectWriter;
54
55private:
56 MCContext &Context;
57
58 std::unique_ptr<MCAsmBackend> Backend;
59 std::unique_ptr<MCCodeEmitter> Emitter;
60 std::unique_ptr<MCObjectWriter> Writer;
61
62 bool HasLayout = false;
63 bool HasFinalLayout = false;
64 bool RelaxAll = false;
65
66 SectionListType Sections;
67
69
70 struct RelocDirective {
71 const MCExpr &Offset;
72 const MCExpr *Expr;
73 uint32_t Kind;
74 };
75 SmallVector<RelocDirective, 0> relocDirectives;
76
77 mutable SmallVector<std::pair<SMLoc, std::string>, 0> PendingErrors;
78
80
81 /// The set of function symbols for which a .thumb_func directive has
82 /// been seen.
83 //
84 // FIXME: We really would like this in target specific code rather than
85 // here. Maybe when the relocation stuff moves to target specific,
86 // this can go with it? The streamer would need some target specific
87 // refactoring too.
88 mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
89
90 /// Evaluate a fixup to a relocatable expression and the value which should be
91 /// placed into the fixup.
92 ///
93 /// \param F The fragment the fixup is inside.
94 /// \param Fixup The fixup to evaluate.
95 /// \param Target [out] On return, the relocatable expression the fixup
96 /// evaluates to.
97 /// \param Value [out] On return, the value of the fixup as currently laid
98 /// out.
99 /// \param RecordReloc Record relocation if needed.
100 /// relocation.
101 bool evaluateFixup(const MCFragment &F, MCFixup &Fixup, MCValue &Target,
102 uint64_t &Value, bool RecordReloc, uint8_t *Data) const;
103
104 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
105 /// (increased in size, in order to hold its value correctly).
106 bool fixupNeedsRelaxation(const MCFragment &, const MCFixup &) const;
107
108 void layoutSection(MCSection &Sec);
109 /// Perform one layout iteration and return the index of the first stable
110 /// section for subsequent optimization.
111 unsigned relaxOnce(unsigned FirstStable);
112
113 /// Perform relaxation on a single fragment.
114 bool relaxFragment(MCFragment &F);
115 void relaxInstruction(MCFragment &F);
116 void relaxLEB(MCFragment &F);
117 void relaxBoundaryAlign(MCBoundaryAlignFragment &BF);
118 void relaxDwarfLineAddr(MCFragment &F);
119 void relaxDwarfCallFrameFragment(MCFragment &F);
120
121public:
122 /// Construct a new assembler instance.
123 //
124 // FIXME: How are we going to parameterize this? Two obvious options are stay
125 // concrete and require clients to pass in a target like object. The other
126 // option is to make this abstract, and have targets provide concrete
127 // implementations as we do with AsmParser.
129 std::unique_ptr<MCAsmBackend> Backend,
130 std::unique_ptr<MCCodeEmitter> Emitter,
131 std::unique_ptr<MCObjectWriter> Writer);
132 MCAssembler(const MCAssembler &) = delete;
134
135 /// Compute the effective fragment size.
137
138 // Get the offset of the given fragment inside its containing section.
139 uint64_t getFragmentOffset(const MCFragment &F) const { return F.Offset; }
140
143
144 // Get the offset of the given symbol, as computed in the current
145 // layout.
146 // \return True on success.
147 LLVM_ABI bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const;
148
149 // Variant that reports a fatal error if the offset is not computable.
151
152 // If this symbol is equivalent to A + Constant, return A.
153 LLVM_ABI const MCSymbol *getBaseSymbol(const MCSymbol &Symbol) const;
154
155 /// Emit the section contents to \p OS.
157 const MCSection *Section) const;
158
159 /// Check whether a given symbol has been flagged with .thumb_func.
160 LLVM_ABI bool isThumbFunc(const MCSymbol *Func) const;
161
162 /// Flag a function symbol as the target of a .thumb_func directive.
163 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
164
165 /// Reuse an assembler instance
166 ///
167 LLVM_ABI void reset();
168
169 MCContext &getContext() const { return Context; }
170
171 MCAsmBackend *getBackendPtr() const { return Backend.get(); }
172
173 MCCodeEmitter *getEmitterPtr() const { return Emitter.get(); }
174
175 MCAsmBackend &getBackend() const { return *Backend; }
176
177 MCCodeEmitter &getEmitter() const { return *Emitter; }
178
179 MCObjectWriter &getWriter() const { return *Writer; }
180
182
183 /// Finish - Do final processing and write the object to the output stream.
184 /// \p Writer is used for custom object writer (as the MCJIT does),
185 /// if not specified it is automatically created from backend.
186 LLVM_ABI void Finish();
187
188 // Layout all section and prepare them for emission.
189 LLVM_ABI void layout();
190
191 bool hasLayout() const { return HasLayout; }
192 bool hasFinalLayout() const { return HasFinalLayout; }
193 bool getRelaxAll() const { return RelaxAll; }
194 void setRelaxAll(bool Value) { RelaxAll = Value; }
195
196 const_iterator begin() const { return Sections.begin(); }
197 const_iterator end() const { return Sections.end(); }
198
202 symbols() const {
203 return make_pointee_range(Symbols);
204 }
205
206 LLVM_ABI bool registerSection(MCSection &Section);
207 LLVM_ABI bool registerSymbol(const MCSymbol &Symbol);
208 LLVM_ABI void addRelocDirective(RelocDirective RD);
209
210 LLVM_ABI void reportError(SMLoc L, const Twine &Msg) const;
211 // Record pending errors during layout iteration, as they may go away once the
212 // layout is finalized.
213 LLVM_ABI void recordError(SMLoc L, const Twine &Msg) const;
214 LLVM_ABI void flushPendingErrors() const;
215
216 LLVM_ABI void dump() const;
217};
218
219} // end namespace llvm
220
221#endif // LLVM_MC_MCASSEMBLER_H
#define LLVM_ABI
Definition: Compiler.h:213
dxil DXContainer Global Emitter
#define F(x, y, z)
Definition: MD5.cpp:55
PowerPC TLS Dynamic Call Fixup
raw_pwrite_stream & OS
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:55
const_iterator begin() const
Definition: MCAssembler.h:196
MCContext & getContext() const
Definition: MCAssembler.h:169
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.
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.
Definition: MCAssembler.h:163
LLVM_ABI void writeSectionData(raw_ostream &OS, const MCSection *Section) const
Emit the section contents to OS.
LLVM_ABI void dump() const
LLVM_ABI void layout()
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:179
bool hasFinalLayout() const
Definition: MCAssembler.h:192
MCCodeEmitter * getEmitterPtr() const
Definition: MCAssembler.h:173
LLVM_ABI void addRelocDirective(RelocDirective RD)
SmallVectorImpl< const MCSymbol * > & getSymbols()
Definition: MCAssembler.h:199
bool getRelaxAll() const
Definition: MCAssembler.h:193
MCAssembler(const MCAssembler &)=delete
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:177
LLVM_ABI void recordError(SMLoc L, const Twine &Msg) const
LLVM_ABI bool isThumbFunc(const MCSymbol *Func) const
Check whether a given symbol has been flagged with .thumb_func.
const_iterator end() const
Definition: MCAssembler.h:197
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:175
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
MCAsmBackend * getBackendPtr() const
Definition: MCAssembler.h:171
iterator_range< pointee_iterator< typename SmallVector< const MCSymbol *, 0 >::const_iterator > > symbols() const
Definition: MCAssembler.h:202
LLVM_ABI uint64_t getSectionFileSize(const MCSection &Sec) const
LLVM_ABI void reset()
Reuse an assembler instance.
Definition: MCAssembler.cpp:90
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
bool hasLayout() const
Definition: MCAssembler.h:191
uint64_t getFragmentOffset(const MCFragment &F) const
Definition: MCAssembler.h:139
MCDwarfLineTableParams getDWARFLinetableParams() const
Definition: MCAssembler.h:181
void setRelaxAll(bool Value)
Definition: MCAssembler.h:194
Represents required padding such that a particular other set of fragments does not cross a particular...
Definition: MCSection.h:459
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:23
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:496
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:23
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:401
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
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: AddressRanges.h:18
iterator_range< pointee_iterator< WrappedIteratorT > > make_pointee_range(RangeT &&Range)
Definition: iterator.h:336
An iterator type that allows iterating over the pointees via some other iterator.
Definition: iterator.h:324