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