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/STLExtras.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/ilist.h"
16 #include "llvm/ADT/ilist_node.h"
17 #include "llvm/ADT/iterator.h"
18 #include "llvm/MC/MCDirectives.h"
19 #include "llvm/MC/MCDwarf.h"
20 #include "llvm/MC/MCFixup.h"
21 #include "llvm/MC/MCFragment.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCLinkerOptimizationHint.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/MC/MCSymbol.h"
37 class MCSubtargetInfo;
41 // FIXME: This really doesn't belong here. See comments below.
42 struct IndirectSymbolData {
47 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
49 struct DataRegionData {
50 // This enum should be kept in sync w/ the mach-o definition in
51 // llvm/Object/MachOFormat.h.
52 enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
58 friend class MCAsmLayout;
61 typedef std::vector<MCSection *> SectionListType;
62 typedef std::vector<const MCSymbol *> SymbolDataListType;
64 typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
65 typedef pointee_iterator<SectionListType::iterator> iterator;
67 typedef pointee_iterator<SymbolDataListType::const_iterator>
68 const_symbol_iterator;
69 typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator;
71 typedef iterator_range<symbol_iterator> symbol_range;
72 typedef iterator_range<const_symbol_iterator> const_symbol_range;
74 typedef std::vector<IndirectSymbolData>::const_iterator
75 const_indirect_symbol_iterator;
76 typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
78 typedef std::vector<DataRegionData>::const_iterator
79 const_data_region_iterator;
80 typedef std::vector<DataRegionData>::iterator data_region_iterator;
82 /// MachO specific deployment target version info.
83 // A Major version of 0 indicates that no version information was supplied
84 // and so the corresponding load command should not be emitted.
86 MCVersionMinType Kind;
93 MCAssembler(const MCAssembler &) = delete;
94 void operator=(const MCAssembler &) = delete;
98 MCAsmBackend &Backend;
100 MCCodeEmitter &Emitter;
102 MCObjectWriter &Writer;
104 SectionListType Sections;
106 SymbolDataListType Symbols;
108 std::vector<IndirectSymbolData> IndirectSymbols;
110 std::vector<DataRegionData> DataRegions;
112 /// The list of linker options to propagate into the object file.
113 std::vector<std::vector<std::string>> LinkerOptions;
115 /// List of declared file names
116 std::vector<std::string> FileNames;
118 MCDwarfLineTableParams LTParams;
120 /// The set of function symbols for which a .thumb_func directive has
123 // FIXME: We really would like this in target specific code rather than
124 // here. Maybe when the relocation stuff moves to target specific,
125 // this can go with it? The streamer would need some target specific
127 mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
129 /// \brief The bundle alignment size currently set in the assembler.
131 /// By default it's 0, which means bundling is disabled.
132 unsigned BundleAlignSize;
134 unsigned RelaxAll : 1;
135 unsigned SubsectionsViaSymbols : 1;
136 unsigned IncrementalLinkerCompatible : 1;
138 /// ELF specific e_header flags
139 // It would be good if there were an MCELFAssembler class to hold this.
140 // ELF header flags are used both by the integrated and standalone assemblers.
141 // Access to the flags is necessary in cases where assembler directives affect
142 // which flags to be set.
143 unsigned ELFHeaderEFlags;
145 /// Used to communicate Linker Optimization Hint information between
146 /// the Streamer and the .o writer
147 MCLOHContainer LOHContainer;
149 VersionMinInfoType VersionMinInfo;
152 /// Evaluate a fixup to a relocatable expression and the value which should be
153 /// placed into the fixup.
155 /// \param Layout The layout to use for evaluation.
156 /// \param Fixup The fixup to evaluate.
157 /// \param DF The fragment the fixup is inside.
158 /// \param Target [out] On return, the relocatable expression the fixup
160 /// \param Value [out] On return, the value of the fixup as currently laid
162 /// \return Whether the fixup value was fully resolved. This is true if the
163 /// \p Value result is fixed, otherwise the value may change due to
165 bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
166 const MCFragment *DF, MCValue &Target,
167 uint64_t &Value) const;
169 /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
170 /// (increased in size, in order to hold its value correctly).
171 bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
172 const MCAsmLayout &Layout) const;
174 /// Check whether the given fragment needs relaxation.
175 bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
176 const MCAsmLayout &Layout) const;
178 /// \brief Perform one layout iteration and return true if any offsets
180 bool layoutOnce(MCAsmLayout &Layout);
182 /// \brief Perform one layout iteration of the given section and return true
183 /// if any offsets were adjusted.
184 bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
186 bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
188 bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
190 bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
191 bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
192 MCDwarfCallFrameFragment &DF);
193 bool relaxCVInlineLineTable(MCAsmLayout &Layout,
194 MCCVInlineLineTableFragment &DF);
195 bool relaxCVDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &DF);
197 /// finishLayout - Finalize a layout, including fragment lowering.
198 void finishLayout(MCAsmLayout &Layout);
200 std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
201 MCFragment &F, const MCFixup &Fixup);
204 /// Compute the effective fragment size assuming it is laid out at the given
205 /// \p SectionAddress and \p FragmentOffset.
206 uint64_t computeFragmentSize(const MCAsmLayout &Layout,
207 const MCFragment &F) const;
209 /// Find the symbol which defines the atom containing the given symbol, or
210 /// null if there is no such symbol.
211 const MCSymbol *getAtom(const MCSymbol &S) const;
213 /// Check whether a particular symbol is visible to the linker and is required
214 /// in the symbol table, or whether it can be discarded by the assembler. This
215 /// also effects whether the assembler treats the label as potentially
216 /// defining a separate atom.
217 bool isSymbolLinkerVisible(const MCSymbol &SD) const;
219 /// Emit the section contents using the given object writer.
220 void writeSectionData(const MCSection *Section,
221 const MCAsmLayout &Layout) const;
223 /// Check whether a given symbol has been flagged with .thumb_func.
224 bool isThumbFunc(const MCSymbol *Func) const;
226 /// Flag a function symbol as the target of a .thumb_func directive.
227 void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
229 /// ELF e_header flags
230 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
231 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
233 /// MachO deployment target version information.
234 const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
235 void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
237 VersionMinInfo.Kind = Kind;
238 VersionMinInfo.Major = Major;
239 VersionMinInfo.Minor = Minor;
240 VersionMinInfo.Update = Update;
244 /// Construct a new assembler instance.
246 // FIXME: How are we going to parameterize this? Two obvious options are stay
247 // concrete and require clients to pass in a target like object. The other
248 // option is to make this abstract, and have targets provide concrete
249 // implementations as we do with AsmParser.
250 MCAssembler(MCContext &Context, MCAsmBackend &Backend,
251 MCCodeEmitter &Emitter, MCObjectWriter &Writer);
254 /// Reuse an assembler instance
258 MCContext &getContext() const { return Context; }
260 MCAsmBackend &getBackend() const { return Backend; }
262 MCCodeEmitter &getEmitter() const { return Emitter; }
264 MCObjectWriter &getWriter() const { return Writer; }
266 MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
267 void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
269 /// Finish - Do final processing and write the object to the output stream.
270 /// \p Writer is used for custom object writer (as the MCJIT does),
271 /// if not specified it is automatically created from backend.
274 // Layout all section and prepare them for emission.
275 void layout(MCAsmLayout &Layout);
277 // FIXME: This does not belong here.
278 bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
279 void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
281 bool isIncrementalLinkerCompatible() const {
282 return IncrementalLinkerCompatible;
284 void setIncrementalLinkerCompatible(bool Value) {
285 IncrementalLinkerCompatible = Value;
288 bool getRelaxAll() const { return RelaxAll; }
289 void setRelaxAll(bool Value) { RelaxAll = Value; }
291 bool isBundlingEnabled() const { return BundleAlignSize != 0; }
293 unsigned getBundleAlignSize() const { return BundleAlignSize; }
295 void setBundleAlignSize(unsigned Size) {
296 assert((Size == 0 || !(Size & (Size - 1))) &&
297 "Expect a power-of-two bundle align size");
298 BundleAlignSize = Size;
301 /// \name Section List Access
304 iterator begin() { return Sections.begin(); }
305 const_iterator begin() const { return Sections.begin(); }
307 iterator end() { return Sections.end(); }
308 const_iterator end() const { return Sections.end(); }
310 size_t size() const { return Sections.size(); }
313 /// \name Symbol List Access
315 symbol_iterator symbol_begin() { return Symbols.begin(); }
316 const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
318 symbol_iterator symbol_end() { return Symbols.end(); }
319 const_symbol_iterator symbol_end() const { return Symbols.end(); }
321 symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
322 const_symbol_range symbols() const {
323 return make_range(symbol_begin(), symbol_end());
326 size_t symbol_size() const { return Symbols.size(); }
329 /// \name Indirect Symbol List Access
332 // FIXME: This is a total hack, this should not be here. Once things are
333 // factored so that the streamer has direct access to the .o writer, it can
335 std::vector<IndirectSymbolData> &getIndirectSymbols() {
336 return IndirectSymbols;
339 indirect_symbol_iterator indirect_symbol_begin() {
340 return IndirectSymbols.begin();
342 const_indirect_symbol_iterator indirect_symbol_begin() const {
343 return IndirectSymbols.begin();
346 indirect_symbol_iterator indirect_symbol_end() {
347 return IndirectSymbols.end();
349 const_indirect_symbol_iterator indirect_symbol_end() const {
350 return IndirectSymbols.end();
353 size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
356 /// \name Linker Option List Access
359 std::vector<std::vector<std::string>> &getLinkerOptions() {
360 return LinkerOptions;
364 /// \name Data Region List Access
367 // FIXME: This is a total hack, this should not be here. Once things are
368 // factored so that the streamer has direct access to the .o writer, it can
370 std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
372 data_region_iterator data_region_begin() { return DataRegions.begin(); }
373 const_data_region_iterator data_region_begin() const {
374 return DataRegions.begin();
377 data_region_iterator data_region_end() { return DataRegions.end(); }
378 const_data_region_iterator data_region_end() const {
379 return DataRegions.end();
382 size_t data_region_size() const { return DataRegions.size(); }
385 /// \name Data Region List Access
388 // FIXME: This is a total hack, this should not be here. Once things are
389 // factored so that the streamer has direct access to the .o writer, it can
391 MCLOHContainer &getLOHContainer() { return LOHContainer; }
392 const MCLOHContainer &getLOHContainer() const {
393 return const_cast<MCAssembler *>(this)->getLOHContainer();
396 /// \name Backend Data Access
399 bool registerSection(MCSection &Section);
401 void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
403 ArrayRef<std::string> getFileNames() { return FileNames; }
405 void addFileName(StringRef FileName) {
406 if (!is_contained(FileNames, FileName))
407 FileNames.push_back(FileName);
410 /// \brief Write the necessary bundle padding to the given object writer.
411 /// Expects a fragment \p F containing instructions and its size \p FSize.
412 void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
413 MCObjectWriter *OW) const;
420 /// \brief Compute the amount of padding required before the fragment \p F to
421 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
422 /// its section and \p FSize is the fragment's size.
423 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
424 uint64_t FOffset, uint64_t FSize);
426 } // end namespace llvm