• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ObjectLinker.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 // ObjectLinker plays the same role as GNU collect2 to prepare all implicit
11 // parameters for FragmentLinker.
12 //
13 //===----------------------------------------------------------------------===//
14 #ifndef MCLD_OBJECT_OBJECT_LINKER_H
15 #define MCLD_OBJECT_OBJECT_LINKER_H
16 #ifdef ENABLE_UNITTEST
17 #include <gtest.h>
18 #endif
19 #include <stddef.h>
20 
21 namespace mcld {
22 
23 class Module;
24 class LinkerConfig;
25 class IRBuilder;
26 class FragmentLinker;
27 class TargetLDBackend;
28 class MemoryArea;
29 class MemoryAreaFactory;
30 class ObjectReader;
31 class DynObjReader;
32 class ArchiveReader;
33 class GroupReader;
34 class BinaryReader;
35 class ObjectWriter;
36 class DynObjWriter;
37 class ExecWriter;
38 class BinaryWriter;
39 
40 /** \class ObjectLinker
41  *  \brief ObjectLinker prepares parameters for FragmentLinker.
42  */
43 class ObjectLinker
44 {
45 public:
46   ObjectLinker(const LinkerConfig& pConfig,
47                TargetLDBackend& pLDBackend);
48 
49   ~ObjectLinker();
50 
51   void setup(Module& pModule, IRBuilder& pBuilder);
52 
53   /// initFragmentLinker - initialize FragmentLinker
54   ///  Connect all components in FragmentLinker
55   bool initFragmentLinker();
56 
57   /// initStdSections - initialize standard sections of the output file.
58   bool initStdSections();
59 
60   /// normalize - normalize the input files
61   void normalize();
62 
63   /// linkable - check the linkability of current LinkerConfig
64   ///  Check list:
65   ///  - check the Attributes are not violate the constaint
66   ///  - check every Input has a correct Attribute
67   bool linkable() const;
68 
69   /// readRelocations - read all relocation entries
70   bool readRelocations();
71 
72   /// mergeSections - put allinput sections into output sections
73   bool mergeSections();
74 
75   /// allocateCommonSymobols - allocate fragments for common symbols to the
76   /// corresponding sections
77   bool allocateCommonSymbols();
78 
79   /// addStandardSymbols - shared object and executable files need some
80   /// standard symbols
81   ///   @return if there are some input symbols with the same name to the
82   ///   standard symbols, return false
83   bool addStandardSymbols();
84 
85   /// addTargetSymbols - some targets, such as MIPS and ARM, need some
86   /// target-dependent symbols
87   ///   @return if there are some input symbols with the same name to the
88   ///   target symbols, return false
89   bool addTargetSymbols();
90 
91   /// addScriptSymbols - define symbols from the command line option or linker
92   /// scripts.
93   bool addScriptSymbols();
94 
95   /// scanRelocations - scan all relocation entries by output symbols.
96   bool scanRelocations();
97 
98   /// initStubs - initialize stub-related stuff.
99   bool initStubs();
100 
101   /// prelayout - help backend to do some modification before layout
102   bool prelayout();
103 
104   /// layout - linearly layout all output sections and reserve some space
105   /// for GOT/PLT
106   ///   Because we do not support instruction relaxing in this early version,
107   ///   if there is a branch can not jump to its target, we return false
108   ///   directly
109   bool layout();
110 
111   /// postlayout - help backend to do some modification after layout
112   bool postlayout();
113 
114   /// relocate - applying relocation entries and create relocation
115   /// section in the output files
116   /// Create relocation section, asking TargetLDBackend to
117   /// read the relocation information into RelocationEntry
118   /// and push_back into the relocation section
119   bool relocation();
120 
121   /// finalizeSymbolValue - finalize the symbol value
122   bool finalizeSymbolValue();
123 
124   /// emitOutput - emit the output file.
125   bool emitOutput(MemoryArea& pOutput);
126 
127   /// postProcessing - do modificatiion after all processes
128   bool postProcessing(MemoryArea& pOutput);
129 
130   /// getLinker - get internal FragmentLinker object
getLinker()131   const FragmentLinker* getLinker() const { return m_pLinker; }
getLinker()132   FragmentLinker*       getLinker()       { return m_pLinker; }
133 
134   /// hasInitLinker - has Linker been initialized?
hasInitLinker()135   bool hasInitLinker() const
136   { return (NULL != m_pLinker); }
137 
138   // -----  readers and writers  ----- //
getObjectReader()139   const ObjectReader*  getObjectReader () const { return m_pObjectReader;  }
getObjectReader()140   ObjectReader*        getObjectReader ()       { return m_pObjectReader;  }
141 
getDynObjReader()142   const DynObjReader*  getDynObjReader () const { return m_pDynObjReader;  }
getDynObjReader()143   DynObjReader*        getDynObjReader ()       { return m_pDynObjReader;  }
144 
getArchiveReader()145   const ArchiveReader* getArchiveReader() const { return m_pArchiveReader; }
getArchiveReader()146   ArchiveReader*       getArchiveReader()       { return m_pArchiveReader; }
147 
getGroupReader()148   const GroupReader*   getGroupReader  () const { return m_pGroupReader;   }
getGroupReader()149   GroupReader*         getGroupReader  ()       { return m_pGroupReader;   }
150 
getBinaryReader()151   const BinaryReader*  getBinaryReader () const { return m_pBinaryReader;  }
getBinaryReader()152   BinaryReader*        getBinaryReader ()       { return m_pBinaryReader;  }
153 
getWriter()154   const ObjectWriter*  getWriter () const { return m_pWriter;  }
getWriter()155   ObjectWriter*        getWriter ()       { return m_pWriter;  }
156 
157 private:
158   const LinkerConfig& m_Config;
159   FragmentLinker* m_pLinker;
160   Module* m_pModule;
161   IRBuilder* m_pBuilder;
162 
163   TargetLDBackend &m_LDBackend;
164 
165   // -----  readers and writers  ----- //
166   ObjectReader*  m_pObjectReader;
167   DynObjReader*  m_pDynObjReader;
168   ArchiveReader* m_pArchiveReader;
169   GroupReader*   m_pGroupReader;
170   BinaryReader*  m_pBinaryReader;
171   ObjectWriter*  m_pWriter;
172 };
173 
174 } // end namespace mcld
175 #endif
176