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