1// powerpc.cc -- powerpc target support for gold. 2 3// Copyright (C) 2008-2017 Free Software Foundation, Inc. 4// Written by David S. Miller <davem@davemloft.net> 5// and David Edelsohn <edelsohn@gnu.org> 6 7// This file is part of gold. 8 9// This program is free software; you can redistribute it and/or modify 10// it under the terms of the GNU General Public License as published by 11// the Free Software Foundation; either version 3 of the License, or 12// (at your option) any later version. 13 14// This program is distributed in the hope that it will be useful, 15// but WITHOUT ANY WARRANTY; without even the implied warranty of 16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17// GNU General Public License for more details. 18 19// You should have received a copy of the GNU General Public License 20// along with this program; if not, write to the Free Software 21// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 22// MA 02110-1301, USA. 23 24#include "gold.h" 25 26#include <set> 27#include <algorithm> 28#include "elfcpp.h" 29#include "dwarf.h" 30#include "parameters.h" 31#include "reloc.h" 32#include "powerpc.h" 33#include "object.h" 34#include "symtab.h" 35#include "layout.h" 36#include "output.h" 37#include "copy-relocs.h" 38#include "target.h" 39#include "target-reloc.h" 40#include "target-select.h" 41#include "tls.h" 42#include "errors.h" 43#include "gc.h" 44 45namespace 46{ 47 48using namespace gold; 49 50template<int size, bool big_endian> 51class Output_data_plt_powerpc; 52 53template<int size, bool big_endian> 54class Output_data_brlt_powerpc; 55 56template<int size, bool big_endian> 57class Output_data_got_powerpc; 58 59template<int size, bool big_endian> 60class Output_data_glink; 61 62template<int size, bool big_endian> 63class Stub_table; 64 65template<int size, bool big_endian> 66class Output_data_save_res; 67 68template<int size, bool big_endian> 69class Target_powerpc; 70 71struct Stub_table_owner 72{ 73 Stub_table_owner() 74 : output_section(NULL), owner(NULL) 75 { } 76 77 Output_section* output_section; 78 const Output_section::Input_section* owner; 79}; 80 81inline bool 82is_branch_reloc(unsigned int r_type); 83 84// Counter incremented on every Powerpc_relobj constructed. 85static uint32_t object_id = 0; 86 87template<int size, bool big_endian> 88class Powerpc_relobj : public Sized_relobj_file<size, big_endian> 89{ 90public: 91 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 92 typedef Unordered_set<Section_id, Section_id_hash> Section_refs; 93 typedef Unordered_map<Address, Section_refs> Access_from; 94 95 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset, 96 const typename elfcpp::Ehdr<size, big_endian>& ehdr) 97 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr), 98 uniq_(object_id++), special_(0), relatoc_(0), toc_(0), 99 has_small_toc_reloc_(false), opd_valid_(false), 100 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(), 101 access_from_map_(), has14_(), stub_table_index_(), st_other_() 102 { 103 this->set_abiversion(0); 104 } 105 106 ~Powerpc_relobj() 107 { } 108 109 // Read the symbols then set up st_other vector. 110 void 111 do_read_symbols(Read_symbols_data*); 112 113 // Arrange to always relocate .toc first. 114 virtual void 115 do_relocate_sections( 116 const Symbol_table* symtab, const Layout* layout, 117 const unsigned char* pshdrs, Output_file* of, 118 typename Sized_relobj_file<size, big_endian>::Views* pviews); 119 120 // The .toc section index. 121 unsigned int 122 toc_shndx() const 123 { 124 return this->toc_; 125 } 126 127 // Mark .toc entry at OFF as not optimizable. 128 void 129 set_no_toc_opt(Address off) 130 { 131 if (this->no_toc_opt_.empty()) 132 this->no_toc_opt_.resize(this->section_size(this->toc_shndx()) 133 / (size / 8)); 134 off /= size / 8; 135 if (off < this->no_toc_opt_.size()) 136 this->no_toc_opt_[off] = true; 137 } 138 139 // Mark the entire .toc as not optimizable. 140 void 141 set_no_toc_opt() 142 { 143 this->no_toc_opt_.resize(1); 144 this->no_toc_opt_[0] = true; 145 } 146 147 // Return true if code using the .toc entry at OFF should not be edited. 148 bool 149 no_toc_opt(Address off) const 150 { 151 if (this->no_toc_opt_.empty()) 152 return false; 153 off /= size / 8; 154 if (off >= this->no_toc_opt_.size()) 155 return true; 156 return this->no_toc_opt_[off]; 157 } 158 159 // The .got2 section shndx. 160 unsigned int 161 got2_shndx() const 162 { 163 if (size == 32) 164 return this->special_; 165 else 166 return 0; 167 } 168 169 // The .opd section shndx. 170 unsigned int 171 opd_shndx() const 172 { 173 if (size == 32) 174 return 0; 175 else 176 return this->special_; 177 } 178 179 // Init OPD entry arrays. 180 void 181 init_opd(size_t opd_size) 182 { 183 size_t count = this->opd_ent_ndx(opd_size); 184 this->opd_ent_.resize(count); 185 } 186 187 // Return section and offset of function entry for .opd + R_OFF. 188 unsigned int 189 get_opd_ent(Address r_off, Address* value = NULL) const 190 { 191 size_t ndx = this->opd_ent_ndx(r_off); 192 gold_assert(ndx < this->opd_ent_.size()); 193 gold_assert(this->opd_ent_[ndx].shndx != 0); 194 if (value != NULL) 195 *value = this->opd_ent_[ndx].off; 196 return this->opd_ent_[ndx].shndx; 197 } 198 199 // Set section and offset of function entry for .opd + R_OFF. 200 void 201 set_opd_ent(Address r_off, unsigned int shndx, Address value) 202 { 203 size_t ndx = this->opd_ent_ndx(r_off); 204 gold_assert(ndx < this->opd_ent_.size()); 205 this->opd_ent_[ndx].shndx = shndx; 206 this->opd_ent_[ndx].off = value; 207 } 208 209 // Return discard flag for .opd + R_OFF. 210 bool 211 get_opd_discard(Address r_off) const 212 { 213 size_t ndx = this->opd_ent_ndx(r_off); 214 gold_assert(ndx < this->opd_ent_.size()); 215 return this->opd_ent_[ndx].discard; 216 } 217 218 // Set discard flag for .opd + R_OFF. 219 void 220 set_opd_discard(Address r_off) 221 { 222 size_t ndx = this->opd_ent_ndx(r_off); 223 gold_assert(ndx < this->opd_ent_.size()); 224 this->opd_ent_[ndx].discard = true; 225 } 226 227 bool 228 opd_valid() const 229 { return this->opd_valid_; } 230 231 void 232 set_opd_valid() 233 { this->opd_valid_ = true; } 234 235 // Examine .rela.opd to build info about function entry points. 236 void 237 scan_opd_relocs(size_t reloc_count, 238 const unsigned char* prelocs, 239 const unsigned char* plocal_syms); 240 241 // Returns true if a code sequence loading a TOC entry can be 242 // converted into code calculating a TOC pointer relative offset. 243 bool 244 make_toc_relative(Target_powerpc<size, big_endian>* target, 245 Address* value); 246 247 // Perform the Sized_relobj_file method, then set up opd info from 248 // .opd relocs. 249 void 250 do_read_relocs(Read_relocs_data*); 251 252 bool 253 do_find_special_sections(Read_symbols_data* sd); 254 255 // Adjust this local symbol value. Return false if the symbol 256 // should be discarded from the output file. 257 bool 258 do_adjust_local_symbol(Symbol_value<size>* lv) const 259 { 260 if (size == 64 && this->opd_shndx() != 0) 261 { 262 bool is_ordinary; 263 if (lv->input_shndx(&is_ordinary) != this->opd_shndx()) 264 return true; 265 if (this->get_opd_discard(lv->input_value())) 266 return false; 267 } 268 return true; 269 } 270 271 Access_from* 272 access_from_map() 273 { return &this->access_from_map_; } 274 275 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd 276 // section at DST_OFF. 277 void 278 add_reference(Relobj* src_obj, 279 unsigned int src_indx, 280 typename elfcpp::Elf_types<size>::Elf_Addr dst_off) 281 { 282 Section_id src_id(src_obj, src_indx); 283 this->access_from_map_[dst_off].insert(src_id); 284 } 285 286 // Add a reference to the code section specified by the .opd entry 287 // at DST_OFF 288 void 289 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off) 290 { 291 size_t ndx = this->opd_ent_ndx(dst_off); 292 if (ndx >= this->opd_ent_.size()) 293 this->opd_ent_.resize(ndx + 1); 294 this->opd_ent_[ndx].gc_mark = true; 295 } 296 297 void 298 process_gc_mark(Symbol_table* symtab) 299 { 300 for (size_t i = 0; i < this->opd_ent_.size(); i++) 301 if (this->opd_ent_[i].gc_mark) 302 { 303 unsigned int shndx = this->opd_ent_[i].shndx; 304 symtab->gc()->worklist().push_back(Section_id(this, shndx)); 305 } 306 } 307 308 // Return offset in output GOT section that this object will use 309 // as a TOC pointer. Won't be just a constant with multi-toc support. 310 Address 311 toc_base_offset() const 312 { return 0x8000; } 313 314 void 315 set_has_small_toc_reloc() 316 { has_small_toc_reloc_ = true; } 317 318 bool 319 has_small_toc_reloc() const 320 { return has_small_toc_reloc_; } 321 322 void 323 set_has_14bit_branch(unsigned int shndx) 324 { 325 if (shndx >= this->has14_.size()) 326 this->has14_.resize(shndx + 1); 327 this->has14_[shndx] = true; 328 } 329 330 bool 331 has_14bit_branch(unsigned int shndx) const 332 { return shndx < this->has14_.size() && this->has14_[shndx]; } 333 334 void 335 set_stub_table(unsigned int shndx, unsigned int stub_index) 336 { 337 if (shndx >= this->stub_table_index_.size()) 338 this->stub_table_index_.resize(shndx + 1, -1); 339 this->stub_table_index_[shndx] = stub_index; 340 } 341 342 Stub_table<size, big_endian>* 343 stub_table(unsigned int shndx) 344 { 345 if (shndx < this->stub_table_index_.size()) 346 { 347 Target_powerpc<size, big_endian>* target 348 = static_cast<Target_powerpc<size, big_endian>*>( 349 parameters->sized_target<size, big_endian>()); 350 unsigned int indx = this->stub_table_index_[shndx]; 351 if (indx < target->stub_tables().size()) 352 return target->stub_tables()[indx]; 353 } 354 return NULL; 355 } 356 357 void 358 clear_stub_table() 359 { 360 this->stub_table_index_.clear(); 361 } 362 363 uint32_t 364 uniq() const 365 { return this->uniq_; } 366 367 int 368 abiversion() const 369 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; } 370 371 // Set ABI version for input and output 372 void 373 set_abiversion(int ver); 374 375 unsigned int 376 ppc64_local_entry_offset(const Symbol* sym) const 377 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); } 378 379 unsigned int 380 ppc64_local_entry_offset(unsigned int symndx) const 381 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); } 382 383private: 384 struct Opd_ent 385 { 386 unsigned int shndx; 387 bool discard : 1; 388 bool gc_mark : 1; 389 Address off; 390 }; 391 392 // Return index into opd_ent_ array for .opd entry at OFF. 393 // .opd entries are 24 bytes long, but they can be spaced 16 bytes 394 // apart when the language doesn't use the last 8-byte word, the 395 // environment pointer. Thus dividing the entry section offset by 396 // 16 will give an index into opd_ent_ that works for either layout 397 // of .opd. (It leaves some elements of the vector unused when .opd 398 // entries are spaced 24 bytes apart, but we don't know the spacing 399 // until relocations are processed, and in any case it is possible 400 // for an object to have some entries spaced 16 bytes apart and 401 // others 24 bytes apart.) 402 size_t 403 opd_ent_ndx(size_t off) const 404 { return off >> 4;} 405 406 // Per object unique identifier 407 uint32_t uniq_; 408 409 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx. 410 unsigned int special_; 411 412 // For 64-bit the .rela.toc and .toc section shdnx. 413 unsigned int relatoc_; 414 unsigned int toc_; 415 416 // For 64-bit, whether this object uses small model relocs to access 417 // the toc. 418 bool has_small_toc_reloc_; 419 420 // Set at the start of gc_process_relocs, when we know opd_ent_ 421 // vector is valid. The flag could be made atomic and set in 422 // do_read_relocs with memory_order_release and then tested with 423 // memory_order_acquire, potentially resulting in fewer entries in 424 // access_from_map_. 425 bool opd_valid_; 426 427 // Header e_flags 428 elfcpp::Elf_Word e_flags_; 429 430 // For 64-bit, an array with one entry per 64-bit word in the .toc 431 // section, set if accesses using that word cannot be optimised. 432 std::vector<bool> no_toc_opt_; 433 434 // The first 8-byte word of an OPD entry gives the address of the 435 // entry point of the function. Relocatable object files have a 436 // relocation on this word. The following vector records the 437 // section and offset specified by these relocations. 438 std::vector<Opd_ent> opd_ent_; 439 440 // References made to this object's .opd section when running 441 // gc_process_relocs for another object, before the opd_ent_ vector 442 // is valid for this object. 443 Access_from access_from_map_; 444 445 // Whether input section has a 14-bit branch reloc. 446 std::vector<bool> has14_; 447 448 // The stub table to use for a given input section. 449 std::vector<unsigned int> stub_table_index_; 450 451 // ELF st_other field for local symbols. 452 std::vector<unsigned char> st_other_; 453}; 454 455template<int size, bool big_endian> 456class Powerpc_dynobj : public Sized_dynobj<size, big_endian> 457{ 458public: 459 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 460 461 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset, 462 const typename elfcpp::Ehdr<size, big_endian>& ehdr) 463 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr), 464 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_() 465 { 466 this->set_abiversion(0); 467 } 468 469 ~Powerpc_dynobj() 470 { } 471 472 // Call Sized_dynobj::do_read_symbols to read the symbols then 473 // read .opd from a dynamic object, filling in opd_ent_ vector, 474 void 475 do_read_symbols(Read_symbols_data*); 476 477 // The .opd section shndx. 478 unsigned int 479 opd_shndx() const 480 { 481 return this->opd_shndx_; 482 } 483 484 // The .opd section address. 485 Address 486 opd_address() const 487 { 488 return this->opd_address_; 489 } 490 491 // Init OPD entry arrays. 492 void 493 init_opd(size_t opd_size) 494 { 495 size_t count = this->opd_ent_ndx(opd_size); 496 this->opd_ent_.resize(count); 497 } 498 499 // Return section and offset of function entry for .opd + R_OFF. 500 unsigned int 501 get_opd_ent(Address r_off, Address* value = NULL) const 502 { 503 size_t ndx = this->opd_ent_ndx(r_off); 504 gold_assert(ndx < this->opd_ent_.size()); 505 gold_assert(this->opd_ent_[ndx].shndx != 0); 506 if (value != NULL) 507 *value = this->opd_ent_[ndx].off; 508 return this->opd_ent_[ndx].shndx; 509 } 510 511 // Set section and offset of function entry for .opd + R_OFF. 512 void 513 set_opd_ent(Address r_off, unsigned int shndx, Address value) 514 { 515 size_t ndx = this->opd_ent_ndx(r_off); 516 gold_assert(ndx < this->opd_ent_.size()); 517 this->opd_ent_[ndx].shndx = shndx; 518 this->opd_ent_[ndx].off = value; 519 } 520 521 int 522 abiversion() const 523 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; } 524 525 // Set ABI version for input and output. 526 void 527 set_abiversion(int ver); 528 529private: 530 // Used to specify extent of executable sections. 531 struct Sec_info 532 { 533 Sec_info(Address start_, Address len_, unsigned int shndx_) 534 : start(start_), len(len_), shndx(shndx_) 535 { } 536 537 bool 538 operator<(const Sec_info& that) const 539 { return this->start < that.start; } 540 541 Address start; 542 Address len; 543 unsigned int shndx; 544 }; 545 546 struct Opd_ent 547 { 548 unsigned int shndx; 549 Address off; 550 }; 551 552 // Return index into opd_ent_ array for .opd entry at OFF. 553 size_t 554 opd_ent_ndx(size_t off) const 555 { return off >> 4;} 556 557 // For 64-bit the .opd section shndx and address. 558 unsigned int opd_shndx_; 559 Address opd_address_; 560 561 // Header e_flags 562 elfcpp::Elf_Word e_flags_; 563 564 // The first 8-byte word of an OPD entry gives the address of the 565 // entry point of the function. Records the section and offset 566 // corresponding to the address. Note that in dynamic objects, 567 // offset is *not* relative to the section. 568 std::vector<Opd_ent> opd_ent_; 569}; 570 571// Powerpc_copy_relocs class. Needed to peek at dynamic relocs the 572// base class will emit. 573 574template<int sh_type, int size, bool big_endian> 575class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian> 576{ 577 public: 578 Powerpc_copy_relocs() 579 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY) 580 { } 581 582 // Emit any saved relocations which turn out to be needed. This is 583 // called after all the relocs have been scanned. 584 void 585 emit(Output_data_reloc<sh_type, true, size, big_endian>*); 586}; 587 588template<int size, bool big_endian> 589class Target_powerpc : public Sized_target<size, big_endian> 590{ 591 public: 592 typedef 593 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section; 594 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 595 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address; 596 static const Address invalid_address = static_cast<Address>(0) - 1; 597 // Offset of tp and dtp pointers from start of TLS block. 598 static const Address tp_offset = 0x7000; 599 static const Address dtp_offset = 0x8000; 600 601 Target_powerpc() 602 : Sized_target<size, big_endian>(&powerpc_info), 603 got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL), 604 glink_(NULL), rela_dyn_(NULL), copy_relocs_(), 605 tlsld_got_offset_(-1U), 606 stub_tables_(), branch_lookup_table_(), branch_info_(), 607 plt_thread_safe_(false), relax_failed_(false), relax_fail_count_(0), 608 stub_group_size_(0), savres_section_(0) 609 { 610 } 611 612 // Process the relocations to determine unreferenced sections for 613 // garbage collection. 614 void 615 gc_process_relocs(Symbol_table* symtab, 616 Layout* layout, 617 Sized_relobj_file<size, big_endian>* object, 618 unsigned int data_shndx, 619 unsigned int sh_type, 620 const unsigned char* prelocs, 621 size_t reloc_count, 622 Output_section* output_section, 623 bool needs_special_offset_handling, 624 size_t local_symbol_count, 625 const unsigned char* plocal_symbols); 626 627 // Scan the relocations to look for symbol adjustments. 628 void 629 scan_relocs(Symbol_table* symtab, 630 Layout* layout, 631 Sized_relobj_file<size, big_endian>* object, 632 unsigned int data_shndx, 633 unsigned int sh_type, 634 const unsigned char* prelocs, 635 size_t reloc_count, 636 Output_section* output_section, 637 bool needs_special_offset_handling, 638 size_t local_symbol_count, 639 const unsigned char* plocal_symbols); 640 641 // Map input .toc section to output .got section. 642 const char* 643 do_output_section_name(const Relobj*, const char* name, size_t* plen) const 644 { 645 if (size == 64 && strcmp(name, ".toc") == 0) 646 { 647 *plen = 4; 648 return ".got"; 649 } 650 return NULL; 651 } 652 653 // Provide linker defined save/restore functions. 654 void 655 define_save_restore_funcs(Layout*, Symbol_table*); 656 657 // No stubs unless a final link. 658 bool 659 do_may_relax() const 660 { return !parameters->options().relocatable(); } 661 662 bool 663 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*); 664 665 void 666 do_plt_fde_location(const Output_data*, unsigned char*, 667 uint64_t*, off_t*) const; 668 669 // Stash info about branches, for stub generation. 670 void 671 push_branch(Powerpc_relobj<size, big_endian>* ppc_object, 672 unsigned int data_shndx, Address r_offset, 673 unsigned int r_type, unsigned int r_sym, Address addend) 674 { 675 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend); 676 this->branch_info_.push_back(info); 677 if (r_type == elfcpp::R_POWERPC_REL14 678 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN 679 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN) 680 ppc_object->set_has_14bit_branch(data_shndx); 681 } 682 683 void 684 do_define_standard_symbols(Symbol_table*, Layout*); 685 686 // Finalize the sections. 687 void 688 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*); 689 690 // Return the value to use for a dynamic which requires special 691 // treatment. 692 uint64_t 693 do_dynsym_value(const Symbol*) const; 694 695 // Return the PLT address to use for a local symbol. 696 uint64_t 697 do_plt_address_for_local(const Relobj*, unsigned int) const; 698 699 // Return the PLT address to use for a global symbol. 700 uint64_t 701 do_plt_address_for_global(const Symbol*) const; 702 703 // Return the offset to use for the GOT_INDX'th got entry which is 704 // for a local tls symbol specified by OBJECT, SYMNDX. 705 int64_t 706 do_tls_offset_for_local(const Relobj* object, 707 unsigned int symndx, 708 unsigned int got_indx) const; 709 710 // Return the offset to use for the GOT_INDX'th got entry which is 711 // for global tls symbol GSYM. 712 int64_t 713 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const; 714 715 void 716 do_function_location(Symbol_location*) const; 717 718 bool 719 do_can_check_for_function_pointers() const 720 { return true; } 721 722 // Adjust -fsplit-stack code which calls non-split-stack code. 723 void 724 do_calls_non_split(Relobj* object, unsigned int shndx, 725 section_offset_type fnoffset, section_size_type fnsize, 726 const unsigned char* prelocs, size_t reloc_count, 727 unsigned char* view, section_size_type view_size, 728 std::string* from, std::string* to) const; 729 730 // Relocate a section. 731 void 732 relocate_section(const Relocate_info<size, big_endian>*, 733 unsigned int sh_type, 734 const unsigned char* prelocs, 735 size_t reloc_count, 736 Output_section* output_section, 737 bool needs_special_offset_handling, 738 unsigned char* view, 739 Address view_address, 740 section_size_type view_size, 741 const Reloc_symbol_changes*); 742 743 // Scan the relocs during a relocatable link. 744 void 745 scan_relocatable_relocs(Symbol_table* symtab, 746 Layout* layout, 747 Sized_relobj_file<size, big_endian>* object, 748 unsigned int data_shndx, 749 unsigned int sh_type, 750 const unsigned char* prelocs, 751 size_t reloc_count, 752 Output_section* output_section, 753 bool needs_special_offset_handling, 754 size_t local_symbol_count, 755 const unsigned char* plocal_symbols, 756 Relocatable_relocs*); 757 758 // Scan the relocs for --emit-relocs. 759 void 760 emit_relocs_scan(Symbol_table* symtab, 761 Layout* layout, 762 Sized_relobj_file<size, big_endian>* object, 763 unsigned int data_shndx, 764 unsigned int sh_type, 765 const unsigned char* prelocs, 766 size_t reloc_count, 767 Output_section* output_section, 768 bool needs_special_offset_handling, 769 size_t local_symbol_count, 770 const unsigned char* plocal_syms, 771 Relocatable_relocs* rr); 772 773 // Emit relocations for a section. 774 void 775 relocate_relocs(const Relocate_info<size, big_endian>*, 776 unsigned int sh_type, 777 const unsigned char* prelocs, 778 size_t reloc_count, 779 Output_section* output_section, 780 typename elfcpp::Elf_types<size>::Elf_Off 781 offset_in_output_section, 782 unsigned char*, 783 Address view_address, 784 section_size_type, 785 unsigned char* reloc_view, 786 section_size_type reloc_view_size); 787 788 // Return whether SYM is defined by the ABI. 789 bool 790 do_is_defined_by_abi(const Symbol* sym) const 791 { 792 return strcmp(sym->name(), "__tls_get_addr") == 0; 793 } 794 795 // Return the size of the GOT section. 796 section_size_type 797 got_size() const 798 { 799 gold_assert(this->got_ != NULL); 800 return this->got_->data_size(); 801 } 802 803 // Get the PLT section. 804 const Output_data_plt_powerpc<size, big_endian>* 805 plt_section() const 806 { 807 gold_assert(this->plt_ != NULL); 808 return this->plt_; 809 } 810 811 // Get the IPLT section. 812 const Output_data_plt_powerpc<size, big_endian>* 813 iplt_section() const 814 { 815 gold_assert(this->iplt_ != NULL); 816 return this->iplt_; 817 } 818 819 // Get the .glink section. 820 const Output_data_glink<size, big_endian>* 821 glink_section() const 822 { 823 gold_assert(this->glink_ != NULL); 824 return this->glink_; 825 } 826 827 Output_data_glink<size, big_endian>* 828 glink_section() 829 { 830 gold_assert(this->glink_ != NULL); 831 return this->glink_; 832 } 833 834 bool has_glink() const 835 { return this->glink_ != NULL; } 836 837 // Get the GOT section. 838 const Output_data_got_powerpc<size, big_endian>* 839 got_section() const 840 { 841 gold_assert(this->got_ != NULL); 842 return this->got_; 843 } 844 845 // Get the GOT section, creating it if necessary. 846 Output_data_got_powerpc<size, big_endian>* 847 got_section(Symbol_table*, Layout*); 848 849 Object* 850 do_make_elf_object(const std::string&, Input_file*, off_t, 851 const elfcpp::Ehdr<size, big_endian>&); 852 853 // Return the number of entries in the GOT. 854 unsigned int 855 got_entry_count() const 856 { 857 if (this->got_ == NULL) 858 return 0; 859 return this->got_size() / (size / 8); 860 } 861 862 // Return the number of entries in the PLT. 863 unsigned int 864 plt_entry_count() const; 865 866 // Return the offset of the first non-reserved PLT entry. 867 unsigned int 868 first_plt_entry_offset() const 869 { 870 if (size == 32) 871 return 0; 872 if (this->abiversion() >= 2) 873 return 16; 874 return 24; 875 } 876 877 // Return the size of each PLT entry. 878 unsigned int 879 plt_entry_size() const 880 { 881 if (size == 32) 882 return 4; 883 if (this->abiversion() >= 2) 884 return 8; 885 return 24; 886 } 887 888 Output_data_save_res<size, big_endian>* 889 savres_section() const 890 { 891 return this->savres_section_; 892 } 893 894 // Add any special sections for this symbol to the gc work list. 895 // For powerpc64, this adds the code section of a function 896 // descriptor. 897 void 898 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const; 899 900 // Handle target specific gc actions when adding a gc reference from 901 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX 902 // and DST_OFF. For powerpc64, this adds a referenc to the code 903 // section of a function descriptor. 904 void 905 do_gc_add_reference(Symbol_table* symtab, 906 Relobj* src_obj, 907 unsigned int src_shndx, 908 Relobj* dst_obj, 909 unsigned int dst_shndx, 910 Address dst_off) const; 911 912 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables; 913 const Stub_tables& 914 stub_tables() const 915 { return this->stub_tables_; } 916 917 const Output_data_brlt_powerpc<size, big_endian>* 918 brlt_section() const 919 { return this->brlt_section_; } 920 921 void 922 add_branch_lookup_table(Address to) 923 { 924 unsigned int off = this->branch_lookup_table_.size() * (size / 8); 925 this->branch_lookup_table_.insert(std::make_pair(to, off)); 926 } 927 928 Address 929 find_branch_lookup_table(Address to) 930 { 931 typename Branch_lookup_table::const_iterator p 932 = this->branch_lookup_table_.find(to); 933 return p == this->branch_lookup_table_.end() ? invalid_address : p->second; 934 } 935 936 void 937 write_branch_lookup_table(unsigned char *oview) 938 { 939 for (typename Branch_lookup_table::const_iterator p 940 = this->branch_lookup_table_.begin(); 941 p != this->branch_lookup_table_.end(); 942 ++p) 943 { 944 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first); 945 } 946 } 947 948 // Wrapper used after relax to define a local symbol in output data, 949 // from the end if value < 0. 950 void 951 define_local(Symbol_table* symtab, const char* name, 952 Output_data* od, Address value, unsigned int symsize) 953 { 954 Symbol* sym 955 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED, 956 od, value, symsize, elfcpp::STT_NOTYPE, 957 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0, 958 static_cast<Signed_address>(value) < 0, 959 false); 960 // We are creating this symbol late, so need to fix up things 961 // done early in Layout::finalize. 962 sym->set_dynsym_index(-1U); 963 } 964 965 bool 966 plt_thread_safe() const 967 { return this->plt_thread_safe_; } 968 969 int 970 abiversion () const 971 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; } 972 973 void 974 set_abiversion (int ver) 975 { 976 elfcpp::Elf_Word flags = this->processor_specific_flags(); 977 flags &= ~elfcpp::EF_PPC64_ABI; 978 flags |= ver & elfcpp::EF_PPC64_ABI; 979 this->set_processor_specific_flags(flags); 980 } 981 982 // Offset to to save stack slot 983 int 984 stk_toc () const 985 { return this->abiversion() < 2 ? 40 : 24; } 986 987 private: 988 989 class Track_tls 990 { 991 public: 992 enum Tls_get_addr 993 { 994 NOT_EXPECTED = 0, 995 EXPECTED = 1, 996 SKIP = 2, 997 NORMAL = 3 998 }; 999 1000 Track_tls() 1001 : tls_get_addr_(NOT_EXPECTED), 1002 relinfo_(NULL), relnum_(0), r_offset_(0) 1003 { } 1004 1005 ~Track_tls() 1006 { 1007 if (this->tls_get_addr_ != NOT_EXPECTED) 1008 this->missing(); 1009 } 1010 1011 void 1012 missing(void) 1013 { 1014 if (this->relinfo_ != NULL) 1015 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_, 1016 _("missing expected __tls_get_addr call")); 1017 } 1018 1019 void 1020 expect_tls_get_addr_call( 1021 const Relocate_info<size, big_endian>* relinfo, 1022 size_t relnum, 1023 Address r_offset) 1024 { 1025 this->tls_get_addr_ = EXPECTED; 1026 this->relinfo_ = relinfo; 1027 this->relnum_ = relnum; 1028 this->r_offset_ = r_offset; 1029 } 1030 1031 void 1032 expect_tls_get_addr_call() 1033 { this->tls_get_addr_ = EXPECTED; } 1034 1035 void 1036 skip_next_tls_get_addr_call() 1037 {this->tls_get_addr_ = SKIP; } 1038 1039 Tls_get_addr 1040 maybe_skip_tls_get_addr_call(unsigned int r_type, const Symbol* gsym) 1041 { 1042 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24 1043 || r_type == elfcpp::R_PPC_PLTREL24) 1044 && gsym != NULL 1045 && strcmp(gsym->name(), "__tls_get_addr") == 0); 1046 Tls_get_addr last_tls = this->tls_get_addr_; 1047 this->tls_get_addr_ = NOT_EXPECTED; 1048 if (is_tls_call && last_tls != EXPECTED) 1049 return last_tls; 1050 else if (!is_tls_call && last_tls != NOT_EXPECTED) 1051 { 1052 this->missing(); 1053 return EXPECTED; 1054 } 1055 return NORMAL; 1056 } 1057 1058 private: 1059 // What we're up to regarding calls to __tls_get_addr. 1060 // On powerpc, the branch and link insn making a call to 1061 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD, 1062 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the 1063 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call. 1064 // The marker relocation always comes first, and has the same 1065 // symbol as the reloc on the insn setting up the __tls_get_addr 1066 // argument. This ties the arg setup insn with the call insn, 1067 // allowing ld to safely optimize away the call. We check that 1068 // every call to __tls_get_addr has a marker relocation, and that 1069 // every marker relocation is on a call to __tls_get_addr. 1070 Tls_get_addr tls_get_addr_; 1071 // Info about the last reloc for error message. 1072 const Relocate_info<size, big_endian>* relinfo_; 1073 size_t relnum_; 1074 Address r_offset_; 1075 }; 1076 1077 // The class which scans relocations. 1078 class Scan : protected Track_tls 1079 { 1080 public: 1081 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 1082 1083 Scan() 1084 : Track_tls(), issued_non_pic_error_(false) 1085 { } 1086 1087 static inline int 1088 get_reference_flags(unsigned int r_type, const Target_powerpc* target); 1089 1090 inline void 1091 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target, 1092 Sized_relobj_file<size, big_endian>* object, 1093 unsigned int data_shndx, 1094 Output_section* output_section, 1095 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type, 1096 const elfcpp::Sym<size, big_endian>& lsym, 1097 bool is_discarded); 1098 1099 inline void 1100 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target, 1101 Sized_relobj_file<size, big_endian>* object, 1102 unsigned int data_shndx, 1103 Output_section* output_section, 1104 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type, 1105 Symbol* gsym); 1106 1107 inline bool 1108 local_reloc_may_be_function_pointer(Symbol_table* , Layout* , 1109 Target_powerpc* , 1110 Sized_relobj_file<size, big_endian>* relobj, 1111 unsigned int , 1112 Output_section* , 1113 const elfcpp::Rela<size, big_endian>& , 1114 unsigned int r_type, 1115 const elfcpp::Sym<size, big_endian>&) 1116 { 1117 // PowerPC64 .opd is not folded, so any identical function text 1118 // may be folded and we'll still keep function addresses distinct. 1119 // That means no reloc is of concern here. 1120 if (size == 64) 1121 { 1122 Powerpc_relobj<size, big_endian>* ppcobj = static_cast 1123 <Powerpc_relobj<size, big_endian>*>(relobj); 1124 if (ppcobj->abiversion() == 1) 1125 return false; 1126 } 1127 // For 32-bit and ELFv2, conservatively assume anything but calls to 1128 // function code might be taking the address of the function. 1129 return !is_branch_reloc(r_type); 1130 } 1131 1132 inline bool 1133 global_reloc_may_be_function_pointer(Symbol_table* , Layout* , 1134 Target_powerpc* , 1135 Sized_relobj_file<size, big_endian>* relobj, 1136 unsigned int , 1137 Output_section* , 1138 const elfcpp::Rela<size, big_endian>& , 1139 unsigned int r_type, 1140 Symbol*) 1141 { 1142 // As above. 1143 if (size == 64) 1144 { 1145 Powerpc_relobj<size, big_endian>* ppcobj = static_cast 1146 <Powerpc_relobj<size, big_endian>*>(relobj); 1147 if (ppcobj->abiversion() == 1) 1148 return false; 1149 } 1150 return !is_branch_reloc(r_type); 1151 } 1152 1153 static bool 1154 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target, 1155 Sized_relobj_file<size, big_endian>* object, 1156 unsigned int r_type, bool report_err); 1157 1158 private: 1159 static void 1160 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*, 1161 unsigned int r_type); 1162 1163 static void 1164 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*, 1165 unsigned int r_type, Symbol*); 1166 1167 static void 1168 generate_tls_call(Symbol_table* symtab, Layout* layout, 1169 Target_powerpc* target); 1170 1171 void 1172 check_non_pic(Relobj*, unsigned int r_type); 1173 1174 // Whether we have issued an error about a non-PIC compilation. 1175 bool issued_non_pic_error_; 1176 }; 1177 1178 bool 1179 symval_for_branch(const Symbol_table* symtab, 1180 const Sized_symbol<size>* gsym, 1181 Powerpc_relobj<size, big_endian>* object, 1182 Address *value, unsigned int *dest_shndx); 1183 1184 // The class which implements relocation. 1185 class Relocate : protected Track_tls 1186 { 1187 public: 1188 // Use 'at' branch hints when true, 'y' when false. 1189 // FIXME maybe: set this with an option. 1190 static const bool is_isa_v2 = true; 1191 1192 Relocate() 1193 : Track_tls() 1194 { } 1195 1196 // Do a relocation. Return false if the caller should not issue 1197 // any warnings about this relocation. 1198 inline bool 1199 relocate(const Relocate_info<size, big_endian>*, unsigned int, 1200 Target_powerpc*, Output_section*, size_t, const unsigned char*, 1201 const Sized_symbol<size>*, const Symbol_value<size>*, 1202 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr, 1203 section_size_type); 1204 }; 1205 1206 class Relocate_comdat_behavior 1207 { 1208 public: 1209 // Decide what the linker should do for relocations that refer to 1210 // discarded comdat sections. 1211 inline Comdat_behavior 1212 get(const char* name) 1213 { 1214 gold::Default_comdat_behavior default_behavior; 1215 Comdat_behavior ret = default_behavior.get(name); 1216 if (ret == CB_WARNING) 1217 { 1218 if (size == 32 1219 && (strcmp(name, ".fixup") == 0 1220 || strcmp(name, ".got2") == 0)) 1221 ret = CB_IGNORE; 1222 if (size == 64 1223 && (strcmp(name, ".opd") == 0 1224 || strcmp(name, ".toc") == 0 1225 || strcmp(name, ".toc1") == 0)) 1226 ret = CB_IGNORE; 1227 } 1228 return ret; 1229 } 1230 }; 1231 1232 // Optimize the TLS relocation type based on what we know about the 1233 // symbol. IS_FINAL is true if the final address of this symbol is 1234 // known at link time. 1235 1236 tls::Tls_optimization 1237 optimize_tls_gd(bool is_final) 1238 { 1239 // If we are generating a shared library, then we can't do anything 1240 // in the linker. 1241 if (parameters->options().shared()) 1242 return tls::TLSOPT_NONE; 1243 1244 if (!is_final) 1245 return tls::TLSOPT_TO_IE; 1246 return tls::TLSOPT_TO_LE; 1247 } 1248 1249 tls::Tls_optimization 1250 optimize_tls_ld() 1251 { 1252 if (parameters->options().shared()) 1253 return tls::TLSOPT_NONE; 1254 1255 return tls::TLSOPT_TO_LE; 1256 } 1257 1258 tls::Tls_optimization 1259 optimize_tls_ie(bool is_final) 1260 { 1261 if (!is_final || parameters->options().shared()) 1262 return tls::TLSOPT_NONE; 1263 1264 return tls::TLSOPT_TO_LE; 1265 } 1266 1267 // Create glink. 1268 void 1269 make_glink_section(Layout*); 1270 1271 // Create the PLT section. 1272 void 1273 make_plt_section(Symbol_table*, Layout*); 1274 1275 void 1276 make_iplt_section(Symbol_table*, Layout*); 1277 1278 void 1279 make_brlt_section(Layout*); 1280 1281 // Create a PLT entry for a global symbol. 1282 void 1283 make_plt_entry(Symbol_table*, Layout*, Symbol*); 1284 1285 // Create a PLT entry for a local IFUNC symbol. 1286 void 1287 make_local_ifunc_plt_entry(Symbol_table*, Layout*, 1288 Sized_relobj_file<size, big_endian>*, 1289 unsigned int); 1290 1291 1292 // Create a GOT entry for local dynamic __tls_get_addr. 1293 unsigned int 1294 tlsld_got_offset(Symbol_table* symtab, Layout* layout, 1295 Sized_relobj_file<size, big_endian>* object); 1296 1297 unsigned int 1298 tlsld_got_offset() const 1299 { 1300 return this->tlsld_got_offset_; 1301 } 1302 1303 // Get the dynamic reloc section, creating it if necessary. 1304 Reloc_section* 1305 rela_dyn_section(Layout*); 1306 1307 // Similarly, but for ifunc symbols get the one for ifunc. 1308 Reloc_section* 1309 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc); 1310 1311 // Copy a relocation against a global symbol. 1312 void 1313 copy_reloc(Symbol_table* symtab, Layout* layout, 1314 Sized_relobj_file<size, big_endian>* object, 1315 unsigned int shndx, Output_section* output_section, 1316 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc) 1317 { 1318 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info()); 1319 this->copy_relocs_.copy_reloc(symtab, layout, 1320 symtab->get_sized_symbol<size>(sym), 1321 object, shndx, output_section, 1322 r_type, reloc.get_r_offset(), 1323 reloc.get_r_addend(), 1324 this->rela_dyn_section(layout)); 1325 } 1326 1327 // Look over all the input sections, deciding where to place stubs. 1328 void 1329 group_sections(Layout*, const Task*, bool); 1330 1331 // Sort output sections by address. 1332 struct Sort_sections 1333 { 1334 bool 1335 operator()(const Output_section* sec1, const Output_section* sec2) 1336 { return sec1->address() < sec2->address(); } 1337 }; 1338 1339 class Branch_info 1340 { 1341 public: 1342 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object, 1343 unsigned int data_shndx, 1344 Address r_offset, 1345 unsigned int r_type, 1346 unsigned int r_sym, 1347 Address addend) 1348 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset), 1349 r_type_(r_type), r_sym_(r_sym), addend_(addend) 1350 { } 1351 1352 ~Branch_info() 1353 { } 1354 1355 // If this branch needs a plt call stub, or a long branch stub, make one. 1356 bool 1357 make_stub(Stub_table<size, big_endian>*, 1358 Stub_table<size, big_endian>*, 1359 Symbol_table*) const; 1360 1361 private: 1362 // The branch location.. 1363 Powerpc_relobj<size, big_endian>* object_; 1364 unsigned int shndx_; 1365 Address offset_; 1366 // ..and the branch type and destination. 1367 unsigned int r_type_; 1368 unsigned int r_sym_; 1369 Address addend_; 1370 }; 1371 1372 // Information about this specific target which we pass to the 1373 // general Target structure. 1374 static Target::Target_info powerpc_info; 1375 1376 // The types of GOT entries needed for this platform. 1377 // These values are exposed to the ABI in an incremental link. 1378 // Do not renumber existing values without changing the version 1379 // number of the .gnu_incremental_inputs section. 1380 enum Got_type 1381 { 1382 GOT_TYPE_STANDARD, 1383 GOT_TYPE_TLSGD, // double entry for @got@tlsgd 1384 GOT_TYPE_DTPREL, // entry for @got@dtprel 1385 GOT_TYPE_TPREL // entry for @got@tprel 1386 }; 1387 1388 // The GOT section. 1389 Output_data_got_powerpc<size, big_endian>* got_; 1390 // The PLT section. This is a container for a table of addresses, 1391 // and their relocations. Each address in the PLT has a dynamic 1392 // relocation (R_*_JMP_SLOT) and each address will have a 1393 // corresponding entry in .glink for lazy resolution of the PLT. 1394 // ppc32 initialises the PLT to point at the .glink entry, while 1395 // ppc64 leaves this to ld.so. To make a call via the PLT, the 1396 // linker adds a stub that loads the PLT entry into ctr then 1397 // branches to ctr. There may be more than one stub for each PLT 1398 // entry. DT_JMPREL points at the first PLT dynamic relocation and 1399 // DT_PLTRELSZ gives the total size of PLT dynamic relocations. 1400 Output_data_plt_powerpc<size, big_endian>* plt_; 1401 // The IPLT section. Like plt_, this is a container for a table of 1402 // addresses and their relocations, specifically for STT_GNU_IFUNC 1403 // functions that resolve locally (STT_GNU_IFUNC functions that 1404 // don't resolve locally go in PLT). Unlike plt_, these have no 1405 // entry in .glink for lazy resolution, and the relocation section 1406 // does not have a 1-1 correspondence with IPLT addresses. In fact, 1407 // the relocation section may contain relocations against 1408 // STT_GNU_IFUNC symbols at locations outside of IPLT. The 1409 // relocation section will appear at the end of other dynamic 1410 // relocations, so that ld.so applies these relocations after other 1411 // dynamic relocations. In a static executable, the relocation 1412 // section is emitted and marked with __rela_iplt_start and 1413 // __rela_iplt_end symbols. 1414 Output_data_plt_powerpc<size, big_endian>* iplt_; 1415 // Section holding long branch destinations. 1416 Output_data_brlt_powerpc<size, big_endian>* brlt_section_; 1417 // The .glink section. 1418 Output_data_glink<size, big_endian>* glink_; 1419 // The dynamic reloc section. 1420 Reloc_section* rela_dyn_; 1421 // Relocs saved to avoid a COPY reloc. 1422 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_; 1423 // Offset of the GOT entry for local dynamic __tls_get_addr calls. 1424 unsigned int tlsld_got_offset_; 1425 1426 Stub_tables stub_tables_; 1427 typedef Unordered_map<Address, unsigned int> Branch_lookup_table; 1428 Branch_lookup_table branch_lookup_table_; 1429 1430 typedef std::vector<Branch_info> Branches; 1431 Branches branch_info_; 1432 1433 bool plt_thread_safe_; 1434 1435 bool relax_failed_; 1436 int relax_fail_count_; 1437 int32_t stub_group_size_; 1438 1439 Output_data_save_res<size, big_endian> *savres_section_; 1440}; 1441 1442template<> 1443Target::Target_info Target_powerpc<32, true>::powerpc_info = 1444{ 1445 32, // size 1446 true, // is_big_endian 1447 elfcpp::EM_PPC, // machine_code 1448 false, // has_make_symbol 1449 false, // has_resolve 1450 false, // has_code_fill 1451 true, // is_default_stack_executable 1452 false, // can_icf_inline_merge_sections 1453 '\0', // wrap_char 1454 "/usr/lib/ld.so.1", // dynamic_linker 1455 0x10000000, // default_text_segment_address 1456 64 * 1024, // abi_pagesize (overridable by -z max-page-size) 1457 4 * 1024, // common_pagesize (overridable by -z common-page-size) 1458 false, // isolate_execinstr 1459 0, // rosegment_gap 1460 elfcpp::SHN_UNDEF, // small_common_shndx 1461 elfcpp::SHN_UNDEF, // large_common_shndx 1462 0, // small_common_section_flags 1463 0, // large_common_section_flags 1464 NULL, // attributes_section 1465 NULL, // attributes_vendor 1466 "_start", // entry_symbol_name 1467 32, // hash_entry_size 1468}; 1469 1470template<> 1471Target::Target_info Target_powerpc<32, false>::powerpc_info = 1472{ 1473 32, // size 1474 false, // is_big_endian 1475 elfcpp::EM_PPC, // machine_code 1476 false, // has_make_symbol 1477 false, // has_resolve 1478 false, // has_code_fill 1479 true, // is_default_stack_executable 1480 false, // can_icf_inline_merge_sections 1481 '\0', // wrap_char 1482 "/usr/lib/ld.so.1", // dynamic_linker 1483 0x10000000, // default_text_segment_address 1484 64 * 1024, // abi_pagesize (overridable by -z max-page-size) 1485 4 * 1024, // common_pagesize (overridable by -z common-page-size) 1486 false, // isolate_execinstr 1487 0, // rosegment_gap 1488 elfcpp::SHN_UNDEF, // small_common_shndx 1489 elfcpp::SHN_UNDEF, // large_common_shndx 1490 0, // small_common_section_flags 1491 0, // large_common_section_flags 1492 NULL, // attributes_section 1493 NULL, // attributes_vendor 1494 "_start", // entry_symbol_name 1495 32, // hash_entry_size 1496}; 1497 1498template<> 1499Target::Target_info Target_powerpc<64, true>::powerpc_info = 1500{ 1501 64, // size 1502 true, // is_big_endian 1503 elfcpp::EM_PPC64, // machine_code 1504 false, // has_make_symbol 1505 false, // has_resolve 1506 false, // has_code_fill 1507 true, // is_default_stack_executable 1508 false, // can_icf_inline_merge_sections 1509 '\0', // wrap_char 1510 "/usr/lib/ld.so.1", // dynamic_linker 1511 0x10000000, // default_text_segment_address 1512 64 * 1024, // abi_pagesize (overridable by -z max-page-size) 1513 4 * 1024, // common_pagesize (overridable by -z common-page-size) 1514 false, // isolate_execinstr 1515 0, // rosegment_gap 1516 elfcpp::SHN_UNDEF, // small_common_shndx 1517 elfcpp::SHN_UNDEF, // large_common_shndx 1518 0, // small_common_section_flags 1519 0, // large_common_section_flags 1520 NULL, // attributes_section 1521 NULL, // attributes_vendor 1522 "_start", // entry_symbol_name 1523 32, // hash_entry_size 1524}; 1525 1526template<> 1527Target::Target_info Target_powerpc<64, false>::powerpc_info = 1528{ 1529 64, // size 1530 false, // is_big_endian 1531 elfcpp::EM_PPC64, // machine_code 1532 false, // has_make_symbol 1533 false, // has_resolve 1534 false, // has_code_fill 1535 true, // is_default_stack_executable 1536 false, // can_icf_inline_merge_sections 1537 '\0', // wrap_char 1538 "/usr/lib/ld.so.1", // dynamic_linker 1539 0x10000000, // default_text_segment_address 1540 64 * 1024, // abi_pagesize (overridable by -z max-page-size) 1541 4 * 1024, // common_pagesize (overridable by -z common-page-size) 1542 false, // isolate_execinstr 1543 0, // rosegment_gap 1544 elfcpp::SHN_UNDEF, // small_common_shndx 1545 elfcpp::SHN_UNDEF, // large_common_shndx 1546 0, // small_common_section_flags 1547 0, // large_common_section_flags 1548 NULL, // attributes_section 1549 NULL, // attributes_vendor 1550 "_start", // entry_symbol_name 1551 32, // hash_entry_size 1552}; 1553 1554inline bool 1555is_branch_reloc(unsigned int r_type) 1556{ 1557 return (r_type == elfcpp::R_POWERPC_REL24 1558 || r_type == elfcpp::R_PPC_PLTREL24 1559 || r_type == elfcpp::R_PPC_LOCAL24PC 1560 || r_type == elfcpp::R_POWERPC_REL14 1561 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN 1562 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN 1563 || r_type == elfcpp::R_POWERPC_ADDR24 1564 || r_type == elfcpp::R_POWERPC_ADDR14 1565 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN 1566 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN); 1567} 1568 1569// If INSN is an opcode that may be used with an @tls operand, return 1570// the transformed insn for TLS optimisation, otherwise return 0. If 1571// REG is non-zero only match an insn with RB or RA equal to REG. 1572uint32_t 1573at_tls_transform(uint32_t insn, unsigned int reg) 1574{ 1575 if ((insn & (0x3f << 26)) != 31 << 26) 1576 return 0; 1577 1578 unsigned int rtra; 1579 if (reg == 0 || ((insn >> 11) & 0x1f) == reg) 1580 rtra = insn & ((1 << 26) - (1 << 16)); 1581 else if (((insn >> 16) & 0x1f) == reg) 1582 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5); 1583 else 1584 return 0; 1585 1586 if ((insn & (0x3ff << 1)) == 266 << 1) 1587 // add -> addi 1588 insn = 14 << 26; 1589 else if ((insn & (0x1f << 1)) == 23 << 1 1590 && ((insn & (0x1f << 6)) < 14 << 6 1591 || ((insn & (0x1f << 6)) >= 16 << 6 1592 && (insn & (0x1f << 6)) < 24 << 6))) 1593 // load and store indexed -> dform 1594 insn = (32 | ((insn >> 6) & 0x1f)) << 26; 1595 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1) 1596 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu 1597 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1); 1598 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1) 1599 // lwax -> lwa 1600 insn = (58 << 26) | 2; 1601 else 1602 return 0; 1603 insn |= rtra; 1604 return insn; 1605} 1606 1607 1608template<int size, bool big_endian> 1609class Powerpc_relocate_functions 1610{ 1611public: 1612 enum Overflow_check 1613 { 1614 CHECK_NONE, 1615 CHECK_SIGNED, 1616 CHECK_UNSIGNED, 1617 CHECK_BITFIELD, 1618 CHECK_LOW_INSN, 1619 CHECK_HIGH_INSN 1620 }; 1621 1622 enum Status 1623 { 1624 STATUS_OK, 1625 STATUS_OVERFLOW 1626 }; 1627 1628private: 1629 typedef Powerpc_relocate_functions<size, big_endian> This; 1630 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 1631 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress; 1632 1633 template<int valsize> 1634 static inline bool 1635 has_overflow_signed(Address value) 1636 { 1637 // limit = 1 << (valsize - 1) without shift count exceeding size of type 1638 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1); 1639 limit <<= ((valsize - 1) >> 1); 1640 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1)); 1641 return value + limit > (limit << 1) - 1; 1642 } 1643 1644 template<int valsize> 1645 static inline bool 1646 has_overflow_unsigned(Address value) 1647 { 1648 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1); 1649 limit <<= ((valsize - 1) >> 1); 1650 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1)); 1651 return value > (limit << 1) - 1; 1652 } 1653 1654 template<int valsize> 1655 static inline bool 1656 has_overflow_bitfield(Address value) 1657 { 1658 return (has_overflow_unsigned<valsize>(value) 1659 && has_overflow_signed<valsize>(value)); 1660 } 1661 1662 template<int valsize> 1663 static inline Status 1664 overflowed(Address value, Overflow_check overflow) 1665 { 1666 if (overflow == CHECK_SIGNED) 1667 { 1668 if (has_overflow_signed<valsize>(value)) 1669 return STATUS_OVERFLOW; 1670 } 1671 else if (overflow == CHECK_UNSIGNED) 1672 { 1673 if (has_overflow_unsigned<valsize>(value)) 1674 return STATUS_OVERFLOW; 1675 } 1676 else if (overflow == CHECK_BITFIELD) 1677 { 1678 if (has_overflow_bitfield<valsize>(value)) 1679 return STATUS_OVERFLOW; 1680 } 1681 return STATUS_OK; 1682 } 1683 1684 // Do a simple RELA relocation 1685 template<int fieldsize, int valsize> 1686 static inline Status 1687 rela(unsigned char* view, Address value, Overflow_check overflow) 1688 { 1689 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype; 1690 Valtype* wv = reinterpret_cast<Valtype*>(view); 1691 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value); 1692 return overflowed<valsize>(value, overflow); 1693 } 1694 1695 template<int fieldsize, int valsize> 1696 static inline Status 1697 rela(unsigned char* view, 1698 unsigned int right_shift, 1699 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask, 1700 Address value, 1701 Overflow_check overflow) 1702 { 1703 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype; 1704 Valtype* wv = reinterpret_cast<Valtype*>(view); 1705 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv); 1706 Valtype reloc = value >> right_shift; 1707 val &= ~dst_mask; 1708 reloc &= dst_mask; 1709 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc); 1710 return overflowed<valsize>(value >> right_shift, overflow); 1711 } 1712 1713 // Do a simple RELA relocation, unaligned. 1714 template<int fieldsize, int valsize> 1715 static inline Status 1716 rela_ua(unsigned char* view, Address value, Overflow_check overflow) 1717 { 1718 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value); 1719 return overflowed<valsize>(value, overflow); 1720 } 1721 1722 template<int fieldsize, int valsize> 1723 static inline Status 1724 rela_ua(unsigned char* view, 1725 unsigned int right_shift, 1726 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask, 1727 Address value, 1728 Overflow_check overflow) 1729 { 1730 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype 1731 Valtype; 1732 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view); 1733 Valtype reloc = value >> right_shift; 1734 val &= ~dst_mask; 1735 reloc &= dst_mask; 1736 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc); 1737 return overflowed<valsize>(value >> right_shift, overflow); 1738 } 1739 1740public: 1741 // R_PPC64_ADDR64: (Symbol + Addend) 1742 static inline void 1743 addr64(unsigned char* view, Address value) 1744 { This::template rela<64,64>(view, value, CHECK_NONE); } 1745 1746 // R_PPC64_UADDR64: (Symbol + Addend) unaligned 1747 static inline void 1748 addr64_u(unsigned char* view, Address value) 1749 { This::template rela_ua<64,64>(view, value, CHECK_NONE); } 1750 1751 // R_POWERPC_ADDR32: (Symbol + Addend) 1752 static inline Status 1753 addr32(unsigned char* view, Address value, Overflow_check overflow) 1754 { return This::template rela<32,32>(view, value, overflow); } 1755 1756 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned 1757 static inline Status 1758 addr32_u(unsigned char* view, Address value, Overflow_check overflow) 1759 { return This::template rela_ua<32,32>(view, value, overflow); } 1760 1761 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc 1762 static inline Status 1763 addr24(unsigned char* view, Address value, Overflow_check overflow) 1764 { 1765 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc, 1766 value, overflow); 1767 if (overflow != CHECK_NONE && (value & 3) != 0) 1768 stat = STATUS_OVERFLOW; 1769 return stat; 1770 } 1771 1772 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff 1773 static inline Status 1774 addr16(unsigned char* view, Address value, Overflow_check overflow) 1775 { return This::template rela<16,16>(view, value, overflow); } 1776 1777 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned 1778 static inline Status 1779 addr16_u(unsigned char* view, Address value, Overflow_check overflow) 1780 { return This::template rela_ua<16,16>(view, value, overflow); } 1781 1782 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc 1783 static inline Status 1784 addr16_ds(unsigned char* view, Address value, Overflow_check overflow) 1785 { 1786 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow); 1787 if ((value & 3) != 0) 1788 stat = STATUS_OVERFLOW; 1789 return stat; 1790 } 1791 1792 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0 1793 static inline Status 1794 addr16_dq(unsigned char* view, Address value, Overflow_check overflow) 1795 { 1796 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow); 1797 if ((value & 15) != 0) 1798 stat = STATUS_OVERFLOW; 1799 return stat; 1800 } 1801 1802 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff 1803 static inline void 1804 addr16_hi(unsigned char* view, Address value) 1805 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); } 1806 1807 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff 1808 static inline void 1809 addr16_ha(unsigned char* view, Address value) 1810 { This::addr16_hi(view, value + 0x8000); } 1811 1812 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff 1813 static inline void 1814 addr16_hi2(unsigned char* view, Address value) 1815 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); } 1816 1817 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff 1818 static inline void 1819 addr16_ha2(unsigned char* view, Address value) 1820 { This::addr16_hi2(view, value + 0x8000); } 1821 1822 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff 1823 static inline void 1824 addr16_hi3(unsigned char* view, Address value) 1825 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); } 1826 1827 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff 1828 static inline void 1829 addr16_ha3(unsigned char* view, Address value) 1830 { This::addr16_hi3(view, value + 0x8000); } 1831 1832 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc 1833 static inline Status 1834 addr14(unsigned char* view, Address value, Overflow_check overflow) 1835 { 1836 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow); 1837 if (overflow != CHECK_NONE && (value & 3) != 0) 1838 stat = STATUS_OVERFLOW; 1839 return stat; 1840 } 1841 1842 // R_POWERPC_REL16DX_HA 1843 static inline Status 1844 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow) 1845 { 1846 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; 1847 Valtype* wv = reinterpret_cast<Valtype*>(view); 1848 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv); 1849 value += 0x8000; 1850 value = static_cast<SignedAddress>(value) >> 16; 1851 val |= (value & 0xffc1) | ((value & 0x3e) << 15); 1852 elfcpp::Swap<32, big_endian>::writeval(wv, val); 1853 return overflowed<16>(value, overflow); 1854 } 1855}; 1856 1857// Set ABI version for input and output. 1858 1859template<int size, bool big_endian> 1860void 1861Powerpc_relobj<size, big_endian>::set_abiversion(int ver) 1862{ 1863 this->e_flags_ |= ver; 1864 if (this->abiversion() != 0) 1865 { 1866 Target_powerpc<size, big_endian>* target = 1867 static_cast<Target_powerpc<size, big_endian>*>( 1868 parameters->sized_target<size, big_endian>()); 1869 if (target->abiversion() == 0) 1870 target->set_abiversion(this->abiversion()); 1871 else if (target->abiversion() != this->abiversion()) 1872 gold_error(_("%s: ABI version %d is not compatible " 1873 "with ABI version %d output"), 1874 this->name().c_str(), 1875 this->abiversion(), target->abiversion()); 1876 1877 } 1878} 1879 1880// Stash away the index of .got2, .opd, .rela.toc, and .toc in a 1881// relocatable object, if such sections exists. 1882 1883template<int size, bool big_endian> 1884bool 1885Powerpc_relobj<size, big_endian>::do_find_special_sections( 1886 Read_symbols_data* sd) 1887{ 1888 const unsigned char* const pshdrs = sd->section_headers->data(); 1889 const unsigned char* namesu = sd->section_names->data(); 1890 const char* names = reinterpret_cast<const char*>(namesu); 1891 section_size_type names_size = sd->section_names_size; 1892 const unsigned char* s; 1893 1894 s = this->template find_shdr<size, big_endian>(pshdrs, 1895 size == 32 ? ".got2" : ".opd", 1896 names, names_size, NULL); 1897 if (s != NULL) 1898 { 1899 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size; 1900 this->special_ = ndx; 1901 if (size == 64) 1902 { 1903 if (this->abiversion() == 0) 1904 this->set_abiversion(1); 1905 else if (this->abiversion() > 1) 1906 gold_error(_("%s: .opd invalid in abiv%d"), 1907 this->name().c_str(), this->abiversion()); 1908 } 1909 } 1910 if (size == 64) 1911 { 1912 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc", 1913 names, names_size, NULL); 1914 if (s != NULL) 1915 { 1916 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size; 1917 this->relatoc_ = ndx; 1918 typename elfcpp::Shdr<size, big_endian> shdr(s); 1919 this->toc_ = this->adjust_shndx(shdr.get_sh_info()); 1920 } 1921 } 1922 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd); 1923} 1924 1925// Examine .rela.opd to build info about function entry points. 1926 1927template<int size, bool big_endian> 1928void 1929Powerpc_relobj<size, big_endian>::scan_opd_relocs( 1930 size_t reloc_count, 1931 const unsigned char* prelocs, 1932 const unsigned char* plocal_syms) 1933{ 1934 if (size == 64) 1935 { 1936 typedef typename elfcpp::Rela<size, big_endian> Reltype; 1937 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size; 1938 const int sym_size = elfcpp::Elf_sizes<size>::sym_size; 1939 Address expected_off = 0; 1940 bool regular = true; 1941 unsigned int opd_ent_size = 0; 1942 1943 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) 1944 { 1945 Reltype reloc(prelocs); 1946 typename elfcpp::Elf_types<size>::Elf_WXword r_info 1947 = reloc.get_r_info(); 1948 unsigned int r_type = elfcpp::elf_r_type<size>(r_info); 1949 if (r_type == elfcpp::R_PPC64_ADDR64) 1950 { 1951 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 1952 typename elfcpp::Elf_types<size>::Elf_Addr value; 1953 bool is_ordinary; 1954 unsigned int shndx; 1955 if (r_sym < this->local_symbol_count()) 1956 { 1957 typename elfcpp::Sym<size, big_endian> 1958 lsym(plocal_syms + r_sym * sym_size); 1959 shndx = lsym.get_st_shndx(); 1960 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary); 1961 value = lsym.get_st_value(); 1962 } 1963 else 1964 shndx = this->symbol_section_and_value(r_sym, &value, 1965 &is_ordinary); 1966 this->set_opd_ent(reloc.get_r_offset(), shndx, 1967 value + reloc.get_r_addend()); 1968 if (i == 2) 1969 { 1970 expected_off = reloc.get_r_offset(); 1971 opd_ent_size = expected_off; 1972 } 1973 else if (expected_off != reloc.get_r_offset()) 1974 regular = false; 1975 expected_off += opd_ent_size; 1976 } 1977 else if (r_type == elfcpp::R_PPC64_TOC) 1978 { 1979 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset()) 1980 regular = false; 1981 } 1982 else 1983 { 1984 gold_warning(_("%s: unexpected reloc type %u in .opd section"), 1985 this->name().c_str(), r_type); 1986 regular = false; 1987 } 1988 } 1989 if (reloc_count <= 2) 1990 opd_ent_size = this->section_size(this->opd_shndx()); 1991 if (opd_ent_size != 24 && opd_ent_size != 16) 1992 regular = false; 1993 if (!regular) 1994 { 1995 gold_warning(_("%s: .opd is not a regular array of opd entries"), 1996 this->name().c_str()); 1997 opd_ent_size = 0; 1998 } 1999 } 2000} 2001 2002// Returns true if a code sequence loading the TOC entry at VALUE 2003// relative to the TOC pointer can be converted into code calculating 2004// a TOC pointer relative offset. 2005// If so, the TOC pointer relative offset is stored to VALUE. 2006 2007template<int size, bool big_endian> 2008bool 2009Powerpc_relobj<size, big_endian>::make_toc_relative( 2010 Target_powerpc<size, big_endian>* target, 2011 Address* value) 2012{ 2013 if (size != 64) 2014 return false; 2015 2016 // With -mcmodel=medium code it is quite possible to have 2017 // toc-relative relocs referring to objects outside the TOC. 2018 // Don't try to look at a non-existent TOC. 2019 if (this->toc_shndx() == 0) 2020 return false; 2021 2022 // Convert VALUE back to an address by adding got_base (see below), 2023 // then to an offset in the TOC by subtracting the TOC output 2024 // section address and the TOC output offset. Since this TOC output 2025 // section and the got output section are one and the same, we can 2026 // omit adding and subtracting the output section address. 2027 Address off = (*value + this->toc_base_offset() 2028 - this->output_section_offset(this->toc_shndx())); 2029 // Is this offset in the TOC? -mcmodel=medium code may be using 2030 // TOC relative access to variables outside the TOC. Those of 2031 // course can't be optimized. We also don't try to optimize code 2032 // that is using a different object's TOC. 2033 if (off >= this->section_size(this->toc_shndx())) 2034 return false; 2035 2036 if (this->no_toc_opt(off)) 2037 return false; 2038 2039 section_size_type vlen; 2040 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen); 2041 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off); 2042 // The TOC pointer 2043 Address got_base = (target->got_section()->output_section()->address() 2044 + this->toc_base_offset()); 2045 addr -= got_base; 2046 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32) 2047 return false; 2048 2049 *value = addr; 2050 return true; 2051} 2052 2053// Perform the Sized_relobj_file method, then set up opd info from 2054// .opd relocs. 2055 2056template<int size, bool big_endian> 2057void 2058Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd) 2059{ 2060 Sized_relobj_file<size, big_endian>::do_read_relocs(rd); 2061 if (size == 64) 2062 { 2063 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin(); 2064 p != rd->relocs.end(); 2065 ++p) 2066 { 2067 if (p->data_shndx == this->opd_shndx()) 2068 { 2069 uint64_t opd_size = this->section_size(this->opd_shndx()); 2070 gold_assert(opd_size == static_cast<size_t>(opd_size)); 2071 if (opd_size != 0) 2072 { 2073 this->init_opd(opd_size); 2074 this->scan_opd_relocs(p->reloc_count, p->contents->data(), 2075 rd->local_symbols->data()); 2076 } 2077 break; 2078 } 2079 } 2080 } 2081} 2082 2083// Read the symbols then set up st_other vector. 2084 2085template<int size, bool big_endian> 2086void 2087Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd) 2088{ 2089 this->base_read_symbols(sd); 2090 if (size == 64) 2091 { 2092 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; 2093 const unsigned char* const pshdrs = sd->section_headers->data(); 2094 const unsigned int loccount = this->do_local_symbol_count(); 2095 if (loccount != 0) 2096 { 2097 this->st_other_.resize(loccount); 2098 const int sym_size = elfcpp::Elf_sizes<size>::sym_size; 2099 off_t locsize = loccount * sym_size; 2100 const unsigned int symtab_shndx = this->symtab_shndx(); 2101 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size; 2102 typename elfcpp::Shdr<size, big_endian> shdr(psymtab); 2103 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(), 2104 locsize, true, false); 2105 psyms += sym_size; 2106 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) 2107 { 2108 elfcpp::Sym<size, big_endian> sym(psyms); 2109 unsigned char st_other = sym.get_st_other(); 2110 this->st_other_[i] = st_other; 2111 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0) 2112 { 2113 if (this->abiversion() == 0) 2114 this->set_abiversion(2); 2115 else if (this->abiversion() < 2) 2116 gold_error(_("%s: local symbol %d has invalid st_other" 2117 " for ABI version 1"), 2118 this->name().c_str(), i); 2119 } 2120 } 2121 } 2122 } 2123} 2124 2125template<int size, bool big_endian> 2126void 2127Powerpc_dynobj<size, big_endian>::set_abiversion(int ver) 2128{ 2129 this->e_flags_ |= ver; 2130 if (this->abiversion() != 0) 2131 { 2132 Target_powerpc<size, big_endian>* target = 2133 static_cast<Target_powerpc<size, big_endian>*>( 2134 parameters->sized_target<size, big_endian>()); 2135 if (target->abiversion() == 0) 2136 target->set_abiversion(this->abiversion()); 2137 else if (target->abiversion() != this->abiversion()) 2138 gold_error(_("%s: ABI version %d is not compatible " 2139 "with ABI version %d output"), 2140 this->name().c_str(), 2141 this->abiversion(), target->abiversion()); 2142 2143 } 2144} 2145 2146// Call Sized_dynobj::base_read_symbols to read the symbols then 2147// read .opd from a dynamic object, filling in opd_ent_ vector, 2148 2149template<int size, bool big_endian> 2150void 2151Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd) 2152{ 2153 this->base_read_symbols(sd); 2154 if (size == 64) 2155 { 2156 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; 2157 const unsigned char* const pshdrs = sd->section_headers->data(); 2158 const unsigned char* namesu = sd->section_names->data(); 2159 const char* names = reinterpret_cast<const char*>(namesu); 2160 const unsigned char* s = NULL; 2161 const unsigned char* opd; 2162 section_size_type opd_size; 2163 2164 // Find and read .opd section. 2165 while (1) 2166 { 2167 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names, 2168 sd->section_names_size, 2169 s); 2170 if (s == NULL) 2171 return; 2172 2173 typename elfcpp::Shdr<size, big_endian> shdr(s); 2174 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS 2175 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0) 2176 { 2177 if (this->abiversion() == 0) 2178 this->set_abiversion(1); 2179 else if (this->abiversion() > 1) 2180 gold_error(_("%s: .opd invalid in abiv%d"), 2181 this->name().c_str(), this->abiversion()); 2182 2183 this->opd_shndx_ = (s - pshdrs) / shdr_size; 2184 this->opd_address_ = shdr.get_sh_addr(); 2185 opd_size = convert_to_section_size_type(shdr.get_sh_size()); 2186 opd = this->get_view(shdr.get_sh_offset(), opd_size, 2187 true, false); 2188 break; 2189 } 2190 } 2191 2192 // Build set of executable sections. 2193 // Using a set is probably overkill. There is likely to be only 2194 // a few executable sections, typically .init, .text and .fini, 2195 // and they are generally grouped together. 2196 typedef std::set<Sec_info> Exec_sections; 2197 Exec_sections exec_sections; 2198 s = pshdrs; 2199 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size) 2200 { 2201 typename elfcpp::Shdr<size, big_endian> shdr(s); 2202 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS 2203 && ((shdr.get_sh_flags() 2204 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR)) 2205 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR)) 2206 && shdr.get_sh_size() != 0) 2207 { 2208 exec_sections.insert(Sec_info(shdr.get_sh_addr(), 2209 shdr.get_sh_size(), i)); 2210 } 2211 } 2212 if (exec_sections.empty()) 2213 return; 2214 2215 // Look over the OPD entries. This is complicated by the fact 2216 // that some binaries will use two-word entries while others 2217 // will use the standard three-word entries. In most cases 2218 // the third word (the environment pointer for languages like 2219 // Pascal) is unused and will be zero. If the third word is 2220 // used it should not be pointing into executable sections, 2221 // I think. 2222 this->init_opd(opd_size); 2223 for (const unsigned char* p = opd; p < opd + opd_size; p += 8) 2224 { 2225 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype; 2226 const Valtype* valp = reinterpret_cast<const Valtype*>(p); 2227 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp); 2228 if (val == 0) 2229 // Chances are that this is the third word of an OPD entry. 2230 continue; 2231 typename Exec_sections::const_iterator e 2232 = exec_sections.upper_bound(Sec_info(val, 0, 0)); 2233 if (e != exec_sections.begin()) 2234 { 2235 --e; 2236 if (e->start <= val && val < e->start + e->len) 2237 { 2238 // We have an address in an executable section. 2239 // VAL ought to be the function entry, set it up. 2240 this->set_opd_ent(p - opd, e->shndx, val); 2241 // Skip second word of OPD entry, the TOC pointer. 2242 p += 8; 2243 } 2244 } 2245 // If we didn't match any executable sections, we likely 2246 // have a non-zero third word in the OPD entry. 2247 } 2248 } 2249} 2250 2251// Relocate sections. 2252 2253template<int size, bool big_endian> 2254void 2255Powerpc_relobj<size, big_endian>::do_relocate_sections( 2256 const Symbol_table* symtab, const Layout* layout, 2257 const unsigned char* pshdrs, Output_file* of, 2258 typename Sized_relobj_file<size, big_endian>::Views* pviews) 2259{ 2260 unsigned int start = 1; 2261 if (size == 64 2262 && this->relatoc_ != 0 2263 && !parameters->options().relocatable()) 2264 { 2265 // Relocate .toc first. 2266 this->relocate_section_range(symtab, layout, pshdrs, of, pviews, 2267 this->relatoc_, this->relatoc_); 2268 this->relocate_section_range(symtab, layout, pshdrs, of, pviews, 2269 1, this->relatoc_ - 1); 2270 start = this->relatoc_ + 1; 2271 } 2272 this->relocate_section_range(symtab, layout, pshdrs, of, pviews, 2273 start, this->shnum() - 1); 2274} 2275 2276// Set up some symbols. 2277 2278template<int size, bool big_endian> 2279void 2280Target_powerpc<size, big_endian>::do_define_standard_symbols( 2281 Symbol_table* symtab, 2282 Layout* layout) 2283{ 2284 if (size == 32) 2285 { 2286 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as 2287 // undefined when scanning relocs (and thus requires 2288 // non-relative dynamic relocs). The proper value will be 2289 // updated later. 2290 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL); 2291 if (gotsym != NULL && gotsym->is_undefined()) 2292 { 2293 Target_powerpc<size, big_endian>* target = 2294 static_cast<Target_powerpc<size, big_endian>*>( 2295 parameters->sized_target<size, big_endian>()); 2296 Output_data_got_powerpc<size, big_endian>* got 2297 = target->got_section(symtab, layout); 2298 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, 2299 Symbol_table::PREDEFINED, 2300 got, 0, 0, 2301 elfcpp::STT_OBJECT, 2302 elfcpp::STB_LOCAL, 2303 elfcpp::STV_HIDDEN, 0, 2304 false, false); 2305 } 2306 2307 // Define _SDA_BASE_ at the start of the .sdata section + 32768. 2308 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL); 2309 if (sdasym != NULL && sdasym->is_undefined()) 2310 { 2311 Output_data_space* sdata = new Output_data_space(4, "** sdata"); 2312 Output_section* os 2313 = layout->add_output_section_data(".sdata", 0, 2314 elfcpp::SHF_ALLOC 2315 | elfcpp::SHF_WRITE, 2316 sdata, ORDER_SMALL_DATA, false); 2317 symtab->define_in_output_data("_SDA_BASE_", NULL, 2318 Symbol_table::PREDEFINED, 2319 os, 32768, 0, elfcpp::STT_OBJECT, 2320 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 2321 0, false, false); 2322 } 2323 } 2324 else 2325 { 2326 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_ 2327 Symbol *gotsym = symtab->lookup(".TOC.", NULL); 2328 if (gotsym != NULL && gotsym->is_undefined()) 2329 { 2330 Target_powerpc<size, big_endian>* target = 2331 static_cast<Target_powerpc<size, big_endian>*>( 2332 parameters->sized_target<size, big_endian>()); 2333 Output_data_got_powerpc<size, big_endian>* got 2334 = target->got_section(symtab, layout); 2335 symtab->define_in_output_data(".TOC.", NULL, 2336 Symbol_table::PREDEFINED, 2337 got, 0x8000, 0, 2338 elfcpp::STT_OBJECT, 2339 elfcpp::STB_LOCAL, 2340 elfcpp::STV_HIDDEN, 0, 2341 false, false); 2342 } 2343 } 2344} 2345 2346// Set up PowerPC target specific relobj. 2347 2348template<int size, bool big_endian> 2349Object* 2350Target_powerpc<size, big_endian>::do_make_elf_object( 2351 const std::string& name, 2352 Input_file* input_file, 2353 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr) 2354{ 2355 int et = ehdr.get_e_type(); 2356 // ET_EXEC files are valid input for --just-symbols/-R, 2357 // and we treat them as relocatable objects. 2358 if (et == elfcpp::ET_REL 2359 || (et == elfcpp::ET_EXEC && input_file->just_symbols())) 2360 { 2361 Powerpc_relobj<size, big_endian>* obj = 2362 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr); 2363 obj->setup(); 2364 return obj; 2365 } 2366 else if (et == elfcpp::ET_DYN) 2367 { 2368 Powerpc_dynobj<size, big_endian>* obj = 2369 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr); 2370 obj->setup(); 2371 return obj; 2372 } 2373 else 2374 { 2375 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et); 2376 return NULL; 2377 } 2378} 2379 2380template<int size, bool big_endian> 2381class Output_data_got_powerpc : public Output_data_got<size, big_endian> 2382{ 2383public: 2384 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype; 2385 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn; 2386 2387 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout) 2388 : Output_data_got<size, big_endian>(), 2389 symtab_(symtab), layout_(layout), 2390 header_ent_cnt_(size == 32 ? 3 : 1), 2391 header_index_(size == 32 ? 0x2000 : 0) 2392 { 2393 if (size == 64) 2394 this->set_addralign(256); 2395 } 2396 2397 // Override all the Output_data_got methods we use so as to first call 2398 // reserve_ent(). 2399 bool 2400 add_global(Symbol* gsym, unsigned int got_type) 2401 { 2402 this->reserve_ent(); 2403 return Output_data_got<size, big_endian>::add_global(gsym, got_type); 2404 } 2405 2406 bool 2407 add_global_plt(Symbol* gsym, unsigned int got_type) 2408 { 2409 this->reserve_ent(); 2410 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type); 2411 } 2412 2413 bool 2414 add_global_tls(Symbol* gsym, unsigned int got_type) 2415 { return this->add_global_plt(gsym, got_type); } 2416 2417 void 2418 add_global_with_rel(Symbol* gsym, unsigned int got_type, 2419 Output_data_reloc_generic* rel_dyn, unsigned int r_type) 2420 { 2421 this->reserve_ent(); 2422 Output_data_got<size, big_endian>:: 2423 add_global_with_rel(gsym, got_type, rel_dyn, r_type); 2424 } 2425 2426 void 2427 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type, 2428 Output_data_reloc_generic* rel_dyn, 2429 unsigned int r_type_1, unsigned int r_type_2) 2430 { 2431 this->reserve_ent(2); 2432 Output_data_got<size, big_endian>:: 2433 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2); 2434 } 2435 2436 bool 2437 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type) 2438 { 2439 this->reserve_ent(); 2440 return Output_data_got<size, big_endian>::add_local(object, sym_index, 2441 got_type); 2442 } 2443 2444 bool 2445 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type) 2446 { 2447 this->reserve_ent(); 2448 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index, 2449 got_type); 2450 } 2451 2452 bool 2453 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type) 2454 { return this->add_local_plt(object, sym_index, got_type); } 2455 2456 void 2457 add_local_tls_pair(Relobj* object, unsigned int sym_index, 2458 unsigned int got_type, 2459 Output_data_reloc_generic* rel_dyn, 2460 unsigned int r_type) 2461 { 2462 this->reserve_ent(2); 2463 Output_data_got<size, big_endian>:: 2464 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type); 2465 } 2466 2467 unsigned int 2468 add_constant(Valtype constant) 2469 { 2470 this->reserve_ent(); 2471 return Output_data_got<size, big_endian>::add_constant(constant); 2472 } 2473 2474 unsigned int 2475 add_constant_pair(Valtype c1, Valtype c2) 2476 { 2477 this->reserve_ent(2); 2478 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2); 2479 } 2480 2481 // Offset of _GLOBAL_OFFSET_TABLE_. 2482 unsigned int 2483 g_o_t() const 2484 { 2485 return this->got_offset(this->header_index_); 2486 } 2487 2488 // Offset of base used to access the GOT/TOC. 2489 // The got/toc pointer reg will be set to this value. 2490 Valtype 2491 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const 2492 { 2493 if (size == 32) 2494 return this->g_o_t(); 2495 else 2496 return (this->output_section()->address() 2497 + object->toc_base_offset() 2498 - this->address()); 2499 } 2500 2501 // Ensure our GOT has a header. 2502 void 2503 set_final_data_size() 2504 { 2505 if (this->header_ent_cnt_ != 0) 2506 this->make_header(); 2507 Output_data_got<size, big_endian>::set_final_data_size(); 2508 } 2509 2510 // First word of GOT header needs some values that are not 2511 // handled by Output_data_got so poke them in here. 2512 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase. 2513 void 2514 do_write(Output_file* of) 2515 { 2516 Valtype val = 0; 2517 if (size == 32 && this->layout_->dynamic_data() != NULL) 2518 val = this->layout_->dynamic_section()->address(); 2519 if (size == 64) 2520 val = this->output_section()->address() + 0x8000; 2521 this->replace_constant(this->header_index_, val); 2522 Output_data_got<size, big_endian>::do_write(of); 2523 } 2524 2525private: 2526 void 2527 reserve_ent(unsigned int cnt = 1) 2528 { 2529 if (this->header_ent_cnt_ == 0) 2530 return; 2531 if (this->num_entries() + cnt > this->header_index_) 2532 this->make_header(); 2533 } 2534 2535 void 2536 make_header() 2537 { 2538 this->header_ent_cnt_ = 0; 2539 this->header_index_ = this->num_entries(); 2540 if (size == 32) 2541 { 2542 Output_data_got<size, big_endian>::add_constant(0); 2543 Output_data_got<size, big_endian>::add_constant(0); 2544 Output_data_got<size, big_endian>::add_constant(0); 2545 2546 // Define _GLOBAL_OFFSET_TABLE_ at the header 2547 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL); 2548 if (gotsym != NULL) 2549 { 2550 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym); 2551 sym->set_value(this->g_o_t()); 2552 } 2553 else 2554 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, 2555 Symbol_table::PREDEFINED, 2556 this, this->g_o_t(), 0, 2557 elfcpp::STT_OBJECT, 2558 elfcpp::STB_LOCAL, 2559 elfcpp::STV_HIDDEN, 0, 2560 false, false); 2561 } 2562 else 2563 Output_data_got<size, big_endian>::add_constant(0); 2564 } 2565 2566 // Stashed pointers. 2567 Symbol_table* symtab_; 2568 Layout* layout_; 2569 2570 // GOT header size. 2571 unsigned int header_ent_cnt_; 2572 // GOT header index. 2573 unsigned int header_index_; 2574}; 2575 2576// Get the GOT section, creating it if necessary. 2577 2578template<int size, bool big_endian> 2579Output_data_got_powerpc<size, big_endian>* 2580Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab, 2581 Layout* layout) 2582{ 2583 if (this->got_ == NULL) 2584 { 2585 gold_assert(symtab != NULL && layout != NULL); 2586 2587 this->got_ 2588 = new Output_data_got_powerpc<size, big_endian>(symtab, layout); 2589 2590 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, 2591 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, 2592 this->got_, ORDER_DATA, false); 2593 } 2594 2595 return this->got_; 2596} 2597 2598// Get the dynamic reloc section, creating it if necessary. 2599 2600template<int size, bool big_endian> 2601typename Target_powerpc<size, big_endian>::Reloc_section* 2602Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout) 2603{ 2604 if (this->rela_dyn_ == NULL) 2605 { 2606 gold_assert(layout != NULL); 2607 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc()); 2608 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA, 2609 elfcpp::SHF_ALLOC, this->rela_dyn_, 2610 ORDER_DYNAMIC_RELOCS, false); 2611 } 2612 return this->rela_dyn_; 2613} 2614 2615// Similarly, but for ifunc symbols get the one for ifunc. 2616 2617template<int size, bool big_endian> 2618typename Target_powerpc<size, big_endian>::Reloc_section* 2619Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab, 2620 Layout* layout, 2621 bool for_ifunc) 2622{ 2623 if (!for_ifunc) 2624 return this->rela_dyn_section(layout); 2625 2626 if (this->iplt_ == NULL) 2627 this->make_iplt_section(symtab, layout); 2628 return this->iplt_->rel_plt(); 2629} 2630 2631class Stub_control 2632{ 2633 public: 2634 // Determine the stub group size. The group size is the absolute 2635 // value of the parameter --stub-group-size. If --stub-group-size 2636 // is passed a negative value, we restrict stubs to be always after 2637 // the stubbed branches. 2638 Stub_control(int32_t size, bool no_size_errors, bool multi_os) 2639 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0), 2640 suppress_size_errors_(no_size_errors), multi_os_(multi_os), 2641 state_(NO_GROUP), group_size_(0), group_start_addr_(0), 2642 owner_(NULL), output_section_(NULL) 2643 { 2644 } 2645 2646 // Return true iff input section can be handled by current stub 2647 // group. 2648 bool 2649 can_add_to_stub_group(Output_section* o, 2650 const Output_section::Input_section* i, 2651 bool has14); 2652 2653 const Output_section::Input_section* 2654 owner() 2655 { return owner_; } 2656 2657 Output_section* 2658 output_section() 2659 { return output_section_; } 2660 2661 void 2662 set_output_and_owner(Output_section* o, 2663 const Output_section::Input_section* i) 2664 { 2665 this->output_section_ = o; 2666 this->owner_ = i; 2667 } 2668 2669 private: 2670 typedef enum 2671 { 2672 // Initial state. 2673 NO_GROUP, 2674 // Adding group sections before the stubs. 2675 FINDING_STUB_SECTION, 2676 // Adding group sections after the stubs. 2677 HAS_STUB_SECTION 2678 } State; 2679 2680 uint32_t stub_group_size_; 2681 bool stubs_always_after_branch_; 2682 bool suppress_size_errors_; 2683 // True if a stub group can serve multiple output sections. 2684 bool multi_os_; 2685 State state_; 2686 // Current max size of group. Starts at stub_group_size_ but is 2687 // reduced to stub_group_size_/1024 on seeing a section with 2688 // external conditional branches. 2689 uint32_t group_size_; 2690 uint64_t group_start_addr_; 2691 // owner_ and output_section_ specify the section to which stubs are 2692 // attached. The stubs are placed at the end of this section. 2693 const Output_section::Input_section* owner_; 2694 Output_section* output_section_; 2695}; 2696 2697// Return true iff input section can be handled by current stub 2698// group. Sections are presented to this function in order, 2699// so the first section is the head of the group. 2700 2701bool 2702Stub_control::can_add_to_stub_group(Output_section* o, 2703 const Output_section::Input_section* i, 2704 bool has14) 2705{ 2706 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI; 2707 uint64_t this_size; 2708 uint64_t start_addr = o->address(); 2709 2710 if (whole_sec) 2711 // .init and .fini sections are pasted together to form a single 2712 // function. We can't be adding stubs in the middle of the function. 2713 this_size = o->data_size(); 2714 else 2715 { 2716 start_addr += i->relobj()->output_section_offset(i->shndx()); 2717 this_size = i->data_size(); 2718 } 2719 2720 uint64_t end_addr = start_addr + this_size; 2721 uint32_t group_size = this->stub_group_size_; 2722 if (has14) 2723 this->group_size_ = group_size = group_size >> 10; 2724 2725 if (this_size > group_size && !this->suppress_size_errors_) 2726 gold_warning(_("%s:%s exceeds group size"), 2727 i->relobj()->name().c_str(), 2728 i->relobj()->section_name(i->shndx()).c_str()); 2729 2730 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx", 2731 has14 ? " 14bit" : "", 2732 i->relobj()->name().c_str(), 2733 i->relobj()->section_name(i->shndx()).c_str(), 2734 (long long) this_size, 2735 (this->state_ == NO_GROUP 2736 ? this_size 2737 : (long long) end_addr - this->group_start_addr_)); 2738 2739 if (this->state_ == NO_GROUP) 2740 { 2741 // Only here on very first use of Stub_control 2742 this->owner_ = i; 2743 this->output_section_ = o; 2744 this->state_ = FINDING_STUB_SECTION; 2745 this->group_size_ = group_size; 2746 this->group_start_addr_ = start_addr; 2747 return true; 2748 } 2749 else if (!this->multi_os_ && this->output_section_ != o) 2750 ; 2751 else if (this->state_ == HAS_STUB_SECTION) 2752 { 2753 // Can we add this section, which is after the stubs, to the 2754 // group? 2755 if (end_addr - this->group_start_addr_ <= this->group_size_) 2756 return true; 2757 } 2758 else if (this->state_ == FINDING_STUB_SECTION) 2759 { 2760 if ((whole_sec && this->output_section_ == o) 2761 || end_addr - this->group_start_addr_ <= this->group_size_) 2762 { 2763 // Stubs are added at the end of "owner_". 2764 this->owner_ = i; 2765 this->output_section_ = o; 2766 return true; 2767 } 2768 // The group before the stubs has reached maximum size. 2769 // Now see about adding sections after the stubs to the 2770 // group. If the current section has a 14-bit branch and 2771 // the group before the stubs exceeds group_size_ (because 2772 // they didn't have 14-bit branches), don't add sections 2773 // after the stubs: The size of stubs for such a large 2774 // group may exceed the reach of a 14-bit branch. 2775 if (!this->stubs_always_after_branch_ 2776 && this_size <= this->group_size_ 2777 && start_addr - this->group_start_addr_ <= this->group_size_) 2778 { 2779 gold_debug(DEBUG_TARGET, "adding after stubs"); 2780 this->state_ = HAS_STUB_SECTION; 2781 this->group_start_addr_ = start_addr; 2782 return true; 2783 } 2784 } 2785 else 2786 gold_unreachable(); 2787 2788 gold_debug(DEBUG_TARGET, 2789 !this->multi_os_ && this->output_section_ != o 2790 ? "nope, new output section\n" 2791 : "nope, didn't fit\n"); 2792 2793 // The section fails to fit in the current group. Set up a few 2794 // things for the next group. owner_ and output_section_ will be 2795 // set later after we've retrieved those values for the current 2796 // group. 2797 this->state_ = FINDING_STUB_SECTION; 2798 this->group_size_ = group_size; 2799 this->group_start_addr_ = start_addr; 2800 return false; 2801} 2802 2803// Look over all the input sections, deciding where to place stubs. 2804 2805template<int size, bool big_endian> 2806void 2807Target_powerpc<size, big_endian>::group_sections(Layout* layout, 2808 const Task*, 2809 bool no_size_errors) 2810{ 2811 Stub_control stub_control(this->stub_group_size_, no_size_errors, 2812 parameters->options().stub_group_multi()); 2813 2814 // Group input sections and insert stub table 2815 Stub_table_owner* table_owner = NULL; 2816 std::vector<Stub_table_owner*> tables; 2817 Layout::Section_list section_list; 2818 layout->get_executable_sections(§ion_list); 2819 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections()); 2820 for (Layout::Section_list::iterator o = section_list.begin(); 2821 o != section_list.end(); 2822 ++o) 2823 { 2824 typedef Output_section::Input_section_list Input_section_list; 2825 for (Input_section_list::const_iterator i 2826 = (*o)->input_sections().begin(); 2827 i != (*o)->input_sections().end(); 2828 ++i) 2829 { 2830 if (i->is_input_section() 2831 || i->is_relaxed_input_section()) 2832 { 2833 Powerpc_relobj<size, big_endian>* ppcobj = static_cast 2834 <Powerpc_relobj<size, big_endian>*>(i->relobj()); 2835 bool has14 = ppcobj->has_14bit_branch(i->shndx()); 2836 if (!stub_control.can_add_to_stub_group(*o, &*i, has14)) 2837 { 2838 table_owner->output_section = stub_control.output_section(); 2839 table_owner->owner = stub_control.owner(); 2840 stub_control.set_output_and_owner(*o, &*i); 2841 table_owner = NULL; 2842 } 2843 if (table_owner == NULL) 2844 { 2845 table_owner = new Stub_table_owner; 2846 tables.push_back(table_owner); 2847 } 2848 ppcobj->set_stub_table(i->shndx(), tables.size() - 1); 2849 } 2850 } 2851 } 2852 if (table_owner != NULL) 2853 { 2854 table_owner->output_section = stub_control.output_section(); 2855 table_owner->owner = stub_control.owner();; 2856 } 2857 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin(); 2858 t != tables.end(); 2859 ++t) 2860 { 2861 Stub_table<size, big_endian>* stub_table; 2862 2863 if ((*t)->owner->is_input_section()) 2864 stub_table = new Stub_table<size, big_endian>(this, 2865 (*t)->output_section, 2866 (*t)->owner, 2867 this->stub_tables_.size()); 2868 else if ((*t)->owner->is_relaxed_input_section()) 2869 stub_table = static_cast<Stub_table<size, big_endian>*>( 2870 (*t)->owner->relaxed_input_section()); 2871 else 2872 gold_unreachable(); 2873 this->stub_tables_.push_back(stub_table); 2874 delete *t; 2875 } 2876} 2877 2878static unsigned long 2879max_branch_delta (unsigned int r_type) 2880{ 2881 if (r_type == elfcpp::R_POWERPC_REL14 2882 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN 2883 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN) 2884 return 1L << 15; 2885 if (r_type == elfcpp::R_POWERPC_REL24 2886 || r_type == elfcpp::R_PPC_PLTREL24 2887 || r_type == elfcpp::R_PPC_LOCAL24PC) 2888 return 1L << 25; 2889 return 0; 2890} 2891 2892// If this branch needs a plt call stub, or a long branch stub, make one. 2893 2894template<int size, bool big_endian> 2895bool 2896Target_powerpc<size, big_endian>::Branch_info::make_stub( 2897 Stub_table<size, big_endian>* stub_table, 2898 Stub_table<size, big_endian>* ifunc_stub_table, 2899 Symbol_table* symtab) const 2900{ 2901 Symbol* sym = this->object_->global_symbol(this->r_sym_); 2902 if (sym != NULL && sym->is_forwarder()) 2903 sym = symtab->resolve_forwards(sym); 2904 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym); 2905 Target_powerpc<size, big_endian>* target = 2906 static_cast<Target_powerpc<size, big_endian>*>( 2907 parameters->sized_target<size, big_endian>()); 2908 bool ok = true; 2909 2910 if (gsym != NULL 2911 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target)) 2912 : this->object_->local_has_plt_offset(this->r_sym_)) 2913 { 2914 if (size == 64 2915 && gsym != NULL 2916 && target->abiversion() >= 2 2917 && !parameters->options().output_is_position_independent() 2918 && !is_branch_reloc(this->r_type_)) 2919 target->glink_section()->add_global_entry(gsym); 2920 else 2921 { 2922 if (stub_table == NULL) 2923 stub_table = this->object_->stub_table(this->shndx_); 2924 if (stub_table == NULL) 2925 { 2926 // This is a ref from a data section to an ifunc symbol. 2927 stub_table = ifunc_stub_table; 2928 } 2929 gold_assert(stub_table != NULL); 2930 Address from = this->object_->get_output_section_offset(this->shndx_); 2931 if (from != invalid_address) 2932 from += (this->object_->output_section(this->shndx_)->address() 2933 + this->offset_); 2934 if (gsym != NULL) 2935 ok = stub_table->add_plt_call_entry(from, 2936 this->object_, gsym, 2937 this->r_type_, this->addend_); 2938 else 2939 ok = stub_table->add_plt_call_entry(from, 2940 this->object_, this->r_sym_, 2941 this->r_type_, this->addend_); 2942 } 2943 } 2944 else 2945 { 2946 Address max_branch_offset = max_branch_delta(this->r_type_); 2947 if (max_branch_offset == 0) 2948 return true; 2949 Address from = this->object_->get_output_section_offset(this->shndx_); 2950 gold_assert(from != invalid_address); 2951 from += (this->object_->output_section(this->shndx_)->address() 2952 + this->offset_); 2953 Address to; 2954 if (gsym != NULL) 2955 { 2956 switch (gsym->source()) 2957 { 2958 case Symbol::FROM_OBJECT: 2959 { 2960 Object* symobj = gsym->object(); 2961 if (symobj->is_dynamic() 2962 || symobj->pluginobj() != NULL) 2963 return true; 2964 bool is_ordinary; 2965 unsigned int shndx = gsym->shndx(&is_ordinary); 2966 if (shndx == elfcpp::SHN_UNDEF) 2967 return true; 2968 } 2969 break; 2970 2971 case Symbol::IS_UNDEFINED: 2972 return true; 2973 2974 default: 2975 break; 2976 } 2977 Symbol_table::Compute_final_value_status status; 2978 to = symtab->compute_final_value<size>(gsym, &status); 2979 if (status != Symbol_table::CFVS_OK) 2980 return true; 2981 if (size == 64) 2982 to += this->object_->ppc64_local_entry_offset(gsym); 2983 } 2984 else 2985 { 2986 const Symbol_value<size>* psymval 2987 = this->object_->local_symbol(this->r_sym_); 2988 Symbol_value<size> symval; 2989 if (psymval->is_section_symbol()) 2990 symval.set_is_section_symbol(); 2991 typedef Sized_relobj_file<size, big_endian> ObjType; 2992 typename ObjType::Compute_final_local_value_status status 2993 = this->object_->compute_final_local_value(this->r_sym_, psymval, 2994 &symval, symtab); 2995 if (status != ObjType::CFLV_OK 2996 || !symval.has_output_value()) 2997 return true; 2998 to = symval.value(this->object_, 0); 2999 if (size == 64) 3000 to += this->object_->ppc64_local_entry_offset(this->r_sym_); 3001 } 3002 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24)) 3003 to += this->addend_; 3004 if (stub_table == NULL) 3005 stub_table = this->object_->stub_table(this->shndx_); 3006 if (size == 64 && target->abiversion() < 2) 3007 { 3008 unsigned int dest_shndx; 3009 if (!target->symval_for_branch(symtab, gsym, this->object_, 3010 &to, &dest_shndx)) 3011 return true; 3012 } 3013 Address delta = to - from; 3014 if (delta + max_branch_offset >= 2 * max_branch_offset) 3015 { 3016 if (stub_table == NULL) 3017 { 3018 gold_warning(_("%s:%s: branch in non-executable section," 3019 " no long branch stub for you"), 3020 this->object_->name().c_str(), 3021 this->object_->section_name(this->shndx_).c_str()); 3022 return true; 3023 } 3024 bool save_res = (size == 64 3025 && gsym != NULL 3026 && gsym->source() == Symbol::IN_OUTPUT_DATA 3027 && gsym->output_data() == target->savres_section()); 3028 ok = stub_table->add_long_branch_entry(this->object_, 3029 this->r_type_, 3030 from, to, save_res); 3031 } 3032 } 3033 if (!ok) 3034 gold_debug(DEBUG_TARGET, 3035 "branch at %s:%s+%#lx\n" 3036 "can't reach stub attached to %s:%s", 3037 this->object_->name().c_str(), 3038 this->object_->section_name(this->shndx_).c_str(), 3039 (unsigned long) this->offset_, 3040 stub_table->relobj()->name().c_str(), 3041 stub_table->relobj()->section_name(stub_table->shndx()).c_str()); 3042 3043 return ok; 3044} 3045 3046// Relaxation hook. This is where we do stub generation. 3047 3048template<int size, bool big_endian> 3049bool 3050Target_powerpc<size, big_endian>::do_relax(int pass, 3051 const Input_objects*, 3052 Symbol_table* symtab, 3053 Layout* layout, 3054 const Task* task) 3055{ 3056 unsigned int prev_brlt_size = 0; 3057 if (pass == 1) 3058 { 3059 bool thread_safe 3060 = this->abiversion() < 2 && parameters->options().plt_thread_safe(); 3061 if (size == 64 3062 && this->abiversion() < 2 3063 && !thread_safe 3064 && !parameters->options().user_set_plt_thread_safe()) 3065 { 3066 static const char* const thread_starter[] = 3067 { 3068 "pthread_create", 3069 /* libstdc++ */ 3070 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE", 3071 /* librt */ 3072 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio", 3073 "mq_notify", "create_timer", 3074 /* libanl */ 3075 "getaddrinfo_a", 3076 /* libgomp */ 3077 "GOMP_parallel", 3078 "GOMP_parallel_start", 3079 "GOMP_parallel_loop_static", 3080 "GOMP_parallel_loop_static_start", 3081 "GOMP_parallel_loop_dynamic", 3082 "GOMP_parallel_loop_dynamic_start", 3083 "GOMP_parallel_loop_guided", 3084 "GOMP_parallel_loop_guided_start", 3085 "GOMP_parallel_loop_runtime", 3086 "GOMP_parallel_loop_runtime_start", 3087 "GOMP_parallel_sections", 3088 "GOMP_parallel_sections_start", 3089 /* libgo */ 3090 "__go_go", 3091 }; 3092 3093 if (parameters->options().shared()) 3094 thread_safe = true; 3095 else 3096 { 3097 for (unsigned int i = 0; 3098 i < sizeof(thread_starter) / sizeof(thread_starter[0]); 3099 i++) 3100 { 3101 Symbol* sym = symtab->lookup(thread_starter[i], NULL); 3102 thread_safe = (sym != NULL 3103 && sym->in_reg() 3104 && sym->in_real_elf()); 3105 if (thread_safe) 3106 break; 3107 } 3108 } 3109 } 3110 this->plt_thread_safe_ = thread_safe; 3111 } 3112 3113 if (pass == 1) 3114 { 3115 this->stub_group_size_ = parameters->options().stub_group_size(); 3116 bool no_size_errors = true; 3117 if (this->stub_group_size_ == 1) 3118 this->stub_group_size_ = 0x1c00000; 3119 else if (this->stub_group_size_ == -1) 3120 this->stub_group_size_ = -0x1e00000; 3121 else 3122 no_size_errors = false; 3123 this->group_sections(layout, task, no_size_errors); 3124 } 3125 else if (this->relax_failed_ && this->relax_fail_count_ < 3) 3126 { 3127 this->branch_lookup_table_.clear(); 3128 for (typename Stub_tables::iterator p = this->stub_tables_.begin(); 3129 p != this->stub_tables_.end(); 3130 ++p) 3131 { 3132 (*p)->clear_stubs(true); 3133 } 3134 this->stub_tables_.clear(); 3135 this->stub_group_size_ = this->stub_group_size_ / 4 * 3; 3136 gold_info(_("%s: stub group size is too large; retrying with %#x"), 3137 program_name, this->stub_group_size_); 3138 this->group_sections(layout, task, true); 3139 } 3140 3141 // We need address of stub tables valid for make_stub. 3142 for (typename Stub_tables::iterator p = this->stub_tables_.begin(); 3143 p != this->stub_tables_.end(); 3144 ++p) 3145 { 3146 const Powerpc_relobj<size, big_endian>* object 3147 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj()); 3148 Address off = object->get_output_section_offset((*p)->shndx()); 3149 gold_assert(off != invalid_address); 3150 Output_section* os = (*p)->output_section(); 3151 (*p)->set_address_and_size(os, off); 3152 } 3153 3154 if (pass != 1) 3155 { 3156 // Clear plt call stubs, long branch stubs and branch lookup table. 3157 prev_brlt_size = this->branch_lookup_table_.size(); 3158 this->branch_lookup_table_.clear(); 3159 for (typename Stub_tables::iterator p = this->stub_tables_.begin(); 3160 p != this->stub_tables_.end(); 3161 ++p) 3162 { 3163 (*p)->clear_stubs(false); 3164 } 3165 } 3166 3167 // Build all the stubs. 3168 this->relax_failed_ = false; 3169 Stub_table<size, big_endian>* ifunc_stub_table 3170 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0]; 3171 Stub_table<size, big_endian>* one_stub_table 3172 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table; 3173 for (typename Branches::const_iterator b = this->branch_info_.begin(); 3174 b != this->branch_info_.end(); 3175 b++) 3176 { 3177 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab) 3178 && !this->relax_failed_) 3179 { 3180 this->relax_failed_ = true; 3181 this->relax_fail_count_++; 3182 if (this->relax_fail_count_ < 3) 3183 return true; 3184 } 3185 } 3186 3187 // Did anything change size? 3188 unsigned int num_huge_branches = this->branch_lookup_table_.size(); 3189 bool again = num_huge_branches != prev_brlt_size; 3190 if (size == 64 && num_huge_branches != 0) 3191 this->make_brlt_section(layout); 3192 if (size == 64 && again) 3193 this->brlt_section_->set_current_size(num_huge_branches); 3194 3195 typedef Unordered_set<Output_section*> Output_sections; 3196 Output_sections os_need_update; 3197 for (typename Stub_tables::iterator p = this->stub_tables_.begin(); 3198 p != this->stub_tables_.end(); 3199 ++p) 3200 { 3201 if ((*p)->size_update()) 3202 { 3203 again = true; 3204 (*p)->add_eh_frame(layout); 3205 os_need_update.insert((*p)->output_section()); 3206 } 3207 } 3208 3209 // Set output section offsets for all input sections in an output 3210 // section that just changed size. Anything past the stubs will 3211 // need updating. 3212 for (typename Output_sections::iterator p = os_need_update.begin(); 3213 p != os_need_update.end(); 3214 p++) 3215 { 3216 Output_section* os = *p; 3217 Address off = 0; 3218 typedef Output_section::Input_section_list Input_section_list; 3219 for (Input_section_list::const_iterator i = os->input_sections().begin(); 3220 i != os->input_sections().end(); 3221 ++i) 3222 { 3223 off = align_address(off, i->addralign()); 3224 if (i->is_input_section() || i->is_relaxed_input_section()) 3225 i->relobj()->set_section_offset(i->shndx(), off); 3226 if (i->is_relaxed_input_section()) 3227 { 3228 Stub_table<size, big_endian>* stub_table 3229 = static_cast<Stub_table<size, big_endian>*>( 3230 i->relaxed_input_section()); 3231 Address stub_table_size = stub_table->set_address_and_size(os, off); 3232 off += stub_table_size; 3233 // After a few iterations, set current stub table size 3234 // as min size threshold, so later stub tables can only 3235 // grow in size. 3236 if (pass >= 4) 3237 stub_table->set_min_size_threshold(stub_table_size); 3238 } 3239 else 3240 off += i->data_size(); 3241 } 3242 // If .branch_lt is part of this output section, then we have 3243 // just done the offset adjustment. 3244 os->clear_section_offsets_need_adjustment(); 3245 } 3246 3247 if (size == 64 3248 && !again 3249 && num_huge_branches != 0 3250 && parameters->options().output_is_position_independent()) 3251 { 3252 // Fill in the BRLT relocs. 3253 this->brlt_section_->reset_brlt_sizes(); 3254 for (typename Branch_lookup_table::const_iterator p 3255 = this->branch_lookup_table_.begin(); 3256 p != this->branch_lookup_table_.end(); 3257 ++p) 3258 { 3259 this->brlt_section_->add_reloc(p->first, p->second); 3260 } 3261 this->brlt_section_->finalize_brlt_sizes(); 3262 } 3263 3264 if (!again 3265 && (parameters->options().user_set_emit_stub_syms() 3266 ? parameters->options().emit_stub_syms() 3267 : (size == 64 3268 || parameters->options().output_is_position_independent() 3269 || parameters->options().emit_relocs()))) 3270 { 3271 for (typename Stub_tables::iterator p = this->stub_tables_.begin(); 3272 p != this->stub_tables_.end(); 3273 ++p) 3274 (*p)->define_stub_syms(symtab); 3275 3276 if (this->glink_ != NULL) 3277 { 3278 int stub_size = this->glink_->pltresolve_size; 3279 Address value = -stub_size; 3280 if (size == 64) 3281 { 3282 value = 8; 3283 stub_size -= 8; 3284 } 3285 this->define_local(symtab, "__glink_PLTresolve", 3286 this->glink_, value, stub_size); 3287 3288 if (size != 64) 3289 this->define_local(symtab, "__glink", this->glink_, 0, 0); 3290 } 3291 } 3292 3293 return again; 3294} 3295 3296template<int size, bool big_endian> 3297void 3298Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt, 3299 unsigned char* oview, 3300 uint64_t* paddress, 3301 off_t* plen) const 3302{ 3303 uint64_t address = plt->address(); 3304 off_t len = plt->data_size(); 3305 3306 if (plt == this->glink_) 3307 { 3308 // See Output_data_glink::do_write() for glink contents. 3309 if (len == 0) 3310 { 3311 gold_assert(parameters->doing_static_link()); 3312 // Static linking may need stubs, to support ifunc and long 3313 // branches. We need to create an output section for 3314 // .eh_frame early in the link process, to have a place to 3315 // attach stub .eh_frame info. We also need to have 3316 // registered a CIE that matches the stub CIE. Both of 3317 // these requirements are satisfied by creating an FDE and 3318 // CIE for .glink, even though static linking will leave 3319 // .glink zero length. 3320 // ??? Hopefully generating an FDE with a zero address range 3321 // won't confuse anything that consumes .eh_frame info. 3322 } 3323 else if (size == 64) 3324 { 3325 // There is one word before __glink_PLTresolve 3326 address += 8; 3327 len -= 8; 3328 } 3329 else if (parameters->options().output_is_position_independent()) 3330 { 3331 // There are two FDEs for a position independent glink. 3332 // The first covers the branch table, the second 3333 // __glink_PLTresolve at the end of glink. 3334 off_t resolve_size = this->glink_->pltresolve_size; 3335 if (oview[9] == elfcpp::DW_CFA_nop) 3336 len -= resolve_size; 3337 else 3338 { 3339 address += len - resolve_size; 3340 len = resolve_size; 3341 } 3342 } 3343 } 3344 else 3345 { 3346 // Must be a stub table. 3347 const Stub_table<size, big_endian>* stub_table 3348 = static_cast<const Stub_table<size, big_endian>*>(plt); 3349 uint64_t stub_address = stub_table->stub_address(); 3350 len -= stub_address - address; 3351 address = stub_address; 3352 } 3353 3354 *paddress = address; 3355 *plen = len; 3356} 3357 3358// A class to handle the PLT data. 3359 3360template<int size, bool big_endian> 3361class Output_data_plt_powerpc : public Output_section_data_build 3362{ 3363 public: 3364 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 3365 size, big_endian> Reloc_section; 3366 3367 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ, 3368 Reloc_section* plt_rel, 3369 const char* name) 3370 : Output_section_data_build(size == 32 ? 4 : 8), 3371 rel_(plt_rel), 3372 targ_(targ), 3373 name_(name) 3374 { } 3375 3376 // Add an entry to the PLT. 3377 void 3378 add_entry(Symbol*); 3379 3380 void 3381 add_ifunc_entry(Symbol*); 3382 3383 void 3384 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int); 3385 3386 // Return the .rela.plt section data. 3387 Reloc_section* 3388 rel_plt() const 3389 { 3390 return this->rel_; 3391 } 3392 3393 // Return the number of PLT entries. 3394 unsigned int 3395 entry_count() const 3396 { 3397 if (this->current_data_size() == 0) 3398 return 0; 3399 return ((this->current_data_size() - this->first_plt_entry_offset()) 3400 / this->plt_entry_size()); 3401 } 3402 3403 protected: 3404 void 3405 do_adjust_output_section(Output_section* os) 3406 { 3407 os->set_entsize(0); 3408 } 3409 3410 // Write to a map file. 3411 void 3412 do_print_to_mapfile(Mapfile* mapfile) const 3413 { mapfile->print_output_data(this, this->name_); } 3414 3415 private: 3416 // Return the offset of the first non-reserved PLT entry. 3417 unsigned int 3418 first_plt_entry_offset() const 3419 { 3420 // IPLT has no reserved entry. 3421 if (this->name_[3] == 'I') 3422 return 0; 3423 return this->targ_->first_plt_entry_offset(); 3424 } 3425 3426 // Return the size of each PLT entry. 3427 unsigned int 3428 plt_entry_size() const 3429 { 3430 return this->targ_->plt_entry_size(); 3431 } 3432 3433 // Write out the PLT data. 3434 void 3435 do_write(Output_file*); 3436 3437 // The reloc section. 3438 Reloc_section* rel_; 3439 // Allows access to .glink for do_write. 3440 Target_powerpc<size, big_endian>* targ_; 3441 // What to report in map file. 3442 const char *name_; 3443}; 3444 3445// Add an entry to the PLT. 3446 3447template<int size, bool big_endian> 3448void 3449Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym) 3450{ 3451 if (!gsym->has_plt_offset()) 3452 { 3453 section_size_type off = this->current_data_size(); 3454 if (off == 0) 3455 off += this->first_plt_entry_offset(); 3456 gsym->set_plt_offset(off); 3457 gsym->set_needs_dynsym_entry(); 3458 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT; 3459 this->rel_->add_global(gsym, dynrel, this, off, 0); 3460 off += this->plt_entry_size(); 3461 this->set_current_data_size(off); 3462 } 3463} 3464 3465// Add an entry for a global ifunc symbol that resolves locally, to the IPLT. 3466 3467template<int size, bool big_endian> 3468void 3469Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym) 3470{ 3471 if (!gsym->has_plt_offset()) 3472 { 3473 section_size_type off = this->current_data_size(); 3474 gsym->set_plt_offset(off); 3475 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE; 3476 if (size == 64 && this->targ_->abiversion() < 2) 3477 dynrel = elfcpp::R_PPC64_JMP_IREL; 3478 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0); 3479 off += this->plt_entry_size(); 3480 this->set_current_data_size(off); 3481 } 3482} 3483 3484// Add an entry for a local ifunc symbol to the IPLT. 3485 3486template<int size, bool big_endian> 3487void 3488Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry( 3489 Sized_relobj_file<size, big_endian>* relobj, 3490 unsigned int local_sym_index) 3491{ 3492 if (!relobj->local_has_plt_offset(local_sym_index)) 3493 { 3494 section_size_type off = this->current_data_size(); 3495 relobj->set_local_plt_offset(local_sym_index, off); 3496 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE; 3497 if (size == 64 && this->targ_->abiversion() < 2) 3498 dynrel = elfcpp::R_PPC64_JMP_IREL; 3499 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel, 3500 this, off, 0); 3501 off += this->plt_entry_size(); 3502 this->set_current_data_size(off); 3503 } 3504} 3505 3506static const uint32_t add_0_11_11 = 0x7c0b5a14; 3507static const uint32_t add_2_2_11 = 0x7c425a14; 3508static const uint32_t add_2_2_12 = 0x7c426214; 3509static const uint32_t add_3_3_2 = 0x7c631214; 3510static const uint32_t add_3_3_13 = 0x7c636a14; 3511static const uint32_t add_11_0_11 = 0x7d605a14; 3512static const uint32_t add_11_2_11 = 0x7d625a14; 3513static const uint32_t add_11_11_2 = 0x7d6b1214; 3514static const uint32_t addi_0_12 = 0x380c0000; 3515static const uint32_t addi_2_2 = 0x38420000; 3516static const uint32_t addi_3_3 = 0x38630000; 3517static const uint32_t addi_11_11 = 0x396b0000; 3518static const uint32_t addi_12_1 = 0x39810000; 3519static const uint32_t addi_12_12 = 0x398c0000; 3520static const uint32_t addis_0_2 = 0x3c020000; 3521static const uint32_t addis_0_13 = 0x3c0d0000; 3522static const uint32_t addis_2_12 = 0x3c4c0000; 3523static const uint32_t addis_11_2 = 0x3d620000; 3524static const uint32_t addis_11_11 = 0x3d6b0000; 3525static const uint32_t addis_11_30 = 0x3d7e0000; 3526static const uint32_t addis_12_1 = 0x3d810000; 3527static const uint32_t addis_12_2 = 0x3d820000; 3528static const uint32_t addis_12_12 = 0x3d8c0000; 3529static const uint32_t b = 0x48000000; 3530static const uint32_t bcl_20_31 = 0x429f0005; 3531static const uint32_t bctr = 0x4e800420; 3532static const uint32_t blr = 0x4e800020; 3533static const uint32_t bnectr_p4 = 0x4ce20420; 3534static const uint32_t cmpld_7_12_0 = 0x7fac0040; 3535static const uint32_t cmpldi_2_0 = 0x28220000; 3536static const uint32_t cror_15_15_15 = 0x4def7b82; 3537static const uint32_t cror_31_31_31 = 0x4ffffb82; 3538static const uint32_t ld_0_1 = 0xe8010000; 3539static const uint32_t ld_0_12 = 0xe80c0000; 3540static const uint32_t ld_2_1 = 0xe8410000; 3541static const uint32_t ld_2_2 = 0xe8420000; 3542static const uint32_t ld_2_11 = 0xe84b0000; 3543static const uint32_t ld_2_12 = 0xe84c0000; 3544static const uint32_t ld_11_2 = 0xe9620000; 3545static const uint32_t ld_11_11 = 0xe96b0000; 3546static const uint32_t ld_12_2 = 0xe9820000; 3547static const uint32_t ld_12_11 = 0xe98b0000; 3548static const uint32_t ld_12_12 = 0xe98c0000; 3549static const uint32_t lfd_0_1 = 0xc8010000; 3550static const uint32_t li_0_0 = 0x38000000; 3551static const uint32_t li_12_0 = 0x39800000; 3552static const uint32_t lis_0 = 0x3c000000; 3553static const uint32_t lis_2 = 0x3c400000; 3554static const uint32_t lis_11 = 0x3d600000; 3555static const uint32_t lis_12 = 0x3d800000; 3556static const uint32_t lvx_0_12_0 = 0x7c0c00ce; 3557static const uint32_t lwz_0_12 = 0x800c0000; 3558static const uint32_t lwz_11_11 = 0x816b0000; 3559static const uint32_t lwz_11_30 = 0x817e0000; 3560static const uint32_t lwz_12_12 = 0x818c0000; 3561static const uint32_t lwzu_0_12 = 0x840c0000; 3562static const uint32_t mflr_0 = 0x7c0802a6; 3563static const uint32_t mflr_11 = 0x7d6802a6; 3564static const uint32_t mflr_12 = 0x7d8802a6; 3565static const uint32_t mtctr_0 = 0x7c0903a6; 3566static const uint32_t mtctr_11 = 0x7d6903a6; 3567static const uint32_t mtctr_12 = 0x7d8903a6; 3568static const uint32_t mtlr_0 = 0x7c0803a6; 3569static const uint32_t mtlr_12 = 0x7d8803a6; 3570static const uint32_t nop = 0x60000000; 3571static const uint32_t ori_0_0_0 = 0x60000000; 3572static const uint32_t srdi_0_0_2 = 0x7800f082; 3573static const uint32_t std_0_1 = 0xf8010000; 3574static const uint32_t std_0_12 = 0xf80c0000; 3575static const uint32_t std_2_1 = 0xf8410000; 3576static const uint32_t stfd_0_1 = 0xd8010000; 3577static const uint32_t stvx_0_12_0 = 0x7c0c01ce; 3578static const uint32_t sub_11_11_12 = 0x7d6c5850; 3579static const uint32_t sub_12_12_11 = 0x7d8b6050; 3580static const uint32_t xor_2_12_12 = 0x7d826278; 3581static const uint32_t xor_11_12_12 = 0x7d8b6278; 3582 3583// Write out the PLT. 3584 3585template<int size, bool big_endian> 3586void 3587Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of) 3588{ 3589 if (size == 32 && this->name_[3] != 'I') 3590 { 3591 const section_size_type offset = this->offset(); 3592 const section_size_type oview_size 3593 = convert_to_section_size_type(this->data_size()); 3594 unsigned char* const oview = of->get_output_view(offset, oview_size); 3595 unsigned char* pov = oview; 3596 unsigned char* endpov = oview + oview_size; 3597 3598 // The address of the .glink branch table 3599 const Output_data_glink<size, big_endian>* glink 3600 = this->targ_->glink_section(); 3601 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address(); 3602 3603 while (pov < endpov) 3604 { 3605 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab); 3606 pov += 4; 3607 branch_tab += 4; 3608 } 3609 3610 of->write_output_view(offset, oview_size, oview); 3611 } 3612} 3613 3614// Create the PLT section. 3615 3616template<int size, bool big_endian> 3617void 3618Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab, 3619 Layout* layout) 3620{ 3621 if (this->plt_ == NULL) 3622 { 3623 if (this->got_ == NULL) 3624 this->got_section(symtab, layout); 3625 3626 if (this->glink_ == NULL) 3627 make_glink_section(layout); 3628 3629 // Ensure that .rela.dyn always appears before .rela.plt This is 3630 // necessary due to how, on PowerPC and some other targets, .rela.dyn 3631 // needs to include .rela.plt in its range. 3632 this->rela_dyn_section(layout); 3633 3634 Reloc_section* plt_rel = new Reloc_section(false); 3635 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, 3636 elfcpp::SHF_ALLOC, plt_rel, 3637 ORDER_DYNAMIC_PLT_RELOCS, false); 3638 this->plt_ 3639 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel, 3640 "** PLT"); 3641 layout->add_output_section_data(".plt", 3642 (size == 32 3643 ? elfcpp::SHT_PROGBITS 3644 : elfcpp::SHT_NOBITS), 3645 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, 3646 this->plt_, 3647 (size == 32 3648 ? ORDER_SMALL_DATA 3649 : ORDER_SMALL_BSS), 3650 false); 3651 3652 Output_section* rela_plt_os = plt_rel->output_section(); 3653 rela_plt_os->set_info_section(this->plt_->output_section()); 3654 } 3655} 3656 3657// Create the IPLT section. 3658 3659template<int size, bool big_endian> 3660void 3661Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab, 3662 Layout* layout) 3663{ 3664 if (this->iplt_ == NULL) 3665 { 3666 this->make_plt_section(symtab, layout); 3667 3668 Reloc_section* iplt_rel = new Reloc_section(false); 3669 if (this->rela_dyn_->output_section()) 3670 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel); 3671 this->iplt_ 3672 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel, 3673 "** IPLT"); 3674 if (this->plt_->output_section()) 3675 this->plt_->output_section()->add_output_section_data(this->iplt_); 3676 } 3677} 3678 3679// A section for huge long branch addresses, similar to plt section. 3680 3681template<int size, bool big_endian> 3682class Output_data_brlt_powerpc : public Output_section_data_build 3683{ 3684 public: 3685 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 3686 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 3687 size, big_endian> Reloc_section; 3688 3689 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ, 3690 Reloc_section* brlt_rel) 3691 : Output_section_data_build(size == 32 ? 4 : 8), 3692 rel_(brlt_rel), 3693 targ_(targ) 3694 { } 3695 3696 void 3697 reset_brlt_sizes() 3698 { 3699 this->reset_data_size(); 3700 this->rel_->reset_data_size(); 3701 } 3702 3703 void 3704 finalize_brlt_sizes() 3705 { 3706 this->finalize_data_size(); 3707 this->rel_->finalize_data_size(); 3708 } 3709 3710 // Add a reloc for an entry in the BRLT. 3711 void 3712 add_reloc(Address to, unsigned int off) 3713 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); } 3714 3715 // Update section and reloc section size. 3716 void 3717 set_current_size(unsigned int num_branches) 3718 { 3719 this->reset_address_and_file_offset(); 3720 this->set_current_data_size(num_branches * 16); 3721 this->finalize_data_size(); 3722 Output_section* os = this->output_section(); 3723 os->set_section_offsets_need_adjustment(); 3724 if (this->rel_ != NULL) 3725 { 3726 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size; 3727 this->rel_->reset_address_and_file_offset(); 3728 this->rel_->set_current_data_size(num_branches * reloc_size); 3729 this->rel_->finalize_data_size(); 3730 Output_section* os = this->rel_->output_section(); 3731 os->set_section_offsets_need_adjustment(); 3732 } 3733 } 3734 3735 protected: 3736 void 3737 do_adjust_output_section(Output_section* os) 3738 { 3739 os->set_entsize(0); 3740 } 3741 3742 // Write to a map file. 3743 void 3744 do_print_to_mapfile(Mapfile* mapfile) const 3745 { mapfile->print_output_data(this, "** BRLT"); } 3746 3747 private: 3748 // Write out the BRLT data. 3749 void 3750 do_write(Output_file*); 3751 3752 // The reloc section. 3753 Reloc_section* rel_; 3754 Target_powerpc<size, big_endian>* targ_; 3755}; 3756 3757// Make the branch lookup table section. 3758 3759template<int size, bool big_endian> 3760void 3761Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout) 3762{ 3763 if (size == 64 && this->brlt_section_ == NULL) 3764 { 3765 Reloc_section* brlt_rel = NULL; 3766 bool is_pic = parameters->options().output_is_position_independent(); 3767 if (is_pic) 3768 { 3769 // When PIC we can't fill in .branch_lt (like .plt it can be 3770 // a bss style section) but must initialise at runtime via 3771 // dynamic relocations. 3772 this->rela_dyn_section(layout); 3773 brlt_rel = new Reloc_section(false); 3774 if (this->rela_dyn_->output_section()) 3775 this->rela_dyn_->output_section() 3776 ->add_output_section_data(brlt_rel); 3777 } 3778 this->brlt_section_ 3779 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel); 3780 if (this->plt_ && is_pic && this->plt_->output_section()) 3781 this->plt_->output_section() 3782 ->add_output_section_data(this->brlt_section_); 3783 else 3784 layout->add_output_section_data(".branch_lt", 3785 (is_pic ? elfcpp::SHT_NOBITS 3786 : elfcpp::SHT_PROGBITS), 3787 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, 3788 this->brlt_section_, 3789 (is_pic ? ORDER_SMALL_BSS 3790 : ORDER_SMALL_DATA), 3791 false); 3792 } 3793} 3794 3795// Write out .branch_lt when non-PIC. 3796 3797template<int size, bool big_endian> 3798void 3799Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of) 3800{ 3801 if (size == 64 && !parameters->options().output_is_position_independent()) 3802 { 3803 const section_size_type offset = this->offset(); 3804 const section_size_type oview_size 3805 = convert_to_section_size_type(this->data_size()); 3806 unsigned char* const oview = of->get_output_view(offset, oview_size); 3807 3808 this->targ_->write_branch_lookup_table(oview); 3809 of->write_output_view(offset, oview_size, oview); 3810 } 3811} 3812 3813static inline uint32_t 3814l(uint32_t a) 3815{ 3816 return a & 0xffff; 3817} 3818 3819static inline uint32_t 3820hi(uint32_t a) 3821{ 3822 return l(a >> 16); 3823} 3824 3825static inline uint32_t 3826ha(uint32_t a) 3827{ 3828 return hi(a + 0x8000); 3829} 3830 3831template<int size> 3832struct Eh_cie 3833{ 3834 static const unsigned char eh_frame_cie[12]; 3835}; 3836 3837template<int size> 3838const unsigned char Eh_cie<size>::eh_frame_cie[] = 3839{ 3840 1, // CIE version. 3841 'z', 'R', 0, // Augmentation string. 3842 4, // Code alignment. 3843 0x80 - size / 8 , // Data alignment. 3844 65, // RA reg. 3845 1, // Augmentation size. 3846 (elfcpp::DW_EH_PE_pcrel 3847 | elfcpp::DW_EH_PE_sdata4), // FDE encoding. 3848 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0. 3849}; 3850 3851// Describe __glink_PLTresolve use of LR, 64-bit version ABIv1. 3852static const unsigned char glink_eh_frame_fde_64v1[] = 3853{ 3854 0, 0, 0, 0, // Replaced with offset to .glink. 3855 0, 0, 0, 0, // Replaced with size of .glink. 3856 0, // Augmentation size. 3857 elfcpp::DW_CFA_advance_loc + 1, 3858 elfcpp::DW_CFA_register, 65, 12, 3859 elfcpp::DW_CFA_advance_loc + 4, 3860 elfcpp::DW_CFA_restore_extended, 65 3861}; 3862 3863// Describe __glink_PLTresolve use of LR, 64-bit version ABIv2. 3864static const unsigned char glink_eh_frame_fde_64v2[] = 3865{ 3866 0, 0, 0, 0, // Replaced with offset to .glink. 3867 0, 0, 0, 0, // Replaced with size of .glink. 3868 0, // Augmentation size. 3869 elfcpp::DW_CFA_advance_loc + 1, 3870 elfcpp::DW_CFA_register, 65, 0, 3871 elfcpp::DW_CFA_advance_loc + 4, 3872 elfcpp::DW_CFA_restore_extended, 65 3873}; 3874 3875// Describe __glink_PLTresolve use of LR, 32-bit version. 3876static const unsigned char glink_eh_frame_fde_32[] = 3877{ 3878 0, 0, 0, 0, // Replaced with offset to .glink. 3879 0, 0, 0, 0, // Replaced with size of .glink. 3880 0, // Augmentation size. 3881 elfcpp::DW_CFA_advance_loc + 2, 3882 elfcpp::DW_CFA_register, 65, 0, 3883 elfcpp::DW_CFA_advance_loc + 4, 3884 elfcpp::DW_CFA_restore_extended, 65 3885}; 3886 3887static const unsigned char default_fde[] = 3888{ 3889 0, 0, 0, 0, // Replaced with offset to stubs. 3890 0, 0, 0, 0, // Replaced with size of stubs. 3891 0, // Augmentation size. 3892 elfcpp::DW_CFA_nop, // Pad. 3893 elfcpp::DW_CFA_nop, 3894 elfcpp::DW_CFA_nop 3895}; 3896 3897template<bool big_endian> 3898static inline void 3899write_insn(unsigned char* p, uint32_t v) 3900{ 3901 elfcpp::Swap<32, big_endian>::writeval(p, v); 3902} 3903 3904// Stub_table holds information about plt and long branch stubs. 3905// Stubs are built in an area following some input section determined 3906// by group_sections(). This input section is converted to a relaxed 3907// input section allowing it to be resized to accommodate the stubs 3908 3909template<int size, bool big_endian> 3910class Stub_table : public Output_relaxed_input_section 3911{ 3912 public: 3913 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 3914 static const Address invalid_address = static_cast<Address>(0) - 1; 3915 3916 Stub_table(Target_powerpc<size, big_endian>* targ, 3917 Output_section* output_section, 3918 const Output_section::Input_section* owner, 3919 uint32_t id) 3920 : Output_relaxed_input_section(owner->relobj(), owner->shndx(), 3921 owner->relobj() 3922 ->section_addralign(owner->shndx())), 3923 targ_(targ), plt_call_stubs_(), long_branch_stubs_(), 3924 orig_data_size_(owner->current_data_size()), 3925 plt_size_(0), last_plt_size_(0), 3926 branch_size_(0), last_branch_size_(0), min_size_threshold_(0), 3927 eh_frame_added_(false), need_save_res_(false), uniq_(id) 3928 { 3929 this->set_output_section(output_section); 3930 3931 std::vector<Output_relaxed_input_section*> new_relaxed; 3932 new_relaxed.push_back(this); 3933 output_section->convert_input_sections_to_relaxed_sections(new_relaxed); 3934 } 3935 3936 // Add a plt call stub. 3937 bool 3938 add_plt_call_entry(Address, 3939 const Sized_relobj_file<size, big_endian>*, 3940 const Symbol*, 3941 unsigned int, 3942 Address); 3943 3944 bool 3945 add_plt_call_entry(Address, 3946 const Sized_relobj_file<size, big_endian>*, 3947 unsigned int, 3948 unsigned int, 3949 Address); 3950 3951 // Find a given plt call stub. 3952 Address 3953 find_plt_call_entry(const Symbol*) const; 3954 3955 Address 3956 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*, 3957 unsigned int) const; 3958 3959 Address 3960 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*, 3961 const Symbol*, 3962 unsigned int, 3963 Address) const; 3964 3965 Address 3966 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*, 3967 unsigned int, 3968 unsigned int, 3969 Address) const; 3970 3971 // Add a long branch stub. 3972 bool 3973 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*, 3974 unsigned int, Address, Address, bool); 3975 3976 Address 3977 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*, 3978 Address) const; 3979 3980 bool 3981 can_reach_stub(Address from, unsigned int off, unsigned int r_type) 3982 { 3983 Address max_branch_offset = max_branch_delta(r_type); 3984 if (max_branch_offset == 0) 3985 return true; 3986 gold_assert(from != invalid_address); 3987 Address loc = off + this->stub_address(); 3988 return loc - from + max_branch_offset < 2 * max_branch_offset; 3989 } 3990 3991 void 3992 clear_stubs(bool all) 3993 { 3994 this->plt_call_stubs_.clear(); 3995 this->plt_size_ = 0; 3996 this->long_branch_stubs_.clear(); 3997 this->branch_size_ = 0; 3998 this->need_save_res_ = false; 3999 if (all) 4000 { 4001 this->last_plt_size_ = 0; 4002 this->last_branch_size_ = 0; 4003 } 4004 } 4005 4006 Address 4007 set_address_and_size(const Output_section* os, Address off) 4008 { 4009 Address start_off = off; 4010 off += this->orig_data_size_; 4011 Address my_size = this->plt_size_ + this->branch_size_; 4012 if (this->need_save_res_) 4013 my_size += this->targ_->savres_section()->data_size(); 4014 if (my_size != 0) 4015 off = align_address(off, this->stub_align()); 4016 // Include original section size and alignment padding in size 4017 my_size += off - start_off; 4018 // Ensure new size is always larger than min size 4019 // threshold. Alignment requirement is included in "my_size", so 4020 // increase "my_size" does not invalidate alignment. 4021 if (my_size < this->min_size_threshold_) 4022 my_size = this->min_size_threshold_; 4023 this->reset_address_and_file_offset(); 4024 this->set_current_data_size(my_size); 4025 this->set_address_and_file_offset(os->address() + start_off, 4026 os->offset() + start_off); 4027 return my_size; 4028 } 4029 4030 Address 4031 stub_address() const 4032 { 4033 return align_address(this->address() + this->orig_data_size_, 4034 this->stub_align()); 4035 } 4036 4037 Address 4038 stub_offset() const 4039 { 4040 return align_address(this->offset() + this->orig_data_size_, 4041 this->stub_align()); 4042 } 4043 4044 section_size_type 4045 plt_size() const 4046 { return this->plt_size_; } 4047 4048 void 4049 set_min_size_threshold(Address min_size) 4050 { this->min_size_threshold_ = min_size; } 4051 4052 void 4053 define_stub_syms(Symbol_table*); 4054 4055 bool 4056 size_update() 4057 { 4058 Output_section* os = this->output_section(); 4059 if (os->addralign() < this->stub_align()) 4060 { 4061 os->set_addralign(this->stub_align()); 4062 // FIXME: get rid of the insane checkpointing. 4063 // We can't increase alignment of the input section to which 4064 // stubs are attached; The input section may be .init which 4065 // is pasted together with other .init sections to form a 4066 // function. Aligning might insert zero padding resulting in 4067 // sigill. However we do need to increase alignment of the 4068 // output section so that the align_address() on offset in 4069 // set_address_and_size() adds the same padding as the 4070 // align_address() on address in stub_address(). 4071 // What's more, we need this alignment for the layout done in 4072 // relaxation_loop_body() so that the output section starts at 4073 // a suitably aligned address. 4074 os->checkpoint_set_addralign(this->stub_align()); 4075 } 4076 if (this->last_plt_size_ != this->plt_size_ 4077 || this->last_branch_size_ != this->branch_size_) 4078 { 4079 this->last_plt_size_ = this->plt_size_; 4080 this->last_branch_size_ = this->branch_size_; 4081 return true; 4082 } 4083 return false; 4084 } 4085 4086 // Add .eh_frame info for this stub section. Unlike other linker 4087 // generated .eh_frame this is added late in the link, because we 4088 // only want the .eh_frame info if this particular stub section is 4089 // non-empty. 4090 void 4091 add_eh_frame(Layout* layout) 4092 { 4093 if (!this->eh_frame_added_) 4094 { 4095 if (!parameters->options().ld_generated_unwind_info()) 4096 return; 4097 4098 // Since we add stub .eh_frame info late, it must be placed 4099 // after all other linker generated .eh_frame info so that 4100 // merge mapping need not be updated for input sections. 4101 // There is no provision to use a different CIE to that used 4102 // by .glink. 4103 if (!this->targ_->has_glink()) 4104 return; 4105 4106 layout->add_eh_frame_for_plt(this, 4107 Eh_cie<size>::eh_frame_cie, 4108 sizeof (Eh_cie<size>::eh_frame_cie), 4109 default_fde, 4110 sizeof (default_fde)); 4111 this->eh_frame_added_ = true; 4112 } 4113 } 4114 4115 Target_powerpc<size, big_endian>* 4116 targ() const 4117 { return targ_; } 4118 4119 private: 4120 class Plt_stub_ent; 4121 class Plt_stub_ent_hash; 4122 typedef Unordered_map<Plt_stub_ent, unsigned int, 4123 Plt_stub_ent_hash> Plt_stub_entries; 4124 class Branch_stub_ent; 4125 class Branch_stub_ent_hash; 4126 typedef Unordered_map<Branch_stub_ent, unsigned int, 4127 Branch_stub_ent_hash> Branch_stub_entries; 4128 4129 // Alignment of stub section. 4130 unsigned int 4131 stub_align() const 4132 { 4133 if (size == 32) 4134 return 16; 4135 unsigned int min_align = 32; 4136 unsigned int user_align = 1 << parameters->options().plt_align(); 4137 return std::max(user_align, min_align); 4138 } 4139 4140 // Return the plt offset for the given call stub. 4141 Address 4142 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const 4143 { 4144 const Symbol* gsym = p->first.sym_; 4145 if (gsym != NULL) 4146 { 4147 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC 4148 && gsym->can_use_relative_reloc(false)); 4149 return gsym->plt_offset(); 4150 } 4151 else 4152 { 4153 *is_iplt = true; 4154 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_; 4155 unsigned int local_sym_index = p->first.locsym_; 4156 return relobj->local_plt_offset(local_sym_index); 4157 } 4158 } 4159 4160 // Size of a given plt call stub. 4161 unsigned int 4162 plt_call_size(typename Plt_stub_entries::const_iterator p) const 4163 { 4164 if (size == 32) 4165 return 16; 4166 4167 bool is_iplt; 4168 Address plt_addr = this->plt_off(p, &is_iplt); 4169 if (is_iplt) 4170 plt_addr += this->targ_->iplt_section()->address(); 4171 else 4172 plt_addr += this->targ_->plt_section()->address(); 4173 Address got_addr = this->targ_->got_section()->output_section()->address(); 4174 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast 4175 <const Powerpc_relobj<size, big_endian>*>(p->first.object_); 4176 got_addr += ppcobj->toc_base_offset(); 4177 Address off = plt_addr - got_addr; 4178 unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0); 4179 if (this->targ_->abiversion() < 2) 4180 { 4181 bool static_chain = parameters->options().plt_static_chain(); 4182 bool thread_safe = this->targ_->plt_thread_safe(); 4183 bytes += (4 4184 + 4 * static_chain 4185 + 8 * thread_safe 4186 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))); 4187 } 4188 unsigned int align = 1 << parameters->options().plt_align(); 4189 if (align > 1) 4190 bytes = (bytes + align - 1) & -align; 4191 return bytes; 4192 } 4193 4194 // Return long branch stub size. 4195 unsigned int 4196 branch_stub_size(typename Branch_stub_entries::const_iterator p) 4197 { 4198 Address loc = this->stub_address() + this->last_plt_size_ + p->second; 4199 if (p->first.dest_ - loc + (1 << 25) < 2 << 25) 4200 return 4; 4201 if (size == 64 || !parameters->options().output_is_position_independent()) 4202 return 16; 4203 return 32; 4204 } 4205 4206 // Write out stubs. 4207 void 4208 do_write(Output_file*); 4209 4210 // Plt call stub keys. 4211 class Plt_stub_ent 4212 { 4213 public: 4214 Plt_stub_ent(const Symbol* sym) 4215 : sym_(sym), object_(0), addend_(0), locsym_(0) 4216 { } 4217 4218 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object, 4219 unsigned int locsym_index) 4220 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index) 4221 { } 4222 4223 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object, 4224 const Symbol* sym, 4225 unsigned int r_type, 4226 Address addend) 4227 : sym_(sym), object_(0), addend_(0), locsym_(0) 4228 { 4229 if (size != 32) 4230 this->addend_ = addend; 4231 else if (parameters->options().output_is_position_independent() 4232 && r_type == elfcpp::R_PPC_PLTREL24) 4233 { 4234 this->addend_ = addend; 4235 if (this->addend_ >= 32768) 4236 this->object_ = object; 4237 } 4238 } 4239 4240 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object, 4241 unsigned int locsym_index, 4242 unsigned int r_type, 4243 Address addend) 4244 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index) 4245 { 4246 if (size != 32) 4247 this->addend_ = addend; 4248 else if (parameters->options().output_is_position_independent() 4249 && r_type == elfcpp::R_PPC_PLTREL24) 4250 this->addend_ = addend; 4251 } 4252 4253 bool operator==(const Plt_stub_ent& that) const 4254 { 4255 return (this->sym_ == that.sym_ 4256 && this->object_ == that.object_ 4257 && this->addend_ == that.addend_ 4258 && this->locsym_ == that.locsym_); 4259 } 4260 4261 const Symbol* sym_; 4262 const Sized_relobj_file<size, big_endian>* object_; 4263 typename elfcpp::Elf_types<size>::Elf_Addr addend_; 4264 unsigned int locsym_; 4265 unsigned int indx_; 4266 }; 4267 4268 class Plt_stub_ent_hash 4269 { 4270 public: 4271 size_t operator()(const Plt_stub_ent& ent) const 4272 { 4273 return (reinterpret_cast<uintptr_t>(ent.sym_) 4274 ^ reinterpret_cast<uintptr_t>(ent.object_) 4275 ^ ent.addend_ 4276 ^ ent.locsym_); 4277 } 4278 }; 4279 4280 // Long branch stub keys. 4281 class Branch_stub_ent 4282 { 4283 public: 4284 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj, 4285 Address to, bool save_res) 4286 : dest_(to), toc_base_off_(0), save_res_(save_res) 4287 { 4288 if (size == 64) 4289 toc_base_off_ = obj->toc_base_offset(); 4290 } 4291 4292 bool operator==(const Branch_stub_ent& that) const 4293 { 4294 return (this->dest_ == that.dest_ 4295 && (size == 32 4296 || this->toc_base_off_ == that.toc_base_off_)); 4297 } 4298 4299 Address dest_; 4300 unsigned int toc_base_off_; 4301 bool save_res_; 4302 }; 4303 4304 class Branch_stub_ent_hash 4305 { 4306 public: 4307 size_t operator()(const Branch_stub_ent& ent) const 4308 { return ent.dest_ ^ ent.toc_base_off_; } 4309 }; 4310 4311 // In a sane world this would be a global. 4312 Target_powerpc<size, big_endian>* targ_; 4313 // Map sym/object/addend to stub offset. 4314 Plt_stub_entries plt_call_stubs_; 4315 // Map destination address to stub offset. 4316 Branch_stub_entries long_branch_stubs_; 4317 // size of input section 4318 section_size_type orig_data_size_; 4319 // size of stubs 4320 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_; 4321 // Some rare cases cause (PR/20529) fluctuation in stub table 4322 // size, which leads to an endless relax loop. This is to be fixed 4323 // by, after the first few iterations, allowing only increase of 4324 // stub table size. This variable sets the minimal possible size of 4325 // a stub table, it is zero for the first few iterations, then 4326 // increases monotonically. 4327 Address min_size_threshold_; 4328 // Whether .eh_frame info has been created for this stub section. 4329 bool eh_frame_added_; 4330 // Set if this stub group needs a copy of out-of-line register 4331 // save/restore functions. 4332 bool need_save_res_; 4333 // Per stub table unique identifier. 4334 uint32_t uniq_; 4335}; 4336 4337// Add a plt call stub, if we do not already have one for this 4338// sym/object/addend combo. 4339 4340template<int size, bool big_endian> 4341bool 4342Stub_table<size, big_endian>::add_plt_call_entry( 4343 Address from, 4344 const Sized_relobj_file<size, big_endian>* object, 4345 const Symbol* gsym, 4346 unsigned int r_type, 4347 Address addend) 4348{ 4349 Plt_stub_ent ent(object, gsym, r_type, addend); 4350 unsigned int off = this->plt_size_; 4351 ent.indx_ = this->plt_call_stubs_.size(); 4352 std::pair<typename Plt_stub_entries::iterator, bool> p 4353 = this->plt_call_stubs_.insert(std::make_pair(ent, off)); 4354 if (p.second) 4355 this->plt_size_ = off + this->plt_call_size(p.first); 4356 return this->can_reach_stub(from, off, r_type); 4357} 4358 4359template<int size, bool big_endian> 4360bool 4361Stub_table<size, big_endian>::add_plt_call_entry( 4362 Address from, 4363 const Sized_relobj_file<size, big_endian>* object, 4364 unsigned int locsym_index, 4365 unsigned int r_type, 4366 Address addend) 4367{ 4368 Plt_stub_ent ent(object, locsym_index, r_type, addend); 4369 unsigned int off = this->plt_size_; 4370 ent.indx_ = this->plt_call_stubs_.size(); 4371 std::pair<typename Plt_stub_entries::iterator, bool> p 4372 = this->plt_call_stubs_.insert(std::make_pair(ent, off)); 4373 if (p.second) 4374 this->plt_size_ = off + this->plt_call_size(p.first); 4375 return this->can_reach_stub(from, off, r_type); 4376} 4377 4378// Find a plt call stub. 4379 4380template<int size, bool big_endian> 4381typename Stub_table<size, big_endian>::Address 4382Stub_table<size, big_endian>::find_plt_call_entry( 4383 const Sized_relobj_file<size, big_endian>* object, 4384 const Symbol* gsym, 4385 unsigned int r_type, 4386 Address addend) const 4387{ 4388 Plt_stub_ent ent(object, gsym, r_type, addend); 4389 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent); 4390 return p == this->plt_call_stubs_.end() ? invalid_address : p->second; 4391} 4392 4393template<int size, bool big_endian> 4394typename Stub_table<size, big_endian>::Address 4395Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const 4396{ 4397 Plt_stub_ent ent(gsym); 4398 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent); 4399 return p == this->plt_call_stubs_.end() ? invalid_address : p->second; 4400} 4401 4402template<int size, bool big_endian> 4403typename Stub_table<size, big_endian>::Address 4404Stub_table<size, big_endian>::find_plt_call_entry( 4405 const Sized_relobj_file<size, big_endian>* object, 4406 unsigned int locsym_index, 4407 unsigned int r_type, 4408 Address addend) const 4409{ 4410 Plt_stub_ent ent(object, locsym_index, r_type, addend); 4411 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent); 4412 return p == this->plt_call_stubs_.end() ? invalid_address : p->second; 4413} 4414 4415template<int size, bool big_endian> 4416typename Stub_table<size, big_endian>::Address 4417Stub_table<size, big_endian>::find_plt_call_entry( 4418 const Sized_relobj_file<size, big_endian>* object, 4419 unsigned int locsym_index) const 4420{ 4421 Plt_stub_ent ent(object, locsym_index); 4422 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent); 4423 return p == this->plt_call_stubs_.end() ? invalid_address : p->second; 4424} 4425 4426// Add a long branch stub if we don't already have one to given 4427// destination. 4428 4429template<int size, bool big_endian> 4430bool 4431Stub_table<size, big_endian>::add_long_branch_entry( 4432 const Powerpc_relobj<size, big_endian>* object, 4433 unsigned int r_type, 4434 Address from, 4435 Address to, 4436 bool save_res) 4437{ 4438 Branch_stub_ent ent(object, to, save_res); 4439 Address off = this->branch_size_; 4440 std::pair<typename Branch_stub_entries::iterator, bool> p 4441 = this->long_branch_stubs_.insert(std::make_pair(ent, off)); 4442 if (p.second) 4443 { 4444 if (save_res) 4445 this->need_save_res_ = true; 4446 else 4447 { 4448 unsigned int stub_size = this->branch_stub_size(p.first); 4449 this->branch_size_ = off + stub_size; 4450 if (size == 64 && stub_size != 4) 4451 this->targ_->add_branch_lookup_table(to); 4452 } 4453 } 4454 return this->can_reach_stub(from, off, r_type); 4455} 4456 4457// Find long branch stub offset. 4458 4459template<int size, bool big_endian> 4460typename Stub_table<size, big_endian>::Address 4461Stub_table<size, big_endian>::find_long_branch_entry( 4462 const Powerpc_relobj<size, big_endian>* object, 4463 Address to) const 4464{ 4465 Branch_stub_ent ent(object, to, false); 4466 typename Branch_stub_entries::const_iterator p 4467 = this->long_branch_stubs_.find(ent); 4468 if (p == this->long_branch_stubs_.end()) 4469 return invalid_address; 4470 if (p->first.save_res_) 4471 return to - this->targ_->savres_section()->address() + this->branch_size_; 4472 return p->second; 4473} 4474 4475// A class to handle .glink. 4476 4477template<int size, bool big_endian> 4478class Output_data_glink : public Output_section_data 4479{ 4480 public: 4481 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 4482 static const Address invalid_address = static_cast<Address>(0) - 1; 4483 static const int pltresolve_size = 16*4; 4484 4485 Output_data_glink(Target_powerpc<size, big_endian>* targ) 4486 : Output_section_data(16), targ_(targ), global_entry_stubs_(), 4487 end_branch_table_(), ge_size_(0) 4488 { } 4489 4490 void 4491 add_eh_frame(Layout* layout); 4492 4493 void 4494 add_global_entry(const Symbol*); 4495 4496 Address 4497 find_global_entry(const Symbol*) const; 4498 4499 Address 4500 global_entry_address() const 4501 { 4502 gold_assert(this->is_data_size_valid()); 4503 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16; 4504 return this->address() + global_entry_off; 4505 } 4506 4507 protected: 4508 // Write to a map file. 4509 void 4510 do_print_to_mapfile(Mapfile* mapfile) const 4511 { mapfile->print_output_data(this, _("** glink")); } 4512 4513 private: 4514 void 4515 set_final_data_size(); 4516 4517 // Write out .glink 4518 void 4519 do_write(Output_file*); 4520 4521 // Allows access to .got and .plt for do_write. 4522 Target_powerpc<size, big_endian>* targ_; 4523 4524 // Map sym to stub offset. 4525 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries; 4526 Global_entry_stub_entries global_entry_stubs_; 4527 4528 unsigned int end_branch_table_, ge_size_; 4529}; 4530 4531template<int size, bool big_endian> 4532void 4533Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout) 4534{ 4535 if (!parameters->options().ld_generated_unwind_info()) 4536 return; 4537 4538 if (size == 64) 4539 { 4540 if (this->targ_->abiversion() < 2) 4541 layout->add_eh_frame_for_plt(this, 4542 Eh_cie<64>::eh_frame_cie, 4543 sizeof (Eh_cie<64>::eh_frame_cie), 4544 glink_eh_frame_fde_64v1, 4545 sizeof (glink_eh_frame_fde_64v1)); 4546 else 4547 layout->add_eh_frame_for_plt(this, 4548 Eh_cie<64>::eh_frame_cie, 4549 sizeof (Eh_cie<64>::eh_frame_cie), 4550 glink_eh_frame_fde_64v2, 4551 sizeof (glink_eh_frame_fde_64v2)); 4552 } 4553 else 4554 { 4555 // 32-bit .glink can use the default since the CIE return 4556 // address reg, LR, is valid. 4557 layout->add_eh_frame_for_plt(this, 4558 Eh_cie<32>::eh_frame_cie, 4559 sizeof (Eh_cie<32>::eh_frame_cie), 4560 default_fde, 4561 sizeof (default_fde)); 4562 // Except where LR is used in a PIC __glink_PLTresolve. 4563 if (parameters->options().output_is_position_independent()) 4564 layout->add_eh_frame_for_plt(this, 4565 Eh_cie<32>::eh_frame_cie, 4566 sizeof (Eh_cie<32>::eh_frame_cie), 4567 glink_eh_frame_fde_32, 4568 sizeof (glink_eh_frame_fde_32)); 4569 } 4570} 4571 4572template<int size, bool big_endian> 4573void 4574Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym) 4575{ 4576 std::pair<typename Global_entry_stub_entries::iterator, bool> p 4577 = this->global_entry_stubs_.insert(std::make_pair(gsym, this->ge_size_)); 4578 if (p.second) 4579 this->ge_size_ += 16; 4580} 4581 4582template<int size, bool big_endian> 4583typename Output_data_glink<size, big_endian>::Address 4584Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const 4585{ 4586 typename Global_entry_stub_entries::const_iterator p 4587 = this->global_entry_stubs_.find(gsym); 4588 return p == this->global_entry_stubs_.end() ? invalid_address : p->second; 4589} 4590 4591template<int size, bool big_endian> 4592void 4593Output_data_glink<size, big_endian>::set_final_data_size() 4594{ 4595 unsigned int count = this->targ_->plt_entry_count(); 4596 section_size_type total = 0; 4597 4598 if (count != 0) 4599 { 4600 if (size == 32) 4601 { 4602 // space for branch table 4603 total += 4 * (count - 1); 4604 4605 total += -total & 15; 4606 total += this->pltresolve_size; 4607 } 4608 else 4609 { 4610 total += this->pltresolve_size; 4611 4612 // space for branch table 4613 total += 4 * count; 4614 if (this->targ_->abiversion() < 2) 4615 { 4616 total += 4 * count; 4617 if (count > 0x8000) 4618 total += 4 * (count - 0x8000); 4619 } 4620 } 4621 } 4622 this->end_branch_table_ = total; 4623 total = (total + 15) & -16; 4624 total += this->ge_size_; 4625 4626 this->set_data_size(total); 4627} 4628 4629// Define symbols on stubs, identifying the stub. 4630 4631template<int size, bool big_endian> 4632void 4633Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab) 4634{ 4635 if (!this->plt_call_stubs_.empty()) 4636 { 4637 // The key for the plt call stub hash table includes addresses, 4638 // therefore traversal order depends on those addresses, which 4639 // can change between runs if gold is a PIE. Unfortunately the 4640 // output .symtab ordering depends on the order in which symbols 4641 // are added to the linker symtab. We want reproducible output 4642 // so must sort the call stub symbols. 4643 typedef typename Plt_stub_entries::const_iterator plt_iter; 4644 std::vector<plt_iter> sorted; 4645 sorted.resize(this->plt_call_stubs_.size()); 4646 4647 for (plt_iter cs = this->plt_call_stubs_.begin(); 4648 cs != this->plt_call_stubs_.end(); 4649 ++cs) 4650 sorted[cs->first.indx_] = cs; 4651 4652 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i) 4653 { 4654 plt_iter cs = sorted[i]; 4655 char add[10]; 4656 add[0] = 0; 4657 if (cs->first.addend_ != 0) 4658 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_)); 4659 char obj[10]; 4660 obj[0] = 0; 4661 if (cs->first.object_) 4662 { 4663 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast 4664 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_); 4665 sprintf(obj, "%x:", ppcobj->uniq()); 4666 } 4667 char localname[9]; 4668 const char *symname; 4669 if (cs->first.sym_ == NULL) 4670 { 4671 sprintf(localname, "%x", cs->first.locsym_); 4672 symname = localname; 4673 } 4674 else 4675 symname = cs->first.sym_->name(); 4676 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1]; 4677 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add); 4678 Address value = this->stub_address() - this->address() + cs->second; 4679 unsigned int stub_size = this->plt_call_size(cs); 4680 this->targ_->define_local(symtab, name, this, value, stub_size); 4681 } 4682 } 4683 4684 typedef typename Branch_stub_entries::const_iterator branch_iter; 4685 for (branch_iter bs = this->long_branch_stubs_.begin(); 4686 bs != this->long_branch_stubs_.end(); 4687 ++bs) 4688 { 4689 if (bs->first.save_res_) 4690 continue; 4691 4692 char* name = new char[8 + 13 + 16 + 1]; 4693 sprintf(name, "%08x.long_branch.%llx", this->uniq_, 4694 static_cast<unsigned long long>(bs->first.dest_)); 4695 Address value = (this->stub_address() - this->address() 4696 + this->plt_size_ + bs->second); 4697 unsigned int stub_size = this->branch_stub_size(bs); 4698 this->targ_->define_local(symtab, name, this, value, stub_size); 4699 } 4700} 4701 4702// Write out plt and long branch stub code. 4703 4704template<int size, bool big_endian> 4705void 4706Stub_table<size, big_endian>::do_write(Output_file* of) 4707{ 4708 if (this->plt_call_stubs_.empty() 4709 && this->long_branch_stubs_.empty()) 4710 return; 4711 4712 const section_size_type start_off = this->offset(); 4713 const section_size_type off = this->stub_offset(); 4714 const section_size_type oview_size = 4715 convert_to_section_size_type(this->data_size() - (off - start_off)); 4716 unsigned char* const oview = of->get_output_view(off, oview_size); 4717 unsigned char* p; 4718 4719 if (size == 64) 4720 { 4721 const Output_data_got_powerpc<size, big_endian>* got 4722 = this->targ_->got_section(); 4723 Address got_os_addr = got->output_section()->address(); 4724 4725 if (!this->plt_call_stubs_.empty()) 4726 { 4727 // The base address of the .plt section. 4728 Address plt_base = this->targ_->plt_section()->address(); 4729 Address iplt_base = invalid_address; 4730 4731 // Write out plt call stubs. 4732 typename Plt_stub_entries::const_iterator cs; 4733 for (cs = this->plt_call_stubs_.begin(); 4734 cs != this->plt_call_stubs_.end(); 4735 ++cs) 4736 { 4737 bool is_iplt; 4738 Address pltoff = this->plt_off(cs, &is_iplt); 4739 Address plt_addr = pltoff; 4740 if (is_iplt) 4741 { 4742 if (iplt_base == invalid_address) 4743 iplt_base = this->targ_->iplt_section()->address(); 4744 plt_addr += iplt_base; 4745 } 4746 else 4747 plt_addr += plt_base; 4748 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast 4749 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_); 4750 Address got_addr = got_os_addr + ppcobj->toc_base_offset(); 4751 Address off = plt_addr - got_addr; 4752 4753 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0) 4754 gold_error(_("%s: linkage table error against `%s'"), 4755 cs->first.object_->name().c_str(), 4756 cs->first.sym_->demangled_name().c_str()); 4757 4758 bool plt_load_toc = this->targ_->abiversion() < 2; 4759 bool static_chain 4760 = plt_load_toc && parameters->options().plt_static_chain(); 4761 bool thread_safe 4762 = plt_load_toc && this->targ_->plt_thread_safe(); 4763 bool use_fake_dep = false; 4764 Address cmp_branch_off = 0; 4765 if (thread_safe) 4766 { 4767 unsigned int pltindex 4768 = ((pltoff - this->targ_->first_plt_entry_offset()) 4769 / this->targ_->plt_entry_size()); 4770 Address glinkoff 4771 = (this->targ_->glink_section()->pltresolve_size 4772 + pltindex * 8); 4773 if (pltindex > 32768) 4774 glinkoff += (pltindex - 32768) * 4; 4775 Address to 4776 = this->targ_->glink_section()->address() + glinkoff; 4777 Address from 4778 = (this->stub_address() + cs->second + 24 4779 + 4 * (ha(off) != 0) 4780 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)) 4781 + 4 * static_chain); 4782 cmp_branch_off = to - from; 4783 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26); 4784 } 4785 4786 p = oview + cs->second; 4787 if (ha(off) != 0) 4788 { 4789 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc()); 4790 p += 4; 4791 if (plt_load_toc) 4792 { 4793 write_insn<big_endian>(p, addis_11_2 + ha(off)); 4794 p += 4; 4795 write_insn<big_endian>(p, ld_12_11 + l(off)); 4796 p += 4; 4797 } 4798 else 4799 { 4800 write_insn<big_endian>(p, addis_12_2 + ha(off)); 4801 p += 4; 4802 write_insn<big_endian>(p, ld_12_12 + l(off)); 4803 p += 4; 4804 } 4805 if (plt_load_toc 4806 && ha(off + 8 + 8 * static_chain) != ha(off)) 4807 { 4808 write_insn<big_endian>(p, addi_11_11 + l(off)); 4809 p += 4; 4810 off = 0; 4811 } 4812 write_insn<big_endian>(p, mtctr_12); 4813 p += 4; 4814 if (plt_load_toc) 4815 { 4816 if (use_fake_dep) 4817 { 4818 write_insn<big_endian>(p, xor_2_12_12); 4819 p += 4; 4820 write_insn<big_endian>(p, add_11_11_2); 4821 p += 4; 4822 } 4823 write_insn<big_endian>(p, ld_2_11 + l(off + 8)); 4824 p += 4; 4825 if (static_chain) 4826 { 4827 write_insn<big_endian>(p, ld_11_11 + l(off + 16)); 4828 p += 4; 4829 } 4830 } 4831 } 4832 else 4833 { 4834 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc()); 4835 p += 4; 4836 write_insn<big_endian>(p, ld_12_2 + l(off)); 4837 p += 4; 4838 if (plt_load_toc 4839 && ha(off + 8 + 8 * static_chain) != ha(off)) 4840 { 4841 write_insn<big_endian>(p, addi_2_2 + l(off)); 4842 p += 4; 4843 off = 0; 4844 } 4845 write_insn<big_endian>(p, mtctr_12); 4846 p += 4; 4847 if (plt_load_toc) 4848 { 4849 if (use_fake_dep) 4850 { 4851 write_insn<big_endian>(p, xor_11_12_12); 4852 p += 4; 4853 write_insn<big_endian>(p, add_2_2_11); 4854 p += 4; 4855 } 4856 if (static_chain) 4857 { 4858 write_insn<big_endian>(p, ld_11_2 + l(off + 16)); 4859 p += 4; 4860 } 4861 write_insn<big_endian>(p, ld_2_2 + l(off + 8)); 4862 p += 4; 4863 } 4864 } 4865 if (thread_safe && !use_fake_dep) 4866 { 4867 write_insn<big_endian>(p, cmpldi_2_0); 4868 p += 4; 4869 write_insn<big_endian>(p, bnectr_p4); 4870 p += 4; 4871 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc)); 4872 } 4873 else 4874 write_insn<big_endian>(p, bctr); 4875 } 4876 } 4877 4878 // Write out long branch stubs. 4879 typename Branch_stub_entries::const_iterator bs; 4880 for (bs = this->long_branch_stubs_.begin(); 4881 bs != this->long_branch_stubs_.end(); 4882 ++bs) 4883 { 4884 if (bs->first.save_res_) 4885 continue; 4886 p = oview + this->plt_size_ + bs->second; 4887 Address loc = this->stub_address() + this->plt_size_ + bs->second; 4888 Address delta = bs->first.dest_ - loc; 4889 if (delta + (1 << 25) < 2 << 25) 4890 write_insn<big_endian>(p, b | (delta & 0x3fffffc)); 4891 else 4892 { 4893 Address brlt_addr 4894 = this->targ_->find_branch_lookup_table(bs->first.dest_); 4895 gold_assert(brlt_addr != invalid_address); 4896 brlt_addr += this->targ_->brlt_section()->address(); 4897 Address got_addr = got_os_addr + bs->first.toc_base_off_; 4898 Address brltoff = brlt_addr - got_addr; 4899 if (ha(brltoff) == 0) 4900 { 4901 write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4; 4902 } 4903 else 4904 { 4905 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4; 4906 write_insn<big_endian>(p, ld_12_12 + l(brltoff)), p += 4; 4907 } 4908 write_insn<big_endian>(p, mtctr_12), p += 4; 4909 write_insn<big_endian>(p, bctr); 4910 } 4911 } 4912 } 4913 else 4914 { 4915 if (!this->plt_call_stubs_.empty()) 4916 { 4917 // The base address of the .plt section. 4918 Address plt_base = this->targ_->plt_section()->address(); 4919 Address iplt_base = invalid_address; 4920 // The address of _GLOBAL_OFFSET_TABLE_. 4921 Address g_o_t = invalid_address; 4922 4923 // Write out plt call stubs. 4924 typename Plt_stub_entries::const_iterator cs; 4925 for (cs = this->plt_call_stubs_.begin(); 4926 cs != this->plt_call_stubs_.end(); 4927 ++cs) 4928 { 4929 bool is_iplt; 4930 Address plt_addr = this->plt_off(cs, &is_iplt); 4931 if (is_iplt) 4932 { 4933 if (iplt_base == invalid_address) 4934 iplt_base = this->targ_->iplt_section()->address(); 4935 plt_addr += iplt_base; 4936 } 4937 else 4938 plt_addr += plt_base; 4939 4940 p = oview + cs->second; 4941 if (parameters->options().output_is_position_independent()) 4942 { 4943 Address got_addr; 4944 const Powerpc_relobj<size, big_endian>* ppcobj 4945 = (static_cast<const Powerpc_relobj<size, big_endian>*> 4946 (cs->first.object_)); 4947 if (ppcobj != NULL && cs->first.addend_ >= 32768) 4948 { 4949 unsigned int got2 = ppcobj->got2_shndx(); 4950 got_addr = ppcobj->get_output_section_offset(got2); 4951 gold_assert(got_addr != invalid_address); 4952 got_addr += (ppcobj->output_section(got2)->address() 4953 + cs->first.addend_); 4954 } 4955 else 4956 { 4957 if (g_o_t == invalid_address) 4958 { 4959 const Output_data_got_powerpc<size, big_endian>* got 4960 = this->targ_->got_section(); 4961 g_o_t = got->address() + got->g_o_t(); 4962 } 4963 got_addr = g_o_t; 4964 } 4965 4966 Address off = plt_addr - got_addr; 4967 if (ha(off) == 0) 4968 { 4969 write_insn<big_endian>(p + 0, lwz_11_30 + l(off)); 4970 write_insn<big_endian>(p + 4, mtctr_11); 4971 write_insn<big_endian>(p + 8, bctr); 4972 } 4973 else 4974 { 4975 write_insn<big_endian>(p + 0, addis_11_30 + ha(off)); 4976 write_insn<big_endian>(p + 4, lwz_11_11 + l(off)); 4977 write_insn<big_endian>(p + 8, mtctr_11); 4978 write_insn<big_endian>(p + 12, bctr); 4979 } 4980 } 4981 else 4982 { 4983 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr)); 4984 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr)); 4985 write_insn<big_endian>(p + 8, mtctr_11); 4986 write_insn<big_endian>(p + 12, bctr); 4987 } 4988 } 4989 } 4990 4991 // Write out long branch stubs. 4992 typename Branch_stub_entries::const_iterator bs; 4993 for (bs = this->long_branch_stubs_.begin(); 4994 bs != this->long_branch_stubs_.end(); 4995 ++bs) 4996 { 4997 if (bs->first.save_res_) 4998 continue; 4999 p = oview + this->plt_size_ + bs->second; 5000 Address loc = this->stub_address() + this->plt_size_ + bs->second; 5001 Address delta = bs->first.dest_ - loc; 5002 if (delta + (1 << 25) < 2 << 25) 5003 write_insn<big_endian>(p, b | (delta & 0x3fffffc)); 5004 else if (!parameters->options().output_is_position_independent()) 5005 { 5006 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_)); 5007 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_)); 5008 write_insn<big_endian>(p + 8, mtctr_12); 5009 write_insn<big_endian>(p + 12, bctr); 5010 } 5011 else 5012 { 5013 delta -= 8; 5014 write_insn<big_endian>(p + 0, mflr_0); 5015 write_insn<big_endian>(p + 4, bcl_20_31); 5016 write_insn<big_endian>(p + 8, mflr_12); 5017 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta)); 5018 write_insn<big_endian>(p + 16, addi_12_12 + l(delta)); 5019 write_insn<big_endian>(p + 20, mtlr_0); 5020 write_insn<big_endian>(p + 24, mtctr_12); 5021 write_insn<big_endian>(p + 28, bctr); 5022 } 5023 } 5024 } 5025 if (this->need_save_res_) 5026 { 5027 p = oview + this->plt_size_ + this->branch_size_; 5028 memcpy (p, this->targ_->savres_section()->contents(), 5029 this->targ_->savres_section()->data_size()); 5030 } 5031} 5032 5033// Write out .glink. 5034 5035template<int size, bool big_endian> 5036void 5037Output_data_glink<size, big_endian>::do_write(Output_file* of) 5038{ 5039 const section_size_type off = this->offset(); 5040 const section_size_type oview_size = 5041 convert_to_section_size_type(this->data_size()); 5042 unsigned char* const oview = of->get_output_view(off, oview_size); 5043 unsigned char* p; 5044 5045 // The base address of the .plt section. 5046 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 5047 Address plt_base = this->targ_->plt_section()->address(); 5048 5049 if (size == 64) 5050 { 5051 if (this->end_branch_table_ != 0) 5052 { 5053 // Write pltresolve stub. 5054 p = oview; 5055 Address after_bcl = this->address() + 16; 5056 Address pltoff = plt_base - after_bcl; 5057 5058 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8; 5059 5060 if (this->targ_->abiversion() < 2) 5061 { 5062 write_insn<big_endian>(p, mflr_12), p += 4; 5063 write_insn<big_endian>(p, bcl_20_31), p += 4; 5064 write_insn<big_endian>(p, mflr_11), p += 4; 5065 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4; 5066 write_insn<big_endian>(p, mtlr_12), p += 4; 5067 write_insn<big_endian>(p, add_11_2_11), p += 4; 5068 write_insn<big_endian>(p, ld_12_11 + 0), p += 4; 5069 write_insn<big_endian>(p, ld_2_11 + 8), p += 4; 5070 write_insn<big_endian>(p, mtctr_12), p += 4; 5071 write_insn<big_endian>(p, ld_11_11 + 16), p += 4; 5072 } 5073 else 5074 { 5075 write_insn<big_endian>(p, mflr_0), p += 4; 5076 write_insn<big_endian>(p, bcl_20_31), p += 4; 5077 write_insn<big_endian>(p, mflr_11), p += 4; 5078 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4; 5079 write_insn<big_endian>(p, mtlr_0), p += 4; 5080 write_insn<big_endian>(p, sub_12_12_11), p += 4; 5081 write_insn<big_endian>(p, add_11_2_11), p += 4; 5082 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4; 5083 write_insn<big_endian>(p, ld_12_11 + 0), p += 4; 5084 write_insn<big_endian>(p, srdi_0_0_2), p += 4; 5085 write_insn<big_endian>(p, mtctr_12), p += 4; 5086 write_insn<big_endian>(p, ld_11_11 + 8), p += 4; 5087 } 5088 write_insn<big_endian>(p, bctr), p += 4; 5089 while (p < oview + this->pltresolve_size) 5090 write_insn<big_endian>(p, nop), p += 4; 5091 5092 // Write lazy link call stubs. 5093 uint32_t indx = 0; 5094 while (p < oview + this->end_branch_table_) 5095 { 5096 if (this->targ_->abiversion() < 2) 5097 { 5098 if (indx < 0x8000) 5099 { 5100 write_insn<big_endian>(p, li_0_0 + indx), p += 4; 5101 } 5102 else 5103 { 5104 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4; 5105 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4; 5106 } 5107 } 5108 uint32_t branch_off = 8 - (p - oview); 5109 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4; 5110 indx++; 5111 } 5112 } 5113 5114 Address plt_base = this->targ_->plt_section()->address(); 5115 Address iplt_base = invalid_address; 5116 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16; 5117 Address global_entry_base = this->address() + global_entry_off; 5118 typename Global_entry_stub_entries::const_iterator ge; 5119 for (ge = this->global_entry_stubs_.begin(); 5120 ge != this->global_entry_stubs_.end(); 5121 ++ge) 5122 { 5123 p = oview + global_entry_off + ge->second; 5124 Address plt_addr = ge->first->plt_offset(); 5125 if (ge->first->type() == elfcpp::STT_GNU_IFUNC 5126 && ge->first->can_use_relative_reloc(false)) 5127 { 5128 if (iplt_base == invalid_address) 5129 iplt_base = this->targ_->iplt_section()->address(); 5130 plt_addr += iplt_base; 5131 } 5132 else 5133 plt_addr += plt_base; 5134 Address my_addr = global_entry_base + ge->second; 5135 Address off = plt_addr - my_addr; 5136 5137 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0) 5138 gold_error(_("%s: linkage table error against `%s'"), 5139 ge->first->object()->name().c_str(), 5140 ge->first->demangled_name().c_str()); 5141 5142 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4; 5143 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4; 5144 write_insn<big_endian>(p, mtctr_12), p += 4; 5145 write_insn<big_endian>(p, bctr); 5146 } 5147 } 5148 else 5149 { 5150 const Output_data_got_powerpc<size, big_endian>* got 5151 = this->targ_->got_section(); 5152 // The address of _GLOBAL_OFFSET_TABLE_. 5153 Address g_o_t = got->address() + got->g_o_t(); 5154 5155 // Write out pltresolve branch table. 5156 p = oview; 5157 unsigned int the_end = oview_size - this->pltresolve_size; 5158 unsigned char* end_p = oview + the_end; 5159 while (p < end_p - 8 * 4) 5160 write_insn<big_endian>(p, b + end_p - p), p += 4; 5161 while (p < end_p) 5162 write_insn<big_endian>(p, nop), p += 4; 5163 5164 // Write out pltresolve call stub. 5165 if (parameters->options().output_is_position_independent()) 5166 { 5167 Address res0_off = 0; 5168 Address after_bcl_off = the_end + 12; 5169 Address bcl_res0 = after_bcl_off - res0_off; 5170 5171 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0)); 5172 write_insn<big_endian>(p + 4, mflr_0); 5173 write_insn<big_endian>(p + 8, bcl_20_31); 5174 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0)); 5175 write_insn<big_endian>(p + 16, mflr_12); 5176 write_insn<big_endian>(p + 20, mtlr_0); 5177 write_insn<big_endian>(p + 24, sub_11_11_12); 5178 5179 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address()); 5180 5181 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl)); 5182 if (ha(got_bcl) == ha(got_bcl + 4)) 5183 { 5184 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl)); 5185 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4)); 5186 } 5187 else 5188 { 5189 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl)); 5190 write_insn<big_endian>(p + 36, lwz_12_12 + 4); 5191 } 5192 write_insn<big_endian>(p + 40, mtctr_0); 5193 write_insn<big_endian>(p + 44, add_0_11_11); 5194 write_insn<big_endian>(p + 48, add_11_0_11); 5195 write_insn<big_endian>(p + 52, bctr); 5196 write_insn<big_endian>(p + 56, nop); 5197 write_insn<big_endian>(p + 60, nop); 5198 } 5199 else 5200 { 5201 Address res0 = this->address(); 5202 5203 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4)); 5204 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0)); 5205 if (ha(g_o_t + 4) == ha(g_o_t + 8)) 5206 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4)); 5207 else 5208 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4)); 5209 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0)); 5210 write_insn<big_endian>(p + 16, mtctr_0); 5211 write_insn<big_endian>(p + 20, add_0_11_11); 5212 if (ha(g_o_t + 4) == ha(g_o_t + 8)) 5213 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8)); 5214 else 5215 write_insn<big_endian>(p + 24, lwz_12_12 + 4); 5216 write_insn<big_endian>(p + 28, add_11_0_11); 5217 write_insn<big_endian>(p + 32, bctr); 5218 write_insn<big_endian>(p + 36, nop); 5219 write_insn<big_endian>(p + 40, nop); 5220 write_insn<big_endian>(p + 44, nop); 5221 write_insn<big_endian>(p + 48, nop); 5222 write_insn<big_endian>(p + 52, nop); 5223 write_insn<big_endian>(p + 56, nop); 5224 write_insn<big_endian>(p + 60, nop); 5225 } 5226 p += 64; 5227 } 5228 5229 of->write_output_view(off, oview_size, oview); 5230} 5231 5232 5233// A class to handle linker generated save/restore functions. 5234 5235template<int size, bool big_endian> 5236class Output_data_save_res : public Output_section_data_build 5237{ 5238 public: 5239 Output_data_save_res(Symbol_table* symtab); 5240 5241 const unsigned char* 5242 contents() const 5243 { 5244 return contents_; 5245 } 5246 5247 protected: 5248 // Write to a map file. 5249 void 5250 do_print_to_mapfile(Mapfile* mapfile) const 5251 { mapfile->print_output_data(this, _("** save/restore")); } 5252 5253 void 5254 do_write(Output_file*); 5255 5256 private: 5257 // The maximum size of save/restore contents. 5258 static const unsigned int savres_max = 218*4; 5259 5260 void 5261 savres_define(Symbol_table* symtab, 5262 const char *name, 5263 unsigned int lo, unsigned int hi, 5264 unsigned char* write_ent(unsigned char*, int), 5265 unsigned char* write_tail(unsigned char*, int)); 5266 5267 unsigned char *contents_; 5268}; 5269 5270template<bool big_endian> 5271static unsigned char* 5272savegpr0(unsigned char* p, int r) 5273{ 5274 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8; 5275 write_insn<big_endian>(p, insn); 5276 return p + 4; 5277} 5278 5279template<bool big_endian> 5280static unsigned char* 5281savegpr0_tail(unsigned char* p, int r) 5282{ 5283 p = savegpr0<big_endian>(p, r); 5284 uint32_t insn = std_0_1 + 16; 5285 write_insn<big_endian>(p, insn); 5286 p = p + 4; 5287 write_insn<big_endian>(p, blr); 5288 return p + 4; 5289} 5290 5291template<bool big_endian> 5292static unsigned char* 5293restgpr0(unsigned char* p, int r) 5294{ 5295 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8; 5296 write_insn<big_endian>(p, insn); 5297 return p + 4; 5298} 5299 5300template<bool big_endian> 5301static unsigned char* 5302restgpr0_tail(unsigned char* p, int r) 5303{ 5304 uint32_t insn = ld_0_1 + 16; 5305 write_insn<big_endian>(p, insn); 5306 p = p + 4; 5307 p = restgpr0<big_endian>(p, r); 5308 write_insn<big_endian>(p, mtlr_0); 5309 p = p + 4; 5310 if (r == 29) 5311 { 5312 p = restgpr0<big_endian>(p, 30); 5313 p = restgpr0<big_endian>(p, 31); 5314 } 5315 write_insn<big_endian>(p, blr); 5316 return p + 4; 5317} 5318 5319template<bool big_endian> 5320static unsigned char* 5321savegpr1(unsigned char* p, int r) 5322{ 5323 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8; 5324 write_insn<big_endian>(p, insn); 5325 return p + 4; 5326} 5327 5328template<bool big_endian> 5329static unsigned char* 5330savegpr1_tail(unsigned char* p, int r) 5331{ 5332 p = savegpr1<big_endian>(p, r); 5333 write_insn<big_endian>(p, blr); 5334 return p + 4; 5335} 5336 5337template<bool big_endian> 5338static unsigned char* 5339restgpr1(unsigned char* p, int r) 5340{ 5341 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8; 5342 write_insn<big_endian>(p, insn); 5343 return p + 4; 5344} 5345 5346template<bool big_endian> 5347static unsigned char* 5348restgpr1_tail(unsigned char* p, int r) 5349{ 5350 p = restgpr1<big_endian>(p, r); 5351 write_insn<big_endian>(p, blr); 5352 return p + 4; 5353} 5354 5355template<bool big_endian> 5356static unsigned char* 5357savefpr(unsigned char* p, int r) 5358{ 5359 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8; 5360 write_insn<big_endian>(p, insn); 5361 return p + 4; 5362} 5363 5364template<bool big_endian> 5365static unsigned char* 5366savefpr0_tail(unsigned char* p, int r) 5367{ 5368 p = savefpr<big_endian>(p, r); 5369 write_insn<big_endian>(p, std_0_1 + 16); 5370 p = p + 4; 5371 write_insn<big_endian>(p, blr); 5372 return p + 4; 5373} 5374 5375template<bool big_endian> 5376static unsigned char* 5377restfpr(unsigned char* p, int r) 5378{ 5379 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8; 5380 write_insn<big_endian>(p, insn); 5381 return p + 4; 5382} 5383 5384template<bool big_endian> 5385static unsigned char* 5386restfpr0_tail(unsigned char* p, int r) 5387{ 5388 write_insn<big_endian>(p, ld_0_1 + 16); 5389 p = p + 4; 5390 p = restfpr<big_endian>(p, r); 5391 write_insn<big_endian>(p, mtlr_0); 5392 p = p + 4; 5393 if (r == 29) 5394 { 5395 p = restfpr<big_endian>(p, 30); 5396 p = restfpr<big_endian>(p, 31); 5397 } 5398 write_insn<big_endian>(p, blr); 5399 return p + 4; 5400} 5401 5402template<bool big_endian> 5403static unsigned char* 5404savefpr1_tail(unsigned char* p, int r) 5405{ 5406 p = savefpr<big_endian>(p, r); 5407 write_insn<big_endian>(p, blr); 5408 return p + 4; 5409} 5410 5411template<bool big_endian> 5412static unsigned char* 5413restfpr1_tail(unsigned char* p, int r) 5414{ 5415 p = restfpr<big_endian>(p, r); 5416 write_insn<big_endian>(p, blr); 5417 return p + 4; 5418} 5419 5420template<bool big_endian> 5421static unsigned char* 5422savevr(unsigned char* p, int r) 5423{ 5424 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16; 5425 write_insn<big_endian>(p, insn); 5426 p = p + 4; 5427 insn = stvx_0_12_0 + (r << 21); 5428 write_insn<big_endian>(p, insn); 5429 return p + 4; 5430} 5431 5432template<bool big_endian> 5433static unsigned char* 5434savevr_tail(unsigned char* p, int r) 5435{ 5436 p = savevr<big_endian>(p, r); 5437 write_insn<big_endian>(p, blr); 5438 return p + 4; 5439} 5440 5441template<bool big_endian> 5442static unsigned char* 5443restvr(unsigned char* p, int r) 5444{ 5445 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16; 5446 write_insn<big_endian>(p, insn); 5447 p = p + 4; 5448 insn = lvx_0_12_0 + (r << 21); 5449 write_insn<big_endian>(p, insn); 5450 return p + 4; 5451} 5452 5453template<bool big_endian> 5454static unsigned char* 5455restvr_tail(unsigned char* p, int r) 5456{ 5457 p = restvr<big_endian>(p, r); 5458 write_insn<big_endian>(p, blr); 5459 return p + 4; 5460} 5461 5462 5463template<int size, bool big_endian> 5464Output_data_save_res<size, big_endian>::Output_data_save_res( 5465 Symbol_table* symtab) 5466 : Output_section_data_build(4), 5467 contents_(NULL) 5468{ 5469 this->savres_define(symtab, 5470 "_savegpr0_", 14, 31, 5471 savegpr0<big_endian>, savegpr0_tail<big_endian>); 5472 this->savres_define(symtab, 5473 "_restgpr0_", 14, 29, 5474 restgpr0<big_endian>, restgpr0_tail<big_endian>); 5475 this->savres_define(symtab, 5476 "_restgpr0_", 30, 31, 5477 restgpr0<big_endian>, restgpr0_tail<big_endian>); 5478 this->savres_define(symtab, 5479 "_savegpr1_", 14, 31, 5480 savegpr1<big_endian>, savegpr1_tail<big_endian>); 5481 this->savres_define(symtab, 5482 "_restgpr1_", 14, 31, 5483 restgpr1<big_endian>, restgpr1_tail<big_endian>); 5484 this->savres_define(symtab, 5485 "_savefpr_", 14, 31, 5486 savefpr<big_endian>, savefpr0_tail<big_endian>); 5487 this->savres_define(symtab, 5488 "_restfpr_", 14, 29, 5489 restfpr<big_endian>, restfpr0_tail<big_endian>); 5490 this->savres_define(symtab, 5491 "_restfpr_", 30, 31, 5492 restfpr<big_endian>, restfpr0_tail<big_endian>); 5493 this->savres_define(symtab, 5494 "._savef", 14, 31, 5495 savefpr<big_endian>, savefpr1_tail<big_endian>); 5496 this->savres_define(symtab, 5497 "._restf", 14, 31, 5498 restfpr<big_endian>, restfpr1_tail<big_endian>); 5499 this->savres_define(symtab, 5500 "_savevr_", 20, 31, 5501 savevr<big_endian>, savevr_tail<big_endian>); 5502 this->savres_define(symtab, 5503 "_restvr_", 20, 31, 5504 restvr<big_endian>, restvr_tail<big_endian>); 5505} 5506 5507template<int size, bool big_endian> 5508void 5509Output_data_save_res<size, big_endian>::savres_define( 5510 Symbol_table* symtab, 5511 const char *name, 5512 unsigned int lo, unsigned int hi, 5513 unsigned char* write_ent(unsigned char*, int), 5514 unsigned char* write_tail(unsigned char*, int)) 5515{ 5516 size_t len = strlen(name); 5517 bool writing = false; 5518 char sym[16]; 5519 5520 memcpy(sym, name, len); 5521 sym[len + 2] = 0; 5522 5523 for (unsigned int i = lo; i <= hi; i++) 5524 { 5525 sym[len + 0] = i / 10 + '0'; 5526 sym[len + 1] = i % 10 + '0'; 5527 Symbol* gsym = symtab->lookup(sym); 5528 bool refd = gsym != NULL && gsym->is_undefined(); 5529 writing = writing || refd; 5530 if (writing) 5531 { 5532 if (this->contents_ == NULL) 5533 this->contents_ = new unsigned char[this->savres_max]; 5534 5535 section_size_type value = this->current_data_size(); 5536 unsigned char* p = this->contents_ + value; 5537 if (i != hi) 5538 p = write_ent(p, i); 5539 else 5540 p = write_tail(p, i); 5541 section_size_type cur_size = p - this->contents_; 5542 this->set_current_data_size(cur_size); 5543 if (refd) 5544 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED, 5545 this, value, cur_size - value, 5546 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL, 5547 elfcpp::STV_HIDDEN, 0, false, false); 5548 } 5549 } 5550} 5551 5552// Write out save/restore. 5553 5554template<int size, bool big_endian> 5555void 5556Output_data_save_res<size, big_endian>::do_write(Output_file* of) 5557{ 5558 const section_size_type off = this->offset(); 5559 const section_size_type oview_size = 5560 convert_to_section_size_type(this->data_size()); 5561 unsigned char* const oview = of->get_output_view(off, oview_size); 5562 memcpy(oview, this->contents_, oview_size); 5563 of->write_output_view(off, oview_size, oview); 5564} 5565 5566 5567// Create the glink section. 5568 5569template<int size, bool big_endian> 5570void 5571Target_powerpc<size, big_endian>::make_glink_section(Layout* layout) 5572{ 5573 if (this->glink_ == NULL) 5574 { 5575 this->glink_ = new Output_data_glink<size, big_endian>(this); 5576 this->glink_->add_eh_frame(layout); 5577 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS, 5578 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR, 5579 this->glink_, ORDER_TEXT, false); 5580 } 5581} 5582 5583// Create a PLT entry for a global symbol. 5584 5585template<int size, bool big_endian> 5586void 5587Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab, 5588 Layout* layout, 5589 Symbol* gsym) 5590{ 5591 if (gsym->type() == elfcpp::STT_GNU_IFUNC 5592 && gsym->can_use_relative_reloc(false)) 5593 { 5594 if (this->iplt_ == NULL) 5595 this->make_iplt_section(symtab, layout); 5596 this->iplt_->add_ifunc_entry(gsym); 5597 } 5598 else 5599 { 5600 if (this->plt_ == NULL) 5601 this->make_plt_section(symtab, layout); 5602 this->plt_->add_entry(gsym); 5603 } 5604} 5605 5606// Make a PLT entry for a local STT_GNU_IFUNC symbol. 5607 5608template<int size, bool big_endian> 5609void 5610Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry( 5611 Symbol_table* symtab, 5612 Layout* layout, 5613 Sized_relobj_file<size, big_endian>* relobj, 5614 unsigned int r_sym) 5615{ 5616 if (this->iplt_ == NULL) 5617 this->make_iplt_section(symtab, layout); 5618 this->iplt_->add_local_ifunc_entry(relobj, r_sym); 5619} 5620 5621// Return the number of entries in the PLT. 5622 5623template<int size, bool big_endian> 5624unsigned int 5625Target_powerpc<size, big_endian>::plt_entry_count() const 5626{ 5627 if (this->plt_ == NULL) 5628 return 0; 5629 return this->plt_->entry_count(); 5630} 5631 5632// Create a GOT entry for local dynamic __tls_get_addr calls. 5633 5634template<int size, bool big_endian> 5635unsigned int 5636Target_powerpc<size, big_endian>::tlsld_got_offset( 5637 Symbol_table* symtab, 5638 Layout* layout, 5639 Sized_relobj_file<size, big_endian>* object) 5640{ 5641 if (this->tlsld_got_offset_ == -1U) 5642 { 5643 gold_assert(symtab != NULL && layout != NULL && object != NULL); 5644 Reloc_section* rela_dyn = this->rela_dyn_section(layout); 5645 Output_data_got_powerpc<size, big_endian>* got 5646 = this->got_section(symtab, layout); 5647 unsigned int got_offset = got->add_constant_pair(0, 0); 5648 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got, 5649 got_offset, 0); 5650 this->tlsld_got_offset_ = got_offset; 5651 } 5652 return this->tlsld_got_offset_; 5653} 5654 5655// Get the Reference_flags for a particular relocation. 5656 5657template<int size, bool big_endian> 5658int 5659Target_powerpc<size, big_endian>::Scan::get_reference_flags( 5660 unsigned int r_type, 5661 const Target_powerpc* target) 5662{ 5663 int ref = 0; 5664 5665 switch (r_type) 5666 { 5667 case elfcpp::R_POWERPC_NONE: 5668 case elfcpp::R_POWERPC_GNU_VTINHERIT: 5669 case elfcpp::R_POWERPC_GNU_VTENTRY: 5670 case elfcpp::R_PPC64_TOC: 5671 // No symbol reference. 5672 break; 5673 5674 case elfcpp::R_PPC64_ADDR64: 5675 case elfcpp::R_PPC64_UADDR64: 5676 case elfcpp::R_POWERPC_ADDR32: 5677 case elfcpp::R_POWERPC_UADDR32: 5678 case elfcpp::R_POWERPC_ADDR16: 5679 case elfcpp::R_POWERPC_UADDR16: 5680 case elfcpp::R_POWERPC_ADDR16_LO: 5681 case elfcpp::R_POWERPC_ADDR16_HI: 5682 case elfcpp::R_POWERPC_ADDR16_HA: 5683 ref = Symbol::ABSOLUTE_REF; 5684 break; 5685 5686 case elfcpp::R_POWERPC_ADDR24: 5687 case elfcpp::R_POWERPC_ADDR14: 5688 case elfcpp::R_POWERPC_ADDR14_BRTAKEN: 5689 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN: 5690 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF; 5691 break; 5692 5693 case elfcpp::R_PPC64_REL64: 5694 case elfcpp::R_POWERPC_REL32: 5695 case elfcpp::R_PPC_LOCAL24PC: 5696 case elfcpp::R_POWERPC_REL16: 5697 case elfcpp::R_POWERPC_REL16_LO: 5698 case elfcpp::R_POWERPC_REL16_HI: 5699 case elfcpp::R_POWERPC_REL16_HA: 5700 ref = Symbol::RELATIVE_REF; 5701 break; 5702 5703 case elfcpp::R_POWERPC_REL24: 5704 case elfcpp::R_PPC_PLTREL24: 5705 case elfcpp::R_POWERPC_REL14: 5706 case elfcpp::R_POWERPC_REL14_BRTAKEN: 5707 case elfcpp::R_POWERPC_REL14_BRNTAKEN: 5708 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF; 5709 break; 5710 5711 case elfcpp::R_POWERPC_GOT16: 5712 case elfcpp::R_POWERPC_GOT16_LO: 5713 case elfcpp::R_POWERPC_GOT16_HI: 5714 case elfcpp::R_POWERPC_GOT16_HA: 5715 case elfcpp::R_PPC64_GOT16_DS: 5716 case elfcpp::R_PPC64_GOT16_LO_DS: 5717 case elfcpp::R_PPC64_TOC16: 5718 case elfcpp::R_PPC64_TOC16_LO: 5719 case elfcpp::R_PPC64_TOC16_HI: 5720 case elfcpp::R_PPC64_TOC16_HA: 5721 case elfcpp::R_PPC64_TOC16_DS: 5722 case elfcpp::R_PPC64_TOC16_LO_DS: 5723 ref = Symbol::RELATIVE_REF; 5724 break; 5725 5726 case elfcpp::R_POWERPC_GOT_TPREL16: 5727 case elfcpp::R_POWERPC_TLS: 5728 ref = Symbol::TLS_REF; 5729 break; 5730 5731 case elfcpp::R_POWERPC_COPY: 5732 case elfcpp::R_POWERPC_GLOB_DAT: 5733 case elfcpp::R_POWERPC_JMP_SLOT: 5734 case elfcpp::R_POWERPC_RELATIVE: 5735 case elfcpp::R_POWERPC_DTPMOD: 5736 default: 5737 // Not expected. We will give an error later. 5738 break; 5739 } 5740 5741 if (size == 64 && target->abiversion() < 2) 5742 ref |= Symbol::FUNC_DESC_ABI; 5743 return ref; 5744} 5745 5746// Report an unsupported relocation against a local symbol. 5747 5748template<int size, bool big_endian> 5749void 5750Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local( 5751 Sized_relobj_file<size, big_endian>* object, 5752 unsigned int r_type) 5753{ 5754 gold_error(_("%s: unsupported reloc %u against local symbol"), 5755 object->name().c_str(), r_type); 5756} 5757 5758// We are about to emit a dynamic relocation of type R_TYPE. If the 5759// dynamic linker does not support it, issue an error. 5760 5761template<int size, bool big_endian> 5762void 5763Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object, 5764 unsigned int r_type) 5765{ 5766 gold_assert(r_type != elfcpp::R_POWERPC_NONE); 5767 5768 // These are the relocation types supported by glibc for both 32-bit 5769 // and 64-bit powerpc. 5770 switch (r_type) 5771 { 5772 case elfcpp::R_POWERPC_NONE: 5773 case elfcpp::R_POWERPC_RELATIVE: 5774 case elfcpp::R_POWERPC_GLOB_DAT: 5775 case elfcpp::R_POWERPC_DTPMOD: 5776 case elfcpp::R_POWERPC_DTPREL: 5777 case elfcpp::R_POWERPC_TPREL: 5778 case elfcpp::R_POWERPC_JMP_SLOT: 5779 case elfcpp::R_POWERPC_COPY: 5780 case elfcpp::R_POWERPC_IRELATIVE: 5781 case elfcpp::R_POWERPC_ADDR32: 5782 case elfcpp::R_POWERPC_UADDR32: 5783 case elfcpp::R_POWERPC_ADDR24: 5784 case elfcpp::R_POWERPC_ADDR16: 5785 case elfcpp::R_POWERPC_UADDR16: 5786 case elfcpp::R_POWERPC_ADDR16_LO: 5787 case elfcpp::R_POWERPC_ADDR16_HI: 5788 case elfcpp::R_POWERPC_ADDR16_HA: 5789 case elfcpp::R_POWERPC_ADDR14: 5790 case elfcpp::R_POWERPC_ADDR14_BRTAKEN: 5791 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN: 5792 case elfcpp::R_POWERPC_REL32: 5793 case elfcpp::R_POWERPC_REL24: 5794 case elfcpp::R_POWERPC_TPREL16: 5795 case elfcpp::R_POWERPC_TPREL16_LO: 5796 case elfcpp::R_POWERPC_TPREL16_HI: 5797 case elfcpp::R_POWERPC_TPREL16_HA: 5798 return; 5799 5800 default: 5801 break; 5802 } 5803 5804 if (size == 64) 5805 { 5806 switch (r_type) 5807 { 5808 // These are the relocation types supported only on 64-bit. 5809 case elfcpp::R_PPC64_ADDR64: 5810 case elfcpp::R_PPC64_UADDR64: 5811 case elfcpp::R_PPC64_JMP_IREL: 5812 case elfcpp::R_PPC64_ADDR16_DS: 5813 case elfcpp::R_PPC64_ADDR16_LO_DS: 5814 case elfcpp::R_PPC64_ADDR16_HIGH: 5815 case elfcpp::R_PPC64_ADDR16_HIGHA: 5816 case elfcpp::R_PPC64_ADDR16_HIGHER: 5817 case elfcpp::R_PPC64_ADDR16_HIGHEST: 5818 case elfcpp::R_PPC64_ADDR16_HIGHERA: 5819 case elfcpp::R_PPC64_ADDR16_HIGHESTA: 5820 case elfcpp::R_PPC64_REL64: 5821 case elfcpp::R_POWERPC_ADDR30: 5822 case elfcpp::R_PPC64_TPREL16_DS: 5823 case elfcpp::R_PPC64_TPREL16_LO_DS: 5824 case elfcpp::R_PPC64_TPREL16_HIGH: 5825 case elfcpp::R_PPC64_TPREL16_HIGHA: 5826 case elfcpp::R_PPC64_TPREL16_HIGHER: 5827 case elfcpp::R_PPC64_TPREL16_HIGHEST: 5828 case elfcpp::R_PPC64_TPREL16_HIGHERA: 5829 case elfcpp::R_PPC64_TPREL16_HIGHESTA: 5830 return; 5831 5832 default: 5833 break; 5834 } 5835 } 5836 else 5837 { 5838 switch (r_type) 5839 { 5840 // These are the relocation types supported only on 32-bit. 5841 // ??? glibc ld.so doesn't need to support these. 5842 case elfcpp::R_POWERPC_DTPREL16: 5843 case elfcpp::R_POWERPC_DTPREL16_LO: 5844 case elfcpp::R_POWERPC_DTPREL16_HI: 5845 case elfcpp::R_POWERPC_DTPREL16_HA: 5846 return; 5847 5848 default: 5849 break; 5850 } 5851 } 5852 5853 // This prevents us from issuing more than one error per reloc 5854 // section. But we can still wind up issuing more than one 5855 // error per object file. 5856 if (this->issued_non_pic_error_) 5857 return; 5858 gold_assert(parameters->options().output_is_position_independent()); 5859 object->error(_("requires unsupported dynamic reloc; " 5860 "recompile with -fPIC")); 5861 this->issued_non_pic_error_ = true; 5862 return; 5863} 5864 5865// Return whether we need to make a PLT entry for a relocation of the 5866// given type against a STT_GNU_IFUNC symbol. 5867 5868template<int size, bool big_endian> 5869bool 5870Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc( 5871 Target_powerpc<size, big_endian>* target, 5872 Sized_relobj_file<size, big_endian>* object, 5873 unsigned int r_type, 5874 bool report_err) 5875{ 5876 // In non-pic code any reference will resolve to the plt call stub 5877 // for the ifunc symbol. 5878 if ((size == 32 || target->abiversion() >= 2) 5879 && !parameters->options().output_is_position_independent()) 5880 return true; 5881 5882 switch (r_type) 5883 { 5884 // Word size refs from data sections are OK, but don't need a PLT entry. 5885 case elfcpp::R_POWERPC_ADDR32: 5886 case elfcpp::R_POWERPC_UADDR32: 5887 if (size == 32) 5888 return false; 5889 break; 5890 5891 case elfcpp::R_PPC64_ADDR64: 5892 case elfcpp::R_PPC64_UADDR64: 5893 if (size == 64) 5894 return false; 5895 break; 5896 5897 // GOT refs are good, but also don't need a PLT entry. 5898 case elfcpp::R_POWERPC_GOT16: 5899 case elfcpp::R_POWERPC_GOT16_LO: 5900 case elfcpp::R_POWERPC_GOT16_HI: 5901 case elfcpp::R_POWERPC_GOT16_HA: 5902 case elfcpp::R_PPC64_GOT16_DS: 5903 case elfcpp::R_PPC64_GOT16_LO_DS: 5904 return false; 5905 5906 // Function calls are good, and these do need a PLT entry. 5907 case elfcpp::R_POWERPC_ADDR24: 5908 case elfcpp::R_POWERPC_ADDR14: 5909 case elfcpp::R_POWERPC_ADDR14_BRTAKEN: 5910 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN: 5911 case elfcpp::R_POWERPC_REL24: 5912 case elfcpp::R_PPC_PLTREL24: 5913 case elfcpp::R_POWERPC_REL14: 5914 case elfcpp::R_POWERPC_REL14_BRTAKEN: 5915 case elfcpp::R_POWERPC_REL14_BRNTAKEN: 5916 return true; 5917 5918 default: 5919 break; 5920 } 5921 5922 // Anything else is a problem. 5923 // If we are building a static executable, the libc startup function 5924 // responsible for applying indirect function relocations is going 5925 // to complain about the reloc type. 5926 // If we are building a dynamic executable, we will have a text 5927 // relocation. The dynamic loader will set the text segment 5928 // writable and non-executable to apply text relocations. So we'll 5929 // segfault when trying to run the indirection function to resolve 5930 // the reloc. 5931 if (report_err) 5932 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"), 5933 object->name().c_str(), r_type); 5934 return false; 5935} 5936 5937// Return TRUE iff INSN is one we expect on a _LO variety toc/got 5938// reloc. 5939 5940static bool 5941ok_lo_toc_insn(uint32_t insn, unsigned int r_type) 5942{ 5943 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */ 5944 || (insn & (0x3f << 26)) == 14u << 26 /* addi */ 5945 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */ 5946 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */ 5947 || (insn & (0x3f << 26)) == 36u << 26 /* stw */ 5948 || (insn & (0x3f << 26)) == 38u << 26 /* stb */ 5949 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */ 5950 || (insn & (0x3f << 26)) == 42u << 26 /* lha */ 5951 || (insn & (0x3f << 26)) == 44u << 26 /* sth */ 5952 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */ 5953 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */ 5954 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */ 5955 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */ 5956 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */ 5957 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */ 5958 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */ 5959 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */ 5960 /* Exclude lfqu by testing reloc. If relocs are ever 5961 defined for the reduced D field in psq_lu then those 5962 will need testing too. */ 5963 && r_type != elfcpp::R_PPC64_TOC16_LO 5964 && r_type != elfcpp::R_POWERPC_GOT16_LO) 5965 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */ 5966 && (insn & 1) == 0) 5967 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */ 5968 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */ 5969 /* Exclude stfqu. psq_stu as above for psq_lu. */ 5970 && r_type != elfcpp::R_PPC64_TOC16_LO 5971 && r_type != elfcpp::R_POWERPC_GOT16_LO) 5972 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */ 5973 && (insn & 1) == 0)); 5974} 5975 5976// Scan a relocation for a local symbol. 5977 5978template<int size, bool big_endian> 5979inline void 5980Target_powerpc<size, big_endian>::Scan::local( 5981 Symbol_table* symtab, 5982 Layout* layout, 5983 Target_powerpc<size, big_endian>* target, 5984 Sized_relobj_file<size, big_endian>* object, 5985 unsigned int data_shndx, 5986 Output_section* output_section, 5987 const elfcpp::Rela<size, big_endian>& reloc, 5988 unsigned int r_type, 5989 const elfcpp::Sym<size, big_endian>& lsym, 5990 bool is_discarded) 5991{ 5992 this->maybe_skip_tls_get_addr_call(r_type, NULL); 5993 5994 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD) 5995 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD)) 5996 { 5997 this->expect_tls_get_addr_call(); 5998 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true); 5999 if (tls_type != tls::TLSOPT_NONE) 6000 this->skip_next_tls_get_addr_call(); 6001 } 6002 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD) 6003 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD)) 6004 { 6005 this->expect_tls_get_addr_call(); 6006 const tls::Tls_optimization tls_type = target->optimize_tls_ld(); 6007 if (tls_type != tls::TLSOPT_NONE) 6008 this->skip_next_tls_get_addr_call(); 6009 } 6010 6011 Powerpc_relobj<size, big_endian>* ppc_object 6012 = static_cast<Powerpc_relobj<size, big_endian>*>(object); 6013 6014 if (is_discarded) 6015 { 6016 if (size == 64 6017 && data_shndx == ppc_object->opd_shndx() 6018 && r_type == elfcpp::R_PPC64_ADDR64) 6019 ppc_object->set_opd_discard(reloc.get_r_offset()); 6020 return; 6021 } 6022 6023 // A local STT_GNU_IFUNC symbol may require a PLT entry. 6024 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC; 6025 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true)) 6026 { 6027 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 6028 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(), 6029 r_type, r_sym, reloc.get_r_addend()); 6030 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym); 6031 } 6032 6033 switch (r_type) 6034 { 6035 case elfcpp::R_POWERPC_NONE: 6036 case elfcpp::R_POWERPC_GNU_VTINHERIT: 6037 case elfcpp::R_POWERPC_GNU_VTENTRY: 6038 case elfcpp::R_PPC64_TOCSAVE: 6039 case elfcpp::R_POWERPC_TLS: 6040 case elfcpp::R_PPC64_ENTRY: 6041 break; 6042 6043 case elfcpp::R_PPC64_TOC: 6044 { 6045 Output_data_got_powerpc<size, big_endian>* got 6046 = target->got_section(symtab, layout); 6047 if (parameters->options().output_is_position_independent()) 6048 { 6049 Address off = reloc.get_r_offset(); 6050 if (size == 64 6051 && target->abiversion() < 2 6052 && data_shndx == ppc_object->opd_shndx() 6053 && ppc_object->get_opd_discard(off - 8)) 6054 break; 6055 6056 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 6057 Powerpc_relobj<size, big_endian>* symobj = ppc_object; 6058 rela_dyn->add_output_section_relative(got->output_section(), 6059 elfcpp::R_POWERPC_RELATIVE, 6060 output_section, 6061 object, data_shndx, off, 6062 symobj->toc_base_offset()); 6063 } 6064 } 6065 break; 6066 6067 case elfcpp::R_PPC64_ADDR64: 6068 case elfcpp::R_PPC64_UADDR64: 6069 case elfcpp::R_POWERPC_ADDR32: 6070 case elfcpp::R_POWERPC_UADDR32: 6071 case elfcpp::R_POWERPC_ADDR24: 6072 case elfcpp::R_POWERPC_ADDR16: 6073 case elfcpp::R_POWERPC_ADDR16_LO: 6074 case elfcpp::R_POWERPC_ADDR16_HI: 6075 case elfcpp::R_POWERPC_ADDR16_HA: 6076 case elfcpp::R_POWERPC_UADDR16: 6077 case elfcpp::R_PPC64_ADDR16_HIGH: 6078 case elfcpp::R_PPC64_ADDR16_HIGHA: 6079 case elfcpp::R_PPC64_ADDR16_HIGHER: 6080 case elfcpp::R_PPC64_ADDR16_HIGHERA: 6081 case elfcpp::R_PPC64_ADDR16_HIGHEST: 6082 case elfcpp::R_PPC64_ADDR16_HIGHESTA: 6083 case elfcpp::R_PPC64_ADDR16_DS: 6084 case elfcpp::R_PPC64_ADDR16_LO_DS: 6085 case elfcpp::R_POWERPC_ADDR14: 6086 case elfcpp::R_POWERPC_ADDR14_BRTAKEN: 6087 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN: 6088 // If building a shared library (or a position-independent 6089 // executable), we need to create a dynamic relocation for 6090 // this location. 6091 if (parameters->options().output_is_position_independent() 6092 || (size == 64 && is_ifunc && target->abiversion() < 2)) 6093 { 6094 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout, 6095 is_ifunc); 6096 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 6097 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32) 6098 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64)) 6099 { 6100 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE 6101 : elfcpp::R_POWERPC_RELATIVE); 6102 rela_dyn->add_local_relative(object, r_sym, dynrel, 6103 output_section, data_shndx, 6104 reloc.get_r_offset(), 6105 reloc.get_r_addend(), false); 6106 } 6107 else if (lsym.get_st_type() != elfcpp::STT_SECTION) 6108 { 6109 check_non_pic(object, r_type); 6110 rela_dyn->add_local(object, r_sym, r_type, output_section, 6111 data_shndx, reloc.get_r_offset(), 6112 reloc.get_r_addend()); 6113 } 6114 else 6115 { 6116 gold_assert(lsym.get_st_value() == 0); 6117 unsigned int shndx = lsym.get_st_shndx(); 6118 bool is_ordinary; 6119 shndx = object->adjust_sym_shndx(r_sym, shndx, 6120 &is_ordinary); 6121 if (!is_ordinary) 6122 object->error(_("section symbol %u has bad shndx %u"), 6123 r_sym, shndx); 6124 else 6125 rela_dyn->add_local_section(object, shndx, r_type, 6126 output_section, data_shndx, 6127 reloc.get_r_offset()); 6128 } 6129 } 6130 break; 6131 6132 case elfcpp::R_POWERPC_REL24: 6133 case elfcpp::R_PPC_PLTREL24: 6134 case elfcpp::R_PPC_LOCAL24PC: 6135 case elfcpp::R_POWERPC_REL14: 6136 case elfcpp::R_POWERPC_REL14_BRTAKEN: 6137 case elfcpp::R_POWERPC_REL14_BRNTAKEN: 6138 if (!is_ifunc) 6139 { 6140 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 6141 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(), 6142 r_type, r_sym, reloc.get_r_addend()); 6143 } 6144 break; 6145 6146 case elfcpp::R_PPC64_REL64: 6147 case elfcpp::R_POWERPC_REL32: 6148 case elfcpp::R_POWERPC_REL16: 6149 case elfcpp::R_POWERPC_REL16_LO: 6150 case elfcpp::R_POWERPC_REL16_HI: 6151 case elfcpp::R_POWERPC_REL16_HA: 6152 case elfcpp::R_POWERPC_REL16DX_HA: 6153 case elfcpp::R_POWERPC_SECTOFF: 6154 case elfcpp::R_POWERPC_SECTOFF_LO: 6155 case elfcpp::R_POWERPC_SECTOFF_HI: 6156 case elfcpp::R_POWERPC_SECTOFF_HA: 6157 case elfcpp::R_PPC64_SECTOFF_DS: 6158 case elfcpp::R_PPC64_SECTOFF_LO_DS: 6159 case elfcpp::R_POWERPC_TPREL16: 6160 case elfcpp::R_POWERPC_TPREL16_LO: 6161 case elfcpp::R_POWERPC_TPREL16_HI: 6162 case elfcpp::R_POWERPC_TPREL16_HA: 6163 case elfcpp::R_PPC64_TPREL16_DS: 6164 case elfcpp::R_PPC64_TPREL16_LO_DS: 6165 case elfcpp::R_PPC64_TPREL16_HIGH: 6166 case elfcpp::R_PPC64_TPREL16_HIGHA: 6167 case elfcpp::R_PPC64_TPREL16_HIGHER: 6168 case elfcpp::R_PPC64_TPREL16_HIGHERA: 6169 case elfcpp::R_PPC64_TPREL16_HIGHEST: 6170 case elfcpp::R_PPC64_TPREL16_HIGHESTA: 6171 case elfcpp::R_POWERPC_DTPREL16: 6172 case elfcpp::R_POWERPC_DTPREL16_LO: 6173 case elfcpp::R_POWERPC_DTPREL16_HI: 6174 case elfcpp::R_POWERPC_DTPREL16_HA: 6175 case elfcpp::R_PPC64_DTPREL16_DS: 6176 case elfcpp::R_PPC64_DTPREL16_LO_DS: 6177 case elfcpp::R_PPC64_DTPREL16_HIGH: 6178 case elfcpp::R_PPC64_DTPREL16_HIGHA: 6179 case elfcpp::R_PPC64_DTPREL16_HIGHER: 6180 case elfcpp::R_PPC64_DTPREL16_HIGHERA: 6181 case elfcpp::R_PPC64_DTPREL16_HIGHEST: 6182 case elfcpp::R_PPC64_DTPREL16_HIGHESTA: 6183 case elfcpp::R_PPC64_TLSGD: 6184 case elfcpp::R_PPC64_TLSLD: 6185 case elfcpp::R_PPC64_ADDR64_LOCAL: 6186 break; 6187 6188 case elfcpp::R_POWERPC_GOT16: 6189 case elfcpp::R_POWERPC_GOT16_LO: 6190 case elfcpp::R_POWERPC_GOT16_HI: 6191 case elfcpp::R_POWERPC_GOT16_HA: 6192 case elfcpp::R_PPC64_GOT16_DS: 6193 case elfcpp::R_PPC64_GOT16_LO_DS: 6194 { 6195 // The symbol requires a GOT entry. 6196 Output_data_got_powerpc<size, big_endian>* got 6197 = target->got_section(symtab, layout); 6198 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 6199 6200 if (!parameters->options().output_is_position_independent()) 6201 { 6202 if (is_ifunc 6203 && (size == 32 || target->abiversion() >= 2)) 6204 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD); 6205 else 6206 got->add_local(object, r_sym, GOT_TYPE_STANDARD); 6207 } 6208 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD)) 6209 { 6210 // If we are generating a shared object or a pie, this 6211 // symbol's GOT entry will be set by a dynamic relocation. 6212 unsigned int off; 6213 off = got->add_constant(0); 6214 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off); 6215 6216 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout, 6217 is_ifunc); 6218 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE 6219 : elfcpp::R_POWERPC_RELATIVE); 6220 rela_dyn->add_local_relative(object, r_sym, dynrel, 6221 got, off, 0, false); 6222 } 6223 } 6224 break; 6225 6226 case elfcpp::R_PPC64_TOC16: 6227 case elfcpp::R_PPC64_TOC16_LO: 6228 case elfcpp::R_PPC64_TOC16_HI: 6229 case elfcpp::R_PPC64_TOC16_HA: 6230 case elfcpp::R_PPC64_TOC16_DS: 6231 case elfcpp::R_PPC64_TOC16_LO_DS: 6232 // We need a GOT section. 6233 target->got_section(symtab, layout); 6234 break; 6235 6236 case elfcpp::R_POWERPC_GOT_TLSGD16: 6237 case elfcpp::R_POWERPC_GOT_TLSGD16_LO: 6238 case elfcpp::R_POWERPC_GOT_TLSGD16_HI: 6239 case elfcpp::R_POWERPC_GOT_TLSGD16_HA: 6240 { 6241 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true); 6242 if (tls_type == tls::TLSOPT_NONE) 6243 { 6244 Output_data_got_powerpc<size, big_endian>* got 6245 = target->got_section(symtab, layout); 6246 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 6247 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 6248 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD, 6249 rela_dyn, elfcpp::R_POWERPC_DTPMOD); 6250 } 6251 else if (tls_type == tls::TLSOPT_TO_LE) 6252 { 6253 // no GOT relocs needed for Local Exec. 6254 } 6255 else 6256 gold_unreachable(); 6257 } 6258 break; 6259 6260 case elfcpp::R_POWERPC_GOT_TLSLD16: 6261 case elfcpp::R_POWERPC_GOT_TLSLD16_LO: 6262 case elfcpp::R_POWERPC_GOT_TLSLD16_HI: 6263 case elfcpp::R_POWERPC_GOT_TLSLD16_HA: 6264 { 6265 const tls::Tls_optimization tls_type = target->optimize_tls_ld(); 6266 if (tls_type == tls::TLSOPT_NONE) 6267 target->tlsld_got_offset(symtab, layout, object); 6268 else if (tls_type == tls::TLSOPT_TO_LE) 6269 { 6270 // no GOT relocs needed for Local Exec. 6271 if (parameters->options().emit_relocs()) 6272 { 6273 Output_section* os = layout->tls_segment()->first_section(); 6274 gold_assert(os != NULL); 6275 os->set_needs_symtab_index(); 6276 } 6277 } 6278 else 6279 gold_unreachable(); 6280 } 6281 break; 6282 6283 case elfcpp::R_POWERPC_GOT_DTPREL16: 6284 case elfcpp::R_POWERPC_GOT_DTPREL16_LO: 6285 case elfcpp::R_POWERPC_GOT_DTPREL16_HI: 6286 case elfcpp::R_POWERPC_GOT_DTPREL16_HA: 6287 { 6288 Output_data_got_powerpc<size, big_endian>* got 6289 = target->got_section(symtab, layout); 6290 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 6291 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL); 6292 } 6293 break; 6294 6295 case elfcpp::R_POWERPC_GOT_TPREL16: 6296 case elfcpp::R_POWERPC_GOT_TPREL16_LO: 6297 case elfcpp::R_POWERPC_GOT_TPREL16_HI: 6298 case elfcpp::R_POWERPC_GOT_TPREL16_HA: 6299 { 6300 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true); 6301 if (tls_type == tls::TLSOPT_NONE) 6302 { 6303 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 6304 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL)) 6305 { 6306 Output_data_got_powerpc<size, big_endian>* got 6307 = target->got_section(symtab, layout); 6308 unsigned int off = got->add_constant(0); 6309 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off); 6310 6311 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 6312 rela_dyn->add_symbolless_local_addend(object, r_sym, 6313 elfcpp::R_POWERPC_TPREL, 6314 got, off, 0); 6315 } 6316 } 6317 else if (tls_type == tls::TLSOPT_TO_LE) 6318 { 6319 // no GOT relocs needed for Local Exec. 6320 } 6321 else 6322 gold_unreachable(); 6323 } 6324 break; 6325 6326 default: 6327 unsupported_reloc_local(object, r_type); 6328 break; 6329 } 6330 6331 if (size == 64 6332 && parameters->options().toc_optimize()) 6333 { 6334 if (data_shndx == ppc_object->toc_shndx()) 6335 { 6336 bool ok = true; 6337 if (r_type != elfcpp::R_PPC64_ADDR64 6338 || (is_ifunc && target->abiversion() < 2)) 6339 ok = false; 6340 else if (parameters->options().output_is_position_independent()) 6341 { 6342 if (is_ifunc) 6343 ok = false; 6344 else 6345 { 6346 unsigned int shndx = lsym.get_st_shndx(); 6347 if (shndx >= elfcpp::SHN_LORESERVE 6348 && shndx != elfcpp::SHN_XINDEX) 6349 ok = false; 6350 } 6351 } 6352 if (!ok) 6353 ppc_object->set_no_toc_opt(reloc.get_r_offset()); 6354 } 6355 6356 enum {no_check, check_lo, check_ha} insn_check; 6357 switch (r_type) 6358 { 6359 default: 6360 insn_check = no_check; 6361 break; 6362 6363 case elfcpp::R_POWERPC_GOT_TLSLD16_HA: 6364 case elfcpp::R_POWERPC_GOT_TLSGD16_HA: 6365 case elfcpp::R_POWERPC_GOT_TPREL16_HA: 6366 case elfcpp::R_POWERPC_GOT_DTPREL16_HA: 6367 case elfcpp::R_POWERPC_GOT16_HA: 6368 case elfcpp::R_PPC64_TOC16_HA: 6369 insn_check = check_ha; 6370 break; 6371 6372 case elfcpp::R_POWERPC_GOT_TLSLD16_LO: 6373 case elfcpp::R_POWERPC_GOT_TLSGD16_LO: 6374 case elfcpp::R_POWERPC_GOT_TPREL16_LO: 6375 case elfcpp::R_POWERPC_GOT_DTPREL16_LO: 6376 case elfcpp::R_POWERPC_GOT16_LO: 6377 case elfcpp::R_PPC64_GOT16_LO_DS: 6378 case elfcpp::R_PPC64_TOC16_LO: 6379 case elfcpp::R_PPC64_TOC16_LO_DS: 6380 insn_check = check_lo; 6381 break; 6382 } 6383 6384 section_size_type slen; 6385 const unsigned char* view = NULL; 6386 if (insn_check != no_check) 6387 { 6388 view = ppc_object->section_contents(data_shndx, &slen, false); 6389 section_size_type off = 6390 convert_to_section_size_type(reloc.get_r_offset()) & -4; 6391 if (off < slen) 6392 { 6393 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off); 6394 if (insn_check == check_lo 6395 ? !ok_lo_toc_insn(insn, r_type) 6396 : ((insn & ((0x3f << 26) | 0x1f << 16)) 6397 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)) 6398 { 6399 ppc_object->set_no_toc_opt(); 6400 gold_warning(_("%s: toc optimization is not supported " 6401 "for %#08x instruction"), 6402 ppc_object->name().c_str(), insn); 6403 } 6404 } 6405 } 6406 6407 switch (r_type) 6408 { 6409 default: 6410 break; 6411 case elfcpp::R_PPC64_TOC16: 6412 case elfcpp::R_PPC64_TOC16_LO: 6413 case elfcpp::R_PPC64_TOC16_HI: 6414 case elfcpp::R_PPC64_TOC16_HA: 6415 case elfcpp::R_PPC64_TOC16_DS: 6416 case elfcpp::R_PPC64_TOC16_LO_DS: 6417 unsigned int shndx = lsym.get_st_shndx(); 6418 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 6419 bool is_ordinary; 6420 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); 6421 if (is_ordinary && shndx == ppc_object->toc_shndx()) 6422 { 6423 Address dst_off = lsym.get_st_value() + reloc.get_r_offset(); 6424 if (dst_off < ppc_object->section_size(shndx)) 6425 { 6426 bool ok = false; 6427 if (r_type == elfcpp::R_PPC64_TOC16_HA) 6428 ok = true; 6429 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS) 6430 { 6431 // Need to check that the insn is a ld 6432 if (!view) 6433 view = ppc_object->section_contents(data_shndx, 6434 &slen, 6435 false); 6436 section_size_type off = 6437 (convert_to_section_size_type(reloc.get_r_offset()) 6438 + (big_endian ? -2 : 3)); 6439 if (off < slen 6440 && (view[off] & (0x3f << 2)) == 58u << 2) 6441 ok = true; 6442 } 6443 if (!ok) 6444 ppc_object->set_no_toc_opt(dst_off); 6445 } 6446 } 6447 break; 6448 } 6449 } 6450 6451 if (size == 32) 6452 { 6453 switch (r_type) 6454 { 6455 case elfcpp::R_POWERPC_REL32: 6456 if (ppc_object->got2_shndx() != 0 6457 && parameters->options().output_is_position_independent()) 6458 { 6459 unsigned int shndx = lsym.get_st_shndx(); 6460 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 6461 bool is_ordinary; 6462 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); 6463 if (is_ordinary && shndx == ppc_object->got2_shndx() 6464 && (ppc_object->section_flags(data_shndx) 6465 & elfcpp::SHF_EXECINSTR) != 0) 6466 gold_error(_("%s: unsupported -mbss-plt code"), 6467 ppc_object->name().c_str()); 6468 } 6469 break; 6470 default: 6471 break; 6472 } 6473 } 6474 6475 switch (r_type) 6476 { 6477 case elfcpp::R_POWERPC_GOT_TLSLD16: 6478 case elfcpp::R_POWERPC_GOT_TLSGD16: 6479 case elfcpp::R_POWERPC_GOT_TPREL16: 6480 case elfcpp::R_POWERPC_GOT_DTPREL16: 6481 case elfcpp::R_POWERPC_GOT16: 6482 case elfcpp::R_PPC64_GOT16_DS: 6483 case elfcpp::R_PPC64_TOC16: 6484 case elfcpp::R_PPC64_TOC16_DS: 6485 ppc_object->set_has_small_toc_reloc(); 6486 default: 6487 break; 6488 } 6489} 6490 6491// Report an unsupported relocation against a global symbol. 6492 6493template<int size, bool big_endian> 6494void 6495Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global( 6496 Sized_relobj_file<size, big_endian>* object, 6497 unsigned int r_type, 6498 Symbol* gsym) 6499{ 6500 gold_error(_("%s: unsupported reloc %u against global symbol %s"), 6501 object->name().c_str(), r_type, gsym->demangled_name().c_str()); 6502} 6503 6504// Scan a relocation for a global symbol. 6505 6506template<int size, bool big_endian> 6507inline void 6508Target_powerpc<size, big_endian>::Scan::global( 6509 Symbol_table* symtab, 6510 Layout* layout, 6511 Target_powerpc<size, big_endian>* target, 6512 Sized_relobj_file<size, big_endian>* object, 6513 unsigned int data_shndx, 6514 Output_section* output_section, 6515 const elfcpp::Rela<size, big_endian>& reloc, 6516 unsigned int r_type, 6517 Symbol* gsym) 6518{ 6519 if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP) 6520 return; 6521 6522 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD) 6523 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD)) 6524 { 6525 this->expect_tls_get_addr_call(); 6526 const bool final = gsym->final_value_is_known(); 6527 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final); 6528 if (tls_type != tls::TLSOPT_NONE) 6529 this->skip_next_tls_get_addr_call(); 6530 } 6531 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD) 6532 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD)) 6533 { 6534 this->expect_tls_get_addr_call(); 6535 const tls::Tls_optimization tls_type = target->optimize_tls_ld(); 6536 if (tls_type != tls::TLSOPT_NONE) 6537 this->skip_next_tls_get_addr_call(); 6538 } 6539 6540 Powerpc_relobj<size, big_endian>* ppc_object 6541 = static_cast<Powerpc_relobj<size, big_endian>*>(object); 6542 6543 // A STT_GNU_IFUNC symbol may require a PLT entry. 6544 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC; 6545 bool pushed_ifunc = false; 6546 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true)) 6547 { 6548 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 6549 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(), 6550 r_type, r_sym, reloc.get_r_addend()); 6551 target->make_plt_entry(symtab, layout, gsym); 6552 pushed_ifunc = true; 6553 } 6554 6555 switch (r_type) 6556 { 6557 case elfcpp::R_POWERPC_NONE: 6558 case elfcpp::R_POWERPC_GNU_VTINHERIT: 6559 case elfcpp::R_POWERPC_GNU_VTENTRY: 6560 case elfcpp::R_PPC_LOCAL24PC: 6561 case elfcpp::R_POWERPC_TLS: 6562 case elfcpp::R_PPC64_ENTRY: 6563 break; 6564 6565 case elfcpp::R_PPC64_TOC: 6566 { 6567 Output_data_got_powerpc<size, big_endian>* got 6568 = target->got_section(symtab, layout); 6569 if (parameters->options().output_is_position_independent()) 6570 { 6571 Address off = reloc.get_r_offset(); 6572 if (size == 64 6573 && data_shndx == ppc_object->opd_shndx() 6574 && ppc_object->get_opd_discard(off - 8)) 6575 break; 6576 6577 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 6578 Powerpc_relobj<size, big_endian>* symobj = ppc_object; 6579 if (data_shndx != ppc_object->opd_shndx()) 6580 symobj = static_cast 6581 <Powerpc_relobj<size, big_endian>*>(gsym->object()); 6582 rela_dyn->add_output_section_relative(got->output_section(), 6583 elfcpp::R_POWERPC_RELATIVE, 6584 output_section, 6585 object, data_shndx, off, 6586 symobj->toc_base_offset()); 6587 } 6588 } 6589 break; 6590 6591 case elfcpp::R_PPC64_ADDR64: 6592 if (size == 64 6593 && target->abiversion() < 2 6594 && data_shndx == ppc_object->opd_shndx() 6595 && (gsym->is_defined_in_discarded_section() 6596 || gsym->object() != object)) 6597 { 6598 ppc_object->set_opd_discard(reloc.get_r_offset()); 6599 break; 6600 } 6601 // Fall through. 6602 case elfcpp::R_PPC64_UADDR64: 6603 case elfcpp::R_POWERPC_ADDR32: 6604 case elfcpp::R_POWERPC_UADDR32: 6605 case elfcpp::R_POWERPC_ADDR24: 6606 case elfcpp::R_POWERPC_ADDR16: 6607 case elfcpp::R_POWERPC_ADDR16_LO: 6608 case elfcpp::R_POWERPC_ADDR16_HI: 6609 case elfcpp::R_POWERPC_ADDR16_HA: 6610 case elfcpp::R_POWERPC_UADDR16: 6611 case elfcpp::R_PPC64_ADDR16_HIGH: 6612 case elfcpp::R_PPC64_ADDR16_HIGHA: 6613 case elfcpp::R_PPC64_ADDR16_HIGHER: 6614 case elfcpp::R_PPC64_ADDR16_HIGHERA: 6615 case elfcpp::R_PPC64_ADDR16_HIGHEST: 6616 case elfcpp::R_PPC64_ADDR16_HIGHESTA: 6617 case elfcpp::R_PPC64_ADDR16_DS: 6618 case elfcpp::R_PPC64_ADDR16_LO_DS: 6619 case elfcpp::R_POWERPC_ADDR14: 6620 case elfcpp::R_POWERPC_ADDR14_BRTAKEN: 6621 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN: 6622 { 6623 // Make a PLT entry if necessary. 6624 if (gsym->needs_plt_entry()) 6625 { 6626 // Since this is not a PC-relative relocation, we may be 6627 // taking the address of a function. In that case we need to 6628 // set the entry in the dynamic symbol table to the address of 6629 // the PLT call stub. 6630 bool need_ifunc_plt = false; 6631 if ((size == 32 || target->abiversion() >= 2) 6632 && gsym->is_from_dynobj() 6633 && !parameters->options().output_is_position_independent()) 6634 { 6635 gsym->set_needs_dynsym_value(); 6636 need_ifunc_plt = true; 6637 } 6638 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt)) 6639 { 6640 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 6641 target->push_branch(ppc_object, data_shndx, 6642 reloc.get_r_offset(), r_type, r_sym, 6643 reloc.get_r_addend()); 6644 target->make_plt_entry(symtab, layout, gsym); 6645 } 6646 } 6647 // Make a dynamic relocation if necessary. 6648 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)) 6649 || (size == 64 && is_ifunc && target->abiversion() < 2)) 6650 { 6651 if (!parameters->options().output_is_position_independent() 6652 && gsym->may_need_copy_reloc()) 6653 { 6654 target->copy_reloc(symtab, layout, object, 6655 data_shndx, output_section, gsym, reloc); 6656 } 6657 else if ((((size == 32 6658 && r_type == elfcpp::R_POWERPC_ADDR32) 6659 || (size == 64 6660 && r_type == elfcpp::R_PPC64_ADDR64 6661 && target->abiversion() >= 2)) 6662 && gsym->can_use_relative_reloc(false) 6663 && !(gsym->visibility() == elfcpp::STV_PROTECTED 6664 && parameters->options().shared())) 6665 || (size == 64 6666 && r_type == elfcpp::R_PPC64_ADDR64 6667 && target->abiversion() < 2 6668 && (gsym->can_use_relative_reloc(false) 6669 || data_shndx == ppc_object->opd_shndx()))) 6670 { 6671 Reloc_section* rela_dyn 6672 = target->rela_dyn_section(symtab, layout, is_ifunc); 6673 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE 6674 : elfcpp::R_POWERPC_RELATIVE); 6675 rela_dyn->add_symbolless_global_addend( 6676 gsym, dynrel, output_section, object, data_shndx, 6677 reloc.get_r_offset(), reloc.get_r_addend()); 6678 } 6679 else 6680 { 6681 Reloc_section* rela_dyn 6682 = target->rela_dyn_section(symtab, layout, is_ifunc); 6683 check_non_pic(object, r_type); 6684 rela_dyn->add_global(gsym, r_type, output_section, 6685 object, data_shndx, 6686 reloc.get_r_offset(), 6687 reloc.get_r_addend()); 6688 6689 if (size == 64 6690 && parameters->options().toc_optimize() 6691 && data_shndx == ppc_object->toc_shndx()) 6692 ppc_object->set_no_toc_opt(reloc.get_r_offset()); 6693 } 6694 } 6695 } 6696 break; 6697 6698 case elfcpp::R_PPC_PLTREL24: 6699 case elfcpp::R_POWERPC_REL24: 6700 if (!is_ifunc) 6701 { 6702 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 6703 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(), 6704 r_type, r_sym, reloc.get_r_addend()); 6705 if (gsym->needs_plt_entry() 6706 || (!gsym->final_value_is_known() 6707 && (gsym->is_undefined() 6708 || gsym->is_from_dynobj() 6709 || gsym->is_preemptible()))) 6710 target->make_plt_entry(symtab, layout, gsym); 6711 } 6712 // Fall through. 6713 6714 case elfcpp::R_PPC64_REL64: 6715 case elfcpp::R_POWERPC_REL32: 6716 // Make a dynamic relocation if necessary. 6717 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))) 6718 { 6719 if (!parameters->options().output_is_position_independent() 6720 && gsym->may_need_copy_reloc()) 6721 { 6722 target->copy_reloc(symtab, layout, object, 6723 data_shndx, output_section, gsym, 6724 reloc); 6725 } 6726 else 6727 { 6728 Reloc_section* rela_dyn 6729 = target->rela_dyn_section(symtab, layout, is_ifunc); 6730 check_non_pic(object, r_type); 6731 rela_dyn->add_global(gsym, r_type, output_section, object, 6732 data_shndx, reloc.get_r_offset(), 6733 reloc.get_r_addend()); 6734 } 6735 } 6736 break; 6737 6738 case elfcpp::R_POWERPC_REL14: 6739 case elfcpp::R_POWERPC_REL14_BRTAKEN: 6740 case elfcpp::R_POWERPC_REL14_BRNTAKEN: 6741 if (!is_ifunc) 6742 { 6743 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 6744 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(), 6745 r_type, r_sym, reloc.get_r_addend()); 6746 } 6747 break; 6748 6749 case elfcpp::R_POWERPC_REL16: 6750 case elfcpp::R_POWERPC_REL16_LO: 6751 case elfcpp::R_POWERPC_REL16_HI: 6752 case elfcpp::R_POWERPC_REL16_HA: 6753 case elfcpp::R_POWERPC_REL16DX_HA: 6754 case elfcpp::R_POWERPC_SECTOFF: 6755 case elfcpp::R_POWERPC_SECTOFF_LO: 6756 case elfcpp::R_POWERPC_SECTOFF_HI: 6757 case elfcpp::R_POWERPC_SECTOFF_HA: 6758 case elfcpp::R_PPC64_SECTOFF_DS: 6759 case elfcpp::R_PPC64_SECTOFF_LO_DS: 6760 case elfcpp::R_POWERPC_TPREL16: 6761 case elfcpp::R_POWERPC_TPREL16_LO: 6762 case elfcpp::R_POWERPC_TPREL16_HI: 6763 case elfcpp::R_POWERPC_TPREL16_HA: 6764 case elfcpp::R_PPC64_TPREL16_DS: 6765 case elfcpp::R_PPC64_TPREL16_LO_DS: 6766 case elfcpp::R_PPC64_TPREL16_HIGH: 6767 case elfcpp::R_PPC64_TPREL16_HIGHA: 6768 case elfcpp::R_PPC64_TPREL16_HIGHER: 6769 case elfcpp::R_PPC64_TPREL16_HIGHERA: 6770 case elfcpp::R_PPC64_TPREL16_HIGHEST: 6771 case elfcpp::R_PPC64_TPREL16_HIGHESTA: 6772 case elfcpp::R_POWERPC_DTPREL16: 6773 case elfcpp::R_POWERPC_DTPREL16_LO: 6774 case elfcpp::R_POWERPC_DTPREL16_HI: 6775 case elfcpp::R_POWERPC_DTPREL16_HA: 6776 case elfcpp::R_PPC64_DTPREL16_DS: 6777 case elfcpp::R_PPC64_DTPREL16_LO_DS: 6778 case elfcpp::R_PPC64_DTPREL16_HIGH: 6779 case elfcpp::R_PPC64_DTPREL16_HIGHA: 6780 case elfcpp::R_PPC64_DTPREL16_HIGHER: 6781 case elfcpp::R_PPC64_DTPREL16_HIGHERA: 6782 case elfcpp::R_PPC64_DTPREL16_HIGHEST: 6783 case elfcpp::R_PPC64_DTPREL16_HIGHESTA: 6784 case elfcpp::R_PPC64_TLSGD: 6785 case elfcpp::R_PPC64_TLSLD: 6786 case elfcpp::R_PPC64_ADDR64_LOCAL: 6787 break; 6788 6789 case elfcpp::R_POWERPC_GOT16: 6790 case elfcpp::R_POWERPC_GOT16_LO: 6791 case elfcpp::R_POWERPC_GOT16_HI: 6792 case elfcpp::R_POWERPC_GOT16_HA: 6793 case elfcpp::R_PPC64_GOT16_DS: 6794 case elfcpp::R_PPC64_GOT16_LO_DS: 6795 { 6796 // The symbol requires a GOT entry. 6797 Output_data_got_powerpc<size, big_endian>* got; 6798 6799 got = target->got_section(symtab, layout); 6800 if (gsym->final_value_is_known()) 6801 { 6802 if (is_ifunc 6803 && (size == 32 || target->abiversion() >= 2)) 6804 got->add_global_plt(gsym, GOT_TYPE_STANDARD); 6805 else 6806 got->add_global(gsym, GOT_TYPE_STANDARD); 6807 } 6808 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD)) 6809 { 6810 // If we are generating a shared object or a pie, this 6811 // symbol's GOT entry will be set by a dynamic relocation. 6812 unsigned int off = got->add_constant(0); 6813 gsym->set_got_offset(GOT_TYPE_STANDARD, off); 6814 6815 Reloc_section* rela_dyn 6816 = target->rela_dyn_section(symtab, layout, is_ifunc); 6817 6818 if (gsym->can_use_relative_reloc(false) 6819 && !((size == 32 6820 || target->abiversion() >= 2) 6821 && gsym->visibility() == elfcpp::STV_PROTECTED 6822 && parameters->options().shared())) 6823 { 6824 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE 6825 : elfcpp::R_POWERPC_RELATIVE); 6826 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false); 6827 } 6828 else 6829 { 6830 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT; 6831 rela_dyn->add_global(gsym, dynrel, got, off, 0); 6832 } 6833 } 6834 } 6835 break; 6836 6837 case elfcpp::R_PPC64_TOC16: 6838 case elfcpp::R_PPC64_TOC16_LO: 6839 case elfcpp::R_PPC64_TOC16_HI: 6840 case elfcpp::R_PPC64_TOC16_HA: 6841 case elfcpp::R_PPC64_TOC16_DS: 6842 case elfcpp::R_PPC64_TOC16_LO_DS: 6843 // We need a GOT section. 6844 target->got_section(symtab, layout); 6845 break; 6846 6847 case elfcpp::R_POWERPC_GOT_TLSGD16: 6848 case elfcpp::R_POWERPC_GOT_TLSGD16_LO: 6849 case elfcpp::R_POWERPC_GOT_TLSGD16_HI: 6850 case elfcpp::R_POWERPC_GOT_TLSGD16_HA: 6851 { 6852 const bool final = gsym->final_value_is_known(); 6853 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final); 6854 if (tls_type == tls::TLSOPT_NONE) 6855 { 6856 Output_data_got_powerpc<size, big_endian>* got 6857 = target->got_section(symtab, layout); 6858 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 6859 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn, 6860 elfcpp::R_POWERPC_DTPMOD, 6861 elfcpp::R_POWERPC_DTPREL); 6862 } 6863 else if (tls_type == tls::TLSOPT_TO_IE) 6864 { 6865 if (!gsym->has_got_offset(GOT_TYPE_TPREL)) 6866 { 6867 Output_data_got_powerpc<size, big_endian>* got 6868 = target->got_section(symtab, layout); 6869 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 6870 if (gsym->is_undefined() 6871 || gsym->is_from_dynobj()) 6872 { 6873 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn, 6874 elfcpp::R_POWERPC_TPREL); 6875 } 6876 else 6877 { 6878 unsigned int off = got->add_constant(0); 6879 gsym->set_got_offset(GOT_TYPE_TPREL, off); 6880 unsigned int dynrel = elfcpp::R_POWERPC_TPREL; 6881 rela_dyn->add_symbolless_global_addend(gsym, dynrel, 6882 got, off, 0); 6883 } 6884 } 6885 } 6886 else if (tls_type == tls::TLSOPT_TO_LE) 6887 { 6888 // no GOT relocs needed for Local Exec. 6889 } 6890 else 6891 gold_unreachable(); 6892 } 6893 break; 6894 6895 case elfcpp::R_POWERPC_GOT_TLSLD16: 6896 case elfcpp::R_POWERPC_GOT_TLSLD16_LO: 6897 case elfcpp::R_POWERPC_GOT_TLSLD16_HI: 6898 case elfcpp::R_POWERPC_GOT_TLSLD16_HA: 6899 { 6900 const tls::Tls_optimization tls_type = target->optimize_tls_ld(); 6901 if (tls_type == tls::TLSOPT_NONE) 6902 target->tlsld_got_offset(symtab, layout, object); 6903 else if (tls_type == tls::TLSOPT_TO_LE) 6904 { 6905 // no GOT relocs needed for Local Exec. 6906 if (parameters->options().emit_relocs()) 6907 { 6908 Output_section* os = layout->tls_segment()->first_section(); 6909 gold_assert(os != NULL); 6910 os->set_needs_symtab_index(); 6911 } 6912 } 6913 else 6914 gold_unreachable(); 6915 } 6916 break; 6917 6918 case elfcpp::R_POWERPC_GOT_DTPREL16: 6919 case elfcpp::R_POWERPC_GOT_DTPREL16_LO: 6920 case elfcpp::R_POWERPC_GOT_DTPREL16_HI: 6921 case elfcpp::R_POWERPC_GOT_DTPREL16_HA: 6922 { 6923 Output_data_got_powerpc<size, big_endian>* got 6924 = target->got_section(symtab, layout); 6925 if (!gsym->final_value_is_known() 6926 && (gsym->is_from_dynobj() 6927 || gsym->is_undefined() 6928 || gsym->is_preemptible())) 6929 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL, 6930 target->rela_dyn_section(layout), 6931 elfcpp::R_POWERPC_DTPREL); 6932 else 6933 got->add_global_tls(gsym, GOT_TYPE_DTPREL); 6934 } 6935 break; 6936 6937 case elfcpp::R_POWERPC_GOT_TPREL16: 6938 case elfcpp::R_POWERPC_GOT_TPREL16_LO: 6939 case elfcpp::R_POWERPC_GOT_TPREL16_HI: 6940 case elfcpp::R_POWERPC_GOT_TPREL16_HA: 6941 { 6942 const bool final = gsym->final_value_is_known(); 6943 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final); 6944 if (tls_type == tls::TLSOPT_NONE) 6945 { 6946 if (!gsym->has_got_offset(GOT_TYPE_TPREL)) 6947 { 6948 Output_data_got_powerpc<size, big_endian>* got 6949 = target->got_section(symtab, layout); 6950 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 6951 if (gsym->is_undefined() 6952 || gsym->is_from_dynobj()) 6953 { 6954 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn, 6955 elfcpp::R_POWERPC_TPREL); 6956 } 6957 else 6958 { 6959 unsigned int off = got->add_constant(0); 6960 gsym->set_got_offset(GOT_TYPE_TPREL, off); 6961 unsigned int dynrel = elfcpp::R_POWERPC_TPREL; 6962 rela_dyn->add_symbolless_global_addend(gsym, dynrel, 6963 got, off, 0); 6964 } 6965 } 6966 } 6967 else if (tls_type == tls::TLSOPT_TO_LE) 6968 { 6969 // no GOT relocs needed for Local Exec. 6970 } 6971 else 6972 gold_unreachable(); 6973 } 6974 break; 6975 6976 default: 6977 unsupported_reloc_global(object, r_type, gsym); 6978 break; 6979 } 6980 6981 if (size == 64 6982 && parameters->options().toc_optimize()) 6983 { 6984 if (data_shndx == ppc_object->toc_shndx()) 6985 { 6986 bool ok = true; 6987 if (r_type != elfcpp::R_PPC64_ADDR64 6988 || (is_ifunc && target->abiversion() < 2)) 6989 ok = false; 6990 else if (parameters->options().output_is_position_independent() 6991 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined())) 6992 ok = false; 6993 if (!ok) 6994 ppc_object->set_no_toc_opt(reloc.get_r_offset()); 6995 } 6996 6997 enum {no_check, check_lo, check_ha} insn_check; 6998 switch (r_type) 6999 { 7000 default: 7001 insn_check = no_check; 7002 break; 7003 7004 case elfcpp::R_POWERPC_GOT_TLSLD16_HA: 7005 case elfcpp::R_POWERPC_GOT_TLSGD16_HA: 7006 case elfcpp::R_POWERPC_GOT_TPREL16_HA: 7007 case elfcpp::R_POWERPC_GOT_DTPREL16_HA: 7008 case elfcpp::R_POWERPC_GOT16_HA: 7009 case elfcpp::R_PPC64_TOC16_HA: 7010 insn_check = check_ha; 7011 break; 7012 7013 case elfcpp::R_POWERPC_GOT_TLSLD16_LO: 7014 case elfcpp::R_POWERPC_GOT_TLSGD16_LO: 7015 case elfcpp::R_POWERPC_GOT_TPREL16_LO: 7016 case elfcpp::R_POWERPC_GOT_DTPREL16_LO: 7017 case elfcpp::R_POWERPC_GOT16_LO: 7018 case elfcpp::R_PPC64_GOT16_LO_DS: 7019 case elfcpp::R_PPC64_TOC16_LO: 7020 case elfcpp::R_PPC64_TOC16_LO_DS: 7021 insn_check = check_lo; 7022 break; 7023 } 7024 7025 section_size_type slen; 7026 const unsigned char* view = NULL; 7027 if (insn_check != no_check) 7028 { 7029 view = ppc_object->section_contents(data_shndx, &slen, false); 7030 section_size_type off = 7031 convert_to_section_size_type(reloc.get_r_offset()) & -4; 7032 if (off < slen) 7033 { 7034 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off); 7035 if (insn_check == check_lo 7036 ? !ok_lo_toc_insn(insn, r_type) 7037 : ((insn & ((0x3f << 26) | 0x1f << 16)) 7038 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)) 7039 { 7040 ppc_object->set_no_toc_opt(); 7041 gold_warning(_("%s: toc optimization is not supported " 7042 "for %#08x instruction"), 7043 ppc_object->name().c_str(), insn); 7044 } 7045 } 7046 } 7047 7048 switch (r_type) 7049 { 7050 default: 7051 break; 7052 case elfcpp::R_PPC64_TOC16: 7053 case elfcpp::R_PPC64_TOC16_LO: 7054 case elfcpp::R_PPC64_TOC16_HI: 7055 case elfcpp::R_PPC64_TOC16_HA: 7056 case elfcpp::R_PPC64_TOC16_DS: 7057 case elfcpp::R_PPC64_TOC16_LO_DS: 7058 if (gsym->source() == Symbol::FROM_OBJECT 7059 && !gsym->object()->is_dynamic()) 7060 { 7061 Powerpc_relobj<size, big_endian>* sym_object 7062 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object()); 7063 bool is_ordinary; 7064 unsigned int shndx = gsym->shndx(&is_ordinary); 7065 if (shndx == sym_object->toc_shndx()) 7066 { 7067 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym); 7068 Address dst_off = sym->value() + reloc.get_r_offset(); 7069 if (dst_off < sym_object->section_size(shndx)) 7070 { 7071 bool ok = false; 7072 if (r_type == elfcpp::R_PPC64_TOC16_HA) 7073 ok = true; 7074 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS) 7075 { 7076 // Need to check that the insn is a ld 7077 if (!view) 7078 view = ppc_object->section_contents(data_shndx, 7079 &slen, 7080 false); 7081 section_size_type off = 7082 (convert_to_section_size_type(reloc.get_r_offset()) 7083 + (big_endian ? -2 : 3)); 7084 if (off < slen 7085 && (view[off] & (0x3f << 2)) == (58u << 2)) 7086 ok = true; 7087 } 7088 if (!ok) 7089 sym_object->set_no_toc_opt(dst_off); 7090 } 7091 } 7092 } 7093 break; 7094 } 7095 } 7096 7097 if (size == 32) 7098 { 7099 switch (r_type) 7100 { 7101 case elfcpp::R_PPC_LOCAL24PC: 7102 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0) 7103 gold_error(_("%s: unsupported -mbss-plt code"), 7104 ppc_object->name().c_str()); 7105 break; 7106 default: 7107 break; 7108 } 7109 } 7110 7111 switch (r_type) 7112 { 7113 case elfcpp::R_POWERPC_GOT_TLSLD16: 7114 case elfcpp::R_POWERPC_GOT_TLSGD16: 7115 case elfcpp::R_POWERPC_GOT_TPREL16: 7116 case elfcpp::R_POWERPC_GOT_DTPREL16: 7117 case elfcpp::R_POWERPC_GOT16: 7118 case elfcpp::R_PPC64_GOT16_DS: 7119 case elfcpp::R_PPC64_TOC16: 7120 case elfcpp::R_PPC64_TOC16_DS: 7121 ppc_object->set_has_small_toc_reloc(); 7122 default: 7123 break; 7124 } 7125} 7126 7127// Process relocations for gc. 7128 7129template<int size, bool big_endian> 7130void 7131Target_powerpc<size, big_endian>::gc_process_relocs( 7132 Symbol_table* symtab, 7133 Layout* layout, 7134 Sized_relobj_file<size, big_endian>* object, 7135 unsigned int data_shndx, 7136 unsigned int, 7137 const unsigned char* prelocs, 7138 size_t reloc_count, 7139 Output_section* output_section, 7140 bool needs_special_offset_handling, 7141 size_t local_symbol_count, 7142 const unsigned char* plocal_symbols) 7143{ 7144 typedef Target_powerpc<size, big_endian> Powerpc; 7145 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian> 7146 Classify_reloc; 7147 7148 Powerpc_relobj<size, big_endian>* ppc_object 7149 = static_cast<Powerpc_relobj<size, big_endian>*>(object); 7150 if (size == 64) 7151 ppc_object->set_opd_valid(); 7152 if (size == 64 && data_shndx == ppc_object->opd_shndx()) 7153 { 7154 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p; 7155 for (p = ppc_object->access_from_map()->begin(); 7156 p != ppc_object->access_from_map()->end(); 7157 ++p) 7158 { 7159 Address dst_off = p->first; 7160 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off); 7161 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s; 7162 for (s = p->second.begin(); s != p->second.end(); ++s) 7163 { 7164 Relobj* src_obj = s->first; 7165 unsigned int src_indx = s->second; 7166 symtab->gc()->add_reference(src_obj, src_indx, 7167 ppc_object, dst_indx); 7168 } 7169 p->second.clear(); 7170 } 7171 ppc_object->access_from_map()->clear(); 7172 ppc_object->process_gc_mark(symtab); 7173 // Don't look at .opd relocs as .opd will reference everything. 7174 return; 7175 } 7176 7177 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>( 7178 symtab, 7179 layout, 7180 this, 7181 object, 7182 data_shndx, 7183 prelocs, 7184 reloc_count, 7185 output_section, 7186 needs_special_offset_handling, 7187 local_symbol_count, 7188 plocal_symbols); 7189} 7190 7191// Handle target specific gc actions when adding a gc reference from 7192// SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX 7193// and DST_OFF. For powerpc64, this adds a referenc to the code 7194// section of a function descriptor. 7195 7196template<int size, bool big_endian> 7197void 7198Target_powerpc<size, big_endian>::do_gc_add_reference( 7199 Symbol_table* symtab, 7200 Relobj* src_obj, 7201 unsigned int src_shndx, 7202 Relobj* dst_obj, 7203 unsigned int dst_shndx, 7204 Address dst_off) const 7205{ 7206 if (size != 64 || dst_obj->is_dynamic()) 7207 return; 7208 7209 Powerpc_relobj<size, big_endian>* ppc_object 7210 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj); 7211 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx()) 7212 { 7213 if (ppc_object->opd_valid()) 7214 { 7215 dst_shndx = ppc_object->get_opd_ent(dst_off); 7216 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx); 7217 } 7218 else 7219 { 7220 // If we haven't run scan_opd_relocs, we must delay 7221 // processing this function descriptor reference. 7222 ppc_object->add_reference(src_obj, src_shndx, dst_off); 7223 } 7224 } 7225} 7226 7227// Add any special sections for this symbol to the gc work list. 7228// For powerpc64, this adds the code section of a function 7229// descriptor. 7230 7231template<int size, bool big_endian> 7232void 7233Target_powerpc<size, big_endian>::do_gc_mark_symbol( 7234 Symbol_table* symtab, 7235 Symbol* sym) const 7236{ 7237 if (size == 64) 7238 { 7239 Powerpc_relobj<size, big_endian>* ppc_object 7240 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object()); 7241 bool is_ordinary; 7242 unsigned int shndx = sym->shndx(&is_ordinary); 7243 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx()) 7244 { 7245 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym); 7246 Address dst_off = gsym->value(); 7247 if (ppc_object->opd_valid()) 7248 { 7249 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off); 7250 symtab->gc()->worklist().push_back(Section_id(ppc_object, 7251 dst_indx)); 7252 } 7253 else 7254 ppc_object->add_gc_mark(dst_off); 7255 } 7256 } 7257} 7258 7259// For a symbol location in .opd, set LOC to the location of the 7260// function entry. 7261 7262template<int size, bool big_endian> 7263void 7264Target_powerpc<size, big_endian>::do_function_location( 7265 Symbol_location* loc) const 7266{ 7267 if (size == 64 && loc->shndx != 0) 7268 { 7269 if (loc->object->is_dynamic()) 7270 { 7271 Powerpc_dynobj<size, big_endian>* ppc_object 7272 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object); 7273 if (loc->shndx == ppc_object->opd_shndx()) 7274 { 7275 Address dest_off; 7276 Address off = loc->offset - ppc_object->opd_address(); 7277 loc->shndx = ppc_object->get_opd_ent(off, &dest_off); 7278 loc->offset = dest_off; 7279 } 7280 } 7281 else 7282 { 7283 const Powerpc_relobj<size, big_endian>* ppc_object 7284 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object); 7285 if (loc->shndx == ppc_object->opd_shndx()) 7286 { 7287 Address dest_off; 7288 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off); 7289 loc->offset = dest_off; 7290 } 7291 } 7292 } 7293} 7294 7295// FNOFFSET in section SHNDX in OBJECT is the start of a function 7296// compiled with -fsplit-stack. The function calls non-split-stack 7297// code. Change the function to ensure it has enough stack space to 7298// call some random function. 7299 7300template<int size, bool big_endian> 7301void 7302Target_powerpc<size, big_endian>::do_calls_non_split( 7303 Relobj* object, 7304 unsigned int shndx, 7305 section_offset_type fnoffset, 7306 section_size_type fnsize, 7307 const unsigned char* prelocs, 7308 size_t reloc_count, 7309 unsigned char* view, 7310 section_size_type view_size, 7311 std::string* from, 7312 std::string* to) const 7313{ 7314 // 32-bit not supported. 7315 if (size == 32) 7316 { 7317 // warn 7318 Target::do_calls_non_split(object, shndx, fnoffset, fnsize, 7319 prelocs, reloc_count, view, view_size, 7320 from, to); 7321 return; 7322 } 7323 7324 // The function always starts with 7325 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss 7326 // addis %r12,%r1,-allocate@ha 7327 // addi %r12,%r12,-allocate@l 7328 // cmpld %r12,%r0 7329 // but note that the addis or addi may be replaced with a nop 7330 7331 unsigned char *entry = view + fnoffset; 7332 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry); 7333 7334 if ((insn & 0xffff0000) == addis_2_12) 7335 { 7336 /* Skip ELFv2 global entry code. */ 7337 entry += 8; 7338 insn = elfcpp::Swap<32, big_endian>::readval(entry); 7339 } 7340 7341 unsigned char *pinsn = entry; 7342 bool ok = false; 7343 const uint32_t ld_private_ss = 0xe80d8fc0; 7344 if (insn == ld_private_ss) 7345 { 7346 int32_t allocate = 0; 7347 while (1) 7348 { 7349 pinsn += 4; 7350 insn = elfcpp::Swap<32, big_endian>::readval(pinsn); 7351 if ((insn & 0xffff0000) == addis_12_1) 7352 allocate += (insn & 0xffff) << 16; 7353 else if ((insn & 0xffff0000) == addi_12_1 7354 || (insn & 0xffff0000) == addi_12_12) 7355 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000; 7356 else if (insn != nop) 7357 break; 7358 } 7359 if (insn == cmpld_7_12_0 && pinsn == entry + 12) 7360 { 7361 int extra = parameters->options().split_stack_adjust_size(); 7362 allocate -= extra; 7363 if (allocate >= 0 || extra < 0) 7364 { 7365 object->error(_("split-stack stack size overflow at " 7366 "section %u offset %0zx"), 7367 shndx, static_cast<size_t>(fnoffset)); 7368 return; 7369 } 7370 pinsn = entry + 4; 7371 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff); 7372 if (insn != addis_12_1) 7373 { 7374 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn); 7375 pinsn += 4; 7376 insn = addi_12_12 | (allocate & 0xffff); 7377 if (insn != addi_12_12) 7378 { 7379 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn); 7380 pinsn += 4; 7381 } 7382 } 7383 else 7384 { 7385 insn = addi_12_1 | (allocate & 0xffff); 7386 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn); 7387 pinsn += 4; 7388 } 7389 if (pinsn != entry + 12) 7390 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop); 7391 7392 ok = true; 7393 } 7394 } 7395 7396 if (!ok) 7397 { 7398 if (!object->has_no_split_stack()) 7399 object->error(_("failed to match split-stack sequence at " 7400 "section %u offset %0zx"), 7401 shndx, static_cast<size_t>(fnoffset)); 7402 } 7403} 7404 7405// Scan relocations for a section. 7406 7407template<int size, bool big_endian> 7408void 7409Target_powerpc<size, big_endian>::scan_relocs( 7410 Symbol_table* symtab, 7411 Layout* layout, 7412 Sized_relobj_file<size, big_endian>* object, 7413 unsigned int data_shndx, 7414 unsigned int sh_type, 7415 const unsigned char* prelocs, 7416 size_t reloc_count, 7417 Output_section* output_section, 7418 bool needs_special_offset_handling, 7419 size_t local_symbol_count, 7420 const unsigned char* plocal_symbols) 7421{ 7422 typedef Target_powerpc<size, big_endian> Powerpc; 7423 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian> 7424 Classify_reloc; 7425 7426 if (sh_type == elfcpp::SHT_REL) 7427 { 7428 gold_error(_("%s: unsupported REL reloc section"), 7429 object->name().c_str()); 7430 return; 7431 } 7432 7433 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>( 7434 symtab, 7435 layout, 7436 this, 7437 object, 7438 data_shndx, 7439 prelocs, 7440 reloc_count, 7441 output_section, 7442 needs_special_offset_handling, 7443 local_symbol_count, 7444 plocal_symbols); 7445} 7446 7447// Functor class for processing the global symbol table. 7448// Removes symbols defined on discarded opd entries. 7449 7450template<bool big_endian> 7451class Global_symbol_visitor_opd 7452{ 7453 public: 7454 Global_symbol_visitor_opd() 7455 { } 7456 7457 void 7458 operator()(Sized_symbol<64>* sym) 7459 { 7460 if (sym->has_symtab_index() 7461 || sym->source() != Symbol::FROM_OBJECT 7462 || !sym->in_real_elf()) 7463 return; 7464 7465 if (sym->object()->is_dynamic()) 7466 return; 7467 7468 Powerpc_relobj<64, big_endian>* symobj 7469 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object()); 7470 if (symobj->opd_shndx() == 0) 7471 return; 7472 7473 bool is_ordinary; 7474 unsigned int shndx = sym->shndx(&is_ordinary); 7475 if (shndx == symobj->opd_shndx() 7476 && symobj->get_opd_discard(sym->value())) 7477 { 7478 sym->set_undefined(); 7479 sym->set_visibility(elfcpp::STV_DEFAULT); 7480 sym->set_is_defined_in_discarded_section(); 7481 sym->set_symtab_index(-1U); 7482 } 7483 } 7484}; 7485 7486template<int size, bool big_endian> 7487void 7488Target_powerpc<size, big_endian>::define_save_restore_funcs( 7489 Layout* layout, 7490 Symbol_table* symtab) 7491{ 7492 if (size == 64) 7493 { 7494 Output_data_save_res<size, big_endian>* savres 7495 = new Output_data_save_res<size, big_endian>(symtab); 7496 this->savres_section_ = savres; 7497 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS, 7498 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR, 7499 savres, ORDER_TEXT, false); 7500 } 7501} 7502 7503// Sort linker created .got section first (for the header), then input 7504// sections belonging to files using small model code. 7505 7506template<bool big_endian> 7507class Sort_toc_sections 7508{ 7509 public: 7510 bool 7511 operator()(const Output_section::Input_section& is1, 7512 const Output_section::Input_section& is2) const 7513 { 7514 if (!is1.is_input_section() && is2.is_input_section()) 7515 return true; 7516 bool small1 7517 = (is1.is_input_section() 7518 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj()) 7519 ->has_small_toc_reloc())); 7520 bool small2 7521 = (is2.is_input_section() 7522 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj()) 7523 ->has_small_toc_reloc())); 7524 return small1 && !small2; 7525 } 7526}; 7527 7528// Finalize the sections. 7529 7530template<int size, bool big_endian> 7531void 7532Target_powerpc<size, big_endian>::do_finalize_sections( 7533 Layout* layout, 7534 const Input_objects*, 7535 Symbol_table* symtab) 7536{ 7537 if (parameters->doing_static_link()) 7538 { 7539 // At least some versions of glibc elf-init.o have a strong 7540 // reference to __rela_iplt marker syms. A weak ref would be 7541 // better.. 7542 if (this->iplt_ != NULL) 7543 { 7544 Reloc_section* rel = this->iplt_->rel_plt(); 7545 symtab->define_in_output_data("__rela_iplt_start", NULL, 7546 Symbol_table::PREDEFINED, rel, 0, 0, 7547 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, 7548 elfcpp::STV_HIDDEN, 0, false, true); 7549 symtab->define_in_output_data("__rela_iplt_end", NULL, 7550 Symbol_table::PREDEFINED, rel, 0, 0, 7551 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, 7552 elfcpp::STV_HIDDEN, 0, true, true); 7553 } 7554 else 7555 { 7556 symtab->define_as_constant("__rela_iplt_start", NULL, 7557 Symbol_table::PREDEFINED, 0, 0, 7558 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, 7559 elfcpp::STV_HIDDEN, 0, true, false); 7560 symtab->define_as_constant("__rela_iplt_end", NULL, 7561 Symbol_table::PREDEFINED, 0, 0, 7562 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, 7563 elfcpp::STV_HIDDEN, 0, true, false); 7564 } 7565 } 7566 7567 if (size == 64) 7568 { 7569 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor; 7570 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor()); 7571 7572 if (!parameters->options().relocatable()) 7573 { 7574 this->define_save_restore_funcs(layout, symtab); 7575 7576 // Annoyingly, we need to make these sections now whether or 7577 // not we need them. If we delay until do_relax then we 7578 // need to mess with the relaxation machinery checkpointing. 7579 this->got_section(symtab, layout); 7580 this->make_brlt_section(layout); 7581 7582 if (parameters->options().toc_sort()) 7583 { 7584 Output_section* os = this->got_->output_section(); 7585 if (os != NULL && os->input_sections().size() > 1) 7586 std::stable_sort(os->input_sections().begin(), 7587 os->input_sections().end(), 7588 Sort_toc_sections<big_endian>()); 7589 } 7590 } 7591 } 7592 7593 // Fill in some more dynamic tags. 7594 Output_data_dynamic* odyn = layout->dynamic_data(); 7595 if (odyn != NULL) 7596 { 7597 const Reloc_section* rel_plt = (this->plt_ == NULL 7598 ? NULL 7599 : this->plt_->rel_plt()); 7600 layout->add_target_dynamic_tags(false, this->plt_, rel_plt, 7601 this->rela_dyn_, true, size == 32); 7602 7603 if (size == 32) 7604 { 7605 if (this->got_ != NULL) 7606 { 7607 this->got_->finalize_data_size(); 7608 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT, 7609 this->got_, this->got_->g_o_t()); 7610 } 7611 } 7612 else 7613 { 7614 if (this->glink_ != NULL) 7615 { 7616 this->glink_->finalize_data_size(); 7617 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK, 7618 this->glink_, 7619 (this->glink_->pltresolve_size 7620 - 32)); 7621 } 7622 } 7623 } 7624 7625 // Emit any relocs we saved in an attempt to avoid generating COPY 7626 // relocs. 7627 if (this->copy_relocs_.any_saved_relocs()) 7628 this->copy_relocs_.emit(this->rela_dyn_section(layout)); 7629} 7630 7631// Emit any saved relocs, and mark toc entries using any of these 7632// relocs as not optimizable. 7633 7634template<int sh_type, int size, bool big_endian> 7635void 7636Powerpc_copy_relocs<sh_type, size, big_endian>::emit( 7637 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section) 7638{ 7639 if (size == 64 7640 && parameters->options().toc_optimize()) 7641 { 7642 for (typename Copy_relocs<sh_type, size, big_endian>:: 7643 Copy_reloc_entries::iterator p = this->entries_.begin(); 7644 p != this->entries_.end(); 7645 ++p) 7646 { 7647 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry& 7648 entry = *p; 7649 7650 // If the symbol is no longer defined in a dynamic object, 7651 // then we emitted a COPY relocation. If it is still 7652 // dynamic then we'll need dynamic relocations and thus 7653 // can't optimize toc entries. 7654 if (entry.sym_->is_from_dynobj()) 7655 { 7656 Powerpc_relobj<size, big_endian>* ppc_object 7657 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_); 7658 if (entry.shndx_ == ppc_object->toc_shndx()) 7659 ppc_object->set_no_toc_opt(entry.address_); 7660 } 7661 } 7662 } 7663 7664 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section); 7665} 7666 7667// Return the value to use for a branch relocation. 7668 7669template<int size, bool big_endian> 7670bool 7671Target_powerpc<size, big_endian>::symval_for_branch( 7672 const Symbol_table* symtab, 7673 const Sized_symbol<size>* gsym, 7674 Powerpc_relobj<size, big_endian>* object, 7675 Address *value, 7676 unsigned int *dest_shndx) 7677{ 7678 if (size == 32 || this->abiversion() >= 2) 7679 gold_unreachable(); 7680 *dest_shndx = 0; 7681 7682 // If the symbol is defined in an opd section, ie. is a function 7683 // descriptor, use the function descriptor code entry address 7684 Powerpc_relobj<size, big_endian>* symobj = object; 7685 if (gsym != NULL 7686 && (gsym->source() != Symbol::FROM_OBJECT 7687 || gsym->object()->is_dynamic())) 7688 return true; 7689 if (gsym != NULL) 7690 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object()); 7691 unsigned int shndx = symobj->opd_shndx(); 7692 if (shndx == 0) 7693 return true; 7694 Address opd_addr = symobj->get_output_section_offset(shndx); 7695 if (opd_addr == invalid_address) 7696 return true; 7697 opd_addr += symobj->output_section_address(shndx); 7698 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx)) 7699 { 7700 Address sec_off; 7701 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off); 7702 if (symtab->is_section_folded(symobj, *dest_shndx)) 7703 { 7704 Section_id folded 7705 = symtab->icf()->get_folded_section(symobj, *dest_shndx); 7706 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first); 7707 *dest_shndx = folded.second; 7708 } 7709 Address sec_addr = symobj->get_output_section_offset(*dest_shndx); 7710 if (sec_addr == invalid_address) 7711 return false; 7712 7713 sec_addr += symobj->output_section(*dest_shndx)->address(); 7714 *value = sec_addr + sec_off; 7715 } 7716 return true; 7717} 7718 7719// Perform a relocation. 7720 7721template<int size, bool big_endian> 7722inline bool 7723Target_powerpc<size, big_endian>::Relocate::relocate( 7724 const Relocate_info<size, big_endian>* relinfo, 7725 unsigned int, 7726 Target_powerpc* target, 7727 Output_section* os, 7728 size_t relnum, 7729 const unsigned char* preloc, 7730 const Sized_symbol<size>* gsym, 7731 const Symbol_value<size>* psymval, 7732 unsigned char* view, 7733 Address address, 7734 section_size_type view_size) 7735{ 7736 if (view == NULL) 7737 return true; 7738 7739 const elfcpp::Rela<size, big_endian> rela(preloc); 7740 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info()); 7741 switch (this->maybe_skip_tls_get_addr_call(r_type, gsym)) 7742 { 7743 case Track_tls::NOT_EXPECTED: 7744 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 7745 _("__tls_get_addr call lacks marker reloc")); 7746 break; 7747 case Track_tls::EXPECTED: 7748 // We have already complained. 7749 break; 7750 case Track_tls::SKIP: 7751 return true; 7752 case Track_tls::NORMAL: 7753 break; 7754 } 7755 7756 typedef Powerpc_relocate_functions<size, big_endian> Reloc; 7757 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn; 7758 typedef typename elfcpp::Rela<size, big_endian> Reltype; 7759 // Offset from start of insn to d-field reloc. 7760 const int d_offset = big_endian ? 2 : 0; 7761 7762 Powerpc_relobj<size, big_endian>* const object 7763 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object); 7764 Address value = 0; 7765 bool has_stub_value = false; 7766 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); 7767 if ((gsym != NULL 7768 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target)) 7769 : object->local_has_plt_offset(r_sym)) 7770 && (!psymval->is_ifunc_symbol() 7771 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false))) 7772 { 7773 if (size == 64 7774 && gsym != NULL 7775 && target->abiversion() >= 2 7776 && !parameters->options().output_is_position_independent() 7777 && !is_branch_reloc(r_type)) 7778 { 7779 Address off = target->glink_section()->find_global_entry(gsym); 7780 if (off != invalid_address) 7781 { 7782 value = target->glink_section()->global_entry_address() + off; 7783 has_stub_value = true; 7784 } 7785 } 7786 else 7787 { 7788 Stub_table<size, big_endian>* stub_table 7789 = object->stub_table(relinfo->data_shndx); 7790 if (stub_table == NULL) 7791 { 7792 // This is a ref from a data section to an ifunc symbol. 7793 if (target->stub_tables().size() != 0) 7794 stub_table = target->stub_tables()[0]; 7795 } 7796 if (stub_table != NULL) 7797 { 7798 Address off; 7799 if (gsym != NULL) 7800 off = stub_table->find_plt_call_entry(object, gsym, r_type, 7801 rela.get_r_addend()); 7802 else 7803 off = stub_table->find_plt_call_entry(object, r_sym, r_type, 7804 rela.get_r_addend()); 7805 if (off != invalid_address) 7806 { 7807 value = stub_table->stub_address() + off; 7808 has_stub_value = true; 7809 } 7810 } 7811 } 7812 // We don't care too much about bogus debug references to 7813 // non-local functions, but otherwise there had better be a plt 7814 // call stub or global entry stub as appropriate. 7815 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC)); 7816 } 7817 7818 if (r_type == elfcpp::R_POWERPC_GOT16 7819 || r_type == elfcpp::R_POWERPC_GOT16_LO 7820 || r_type == elfcpp::R_POWERPC_GOT16_HI 7821 || r_type == elfcpp::R_POWERPC_GOT16_HA 7822 || r_type == elfcpp::R_PPC64_GOT16_DS 7823 || r_type == elfcpp::R_PPC64_GOT16_LO_DS) 7824 { 7825 if (gsym != NULL) 7826 { 7827 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD)); 7828 value = gsym->got_offset(GOT_TYPE_STANDARD); 7829 } 7830 else 7831 { 7832 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD)); 7833 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD); 7834 } 7835 value -= target->got_section()->got_base_offset(object); 7836 } 7837 else if (r_type == elfcpp::R_PPC64_TOC) 7838 { 7839 value = (target->got_section()->output_section()->address() 7840 + object->toc_base_offset()); 7841 } 7842 else if (gsym != NULL 7843 && (r_type == elfcpp::R_POWERPC_REL24 7844 || r_type == elfcpp::R_PPC_PLTREL24) 7845 && has_stub_value) 7846 { 7847 if (size == 64) 7848 { 7849 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; 7850 Valtype* wv = reinterpret_cast<Valtype*>(view); 7851 bool can_plt_call = false; 7852 if (rela.get_r_offset() + 8 <= view_size) 7853 { 7854 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv); 7855 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1); 7856 if ((insn & 1) != 0 7857 && (insn2 == nop 7858 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31)) 7859 { 7860 elfcpp::Swap<32, big_endian>:: 7861 writeval(wv + 1, ld_2_1 + target->stk_toc()); 7862 can_plt_call = true; 7863 } 7864 } 7865 if (!can_plt_call) 7866 { 7867 // If we don't have a branch and link followed by a nop, 7868 // we can't go via the plt because there is no place to 7869 // put a toc restoring instruction. 7870 // Unless we know we won't be returning. 7871 if (strcmp(gsym->name(), "__libc_start_main") == 0) 7872 can_plt_call = true; 7873 } 7874 if (!can_plt_call) 7875 { 7876 // g++ as of 20130507 emits self-calls without a 7877 // following nop. This is arguably wrong since we have 7878 // conflicting information. On the one hand a global 7879 // symbol and on the other a local call sequence, but 7880 // don't error for this special case. 7881 // It isn't possible to cheaply verify we have exactly 7882 // such a call. Allow all calls to the same section. 7883 bool ok = false; 7884 Address code = value; 7885 if (gsym->source() == Symbol::FROM_OBJECT 7886 && gsym->object() == object) 7887 { 7888 unsigned int dest_shndx = 0; 7889 if (target->abiversion() < 2) 7890 { 7891 Address addend = rela.get_r_addend(); 7892 code = psymval->value(object, addend); 7893 target->symval_for_branch(relinfo->symtab, gsym, object, 7894 &code, &dest_shndx); 7895 } 7896 bool is_ordinary; 7897 if (dest_shndx == 0) 7898 dest_shndx = gsym->shndx(&is_ordinary); 7899 ok = dest_shndx == relinfo->data_shndx; 7900 } 7901 if (!ok) 7902 { 7903 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 7904 _("call lacks nop, can't restore toc; " 7905 "recompile with -fPIC")); 7906 value = code; 7907 } 7908 } 7909 } 7910 } 7911 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16 7912 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO 7913 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI 7914 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA) 7915 { 7916 // First instruction of a global dynamic sequence, arg setup insn. 7917 const bool final = gsym == NULL || gsym->final_value_is_known(); 7918 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final); 7919 enum Got_type got_type = GOT_TYPE_STANDARD; 7920 if (tls_type == tls::TLSOPT_NONE) 7921 got_type = GOT_TYPE_TLSGD; 7922 else if (tls_type == tls::TLSOPT_TO_IE) 7923 got_type = GOT_TYPE_TPREL; 7924 if (got_type != GOT_TYPE_STANDARD) 7925 { 7926 if (gsym != NULL) 7927 { 7928 gold_assert(gsym->has_got_offset(got_type)); 7929 value = gsym->got_offset(got_type); 7930 } 7931 else 7932 { 7933 gold_assert(object->local_has_got_offset(r_sym, got_type)); 7934 value = object->local_got_offset(r_sym, got_type); 7935 } 7936 value -= target->got_section()->got_base_offset(object); 7937 } 7938 if (tls_type == tls::TLSOPT_TO_IE) 7939 { 7940 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16 7941 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO) 7942 { 7943 Insn* iview = reinterpret_cast<Insn*>(view - d_offset); 7944 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview); 7945 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi 7946 if (size == 32) 7947 insn |= 32 << 26; // lwz 7948 else 7949 insn |= 58 << 26; // ld 7950 elfcpp::Swap<32, big_endian>::writeval(iview, insn); 7951 } 7952 r_type += (elfcpp::R_POWERPC_GOT_TPREL16 7953 - elfcpp::R_POWERPC_GOT_TLSGD16); 7954 } 7955 else if (tls_type == tls::TLSOPT_TO_LE) 7956 { 7957 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16 7958 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO) 7959 { 7960 Insn* iview = reinterpret_cast<Insn*>(view - d_offset); 7961 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview); 7962 insn &= (1 << 26) - (1 << 21); // extract rt 7963 if (size == 32) 7964 insn |= addis_0_2; 7965 else 7966 insn |= addis_0_13; 7967 elfcpp::Swap<32, big_endian>::writeval(iview, insn); 7968 r_type = elfcpp::R_POWERPC_TPREL16_HA; 7969 value = psymval->value(object, rela.get_r_addend()); 7970 } 7971 else 7972 { 7973 Insn* iview = reinterpret_cast<Insn*>(view - d_offset); 7974 Insn insn = nop; 7975 elfcpp::Swap<32, big_endian>::writeval(iview, insn); 7976 r_type = elfcpp::R_POWERPC_NONE; 7977 } 7978 } 7979 } 7980 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16 7981 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO 7982 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI 7983 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA) 7984 { 7985 // First instruction of a local dynamic sequence, arg setup insn. 7986 const tls::Tls_optimization tls_type = target->optimize_tls_ld(); 7987 if (tls_type == tls::TLSOPT_NONE) 7988 { 7989 value = target->tlsld_got_offset(); 7990 value -= target->got_section()->got_base_offset(object); 7991 } 7992 else 7993 { 7994 gold_assert(tls_type == tls::TLSOPT_TO_LE); 7995 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16 7996 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO) 7997 { 7998 Insn* iview = reinterpret_cast<Insn*>(view - d_offset); 7999 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview); 8000 insn &= (1 << 26) - (1 << 21); // extract rt 8001 if (size == 32) 8002 insn |= addis_0_2; 8003 else 8004 insn |= addis_0_13; 8005 elfcpp::Swap<32, big_endian>::writeval(iview, insn); 8006 r_type = elfcpp::R_POWERPC_TPREL16_HA; 8007 value = dtp_offset; 8008 } 8009 else 8010 { 8011 Insn* iview = reinterpret_cast<Insn*>(view - d_offset); 8012 Insn insn = nop; 8013 elfcpp::Swap<32, big_endian>::writeval(iview, insn); 8014 r_type = elfcpp::R_POWERPC_NONE; 8015 } 8016 } 8017 } 8018 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16 8019 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO 8020 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI 8021 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA) 8022 { 8023 // Accesses relative to a local dynamic sequence address, 8024 // no optimisation here. 8025 if (gsym != NULL) 8026 { 8027 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL)); 8028 value = gsym->got_offset(GOT_TYPE_DTPREL); 8029 } 8030 else 8031 { 8032 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL)); 8033 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL); 8034 } 8035 value -= target->got_section()->got_base_offset(object); 8036 } 8037 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16 8038 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO 8039 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI 8040 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA) 8041 { 8042 // First instruction of initial exec sequence. 8043 const bool final = gsym == NULL || gsym->final_value_is_known(); 8044 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final); 8045 if (tls_type == tls::TLSOPT_NONE) 8046 { 8047 if (gsym != NULL) 8048 { 8049 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL)); 8050 value = gsym->got_offset(GOT_TYPE_TPREL); 8051 } 8052 else 8053 { 8054 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL)); 8055 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL); 8056 } 8057 value -= target->got_section()->got_base_offset(object); 8058 } 8059 else 8060 { 8061 gold_assert(tls_type == tls::TLSOPT_TO_LE); 8062 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16 8063 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO) 8064 { 8065 Insn* iview = reinterpret_cast<Insn*>(view - d_offset); 8066 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview); 8067 insn &= (1 << 26) - (1 << 21); // extract rt from ld 8068 if (size == 32) 8069 insn |= addis_0_2; 8070 else 8071 insn |= addis_0_13; 8072 elfcpp::Swap<32, big_endian>::writeval(iview, insn); 8073 r_type = elfcpp::R_POWERPC_TPREL16_HA; 8074 value = psymval->value(object, rela.get_r_addend()); 8075 } 8076 else 8077 { 8078 Insn* iview = reinterpret_cast<Insn*>(view - d_offset); 8079 Insn insn = nop; 8080 elfcpp::Swap<32, big_endian>::writeval(iview, insn); 8081 r_type = elfcpp::R_POWERPC_NONE; 8082 } 8083 } 8084 } 8085 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD) 8086 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD)) 8087 { 8088 // Second instruction of a global dynamic sequence, 8089 // the __tls_get_addr call 8090 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset()); 8091 const bool final = gsym == NULL || gsym->final_value_is_known(); 8092 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final); 8093 if (tls_type != tls::TLSOPT_NONE) 8094 { 8095 if (tls_type == tls::TLSOPT_TO_IE) 8096 { 8097 Insn* iview = reinterpret_cast<Insn*>(view); 8098 Insn insn = add_3_3_13; 8099 if (size == 32) 8100 insn = add_3_3_2; 8101 elfcpp::Swap<32, big_endian>::writeval(iview, insn); 8102 r_type = elfcpp::R_POWERPC_NONE; 8103 } 8104 else 8105 { 8106 Insn* iview = reinterpret_cast<Insn*>(view); 8107 Insn insn = addi_3_3; 8108 elfcpp::Swap<32, big_endian>::writeval(iview, insn); 8109 r_type = elfcpp::R_POWERPC_TPREL16_LO; 8110 view += d_offset; 8111 value = psymval->value(object, rela.get_r_addend()); 8112 } 8113 this->skip_next_tls_get_addr_call(); 8114 } 8115 } 8116 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD) 8117 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD)) 8118 { 8119 // Second instruction of a local dynamic sequence, 8120 // the __tls_get_addr call 8121 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset()); 8122 const tls::Tls_optimization tls_type = target->optimize_tls_ld(); 8123 if (tls_type == tls::TLSOPT_TO_LE) 8124 { 8125 Insn* iview = reinterpret_cast<Insn*>(view); 8126 Insn insn = addi_3_3; 8127 elfcpp::Swap<32, big_endian>::writeval(iview, insn); 8128 this->skip_next_tls_get_addr_call(); 8129 r_type = elfcpp::R_POWERPC_TPREL16_LO; 8130 view += d_offset; 8131 value = dtp_offset; 8132 } 8133 } 8134 else if (r_type == elfcpp::R_POWERPC_TLS) 8135 { 8136 // Second instruction of an initial exec sequence 8137 const bool final = gsym == NULL || gsym->final_value_is_known(); 8138 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final); 8139 if (tls_type == tls::TLSOPT_TO_LE) 8140 { 8141 Insn* iview = reinterpret_cast<Insn*>(view); 8142 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview); 8143 unsigned int reg = size == 32 ? 2 : 13; 8144 insn = at_tls_transform(insn, reg); 8145 gold_assert(insn != 0); 8146 elfcpp::Swap<32, big_endian>::writeval(iview, insn); 8147 r_type = elfcpp::R_POWERPC_TPREL16_LO; 8148 view += d_offset; 8149 value = psymval->value(object, rela.get_r_addend()); 8150 } 8151 } 8152 else if (!has_stub_value) 8153 { 8154 Address addend = 0; 8155 if (!(size == 32 && r_type == elfcpp::R_PPC_PLTREL24)) 8156 addend = rela.get_r_addend(); 8157 value = psymval->value(object, addend); 8158 if (size == 64 && is_branch_reloc(r_type)) 8159 { 8160 if (target->abiversion() >= 2) 8161 { 8162 if (gsym != NULL) 8163 value += object->ppc64_local_entry_offset(gsym); 8164 else 8165 value += object->ppc64_local_entry_offset(r_sym); 8166 } 8167 else 8168 { 8169 unsigned int dest_shndx; 8170 target->symval_for_branch(relinfo->symtab, gsym, object, 8171 &value, &dest_shndx); 8172 } 8173 } 8174 Address max_branch_offset = max_branch_delta(r_type); 8175 if (max_branch_offset != 0 8176 && value - address + max_branch_offset >= 2 * max_branch_offset) 8177 { 8178 Stub_table<size, big_endian>* stub_table 8179 = object->stub_table(relinfo->data_shndx); 8180 if (stub_table != NULL) 8181 { 8182 Address off = stub_table->find_long_branch_entry(object, value); 8183 if (off != invalid_address) 8184 { 8185 value = (stub_table->stub_address() + stub_table->plt_size() 8186 + off); 8187 has_stub_value = true; 8188 } 8189 } 8190 } 8191 } 8192 8193 switch (r_type) 8194 { 8195 case elfcpp::R_PPC64_REL64: 8196 case elfcpp::R_POWERPC_REL32: 8197 case elfcpp::R_POWERPC_REL24: 8198 case elfcpp::R_PPC_PLTREL24: 8199 case elfcpp::R_PPC_LOCAL24PC: 8200 case elfcpp::R_POWERPC_REL16: 8201 case elfcpp::R_POWERPC_REL16_LO: 8202 case elfcpp::R_POWERPC_REL16_HI: 8203 case elfcpp::R_POWERPC_REL16_HA: 8204 case elfcpp::R_POWERPC_REL16DX_HA: 8205 case elfcpp::R_POWERPC_REL14: 8206 case elfcpp::R_POWERPC_REL14_BRTAKEN: 8207 case elfcpp::R_POWERPC_REL14_BRNTAKEN: 8208 value -= address; 8209 break; 8210 8211 case elfcpp::R_PPC64_TOC16: 8212 case elfcpp::R_PPC64_TOC16_LO: 8213 case elfcpp::R_PPC64_TOC16_HI: 8214 case elfcpp::R_PPC64_TOC16_HA: 8215 case elfcpp::R_PPC64_TOC16_DS: 8216 case elfcpp::R_PPC64_TOC16_LO_DS: 8217 // Subtract the TOC base address. 8218 value -= (target->got_section()->output_section()->address() 8219 + object->toc_base_offset()); 8220 break; 8221 8222 case elfcpp::R_POWERPC_SECTOFF: 8223 case elfcpp::R_POWERPC_SECTOFF_LO: 8224 case elfcpp::R_POWERPC_SECTOFF_HI: 8225 case elfcpp::R_POWERPC_SECTOFF_HA: 8226 case elfcpp::R_PPC64_SECTOFF_DS: 8227 case elfcpp::R_PPC64_SECTOFF_LO_DS: 8228 if (os != NULL) 8229 value -= os->address(); 8230 break; 8231 8232 case elfcpp::R_PPC64_TPREL16_DS: 8233 case elfcpp::R_PPC64_TPREL16_LO_DS: 8234 case elfcpp::R_PPC64_TPREL16_HIGH: 8235 case elfcpp::R_PPC64_TPREL16_HIGHA: 8236 if (size != 64) 8237 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI 8238 break; 8239 // Fall through. 8240 case elfcpp::R_POWERPC_TPREL16: 8241 case elfcpp::R_POWERPC_TPREL16_LO: 8242 case elfcpp::R_POWERPC_TPREL16_HI: 8243 case elfcpp::R_POWERPC_TPREL16_HA: 8244 case elfcpp::R_POWERPC_TPREL: 8245 case elfcpp::R_PPC64_TPREL16_HIGHER: 8246 case elfcpp::R_PPC64_TPREL16_HIGHERA: 8247 case elfcpp::R_PPC64_TPREL16_HIGHEST: 8248 case elfcpp::R_PPC64_TPREL16_HIGHESTA: 8249 // tls symbol values are relative to tls_segment()->vaddr() 8250 value -= tp_offset; 8251 break; 8252 8253 case elfcpp::R_PPC64_DTPREL16_DS: 8254 case elfcpp::R_PPC64_DTPREL16_LO_DS: 8255 case elfcpp::R_PPC64_DTPREL16_HIGHER: 8256 case elfcpp::R_PPC64_DTPREL16_HIGHERA: 8257 case elfcpp::R_PPC64_DTPREL16_HIGHEST: 8258 case elfcpp::R_PPC64_DTPREL16_HIGHESTA: 8259 if (size != 64) 8260 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO 8261 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16 8262 break; 8263 // Fall through. 8264 case elfcpp::R_POWERPC_DTPREL16: 8265 case elfcpp::R_POWERPC_DTPREL16_LO: 8266 case elfcpp::R_POWERPC_DTPREL16_HI: 8267 case elfcpp::R_POWERPC_DTPREL16_HA: 8268 case elfcpp::R_POWERPC_DTPREL: 8269 case elfcpp::R_PPC64_DTPREL16_HIGH: 8270 case elfcpp::R_PPC64_DTPREL16_HIGHA: 8271 // tls symbol values are relative to tls_segment()->vaddr() 8272 value -= dtp_offset; 8273 break; 8274 8275 case elfcpp::R_PPC64_ADDR64_LOCAL: 8276 if (gsym != NULL) 8277 value += object->ppc64_local_entry_offset(gsym); 8278 else 8279 value += object->ppc64_local_entry_offset(r_sym); 8280 break; 8281 8282 default: 8283 break; 8284 } 8285 8286 Insn branch_bit = 0; 8287 switch (r_type) 8288 { 8289 case elfcpp::R_POWERPC_ADDR14_BRTAKEN: 8290 case elfcpp::R_POWERPC_REL14_BRTAKEN: 8291 branch_bit = 1 << 21; 8292 // Fall through. 8293 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN: 8294 case elfcpp::R_POWERPC_REL14_BRNTAKEN: 8295 { 8296 Insn* iview = reinterpret_cast<Insn*>(view); 8297 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview); 8298 insn &= ~(1 << 21); 8299 insn |= branch_bit; 8300 if (this->is_isa_v2) 8301 { 8302 // Set 'a' bit. This is 0b00010 in BO field for branch 8303 // on CR(BI) insns (BO == 001at or 011at), and 0b01000 8304 // for branch on CTR insns (BO == 1a00t or 1a01t). 8305 if ((insn & (0x14 << 21)) == (0x04 << 21)) 8306 insn |= 0x02 << 21; 8307 else if ((insn & (0x14 << 21)) == (0x10 << 21)) 8308 insn |= 0x08 << 21; 8309 else 8310 break; 8311 } 8312 else 8313 { 8314 // Invert 'y' bit if not the default. 8315 if (static_cast<Signed_address>(value) < 0) 8316 insn ^= 1 << 21; 8317 } 8318 elfcpp::Swap<32, big_endian>::writeval(iview, insn); 8319 } 8320 break; 8321 8322 default: 8323 break; 8324 } 8325 8326 if (size == 64) 8327 { 8328 switch (r_type) 8329 { 8330 default: 8331 break; 8332 8333 // Multi-instruction sequences that access the GOT/TOC can 8334 // be optimized, eg. 8335 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra); 8336 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l; 8337 // and 8338 // addis ra,r2,0; addi rb,ra,x@toc@l; 8339 // to nop; addi rb,r2,x@toc; 8340 // FIXME: the @got sequence shown above is not yet 8341 // optimized. Note that gcc as of 2017-01-07 doesn't use 8342 // the ELF @got relocs except for TLS, instead using the 8343 // PowerOpen variant of a compiler managed GOT (called TOC). 8344 // The PowerOpen TOC sequence equivalent to the first 8345 // example is optimized. 8346 case elfcpp::R_POWERPC_GOT_TLSLD16_HA: 8347 case elfcpp::R_POWERPC_GOT_TLSGD16_HA: 8348 case elfcpp::R_POWERPC_GOT_TPREL16_HA: 8349 case elfcpp::R_POWERPC_GOT_DTPREL16_HA: 8350 case elfcpp::R_POWERPC_GOT16_HA: 8351 case elfcpp::R_PPC64_TOC16_HA: 8352 if (parameters->options().toc_optimize()) 8353 { 8354 Insn* iview = reinterpret_cast<Insn*>(view - d_offset); 8355 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview); 8356 if (r_type == elfcpp::R_PPC64_TOC16_HA 8357 && object->make_toc_relative(target, &value)) 8358 { 8359 gold_assert((insn & ((0x3f << 26) | 0x1f << 16)) 8360 == ((15u << 26) | (2 << 16))); 8361 } 8362 if (((insn & ((0x3f << 26) | 0x1f << 16)) 8363 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */) 8364 && value + 0x8000 < 0x10000) 8365 { 8366 elfcpp::Swap<32, big_endian>::writeval(iview, nop); 8367 return true; 8368 } 8369 } 8370 break; 8371 8372 case elfcpp::R_POWERPC_GOT_TLSLD16_LO: 8373 case elfcpp::R_POWERPC_GOT_TLSGD16_LO: 8374 case elfcpp::R_POWERPC_GOT_TPREL16_LO: 8375 case elfcpp::R_POWERPC_GOT_DTPREL16_LO: 8376 case elfcpp::R_POWERPC_GOT16_LO: 8377 case elfcpp::R_PPC64_GOT16_LO_DS: 8378 case elfcpp::R_PPC64_TOC16_LO: 8379 case elfcpp::R_PPC64_TOC16_LO_DS: 8380 if (parameters->options().toc_optimize()) 8381 { 8382 Insn* iview = reinterpret_cast<Insn*>(view - d_offset); 8383 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview); 8384 bool changed = false; 8385 if (r_type == elfcpp::R_PPC64_TOC16_LO_DS 8386 && object->make_toc_relative(target, &value)) 8387 { 8388 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */); 8389 insn ^= (14u << 26) ^ (58u << 26); 8390 r_type = elfcpp::R_PPC64_TOC16_LO; 8391 changed = true; 8392 } 8393 if (ok_lo_toc_insn(insn, r_type) 8394 && value + 0x8000 < 0x10000) 8395 { 8396 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */) 8397 { 8398 // Transform addic to addi when we change reg. 8399 insn &= ~((0x3f << 26) | (0x1f << 16)); 8400 insn |= (14u << 26) | (2 << 16); 8401 } 8402 else 8403 { 8404 insn &= ~(0x1f << 16); 8405 insn |= 2 << 16; 8406 } 8407 changed = true; 8408 } 8409 if (changed) 8410 elfcpp::Swap<32, big_endian>::writeval(iview, insn); 8411 } 8412 break; 8413 8414 case elfcpp::R_PPC64_ENTRY: 8415 value = (target->got_section()->output_section()->address() 8416 + object->toc_base_offset()); 8417 if (value + 0x80008000 <= 0xffffffff 8418 && !parameters->options().output_is_position_independent()) 8419 { 8420 Insn* iview = reinterpret_cast<Insn*>(view); 8421 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview); 8422 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1); 8423 8424 if ((insn1 & ~0xfffc) == ld_2_12 8425 && insn2 == add_2_2_12) 8426 { 8427 insn1 = lis_2 + ha(value); 8428 elfcpp::Swap<32, big_endian>::writeval(iview, insn1); 8429 insn2 = addi_2_2 + l(value); 8430 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2); 8431 return true; 8432 } 8433 } 8434 else 8435 { 8436 value -= address; 8437 if (value + 0x80008000 <= 0xffffffff) 8438 { 8439 Insn* iview = reinterpret_cast<Insn*>(view); 8440 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview); 8441 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1); 8442 8443 if ((insn1 & ~0xfffc) == ld_2_12 8444 && insn2 == add_2_2_12) 8445 { 8446 insn1 = addis_2_12 + ha(value); 8447 elfcpp::Swap<32, big_endian>::writeval(iview, insn1); 8448 insn2 = addi_2_2 + l(value); 8449 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2); 8450 return true; 8451 } 8452 } 8453 } 8454 break; 8455 8456 case elfcpp::R_POWERPC_REL16_LO: 8457 // If we are generating a non-PIC executable, edit 8458 // 0: addis 2,12,.TOC.-0b@ha 8459 // addi 2,2,.TOC.-0b@l 8460 // used by ELFv2 global entry points to set up r2, to 8461 // lis 2,.TOC.@ha 8462 // addi 2,2,.TOC.@l 8463 // if .TOC. is in range. */ 8464 if (value + address - 4 + 0x80008000 <= 0xffffffff 8465 && relnum != 0 8466 && preloc != NULL 8467 && target->abiversion() >= 2 8468 && !parameters->options().output_is_position_independent() 8469 && rela.get_r_addend() == d_offset + 4 8470 && gsym != NULL 8471 && strcmp(gsym->name(), ".TOC.") == 0) 8472 { 8473 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size; 8474 Reltype prev_rela(preloc - reloc_size); 8475 if ((prev_rela.get_r_info() 8476 == elfcpp::elf_r_info<size>(r_sym, 8477 elfcpp::R_POWERPC_REL16_HA)) 8478 && prev_rela.get_r_offset() + 4 == rela.get_r_offset() 8479 && prev_rela.get_r_addend() + 4 == rela.get_r_addend()) 8480 { 8481 Insn* iview = reinterpret_cast<Insn*>(view - d_offset); 8482 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1); 8483 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview); 8484 8485 if ((insn1 & 0xffff0000) == addis_2_12 8486 && (insn2 & 0xffff0000) == addi_2_2) 8487 { 8488 insn1 = lis_2 + ha(value + address - 4); 8489 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1); 8490 insn2 = addi_2_2 + l(value + address - 4); 8491 elfcpp::Swap<32, big_endian>::writeval(iview, insn2); 8492 if (relinfo->rr) 8493 { 8494 relinfo->rr->set_strategy(relnum - 1, 8495 Relocatable_relocs::RELOC_SPECIAL); 8496 relinfo->rr->set_strategy(relnum, 8497 Relocatable_relocs::RELOC_SPECIAL); 8498 } 8499 return true; 8500 } 8501 } 8502 } 8503 break; 8504 } 8505 } 8506 8507 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE; 8508 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr); 8509 switch (r_type) 8510 { 8511 case elfcpp::R_POWERPC_ADDR32: 8512 case elfcpp::R_POWERPC_UADDR32: 8513 if (size == 64) 8514 overflow = Reloc::CHECK_BITFIELD; 8515 break; 8516 8517 case elfcpp::R_POWERPC_REL32: 8518 case elfcpp::R_POWERPC_REL16DX_HA: 8519 if (size == 64) 8520 overflow = Reloc::CHECK_SIGNED; 8521 break; 8522 8523 case elfcpp::R_POWERPC_UADDR16: 8524 overflow = Reloc::CHECK_BITFIELD; 8525 break; 8526 8527 case elfcpp::R_POWERPC_ADDR16: 8528 // We really should have three separate relocations, 8529 // one for 16-bit data, one for insns with 16-bit signed fields, 8530 // and one for insns with 16-bit unsigned fields. 8531 overflow = Reloc::CHECK_BITFIELD; 8532 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0) 8533 overflow = Reloc::CHECK_LOW_INSN; 8534 break; 8535 8536 case elfcpp::R_POWERPC_ADDR16_HI: 8537 case elfcpp::R_POWERPC_ADDR16_HA: 8538 case elfcpp::R_POWERPC_GOT16_HI: 8539 case elfcpp::R_POWERPC_GOT16_HA: 8540 case elfcpp::R_POWERPC_PLT16_HI: 8541 case elfcpp::R_POWERPC_PLT16_HA: 8542 case elfcpp::R_POWERPC_SECTOFF_HI: 8543 case elfcpp::R_POWERPC_SECTOFF_HA: 8544 case elfcpp::R_PPC64_TOC16_HI: 8545 case elfcpp::R_PPC64_TOC16_HA: 8546 case elfcpp::R_PPC64_PLTGOT16_HI: 8547 case elfcpp::R_PPC64_PLTGOT16_HA: 8548 case elfcpp::R_POWERPC_TPREL16_HI: 8549 case elfcpp::R_POWERPC_TPREL16_HA: 8550 case elfcpp::R_POWERPC_DTPREL16_HI: 8551 case elfcpp::R_POWERPC_DTPREL16_HA: 8552 case elfcpp::R_POWERPC_GOT_TLSGD16_HI: 8553 case elfcpp::R_POWERPC_GOT_TLSGD16_HA: 8554 case elfcpp::R_POWERPC_GOT_TLSLD16_HI: 8555 case elfcpp::R_POWERPC_GOT_TLSLD16_HA: 8556 case elfcpp::R_POWERPC_GOT_TPREL16_HI: 8557 case elfcpp::R_POWERPC_GOT_TPREL16_HA: 8558 case elfcpp::R_POWERPC_GOT_DTPREL16_HI: 8559 case elfcpp::R_POWERPC_GOT_DTPREL16_HA: 8560 case elfcpp::R_POWERPC_REL16_HI: 8561 case elfcpp::R_POWERPC_REL16_HA: 8562 if (size != 32) 8563 overflow = Reloc::CHECK_HIGH_INSN; 8564 break; 8565 8566 case elfcpp::R_POWERPC_REL16: 8567 case elfcpp::R_PPC64_TOC16: 8568 case elfcpp::R_POWERPC_GOT16: 8569 case elfcpp::R_POWERPC_SECTOFF: 8570 case elfcpp::R_POWERPC_TPREL16: 8571 case elfcpp::R_POWERPC_DTPREL16: 8572 case elfcpp::R_POWERPC_GOT_TLSGD16: 8573 case elfcpp::R_POWERPC_GOT_TLSLD16: 8574 case elfcpp::R_POWERPC_GOT_TPREL16: 8575 case elfcpp::R_POWERPC_GOT_DTPREL16: 8576 overflow = Reloc::CHECK_LOW_INSN; 8577 break; 8578 8579 case elfcpp::R_POWERPC_ADDR24: 8580 case elfcpp::R_POWERPC_ADDR14: 8581 case elfcpp::R_POWERPC_ADDR14_BRTAKEN: 8582 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN: 8583 case elfcpp::R_PPC64_ADDR16_DS: 8584 case elfcpp::R_POWERPC_REL24: 8585 case elfcpp::R_PPC_PLTREL24: 8586 case elfcpp::R_PPC_LOCAL24PC: 8587 case elfcpp::R_PPC64_TPREL16_DS: 8588 case elfcpp::R_PPC64_DTPREL16_DS: 8589 case elfcpp::R_PPC64_TOC16_DS: 8590 case elfcpp::R_PPC64_GOT16_DS: 8591 case elfcpp::R_PPC64_SECTOFF_DS: 8592 case elfcpp::R_POWERPC_REL14: 8593 case elfcpp::R_POWERPC_REL14_BRTAKEN: 8594 case elfcpp::R_POWERPC_REL14_BRNTAKEN: 8595 overflow = Reloc::CHECK_SIGNED; 8596 break; 8597 } 8598 8599 Insn* iview = reinterpret_cast<Insn*>(view - d_offset); 8600 Insn insn = 0; 8601 8602 if (overflow == Reloc::CHECK_LOW_INSN 8603 || overflow == Reloc::CHECK_HIGH_INSN) 8604 { 8605 insn = elfcpp::Swap<32, big_endian>::readval(iview); 8606 8607 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */) 8608 overflow = Reloc::CHECK_BITFIELD; 8609 else if (overflow == Reloc::CHECK_LOW_INSN 8610 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */ 8611 || (insn & (0x3f << 26)) == 24u << 26 /* ori */ 8612 || (insn & (0x3f << 26)) == 26u << 26 /* xori */) 8613 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */ 8614 || (insn & (0x3f << 26)) == 25u << 26 /* oris */ 8615 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */)) 8616 overflow = Reloc::CHECK_UNSIGNED; 8617 else 8618 overflow = Reloc::CHECK_SIGNED; 8619 } 8620 8621 bool maybe_dq_reloc = false; 8622 typename Powerpc_relocate_functions<size, big_endian>::Status status 8623 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK; 8624 switch (r_type) 8625 { 8626 case elfcpp::R_POWERPC_NONE: 8627 case elfcpp::R_POWERPC_TLS: 8628 case elfcpp::R_POWERPC_GNU_VTINHERIT: 8629 case elfcpp::R_POWERPC_GNU_VTENTRY: 8630 break; 8631 8632 case elfcpp::R_PPC64_ADDR64: 8633 case elfcpp::R_PPC64_REL64: 8634 case elfcpp::R_PPC64_TOC: 8635 case elfcpp::R_PPC64_ADDR64_LOCAL: 8636 Reloc::addr64(view, value); 8637 break; 8638 8639 case elfcpp::R_POWERPC_TPREL: 8640 case elfcpp::R_POWERPC_DTPREL: 8641 if (size == 64) 8642 Reloc::addr64(view, value); 8643 else 8644 status = Reloc::addr32(view, value, overflow); 8645 break; 8646 8647 case elfcpp::R_PPC64_UADDR64: 8648 Reloc::addr64_u(view, value); 8649 break; 8650 8651 case elfcpp::R_POWERPC_ADDR32: 8652 status = Reloc::addr32(view, value, overflow); 8653 break; 8654 8655 case elfcpp::R_POWERPC_REL32: 8656 case elfcpp::R_POWERPC_UADDR32: 8657 status = Reloc::addr32_u(view, value, overflow); 8658 break; 8659 8660 case elfcpp::R_POWERPC_ADDR24: 8661 case elfcpp::R_POWERPC_REL24: 8662 case elfcpp::R_PPC_PLTREL24: 8663 case elfcpp::R_PPC_LOCAL24PC: 8664 status = Reloc::addr24(view, value, overflow); 8665 break; 8666 8667 case elfcpp::R_POWERPC_GOT_DTPREL16: 8668 case elfcpp::R_POWERPC_GOT_DTPREL16_LO: 8669 case elfcpp::R_POWERPC_GOT_TPREL16: 8670 case elfcpp::R_POWERPC_GOT_TPREL16_LO: 8671 if (size == 64) 8672 { 8673 // On ppc64 these are all ds form 8674 maybe_dq_reloc = true; 8675 break; 8676 } 8677 // Fall through. 8678 case elfcpp::R_POWERPC_ADDR16: 8679 case elfcpp::R_POWERPC_REL16: 8680 case elfcpp::R_PPC64_TOC16: 8681 case elfcpp::R_POWERPC_GOT16: 8682 case elfcpp::R_POWERPC_SECTOFF: 8683 case elfcpp::R_POWERPC_TPREL16: 8684 case elfcpp::R_POWERPC_DTPREL16: 8685 case elfcpp::R_POWERPC_GOT_TLSGD16: 8686 case elfcpp::R_POWERPC_GOT_TLSLD16: 8687 case elfcpp::R_POWERPC_ADDR16_LO: 8688 case elfcpp::R_POWERPC_REL16_LO: 8689 case elfcpp::R_PPC64_TOC16_LO: 8690 case elfcpp::R_POWERPC_GOT16_LO: 8691 case elfcpp::R_POWERPC_SECTOFF_LO: 8692 case elfcpp::R_POWERPC_TPREL16_LO: 8693 case elfcpp::R_POWERPC_DTPREL16_LO: 8694 case elfcpp::R_POWERPC_GOT_TLSGD16_LO: 8695 case elfcpp::R_POWERPC_GOT_TLSLD16_LO: 8696 if (size == 64) 8697 status = Reloc::addr16(view, value, overflow); 8698 else 8699 maybe_dq_reloc = true; 8700 break; 8701 8702 case elfcpp::R_POWERPC_UADDR16: 8703 status = Reloc::addr16_u(view, value, overflow); 8704 break; 8705 8706 case elfcpp::R_PPC64_ADDR16_HIGH: 8707 case elfcpp::R_PPC64_TPREL16_HIGH: 8708 case elfcpp::R_PPC64_DTPREL16_HIGH: 8709 if (size == 32) 8710 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA 8711 goto unsupp; 8712 // Fall through. 8713 case elfcpp::R_POWERPC_ADDR16_HI: 8714 case elfcpp::R_POWERPC_REL16_HI: 8715 case elfcpp::R_PPC64_TOC16_HI: 8716 case elfcpp::R_POWERPC_GOT16_HI: 8717 case elfcpp::R_POWERPC_SECTOFF_HI: 8718 case elfcpp::R_POWERPC_TPREL16_HI: 8719 case elfcpp::R_POWERPC_DTPREL16_HI: 8720 case elfcpp::R_POWERPC_GOT_TLSGD16_HI: 8721 case elfcpp::R_POWERPC_GOT_TLSLD16_HI: 8722 case elfcpp::R_POWERPC_GOT_TPREL16_HI: 8723 case elfcpp::R_POWERPC_GOT_DTPREL16_HI: 8724 Reloc::addr16_hi(view, value); 8725 break; 8726 8727 case elfcpp::R_PPC64_ADDR16_HIGHA: 8728 case elfcpp::R_PPC64_TPREL16_HIGHA: 8729 case elfcpp::R_PPC64_DTPREL16_HIGHA: 8730 if (size == 32) 8731 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD 8732 goto unsupp; 8733 // Fall through. 8734 case elfcpp::R_POWERPC_ADDR16_HA: 8735 case elfcpp::R_POWERPC_REL16_HA: 8736 case elfcpp::R_PPC64_TOC16_HA: 8737 case elfcpp::R_POWERPC_GOT16_HA: 8738 case elfcpp::R_POWERPC_SECTOFF_HA: 8739 case elfcpp::R_POWERPC_TPREL16_HA: 8740 case elfcpp::R_POWERPC_DTPREL16_HA: 8741 case elfcpp::R_POWERPC_GOT_TLSGD16_HA: 8742 case elfcpp::R_POWERPC_GOT_TLSLD16_HA: 8743 case elfcpp::R_POWERPC_GOT_TPREL16_HA: 8744 case elfcpp::R_POWERPC_GOT_DTPREL16_HA: 8745 Reloc::addr16_ha(view, value); 8746 break; 8747 8748 case elfcpp::R_POWERPC_REL16DX_HA: 8749 status = Reloc::addr16dx_ha(view, value, overflow); 8750 break; 8751 8752 case elfcpp::R_PPC64_DTPREL16_HIGHER: 8753 if (size == 32) 8754 // R_PPC_EMB_NADDR16_LO 8755 goto unsupp; 8756 // Fall through. 8757 case elfcpp::R_PPC64_ADDR16_HIGHER: 8758 case elfcpp::R_PPC64_TPREL16_HIGHER: 8759 Reloc::addr16_hi2(view, value); 8760 break; 8761 8762 case elfcpp::R_PPC64_DTPREL16_HIGHERA: 8763 if (size == 32) 8764 // R_PPC_EMB_NADDR16_HI 8765 goto unsupp; 8766 // Fall through. 8767 case elfcpp::R_PPC64_ADDR16_HIGHERA: 8768 case elfcpp::R_PPC64_TPREL16_HIGHERA: 8769 Reloc::addr16_ha2(view, value); 8770 break; 8771 8772 case elfcpp::R_PPC64_DTPREL16_HIGHEST: 8773 if (size == 32) 8774 // R_PPC_EMB_NADDR16_HA 8775 goto unsupp; 8776 // Fall through. 8777 case elfcpp::R_PPC64_ADDR16_HIGHEST: 8778 case elfcpp::R_PPC64_TPREL16_HIGHEST: 8779 Reloc::addr16_hi3(view, value); 8780 break; 8781 8782 case elfcpp::R_PPC64_DTPREL16_HIGHESTA: 8783 if (size == 32) 8784 // R_PPC_EMB_SDAI16 8785 goto unsupp; 8786 // Fall through. 8787 case elfcpp::R_PPC64_ADDR16_HIGHESTA: 8788 case elfcpp::R_PPC64_TPREL16_HIGHESTA: 8789 Reloc::addr16_ha3(view, value); 8790 break; 8791 8792 case elfcpp::R_PPC64_DTPREL16_DS: 8793 case elfcpp::R_PPC64_DTPREL16_LO_DS: 8794 if (size == 32) 8795 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16 8796 goto unsupp; 8797 // Fall through. 8798 case elfcpp::R_PPC64_TPREL16_DS: 8799 case elfcpp::R_PPC64_TPREL16_LO_DS: 8800 if (size == 32) 8801 // R_PPC_TLSGD, R_PPC_TLSLD 8802 break; 8803 // Fall through. 8804 case elfcpp::R_PPC64_ADDR16_DS: 8805 case elfcpp::R_PPC64_ADDR16_LO_DS: 8806 case elfcpp::R_PPC64_TOC16_DS: 8807 case elfcpp::R_PPC64_TOC16_LO_DS: 8808 case elfcpp::R_PPC64_GOT16_DS: 8809 case elfcpp::R_PPC64_GOT16_LO_DS: 8810 case elfcpp::R_PPC64_SECTOFF_DS: 8811 case elfcpp::R_PPC64_SECTOFF_LO_DS: 8812 maybe_dq_reloc = true; 8813 break; 8814 8815 case elfcpp::R_POWERPC_ADDR14: 8816 case elfcpp::R_POWERPC_ADDR14_BRTAKEN: 8817 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN: 8818 case elfcpp::R_POWERPC_REL14: 8819 case elfcpp::R_POWERPC_REL14_BRTAKEN: 8820 case elfcpp::R_POWERPC_REL14_BRNTAKEN: 8821 status = Reloc::addr14(view, value, overflow); 8822 break; 8823 8824 case elfcpp::R_POWERPC_COPY: 8825 case elfcpp::R_POWERPC_GLOB_DAT: 8826 case elfcpp::R_POWERPC_JMP_SLOT: 8827 case elfcpp::R_POWERPC_RELATIVE: 8828 case elfcpp::R_POWERPC_DTPMOD: 8829 case elfcpp::R_PPC64_JMP_IREL: 8830 case elfcpp::R_POWERPC_IRELATIVE: 8831 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 8832 _("unexpected reloc %u in object file"), 8833 r_type); 8834 break; 8835 8836 case elfcpp::R_PPC_EMB_SDA21: 8837 if (size == 32) 8838 goto unsupp; 8839 else 8840 { 8841 // R_PPC64_TOCSAVE. For the time being this can be ignored. 8842 } 8843 break; 8844 8845 case elfcpp::R_PPC_EMB_SDA2I16: 8846 case elfcpp::R_PPC_EMB_SDA2REL: 8847 if (size == 32) 8848 goto unsupp; 8849 // R_PPC64_TLSGD, R_PPC64_TLSLD 8850 break; 8851 8852 case elfcpp::R_POWERPC_PLT32: 8853 case elfcpp::R_POWERPC_PLTREL32: 8854 case elfcpp::R_POWERPC_PLT16_LO: 8855 case elfcpp::R_POWERPC_PLT16_HI: 8856 case elfcpp::R_POWERPC_PLT16_HA: 8857 case elfcpp::R_PPC_SDAREL16: 8858 case elfcpp::R_POWERPC_ADDR30: 8859 case elfcpp::R_PPC64_PLT64: 8860 case elfcpp::R_PPC64_PLTREL64: 8861 case elfcpp::R_PPC64_PLTGOT16: 8862 case elfcpp::R_PPC64_PLTGOT16_LO: 8863 case elfcpp::R_PPC64_PLTGOT16_HI: 8864 case elfcpp::R_PPC64_PLTGOT16_HA: 8865 case elfcpp::R_PPC64_PLT16_LO_DS: 8866 case elfcpp::R_PPC64_PLTGOT16_DS: 8867 case elfcpp::R_PPC64_PLTGOT16_LO_DS: 8868 case elfcpp::R_PPC_EMB_RELSDA: 8869 case elfcpp::R_PPC_TOC16: 8870 default: 8871 unsupp: 8872 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 8873 _("unsupported reloc %u"), 8874 r_type); 8875 break; 8876 } 8877 8878 if (maybe_dq_reloc) 8879 { 8880 if (insn == 0) 8881 insn = elfcpp::Swap<32, big_endian>::readval(iview); 8882 8883 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */ 8884 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */ 8885 && (insn & 3) == 1)) 8886 status = Reloc::addr16_dq(view, value, overflow); 8887 else if (size == 64 8888 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */ 8889 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */ 8890 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */ 8891 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */) 8892 status = Reloc::addr16_ds(view, value, overflow); 8893 else 8894 status = Reloc::addr16(view, value, overflow); 8895 } 8896 8897 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK 8898 && (has_stub_value 8899 || !(gsym != NULL 8900 && gsym->is_undefined() 8901 && is_branch_reloc(r_type)))) 8902 { 8903 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 8904 _("relocation overflow")); 8905 if (has_stub_value) 8906 gold_info(_("try relinking with a smaller --stub-group-size")); 8907 } 8908 8909 return true; 8910} 8911 8912// Relocate section data. 8913 8914template<int size, bool big_endian> 8915void 8916Target_powerpc<size, big_endian>::relocate_section( 8917 const Relocate_info<size, big_endian>* relinfo, 8918 unsigned int sh_type, 8919 const unsigned char* prelocs, 8920 size_t reloc_count, 8921 Output_section* output_section, 8922 bool needs_special_offset_handling, 8923 unsigned char* view, 8924 Address address, 8925 section_size_type view_size, 8926 const Reloc_symbol_changes* reloc_symbol_changes) 8927{ 8928 typedef Target_powerpc<size, big_endian> Powerpc; 8929 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate; 8930 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior 8931 Powerpc_comdat_behavior; 8932 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian> 8933 Classify_reloc; 8934 8935 gold_assert(sh_type == elfcpp::SHT_RELA); 8936 8937 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate, 8938 Powerpc_comdat_behavior, Classify_reloc>( 8939 relinfo, 8940 this, 8941 prelocs, 8942 reloc_count, 8943 output_section, 8944 needs_special_offset_handling, 8945 view, 8946 address, 8947 view_size, 8948 reloc_symbol_changes); 8949} 8950 8951template<int size, bool big_endian> 8952class Powerpc_scan_relocatable_reloc 8953{ 8954public: 8955 typedef typename elfcpp::Rela<size, big_endian> Reltype; 8956 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size; 8957 static const int sh_type = elfcpp::SHT_RELA; 8958 8959 // Return the symbol referred to by the relocation. 8960 static inline unsigned int 8961 get_r_sym(const Reltype* reloc) 8962 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); } 8963 8964 // Return the type of the relocation. 8965 static inline unsigned int 8966 get_r_type(const Reltype* reloc) 8967 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); } 8968 8969 // Return the strategy to use for a local symbol which is not a 8970 // section symbol, given the relocation type. 8971 inline Relocatable_relocs::Reloc_strategy 8972 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym) 8973 { 8974 if (r_type == 0 && r_sym == 0) 8975 return Relocatable_relocs::RELOC_DISCARD; 8976 return Relocatable_relocs::RELOC_COPY; 8977 } 8978 8979 // Return the strategy to use for a local symbol which is a section 8980 // symbol, given the relocation type. 8981 inline Relocatable_relocs::Reloc_strategy 8982 local_section_strategy(unsigned int, Relobj*) 8983 { 8984 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA; 8985 } 8986 8987 // Return the strategy to use for a global symbol, given the 8988 // relocation type, the object, and the symbol index. 8989 inline Relocatable_relocs::Reloc_strategy 8990 global_strategy(unsigned int r_type, Relobj*, unsigned int) 8991 { 8992 if (r_type == elfcpp::R_PPC_PLTREL24) 8993 return Relocatable_relocs::RELOC_SPECIAL; 8994 return Relocatable_relocs::RELOC_COPY; 8995 } 8996}; 8997 8998// Scan the relocs during a relocatable link. 8999 9000template<int size, bool big_endian> 9001void 9002Target_powerpc<size, big_endian>::scan_relocatable_relocs( 9003 Symbol_table* symtab, 9004 Layout* layout, 9005 Sized_relobj_file<size, big_endian>* object, 9006 unsigned int data_shndx, 9007 unsigned int sh_type, 9008 const unsigned char* prelocs, 9009 size_t reloc_count, 9010 Output_section* output_section, 9011 bool needs_special_offset_handling, 9012 size_t local_symbol_count, 9013 const unsigned char* plocal_symbols, 9014 Relocatable_relocs* rr) 9015{ 9016 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy; 9017 9018 gold_assert(sh_type == elfcpp::SHT_RELA); 9019 9020 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>( 9021 symtab, 9022 layout, 9023 object, 9024 data_shndx, 9025 prelocs, 9026 reloc_count, 9027 output_section, 9028 needs_special_offset_handling, 9029 local_symbol_count, 9030 plocal_symbols, 9031 rr); 9032} 9033 9034// Scan the relocs for --emit-relocs. 9035 9036template<int size, bool big_endian> 9037void 9038Target_powerpc<size, big_endian>::emit_relocs_scan( 9039 Symbol_table* symtab, 9040 Layout* layout, 9041 Sized_relobj_file<size, big_endian>* object, 9042 unsigned int data_shndx, 9043 unsigned int sh_type, 9044 const unsigned char* prelocs, 9045 size_t reloc_count, 9046 Output_section* output_section, 9047 bool needs_special_offset_handling, 9048 size_t local_symbol_count, 9049 const unsigned char* plocal_syms, 9050 Relocatable_relocs* rr) 9051{ 9052 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian> 9053 Classify_reloc; 9054 typedef gold::Default_emit_relocs_strategy<Classify_reloc> 9055 Emit_relocs_strategy; 9056 9057 gold_assert(sh_type == elfcpp::SHT_RELA); 9058 9059 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>( 9060 symtab, 9061 layout, 9062 object, 9063 data_shndx, 9064 prelocs, 9065 reloc_count, 9066 output_section, 9067 needs_special_offset_handling, 9068 local_symbol_count, 9069 plocal_syms, 9070 rr); 9071} 9072 9073// Emit relocations for a section. 9074// This is a modified version of the function by the same name in 9075// target-reloc.h. Using relocate_special_relocatable for 9076// R_PPC_PLTREL24 would require duplication of the entire body of the 9077// loop, so we may as well duplicate the whole thing. 9078 9079template<int size, bool big_endian> 9080void 9081Target_powerpc<size, big_endian>::relocate_relocs( 9082 const Relocate_info<size, big_endian>* relinfo, 9083 unsigned int sh_type, 9084 const unsigned char* prelocs, 9085 size_t reloc_count, 9086 Output_section* output_section, 9087 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section, 9088 unsigned char*, 9089 Address view_address, 9090 section_size_type, 9091 unsigned char* reloc_view, 9092 section_size_type reloc_view_size) 9093{ 9094 gold_assert(sh_type == elfcpp::SHT_RELA); 9095 9096 typedef typename elfcpp::Rela<size, big_endian> Reltype; 9097 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write; 9098 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size; 9099 // Offset from start of insn to d-field reloc. 9100 const int d_offset = big_endian ? 2 : 0; 9101 9102 Powerpc_relobj<size, big_endian>* const object 9103 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object); 9104 const unsigned int local_count = object->local_symbol_count(); 9105 unsigned int got2_shndx = object->got2_shndx(); 9106 Address got2_addend = 0; 9107 if (got2_shndx != 0) 9108 { 9109 got2_addend = object->get_output_section_offset(got2_shndx); 9110 gold_assert(got2_addend != invalid_address); 9111 } 9112 9113 unsigned char* pwrite = reloc_view; 9114 bool zap_next = false; 9115 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) 9116 { 9117 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i); 9118 if (strategy == Relocatable_relocs::RELOC_DISCARD) 9119 continue; 9120 9121 Reltype reloc(prelocs); 9122 Reltype_write reloc_write(pwrite); 9123 9124 Address offset = reloc.get_r_offset(); 9125 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); 9126 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 9127 unsigned int r_type = elfcpp::elf_r_type<size>(r_info); 9128 const unsigned int orig_r_sym = r_sym; 9129 typename elfcpp::Elf_types<size>::Elf_Swxword addend 9130 = reloc.get_r_addend(); 9131 const Symbol* gsym = NULL; 9132 9133 if (zap_next) 9134 { 9135 // We could arrange to discard these and other relocs for 9136 // tls optimised sequences in the strategy methods, but for 9137 // now do as BFD ld does. 9138 r_type = elfcpp::R_POWERPC_NONE; 9139 zap_next = false; 9140 } 9141 9142 // Get the new symbol index. 9143 Output_section* os = NULL; 9144 if (r_sym < local_count) 9145 { 9146 switch (strategy) 9147 { 9148 case Relocatable_relocs::RELOC_COPY: 9149 case Relocatable_relocs::RELOC_SPECIAL: 9150 if (r_sym != 0) 9151 { 9152 r_sym = object->symtab_index(r_sym); 9153 gold_assert(r_sym != -1U); 9154 } 9155 break; 9156 9157 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA: 9158 { 9159 // We are adjusting a section symbol. We need to find 9160 // the symbol table index of the section symbol for 9161 // the output section corresponding to input section 9162 // in which this symbol is defined. 9163 gold_assert(r_sym < local_count); 9164 bool is_ordinary; 9165 unsigned int shndx = 9166 object->local_symbol_input_shndx(r_sym, &is_ordinary); 9167 gold_assert(is_ordinary); 9168 os = object->output_section(shndx); 9169 gold_assert(os != NULL); 9170 gold_assert(os->needs_symtab_index()); 9171 r_sym = os->symtab_index(); 9172 } 9173 break; 9174 9175 default: 9176 gold_unreachable(); 9177 } 9178 } 9179 else 9180 { 9181 gsym = object->global_symbol(r_sym); 9182 gold_assert(gsym != NULL); 9183 if (gsym->is_forwarder()) 9184 gsym = relinfo->symtab->resolve_forwards(gsym); 9185 9186 gold_assert(gsym->has_symtab_index()); 9187 r_sym = gsym->symtab_index(); 9188 } 9189 9190 // Get the new offset--the location in the output section where 9191 // this relocation should be applied. 9192 if (static_cast<Address>(offset_in_output_section) != invalid_address) 9193 offset += offset_in_output_section; 9194 else 9195 { 9196 section_offset_type sot_offset = 9197 convert_types<section_offset_type, Address>(offset); 9198 section_offset_type new_sot_offset = 9199 output_section->output_offset(object, relinfo->data_shndx, 9200 sot_offset); 9201 gold_assert(new_sot_offset != -1); 9202 offset = new_sot_offset; 9203 } 9204 9205 // In an object file, r_offset is an offset within the section. 9206 // In an executable or dynamic object, generated by 9207 // --emit-relocs, r_offset is an absolute address. 9208 if (!parameters->options().relocatable()) 9209 { 9210 offset += view_address; 9211 if (static_cast<Address>(offset_in_output_section) != invalid_address) 9212 offset -= offset_in_output_section; 9213 } 9214 9215 // Handle the reloc addend based on the strategy. 9216 if (strategy == Relocatable_relocs::RELOC_COPY) 9217 ; 9218 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA) 9219 { 9220 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym); 9221 gold_assert(os != NULL); 9222 addend = psymval->value(object, addend) - os->address(); 9223 } 9224 else if (strategy == Relocatable_relocs::RELOC_SPECIAL) 9225 { 9226 if (size == 32) 9227 { 9228 if (addend >= 32768) 9229 addend += got2_addend; 9230 } 9231 else if (r_type == elfcpp::R_POWERPC_REL16_HA) 9232 { 9233 r_type = elfcpp::R_POWERPC_ADDR16_HA; 9234 addend -= d_offset; 9235 } 9236 else if (r_type == elfcpp::R_POWERPC_REL16_LO) 9237 { 9238 r_type = elfcpp::R_POWERPC_ADDR16_LO; 9239 addend -= d_offset + 4; 9240 } 9241 } 9242 else 9243 gold_unreachable(); 9244 9245 if (!parameters->options().relocatable()) 9246 { 9247 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16 9248 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO 9249 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI 9250 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA) 9251 { 9252 // First instruction of a global dynamic sequence, 9253 // arg setup insn. 9254 const bool final = gsym == NULL || gsym->final_value_is_known(); 9255 switch (this->optimize_tls_gd(final)) 9256 { 9257 case tls::TLSOPT_TO_IE: 9258 r_type += (elfcpp::R_POWERPC_GOT_TPREL16 9259 - elfcpp::R_POWERPC_GOT_TLSGD16); 9260 break; 9261 case tls::TLSOPT_TO_LE: 9262 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16 9263 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO) 9264 r_type = elfcpp::R_POWERPC_TPREL16_HA; 9265 else 9266 { 9267 r_type = elfcpp::R_POWERPC_NONE; 9268 offset -= d_offset; 9269 } 9270 break; 9271 default: 9272 break; 9273 } 9274 } 9275 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16 9276 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO 9277 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI 9278 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA) 9279 { 9280 // First instruction of a local dynamic sequence, 9281 // arg setup insn. 9282 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE) 9283 { 9284 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16 9285 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO) 9286 { 9287 r_type = elfcpp::R_POWERPC_TPREL16_HA; 9288 const Output_section* os = relinfo->layout->tls_segment() 9289 ->first_section(); 9290 gold_assert(os != NULL); 9291 gold_assert(os->needs_symtab_index()); 9292 r_sym = os->symtab_index(); 9293 addend = dtp_offset; 9294 } 9295 else 9296 { 9297 r_type = elfcpp::R_POWERPC_NONE; 9298 offset -= d_offset; 9299 } 9300 } 9301 } 9302 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16 9303 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO 9304 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI 9305 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA) 9306 { 9307 // First instruction of initial exec sequence. 9308 const bool final = gsym == NULL || gsym->final_value_is_known(); 9309 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE) 9310 { 9311 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16 9312 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO) 9313 r_type = elfcpp::R_POWERPC_TPREL16_HA; 9314 else 9315 { 9316 r_type = elfcpp::R_POWERPC_NONE; 9317 offset -= d_offset; 9318 } 9319 } 9320 } 9321 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD) 9322 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD)) 9323 { 9324 // Second instruction of a global dynamic sequence, 9325 // the __tls_get_addr call 9326 const bool final = gsym == NULL || gsym->final_value_is_known(); 9327 switch (this->optimize_tls_gd(final)) 9328 { 9329 case tls::TLSOPT_TO_IE: 9330 r_type = elfcpp::R_POWERPC_NONE; 9331 zap_next = true; 9332 break; 9333 case tls::TLSOPT_TO_LE: 9334 r_type = elfcpp::R_POWERPC_TPREL16_LO; 9335 offset += d_offset; 9336 zap_next = true; 9337 break; 9338 default: 9339 break; 9340 } 9341 } 9342 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD) 9343 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD)) 9344 { 9345 // Second instruction of a local dynamic sequence, 9346 // the __tls_get_addr call 9347 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE) 9348 { 9349 const Output_section* os = relinfo->layout->tls_segment() 9350 ->first_section(); 9351 gold_assert(os != NULL); 9352 gold_assert(os->needs_symtab_index()); 9353 r_sym = os->symtab_index(); 9354 addend = dtp_offset; 9355 r_type = elfcpp::R_POWERPC_TPREL16_LO; 9356 offset += d_offset; 9357 zap_next = true; 9358 } 9359 } 9360 else if (r_type == elfcpp::R_POWERPC_TLS) 9361 { 9362 // Second instruction of an initial exec sequence 9363 const bool final = gsym == NULL || gsym->final_value_is_known(); 9364 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE) 9365 { 9366 r_type = elfcpp::R_POWERPC_TPREL16_LO; 9367 offset += d_offset; 9368 } 9369 } 9370 } 9371 9372 reloc_write.put_r_offset(offset); 9373 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type)); 9374 reloc_write.put_r_addend(addend); 9375 9376 pwrite += reloc_size; 9377 } 9378 9379 gold_assert(static_cast<section_size_type>(pwrite - reloc_view) 9380 == reloc_view_size); 9381} 9382 9383// Return the value to use for a dynamic symbol which requires special 9384// treatment. This is how we support equality comparisons of function 9385// pointers across shared library boundaries, as described in the 9386// processor specific ABI supplement. 9387 9388template<int size, bool big_endian> 9389uint64_t 9390Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const 9391{ 9392 if (size == 32) 9393 { 9394 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset()); 9395 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin(); 9396 p != this->stub_tables_.end(); 9397 ++p) 9398 { 9399 Address off = (*p)->find_plt_call_entry(gsym); 9400 if (off != invalid_address) 9401 return (*p)->stub_address() + off; 9402 } 9403 } 9404 else if (this->abiversion() >= 2) 9405 { 9406 Address off = this->glink_section()->find_global_entry(gsym); 9407 if (off != invalid_address) 9408 return this->glink_section()->global_entry_address() + off; 9409 } 9410 gold_unreachable(); 9411} 9412 9413// Return the PLT address to use for a local symbol. 9414template<int size, bool big_endian> 9415uint64_t 9416Target_powerpc<size, big_endian>::do_plt_address_for_local( 9417 const Relobj* object, 9418 unsigned int symndx) const 9419{ 9420 if (size == 32) 9421 { 9422 const Sized_relobj<size, big_endian>* relobj 9423 = static_cast<const Sized_relobj<size, big_endian>*>(object); 9424 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin(); 9425 p != this->stub_tables_.end(); 9426 ++p) 9427 { 9428 Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(), 9429 symndx); 9430 if (off != invalid_address) 9431 return (*p)->stub_address() + off; 9432 } 9433 } 9434 gold_unreachable(); 9435} 9436 9437// Return the PLT address to use for a global symbol. 9438template<int size, bool big_endian> 9439uint64_t 9440Target_powerpc<size, big_endian>::do_plt_address_for_global( 9441 const Symbol* gsym) const 9442{ 9443 if (size == 32) 9444 { 9445 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin(); 9446 p != this->stub_tables_.end(); 9447 ++p) 9448 { 9449 Address off = (*p)->find_plt_call_entry(gsym); 9450 if (off != invalid_address) 9451 return (*p)->stub_address() + off; 9452 } 9453 } 9454 else if (this->abiversion() >= 2) 9455 { 9456 Address off = this->glink_section()->find_global_entry(gsym); 9457 if (off != invalid_address) 9458 return this->glink_section()->global_entry_address() + off; 9459 } 9460 gold_unreachable(); 9461} 9462 9463// Return the offset to use for the GOT_INDX'th got entry which is 9464// for a local tls symbol specified by OBJECT, SYMNDX. 9465template<int size, bool big_endian> 9466int64_t 9467Target_powerpc<size, big_endian>::do_tls_offset_for_local( 9468 const Relobj* object, 9469 unsigned int symndx, 9470 unsigned int got_indx) const 9471{ 9472 const Powerpc_relobj<size, big_endian>* ppc_object 9473 = static_cast<const Powerpc_relobj<size, big_endian>*>(object); 9474 if (ppc_object->local_symbol(symndx)->is_tls_symbol()) 9475 { 9476 for (Got_type got_type = GOT_TYPE_TLSGD; 9477 got_type <= GOT_TYPE_TPREL; 9478 got_type = Got_type(got_type + 1)) 9479 if (ppc_object->local_has_got_offset(symndx, got_type)) 9480 { 9481 unsigned int off = ppc_object->local_got_offset(symndx, got_type); 9482 if (got_type == GOT_TYPE_TLSGD) 9483 off += size / 8; 9484 if (off == got_indx * (size / 8)) 9485 { 9486 if (got_type == GOT_TYPE_TPREL) 9487 return -tp_offset; 9488 else 9489 return -dtp_offset; 9490 } 9491 } 9492 } 9493 gold_unreachable(); 9494} 9495 9496// Return the offset to use for the GOT_INDX'th got entry which is 9497// for global tls symbol GSYM. 9498template<int size, bool big_endian> 9499int64_t 9500Target_powerpc<size, big_endian>::do_tls_offset_for_global( 9501 Symbol* gsym, 9502 unsigned int got_indx) const 9503{ 9504 if (gsym->type() == elfcpp::STT_TLS) 9505 { 9506 for (Got_type got_type = GOT_TYPE_TLSGD; 9507 got_type <= GOT_TYPE_TPREL; 9508 got_type = Got_type(got_type + 1)) 9509 if (gsym->has_got_offset(got_type)) 9510 { 9511 unsigned int off = gsym->got_offset(got_type); 9512 if (got_type == GOT_TYPE_TLSGD) 9513 off += size / 8; 9514 if (off == got_indx * (size / 8)) 9515 { 9516 if (got_type == GOT_TYPE_TPREL) 9517 return -tp_offset; 9518 else 9519 return -dtp_offset; 9520 } 9521 } 9522 } 9523 gold_unreachable(); 9524} 9525 9526// The selector for powerpc object files. 9527 9528template<int size, bool big_endian> 9529class Target_selector_powerpc : public Target_selector 9530{ 9531public: 9532 Target_selector_powerpc() 9533 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC, 9534 size, big_endian, 9535 (size == 64 9536 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle") 9537 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")), 9538 (size == 64 9539 ? (big_endian ? "elf64ppc" : "elf64lppc") 9540 : (big_endian ? "elf32ppc" : "elf32lppc"))) 9541 { } 9542 9543 virtual Target* 9544 do_instantiate_target() 9545 { return new Target_powerpc<size, big_endian>(); } 9546}; 9547 9548Target_selector_powerpc<32, true> target_selector_ppc32; 9549Target_selector_powerpc<32, false> target_selector_ppc32le; 9550Target_selector_powerpc<64, true> target_selector_ppc64; 9551Target_selector_powerpc<64, false> target_selector_ppc64le; 9552 9553// Instantiate these constants for -O0 9554template<int size, bool big_endian> 9555const int Output_data_glink<size, big_endian>::pltresolve_size; 9556template<int size, bool big_endian> 9557const typename Output_data_glink<size, big_endian>::Address 9558 Output_data_glink<size, big_endian>::invalid_address; 9559template<int size, bool big_endian> 9560const typename Stub_table<size, big_endian>::Address 9561 Stub_table<size, big_endian>::invalid_address; 9562template<int size, bool big_endian> 9563const typename Target_powerpc<size, big_endian>::Address 9564 Target_powerpc<size, big_endian>::invalid_address; 9565 9566} // End anonymous namespace. 9567