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;
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 relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
187 
188  bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
189  bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
191  bool relaxCVInlineLineTable(MCAsmLayout &Layout,
193  bool relaxCVDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &DF);
194 
195  /// finishLayout - Finalize a layout, including fragment lowering.
196  void finishLayout(MCAsmLayout &Layout);
197 
198  std::tuple<MCValue, uint64_t, bool>
199  handleFixup(const MCAsmLayout &Layout, MCFragment &F, const MCFixup &Fixup);
200 
201 public:
202  /// Construct a new assembler instance.
203  //
204  // FIXME: How are we going to parameterize this? Two obvious options are stay
205  // concrete and require clients to pass in a target like object. The other
206  // option is to make this abstract, and have targets provide concrete
207  // implementations as we do with AsmParser.
208  MCAssembler(MCContext &Context, MCAsmBackend &Backend,
209  MCCodeEmitter &Emitter, MCObjectWriter &Writer);
210  MCAssembler(const MCAssembler &) = delete;
211  MCAssembler &operator=(const MCAssembler &) = delete;
212  ~MCAssembler();
213 
214  /// Compute the effective fragment size assuming it is laid out at the given
215  /// \p SectionAddress and \p FragmentOffset.
216  uint64_t computeFragmentSize(const MCAsmLayout &Layout,
217  const MCFragment &F) const;
218 
219  /// Find the symbol which defines the atom containing the given symbol, or
220  /// null if there is no such symbol.
221  const MCSymbol *getAtom(const MCSymbol &S) const;
222 
223  /// Check whether a particular symbol is visible to the linker and is required
224  /// in the symbol table, or whether it can be discarded by the assembler. This
225  /// also effects whether the assembler treats the label as potentially
226  /// defining a separate atom.
227  bool isSymbolLinkerVisible(const MCSymbol &SD) const;
228 
229  /// Emit the section contents using the given object writer.
230  void writeSectionData(const MCSection *Section,
231  const MCAsmLayout &Layout) const;
232 
233  /// Check whether a given symbol has been flagged with .thumb_func.
234  bool isThumbFunc(const MCSymbol *Func) const;
235 
236  /// Flag a function symbol as the target of a .thumb_func directive.
237  void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
238 
239  /// ELF e_header flags
240  unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
241  void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
242 
243  /// MachO deployment target version information.
244  const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
245  void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
246  unsigned Update) {
247  VersionMinInfo.Kind = Kind;
248  VersionMinInfo.Major = Major;
249  VersionMinInfo.Minor = Minor;
250  VersionMinInfo.Update = Update;
251  }
252 
253  /// Reuse an assembler instance
254  ///
255  void reset();
256 
257  MCContext &getContext() const { return Context; }
258 
259  MCAsmBackend &getBackend() const { return Backend; }
260 
261  MCCodeEmitter &getEmitter() const { return Emitter; }
262 
263  MCObjectWriter &getWriter() const { return Writer; }
264 
265  MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
267 
268  /// Finish - Do final processing and write the object to the output stream.
269  /// \p Writer is used for custom object writer (as the MCJIT does),
270  /// if not specified it is automatically created from backend.
271  void Finish();
272 
273  // Layout all section and prepare them for emission.
274  void layout(MCAsmLayout &Layout);
275 
276  // FIXME: This does not belong here.
277  bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
278  void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
279 
282  }
285  }
286 
287  bool getRelaxAll() const { return RelaxAll; }
288  void setRelaxAll(bool Value) { RelaxAll = Value; }
289 
290  bool isBundlingEnabled() const { return BundleAlignSize != 0; }
291 
292  unsigned getBundleAlignSize() const { return BundleAlignSize; }
293 
294  void setBundleAlignSize(unsigned Size) {
295  assert((Size == 0 || !(Size & (Size - 1))) &&
296  "Expect a power-of-two bundle align size");
297  BundleAlignSize = Size;
298  }
299 
300  /// \name Section List Access
301  /// @{
302 
303  iterator begin() { return Sections.begin(); }
304  const_iterator begin() const { return Sections.begin(); }
305 
306  iterator end() { return Sections.end(); }
307  const_iterator end() const { return Sections.end(); }
308 
309  size_t size() const { return Sections.size(); }
310 
311  /// @}
312  /// \name Symbol List Access
313  /// @{
314  symbol_iterator symbol_begin() { return Symbols.begin(); }
315  const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
316 
317  symbol_iterator symbol_end() { return Symbols.end(); }
318  const_symbol_iterator symbol_end() const { return Symbols.end(); }
319 
320  symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
322  return make_range(symbol_begin(), symbol_end());
323  }
324 
325  size_t symbol_size() const { return Symbols.size(); }
326 
327  /// @}
328  /// \name Indirect Symbol List Access
329  /// @{
330 
331  // FIXME: This is a total hack, this should not be here. Once things are
332  // factored so that the streamer has direct access to the .o writer, it can
333  // disappear.
334  std::vector<IndirectSymbolData> &getIndirectSymbols() {
335  return IndirectSymbols;
336  }
337 
339  return IndirectSymbols.begin();
340  }
342  return IndirectSymbols.begin();
343  }
344 
346  return IndirectSymbols.end();
347  }
349  return IndirectSymbols.end();
350  }
351 
352  size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
353 
354  /// @}
355  /// \name Linker Option List Access
356  /// @{
357 
358  std::vector<std::vector<std::string>> &getLinkerOptions() {
359  return LinkerOptions;
360  }
361 
362  /// @}
363  /// \name Data Region List Access
364  /// @{
365 
366  // FIXME: This is a total hack, this should not be here. Once things are
367  // factored so that the streamer has direct access to the .o writer, it can
368  // disappear.
369  std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
370 
371  data_region_iterator data_region_begin() { return DataRegions.begin(); }
373  return DataRegions.begin();
374  }
375 
376  data_region_iterator data_region_end() { return DataRegions.end(); }
378  return DataRegions.end();
379  }
380 
381  size_t data_region_size() const { return DataRegions.size(); }
382 
383  /// @}
384  /// \name Data Region List Access
385  /// @{
386 
387  // FIXME: This is a total hack, this should not be here. Once things are
388  // factored so that the streamer has direct access to the .o writer, it can
389  // disappear.
390  MCLOHContainer &getLOHContainer() { return LOHContainer; }
392  return const_cast<MCAssembler *>(this)->getLOHContainer();
393  }
394  /// @}
395  /// \name Backend Data Access
396  /// @{
397 
398  bool registerSection(MCSection &Section);
399 
400  void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
401 
402  ArrayRef<std::string> getFileNames() { return FileNames; }
403 
404  void addFileName(StringRef FileName) {
405  if (!is_contained(FileNames, FileName))
406  FileNames.push_back(FileName);
407  }
408 
409  /// \brief Write the necessary bundle padding to the given object writer.
410  /// Expects a fragment \p F containing instructions and its size \p FSize.
411  void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
412  MCObjectWriter *OW) const;
413 
414  /// @}
415 
416  void dump() const;
417 };
418 
419 /// \brief Compute the amount of padding required before the fragment \p F to
420 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
421 /// its section and \p FSize is the fragment's size.
422 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
423  uint64_t FOffset, uint64_t FSize);
424 
425 } // end namespace llvm
426 
427 #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:237
void setELFHeaderEFlags(unsigned Flags)
Definition: MCAssembler.h:241
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
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:245
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:303
const_symbol_iterator symbol_end() const
Definition: MCAssembler.h:318
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:325
F(f)
std::vector< IndirectSymbolData > & getIndirectSymbols()
Definition: MCAssembler.h:334
const_data_region_iterator data_region_begin() const
Definition: MCAssembler.h:372
const_iterator end() const
Definition: MCAssembler.h:307
bool isBundlingEnabled() const
Definition: MCAssembler.h:290
Defines the object file and target independent interfaces used by the assembler backend to write nati...
symbol_iterator symbol_begin()
Definition: MCAssembler.h:314
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:292
const_indirect_symbol_iterator indirect_symbol_end() const
Definition: MCAssembler.h:348
MCContext & getContext() const
Definition: MCAssembler.h:257
MCDwarfLineTableParams getDWARFLinetableParams() const
Definition: MCAssembler.h:265
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:261
void setRelaxAll(bool Value)
Definition: MCAssembler.h:288
const MCLOHContainer & getLOHContainer() const
Definition: MCAssembler.h:391
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:263
struct { MCVersionMinType Kind VersionMinInfoType
MachO specific deployment target version info.
Definition: MCAssembler.h:88
void setSubsectionsViaSymbols(bool Value)
Definition: MCAssembler.h:278
ArrayRef< std::string > getFileNames()
Definition: MCAssembler.h:402
Context object for machine code objects.
Definition: MCContext.h:59
void setDWARFLinetableParams(MCDwarfLineTableParams P)
Definition: MCAssembler.h:266
const_symbol_iterator symbol_begin() const
Definition: MCAssembler.h:315
cl::opt< bool > RelaxAll("mc-relax-all", cl::desc("When used with filetype=obj, " "relax all fixups in the emitted object file"))
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:294
iterator end()
Definition: MCAssembler.h:306
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:255
#define P(N)
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
cl::opt< bool > IncrementalLinkerCompatible("incremental-linker-compatible", cl::desc("When used with filetype=obj, " "emit an object file which can be used with an incremental linker"))
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:371
unsigned getELFHeaderEFlags() const
ELF e_header flags.
Definition: MCAssembler.h:240
std::vector< const MCSymbol * > SymbolDataListType
Definition: MCAssembler.h:64
Fragment representing the .cv_def_range directive.
Definition: MCFragment.h:532
size_t data_region_size() const
Definition: MCAssembler.h:381
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:341
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:358
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:338
void setIncrementalLinkerCompatible(bool Value)
Definition: MCAssembler.h:283
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:259
An iterator type that allows iterating over the pointees via some other iterator. ...
Definition: iterator.h:289
bool getSubsectionsViaSymbols() const
Definition: MCAssembler.h:277
size_t indirect_symbol_size() const
Definition: MCAssembler.h:352
MCLOHContainer & getLOHContainer()
Definition: MCAssembler.h:390
void addFileName(StringRef FileName)
Definition: MCAssembler.h:404
bool getRelaxAll() const
Definition: MCAssembler.h:287
A range adaptor for a pair of iterators.
Target - Wrapper for Target specific information.
const_iterator begin() const
Definition: MCAssembler.h:304
Fragment representing the binary annotations produced by the .cv_inline_linetable directive...
Definition: MCFragment.h:494
const VersionMinInfoType & getVersionMinInfo() const
MachO deployment target version information.
Definition: MCAssembler.h:244
const_data_region_iterator data_region_end() const
Definition: MCAssembler.h:377
symbol_range symbols()
Definition: MCAssembler.h:320
data_region_iterator data_region_end()
Definition: MCAssembler.h:376
size_t size() const
Definition: MCAssembler.h:309
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:280
MCVersionMinType
Definition: MCDirectives.h:64
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:35
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
indirect_symbol_iterator indirect_symbol_end()
Definition: MCAssembler.h:345
const_symbol_range symbols() const
Definition: MCAssembler.h:321
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
std::vector< DataRegionData > & getDataRegions()
Definition: MCAssembler.h:369
std::vector< MCSection * > SectionListType
Definition: MCAssembler.h:63
symbol_iterator symbol_end()
Definition: MCAssembler.h:317
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:870