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"
35 class MCSubtargetInfo;
39 // FIXME: This really doesn't belong here. See comments below.
40 struct IndirectSymbolData {
45 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
47 struct DataRegionData {
48 // This enum should be kept in sync w/ the mach-o definition in
49 // llvm/Object/MachOFormat.h.
50 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
56 friend class MCAsmLayout;
59 typedef std::vector<MCSection *> SectionListType;
60 typedef std::vector<const MCSymbol *> SymbolDataListType;
62 typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
63 typedef pointee_iterator<SectionListType::iterator> iterator;
65 typedef pointee_iterator<SymbolDataListType::const_iterator>
66 const_symbol_iterator;
67 typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator;
69 typedef iterator_range<symbol_iterator> symbol_range;
70 typedef iterator_range<const_symbol_iterator> const_symbol_range;
72 typedef std::vector<IndirectSymbolData>::const_iterator
73 const_indirect_symbol_iterator;
74 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
76 typedef std::vector<DataRegionData>::const_iterator
77 const_data_region_iterator;
78 typedef std::vector<DataRegionData>::iterator data_region_iterator;
80 /// MachO specific deployment target version info.
81 // A Major version of 0 indicates that no version information was supplied
82 // and so the corresponding load command should not be emitted.
84 MCVersionMinType Kind;
91 MCAssembler(const MCAssembler &) = delete;
92 void operator=(const MCAssembler &) = delete;
96 MCAsmBackend &Backend;
98 MCCodeEmitter &Emitter;
100 MCObjectWriter &Writer;
102 SectionListType Sections;
104 SymbolDataListType Symbols;
106 std::vector<IndirectSymbolData> IndirectSymbols;
108 std::vector<DataRegionData> DataRegions;
110 /// The list of linker options to propagate into the object file.
111 std::vector<std::vector<std::string>> LinkerOptions;
113 /// List of declared file names
114 std::vector<std::string> FileNames;
116 MCDwarfLineTableParams LTParams;
118 /// The set of function symbols for which a .thumb_func directive has
121 // FIXME: We really would like this in target specific code rather than
122 // here. Maybe when the relocation stuff moves to target specific,
123 // this can go with it? The streamer would need some target specific
125 mutable SmallPtrSet<const MCSymbol *, 64> ThumbFuncs;
127 /// \brief The bundle alignment size currently set in the assembler.
129 /// By default it's 0, which means bundling is disabled.
130 unsigned BundleAlignSize;
132 unsigned RelaxAll : 1;
133 unsigned SubsectionsViaSymbols : 1;
134 unsigned IncrementalLinkerCompatible : 1;
136 /// ELF specific e_header flags
137 // It would be good if there were an MCELFAssembler class to hold this.
138 // ELF header flags are used both by the integrated and standalone assemblers.
139 // Access to the flags is necessary in cases where assembler directives affect
140 // which flags to be set.
141 unsigned ELFHeaderEFlags;
143 /// Used to communicate Linker Optimization Hint information between
144 /// the Streamer and the .o writer
145 MCLOHContainer LOHContainer;
147 VersionMinInfoType VersionMinInfo;
150 /// Evaluate a fixup to a relocatable expression and the value which should be
151 /// placed into the fixup.
153 /// \param Layout The layout to use for evaluation.
154 /// \param Fixup The fixup to evaluate.
155 /// \param DF The fragment the fixup is inside.
156 /// \param Target [out] On return, the relocatable expression the fixup
158 /// \param Value [out] On return, the value of the fixup as currently laid
160 /// \return Whether the fixup value was fully resolved. This is true if the
161 /// \p Value result is fixed, otherwise the value may change due to
163 bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
164 const MCFragment *DF, MCValue &Target,
165 uint64_t &Value) const;
167 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
168 /// (increased in size, in order to hold its value correctly).
169 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
170 const MCAsmLayout &Layout) const;
172 /// Check whether the given fragment needs relaxation.
173 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
174 const MCAsmLayout &Layout) const;
176 /// \brief Perform one layout iteration and return true if any offsets
178 bool layoutOnce(MCAsmLayout &Layout);
180 /// \brief Perform one layout iteration of the given section and return true
181 /// if any offsets were adjusted.
182 bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
184 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
186 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
188 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
189 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
190 MCDwarfCallFrameFragment &DF);
192 /// finishLayout - Finalize a layout, including fragment lowering.
193 void finishLayout(MCAsmLayout &Layout);
195 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
196 MCFragment &F, const MCFixup &Fixup);
199 /// Compute the effective fragment size assuming it is laid out at the given
200 /// \p SectionAddress and \p FragmentOffset.
201 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
202 const MCFragment &F) const;
204 /// Find the symbol which defines the atom containing the given symbol, or
205 /// null if there is no such symbol.
206 const MCSymbol *getAtom(const MCSymbol &S) const;
208 /// Check whether a particular symbol is visible to the linker and is required
209 /// in the symbol table, or whether it can be discarded by the assembler. This
210 /// also effects whether the assembler treats the label as potentially
211 /// defining a separate atom.
212 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
214 /// Emit the section contents using the given object writer.
215 void writeSectionData(const MCSection *Section,
216 const MCAsmLayout &Layout) const;
218 /// Check whether a given symbol has been flagged with .thumb_func.
219 bool isThumbFunc(const MCSymbol *Func) const;
221 /// Flag a function symbol as the target of a .thumb_func directive.
222 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
224 /// ELF e_header flags
225 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
226 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
228 /// MachO deployment target version information.
229 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
230 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
232 VersionMinInfo.Kind = Kind;
233 VersionMinInfo.Major = Major;
234 VersionMinInfo.Minor = Minor;
235 VersionMinInfo.Update = Update;
239 /// Construct a new assembler instance.
241 // FIXME: How are we going to parameterize this? Two obvious options are stay
242 // concrete and require clients to pass in a target like object. The other
243 // option is to make this abstract, and have targets provide concrete
244 // implementations as we do with AsmParser.
245 MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
246 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_);
249 /// Reuse an assembler instance
253 MCContext &getContext() const { return Context; }
255 MCAsmBackend &getBackend() const { return Backend; }
257 MCCodeEmitter &getEmitter() const { return Emitter; }
259 MCObjectWriter &getWriter() const { return Writer; }
261 MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
262 void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
264 /// Finish - Do final processing and write the object to the output stream.
265 /// \p Writer is used for custom object writer (as the MCJIT does),
266 /// if not specified it is automatically created from backend.
269 // Layout all section and prepare them for emission.
270 void layout(MCAsmLayout &Layout);
272 // FIXME: This does not belong here.
273 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
274 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
276 bool isIncrementalLinkerCompatible() const {
277 return IncrementalLinkerCompatible;
279 void setIncrementalLinkerCompatible(bool Value) {
280 IncrementalLinkerCompatible = Value;
283 bool getRelaxAll() const { return RelaxAll; }
284 void setRelaxAll(bool Value) { RelaxAll = Value; }
286 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
288 unsigned getBundleAlignSize() const { return BundleAlignSize; }
290 void setBundleAlignSize(unsigned Size) {
291 assert((Size == 0 || !(Size & (Size - 1))) &&
292 "Expect a power-of-two bundle align size");
293 BundleAlignSize = Size;
296 /// \name Section List Access
299 iterator begin() { return Sections.begin(); }
300 const_iterator begin() const { return Sections.begin(); }
302 iterator end() { return Sections.end(); }
303 const_iterator end() const { return Sections.end(); }
305 size_t size() const { return Sections.size(); }
308 /// \name Symbol List Access
310 symbol_iterator symbol_begin() { return Symbols.begin(); }
311 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
313 symbol_iterator symbol_end() { return Symbols.end(); }
314 const_symbol_iterator symbol_end() const { return Symbols.end(); }
316 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
317 const_symbol_range symbols() const {
318 return make_range(symbol_begin(), symbol_end());
321 size_t symbol_size() const { return Symbols.size(); }
324 /// \name Indirect Symbol List Access
327 // FIXME: This is a total hack, this should not be here. Once things are
328 // factored so that the streamer has direct access to the .o writer, it can
330 std::vector<IndirectSymbolData> &getIndirectSymbols() {
331 return IndirectSymbols;
334 indirect_symbol_iterator indirect_symbol_begin() {
335 return IndirectSymbols.begin();
337 const_indirect_symbol_iterator indirect_symbol_begin() const {
338 return IndirectSymbols.begin();
341 indirect_symbol_iterator indirect_symbol_end() {
342 return IndirectSymbols.end();
344 const_indirect_symbol_iterator indirect_symbol_end() const {
345 return IndirectSymbols.end();
348 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
351 /// \name Linker Option List Access
354 std::vector<std::vector<std::string>> &getLinkerOptions() {
355 return LinkerOptions;
359 /// \name Data Region List Access
362 // FIXME: This is a total hack, this should not be here. Once things are
363 // factored so that the streamer has direct access to the .o writer, it can
365 std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
367 data_region_iterator data_region_begin() { return DataRegions.begin(); }
368 const_data_region_iterator data_region_begin() const {
369 return DataRegions.begin();
372 data_region_iterator data_region_end() { return DataRegions.end(); }
373 const_data_region_iterator data_region_end() const {
374 return DataRegions.end();
377 size_t data_region_size() const { return DataRegions.size(); }
380 /// \name Data Region List Access
383 // FIXME: This is a total hack, this should not be here. Once things are
384 // factored so that the streamer has direct access to the .o writer, it can
386 MCLOHContainer &getLOHContainer() { return LOHContainer; }
387 const MCLOHContainer &getLOHContainer() const {
388 return const_cast<MCAssembler *>(this)->getLOHContainer();
391 /// \name Backend Data Access
394 bool registerSection(MCSection &Section);
396 void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
398 ArrayRef<std::string> getFileNames() { return FileNames; }
400 void addFileName(StringRef FileName) {
401 if (std::find(FileNames.begin(), FileNames.end(), FileName) ==
403 FileNames.push_back(FileName);
406 /// \brief Write the necessary bundle padding to the given object writer.
407 /// Expects a fragment \p F containing instructions and its size \p FSize.
408 void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
409 MCObjectWriter *OW) const;
416 /// \brief Compute the amount of padding required before the fragment \p F to
417 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
418 /// its section and \p FSize is the fragment's size.
419 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
420 uint64_t FOffset, uint64_t FSize);
422 } // end namespace llvm