MipsLDBackend.cpp revision 6f75755c9204b1d8817ae5a65a2f7e5af0ec3f70
1//===- MipsLDBackend.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 "Mips.h" 10#include "MipsGNUInfo.h" 11#include "MipsELFDynamic.h" 12#include "MipsLDBackend.h" 13#include "MipsRelocator.h" 14 15#include <llvm/ADT/Triple.h> 16#include <llvm/Support/ELF.h> 17 18#include <mcld/Module.h> 19#include <mcld/LinkerConfig.h> 20#include <mcld/IRBuilder.h> 21#include <mcld/MC/Attribute.h> 22#include <mcld/Fragment/FillFragment.h> 23#include <mcld/Support/MemoryRegion.h> 24#include <mcld/Support/MemoryArea.h> 25#include <mcld/Support/MsgHandling.h> 26#include <mcld/Support/TargetRegistry.h> 27#include <mcld/Target/OutputRelocSection.h> 28#include <mcld/Object/ObjectBuilder.h> 29 30using namespace mcld; 31 32//===----------------------------------------------------------------------===// 33// MipsGNULDBackend 34//===----------------------------------------------------------------------===// 35MipsGNULDBackend::MipsGNULDBackend(const LinkerConfig& pConfig, 36 MipsGNUInfo* pInfo) 37 : GNULDBackend(pConfig, pInfo), 38 m_pRelocator(NULL), 39 m_pGOT(NULL), 40 m_pRelDyn(NULL), 41 m_pDynamic(NULL), 42 m_pGOTSymbol(NULL), 43 m_pGpDispSymbol(NULL) 44{ 45} 46 47MipsGNULDBackend::~MipsGNULDBackend() 48{ 49 delete m_pRelocator; 50 delete m_pGOT; 51 delete m_pRelDyn; 52 delete m_pDynamic; 53} 54 55void MipsGNULDBackend::initTargetSections(Module& pModule, ObjectBuilder& pBuilder) 56{ 57 if (LinkerConfig::Object != config().codeGenType()) { 58 ELFFileFormat* file_format = getOutputFormat(); 59 60 // initialize .got 61 LDSection& got = file_format->getGOT(); 62 m_pGOT = new MipsGOT(got); 63 64 // initialize .rel.dyn 65 LDSection& reldyn = file_format->getRelDyn(); 66 m_pRelDyn = new OutputRelocSection(pModule, reldyn); 67 } 68} 69 70void MipsGNULDBackend::initTargetSymbols(IRBuilder& pBuilder, Module& pModule) 71{ 72 // Define the symbol _GLOBAL_OFFSET_TABLE_ if there is a symbol with the 73 // same name in input 74 m_pGOTSymbol = pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>( 75 "_GLOBAL_OFFSET_TABLE_", 76 ResolveInfo::Object, 77 ResolveInfo::Define, 78 ResolveInfo::Local, 79 0x0, // size 80 0x0, // value 81 FragmentRef::Null(), // FragRef 82 ResolveInfo::Hidden); 83 84 m_pGpDispSymbol = pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>( 85 "_gp_disp", 86 ResolveInfo::Section, 87 ResolveInfo::Define, 88 ResolveInfo::Absolute, 89 0x0, // size 90 0x0, // value 91 FragmentRef::Null(), // FragRef 92 ResolveInfo::Default); 93 94 if (NULL != m_pGpDispSymbol) { 95 m_pGpDispSymbol->resolveInfo()->setReserved(ReserveGpDisp); 96 } 97} 98 99bool MipsGNULDBackend::initRelocator() 100{ 101 if (NULL == m_pRelocator) { 102 m_pRelocator = new MipsRelocator(*this); 103 } 104 return true; 105} 106 107Relocator* MipsGNULDBackend::getRelocator() 108{ 109 assert(NULL != m_pRelocator); 110 return m_pRelocator; 111} 112 113void MipsGNULDBackend::scanRelocation(Relocation& pReloc, 114 IRBuilder& pBuilder, 115 Module& pModule, 116 LDSection& pSection) 117{ 118 // rsym - The relocation target symbol 119 ResolveInfo* rsym = pReloc.symInfo(); 120 assert(NULL != rsym && "ResolveInfo of relocation not set while scanRelocation"); 121 122 // Skip relocation against _gp_disp 123 if (NULL != m_pGpDispSymbol) { 124 if (pReloc.symInfo() == m_pGpDispSymbol->resolveInfo()) 125 return; 126 } 127 128 pReloc.updateAddend(); 129 130 assert(NULL != pSection.getLink()); 131 if (0 == (pSection.getLink()->flag() & llvm::ELF::SHF_ALLOC)) 132 return; 133 134 // We test isLocal or if pInputSym is not a dynamic symbol 135 // We assume -Bsymbolic to bind all symbols internaly via !rsym->isDyn() 136 // Don't put undef symbols into local entries. 137 if ((rsym->isLocal() || !isDynamicSymbol(*rsym) || 138 !rsym->isDyn()) && !rsym->isUndef()) 139 scanLocalReloc(pReloc, pBuilder, pSection); 140 else 141 scanGlobalReloc(pReloc, pBuilder, pSection); 142 143 // check if we shoule issue undefined reference for the relocation target 144 // symbol 145 if (rsym->isUndef() && !rsym->isDyn() && !rsym->isWeak() && !rsym->isNull()) 146 fatal(diag::undefined_reference) << rsym->name(); 147} 148 149void MipsGNULDBackend::doPreLayout(IRBuilder& pBuilder) 150{ 151 // initialize .dynamic data 152 if (!config().isCodeStatic() && NULL == m_pDynamic) 153 m_pDynamic = new MipsELFDynamic(*this, config()); 154 155 // set .got size 156 // when building shared object, the .got section is must. 157 if (LinkerConfig::Object != config().codeGenType()) { 158 if (LinkerConfig::DynObj == config().codeGenType() || 159 m_pGOT->hasGOT1() || 160 NULL != m_pGOTSymbol) { 161 m_pGOT->finalizeSectionSize(); 162 defineGOTSymbol(pBuilder); 163 } 164 165 ELFFileFormat* file_format = getOutputFormat(); 166 // set .rel.dyn size 167 if (!m_pRelDyn->empty()) { 168 assert(!config().isCodeStatic() && 169 "static linkage should not result in a dynamic relocation section"); 170 file_format->getRelDyn().setSize( 171 m_pRelDyn->numOfRelocs() * getRelEntrySize()); 172 } 173 } 174} 175 176void MipsGNULDBackend::doPostLayout(Module& pModule, IRBuilder& pBuilder) 177{ 178} 179 180/// dynamic - the dynamic section of the target machine. 181/// Use co-variant return type to return its own dynamic section. 182MipsELFDynamic& MipsGNULDBackend::dynamic() 183{ 184 assert(NULL != m_pDynamic); 185 return *m_pDynamic; 186} 187 188/// dynamic - the dynamic section of the target machine. 189/// Use co-variant return type to return its own dynamic section. 190const MipsELFDynamic& MipsGNULDBackend::dynamic() const 191{ 192 assert(NULL != m_pDynamic); 193 return *m_pDynamic; 194} 195 196uint64_t MipsGNULDBackend::emitSectionData(const LDSection& pSection, 197 MemoryRegion& pRegion) const 198{ 199 assert(pRegion.size() && "Size of MemoryRegion is zero!"); 200 201 const ELFFileFormat* file_format = getOutputFormat(); 202 203 if (&pSection == &(file_format->getGOT())) { 204 assert(NULL != m_pGOT && "emitSectionData failed, m_pGOT is NULL!"); 205 uint64_t result = m_pGOT->emit(pRegion); 206 return result; 207 } 208 209 fatal(diag::unrecognized_output_sectoin) 210 << pSection.name() 211 << "mclinker@googlegroups.com"; 212 return 0; 213} 214/// isGlobalGOTSymbol - return true if the symbol is the global GOT entry. 215bool MipsGNULDBackend::isGlobalGOTSymbol(const LDSymbol& pSymbol) const 216{ 217 return std::find(m_GlobalGOTSyms.begin(), 218 m_GlobalGOTSyms.end(), &pSymbol) != m_GlobalGOTSyms.end(); 219} 220 221/// sizeNamePools - compute the size of regular name pools 222/// In ELF executable files, regular name pools are .symtab, .strtab, 223/// .dynsym, .dynstr, .hash and .shstrtab. 224void 225MipsGNULDBackend::sizeNamePools(Module& pModule, bool pIsStaticLink) 226{ 227 // number of entries in symbol tables starts from 1 to hold the special entry 228 // at index 0 (STN_UNDEF). See ELF Spec Book I, p1-21. 229 size_t symtab = 1; 230 size_t dynsym = pIsStaticLink ? 0 : 1; 231 232 // size of string tables starts from 1 to hold the null character in their 233 // first byte 234 size_t strtab = 1; 235 size_t dynstr = pIsStaticLink ? 0 : 1; 236 size_t shstrtab = 1; 237 size_t hash = 0; 238 239 // number of local symbol in the .dynsym 240 size_t symtab_local_cnt = 0; 241 size_t dynsym_local_cnt = 0; 242 243 const Module::SymbolTable& symbols = pModule.getSymbolTable(); 244 Module::const_sym_iterator symbol, symEnd; 245 /// Compute the size of .symtab, .strtab, and symtab_local_cnt 246 /// @{ 247 symEnd = symbols.end(); 248 for (symbol = symbols.begin(); symbol != symEnd; ++symbol) { 249 ++symtab; 250 if (ResolveInfo::Section != (*symbol)->type() || 251 *symbol == m_pGpDispSymbol) 252 strtab += (*symbol)->nameSize() + 1; 253 } 254 symtab_local_cnt = 1 + symbols.numOfFiles() + symbols.numOfLocals() + 255 symbols.numOfLocalDyns(); 256 /// @} 257 258 /// Compute the size of .dynsym, .dynstr, and dynsym_local_cnt 259 /// @{ 260 if (!pIsStaticLink) { 261 symEnd = symbols.dynamicEnd(); 262 for (symbol = symbols.localDynBegin(); symbol != symEnd; ++symbol) { 263 ++dynsym; 264 if (ResolveInfo::Section != (*symbol)->type() || 265 *symbol == m_pGpDispSymbol) 266 dynstr += (*symbol)->nameSize() + 1; 267 } 268 dynsym_local_cnt = 1 + symbols.numOfLocalDyns(); 269 } 270 /// @} 271 272 ELFFileFormat* file_format = getOutputFormat(); 273 274 switch(config().codeGenType()) { 275 // compute size of .dynstr and .hash 276 case LinkerConfig::DynObj: { 277 // soname 278 if (!pIsStaticLink) 279 dynstr += pModule.name().size() + 1; 280 } 281 /** fall through **/ 282 case LinkerConfig::Exec: { 283 // add DT_NEED strings into .dynstr and .dynamic 284 // Rules: 285 // 1. ignore --no-add-needed 286 // 2. force count in --no-as-needed 287 // 3. judge --as-needed 288 if (!pIsStaticLink) { 289 Module::const_lib_iterator lib, libEnd = pModule.lib_end(); 290 for (lib = pModule.lib_begin(); lib != libEnd; ++lib) { 291 // --add-needed 292 if ((*lib)->attribute()->isAddNeeded()) { 293 // --no-as-needed 294 if (!(*lib)->attribute()->isAsNeeded()) { 295 dynstr += (*lib)->name().size() + 1; 296 dynamic().reserveNeedEntry(); 297 } 298 // --as-needed 299 else if ((*lib)->isNeeded()) { 300 dynstr += (*lib)->name().size() + 1; 301 dynamic().reserveNeedEntry(); 302 } 303 } 304 } 305 306 if (!config().options().getRpathList().empty()) { 307 dynamic().reserveNeedEntry(); 308 GeneralOptions::const_rpath_iterator rpath, 309 rpathEnd = config().options().rpath_end(); 310 for (rpath = config().options().rpath_begin(); 311 rpath != rpathEnd; ++rpath) 312 dynstr += (*rpath).size() + 1; 313 } 314 315 // compute .hash 316 // Both Elf32_Word and Elf64_Word are 4 bytes 317 hash = (2 + getHashBucketCount(dynsym, false) + dynsym) * 318 sizeof(llvm::ELF::Elf32_Word); 319 } 320 321 // set size 322 if (config().targets().is32Bits()) 323 file_format->getDynSymTab().setSize(dynsym*sizeof(llvm::ELF::Elf32_Sym)); 324 else 325 file_format->getDynSymTab().setSize(dynsym*sizeof(llvm::ELF::Elf64_Sym)); 326 file_format->getDynStrTab().setSize(dynstr); 327 file_format->getHashTab().setSize(hash); 328 329 // set .dynsym sh_info to one greater than the symbol table 330 // index of the last local symbol 331 file_format->getDynSymTab().setInfo(dynsym_local_cnt); 332 } 333 /* fall through */ 334 case LinkerConfig::Object: { 335 if (config().targets().is32Bits()) 336 file_format->getSymTab().setSize(symtab*sizeof(llvm::ELF::Elf32_Sym)); 337 else 338 file_format->getSymTab().setSize(symtab*sizeof(llvm::ELF::Elf64_Sym)); 339 file_format->getStrTab().setSize(strtab); 340 341 // set .symtab sh_info to one greater than the symbol table 342 // index of the last local symbol 343 file_format->getSymTab().setInfo(symtab_local_cnt); 344 break; 345 } 346 default: { 347 fatal(diag::fatal_illegal_codegen_type) << pModule.name(); 348 break; 349 } 350 } // end of switch 351 /// @} 352 353 /// reserve fixed entries in the .dynamic section. 354 /// @{ 355 if (LinkerConfig::DynObj == config().codeGenType() || 356 LinkerConfig::Exec == config().codeGenType()) { 357 // Because some entries in .dynamic section need information of .dynsym, 358 // .dynstr, .symtab, .strtab and .hash, we can not reserve non-DT_NEEDED 359 // entries until we get the size of the sections mentioned above 360 dynamic().reserveEntries(*file_format); 361 file_format->getDynamic().setSize(dynamic().numOfBytes()); 362 } 363 /// @} 364 365 /// compute the size of .shstrtab section. 366 /// @{ 367 Module::const_iterator sect, sectEnd = pModule.end(); 368 for (sect = pModule.begin(); sect != sectEnd; ++sect) { 369 // StackNote sections will always be in output! 370 if (0 != (*sect)->size() || LDFileFormat::StackNote == (*sect)->kind()) { 371 shstrtab += ((*sect)->name().size() + 1); 372 } 373 } 374 shstrtab += (strlen(".shstrtab") + 1); 375 file_format->getShStrTab().setSize(shstrtab); 376 /// @} 377} 378 379/// emitSymbol32 - emit an ELF32 symbol 380void MipsGNULDBackend::emitSymbol32(llvm::ELF::Elf32_Sym& pSym, 381 LDSymbol& pSymbol, 382 char* pStrtab, 383 size_t pStrtabsize, 384 size_t pSymtabIdx) 385{ 386 // FIXME: check the endian between host and target 387 // write out symbol 388 if (ResolveInfo::Section != pSymbol.type() || 389 &pSymbol == m_pGpDispSymbol) { 390 pSym.st_name = pStrtabsize; 391 strcpy((pStrtab + pStrtabsize), pSymbol.name()); 392 } 393 else { 394 pSym.st_name = 0; 395 } 396 pSym.st_value = pSymbol.value(); 397 pSym.st_size = getSymbolSize(pSymbol); 398 pSym.st_info = getSymbolInfo(pSymbol); 399 pSym.st_other = pSymbol.visibility(); 400 pSym.st_shndx = getSymbolShndx(pSymbol); 401} 402 403/// emitNamePools - emit dynamic name pools - .dyntab, .dynstr, .hash 404/// 405/// the size of these tables should be computed before layout 406/// layout should computes the start offset of these tables 407void MipsGNULDBackend::emitDynNamePools(Module& pModule, MemoryArea& pOutput) 408{ 409 ELFFileFormat* file_format = getOutputFormat(); 410 if (!file_format->hasDynSymTab() || 411 !file_format->hasDynStrTab() || 412 !file_format->hasHashTab() || 413 !file_format->hasDynamic()) 414 return; 415 416 LDSection& symtab_sect = file_format->getDynSymTab(); 417 LDSection& strtab_sect = file_format->getDynStrTab(); 418 LDSection& hash_sect = file_format->getHashTab(); 419 LDSection& dyn_sect = file_format->getDynamic(); 420 421 MemoryRegion* symtab_region = pOutput.request(symtab_sect.offset(), 422 symtab_sect.size()); 423 MemoryRegion* strtab_region = pOutput.request(strtab_sect.offset(), 424 strtab_sect.size()); 425 MemoryRegion* hash_region = pOutput.request(hash_sect.offset(), 426 hash_sect.size()); 427 MemoryRegion* dyn_region = pOutput.request(dyn_sect.offset(), 428 dyn_sect.size()); 429 430 // set up symtab_region 431 llvm::ELF::Elf32_Sym* symtab32 = NULL; 432 symtab32 = (llvm::ELF::Elf32_Sym*)symtab_region->start(); 433 434 symtab32[0].st_name = 0; 435 symtab32[0].st_value = 0; 436 symtab32[0].st_size = 0; 437 symtab32[0].st_info = 0; 438 symtab32[0].st_other = 0; 439 symtab32[0].st_shndx = 0; 440 441 // set up strtab_region 442 char* strtab = (char*)strtab_region->start(); 443 strtab[0] = '\0'; 444 445 bool sym_exist = false; 446 HashTableType::entry_type* entry = 0; 447 448 // add index 0 symbol into SymIndexMap 449 entry = m_pSymIndexMap->insert(NULL, sym_exist); 450 entry->setValue(0); 451 452 size_t symtabIdx = 1; 453 size_t strtabsize = 1; 454 455 // emit .dynsym, and .dynstr (emit LocalDyn and Dynamic category) except GOT 456 // entries 457 const Module::SymbolTable& symbols = pModule.getSymbolTable(); 458 Module::const_sym_iterator symbol, symEnd = symbols.dynamicEnd(); 459 for (symbol = symbols.localDynBegin(); symbol != symEnd; ++symbol) { 460 if (isGlobalGOTSymbol(**symbol)) 461 continue; 462 emitSymbol32(symtab32[symtabIdx], **symbol, strtab, strtabsize, symtabIdx); 463 // maintain output's symbol and index map 464 entry = m_pSymIndexMap->insert(*symbol, sym_exist); 465 entry->setValue(symtabIdx); 466 // sum up counters 467 ++symtabIdx; 468 if (ResolveInfo::Section != (*symbol)->type() || *symbol == m_pGpDispSymbol) 469 strtabsize += (*symbol)->nameSize() + 1; 470 } 471 472 // emit global GOT 473 for (std::vector<LDSymbol*>::const_iterator symbol = m_GlobalGOTSyms.begin(), 474 symbol_end = m_GlobalGOTSyms.end(); 475 symbol != symbol_end; ++symbol) { 476 // Make sure this golbal GOT entry is a dynamic symbol. 477 // If not, something is wrong earlier when putting this symbol into 478 // global GOT. 479 if (!isDynamicSymbol(**symbol)) 480 fatal(diag::mips_got_symbol) << (*symbol)->name(); 481 482 emitSymbol32(symtab32[symtabIdx], **symbol, strtab, strtabsize, symtabIdx); 483 // maintain output's symbol and index map 484 entry = m_pSymIndexMap->insert(*symbol, sym_exist); 485 entry->setValue(symtabIdx); 486 // sum up counters 487 ++symtabIdx; 488 if (ResolveInfo::Section != (*symbol)->type() || *symbol == m_pGpDispSymbol) 489 strtabsize += (*symbol)->nameSize() + 1; 490 } 491 492 // emit DT_NEED 493 // add DT_NEED strings into .dynstr 494 // Rules: 495 // 1. ignore --no-add-needed 496 // 2. force count in --no-as-needed 497 // 3. judge --as-needed 498 ELFDynamic::iterator dt_need = dynamic().needBegin(); 499 Module::const_lib_iterator lib, libEnd = pModule.lib_end(); 500 for (lib = pModule.lib_begin(); lib != libEnd; ++lib) { 501 // --add-needed 502 if ((*lib)->attribute()->isAddNeeded()) { 503 // --no-as-needed 504 if (!(*lib)->attribute()->isAsNeeded()) { 505 strcpy((strtab + strtabsize), (*lib)->name().c_str()); 506 (*dt_need)->setValue(llvm::ELF::DT_NEEDED, strtabsize); 507 strtabsize += (*lib)->name().size() + 1; 508 ++dt_need; 509 } 510 // --as-needed 511 else if ((*lib)->isNeeded()) { 512 strcpy((strtab + strtabsize), (*lib)->name().c_str()); 513 (*dt_need)->setValue(llvm::ELF::DT_NEEDED, strtabsize); 514 strtabsize += (*lib)->name().size() + 1; 515 ++dt_need; 516 } 517 } 518 } // for 519 520 // emit soname 521 522 if (!config().options().getRpathList().empty()) { 523 (*dt_need)->setValue(llvm::ELF::DT_RPATH, strtabsize); 524 ++dt_need; 525 GeneralOptions::const_rpath_iterator rpath, 526 rpathEnd = config().options().rpath_end(); 527 for (rpath = config().options().rpath_begin(); rpath != rpathEnd; ++rpath) { 528 memcpy((strtab + strtabsize), (*rpath).data(), (*rpath).size()); 529 strtabsize += (*rpath).size(); 530 strtab[strtabsize++] = (rpath + 1 == rpathEnd ? '\0' : ':'); 531 } 532 } 533 534 // initialize value of ELF .dynamic section 535 if (LinkerConfig::DynObj == config().codeGenType()) 536 dynamic().applySoname(strtabsize); 537 dynamic().applyEntries(*file_format); 538 dynamic().emit(dyn_sect, *dyn_region); 539 540 strcpy((strtab + strtabsize), pModule.name().c_str()); 541 strtabsize += pModule.name().size() + 1; 542 543 // emit hash table 544 // FIXME: this verion only emit SVR4 hash section. 545 // Please add GNU new hash section 546 547 // both 32 and 64 bits hash table use 32-bit entry 548 // set up hash_region 549 uint32_t* word_array = (uint32_t*)hash_region->start(); 550 uint32_t& nbucket = word_array[0]; 551 uint32_t& nchain = word_array[1]; 552 553 nbucket = getHashBucketCount(symtabIdx, false); 554 nchain = symtabIdx; 555 556 uint32_t* bucket = (word_array + 2); 557 uint32_t* chain = (bucket + nbucket); 558 559 // initialize bucket 560 bzero((void*)bucket, nbucket); 561 562 StringHash<ELF> hash_func; 563 564 for (size_t sym_idx=0; sym_idx < symtabIdx; ++sym_idx) { 565 llvm::StringRef name(strtab + symtab32[sym_idx].st_name); 566 size_t bucket_pos = hash_func(name) % nbucket; 567 chain[sym_idx] = bucket[bucket_pos]; 568 bucket[bucket_pos] = sym_idx; 569 } 570 571} 572 573MipsGOT& MipsGNULDBackend::getGOT() 574{ 575 assert(NULL != m_pGOT); 576 return *m_pGOT; 577} 578 579const MipsGOT& MipsGNULDBackend::getGOT() const 580{ 581 assert(NULL != m_pGOT); 582 return *m_pGOT; 583} 584 585OutputRelocSection& MipsGNULDBackend::getRelDyn() 586{ 587 assert(NULL != m_pRelDyn); 588 return *m_pRelDyn; 589} 590 591const OutputRelocSection& MipsGNULDBackend::getRelDyn() const 592{ 593 assert(NULL != m_pRelDyn); 594 return *m_pRelDyn; 595} 596 597unsigned int 598MipsGNULDBackend::getTargetSectionOrder(const LDSection& pSectHdr) const 599{ 600 const ELFFileFormat* file_format = getOutputFormat(); 601 602 if (&pSectHdr == &file_format->getGOT()) 603 return SHO_DATA; 604 605 return SHO_UNDEFINED; 606} 607 608/// finalizeSymbol - finalize the symbol value 609bool MipsGNULDBackend::finalizeTargetSymbols() 610{ 611 if (NULL != m_pGpDispSymbol) 612 m_pGpDispSymbol->setValue(m_pGOT->addr() + 0x7FF0); 613 return true; 614} 615 616/// allocateCommonSymbols - allocate common symbols in the corresponding 617/// sections. This is called at pre-layout stage. 618/// @refer Google gold linker: common.cc: 214 619/// FIXME: Mips needs to allocate small common symbol 620bool MipsGNULDBackend::allocateCommonSymbols(Module& pModule) 621{ 622 SymbolCategory& symbol_list = pModule.getSymbolTable(); 623 624 if (symbol_list.emptyCommons() && symbol_list.emptyFiles() && 625 symbol_list.emptyLocals() && symbol_list.emptyLocalDyns()) 626 return true; 627 628 SymbolCategory::iterator com_sym, com_end; 629 630 // FIXME: If the order of common symbols is defined, then sort common symbols 631 // std::sort(com_sym, com_end, some kind of order); 632 633 // get corresponding BSS LDSection 634 ELFFileFormat* file_format = getOutputFormat(); 635 LDSection& bss_sect = file_format->getBSS(); 636 LDSection& tbss_sect = file_format->getTBSS(); 637 638 // get or create corresponding BSS SectionData 639 SectionData* bss_sect_data = NULL; 640 if (bss_sect.hasSectionData()) 641 bss_sect_data = bss_sect.getSectionData(); 642 else 643 bss_sect_data = IRBuilder::CreateSectionData(bss_sect); 644 645 SectionData* tbss_sect_data = NULL; 646 if (tbss_sect.hasSectionData()) 647 tbss_sect_data = tbss_sect.getSectionData(); 648 else 649 tbss_sect_data = IRBuilder::CreateSectionData(tbss_sect); 650 651 // remember original BSS size 652 uint64_t bss_offset = bss_sect.size(); 653 uint64_t tbss_offset = tbss_sect.size(); 654 655 // allocate all local common symbols 656 com_end = symbol_list.localEnd(); 657 658 for (com_sym = symbol_list.localBegin(); com_sym != com_end; ++com_sym) { 659 if (ResolveInfo::Common == (*com_sym)->desc()) { 660 // We have to reset the description of the symbol here. When doing 661 // incremental linking, the output relocatable object may have common 662 // symbols. Therefore, we can not treat common symbols as normal symbols 663 // when emitting the regular name pools. We must change the symbols' 664 // description here. 665 (*com_sym)->resolveInfo()->setDesc(ResolveInfo::Define); 666 Fragment* frag = new FillFragment(0x0, 1, (*com_sym)->size()); 667 (*com_sym)->setFragmentRef(FragmentRef::Create(*frag, 0)); 668 669 if (ResolveInfo::ThreadLocal == (*com_sym)->type()) { 670 // allocate TLS common symbol in tbss section 671 tbss_offset += ObjectBuilder::AppendFragment(*frag, 672 *tbss_sect_data, 673 (*com_sym)->value()); 674 } 675 // FIXME: how to identify small and large common symbols? 676 else { 677 bss_offset += ObjectBuilder::AppendFragment(*frag, 678 *bss_sect_data, 679 (*com_sym)->value()); 680 } 681 } 682 } 683 684 // allocate all global common symbols 685 com_end = symbol_list.commonEnd(); 686 for (com_sym = symbol_list.commonBegin(); com_sym != com_end; ++com_sym) { 687 // We have to reset the description of the symbol here. When doing 688 // incremental linking, the output relocatable object may have common 689 // symbols. Therefore, we can not treat common symbols as normal symbols 690 // when emitting the regular name pools. We must change the symbols' 691 // description here. 692 (*com_sym)->resolveInfo()->setDesc(ResolveInfo::Define); 693 Fragment* frag = new FillFragment(0x0, 1, (*com_sym)->size()); 694 (*com_sym)->setFragmentRef(FragmentRef::Create(*frag, 0)); 695 696 if (ResolveInfo::ThreadLocal == (*com_sym)->type()) { 697 // allocate TLS common symbol in tbss section 698 tbss_offset += ObjectBuilder::AppendFragment(*frag, 699 *tbss_sect_data, 700 (*com_sym)->value()); 701 } 702 // FIXME: how to identify small and large common symbols? 703 else { 704 bss_offset += ObjectBuilder::AppendFragment(*frag, 705 *bss_sect_data, 706 (*com_sym)->value()); 707 } 708 } 709 710 bss_sect.setSize(bss_offset); 711 tbss_sect.setSize(tbss_offset); 712 symbol_list.changeCommonsToGlobal(); 713 return true; 714} 715 716void MipsGNULDBackend::scanLocalReloc(Relocation& pReloc, 717 IRBuilder& pBuilder, 718 const LDSection& pSection) 719{ 720 ResolveInfo* rsym = pReloc.symInfo(); 721 722 switch (pReloc.type()){ 723 case llvm::ELF::R_MIPS_NONE: 724 case llvm::ELF::R_MIPS_16: 725 break; 726 case llvm::ELF::R_MIPS_32: 727 if (LinkerConfig::DynObj == config().codeGenType()) { 728 // TODO: (simon) The gold linker does not create an entry in .rel.dyn 729 // section if the symbol section flags contains SHF_EXECINSTR. 730 // 1. Find the reason of this condition. 731 // 2. Check this condition here. 732 m_pRelDyn->reserveEntry(); 733 rsym->setReserved(rsym->reserved() | ReserveRel); 734 checkAndSetHasTextRel(*pSection.getLink()); 735 736 // Remeber this rsym is a local GOT entry (as if it needs an entry). 737 // Actually we don't allocate an GOT entry. 738 m_pGOT->setLocal(rsym); 739 } 740 break; 741 case llvm::ELF::R_MIPS_REL32: 742 case llvm::ELF::R_MIPS_26: 743 case llvm::ELF::R_MIPS_HI16: 744 case llvm::ELF::R_MIPS_LO16: 745 case llvm::ELF::R_MIPS_PC16: 746 case llvm::ELF::R_MIPS_SHIFT5: 747 case llvm::ELF::R_MIPS_SHIFT6: 748 case llvm::ELF::R_MIPS_64: 749 case llvm::ELF::R_MIPS_GOT_PAGE: 750 case llvm::ELF::R_MIPS_GOT_OFST: 751 case llvm::ELF::R_MIPS_SUB: 752 case llvm::ELF::R_MIPS_INSERT_A: 753 case llvm::ELF::R_MIPS_INSERT_B: 754 case llvm::ELF::R_MIPS_DELETE: 755 case llvm::ELF::R_MIPS_HIGHER: 756 case llvm::ELF::R_MIPS_HIGHEST: 757 case llvm::ELF::R_MIPS_SCN_DISP: 758 case llvm::ELF::R_MIPS_REL16: 759 case llvm::ELF::R_MIPS_ADD_IMMEDIATE: 760 case llvm::ELF::R_MIPS_PJUMP: 761 case llvm::ELF::R_MIPS_RELGOT: 762 case llvm::ELF::R_MIPS_JALR: 763 case llvm::ELF::R_MIPS_GLOB_DAT: 764 case llvm::ELF::R_MIPS_COPY: 765 case llvm::ELF::R_MIPS_JUMP_SLOT: 766 break; 767 case llvm::ELF::R_MIPS_GOT16: 768 case llvm::ELF::R_MIPS_CALL16: 769 case llvm::ELF::R_MIPS_GOT_HI16: 770 case llvm::ELF::R_MIPS_CALL_HI16: 771 case llvm::ELF::R_MIPS_GOT_LO16: 772 case llvm::ELF::R_MIPS_CALL_LO16: 773 // For got16 section based relocations, we need to reserve got entries. 774 if (rsym->type() == ResolveInfo::Section) { 775 m_pGOT->reserveLocalEntry(); 776 // Remeber this rsym is a local GOT entry 777 m_pGOT->setLocal(rsym); 778 return; 779 } 780 781 if (!(rsym->reserved() & MipsGNULDBackend::ReserveGot)) { 782 m_pGOT->reserveLocalEntry(); 783 rsym->setReserved(rsym->reserved() | ReserveGot); 784 // Remeber this rsym is a local GOT entry 785 m_pGOT->setLocal(rsym); 786 } 787 break; 788 case llvm::ELF::R_MIPS_GPREL32: 789 case llvm::ELF::R_MIPS_GPREL16: 790 case llvm::ELF::R_MIPS_LITERAL: 791 case llvm::ELF::R_MIPS_GOT_DISP: 792 break; 793 case llvm::ELF::R_MIPS_TLS_DTPMOD32: 794 case llvm::ELF::R_MIPS_TLS_DTPREL32: 795 case llvm::ELF::R_MIPS_TLS_DTPMOD64: 796 case llvm::ELF::R_MIPS_TLS_DTPREL64: 797 case llvm::ELF::R_MIPS_TLS_GD: 798 case llvm::ELF::R_MIPS_TLS_LDM: 799 case llvm::ELF::R_MIPS_TLS_DTPREL_HI16: 800 case llvm::ELF::R_MIPS_TLS_DTPREL_LO16: 801 case llvm::ELF::R_MIPS_TLS_GOTTPREL: 802 case llvm::ELF::R_MIPS_TLS_TPREL32: 803 case llvm::ELF::R_MIPS_TLS_TPREL64: 804 case llvm::ELF::R_MIPS_TLS_TPREL_HI16: 805 case llvm::ELF::R_MIPS_TLS_TPREL_LO16: 806 break; 807 default: 808 fatal(diag::unknown_relocation) << (int)pReloc.type() 809 << pReloc.symInfo()->name(); 810 } 811} 812 813void MipsGNULDBackend::scanGlobalReloc(Relocation& pReloc, 814 IRBuilder& pBuilder, 815 const LDSection& pSection) 816{ 817 ResolveInfo* rsym = pReloc.symInfo(); 818 819 switch (pReloc.type()){ 820 case llvm::ELF::R_MIPS_NONE: 821 case llvm::ELF::R_MIPS_INSERT_A: 822 case llvm::ELF::R_MIPS_INSERT_B: 823 case llvm::ELF::R_MIPS_DELETE: 824 case llvm::ELF::R_MIPS_TLS_DTPMOD64: 825 case llvm::ELF::R_MIPS_TLS_DTPREL64: 826 case llvm::ELF::R_MIPS_REL16: 827 case llvm::ELF::R_MIPS_ADD_IMMEDIATE: 828 case llvm::ELF::R_MIPS_PJUMP: 829 case llvm::ELF::R_MIPS_RELGOT: 830 case llvm::ELF::R_MIPS_TLS_TPREL64: 831 break; 832 case llvm::ELF::R_MIPS_32: 833 case llvm::ELF::R_MIPS_64: 834 case llvm::ELF::R_MIPS_HI16: 835 case llvm::ELF::R_MIPS_LO16: 836 if (symbolNeedsDynRel(*rsym, false, true)) { 837 m_pRelDyn->reserveEntry(); 838 rsym->setReserved(rsym->reserved() | ReserveRel); 839 checkAndSetHasTextRel(*pSection.getLink()); 840 841 // Remeber this rsym is a global GOT entry (as if it needs an entry). 842 // Actually we don't allocate an GOT entry. 843 m_pGOT->setGlobal(rsym); 844 } 845 break; 846 case llvm::ELF::R_MIPS_GOT16: 847 case llvm::ELF::R_MIPS_CALL16: 848 case llvm::ELF::R_MIPS_GOT_DISP: 849 case llvm::ELF::R_MIPS_GOT_HI16: 850 case llvm::ELF::R_MIPS_CALL_HI16: 851 case llvm::ELF::R_MIPS_GOT_LO16: 852 case llvm::ELF::R_MIPS_CALL_LO16: 853 case llvm::ELF::R_MIPS_GOT_PAGE: 854 case llvm::ELF::R_MIPS_GOT_OFST: 855 if (!(rsym->reserved() & MipsGNULDBackend::ReserveGot)) { 856 m_pGOT->reserveGlobalEntry(); 857 rsym->setReserved(rsym->reserved() | ReserveGot); 858 m_GlobalGOTSyms.push_back(rsym->outSymbol()); 859 // Remeber this rsym is a global GOT entry 860 m_pGOT->setGlobal(rsym); 861 } 862 break; 863 case llvm::ELF::R_MIPS_LITERAL: 864 case llvm::ELF::R_MIPS_GPREL32: 865 fatal(diag::invalid_global_relocation) << (int)pReloc.type() 866 << pReloc.symInfo()->name(); 867 break; 868 case llvm::ELF::R_MIPS_GPREL16: 869 break; 870 case llvm::ELF::R_MIPS_26: 871 case llvm::ELF::R_MIPS_PC16: 872 break; 873 case llvm::ELF::R_MIPS_16: 874 case llvm::ELF::R_MIPS_SHIFT5: 875 case llvm::ELF::R_MIPS_SHIFT6: 876 case llvm::ELF::R_MIPS_SUB: 877 case llvm::ELF::R_MIPS_HIGHER: 878 case llvm::ELF::R_MIPS_HIGHEST: 879 case llvm::ELF::R_MIPS_SCN_DISP: 880 break; 881 case llvm::ELF::R_MIPS_TLS_DTPREL32: 882 case llvm::ELF::R_MIPS_TLS_GD: 883 case llvm::ELF::R_MIPS_TLS_LDM: 884 case llvm::ELF::R_MIPS_TLS_DTPREL_HI16: 885 case llvm::ELF::R_MIPS_TLS_DTPREL_LO16: 886 case llvm::ELF::R_MIPS_TLS_GOTTPREL: 887 case llvm::ELF::R_MIPS_TLS_TPREL32: 888 case llvm::ELF::R_MIPS_TLS_TPREL_HI16: 889 case llvm::ELF::R_MIPS_TLS_TPREL_LO16: 890 break; 891 case llvm::ELF::R_MIPS_REL32: 892 break; 893 case llvm::ELF::R_MIPS_JALR: 894 break; 895 case llvm::ELF::R_MIPS_COPY: 896 case llvm::ELF::R_MIPS_GLOB_DAT: 897 case llvm::ELF::R_MIPS_JUMP_SLOT: 898 fatal(diag::dynamic_relocation) << (int)pReloc.type(); 899 break; 900 default: 901 fatal(diag::unknown_relocation) << (int)pReloc.type() 902 << pReloc.symInfo()->name(); 903 } 904} 905 906void MipsGNULDBackend::defineGOTSymbol(IRBuilder& pBuilder) 907{ 908 // define symbol _GLOBAL_OFFSET_TABLE_ 909 if ( m_pGOTSymbol != NULL ) { 910 pBuilder.AddSymbol<IRBuilder::Force, IRBuilder::Unresolve>( 911 "_GLOBAL_OFFSET_TABLE_", 912 ResolveInfo::Object, 913 ResolveInfo::Define, 914 ResolveInfo::Local, 915 0x0, // size 916 0x0, // value 917 FragmentRef::Create(*(m_pGOT->begin()), 0x0), 918 ResolveInfo::Hidden); 919 } 920 else { 921 m_pGOTSymbol = pBuilder.AddSymbol<IRBuilder::Force, IRBuilder::Resolve>( 922 "_GLOBAL_OFFSET_TABLE_", 923 ResolveInfo::Object, 924 ResolveInfo::Define, 925 ResolveInfo::Local, 926 0x0, // size 927 0x0, // value 928 FragmentRef::Create(*(m_pGOT->begin()), 0x0), 929 ResolveInfo::Hidden); 930 } 931} 932 933/// doCreateProgramHdrs - backend can implement this function to create the 934/// target-dependent segments 935void MipsGNULDBackend::doCreateProgramHdrs(Module& pModule) 936{ 937 // TODO 938} 939 940//===----------------------------------------------------------------------===// 941/// createMipsLDBackend - the help funtion to create corresponding MipsLDBackend 942/// 943static TargetLDBackend* createMipsLDBackend(const llvm::Target& pTarget, 944 const LinkerConfig& pConfig) 945{ 946 if (pConfig.targets().triple().isOSDarwin()) { 947 assert(0 && "MachO linker is not supported yet"); 948 } 949 if (pConfig.targets().triple().isOSWindows()) { 950 assert(0 && "COFF linker is not supported yet"); 951 } 952 return new MipsGNULDBackend(pConfig, new MipsGNUInfo(pConfig.targets().triple())); 953} 954 955//===----------------------------------------------------------------------===// 956// Force static initialization. 957//===----------------------------------------------------------------------===// 958extern "C" void MCLDInitializeMipsLDBackend() { 959 // Register the linker backend 960 mcld::TargetRegistry::RegisterTargetLDBackend(mcld::TheMipselTarget, 961 createMipsLDBackend); 962} 963 964