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 
name()66   const std::string& name() const { return m_Name; }
67 
setName(const std::string & pName)68   void setName(const std::string& pName) { m_Name = pName; }
69 
getScript()70   const LinkerScript& getScript() const { return m_Script; }
71 
getScript()72   LinkerScript& getScript() { return m_Script; }
73 
74   // -----  link-in objects ----- //
getObjectList()75   const ObjectList& getObjectList() const { return m_ObjectList; }
getObjectList()76   ObjectList& getObjectList() { return m_ObjectList; }
77 
obj_begin()78   const_obj_iterator obj_begin() const { return m_ObjectList.begin(); }
obj_begin()79   obj_iterator obj_begin() { return m_ObjectList.begin(); }
obj_end()80   const_obj_iterator obj_end() const { return m_ObjectList.end(); }
obj_end()81   obj_iterator obj_end() { return m_ObjectList.end(); }
82 
83   // -----  link-in libraries  ----- //
getLibraryList()84   const LibraryList& getLibraryList() const { return m_LibraryList; }
getLibraryList()85   LibraryList& getLibraryList() { return m_LibraryList; }
86 
lib_begin()87   const_lib_iterator lib_begin() const { return m_LibraryList.begin(); }
lib_begin()88   lib_iterator lib_begin() { return m_LibraryList.begin(); }
lib_end()89   const_lib_iterator lib_end() const { return m_LibraryList.end(); }
lib_end()90   lib_iterator lib_end() { return m_LibraryList.end(); }
91 
92   // -----  link-in inputs  ----- //
getInputTree()93   const InputTree& getInputTree() const { return m_MainTree; }
getInputTree()94   InputTree& getInputTree() { return m_MainTree; }
95 
input_begin()96   const_input_iterator input_begin() const { return m_MainTree.begin(); }
input_begin()97   input_iterator input_begin() { return m_MainTree.begin(); }
input_end()98   const_input_iterator input_end() const { return m_MainTree.end(); }
input_end()99   input_iterator input_end() { return m_MainTree.end(); }
100 
101   /// @}
102   /// @name Section Accessors
103   /// @{
104 
105   // -----  sections  ----- //
getSectionTable()106   const SectionTable& getSectionTable() const { return m_SectionTable; }
getSectionTable()107   SectionTable& getSectionTable() { return m_SectionTable; }
108 
begin()109   iterator begin() { return m_SectionTable.begin(); }
begin()110   const_iterator begin() const { return m_SectionTable.begin(); }
end()111   iterator end() { return m_SectionTable.end(); }
end()112   const_iterator end() const { return m_SectionTable.end(); }
front()113   LDSection* front() { return m_SectionTable.front(); }
front()114   const LDSection* front() const { return m_SectionTable.front(); }
back()115   LDSection* back() { return m_SectionTable.back(); }
back()116   const LDSection* back() const { return m_SectionTable.back(); }
size()117   size_t size() const { return m_SectionTable.size(); }
empty()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  ----- //
getSymbolTable()128   const SymbolTable& getSymbolTable() const { return m_SymbolTable; }
getSymbolTable()129   SymbolTable& getSymbolTable() { return m_SymbolTable; }
130 
sym_begin()131   sym_iterator sym_begin() { return m_SymbolTable.begin(); }
sym_begin()132   const_sym_iterator sym_begin() const { return m_SymbolTable.begin(); }
sym_end()133   sym_iterator sym_end() { return m_SymbolTable.end(); }
sym_end()134   const_sym_iterator sym_end() const { return m_SymbolTable.end(); }
sym_size()135   size_t sym_size() const { return m_SymbolTable.numOfSymbols(); }
136 
137   // ----- section symbols ----- //
getSectionSymbol(const LDSection & pSection)138   const LDSymbol* getSectionSymbol(const LDSection& pSection) const {
139     return m_SectSymbolSet.get(pSection);
140   }
141 
getSectionSymbol(const LDSection & pSection)142   LDSymbol* getSectionSymbol(const LDSection& pSection) {
143     return m_SectSymbolSet.get(pSection);
144   }
145 
getSectionSymbolSet()146   const SectionSymbolSet& getSectionSymbolSet() const {
147     return m_SectSymbolSet;
148   }
getSectionSymbolSet()149   SectionSymbolSet& getSectionSymbolSet() { return m_SectSymbolSet; }
150 
151   // -----  names  ----- //
getNamePool()152   const NamePool& getNamePool() const { return m_NamePool; }
getNamePool()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