LLVM  6.0.0svn
MCAssembler.h
Go to the documentation of this file.
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/ArrayRef.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/iterator.h"
19 #include "llvm/MC/MCDirectives.h"
20 #include "llvm/MC/MCDwarf.h"
21 #include "llvm/MC/MCFixup.h"
22 #include "llvm/MC/MCFragment.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include <cassert>
26 #include <cstddef>
27 #include <cstdint>
28 #include <string>
29 #include <utility>
30 #include <vector>
31 
32 namespace llvm {
33 
34 class MCAsmBackend;
35 class MCAsmLayout;
36 class MCContext;
37 class MCCodeEmitter;
38 class MCFragment;
39 class MCObjectWriter;
40 class MCSection;
41 class MCValue;
42 
43 // FIXME: This really doesn't belong here. See comments below.
47 };
48 
49 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
50 // to one another.
52  // This enum should be kept in sync w/ the mach-o definition in
53  // llvm/Object/MachOFormat.h.
54  enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
57 };
58 
59 class MCAssembler {
60  friend class MCAsmLayout;
61 
62 public:
63  using SectionListType = std::vector<MCSection *>;
64  using SymbolDataListType = std::vector<const MCSymbol *>;
65 
68 
69  using const_symbol_iterator =
72 
75 
77  std::vector<IndirectSymbolData>::const_iterator;
78  using indirect_symbol_iterator = std::vector<IndirectSymbolData>::iterator;
79 
81  std::vector<DataRegionData>::const_iterator;
82  using data_region_iterator = std::vector<DataRegionData>::iterator;
83 
84  /// MachO specific deployment target version info.
85  // A Major version of 0 indicates that no version information was supplied
86  // and so the corresponding load command should not be emitted.
87  using VersionMinInfoType = struct {
89  unsigned Major;
90  unsigned Minor;
91  unsigned Update;
92  };
93 
94 private:
96 
97  MCAsmBackend &Backend;
98 
99  MCCodeEmitter &Emitter;
100 
101  MCObjectWriter &Writer;
102 
103  SectionListType Sections;
104 
105  SymbolDataListType Symbols;
106 
107  std::vector<IndirectSymbolData> IndirectSymbols;
108 
109  std::vector<DataRegionData> DataRegions;
110 
111  /// The list of linker options to propagate into the object file.
112  std::vector<std::vector<std::string>> LinkerOptions;
113 
114  /// List of declared file names
115  std::vector<std::string> FileNames;
116 
117  MCDwarfLineTableParams LTParams;
118 
119  /// The set of function symbols for which a .thumb_func directive has
120  /// been seen.
121  //
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
125  // refactoring too.
126  mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
127 
128  /// \brief The bundle alignment size currently set in the assembler.
129  ///
130  /// By default it's 0, which means bundling is disabled.
131  unsigned BundleAlignSize;
132 
133  bool RelaxAll : 1;
134  bool SubsectionsViaSymbols : 1;
135  bool IncrementalLinkerCompatible : 1;
136 
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;
143 
144  /// Used to communicate Linker Optimization Hint information between
145  /// the Streamer and the .o writer
146  MCLOHContainer LOHContainer;
147 
148  VersionMinInfoType VersionMinInfo;
149 
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 relaxPaddingFragment(MCAsmLayout &Layout, MCPaddingFragment &PF);
187 
188  bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
189 
190  bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
191  bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
193  bool relaxCVInlineLineTable(MCAsmLayout &Layout,
195  bool relaxCVDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &DF);
196 
197  /// finishLayout - Finalize a layout, including fragment lowering.
198  void finishLayout(MCAsmLayout &Layout);
199 
200  std::tuple<MCValue, uint64_t, bool>
201  handleFixup(const MCAsmLayout &Layout, MCFragment &F, const MCFixup &Fixup);
202 
203 public:
204  /// Construct a new assembler instance.
205  //
206  // FIXME: How are we going to parameterize this? Two obvious options are stay
207  // concrete and require clients to pass in a target like object. The other
208  // option is to make this abstract, and have targets provide concrete
209  // implementations as we do with AsmParser.
210  MCAssembler(MCContext &Context, MCAsmBackend &Backend,
211  MCCodeEmitter &Emitter, MCObjectWriter &Writer);
212  MCAssembler(const MCAssembler &) = delete;
213  MCAssembler &operator=(const MCAssembler &) = delete;
214  ~MCAssembler();
215 
216  /// Compute the effective fragment size assuming it is laid out at the given
217  /// \p SectionAddress and \p FragmentOffset.
218  uint64_t computeFragmentSize(const MCAsmLayout &Layout,
219  const MCFragment &F) const;
220 
221  /// Find the symbol which defines the atom containing the given symbol, or
222  /// null if there is no such symbol.
223  const MCSymbol *getAtom(const MCSymbol &S) const;
224 
225  /// Check whether a particular symbol is visible to the linker and is required
226  /// in the symbol table, or whether it can be discarded by the assembler. This
227  /// also effects whether the assembler treats the label as potentially
228  /// defining a separate atom.
229  bool isSymbolLinkerVisible(const MCSymbol &SD) const;
230 
231  /// Emit the section contents using the given object writer.
232  void writeSectionData(const MCSection *Section,
233  const MCAsmLayout &Layout) const;
234 
235  /// Check whether a given symbol has been flagged with .thumb_func.
236  bool isThumbFunc(const MCSymbol *Func) const;
237 
238  /// Flag a function symbol as the target of a .thumb_func directive.
239  void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
240 
241  /// ELF e_header flags
242  unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
243  void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
244 
245  /// MachO deployment target version information.
246  const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
247  void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
248  unsigned Update) {
249  VersionMinInfo.Kind = Kind;
250  VersionMinInfo.Major = Major;
251  VersionMinInfo.Minor = Minor;
252  VersionMinInfo.Update = Update;
253  }
254 
255  /// Reuse an assembler instance
256  ///
257  void reset();
258 
259  MCContext &getContext() const { return Context; }
260 
261  MCAsmBackend &getBackend() const { return Backend; }
262 
263  MCCodeEmitter &getEmitter() const { return Emitter; }
264 
265  MCObjectWriter &getWriter() const { return Writer; }
266 
267  MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
269 
270  /// Finish - Do final processing and write the object to the output stream.
271  /// \p Writer is used for custom object writer (as the MCJIT does),
272  /// if not specified it is automatically created from backend.
273  void Finish();
274 
275  // Layout all section and prepare them for emission.
276  void layout(MCAsmLayout &Layout);
277 
278  // FIXME: This does not belong here.
279  bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
280  void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
281 
283  return IncrementalLinkerCompatible;
284  }
286  IncrementalLinkerCompatible = Value;
287  }
288 
289  bool getRelaxAll() const { return RelaxAll; }
290  void setRelaxAll(bool Value) { RelaxAll = Value; }
291 
292  bool isBundlingEnabled() const { return BundleAlignSize != 0; }
293 
294  unsigned getBundleAlignSize() const { return BundleAlignSize; }
295 
296  void setBundleAlignSize(unsigned Size) {
297  assert((Size == 0 || !(Size & (Size - 1))) &&
298  "Expect a power-of-two bundle align size");
299  BundleAlignSize = Size;
300  }
301 
302  /// \name Section List Access
303  /// @{
304 
305  iterator begin() { return Sections.begin(); }
306  const_iterator begin() const { return Sections.begin(); }
307 
308  iterator end() { return Sections.end(); }
309  const_iterator end() const { return Sections.end(); }
310 
311  size_t size() const { return Sections.size(); }
312 
313  /// @}
314  /// \name Symbol List Access
315  /// @{
316  symbol_iterator symbol_begin() { return Symbols.begin(); }
317  const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
318 
319  symbol_iterator symbol_end() { return Symbols.end(); }
320  const_symbol_iterator symbol_end() const { return Symbols.end(); }
321 
322  symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
324  return make_range(symbol_begin(), symbol_end());
325  }
326 
327  size_t symbol_size() const { return Symbols.size(); }
328 
329  /// @}
330  /// \name Indirect Symbol List Access
331  /// @{
332 
333  // FIXME: This is a total hack, this should not be here. Once things are
334  // factored so that the streamer has direct access to the .o writer, it can
335  // disappear.
336  std::vector<IndirectSymbolData> &getIndirectSymbols() {
337  return IndirectSymbols;
338  }
339 
341  return IndirectSymbols.begin();
342  }
344  return IndirectSymbols.begin();
345  }
346 
348  return IndirectSymbols.end();
349  }
351  return IndirectSymbols.end();
352  }
353 
354  size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
355 
356  /// @}
357  /// \name Linker Option List Access
358  /// @{
359 
360  std::vector<std::vector<std::string>> &getLinkerOptions() {
361  return LinkerOptions;
362  }
363 
364  /// @}
365  /// \name Data Region List Access
366  /// @{
367 
368  // FIXME: This is a total hack, this should not be here. Once things are
369  // factored so that the streamer has direct access to the .o writer, it can
370  // disappear.
371  std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
372 
373  data_region_iterator data_region_begin() { return DataRegions.begin(); }
375  return DataRegions.begin();
376  }
377 
378  data_region_iterator data_region_end() { return DataRegions.end(); }
380  return DataRegions.end();
381  }
382 
383  size_t data_region_size() const { return DataRegions.size(); }
384 
385  /// @}
386  /// \name Data Region List Access
387  /// @{
388 
389  // FIXME: This is a total hack, this should not be here. Once things are
390  // factored so that the streamer has direct access to the .o writer, it can
391  // disappear.
392  MCLOHContainer &getLOHContainer() { return LOHContainer; }
394  return const_cast<MCAssembler *>(this)->getLOHContainer();
395  }
396  /// @}
397  /// \name Backend Data Access
398  /// @{
399 
400  bool registerSection(MCSection &Section);
401 
402  void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
403 
404  ArrayRef<std::string> getFileNames() { return FileNames; }
405 
406  void addFileName(StringRef FileName) {
407  if (!is_contained(FileNames, FileName))
408  FileNames.push_back(FileName);
409  }
410 
411  /// \brief Write the necessary bundle padding to the given object writer.
412  /// Expects a fragment \p F containing instructions and its size \p FSize.
413  void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
414  MCObjectWriter *OW) const;
415 
416  /// @}
417 
418  void dump() const;
419 };
420 
421 /// \brief Compute the amount of padding required before the fragment \p F to
422 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
423 /// its section and \p FSize is the fragment's size.
424 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
425  uint64_t FOffset, uint64_t FSize);
426 
427 } // end namespace llvm
428 
429 #endif // LLVM_MC_MCASSEMBLER_H
void setIsThumbFunc(const MCSymbol *Func)
Flag a function symbol as the target of a .thumb_func directive.
Definition: MCAssembler.h:239
void setELFHeaderEFlags(unsigned Flags)
Definition: MCAssembler.h:243
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
Fragment for adding required padding.
Definition: MCFragment.h:333
LLVMContext & Context
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor, unsigned Update)
Definition: MCAssembler.h:247
This represents an "assembler immediate".
Definition: MCValue.h:40
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
iterator begin()
Definition: MCAssembler.h:305
const_symbol_iterator symbol_end() const
Definition: MCAssembler.h:320
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
size_t symbol_size() const
Definition: MCAssembler.h:327
F(f)
std::vector< IndirectSymbolData > & getIndirectSymbols()
Definition: MCAssembler.h:336
const_data_region_iterator data_region_begin() const
Definition: MCAssembler.h:374
const_iterator end() const
Definition: MCAssembler.h:309
bool isBundlingEnabled() const
Definition: MCAssembler.h:292
Defines the object file and target independent interfaces used by the assembler backend to write nati...
symbol_iterator symbol_begin()
Definition: MCAssembler.h:316
std::vector< DataRegionData >::iterator data_region_iterator
Definition: MCAssembler.h:82
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
unsigned getBundleAlignSize() const
Definition: MCAssembler.h:294
const_indirect_symbol_iterator indirect_symbol_end() const
Definition: MCAssembler.h:350
MCContext & getContext() const
Definition: MCAssembler.h:259
MCDwarfLineTableParams getDWARFLinetableParams() const
Definition: MCAssembler.h:267
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:263
void setRelaxAll(bool Value)
Definition: MCAssembler.h:290
const MCLOHContainer & getLOHContainer() const
Definition: MCAssembler.h:393
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:265
struct { MCVersionMinType Kind VersionMinInfoType
MachO specific deployment target version info.
Definition: MCAssembler.h:88
void setSubsectionsViaSymbols(bool Value)
Definition: MCAssembler.h:280
ArrayRef< std::string > getFileNames()
Definition: MCAssembler.h:404
Context object for machine code objects.
Definition: MCContext.h:59
void setDWARFLinetableParams(MCDwarfLineTableParams P)
Definition: MCAssembler.h:268
const_symbol_iterator symbol_begin() const
Definition: MCAssembler.h:317
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void setBundleAlignSize(unsigned Size)
Definition: MCAssembler.h:296
iterator end()
Definition: MCAssembler.h:308
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:257
#define P(N)
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:371
std::vector< DataRegionData >::const_iterator const_data_region_iterator
Definition: MCAssembler.h:81
std::vector< IndirectSymbolData >::const_iterator const_indirect_symbol_iterator
Definition: MCAssembler.h:77
data_region_iterator data_region_begin()
Definition: MCAssembler.h:373
unsigned getELFHeaderEFlags() const
ELF e_header flags.
Definition: MCAssembler.h:242
std::vector< const MCSymbol * > SymbolDataListType
Definition: MCAssembler.h:64
Fragment representing the .cv_def_range directive.
Definition: MCFragment.h:627
size_t data_region_size() const
Definition: MCAssembler.h:383
uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F, uint64_t FOffset, uint64_t FSize)
Compute the amount of padding required before the fragment F to obey bundling restrictions, where FOffset is the fragment&#39;s offset in its section and FSize is the fragment&#39;s size.
Definition: MCFragment.cpp:190
static bool isThumbFunc(symbol_iterator Symbol, const ObjectFile &Obj, section_iterator Section)
const_indirect_symbol_iterator indirect_symbol_begin() const
Definition: MCAssembler.h:343
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
std::vector< std::vector< std::string > > & getLinkerOptions()
Definition: MCAssembler.h:360
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
indirect_symbol_iterator indirect_symbol_begin()
Definition: MCAssembler.h:340
void setIncrementalLinkerCompatible(bool Value)
Definition: MCAssembler.h:285
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:261
An iterator type that allows iterating over the pointees via some other iterator. ...
Definition: iterator.h:289
bool getSubsectionsViaSymbols() const
Definition: MCAssembler.h:279
size_t indirect_symbol_size() const
Definition: MCAssembler.h:354
MCLOHContainer & getLOHContainer()
Definition: MCAssembler.h:392
void addFileName(StringRef FileName)
Definition: MCAssembler.h:406
bool getRelaxAll() const
Definition: MCAssembler.h:289
A range adaptor for a pair of iterators.
Target - Wrapper for Target specific information.
const_iterator begin() const
Definition: MCAssembler.h:306
Fragment representing the binary annotations produced by the .cv_inline_linetable directive...
Definition: MCFragment.h:589
const VersionMinInfoType & getVersionMinInfo() const
MachO deployment target version information.
Definition: MCAssembler.h:246
const_data_region_iterator data_region_end() const
Definition: MCAssembler.h:379
symbol_range symbols()
Definition: MCAssembler.h:322
data_region_iterator data_region_end()
Definition: MCAssembler.h:378
size_t size() const
Definition: MCAssembler.h:311
std::vector< IndirectSymbolData >::iterator indirect_symbol_iterator
Definition: MCAssembler.h:78
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isIncrementalLinkerCompatible() const
Definition: MCAssembler.h:282
MCVersionMinType
Definition: MCDirectives.h:64
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:40
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
indirect_symbol_iterator indirect_symbol_end()
Definition: MCAssembler.h:347
const_symbol_range symbols() const
Definition: MCAssembler.h:323
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
std::vector< DataRegionData > & getDataRegions()
Definition: MCAssembler.h:371
std::vector< MCSection * > SectionListType
Definition: MCAssembler.h:63
symbol_iterator symbol_end()
Definition: MCAssembler.h:319
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:867