ObjectLinker.cpp revision d0fbbb227051be16931a1aa9b4a7722ac039c698
194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng//===- ObjectLinker.cpp ---------------------------------------------------===// 294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng// 394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng// The MCLinker Project 494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng// 594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng// This file is distributed under the University of Illinois Open Source 694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng// License. See LICENSE.TXT for details. 794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng// 894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng//===----------------------------------------------------------------------===// 994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/Object/ObjectLinker.h> 1094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng 1194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LinkerConfig.h> 1294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/Module.h> 1394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/InputTree.h> 1494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/IRBuilder.h> 1594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/LDSection.h> 1694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/LDContext.h> 1794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/Archive.h> 1894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/ArchiveReader.h> 1994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/ObjectReader.h> 2094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/DynObjReader.h> 2194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/GroupReader.h> 2294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/BinaryReader.h> 2394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/ObjectWriter.h> 2494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/DynObjWriter.h> 2594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/ExecWriter.h> 2694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/BinaryWriter.h> 2794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/ResolveInfo.h> 2894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/LD/RelocData.h> 2994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/Support/RealPath.h> 3094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/Support/MemoryArea.h> 3194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/Support/MsgHandling.h> 3294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/Target/TargetLDBackend.h> 3394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/Fragment/FragmentLinker.h> 3494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <mcld/Object/ObjectBuilder.h> 3594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng 3694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng#include <llvm/Support/Casting.h> 3794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng 3894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengusing namespace llvm; 3994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengusing namespace mcld; 4094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng 4194b10b530c0fc297e2974e57e094c500d3ee6003Chiao ChengObjectLinker::ObjectLinker(const LinkerConfig& pConfig, 4294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng Module& pModule, 4394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng IRBuilder& pBuilder, 4494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng TargetLDBackend& pLDBackend) 4594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng : m_Config(pConfig), 4694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng m_Module(pModule), 4794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng m_Builder(pBuilder), 4894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng m_pLinker(NULL), 4994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng m_LDBackend(pLDBackend), 5094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng m_pObjectReader(NULL), 51 m_pDynObjReader(NULL), 52 m_pArchiveReader(NULL), 53 m_pGroupReader(NULL), 54 m_pBinaryReader(NULL), 55 m_pObjectWriter(NULL), 56 m_pDynObjWriter(NULL), 57 m_pExecWriter(NULL), 58 m_pBinaryWriter(NULL) 59{ 60 // set up soname 61 if (!m_Config.options().soname().empty()) { 62 m_Module.setName(m_Config.options().soname()); 63 } 64} 65 66ObjectLinker::~ObjectLinker() 67{ 68 delete m_pLinker; 69 delete m_pObjectReader; 70 delete m_pDynObjReader; 71 delete m_pArchiveReader; 72 delete m_pGroupReader; 73 delete m_pBinaryReader; 74 delete m_pObjectWriter; 75 delete m_pDynObjWriter; 76 delete m_pExecWriter; 77 delete m_pBinaryWriter; 78} 79 80/// initFragmentLinker - initialize FragmentLinker 81/// Connect all components with FragmentLinker 82bool ObjectLinker::initFragmentLinker() 83{ 84 if (NULL == m_pLinker) { 85 m_pLinker = new FragmentLinker(m_Config, 86 m_Module, 87 m_LDBackend); 88 } 89 90 // initialize the readers and writers 91 // Because constructor can not be failed, we initalize all readers and 92 // writers outside the FragmentLinker constructors. 93 m_pObjectReader = m_LDBackend.createObjectReader(m_Builder); 94 m_pArchiveReader = m_LDBackend.createArchiveReader(m_Module); 95 m_pDynObjReader = m_LDBackend.createDynObjReader(m_Builder); 96 m_pGroupReader = new GroupReader(m_Module, *m_pObjectReader, 97 *m_pDynObjReader, *m_pArchiveReader); 98 m_pBinaryReader = m_LDBackend.createBinaryReader(m_Builder); 99 m_pObjectWriter = m_LDBackend.createObjectWriter(); 100 m_pDynObjWriter = m_LDBackend.createDynObjWriter(); 101 m_pExecWriter = m_LDBackend.createExecWriter(); 102 m_pBinaryWriter = m_LDBackend.createBinaryWriter(); 103 104 // initialize Relocator 105 m_LDBackend.initRelocator(*m_pLinker); 106 107 // initialize BranchIslandFactory 108 m_LDBackend.initBRIslandFactory(); 109 110 // initialize StubFactory 111 m_LDBackend.initStubFactory(); 112 113 // initialize target stubs 114 m_LDBackend.initTargetStubs(*m_pLinker); 115 return true; 116} 117 118/// initStdSections - initialize standard sections 119bool ObjectLinker::initStdSections() 120{ 121 ObjectBuilder builder(m_Config, m_Module); 122 123 // initialize standard sections 124 if (!m_LDBackend.initStdSections(builder)) 125 return false; 126 127 // initialize target-dependent sections 128 m_LDBackend.initTargetSections(m_Module, builder); 129 130 return true; 131} 132 133void ObjectLinker::normalize() 134{ 135 // ----- set up inputs ----- // 136 Module::input_iterator input, inEnd = m_Module.input_end(); 137 for (input = m_Module.input_begin(); input!=inEnd; ++input) { 138 // is a group node 139 if (isGroup(input)) { 140 getGroupReader()->readGroup(input, m_Builder.getInputBuilder(), m_Config); 141 continue; 142 } 143 144 // already got type - for example, bitcode or external OIR (object 145 // intermediate representation) 146 if ((*input)->type() == Input::Script || 147 (*input)->type() == Input::Archive || 148 (*input)->type() == Input::External) 149 continue; 150 151 if (Input::Object == (*input)->type()) { 152 m_Module.getObjectList().push_back(*input); 153 continue; 154 } 155 156 if (Input::DynObj == (*input)->type()) { 157 m_Module.getLibraryList().push_back(*input); 158 continue; 159 } 160 161 // read input as a binary file 162 if (m_Config.options().isBinaryInput()) { 163 (*input)->setType(Input::Object); 164 getBinaryReader()->readBinary(**input); 165 m_Module.getObjectList().push_back(*input); 166 } 167 // is a relocatable object file 168 else if (getObjectReader()->isMyFormat(**input)) { 169 (*input)->setType(Input::Object); 170 getObjectReader()->readHeader(**input); 171 getObjectReader()->readSections(**input); 172 getObjectReader()->readSymbols(**input); 173 m_Module.getObjectList().push_back(*input); 174 } 175 // is a shared object file 176 else if (getDynObjReader()->isMyFormat(**input)) { 177 (*input)->setType(Input::DynObj); 178 getDynObjReader()->readHeader(**input); 179 getDynObjReader()->readSymbols(**input); 180 m_Module.getLibraryList().push_back(*input); 181 } 182 // is an archive 183 else if (getArchiveReader()->isMyFormat(**input)) { 184 (*input)->setType(Input::Archive); 185 Archive archive(**input, m_Builder.getInputBuilder()); 186 getArchiveReader()->readArchive(archive); 187 if(archive.numOfObjectMember() > 0) { 188 m_Module.getInputTree().merge<InputTree::Inclusive>(input, 189 archive.inputs()); 190 } 191 } 192 else { 193 fatal(diag::err_unrecognized_input_file) << (*input)->path() 194 << m_Config.targets().triple().str(); 195 } 196 } // end of for 197} 198 199bool ObjectLinker::linkable() const 200{ 201 // check we have input and output files 202 if (m_Module.getInputTree().empty()) { 203 error(diag::err_no_inputs); 204 return false; 205 } 206 207 // can not mix -static with shared objects 208 Module::const_lib_iterator lib, libEnd = m_Module.lib_end(); 209 for (lib = m_Module.lib_begin(); lib != libEnd; ++lib) { 210 if((*lib)->attribute()->isStatic()) { 211 error(diag::err_mixed_shared_static_objects) 212 << (*lib)->name() << (*lib)->path(); 213 return false; 214 } 215 } 216 217 // can not mix -r with shared objects 218 return true; 219} 220 221/// readRelocations - read all relocation entries 222/// 223/// All symbols should be read and resolved before this function. 224bool ObjectLinker::readRelocations() 225{ 226 // Bitcode is read by the other path. This function reads relocation sections 227 // in object files. 228 mcld::InputTree::bfs_iterator input, inEnd = m_Module.getInputTree().bfs_end(); 229 for (input=m_Module.getInputTree().bfs_begin(); input!=inEnd; ++input) { 230 if ((*input)->type() == Input::Object && (*input)->hasMemArea()) { 231 if (!getObjectReader()->readRelocations(**input)) 232 return false; 233 } 234 // ignore the other kinds of files. 235 } 236 return true; 237} 238 239/// mergeSections - put allinput sections into output sections 240bool ObjectLinker::mergeSections() 241{ 242 ObjectBuilder builder(m_Config, m_Module); 243 Module::obj_iterator obj, objEnd = m_Module.obj_end(); 244 for (obj = m_Module.obj_begin(); obj != objEnd; ++obj) { 245 LDContext::sect_iterator sect, sectEnd = (*obj)->context()->sectEnd(); 246 for (sect = (*obj)->context()->sectBegin(); sect != sectEnd; ++sect) { 247 switch ((*sect)->kind()) { 248 // Some *INPUT sections should not be merged. 249 case LDFileFormat::Ignore: 250 case LDFileFormat::Null: 251 case LDFileFormat::Relocation: 252 case LDFileFormat::NamePool: 253 case LDFileFormat::Group: 254 case LDFileFormat::StackNote: 255 // skip 256 continue; 257 case LDFileFormat::Target: 258 if (!m_LDBackend.mergeSection(m_Module, **sect)) { 259 error(diag::err_cannot_merge_section) << (*sect)->name() 260 << (*obj)->name(); 261 return false; 262 } 263 break; 264 case LDFileFormat::EhFrame: { 265 if (!(*sect)->hasEhFrame()) 266 continue; // skip 267 268 if (!builder.MergeSection(**sect)) { 269 error(diag::err_cannot_merge_section) << (*sect)->name() 270 << (*obj)->name(); 271 return false; 272 } 273 break; 274 } 275 default: { 276 if (!(*sect)->hasSectionData()) 277 continue; // skip 278 279 if (!builder.MergeSection(**sect)) { 280 error(diag::err_cannot_merge_section) << (*sect)->name() 281 << (*obj)->name(); 282 return false; 283 } 284 break; 285 } 286 } // end of switch 287 } // for each section 288 } // for each obj 289 return true; 290} 291 292/// addStandardSymbols - shared object and executable files need some 293/// standard symbols 294/// @return if there are some input symbols with the same name to the 295/// standard symbols, return false 296bool ObjectLinker::addStandardSymbols() 297{ 298 // create and add section symbols for each output section 299 Module::iterator iter, iterEnd = m_Module.end(); 300 for (iter = m_Module.begin(); iter != iterEnd; ++iter) { 301 m_Module.getSectionSymbolSet().add(**iter, m_Module.getNamePool()); 302 } 303 304 return m_LDBackend.initStandardSymbols(*m_pLinker, m_Module); 305} 306 307/// addTargetSymbols - some targets, such as MIPS and ARM, need some 308/// target-dependent symbols 309/// @return if there are some input symbols with the same name to the 310/// target symbols, return false 311bool ObjectLinker::addTargetSymbols() 312{ 313 m_LDBackend.initTargetSymbols(*m_pLinker); 314 return true; 315} 316 317bool ObjectLinker::scanRelocations() 318{ 319 // apply all relocations of all inputs 320 Module::obj_iterator input, inEnd = m_Module.obj_end(); 321 for (input = m_Module.obj_begin(); input != inEnd; ++input) { 322 LDContext::sect_iterator rs, rsEnd = (*input)->context()->relocSectEnd(); 323 for (rs = (*input)->context()->relocSectBegin(); rs != rsEnd; ++rs) { 324 // bypass the reloc section if 325 // 1. its section kind is changed to Ignore. (The target section is a 326 // discarded group section.) 327 // 2. it has no reloc data. (All symbols in the input relocs are in the 328 // discarded group sections) 329 if (LDFileFormat::Ignore == (*rs)->kind() || !(*rs)->hasRelocData()) 330 continue; 331 RelocData::iterator reloc, rEnd = (*rs)->getRelocData()->end(); 332 for (reloc = (*rs)->getRelocData()->begin(); reloc != rEnd; ++reloc) { 333 Relocation* relocation = llvm::cast<Relocation>(reloc); 334 // scan relocation 335 if (LinkerConfig::Object != m_Config.codeGenType()) { 336 m_LDBackend.scanRelocation(*relocation, 337 *m_pLinker, 338 m_Module, 339 *(*rs)->getLink()); 340 } 341 else { 342 m_LDBackend.partialScanRelocation(*relocation, 343 *m_pLinker, 344 m_Module, 345 *(*rs)->getLink()); 346 } 347 } // for all relocations 348 } // for all relocation section 349 } // for all inputs 350 return true; 351} 352 353/// prelayout - help backend to do some modification before layout 354bool ObjectLinker::prelayout() 355{ 356 // finalize the section symbols, set their fragment reference and push them 357 // into output symbol table 358 Module::iterator sect, sEnd = m_Module.end(); 359 for (sect = m_Module.begin(); sect != sEnd; ++sect) { 360 m_Module.getSectionSymbolSet().finalize(**sect, m_Module.getSymbolTable()); 361 } 362 363 m_LDBackend.preLayout(m_Module, *m_pLinker); 364 365 if (LinkerConfig::Object != m_Config.codeGenType() || 366 m_Config.options().isDefineCommon()) 367 m_LDBackend.allocateCommonSymbols(m_Module); 368 369 /// check program interpreter - computer the name size of the runtime dyld 370 if (!m_pLinker->isStaticLink() && 371 (LinkerConfig::Exec == m_Config.codeGenType() || 372 m_Config.options().isPIE() || 373 m_Config.options().hasDyld())) 374 m_LDBackend.sizeInterp(); 375 376 /// measure NamePools - compute the size of name pool sections 377 /// In ELF, will compute the size of.symtab, .strtab, .dynsym, .dynstr, 378 /// .hash and .shstrtab sections. 379 /// 380 /// dump all symbols and strings from FragmentLinker and build the format-dependent 381 /// hash table. 382 m_LDBackend.sizeNamePools(m_Module, m_pLinker->isStaticLink()); 383 384 return true; 385} 386 387/// layout - linearly layout all output sections and reserve some space 388/// for GOT/PLT 389/// Because we do not support instruction relaxing in this early version, 390/// if there is a branch can not jump to its target, we return false 391/// directly 392bool ObjectLinker::layout() 393{ 394 m_LDBackend.layout(m_Module, *m_pLinker); 395 return true; 396} 397 398/// prelayout - help backend to do some modification after layout 399bool ObjectLinker::postlayout() 400{ 401 m_LDBackend.postLayout(m_Module, *m_pLinker); 402 return true; 403} 404 405/// finalizeSymbolValue - finalize the resolved symbol value. 406/// Before relocate(), after layout(), FragmentLinker should correct value of all 407/// symbol. 408bool ObjectLinker::finalizeSymbolValue() 409{ 410 return m_pLinker->finalizeSymbols(); 411} 412 413/// relocate - applying relocation entries and create relocation 414/// section in the output files 415/// Create relocation section, asking TargetLDBackend to 416/// read the relocation information into RelocationEntry 417/// and push_back into the relocation section 418bool ObjectLinker::relocation() 419{ 420 return m_pLinker->applyRelocations(); 421} 422 423/// emitOutput - emit the output file. 424bool ObjectLinker::emitOutput(MemoryArea& pOutput) 425{ 426 switch(m_Config.codeGenType()) { 427 case LinkerConfig::Object: 428 getObjectWriter()->writeObject(m_Module, pOutput); 429 return true; 430 case LinkerConfig::DynObj: 431 getDynObjWriter()->writeDynObj(m_Module, pOutput); 432 return true; 433 case LinkerConfig::Exec: 434 getExecWriter()->writeExecutable(m_Module, pOutput); 435 return true; 436 case LinkerConfig::Binary: 437 getBinaryWriter()->writeBinary(m_Module, pOutput); 438 return true; 439 default: 440 fatal(diag::unrecognized_output_file) << m_Config.codeGenType(); 441 } 442 return false; 443} 444 445/// postProcessing - do modification after all processes 446bool ObjectLinker::postProcessing(MemoryArea& pOutput) 447{ 448 m_pLinker->syncRelocationResult(pOutput); 449 450 // emit .eh_frame_hdr 451 // eh_frame_hdr should be emitted after syncRelocation, because eh_frame_hdr 452 // needs FDE PC value, which will be corrected at syncRelocation 453 m_LDBackend.postProcessing(*m_pLinker, pOutput); 454 return true; 455} 456 457