ObjectLinker.h revision 533eae20118036f425f27bf0536ef0ccbb090b65
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#ifndef MCLD_OBJECT_OBJECTLINKER_H
10#define MCLD_OBJECT_OBJECTLINKER_H
11#include <llvm/Support/DataTypes.h>
12
13namespace mcld {
14
15class Module;
16class LinkerConfig;
17class IRBuilder;
18class TargetLDBackend;
19class FileOutputBuffer;
20class ObjectReader;
21class DynObjReader;
22class ArchiveReader;
23class GroupReader;
24class BinaryReader;
25class ScriptReader;
26class ObjectWriter;
27class DynObjWriter;
28class ExecWriter;
29class BinaryWriter;
30class Relocation;
31class ResolveInfo;
32
33/** \class ObjectLinker
34 */
35class ObjectLinker
36{
37public:
38  ObjectLinker(const LinkerConfig& pConfig,
39               TargetLDBackend& pLDBackend);
40
41  ~ObjectLinker();
42
43  bool initialize(Module& pModule, IRBuilder& pBuilder);
44
45  /// initStdSections - initialize standard sections of the output file.
46  bool initStdSections();
47
48  /// normalize - normalize the input files
49  void normalize();
50
51  /// linkable - check the linkability of current LinkerConfig
52  ///  Check list:
53  ///  - check the Attributes are not violate the constaint
54  ///  - check every Input has a correct Attribute
55  bool linkable() const;
56
57  /// readRelocations - read all relocation entries
58  bool readRelocations();
59
60  /// dataStrippingOpt - optimizations for reducing code size
61  void dataStrippingOpt();
62
63  /// mergeSections - put allinput sections into output sections
64  bool mergeSections();
65
66  /// addSymbolsToOutput - after all symbols has been resolved, add the symbol
67  /// to output
68  void addSymbolsToOutput(Module& pModule);
69
70  /// allocateCommonSymobols - allocate fragments for common symbols to the
71  /// corresponding sections
72  bool allocateCommonSymbols();
73
74  /// addStandardSymbols - shared object and executable files need some
75  /// standard symbols
76  ///   @return if there are some input symbols with the same name to the
77  ///   standard symbols, return false
78  bool addStandardSymbols();
79
80  /// addTargetSymbols - some targets, such as MIPS and ARM, need some
81  /// target-dependent symbols
82  ///   @return if there are some input symbols with the same name to the
83  ///   target symbols, return false
84  bool addTargetSymbols();
85
86  /// addScriptSymbols - define symbols from the command line option or linker
87  /// scripts.
88  bool addScriptSymbols();
89
90  /// scanRelocations - scan all relocation entries by output symbols.
91  bool scanRelocations();
92
93  /// initStubs - initialize stub-related stuff.
94  bool initStubs();
95
96  /// prelayout - help backend to do some modification before layout
97  bool prelayout();
98
99  /// layout - linearly layout all output sections and reserve some space
100  /// for GOT/PLT
101  ///   Because we do not support instruction relaxing in this early version,
102  ///   if there is a branch can not jump to its target, we return false
103  ///   directly
104  bool layout();
105
106  /// postlayout - help backend to do some modification after layout
107  bool postlayout();
108
109  /// relocate - applying relocation entries and create relocation
110  /// section in the output files
111  /// Create relocation section, asking TargetLDBackend to
112  /// read the relocation information into RelocationEntry
113  /// and push_back into the relocation section
114  bool relocation();
115
116  /// finalizeSymbolValue - finalize the symbol value
117  bool finalizeSymbolValue();
118
119  /// emitOutput - emit the output file.
120  bool emitOutput(FileOutputBuffer& pOutput);
121
122  /// postProcessing - do modificatiion after all processes
123  bool postProcessing(FileOutputBuffer& pOutput);
124
125  // -----  readers and writers  ----- //
126  const ObjectReader*  getObjectReader () const { return m_pObjectReader;  }
127  ObjectReader*        getObjectReader ()       { return m_pObjectReader;  }
128
129  const DynObjReader*  getDynObjReader () const { return m_pDynObjReader;  }
130  DynObjReader*        getDynObjReader ()       { return m_pDynObjReader;  }
131
132  const ArchiveReader* getArchiveReader() const { return m_pArchiveReader; }
133  ArchiveReader*       getArchiveReader()       { return m_pArchiveReader; }
134
135  const GroupReader*   getGroupReader  () const { return m_pGroupReader;   }
136  GroupReader*         getGroupReader  ()       { return m_pGroupReader;   }
137
138  const BinaryReader*  getBinaryReader () const { return m_pBinaryReader;  }
139  BinaryReader*        getBinaryReader ()       { return m_pBinaryReader;  }
140
141  const ScriptReader*  getScriptReader () const { return m_pScriptReader;  }
142  ScriptReader*        getScriptReader ()       { return m_pScriptReader;  }
143
144  const ObjectWriter*  getWriter () const { return m_pWriter;  }
145  ObjectWriter*        getWriter ()       { return m_pWriter;  }
146
147private:
148  /// normalSyncRelocationResult - sync relocation result when producing shared
149  /// objects or executables
150  void normalSyncRelocationResult(FileOutputBuffer& pOutput);
151
152  /// partialSyncRelocationResult - sync relocation result when doing partial
153  /// link
154  void partialSyncRelocationResult(FileOutputBuffer& pOutput);
155
156  /// writeRelocationResult - helper function of syncRelocationResult, write
157  /// relocation target data to output
158  void writeRelocationResult(Relocation& pReloc, uint8_t* pOutput);
159
160  /// addSymbolToOutput - add a symbol to output symbol table if it's not a
161  /// section symbol and not defined in the discarded section
162  void addSymbolToOutput(ResolveInfo& pInfo, Module& pModule);
163
164private:
165  const LinkerConfig& m_Config;
166  Module* m_pModule;
167  IRBuilder* m_pBuilder;
168
169  TargetLDBackend &m_LDBackend;
170
171  // -----  readers and writers  ----- //
172  ObjectReader*  m_pObjectReader;
173  DynObjReader*  m_pDynObjReader;
174  ArchiveReader* m_pArchiveReader;
175  GroupReader*   m_pGroupReader;
176  BinaryReader*  m_pBinaryReader;
177  ScriptReader*  m_pScriptReader;
178  ObjectWriter*  m_pWriter;
179};
180
181} // end namespace mcld
182#endif
183