ObjectLinker.h revision d0fbbb227051be16931a1aa9b4a7722ac039c698
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
21namespace mcld {
22
23class Module;
24class LinkerConfig;
25class IRBuilder;
26class FragmentLinker;
27class TargetLDBackend;
28class MemoryArea;
29class MemoryAreaFactory;
30class ObjectReader;
31class DynObjReader;
32class ArchiveReader;
33class GroupReader;
34class BinaryReader;
35class ObjectWriter;
36class DynObjWriter;
37class ExecWriter;
38class BinaryWriter;
39
40/** \class ObjectLinker
41 *  \brief ObjectLinker prepares parameters for FragmentLinker.
42 */
43class ObjectLinker
44{
45public:
46  ObjectLinker(const LinkerConfig& pConfig,
47               Module& pModule,
48               IRBuilder& pBuilder,
49               TargetLDBackend& pLDBackend);
50
51  ~ObjectLinker();
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  /// addStandardSymbols - shared object and executable files need some
76  /// standard symbols
77  ///   @return if there are some input symbols with the same name to the
78  ///   standard symbols, return false
79  bool addStandardSymbols();
80
81  /// addTargetSymbols - some targets, such as MIPS and ARM, need some
82  /// target-dependent symbols
83  ///   @return if there are some input symbols with the same name to the
84  ///   target symbols, return false
85  bool addTargetSymbols();
86
87  /// scanRelocations - scan all relocation entries by output symbols.
88  bool scanRelocations();
89
90  /// prelayout - help backend to do some modification before layout
91  bool prelayout();
92
93  /// layout - linearly layout all output sections and reserve some space
94  /// for GOT/PLT
95  ///   Because we do not support instruction relaxing in this early version,
96  ///   if there is a branch can not jump to its target, we return false
97  ///   directly
98  bool layout();
99
100  /// postlayout - help backend to do some modification after layout
101  bool postlayout();
102
103  /// relocate - applying relocation entries and create relocation
104  /// section in the output files
105  /// Create relocation section, asking TargetLDBackend to
106  /// read the relocation information into RelocationEntry
107  /// and push_back into the relocation section
108  bool relocation();
109
110  /// finalizeSymbolValue - finalize the symbol value
111  bool finalizeSymbolValue();
112
113  /// emitOutput - emit the output file.
114  bool emitOutput(MemoryArea& pOutput);
115
116  /// postProcessing - do modificatiion after all processes
117  bool postProcessing(MemoryArea& pOutput);
118
119  /// getLinker - get internal FragmentLinker object
120  const FragmentLinker* getLinker() const { return m_pLinker; }
121  FragmentLinker*       getLinker()       { return m_pLinker; }
122
123  /// hasInitLinker - has Linker been initialized?
124  bool hasInitLinker() const
125  { return (NULL != m_pLinker); }
126
127  // -----  readers and writers  ----- //
128  const ObjectReader*  getObjectReader () const { return m_pObjectReader;  }
129  ObjectReader*        getObjectReader ()       { return m_pObjectReader;  }
130
131  const DynObjReader*  getDynObjReader () const { return m_pDynObjReader;  }
132  DynObjReader*        getDynObjReader ()       { return m_pDynObjReader;  }
133
134  const ArchiveReader* getArchiveReader() const { return m_pArchiveReader; }
135  ArchiveReader*       getArchiveReader()       { return m_pArchiveReader; }
136
137  const GroupReader*   getGroupReader  () const { return m_pGroupReader;   }
138  GroupReader*         getGroupReader  ()       { return m_pGroupReader;   }
139
140  const BinaryReader*  getBinaryReader () const { return m_pBinaryReader;  }
141  BinaryReader*        getBinaryReader ()       { return m_pBinaryReader;  }
142
143  const ObjectWriter*  getObjectWriter () const { return m_pObjectWriter;  }
144  ObjectWriter*        getObjectWriter ()       { return m_pObjectWriter;  }
145
146  const DynObjWriter*  getDynObjWriter () const { return m_pDynObjWriter;  }
147  DynObjWriter*        getDynObjWriter ()       { return m_pDynObjWriter;  }
148
149  const ExecWriter*    getExecWriter   () const { return m_pExecWriter;    }
150  ExecWriter*          getExecWriter   ()       { return m_pExecWriter;    }
151
152  const BinaryWriter*  getBinaryWriter () const { return m_pBinaryWriter;  }
153  BinaryWriter*        getBinaryWriter ()       { return m_pBinaryWriter;  }
154
155private:
156  const LinkerConfig& m_Config;
157  Module& m_Module;
158
159  IRBuilder& m_Builder;
160
161  FragmentLinker* m_pLinker;
162  TargetLDBackend &m_LDBackend;
163
164  // -----  readers and writers  ----- //
165  ObjectReader*  m_pObjectReader;
166  DynObjReader*  m_pDynObjReader;
167  ArchiveReader* m_pArchiveReader;
168  GroupReader*   m_pGroupReader;
169  BinaryReader*  m_pBinaryReader;
170  ObjectWriter*  m_pObjectWriter;
171  DynObjWriter*  m_pDynObjWriter;
172  ExecWriter*    m_pExecWriter;
173  BinaryWriter*  m_pBinaryWriter;
174};
175
176} // end namespace mcld
177#endif
178