LLVM  7.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"
20 #include "llvm/MC/MCDirectives.h"
21 #include "llvm/MC/MCDwarf.h"
22 #include "llvm/MC/MCFixup.h"
23 #include "llvm/MC/MCFragment.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include <cassert>
27 #include <cstddef>
28 #include <cstdint>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 namespace llvm {
34 
35 class MCAsmBackend;
36 class MCAsmLayout;
37 class MCContext;
38 class MCCodeEmitter;
39 class MCFragment;
40 class MCObjectWriter;
41 class MCSection;
42 class MCValue;
43 
44 // FIXME: This really doesn't belong here. See comments below.
48 };
49 
50 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
51 // to one another.
53  // This enum should be kept in sync w/ the mach-o definition in
54  // llvm/Object/MachOFormat.h.
55  enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
58 };
59 
60 class MCAssembler {
61  friend class MCAsmLayout;
62 
63 public:
64  using SectionListType = std::vector<MCSection *>;
65  using SymbolDataListType = std::vector<const MCSymbol *>;
66 
69 
70  using const_symbol_iterator =
73 
76 
78  std::vector<IndirectSymbolData>::const_iterator;
79  using indirect_symbol_iterator = std::vector<IndirectSymbolData>::iterator;
80 
82  std::vector<DataRegionData>::const_iterator;
83  using data_region_iterator = std::vector<DataRegionData>::iterator;
84 
85  /// MachO specific deployment target version info.
86  // A Major version of 0 indicates that no version information was supplied
87  // and so the corresponding load command should not be emitted.
88  using VersionInfoType = struct {
89  bool EmitBuildVersion;
90  union {
91  MCVersionMinType Type; ///< Used when EmitBuildVersion==false.
92  MachO::PlatformType Platform; ///< Used when EmitBuildVersion==true.
93  } TypeOrPlatform;
94  unsigned Major;
95  unsigned Minor;
96  unsigned Update;
97  };
98 
99 private:
101 
102  MCAsmBackend &Backend;
103 
104  MCCodeEmitter &Emitter;
105 
106  MCObjectWriter &Writer;
107 
108  SectionListType Sections;
109 
110  SymbolDataListType Symbols;
111 
112  std::vector<IndirectSymbolData> IndirectSymbols;
113 
114  std::vector<DataRegionData> DataRegions;
115 
116  /// The list of linker options to propagate into the object file.
117  std::vector<std::vector<std::string>> LinkerOptions;
118 
119  /// List of declared file names
120  std::vector<std::string> FileNames;
121 
122  MCDwarfLineTableParams LTParams;
123 
124  /// The set of function symbols for which a .thumb_func directive has
125  /// been seen.
126  //
127  // FIXME: We really would like this in target specific code rather than
128  // here. Maybe when the relocation stuff moves to target specific,
129  // this can go with it? The streamer would need some target specific
130  // refactoring too.
131  mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
132 
133  /// \brief The bundle alignment size currently set in the assembler.
134  ///
135  /// By default it's 0, which means bundling is disabled.
136  unsigned BundleAlignSize;
137 
138  bool RelaxAll : 1;
139  bool SubsectionsViaSymbols : 1;
140  bool IncrementalLinkerCompatible : 1;
141 
142  /// ELF specific e_header flags
143  // It would be good if there were an MCELFAssembler class to hold this.
144  // ELF header flags are used both by the integrated and standalone assemblers.
145  // Access to the flags is necessary in cases where assembler directives affect
146  // which flags to be set.
147  unsigned ELFHeaderEFlags;
148 
149  /// Used to communicate Linker Optimization Hint information between
150  /// the Streamer and the .o writer
151  MCLOHContainer LOHContainer;
152 
153  VersionInfoType VersionInfo;
154 
155  /// Evaluate a fixup to a relocatable expression and the value which should be
156  /// placed into the fixup.
157  ///
158  /// \param Layout The layout to use for evaluation.
159  /// \param Fixup The fixup to evaluate.
160  /// \param DF The fragment the fixup is inside.
161  /// \param Target [out] On return, the relocatable expression the fixup
162  /// evaluates to.
163  /// \param Value [out] On return, the value of the fixup as currently laid
164  /// out.
165  /// \return Whether the fixup value was fully resolved. This is true if the
166  /// \p Value result is fixed, otherwise the value may change due to
167  /// relocation.
168  bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
169  const MCFragment *DF, MCValue &Target,
170  uint64_t &Value) const;
171 
172  /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
173  /// (increased in size, in order to hold its value correctly).
174  bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
175  const MCAsmLayout &Layout) const;
176 
177  /// Check whether the given fragment needs relaxation.
178  bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
179  const MCAsmLayout &Layout) const;
180 
181  /// \brief Perform one layout iteration and return true if any offsets
182  /// were adjusted.
183  bool layoutOnce(MCAsmLayout &Layout);
184 
185  /// \brief Perform one layout iteration of the given section and return true
186  /// if any offsets were adjusted.
187  bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
188 
189  bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
190 
191  bool relaxPaddingFragment(MCAsmLayout &Layout, MCPaddingFragment &PF);
192 
193  bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
194 
195  bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
196  bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
198  bool relaxCVInlineLineTable(MCAsmLayout &Layout,
200  bool relaxCVDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &DF);
201 
202  /// finishLayout - Finalize a layout, including fragment lowering.
203  void finishLayout(MCAsmLayout &Layout);
204 
205  std::tuple<MCValue, uint64_t, bool>
206  handleFixup(const MCAsmLayout &Layout, MCFragment &F, const MCFixup &Fixup);
207 
208 public:
209  /// Construct a new assembler instance.
210  //
211  // FIXME: How are we going to parameterize this? Two obvious options are stay
212  // concrete and require clients to pass in a target like object. The other
213  // option is to make this abstract, and have targets provide concrete
214  // implementations as we do with AsmParser.
215  MCAssembler(MCContext &Context, MCAsmBackend &Backend,
216  MCCodeEmitter &Emitter, MCObjectWriter &Writer);
217  MCAssembler(const MCAssembler &) = delete;
218  MCAssembler &operator=(const MCAssembler &) = delete;
219  ~MCAssembler();
220 
221  /// Compute the effective fragment size assuming it is laid out at the given
222  /// \p SectionAddress and \p FragmentOffset.
223  uint64_t computeFragmentSize(const MCAsmLayout &Layout,
224  const MCFragment &F) const;
225 
226  /// Find the symbol which defines the atom containing the given symbol, or
227  /// null if there is no such symbol.
228  const MCSymbol *getAtom(const MCSymbol &S) const;
229 
230  /// Check whether a particular symbol is visible to the linker and is required
231  /// in the symbol table, or whether it can be discarded by the assembler. This
232  /// also effects whether the assembler treats the label as potentially
233  /// defining a separate atom.
234  bool isSymbolLinkerVisible(const MCSymbol &SD) const;
235 
236  /// Emit the section contents using the given object writer.
237  void writeSectionData(const MCSection *Section,
238  const MCAsmLayout &Layout) const;
239 
240  /// Check whether a given symbol has been flagged with .thumb_func.
241  bool isThumbFunc(const MCSymbol *Func) const;
242 
243  /// Flag a function symbol as the target of a .thumb_func directive.
244  void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
245 
246  /// ELF e_header flags
247  unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
248  void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
249 
250  /// MachO deployment target version information.
251  const VersionInfoType &getVersionInfo() const { return VersionInfo; }
252  void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
253  unsigned Update) {
254  VersionInfo.EmitBuildVersion = false;
255  VersionInfo.TypeOrPlatform.Type = Type;
256  VersionInfo.Major = Major;
257  VersionInfo.Minor = Minor;
258  VersionInfo.Update = Update;
259  }
260  void setBuildVersion(MachO::PlatformType Platform, unsigned Major,
261  unsigned Minor, unsigned Update) {
262  VersionInfo.EmitBuildVersion = true;
263  VersionInfo.TypeOrPlatform.Platform = Platform;
264  VersionInfo.Major = Major;
265  VersionInfo.Minor = Minor;
266  VersionInfo.Update = Update;
267  }
268 
269  /// Reuse an assembler instance
270  ///
271  void reset();
272 
273  MCContext &getContext() const { return Context; }
274 
275  MCAsmBackend &getBackend() const { return Backend; }
276 
277  MCCodeEmitter &getEmitter() const { return Emitter; }
278 
279  MCObjectWriter &getWriter() const { return Writer; }
280 
281  MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
283 
284  /// Finish - Do final processing and write the object to the output stream.
285  /// \p Writer is used for custom object writer (as the MCJIT does),
286  /// if not specified it is automatically created from backend.
287  void Finish();
288 
289  // Layout all section and prepare them for emission.
290  void layout(MCAsmLayout &Layout);
291 
292  // FIXME: This does not belong here.
293  bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
294  void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
295 
297  return IncrementalLinkerCompatible;
298  }
300  IncrementalLinkerCompatible = Value;
301  }
302 
303  bool getRelaxAll() const { return RelaxAll; }
304  void setRelaxAll(bool Value) { RelaxAll = Value; }
305 
306  bool isBundlingEnabled() const { return BundleAlignSize != 0; }
307 
308  unsigned getBundleAlignSize() const { return BundleAlignSize; }
309 
310  void setBundleAlignSize(unsigned Size) {
311  assert((Size == 0 || !(Size & (Size - 1))) &&
312  "Expect a power-of-two bundle align size");
313  BundleAlignSize = Size;
314  }
315 
316  /// \name Section List Access
317  /// @{
318 
319  iterator begin() { return Sections.begin(); }
320  const_iterator begin() const { return Sections.begin(); }
321 
322  iterator end() { return Sections.end(); }
323  const_iterator end() const { return Sections.end(); }
324 
325  size_t size() const { return Sections.size(); }
326 
327  /// @}
328  /// \name Symbol List Access
329  /// @{
330  symbol_iterator symbol_begin() { return Symbols.begin(); }
331  const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
332 
333  symbol_iterator symbol_end() { return Symbols.end(); }
334  const_symbol_iterator symbol_end() const { return Symbols.end(); }
335 
336  symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
338  return make_range(symbol_begin(), symbol_end());
339  }
340 
341  size_t symbol_size() const { return Symbols.size(); }
342 
343  /// @}
344  /// \name Indirect Symbol List Access
345  /// @{
346 
347  // FIXME: This is a total hack, this should not be here. Once things are
348  // factored so that the streamer has direct access to the .o writer, it can
349  // disappear.
350  std::vector<IndirectSymbolData> &getIndirectSymbols() {
351  return IndirectSymbols;
352  }
353 
355  return IndirectSymbols.begin();
356  }
358  return IndirectSymbols.begin();
359  }
360 
362  return IndirectSymbols.end();
363  }
365  return IndirectSymbols.end();
366  }
367 
368  size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
369 
370  /// @}
371  /// \name Linker Option List Access
372  /// @{
373 
374  std::vector<std::vector<std::string>> &getLinkerOptions() {
375  return LinkerOptions;
376  }
377 
378  /// @}
379  /// \name Data Region List Access
380  /// @{
381 
382  // FIXME: This is a total hack, this should not be here. Once things are
383  // factored so that the streamer has direct access to the .o writer, it can
384  // disappear.
385  std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
386 
387  data_region_iterator data_region_begin() { return DataRegions.begin(); }
389  return DataRegions.begin();
390  }
391 
392  data_region_iterator data_region_end() { return DataRegions.end(); }
394  return DataRegions.end();
395  }
396 
397  size_t data_region_size() const { return DataRegions.size(); }
398 
399  /// @}
400  /// \name Data Region List Access
401  /// @{
402 
403  // FIXME: This is a total hack, this should not be here. Once things are
404  // factored so that the streamer has direct access to the .o writer, it can
405  // disappear.
406  MCLOHContainer &getLOHContainer() { return LOHContainer; }
408  return const_cast<MCAssembler *>(this)->getLOHContainer();
409  }
410  /// @}
411  /// \name Backend Data Access
412  /// @{
413 
414  bool registerSection(MCSection &Section);
415 
416  void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
417 
418  ArrayRef<std::string> getFileNames() { return FileNames; }
419 
420  void addFileName(StringRef FileName) {
421  if (!is_contained(FileNames, FileName))
422  FileNames.push_back(FileName);
423  }
424 
425  /// \brief Write the necessary bundle padding to the given object writer.
426  /// Expects a fragment \p F containing instructions and its size \p FSize.
427  void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
428  MCObjectWriter *OW) const;
429 
430  /// @}
431 
432  void dump() const;
433 };
434 
435 /// \brief Compute the amount of padding required before the fragment \p F to
436 /// obey bundling restrictions, where \p FOffset is the fragment's offset in
437 /// its section and \p FSize is the fragment's size.
438 uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
439  uint64_t FOffset, uint64_t FSize);
440 
441 } // end namespace llvm
442 
443 #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:244
void setELFHeaderEFlags(unsigned Flags)
Definition: MCAssembler.h:248
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
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:319
const_symbol_iterator symbol_end() const
Definition: MCAssembler.h:334
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:341
F(f)
std::vector< IndirectSymbolData > & getIndirectSymbols()
Definition: MCAssembler.h:350
const_data_region_iterator data_region_begin() const
Definition: MCAssembler.h:388
const_iterator end() const
Definition: MCAssembler.h:323
bool isBundlingEnabled() const
Definition: MCAssembler.h:306
Defines the object file and target independent interfaces used by the assembler backend to write nati...
symbol_iterator symbol_begin()
Definition: MCAssembler.h:330
std::vector< DataRegionData >::iterator data_region_iterator
Definition: MCAssembler.h:83
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:308
const_indirect_symbol_iterator indirect_symbol_end() const
Definition: MCAssembler.h:364
MCContext & getContext() const
Definition: MCAssembler.h:273
void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor, unsigned Update)
Definition: MCAssembler.h:252
MCDwarfLineTableParams getDWARFLinetableParams() const
Definition: MCAssembler.h:281
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:277
void setRelaxAll(bool Value)
Definition: MCAssembler.h:304
const MCLOHContainer & getLOHContainer() const
Definition: MCAssembler.h:407
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:279
void setSubsectionsViaSymbols(bool Value)
Definition: MCAssembler.h:294
PlatformType
Definition: MachO.h:484
MCVersionMinType Type
Used when EmitBuildVersion==false.
Definition: MCAssembler.h:91
ArrayRef< std::string > getFileNames()
Definition: MCAssembler.h:418
Context object for machine code objects.
Definition: MCContext.h:62
void setDWARFLinetableParams(MCDwarfLineTableParams P)
Definition: MCAssembler.h:282
const_symbol_iterator symbol_begin() const
Definition: MCAssembler.h:331
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:310
const VersionInfoType & getVersionInfo() const
MachO deployment target version information.
Definition: MCAssembler.h:251
iterator end()
Definition: MCAssembler.h:322
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 setBuildVersion(MachO::PlatformType Platform, unsigned Major, unsigned Minor, unsigned Update)
Definition: MCAssembler.h:260
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
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
struct { bool EmitBuildVersion VersionInfoType
MachO specific deployment target version info.
Definition: MCAssembler.h:89
MachO::PlatformType Platform
Used when EmitBuildVersion==true.
Definition: MCAssembler.h:92
std::vector< DataRegionData >::const_iterator const_data_region_iterator
Definition: MCAssembler.h:82
std::vector< IndirectSymbolData >::const_iterator const_indirect_symbol_iterator
Definition: MCAssembler.h:78
data_region_iterator data_region_begin()
Definition: MCAssembler.h:387
unsigned getELFHeaderEFlags() const
ELF e_header flags.
Definition: MCAssembler.h:247
std::vector< const MCSymbol * > SymbolDataListType
Definition: MCAssembler.h:65
Fragment representing the .cv_def_range directive.
Definition: MCFragment.h:634
size_t data_region_size() const
Definition: MCAssembler.h:397
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:357
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:374
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:354
void setIncrementalLinkerCompatible(bool Value)
Definition: MCAssembler.h:299
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:275
An iterator type that allows iterating over the pointees via some other iterator. ...
Definition: iterator.h:289
bool getSubsectionsViaSymbols() const
Definition: MCAssembler.h:293
size_t indirect_symbol_size() const
Definition: MCAssembler.h:368
MCLOHContainer & getLOHContainer()
Definition: MCAssembler.h:406
void addFileName(StringRef FileName)
Definition: MCAssembler.h:420
bool getRelaxAll() const
Definition: MCAssembler.h:303
A range adaptor for a pair of iterators.
Target - Wrapper for Target specific information.
const_iterator begin() const
Definition: MCAssembler.h:320
Fragment representing the binary annotations produced by the .cv_inline_linetable directive...
Definition: MCFragment.h:596
const_data_region_iterator data_region_end() const
Definition: MCAssembler.h:393
symbol_range symbols()
Definition: MCAssembler.h:336
data_region_iterator data_region_end()
Definition: MCAssembler.h:392
size_t size() const
Definition: MCAssembler.h:325
std::vector< IndirectSymbolData >::iterator indirect_symbol_iterator
Definition: MCAssembler.h:79
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isIncrementalLinkerCompatible() const
Definition: MCAssembler.h:296
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:361
const_symbol_range symbols() const
Definition: MCAssembler.h:337
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
std::vector< DataRegionData > & getDataRegions()
Definition: MCAssembler.h:385
std::vector< MCSection * > SectionListType
Definition: MCAssembler.h:64
symbol_iterator symbol_end()
Definition: MCAssembler.h:333
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:873