1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright (C) 2008-2018 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
7 // This file is part of gold.
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.
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.
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.
30 #include "parameters.h"
37 #include "copy-relocs.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
50 template<int size, bool big_endian>
51 class Output_data_plt_powerpc;
53 template<int size, bool big_endian>
54 class Output_data_brlt_powerpc;
56 template<int size, bool big_endian>
57 class Output_data_got_powerpc;
59 template<int size, bool big_endian>
60 class Output_data_glink;
62 template<int size, bool big_endian>
65 template<int size, bool big_endian>
66 class Output_data_save_res;
68 template<int size, bool big_endian>
71 struct Stub_table_owner
74 : output_section(NULL), owner(NULL)
77 Output_section* output_section;
78 const Output_section::Input_section* owner;
82 is_branch_reloc(unsigned int r_type);
84 // Counter incremented on every Powerpc_relobj constructed.
85 static uint32_t object_id = 0;
87 template<int size, bool big_endian>
88 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
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;
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_()
103 this->set_abiversion(0);
109 // Read the symbols then set up st_other vector.
111 do_read_symbols(Read_symbols_data*);
113 // Arrange to always relocate .toc first.
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);
120 // The .toc section index.
127 // Mark .toc entry at OFF as not optimizable.
129 set_no_toc_opt(Address off)
131 if (this->no_toc_opt_.empty())
132 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
135 if (off < this->no_toc_opt_.size())
136 this->no_toc_opt_[off] = true;
139 // Mark the entire .toc as not optimizable.
143 this->no_toc_opt_.resize(1);
144 this->no_toc_opt_[0] = true;
147 // Return true if code using the .toc entry at OFF should not be edited.
149 no_toc_opt(Address off) const
151 if (this->no_toc_opt_.empty())
154 if (off >= this->no_toc_opt_.size())
156 return this->no_toc_opt_[off];
159 // The .got2 section shndx.
164 return this->special_;
169 // The .opd section shndx.
176 return this->special_;
179 // Init OPD entry arrays.
181 init_opd(size_t opd_size)
183 size_t count = this->opd_ent_ndx(opd_size);
184 this->opd_ent_.resize(count);
187 // Return section and offset of function entry for .opd + R_OFF.
189 get_opd_ent(Address r_off, Address* value = NULL) const
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);
195 *value = this->opd_ent_[ndx].off;
196 return this->opd_ent_[ndx].shndx;
199 // Set section and offset of function entry for .opd + R_OFF.
201 set_opd_ent(Address r_off, unsigned int shndx, Address value)
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;
209 // Return discard flag for .opd + R_OFF.
211 get_opd_discard(Address r_off) const
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;
218 // Set discard flag for .opd + R_OFF.
220 set_opd_discard(Address r_off)
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;
229 { return this->opd_valid_; }
233 { this->opd_valid_ = true; }
235 // Examine .rela.opd to build info about function entry points.
237 scan_opd_relocs(size_t reloc_count,
238 const unsigned char* prelocs,
239 const unsigned char* plocal_syms);
241 // Returns true if a code sequence loading a TOC entry can be
242 // converted into code calculating a TOC pointer relative offset.
244 make_toc_relative(Target_powerpc<size, big_endian>* target,
247 // Perform the Sized_relobj_file method, then set up opd info from
250 do_read_relocs(Read_relocs_data*);
253 do_find_special_sections(Read_symbols_data* sd);
255 // Adjust this local symbol value. Return false if the symbol
256 // should be discarded from the output file.
258 do_adjust_local_symbol(Symbol_value<size>* lv) const
260 if (size == 64 && this->opd_shndx() != 0)
263 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
265 if (this->get_opd_discard(lv->input_value()))
273 { return &this->access_from_map_; }
275 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
276 // section at DST_OFF.
278 add_reference(Relobj* src_obj,
279 unsigned int src_indx,
280 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
282 Section_id src_id(src_obj, src_indx);
283 this->access_from_map_[dst_off].insert(src_id);
286 // Add a reference to the code section specified by the .opd entry
289 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
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;
298 process_gc_mark(Symbol_table* symtab)
300 for (size_t i = 0; i < this->opd_ent_.size(); i++)
301 if (this->opd_ent_[i].gc_mark)
303 unsigned int shndx = this->opd_ent_[i].shndx;
304 symtab->gc()->worklist().push_back(Section_id(this, shndx));
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.
311 toc_base_offset() const
315 set_has_small_toc_reloc()
316 { has_small_toc_reloc_ = true; }
319 has_small_toc_reloc() const
320 { return has_small_toc_reloc_; }
323 set_has_14bit_branch(unsigned int shndx)
325 if (shndx >= this->has14_.size())
326 this->has14_.resize(shndx + 1);
327 this->has14_[shndx] = true;
331 has_14bit_branch(unsigned int shndx) const
332 { return shndx < this->has14_.size() && this->has14_[shndx]; }
335 set_stub_table(unsigned int shndx, unsigned int stub_index)
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;
342 Stub_table<size, big_endian>*
343 stub_table(unsigned int shndx)
345 if (shndx < this->stub_table_index_.size())
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];
360 this->stub_table_index_.clear();
365 { return this->uniq_; }
369 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
371 // Set ABI version for input and output
373 set_abiversion(int ver);
376 st_other (unsigned int symndx) const
378 return this->st_other_[symndx];
382 ppc64_local_entry_offset(const Symbol* sym) const
383 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
386 ppc64_local_entry_offset(unsigned int symndx) const
387 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
398 // Return index into opd_ent_ array for .opd entry at OFF.
399 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
400 // apart when the language doesn't use the last 8-byte word, the
401 // environment pointer. Thus dividing the entry section offset by
402 // 16 will give an index into opd_ent_ that works for either layout
403 // of .opd. (It leaves some elements of the vector unused when .opd
404 // entries are spaced 24 bytes apart, but we don't know the spacing
405 // until relocations are processed, and in any case it is possible
406 // for an object to have some entries spaced 16 bytes apart and
407 // others 24 bytes apart.)
409 opd_ent_ndx(size_t off) const
412 // Per object unique identifier
415 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
416 unsigned int special_;
418 // For 64-bit the .rela.toc and .toc section shdnx.
419 unsigned int relatoc_;
422 // For 64-bit, whether this object uses small model relocs to access
424 bool has_small_toc_reloc_;
426 // Set at the start of gc_process_relocs, when we know opd_ent_
427 // vector is valid. The flag could be made atomic and set in
428 // do_read_relocs with memory_order_release and then tested with
429 // memory_order_acquire, potentially resulting in fewer entries in
434 elfcpp::Elf_Word e_flags_;
436 // For 64-bit, an array with one entry per 64-bit word in the .toc
437 // section, set if accesses using that word cannot be optimised.
438 std::vector<bool> no_toc_opt_;
440 // The first 8-byte word of an OPD entry gives the address of the
441 // entry point of the function. Relocatable object files have a
442 // relocation on this word. The following vector records the
443 // section and offset specified by these relocations.
444 std::vector<Opd_ent> opd_ent_;
446 // References made to this object's .opd section when running
447 // gc_process_relocs for another object, before the opd_ent_ vector
448 // is valid for this object.
449 Access_from access_from_map_;
451 // Whether input section has a 14-bit branch reloc.
452 std::vector<bool> has14_;
454 // The stub table to use for a given input section.
455 std::vector<unsigned int> stub_table_index_;
457 // ELF st_other field for local symbols.
458 std::vector<unsigned char> st_other_;
461 template<int size, bool big_endian>
462 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
465 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
467 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
468 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
469 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
470 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_()
472 this->set_abiversion(0);
478 // Call Sized_dynobj::do_read_symbols to read the symbols then
479 // read .opd from a dynamic object, filling in opd_ent_ vector,
481 do_read_symbols(Read_symbols_data*);
483 // The .opd section shndx.
487 return this->opd_shndx_;
490 // The .opd section address.
494 return this->opd_address_;
497 // Init OPD entry arrays.
499 init_opd(size_t opd_size)
501 size_t count = this->opd_ent_ndx(opd_size);
502 this->opd_ent_.resize(count);
505 // Return section and offset of function entry for .opd + R_OFF.
507 get_opd_ent(Address r_off, Address* value = NULL) const
509 size_t ndx = this->opd_ent_ndx(r_off);
510 gold_assert(ndx < this->opd_ent_.size());
511 gold_assert(this->opd_ent_[ndx].shndx != 0);
513 *value = this->opd_ent_[ndx].off;
514 return this->opd_ent_[ndx].shndx;
517 // Set section and offset of function entry for .opd + R_OFF.
519 set_opd_ent(Address r_off, unsigned int shndx, Address value)
521 size_t ndx = this->opd_ent_ndx(r_off);
522 gold_assert(ndx < this->opd_ent_.size());
523 this->opd_ent_[ndx].shndx = shndx;
524 this->opd_ent_[ndx].off = value;
529 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
531 // Set ABI version for input and output.
533 set_abiversion(int ver);
536 // Used to specify extent of executable sections.
539 Sec_info(Address start_, Address len_, unsigned int shndx_)
540 : start(start_), len(len_), shndx(shndx_)
544 operator<(const Sec_info& that) const
545 { return this->start < that.start; }
558 // Return index into opd_ent_ array for .opd entry at OFF.
560 opd_ent_ndx(size_t off) const
563 // For 64-bit the .opd section shndx and address.
564 unsigned int opd_shndx_;
565 Address opd_address_;
568 elfcpp::Elf_Word e_flags_;
570 // The first 8-byte word of an OPD entry gives the address of the
571 // entry point of the function. Records the section and offset
572 // corresponding to the address. Note that in dynamic objects,
573 // offset is *not* relative to the section.
574 std::vector<Opd_ent> opd_ent_;
577 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
578 // base class will emit.
580 template<int sh_type, int size, bool big_endian>
581 class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
584 Powerpc_copy_relocs()
585 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
588 // Emit any saved relocations which turn out to be needed. This is
589 // called after all the relocs have been scanned.
591 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
594 template<int size, bool big_endian>
595 class Target_powerpc : public Sized_target<size, big_endian>
599 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
600 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
601 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
602 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
603 static const Address invalid_address = static_cast<Address>(0) - 1;
604 // Offset of tp and dtp pointers from start of TLS block.
605 static const Address tp_offset = 0x7000;
606 static const Address dtp_offset = 0x8000;
609 : Sized_target<size, big_endian>(&powerpc_info),
610 got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL),
611 glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
612 tlsld_got_offset_(-1U),
613 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
614 plt_thread_safe_(false), plt_localentry0_(false),
615 plt_localentry0_init_(false), has_localentry0_(false),
616 has_tls_get_addr_opt_(false),
617 relax_failed_(false), relax_fail_count_(0),
618 stub_group_size_(0), savres_section_(0),
619 tls_get_addr_(NULL), tls_get_addr_opt_(NULL)
623 // Process the relocations to determine unreferenced sections for
624 // garbage collection.
626 gc_process_relocs(Symbol_table* symtab,
628 Sized_relobj_file<size, big_endian>* object,
629 unsigned int data_shndx,
630 unsigned int sh_type,
631 const unsigned char* prelocs,
633 Output_section* output_section,
634 bool needs_special_offset_handling,
635 size_t local_symbol_count,
636 const unsigned char* plocal_symbols);
638 // Scan the relocations to look for symbol adjustments.
640 scan_relocs(Symbol_table* symtab,
642 Sized_relobj_file<size, big_endian>* object,
643 unsigned int data_shndx,
644 unsigned int sh_type,
645 const unsigned char* prelocs,
647 Output_section* output_section,
648 bool needs_special_offset_handling,
649 size_t local_symbol_count,
650 const unsigned char* plocal_symbols);
652 // Map input .toc section to output .got section.
654 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
656 if (size == 64 && strcmp(name, ".toc") == 0)
664 // Provide linker defined save/restore functions.
666 define_save_restore_funcs(Layout*, Symbol_table*);
668 // No stubs unless a final link.
671 { return !parameters->options().relocatable(); }
674 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
677 do_plt_fde_location(const Output_data*, unsigned char*,
678 uint64_t*, off_t*) const;
680 // Stash info about branches, for stub generation.
682 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
683 unsigned int data_shndx, Address r_offset,
684 unsigned int r_type, unsigned int r_sym, Address addend)
686 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
687 this->branch_info_.push_back(info);
688 if (r_type == elfcpp::R_POWERPC_REL14
689 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
690 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
691 ppc_object->set_has_14bit_branch(data_shndx);
694 // Return whether the last branch is a plt call, and if so, mark the
695 // branch as having an R_PPC64_TOCSAVE.
697 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
698 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
701 && !this->branch_info_.empty()
702 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
703 r_offset, this, symtab));
706 // Say the given location, that of a nop in a function prologue with
707 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
708 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
710 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
711 unsigned int shndx, Address offset)
714 loc.object = ppc_object;
717 this->tocsave_loc_.insert(loc);
724 return this->tocsave_loc_;
728 do_define_standard_symbols(Symbol_table*, Layout*);
730 // Finalize the sections.
732 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
734 // Return the value to use for a dynamic which requires special
737 do_dynsym_value(const Symbol*) const;
739 // Return the PLT address to use for a local symbol.
741 do_plt_address_for_local(const Relobj*, unsigned int) const;
743 // Return the PLT address to use for a global symbol.
745 do_plt_address_for_global(const Symbol*) const;
747 // Return the offset to use for the GOT_INDX'th got entry which is
748 // for a local tls symbol specified by OBJECT, SYMNDX.
750 do_tls_offset_for_local(const Relobj* object,
752 unsigned int got_indx) const;
754 // Return the offset to use for the GOT_INDX'th got entry which is
755 // for global tls symbol GSYM.
757 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
760 do_function_location(Symbol_location*) const;
763 do_can_check_for_function_pointers() const
766 // Adjust -fsplit-stack code which calls non-split-stack code.
768 do_calls_non_split(Relobj* object, unsigned int shndx,
769 section_offset_type fnoffset, section_size_type fnsize,
770 const unsigned char* prelocs, size_t reloc_count,
771 unsigned char* view, section_size_type view_size,
772 std::string* from, std::string* to) const;
774 // Relocate a section.
776 relocate_section(const Relocate_info<size, big_endian>*,
777 unsigned int sh_type,
778 const unsigned char* prelocs,
780 Output_section* output_section,
781 bool needs_special_offset_handling,
783 Address view_address,
784 section_size_type view_size,
785 const Reloc_symbol_changes*);
787 // Scan the relocs during a relocatable link.
789 scan_relocatable_relocs(Symbol_table* symtab,
791 Sized_relobj_file<size, big_endian>* object,
792 unsigned int data_shndx,
793 unsigned int sh_type,
794 const unsigned char* prelocs,
796 Output_section* output_section,
797 bool needs_special_offset_handling,
798 size_t local_symbol_count,
799 const unsigned char* plocal_symbols,
800 Relocatable_relocs*);
802 // Scan the relocs for --emit-relocs.
804 emit_relocs_scan(Symbol_table* symtab,
806 Sized_relobj_file<size, big_endian>* object,
807 unsigned int data_shndx,
808 unsigned int sh_type,
809 const unsigned char* prelocs,
811 Output_section* output_section,
812 bool needs_special_offset_handling,
813 size_t local_symbol_count,
814 const unsigned char* plocal_syms,
815 Relocatable_relocs* rr);
817 // Emit relocations for a section.
819 relocate_relocs(const Relocate_info<size, big_endian>*,
820 unsigned int sh_type,
821 const unsigned char* prelocs,
823 Output_section* output_section,
824 typename elfcpp::Elf_types<size>::Elf_Off
825 offset_in_output_section,
827 Address view_address,
829 unsigned char* reloc_view,
830 section_size_type reloc_view_size);
832 // Return whether SYM is defined by the ABI.
834 do_is_defined_by_abi(const Symbol* sym) const
836 return strcmp(sym->name(), "__tls_get_addr") == 0;
839 // Return the size of the GOT section.
843 gold_assert(this->got_ != NULL);
844 return this->got_->data_size();
847 // Get the PLT section.
848 const Output_data_plt_powerpc<size, big_endian>*
851 gold_assert(this->plt_ != NULL);
855 // Get the IPLT section.
856 const Output_data_plt_powerpc<size, big_endian>*
859 gold_assert(this->iplt_ != NULL);
863 // Get the .glink section.
864 const Output_data_glink<size, big_endian>*
865 glink_section() const
867 gold_assert(this->glink_ != NULL);
871 Output_data_glink<size, big_endian>*
874 gold_assert(this->glink_ != NULL);
878 bool has_glink() const
879 { return this->glink_ != NULL; }
881 // Get the GOT section.
882 const Output_data_got_powerpc<size, big_endian>*
885 gold_assert(this->got_ != NULL);
889 // Get the GOT section, creating it if necessary.
890 Output_data_got_powerpc<size, big_endian>*
891 got_section(Symbol_table*, Layout*);
894 do_make_elf_object(const std::string&, Input_file*, off_t,
895 const elfcpp::Ehdr<size, big_endian>&);
897 // Return the number of entries in the GOT.
899 got_entry_count() const
901 if (this->got_ == NULL)
903 return this->got_size() / (size / 8);
906 // Return the number of entries in the PLT.
908 plt_entry_count() const;
910 // Return the offset of the first non-reserved PLT entry.
912 first_plt_entry_offset() const
916 if (this->abiversion() >= 2)
921 // Return the size of each PLT entry.
923 plt_entry_size() const
927 if (this->abiversion() >= 2)
932 Output_data_save_res<size, big_endian>*
933 savres_section() const
935 return this->savres_section_;
938 // Add any special sections for this symbol to the gc work list.
939 // For powerpc64, this adds the code section of a function
942 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
944 // Handle target specific gc actions when adding a gc reference from
945 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
946 // and DST_OFF. For powerpc64, this adds a referenc to the code
947 // section of a function descriptor.
949 do_gc_add_reference(Symbol_table* symtab,
951 unsigned int src_shndx,
953 unsigned int dst_shndx,
954 Address dst_off) const;
956 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
959 { return this->stub_tables_; }
961 const Output_data_brlt_powerpc<size, big_endian>*
963 { return this->brlt_section_; }
966 add_branch_lookup_table(Address to)
968 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
969 this->branch_lookup_table_.insert(std::make_pair(to, off));
973 find_branch_lookup_table(Address to)
975 typename Branch_lookup_table::const_iterator p
976 = this->branch_lookup_table_.find(to);
977 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
981 write_branch_lookup_table(unsigned char *oview)
983 for (typename Branch_lookup_table::const_iterator p
984 = this->branch_lookup_table_.begin();
985 p != this->branch_lookup_table_.end();
988 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
992 // Wrapper used after relax to define a local symbol in output data,
993 // from the end if value < 0.
995 define_local(Symbol_table* symtab, const char* name,
996 Output_data* od, Address value, unsigned int symsize)
999 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1000 od, value, symsize, elfcpp::STT_NOTYPE,
1001 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1002 static_cast<Signed_address>(value) < 0,
1004 // We are creating this symbol late, so need to fix up things
1005 // done early in Layout::finalize.
1006 sym->set_dynsym_index(-1U);
1010 plt_thread_safe() const
1011 { return this->plt_thread_safe_; }
1014 plt_localentry0() const
1015 { return this->plt_localentry0_; }
1018 set_has_localentry0()
1020 this->has_localentry0_ = true;
1024 is_elfv2_localentry0(const Symbol* gsym) const
1027 && this->abiversion() >= 2
1028 && this->plt_localentry0()
1029 && gsym->type() == elfcpp::STT_FUNC
1030 && gsym->is_defined()
1031 && gsym->nonvis() >> 3 == 0
1032 && !gsym->non_zero_localentry());
1036 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1037 unsigned int r_sym) const
1039 const Powerpc_relobj<size, big_endian>* ppc_object
1040 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1043 && this->abiversion() >= 2
1044 && this->plt_localentry0()
1045 && ppc_object->st_other(r_sym) >> 5 == 0)
1047 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1049 if (!psymval->is_ifunc_symbol()
1050 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1057 // Remember any symbols seen with non-zero localentry, even those
1058 // not providing a definition
1060 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1065 unsigned char st_other = sym.get_st_other();
1066 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1067 to->set_non_zero_localentry();
1069 // We haven't resolved anything, continue normal processing.
1075 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1078 set_abiversion(int ver)
1080 elfcpp::Elf_Word flags = this->processor_specific_flags();
1081 flags &= ~elfcpp::EF_PPC64_ABI;
1082 flags |= ver & elfcpp::EF_PPC64_ABI;
1083 this->set_processor_specific_flags(flags);
1087 tls_get_addr_opt() const
1088 { return this->tls_get_addr_opt_; }
1091 tls_get_addr() const
1092 { return this->tls_get_addr_; }
1094 // If optimizing __tls_get_addr calls, whether this is the
1095 // "__tls_get_addr" symbol.
1097 is_tls_get_addr_opt(const Symbol* gsym) const
1099 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1100 || gsym == this->tls_get_addr_opt_);
1104 replace_tls_get_addr(const Symbol* gsym) const
1105 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1108 set_has_tls_get_addr_opt()
1109 { this->has_tls_get_addr_opt_ = true; }
1111 // Offset to toc save stack slot
1114 { return this->abiversion() < 2 ? 40 : 24; }
1116 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1117 // so use the CR save slot. Used only by __tls_get_addr call stub,
1118 // relying on __tls_get_addr not saving CR itself.
1121 { return this->abiversion() < 2 ? 32 : 8; }
1137 : tls_get_addr_state_(NOT_EXPECTED),
1138 relinfo_(NULL), relnum_(0), r_offset_(0)
1143 if (this->tls_get_addr_state_ != NOT_EXPECTED)
1150 if (this->relinfo_ != NULL)
1151 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1152 _("missing expected __tls_get_addr call"));
1156 expect_tls_get_addr_call(
1157 const Relocate_info<size, big_endian>* relinfo,
1161 this->tls_get_addr_state_ = EXPECTED;
1162 this->relinfo_ = relinfo;
1163 this->relnum_ = relnum;
1164 this->r_offset_ = r_offset;
1168 expect_tls_get_addr_call()
1169 { this->tls_get_addr_state_ = EXPECTED; }
1172 skip_next_tls_get_addr_call()
1173 {this->tls_get_addr_state_ = SKIP; }
1176 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1177 unsigned int r_type, const Symbol* gsym)
1179 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
1180 || r_type == elfcpp::R_PPC_PLTREL24)
1182 && (gsym == target->tls_get_addr()
1183 || gsym == target->tls_get_addr_opt()));
1184 Tls_get_addr last_tls = this->tls_get_addr_state_;
1185 this->tls_get_addr_state_ = NOT_EXPECTED;
1186 if (is_tls_call && last_tls != EXPECTED)
1188 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1197 // What we're up to regarding calls to __tls_get_addr.
1198 // On powerpc, the branch and link insn making a call to
1199 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1200 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1201 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
1202 // The marker relocation always comes first, and has the same
1203 // symbol as the reloc on the insn setting up the __tls_get_addr
1204 // argument. This ties the arg setup insn with the call insn,
1205 // allowing ld to safely optimize away the call. We check that
1206 // every call to __tls_get_addr has a marker relocation, and that
1207 // every marker relocation is on a call to __tls_get_addr.
1208 Tls_get_addr tls_get_addr_state_;
1209 // Info about the last reloc for error message.
1210 const Relocate_info<size, big_endian>* relinfo_;
1215 // The class which scans relocations.
1216 class Scan : protected Track_tls
1219 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1222 : Track_tls(), issued_non_pic_error_(false)
1226 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
1229 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1230 Sized_relobj_file<size, big_endian>* object,
1231 unsigned int data_shndx,
1232 Output_section* output_section,
1233 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1234 const elfcpp::Sym<size, big_endian>& lsym,
1238 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1239 Sized_relobj_file<size, big_endian>* object,
1240 unsigned int data_shndx,
1241 Output_section* output_section,
1242 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1246 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1248 Sized_relobj_file<size, big_endian>* relobj,
1251 const elfcpp::Rela<size, big_endian>& ,
1252 unsigned int r_type,
1253 const elfcpp::Sym<size, big_endian>&)
1255 // PowerPC64 .opd is not folded, so any identical function text
1256 // may be folded and we'll still keep function addresses distinct.
1257 // That means no reloc is of concern here.
1260 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1261 <Powerpc_relobj<size, big_endian>*>(relobj);
1262 if (ppcobj->abiversion() == 1)
1265 // For 32-bit and ELFv2, conservatively assume anything but calls to
1266 // function code might be taking the address of the function.
1267 return !is_branch_reloc(r_type);
1271 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1273 Sized_relobj_file<size, big_endian>* relobj,
1276 const elfcpp::Rela<size, big_endian>& ,
1277 unsigned int r_type,
1283 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1284 <Powerpc_relobj<size, big_endian>*>(relobj);
1285 if (ppcobj->abiversion() == 1)
1288 return !is_branch_reloc(r_type);
1292 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1293 Sized_relobj_file<size, big_endian>* object,
1294 unsigned int r_type, bool report_err);
1298 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1299 unsigned int r_type);
1302 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1303 unsigned int r_type, Symbol*);
1306 generate_tls_call(Symbol_table* symtab, Layout* layout,
1307 Target_powerpc* target);
1310 check_non_pic(Relobj*, unsigned int r_type);
1312 // Whether we have issued an error about a non-PIC compilation.
1313 bool issued_non_pic_error_;
1317 symval_for_branch(const Symbol_table* symtab,
1318 const Sized_symbol<size>* gsym,
1319 Powerpc_relobj<size, big_endian>* object,
1320 Address *value, unsigned int *dest_shndx);
1322 // The class which implements relocation.
1323 class Relocate : protected Track_tls
1326 // Use 'at' branch hints when true, 'y' when false.
1327 // FIXME maybe: set this with an option.
1328 static const bool is_isa_v2 = true;
1334 // Do a relocation. Return false if the caller should not issue
1335 // any warnings about this relocation.
1337 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1338 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1339 const Sized_symbol<size>*, const Symbol_value<size>*,
1340 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1344 class Relocate_comdat_behavior
1347 // Decide what the linker should do for relocations that refer to
1348 // discarded comdat sections.
1349 inline Comdat_behavior
1350 get(const char* name)
1352 gold::Default_comdat_behavior default_behavior;
1353 Comdat_behavior ret = default_behavior.get(name);
1354 if (ret == CB_WARNING)
1357 && (strcmp(name, ".fixup") == 0
1358 || strcmp(name, ".got2") == 0))
1361 && (strcmp(name, ".opd") == 0
1362 || strcmp(name, ".toc") == 0
1363 || strcmp(name, ".toc1") == 0))
1370 // Optimize the TLS relocation type based on what we know about the
1371 // symbol. IS_FINAL is true if the final address of this symbol is
1372 // known at link time.
1374 tls::Tls_optimization
1375 optimize_tls_gd(bool is_final)
1377 // If we are generating a shared library, then we can't do anything
1379 if (parameters->options().shared()
1380 || !parameters->options().tls_optimize())
1381 return tls::TLSOPT_NONE;
1384 return tls::TLSOPT_TO_IE;
1385 return tls::TLSOPT_TO_LE;
1388 tls::Tls_optimization
1391 if (parameters->options().shared()
1392 || !parameters->options().tls_optimize())
1393 return tls::TLSOPT_NONE;
1395 return tls::TLSOPT_TO_LE;
1398 tls::Tls_optimization
1399 optimize_tls_ie(bool is_final)
1402 || parameters->options().shared()
1403 || !parameters->options().tls_optimize())
1404 return tls::TLSOPT_NONE;
1406 return tls::TLSOPT_TO_LE;
1411 make_glink_section(Layout*);
1413 // Create the PLT section.
1415 make_plt_section(Symbol_table*, Layout*);
1418 make_iplt_section(Symbol_table*, Layout*);
1421 make_brlt_section(Layout*);
1423 // Create a PLT entry for a global symbol.
1425 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1427 // Create a PLT entry for a local IFUNC symbol.
1429 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1430 Sized_relobj_file<size, big_endian>*,
1434 // Create a GOT entry for local dynamic __tls_get_addr.
1436 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1437 Sized_relobj_file<size, big_endian>* object);
1440 tlsld_got_offset() const
1442 return this->tlsld_got_offset_;
1445 // Get the dynamic reloc section, creating it if necessary.
1447 rela_dyn_section(Layout*);
1449 // Similarly, but for ifunc symbols get the one for ifunc.
1451 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1453 // Copy a relocation against a global symbol.
1455 copy_reloc(Symbol_table* symtab, Layout* layout,
1456 Sized_relobj_file<size, big_endian>* object,
1457 unsigned int shndx, Output_section* output_section,
1458 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1460 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1461 this->copy_relocs_.copy_reloc(symtab, layout,
1462 symtab->get_sized_symbol<size>(sym),
1463 object, shndx, output_section,
1464 r_type, reloc.get_r_offset(),
1465 reloc.get_r_addend(),
1466 this->rela_dyn_section(layout));
1469 // Look over all the input sections, deciding where to place stubs.
1471 group_sections(Layout*, const Task*, bool);
1473 // Sort output sections by address.
1474 struct Sort_sections
1477 operator()(const Output_section* sec1, const Output_section* sec2)
1478 { return sec1->address() < sec2->address(); }
1484 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1485 unsigned int data_shndx,
1487 unsigned int r_type,
1490 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1491 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
1497 // Return whether this branch is going via a plt call stub, and if
1498 // so, mark it as having an R_PPC64_TOCSAVE.
1500 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1501 unsigned int shndx, Address offset,
1502 Target_powerpc* target, Symbol_table* symtab);
1504 // If this branch needs a plt call stub, or a long branch stub, make one.
1506 make_stub(Stub_table<size, big_endian>*,
1507 Stub_table<size, big_endian>*,
1508 Symbol_table*) const;
1511 // The branch location..
1512 Powerpc_relobj<size, big_endian>* object_;
1513 unsigned int shndx_;
1515 // ..and the branch type and destination.
1516 unsigned int r_type_ : 31;
1517 unsigned int tocsave_ : 1;
1518 unsigned int r_sym_;
1522 // Information about this specific target which we pass to the
1523 // general Target structure.
1524 static Target::Target_info powerpc_info;
1526 // The types of GOT entries needed for this platform.
1527 // These values are exposed to the ABI in an incremental link.
1528 // Do not renumber existing values without changing the version
1529 // number of the .gnu_incremental_inputs section.
1533 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1534 GOT_TYPE_DTPREL, // entry for @got@dtprel
1535 GOT_TYPE_TPREL // entry for @got@tprel
1539 Output_data_got_powerpc<size, big_endian>* got_;
1540 // The PLT section. This is a container for a table of addresses,
1541 // and their relocations. Each address in the PLT has a dynamic
1542 // relocation (R_*_JMP_SLOT) and each address will have a
1543 // corresponding entry in .glink for lazy resolution of the PLT.
1544 // ppc32 initialises the PLT to point at the .glink entry, while
1545 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1546 // linker adds a stub that loads the PLT entry into ctr then
1547 // branches to ctr. There may be more than one stub for each PLT
1548 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1549 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1550 Output_data_plt_powerpc<size, big_endian>* plt_;
1551 // The IPLT section. Like plt_, this is a container for a table of
1552 // addresses and their relocations, specifically for STT_GNU_IFUNC
1553 // functions that resolve locally (STT_GNU_IFUNC functions that
1554 // don't resolve locally go in PLT). Unlike plt_, these have no
1555 // entry in .glink for lazy resolution, and the relocation section
1556 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1557 // the relocation section may contain relocations against
1558 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1559 // relocation section will appear at the end of other dynamic
1560 // relocations, so that ld.so applies these relocations after other
1561 // dynamic relocations. In a static executable, the relocation
1562 // section is emitted and marked with __rela_iplt_start and
1563 // __rela_iplt_end symbols.
1564 Output_data_plt_powerpc<size, big_endian>* iplt_;
1565 // Section holding long branch destinations.
1566 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1567 // The .glink section.
1568 Output_data_glink<size, big_endian>* glink_;
1569 // The dynamic reloc section.
1570 Reloc_section* rela_dyn_;
1571 // Relocs saved to avoid a COPY reloc.
1572 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1573 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1574 unsigned int tlsld_got_offset_;
1576 Stub_tables stub_tables_;
1577 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1578 Branch_lookup_table branch_lookup_table_;
1580 typedef std::vector<Branch_info> Branches;
1581 Branches branch_info_;
1582 Tocsave_loc tocsave_loc_;
1584 bool plt_thread_safe_;
1585 bool plt_localentry0_;
1586 bool plt_localentry0_init_;
1587 bool has_localentry0_;
1588 bool has_tls_get_addr_opt_;
1591 int relax_fail_count_;
1592 int32_t stub_group_size_;
1594 Output_data_save_res<size, big_endian> *savres_section_;
1596 // The "__tls_get_addr" symbol, if present
1597 Symbol* tls_get_addr_;
1598 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1599 Symbol* tls_get_addr_opt_;
1603 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1606 true, // is_big_endian
1607 elfcpp::EM_PPC, // machine_code
1608 false, // has_make_symbol
1609 false, // has_resolve
1610 false, // has_code_fill
1611 true, // is_default_stack_executable
1612 false, // can_icf_inline_merge_sections
1614 "/usr/lib/ld.so.1", // dynamic_linker
1615 0x10000000, // default_text_segment_address
1616 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1617 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1618 false, // isolate_execinstr
1620 elfcpp::SHN_UNDEF, // small_common_shndx
1621 elfcpp::SHN_UNDEF, // large_common_shndx
1622 0, // small_common_section_flags
1623 0, // large_common_section_flags
1624 NULL, // attributes_section
1625 NULL, // attributes_vendor
1626 "_start", // entry_symbol_name
1627 32, // hash_entry_size
1631 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1634 false, // is_big_endian
1635 elfcpp::EM_PPC, // machine_code
1636 false, // has_make_symbol
1637 false, // has_resolve
1638 false, // has_code_fill
1639 true, // is_default_stack_executable
1640 false, // can_icf_inline_merge_sections
1642 "/usr/lib/ld.so.1", // dynamic_linker
1643 0x10000000, // default_text_segment_address
1644 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1645 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1646 false, // isolate_execinstr
1648 elfcpp::SHN_UNDEF, // small_common_shndx
1649 elfcpp::SHN_UNDEF, // large_common_shndx
1650 0, // small_common_section_flags
1651 0, // large_common_section_flags
1652 NULL, // attributes_section
1653 NULL, // attributes_vendor
1654 "_start", // entry_symbol_name
1655 32, // hash_entry_size
1659 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1662 true, // is_big_endian
1663 elfcpp::EM_PPC64, // machine_code
1664 false, // has_make_symbol
1665 true, // has_resolve
1666 false, // has_code_fill
1667 false, // is_default_stack_executable
1668 false, // can_icf_inline_merge_sections
1670 "/usr/lib/ld.so.1", // dynamic_linker
1671 0x10000000, // default_text_segment_address
1672 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1673 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1674 false, // isolate_execinstr
1676 elfcpp::SHN_UNDEF, // small_common_shndx
1677 elfcpp::SHN_UNDEF, // large_common_shndx
1678 0, // small_common_section_flags
1679 0, // large_common_section_flags
1680 NULL, // attributes_section
1681 NULL, // attributes_vendor
1682 "_start", // entry_symbol_name
1683 32, // hash_entry_size
1687 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1690 false, // is_big_endian
1691 elfcpp::EM_PPC64, // machine_code
1692 false, // has_make_symbol
1693 true, // has_resolve
1694 false, // has_code_fill
1695 false, // is_default_stack_executable
1696 false, // can_icf_inline_merge_sections
1698 "/usr/lib/ld.so.1", // dynamic_linker
1699 0x10000000, // default_text_segment_address
1700 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1701 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1702 false, // isolate_execinstr
1704 elfcpp::SHN_UNDEF, // small_common_shndx
1705 elfcpp::SHN_UNDEF, // large_common_shndx
1706 0, // small_common_section_flags
1707 0, // large_common_section_flags
1708 NULL, // attributes_section
1709 NULL, // attributes_vendor
1710 "_start", // entry_symbol_name
1711 32, // hash_entry_size
1715 is_branch_reloc(unsigned int r_type)
1717 return (r_type == elfcpp::R_POWERPC_REL24
1718 || r_type == elfcpp::R_PPC_PLTREL24
1719 || r_type == elfcpp::R_PPC_LOCAL24PC
1720 || r_type == elfcpp::R_POWERPC_REL14
1721 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1722 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1723 || r_type == elfcpp::R_POWERPC_ADDR24
1724 || r_type == elfcpp::R_POWERPC_ADDR14
1725 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1726 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1729 // If INSN is an opcode that may be used with an @tls operand, return
1730 // the transformed insn for TLS optimisation, otherwise return 0. If
1731 // REG is non-zero only match an insn with RB or RA equal to REG.
1733 at_tls_transform(uint32_t insn, unsigned int reg)
1735 if ((insn & (0x3f << 26)) != 31 << 26)
1739 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1740 rtra = insn & ((1 << 26) - (1 << 16));
1741 else if (((insn >> 16) & 0x1f) == reg)
1742 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1746 if ((insn & (0x3ff << 1)) == 266 << 1)
1749 else if ((insn & (0x1f << 1)) == 23 << 1
1750 && ((insn & (0x1f << 6)) < 14 << 6
1751 || ((insn & (0x1f << 6)) >= 16 << 6
1752 && (insn & (0x1f << 6)) < 24 << 6)))
1753 // load and store indexed -> dform
1754 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1755 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1756 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1757 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1758 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1760 insn = (58 << 26) | 2;
1768 template<int size, bool big_endian>
1769 class Powerpc_relocate_functions
1789 typedef Powerpc_relocate_functions<size, big_endian> This;
1790 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1791 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
1793 template<int valsize>
1795 has_overflow_signed(Address value)
1797 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1798 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1799 limit <<= ((valsize - 1) >> 1);
1800 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1801 return value + limit > (limit << 1) - 1;
1804 template<int valsize>
1806 has_overflow_unsigned(Address value)
1808 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1809 limit <<= ((valsize - 1) >> 1);
1810 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1811 return value > (limit << 1) - 1;
1814 template<int valsize>
1816 has_overflow_bitfield(Address value)
1818 return (has_overflow_unsigned<valsize>(value)
1819 && has_overflow_signed<valsize>(value));
1822 template<int valsize>
1823 static inline Status
1824 overflowed(Address value, Overflow_check overflow)
1826 if (overflow == CHECK_SIGNED)
1828 if (has_overflow_signed<valsize>(value))
1829 return STATUS_OVERFLOW;
1831 else if (overflow == CHECK_UNSIGNED)
1833 if (has_overflow_unsigned<valsize>(value))
1834 return STATUS_OVERFLOW;
1836 else if (overflow == CHECK_BITFIELD)
1838 if (has_overflow_bitfield<valsize>(value))
1839 return STATUS_OVERFLOW;
1844 // Do a simple RELA relocation
1845 template<int fieldsize, int valsize>
1846 static inline Status
1847 rela(unsigned char* view, Address value, Overflow_check overflow)
1849 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1850 Valtype* wv = reinterpret_cast<Valtype*>(view);
1851 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
1852 return overflowed<valsize>(value, overflow);
1855 template<int fieldsize, int valsize>
1856 static inline Status
1857 rela(unsigned char* view,
1858 unsigned int right_shift,
1859 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1861 Overflow_check overflow)
1863 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1864 Valtype* wv = reinterpret_cast<Valtype*>(view);
1865 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
1866 Valtype reloc = value >> right_shift;
1869 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
1870 return overflowed<valsize>(value >> right_shift, overflow);
1873 // Do a simple RELA relocation, unaligned.
1874 template<int fieldsize, int valsize>
1875 static inline Status
1876 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1878 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
1879 return overflowed<valsize>(value, overflow);
1882 template<int fieldsize, int valsize>
1883 static inline Status
1884 rela_ua(unsigned char* view,
1885 unsigned int right_shift,
1886 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1888 Overflow_check overflow)
1890 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
1892 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
1893 Valtype reloc = value >> right_shift;
1896 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
1897 return overflowed<valsize>(value >> right_shift, overflow);
1901 // R_PPC64_ADDR64: (Symbol + Addend)
1903 addr64(unsigned char* view, Address value)
1904 { This::template rela<64,64>(view, value, CHECK_NONE); }
1906 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1908 addr64_u(unsigned char* view, Address value)
1909 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
1911 // R_POWERPC_ADDR32: (Symbol + Addend)
1912 static inline Status
1913 addr32(unsigned char* view, Address value, Overflow_check overflow)
1914 { return This::template rela<32,32>(view, value, overflow); }
1916 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1917 static inline Status
1918 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1919 { return This::template rela_ua<32,32>(view, value, overflow); }
1921 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1922 static inline Status
1923 addr24(unsigned char* view, Address value, Overflow_check overflow)
1925 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
1927 if (overflow != CHECK_NONE && (value & 3) != 0)
1928 stat = STATUS_OVERFLOW;
1932 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1933 static inline Status
1934 addr16(unsigned char* view, Address value, Overflow_check overflow)
1935 { return This::template rela<16,16>(view, value, overflow); }
1937 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1938 static inline Status
1939 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1940 { return This::template rela_ua<16,16>(view, value, overflow); }
1942 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1943 static inline Status
1944 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1946 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
1947 if ((value & 3) != 0)
1948 stat = STATUS_OVERFLOW;
1952 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
1953 static inline Status
1954 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
1956 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
1957 if ((value & 15) != 0)
1958 stat = STATUS_OVERFLOW;
1962 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1964 addr16_hi(unsigned char* view, Address value)
1965 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
1967 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1969 addr16_ha(unsigned char* view, Address value)
1970 { This::addr16_hi(view, value + 0x8000); }
1972 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1974 addr16_hi2(unsigned char* view, Address value)
1975 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
1977 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1979 addr16_ha2(unsigned char* view, Address value)
1980 { This::addr16_hi2(view, value + 0x8000); }
1982 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1984 addr16_hi3(unsigned char* view, Address value)
1985 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
1987 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1989 addr16_ha3(unsigned char* view, Address value)
1990 { This::addr16_hi3(view, value + 0x8000); }
1992 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1993 static inline Status
1994 addr14(unsigned char* view, Address value, Overflow_check overflow)
1996 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
1997 if (overflow != CHECK_NONE && (value & 3) != 0)
1998 stat = STATUS_OVERFLOW;
2002 // R_POWERPC_REL16DX_HA
2003 static inline Status
2004 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2006 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2007 Valtype* wv = reinterpret_cast<Valtype*>(view);
2008 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2010 value = static_cast<SignedAddress>(value) >> 16;
2011 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2012 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2013 return overflowed<16>(value, overflow);
2017 // Set ABI version for input and output.
2019 template<int size, bool big_endian>
2021 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2023 this->e_flags_ |= ver;
2024 if (this->abiversion() != 0)
2026 Target_powerpc<size, big_endian>* target =
2027 static_cast<Target_powerpc<size, big_endian>*>(
2028 parameters->sized_target<size, big_endian>());
2029 if (target->abiversion() == 0)
2030 target->set_abiversion(this->abiversion());
2031 else if (target->abiversion() != this->abiversion())
2032 gold_error(_("%s: ABI version %d is not compatible "
2033 "with ABI version %d output"),
2034 this->name().c_str(),
2035 this->abiversion(), target->abiversion());
2040 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2041 // relocatable object, if such sections exists.
2043 template<int size, bool big_endian>
2045 Powerpc_relobj<size, big_endian>::do_find_special_sections(
2046 Read_symbols_data* sd)
2048 const unsigned char* const pshdrs = sd->section_headers->data();
2049 const unsigned char* namesu = sd->section_names->data();
2050 const char* names = reinterpret_cast<const char*>(namesu);
2051 section_size_type names_size = sd->section_names_size;
2052 const unsigned char* s;
2054 s = this->template find_shdr<size, big_endian>(pshdrs,
2055 size == 32 ? ".got2" : ".opd",
2056 names, names_size, NULL);
2059 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2060 this->special_ = ndx;
2063 if (this->abiversion() == 0)
2064 this->set_abiversion(1);
2065 else if (this->abiversion() > 1)
2066 gold_error(_("%s: .opd invalid in abiv%d"),
2067 this->name().c_str(), this->abiversion());
2072 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2073 names, names_size, NULL);
2076 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2077 this->relatoc_ = ndx;
2078 typename elfcpp::Shdr<size, big_endian> shdr(s);
2079 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2082 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2085 // Examine .rela.opd to build info about function entry points.
2087 template<int size, bool big_endian>
2089 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2091 const unsigned char* prelocs,
2092 const unsigned char* plocal_syms)
2096 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2097 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2098 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2099 Address expected_off = 0;
2100 bool regular = true;
2101 unsigned int opd_ent_size = 0;
2103 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2105 Reltype reloc(prelocs);
2106 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2107 = reloc.get_r_info();
2108 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2109 if (r_type == elfcpp::R_PPC64_ADDR64)
2111 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2112 typename elfcpp::Elf_types<size>::Elf_Addr value;
2115 if (r_sym < this->local_symbol_count())
2117 typename elfcpp::Sym<size, big_endian>
2118 lsym(plocal_syms + r_sym * sym_size);
2119 shndx = lsym.get_st_shndx();
2120 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2121 value = lsym.get_st_value();
2124 shndx = this->symbol_section_and_value(r_sym, &value,
2126 this->set_opd_ent(reloc.get_r_offset(), shndx,
2127 value + reloc.get_r_addend());
2130 expected_off = reloc.get_r_offset();
2131 opd_ent_size = expected_off;
2133 else if (expected_off != reloc.get_r_offset())
2135 expected_off += opd_ent_size;
2137 else if (r_type == elfcpp::R_PPC64_TOC)
2139 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2144 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2145 this->name().c_str(), r_type);
2149 if (reloc_count <= 2)
2150 opd_ent_size = this->section_size(this->opd_shndx());
2151 if (opd_ent_size != 24 && opd_ent_size != 16)
2155 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2156 this->name().c_str());
2162 // Returns true if a code sequence loading the TOC entry at VALUE
2163 // relative to the TOC pointer can be converted into code calculating
2164 // a TOC pointer relative offset.
2165 // If so, the TOC pointer relative offset is stored to VALUE.
2167 template<int size, bool big_endian>
2169 Powerpc_relobj<size, big_endian>::make_toc_relative(
2170 Target_powerpc<size, big_endian>* target,
2176 // With -mcmodel=medium code it is quite possible to have
2177 // toc-relative relocs referring to objects outside the TOC.
2178 // Don't try to look at a non-existent TOC.
2179 if (this->toc_shndx() == 0)
2182 // Convert VALUE back to an address by adding got_base (see below),
2183 // then to an offset in the TOC by subtracting the TOC output
2184 // section address and the TOC output offset. Since this TOC output
2185 // section and the got output section are one and the same, we can
2186 // omit adding and subtracting the output section address.
2187 Address off = (*value + this->toc_base_offset()
2188 - this->output_section_offset(this->toc_shndx()));
2189 // Is this offset in the TOC? -mcmodel=medium code may be using
2190 // TOC relative access to variables outside the TOC. Those of
2191 // course can't be optimized. We also don't try to optimize code
2192 // that is using a different object's TOC.
2193 if (off >= this->section_size(this->toc_shndx()))
2196 if (this->no_toc_opt(off))
2199 section_size_type vlen;
2200 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2201 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2203 Address got_base = (target->got_section()->output_section()->address()
2204 + this->toc_base_offset());
2206 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2213 // Perform the Sized_relobj_file method, then set up opd info from
2216 template<int size, bool big_endian>
2218 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2220 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2223 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2224 p != rd->relocs.end();
2227 if (p->data_shndx == this->opd_shndx())
2229 uint64_t opd_size = this->section_size(this->opd_shndx());
2230 gold_assert(opd_size == static_cast<size_t>(opd_size));
2233 this->init_opd(opd_size);
2234 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2235 rd->local_symbols->data());
2243 // Read the symbols then set up st_other vector.
2245 template<int size, bool big_endian>
2247 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2249 this->base_read_symbols(sd);
2252 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2253 const unsigned char* const pshdrs = sd->section_headers->data();
2254 const unsigned int loccount = this->do_local_symbol_count();
2257 this->st_other_.resize(loccount);
2258 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2259 off_t locsize = loccount * sym_size;
2260 const unsigned int symtab_shndx = this->symtab_shndx();
2261 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2262 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2263 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2264 locsize, true, false);
2266 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2268 elfcpp::Sym<size, big_endian> sym(psyms);
2269 unsigned char st_other = sym.get_st_other();
2270 this->st_other_[i] = st_other;
2271 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2273 if (this->abiversion() == 0)
2274 this->set_abiversion(2);
2275 else if (this->abiversion() < 2)
2276 gold_error(_("%s: local symbol %d has invalid st_other"
2277 " for ABI version 1"),
2278 this->name().c_str(), i);
2285 template<int size, bool big_endian>
2287 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2289 this->e_flags_ |= ver;
2290 if (this->abiversion() != 0)
2292 Target_powerpc<size, big_endian>* target =
2293 static_cast<Target_powerpc<size, big_endian>*>(
2294 parameters->sized_target<size, big_endian>());
2295 if (target->abiversion() == 0)
2296 target->set_abiversion(this->abiversion());
2297 else if (target->abiversion() != this->abiversion())
2298 gold_error(_("%s: ABI version %d is not compatible "
2299 "with ABI version %d output"),
2300 this->name().c_str(),
2301 this->abiversion(), target->abiversion());
2306 // Call Sized_dynobj::base_read_symbols to read the symbols then
2307 // read .opd from a dynamic object, filling in opd_ent_ vector,
2309 template<int size, bool big_endian>
2311 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2313 this->base_read_symbols(sd);
2316 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2317 const unsigned char* const pshdrs = sd->section_headers->data();
2318 const unsigned char* namesu = sd->section_names->data();
2319 const char* names = reinterpret_cast<const char*>(namesu);
2320 const unsigned char* s = NULL;
2321 const unsigned char* opd;
2322 section_size_type opd_size;
2324 // Find and read .opd section.
2327 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2328 sd->section_names_size,
2333 typename elfcpp::Shdr<size, big_endian> shdr(s);
2334 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2335 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2337 if (this->abiversion() == 0)
2338 this->set_abiversion(1);
2339 else if (this->abiversion() > 1)
2340 gold_error(_("%s: .opd invalid in abiv%d"),
2341 this->name().c_str(), this->abiversion());
2343 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2344 this->opd_address_ = shdr.get_sh_addr();
2345 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2346 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2352 // Build set of executable sections.
2353 // Using a set is probably overkill. There is likely to be only
2354 // a few executable sections, typically .init, .text and .fini,
2355 // and they are generally grouped together.
2356 typedef std::set<Sec_info> Exec_sections;
2357 Exec_sections exec_sections;
2359 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2361 typename elfcpp::Shdr<size, big_endian> shdr(s);
2362 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2363 && ((shdr.get_sh_flags()
2364 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2365 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2366 && shdr.get_sh_size() != 0)
2368 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2369 shdr.get_sh_size(), i));
2372 if (exec_sections.empty())
2375 // Look over the OPD entries. This is complicated by the fact
2376 // that some binaries will use two-word entries while others
2377 // will use the standard three-word entries. In most cases
2378 // the third word (the environment pointer for languages like
2379 // Pascal) is unused and will be zero. If the third word is
2380 // used it should not be pointing into executable sections,
2382 this->init_opd(opd_size);
2383 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2385 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2386 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2387 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2389 // Chances are that this is the third word of an OPD entry.
2391 typename Exec_sections::const_iterator e
2392 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2393 if (e != exec_sections.begin())
2396 if (e->start <= val && val < e->start + e->len)
2398 // We have an address in an executable section.
2399 // VAL ought to be the function entry, set it up.
2400 this->set_opd_ent(p - opd, e->shndx, val);
2401 // Skip second word of OPD entry, the TOC pointer.
2405 // If we didn't match any executable sections, we likely
2406 // have a non-zero third word in the OPD entry.
2411 // Relocate sections.
2413 template<int size, bool big_endian>
2415 Powerpc_relobj<size, big_endian>::do_relocate_sections(
2416 const Symbol_table* symtab, const Layout* layout,
2417 const unsigned char* pshdrs, Output_file* of,
2418 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2420 unsigned int start = 1;
2422 && this->relatoc_ != 0
2423 && !parameters->options().relocatable())
2425 // Relocate .toc first.
2426 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2427 this->relatoc_, this->relatoc_);
2428 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2429 1, this->relatoc_ - 1);
2430 start = this->relatoc_ + 1;
2432 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2433 start, this->shnum() - 1);
2436 // Set up some symbols.
2438 template<int size, bool big_endian>
2440 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2441 Symbol_table* symtab,
2446 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2447 // undefined when scanning relocs (and thus requires
2448 // non-relative dynamic relocs). The proper value will be
2450 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2451 if (gotsym != NULL && gotsym->is_undefined())
2453 Target_powerpc<size, big_endian>* target =
2454 static_cast<Target_powerpc<size, big_endian>*>(
2455 parameters->sized_target<size, big_endian>());
2456 Output_data_got_powerpc<size, big_endian>* got
2457 = target->got_section(symtab, layout);
2458 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2459 Symbol_table::PREDEFINED,
2463 elfcpp::STV_HIDDEN, 0,
2467 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2468 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2469 if (sdasym != NULL && sdasym->is_undefined())
2471 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2473 = layout->add_output_section_data(".sdata", 0,
2475 | elfcpp::SHF_WRITE,
2476 sdata, ORDER_SMALL_DATA, false);
2477 symtab->define_in_output_data("_SDA_BASE_", NULL,
2478 Symbol_table::PREDEFINED,
2479 os, 32768, 0, elfcpp::STT_OBJECT,
2480 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2486 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2487 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2488 if (gotsym != NULL && gotsym->is_undefined())
2490 Target_powerpc<size, big_endian>* target =
2491 static_cast<Target_powerpc<size, big_endian>*>(
2492 parameters->sized_target<size, big_endian>());
2493 Output_data_got_powerpc<size, big_endian>* got
2494 = target->got_section(symtab, layout);
2495 symtab->define_in_output_data(".TOC.", NULL,
2496 Symbol_table::PREDEFINED,
2500 elfcpp::STV_HIDDEN, 0,
2505 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2506 if (parameters->options().tls_get_addr_optimize()
2507 && this->tls_get_addr_ != NULL
2508 && this->tls_get_addr_->in_reg())
2509 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2510 if (this->tls_get_addr_opt_ != NULL)
2512 if (this->tls_get_addr_->is_undefined()
2513 || this->tls_get_addr_->is_from_dynobj())
2515 // Make it seem as if references to __tls_get_addr are
2516 // really to __tls_get_addr_opt, so the latter symbol is
2517 // made dynamic, not the former.
2518 this->tls_get_addr_->clear_in_reg();
2519 this->tls_get_addr_opt_->set_in_reg();
2521 // We have a non-dynamic definition for __tls_get_addr.
2522 // Make __tls_get_addr_opt the same, if it does not already have
2523 // a non-dynamic definition.
2524 else if (this->tls_get_addr_opt_->is_undefined()
2525 || this->tls_get_addr_opt_->is_from_dynobj())
2527 Sized_symbol<size>* from
2528 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2529 Sized_symbol<size>* to
2530 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2531 symtab->clone<size>(to, from);
2536 // Set up PowerPC target specific relobj.
2538 template<int size, bool big_endian>
2540 Target_powerpc<size, big_endian>::do_make_elf_object(
2541 const std::string& name,
2542 Input_file* input_file,
2543 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2545 int et = ehdr.get_e_type();
2546 // ET_EXEC files are valid input for --just-symbols/-R,
2547 // and we treat them as relocatable objects.
2548 if (et == elfcpp::ET_REL
2549 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2551 Powerpc_relobj<size, big_endian>* obj =
2552 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2556 else if (et == elfcpp::ET_DYN)
2558 Powerpc_dynobj<size, big_endian>* obj =
2559 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2565 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2570 template<int size, bool big_endian>
2571 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2574 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2575 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2577 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2578 : Output_data_got<size, big_endian>(),
2579 symtab_(symtab), layout_(layout),
2580 header_ent_cnt_(size == 32 ? 3 : 1),
2581 header_index_(size == 32 ? 0x2000 : 0)
2584 this->set_addralign(256);
2587 // Override all the Output_data_got methods we use so as to first call
2590 add_global(Symbol* gsym, unsigned int got_type)
2592 this->reserve_ent();
2593 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2597 add_global_plt(Symbol* gsym, unsigned int got_type)
2599 this->reserve_ent();
2600 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2604 add_global_tls(Symbol* gsym, unsigned int got_type)
2605 { return this->add_global_plt(gsym, got_type); }
2608 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2609 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2611 this->reserve_ent();
2612 Output_data_got<size, big_endian>::
2613 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2617 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2618 Output_data_reloc_generic* rel_dyn,
2619 unsigned int r_type_1, unsigned int r_type_2)
2621 if (gsym->has_got_offset(got_type))
2624 this->reserve_ent(2);
2625 Output_data_got<size, big_endian>::
2626 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2630 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2632 this->reserve_ent();
2633 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2638 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2640 this->reserve_ent();
2641 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2646 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2647 { return this->add_local_plt(object, sym_index, got_type); }
2650 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2651 unsigned int got_type,
2652 Output_data_reloc_generic* rel_dyn,
2653 unsigned int r_type)
2655 if (object->local_has_got_offset(sym_index, got_type))
2658 this->reserve_ent(2);
2659 Output_data_got<size, big_endian>::
2660 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2664 add_constant(Valtype constant)
2666 this->reserve_ent();
2667 return Output_data_got<size, big_endian>::add_constant(constant);
2671 add_constant_pair(Valtype c1, Valtype c2)
2673 this->reserve_ent(2);
2674 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
2677 // Offset of _GLOBAL_OFFSET_TABLE_.
2681 return this->got_offset(this->header_index_);
2684 // Offset of base used to access the GOT/TOC.
2685 // The got/toc pointer reg will be set to this value.
2687 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2690 return this->g_o_t();
2692 return (this->output_section()->address()
2693 + object->toc_base_offset()
2697 // Ensure our GOT has a header.
2699 set_final_data_size()
2701 if (this->header_ent_cnt_ != 0)
2702 this->make_header();
2703 Output_data_got<size, big_endian>::set_final_data_size();
2706 // First word of GOT header needs some values that are not
2707 // handled by Output_data_got so poke them in here.
2708 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2710 do_write(Output_file* of)
2713 if (size == 32 && this->layout_->dynamic_data() != NULL)
2714 val = this->layout_->dynamic_section()->address();
2716 val = this->output_section()->address() + 0x8000;
2717 this->replace_constant(this->header_index_, val);
2718 Output_data_got<size, big_endian>::do_write(of);
2723 reserve_ent(unsigned int cnt = 1)
2725 if (this->header_ent_cnt_ == 0)
2727 if (this->num_entries() + cnt > this->header_index_)
2728 this->make_header();
2734 this->header_ent_cnt_ = 0;
2735 this->header_index_ = this->num_entries();
2738 Output_data_got<size, big_endian>::add_constant(0);
2739 Output_data_got<size, big_endian>::add_constant(0);
2740 Output_data_got<size, big_endian>::add_constant(0);
2742 // Define _GLOBAL_OFFSET_TABLE_ at the header
2743 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2746 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
2747 sym->set_value(this->g_o_t());
2750 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2751 Symbol_table::PREDEFINED,
2752 this, this->g_o_t(), 0,
2755 elfcpp::STV_HIDDEN, 0,
2759 Output_data_got<size, big_endian>::add_constant(0);
2762 // Stashed pointers.
2763 Symbol_table* symtab_;
2767 unsigned int header_ent_cnt_;
2768 // GOT header index.
2769 unsigned int header_index_;
2772 // Get the GOT section, creating it if necessary.
2774 template<int size, bool big_endian>
2775 Output_data_got_powerpc<size, big_endian>*
2776 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
2779 if (this->got_ == NULL)
2781 gold_assert(symtab != NULL && layout != NULL);
2784 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
2786 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2787 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2788 this->got_, ORDER_DATA, false);
2794 // Get the dynamic reloc section, creating it if necessary.
2796 template<int size, bool big_endian>
2797 typename Target_powerpc<size, big_endian>::Reloc_section*
2798 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
2800 if (this->rela_dyn_ == NULL)
2802 gold_assert(layout != NULL);
2803 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2804 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2805 elfcpp::SHF_ALLOC, this->rela_dyn_,
2806 ORDER_DYNAMIC_RELOCS, false);
2808 return this->rela_dyn_;
2811 // Similarly, but for ifunc symbols get the one for ifunc.
2813 template<int size, bool big_endian>
2814 typename Target_powerpc<size, big_endian>::Reloc_section*
2815 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
2820 return this->rela_dyn_section(layout);
2822 if (this->iplt_ == NULL)
2823 this->make_iplt_section(symtab, layout);
2824 return this->iplt_->rel_plt();
2830 // Determine the stub group size. The group size is the absolute
2831 // value of the parameter --stub-group-size. If --stub-group-size
2832 // is passed a negative value, we restrict stubs to be always after
2833 // the stubbed branches.
2834 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
2835 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
2836 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
2837 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
2838 owner_(NULL), output_section_(NULL)
2842 // Return true iff input section can be handled by current stub
2845 can_add_to_stub_group(Output_section* o,
2846 const Output_section::Input_section* i,
2849 const Output_section::Input_section*
2855 { return output_section_; }
2858 set_output_and_owner(Output_section* o,
2859 const Output_section::Input_section* i)
2861 this->output_section_ = o;
2870 // Adding group sections before the stubs.
2871 FINDING_STUB_SECTION,
2872 // Adding group sections after the stubs.
2876 uint32_t stub_group_size_;
2877 bool stubs_always_after_branch_;
2878 bool suppress_size_errors_;
2879 // True if a stub group can serve multiple output sections.
2882 // Current max size of group. Starts at stub_group_size_ but is
2883 // reduced to stub_group_size_/1024 on seeing a section with
2884 // external conditional branches.
2885 uint32_t group_size_;
2886 uint64_t group_start_addr_;
2887 // owner_ and output_section_ specify the section to which stubs are
2888 // attached. The stubs are placed at the end of this section.
2889 const Output_section::Input_section* owner_;
2890 Output_section* output_section_;
2893 // Return true iff input section can be handled by current stub
2894 // group. Sections are presented to this function in order,
2895 // so the first section is the head of the group.
2898 Stub_control::can_add_to_stub_group(Output_section* o,
2899 const Output_section::Input_section* i,
2902 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
2904 uint64_t start_addr = o->address();
2907 // .init and .fini sections are pasted together to form a single
2908 // function. We can't be adding stubs in the middle of the function.
2909 this_size = o->data_size();
2912 start_addr += i->relobj()->output_section_offset(i->shndx());
2913 this_size = i->data_size();
2916 uint64_t end_addr = start_addr + this_size;
2917 uint32_t group_size = this->stub_group_size_;
2919 this->group_size_ = group_size = group_size >> 10;
2921 if (this_size > group_size && !this->suppress_size_errors_)
2922 gold_warning(_("%s:%s exceeds group size"),
2923 i->relobj()->name().c_str(),
2924 i->relobj()->section_name(i->shndx()).c_str());
2926 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
2927 has14 ? " 14bit" : "",
2928 i->relobj()->name().c_str(),
2929 i->relobj()->section_name(i->shndx()).c_str(),
2930 (long long) this_size,
2931 (this->state_ == NO_GROUP
2933 : (long long) end_addr - this->group_start_addr_));
2935 if (this->state_ == NO_GROUP)
2937 // Only here on very first use of Stub_control
2939 this->output_section_ = o;
2940 this->state_ = FINDING_STUB_SECTION;
2941 this->group_size_ = group_size;
2942 this->group_start_addr_ = start_addr;
2945 else if (!this->multi_os_ && this->output_section_ != o)
2947 else if (this->state_ == HAS_STUB_SECTION)
2949 // Can we add this section, which is after the stubs, to the
2951 if (end_addr - this->group_start_addr_ <= this->group_size_)
2954 else if (this->state_ == FINDING_STUB_SECTION)
2956 if ((whole_sec && this->output_section_ == o)
2957 || end_addr - this->group_start_addr_ <= this->group_size_)
2959 // Stubs are added at the end of "owner_".
2961 this->output_section_ = o;
2964 // The group before the stubs has reached maximum size.
2965 // Now see about adding sections after the stubs to the
2966 // group. If the current section has a 14-bit branch and
2967 // the group before the stubs exceeds group_size_ (because
2968 // they didn't have 14-bit branches), don't add sections
2969 // after the stubs: The size of stubs for such a large
2970 // group may exceed the reach of a 14-bit branch.
2971 if (!this->stubs_always_after_branch_
2972 && this_size <= this->group_size_
2973 && start_addr - this->group_start_addr_ <= this->group_size_)
2975 gold_debug(DEBUG_TARGET, "adding after stubs");
2976 this->state_ = HAS_STUB_SECTION;
2977 this->group_start_addr_ = start_addr;
2984 gold_debug(DEBUG_TARGET,
2985 !this->multi_os_ && this->output_section_ != o
2986 ? "nope, new output section\n"
2987 : "nope, didn't fit\n");
2989 // The section fails to fit in the current group. Set up a few
2990 // things for the next group. owner_ and output_section_ will be
2991 // set later after we've retrieved those values for the current
2993 this->state_ = FINDING_STUB_SECTION;
2994 this->group_size_ = group_size;
2995 this->group_start_addr_ = start_addr;
2999 // Look over all the input sections, deciding where to place stubs.
3001 template<int size, bool big_endian>
3003 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
3005 bool no_size_errors)
3007 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3008 parameters->options().stub_group_multi());
3010 // Group input sections and insert stub table
3011 Stub_table_owner* table_owner = NULL;
3012 std::vector<Stub_table_owner*> tables;
3013 Layout::Section_list section_list;
3014 layout->get_executable_sections(§ion_list);
3015 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
3016 for (Layout::Section_list::iterator o = section_list.begin();
3017 o != section_list.end();
3020 typedef Output_section::Input_section_list Input_section_list;
3021 for (Input_section_list::const_iterator i
3022 = (*o)->input_sections().begin();
3023 i != (*o)->input_sections().end();
3026 if (i->is_input_section()
3027 || i->is_relaxed_input_section())
3029 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3030 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3031 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3032 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3034 table_owner->output_section = stub_control.output_section();
3035 table_owner->owner = stub_control.owner();
3036 stub_control.set_output_and_owner(*o, &*i);
3039 if (table_owner == NULL)
3041 table_owner = new Stub_table_owner;
3042 tables.push_back(table_owner);
3044 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
3048 if (table_owner != NULL)
3050 table_owner->output_section = stub_control.output_section();
3051 table_owner->owner = stub_control.owner();;
3053 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3057 Stub_table<size, big_endian>* stub_table;
3059 if ((*t)->owner->is_input_section())
3060 stub_table = new Stub_table<size, big_endian>(this,
3061 (*t)->output_section,
3063 this->stub_tables_.size());
3064 else if ((*t)->owner->is_relaxed_input_section())
3065 stub_table = static_cast<Stub_table<size, big_endian>*>(
3066 (*t)->owner->relaxed_input_section());
3069 this->stub_tables_.push_back(stub_table);
3074 static unsigned long
3075 max_branch_delta (unsigned int r_type)
3077 if (r_type == elfcpp::R_POWERPC_REL14
3078 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3079 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3081 if (r_type == elfcpp::R_POWERPC_REL24
3082 || r_type == elfcpp::R_PPC_PLTREL24
3083 || r_type == elfcpp::R_PPC_LOCAL24PC)
3088 // Return whether this branch is going via a plt call stub.
3090 template<int size, bool big_endian>
3092 Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3093 Powerpc_relobj<size, big_endian>* ppc_object,
3096 Target_powerpc* target,
3097 Symbol_table* symtab)
3099 if (this->object_ != ppc_object
3100 || this->shndx_ != shndx
3101 || this->offset_ != offset)
3104 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3105 if (sym != NULL && sym->is_forwarder())
3106 sym = symtab->resolve_forwards(sym);
3107 if (target->replace_tls_get_addr(sym))
3108 sym = target->tls_get_addr_opt();
3109 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3111 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3112 && !target->is_elfv2_localentry0(gsym))
3113 : (this->object_->local_has_plt_offset(this->r_sym_)
3114 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
3122 // If this branch needs a plt call stub, or a long branch stub, make one.
3124 template<int size, bool big_endian>
3126 Target_powerpc<size, big_endian>::Branch_info::make_stub(
3127 Stub_table<size, big_endian>* stub_table,
3128 Stub_table<size, big_endian>* ifunc_stub_table,
3129 Symbol_table* symtab) const
3131 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3132 Target_powerpc<size, big_endian>* target =
3133 static_cast<Target_powerpc<size, big_endian>*>(
3134 parameters->sized_target<size, big_endian>());
3135 if (sym != NULL && sym->is_forwarder())
3136 sym = symtab->resolve_forwards(sym);
3137 if (target->replace_tls_get_addr(sym))
3138 sym = target->tls_get_addr_opt();
3139 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3143 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3144 : this->object_->local_has_plt_offset(this->r_sym_))
3148 && target->abiversion() >= 2
3149 && !parameters->options().output_is_position_independent()
3150 && !is_branch_reloc(this->r_type_))
3151 target->glink_section()->add_global_entry(gsym);
3154 if (stub_table == NULL
3157 && !parameters->options().output_is_position_independent()
3158 && !is_branch_reloc(this->r_type_)))
3159 stub_table = this->object_->stub_table(this->shndx_);
3160 if (stub_table == NULL)
3162 // This is a ref from a data section to an ifunc symbol,
3163 // or a non-branch reloc for which we always want to use
3164 // one set of stubs for resolving function addresses.
3165 stub_table = ifunc_stub_table;
3167 gold_assert(stub_table != NULL);
3168 Address from = this->object_->get_output_section_offset(this->shndx_);
3169 if (from != invalid_address)
3170 from += (this->object_->output_section(this->shndx_)->address()
3173 ok = stub_table->add_plt_call_entry(from,
3174 this->object_, gsym,
3175 this->r_type_, this->addend_,
3178 ok = stub_table->add_plt_call_entry(from,
3179 this->object_, this->r_sym_,
3180 this->r_type_, this->addend_,
3186 Address max_branch_offset = max_branch_delta(this->r_type_);
3187 if (max_branch_offset == 0)
3189 Address from = this->object_->get_output_section_offset(this->shndx_);
3190 gold_assert(from != invalid_address);
3191 from += (this->object_->output_section(this->shndx_)->address()
3196 switch (gsym->source())
3198 case Symbol::FROM_OBJECT:
3200 Object* symobj = gsym->object();
3201 if (symobj->is_dynamic()
3202 || symobj->pluginobj() != NULL)
3205 unsigned int shndx = gsym->shndx(&is_ordinary);
3206 if (shndx == elfcpp::SHN_UNDEF)
3211 case Symbol::IS_UNDEFINED:
3217 Symbol_table::Compute_final_value_status status;
3218 to = symtab->compute_final_value<size>(gsym, &status);
3219 if (status != Symbol_table::CFVS_OK)
3222 to += this->object_->ppc64_local_entry_offset(gsym);
3226 const Symbol_value<size>* psymval
3227 = this->object_->local_symbol(this->r_sym_);
3228 Symbol_value<size> symval;
3229 if (psymval->is_section_symbol())
3230 symval.set_is_section_symbol();
3231 typedef Sized_relobj_file<size, big_endian> ObjType;
3232 typename ObjType::Compute_final_local_value_status status
3233 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3235 if (status != ObjType::CFLV_OK
3236 || !symval.has_output_value())
3238 to = symval.value(this->object_, 0);
3240 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
3242 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3243 to += this->addend_;
3244 if (stub_table == NULL)
3245 stub_table = this->object_->stub_table(this->shndx_);
3246 if (size == 64 && target->abiversion() < 2)
3248 unsigned int dest_shndx;
3249 if (!target->symval_for_branch(symtab, gsym, this->object_,
3253 Address delta = to - from;
3254 if (delta + max_branch_offset >= 2 * max_branch_offset)
3256 if (stub_table == NULL)
3258 gold_warning(_("%s:%s: branch in non-executable section,"
3259 " no long branch stub for you"),
3260 this->object_->name().c_str(),
3261 this->object_->section_name(this->shndx_).c_str());
3264 bool save_res = (size == 64
3266 && gsym->source() == Symbol::IN_OUTPUT_DATA
3267 && gsym->output_data() == target->savres_section());
3268 ok = stub_table->add_long_branch_entry(this->object_,
3270 from, to, save_res);
3274 gold_debug(DEBUG_TARGET,
3275 "branch at %s:%s+%#lx\n"
3276 "can't reach stub attached to %s:%s",
3277 this->object_->name().c_str(),
3278 this->object_->section_name(this->shndx_).c_str(),
3279 (unsigned long) this->offset_,
3280 stub_table->relobj()->name().c_str(),
3281 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3286 // Relaxation hook. This is where we do stub generation.
3288 template<int size, bool big_endian>
3290 Target_powerpc<size, big_endian>::do_relax(int pass,
3291 const Input_objects*,
3292 Symbol_table* symtab,
3296 unsigned int prev_brlt_size = 0;
3300 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3302 && this->abiversion() < 2
3304 && !parameters->options().user_set_plt_thread_safe())
3306 static const char* const thread_starter[] =
3310 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3312 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3313 "mq_notify", "create_timer",
3318 "GOMP_parallel_start",
3319 "GOMP_parallel_loop_static",
3320 "GOMP_parallel_loop_static_start",
3321 "GOMP_parallel_loop_dynamic",
3322 "GOMP_parallel_loop_dynamic_start",
3323 "GOMP_parallel_loop_guided",
3324 "GOMP_parallel_loop_guided_start",
3325 "GOMP_parallel_loop_runtime",
3326 "GOMP_parallel_loop_runtime_start",
3327 "GOMP_parallel_sections",
3328 "GOMP_parallel_sections_start",
3333 if (parameters->options().shared())
3337 for (unsigned int i = 0;
3338 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3341 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3342 thread_safe = (sym != NULL
3344 && sym->in_real_elf());
3350 this->plt_thread_safe_ = thread_safe;
3355 this->stub_group_size_ = parameters->options().stub_group_size();
3356 bool no_size_errors = true;
3357 if (this->stub_group_size_ == 1)
3358 this->stub_group_size_ = 0x1c00000;
3359 else if (this->stub_group_size_ == -1)
3360 this->stub_group_size_ = -0x1e00000;
3362 no_size_errors = false;
3363 this->group_sections(layout, task, no_size_errors);
3365 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3367 this->branch_lookup_table_.clear();
3368 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3369 p != this->stub_tables_.end();
3372 (*p)->clear_stubs(true);
3374 this->stub_tables_.clear();
3375 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3376 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3377 program_name, this->stub_group_size_);
3378 this->group_sections(layout, task, true);
3381 // We need address of stub tables valid for make_stub.
3382 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3383 p != this->stub_tables_.end();
3386 const Powerpc_relobj<size, big_endian>* object
3387 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3388 Address off = object->get_output_section_offset((*p)->shndx());
3389 gold_assert(off != invalid_address);
3390 Output_section* os = (*p)->output_section();
3391 (*p)->set_address_and_size(os, off);
3396 // Clear plt call stubs, long branch stubs and branch lookup table.
3397 prev_brlt_size = this->branch_lookup_table_.size();
3398 this->branch_lookup_table_.clear();
3399 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3400 p != this->stub_tables_.end();
3403 (*p)->clear_stubs(false);
3407 // Build all the stubs.
3408 this->relax_failed_ = false;
3409 Stub_table<size, big_endian>* ifunc_stub_table
3410 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3411 Stub_table<size, big_endian>* one_stub_table
3412 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3413 for (typename Branches::const_iterator b = this->branch_info_.begin();
3414 b != this->branch_info_.end();
3417 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3418 && !this->relax_failed_)
3420 this->relax_failed_ = true;
3421 this->relax_fail_count_++;
3422 if (this->relax_fail_count_ < 3)
3427 // Did anything change size?
3428 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3429 bool again = num_huge_branches != prev_brlt_size;
3430 if (size == 64 && num_huge_branches != 0)
3431 this->make_brlt_section(layout);
3432 if (size == 64 && again)
3433 this->brlt_section_->set_current_size(num_huge_branches);
3435 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3436 p != this->stub_tables_.rend();
3438 (*p)->remove_eh_frame(layout);
3440 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3441 p != this->stub_tables_.end();
3443 (*p)->add_eh_frame(layout);
3445 typedef Unordered_set<Output_section*> Output_sections;
3446 Output_sections os_need_update;
3447 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3448 p != this->stub_tables_.end();
3451 if ((*p)->size_update())
3454 os_need_update.insert((*p)->output_section());
3458 // Set output section offsets for all input sections in an output
3459 // section that just changed size. Anything past the stubs will
3461 for (typename Output_sections::iterator p = os_need_update.begin();
3462 p != os_need_update.end();
3465 Output_section* os = *p;
3467 typedef Output_section::Input_section_list Input_section_list;
3468 for (Input_section_list::const_iterator i = os->input_sections().begin();
3469 i != os->input_sections().end();
3472 off = align_address(off, i->addralign());
3473 if (i->is_input_section() || i->is_relaxed_input_section())
3474 i->relobj()->set_section_offset(i->shndx(), off);
3475 if (i->is_relaxed_input_section())
3477 Stub_table<size, big_endian>* stub_table
3478 = static_cast<Stub_table<size, big_endian>*>(
3479 i->relaxed_input_section());
3480 Address stub_table_size = stub_table->set_address_and_size(os, off);
3481 off += stub_table_size;
3482 // After a few iterations, set current stub table size
3483 // as min size threshold, so later stub tables can only
3486 stub_table->set_min_size_threshold(stub_table_size);
3489 off += i->data_size();
3491 // If .branch_lt is part of this output section, then we have
3492 // just done the offset adjustment.
3493 os->clear_section_offsets_need_adjustment();
3498 && num_huge_branches != 0
3499 && parameters->options().output_is_position_independent())
3501 // Fill in the BRLT relocs.
3502 this->brlt_section_->reset_brlt_sizes();
3503 for (typename Branch_lookup_table::const_iterator p
3504 = this->branch_lookup_table_.begin();
3505 p != this->branch_lookup_table_.end();
3508 this->brlt_section_->add_reloc(p->first, p->second);
3510 this->brlt_section_->finalize_brlt_sizes();
3514 && (parameters->options().user_set_emit_stub_syms()
3515 ? parameters->options().emit_stub_syms()
3517 || parameters->options().output_is_position_independent()
3518 || parameters->options().emit_relocs())))
3520 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3521 p != this->stub_tables_.end();
3523 (*p)->define_stub_syms(symtab);
3525 if (this->glink_ != NULL)
3527 int stub_size = this->glink_->pltresolve_size();
3528 Address value = -stub_size;
3534 this->define_local(symtab, "__glink_PLTresolve",
3535 this->glink_, value, stub_size);
3538 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3545 template<int size, bool big_endian>
3547 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3548 unsigned char* oview,
3552 uint64_t address = plt->address();
3553 off_t len = plt->data_size();
3555 if (plt == this->glink_)
3557 // See Output_data_glink::do_write() for glink contents.
3560 gold_assert(parameters->doing_static_link());
3561 // Static linking may need stubs, to support ifunc and long
3562 // branches. We need to create an output section for
3563 // .eh_frame early in the link process, to have a place to
3564 // attach stub .eh_frame info. We also need to have
3565 // registered a CIE that matches the stub CIE. Both of
3566 // these requirements are satisfied by creating an FDE and
3567 // CIE for .glink, even though static linking will leave
3568 // .glink zero length.
3569 // ??? Hopefully generating an FDE with a zero address range
3570 // won't confuse anything that consumes .eh_frame info.
3572 else if (size == 64)
3574 // There is one word before __glink_PLTresolve
3578 else if (parameters->options().output_is_position_independent())
3580 // There are two FDEs for a position independent glink.
3581 // The first covers the branch table, the second
3582 // __glink_PLTresolve at the end of glink.
3583 off_t resolve_size = this->glink_->pltresolve_size();
3584 if (oview[9] == elfcpp::DW_CFA_nop)
3585 len -= resolve_size;
3588 address += len - resolve_size;
3595 // Must be a stub table.
3596 const Stub_table<size, big_endian>* stub_table
3597 = static_cast<const Stub_table<size, big_endian>*>(plt);
3598 uint64_t stub_address = stub_table->stub_address();
3599 len -= stub_address - address;
3600 address = stub_address;
3603 *paddress = address;
3607 // A class to handle the PLT data.
3609 template<int size, bool big_endian>
3610 class Output_data_plt_powerpc : public Output_section_data_build
3613 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3614 size, big_endian> Reloc_section;
3616 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
3617 Reloc_section* plt_rel,
3619 : Output_section_data_build(size == 32 ? 4 : 8),
3625 // Add an entry to the PLT.
3630 add_ifunc_entry(Symbol*);
3633 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3635 // Return the .rela.plt section data.
3642 // Return the number of PLT entries.
3646 if (this->current_data_size() == 0)
3648 return ((this->current_data_size() - this->first_plt_entry_offset())
3649 / this->plt_entry_size());
3654 do_adjust_output_section(Output_section* os)
3659 // Write to a map file.
3661 do_print_to_mapfile(Mapfile* mapfile) const
3662 { mapfile->print_output_data(this, this->name_); }
3665 // Return the offset of the first non-reserved PLT entry.
3667 first_plt_entry_offset() const
3669 // IPLT has no reserved entry.
3670 if (this->name_[3] == 'I')
3672 return this->targ_->first_plt_entry_offset();
3675 // Return the size of each PLT entry.
3677 plt_entry_size() const
3679 return this->targ_->plt_entry_size();
3682 // Write out the PLT data.
3684 do_write(Output_file*);
3686 // The reloc section.
3687 Reloc_section* rel_;
3688 // Allows access to .glink for do_write.
3689 Target_powerpc<size, big_endian>* targ_;
3690 // What to report in map file.
3694 // Add an entry to the PLT.
3696 template<int size, bool big_endian>
3698 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
3700 if (!gsym->has_plt_offset())
3702 section_size_type off = this->current_data_size();
3704 off += this->first_plt_entry_offset();
3705 gsym->set_plt_offset(off);
3706 gsym->set_needs_dynsym_entry();
3707 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3708 this->rel_->add_global(gsym, dynrel, this, off, 0);
3709 off += this->plt_entry_size();
3710 this->set_current_data_size(off);
3714 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3716 template<int size, bool big_endian>
3718 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
3720 if (!gsym->has_plt_offset())
3722 section_size_type off = this->current_data_size();
3723 gsym->set_plt_offset(off);
3724 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3725 if (size == 64 && this->targ_->abiversion() < 2)
3726 dynrel = elfcpp::R_PPC64_JMP_IREL;
3727 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
3728 off += this->plt_entry_size();
3729 this->set_current_data_size(off);
3733 // Add an entry for a local ifunc symbol to the IPLT.
3735 template<int size, bool big_endian>
3737 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
3738 Sized_relobj_file<size, big_endian>* relobj,
3739 unsigned int local_sym_index)
3741 if (!relobj->local_has_plt_offset(local_sym_index))
3743 section_size_type off = this->current_data_size();
3744 relobj->set_local_plt_offset(local_sym_index, off);
3745 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3746 if (size == 64 && this->targ_->abiversion() < 2)
3747 dynrel = elfcpp::R_PPC64_JMP_IREL;
3748 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
3750 off += this->plt_entry_size();
3751 this->set_current_data_size(off);
3755 static const uint32_t add_0_11_11 = 0x7c0b5a14;
3756 static const uint32_t add_2_2_11 = 0x7c425a14;
3757 static const uint32_t add_2_2_12 = 0x7c426214;
3758 static const uint32_t add_3_3_2 = 0x7c631214;
3759 static const uint32_t add_3_3_13 = 0x7c636a14;
3760 static const uint32_t add_3_12_2 = 0x7c6c1214;
3761 static const uint32_t add_3_12_13 = 0x7c6c6a14;
3762 static const uint32_t add_11_0_11 = 0x7d605a14;
3763 static const uint32_t add_11_2_11 = 0x7d625a14;
3764 static const uint32_t add_11_11_2 = 0x7d6b1214;
3765 static const uint32_t addi_0_12 = 0x380c0000;
3766 static const uint32_t addi_2_2 = 0x38420000;
3767 static const uint32_t addi_3_3 = 0x38630000;
3768 static const uint32_t addi_11_11 = 0x396b0000;
3769 static const uint32_t addi_12_1 = 0x39810000;
3770 static const uint32_t addi_12_12 = 0x398c0000;
3771 static const uint32_t addis_0_2 = 0x3c020000;
3772 static const uint32_t addis_0_13 = 0x3c0d0000;
3773 static const uint32_t addis_2_12 = 0x3c4c0000;
3774 static const uint32_t addis_11_2 = 0x3d620000;
3775 static const uint32_t addis_11_11 = 0x3d6b0000;
3776 static const uint32_t addis_11_30 = 0x3d7e0000;
3777 static const uint32_t addis_12_1 = 0x3d810000;
3778 static const uint32_t addis_12_2 = 0x3d820000;
3779 static const uint32_t addis_12_12 = 0x3d8c0000;
3780 static const uint32_t b = 0x48000000;
3781 static const uint32_t bcl_20_31 = 0x429f0005;
3782 static const uint32_t bctr = 0x4e800420;
3783 static const uint32_t bctrl = 0x4e800421;
3784 static const uint32_t beqctrm = 0x4dc20420;
3785 static const uint32_t beqctrlm = 0x4dc20421;
3786 static const uint32_t beqlr = 0x4d820020;
3787 static const uint32_t blr = 0x4e800020;
3788 static const uint32_t bnectr_p4 = 0x4ce20420;
3789 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
3790 static const uint32_t cmpldi_2_0 = 0x28220000;
3791 static const uint32_t cmpdi_11_0 = 0x2c2b0000;
3792 static const uint32_t cmpwi_11_0 = 0x2c0b0000;
3793 static const uint32_t cror_15_15_15 = 0x4def7b82;
3794 static const uint32_t cror_31_31_31 = 0x4ffffb82;
3795 static const uint32_t crseteq = 0x4c421242;
3796 static const uint32_t ld_0_1 = 0xe8010000;
3797 static const uint32_t ld_0_12 = 0xe80c0000;
3798 static const uint32_t ld_2_1 = 0xe8410000;
3799 static const uint32_t ld_2_2 = 0xe8420000;
3800 static const uint32_t ld_2_11 = 0xe84b0000;
3801 static const uint32_t ld_2_12 = 0xe84c0000;
3802 static const uint32_t ld_11_1 = 0xe9610000;
3803 static const uint32_t ld_11_2 = 0xe9620000;
3804 static const uint32_t ld_11_3 = 0xe9630000;
3805 static const uint32_t ld_11_11 = 0xe96b0000;
3806 static const uint32_t ld_12_2 = 0xe9820000;
3807 static const uint32_t ld_12_3 = 0xe9830000;
3808 static const uint32_t ld_12_11 = 0xe98b0000;
3809 static const uint32_t ld_12_12 = 0xe98c0000;
3810 static const uint32_t lfd_0_1 = 0xc8010000;
3811 static const uint32_t li_0_0 = 0x38000000;
3812 static const uint32_t li_12_0 = 0x39800000;
3813 static const uint32_t lis_0 = 0x3c000000;
3814 static const uint32_t lis_2 = 0x3c400000;
3815 static const uint32_t lis_11 = 0x3d600000;
3816 static const uint32_t lis_12 = 0x3d800000;
3817 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
3818 static const uint32_t lwz_0_12 = 0x800c0000;
3819 static const uint32_t lwz_11_3 = 0x81630000;
3820 static const uint32_t lwz_11_11 = 0x816b0000;
3821 static const uint32_t lwz_11_30 = 0x817e0000;
3822 static const uint32_t lwz_12_3 = 0x81830000;
3823 static const uint32_t lwz_12_12 = 0x818c0000;
3824 static const uint32_t lwzu_0_12 = 0x840c0000;
3825 static const uint32_t mflr_0 = 0x7c0802a6;
3826 static const uint32_t mflr_11 = 0x7d6802a6;
3827 static const uint32_t mflr_12 = 0x7d8802a6;
3828 static const uint32_t mr_0_3 = 0x7c601b78;
3829 static const uint32_t mr_3_0 = 0x7c030378;
3830 static const uint32_t mtctr_0 = 0x7c0903a6;
3831 static const uint32_t mtctr_11 = 0x7d6903a6;
3832 static const uint32_t mtctr_12 = 0x7d8903a6;
3833 static const uint32_t mtlr_0 = 0x7c0803a6;
3834 static const uint32_t mtlr_11 = 0x7d6803a6;
3835 static const uint32_t mtlr_12 = 0x7d8803a6;
3836 static const uint32_t nop = 0x60000000;
3837 static const uint32_t ori_0_0_0 = 0x60000000;
3838 static const uint32_t srdi_0_0_2 = 0x7800f082;
3839 static const uint32_t std_0_1 = 0xf8010000;
3840 static const uint32_t std_0_12 = 0xf80c0000;
3841 static const uint32_t std_2_1 = 0xf8410000;
3842 static const uint32_t std_11_1 = 0xf9610000;
3843 static const uint32_t stfd_0_1 = 0xd8010000;
3844 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
3845 static const uint32_t sub_11_11_12 = 0x7d6c5850;
3846 static const uint32_t sub_12_12_11 = 0x7d8b6050;
3847 static const uint32_t xor_2_12_12 = 0x7d826278;
3848 static const uint32_t xor_11_12_12 = 0x7d8b6278;
3850 // Write out the PLT.
3852 template<int size, bool big_endian>
3854 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
3856 if (size == 32 && this->name_[3] != 'I')
3858 const section_size_type offset = this->offset();
3859 const section_size_type oview_size
3860 = convert_to_section_size_type(this->data_size());
3861 unsigned char* const oview = of->get_output_view(offset, oview_size);
3862 unsigned char* pov = oview;
3863 unsigned char* endpov = oview + oview_size;
3865 // The address of the .glink branch table
3866 const Output_data_glink<size, big_endian>* glink
3867 = this->targ_->glink_section();
3868 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
3870 while (pov < endpov)
3872 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
3877 of->write_output_view(offset, oview_size, oview);
3881 // Create the PLT section.
3883 template<int size, bool big_endian>
3885 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
3888 if (this->plt_ == NULL)
3890 if (this->got_ == NULL)
3891 this->got_section(symtab, layout);
3893 if (this->glink_ == NULL)
3894 make_glink_section(layout);
3896 // Ensure that .rela.dyn always appears before .rela.plt This is
3897 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3898 // needs to include .rela.plt in its range.
3899 this->rela_dyn_section(layout);
3901 Reloc_section* plt_rel = new Reloc_section(false);
3902 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3903 elfcpp::SHF_ALLOC, plt_rel,
3904 ORDER_DYNAMIC_PLT_RELOCS, false);
3906 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
3908 layout->add_output_section_data(".plt",
3910 ? elfcpp::SHT_PROGBITS
3911 : elfcpp::SHT_NOBITS),
3912 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3919 Output_section* rela_plt_os = plt_rel->output_section();
3920 rela_plt_os->set_info_section(this->plt_->output_section());
3924 // Create the IPLT section.
3926 template<int size, bool big_endian>
3928 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
3931 if (this->iplt_ == NULL)
3933 this->make_plt_section(symtab, layout);
3935 Reloc_section* iplt_rel = new Reloc_section(false);
3936 if (this->rela_dyn_->output_section())
3937 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
3939 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
3941 if (this->plt_->output_section())
3942 this->plt_->output_section()->add_output_section_data(this->iplt_);
3946 // A section for huge long branch addresses, similar to plt section.
3948 template<int size, bool big_endian>
3949 class Output_data_brlt_powerpc : public Output_section_data_build
3952 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3953 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3954 size, big_endian> Reloc_section;
3956 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
3957 Reloc_section* brlt_rel)
3958 : Output_section_data_build(size == 32 ? 4 : 8),
3966 this->reset_data_size();
3967 this->rel_->reset_data_size();
3971 finalize_brlt_sizes()
3973 this->finalize_data_size();
3974 this->rel_->finalize_data_size();
3977 // Add a reloc for an entry in the BRLT.
3979 add_reloc(Address to, unsigned int off)
3980 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
3982 // Update section and reloc section size.
3984 set_current_size(unsigned int num_branches)
3986 this->reset_address_and_file_offset();
3987 this->set_current_data_size(num_branches * 16);
3988 this->finalize_data_size();
3989 Output_section* os = this->output_section();
3990 os->set_section_offsets_need_adjustment();
3991 if (this->rel_ != NULL)
3993 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
3994 this->rel_->reset_address_and_file_offset();
3995 this->rel_->set_current_data_size(num_branches * reloc_size);
3996 this->rel_->finalize_data_size();
3997 Output_section* os = this->rel_->output_section();
3998 os->set_section_offsets_need_adjustment();
4004 do_adjust_output_section(Output_section* os)
4009 // Write to a map file.
4011 do_print_to_mapfile(Mapfile* mapfile) const
4012 { mapfile->print_output_data(this, "** BRLT"); }
4015 // Write out the BRLT data.
4017 do_write(Output_file*);
4019 // The reloc section.
4020 Reloc_section* rel_;
4021 Target_powerpc<size, big_endian>* targ_;
4024 // Make the branch lookup table section.
4026 template<int size, bool big_endian>
4028 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4030 if (size == 64 && this->brlt_section_ == NULL)
4032 Reloc_section* brlt_rel = NULL;
4033 bool is_pic = parameters->options().output_is_position_independent();
4036 // When PIC we can't fill in .branch_lt (like .plt it can be
4037 // a bss style section) but must initialise at runtime via
4038 // dynamic relocations.
4039 this->rela_dyn_section(layout);
4040 brlt_rel = new Reloc_section(false);
4041 if (this->rela_dyn_->output_section())
4042 this->rela_dyn_->output_section()
4043 ->add_output_section_data(brlt_rel);
4046 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
4047 if (this->plt_ && is_pic && this->plt_->output_section())
4048 this->plt_->output_section()
4049 ->add_output_section_data(this->brlt_section_);
4051 layout->add_output_section_data(".branch_lt",
4052 (is_pic ? elfcpp::SHT_NOBITS
4053 : elfcpp::SHT_PROGBITS),
4054 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4055 this->brlt_section_,
4056 (is_pic ? ORDER_SMALL_BSS
4057 : ORDER_SMALL_DATA),
4062 // Write out .branch_lt when non-PIC.
4064 template<int size, bool big_endian>
4066 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4068 if (size == 64 && !parameters->options().output_is_position_independent())
4070 const section_size_type offset = this->offset();
4071 const section_size_type oview_size
4072 = convert_to_section_size_type(this->data_size());
4073 unsigned char* const oview = of->get_output_view(offset, oview_size);
4075 this->targ_->write_branch_lookup_table(oview);
4076 of->write_output_view(offset, oview_size, oview);
4080 static inline uint32_t
4086 static inline uint32_t
4092 static inline uint32_t
4095 return hi(a + 0x8000);
4101 static const unsigned char eh_frame_cie[12];
4105 const unsigned char Eh_cie<size>::eh_frame_cie[] =
4108 'z', 'R', 0, // Augmentation string.
4109 4, // Code alignment.
4110 0x80 - size / 8 , // Data alignment.
4112 1, // Augmentation size.
4113 (elfcpp::DW_EH_PE_pcrel
4114 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4115 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4118 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4119 static const unsigned char glink_eh_frame_fde_64v1[] =
4121 0, 0, 0, 0, // Replaced with offset to .glink.
4122 0, 0, 0, 0, // Replaced with size of .glink.
4123 0, // Augmentation size.
4124 elfcpp::DW_CFA_advance_loc + 1,
4125 elfcpp::DW_CFA_register, 65, 12,
4126 elfcpp::DW_CFA_advance_loc + 5,
4127 elfcpp::DW_CFA_restore_extended, 65
4130 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4131 static const unsigned char glink_eh_frame_fde_64v2[] =
4133 0, 0, 0, 0, // Replaced with offset to .glink.
4134 0, 0, 0, 0, // Replaced with size of .glink.
4135 0, // Augmentation size.
4136 elfcpp::DW_CFA_advance_loc + 1,
4137 elfcpp::DW_CFA_register, 65, 0,
4138 elfcpp::DW_CFA_advance_loc + 7,
4139 elfcpp::DW_CFA_restore_extended, 65
4142 // Describe __glink_PLTresolve use of LR, 32-bit version.
4143 static const unsigned char glink_eh_frame_fde_32[] =
4145 0, 0, 0, 0, // Replaced with offset to .glink.
4146 0, 0, 0, 0, // Replaced with size of .glink.
4147 0, // Augmentation size.
4148 elfcpp::DW_CFA_advance_loc + 2,
4149 elfcpp::DW_CFA_register, 65, 0,
4150 elfcpp::DW_CFA_advance_loc + 4,
4151 elfcpp::DW_CFA_restore_extended, 65
4154 static const unsigned char default_fde[] =
4156 0, 0, 0, 0, // Replaced with offset to stubs.
4157 0, 0, 0, 0, // Replaced with size of stubs.
4158 0, // Augmentation size.
4159 elfcpp::DW_CFA_nop, // Pad.
4164 template<bool big_endian>
4166 write_insn(unsigned char* p, uint32_t v)
4168 elfcpp::Swap<32, big_endian>::writeval(p, v);
4171 template<bool big_endian>
4172 static unsigned char*
4173 output_bctr(unsigned char* p)
4175 if (!parameters->options().speculate_indirect_jumps())
4177 write_insn<big_endian>(p, crseteq);
4179 write_insn<big_endian>(p, beqctrm);
4181 write_insn<big_endian>(p, b);
4184 write_insn<big_endian>(p, bctr);
4190 static inline unsigned int
4193 if (!parameters->options().user_set_plt_align())
4194 return size == 64 ? 32 : 8;
4195 return 1 << parameters->options().plt_align();
4198 // Stub_table holds information about plt and long branch stubs.
4199 // Stubs are built in an area following some input section determined
4200 // by group_sections(). This input section is converted to a relaxed
4201 // input section allowing it to be resized to accommodate the stubs
4203 template<int size, bool big_endian>
4204 class Stub_table : public Output_relaxed_input_section
4209 Plt_stub_ent(unsigned int off, unsigned int indx)
4210 : off_(off), indx_(indx), r2save_(0), localentry0_(0)
4214 unsigned int indx_ : 30;
4215 unsigned int r2save_ : 1;
4216 unsigned int localentry0_ : 1;
4218 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4219 static const Address invalid_address = static_cast<Address>(0) - 1;
4221 Stub_table(Target_powerpc<size, big_endian>* targ,
4222 Output_section* output_section,
4223 const Output_section::Input_section* owner,
4225 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4227 ->section_addralign(owner->shndx())),
4228 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4229 orig_data_size_(owner->current_data_size()),
4230 plt_size_(0), last_plt_size_(0),
4231 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4232 need_save_res_(false), uniq_(id), tls_get_addr_opt_bctrl_(-1u),
4235 this->set_output_section(output_section);
4237 std::vector<Output_relaxed_input_section*> new_relaxed;
4238 new_relaxed.push_back(this);
4239 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4242 // Add a plt call stub.
4244 add_plt_call_entry(Address,
4245 const Sized_relobj_file<size, big_endian>*,
4252 add_plt_call_entry(Address,
4253 const Sized_relobj_file<size, big_endian>*,
4259 // Find a given plt call stub.
4261 find_plt_call_entry(const Symbol*) const;
4264 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4265 unsigned int) const;
4268 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4274 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4279 // Add a long branch stub.
4281 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4282 unsigned int, Address, Address, bool);
4285 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4289 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4291 Address max_branch_offset = max_branch_delta(r_type);
4292 if (max_branch_offset == 0)
4294 gold_assert(from != invalid_address);
4295 Address loc = off + this->stub_address();
4296 return loc - from + max_branch_offset < 2 * max_branch_offset;
4300 clear_stubs(bool all)
4302 this->plt_call_stubs_.clear();
4303 this->plt_size_ = 0;
4304 this->long_branch_stubs_.clear();
4305 this->branch_size_ = 0;
4306 this->need_save_res_ = false;
4309 this->last_plt_size_ = 0;
4310 this->last_branch_size_ = 0;
4315 set_address_and_size(const Output_section* os, Address off)
4317 Address start_off = off;
4318 off += this->orig_data_size_;
4319 Address my_size = this->plt_size_ + this->branch_size_;
4320 if (this->need_save_res_)
4321 my_size += this->targ_->savres_section()->data_size();
4323 off = align_address(off, this->stub_align());
4324 // Include original section size and alignment padding in size
4325 my_size += off - start_off;
4326 // Ensure new size is always larger than min size
4327 // threshold. Alignment requirement is included in "my_size", so
4328 // increase "my_size" does not invalidate alignment.
4329 if (my_size < this->min_size_threshold_)
4330 my_size = this->min_size_threshold_;
4331 this->reset_address_and_file_offset();
4332 this->set_current_data_size(my_size);
4333 this->set_address_and_file_offset(os->address() + start_off,
4334 os->offset() + start_off);
4339 stub_address() const
4341 return align_address(this->address() + this->orig_data_size_,
4342 this->stub_align());
4348 return align_address(this->offset() + this->orig_data_size_,
4349 this->stub_align());
4354 { return this->plt_size_; }
4357 set_min_size_threshold(Address min_size)
4358 { this->min_size_threshold_ = min_size; }
4361 define_stub_syms(Symbol_table*);
4366 Output_section* os = this->output_section();
4367 if (os->addralign() < this->stub_align())
4369 os->set_addralign(this->stub_align());
4370 // FIXME: get rid of the insane checkpointing.
4371 // We can't increase alignment of the input section to which
4372 // stubs are attached; The input section may be .init which
4373 // is pasted together with other .init sections to form a
4374 // function. Aligning might insert zero padding resulting in
4375 // sigill. However we do need to increase alignment of the
4376 // output section so that the align_address() on offset in
4377 // set_address_and_size() adds the same padding as the
4378 // align_address() on address in stub_address().
4379 // What's more, we need this alignment for the layout done in
4380 // relaxation_loop_body() so that the output section starts at
4381 // a suitably aligned address.
4382 os->checkpoint_set_addralign(this->stub_align());
4384 if (this->last_plt_size_ != this->plt_size_
4385 || this->last_branch_size_ != this->branch_size_)
4387 this->last_plt_size_ = this->plt_size_;
4388 this->last_branch_size_ = this->branch_size_;
4394 // Generate a suitable FDE to describe code in this stub group.
4398 // Add .eh_frame info for this stub section.
4400 add_eh_frame(Layout* layout);
4402 // Remove .eh_frame info for this stub section.
4404 remove_eh_frame(Layout* layout);
4406 Target_powerpc<size, big_endian>*
4412 class Plt_stub_key_hash;
4413 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4414 Plt_stub_key_hash> Plt_stub_entries;
4415 class Branch_stub_ent;
4416 class Branch_stub_ent_hash;
4417 typedef Unordered_map<Branch_stub_ent, unsigned int,
4418 Branch_stub_ent_hash> Branch_stub_entries;
4420 // Alignment of stub section.
4424 unsigned int min_align = size == 64 ? 32 : 16;
4425 unsigned int user_align = 1 << parameters->options().plt_align();
4426 return std::max(user_align, min_align);
4429 // Return the plt offset for the given call stub.
4431 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
4433 const Symbol* gsym = p->first.sym_;
4436 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
4437 && gsym->can_use_relative_reloc(false));
4438 return gsym->plt_offset();
4443 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4444 unsigned int local_sym_index = p->first.locsym_;
4445 return relobj->local_plt_offset(local_sym_index);
4449 // Size of a given plt call stub.
4451 plt_call_size(typename Plt_stub_entries::const_iterator p) const
4455 const Symbol* gsym = p->first.sym_;
4457 + (!parameters->options().speculate_indirect_jumps() ? 2 * 4 : 0)
4458 + (this->targ_->is_tls_get_addr_opt(gsym) ? 8 * 4 : 0));
4462 Address plt_addr = this->plt_off(p, &is_iplt);
4464 plt_addr += this->targ_->iplt_section()->address();
4466 plt_addr += this->targ_->plt_section()->address();
4467 Address got_addr = this->targ_->got_section()->output_section()->address();
4468 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4469 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
4470 got_addr += ppcobj->toc_base_offset();
4471 Address off = plt_addr - got_addr;
4472 unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
4473 if (!parameters->options().speculate_indirect_jumps())
4475 const Symbol* gsym = p->first.sym_;
4476 if (this->targ_->is_tls_get_addr_opt(gsym))
4478 if (this->targ_->abiversion() < 2)
4480 bool static_chain = parameters->options().plt_static_chain();
4481 bool thread_safe = this->targ_->plt_thread_safe();
4485 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
4491 plt_call_align(unsigned int bytes) const
4493 unsigned int align = param_plt_align<size>();
4494 return (bytes + align - 1) & -align;
4497 // Return long branch stub size.
4499 branch_stub_size(typename Branch_stub_entries::const_iterator p)
4501 Address loc = this->stub_address() + this->last_plt_size_ + p->second;
4502 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
4504 unsigned int bytes = 16;
4505 if (!parameters->options().speculate_indirect_jumps())
4507 if (size == 32 && parameters->options().output_is_position_independent())
4514 do_write(Output_file*);
4516 // Plt call stub keys.
4520 Plt_stub_key(const Symbol* sym)
4521 : sym_(sym), object_(0), addend_(0), locsym_(0)
4524 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4525 unsigned int locsym_index)
4526 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4529 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4531 unsigned int r_type,
4533 : sym_(sym), object_(0), addend_(0), locsym_(0)
4536 this->addend_ = addend;
4537 else if (parameters->options().output_is_position_independent()
4538 && r_type == elfcpp::R_PPC_PLTREL24)
4540 this->addend_ = addend;
4541 if (this->addend_ >= 32768)
4542 this->object_ = object;
4546 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4547 unsigned int locsym_index,
4548 unsigned int r_type,
4550 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4553 this->addend_ = addend;
4554 else if (parameters->options().output_is_position_independent()
4555 && r_type == elfcpp::R_PPC_PLTREL24)
4556 this->addend_ = addend;
4559 bool operator==(const Plt_stub_key& that) const
4561 return (this->sym_ == that.sym_
4562 && this->object_ == that.object_
4563 && this->addend_ == that.addend_
4564 && this->locsym_ == that.locsym_);
4568 const Sized_relobj_file<size, big_endian>* object_;
4569 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4570 unsigned int locsym_;
4573 class Plt_stub_key_hash
4576 size_t operator()(const Plt_stub_key& ent) const
4578 return (reinterpret_cast<uintptr_t>(ent.sym_)
4579 ^ reinterpret_cast<uintptr_t>(ent.object_)
4585 // Long branch stub keys.
4586 class Branch_stub_ent
4589 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj,
4590 Address to, bool save_res)
4591 : dest_(to), toc_base_off_(0), save_res_(save_res)
4594 toc_base_off_ = obj->toc_base_offset();
4597 bool operator==(const Branch_stub_ent& that) const
4599 return (this->dest_ == that.dest_
4601 || this->toc_base_off_ == that.toc_base_off_));
4605 unsigned int toc_base_off_;
4609 class Branch_stub_ent_hash
4612 size_t operator()(const Branch_stub_ent& ent) const
4613 { return ent.dest_ ^ ent.toc_base_off_; }
4616 // In a sane world this would be a global.
4617 Target_powerpc<size, big_endian>* targ_;
4618 // Map sym/object/addend to stub offset.
4619 Plt_stub_entries plt_call_stubs_;
4620 // Map destination address to stub offset.
4621 Branch_stub_entries long_branch_stubs_;
4622 // size of input section
4623 section_size_type orig_data_size_;
4625 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
4626 // Some rare cases cause (PR/20529) fluctuation in stub table
4627 // size, which leads to an endless relax loop. This is to be fixed
4628 // by, after the first few iterations, allowing only increase of
4629 // stub table size. This variable sets the minimal possible size of
4630 // a stub table, it is zero for the first few iterations, then
4631 // increases monotonically.
4632 Address min_size_threshold_;
4633 // Set if this stub group needs a copy of out-of-line register
4634 // save/restore functions.
4635 bool need_save_res_;
4636 // Per stub table unique identifier.
4638 // The bctrl in the __tls_get_addr_opt stub, if present.
4639 unsigned int tls_get_addr_opt_bctrl_;
4640 // FDE unwind info for this stub group.
4641 unsigned int plt_fde_len_;
4642 unsigned char plt_fde_[20];
4645 // Add a plt call stub, if we do not already have one for this
4646 // sym/object/addend combo.
4648 template<int size, bool big_endian>
4650 Stub_table<size, big_endian>::add_plt_call_entry(
4652 const Sized_relobj_file<size, big_endian>* object,
4654 unsigned int r_type,
4658 Plt_stub_key key(object, gsym, r_type, addend);
4659 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
4660 std::pair<typename Plt_stub_entries::iterator, bool> p
4661 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
4664 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
4666 && this->targ_->is_elfv2_localentry0(gsym))
4668 p.first->second.localentry0_ = 1;
4669 this->targ_->set_has_localentry0();
4671 if (this->targ_->is_tls_get_addr_opt(gsym))
4673 this->targ_->set_has_tls_get_addr_opt();
4674 this->tls_get_addr_opt_bctrl_ = this->plt_size_ - 5 * 4;
4676 this->plt_size_ = this->plt_call_align(this->plt_size_);
4680 && !p.first->second.localentry0_)
4681 p.first->second.r2save_ = 1;
4682 return this->can_reach_stub(from, ent.off_, r_type);
4685 template<int size, bool big_endian>
4687 Stub_table<size, big_endian>::add_plt_call_entry(
4689 const Sized_relobj_file<size, big_endian>* object,
4690 unsigned int locsym_index,
4691 unsigned int r_type,
4695 Plt_stub_key key(object, locsym_index, r_type, addend);
4696 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
4697 std::pair<typename Plt_stub_entries::iterator, bool> p
4698 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
4701 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
4702 this->plt_size_ = this->plt_call_align(this->plt_size_);
4704 && this->targ_->is_elfv2_localentry0(object, locsym_index))
4706 p.first->second.localentry0_ = 1;
4707 this->targ_->set_has_localentry0();
4712 && !p.first->second.localentry0_)
4713 p.first->second.r2save_ = 1;
4714 return this->can_reach_stub(from, ent.off_, r_type);
4717 // Find a plt call stub.
4719 template<int size, bool big_endian>
4720 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4721 Stub_table<size, big_endian>::find_plt_call_entry(
4722 const Sized_relobj_file<size, big_endian>* object,
4724 unsigned int r_type,
4725 Address addend) const
4727 Plt_stub_key key(object, gsym, r_type, addend);
4728 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4729 if (p == this->plt_call_stubs_.end())
4734 template<int size, bool big_endian>
4735 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4736 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
4738 Plt_stub_key key(gsym);
4739 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4740 if (p == this->plt_call_stubs_.end())
4745 template<int size, bool big_endian>
4746 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4747 Stub_table<size, big_endian>::find_plt_call_entry(
4748 const Sized_relobj_file<size, big_endian>* object,
4749 unsigned int locsym_index,
4750 unsigned int r_type,
4751 Address addend) const
4753 Plt_stub_key key(object, locsym_index, r_type, addend);
4754 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4755 if (p == this->plt_call_stubs_.end())
4760 template<int size, bool big_endian>
4761 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4762 Stub_table<size, big_endian>::find_plt_call_entry(
4763 const Sized_relobj_file<size, big_endian>* object,
4764 unsigned int locsym_index) const
4766 Plt_stub_key key(object, locsym_index);
4767 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4768 if (p == this->plt_call_stubs_.end())
4773 // Add a long branch stub if we don't already have one to given
4776 template<int size, bool big_endian>
4778 Stub_table<size, big_endian>::add_long_branch_entry(
4779 const Powerpc_relobj<size, big_endian>* object,
4780 unsigned int r_type,
4785 Branch_stub_ent ent(object, to, save_res);
4786 Address off = this->branch_size_;
4787 std::pair<typename Branch_stub_entries::iterator, bool> p
4788 = this->long_branch_stubs_.insert(std::make_pair(ent, off));
4792 this->need_save_res_ = true;
4795 unsigned int stub_size = this->branch_stub_size(p.first);
4796 this->branch_size_ = off + stub_size;
4797 if (size == 64 && stub_size != 4)
4798 this->targ_->add_branch_lookup_table(to);
4801 return this->can_reach_stub(from, off, r_type);
4804 // Find long branch stub offset.
4806 template<int size, bool big_endian>
4807 typename Stub_table<size, big_endian>::Address
4808 Stub_table<size, big_endian>::find_long_branch_entry(
4809 const Powerpc_relobj<size, big_endian>* object,
4812 Branch_stub_ent ent(object, to, false);
4813 typename Branch_stub_entries::const_iterator p
4814 = this->long_branch_stubs_.find(ent);
4815 if (p == this->long_branch_stubs_.end())
4816 return invalid_address;
4817 if (p->first.save_res_)
4818 return to - this->targ_->savres_section()->address() + this->branch_size_;
4822 // Generate a suitable FDE to describe code in this stub group.
4823 // The __tls_get_addr_opt call stub needs to describe where it saves
4824 // LR, to support exceptions that might be thrown from __tls_get_addr.
4826 template<int size, bool big_endian>
4828 Stub_table<size, big_endian>::init_plt_fde()
4830 unsigned char* p = this->plt_fde_;
4831 // offset pcrel sdata4, size udata4, and augmentation size byte.
4834 if (this->tls_get_addr_opt_bctrl_ != -1u)
4836 unsigned int to_bctrl = this->tls_get_addr_opt_bctrl_ / 4;
4838 *p++ = elfcpp::DW_CFA_advance_loc + to_bctrl;
4839 else if (to_bctrl < 256)
4841 *p++ = elfcpp::DW_CFA_advance_loc1;
4844 else if (to_bctrl < 65536)
4846 *p++ = elfcpp::DW_CFA_advance_loc2;
4847 elfcpp::Swap<16, big_endian>::writeval(p, to_bctrl);
4852 *p++ = elfcpp::DW_CFA_advance_loc4;
4853 elfcpp::Swap<32, big_endian>::writeval(p, to_bctrl);
4856 *p++ = elfcpp::DW_CFA_offset_extended_sf;
4858 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
4859 *p++ = elfcpp::DW_CFA_advance_loc + 4;
4860 *p++ = elfcpp::DW_CFA_restore_extended;
4863 this->plt_fde_len_ = p - this->plt_fde_;
4866 // Add .eh_frame info for this stub section. Unlike other linker
4867 // generated .eh_frame this is added late in the link, because we
4868 // only want the .eh_frame info if this particular stub section is
4871 template<int size, bool big_endian>
4873 Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
4875 if (!parameters->options().ld_generated_unwind_info())
4878 // Since we add stub .eh_frame info late, it must be placed
4879 // after all other linker generated .eh_frame info so that
4880 // merge mapping need not be updated for input sections.
4881 // There is no provision to use a different CIE to that used
4883 if (!this->targ_->has_glink())
4886 if (this->plt_size_ + this->branch_size_ + this->need_save_res_ == 0)
4889 this->init_plt_fde();
4890 layout->add_eh_frame_for_plt(this,
4891 Eh_cie<size>::eh_frame_cie,
4892 sizeof (Eh_cie<size>::eh_frame_cie),
4893 this->plt_fde_, this->plt_fde_len_);
4896 template<int size, bool big_endian>
4898 Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
4900 if (this->plt_fde_len_ != 0)
4902 layout->remove_eh_frame_for_plt(this,
4903 Eh_cie<size>::eh_frame_cie,
4904 sizeof (Eh_cie<size>::eh_frame_cie),
4905 this->plt_fde_, this->plt_fde_len_);
4906 this->plt_fde_len_ = 0;
4910 // A class to handle .glink.
4912 template<int size, bool big_endian>
4913 class Output_data_glink : public Output_section_data
4916 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4917 static const Address invalid_address = static_cast<Address>(0) - 1;
4919 Output_data_glink(Target_powerpc<size, big_endian>* targ)
4920 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
4921 end_branch_table_(), ge_size_(0)
4925 add_eh_frame(Layout* layout);
4928 add_global_entry(const Symbol*);
4931 find_global_entry(const Symbol*) const;
4934 global_entry_align(unsigned int off) const
4936 unsigned int align = param_plt_align<size>();
4937 return (off + align - 1) & -align;
4941 global_entry_off() const
4943 return this->global_entry_align(this->end_branch_table_);
4947 global_entry_address() const
4949 gold_assert(this->is_data_size_valid());
4950 return this->address() + this->global_entry_off();
4954 pltresolve_size() const
4958 + (this->targ_->abiversion() < 2 ? 11 * 4 : 14 * 4)
4959 + (!parameters->options().speculate_indirect_jumps() ? 2 * 4 : 0));
4964 // Write to a map file.
4966 do_print_to_mapfile(Mapfile* mapfile) const
4967 { mapfile->print_output_data(this, _("** glink")); }
4971 set_final_data_size();
4975 do_write(Output_file*);
4977 // Allows access to .got and .plt for do_write.
4978 Target_powerpc<size, big_endian>* targ_;
4980 // Map sym to stub offset.
4981 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
4982 Global_entry_stub_entries global_entry_stubs_;
4984 unsigned int end_branch_table_, ge_size_;
4987 template<int size, bool big_endian>
4989 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
4991 if (!parameters->options().ld_generated_unwind_info())
4996 if (this->targ_->abiversion() < 2)
4997 layout->add_eh_frame_for_plt(this,
4998 Eh_cie<64>::eh_frame_cie,
4999 sizeof (Eh_cie<64>::eh_frame_cie),
5000 glink_eh_frame_fde_64v1,
5001 sizeof (glink_eh_frame_fde_64v1));
5003 layout->add_eh_frame_for_plt(this,
5004 Eh_cie<64>::eh_frame_cie,
5005 sizeof (Eh_cie<64>::eh_frame_cie),
5006 glink_eh_frame_fde_64v2,
5007 sizeof (glink_eh_frame_fde_64v2));
5011 // 32-bit .glink can use the default since the CIE return
5012 // address reg, LR, is valid.
5013 layout->add_eh_frame_for_plt(this,
5014 Eh_cie<32>::eh_frame_cie,
5015 sizeof (Eh_cie<32>::eh_frame_cie),
5017 sizeof (default_fde));
5018 // Except where LR is used in a PIC __glink_PLTresolve.
5019 if (parameters->options().output_is_position_independent())
5020 layout->add_eh_frame_for_plt(this,
5021 Eh_cie<32>::eh_frame_cie,
5022 sizeof (Eh_cie<32>::eh_frame_cie),
5023 glink_eh_frame_fde_32,
5024 sizeof (glink_eh_frame_fde_32));
5028 template<int size, bool big_endian>
5030 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5032 unsigned int off = this->global_entry_align(this->ge_size_);
5033 std::pair<typename Global_entry_stub_entries::iterator, bool> p
5034 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
5037 = off + 16 + (!parameters->options().speculate_indirect_jumps() ? 8 : 0);
5040 template<int size, bool big_endian>
5041 typename Output_data_glink<size, big_endian>::Address
5042 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5044 typename Global_entry_stub_entries::const_iterator p
5045 = this->global_entry_stubs_.find(gsym);
5046 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5049 template<int size, bool big_endian>
5051 Output_data_glink<size, big_endian>::set_final_data_size()
5053 unsigned int count = this->targ_->plt_entry_count();
5054 section_size_type total = 0;
5060 // space for branch table
5061 total += 4 * (count - 1);
5063 total += -total & 15;
5064 total += this->pltresolve_size();
5068 total += this->pltresolve_size();
5070 // space for branch table
5072 if (this->targ_->abiversion() < 2)
5076 total += 4 * (count - 0x8000);
5080 this->end_branch_table_ = total;
5081 total = this->global_entry_align(total);
5082 total += this->ge_size_;
5084 this->set_data_size(total);
5087 // Define symbols on stubs, identifying the stub.
5089 template<int size, bool big_endian>
5091 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5093 if (!this->plt_call_stubs_.empty())
5095 // The key for the plt call stub hash table includes addresses,
5096 // therefore traversal order depends on those addresses, which
5097 // can change between runs if gold is a PIE. Unfortunately the
5098 // output .symtab ordering depends on the order in which symbols
5099 // are added to the linker symtab. We want reproducible output
5100 // so must sort the call stub symbols.
5101 typedef typename Plt_stub_entries::const_iterator plt_iter;
5102 std::vector<plt_iter> sorted;
5103 sorted.resize(this->plt_call_stubs_.size());
5105 for (plt_iter cs = this->plt_call_stubs_.begin();
5106 cs != this->plt_call_stubs_.end();
5108 sorted[cs->second.indx_] = cs;
5110 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5112 plt_iter cs = sorted[i];
5115 if (cs->first.addend_ != 0)
5116 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
5119 if (cs->first.object_)
5121 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5122 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5123 sprintf(obj, "%x:", ppcobj->uniq());
5126 const char *symname;
5127 if (cs->first.sym_ == NULL)
5129 sprintf(localname, "%x", cs->first.locsym_);
5130 symname = localname;
5132 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5133 symname = this->targ_->tls_get_addr_opt()->name();
5135 symname = cs->first.sym_->name();
5136 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5137 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
5139 = this->stub_address() - this->address() + cs->second.off_;
5140 unsigned int stub_size = this->plt_call_align(this->plt_call_size(cs));
5141 this->targ_->define_local(symtab, name, this, value, stub_size);
5145 typedef typename Branch_stub_entries::const_iterator branch_iter;
5146 for (branch_iter bs = this->long_branch_stubs_.begin();
5147 bs != this->long_branch_stubs_.end();
5150 if (bs->first.save_res_)
5153 char* name = new char[8 + 13 + 16 + 1];
5154 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5155 static_cast<unsigned long long>(bs->first.dest_));
5156 Address value = (this->stub_address() - this->address()
5157 + this->plt_size_ + bs->second);
5158 unsigned int stub_size = this->branch_stub_size(bs);
5159 this->targ_->define_local(symtab, name, this, value, stub_size);
5163 // Write out plt and long branch stub code.
5165 template<int size, bool big_endian>
5167 Stub_table<size, big_endian>::do_write(Output_file* of)
5169 if (this->plt_call_stubs_.empty()
5170 && this->long_branch_stubs_.empty())
5173 const section_size_type start_off = this->offset();
5174 const section_size_type off = this->stub_offset();
5175 const section_size_type oview_size =
5176 convert_to_section_size_type(this->data_size() - (off - start_off));
5177 unsigned char* const oview = of->get_output_view(off, oview_size);
5182 const Output_data_got_powerpc<size, big_endian>* got
5183 = this->targ_->got_section();
5184 Address got_os_addr = got->output_section()->address();
5186 if (!this->plt_call_stubs_.empty())
5188 // The base address of the .plt section.
5189 Address plt_base = this->targ_->plt_section()->address();
5190 Address iplt_base = invalid_address;
5192 // Write out plt call stubs.
5193 typename Plt_stub_entries::const_iterator cs;
5194 for (cs = this->plt_call_stubs_.begin();
5195 cs != this->plt_call_stubs_.end();
5199 Address pltoff = this->plt_off(cs, &is_iplt);
5200 Address plt_addr = pltoff;
5203 if (iplt_base == invalid_address)
5204 iplt_base = this->targ_->iplt_section()->address();
5205 plt_addr += iplt_base;
5208 plt_addr += plt_base;
5209 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5210 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5211 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
5212 Address off = plt_addr - got_addr;
5214 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5215 gold_error(_("%s: linkage table error against `%s'"),
5216 cs->first.object_->name().c_str(),
5217 cs->first.sym_->demangled_name().c_str());
5219 bool plt_load_toc = this->targ_->abiversion() < 2;
5221 = plt_load_toc && parameters->options().plt_static_chain();
5223 = plt_load_toc && this->targ_->plt_thread_safe();
5224 bool use_fake_dep = false;
5225 Address cmp_branch_off = 0;
5227 && !parameters->options().speculate_indirect_jumps())
5228 use_fake_dep = true;
5229 else if (thread_safe)
5231 unsigned int pltindex
5232 = ((pltoff - this->targ_->first_plt_entry_offset())
5233 / this->targ_->plt_entry_size());
5235 = (this->targ_->glink_section()->pltresolve_size()
5237 if (pltindex > 32768)
5238 glinkoff += (pltindex - 32768) * 4;
5240 = this->targ_->glink_section()->address() + glinkoff;
5242 = (this->stub_address() + cs->second.off_ + 20
5243 + 4 * cs->second.r2save_
5244 + 4 * (ha(off) != 0)
5245 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
5246 + 4 * static_chain);
5247 cmp_branch_off = to - from;
5248 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
5251 p = oview + cs->second.off_;
5252 const Symbol* gsym = cs->first.sym_;
5253 if (this->targ_->is_tls_get_addr_opt(gsym))
5255 write_insn<big_endian>(p, ld_11_3 + 0);
5257 write_insn<big_endian>(p, ld_12_3 + 8);
5259 write_insn<big_endian>(p, mr_0_3);
5261 write_insn<big_endian>(p, cmpdi_11_0);
5263 write_insn<big_endian>(p, add_3_12_13);
5265 write_insn<big_endian>(p, beqlr);
5267 write_insn<big_endian>(p, mr_3_0);
5269 if (!cs->second.localentry0_)
5271 write_insn<big_endian>(p, mflr_11);
5273 write_insn<big_endian>(p, (std_11_1
5274 + this->targ_->stk_linker()));
5277 use_fake_dep |= thread_safe;
5281 if (cs->second.r2save_)
5283 write_insn<big_endian>(p,
5284 std_2_1 + this->targ_->stk_toc());
5289 write_insn<big_endian>(p, addis_11_2 + ha(off));
5291 write_insn<big_endian>(p, ld_12_11 + l(off));
5296 write_insn<big_endian>(p, addis_12_2 + ha(off));
5298 write_insn<big_endian>(p, ld_12_12 + l(off));
5302 && ha(off + 8 + 8 * static_chain) != ha(off))
5304 write_insn<big_endian>(p, addi_11_11 + l(off));
5308 write_insn<big_endian>(p, mtctr_12);
5314 write_insn<big_endian>(p, xor_2_12_12);
5316 write_insn<big_endian>(p, add_11_11_2);
5319 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
5323 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
5330 if (cs->second.r2save_)
5332 write_insn<big_endian>(p,
5333 std_2_1 + this->targ_->stk_toc());
5336 write_insn<big_endian>(p, ld_12_2 + l(off));
5339 && ha(off + 8 + 8 * static_chain) != ha(off))
5341 write_insn<big_endian>(p, addi_2_2 + l(off));
5345 write_insn<big_endian>(p, mtctr_12);
5351 write_insn<big_endian>(p, xor_11_12_12);
5353 write_insn<big_endian>(p, add_2_2_11);
5358 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
5361 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
5365 if (!cs->second.localentry0_
5366 && this->targ_->is_tls_get_addr_opt(gsym))
5368 if (!parameters->options().speculate_indirect_jumps())
5370 write_insn<big_endian>(p, crseteq);
5372 write_insn<big_endian>(p, beqctrlm);
5375 write_insn<big_endian>(p, bctrl);
5377 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5379 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5381 write_insn<big_endian>(p, mtlr_11);
5383 write_insn<big_endian>(p, blr);
5385 else if (thread_safe && !use_fake_dep)
5387 write_insn<big_endian>(p, cmpldi_2_0);
5389 write_insn<big_endian>(p, bnectr_p4);
5391 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
5394 output_bctr<big_endian>(p);
5398 // Write out long branch stubs.
5399 typename Branch_stub_entries::const_iterator bs;
5400 for (bs = this->long_branch_stubs_.begin();
5401 bs != this->long_branch_stubs_.end();
5404 if (bs->first.save_res_)
5406 p = oview + this->plt_size_ + bs->second;
5407 Address loc = this->stub_address() + this->plt_size_ + bs->second;
5408 Address delta = bs->first.dest_ - loc;
5409 if (delta + (1 << 25) < 2 << 25)
5410 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
5414 = this->targ_->find_branch_lookup_table(bs->first.dest_);
5415 gold_assert(brlt_addr != invalid_address);
5416 brlt_addr += this->targ_->brlt_section()->address();
5417 Address got_addr = got_os_addr + bs->first.toc_base_off_;
5418 Address brltoff = brlt_addr - got_addr;
5419 if (ha(brltoff) == 0)
5421 write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4;
5425 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
5426 write_insn<big_endian>(p, ld_12_12 + l(brltoff)), p += 4;
5428 write_insn<big_endian>(p, mtctr_12), p += 4;
5429 output_bctr<big_endian>(p);
5435 if (!this->plt_call_stubs_.empty())
5437 // The base address of the .plt section.
5438 Address plt_base = this->targ_->plt_section()->address();
5439 Address iplt_base = invalid_address;
5440 // The address of _GLOBAL_OFFSET_TABLE_.
5441 Address g_o_t = invalid_address;
5443 // Write out plt call stubs.
5444 typename Plt_stub_entries::const_iterator cs;
5445 for (cs = this->plt_call_stubs_.begin();
5446 cs != this->plt_call_stubs_.end();
5450 Address plt_addr = this->plt_off(cs, &is_iplt);
5453 if (iplt_base == invalid_address)
5454 iplt_base = this->targ_->iplt_section()->address();
5455 plt_addr += iplt_base;
5458 plt_addr += plt_base;
5460 p = oview + cs->second.off_;
5461 const Symbol* gsym = cs->first.sym_;
5462 if (this->targ_->is_tls_get_addr_opt(gsym))
5464 write_insn<big_endian>(p, lwz_11_3 + 0);
5466 write_insn<big_endian>(p, lwz_12_3 + 4);
5468 write_insn<big_endian>(p, mr_0_3);
5470 write_insn<big_endian>(p, cmpwi_11_0);
5472 write_insn<big_endian>(p, add_3_12_2);
5474 write_insn<big_endian>(p, beqlr);
5476 write_insn<big_endian>(p, mr_3_0);
5478 write_insn<big_endian>(p, nop);
5481 if (parameters->options().output_is_position_independent())
5484 const Powerpc_relobj<size, big_endian>* ppcobj
5485 = (static_cast<const Powerpc_relobj<size, big_endian>*>
5486 (cs->first.object_));
5487 if (ppcobj != NULL && cs->first.addend_ >= 32768)
5489 unsigned int got2 = ppcobj->got2_shndx();
5490 got_addr = ppcobj->get_output_section_offset(got2);
5491 gold_assert(got_addr != invalid_address);
5492 got_addr += (ppcobj->output_section(got2)->address()
5493 + cs->first.addend_);
5497 if (g_o_t == invalid_address)
5499 const Output_data_got_powerpc<size, big_endian>* got
5500 = this->targ_->got_section();
5501 g_o_t = got->address() + got->g_o_t();
5506 Address off = plt_addr - got_addr;
5508 write_insn<big_endian>(p, lwz_11_30 + l(off));
5511 write_insn<big_endian>(p, addis_11_30 + ha(off));
5513 write_insn<big_endian>(p, lwz_11_11 + l(off));
5518 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
5520 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
5523 write_insn<big_endian>(p, mtctr_11);
5525 output_bctr<big_endian>(p);
5529 // Write out long branch stubs.
5530 typename Branch_stub_entries::const_iterator bs;
5531 for (bs = this->long_branch_stubs_.begin();
5532 bs != this->long_branch_stubs_.end();
5535 if (bs->first.save_res_)
5537 p = oview + this->plt_size_ + bs->second;
5538 Address loc = this->stub_address() + this->plt_size_ + bs->second;
5539 Address delta = bs->first.dest_ - loc;
5540 if (delta + (1 << 25) < 2 << 25)
5541 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
5542 else if (!parameters->options().output_is_position_independent())
5544 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
5546 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
5551 write_insn<big_endian>(p, mflr_0);
5553 write_insn<big_endian>(p, bcl_20_31);
5555 write_insn<big_endian>(p, mflr_12);
5557 write_insn<big_endian>(p, addis_12_12 + ha(delta));
5559 write_insn<big_endian>(p, addi_12_12 + l(delta));
5561 write_insn<big_endian>(p, mtlr_0);
5564 write_insn<big_endian>(p, mtctr_12);
5566 output_bctr<big_endian>(p);
5569 if (this->need_save_res_)
5571 p = oview + this->plt_size_ + this->branch_size_;
5572 memcpy (p, this->targ_->savres_section()->contents(),
5573 this->targ_->savres_section()->data_size());
5577 // Write out .glink.
5579 template<int size, bool big_endian>
5581 Output_data_glink<size, big_endian>::do_write(Output_file* of)
5583 const section_size_type off = this->offset();
5584 const section_size_type oview_size =
5585 convert_to_section_size_type(this->data_size());
5586 unsigned char* const oview = of->get_output_view(off, oview_size);
5589 // The base address of the .plt section.
5590 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5591 Address plt_base = this->targ_->plt_section()->address();
5595 if (this->end_branch_table_ != 0)
5597 // Write pltresolve stub.
5599 Address after_bcl = this->address() + 16;
5600 Address pltoff = plt_base - after_bcl;
5602 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
5604 if (this->targ_->abiversion() < 2)
5606 write_insn<big_endian>(p, mflr_12), p += 4;
5607 write_insn<big_endian>(p, bcl_20_31), p += 4;
5608 write_insn<big_endian>(p, mflr_11), p += 4;
5609 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5610 write_insn<big_endian>(p, mtlr_12), p += 4;
5611 write_insn<big_endian>(p, add_11_2_11), p += 4;
5612 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5613 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
5614 write_insn<big_endian>(p, mtctr_12), p += 4;
5615 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
5619 write_insn<big_endian>(p, mflr_0), p += 4;
5620 write_insn<big_endian>(p, bcl_20_31), p += 4;
5621 write_insn<big_endian>(p, mflr_11), p += 4;
5622 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
5623 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5624 write_insn<big_endian>(p, mtlr_0), p += 4;
5625 write_insn<big_endian>(p, sub_12_12_11), p += 4;
5626 write_insn<big_endian>(p, add_11_2_11), p += 4;
5627 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
5628 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5629 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
5630 write_insn<big_endian>(p, mtctr_12), p += 4;
5631 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
5633 p = output_bctr<big_endian>(p);
5634 gold_assert(p == oview + this->pltresolve_size());
5636 // Write lazy link call stubs.
5638 while (p < oview + this->end_branch_table_)
5640 if (this->targ_->abiversion() < 2)
5644 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
5648 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
5649 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
5652 uint32_t branch_off = 8 - (p - oview);
5653 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
5658 Address plt_base = this->targ_->plt_section()->address();
5659 Address iplt_base = invalid_address;
5660 unsigned int global_entry_off = this->global_entry_off();
5661 Address global_entry_base = this->address() + global_entry_off;
5662 typename Global_entry_stub_entries::const_iterator ge;
5663 for (ge = this->global_entry_stubs_.begin();
5664 ge != this->global_entry_stubs_.end();
5667 p = oview + global_entry_off + ge->second;
5668 Address plt_addr = ge->first->plt_offset();
5669 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
5670 && ge->first->can_use_relative_reloc(false))
5672 if (iplt_base == invalid_address)
5673 iplt_base = this->targ_->iplt_section()->address();
5674 plt_addr += iplt_base;
5677 plt_addr += plt_base;
5678 Address my_addr = global_entry_base + ge->second;
5679 Address off = plt_addr - my_addr;
5681 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
5682 gold_error(_("%s: linkage table error against `%s'"),
5683 ge->first->object()->name().c_str(),
5684 ge->first->demangled_name().c_str());
5686 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
5687 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
5688 write_insn<big_endian>(p, mtctr_12), p += 4;
5689 output_bctr<big_endian>(p);
5694 const Output_data_got_powerpc<size, big_endian>* got
5695 = this->targ_->got_section();
5696 // The address of _GLOBAL_OFFSET_TABLE_.
5697 Address g_o_t = got->address() + got->g_o_t();
5699 // Write out pltresolve branch table.
5701 unsigned int the_end = oview_size - this->pltresolve_size();
5702 unsigned char* end_p = oview + the_end;
5703 while (p < end_p - 8 * 4)
5704 write_insn<big_endian>(p, b + end_p - p), p += 4;
5706 write_insn<big_endian>(p, nop), p += 4;
5708 // Write out pltresolve call stub.
5709 end_p = oview + oview_size;
5710 if (parameters->options().output_is_position_independent())
5712 Address res0_off = 0;
5713 Address after_bcl_off = the_end + 12;
5714 Address bcl_res0 = after_bcl_off - res0_off;
5716 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
5718 write_insn<big_endian>(p, mflr_0);
5720 write_insn<big_endian>(p, bcl_20_31);
5722 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
5724 write_insn<big_endian>(p, mflr_12);
5726 write_insn<big_endian>(p, mtlr_0);
5728 write_insn<big_endian>(p, sub_11_11_12);
5731 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
5733 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
5735 if (ha(got_bcl) == ha(got_bcl + 4))
5737 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
5739 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
5743 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
5745 write_insn<big_endian>(p, lwz_12_12 + 4);
5748 write_insn<big_endian>(p, mtctr_0);
5750 write_insn<big_endian>(p, add_0_11_11);
5752 write_insn<big_endian>(p, add_11_0_11);
5756 Address res0 = this->address();
5758 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
5760 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
5762 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5763 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
5765 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
5767 write_insn<big_endian>(p, addi_11_11 + l(-res0));
5769 write_insn<big_endian>(p, mtctr_0);
5771 write_insn<big_endian>(p, add_0_11_11);
5773 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5774 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
5776 write_insn<big_endian>(p, lwz_12_12 + 4);
5778 write_insn<big_endian>(p, add_11_0_11);
5781 p = output_bctr<big_endian>(p);
5784 write_insn<big_endian>(p, nop);
5789 of->write_output_view(off, oview_size, oview);
5793 // A class to handle linker generated save/restore functions.
5795 template<int size, bool big_endian>
5796 class Output_data_save_res : public Output_section_data_build
5799 Output_data_save_res(Symbol_table* symtab);
5801 const unsigned char*
5808 // Write to a map file.
5810 do_print_to_mapfile(Mapfile* mapfile) const
5811 { mapfile->print_output_data(this, _("** save/restore")); }
5814 do_write(Output_file*);
5817 // The maximum size of save/restore contents.
5818 static const unsigned int savres_max = 218*4;
5821 savres_define(Symbol_table* symtab,
5823 unsigned int lo, unsigned int hi,
5824 unsigned char* write_ent(unsigned char*, int),
5825 unsigned char* write_tail(unsigned char*, int));
5827 unsigned char *contents_;
5830 template<bool big_endian>
5831 static unsigned char*
5832 savegpr0(unsigned char* p, int r)
5834 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5835 write_insn<big_endian>(p, insn);
5839 template<bool big_endian>
5840 static unsigned char*
5841 savegpr0_tail(unsigned char* p, int r)
5843 p = savegpr0<big_endian>(p, r);
5844 uint32_t insn = std_0_1 + 16;
5845 write_insn<big_endian>(p, insn);
5847 write_insn<big_endian>(p, blr);
5851 template<bool big_endian>
5852 static unsigned char*
5853 restgpr0(unsigned char* p, int r)
5855 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5856 write_insn<big_endian>(p, insn);
5860 template<bool big_endian>
5861 static unsigned char*
5862 restgpr0_tail(unsigned char* p, int r)
5864 uint32_t insn = ld_0_1 + 16;
5865 write_insn<big_endian>(p, insn);
5867 p = restgpr0<big_endian>(p, r);
5868 write_insn<big_endian>(p, mtlr_0);
5872 p = restgpr0<big_endian>(p, 30);
5873 p = restgpr0<big_endian>(p, 31);
5875 write_insn<big_endian>(p, blr);
5879 template<bool big_endian>
5880 static unsigned char*
5881 savegpr1(unsigned char* p, int r)
5883 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5884 write_insn<big_endian>(p, insn);
5888 template<bool big_endian>
5889 static unsigned char*
5890 savegpr1_tail(unsigned char* p, int r)
5892 p = savegpr1<big_endian>(p, r);
5893 write_insn<big_endian>(p, blr);
5897 template<bool big_endian>
5898 static unsigned char*
5899 restgpr1(unsigned char* p, int r)
5901 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5902 write_insn<big_endian>(p, insn);
5906 template<bool big_endian>
5907 static unsigned char*
5908 restgpr1_tail(unsigned char* p, int r)
5910 p = restgpr1<big_endian>(p, r);
5911 write_insn<big_endian>(p, blr);
5915 template<bool big_endian>
5916 static unsigned char*
5917 savefpr(unsigned char* p, int r)
5919 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5920 write_insn<big_endian>(p, insn);
5924 template<bool big_endian>
5925 static unsigned char*
5926 savefpr0_tail(unsigned char* p, int r)
5928 p = savefpr<big_endian>(p, r);
5929 write_insn<big_endian>(p, std_0_1 + 16);
5931 write_insn<big_endian>(p, blr);
5935 template<bool big_endian>
5936 static unsigned char*
5937 restfpr(unsigned char* p, int r)
5939 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5940 write_insn<big_endian>(p, insn);
5944 template<bool big_endian>
5945 static unsigned char*
5946 restfpr0_tail(unsigned char* p, int r)
5948 write_insn<big_endian>(p, ld_0_1 + 16);
5950 p = restfpr<big_endian>(p, r);
5951 write_insn<big_endian>(p, mtlr_0);
5955 p = restfpr<big_endian>(p, 30);
5956 p = restfpr<big_endian>(p, 31);
5958 write_insn<big_endian>(p, blr);
5962 template<bool big_endian>
5963 static unsigned char*
5964 savefpr1_tail(unsigned char* p, int r)
5966 p = savefpr<big_endian>(p, r);
5967 write_insn<big_endian>(p, blr);
5971 template<bool big_endian>
5972 static unsigned char*
5973 restfpr1_tail(unsigned char* p, int r)
5975 p = restfpr<big_endian>(p, r);
5976 write_insn<big_endian>(p, blr);
5980 template<bool big_endian>
5981 static unsigned char*
5982 savevr(unsigned char* p, int r)
5984 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5985 write_insn<big_endian>(p, insn);
5987 insn = stvx_0_12_0 + (r << 21);
5988 write_insn<big_endian>(p, insn);
5992 template<bool big_endian>
5993 static unsigned char*
5994 savevr_tail(unsigned char* p, int r)
5996 p = savevr<big_endian>(p, r);
5997 write_insn<big_endian>(p, blr);
6001 template<bool big_endian>
6002 static unsigned char*
6003 restvr(unsigned char* p, int r)
6005 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
6006 write_insn<big_endian>(p, insn);
6008 insn = lvx_0_12_0 + (r << 21);
6009 write_insn<big_endian>(p, insn);
6013 template<bool big_endian>
6014 static unsigned char*
6015 restvr_tail(unsigned char* p, int r)
6017 p = restvr<big_endian>(p, r);
6018 write_insn<big_endian>(p, blr);
6023 template<int size, bool big_endian>
6024 Output_data_save_res<size, big_endian>::Output_data_save_res(
6025 Symbol_table* symtab)
6026 : Output_section_data_build(4),
6029 this->savres_define(symtab,
6030 "_savegpr0_", 14, 31,
6031 savegpr0<big_endian>, savegpr0_tail<big_endian>);
6032 this->savres_define(symtab,
6033 "_restgpr0_", 14, 29,
6034 restgpr0<big_endian>, restgpr0_tail<big_endian>);
6035 this->savres_define(symtab,
6036 "_restgpr0_", 30, 31,
6037 restgpr0<big_endian>, restgpr0_tail<big_endian>);
6038 this->savres_define(symtab,
6039 "_savegpr1_", 14, 31,
6040 savegpr1<big_endian>, savegpr1_tail<big_endian>);
6041 this->savres_define(symtab,
6042 "_restgpr1_", 14, 31,
6043 restgpr1<big_endian>, restgpr1_tail<big_endian>);
6044 this->savres_define(symtab,
6045 "_savefpr_", 14, 31,
6046 savefpr<big_endian>, savefpr0_tail<big_endian>);
6047 this->savres_define(symtab,
6048 "_restfpr_", 14, 29,
6049 restfpr<big_endian>, restfpr0_tail<big_endian>);
6050 this->savres_define(symtab,
6051 "_restfpr_", 30, 31,
6052 restfpr<big_endian>, restfpr0_tail<big_endian>);
6053 this->savres_define(symtab,
6055 savefpr<big_endian>, savefpr1_tail<big_endian>);
6056 this->savres_define(symtab,
6058 restfpr<big_endian>, restfpr1_tail<big_endian>);
6059 this->savres_define(symtab,
6061 savevr<big_endian>, savevr_tail<big_endian>);
6062 this->savres_define(symtab,
6064 restvr<big_endian>, restvr_tail<big_endian>);
6067 template<int size, bool big_endian>
6069 Output_data_save_res<size, big_endian>::savres_define(
6070 Symbol_table* symtab,
6072 unsigned int lo, unsigned int hi,
6073 unsigned char* write_ent(unsigned char*, int),
6074 unsigned char* write_tail(unsigned char*, int))
6076 size_t len = strlen(name);
6077 bool writing = false;
6080 memcpy(sym, name, len);
6083 for (unsigned int i = lo; i <= hi; i++)
6085 sym[len + 0] = i / 10 + '0';
6086 sym[len + 1] = i % 10 + '0';
6087 Symbol* gsym = symtab->lookup(sym);
6088 bool refd = gsym != NULL && gsym->is_undefined();
6089 writing = writing || refd;
6092 if (this->contents_ == NULL)
6093 this->contents_ = new unsigned char[this->savres_max];
6095 section_size_type value = this->current_data_size();
6096 unsigned char* p = this->contents_ + value;
6098 p = write_ent(p, i);
6100 p = write_tail(p, i);
6101 section_size_type cur_size = p - this->contents_;
6102 this->set_current_data_size(cur_size);
6104 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
6105 this, value, cur_size - value,
6106 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
6107 elfcpp::STV_HIDDEN, 0, false, false);
6112 // Write out save/restore.
6114 template<int size, bool big_endian>
6116 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
6118 const section_size_type off = this->offset();
6119 const section_size_type oview_size =
6120 convert_to_section_size_type(this->data_size());
6121 unsigned char* const oview = of->get_output_view(off, oview_size);
6122 memcpy(oview, this->contents_, oview_size);
6123 of->write_output_view(off, oview_size, oview);
6127 // Create the glink section.
6129 template<int size, bool big_endian>
6131 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
6133 if (this->glink_ == NULL)
6135 this->glink_ = new Output_data_glink<size, big_endian>(this);
6136 this->glink_->add_eh_frame(layout);
6137 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
6138 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
6139 this->glink_, ORDER_TEXT, false);
6143 // Create a PLT entry for a global symbol.
6145 template<int size, bool big_endian>
6147 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
6151 if (gsym->type() == elfcpp::STT_GNU_IFUNC
6152 && gsym->can_use_relative_reloc(false))
6154 if (this->iplt_ == NULL)
6155 this->make_iplt_section(symtab, layout);
6156 this->iplt_->add_ifunc_entry(gsym);
6160 if (this->plt_ == NULL)
6161 this->make_plt_section(symtab, layout);
6162 this->plt_->add_entry(gsym);
6166 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
6168 template<int size, bool big_endian>
6170 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
6171 Symbol_table* symtab,
6173 Sized_relobj_file<size, big_endian>* relobj,
6176 if (this->iplt_ == NULL)
6177 this->make_iplt_section(symtab, layout);
6178 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
6181 // Return the number of entries in the PLT.
6183 template<int size, bool big_endian>
6185 Target_powerpc<size, big_endian>::plt_entry_count() const
6187 if (this->plt_ == NULL)
6189 return this->plt_->entry_count();
6192 // Create a GOT entry for local dynamic __tls_get_addr calls.
6194 template<int size, bool big_endian>
6196 Target_powerpc<size, big_endian>::tlsld_got_offset(
6197 Symbol_table* symtab,
6199 Sized_relobj_file<size, big_endian>* object)
6201 if (this->tlsld_got_offset_ == -1U)
6203 gold_assert(symtab != NULL && layout != NULL && object != NULL);
6204 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
6205 Output_data_got_powerpc<size, big_endian>* got
6206 = this->got_section(symtab, layout);
6207 unsigned int got_offset = got->add_constant_pair(0, 0);
6208 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
6210 this->tlsld_got_offset_ = got_offset;
6212 return this->tlsld_got_offset_;
6215 // Get the Reference_flags for a particular relocation.
6217 template<int size, bool big_endian>
6219 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
6220 unsigned int r_type,
6221 const Target_powerpc* target)
6227 case elfcpp::R_POWERPC_NONE:
6228 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6229 case elfcpp::R_POWERPC_GNU_VTENTRY:
6230 case elfcpp::R_PPC64_TOC:
6231 // No symbol reference.
6234 case elfcpp::R_PPC64_ADDR64:
6235 case elfcpp::R_PPC64_UADDR64:
6236 case elfcpp::R_POWERPC_ADDR32:
6237 case elfcpp::R_POWERPC_UADDR32:
6238 case elfcpp::R_POWERPC_ADDR16:
6239 case elfcpp::R_POWERPC_UADDR16:
6240 case elfcpp::R_POWERPC_ADDR16_LO:
6241 case elfcpp::R_POWERPC_ADDR16_HI:
6242 case elfcpp::R_POWERPC_ADDR16_HA:
6243 ref = Symbol::ABSOLUTE_REF;
6246 case elfcpp::R_POWERPC_ADDR24:
6247 case elfcpp::R_POWERPC_ADDR14:
6248 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6249 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6250 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
6253 case elfcpp::R_PPC64_REL64:
6254 case elfcpp::R_POWERPC_REL32:
6255 case elfcpp::R_PPC_LOCAL24PC:
6256 case elfcpp::R_POWERPC_REL16:
6257 case elfcpp::R_POWERPC_REL16_LO:
6258 case elfcpp::R_POWERPC_REL16_HI:
6259 case elfcpp::R_POWERPC_REL16_HA:
6260 ref = Symbol::RELATIVE_REF;
6263 case elfcpp::R_POWERPC_REL24:
6264 case elfcpp::R_PPC_PLTREL24:
6265 case elfcpp::R_POWERPC_REL14:
6266 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6267 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6268 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
6271 case elfcpp::R_POWERPC_GOT16:
6272 case elfcpp::R_POWERPC_GOT16_LO:
6273 case elfcpp::R_POWERPC_GOT16_HI:
6274 case elfcpp::R_POWERPC_GOT16_HA:
6275 case elfcpp::R_PPC64_GOT16_DS:
6276 case elfcpp::R_PPC64_GOT16_LO_DS:
6277 case elfcpp::R_PPC64_TOC16:
6278 case elfcpp::R_PPC64_TOC16_LO:
6279 case elfcpp::R_PPC64_TOC16_HI:
6280 case elfcpp::R_PPC64_TOC16_HA:
6281 case elfcpp::R_PPC64_TOC16_DS:
6282 case elfcpp::R_PPC64_TOC16_LO_DS:
6283 ref = Symbol::RELATIVE_REF;
6286 case elfcpp::R_POWERPC_GOT_TPREL16:
6287 case elfcpp::R_POWERPC_TLS:
6288 ref = Symbol::TLS_REF;
6291 case elfcpp::R_POWERPC_COPY:
6292 case elfcpp::R_POWERPC_GLOB_DAT:
6293 case elfcpp::R_POWERPC_JMP_SLOT:
6294 case elfcpp::R_POWERPC_RELATIVE:
6295 case elfcpp::R_POWERPC_DTPMOD:
6297 // Not expected. We will give an error later.
6301 if (size == 64 && target->abiversion() < 2)
6302 ref |= Symbol::FUNC_DESC_ABI;
6306 // Report an unsupported relocation against a local symbol.
6308 template<int size, bool big_endian>
6310 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
6311 Sized_relobj_file<size, big_endian>* object,
6312 unsigned int r_type)
6314 gold_error(_("%s: unsupported reloc %u against local symbol"),
6315 object->name().c_str(), r_type);
6318 // We are about to emit a dynamic relocation of type R_TYPE. If the
6319 // dynamic linker does not support it, issue an error.
6321 template<int size, bool big_endian>
6323 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
6324 unsigned int r_type)
6326 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
6328 // These are the relocation types supported by glibc for both 32-bit
6329 // and 64-bit powerpc.
6332 case elfcpp::R_POWERPC_NONE:
6333 case elfcpp::R_POWERPC_RELATIVE:
6334 case elfcpp::R_POWERPC_GLOB_DAT:
6335 case elfcpp::R_POWERPC_DTPMOD:
6336 case elfcpp::R_POWERPC_DTPREL:
6337 case elfcpp::R_POWERPC_TPREL:
6338 case elfcpp::R_POWERPC_JMP_SLOT:
6339 case elfcpp::R_POWERPC_COPY:
6340 case elfcpp::R_POWERPC_IRELATIVE:
6341 case elfcpp::R_POWERPC_ADDR32:
6342 case elfcpp::R_POWERPC_UADDR32:
6343 case elfcpp::R_POWERPC_ADDR24:
6344 case elfcpp::R_POWERPC_ADDR16:
6345 case elfcpp::R_POWERPC_UADDR16:
6346 case elfcpp::R_POWERPC_ADDR16_LO:
6347 case elfcpp::R_POWERPC_ADDR16_HI:
6348 case elfcpp::R_POWERPC_ADDR16_HA:
6349 case elfcpp::R_POWERPC_ADDR14:
6350 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6351 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6352 case elfcpp::R_POWERPC_REL32:
6353 case elfcpp::R_POWERPC_REL24:
6354 case elfcpp::R_POWERPC_TPREL16:
6355 case elfcpp::R_POWERPC_TPREL16_LO:
6356 case elfcpp::R_POWERPC_TPREL16_HI:
6357 case elfcpp::R_POWERPC_TPREL16_HA:
6368 // These are the relocation types supported only on 64-bit.
6369 case elfcpp::R_PPC64_ADDR64:
6370 case elfcpp::R_PPC64_UADDR64:
6371 case elfcpp::R_PPC64_JMP_IREL:
6372 case elfcpp::R_PPC64_ADDR16_DS:
6373 case elfcpp::R_PPC64_ADDR16_LO_DS:
6374 case elfcpp::R_PPC64_ADDR16_HIGH:
6375 case elfcpp::R_PPC64_ADDR16_HIGHA:
6376 case elfcpp::R_PPC64_ADDR16_HIGHER:
6377 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6378 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6379 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6380 case elfcpp::R_PPC64_REL64:
6381 case elfcpp::R_POWERPC_ADDR30:
6382 case elfcpp::R_PPC64_TPREL16_DS:
6383 case elfcpp::R_PPC64_TPREL16_LO_DS:
6384 case elfcpp::R_PPC64_TPREL16_HIGH:
6385 case elfcpp::R_PPC64_TPREL16_HIGHA:
6386 case elfcpp::R_PPC64_TPREL16_HIGHER:
6387 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6388 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6389 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6400 // These are the relocation types supported only on 32-bit.
6401 // ??? glibc ld.so doesn't need to support these.
6402 case elfcpp::R_POWERPC_DTPREL16:
6403 case elfcpp::R_POWERPC_DTPREL16_LO:
6404 case elfcpp::R_POWERPC_DTPREL16_HI:
6405 case elfcpp::R_POWERPC_DTPREL16_HA:
6413 // This prevents us from issuing more than one error per reloc
6414 // section. But we can still wind up issuing more than one
6415 // error per object file.
6416 if (this->issued_non_pic_error_)
6418 gold_assert(parameters->options().output_is_position_independent());
6419 object->error(_("requires unsupported dynamic reloc; "
6420 "recompile with -fPIC"));
6421 this->issued_non_pic_error_ = true;
6425 // Return whether we need to make a PLT entry for a relocation of the
6426 // given type against a STT_GNU_IFUNC symbol.
6428 template<int size, bool big_endian>
6430 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
6431 Target_powerpc<size, big_endian>* target,
6432 Sized_relobj_file<size, big_endian>* object,
6433 unsigned int r_type,
6436 // In non-pic code any reference will resolve to the plt call stub
6437 // for the ifunc symbol.
6438 if ((size == 32 || target->abiversion() >= 2)
6439 && !parameters->options().output_is_position_independent())
6444 // Word size refs from data sections are OK, but don't need a PLT entry.
6445 case elfcpp::R_POWERPC_ADDR32:
6446 case elfcpp::R_POWERPC_UADDR32:
6451 case elfcpp::R_PPC64_ADDR64:
6452 case elfcpp::R_PPC64_UADDR64:
6457 // GOT refs are good, but also don't need a PLT entry.
6458 case elfcpp::R_POWERPC_GOT16:
6459 case elfcpp::R_POWERPC_GOT16_LO:
6460 case elfcpp::R_POWERPC_GOT16_HI:
6461 case elfcpp::R_POWERPC_GOT16_HA:
6462 case elfcpp::R_PPC64_GOT16_DS:
6463 case elfcpp::R_PPC64_GOT16_LO_DS:
6466 // Function calls are good, and these do need a PLT entry.
6467 case elfcpp::R_POWERPC_ADDR24:
6468 case elfcpp::R_POWERPC_ADDR14:
6469 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6470 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6471 case elfcpp::R_POWERPC_REL24:
6472 case elfcpp::R_PPC_PLTREL24:
6473 case elfcpp::R_POWERPC_REL14:
6474 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6475 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6482 // Anything else is a problem.
6483 // If we are building a static executable, the libc startup function
6484 // responsible for applying indirect function relocations is going
6485 // to complain about the reloc type.
6486 // If we are building a dynamic executable, we will have a text
6487 // relocation. The dynamic loader will set the text segment
6488 // writable and non-executable to apply text relocations. So we'll
6489 // segfault when trying to run the indirection function to resolve
6492 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
6493 object->name().c_str(), r_type);
6497 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6501 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
6503 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
6504 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
6505 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6506 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6507 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6508 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6509 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6510 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6511 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6512 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6513 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6514 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6515 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6516 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6517 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6518 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
6519 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
6520 /* Exclude lfqu by testing reloc. If relocs are ever
6521 defined for the reduced D field in psq_lu then those
6522 will need testing too. */
6523 && r_type != elfcpp::R_PPC64_TOC16_LO
6524 && r_type != elfcpp::R_POWERPC_GOT16_LO)
6525 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
6527 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
6528 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
6529 /* Exclude stfqu. psq_stu as above for psq_lu. */
6530 && r_type != elfcpp::R_PPC64_TOC16_LO
6531 && r_type != elfcpp::R_POWERPC_GOT16_LO)
6532 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
6533 && (insn & 1) == 0));
6536 // Scan a relocation for a local symbol.
6538 template<int size, bool big_endian>
6540 Target_powerpc<size, big_endian>::Scan::local(
6541 Symbol_table* symtab,
6543 Target_powerpc<size, big_endian>* target,
6544 Sized_relobj_file<size, big_endian>* object,
6545 unsigned int data_shndx,
6546 Output_section* output_section,
6547 const elfcpp::Rela<size, big_endian>& reloc,
6548 unsigned int r_type,
6549 const elfcpp::Sym<size, big_endian>& lsym,
6552 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
6554 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6555 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6557 this->expect_tls_get_addr_call();
6558 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6559 if (tls_type != tls::TLSOPT_NONE)
6560 this->skip_next_tls_get_addr_call();
6562 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6563 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6565 this->expect_tls_get_addr_call();
6566 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6567 if (tls_type != tls::TLSOPT_NONE)
6568 this->skip_next_tls_get_addr_call();
6571 Powerpc_relobj<size, big_endian>* ppc_object
6572 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6577 && data_shndx == ppc_object->opd_shndx()
6578 && r_type == elfcpp::R_PPC64_ADDR64)
6579 ppc_object->set_opd_discard(reloc.get_r_offset());
6583 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6584 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
6585 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
6587 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6588 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6589 r_type, r_sym, reloc.get_r_addend());
6590 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
6595 case elfcpp::R_POWERPC_NONE:
6596 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6597 case elfcpp::R_POWERPC_GNU_VTENTRY:
6598 case elfcpp::R_POWERPC_TLS:
6599 case elfcpp::R_PPC64_ENTRY:
6602 case elfcpp::R_PPC64_TOC:
6604 Output_data_got_powerpc<size, big_endian>* got
6605 = target->got_section(symtab, layout);
6606 if (parameters->options().output_is_position_independent())
6608 Address off = reloc.get_r_offset();
6610 && target->abiversion() < 2
6611 && data_shndx == ppc_object->opd_shndx()
6612 && ppc_object->get_opd_discard(off - 8))
6615 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6616 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
6617 rela_dyn->add_output_section_relative(got->output_section(),
6618 elfcpp::R_POWERPC_RELATIVE,
6620 object, data_shndx, off,
6621 symobj->toc_base_offset());
6626 case elfcpp::R_PPC64_ADDR64:
6627 case elfcpp::R_PPC64_UADDR64:
6628 case elfcpp::R_POWERPC_ADDR32:
6629 case elfcpp::R_POWERPC_UADDR32:
6630 case elfcpp::R_POWERPC_ADDR24:
6631 case elfcpp::R_POWERPC_ADDR16:
6632 case elfcpp::R_POWERPC_ADDR16_LO:
6633 case elfcpp::R_POWERPC_ADDR16_HI:
6634 case elfcpp::R_POWERPC_ADDR16_HA:
6635 case elfcpp::R_POWERPC_UADDR16:
6636 case elfcpp::R_PPC64_ADDR16_HIGH:
6637 case elfcpp::R_PPC64_ADDR16_HIGHA:
6638 case elfcpp::R_PPC64_ADDR16_HIGHER:
6639 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6640 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6641 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6642 case elfcpp::R_PPC64_ADDR16_DS:
6643 case elfcpp::R_PPC64_ADDR16_LO_DS:
6644 case elfcpp::R_POWERPC_ADDR14:
6645 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6646 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6647 // If building a shared library (or a position-independent
6648 // executable), we need to create a dynamic relocation for
6650 if (parameters->options().output_is_position_independent()
6651 || (size == 64 && is_ifunc && target->abiversion() < 2))
6653 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6655 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6656 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
6657 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
6659 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6660 : elfcpp::R_POWERPC_RELATIVE);
6661 rela_dyn->add_local_relative(object, r_sym, dynrel,
6662 output_section, data_shndx,
6663 reloc.get_r_offset(),
6664 reloc.get_r_addend(), false);
6666 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
6668 check_non_pic(object, r_type);
6669 rela_dyn->add_local(object, r_sym, r_type, output_section,
6670 data_shndx, reloc.get_r_offset(),
6671 reloc.get_r_addend());
6675 gold_assert(lsym.get_st_value() == 0);
6676 unsigned int shndx = lsym.get_st_shndx();
6678 shndx = object->adjust_sym_shndx(r_sym, shndx,
6681 object->error(_("section symbol %u has bad shndx %u"),
6684 rela_dyn->add_local_section(object, shndx, r_type,
6685 output_section, data_shndx,
6686 reloc.get_r_offset());
6691 case elfcpp::R_POWERPC_REL24:
6692 case elfcpp::R_PPC_PLTREL24:
6693 case elfcpp::R_PPC_LOCAL24PC:
6694 case elfcpp::R_POWERPC_REL14:
6695 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6696 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6699 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6700 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6701 r_type, r_sym, reloc.get_r_addend());
6705 case elfcpp::R_PPC64_TOCSAVE:
6706 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6707 // caller has already saved r2 and thus a plt call stub need not
6710 && target->mark_pltcall(ppc_object, data_shndx,
6711 reloc.get_r_offset() - 4, symtab))
6713 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6714 unsigned int shndx = lsym.get_st_shndx();
6716 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6718 object->error(_("tocsave symbol %u has bad shndx %u"),
6721 target->add_tocsave(ppc_object, shndx,
6722 lsym.get_st_value() + reloc.get_r_addend());
6726 case elfcpp::R_PPC64_REL64:
6727 case elfcpp::R_POWERPC_REL32:
6728 case elfcpp::R_POWERPC_REL16:
6729 case elfcpp::R_POWERPC_REL16_LO:
6730 case elfcpp::R_POWERPC_REL16_HI:
6731 case elfcpp::R_POWERPC_REL16_HA:
6732 case elfcpp::R_POWERPC_REL16DX_HA:
6733 case elfcpp::R_POWERPC_SECTOFF:
6734 case elfcpp::R_POWERPC_SECTOFF_LO:
6735 case elfcpp::R_POWERPC_SECTOFF_HI:
6736 case elfcpp::R_POWERPC_SECTOFF_HA:
6737 case elfcpp::R_PPC64_SECTOFF_DS:
6738 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6739 case elfcpp::R_POWERPC_TPREL16:
6740 case elfcpp::R_POWERPC_TPREL16_LO:
6741 case elfcpp::R_POWERPC_TPREL16_HI:
6742 case elfcpp::R_POWERPC_TPREL16_HA:
6743 case elfcpp::R_PPC64_TPREL16_DS:
6744 case elfcpp::R_PPC64_TPREL16_LO_DS:
6745 case elfcpp::R_PPC64_TPREL16_HIGH:
6746 case elfcpp::R_PPC64_TPREL16_HIGHA:
6747 case elfcpp::R_PPC64_TPREL16_HIGHER:
6748 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6749 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6750 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6751 case elfcpp::R_POWERPC_DTPREL16:
6752 case elfcpp::R_POWERPC_DTPREL16_LO:
6753 case elfcpp::R_POWERPC_DTPREL16_HI:
6754 case elfcpp::R_POWERPC_DTPREL16_HA:
6755 case elfcpp::R_PPC64_DTPREL16_DS:
6756 case elfcpp::R_PPC64_DTPREL16_LO_DS:
6757 case elfcpp::R_PPC64_DTPREL16_HIGH:
6758 case elfcpp::R_PPC64_DTPREL16_HIGHA:
6759 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6760 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6761 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6762 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6763 case elfcpp::R_PPC64_TLSGD:
6764 case elfcpp::R_PPC64_TLSLD:
6765 case elfcpp::R_PPC64_ADDR64_LOCAL:
6768 case elfcpp::R_POWERPC_GOT16:
6769 case elfcpp::R_POWERPC_GOT16_LO:
6770 case elfcpp::R_POWERPC_GOT16_HI:
6771 case elfcpp::R_POWERPC_GOT16_HA:
6772 case elfcpp::R_PPC64_GOT16_DS:
6773 case elfcpp::R_PPC64_GOT16_LO_DS:
6775 // The symbol requires a GOT entry.
6776 Output_data_got_powerpc<size, big_endian>* got
6777 = target->got_section(symtab, layout);
6778 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6780 if (!parameters->options().output_is_position_independent())
6783 && (size == 32 || target->abiversion() >= 2))
6784 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
6786 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
6788 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
6790 // If we are generating a shared object or a pie, this
6791 // symbol's GOT entry will be set by a dynamic relocation.
6793 off = got->add_constant(0);
6794 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
6796 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6798 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6799 : elfcpp::R_POWERPC_RELATIVE);
6800 rela_dyn->add_local_relative(object, r_sym, dynrel,
6801 got, off, 0, false);
6806 case elfcpp::R_PPC64_TOC16:
6807 case elfcpp::R_PPC64_TOC16_LO:
6808 case elfcpp::R_PPC64_TOC16_HI:
6809 case elfcpp::R_PPC64_TOC16_HA:
6810 case elfcpp::R_PPC64_TOC16_DS:
6811 case elfcpp::R_PPC64_TOC16_LO_DS:
6812 // We need a GOT section.
6813 target->got_section(symtab, layout);
6816 case elfcpp::R_POWERPC_GOT_TLSGD16:
6817 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6818 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6819 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6821 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6822 if (tls_type == tls::TLSOPT_NONE)
6824 Output_data_got_powerpc<size, big_endian>* got
6825 = target->got_section(symtab, layout);
6826 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6827 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6828 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
6829 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
6831 else if (tls_type == tls::TLSOPT_TO_LE)
6833 // no GOT relocs needed for Local Exec.
6840 case elfcpp::R_POWERPC_GOT_TLSLD16:
6841 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6842 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6843 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6845 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6846 if (tls_type == tls::TLSOPT_NONE)
6847 target->tlsld_got_offset(symtab, layout, object);
6848 else if (tls_type == tls::TLSOPT_TO_LE)
6850 // no GOT relocs needed for Local Exec.
6851 if (parameters->options().emit_relocs())
6853 Output_section* os = layout->tls_segment()->first_section();
6854 gold_assert(os != NULL);
6855 os->set_needs_symtab_index();
6863 case elfcpp::R_POWERPC_GOT_DTPREL16:
6864 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6865 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6866 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6868 Output_data_got_powerpc<size, big_endian>* got
6869 = target->got_section(symtab, layout);
6870 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6871 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
6875 case elfcpp::R_POWERPC_GOT_TPREL16:
6876 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6877 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6878 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6880 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
6881 if (tls_type == tls::TLSOPT_NONE)
6883 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6884 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
6886 Output_data_got_powerpc<size, big_endian>* got
6887 = target->got_section(symtab, layout);
6888 unsigned int off = got->add_constant(0);
6889 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
6891 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6892 rela_dyn->add_symbolless_local_addend(object, r_sym,
6893 elfcpp::R_POWERPC_TPREL,
6897 else if (tls_type == tls::TLSOPT_TO_LE)
6899 // no GOT relocs needed for Local Exec.
6907 unsupported_reloc_local(object, r_type);
6912 && parameters->options().toc_optimize())
6914 if (data_shndx == ppc_object->toc_shndx())
6917 if (r_type != elfcpp::R_PPC64_ADDR64
6918 || (is_ifunc && target->abiversion() < 2))
6920 else if (parameters->options().output_is_position_independent())
6926 unsigned int shndx = lsym.get_st_shndx();
6927 if (shndx >= elfcpp::SHN_LORESERVE
6928 && shndx != elfcpp::SHN_XINDEX)
6933 ppc_object->set_no_toc_opt(reloc.get_r_offset());
6936 enum {no_check, check_lo, check_ha} insn_check;
6940 insn_check = no_check;
6943 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6944 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6945 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6946 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6947 case elfcpp::R_POWERPC_GOT16_HA:
6948 case elfcpp::R_PPC64_TOC16_HA:
6949 insn_check = check_ha;
6952 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6953 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6954 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6955 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6956 case elfcpp::R_POWERPC_GOT16_LO:
6957 case elfcpp::R_PPC64_GOT16_LO_DS:
6958 case elfcpp::R_PPC64_TOC16_LO:
6959 case elfcpp::R_PPC64_TOC16_LO_DS:
6960 insn_check = check_lo;
6964 section_size_type slen;
6965 const unsigned char* view = NULL;
6966 if (insn_check != no_check)
6968 view = ppc_object->section_contents(data_shndx, &slen, false);
6969 section_size_type off =
6970 convert_to_section_size_type(reloc.get_r_offset()) & -4;
6973 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
6974 if (insn_check == check_lo
6975 ? !ok_lo_toc_insn(insn, r_type)
6976 : ((insn & ((0x3f << 26) | 0x1f << 16))
6977 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
6979 ppc_object->set_no_toc_opt();
6980 gold_warning(_("%s: toc optimization is not supported "
6981 "for %#08x instruction"),
6982 ppc_object->name().c_str(), insn);
6991 case elfcpp::R_PPC64_TOC16:
6992 case elfcpp::R_PPC64_TOC16_LO:
6993 case elfcpp::R_PPC64_TOC16_HI:
6994 case elfcpp::R_PPC64_TOC16_HA:
6995 case elfcpp::R_PPC64_TOC16_DS:
6996 case elfcpp::R_PPC64_TOC16_LO_DS:
6997 unsigned int shndx = lsym.get_st_shndx();
6998 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7000 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7001 if (is_ordinary && shndx == ppc_object->toc_shndx())
7003 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
7004 if (dst_off < ppc_object->section_size(shndx))
7007 if (r_type == elfcpp::R_PPC64_TOC16_HA)
7009 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
7011 // Need to check that the insn is a ld
7013 view = ppc_object->section_contents(data_shndx,
7016 section_size_type off =
7017 (convert_to_section_size_type(reloc.get_r_offset())
7018 + (big_endian ? -2 : 3));
7020 && (view[off] & (0x3f << 2)) == 58u << 2)
7024 ppc_object->set_no_toc_opt(dst_off);
7035 case elfcpp::R_POWERPC_REL32:
7036 if (ppc_object->got2_shndx() != 0
7037 && parameters->options().output_is_position_independent())
7039 unsigned int shndx = lsym.get_st_shndx();
7040 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7042 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7043 if (is_ordinary && shndx == ppc_object->got2_shndx()
7044 && (ppc_object->section_flags(data_shndx)
7045 & elfcpp::SHF_EXECINSTR) != 0)
7046 gold_error(_("%s: unsupported -mbss-plt code"),
7047 ppc_object->name().c_str());
7057 case elfcpp::R_POWERPC_GOT_TLSLD16:
7058 case elfcpp::R_POWERPC_GOT_TLSGD16:
7059 case elfcpp::R_POWERPC_GOT_TPREL16:
7060 case elfcpp::R_POWERPC_GOT_DTPREL16:
7061 case elfcpp::R_POWERPC_GOT16:
7062 case elfcpp::R_PPC64_GOT16_DS:
7063 case elfcpp::R_PPC64_TOC16:
7064 case elfcpp::R_PPC64_TOC16_DS:
7065 ppc_object->set_has_small_toc_reloc();
7071 // Report an unsupported relocation against a global symbol.
7073 template<int size, bool big_endian>
7075 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
7076 Sized_relobj_file<size, big_endian>* object,
7077 unsigned int r_type,
7080 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7081 object->name().c_str(), r_type, gsym->demangled_name().c_str());
7084 // Scan a relocation for a global symbol.
7086 template<int size, bool big_endian>
7088 Target_powerpc<size, big_endian>::Scan::global(
7089 Symbol_table* symtab,
7091 Target_powerpc<size, big_endian>* target,
7092 Sized_relobj_file<size, big_endian>* object,
7093 unsigned int data_shndx,
7094 Output_section* output_section,
7095 const elfcpp::Rela<size, big_endian>& reloc,
7096 unsigned int r_type,
7099 if (this->maybe_skip_tls_get_addr_call(target, r_type, gsym)
7103 if (target->replace_tls_get_addr(gsym))
7104 // Change a __tls_get_addr reference to __tls_get_addr_opt
7105 // so dynamic relocs are emitted against the latter symbol.
7106 gsym = target->tls_get_addr_opt();
7108 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7109 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7111 this->expect_tls_get_addr_call();
7112 const bool final = gsym->final_value_is_known();
7113 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7114 if (tls_type != tls::TLSOPT_NONE)
7115 this->skip_next_tls_get_addr_call();
7117 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7118 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7120 this->expect_tls_get_addr_call();
7121 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7122 if (tls_type != tls::TLSOPT_NONE)
7123 this->skip_next_tls_get_addr_call();
7126 Powerpc_relobj<size, big_endian>* ppc_object
7127 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7129 // A STT_GNU_IFUNC symbol may require a PLT entry.
7130 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
7131 bool pushed_ifunc = false;
7132 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
7134 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7135 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7136 r_type, r_sym, reloc.get_r_addend());
7137 target->make_plt_entry(symtab, layout, gsym);
7138 pushed_ifunc = true;
7143 case elfcpp::R_POWERPC_NONE:
7144 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7145 case elfcpp::R_POWERPC_GNU_VTENTRY:
7146 case elfcpp::R_PPC_LOCAL24PC:
7147 case elfcpp::R_POWERPC_TLS:
7148 case elfcpp::R_PPC64_ENTRY:
7151 case elfcpp::R_PPC64_TOC:
7153 Output_data_got_powerpc<size, big_endian>* got
7154 = target->got_section(symtab, layout);
7155 if (parameters->options().output_is_position_independent())
7157 Address off = reloc.get_r_offset();
7159 && data_shndx == ppc_object->opd_shndx()
7160 && ppc_object->get_opd_discard(off - 8))
7163 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7164 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
7165 if (data_shndx != ppc_object->opd_shndx())
7166 symobj = static_cast
7167 <Powerpc_relobj<size, big_endian>*>(gsym->object());
7168 rela_dyn->add_output_section_relative(got->output_section(),
7169 elfcpp::R_POWERPC_RELATIVE,
7171 object, data_shndx, off,
7172 symobj->toc_base_offset());
7177 case elfcpp::R_PPC64_ADDR64:
7179 && target->abiversion() < 2
7180 && data_shndx == ppc_object->opd_shndx()
7181 && (gsym->is_defined_in_discarded_section()
7182 || gsym->object() != object))
7184 ppc_object->set_opd_discard(reloc.get_r_offset());
7188 case elfcpp::R_PPC64_UADDR64:
7189 case elfcpp::R_POWERPC_ADDR32:
7190 case elfcpp::R_POWERPC_UADDR32:
7191 case elfcpp::R_POWERPC_ADDR24:
7192 case elfcpp::R_POWERPC_ADDR16:
7193 case elfcpp::R_POWERPC_ADDR16_LO:
7194 case elfcpp::R_POWERPC_ADDR16_HI:
7195 case elfcpp::R_POWERPC_ADDR16_HA:
7196 case elfcpp::R_POWERPC_UADDR16:
7197 case elfcpp::R_PPC64_ADDR16_HIGH:
7198 case elfcpp::R_PPC64_ADDR16_HIGHA:
7199 case elfcpp::R_PPC64_ADDR16_HIGHER:
7200 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7201 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7202 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7203 case elfcpp::R_PPC64_ADDR16_DS:
7204 case elfcpp::R_PPC64_ADDR16_LO_DS:
7205 case elfcpp::R_POWERPC_ADDR14:
7206 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7207 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7209 // Make a PLT entry if necessary.
7210 if (gsym->needs_plt_entry())
7212 // Since this is not a PC-relative relocation, we may be
7213 // taking the address of a function. In that case we need to
7214 // set the entry in the dynamic symbol table to the address of
7215 // the PLT call stub.
7216 bool need_ifunc_plt = false;
7217 if ((size == 32 || target->abiversion() >= 2)
7218 && gsym->is_from_dynobj()
7219 && !parameters->options().output_is_position_independent())
7221 gsym->set_needs_dynsym_value();
7222 need_ifunc_plt = true;
7224 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
7226 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7227 target->push_branch(ppc_object, data_shndx,
7228 reloc.get_r_offset(), r_type, r_sym,
7229 reloc.get_r_addend());
7230 target->make_plt_entry(symtab, layout, gsym);
7233 // Make a dynamic relocation if necessary.
7234 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
7235 || (size == 64 && is_ifunc && target->abiversion() < 2))
7237 if (!parameters->options().output_is_position_independent()
7238 && gsym->may_need_copy_reloc())
7240 target->copy_reloc(symtab, layout, object,
7241 data_shndx, output_section, gsym, reloc);
7243 else if ((((size == 32
7244 && r_type == elfcpp::R_POWERPC_ADDR32)
7246 && r_type == elfcpp::R_PPC64_ADDR64
7247 && target->abiversion() >= 2))
7248 && gsym->can_use_relative_reloc(false)
7249 && !(gsym->visibility() == elfcpp::STV_PROTECTED
7250 && parameters->options().shared()))
7252 && r_type == elfcpp::R_PPC64_ADDR64
7253 && target->abiversion() < 2
7254 && (gsym->can_use_relative_reloc(false)
7255 || data_shndx == ppc_object->opd_shndx())))
7257 Reloc_section* rela_dyn
7258 = target->rela_dyn_section(symtab, layout, is_ifunc);
7259 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7260 : elfcpp::R_POWERPC_RELATIVE);
7261 rela_dyn->add_symbolless_global_addend(
7262 gsym, dynrel, output_section, object, data_shndx,
7263 reloc.get_r_offset(), reloc.get_r_addend());
7267 Reloc_section* rela_dyn
7268 = target->rela_dyn_section(symtab, layout, is_ifunc);
7269 check_non_pic(object, r_type);
7270 rela_dyn->add_global(gsym, r_type, output_section,
7272 reloc.get_r_offset(),
7273 reloc.get_r_addend());
7276 && parameters->options().toc_optimize()
7277 && data_shndx == ppc_object->toc_shndx())
7278 ppc_object->set_no_toc_opt(reloc.get_r_offset());
7284 case elfcpp::R_PPC_PLTREL24:
7285 case elfcpp::R_POWERPC_REL24:
7288 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7289 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7290 r_type, r_sym, reloc.get_r_addend());
7291 if (gsym->needs_plt_entry()
7292 || (!gsym->final_value_is_known()
7293 && (gsym->is_undefined()
7294 || gsym->is_from_dynobj()
7295 || gsym->is_preemptible())))
7296 target->make_plt_entry(symtab, layout, gsym);
7300 case elfcpp::R_PPC64_REL64:
7301 case elfcpp::R_POWERPC_REL32:
7302 // Make a dynamic relocation if necessary.
7303 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
7305 if (!parameters->options().output_is_position_independent()
7306 && gsym->may_need_copy_reloc())
7308 target->copy_reloc(symtab, layout, object,
7309 data_shndx, output_section, gsym,
7314 Reloc_section* rela_dyn
7315 = target->rela_dyn_section(symtab, layout, is_ifunc);
7316 check_non_pic(object, r_type);
7317 rela_dyn->add_global(gsym, r_type, output_section, object,
7318 data_shndx, reloc.get_r_offset(),
7319 reloc.get_r_addend());
7324 case elfcpp::R_POWERPC_REL14:
7325 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7326 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7329 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7330 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7331 r_type, r_sym, reloc.get_r_addend());
7335 case elfcpp::R_PPC64_TOCSAVE:
7336 // R_PPC64_TOCSAVE follows a call instruction to indicate the
7337 // caller has already saved r2 and thus a plt call stub need not
7340 && target->mark_pltcall(ppc_object, data_shndx,
7341 reloc.get_r_offset() - 4, symtab))
7343 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7345 unsigned int shndx = gsym->shndx(&is_ordinary);
7347 object->error(_("tocsave symbol %u has bad shndx %u"),
7351 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
7352 target->add_tocsave(ppc_object, shndx,
7353 sym->value() + reloc.get_r_addend());
7358 case elfcpp::R_POWERPC_REL16:
7359 case elfcpp::R_POWERPC_REL16_LO:
7360 case elfcpp::R_POWERPC_REL16_HI:
7361 case elfcpp::R_POWERPC_REL16_HA:
7362 case elfcpp::R_POWERPC_REL16DX_HA:
7363 case elfcpp::R_POWERPC_SECTOFF:
7364 case elfcpp::R_POWERPC_SECTOFF_LO:
7365 case elfcpp::R_POWERPC_SECTOFF_HI:
7366 case elfcpp::R_POWERPC_SECTOFF_HA:
7367 case elfcpp::R_PPC64_SECTOFF_DS:
7368 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7369 case elfcpp::R_POWERPC_TPREL16:
7370 case elfcpp::R_POWERPC_TPREL16_LO:
7371 case elfcpp::R_POWERPC_TPREL16_HI:
7372 case elfcpp::R_POWERPC_TPREL16_HA:
7373 case elfcpp::R_PPC64_TPREL16_DS:
7374 case elfcpp::R_PPC64_TPREL16_LO_DS:
7375 case elfcpp::R_PPC64_TPREL16_HIGH:
7376 case elfcpp::R_PPC64_TPREL16_HIGHA:
7377 case elfcpp::R_PPC64_TPREL16_HIGHER:
7378 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7379 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7380 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7381 case elfcpp::R_POWERPC_DTPREL16:
7382 case elfcpp::R_POWERPC_DTPREL16_LO:
7383 case elfcpp::R_POWERPC_DTPREL16_HI:
7384 case elfcpp::R_POWERPC_DTPREL16_HA:
7385 case elfcpp::R_PPC64_DTPREL16_DS:
7386 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7387 case elfcpp::R_PPC64_DTPREL16_HIGH:
7388 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7389 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7390 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7391 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7392 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7393 case elfcpp::R_PPC64_TLSGD:
7394 case elfcpp::R_PPC64_TLSLD:
7395 case elfcpp::R_PPC64_ADDR64_LOCAL:
7398 case elfcpp::R_POWERPC_GOT16:
7399 case elfcpp::R_POWERPC_GOT16_LO:
7400 case elfcpp::R_POWERPC_GOT16_HI:
7401 case elfcpp::R_POWERPC_GOT16_HA:
7402 case elfcpp::R_PPC64_GOT16_DS:
7403 case elfcpp::R_PPC64_GOT16_LO_DS:
7405 // The symbol requires a GOT entry.
7406 Output_data_got_powerpc<size, big_endian>* got;
7408 got = target->got_section(symtab, layout);
7409 if (gsym->final_value_is_known())
7412 && (size == 32 || target->abiversion() >= 2))
7413 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
7415 got->add_global(gsym, GOT_TYPE_STANDARD);
7417 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
7419 // If we are generating a shared object or a pie, this
7420 // symbol's GOT entry will be set by a dynamic relocation.
7421 unsigned int off = got->add_constant(0);
7422 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
7424 Reloc_section* rela_dyn
7425 = target->rela_dyn_section(symtab, layout, is_ifunc);
7427 if (gsym->can_use_relative_reloc(false)
7429 || target->abiversion() >= 2)
7430 && gsym->visibility() == elfcpp::STV_PROTECTED
7431 && parameters->options().shared()))
7433 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7434 : elfcpp::R_POWERPC_RELATIVE);
7435 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
7439 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
7440 rela_dyn->add_global(gsym, dynrel, got, off, 0);
7446 case elfcpp::R_PPC64_TOC16:
7447 case elfcpp::R_PPC64_TOC16_LO:
7448 case elfcpp::R_PPC64_TOC16_HI:
7449 case elfcpp::R_PPC64_TOC16_HA:
7450 case elfcpp::R_PPC64_TOC16_DS:
7451 case elfcpp::R_PPC64_TOC16_LO_DS:
7452 // We need a GOT section.
7453 target->got_section(symtab, layout);
7456 case elfcpp::R_POWERPC_GOT_TLSGD16:
7457 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7458 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7459 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7461 const bool final = gsym->final_value_is_known();
7462 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7463 if (tls_type == tls::TLSOPT_NONE)
7465 Output_data_got_powerpc<size, big_endian>* got
7466 = target->got_section(symtab, layout);
7467 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7468 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
7469 elfcpp::R_POWERPC_DTPMOD,
7470 elfcpp::R_POWERPC_DTPREL);
7472 else if (tls_type == tls::TLSOPT_TO_IE)
7474 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
7476 Output_data_got_powerpc<size, big_endian>* got
7477 = target->got_section(symtab, layout);
7478 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7479 if (gsym->is_undefined()
7480 || gsym->is_from_dynobj())
7482 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
7483 elfcpp::R_POWERPC_TPREL);
7487 unsigned int off = got->add_constant(0);
7488 gsym->set_got_offset(GOT_TYPE_TPREL, off);
7489 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
7490 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
7495 else if (tls_type == tls::TLSOPT_TO_LE)
7497 // no GOT relocs needed for Local Exec.
7504 case elfcpp::R_POWERPC_GOT_TLSLD16:
7505 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7506 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7507 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7509 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7510 if (tls_type == tls::TLSOPT_NONE)
7511 target->tlsld_got_offset(symtab, layout, object);
7512 else if (tls_type == tls::TLSOPT_TO_LE)
7514 // no GOT relocs needed for Local Exec.
7515 if (parameters->options().emit_relocs())
7517 Output_section* os = layout->tls_segment()->first_section();
7518 gold_assert(os != NULL);
7519 os->set_needs_symtab_index();
7527 case elfcpp::R_POWERPC_GOT_DTPREL16:
7528 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7529 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7530 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7532 Output_data_got_powerpc<size, big_endian>* got
7533 = target->got_section(symtab, layout);
7534 if (!gsym->final_value_is_known()
7535 && (gsym->is_from_dynobj()
7536 || gsym->is_undefined()
7537 || gsym->is_preemptible()))
7538 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
7539 target->rela_dyn_section(layout),
7540 elfcpp::R_POWERPC_DTPREL);
7542 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
7546 case elfcpp::R_POWERPC_GOT_TPREL16:
7547 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7548 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7549 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7551 const bool final = gsym->final_value_is_known();
7552 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7553 if (tls_type == tls::TLSOPT_NONE)
7555 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
7557 Output_data_got_powerpc<size, big_endian>* got
7558 = target->got_section(symtab, layout);
7559 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7560 if (gsym->is_undefined()
7561 || gsym->is_from_dynobj())
7563 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
7564 elfcpp::R_POWERPC_TPREL);
7568 unsigned int off = got->add_constant(0);
7569 gsym->set_got_offset(GOT_TYPE_TPREL, off);
7570 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
7571 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
7576 else if (tls_type == tls::TLSOPT_TO_LE)
7578 // no GOT relocs needed for Local Exec.
7586 unsupported_reloc_global(object, r_type, gsym);
7591 && parameters->options().toc_optimize())
7593 if (data_shndx == ppc_object->toc_shndx())
7596 if (r_type != elfcpp::R_PPC64_ADDR64
7597 || (is_ifunc && target->abiversion() < 2))
7599 else if (parameters->options().output_is_position_independent()
7600 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
7603 ppc_object->set_no_toc_opt(reloc.get_r_offset());
7606 enum {no_check, check_lo, check_ha} insn_check;
7610 insn_check = no_check;
7613 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7614 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7615 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7616 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7617 case elfcpp::R_POWERPC_GOT16_HA:
7618 case elfcpp::R_PPC64_TOC16_HA:
7619 insn_check = check_ha;
7622 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7623 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7624 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7625 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7626 case elfcpp::R_POWERPC_GOT16_LO:
7627 case elfcpp::R_PPC64_GOT16_LO_DS:
7628 case elfcpp::R_PPC64_TOC16_LO:
7629 case elfcpp::R_PPC64_TOC16_LO_DS:
7630 insn_check = check_lo;
7634 section_size_type slen;
7635 const unsigned char* view = NULL;
7636 if (insn_check != no_check)
7638 view = ppc_object->section_contents(data_shndx, &slen, false);
7639 section_size_type off =
7640 convert_to_section_size_type(reloc.get_r_offset()) & -4;
7643 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
7644 if (insn_check == check_lo
7645 ? !ok_lo_toc_insn(insn, r_type)
7646 : ((insn & ((0x3f << 26) | 0x1f << 16))
7647 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7649 ppc_object->set_no_toc_opt();
7650 gold_warning(_("%s: toc optimization is not supported "
7651 "for %#08x instruction"),
7652 ppc_object->name().c_str(), insn);
7661 case elfcpp::R_PPC64_TOC16:
7662 case elfcpp::R_PPC64_TOC16_LO:
7663 case elfcpp::R_PPC64_TOC16_HI:
7664 case elfcpp::R_PPC64_TOC16_HA:
7665 case elfcpp::R_PPC64_TOC16_DS:
7666 case elfcpp::R_PPC64_TOC16_LO_DS:
7667 if (gsym->source() == Symbol::FROM_OBJECT
7668 && !gsym->object()->is_dynamic())
7670 Powerpc_relobj<size, big_endian>* sym_object
7671 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
7673 unsigned int shndx = gsym->shndx(&is_ordinary);
7674 if (shndx == sym_object->toc_shndx())
7676 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
7677 Address dst_off = sym->value() + reloc.get_r_addend();
7678 if (dst_off < sym_object->section_size(shndx))
7681 if (r_type == elfcpp::R_PPC64_TOC16_HA)
7683 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
7685 // Need to check that the insn is a ld
7687 view = ppc_object->section_contents(data_shndx,
7690 section_size_type off =
7691 (convert_to_section_size_type(reloc.get_r_offset())
7692 + (big_endian ? -2 : 3));
7694 && (view[off] & (0x3f << 2)) == (58u << 2))
7698 sym_object->set_no_toc_opt(dst_off);
7710 case elfcpp::R_PPC_LOCAL24PC:
7711 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7712 gold_error(_("%s: unsupported -mbss-plt code"),
7713 ppc_object->name().c_str());
7722 case elfcpp::R_POWERPC_GOT_TLSLD16:
7723 case elfcpp::R_POWERPC_GOT_TLSGD16:
7724 case elfcpp::R_POWERPC_GOT_TPREL16:
7725 case elfcpp::R_POWERPC_GOT_DTPREL16:
7726 case elfcpp::R_POWERPC_GOT16:
7727 case elfcpp::R_PPC64_GOT16_DS:
7728 case elfcpp::R_PPC64_TOC16:
7729 case elfcpp::R_PPC64_TOC16_DS:
7730 ppc_object->set_has_small_toc_reloc();
7736 // Process relocations for gc.
7738 template<int size, bool big_endian>
7740 Target_powerpc<size, big_endian>::gc_process_relocs(
7741 Symbol_table* symtab,
7743 Sized_relobj_file<size, big_endian>* object,
7744 unsigned int data_shndx,
7746 const unsigned char* prelocs,
7748 Output_section* output_section,
7749 bool needs_special_offset_handling,
7750 size_t local_symbol_count,
7751 const unsigned char* plocal_symbols)
7753 typedef Target_powerpc<size, big_endian> Powerpc;
7754 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7757 Powerpc_relobj<size, big_endian>* ppc_object
7758 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7760 ppc_object->set_opd_valid();
7761 if (size == 64 && data_shndx == ppc_object->opd_shndx())
7763 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
7764 for (p = ppc_object->access_from_map()->begin();
7765 p != ppc_object->access_from_map()->end();
7768 Address dst_off = p->first;
7769 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
7770 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
7771 for (s = p->second.begin(); s != p->second.end(); ++s)
7773 Relobj* src_obj = s->first;
7774 unsigned int src_indx = s->second;
7775 symtab->gc()->add_reference(src_obj, src_indx,
7776 ppc_object, dst_indx);
7780 ppc_object->access_from_map()->clear();
7781 ppc_object->process_gc_mark(symtab);
7782 // Don't look at .opd relocs as .opd will reference everything.
7786 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
7795 needs_special_offset_handling,
7800 // Handle target specific gc actions when adding a gc reference from
7801 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7802 // and DST_OFF. For powerpc64, this adds a referenc to the code
7803 // section of a function descriptor.
7805 template<int size, bool big_endian>
7807 Target_powerpc<size, big_endian>::do_gc_add_reference(
7808 Symbol_table* symtab,
7810 unsigned int src_shndx,
7812 unsigned int dst_shndx,
7813 Address dst_off) const
7815 if (size != 64 || dst_obj->is_dynamic())
7818 Powerpc_relobj<size, big_endian>* ppc_object
7819 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
7820 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
7822 if (ppc_object->opd_valid())
7824 dst_shndx = ppc_object->get_opd_ent(dst_off);
7825 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
7829 // If we haven't run scan_opd_relocs, we must delay
7830 // processing this function descriptor reference.
7831 ppc_object->add_reference(src_obj, src_shndx, dst_off);
7836 // Add any special sections for this symbol to the gc work list.
7837 // For powerpc64, this adds the code section of a function
7840 template<int size, bool big_endian>
7842 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
7843 Symbol_table* symtab,
7848 Powerpc_relobj<size, big_endian>* ppc_object
7849 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
7851 unsigned int shndx = sym->shndx(&is_ordinary);
7852 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
7854 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
7855 Address dst_off = gsym->value();
7856 if (ppc_object->opd_valid())
7858 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
7859 symtab->gc()->worklist().push_back(Section_id(ppc_object,
7863 ppc_object->add_gc_mark(dst_off);
7868 // For a symbol location in .opd, set LOC to the location of the
7871 template<int size, bool big_endian>
7873 Target_powerpc<size, big_endian>::do_function_location(
7874 Symbol_location* loc) const
7876 if (size == 64 && loc->shndx != 0)
7878 if (loc->object->is_dynamic())
7880 Powerpc_dynobj<size, big_endian>* ppc_object
7881 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
7882 if (loc->shndx == ppc_object->opd_shndx())
7885 Address off = loc->offset - ppc_object->opd_address();
7886 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
7887 loc->offset = dest_off;
7892 const Powerpc_relobj<size, big_endian>* ppc_object
7893 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
7894 if (loc->shndx == ppc_object->opd_shndx())
7897 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
7898 loc->offset = dest_off;
7904 // FNOFFSET in section SHNDX in OBJECT is the start of a function
7905 // compiled with -fsplit-stack. The function calls non-split-stack
7906 // code. Change the function to ensure it has enough stack space to
7907 // call some random function.
7909 template<int size, bool big_endian>
7911 Target_powerpc<size, big_endian>::do_calls_non_split(
7914 section_offset_type fnoffset,
7915 section_size_type fnsize,
7916 const unsigned char* prelocs,
7918 unsigned char* view,
7919 section_size_type view_size,
7921 std::string* to) const
7923 // 32-bit not supported.
7927 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
7928 prelocs, reloc_count, view, view_size,
7933 // The function always starts with
7934 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
7935 // addis %r12,%r1,-allocate@ha
7936 // addi %r12,%r12,-allocate@l
7938 // but note that the addis or addi may be replaced with a nop
7940 unsigned char *entry = view + fnoffset;
7941 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
7943 if ((insn & 0xffff0000) == addis_2_12)
7945 /* Skip ELFv2 global entry code. */
7947 insn = elfcpp::Swap<32, big_endian>::readval(entry);
7950 unsigned char *pinsn = entry;
7952 const uint32_t ld_private_ss = 0xe80d8fc0;
7953 if (insn == ld_private_ss)
7955 int32_t allocate = 0;
7959 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
7960 if ((insn & 0xffff0000) == addis_12_1)
7961 allocate += (insn & 0xffff) << 16;
7962 else if ((insn & 0xffff0000) == addi_12_1
7963 || (insn & 0xffff0000) == addi_12_12)
7964 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
7965 else if (insn != nop)
7968 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
7970 int extra = parameters->options().split_stack_adjust_size();
7972 if (allocate >= 0 || extra < 0)
7974 object->error(_("split-stack stack size overflow at "
7975 "section %u offset %0zx"),
7976 shndx, static_cast<size_t>(fnoffset));
7980 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
7981 if (insn != addis_12_1)
7983 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7985 insn = addi_12_12 | (allocate & 0xffff);
7986 if (insn != addi_12_12)
7988 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7994 insn = addi_12_1 | (allocate & 0xffff);
7995 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7998 if (pinsn != entry + 12)
7999 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
8007 if (!object->has_no_split_stack())
8008 object->error(_("failed to match split-stack sequence at "
8009 "section %u offset %0zx"),
8010 shndx, static_cast<size_t>(fnoffset));
8014 // Scan relocations for a section.
8016 template<int size, bool big_endian>
8018 Target_powerpc<size, big_endian>::scan_relocs(
8019 Symbol_table* symtab,
8021 Sized_relobj_file<size, big_endian>* object,
8022 unsigned int data_shndx,
8023 unsigned int sh_type,
8024 const unsigned char* prelocs,
8026 Output_section* output_section,
8027 bool needs_special_offset_handling,
8028 size_t local_symbol_count,
8029 const unsigned char* plocal_symbols)
8031 typedef Target_powerpc<size, big_endian> Powerpc;
8032 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8035 if (!this->plt_localentry0_init_)
8037 bool plt_localentry0 = false;
8039 && this->abiversion() >= 2)
8041 if (parameters->options().user_set_plt_localentry())
8042 plt_localentry0 = parameters->options().plt_localentry();
8044 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
8045 gold_warning(_("--plt-localentry is especially dangerous without "
8046 "ld.so support to detect ABI violations"));
8048 this->plt_localentry0_ = plt_localentry0;
8049 this->plt_localentry0_init_ = true;
8052 if (sh_type == elfcpp::SHT_REL)
8054 gold_error(_("%s: unsupported REL reloc section"),
8055 object->name().c_str());
8059 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
8068 needs_special_offset_handling,
8073 // Functor class for processing the global symbol table.
8074 // Removes symbols defined on discarded opd entries.
8076 template<bool big_endian>
8077 class Global_symbol_visitor_opd
8080 Global_symbol_visitor_opd()
8084 operator()(Sized_symbol<64>* sym)
8086 if (sym->has_symtab_index()
8087 || sym->source() != Symbol::FROM_OBJECT
8088 || !sym->in_real_elf())
8091 if (sym->object()->is_dynamic())
8094 Powerpc_relobj<64, big_endian>* symobj
8095 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
8096 if (symobj->opd_shndx() == 0)
8100 unsigned int shndx = sym->shndx(&is_ordinary);
8101 if (shndx == symobj->opd_shndx()
8102 && symobj->get_opd_discard(sym->value()))
8104 sym->set_undefined();
8105 sym->set_visibility(elfcpp::STV_DEFAULT);
8106 sym->set_is_defined_in_discarded_section();
8107 sym->set_symtab_index(-1U);
8112 template<int size, bool big_endian>
8114 Target_powerpc<size, big_endian>::define_save_restore_funcs(
8116 Symbol_table* symtab)
8120 Output_data_save_res<size, big_endian>* savres
8121 = new Output_data_save_res<size, big_endian>(symtab);
8122 this->savres_section_ = savres;
8123 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8124 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
8125 savres, ORDER_TEXT, false);
8129 // Sort linker created .got section first (for the header), then input
8130 // sections belonging to files using small model code.
8132 template<bool big_endian>
8133 class Sort_toc_sections
8137 operator()(const Output_section::Input_section& is1,
8138 const Output_section::Input_section& is2) const
8140 if (!is1.is_input_section() && is2.is_input_section())
8143 = (is1.is_input_section()
8144 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
8145 ->has_small_toc_reloc()));
8147 = (is2.is_input_section()
8148 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
8149 ->has_small_toc_reloc()));
8150 return small1 && !small2;
8154 // Finalize the sections.
8156 template<int size, bool big_endian>
8158 Target_powerpc<size, big_endian>::do_finalize_sections(
8160 const Input_objects*,
8161 Symbol_table* symtab)
8163 if (parameters->doing_static_link())
8165 // At least some versions of glibc elf-init.o have a strong
8166 // reference to __rela_iplt marker syms. A weak ref would be
8168 if (this->iplt_ != NULL)
8170 Reloc_section* rel = this->iplt_->rel_plt();
8171 symtab->define_in_output_data("__rela_iplt_start", NULL,
8172 Symbol_table::PREDEFINED, rel, 0, 0,
8173 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8174 elfcpp::STV_HIDDEN, 0, false, true);
8175 symtab->define_in_output_data("__rela_iplt_end", NULL,
8176 Symbol_table::PREDEFINED, rel, 0, 0,
8177 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8178 elfcpp::STV_HIDDEN, 0, true, true);
8182 symtab->define_as_constant("__rela_iplt_start", NULL,
8183 Symbol_table::PREDEFINED, 0, 0,
8184 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8185 elfcpp::STV_HIDDEN, 0, true, false);
8186 symtab->define_as_constant("__rela_iplt_end", NULL,
8187 Symbol_table::PREDEFINED, 0, 0,
8188 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8189 elfcpp::STV_HIDDEN, 0, true, false);
8195 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
8196 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
8198 if (!parameters->options().relocatable())
8200 this->define_save_restore_funcs(layout, symtab);
8202 // Annoyingly, we need to make these sections now whether or
8203 // not we need them. If we delay until do_relax then we
8204 // need to mess with the relaxation machinery checkpointing.
8205 this->got_section(symtab, layout);
8206 this->make_brlt_section(layout);
8208 if (parameters->options().toc_sort())
8210 Output_section* os = this->got_->output_section();
8211 if (os != NULL && os->input_sections().size() > 1)
8212 std::stable_sort(os->input_sections().begin(),
8213 os->input_sections().end(),
8214 Sort_toc_sections<big_endian>());
8219 // Fill in some more dynamic tags.
8220 Output_data_dynamic* odyn = layout->dynamic_data();
8223 const Reloc_section* rel_plt = (this->plt_ == NULL
8225 : this->plt_->rel_plt());
8226 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
8227 this->rela_dyn_, true, size == 32);
8231 if (this->got_ != NULL)
8233 this->got_->finalize_data_size();
8234 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
8235 this->got_, this->got_->g_o_t());
8237 if (this->has_tls_get_addr_opt_)
8238 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
8242 if (this->glink_ != NULL)
8244 this->glink_->finalize_data_size();
8245 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
8247 (this->glink_->pltresolve_size()
8250 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
8251 odyn->add_constant(elfcpp::DT_PPC64_OPT,
8252 ((this->has_localentry0_
8253 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
8254 | (this->has_tls_get_addr_opt_
8255 ? elfcpp::PPC64_OPT_TLS : 0)));
8259 // Emit any relocs we saved in an attempt to avoid generating COPY
8261 if (this->copy_relocs_.any_saved_relocs())
8262 this->copy_relocs_.emit(this->rela_dyn_section(layout));
8265 // Emit any saved relocs, and mark toc entries using any of these
8266 // relocs as not optimizable.
8268 template<int sh_type, int size, bool big_endian>
8270 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
8271 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
8274 && parameters->options().toc_optimize())
8276 for (typename Copy_relocs<sh_type, size, big_endian>::
8277 Copy_reloc_entries::iterator p = this->entries_.begin();
8278 p != this->entries_.end();
8281 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
8284 // If the symbol is no longer defined in a dynamic object,
8285 // then we emitted a COPY relocation. If it is still
8286 // dynamic then we'll need dynamic relocations and thus
8287 // can't optimize toc entries.
8288 if (entry.sym_->is_from_dynobj())
8290 Powerpc_relobj<size, big_endian>* ppc_object
8291 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
8292 if (entry.shndx_ == ppc_object->toc_shndx())
8293 ppc_object->set_no_toc_opt(entry.address_);
8298 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
8301 // Return the value to use for a branch relocation.
8303 template<int size, bool big_endian>
8305 Target_powerpc<size, big_endian>::symval_for_branch(
8306 const Symbol_table* symtab,
8307 const Sized_symbol<size>* gsym,
8308 Powerpc_relobj<size, big_endian>* object,
8310 unsigned int *dest_shndx)
8312 if (size == 32 || this->abiversion() >= 2)
8316 // If the symbol is defined in an opd section, ie. is a function
8317 // descriptor, use the function descriptor code entry address
8318 Powerpc_relobj<size, big_endian>* symobj = object;
8320 && (gsym->source() != Symbol::FROM_OBJECT
8321 || gsym->object()->is_dynamic()))
8324 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
8325 unsigned int shndx = symobj->opd_shndx();
8328 Address opd_addr = symobj->get_output_section_offset(shndx);
8329 if (opd_addr == invalid_address)
8331 opd_addr += symobj->output_section_address(shndx);
8332 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
8335 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
8336 if (symtab->is_section_folded(symobj, *dest_shndx))
8339 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
8340 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
8341 *dest_shndx = folded.second;
8343 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
8344 if (sec_addr == invalid_address)
8347 sec_addr += symobj->output_section(*dest_shndx)->address();
8348 *value = sec_addr + sec_off;
8353 // Perform a relocation.
8355 template<int size, bool big_endian>
8357 Target_powerpc<size, big_endian>::Relocate::relocate(
8358 const Relocate_info<size, big_endian>* relinfo,
8360 Target_powerpc* target,
8363 const unsigned char* preloc,
8364 const Sized_symbol<size>* gsym,
8365 const Symbol_value<size>* psymval,
8366 unsigned char* view,
8368 section_size_type view_size)
8373 if (target->replace_tls_get_addr(gsym))
8374 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
8376 const elfcpp::Rela<size, big_endian> rela(preloc);
8377 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
8378 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
8380 case Track_tls::NOT_EXPECTED:
8381 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8382 _("__tls_get_addr call lacks marker reloc"));
8384 case Track_tls::EXPECTED:
8385 // We have already complained.
8387 case Track_tls::SKIP:
8389 case Track_tls::NORMAL:
8393 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
8394 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
8395 typedef typename elfcpp::Rela<size, big_endian> Reltype;
8396 // Offset from start of insn to d-field reloc.
8397 const int d_offset = big_endian ? 2 : 0;
8399 Powerpc_relobj<size, big_endian>* const object
8400 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
8402 bool has_stub_value = false;
8403 bool localentry0 = false;
8404 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
8406 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
8407 : object->local_has_plt_offset(r_sym))
8408 && (!psymval->is_ifunc_symbol()
8409 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
8413 && target->abiversion() >= 2
8414 && !parameters->options().output_is_position_independent()
8415 && !is_branch_reloc(r_type))
8417 Address off = target->glink_section()->find_global_entry(gsym);
8418 if (off != invalid_address)
8420 value = target->glink_section()->global_entry_address() + off;
8421 has_stub_value = true;
8426 Stub_table<size, big_endian>* stub_table = NULL;
8427 if (target->stub_tables().size() == 1)
8428 stub_table = target->stub_tables()[0];
8429 if (stub_table == NULL
8432 && !parameters->options().output_is_position_independent()
8433 && !is_branch_reloc(r_type)))
8434 stub_table = object->stub_table(relinfo->data_shndx);
8435 if (stub_table == NULL)
8437 // This is a ref from a data section to an ifunc symbol,
8438 // or a non-branch reloc for which we always want to use
8439 // one set of stubs for resolving function addresses.
8440 if (target->stub_tables().size() != 0)
8441 stub_table = target->stub_tables()[0];
8443 if (stub_table != NULL)
8445 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
8447 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
8448 rela.get_r_addend());
8450 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
8451 rela.get_r_addend());
8454 value = stub_table->stub_address() + ent->off_;
8455 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
8456 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
8457 size_t reloc_count = shdr.get_sh_size() / reloc_size;
8460 && relnum + 1 < reloc_count)
8462 Reltype next_rela(preloc + reloc_size);
8463 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
8464 == elfcpp::R_PPC64_TOCSAVE
8465 && next_rela.get_r_offset() == rela.get_r_offset() + 4)
8468 localentry0 = ent->localentry0_;
8469 has_stub_value = true;
8473 // We don't care too much about bogus debug references to
8474 // non-local functions, but otherwise there had better be a plt
8475 // call stub or global entry stub as appropriate.
8476 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
8479 if (r_type == elfcpp::R_POWERPC_GOT16
8480 || r_type == elfcpp::R_POWERPC_GOT16_LO
8481 || r_type == elfcpp::R_POWERPC_GOT16_HI
8482 || r_type == elfcpp::R_POWERPC_GOT16_HA
8483 || r_type == elfcpp::R_PPC64_GOT16_DS
8484 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
8488 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
8489 value = gsym->got_offset(GOT_TYPE_STANDARD);
8493 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
8494 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
8496 value -= target->got_section()->got_base_offset(object);
8498 else if (r_type == elfcpp::R_PPC64_TOC)
8500 value = (target->got_section()->output_section()->address()
8501 + object->toc_base_offset());
8503 else if (gsym != NULL
8504 && (r_type == elfcpp::R_POWERPC_REL24
8505 || r_type == elfcpp::R_PPC_PLTREL24)
8510 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
8511 Valtype* wv = reinterpret_cast<Valtype*>(view);
8512 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
8513 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
8515 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
8516 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
8519 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
8521 elfcpp::Swap<32, big_endian>::
8522 writeval(wv + 1, ld_2_1 + target->stk_toc());
8523 can_plt_call = true;
8528 // If we don't have a branch and link followed by a nop,
8529 // we can't go via the plt because there is no place to
8530 // put a toc restoring instruction.
8531 // Unless we know we won't be returning.
8532 if (strcmp(gsym->name(), "__libc_start_main") == 0)
8533 can_plt_call = true;
8537 // g++ as of 20130507 emits self-calls without a
8538 // following nop. This is arguably wrong since we have
8539 // conflicting information. On the one hand a global
8540 // symbol and on the other a local call sequence, but
8541 // don't error for this special case.
8542 // It isn't possible to cheaply verify we have exactly
8543 // such a call. Allow all calls to the same section.
8545 Address code = value;
8546 if (gsym->source() == Symbol::FROM_OBJECT
8547 && gsym->object() == object)
8549 unsigned int dest_shndx = 0;
8550 if (target->abiversion() < 2)
8552 Address addend = rela.get_r_addend();
8553 code = psymval->value(object, addend);
8554 target->symval_for_branch(relinfo->symtab, gsym, object,
8555 &code, &dest_shndx);
8558 if (dest_shndx == 0)
8559 dest_shndx = gsym->shndx(&is_ordinary);
8560 ok = dest_shndx == relinfo->data_shndx;
8564 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8565 _("call lacks nop, can't restore toc; "
8566 "recompile with -fPIC"));
8572 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8573 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
8574 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
8575 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
8577 // First instruction of a global dynamic sequence, arg setup insn.
8578 const bool final = gsym == NULL || gsym->final_value_is_known();
8579 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8580 enum Got_type got_type = GOT_TYPE_STANDARD;
8581 if (tls_type == tls::TLSOPT_NONE)
8582 got_type = GOT_TYPE_TLSGD;
8583 else if (tls_type == tls::TLSOPT_TO_IE)
8584 got_type = GOT_TYPE_TPREL;
8585 if (got_type != GOT_TYPE_STANDARD)
8589 gold_assert(gsym->has_got_offset(got_type));
8590 value = gsym->got_offset(got_type);
8594 gold_assert(object->local_has_got_offset(r_sym, got_type));
8595 value = object->local_got_offset(r_sym, got_type);
8597 value -= target->got_section()->got_base_offset(object);
8599 if (tls_type == tls::TLSOPT_TO_IE)
8601 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8602 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8604 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8605 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8606 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
8608 insn |= 32 << 26; // lwz
8610 insn |= 58 << 26; // ld
8611 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8613 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
8614 - elfcpp::R_POWERPC_GOT_TLSGD16);
8616 else if (tls_type == tls::TLSOPT_TO_LE)
8618 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8619 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8621 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8622 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8623 insn &= (1 << 26) - (1 << 21); // extract rt
8628 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8629 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8630 value = psymval->value(object, rela.get_r_addend());
8634 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8636 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8637 r_type = elfcpp::R_POWERPC_NONE;
8641 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8642 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
8643 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
8644 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
8646 // First instruction of a local dynamic sequence, arg setup insn.
8647 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8648 if (tls_type == tls::TLSOPT_NONE)
8650 value = target->tlsld_got_offset();
8651 value -= target->got_section()->got_base_offset(object);
8655 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8656 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8657 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
8659 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8660 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8661 insn &= (1 << 26) - (1 << 21); // extract rt
8666 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8667 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8672 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8674 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8675 r_type = elfcpp::R_POWERPC_NONE;
8679 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
8680 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
8681 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
8682 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
8684 // Accesses relative to a local dynamic sequence address,
8685 // no optimisation here.
8688 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
8689 value = gsym->got_offset(GOT_TYPE_DTPREL);
8693 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
8694 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
8696 value -= target->got_section()->got_base_offset(object);
8698 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8699 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
8700 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
8701 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
8703 // First instruction of initial exec sequence.
8704 const bool final = gsym == NULL || gsym->final_value_is_known();
8705 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8706 if (tls_type == tls::TLSOPT_NONE)
8710 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
8711 value = gsym->got_offset(GOT_TYPE_TPREL);
8715 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
8716 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
8718 value -= target->got_section()->got_base_offset(object);
8722 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8723 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8724 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
8726 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8727 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8728 insn &= (1 << 26) - (1 << 21); // extract rt from ld
8733 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8734 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8735 value = psymval->value(object, rela.get_r_addend());
8739 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8741 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8742 r_type = elfcpp::R_POWERPC_NONE;
8746 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8747 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8749 // Second instruction of a global dynamic sequence,
8750 // the __tls_get_addr call
8751 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
8752 const bool final = gsym == NULL || gsym->final_value_is_known();
8753 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8754 if (tls_type != tls::TLSOPT_NONE)
8756 if (tls_type == tls::TLSOPT_TO_IE)
8758 Insn* iview = reinterpret_cast<Insn*>(view);
8759 Insn insn = add_3_3_13;
8762 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8763 r_type = elfcpp::R_POWERPC_NONE;
8767 Insn* iview = reinterpret_cast<Insn*>(view);
8768 Insn insn = addi_3_3;
8769 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8770 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8772 value = psymval->value(object, rela.get_r_addend());
8774 this->skip_next_tls_get_addr_call();
8777 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8778 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8780 // Second instruction of a local dynamic sequence,
8781 // the __tls_get_addr call
8782 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
8783 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8784 if (tls_type == tls::TLSOPT_TO_LE)
8786 Insn* iview = reinterpret_cast<Insn*>(view);
8787 Insn insn = addi_3_3;
8788 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8789 this->skip_next_tls_get_addr_call();
8790 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8795 else if (r_type == elfcpp::R_POWERPC_TLS)
8797 // Second instruction of an initial exec sequence
8798 const bool final = gsym == NULL || gsym->final_value_is_known();
8799 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8800 if (tls_type == tls::TLSOPT_TO_LE)
8802 Insn* iview = reinterpret_cast<Insn*>(view);
8803 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8804 unsigned int reg = size == 32 ? 2 : 13;
8805 insn = at_tls_transform(insn, reg);
8806 gold_assert(insn != 0);
8807 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8808 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8810 value = psymval->value(object, rela.get_r_addend());
8813 else if (!has_stub_value)
8816 if (!(size == 32 && r_type == elfcpp::R_PPC_PLTREL24))
8817 addend = rela.get_r_addend();
8818 value = psymval->value(object, addend);
8819 if (size == 64 && is_branch_reloc(r_type))
8821 if (target->abiversion() >= 2)
8824 value += object->ppc64_local_entry_offset(gsym);
8826 value += object->ppc64_local_entry_offset(r_sym);
8830 unsigned int dest_shndx;
8831 target->symval_for_branch(relinfo->symtab, gsym, object,
8832 &value, &dest_shndx);
8835 Address max_branch_offset = max_branch_delta(r_type);
8836 if (max_branch_offset != 0
8837 && value - address + max_branch_offset >= 2 * max_branch_offset)
8839 Stub_table<size, big_endian>* stub_table
8840 = object->stub_table(relinfo->data_shndx);
8841 if (stub_table != NULL)
8843 Address off = stub_table->find_long_branch_entry(object, value);
8844 if (off != invalid_address)
8846 value = (stub_table->stub_address() + stub_table->plt_size()
8848 has_stub_value = true;
8856 case elfcpp::R_PPC64_REL64:
8857 case elfcpp::R_POWERPC_REL32:
8858 case elfcpp::R_POWERPC_REL24:
8859 case elfcpp::R_PPC_PLTREL24:
8860 case elfcpp::R_PPC_LOCAL24PC:
8861 case elfcpp::R_POWERPC_REL16:
8862 case elfcpp::R_POWERPC_REL16_LO:
8863 case elfcpp::R_POWERPC_REL16_HI:
8864 case elfcpp::R_POWERPC_REL16_HA:
8865 case elfcpp::R_POWERPC_REL16DX_HA:
8866 case elfcpp::R_POWERPC_REL14:
8867 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8868 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8872 case elfcpp::R_PPC64_TOC16:
8873 case elfcpp::R_PPC64_TOC16_LO:
8874 case elfcpp::R_PPC64_TOC16_HI:
8875 case elfcpp::R_PPC64_TOC16_HA:
8876 case elfcpp::R_PPC64_TOC16_DS:
8877 case elfcpp::R_PPC64_TOC16_LO_DS:
8878 // Subtract the TOC base address.
8879 value -= (target->got_section()->output_section()->address()
8880 + object->toc_base_offset());
8883 case elfcpp::R_POWERPC_SECTOFF:
8884 case elfcpp::R_POWERPC_SECTOFF_LO:
8885 case elfcpp::R_POWERPC_SECTOFF_HI:
8886 case elfcpp::R_POWERPC_SECTOFF_HA:
8887 case elfcpp::R_PPC64_SECTOFF_DS:
8888 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8890 value -= os->address();
8893 case elfcpp::R_PPC64_TPREL16_DS:
8894 case elfcpp::R_PPC64_TPREL16_LO_DS:
8895 case elfcpp::R_PPC64_TPREL16_HIGH:
8896 case elfcpp::R_PPC64_TPREL16_HIGHA:
8898 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
8901 case elfcpp::R_POWERPC_TPREL16:
8902 case elfcpp::R_POWERPC_TPREL16_LO:
8903 case elfcpp::R_POWERPC_TPREL16_HI:
8904 case elfcpp::R_POWERPC_TPREL16_HA:
8905 case elfcpp::R_POWERPC_TPREL:
8906 case elfcpp::R_PPC64_TPREL16_HIGHER:
8907 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8908 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8909 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8910 // tls symbol values are relative to tls_segment()->vaddr()
8914 case elfcpp::R_PPC64_DTPREL16_DS:
8915 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8916 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8917 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8918 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8919 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8921 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
8922 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
8925 case elfcpp::R_POWERPC_DTPREL16:
8926 case elfcpp::R_POWERPC_DTPREL16_LO:
8927 case elfcpp::R_POWERPC_DTPREL16_HI:
8928 case elfcpp::R_POWERPC_DTPREL16_HA:
8929 case elfcpp::R_POWERPC_DTPREL:
8930 case elfcpp::R_PPC64_DTPREL16_HIGH:
8931 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8932 // tls symbol values are relative to tls_segment()->vaddr()
8933 value -= dtp_offset;
8936 case elfcpp::R_PPC64_ADDR64_LOCAL:
8938 value += object->ppc64_local_entry_offset(gsym);
8940 value += object->ppc64_local_entry_offset(r_sym);
8947 Insn branch_bit = 0;
8950 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8951 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8952 branch_bit = 1 << 21;
8954 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8955 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8957 Insn* iview = reinterpret_cast<Insn*>(view);
8958 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8961 if (this->is_isa_v2)
8963 // Set 'a' bit. This is 0b00010 in BO field for branch
8964 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
8965 // for branch on CTR insns (BO == 1a00t or 1a01t).
8966 if ((insn & (0x14 << 21)) == (0x04 << 21))
8968 else if ((insn & (0x14 << 21)) == (0x10 << 21))
8975 // Invert 'y' bit if not the default.
8976 if (static_cast<Signed_address>(value) < 0)
8979 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8994 // Multi-instruction sequences that access the GOT/TOC can
8995 // be optimized, eg.
8996 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
8997 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
8999 // addis ra,r2,0; addi rb,ra,x@toc@l;
9000 // to nop; addi rb,r2,x@toc;
9001 // FIXME: the @got sequence shown above is not yet
9002 // optimized. Note that gcc as of 2017-01-07 doesn't use
9003 // the ELF @got relocs except for TLS, instead using the
9004 // PowerOpen variant of a compiler managed GOT (called TOC).
9005 // The PowerOpen TOC sequence equivalent to the first
9006 // example is optimized.
9007 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9008 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9009 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9010 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9011 case elfcpp::R_POWERPC_GOT16_HA:
9012 case elfcpp::R_PPC64_TOC16_HA:
9013 if (parameters->options().toc_optimize())
9015 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9016 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9017 if (r_type == elfcpp::R_PPC64_TOC16_HA
9018 && object->make_toc_relative(target, &value))
9020 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
9021 == ((15u << 26) | (2 << 16)));
9023 if (((insn & ((0x3f << 26) | 0x1f << 16))
9024 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
9025 && value + 0x8000 < 0x10000)
9027 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
9033 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9034 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9035 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9036 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9037 case elfcpp::R_POWERPC_GOT16_LO:
9038 case elfcpp::R_PPC64_GOT16_LO_DS:
9039 case elfcpp::R_PPC64_TOC16_LO:
9040 case elfcpp::R_PPC64_TOC16_LO_DS:
9041 if (parameters->options().toc_optimize())
9043 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9044 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9045 bool changed = false;
9046 if (r_type == elfcpp::R_PPC64_TOC16_LO_DS
9047 && object->make_toc_relative(target, &value))
9049 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
9050 insn ^= (14u << 26) ^ (58u << 26);
9051 r_type = elfcpp::R_PPC64_TOC16_LO;
9054 if (ok_lo_toc_insn(insn, r_type)
9055 && value + 0x8000 < 0x10000)
9057 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
9059 // Transform addic to addi when we change reg.
9060 insn &= ~((0x3f << 26) | (0x1f << 16));
9061 insn |= (14u << 26) | (2 << 16);
9065 insn &= ~(0x1f << 16);
9071 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9075 case elfcpp::R_POWERPC_TPREL16_HA:
9076 if (parameters->options().tls_optimize() && value + 0x8000 < 0x10000)
9078 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9079 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9080 if ((insn & ((0x3f << 26) | 0x1f << 16))
9081 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
9085 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
9091 case elfcpp::R_PPC64_TPREL16_LO_DS:
9093 // R_PPC_TLSGD, R_PPC_TLSLD
9096 case elfcpp::R_POWERPC_TPREL16_LO:
9097 if (parameters->options().tls_optimize() && value + 0x8000 < 0x10000)
9099 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9100 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9101 insn &= ~(0x1f << 16);
9102 insn |= (size == 32 ? 2 : 13) << 16;
9103 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9107 case elfcpp::R_PPC64_ENTRY:
9108 value = (target->got_section()->output_section()->address()
9109 + object->toc_base_offset());
9110 if (value + 0x80008000 <= 0xffffffff
9111 && !parameters->options().output_is_position_independent())
9113 Insn* iview = reinterpret_cast<Insn*>(view);
9114 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
9115 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
9117 if ((insn1 & ~0xfffc) == ld_2_12
9118 && insn2 == add_2_2_12)
9120 insn1 = lis_2 + ha(value);
9121 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
9122 insn2 = addi_2_2 + l(value);
9123 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
9130 if (value + 0x80008000 <= 0xffffffff)
9132 Insn* iview = reinterpret_cast<Insn*>(view);
9133 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
9134 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
9136 if ((insn1 & ~0xfffc) == ld_2_12
9137 && insn2 == add_2_2_12)
9139 insn1 = addis_2_12 + ha(value);
9140 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
9141 insn2 = addi_2_2 + l(value);
9142 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
9149 case elfcpp::R_POWERPC_REL16_LO:
9150 // If we are generating a non-PIC executable, edit
9151 // 0: addis 2,12,.TOC.-0b@ha
9152 // addi 2,2,.TOC.-0b@l
9153 // used by ELFv2 global entry points to set up r2, to
9156 // if .TOC. is in range. */
9157 if (value + address - 4 + 0x80008000 <= 0xffffffff
9160 && target->abiversion() >= 2
9161 && !parameters->options().output_is_position_independent()
9162 && rela.get_r_addend() == d_offset + 4
9164 && strcmp(gsym->name(), ".TOC.") == 0)
9166 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
9167 Reltype prev_rela(preloc - reloc_size);
9168 if ((prev_rela.get_r_info()
9169 == elfcpp::elf_r_info<size>(r_sym,
9170 elfcpp::R_POWERPC_REL16_HA))
9171 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
9172 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
9174 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9175 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
9176 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
9178 if ((insn1 & 0xffff0000) == addis_2_12
9179 && (insn2 & 0xffff0000) == addi_2_2)
9181 insn1 = lis_2 + ha(value + address - 4);
9182 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
9183 insn2 = addi_2_2 + l(value + address - 4);
9184 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
9187 relinfo->rr->set_strategy(relnum - 1,
9188 Relocatable_relocs::RELOC_SPECIAL);
9189 relinfo->rr->set_strategy(relnum,
9190 Relocatable_relocs::RELOC_SPECIAL);
9200 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
9201 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
9204 case elfcpp::R_POWERPC_ADDR32:
9205 case elfcpp::R_POWERPC_UADDR32:
9207 overflow = Reloc::CHECK_BITFIELD;
9210 case elfcpp::R_POWERPC_REL32:
9211 case elfcpp::R_POWERPC_REL16DX_HA:
9213 overflow = Reloc::CHECK_SIGNED;
9216 case elfcpp::R_POWERPC_UADDR16:
9217 overflow = Reloc::CHECK_BITFIELD;
9220 case elfcpp::R_POWERPC_ADDR16:
9221 // We really should have three separate relocations,
9222 // one for 16-bit data, one for insns with 16-bit signed fields,
9223 // and one for insns with 16-bit unsigned fields.
9224 overflow = Reloc::CHECK_BITFIELD;
9225 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
9226 overflow = Reloc::CHECK_LOW_INSN;
9229 case elfcpp::R_POWERPC_ADDR16_HI:
9230 case elfcpp::R_POWERPC_ADDR16_HA:
9231 case elfcpp::R_POWERPC_GOT16_HI:
9232 case elfcpp::R_POWERPC_GOT16_HA:
9233 case elfcpp::R_POWERPC_PLT16_HI:
9234 case elfcpp::R_POWERPC_PLT16_HA:
9235 case elfcpp::R_POWERPC_SECTOFF_HI:
9236 case elfcpp::R_POWERPC_SECTOFF_HA:
9237 case elfcpp::R_PPC64_TOC16_HI:
9238 case elfcpp::R_PPC64_TOC16_HA:
9239 case elfcpp::R_PPC64_PLTGOT16_HI:
9240 case elfcpp::R_PPC64_PLTGOT16_HA:
9241 case elfcpp::R_POWERPC_TPREL16_HI:
9242 case elfcpp::R_POWERPC_TPREL16_HA:
9243 case elfcpp::R_POWERPC_DTPREL16_HI:
9244 case elfcpp::R_POWERPC_DTPREL16_HA:
9245 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
9246 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9247 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9248 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9249 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9250 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9251 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9252 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9253 case elfcpp::R_POWERPC_REL16_HI:
9254 case elfcpp::R_POWERPC_REL16_HA:
9256 overflow = Reloc::CHECK_HIGH_INSN;
9259 case elfcpp::R_POWERPC_REL16:
9260 case elfcpp::R_PPC64_TOC16:
9261 case elfcpp::R_POWERPC_GOT16:
9262 case elfcpp::R_POWERPC_SECTOFF:
9263 case elfcpp::R_POWERPC_TPREL16:
9264 case elfcpp::R_POWERPC_DTPREL16:
9265 case elfcpp::R_POWERPC_GOT_TLSGD16:
9266 case elfcpp::R_POWERPC_GOT_TLSLD16:
9267 case elfcpp::R_POWERPC_GOT_TPREL16:
9268 case elfcpp::R_POWERPC_GOT_DTPREL16:
9269 overflow = Reloc::CHECK_LOW_INSN;
9272 case elfcpp::R_POWERPC_ADDR24:
9273 case elfcpp::R_POWERPC_ADDR14:
9274 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
9275 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
9276 case elfcpp::R_PPC64_ADDR16_DS:
9277 case elfcpp::R_POWERPC_REL24:
9278 case elfcpp::R_PPC_PLTREL24:
9279 case elfcpp::R_PPC_LOCAL24PC:
9280 case elfcpp::R_PPC64_TPREL16_DS:
9281 case elfcpp::R_PPC64_DTPREL16_DS:
9282 case elfcpp::R_PPC64_TOC16_DS:
9283 case elfcpp::R_PPC64_GOT16_DS:
9284 case elfcpp::R_PPC64_SECTOFF_DS:
9285 case elfcpp::R_POWERPC_REL14:
9286 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9287 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
9288 overflow = Reloc::CHECK_SIGNED;
9292 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9295 if (overflow == Reloc::CHECK_LOW_INSN
9296 || overflow == Reloc::CHECK_HIGH_INSN)
9298 insn = elfcpp::Swap<32, big_endian>::readval(iview);
9300 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
9301 overflow = Reloc::CHECK_BITFIELD;
9302 else if (overflow == Reloc::CHECK_LOW_INSN
9303 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
9304 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
9305 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
9306 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
9307 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
9308 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
9309 overflow = Reloc::CHECK_UNSIGNED;
9311 overflow = Reloc::CHECK_SIGNED;
9314 bool maybe_dq_reloc = false;
9315 typename Powerpc_relocate_functions<size, big_endian>::Status status
9316 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
9319 case elfcpp::R_POWERPC_NONE:
9320 case elfcpp::R_POWERPC_TLS:
9321 case elfcpp::R_POWERPC_GNU_VTINHERIT:
9322 case elfcpp::R_POWERPC_GNU_VTENTRY:
9325 case elfcpp::R_PPC64_ADDR64:
9326 case elfcpp::R_PPC64_REL64:
9327 case elfcpp::R_PPC64_TOC:
9328 case elfcpp::R_PPC64_ADDR64_LOCAL:
9329 Reloc::addr64(view, value);
9332 case elfcpp::R_POWERPC_TPREL:
9333 case elfcpp::R_POWERPC_DTPREL:
9335 Reloc::addr64(view, value);
9337 status = Reloc::addr32(view, value, overflow);
9340 case elfcpp::R_PPC64_UADDR64:
9341 Reloc::addr64_u(view, value);
9344 case elfcpp::R_POWERPC_ADDR32:
9345 status = Reloc::addr32(view, value, overflow);
9348 case elfcpp::R_POWERPC_REL32:
9349 case elfcpp::R_POWERPC_UADDR32:
9350 status = Reloc::addr32_u(view, value, overflow);
9353 case elfcpp::R_POWERPC_ADDR24:
9354 case elfcpp::R_POWERPC_REL24:
9355 case elfcpp::R_PPC_PLTREL24:
9356 case elfcpp::R_PPC_LOCAL24PC:
9357 status = Reloc::addr24(view, value, overflow);
9360 case elfcpp::R_POWERPC_GOT_DTPREL16:
9361 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9362 case elfcpp::R_POWERPC_GOT_TPREL16:
9363 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9366 // On ppc64 these are all ds form
9367 maybe_dq_reloc = true;
9371 case elfcpp::R_POWERPC_ADDR16:
9372 case elfcpp::R_POWERPC_REL16:
9373 case elfcpp::R_PPC64_TOC16:
9374 case elfcpp::R_POWERPC_GOT16:
9375 case elfcpp::R_POWERPC_SECTOFF:
9376 case elfcpp::R_POWERPC_TPREL16:
9377 case elfcpp::R_POWERPC_DTPREL16:
9378 case elfcpp::R_POWERPC_GOT_TLSGD16:
9379 case elfcpp::R_POWERPC_GOT_TLSLD16:
9380 case elfcpp::R_POWERPC_ADDR16_LO:
9381 case elfcpp::R_POWERPC_REL16_LO:
9382 case elfcpp::R_PPC64_TOC16_LO:
9383 case elfcpp::R_POWERPC_GOT16_LO:
9384 case elfcpp::R_POWERPC_SECTOFF_LO:
9385 case elfcpp::R_POWERPC_TPREL16_LO:
9386 case elfcpp::R_POWERPC_DTPREL16_LO:
9387 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9388 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9390 status = Reloc::addr16(view, value, overflow);
9392 maybe_dq_reloc = true;
9395 case elfcpp::R_POWERPC_UADDR16:
9396 status = Reloc::addr16_u(view, value, overflow);
9399 case elfcpp::R_PPC64_ADDR16_HIGH:
9400 case elfcpp::R_PPC64_TPREL16_HIGH:
9401 case elfcpp::R_PPC64_DTPREL16_HIGH:
9403 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
9406 case elfcpp::R_POWERPC_ADDR16_HI:
9407 case elfcpp::R_POWERPC_REL16_HI:
9408 case elfcpp::R_PPC64_TOC16_HI:
9409 case elfcpp::R_POWERPC_GOT16_HI:
9410 case elfcpp::R_POWERPC_SECTOFF_HI:
9411 case elfcpp::R_POWERPC_TPREL16_HI:
9412 case elfcpp::R_POWERPC_DTPREL16_HI:
9413 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
9414 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9415 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9416 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9417 Reloc::addr16_hi(view, value);
9420 case elfcpp::R_PPC64_ADDR16_HIGHA:
9421 case elfcpp::R_PPC64_TPREL16_HIGHA:
9422 case elfcpp::R_PPC64_DTPREL16_HIGHA:
9424 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
9427 case elfcpp::R_POWERPC_ADDR16_HA:
9428 case elfcpp::R_POWERPC_REL16_HA:
9429 case elfcpp::R_PPC64_TOC16_HA:
9430 case elfcpp::R_POWERPC_GOT16_HA:
9431 case elfcpp::R_POWERPC_SECTOFF_HA:
9432 case elfcpp::R_POWERPC_TPREL16_HA:
9433 case elfcpp::R_POWERPC_DTPREL16_HA:
9434 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9435 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9436 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9437 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9438 Reloc::addr16_ha(view, value);
9441 case elfcpp::R_POWERPC_REL16DX_HA:
9442 status = Reloc::addr16dx_ha(view, value, overflow);
9445 case elfcpp::R_PPC64_DTPREL16_HIGHER:
9447 // R_PPC_EMB_NADDR16_LO
9450 case elfcpp::R_PPC64_ADDR16_HIGHER:
9451 case elfcpp::R_PPC64_TPREL16_HIGHER:
9452 Reloc::addr16_hi2(view, value);
9455 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
9457 // R_PPC_EMB_NADDR16_HI
9460 case elfcpp::R_PPC64_ADDR16_HIGHERA:
9461 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9462 Reloc::addr16_ha2(view, value);
9465 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
9467 // R_PPC_EMB_NADDR16_HA
9470 case elfcpp::R_PPC64_ADDR16_HIGHEST:
9471 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9472 Reloc::addr16_hi3(view, value);
9475 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
9480 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
9481 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9482 Reloc::addr16_ha3(view, value);
9485 case elfcpp::R_PPC64_DTPREL16_DS:
9486 case elfcpp::R_PPC64_DTPREL16_LO_DS:
9488 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
9491 case elfcpp::R_PPC64_TPREL16_DS:
9492 case elfcpp::R_PPC64_TPREL16_LO_DS:
9494 // R_PPC_TLSGD, R_PPC_TLSLD
9497 case elfcpp::R_PPC64_ADDR16_DS:
9498 case elfcpp::R_PPC64_ADDR16_LO_DS:
9499 case elfcpp::R_PPC64_TOC16_DS:
9500 case elfcpp::R_PPC64_TOC16_LO_DS:
9501 case elfcpp::R_PPC64_GOT16_DS:
9502 case elfcpp::R_PPC64_GOT16_LO_DS:
9503 case elfcpp::R_PPC64_SECTOFF_DS:
9504 case elfcpp::R_PPC64_SECTOFF_LO_DS:
9505 maybe_dq_reloc = true;
9508 case elfcpp::R_POWERPC_ADDR14:
9509 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
9510 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
9511 case elfcpp::R_POWERPC_REL14:
9512 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9513 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
9514 status = Reloc::addr14(view, value, overflow);
9517 case elfcpp::R_POWERPC_COPY:
9518 case elfcpp::R_POWERPC_GLOB_DAT:
9519 case elfcpp::R_POWERPC_JMP_SLOT:
9520 case elfcpp::R_POWERPC_RELATIVE:
9521 case elfcpp::R_POWERPC_DTPMOD:
9522 case elfcpp::R_PPC64_JMP_IREL:
9523 case elfcpp::R_POWERPC_IRELATIVE:
9524 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9525 _("unexpected reloc %u in object file"),
9529 case elfcpp::R_PPC64_TOCSAVE:
9535 Symbol_location loc;
9536 loc.object = relinfo->object;
9537 loc.shndx = relinfo->data_shndx;
9538 loc.offset = rela.get_r_offset();
9539 Tocsave_loc::const_iterator p = target->tocsave_loc().find(loc);
9540 if (p != target->tocsave_loc().end())
9542 // If we've generated plt calls using this tocsave, then
9543 // the nop needs to be changed to save r2.
9544 Insn* iview = reinterpret_cast<Insn*>(view);
9545 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
9546 elfcpp::Swap<32, big_endian>::
9547 writeval(iview, std_2_1 + target->stk_toc());
9552 case elfcpp::R_PPC_EMB_SDA2I16:
9553 case elfcpp::R_PPC_EMB_SDA2REL:
9556 // R_PPC64_TLSGD, R_PPC64_TLSLD
9559 case elfcpp::R_POWERPC_PLT32:
9560 case elfcpp::R_POWERPC_PLTREL32:
9561 case elfcpp::R_POWERPC_PLT16_LO:
9562 case elfcpp::R_POWERPC_PLT16_HI:
9563 case elfcpp::R_POWERPC_PLT16_HA:
9564 case elfcpp::R_PPC_SDAREL16:
9565 case elfcpp::R_POWERPC_ADDR30:
9566 case elfcpp::R_PPC64_PLT64:
9567 case elfcpp::R_PPC64_PLTREL64:
9568 case elfcpp::R_PPC64_PLTGOT16:
9569 case elfcpp::R_PPC64_PLTGOT16_LO:
9570 case elfcpp::R_PPC64_PLTGOT16_HI:
9571 case elfcpp::R_PPC64_PLTGOT16_HA:
9572 case elfcpp::R_PPC64_PLT16_LO_DS:
9573 case elfcpp::R_PPC64_PLTGOT16_DS:
9574 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
9575 case elfcpp::R_PPC_EMB_RELSDA:
9576 case elfcpp::R_PPC_TOC16:
9579 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9580 _("unsupported reloc %u"),
9588 insn = elfcpp::Swap<32, big_endian>::readval(iview);
9590 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
9591 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
9592 && (insn & 3) == 1))
9593 status = Reloc::addr16_dq(view, value, overflow);
9595 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
9596 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
9597 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
9598 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
9599 status = Reloc::addr16_ds(view, value, overflow);
9601 status = Reloc::addr16(view, value, overflow);
9604 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
9607 && gsym->is_undefined()
9608 && is_branch_reloc(r_type))))
9610 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9611 _("relocation overflow"));
9613 gold_info(_("try relinking with a smaller --stub-group-size"));
9619 // Relocate section data.
9621 template<int size, bool big_endian>
9623 Target_powerpc<size, big_endian>::relocate_section(
9624 const Relocate_info<size, big_endian>* relinfo,
9625 unsigned int sh_type,
9626 const unsigned char* prelocs,
9628 Output_section* output_section,
9629 bool needs_special_offset_handling,
9630 unsigned char* view,
9632 section_size_type view_size,
9633 const Reloc_symbol_changes* reloc_symbol_changes)
9635 typedef Target_powerpc<size, big_endian> Powerpc;
9636 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
9637 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
9638 Powerpc_comdat_behavior;
9639 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9642 gold_assert(sh_type == elfcpp::SHT_RELA);
9644 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
9645 Powerpc_comdat_behavior, Classify_reloc>(
9651 needs_special_offset_handling,
9655 reloc_symbol_changes);
9658 template<int size, bool big_endian>
9659 class Powerpc_scan_relocatable_reloc
9662 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9663 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
9664 static const int sh_type = elfcpp::SHT_RELA;
9666 // Return the symbol referred to by the relocation.
9667 static inline unsigned int
9668 get_r_sym(const Reltype* reloc)
9669 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
9671 // Return the type of the relocation.
9672 static inline unsigned int
9673 get_r_type(const Reltype* reloc)
9674 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
9676 // Return the strategy to use for a local symbol which is not a
9677 // section symbol, given the relocation type.
9678 inline Relocatable_relocs::Reloc_strategy
9679 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
9681 if (r_type == 0 && r_sym == 0)
9682 return Relocatable_relocs::RELOC_DISCARD;
9683 return Relocatable_relocs::RELOC_COPY;
9686 // Return the strategy to use for a local symbol which is a section
9687 // symbol, given the relocation type.
9688 inline Relocatable_relocs::Reloc_strategy
9689 local_section_strategy(unsigned int, Relobj*)
9691 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
9694 // Return the strategy to use for a global symbol, given the
9695 // relocation type, the object, and the symbol index.
9696 inline Relocatable_relocs::Reloc_strategy
9697 global_strategy(unsigned int r_type, Relobj*, unsigned int)
9699 if (r_type == elfcpp::R_PPC_PLTREL24)
9700 return Relocatable_relocs::RELOC_SPECIAL;
9701 return Relocatable_relocs::RELOC_COPY;
9705 // Scan the relocs during a relocatable link.
9707 template<int size, bool big_endian>
9709 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
9710 Symbol_table* symtab,
9712 Sized_relobj_file<size, big_endian>* object,
9713 unsigned int data_shndx,
9714 unsigned int sh_type,
9715 const unsigned char* prelocs,
9717 Output_section* output_section,
9718 bool needs_special_offset_handling,
9719 size_t local_symbol_count,
9720 const unsigned char* plocal_symbols,
9721 Relocatable_relocs* rr)
9723 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
9725 gold_assert(sh_type == elfcpp::SHT_RELA);
9727 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
9735 needs_special_offset_handling,
9741 // Scan the relocs for --emit-relocs.
9743 template<int size, bool big_endian>
9745 Target_powerpc<size, big_endian>::emit_relocs_scan(
9746 Symbol_table* symtab,
9748 Sized_relobj_file<size, big_endian>* object,
9749 unsigned int data_shndx,
9750 unsigned int sh_type,
9751 const unsigned char* prelocs,
9753 Output_section* output_section,
9754 bool needs_special_offset_handling,
9755 size_t local_symbol_count,
9756 const unsigned char* plocal_syms,
9757 Relocatable_relocs* rr)
9759 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9761 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
9762 Emit_relocs_strategy;
9764 gold_assert(sh_type == elfcpp::SHT_RELA);
9766 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
9774 needs_special_offset_handling,
9780 // Emit relocations for a section.
9781 // This is a modified version of the function by the same name in
9782 // target-reloc.h. Using relocate_special_relocatable for
9783 // R_PPC_PLTREL24 would require duplication of the entire body of the
9784 // loop, so we may as well duplicate the whole thing.
9786 template<int size, bool big_endian>
9788 Target_powerpc<size, big_endian>::relocate_relocs(
9789 const Relocate_info<size, big_endian>* relinfo,
9790 unsigned int sh_type,
9791 const unsigned char* prelocs,
9793 Output_section* output_section,
9794 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
9796 Address view_address,
9798 unsigned char* reloc_view,
9799 section_size_type reloc_view_size)
9801 gold_assert(sh_type == elfcpp::SHT_RELA);
9803 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9804 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
9805 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
9806 // Offset from start of insn to d-field reloc.
9807 const int d_offset = big_endian ? 2 : 0;
9809 Powerpc_relobj<size, big_endian>* const object
9810 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
9811 const unsigned int local_count = object->local_symbol_count();
9812 unsigned int got2_shndx = object->got2_shndx();
9813 Address got2_addend = 0;
9814 if (got2_shndx != 0)
9816 got2_addend = object->get_output_section_offset(got2_shndx);
9817 gold_assert(got2_addend != invalid_address);
9820 const bool relocatable = parameters->options().relocatable();
9822 unsigned char* pwrite = reloc_view;
9823 bool zap_next = false;
9824 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
9826 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
9827 if (strategy == Relocatable_relocs::RELOC_DISCARD)
9830 Reltype reloc(prelocs);
9831 Reltype_write reloc_write(pwrite);
9833 Address offset = reloc.get_r_offset();
9834 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
9835 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
9836 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
9837 const unsigned int orig_r_sym = r_sym;
9838 typename elfcpp::Elf_types<size>::Elf_Swxword addend
9839 = reloc.get_r_addend();
9840 const Symbol* gsym = NULL;
9844 // We could arrange to discard these and other relocs for
9845 // tls optimised sequences in the strategy methods, but for
9846 // now do as BFD ld does.
9847 r_type = elfcpp::R_POWERPC_NONE;
9851 // Get the new symbol index.
9852 Output_section* os = NULL;
9853 if (r_sym < local_count)
9857 case Relocatable_relocs::RELOC_COPY:
9858 case Relocatable_relocs::RELOC_SPECIAL:
9861 r_sym = object->symtab_index(r_sym);
9862 gold_assert(r_sym != -1U);
9866 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
9868 // We are adjusting a section symbol. We need to find
9869 // the symbol table index of the section symbol for
9870 // the output section corresponding to input section
9871 // in which this symbol is defined.
9872 gold_assert(r_sym < local_count);
9874 unsigned int shndx =
9875 object->local_symbol_input_shndx(r_sym, &is_ordinary);
9876 gold_assert(is_ordinary);
9877 os = object->output_section(shndx);
9878 gold_assert(os != NULL);
9879 gold_assert(os->needs_symtab_index());
9880 r_sym = os->symtab_index();
9890 gsym = object->global_symbol(r_sym);
9891 gold_assert(gsym != NULL);
9892 if (gsym->is_forwarder())
9893 gsym = relinfo->symtab->resolve_forwards(gsym);
9895 gold_assert(gsym->has_symtab_index());
9896 r_sym = gsym->symtab_index();
9899 // Get the new offset--the location in the output section where
9900 // this relocation should be applied.
9901 if (static_cast<Address>(offset_in_output_section) != invalid_address)
9902 offset += offset_in_output_section;
9905 section_offset_type sot_offset =
9906 convert_types<section_offset_type, Address>(offset);
9907 section_offset_type new_sot_offset =
9908 output_section->output_offset(object, relinfo->data_shndx,
9910 gold_assert(new_sot_offset != -1);
9911 offset = new_sot_offset;
9914 // In an object file, r_offset is an offset within the section.
9915 // In an executable or dynamic object, generated by
9916 // --emit-relocs, r_offset is an absolute address.
9919 offset += view_address;
9920 if (static_cast<Address>(offset_in_output_section) != invalid_address)
9921 offset -= offset_in_output_section;
9924 // Handle the reloc addend based on the strategy.
9925 if (strategy == Relocatable_relocs::RELOC_COPY)
9927 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
9929 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
9930 addend = psymval->value(object, addend);
9931 // In a relocatable link, the symbol value is relative to
9932 // the start of the output section. For a non-relocatable
9933 // link, we need to adjust the addend.
9936 gold_assert(os != NULL);
9937 addend -= os->address();
9940 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
9944 if (addend >= 32768)
9945 addend += got2_addend;
9947 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
9949 r_type = elfcpp::R_POWERPC_ADDR16_HA;
9952 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
9954 r_type = elfcpp::R_POWERPC_ADDR16_LO;
9955 addend -= d_offset + 4;
9963 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9964 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
9965 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
9966 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
9968 // First instruction of a global dynamic sequence,
9970 const bool final = gsym == NULL || gsym->final_value_is_known();
9971 switch (this->optimize_tls_gd(final))
9973 case tls::TLSOPT_TO_IE:
9974 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
9975 - elfcpp::R_POWERPC_GOT_TLSGD16);
9977 case tls::TLSOPT_TO_LE:
9978 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9979 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
9980 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9983 r_type = elfcpp::R_POWERPC_NONE;
9991 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9992 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
9993 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
9994 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
9996 // First instruction of a local dynamic sequence,
9998 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
10000 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10001 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
10003 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10004 const Output_section* os = relinfo->layout->tls_segment()
10006 gold_assert(os != NULL);
10007 gold_assert(os->needs_symtab_index());
10008 r_sym = os->symtab_index();
10009 addend = dtp_offset;
10013 r_type = elfcpp::R_POWERPC_NONE;
10014 offset -= d_offset;
10018 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10019 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
10020 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
10021 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
10023 // First instruction of initial exec sequence.
10024 const bool final = gsym == NULL || gsym->final_value_is_known();
10025 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
10027 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10028 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
10029 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10032 r_type = elfcpp::R_POWERPC_NONE;
10033 offset -= d_offset;
10037 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
10038 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
10040 // Second instruction of a global dynamic sequence,
10041 // the __tls_get_addr call
10042 const bool final = gsym == NULL || gsym->final_value_is_known();
10043 switch (this->optimize_tls_gd(final))
10045 case tls::TLSOPT_TO_IE:
10046 r_type = elfcpp::R_POWERPC_NONE;
10049 case tls::TLSOPT_TO_LE:
10050 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10051 offset += d_offset;
10058 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
10059 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
10061 // Second instruction of a local dynamic sequence,
10062 // the __tls_get_addr call
10063 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
10065 const Output_section* os = relinfo->layout->tls_segment()
10067 gold_assert(os != NULL);
10068 gold_assert(os->needs_symtab_index());
10069 r_sym = os->symtab_index();
10070 addend = dtp_offset;
10071 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10072 offset += d_offset;
10076 else if (r_type == elfcpp::R_POWERPC_TLS)
10078 // Second instruction of an initial exec sequence
10079 const bool final = gsym == NULL || gsym->final_value_is_known();
10080 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
10082 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10083 offset += d_offset;
10088 reloc_write.put_r_offset(offset);
10089 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
10090 reloc_write.put_r_addend(addend);
10092 pwrite += reloc_size;
10095 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
10096 == reloc_view_size);
10099 // Return the value to use for a dynamic symbol which requires special
10100 // treatment. This is how we support equality comparisons of function
10101 // pointers across shared library boundaries, as described in the
10102 // processor specific ABI supplement.
10104 template<int size, bool big_endian>
10106 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
10110 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
10111 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
10112 p != this->stub_tables_.end();
10115 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
10116 = (*p)->find_plt_call_entry(gsym);
10118 return (*p)->stub_address() + ent->off_;
10121 else if (this->abiversion() >= 2)
10123 Address off = this->glink_section()->find_global_entry(gsym);
10124 if (off != invalid_address)
10125 return this->glink_section()->global_entry_address() + off;
10127 gold_unreachable();
10130 // Return the PLT address to use for a local symbol.
10131 template<int size, bool big_endian>
10133 Target_powerpc<size, big_endian>::do_plt_address_for_local(
10134 const Relobj* object,
10135 unsigned int symndx) const
10139 const Sized_relobj<size, big_endian>* relobj
10140 = static_cast<const Sized_relobj<size, big_endian>*>(object);
10141 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
10142 p != this->stub_tables_.end();
10145 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
10146 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
10148 return (*p)->stub_address() + ent->off_;
10151 gold_unreachable();
10154 // Return the PLT address to use for a global symbol.
10155 template<int size, bool big_endian>
10157 Target_powerpc<size, big_endian>::do_plt_address_for_global(
10158 const Symbol* gsym) const
10162 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
10163 p != this->stub_tables_.end();
10166 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
10167 = (*p)->find_plt_call_entry(gsym);
10169 return (*p)->stub_address() + ent->off_;
10172 else if (this->abiversion() >= 2)
10174 Address off = this->glink_section()->find_global_entry(gsym);
10175 if (off != invalid_address)
10176 return this->glink_section()->global_entry_address() + off;
10178 gold_unreachable();
10181 // Return the offset to use for the GOT_INDX'th got entry which is
10182 // for a local tls symbol specified by OBJECT, SYMNDX.
10183 template<int size, bool big_endian>
10185 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
10186 const Relobj* object,
10187 unsigned int symndx,
10188 unsigned int got_indx) const
10190 const Powerpc_relobj<size, big_endian>* ppc_object
10191 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
10192 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
10194 for (Got_type got_type = GOT_TYPE_TLSGD;
10195 got_type <= GOT_TYPE_TPREL;
10196 got_type = Got_type(got_type + 1))
10197 if (ppc_object->local_has_got_offset(symndx, got_type))
10199 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
10200 if (got_type == GOT_TYPE_TLSGD)
10202 if (off == got_indx * (size / 8))
10204 if (got_type == GOT_TYPE_TPREL)
10207 return -dtp_offset;
10211 gold_unreachable();
10214 // Return the offset to use for the GOT_INDX'th got entry which is
10215 // for global tls symbol GSYM.
10216 template<int size, bool big_endian>
10218 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
10220 unsigned int got_indx) const
10222 if (gsym->type() == elfcpp::STT_TLS)
10224 for (Got_type got_type = GOT_TYPE_TLSGD;
10225 got_type <= GOT_TYPE_TPREL;
10226 got_type = Got_type(got_type + 1))
10227 if (gsym->has_got_offset(got_type))
10229 unsigned int off = gsym->got_offset(got_type);
10230 if (got_type == GOT_TYPE_TLSGD)
10232 if (off == got_indx * (size / 8))
10234 if (got_type == GOT_TYPE_TPREL)
10237 return -dtp_offset;
10241 gold_unreachable();
10244 // The selector for powerpc object files.
10246 template<int size, bool big_endian>
10247 class Target_selector_powerpc : public Target_selector
10250 Target_selector_powerpc()
10251 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
10254 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
10255 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
10257 ? (big_endian ? "elf64ppc" : "elf64lppc")
10258 : (big_endian ? "elf32ppc" : "elf32lppc")))
10262 do_instantiate_target()
10263 { return new Target_powerpc<size, big_endian>(); }
10266 Target_selector_powerpc<32, true> target_selector_ppc32;
10267 Target_selector_powerpc<32, false> target_selector_ppc32le;
10268 Target_selector_powerpc<64, true> target_selector_ppc64;
10269 Target_selector_powerpc<64, false> target_selector_ppc64le;
10271 // Instantiate these constants for -O0
10272 template<int size, bool big_endian>
10273 const typename Output_data_glink<size, big_endian>::Address
10274 Output_data_glink<size, big_endian>::invalid_address;
10275 template<int size, bool big_endian>
10276 const typename Stub_table<size, big_endian>::Address
10277 Stub_table<size, big_endian>::invalid_address;
10278 template<int size, bool big_endian>
10279 const typename Target_powerpc<size, big_endian>::Address
10280 Target_powerpc<size, big_endian>::invalid_address;
10282 } // End anonymous namespace.