ObjectLinker.h revision 87f34658dec9097d987d254a990ea7f311bfc95f
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#ifdef ENABLE_UNITTEST 12#include <gtest.h> 13#endif 14#include <llvm/Support/DataTypes.h> 15 16namespace mcld { 17 18class Module; 19class LinkerConfig; 20class IRBuilder; 21class TargetLDBackend; 22class FileOutputBuffer; 23class ObjectReader; 24class DynObjReader; 25class ArchiveReader; 26class GroupReader; 27class BinaryReader; 28class ScriptReader; 29class ObjectWriter; 30class DynObjWriter; 31class ExecWriter; 32class BinaryWriter; 33class Relocation; 34class ResolveInfo; 35 36/** \class ObjectLinker 37 */ 38class ObjectLinker 39{ 40public: 41 ObjectLinker(const LinkerConfig& pConfig, 42 TargetLDBackend& pLDBackend); 43 44 ~ObjectLinker(); 45 46 bool initialize(Module& pModule, IRBuilder& pBuilder); 47 48 /// initStdSections - initialize standard sections of the output file. 49 bool initStdSections(); 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 ----- // 129 const ObjectReader* getObjectReader () const { return m_pObjectReader; } 130 ObjectReader* getObjectReader () { return m_pObjectReader; } 131 132 const DynObjReader* getDynObjReader () const { return m_pDynObjReader; } 133 DynObjReader* getDynObjReader () { return m_pDynObjReader; } 134 135 const ArchiveReader* getArchiveReader() const { return m_pArchiveReader; } 136 ArchiveReader* getArchiveReader() { return m_pArchiveReader; } 137 138 const GroupReader* getGroupReader () const { return m_pGroupReader; } 139 GroupReader* getGroupReader () { return m_pGroupReader; } 140 141 const BinaryReader* getBinaryReader () const { return m_pBinaryReader; } 142 BinaryReader* getBinaryReader () { return m_pBinaryReader; } 143 144 const ScriptReader* getScriptReader () const { return m_pScriptReader; } 145 ScriptReader* getScriptReader () { return m_pScriptReader; } 146 147 const ObjectWriter* getWriter () const { return m_pWriter; } 148 ObjectWriter* getWriter () { return m_pWriter; } 149 150private: 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 167private: 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