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