7e11b8dc0dd65b1ed5d06afb1067d522e238543b
[oota-llvm.git] / include / llvm / MC / MCAssembler.h
1 //===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_MC_MCASSEMBLER_H
11 #define LLVM_MC_MCASSEMBLER_H
12
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
25 namespace llvm {
26 class raw_ostream;
27 class MCAsmLayout;
28 class MCAssembler;
29 class MCContext;
30 class MCCodeEmitter;
31 class MCExpr;
32 class MCFragment;
33 class MCObjectWriter;
34 class MCSection;
35 class MCSubtargetInfo;
36 class MCValue;
37 class MCAsmBackend;
38
39 // FIXME: This really doesn't belong here. See comments below.
40 struct IndirectSymbolData {
41   MCSymbol *Symbol;
42   MCSection *Section;
43 };
44
45 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
46 // to one another.
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;
51   MCSymbol *Start;
52   MCSymbol *End;
53 };
54
55 class MCAssembler {
56   friend class MCAsmLayout;
57
58 public:
59   typedef std::vector<MCSection *> SectionListType;
60   typedef std::vector<const MCSymbol *> SymbolDataListType;
61
62   typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
63   typedef pointee_iterator<SectionListType::iterator> iterator;
64
65   typedef pointee_iterator<SymbolDataListType::const_iterator>
66   const_symbol_iterator;
67   typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator;
68
69   typedef iterator_range<symbol_iterator> symbol_range;
70   typedef iterator_range<const_symbol_iterator> const_symbol_range;
71
72   typedef std::vector<IndirectSymbolData>::const_iterator
73       const_indirect_symbol_iterator;
74   typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
75
76   typedef std::vector<DataRegionData>::const_iterator
77       const_data_region_iterator;
78   typedef std::vector<DataRegionData>::iterator data_region_iterator;
79
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.
83   typedef struct {
84     MCVersionMinType Kind;
85     unsigned Major;
86     unsigned Minor;
87     unsigned Update;
88   } VersionMinInfoType;
89
90 private:
91   MCAssembler(const MCAssembler &) = delete;
92   void operator=(const MCAssembler &) = delete;
93
94   MCContext &Context;
95
96   MCAsmBackend &Backend;
97
98   MCCodeEmitter &Emitter;
99
100   MCObjectWriter &Writer;
101
102   SectionListType Sections;
103
104   SymbolDataListType Symbols;
105
106   std::vector<IndirectSymbolData> IndirectSymbols;
107
108   std::vector<DataRegionData> DataRegions;
109
110   /// The list of linker options to propagate into the object file.
111   std::vector<std::vector<std::string>> LinkerOptions;
112
113   /// List of declared file names
114   std::vector<std::string> FileNames;
115
116   MCDwarfLineTableParams LTParams;
117
118   /// The set of function symbols for which a .thumb_func directive has
119   /// been seen.
120   //
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
124   // refactoring too.
125   mutable SmallPtrSet<const MCSymbol *, 64> ThumbFuncs;
126
127   /// \brief The bundle alignment size currently set in the assembler.
128   ///
129   /// By default it's 0, which means bundling is disabled.
130   unsigned BundleAlignSize;
131
132   unsigned RelaxAll : 1;
133   unsigned SubsectionsViaSymbols : 1;
134   unsigned IncrementalLinkerCompatible : 1;
135
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;
142
143   /// Used to communicate Linker Optimization Hint information between
144   /// the Streamer and the .o writer
145   MCLOHContainer LOHContainer;
146
147   VersionMinInfoType VersionMinInfo;
148
149 private:
150   /// Evaluate a fixup to a relocatable expression and the value which should be
151   /// placed into the fixup.
152   ///
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
157   /// evaluates to.
158   /// \param Value [out] On return, the value of the fixup as currently laid
159   /// out.
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
162   /// relocation.
163   bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
164                      const MCFragment *DF, MCValue &Target,
165                      uint64_t &Value) const;
166
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;
171
172   /// Check whether the given fragment needs relaxation.
173   bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
174                                const MCAsmLayout &Layout) const;
175
176   /// \brief Perform one layout iteration and return true if any offsets
177   /// were adjusted.
178   bool layoutOnce(MCAsmLayout &Layout);
179
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);
183
184   bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
185
186   bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
187
188   bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
189   bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
190                                    MCDwarfCallFrameFragment &DF);
191
192   /// finishLayout - Finalize a layout, including fragment lowering.
193   void finishLayout(MCAsmLayout &Layout);
194
195   std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
196                                         MCFragment &F, const MCFixup &Fixup);
197
198 public:
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;
203
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;
207
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;
213
214   /// Emit the section contents using the given object writer.
215   void writeSectionData(const MCSection *Section,
216                         const MCAsmLayout &Layout) const;
217
218   /// Check whether a given symbol has been flagged with .thumb_func.
219   bool isThumbFunc(const MCSymbol *Func) const;
220
221   /// Flag a function symbol as the target of a .thumb_func directive.
222   void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
223
224   /// ELF e_header flags
225   unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
226   void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
227
228   /// MachO deployment target version information.
229   const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
230   void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
231                          unsigned Update) {
232     VersionMinInfo.Kind = Kind;
233     VersionMinInfo.Major = Major;
234     VersionMinInfo.Minor = Minor;
235     VersionMinInfo.Update = Update;
236   }
237
238 public:
239   /// Construct a new assembler instance.
240   //
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_);
247   ~MCAssembler();
248
249   /// Reuse an assembler instance
250   ///
251   void reset();
252
253   MCContext &getContext() const { return Context; }
254
255   MCAsmBackend &getBackend() const { return Backend; }
256
257   MCCodeEmitter &getEmitter() const { return Emitter; }
258
259   MCObjectWriter &getWriter() const { return Writer; }
260
261   MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
262   void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
263
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.
267   void Finish();
268
269   // Layout all section and prepare them for emission.
270   void layout(MCAsmLayout &Layout);
271
272   // FIXME: This does not belong here.
273   bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
274   void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
275
276   bool isIncrementalLinkerCompatible() const {
277     return IncrementalLinkerCompatible;
278   }
279   void setIncrementalLinkerCompatible(bool Value) {
280     IncrementalLinkerCompatible = Value;
281   }
282
283   bool getRelaxAll() const { return RelaxAll; }
284   void setRelaxAll(bool Value) { RelaxAll = Value; }
285
286   bool isBundlingEnabled() const { return BundleAlignSize != 0; }
287
288   unsigned getBundleAlignSize() const { return BundleAlignSize; }
289
290   void setBundleAlignSize(unsigned Size) {
291     assert((Size == 0 || !(Size & (Size - 1))) &&
292            "Expect a power-of-two bundle align size");
293     BundleAlignSize = Size;
294   }
295
296   /// \name Section List Access
297   /// @{
298
299   iterator begin() { return Sections.begin(); }
300   const_iterator begin() const { return Sections.begin(); }
301
302   iterator end() { return Sections.end(); }
303   const_iterator end() const { return Sections.end(); }
304
305   size_t size() const { return Sections.size(); }
306
307   /// @}
308   /// \name Symbol List Access
309   /// @{
310   symbol_iterator symbol_begin() { return Symbols.begin(); }
311   const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
312
313   symbol_iterator symbol_end() { return Symbols.end(); }
314   const_symbol_iterator symbol_end() const { return Symbols.end(); }
315
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());
319   }
320
321   size_t symbol_size() const { return Symbols.size(); }
322
323   /// @}
324   /// \name Indirect Symbol List Access
325   /// @{
326
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
329   // disappear.
330   std::vector<IndirectSymbolData> &getIndirectSymbols() {
331     return IndirectSymbols;
332   }
333
334   indirect_symbol_iterator indirect_symbol_begin() {
335     return IndirectSymbols.begin();
336   }
337   const_indirect_symbol_iterator indirect_symbol_begin() const {
338     return IndirectSymbols.begin();
339   }
340
341   indirect_symbol_iterator indirect_symbol_end() {
342     return IndirectSymbols.end();
343   }
344   const_indirect_symbol_iterator indirect_symbol_end() const {
345     return IndirectSymbols.end();
346   }
347
348   size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
349
350   /// @}
351   /// \name Linker Option List Access
352   /// @{
353
354   std::vector<std::vector<std::string>> &getLinkerOptions() {
355     return LinkerOptions;
356   }
357
358   /// @}
359   /// \name Data Region List Access
360   /// @{
361
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
364   // disappear.
365   std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
366
367   data_region_iterator data_region_begin() { return DataRegions.begin(); }
368   const_data_region_iterator data_region_begin() const {
369     return DataRegions.begin();
370   }
371
372   data_region_iterator data_region_end() { return DataRegions.end(); }
373   const_data_region_iterator data_region_end() const {
374     return DataRegions.end();
375   }
376
377   size_t data_region_size() const { return DataRegions.size(); }
378
379   /// @}
380   /// \name Data Region List Access
381   /// @{
382
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
385   // disappear.
386   MCLOHContainer &getLOHContainer() { return LOHContainer; }
387   const MCLOHContainer &getLOHContainer() const {
388     return const_cast<MCAssembler *>(this)->getLOHContainer();
389   }
390   /// @}
391   /// \name Backend Data Access
392   /// @{
393
394   bool registerSection(MCSection &Section);
395
396   void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
397
398   ArrayRef<std::string> getFileNames() { return FileNames; }
399
400   void addFileName(StringRef FileName) {
401     if (std::find(FileNames.begin(), FileNames.end(), FileName) ==
402         FileNames.end())
403       FileNames.push_back(FileName);
404   }
405
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;
410
411   /// @}
412
413   void dump();
414 };
415
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);
421
422 } // end namespace llvm
423
424 #endif