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