LLVM  4.0.0
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/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"
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.
45 };
46 
47 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
48 // to one another.
50  // This enum should be kept in sync w/ the mach-o definition in
51  // llvm/Object/MachOFormat.h.
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 
66 
70 
73 
74  typedef std::vector<IndirectSymbolData>::const_iterator
76  typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
77 
78  typedef std::vector<DataRegionData>::const_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 {
87  unsigned Major;
88  unsigned Minor;
89  unsigned Update;
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,
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::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; }
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 
282  return IncrementalLinkerCompatible;
283  }
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 
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 
340  return IndirectSymbols.begin();
341  }
343  return IndirectSymbols.begin();
344  }
345 
347  return IndirectSymbols.end();
348  }
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(); }
374  return DataRegions.begin();
375  }
376 
377  data_region_iterator data_region_end() { return DataRegions.end(); }
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; }
393  return const_cast<MCAssembler *>(this)->getLOHContainer();
394  }
395  /// @}
396  /// \name Backend Data Access
397  /// @{
398 
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
void setIsThumbFunc(const MCSymbol *Func)
Flag a function symbol as the target of a .thumb_func directive.
Definition: MCAssembler.h:227
size_t indirect_symbol_size() const
Definition: MCAssembler.h:353
void setELFHeaderEFlags(unsigned Flags)
Definition: MCAssembler.h:231
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:40
std::vector< DataRegionData >::iterator data_region_iterator
Definition: MCAssembler.h:80
const MCLOHContainer & getLOHContainer() const
Definition: MCAssembler.h:392
void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor, unsigned Update)
Definition: MCAssembler.h:235
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:39
iterator begin()
Definition: MCAssembler.h:304
pointee_iterator< SectionListType::const_iterator > const_iterator
Definition: MCAssembler.h:64
std::vector< MCSection * > SectionListType
Definition: MCAssembler.h:61
unsigned getBundleAlignSize() const
Definition: MCAssembler.h:293
const VersionMinInfoType & getVersionMinInfo() const
MachO deployment target version information.
Definition: MCAssembler.h:234
void writeFragmentPadding(const MCFragment &F, uint64_t FSize, MCObjectWriter *OW) const
Write the necessary bundle padding to the given object writer.
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:262
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
std::vector< IndirectSymbolData > & getIndirectSymbols()
Definition: MCAssembler.h:335
unsigned getELFHeaderEFlags() const
ELF e_header flags.
Definition: MCAssembler.h:230
MCContext & getContext() const
Definition: MCAssembler.h:258
Defines the object file and target independent interfaces used by the assembler backend to write nati...
symbol_iterator symbol_begin()
Definition: MCAssembler.h:315
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
enum llvm::DataRegionData::KindTy Kind
struct fuzzer::@269 Flags
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
std::vector< DataRegionData >::const_iterator const_data_region_iterator
Definition: MCAssembler.h:79
void setRelaxAll(bool Value)
Definition: MCAssembler.h:289
bool registerSection(MCSection &Section)
void setSubsectionsViaSymbols(bool Value)
Definition: MCAssembler.h:279
ArrayRef< std::string > getFileNames()
Definition: MCAssembler.h:403
Context object for machine code objects.
Definition: MCContext.h:51
void setDWARFLinetableParams(MCDwarfLineTableParams P)
Definition: MCAssembler.h:267
#define F(x, y, z)
Definition: MD5.cpp:51
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:264
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:295
bool isThumbFunc(const MCSymbol *Func) const
Check whether a given symbol has been flagged with .thumb_func.
const MCSymbol * getAtom(const MCSymbol &S) const
Find the symbol which defines the atom containing the given symbol, or null if there is no such symbo...
std::vector< IndirectSymbolData >::const_iterator const_indirect_symbol_iterator
Definition: MCAssembler.h:75
iterator end()
Definition: MCAssembler.h:307
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:249
#define P(N)
const_data_region_iterator data_region_begin() const
Definition: MCAssembler.h:373
MCDwarfLineTableParams getDWARFLinetableParams() const
Definition: MCAssembler.h:266
void writeSectionData(const MCSection *Section, const MCAsmLayout &Layout) const
Emit the section contents using the given object writer.
const_symbol_iterator symbol_end() const
Definition: MCAssembler.h:319
uint64_t computeFragmentSize(const MCAsmLayout &Layout, const MCFragment &F) const
Compute the effective fragment size assuming it is laid out at the given SectionAddress and FragmentO...
const_iterator end() const
Definition: MCAssembler.h:308
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:23
bool getRelaxAll() const
Definition: MCAssembler.h:288
pointee_iterator< SymbolDataListType::iterator > symbol_iterator
Definition: MCAssembler.h:69
size_t size() const
Definition: MCAssembler.h:310
const_symbol_iterator symbol_begin() const
Definition: MCAssembler.h:316
size_t symbol_size() const
Definition: MCAssembler.h:326
data_region_iterator data_region_begin()
Definition: MCAssembler.h:372
iterator_range< symbol_iterator > symbol_range
Definition: MCAssembler.h:71
Fragment representing the .cv_def_range directive.
Definition: MCFragment.h:532
void layout(MCAsmLayout &Layout)
const_indirect_symbol_iterator indirect_symbol_end() const
Definition: MCAssembler.h:349
bool isSymbolLinkerVisible(const MCSymbol &SD) const
Check whether a particular symbol is visible to the linker and is required in the symbol table...
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's offset in its section and FSize is the fragment's size.
Definition: MCFragment.cpp:192
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:359
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:425
indirect_symbol_iterator indirect_symbol_begin()
Definition: MCAssembler.h:339
void setIncrementalLinkerCompatible(bool Value)
Definition: MCAssembler.h:284
bool isBundlingEnabled() const
Definition: MCAssembler.h:291
An iterator type that allows iterating over the pointees via some other iterator. ...
Definition: iterator.h:273
MCLOHContainer & getLOHContainer()
Definition: MCAssembler.h:391
void addFileName(StringRef FileName)
Definition: MCAssembler.h:405
A range adaptor for a pair of iterators.
Target - Wrapper for Target specific information.
MachO specific deployment target version info.
Definition: MCAssembler.h:85
Fragment representing the binary annotations produced by the .cv_inline_linetable directive...
Definition: MCFragment.h:494
bool getSubsectionsViaSymbols() const
Definition: MCAssembler.h:278
const_symbol_range symbols() const
Definition: MCAssembler.h:322
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:260
iterator_range< const_symbol_iterator > const_symbol_range
Definition: MCAssembler.h:72
symbol_range symbols()
Definition: MCAssembler.h:321
data_region_iterator data_region_end()
Definition: MCAssembler.h:377
const unsigned Kind
void reset()
Reuse an assembler instance.
Definition: MCAssembler.cpp:79
const_iterator begin() const
Definition: MCAssembler.h:305
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const_data_region_iterator data_region_end() const
Definition: MCAssembler.h:378
bool isIncrementalLinkerCompatible() const
Definition: MCAssembler.h:281
MCVersionMinType
Definition: MCDirectives.h:64
LLVM Value Representation.
Definition: Value.h:71
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:36
indirect_symbol_iterator indirect_symbol_end()
Definition: MCAssembler.h:346
const_indirect_symbol_iterator indirect_symbol_begin() const
Definition: MCAssembler.h:342
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
std::vector< DataRegionData > & getDataRegions()
Definition: MCAssembler.h:370
std::vector< IndirectSymbolData >::iterator indirect_symbol_iterator
Definition: MCAssembler.h:76
pointee_iterator< SectionListType::iterator > iterator
Definition: MCAssembler.h:65
void Finish()
Finish - Do final processing and write the object to the output stream.
pointee_iterator< SymbolDataListType::const_iterator > const_symbol_iterator
Definition: MCAssembler.h:68
size_t data_region_size() const
Definition: MCAssembler.h:382
std::vector< const MCSymbol * > SymbolDataListType
Definition: MCAssembler.h:62
symbol_iterator symbol_end()
Definition: MCAssembler.h:318
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:783