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/SmallPtrSet.h"
     14 #include "llvm/ADT/ilist.h"
     15 #include "llvm/ADT/ilist_node.h"
     16 #include "llvm/ADT/iterator.h"
     17 #include "llvm/MC/MCDirectives.h"
     18 #include "llvm/MC/MCDwarf.h"
     19 #include "llvm/MC/MCFixup.h"
     20 #include "llvm/MC/MCFragment.h"
     21 #include "llvm/MC/MCInst.h"
     22 #include "llvm/MC/MCLinkerOptimizationHint.h"
     23 #include "llvm/MC/MCSubtargetInfo.h"
     24 #include "llvm/MC/MCSymbol.h"
     25 
     26 namespace llvm {
     27 class raw_ostream;
     28 class MCAsmLayout;
     29 class MCAssembler;
     30 class MCContext;
     31 class MCCodeEmitter;
     32 class MCExpr;
     33 class MCFragment;
     34 class MCObjectWriter;
     35 class MCSection;
     36 class MCSubtargetInfo;
     37 class MCValue;
     38 class MCAsmBackend;
     39 
     40 // FIXME: This really doesn't belong here. See comments below.
     41 struct IndirectSymbolData {
     42   MCSymbol *Symbol;
     43   MCSection *Section;
     44 };
     45 
     46 // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
     47 // to one another.
     48 struct DataRegionData {
     49   // This enum should be kept in sync w/ the mach-o definition in
     50   // llvm/Object/MachOFormat.h.
     51   enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
     52   MCSymbol *Start;
     53   MCSymbol *End;
     54 };
     55 
     56 class MCAssembler {
     57   friend class MCAsmLayout;
     58 
     59 public:
     60   typedef std::vector<MCSection *> SectionListType;
     61   typedef std::vector<const MCSymbol *> SymbolDataListType;
     62 
     63   typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
     64   typedef pointee_iterator<SectionListType::iterator> iterator;
     65 
     66   typedef pointee_iterator<SymbolDataListType::const_iterator>
     67   const_symbol_iterator;
     68   typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator;
     69 
     70   typedef iterator_range<symbol_iterator> symbol_range;
     71   typedef iterator_range<const_symbol_iterator> const_symbol_range;
     72 
     73   typedef std::vector<IndirectSymbolData>::const_iterator
     74       const_indirect_symbol_iterator;
     75   typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
     76 
     77   typedef std::vector<DataRegionData>::const_iterator
     78       const_data_region_iterator;
     79   typedef std::vector<DataRegionData>::iterator data_region_iterator;
     80 
     81   /// MachO specific deployment target version info.
     82   // A Major version of 0 indicates that no version information was supplied
     83   // and so the corresponding load command should not be emitted.
     84   typedef struct {
     85     MCVersionMinType Kind;
     86     unsigned Major;
     87     unsigned Minor;
     88     unsigned Update;
     89   } VersionMinInfoType;
     90 
     91 private:
     92   MCAssembler(const MCAssembler &) = delete;
     93   void operator=(const MCAssembler &) = delete;
     94 
     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   unsigned RelaxAll : 1;
    134   unsigned SubsectionsViaSymbols : 1;
    135   unsigned 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 private:
    151   /// Evaluate a fixup to a relocatable expression and the value which should be
    152   /// placed into the fixup.
    153   ///
    154   /// \param Layout The layout to use for evaluation.
    155   /// \param Fixup The fixup to evaluate.
    156   /// \param DF The fragment the fixup is inside.
    157   /// \param Target [out] On return, the relocatable expression the fixup
    158   /// evaluates to.
    159   /// \param Value [out] On return, the value of the fixup as currently laid
    160   /// out.
    161   /// \return Whether the fixup value was fully resolved. This is true if the
    162   /// \p Value result is fixed, otherwise the value may change due to
    163   /// relocation.
    164   bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
    165                      const MCFragment *DF, MCValue &Target,
    166                      uint64_t &Value) const;
    167 
    168   /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
    169   /// (increased in size, in order to hold its value correctly).
    170   bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
    171                             const MCAsmLayout &Layout) const;
    172 
    173   /// Check whether the given fragment needs relaxation.
    174   bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
    175                                const MCAsmLayout &Layout) const;
    176 
    177   /// \brief Perform one layout iteration and return true if any offsets
    178   /// were adjusted.
    179   bool layoutOnce(MCAsmLayout &Layout);
    180 
    181   /// \brief Perform one layout iteration of the given section and return true
    182   /// if any offsets were adjusted.
    183   bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
    184 
    185   bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
    186 
    187   bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
    188 
    189   bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
    190   bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
    191                                    MCDwarfCallFrameFragment &DF);
    192   bool relaxCVInlineLineTable(MCAsmLayout &Layout,
    193                               MCCVInlineLineTableFragment &DF);
    194   bool relaxCVDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &DF);
    195 
    196   /// finishLayout - Finalize a layout, including fragment lowering.
    197   void finishLayout(MCAsmLayout &Layout);
    198 
    199   std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
    200                                         MCFragment &F, const MCFixup &Fixup);
    201 
    202 public:
    203   /// Compute the effective fragment size assuming it is laid out at the given
    204   /// \p SectionAddress and \p FragmentOffset.
    205   uint64_t computeFragmentSize(const MCAsmLayout &Layout,
    206                                const MCFragment &F) const;
    207 
    208   /// Find the symbol which defines the atom containing the given symbol, or
    209   /// null if there is no such symbol.
    210   const MCSymbol *getAtom(const MCSymbol &S) const;
    211 
    212   /// Check whether a particular symbol is visible to the linker and is required
    213   /// in the symbol table, or whether it can be discarded by the assembler. This
    214   /// also effects whether the assembler treats the label as potentially
    215   /// defining a separate atom.
    216   bool isSymbolLinkerVisible(const MCSymbol &SD) const;
    217 
    218   /// Emit the section contents using the given object writer.
    219   void writeSectionData(const MCSection *Section,
    220                         const MCAsmLayout &Layout) const;
    221 
    222   /// Check whether a given symbol has been flagged with .thumb_func.
    223   bool isThumbFunc(const MCSymbol *Func) const;
    224 
    225   /// Flag a function symbol as the target of a .thumb_func directive.
    226   void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
    227 
    228   /// ELF e_header flags
    229   unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
    230   void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
    231 
    232   /// MachO deployment target version information.
    233   const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
    234   void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
    235                          unsigned Update) {
    236     VersionMinInfo.Kind = Kind;
    237     VersionMinInfo.Major = Major;
    238     VersionMinInfo.Minor = Minor;
    239     VersionMinInfo.Update = Update;
    240   }
    241 
    242 public:
    243   /// Construct a new assembler instance.
    244   //
    245   // FIXME: How are we going to parameterize this? Two obvious options are stay
    246   // concrete and require clients to pass in a target like object. The other
    247   // option is to make this abstract, and have targets provide concrete
    248   // implementations as we do with AsmParser.
    249   MCAssembler(MCContext &Context, MCAsmBackend &Backend,
    250               MCCodeEmitter &Emitter, MCObjectWriter &Writer);
    251   ~MCAssembler();
    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 (std::find(FileNames.begin(), FileNames.end(), FileName) ==
    406         FileNames.end())
    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
    429