Home | History | Annotate | Download | only in mcld
      1 //===- Module.h -----------------------------------------------------------===//
      2 //
      3 //                     The MCLinker Project
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // Module contains the intermediate representation (LDIR) of MCLinker.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 #ifndef MCLD_MODULE_H
     14 #define MCLD_MODULE_H
     15 #ifdef ENABLE_UNITTEST
     16 #include <gtest.h>
     17 #endif
     18 
     19 #include <vector>
     20 #include <string>
     21 #include <map>
     22 
     23 #include <mcld/LinkerScript.h>
     24 #include <mcld/InputTree.h>
     25 #include <mcld/ADT/HashTable.h>
     26 #include <mcld/ADT/HashEntry.h>
     27 #include <mcld/Support/GCFactoryListTraits.h>
     28 #include <mcld/Fragment/Fragment.h>
     29 #include <mcld/LD/NamePool.h>
     30 #include <mcld/LD/SectionSymbolSet.h>
     31 #include <mcld/MC/SymbolCategory.h>
     32 #include <mcld/MC/MCLDInput.h>
     33 
     34 #include <llvm/ADT/ilist.h>
     35 
     36 namespace mcld {
     37 
     38 class LDSection;
     39 class LDSymbol;
     40 
     41 /** \class Module
     42  *  \brief Module provides the intermediate representation for linking.
     43  */
     44 class Module
     45 {
     46 public:
     47   typedef std::vector<Input*> ObjectList;
     48   typedef ObjectList::iterator obj_iterator;
     49   typedef ObjectList::const_iterator const_obj_iterator;
     50 
     51   typedef std::vector<Input*> LibraryList;
     52   typedef LibraryList::iterator lib_iterator;
     53   typedef LibraryList::const_iterator const_lib_iterator;
     54 
     55   typedef InputTree::iterator input_iterator;
     56   typedef InputTree::const_iterator const_input_iterator;
     57 
     58   typedef std::vector<LDSection*> SectionTable;
     59   typedef SectionTable::iterator iterator;
     60   typedef SectionTable::const_iterator const_iterator;
     61 
     62   typedef SymbolCategory SymbolTable;
     63   typedef SymbolTable::iterator sym_iterator;
     64   typedef SymbolTable::const_iterator const_sym_iterator;
     65 
     66   typedef std::vector<const ResolveInfo*> AliasList;
     67   typedef AliasList::iterator alias_iterator;
     68   typedef AliasList::const_iterator const_alias_iterator;
     69 
     70 public:
     71   explicit Module(LinkerScript& pScript);
     72 
     73   Module(const std::string& pName, LinkerScript& pScript);
     74 
     75   ~Module();
     76 
     77   const std::string& name() const { return m_Name; }
     78 
     79   void setName(const std::string& pName) { m_Name = pName; }
     80 
     81   const LinkerScript& getScript() const { return m_Script; }
     82 
     83   LinkerScript&       getScript()       { return m_Script; }
     84 
     85   // -----  link-in objects ----- //
     86   const ObjectList& getObjectList() const { return m_ObjectList; }
     87   ObjectList&       getObjectList()       { return m_ObjectList; }
     88 
     89   const_obj_iterator obj_begin() const { return m_ObjectList.begin(); }
     90   obj_iterator       obj_begin()       { return m_ObjectList.begin(); }
     91   const_obj_iterator obj_end  () const { return m_ObjectList.end();   }
     92   obj_iterator       obj_end  ()       { return m_ObjectList.end();   }
     93 
     94   // -----  link-in libraries  ----- //
     95   const LibraryList& getLibraryList() const { return m_LibraryList; }
     96   LibraryList&       getLibraryList()       { return m_LibraryList; }
     97 
     98   const_lib_iterator lib_begin() const { return m_LibraryList.begin(); }
     99   lib_iterator       lib_begin()       { return m_LibraryList.begin(); }
    100   const_lib_iterator lib_end  () const { return m_LibraryList.end();   }
    101   lib_iterator       lib_end  ()       { return m_LibraryList.end();   }
    102 
    103   // -----  link-in inputs  ----- //
    104   const InputTree& getInputTree() const { return m_MainTree; }
    105   InputTree&       getInputTree()       { return m_MainTree; }
    106 
    107   const_input_iterator input_begin() const { return m_MainTree.begin(); }
    108   input_iterator       input_begin()       { return m_MainTree.begin(); }
    109   const_input_iterator input_end  () const { return m_MainTree.end();   }
    110   input_iterator       input_end  ()       { return m_MainTree.end();   }
    111 
    112 /// @}
    113 /// @name Section Accessors
    114 /// @{
    115 
    116   // -----  sections  ----- //
    117   const SectionTable& getSectionTable() const { return m_SectionTable; }
    118   SectionTable&       getSectionTable()       { return m_SectionTable; }
    119 
    120   iterator         begin()       { return m_SectionTable.begin(); }
    121   const_iterator   begin() const { return m_SectionTable.begin(); }
    122   iterator         end  ()       { return m_SectionTable.end();   }
    123   const_iterator   end  () const { return m_SectionTable.end();   }
    124   LDSection*       front()       { return m_SectionTable.front(); }
    125   const LDSection* front() const { return m_SectionTable.front(); }
    126   LDSection*       back ()       { return m_SectionTable.back();  }
    127   const LDSection* back () const { return m_SectionTable.back();  }
    128   size_t           size () const { return m_SectionTable.size();  }
    129   bool             empty() const { return m_SectionTable.empty(); }
    130 
    131   LDSection*       getSection(const std::string& pName);
    132   const LDSection* getSection(const std::string& pName) const;
    133 
    134 /// @}
    135 /// @name Symbol Accessors
    136 /// @{
    137 
    138   // -----  symbols  ----- //
    139   const SymbolTable& getSymbolTable() const { return m_SymbolTable; }
    140   SymbolTable&       getSymbolTable()       { return m_SymbolTable; }
    141 
    142   sym_iterator       sym_begin()       { return m_SymbolTable.begin();         }
    143   const_sym_iterator sym_begin() const { return m_SymbolTable.begin();         }
    144   sym_iterator       sym_end  ()       { return m_SymbolTable.end();           }
    145   const_sym_iterator sym_end  () const { return m_SymbolTable.end();           }
    146   size_t             sym_size () const { return m_SymbolTable.numOfSymbols();  }
    147 
    148   // ----- section symbols ----- //
    149   const LDSymbol* getSectionSymbol(const LDSection& pSection) const
    150   { return m_SectSymbolSet.get(pSection); }
    151 
    152   LDSymbol* getSectionSymbol(const LDSection& pSection)
    153   { return m_SectSymbolSet.get(pSection); }
    154 
    155   const SectionSymbolSet& getSectionSymbolSet() const
    156   { return m_SectSymbolSet; }
    157   SectionSymbolSet&       getSectionSymbolSet()
    158   { return m_SectSymbolSet; }
    159 
    160   // -----  names  ----- //
    161   const NamePool& getNamePool() const { return m_NamePool; }
    162   NamePool&       getNamePool()       { return m_NamePool; }
    163 
    164   // -----  Aliases  ----- //
    165   // create an alias list for pSym, the aliases of pSym
    166   // can be added into the list by calling addAlias
    167   void CreateAliasList(const ResolveInfo& pSym);
    168 
    169   // add pAlias into the newly created alias list
    170   void addAlias(const ResolveInfo& pAlias);
    171   AliasList* getAliasList(const ResolveInfo& pSym);
    172 
    173 private:
    174   std::string m_Name;
    175   LinkerScript& m_Script;
    176   ObjectList m_ObjectList;
    177   LibraryList m_LibraryList;
    178   InputTree m_MainTree;
    179   SectionTable m_SectionTable;
    180   SymbolTable m_SymbolTable;
    181   NamePool m_NamePool;
    182   SectionSymbolSet m_SectSymbolSet;
    183   std::vector<AliasList*> m_AliasLists;
    184 };
    185 
    186 } // namespace of mcld
    187 
    188 #endif
    189 
    190