Home | History | Annotate | Download | only in MC
      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"
     18 #include "llvm/ADT/iterator_range.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"
     23 #include "llvm/MC/MCLinkerOptimizationHint.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.
     44 struct IndirectSymbolData {
     45   MCSymbol *Symbol;
     46   MCSection *Section;
     47 };
     48 
     49 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
     50 // to one another.
     51 struct DataRegionData {
     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;
     55   MCSymbol *Start;
     56   MCSymbol *End;
     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 
     66   using const_iterator = pointee_iterator<SectionListType::const_iterator>;
     67   using iterator = pointee_iterator<SectionListType::iterator>;
     68 
     69   using const_symbol_iterator =
     70       pointee_iterator<SymbolDataListType::const_iterator>;
     71   using symbol_iterator = pointee_iterator<SymbolDataListType::iterator>;
     72 
     73   using symbol_range = iterator_range<symbol_iterator>;
     74   using const_symbol_range = iterator_range<const_symbol_iterator>;
     75 
     76   using const_indirect_symbol_iterator =
     77       std::vector<IndirectSymbolData>::const_iterator;
     78   using indirect_symbol_iterator = std::vector<IndirectSymbolData>::iterator;
     79 
     80   using const_data_region_iterator =
     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 {
     88     MCVersionMinType Kind;
     89     unsigned Major;
     90     unsigned Minor;
     91     unsigned Update;
     92   };
     93 
     94 private:
     95   MCContext &Context;
     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 relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
    187 
    188   bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
    189   bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
    190                                    MCDwarfCallFrameFragment &DF);
    191   bool relaxCVInlineLineTable(MCAsmLayout &Layout,
    192                               MCCVInlineLineTableFragment &DF);
    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; }
    266   void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
    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 
    280   bool isIncrementalLinkerCompatible() const {
    281     return IncrementalLinkerCompatible;
    282   }
    283   void setIncrementalLinkerCompatible(bool Value) {
    284     IncrementalLinkerCompatible = Value;
    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()); }
    321   const_symbol_range symbols() const {
    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 
    338   indirect_symbol_iterator indirect_symbol_begin() {
    339     return IndirectSymbols.begin();
    340   }
    341   const_indirect_symbol_iterator indirect_symbol_begin() const {
    342     return IndirectSymbols.begin();
    343   }
    344 
    345   indirect_symbol_iterator indirect_symbol_end() {
    346     return IndirectSymbols.end();
    347   }
    348   const_indirect_symbol_iterator indirect_symbol_end() const {
    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(); }
    372   const_data_region_iterator data_region_begin() const {
    373     return DataRegions.begin();
    374   }
    375 
    376   data_region_iterator data_region_end() { return DataRegions.end(); }
    377   const_data_region_iterator data_region_end() const {
    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; }
    391   const MCLOHContainer &getLOHContainer() const {
    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
    428