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