1 //===- GNULDBackend.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 10 #include <mcld/Target/GNULDBackend.h> 11 12 #include <string> 13 #include <cstring> 14 #include <cassert> 15 16 #include <llvm/Support/ELF.h> 17 18 #include <mcld/ADT/SizeTraits.h> 19 #include <mcld/LD/LDSymbol.h> 20 #include <mcld/LD/Layout.h> 21 #include <mcld/LD/FillFragment.h> 22 #include <mcld/MC/MCLDInfo.h> 23 #include <mcld/MC/MCLDOutput.h> 24 #include <mcld/MC/InputTree.h> 25 #include <mcld/MC/SymbolCategory.h> 26 #include <mcld/MC/MCLinker.h> 27 #include <mcld/Support/MemoryArea.h> 28 #include <mcld/Support/MemoryRegion.h> 29 #include <mcld/Support/MsgHandling.h> 30 #include <mcld/Support/MemoryAreaFactory.h> 31 32 using namespace mcld; 33 34 //===----------------------------------------------------------------------===// 35 // GNULDBackend 36 //===----------------------------------------------------------------------===// GNULDBackend()37 GNULDBackend::GNULDBackend() 38 : m_pArchiveReader(NULL), 39 m_pObjectReader(NULL), 40 m_pDynObjReader(NULL), 41 m_pObjectWriter(NULL), 42 m_pDynObjWriter(NULL), 43 m_pExecWriter(NULL), 44 m_pDynObjFileFormat(NULL), 45 m_pExecFileFormat(NULL), 46 m_ELFSegmentTable(9), // magic number 47 m_pEhFrameHdr(NULL), 48 f_pPreInitArrayStart(NULL), 49 f_pPreInitArrayEnd(NULL), 50 f_pInitArrayStart(NULL), 51 f_pInitArrayEnd(NULL), 52 f_pFiniArrayStart(NULL), 53 f_pFiniArrayEnd(NULL), 54 f_pStack(NULL), 55 f_pExecutableStart(NULL), 56 f_pEText(NULL), 57 f_p_EText(NULL), 58 f_p__EText(NULL), 59 f_pEData(NULL), 60 f_p_EData(NULL), 61 f_pBSSStart(NULL), 62 f_pEnd(NULL), 63 f_p_End(NULL) { 64 m_pSymIndexMap = new HashTableType(1024); 65 } 66 ~GNULDBackend()67 GNULDBackend::~GNULDBackend() 68 { 69 if (NULL != m_pArchiveReader) 70 delete m_pArchiveReader; 71 if (NULL != m_pObjectReader) 72 delete m_pObjectReader; 73 if (NULL != m_pDynObjReader) 74 delete m_pDynObjReader; 75 if (NULL != m_pObjectWriter) 76 delete m_pObjectWriter; 77 if (NULL != m_pDynObjWriter) 78 delete m_pDynObjWriter; 79 if (NULL != m_pExecWriter) 80 delete m_pExecWriter; 81 if (NULL != m_pDynObjFileFormat) 82 delete m_pDynObjFileFormat; 83 if (NULL != m_pExecFileFormat) 84 delete m_pExecFileFormat; 85 if (NULL != m_pSymIndexMap) 86 delete m_pSymIndexMap; 87 if (NULL != m_pEhFrameHdr) 88 delete m_pEhFrameHdr; 89 } 90 sectionStartOffset() const91 size_t GNULDBackend::sectionStartOffset() const 92 { 93 // FIXME: use fixed offset, we need 10 segments by default 94 return sizeof(llvm::ELF::Elf64_Ehdr)+10*sizeof(llvm::ELF::Elf64_Phdr); 95 } 96 segmentStartAddr(const Output & pOutput,const MCLDInfo & pInfo) const97 uint64_t GNULDBackend::segmentStartAddr(const Output& pOutput, 98 const MCLDInfo& pInfo) const 99 { 100 // TODO: handle the user option: -TText= 101 if (isOutputPIC(pOutput, pInfo)) 102 return 0x0; 103 else 104 return defaultTextSegmentAddr(); 105 } 106 initArchiveReader(MCLinker & pLinker,MCLDInfo & pInfo,MemoryAreaFactory & pMemAreaFactory)107 bool GNULDBackend::initArchiveReader(MCLinker& pLinker, 108 MCLDInfo& pInfo, 109 MemoryAreaFactory& pMemAreaFactory) 110 { 111 if (NULL == m_pArchiveReader) { 112 assert(NULL != m_pObjectReader); 113 m_pArchiveReader = new GNUArchiveReader(pInfo, 114 pMemAreaFactory, 115 *m_pObjectReader); 116 } 117 return true; 118 } 119 initObjectReader(MCLinker & pLinker)120 bool GNULDBackend::initObjectReader(MCLinker& pLinker) 121 { 122 if (NULL == m_pObjectReader) 123 m_pObjectReader = new ELFObjectReader(*this, pLinker); 124 return true; 125 } 126 initDynObjReader(MCLinker & pLinker)127 bool GNULDBackend::initDynObjReader(MCLinker& pLinker) 128 { 129 if (NULL == m_pDynObjReader) 130 m_pDynObjReader = new ELFDynObjReader(*this, pLinker); 131 return true; 132 } 133 initObjectWriter(MCLinker &)134 bool GNULDBackend::initObjectWriter(MCLinker&) 135 { 136 // TODO 137 return true; 138 } 139 initDynObjWriter(MCLinker & pLinker)140 bool GNULDBackend::initDynObjWriter(MCLinker& pLinker) 141 { 142 if (NULL == m_pDynObjWriter) 143 m_pDynObjWriter = new ELFDynObjWriter(*this, pLinker); 144 return true; 145 } 146 initExecWriter(MCLinker & pLinker)147 bool GNULDBackend::initExecWriter(MCLinker& pLinker) 148 { 149 if (NULL == m_pExecWriter) 150 m_pExecWriter = new ELFExecWriter(*this, pLinker); 151 return true; 152 } 153 initExecSections(MCLinker & pMCLinker)154 bool GNULDBackend::initExecSections(MCLinker& pMCLinker) 155 { 156 if (NULL == m_pExecFileFormat) 157 m_pExecFileFormat = new ELFExecFileFormat(*this); 158 159 // initialize standard sections 160 m_pExecFileFormat->initStdSections(pMCLinker); 161 return true; 162 } 163 initDynObjSections(MCLinker & pMCLinker)164 bool GNULDBackend::initDynObjSections(MCLinker& pMCLinker) 165 { 166 if (NULL == m_pDynObjFileFormat) 167 m_pDynObjFileFormat = new ELFDynObjFileFormat(*this); 168 169 // initialize standard sections 170 m_pDynObjFileFormat->initStdSections(pMCLinker); 171 return true; 172 } 173 initStandardSymbols(MCLinker & pLinker,const Output & pOutput)174 bool GNULDBackend::initStandardSymbols(MCLinker& pLinker, const Output& pOutput) 175 { 176 ELFFileFormat* file_format = getOutputFormat(pOutput); 177 178 // ----- section symbols ----- // 179 // .preinit_array 180 FragmentRef* preinit_array = NULL; 181 if (file_format->hasPreInitArray()) { 182 preinit_array = pLinker.getLayout().getFragmentRef( 183 *(file_format->getPreInitArray().getSectionData()->begin()), 184 0x0); 185 } 186 f_pPreInitArrayStart = 187 pLinker.defineSymbol<MCLinker::AsRefered, 188 MCLinker::Resolve>("__preinit_array_start", 189 false, // isDyn 190 ResolveInfo::NoType, 191 ResolveInfo::Define, 192 ResolveInfo::Global, 193 0x0, // size 194 0x0, // value 195 preinit_array, // FragRef 196 ResolveInfo::Hidden); 197 f_pPreInitArrayEnd = 198 pLinker.defineSymbol<MCLinker::AsRefered, 199 MCLinker::Resolve>("__preinit_array_end", 200 false, // isDyn 201 ResolveInfo::NoType, 202 ResolveInfo::Define, 203 ResolveInfo::Global, 204 0x0, // size 205 0x0, // value 206 NULL, // FragRef 207 ResolveInfo::Hidden); 208 209 // .init_array 210 FragmentRef* init_array = NULL; 211 if (file_format->hasInitArray()) { 212 init_array = pLinker.getLayout().getFragmentRef( 213 *(file_format->getInitArray().getSectionData()->begin()), 214 0x0); 215 } 216 217 f_pInitArrayStart = 218 pLinker.defineSymbol<MCLinker::AsRefered, 219 MCLinker::Resolve>("__init_array_start", 220 false, // isDyn 221 ResolveInfo::NoType, 222 ResolveInfo::Define, 223 ResolveInfo::Global, 224 0x0, // size 225 0x0, // value 226 init_array, // FragRef 227 ResolveInfo::Hidden); 228 f_pInitArrayEnd = 229 pLinker.defineSymbol<MCLinker::AsRefered, 230 MCLinker::Resolve>("__init_array_end", 231 false, // isDyn 232 ResolveInfo::NoType, 233 ResolveInfo::Define, 234 ResolveInfo::Global, 235 0x0, // size 236 0x0, // value 237 init_array, // FragRef 238 ResolveInfo::Hidden); 239 240 // .fini_array 241 FragmentRef* fini_array = NULL; 242 if (file_format->hasFiniArray()) { 243 fini_array = pLinker.getLayout().getFragmentRef( 244 *(file_format->getFiniArray().getSectionData()->begin()), 245 0x0); 246 } 247 248 f_pFiniArrayStart = 249 pLinker.defineSymbol<MCLinker::AsRefered, 250 MCLinker::Resolve>("__fini_array_start", 251 false, // isDyn 252 ResolveInfo::NoType, 253 ResolveInfo::Define, 254 ResolveInfo::Global, 255 0x0, // size 256 0x0, // value 257 fini_array, // FragRef 258 ResolveInfo::Hidden); 259 f_pFiniArrayEnd = 260 pLinker.defineSymbol<MCLinker::AsRefered, 261 MCLinker::Resolve>("__fini_array_end", 262 false, // isDyn 263 ResolveInfo::NoType, 264 ResolveInfo::Define, 265 ResolveInfo::Global, 266 0x0, // size 267 0x0, // value 268 fini_array, // FragRef 269 ResolveInfo::Hidden); 270 271 // .stack 272 FragmentRef* stack = NULL; 273 if (file_format->hasStack()) { 274 stack = pLinker.getLayout().getFragmentRef( 275 *(file_format->getStack().getSectionData()->begin()), 276 0x0); 277 } 278 f_pStack = 279 pLinker.defineSymbol<MCLinker::AsRefered, 280 MCLinker::Resolve>("__stack", 281 false, // isDyn 282 ResolveInfo::NoType, 283 ResolveInfo::Define, 284 ResolveInfo::Global, 285 0x0, // size 286 0x0, // value 287 stack, // FragRef 288 ResolveInfo::Hidden); 289 290 // ----- segment symbols ----- // 291 f_pExecutableStart = 292 pLinker.defineSymbol<MCLinker::AsRefered, 293 MCLinker::Resolve>("__executable_start", 294 false, // isDyn 295 ResolveInfo::NoType, 296 ResolveInfo::Define, 297 ResolveInfo::Absolute, 298 0x0, // size 299 0x0, // value 300 NULL, // FragRef 301 ResolveInfo::Default); 302 f_pEText = 303 pLinker.defineSymbol<MCLinker::AsRefered, 304 MCLinker::Resolve>("etext", 305 false, // isDyn 306 ResolveInfo::NoType, 307 ResolveInfo::Define, 308 ResolveInfo::Absolute, 309 0x0, // size 310 0x0, // value 311 NULL, // FragRef 312 ResolveInfo::Default); 313 f_p_EText = 314 pLinker.defineSymbol<MCLinker::AsRefered, 315 MCLinker::Resolve>("_etext", 316 false, // isDyn 317 ResolveInfo::NoType, 318 ResolveInfo::Define, 319 ResolveInfo::Absolute, 320 0x0, // size 321 0x0, // value 322 NULL, // FragRef 323 ResolveInfo::Default); 324 f_p__EText = 325 pLinker.defineSymbol<MCLinker::AsRefered, 326 MCLinker::Resolve>("__etext", 327 false, // isDyn 328 ResolveInfo::NoType, 329 ResolveInfo::Define, 330 ResolveInfo::Absolute, 331 0x0, // size 332 0x0, // value 333 NULL, // FragRef 334 ResolveInfo::Default); 335 f_pEData = 336 pLinker.defineSymbol<MCLinker::AsRefered, 337 MCLinker::Resolve>("edata", 338 false, // isDyn 339 ResolveInfo::NoType, 340 ResolveInfo::Define, 341 ResolveInfo::Absolute, 342 0x0, // size 343 0x0, // value 344 NULL, // FragRef 345 ResolveInfo::Default); 346 347 f_pEnd = 348 pLinker.defineSymbol<MCLinker::AsRefered, 349 MCLinker::Resolve>("end", 350 false, // isDyn 351 ResolveInfo::NoType, 352 ResolveInfo::Define, 353 ResolveInfo::Absolute, 354 0x0, // size 355 0x0, // value 356 NULL, // FragRef 357 ResolveInfo::Default); 358 359 // _edata is defined forcefully. 360 // @ref Google gold linker: defstd.cc: 186 361 f_p_EData = 362 pLinker.defineSymbol<MCLinker::Force, 363 MCLinker::Resolve>("_edata", 364 false, // isDyn 365 ResolveInfo::NoType, 366 ResolveInfo::Define, 367 ResolveInfo::Absolute, 368 0x0, // size 369 0x0, // value 370 NULL, // FragRef 371 ResolveInfo::Default); 372 373 // __bss_start is defined forcefully. 374 // @ref Google gold linker: defstd.cc: 214 375 f_pBSSStart = 376 pLinker.defineSymbol<MCLinker::Force, 377 MCLinker::Resolve>("__bss_start", 378 false, // isDyn 379 ResolveInfo::NoType, 380 ResolveInfo::Define, 381 ResolveInfo::Absolute, 382 0x0, // size 383 0x0, // value 384 NULL, // FragRef 385 ResolveInfo::Default); 386 387 // _end is defined forcefully. 388 // @ref Google gold linker: defstd.cc: 228 389 f_p_End = 390 pLinker.defineSymbol<MCLinker::Force, 391 MCLinker::Resolve>("_end", 392 false, // isDyn 393 ResolveInfo::NoType, 394 ResolveInfo::Define, 395 ResolveInfo::Absolute, 396 0x0, // size 397 0x0, // value 398 NULL, // FragRef 399 ResolveInfo::Default); 400 401 return true; 402 } 403 404 bool finalizeStandardSymbols(MCLinker & pLinker,const Output & pOutput)405 GNULDBackend::finalizeStandardSymbols(MCLinker& pLinker, const Output& pOutput) 406 { 407 ELFFileFormat* file_format = getOutputFormat(pOutput); 408 409 // ----- section symbols ----- // 410 if (NULL != f_pPreInitArrayStart) { 411 if (!f_pPreInitArrayStart->hasFragRef()) { 412 f_pPreInitArrayStart->resolveInfo()->setBinding(ResolveInfo::Absolute); 413 f_pPreInitArrayStart->setValue(0x0); 414 } 415 } 416 417 if (NULL != f_pPreInitArrayEnd) { 418 if (f_pPreInitArrayEnd->hasFragRef()) { 419 f_pPreInitArrayEnd->setValue(f_pPreInitArrayEnd->value() + 420 file_format->getPreInitArray().size()); 421 } 422 else { 423 f_pPreInitArrayEnd->resolveInfo()->setBinding(ResolveInfo::Absolute); 424 f_pPreInitArrayEnd->setValue(0x0); 425 } 426 } 427 428 if (NULL != f_pInitArrayStart) { 429 if (!f_pInitArrayStart->hasFragRef()) { 430 f_pInitArrayStart->resolveInfo()->setBinding(ResolveInfo::Absolute); 431 f_pInitArrayStart->setValue(0x0); 432 } 433 } 434 435 if (NULL != f_pInitArrayEnd) { 436 if (f_pInitArrayEnd->hasFragRef()) { 437 f_pInitArrayEnd->setValue(f_pInitArrayEnd->value() + 438 file_format->getInitArray().size()); 439 } 440 else { 441 f_pInitArrayEnd->resolveInfo()->setBinding(ResolveInfo::Absolute); 442 f_pInitArrayEnd->setValue(0x0); 443 } 444 } 445 446 if (NULL != f_pFiniArrayStart) { 447 if (!f_pFiniArrayStart->hasFragRef()) { 448 f_pFiniArrayStart->resolveInfo()->setBinding(ResolveInfo::Absolute); 449 f_pFiniArrayStart->setValue(0x0); 450 } 451 } 452 453 if (NULL != f_pFiniArrayEnd) { 454 if (f_pFiniArrayEnd->hasFragRef()) { 455 f_pFiniArrayEnd->setValue(f_pFiniArrayEnd->value() + 456 file_format->getFiniArray().size()); 457 } 458 else { 459 f_pFiniArrayEnd->resolveInfo()->setBinding(ResolveInfo::Absolute); 460 f_pFiniArrayEnd->setValue(0x0); 461 } 462 } 463 464 if (NULL != f_pStack) { 465 if (!f_pStack->hasFragRef()) { 466 f_pStack->resolveInfo()->setBinding(ResolveInfo::Absolute); 467 f_pStack->setValue(0x0); 468 } 469 } 470 471 // ----- segment symbols ----- // 472 if (NULL != f_pExecutableStart) { 473 ELFSegment* exec_start = m_ELFSegmentTable.find(llvm::ELF::PT_LOAD, 0x0, 0x0); 474 if (NULL != exec_start) { 475 if (ResolveInfo::ThreadLocal != f_pExecutableStart->type()) { 476 f_pExecutableStart->setValue(f_pExecutableStart->value() + 477 exec_start->vaddr()); 478 } 479 } 480 else 481 f_pExecutableStart->setValue(0x0); 482 } 483 484 if (NULL != f_pEText || NULL != f_p_EText || NULL !=f_p__EText) { 485 ELFSegment* etext = m_ELFSegmentTable.find(llvm::ELF::PT_LOAD, 486 llvm::ELF::PF_X, 487 llvm::ELF::PF_W); 488 if (NULL != etext) { 489 if (NULL != f_pEText && ResolveInfo::ThreadLocal != f_pEText->type()) { 490 f_pEText->setValue(f_pEText->value() + 491 etext->vaddr() + 492 etext->memsz()); 493 } 494 if (NULL != f_p_EText && ResolveInfo::ThreadLocal != f_p_EText->type()) { 495 f_p_EText->setValue(f_p_EText->value() + 496 etext->vaddr() + 497 etext->memsz()); 498 } 499 if (NULL != f_p__EText && ResolveInfo::ThreadLocal != f_p__EText->type()) { 500 f_p__EText->setValue(f_p__EText->value() + 501 etext->vaddr() + 502 etext->memsz()); 503 } 504 } 505 else { 506 if (NULL != f_pEText) 507 f_pEText->setValue(0x0); 508 if (NULL != f_p_EText) 509 f_p_EText->setValue(0x0); 510 if (NULL != f_p__EText) 511 f_p__EText->setValue(0x0); 512 } 513 } 514 515 if (NULL != f_pEData || NULL != f_p_EData || NULL != f_pBSSStart || 516 NULL != f_pEnd || NULL != f_p_End) { 517 ELFSegment* edata = m_ELFSegmentTable.find(llvm::ELF::PT_LOAD, 518 llvm::ELF::PF_W, 519 0x0); 520 if (NULL != edata) { 521 if (NULL != f_pEData && ResolveInfo::ThreadLocal != f_pEData->type()) { 522 f_pEData->setValue(f_pEData->value() + 523 edata->vaddr() + 524 edata->filesz()); 525 } 526 if (NULL != f_p_EData && ResolveInfo::ThreadLocal != f_p_EData->type()) { 527 f_p_EData->setValue(f_p_EData->value() + 528 edata->vaddr() + 529 edata->filesz()); 530 } 531 if (NULL != f_pBSSStart && ResolveInfo::ThreadLocal != f_pBSSStart->type()) { 532 f_pBSSStart->setValue(f_pBSSStart->value() + 533 edata->vaddr() + 534 edata->filesz()); 535 } 536 537 if (NULL != f_pEnd && ResolveInfo::ThreadLocal != f_pEnd->type()) { 538 f_pEnd->setValue(f_pEnd->value() + 539 edata->vaddr() + 540 edata->memsz()); 541 } 542 if (NULL != f_p_End && ResolveInfo::ThreadLocal != f_p_End->type()) { 543 f_p_End->setValue(f_p_End->value() + 544 edata->vaddr() + 545 edata->memsz()); 546 } 547 } 548 else { 549 if (NULL != f_pEData) 550 f_pEData->setValue(0x0); 551 if (NULL != f_p_EData) 552 f_p_EData->setValue(0x0); 553 if (NULL != f_pBSSStart) 554 f_pBSSStart->setValue(0x0); 555 556 if (NULL != f_pEnd) 557 f_pEnd->setValue(0x0); 558 if (NULL != f_p_End) 559 f_p_End->setValue(0x0); 560 } 561 } 562 563 return true; 564 } 565 getArchiveReader()566 GNUArchiveReader *GNULDBackend::getArchiveReader() 567 { 568 assert(NULL != m_pArchiveReader); 569 return m_pArchiveReader; 570 } 571 getArchiveReader() const572 const GNUArchiveReader *GNULDBackend::getArchiveReader() const 573 { 574 assert(NULL != m_pArchiveReader); 575 return m_pArchiveReader; 576 } 577 getObjectReader()578 ELFObjectReader *GNULDBackend::getObjectReader() 579 { 580 assert(NULL != m_pObjectReader); 581 return m_pObjectReader; 582 } 583 getObjectReader() const584 const ELFObjectReader *GNULDBackend::getObjectReader() const 585 { 586 assert(NULL != m_pObjectReader); 587 return m_pObjectReader; 588 } 589 getDynObjReader()590 ELFDynObjReader *GNULDBackend::getDynObjReader() 591 { 592 assert(NULL != m_pDynObjReader); 593 return m_pDynObjReader; 594 } 595 getDynObjReader() const596 const ELFDynObjReader *GNULDBackend::getDynObjReader() const 597 { 598 assert(NULL != m_pDynObjReader); 599 return m_pDynObjReader; 600 } 601 getObjectWriter()602 ELFObjectWriter *GNULDBackend::getObjectWriter() 603 { 604 // TODO 605 return NULL; 606 } 607 getObjectWriter() const608 const ELFObjectWriter *GNULDBackend::getObjectWriter() const 609 { 610 // TODO 611 return NULL; 612 } 613 getDynObjWriter()614 ELFDynObjWriter *GNULDBackend::getDynObjWriter() 615 { 616 assert(NULL != m_pDynObjWriter); 617 return m_pDynObjWriter; 618 } 619 getDynObjWriter() const620 const ELFDynObjWriter *GNULDBackend::getDynObjWriter() const 621 { 622 assert(NULL != m_pDynObjWriter); 623 return m_pDynObjWriter; 624 } 625 getExecWriter()626 ELFExecWriter *GNULDBackend::getExecWriter() 627 { 628 assert(NULL != m_pExecWriter); 629 return m_pExecWriter; 630 } 631 getExecWriter() const632 const ELFExecWriter *GNULDBackend::getExecWriter() const 633 { 634 assert(NULL != m_pExecWriter); 635 return m_pExecWriter; 636 } 637 getOutputFormat(const Output & pOutput)638 ELFFileFormat* GNULDBackend::getOutputFormat(const Output& pOutput) 639 { 640 switch (pOutput.type()) { 641 case Output::DynObj: 642 return getDynObjFileFormat(); 643 case Output::Exec: 644 return getExecFileFormat(); 645 // FIXME: We do not support building .o now 646 case Output::Object: 647 default: 648 fatal(diag::unrecognized_output_file) << pOutput.type(); 649 return NULL; 650 } 651 } 652 getOutputFormat(const Output & pOutput) const653 const ELFFileFormat* GNULDBackend::getOutputFormat(const Output& pOutput) const 654 { 655 switch (pOutput.type()) { 656 case Output::DynObj: 657 return getDynObjFileFormat(); 658 case Output::Exec: 659 return getExecFileFormat(); 660 // FIXME: We do not support building .o now 661 case Output::Object: 662 default: 663 fatal(diag::unrecognized_output_file) << pOutput.type(); 664 return NULL; 665 } 666 } 667 getDynObjFileFormat()668 ELFDynObjFileFormat* GNULDBackend::getDynObjFileFormat() 669 { 670 assert(NULL != m_pDynObjFileFormat); 671 return m_pDynObjFileFormat; 672 } 673 getDynObjFileFormat() const674 const ELFDynObjFileFormat* GNULDBackend::getDynObjFileFormat() const 675 { 676 assert(NULL != m_pDynObjFileFormat); 677 return m_pDynObjFileFormat; 678 } 679 getExecFileFormat()680 ELFExecFileFormat* GNULDBackend::getExecFileFormat() 681 { 682 assert(NULL != m_pExecFileFormat); 683 return m_pExecFileFormat; 684 } 685 getExecFileFormat() const686 const ELFExecFileFormat* GNULDBackend::getExecFileFormat() const 687 { 688 assert(NULL != m_pExecFileFormat); 689 return m_pExecFileFormat; 690 } 691 692 /// sizeNamePools - compute the size of regular name pools 693 /// In ELF executable files, regular name pools are .symtab, .strtab, 694 /// .dynsym, .dynstr, and .hash 695 void sizeNamePools(const Output & pOutput,const SymbolCategory & pSymbols,const MCLDInfo & pLDInfo)696 GNULDBackend::sizeNamePools(const Output& pOutput, 697 const SymbolCategory& pSymbols, 698 const MCLDInfo& pLDInfo) 699 { 700 // size of string tables starts from 1 to hold the null character in their 701 // first byte 702 size_t symtab = 1; 703 size_t dynsym = 1; 704 // number of entries in symbol tables starts from 1 to hold the special entry 705 // at index 0 (STN_UNDEF). See ELF Spec Book I, p1-21. 706 size_t strtab = 1; 707 size_t dynstr = 1; 708 size_t hash = 0; 709 710 // compute size of .symtab, .dynsym and .strtab 711 SymbolCategory::const_iterator symbol; 712 SymbolCategory::const_iterator symEnd = pSymbols.end(); 713 for (symbol = pSymbols.begin(); symbol != symEnd; ++symbol) { 714 size_t str_size = (*symbol)->nameSize() + 1; 715 if (isDynamicSymbol(**symbol, pOutput)) { 716 ++dynsym; 717 dynstr += str_size; 718 } 719 ++symtab; 720 strtab += str_size; 721 } 722 723 ELFFileFormat* file_format = getOutputFormat(pOutput); 724 725 switch(pOutput.type()) { 726 // compute size of .dynstr and .hash 727 case Output::DynObj: 728 case Output::Exec: { 729 // add DT_NEED strings into .dynstr and .dynamic 730 // Rules: 731 // 1. ignore --no-add-needed 732 // 2. force count in --no-as-needed 733 // 3. judge --as-needed 734 InputTree::const_bfs_iterator input, inputEnd = pLDInfo.inputs().bfs_end(); 735 for (input = pLDInfo.inputs().bfs_begin(); input != inputEnd; ++input) { 736 if (Input::DynObj == (*input)->type()) { 737 // --add-needed 738 if ((*input)->attribute()->isAddNeeded()) { 739 // --no-as-needed 740 if (!(*input)->attribute()->isAsNeeded()) { 741 dynstr += (*input)->name().size() + 1; 742 dynamic().reserveNeedEntry(); 743 } 744 // --as-needed 745 else if ((*input)->isNeeded()) { 746 dynstr += (*input)->name().size() + 1; 747 dynamic().reserveNeedEntry(); 748 } 749 } 750 } 751 } // for 752 753 // compute .hash 754 // Both Elf32_Word and Elf64_Word are 4 bytes 755 hash = (2 + getHashBucketCount(dynsym, false) + dynsym) * 756 sizeof(llvm::ELF::Elf32_Word); 757 758 // set size 759 dynstr += pOutput.name().size() + 1; 760 if (32 == bitclass()) 761 file_format->getDynSymTab().setSize(dynsym*sizeof(llvm::ELF::Elf32_Sym)); 762 else 763 file_format->getDynSymTab().setSize(dynsym*sizeof(llvm::ELF::Elf64_Sym)); 764 file_format->getDynStrTab().setSize(dynstr); 765 file_format->getHashTab().setSize(hash); 766 767 } 768 /* fall through */ 769 case Output::Object: { 770 if (32 == bitclass()) 771 file_format->getSymTab().setSize(symtab*sizeof(llvm::ELF::Elf32_Sym)); 772 else 773 file_format->getSymTab().setSize(symtab*sizeof(llvm::ELF::Elf64_Sym)); 774 file_format->getStrTab().setSize(strtab); 775 break; 776 } 777 } // end of switch 778 779 // reserve fixed entries in the .dynamic section. 780 if (Output::DynObj == pOutput.type() || Output::Exec == pOutput.type()) { 781 // Because some entries in .dynamic section need information of .dynsym, 782 // .dynstr, .symtab, .strtab and .hash, we can not reserve non-DT_NEEDED 783 // entries until we get the size of the sections mentioned above 784 dynamic().reserveEntries(pLDInfo, *file_format); 785 file_format->getDynamic().setSize(dynamic().numOfBytes()); 786 } 787 } 788 789 /// emitRegNamePools - emit regular name pools - .symtab, .strtab 790 /// 791 /// the size of these tables should be computed before layout 792 /// layout should computes the start offset of these tables emitRegNamePools(Output & pOutput,SymbolCategory & pSymbols,const Layout & pLayout,const MCLDInfo & pLDInfo)793 void GNULDBackend::emitRegNamePools(Output& pOutput, 794 SymbolCategory& pSymbols, 795 const Layout& pLayout, 796 const MCLDInfo& pLDInfo) 797 { 798 799 assert(pOutput.hasMemArea()); 800 801 bool sym_exist = false; 802 HashTableType::entry_type* entry = 0; 803 804 ELFFileFormat* file_format = getOutputFormat(pOutput); 805 if (pOutput.type() == Output::Object) { 806 // add first symbol into m_pSymIndexMap 807 entry = m_pSymIndexMap->insert(NULL, sym_exist); 808 entry->setValue(0); 809 810 // TODO: not support yet 811 return; 812 } 813 814 LDSection& symtab_sect = file_format->getSymTab(); 815 LDSection& strtab_sect = file_format->getStrTab(); 816 817 MemoryRegion* symtab_region = pOutput.memArea()->request(symtab_sect.offset(), 818 symtab_sect.size()); 819 MemoryRegion* strtab_region = pOutput.memArea()->request(strtab_sect.offset(), 820 strtab_sect.size()); 821 822 // set up symtab_region 823 llvm::ELF::Elf32_Sym* symtab32 = NULL; 824 llvm::ELF::Elf64_Sym* symtab64 = NULL; 825 if (32 == bitclass()) 826 symtab32 = (llvm::ELF::Elf32_Sym*)symtab_region->start(); 827 else if (64 == bitclass()) 828 symtab64 = (llvm::ELF::Elf64_Sym*)symtab_region->start(); 829 else 830 llvm::report_fatal_error(llvm::Twine("unsupported bitclass ") + 831 llvm::Twine(bitclass()) + 832 llvm::Twine(".\n")); 833 // set up strtab_region 834 char* strtab = (char*)strtab_region->start(); 835 strtab[0] = '\0'; 836 837 // initialize the first ELF symbol 838 if (32 == bitclass()) { 839 symtab32[0].st_name = 0; 840 symtab32[0].st_value = 0; 841 symtab32[0].st_size = 0; 842 symtab32[0].st_info = 0; 843 symtab32[0].st_other = 0; 844 symtab32[0].st_shndx = 0; 845 } 846 else { // must 64 847 symtab64[0].st_name = 0; 848 symtab64[0].st_value = 0; 849 symtab64[0].st_size = 0; 850 symtab64[0].st_info = 0; 851 symtab64[0].st_other = 0; 852 symtab64[0].st_shndx = 0; 853 } 854 855 size_t symtabIdx = 1; 856 size_t strtabsize = 1; 857 // compute size of .symtab, .dynsym and .strtab 858 SymbolCategory::iterator symbol; 859 SymbolCategory::iterator symEnd = pSymbols.end(); 860 for (symbol = pSymbols.begin(); symbol != symEnd; ++symbol) { 861 862 // maintain output's symbol and index map if building .o file 863 if (Output::Object == pOutput.type()) { 864 entry = m_pSymIndexMap->insert(NULL, sym_exist); 865 entry->setValue(symtabIdx); 866 } 867 868 // FIXME: check the endian between host and target 869 // write out symbol 870 if (32 == bitclass()) { 871 symtab32[symtabIdx].st_name = strtabsize; 872 symtab32[symtabIdx].st_value = getSymbolValue(**symbol); 873 symtab32[symtabIdx].st_size = getSymbolSize(**symbol); 874 symtab32[symtabIdx].st_info = getSymbolInfo(**symbol); 875 symtab32[symtabIdx].st_other = (*symbol)->visibility(); 876 symtab32[symtabIdx].st_shndx = getSymbolShndx(**symbol, pLayout); 877 } 878 else { // must 64 879 symtab64[symtabIdx].st_name = strtabsize; 880 symtab64[symtabIdx].st_value = getSymbolValue(**symbol); 881 symtab64[symtabIdx].st_size = getSymbolSize(**symbol); 882 symtab64[symtabIdx].st_info = getSymbolInfo(**symbol); 883 symtab64[symtabIdx].st_other = (*symbol)->visibility(); 884 symtab64[symtabIdx].st_shndx = getSymbolShndx(**symbol, pLayout); 885 } 886 // write out string 887 strcpy((strtab + strtabsize), (*symbol)->name()); 888 889 // write out 890 // sum up counters 891 ++symtabIdx; 892 strtabsize += (*symbol)->nameSize() + 1; 893 } 894 } 895 896 /// emitNamePools - emit dynamic name pools - .dyntab, .dynstr, .hash 897 /// 898 /// the size of these tables should be computed before layout 899 /// layout should computes the start offset of these tables emitDynNamePools(Output & pOutput,SymbolCategory & pSymbols,const Layout & pLayout,const MCLDInfo & pLDInfo)900 void GNULDBackend::emitDynNamePools(Output& pOutput, 901 SymbolCategory& pSymbols, 902 const Layout& pLayout, 903 const MCLDInfo& pLDInfo) 904 { 905 assert(pOutput.hasMemArea()); 906 ELFFileFormat* file_format = getOutputFormat(pOutput); 907 908 bool sym_exist = false; 909 HashTableType::entry_type* entry = 0; 910 911 LDSection& symtab_sect = file_format->getDynSymTab(); 912 LDSection& strtab_sect = file_format->getDynStrTab(); 913 LDSection& hash_sect = file_format->getHashTab(); 914 LDSection& dyn_sect = file_format->getDynamic(); 915 916 MemoryRegion* symtab_region = pOutput.memArea()->request(symtab_sect.offset(), 917 symtab_sect.size()); 918 MemoryRegion* strtab_region = pOutput.memArea()->request(strtab_sect.offset(), 919 strtab_sect.size()); 920 MemoryRegion* hash_region = pOutput.memArea()->request(hash_sect.offset(), 921 hash_sect.size()); 922 MemoryRegion* dyn_region = pOutput.memArea()->request(dyn_sect.offset(), 923 dyn_sect.size()); 924 // set up symtab_region 925 llvm::ELF::Elf32_Sym* symtab32 = NULL; 926 llvm::ELF::Elf64_Sym* symtab64 = NULL; 927 if (32 == bitclass()) 928 symtab32 = (llvm::ELF::Elf32_Sym*)symtab_region->start(); 929 else if (64 == bitclass()) 930 symtab64 = (llvm::ELF::Elf64_Sym*)symtab_region->start(); 931 else 932 llvm::report_fatal_error(llvm::Twine("unsupported bitclass ") + 933 llvm::Twine(bitclass()) + 934 llvm::Twine(".\n")); 935 936 // initialize the first ELF symbol 937 if (32 == bitclass()) { 938 symtab32[0].st_name = 0; 939 symtab32[0].st_value = 0; 940 symtab32[0].st_size = 0; 941 symtab32[0].st_info = 0; 942 symtab32[0].st_other = 0; 943 symtab32[0].st_shndx = 0; 944 } 945 else { // must 64 946 symtab64[0].st_name = 0; 947 symtab64[0].st_value = 0; 948 symtab64[0].st_size = 0; 949 symtab64[0].st_info = 0; 950 symtab64[0].st_other = 0; 951 symtab64[0].st_shndx = 0; 952 } 953 // set up strtab_region 954 char* strtab = (char*)strtab_region->start(); 955 strtab[0] = '\0'; 956 957 // add the first symbol into m_pSymIndexMap 958 entry = m_pSymIndexMap->insert(NULL, sym_exist); 959 entry->setValue(0); 960 961 size_t symtabIdx = 1; 962 size_t strtabsize = 1; 963 964 // emit of .dynsym, and .dynstr 965 SymbolCategory::iterator symbol; 966 SymbolCategory::iterator symEnd = pSymbols.end(); 967 for (symbol = pSymbols.begin(); symbol != symEnd; ++symbol) { 968 if (!isDynamicSymbol(**symbol, pOutput)) 969 continue; 970 971 // maintain output's symbol and index map 972 entry = m_pSymIndexMap->insert(*symbol, sym_exist); 973 entry->setValue(symtabIdx); 974 975 // FIXME: check the endian between host and target 976 // write out symbol 977 if (32 == bitclass()) { 978 symtab32[symtabIdx].st_name = strtabsize; 979 symtab32[symtabIdx].st_value = (*symbol)->value(); 980 symtab32[symtabIdx].st_size = getSymbolSize(**symbol); 981 symtab32[symtabIdx].st_info = getSymbolInfo(**symbol); 982 symtab32[symtabIdx].st_other = (*symbol)->visibility(); 983 symtab32[symtabIdx].st_shndx = getSymbolShndx(**symbol, pLayout); 984 } 985 else { // must 64 986 symtab64[symtabIdx].st_name = strtabsize; 987 symtab64[symtabIdx].st_value = (*symbol)->value(); 988 symtab64[symtabIdx].st_size = getSymbolSize(**symbol); 989 symtab64[symtabIdx].st_info = getSymbolInfo(**symbol); 990 symtab64[symtabIdx].st_other = (*symbol)->visibility(); 991 symtab64[symtabIdx].st_shndx = getSymbolShndx(**symbol, pLayout); 992 } 993 // write out string 994 strcpy((strtab + strtabsize), (*symbol)->name()); 995 996 // sum up counters 997 ++symtabIdx; 998 strtabsize += (*symbol)->nameSize() + 1; 999 } 1000 1001 // emit DT_NEED 1002 // add DT_NEED strings into .dynstr 1003 // Rules: 1004 // 1. ignore --no-add-needed 1005 // 2. force count in --no-as-needed 1006 // 3. judge --as-needed 1007 ELFDynamic::iterator dt_need = dynamic().needBegin(); 1008 InputTree::const_bfs_iterator input, inputEnd = pLDInfo.inputs().bfs_end(); 1009 for (input = pLDInfo.inputs().bfs_begin(); input != inputEnd; ++input) { 1010 if (Input::DynObj == (*input)->type()) { 1011 // --add-needed 1012 if ((*input)->attribute()->isAddNeeded()) { 1013 // --no-as-needed 1014 if (!(*input)->attribute()->isAsNeeded()) { 1015 strcpy((strtab + strtabsize), (*input)->name().c_str()); 1016 (*dt_need)->setValue(llvm::ELF::DT_NEEDED, strtabsize); 1017 strtabsize += (*input)->name().size() + 1; 1018 ++dt_need; 1019 } 1020 // --as-needed 1021 else if ((*input)->isNeeded()) { 1022 strcpy((strtab + strtabsize), (*input)->name().c_str()); 1023 (*dt_need)->setValue(llvm::ELF::DT_NEEDED, strtabsize); 1024 strtabsize += (*input)->name().size() + 1; 1025 ++dt_need; 1026 } 1027 } 1028 } 1029 } // for 1030 1031 // emit soname 1032 // initialize value of ELF .dynamic section 1033 if (Output::DynObj == pOutput.type()) 1034 dynamic().applySoname(strtabsize); 1035 dynamic().applyEntries(pLDInfo, *file_format); 1036 dynamic().emit(dyn_sect, *dyn_region); 1037 1038 strcpy((strtab + strtabsize), pOutput.name().c_str()); 1039 strtabsize += pOutput.name().size() + 1; 1040 1041 // emit hash table 1042 // FIXME: this verion only emit SVR4 hash section. 1043 // Please add GNU new hash section 1044 1045 // both 32 and 64 bits hash table use 32-bit entry 1046 // set up hash_region 1047 uint32_t* word_array = (uint32_t*)hash_region->start(); 1048 uint32_t& nbucket = word_array[0]; 1049 uint32_t& nchain = word_array[1]; 1050 1051 nbucket = getHashBucketCount(symtabIdx, false); 1052 nchain = symtabIdx; 1053 1054 uint32_t* bucket = (word_array + 2); 1055 uint32_t* chain = (bucket + nbucket); 1056 1057 // initialize bucket 1058 bzero((void*)bucket, nbucket); 1059 1060 StringHash<ELF> hash_func; 1061 1062 if (32 == bitclass()) { 1063 for (size_t sym_idx=0; sym_idx < symtabIdx; ++sym_idx) { 1064 llvm::StringRef name(strtab + symtab32[sym_idx].st_name); 1065 size_t bucket_pos = hash_func(name) % nbucket; 1066 chain[sym_idx] = bucket[bucket_pos]; 1067 bucket[bucket_pos] = sym_idx; 1068 } 1069 } 1070 else if (64 == bitclass()) { 1071 for (size_t sym_idx=0; sym_idx < symtabIdx; ++sym_idx) { 1072 llvm::StringRef name(strtab + symtab64[sym_idx].st_name); 1073 size_t bucket_pos = hash_func(name) % nbucket; 1074 chain[sym_idx] = bucket[bucket_pos]; 1075 bucket[bucket_pos] = sym_idx; 1076 } 1077 } 1078 } 1079 1080 /// sizeInterp - compute the size of the .interp section sizeInterp(const Output & pOutput,const MCLDInfo & pLDInfo)1081 void GNULDBackend::sizeInterp(const Output& pOutput, const MCLDInfo& pLDInfo) 1082 { 1083 assert(pOutput.type() == Output::Exec); 1084 1085 const char* dyld_name; 1086 if (pLDInfo.options().hasDyld()) 1087 dyld_name = pLDInfo.options().dyld().c_str(); 1088 else 1089 dyld_name = dyld(); 1090 1091 LDSection& interp = getExecFileFormat()->getInterp(); 1092 interp.setSize(std::strlen(dyld_name) + 1); 1093 } 1094 1095 /// emitInterp - emit the .interp emitInterp(Output & pOutput,const MCLDInfo & pLDInfo)1096 void GNULDBackend::emitInterp(Output& pOutput, const MCLDInfo& pLDInfo) 1097 { 1098 assert(pOutput.type() == Output::Exec && 1099 getExecFileFormat()->hasInterp() && 1100 pOutput.hasMemArea()); 1101 1102 const LDSection& interp = getExecFileFormat()->getInterp(); 1103 MemoryRegion *region = pOutput.memArea()->request( 1104 interp.offset(), interp.size()); 1105 const char* dyld_name; 1106 if (pLDInfo.options().hasDyld()) 1107 dyld_name = pLDInfo.options().dyld().c_str(); 1108 else 1109 dyld_name = dyld(); 1110 1111 std::memcpy(region->start(), dyld_name, interp.size()); 1112 } 1113 1114 /// getSectionOrder getSectionOrder(const Output & pOutput,const LDSection & pSectHdr,const MCLDInfo & pInfo) const1115 unsigned int GNULDBackend::getSectionOrder(const Output& pOutput, 1116 const LDSection& pSectHdr, 1117 const MCLDInfo& pInfo) const 1118 { 1119 // NULL section should be the "1st" section 1120 if (LDFileFormat::Null == pSectHdr.kind()) 1121 return 0; 1122 1123 // if the section is not ALLOC, lay it out until the last possible moment 1124 if (0 == (pSectHdr.flag() & llvm::ELF::SHF_ALLOC)) 1125 return SHO_UNDEFINED; 1126 1127 bool is_write = (pSectHdr.flag() & llvm::ELF::SHF_WRITE) != 0; 1128 bool is_exec = (pSectHdr.flag() & llvm::ELF::SHF_EXECINSTR) != 0; 1129 const ELFFileFormat* file_format = getOutputFormat(pOutput); 1130 1131 // TODO: need to take care other possible output sections 1132 switch (pSectHdr.kind()) { 1133 case LDFileFormat::Regular: 1134 if (is_exec) { 1135 if (&pSectHdr == &file_format->getInit()) 1136 return SHO_INIT; 1137 if (&pSectHdr == &file_format->getFini()) 1138 return SHO_FINI; 1139 return SHO_TEXT; 1140 } else if (!is_write) { 1141 return SHO_RO; 1142 } else { 1143 if (pInfo.options().hasRelro()) { 1144 if (pSectHdr.type() == llvm::ELF::SHT_PREINIT_ARRAY || 1145 pSectHdr.type() == llvm::ELF::SHT_INIT_ARRAY || 1146 pSectHdr.type() == llvm::ELF::SHT_FINI_ARRAY || 1147 &pSectHdr == &file_format->getCtors() || 1148 &pSectHdr == &file_format->getDtors() || 1149 &pSectHdr == &file_format->getJCR() || 1150 0 == pSectHdr.name().compare(".data.rel.ro")) 1151 return SHO_RELRO; 1152 if (0 == pSectHdr.name().compare(".data.rel.ro.local")) 1153 return SHO_RELRO_LOCAL; 1154 } 1155 return SHO_DATA; 1156 } 1157 1158 case LDFileFormat::BSS: 1159 return SHO_BSS; 1160 1161 case LDFileFormat::NamePool: 1162 if (&pSectHdr == &file_format->getDynamic()) 1163 return SHO_RELRO; 1164 return SHO_NAMEPOOL; 1165 1166 case LDFileFormat::Relocation: 1167 if (&pSectHdr == &file_format->getRelPlt() || 1168 &pSectHdr == &file_format->getRelaPlt()) 1169 return SHO_REL_PLT; 1170 return SHO_RELOCATION; 1171 1172 // get the order from target for target specific sections 1173 case LDFileFormat::Target: 1174 return getTargetSectionOrder(pOutput, pSectHdr, pInfo); 1175 1176 // handle .interp 1177 case LDFileFormat::Note: 1178 return SHO_INTERP; 1179 1180 case LDFileFormat::EhFrame: 1181 case LDFileFormat::EhFrameHdr: 1182 case LDFileFormat::GCCExceptTable: 1183 return SHO_EXCEPTION; 1184 1185 case LDFileFormat::MetaData: 1186 case LDFileFormat::Debug: 1187 default: 1188 return SHO_UNDEFINED; 1189 } 1190 } 1191 1192 /// getSymbolSize getSymbolSize(const LDSymbol & pSymbol) const1193 uint64_t GNULDBackend::getSymbolSize(const LDSymbol& pSymbol) const 1194 { 1195 // @ref Google gold linker: symtab.cc: 2780 1196 // undefined and dynamic symbols should have zero size. 1197 if (pSymbol.isDyn() || pSymbol.desc() == ResolveInfo::Undefined) 1198 return 0x0; 1199 return pSymbol.resolveInfo()->size(); 1200 } 1201 1202 /// getSymbolInfo getSymbolInfo(const LDSymbol & pSymbol) const1203 uint64_t GNULDBackend::getSymbolInfo(const LDSymbol& pSymbol) const 1204 { 1205 // set binding 1206 uint8_t bind = 0x0; 1207 if (pSymbol.resolveInfo()->isLocal()) 1208 bind = llvm::ELF::STB_LOCAL; 1209 else if (pSymbol.resolveInfo()->isGlobal()) 1210 bind = llvm::ELF::STB_GLOBAL; 1211 else if (pSymbol.resolveInfo()->isWeak()) 1212 bind = llvm::ELF::STB_WEAK; 1213 else if (pSymbol.resolveInfo()->isAbsolute()) { 1214 // (Luba) Is a absolute but not global (weak or local) symbol meaningful? 1215 bind = llvm::ELF::STB_GLOBAL; 1216 } 1217 1218 if (pSymbol.visibility() == llvm::ELF::STV_INTERNAL || 1219 pSymbol.visibility() == llvm::ELF::STV_HIDDEN) 1220 bind = llvm::ELF::STB_LOCAL; 1221 1222 uint32_t type = pSymbol.resolveInfo()->type(); 1223 // if the IndirectFunc symbol (i.e., STT_GNU_IFUNC) is from dynobj, change 1224 // its type to Function 1225 if (type == ResolveInfo::IndirectFunc && pSymbol.isDyn()) 1226 type = ResolveInfo::Function; 1227 return (type | (bind << 4)); 1228 } 1229 1230 /// getSymbolValue - this function is called after layout() getSymbolValue(const LDSymbol & pSymbol) const1231 uint64_t GNULDBackend::getSymbolValue(const LDSymbol& pSymbol) const 1232 { 1233 if (pSymbol.isDyn()) 1234 return 0x0; 1235 1236 return pSymbol.value(); 1237 } 1238 1239 /// getSymbolShndx - this function is called after layout() 1240 uint64_t getSymbolShndx(const LDSymbol & pSymbol,const Layout & pLayout) const1241 GNULDBackend::getSymbolShndx(const LDSymbol& pSymbol, const Layout& pLayout) const 1242 { 1243 if (pSymbol.resolveInfo()->isAbsolute()) 1244 return llvm::ELF::SHN_ABS; 1245 if (pSymbol.resolveInfo()->isCommon()) 1246 return llvm::ELF::SHN_COMMON; 1247 if (pSymbol.resolveInfo()->isUndef() || pSymbol.isDyn()) 1248 return llvm::ELF::SHN_UNDEF; 1249 1250 if (pSymbol.resolveInfo()->isLocal()) { 1251 switch (pSymbol.type()) { 1252 case ResolveInfo::NoType: 1253 case ResolveInfo::File: 1254 return llvm::ELF::SHN_ABS; 1255 } 1256 } 1257 1258 assert(pSymbol.hasFragRef() && "symbols must have fragment reference to get its index"); 1259 return pLayout.getOutputLDSection(*pSymbol.fragRef()->frag())->index(); 1260 } 1261 1262 /// getSymbolIdx - called by emitRelocation to get the ouput symbol table index getSymbolIdx(LDSymbol * pSymbol) const1263 size_t GNULDBackend::getSymbolIdx(LDSymbol* pSymbol) const 1264 { 1265 HashTableType::iterator entry = m_pSymIndexMap->find(pSymbol); 1266 return entry.getEntry()->value(); 1267 } 1268 1269 /// allocateCommonSymbols - allocate common symbols in the corresponding 1270 /// sections. 1271 /// @refer Google gold linker: common.cc: 214 1272 bool allocateCommonSymbols(const MCLDInfo & pInfo,MCLinker & pLinker) const1273 GNULDBackend::allocateCommonSymbols(const MCLDInfo& pInfo, MCLinker& pLinker) const 1274 { 1275 SymbolCategory& symbol_list = pLinker.getOutputSymbols(); 1276 1277 if (symbol_list.emptyCommons() && symbol_list.emptyLocals()) 1278 return true; 1279 1280 SymbolCategory::iterator com_sym, com_end; 1281 1282 // FIXME: If the order of common symbols is defined, then sort common symbols 1283 // std::sort(com_sym, com_end, some kind of order); 1284 1285 // get or create corresponding BSS LDSection 1286 LDSection* bss_sect = &pLinker.getOrCreateOutputSectHdr(".bss", 1287 LDFileFormat::BSS, 1288 llvm::ELF::SHT_NOBITS, 1289 llvm::ELF::SHF_WRITE | llvm::ELF::SHF_ALLOC); 1290 1291 LDSection* tbss_sect = &pLinker.getOrCreateOutputSectHdr( 1292 ".tbss", 1293 LDFileFormat::BSS, 1294 llvm::ELF::SHT_NOBITS, 1295 llvm::ELF::SHF_WRITE | llvm::ELF::SHF_ALLOC); 1296 1297 assert(NULL != bss_sect && NULL !=tbss_sect); 1298 1299 // get or create corresponding BSS SectionData 1300 SectionData& bss_sect_data = pLinker.getOrCreateSectData(*bss_sect); 1301 SectionData& tbss_sect_data = pLinker.getOrCreateSectData(*tbss_sect); 1302 1303 // remember original BSS size 1304 uint64_t bss_offset = bss_sect->size(); 1305 uint64_t tbss_offset = tbss_sect->size(); 1306 1307 // allocate all local common symbols 1308 com_end = symbol_list.localEnd(); 1309 1310 for (com_sym = symbol_list.localBegin(); com_sym != com_end; ++com_sym) { 1311 if (ResolveInfo::Common == (*com_sym)->desc()) { 1312 // We have to reset the description of the symbol here. When doing 1313 // incremental linking, the output relocatable object may have common 1314 // symbols. Therefore, we can not treat common symbols as normal symbols 1315 // when emitting the regular name pools. We must change the symbols' 1316 // description here. 1317 (*com_sym)->resolveInfo()->setDesc(ResolveInfo::Define); 1318 Fragment* frag = new FillFragment(0x0, 1, (*com_sym)->size()); 1319 (*com_sym)->setFragmentRef(new FragmentRef(*frag, 0)); 1320 1321 if (ResolveInfo::ThreadLocal == (*com_sym)->type()) { 1322 // allocate TLS common symbol in tbss section 1323 tbss_offset += pLinker.getLayout().appendFragment(*frag, 1324 tbss_sect_data, 1325 (*com_sym)->value()); 1326 } 1327 else { 1328 bss_offset += pLinker.getLayout().appendFragment(*frag, 1329 bss_sect_data, 1330 (*com_sym)->value()); 1331 } 1332 } 1333 } 1334 1335 // allocate all global common symbols 1336 com_end = symbol_list.commonEnd(); 1337 for (com_sym = symbol_list.commonBegin(); com_sym != com_end; ++com_sym) { 1338 // We have to reset the description of the symbol here. When doing 1339 // incremental linking, the output relocatable object may have common 1340 // symbols. Therefore, we can not treat common symbols as normal symbols 1341 // when emitting the regular name pools. We must change the symbols' 1342 // description here. 1343 (*com_sym)->resolveInfo()->setDesc(ResolveInfo::Define); 1344 Fragment* frag = new FillFragment(0x0, 1, (*com_sym)->size()); 1345 (*com_sym)->setFragmentRef(new FragmentRef(*frag, 0)); 1346 1347 if (ResolveInfo::ThreadLocal == (*com_sym)->type()) { 1348 // allocate TLS common symbol in tbss section 1349 tbss_offset += pLinker.getLayout().appendFragment(*frag, 1350 tbss_sect_data, 1351 (*com_sym)->value()); 1352 } 1353 else { 1354 bss_offset += pLinker.getLayout().appendFragment(*frag, 1355 bss_sect_data, 1356 (*com_sym)->value()); 1357 } 1358 } 1359 1360 bss_sect->setSize(bss_offset); 1361 tbss_sect->setSize(tbss_offset); 1362 symbol_list.changeCommonsToGlobal(); 1363 return true; 1364 } 1365 1366 1367 /// createProgramHdrs - base on output sections to create the program headers createProgramHdrs(Output & pOutput,const MCLDInfo & pInfo)1368 void GNULDBackend::createProgramHdrs(Output& pOutput, const MCLDInfo& pInfo) 1369 { 1370 assert(pOutput.hasContext()); 1371 ELFFileFormat *file_format = getOutputFormat(pOutput); 1372 1373 // make PT_PHDR 1374 m_ELFSegmentTable.produce(llvm::ELF::PT_PHDR); 1375 1376 // make PT_INTERP 1377 if (file_format->hasInterp()) { 1378 ELFSegment* interp_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_INTERP); 1379 interp_seg->addSection(&file_format->getInterp()); 1380 } 1381 1382 // FIXME: Should we consider -z relro here? 1383 if (pInfo.options().hasRelro()) { 1384 // if -z relro is given, we need to adjust sections' offset again, and let 1385 // PT_GNU_RELRO end on a common page boundary 1386 LDContext::SectionTable& sect_table = pOutput.context()->getSectionTable(); 1387 1388 size_t idx; 1389 for (idx = 0; idx < pOutput.context()->numOfSections(); ++idx) { 1390 // find the first non-relro section 1391 if (getSectionOrder(pOutput, *sect_table[idx], pInfo) > SHO_RELRO_LAST) { 1392 break; 1393 } 1394 } 1395 1396 // align the first non-relro section to page boundary 1397 uint64_t offset = sect_table[idx]->offset(); 1398 alignAddress(offset, commonPageSize(pInfo)); 1399 sect_table[idx]->setOffset(offset); 1400 1401 // set up remaining section's offset 1402 for (++idx; idx < pOutput.context()->numOfSections(); ++idx) { 1403 uint64_t offset; 1404 size_t prev_idx = idx - 1; 1405 if (LDFileFormat::BSS == sect_table[prev_idx]->kind()) 1406 offset = sect_table[prev_idx]->offset(); 1407 else 1408 offset = sect_table[prev_idx]->offset() + sect_table[prev_idx]->size(); 1409 1410 alignAddress(offset, sect_table[idx]->align()); 1411 sect_table[idx]->setOffset(offset); 1412 } 1413 } // relro 1414 1415 uint32_t cur_seg_flag, prev_seg_flag = getSegmentFlag(0); 1416 uint64_t padding = 0; 1417 ELFSegment* load_seg = NULL; 1418 // make possible PT_LOAD segments 1419 LDContext::sect_iterator sect, sect_end = pOutput.context()->sectEnd(); 1420 for (sect = pOutput.context()->sectBegin(); sect != sect_end; ++sect) { 1421 1422 if (0 == ((*sect)->flag() & llvm::ELF::SHF_ALLOC) && 1423 LDFileFormat::Null != (*sect)->kind()) 1424 continue; 1425 1426 // FIXME: Now only separate writable and non-writable PT_LOAD 1427 cur_seg_flag = getSegmentFlag((*sect)->flag()); 1428 if ((prev_seg_flag & llvm::ELF::PF_W) ^ (cur_seg_flag & llvm::ELF::PF_W) || 1429 LDFileFormat::Null == (*sect)->kind()) { 1430 // create new PT_LOAD segment 1431 load_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_LOAD); 1432 load_seg->setAlign(abiPageSize(pInfo)); 1433 1434 // check if this segment needs padding 1435 padding = 0; 1436 if (((*sect)->offset() & (abiPageSize(pInfo) - 1)) != 0) 1437 padding = abiPageSize(pInfo); 1438 } 1439 1440 assert(NULL != load_seg); 1441 load_seg->addSection((*sect)); 1442 if (cur_seg_flag != prev_seg_flag) 1443 load_seg->updateFlag(cur_seg_flag); 1444 1445 if (LDFileFormat::Null != (*sect)->kind()) 1446 (*sect)->setAddr(segmentStartAddr(pOutput, pInfo) + 1447 (*sect)->offset() + 1448 padding); 1449 1450 prev_seg_flag = cur_seg_flag; 1451 } 1452 1453 // make PT_DYNAMIC 1454 if (file_format->hasDynamic()) { 1455 ELFSegment* dyn_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_DYNAMIC, 1456 llvm::ELF::PF_R | 1457 llvm::ELF::PF_W); 1458 dyn_seg->addSection(&file_format->getDynamic()); 1459 } 1460 1461 if (pInfo.options().hasRelro()) { 1462 // make PT_GNU_RELRO 1463 ELFSegment* relro_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_GNU_RELRO); 1464 for (LDContext::sect_iterator sect = pOutput.context()->sectBegin(); 1465 sect != pOutput.context()->sectEnd(); ++sect) { 1466 unsigned int order = getSectionOrder(pOutput, **sect, pInfo); 1467 if (SHO_RELRO_LOCAL == order || 1468 SHO_RELRO == order || 1469 SHO_RELRO_LAST == order) { 1470 relro_seg->addSection(*sect); 1471 } 1472 } 1473 } 1474 1475 // make PT_GNU_EH_FRAME 1476 if (file_format->hasEhFrameHdr()) { 1477 ELFSegment* eh_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_GNU_EH_FRAME); 1478 eh_seg->addSection(&file_format->getEhFrameHdr()); 1479 } 1480 } 1481 1482 /// setupProgramHdrs - set up the attributes of segments setupProgramHdrs(const Output & pOutput,const MCLDInfo & pInfo)1483 void GNULDBackend:: setupProgramHdrs(const Output& pOutput, const MCLDInfo& pInfo) 1484 { 1485 // update segment info 1486 ELFSegmentFactory::iterator seg, seg_end = m_ELFSegmentTable.end(); 1487 for (seg = m_ELFSegmentTable.begin(); seg != seg_end; ++seg) { 1488 ELFSegment& segment = *seg; 1489 1490 // update PT_PHDR 1491 if (llvm::ELF::PT_PHDR == segment.type()) { 1492 uint64_t offset, phdr_size; 1493 if (32 == bitclass()) { 1494 offset = sizeof(llvm::ELF::Elf32_Ehdr); 1495 phdr_size = sizeof(llvm::ELF::Elf32_Phdr); 1496 } 1497 else { 1498 offset = sizeof(llvm::ELF::Elf64_Ehdr); 1499 phdr_size = sizeof(llvm::ELF::Elf64_Phdr); 1500 } 1501 segment.setOffset(offset); 1502 segment.setVaddr(segmentStartAddr(pOutput, pInfo) + offset); 1503 segment.setPaddr(segment.vaddr()); 1504 segment.setFilesz(numOfSegments() * phdr_size); 1505 segment.setMemsz(numOfSegments() * phdr_size); 1506 segment.setAlign(bitclass() / 8); 1507 continue; 1508 } 1509 1510 // bypass if there is no section in this segment (e.g., PT_GNU_STACK) 1511 if (segment.numOfSections() == 0) 1512 continue; 1513 1514 segment.setOffset(segment.getFirstSection()->offset()); 1515 if (llvm::ELF::PT_LOAD == segment.type() && 1516 LDFileFormat::Null == segment.getFirstSection()->kind()) 1517 segment.setVaddr(segmentStartAddr(pOutput, pInfo)); 1518 else 1519 segment.setVaddr(segment.getFirstSection()->addr()); 1520 segment.setPaddr(segment.vaddr()); 1521 1522 const LDSection* last_sect = segment.getLastSection(); 1523 assert(NULL != last_sect); 1524 uint64_t file_size = last_sect->offset() - segment.offset(); 1525 if (LDFileFormat::BSS != last_sect->kind()) 1526 file_size += last_sect->size(); 1527 segment.setFilesz(file_size); 1528 1529 segment.setMemsz(last_sect->addr() - segment.vaddr() + last_sect->size()); 1530 } 1531 } 1532 1533 /// createGNUStackInfo - create an output GNU stack section or segment if needed 1534 /// @ref gold linker: layout.cc:2608 createGNUStackInfo(const Output & pOutput,const MCLDInfo & pInfo,MCLinker & pLinker)1535 void GNULDBackend::createGNUStackInfo(const Output& pOutput, 1536 const MCLDInfo& pInfo, 1537 MCLinker& pLinker) 1538 { 1539 uint32_t flag = 0x0; 1540 if (pInfo.options().hasStackSet()) { 1541 // 1. check the command line option (-z execstack or -z noexecstack) 1542 if (pInfo.options().hasExecStack()) 1543 flag = llvm::ELF::SHF_EXECINSTR; 1544 } else { 1545 // 2. check the stack info from the input objects 1546 size_t object_count = 0, stack_note_count = 0; 1547 mcld::InputTree::const_bfs_iterator input, inEnd = pInfo.inputs().bfs_end(); 1548 for (input=pInfo.inputs().bfs_begin(); input!=inEnd; ++input) { 1549 if ((*input)->type() == Input::Object) { 1550 ++object_count; 1551 const LDSection* sect = (*input)->context()->getSection( 1552 ".note.GNU-stack"); 1553 if (NULL != sect) { 1554 ++stack_note_count; 1555 // 2.1 found a stack note that is set as executable 1556 if (0 != (llvm::ELF::SHF_EXECINSTR & sect->flag())) { 1557 flag = llvm::ELF::SHF_EXECINSTR; 1558 break; 1559 } 1560 } 1561 } 1562 } 1563 1564 // 2.2 there are no stack note sections in all input objects 1565 if (0 == stack_note_count) 1566 return; 1567 1568 // 2.3 a special case. Use the target default to decide if the stack should 1569 // be executable 1570 if (llvm::ELF::SHF_EXECINSTR != flag && object_count != stack_note_count) 1571 if (isDefaultExecStack()) 1572 flag = llvm::ELF::SHF_EXECINSTR; 1573 } 1574 1575 if (pOutput.type() != Output::Object) 1576 m_ELFSegmentTable.produce(llvm::ELF::PT_GNU_STACK, 1577 llvm::ELF::PF_R | 1578 llvm::ELF::PF_W | 1579 getSegmentFlag(flag)); 1580 else 1581 pLinker.getOrCreateOutputSectHdr(".note.GNU-stack", 1582 LDFileFormat::Note, 1583 llvm::ELF::SHT_PROGBITS, 1584 flag); 1585 } 1586 1587 /// preLayout - Backend can do any needed modification before layout preLayout(const Output & pOutput,const MCLDInfo & pLDInfo,MCLinker & pLinker)1588 void GNULDBackend::preLayout(const Output& pOutput, 1589 const MCLDInfo& pLDInfo, 1590 MCLinker& pLinker) 1591 { 1592 // prelayout target first 1593 doPreLayout(pOutput, pLDInfo, pLinker); 1594 1595 if (pLDInfo.options().hasEhFrameHdr()) { 1596 // init EhFrameHdr and size the output section 1597 ELFFileFormat* format = getOutputFormat(pOutput); 1598 assert(NULL != getEhFrame()); 1599 m_pEhFrameHdr = new EhFrameHdr(*getEhFrame(), 1600 format->getEhFrame(), 1601 format->getEhFrameHdr()); 1602 m_pEhFrameHdr->sizeOutput(); 1603 } 1604 } 1605 1606 /// postLayout - Backend can do any needed modification after layout postLayout(const Output & pOutput,const MCLDInfo & pInfo,MCLinker & pLinker)1607 void GNULDBackend::postLayout(const Output& pOutput, 1608 const MCLDInfo& pInfo, 1609 MCLinker& pLinker) 1610 { 1611 // 1. emit program headers 1612 if (pOutput.type() != Output::Object) { 1613 // 1.1 create program headers 1614 createProgramHdrs(pLinker.getLDInfo().output(), pInfo); 1615 } 1616 1617 // 1.2 create special GNU Stack note section or segment 1618 createGNUStackInfo(pOutput, pInfo, pLinker); 1619 1620 if (pOutput.type() != Output::Object) { 1621 // 1.3 set up the attributes of program headers 1622 setupProgramHdrs(pOutput, pInfo); 1623 } 1624 1625 // 2. target specific post layout 1626 doPostLayout(pOutput, pInfo, pLinker); 1627 } 1628 postProcessing(const Output & pOutput,const MCLDInfo & pInfo,MCLinker & pLinker)1629 void GNULDBackend::postProcessing(const Output& pOutput, 1630 const MCLDInfo& pInfo, 1631 MCLinker& pLinker) 1632 { 1633 if (pInfo.options().hasEhFrameHdr()) { 1634 // emit eh_frame_hdr 1635 if (bitclass() == 32) 1636 m_pEhFrameHdr->emitOutput<32>(pLinker.getLDInfo().output(), 1637 pLinker); 1638 } 1639 } 1640 1641 /// getHashBucketCount - calculate hash bucket count. 1642 /// @ref Google gold linker, dynobj.cc:791 getHashBucketCount(unsigned pNumOfSymbols,bool pIsGNUStyle)1643 unsigned GNULDBackend::getHashBucketCount(unsigned pNumOfSymbols, 1644 bool pIsGNUStyle) 1645 { 1646 // @ref Google gold, dynobj.cc:loc 791 1647 static const unsigned int buckets[] = 1648 { 1649 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, 1650 16411, 32771, 65537, 131101, 262147 1651 }; 1652 const unsigned buckets_count = sizeof buckets / sizeof buckets[0]; 1653 1654 unsigned int result = 1; 1655 for (unsigned i = 0; i < buckets_count; ++i) { 1656 if (pNumOfSymbols < buckets[i]) 1657 break; 1658 result = buckets[i]; 1659 } 1660 1661 if (pIsGNUStyle && result < 2) 1662 result = 2; 1663 1664 return result; 1665 } 1666 1667 /// isDynamicSymbol 1668 /// @ref Google gold linker: symtab.cc:311 isDynamicSymbol(const LDSymbol & pSymbol,const Output & pOutput)1669 bool GNULDBackend::isDynamicSymbol(const LDSymbol& pSymbol, 1670 const Output& pOutput) 1671 { 1672 // If a local symbol is in the LDContext's symbol table, it's a real local 1673 // symbol. We should not add it 1674 if (pSymbol.binding() == ResolveInfo::Local) 1675 return false; 1676 1677 // If we are building shared object, and the visibility is external, we 1678 // need to add it. 1679 if (Output::DynObj == pOutput.type() || Output::Exec == pOutput.type()) 1680 if (pSymbol.resolveInfo()->visibility() == ResolveInfo::Default || 1681 pSymbol.resolveInfo()->visibility() == ResolveInfo::Protected) 1682 return true; 1683 return false; 1684 } 1685 1686 /// commonPageSize - the common page size of the target machine. 1687 /// @ref gold linker: target.h:135 commonPageSize(const MCLDInfo & pInfo) const1688 uint64_t GNULDBackend::commonPageSize(const MCLDInfo& pInfo) const 1689 { 1690 if (pInfo.options().commPageSize() > 0) 1691 return std::min(pInfo.options().commPageSize(), abiPageSize(pInfo)); 1692 else 1693 return std::min(static_cast<uint64_t>(0x1000), abiPageSize(pInfo)); 1694 } 1695 1696 /// abiPageSize - the abi page size of the target machine. 1697 /// @ref gold linker: target.h:125 abiPageSize(const MCLDInfo & pInfo) const1698 uint64_t GNULDBackend::abiPageSize(const MCLDInfo& pInfo) const 1699 { 1700 if (pInfo.options().maxPageSize() > 0) 1701 return pInfo.options().maxPageSize(); 1702 else 1703 return static_cast<uint64_t>(0x1000); 1704 } 1705 1706 /// isOutputPIC - return whether the output is position-independent isOutputPIC(const Output & pOutput,const MCLDInfo & pInfo) const1707 bool GNULDBackend::isOutputPIC(const Output& pOutput, 1708 const MCLDInfo& pInfo) const 1709 { 1710 if (Output::DynObj == pOutput.type() || pInfo.options().isPIE()) 1711 return true; 1712 return false; 1713 } 1714 1715 /// isStaticLink - return whether we're doing static link isStaticLink(const Output & pOutput,const MCLDInfo & pInfo) const1716 bool GNULDBackend::isStaticLink(const Output& pOutput, 1717 const MCLDInfo& pInfo) const 1718 { 1719 InputTree::const_iterator it = pInfo.inputs().begin(); 1720 if (!isOutputPIC(pOutput, pInfo) && (*it)->attribute()->isStatic()) 1721 return true; 1722 return false; 1723 } 1724 1725 /// isSymbolPreemtible - whether the symbol can be preemted by other 1726 /// link unit 1727 /// @ref Google gold linker, symtab.h:551 isSymbolPreemptible(const ResolveInfo & pSym,const MCLDInfo & pLDInfo,const Output & pOutput) const1728 bool GNULDBackend::isSymbolPreemptible(const ResolveInfo& pSym, 1729 const MCLDInfo& pLDInfo, 1730 const Output& pOutput) const 1731 { 1732 if (pSym.other() != ResolveInfo::Default) 1733 return false; 1734 1735 if (Output::DynObj != pOutput.type()) 1736 return false; 1737 1738 if (pLDInfo.options().Bsymbolic()) 1739 return false; 1740 1741 return true; 1742 } 1743 1744 /// symbolNeedsPLT - return whether the symbol needs a PLT entry 1745 /// @ref Google gold linker, symtab.h:596 symbolNeedsPLT(const ResolveInfo & pSym,const MCLDInfo & pLDInfo,const Output & pOutput) const1746 bool GNULDBackend::symbolNeedsPLT(const ResolveInfo& pSym, 1747 const MCLDInfo& pLDInfo, 1748 const Output& pOutput) const 1749 { 1750 if (pSym.isUndef() && !pSym.isDyn() && pOutput.type() != Output::DynObj) 1751 return false; 1752 1753 // An IndirectFunc symbol (i.e., STT_GNU_IFUNC) always needs a plt entry 1754 if (pSym.type() == ResolveInfo::IndirectFunc) 1755 return true; 1756 1757 if (pSym.type() != ResolveInfo::Function) 1758 return false; 1759 1760 if (isStaticLink(pOutput, pLDInfo) || pLDInfo.options().isPIE()) 1761 return false; 1762 1763 return (pSym.isDyn() || 1764 pSym.isUndef() || 1765 isSymbolPreemptible(pSym, pLDInfo, pOutput)); 1766 } 1767 1768 /// symbolNeedsDynRel - return whether the symbol needs a dynamic relocation 1769 /// @ref Google gold linker, symtab.h:645 symbolNeedsDynRel(const ResolveInfo & pSym,bool pSymHasPLT,const MCLDInfo & pLDInfo,const Output & pOutput,bool isAbsReloc) const1770 bool GNULDBackend::symbolNeedsDynRel(const ResolveInfo& pSym, 1771 bool pSymHasPLT, 1772 const MCLDInfo& pLDInfo, 1773 const Output& pOutput, 1774 bool isAbsReloc) const 1775 { 1776 // an undefined reference in the executables should be statically 1777 // resolved to 0 and no need a dynamic relocation 1778 if (pSym.isUndef() && !pSym.isDyn() && (Output::Exec == pOutput.type())) 1779 return false; 1780 if (pSym.isAbsolute()) 1781 return false; 1782 if (isOutputPIC(pOutput, pLDInfo) && isAbsReloc) 1783 return true; 1784 if (pSymHasPLT && ResolveInfo::Function == pSym.type()) 1785 return false; 1786 if (!isOutputPIC(pOutput, pLDInfo) && pSymHasPLT) 1787 return false; 1788 if (pSym.isDyn() || pSym.isUndef() || 1789 isSymbolPreemptible(pSym, pLDInfo, pOutput)) 1790 return true; 1791 1792 return false; 1793 } 1794 1795 /// symbolNeedsCopyReloc - return whether the symbol needs a copy relocation symbolNeedsCopyReloc(const Layout & pLayout,const Relocation & pReloc,const ResolveInfo & pSym,const MCLDInfo & pLDInfo,const Output & pOutput) const1796 bool GNULDBackend::symbolNeedsCopyReloc(const Layout& pLayout, 1797 const Relocation& pReloc, 1798 const ResolveInfo& pSym, 1799 const MCLDInfo& pLDInfo, 1800 const Output& pOutput) const 1801 { 1802 // only the reference from dynamic executable to non-function symbol in 1803 // the dynamic objects may need copy relocation 1804 if (isOutputPIC(pOutput, pLDInfo) || 1805 !pSym.isDyn() || 1806 pSym.type() == ResolveInfo::Function || 1807 pSym.size() == 0) 1808 return false; 1809 1810 // check if the option -z nocopyreloc is given 1811 if (pLDInfo.options().hasNoCopyReloc()) 1812 return false; 1813 1814 // TODO: Is this check necessary? 1815 // if relocation target place is readonly, a copy relocation is needed 1816 if ((pLayout.getOutputLDSection(*pReloc.targetRef().frag())->flag() & 1817 llvm::ELF::SHF_WRITE) == 0) 1818 return true; 1819 1820 return false; 1821 } 1822 1823