1 //===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCASSEMBLER_H
11 #define LLVM_MC_MCASSEMBLER_H
13 #include "llvm/ADT/SmallPtrSet.h"
14 #include "llvm/ADT/ilist.h"
15 #include "llvm/ADT/ilist_node.h"
16 #include "llvm/ADT/iterator.h"
17 #include "llvm/MC/MCDirectives.h"
18 #include "llvm/MC/MCDwarf.h"
19 #include "llvm/MC/MCFixup.h"
20 #include "llvm/MC/MCFragment.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCLinkerOptimizationHint.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/MC/MCSymbol.h"
36 class MCSubtargetInfo;
40 // FIXME: This really doesn't belong here. See comments below.
41 struct IndirectSymbolData {
46 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
48 struct DataRegionData {
49 // This enum should be kept in sync w/ the mach-o definition in
50 // llvm/Object/MachOFormat.h.
51 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
57 friend class MCAsmLayout;
60 typedef std::vector<MCSection *> SectionListType;
61 typedef std::vector<const MCSymbol *> SymbolDataListType;
63 typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
64 typedef pointee_iterator<SectionListType::iterator> iterator;
66 typedef pointee_iterator<SymbolDataListType::const_iterator>
67 const_symbol_iterator;
68 typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator;
70 typedef iterator_range<symbol_iterator> symbol_range;
71 typedef iterator_range<const_symbol_iterator> const_symbol_range;
73 typedef std::vector<IndirectSymbolData>::const_iterator
74 const_indirect_symbol_iterator;
75 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
77 typedef std::vector<DataRegionData>::const_iterator
78 const_data_region_iterator;
79 typedef std::vector<DataRegionData>::iterator data_region_iterator;
81 /// MachO specific deployment target version info.
82 // A Major version of 0 indicates that no version information was supplied
83 // and so the corresponding load command should not be emitted.
85 MCVersionMinType Kind;
92 MCAssembler(const MCAssembler &) = delete;
93 void operator=(const MCAssembler &) = delete;
97 MCAsmBackend &Backend;
99 MCCodeEmitter &Emitter;
101 MCObjectWriter &Writer;
103 SectionListType Sections;
105 SymbolDataListType Symbols;
107 std::vector<IndirectSymbolData> IndirectSymbols;
109 std::vector<DataRegionData> DataRegions;
111 /// The list of linker options to propagate into the object file.
112 std::vector<std::vector<std::string>> LinkerOptions;
114 /// List of declared file names
115 std::vector<std::string> FileNames;
117 MCDwarfLineTableParams LTParams;
119 /// The set of function symbols for which a .thumb_func directive has
122 // FIXME: We really would like this in target specific code rather than
123 // here. Maybe when the relocation stuff moves to target specific,
124 // this can go with it? The streamer would need some target specific
126 mutable SmallPtrSet<const MCSymbol *, 64> ThumbFuncs;
128 /// \brief The bundle alignment size currently set in the assembler.
130 /// By default it's 0, which means bundling is disabled.
131 unsigned BundleAlignSize;
133 unsigned RelaxAll : 1;
134 unsigned SubsectionsViaSymbols : 1;
135 unsigned IncrementalLinkerCompatible : 1;
137 /// ELF specific e_header flags
138 // It would be good if there were an MCELFAssembler class to hold this.
139 // ELF header flags are used both by the integrated and standalone assemblers.
140 // Access to the flags is necessary in cases where assembler directives affect
141 // which flags to be set.
142 unsigned ELFHeaderEFlags;
144 /// Used to communicate Linker Optimization Hint information between
145 /// the Streamer and the .o writer
146 MCLOHContainer LOHContainer;
148 VersionMinInfoType VersionMinInfo;
151 /// Evaluate a fixup to a relocatable expression and the value which should be
152 /// placed into the fixup.
154 /// \param Layout The layout to use for evaluation.
155 /// \param Fixup The fixup to evaluate.
156 /// \param DF The fragment the fixup is inside.
157 /// \param Target [out] On return, the relocatable expression the fixup
159 /// \param Value [out] On return, the value of the fixup as currently laid
161 /// \return Whether the fixup value was fully resolved. This is true if the
162 /// \p Value result is fixed, otherwise the value may change due to
164 bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
165 const MCFragment *DF, MCValue &Target,
166 uint64_t &Value) const;
168 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
169 /// (increased in size, in order to hold its value correctly).
170 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
171 const MCAsmLayout &Layout) const;
173 /// Check whether the given fragment needs relaxation.
174 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
175 const MCAsmLayout &Layout) const;
177 /// \brief Perform one layout iteration and return true if any offsets
179 bool layoutOnce(MCAsmLayout &Layout);
181 /// \brief Perform one layout iteration of the given section and return true
182 /// if any offsets were adjusted.
183 bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
185 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
187 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
189 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
190 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
191 MCDwarfCallFrameFragment &DF);
193 /// finishLayout - Finalize a layout, including fragment lowering.
194 void finishLayout(MCAsmLayout &Layout);
196 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
197 MCFragment &F, const MCFixup &Fixup);
200 /// Compute the effective fragment size assuming it is laid out at the given
201 /// \p SectionAddress and \p FragmentOffset.
202 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
203 const MCFragment &F) const;
205 /// Find the symbol which defines the atom containing the given symbol, or
206 /// null if there is no such symbol.
207 const MCSymbol *getAtom(const MCSymbol &S) const;
209 /// Check whether a particular symbol is visible to the linker and is required
210 /// in the symbol table, or whether it can be discarded by the assembler. This
211 /// also effects whether the assembler treats the label as potentially
212 /// defining a separate atom.
213 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
215 /// Emit the section contents using the given object writer.
216 void writeSectionData(const MCSection *Section,
217 const MCAsmLayout &Layout) const;
219 /// Check whether a given symbol has been flagged with .thumb_func.
220 bool isThumbFunc(const MCSymbol *Func) const;
222 /// Flag a function symbol as the target of a .thumb_func directive.
223 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
225 /// ELF e_header flags
226 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
227 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
229 /// MachO deployment target version information.
230 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
231 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
233 VersionMinInfo.Kind = Kind;
234 VersionMinInfo.Major = Major;
235 VersionMinInfo.Minor = Minor;
236 VersionMinInfo.Update = Update;
240 /// Construct a new assembler instance.
242 // FIXME: How are we going to parameterize this? Two obvious options are stay
243 // concrete and require clients to pass in a target like object. The other
244 // option is to make this abstract, and have targets provide concrete
245 // implementations as we do with AsmParser.
246 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
247 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_);
250 /// Reuse an assembler instance
254 MCContext &getContext() const { return Context; }
256 MCAsmBackend &getBackend() const { return Backend; }
258 MCCodeEmitter &getEmitter() const { return Emitter; }
260 MCObjectWriter &getWriter() const { return Writer; }
262 MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
263 void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
265 /// Finish - Do final processing and write the object to the output stream.
266 /// \p Writer is used for custom object writer (as the MCJIT does),
267 /// if not specified it is automatically created from backend.
270 // Layout all section and prepare them for emission.
271 void layout(MCAsmLayout &Layout);
273 // FIXME: This does not belong here.
274 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
275 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
277 bool isIncrementalLinkerCompatible() const {
278 return IncrementalLinkerCompatible;
280 void setIncrementalLinkerCompatible(bool Value) {
281 IncrementalLinkerCompatible = Value;
284 bool getRelaxAll() const { return RelaxAll; }
285 void setRelaxAll(bool Value) { RelaxAll = Value; }
287 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
289 unsigned getBundleAlignSize() const { return BundleAlignSize; }
291 void setBundleAlignSize(unsigned Size) {
292 assert((Size == 0 || !(Size & (Size - 1))) &&
293 "Expect a power-of-two bundle align size");
294 BundleAlignSize = Size;
297 /// \name Section List Access
300 iterator begin() { return Sections.begin(); }
301 const_iterator begin() const { return Sections.begin(); }
303 iterator end() { return Sections.end(); }
304 const_iterator end() const { return Sections.end(); }
306 size_t size() const { return Sections.size(); }
309 /// \name Symbol List Access
311 symbol_iterator symbol_begin() { return Symbols.begin(); }
312 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
314 symbol_iterator symbol_end() { return Symbols.end(); }
315 const_symbol_iterator symbol_end() const { return Symbols.end(); }
317 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
318 const_symbol_range symbols() const {
319 return make_range(symbol_begin(), symbol_end());
322 size_t symbol_size() const { return Symbols.size(); }
325 /// \name Indirect Symbol List Access
328 // FIXME: This is a total hack, this should not be here. Once things are
329 // factored so that the streamer has direct access to the .o writer, it can
331 std::vector<IndirectSymbolData> &getIndirectSymbols() {
332 return IndirectSymbols;
335 indirect_symbol_iterator indirect_symbol_begin() {
336 return IndirectSymbols.begin();
338 const_indirect_symbol_iterator indirect_symbol_begin() const {
339 return IndirectSymbols.begin();
342 indirect_symbol_iterator indirect_symbol_end() {
343 return IndirectSymbols.end();
345 const_indirect_symbol_iterator indirect_symbol_end() const {
346 return IndirectSymbols.end();
349 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
352 /// \name Linker Option List Access
355 std::vector<std::vector<std::string>> &getLinkerOptions() {
356 return LinkerOptions;
360 /// \name Data Region List Access
363 // FIXME: This is a total hack, this should not be here. Once things are
364 // factored so that the streamer has direct access to the .o writer, it can
366 std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
368 data_region_iterator data_region_begin() { return DataRegions.begin(); }
369 const_data_region_iterator data_region_begin() const {
370 return DataRegions.begin();
373 data_region_iterator data_region_end() { return DataRegions.end(); }
374 const_data_region_iterator data_region_end() const {
375 return DataRegions.end();
378 size_t data_region_size() const { return DataRegions.size(); }
381 /// \name Data Region List Access
384 // FIXME: This is a total hack, this should not be here. Once things are
385 // factored so that the streamer has direct access to the .o writer, it can
387 MCLOHContainer &getLOHContainer() { return LOHContainer; }
388 const MCLOHContainer &getLOHContainer() const {
389 return const_cast<MCAssembler *>(this)->getLOHContainer();
392 /// \name Backend Data Access
395 bool registerSection(MCSection &Section);
397 void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
399 ArrayRef<std::string> getFileNames() { return FileNames; }
401 void addFileName(StringRef FileName) {
402 if (std::find(FileNames.begin(), FileNames.end(), FileName) ==
404 FileNames.push_back(FileName);
407 /// \brief Write the necessary bundle padding to the given object writer.
408 /// Expects a fragment \p F containing instructions and its size \p FSize.
409 void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
410 MCObjectWriter *OW) const;
417 /// \brief Compute the amount of padding required before the fragment \p F to
418 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
419 /// its section and \p FSize is the fragment's size.
420 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
421 uint64_t FOffset, uint64_t FSize);
423 } // end namespace llvm