blob: fdeb4425fdd4d32683b3cbf76162c6494498eeae [file] [log] [blame]
Shih-wei Liao22add6f2012-12-15 17:21:00 -08001//===- 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//===----------------------------------------------------------------------===//
Stephen Hines37b74a32014-11-26 18:48:20 -080013#ifndef MCLD_MODULE_H_
14#define MCLD_MODULE_H_
Shih-wei Liao22add6f2012-12-15 17:21:00 -080015
Stephen Hines37b74a32014-11-26 18:48:20 -080016#include "mcld/InputTree.h"
17#include "mcld/LD/NamePool.h"
18#include "mcld/LD/SectionSymbolSet.h"
19#include "mcld/MC/SymbolCategory.h"
Shih-wei Liao22add6f2012-12-15 17:21:00 -080020
Stephen Hines87f34652014-02-14 18:00:16 -080021#include <vector>
22#include <string>
Stephen Hinesf7ac0f12013-05-03 19:09:24 -070023
Shih-wei Liao22add6f2012-12-15 17:21:00 -080024namespace mcld {
25
Stephen Hines87f34652014-02-14 18:00:16 -080026class Input;
27class LinkerScript;
Shih-wei Liao22add6f2012-12-15 17:21:00 -080028class LDSection;
29class LDSymbol;
30
31/** \class Module
32 * \brief Module provides the intermediate representation for linking.
33 */
Stephen Hines37b74a32014-11-26 18:48:20 -080034class Module {
35 public:
Shih-wei Liao22add6f2012-12-15 17:21:00 -080036 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
Stephen Hinesf7ac0f12013-05-03 19:09:24 -070055 typedef std::vector<const ResolveInfo*> AliasList;
56 typedef AliasList::iterator alias_iterator;
57 typedef AliasList::const_iterator const_alias_iterator;
Shih-wei Liao22add6f2012-12-15 17:21:00 -080058
Stephen Hines37b74a32014-11-26 18:48:20 -080059 public:
Stephen Hinesf7ac0f12013-05-03 19:09:24 -070060 explicit Module(LinkerScript& pScript);
61
62 Module(const std::string& pName, LinkerScript& pScript);
Shih-wei Liao22add6f2012-12-15 17:21:00 -080063
64 ~Module();
65
Shih-wei Liao22add6f2012-12-15 17:21:00 -080066 const std::string& name() const { return m_Name; }
67
68 void setName(const std::string& pName) { m_Name = pName; }
69
Stephen Hinesf7ac0f12013-05-03 19:09:24 -070070 const LinkerScript& getScript() const { return m_Script; }
71
Stephen Hines37b74a32014-11-26 18:48:20 -080072 LinkerScript& getScript() { return m_Script; }
Stephen Hinesf7ac0f12013-05-03 19:09:24 -070073
Shih-wei Liao22add6f2012-12-15 17:21:00 -080074 // ----- link-in objects ----- //
75 const ObjectList& getObjectList() const { return m_ObjectList; }
Stephen Hines37b74a32014-11-26 18:48:20 -080076 ObjectList& getObjectList() { return m_ObjectList; }
Shih-wei Liao22add6f2012-12-15 17:21:00 -080077
78 const_obj_iterator obj_begin() const { return m_ObjectList.begin(); }
Stephen Hines37b74a32014-11-26 18:48:20 -080079 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(); }
Shih-wei Liao22add6f2012-12-15 17:21:00 -080082
83 // ----- link-in libraries ----- //
84 const LibraryList& getLibraryList() const { return m_LibraryList; }
Stephen Hines37b74a32014-11-26 18:48:20 -080085 LibraryList& getLibraryList() { return m_LibraryList; }
Shih-wei Liao22add6f2012-12-15 17:21:00 -080086
87 const_lib_iterator lib_begin() const { return m_LibraryList.begin(); }
Stephen Hines37b74a32014-11-26 18:48:20 -080088 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(); }
Shih-wei Liao22add6f2012-12-15 17:21:00 -080091
92 // ----- link-in inputs ----- //
93 const InputTree& getInputTree() const { return m_MainTree; }
Stephen Hines37b74a32014-11-26 18:48:20 -080094 InputTree& getInputTree() { return m_MainTree; }
Shih-wei Liao22add6f2012-12-15 17:21:00 -080095
96 const_input_iterator input_begin() const { return m_MainTree.begin(); }
Stephen Hines37b74a32014-11-26 18:48:20 -080097 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(); }
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800100
Stephen Hines37b74a32014-11-26 18:48:20 -0800101 /// @}
102 /// @name Section Accessors
103 /// @{
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800104
105 // ----- sections ----- //
106 const SectionTable& getSectionTable() const { return m_SectionTable; }
Stephen Hines37b74a32014-11-26 18:48:20 -0800107 SectionTable& getSectionTable() { return m_SectionTable; }
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800108
Stephen Hines37b74a32014-11-26 18:48:20 -0800109 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(); }
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800114 const LDSection* front() const { return m_SectionTable.front(); }
Stephen Hines37b74a32014-11-26 18:48:20 -0800115 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(); }
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800119
Stephen Hines37b74a32014-11-26 18:48:20 -0800120 LDSection* getSection(const std::string& pName);
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800121 const LDSection* getSection(const std::string& pName) const;
122
Stephen Hines37b74a32014-11-26 18:48:20 -0800123 /// @}
124 /// @name Symbol Accessors
125 /// @{
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800126
127 // ----- symbols ----- //
128 const SymbolTable& getSymbolTable() const { return m_SymbolTable; }
Stephen Hines37b74a32014-11-26 18:48:20 -0800129 SymbolTable& getSymbolTable() { return m_SymbolTable; }
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800130
Stephen Hines37b74a32014-11-26 18:48:20 -0800131 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(); }
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800136
137 // ----- section symbols ----- //
Stephen Hines37b74a32014-11-26 18:48:20 -0800138 const LDSymbol* getSectionSymbol(const LDSection& pSection) const {
139 return m_SectSymbolSet.get(pSection);
140 }
Stephen Hinesf7ac0f12013-05-03 19:09:24 -0700141
Stephen Hines37b74a32014-11-26 18:48:20 -0800142 LDSymbol* getSectionSymbol(const LDSection& pSection) {
143 return m_SectSymbolSet.get(pSection);
144 }
Stephen Hinesf7ac0f12013-05-03 19:09:24 -0700145
Stephen Hines37b74a32014-11-26 18:48:20 -0800146 const SectionSymbolSet& getSectionSymbolSet() const {
147 return m_SectSymbolSet;
148 }
149 SectionSymbolSet& getSectionSymbolSet() { return m_SectSymbolSet; }
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800150
151 // ----- names ----- //
152 const NamePool& getNamePool() const { return m_NamePool; }
Stephen Hines37b74a32014-11-26 18:48:20 -0800153 NamePool& getNamePool() { return m_NamePool; }
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800154
Stephen Hinesf7ac0f12013-05-03 19:09:24 -0700155 // ----- 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
Stephen Hines37b74a32014-11-26 18:48:20 -0800164 private:
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800165 std::string m_Name;
Stephen Hinesf7ac0f12013-05-03 19:09:24 -0700166 LinkerScript& m_Script;
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800167 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;
Stephen Hinesf7ac0f12013-05-03 19:09:24 -0700174 std::vector<AliasList*> m_AliasLists;
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800175};
176
Stephen Hines37b74a32014-11-26 18:48:20 -0800177} // namespace mcld
Shih-wei Liao22add6f2012-12-15 17:21:00 -0800178
Stephen Hines37b74a32014-11-26 18:48:20 -0800179#endif // MCLD_MODULE_H_