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), lplt_(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 LPLT section.
864 const Output_data_plt_powerpc<size, big_endian>*
870 // Return the plt offset and section for the given global sym.
872 plt_off(const Symbol* gsym,
873 const Output_data_plt_powerpc<size, big_endian>** sec) const
875 if (gsym->type() == elfcpp::STT_GNU_IFUNC
876 && gsym->can_use_relative_reloc(false))
877 *sec = this->iplt_section();
879 *sec = this->plt_section();
880 return gsym->plt_offset();
883 // Return the plt offset and section for the given local sym.
885 plt_off(const Sized_relobj_file<size, big_endian>* relobj,
886 unsigned int local_sym_index,
887 const Output_data_plt_powerpc<size, big_endian>** sec) const
889 const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
890 if (lsym->is_ifunc_symbol())
891 *sec = this->iplt_section();
893 *sec = this->lplt_section();
894 return relobj->local_plt_offset(local_sym_index);
897 // Get the .glink section.
898 const Output_data_glink<size, big_endian>*
899 glink_section() const
901 gold_assert(this->glink_ != NULL);
905 Output_data_glink<size, big_endian>*
908 gold_assert(this->glink_ != NULL);
912 bool has_glink() const
913 { return this->glink_ != NULL; }
915 // Get the GOT section.
916 const Output_data_got_powerpc<size, big_endian>*
919 gold_assert(this->got_ != NULL);
923 // Get the GOT section, creating it if necessary.
924 Output_data_got_powerpc<size, big_endian>*
925 got_section(Symbol_table*, Layout*);
928 do_make_elf_object(const std::string&, Input_file*, off_t,
929 const elfcpp::Ehdr<size, big_endian>&);
931 // Return the number of entries in the GOT.
933 got_entry_count() const
935 if (this->got_ == NULL)
937 return this->got_size() / (size / 8);
940 // Return the number of entries in the PLT.
942 plt_entry_count() const;
944 // Return the offset of the first non-reserved PLT entry.
946 first_plt_entry_offset() const
950 if (this->abiversion() >= 2)
955 // Return the size of each PLT entry.
957 plt_entry_size() const
961 if (this->abiversion() >= 2)
966 Output_data_save_res<size, big_endian>*
967 savres_section() const
969 return this->savres_section_;
972 // Add any special sections for this symbol to the gc work list.
973 // For powerpc64, this adds the code section of a function
976 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
978 // Handle target specific gc actions when adding a gc reference from
979 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
980 // and DST_OFF. For powerpc64, this adds a referenc to the code
981 // section of a function descriptor.
983 do_gc_add_reference(Symbol_table* symtab,
985 unsigned int src_shndx,
987 unsigned int dst_shndx,
988 Address dst_off) const;
990 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
993 { return this->stub_tables_; }
995 const Output_data_brlt_powerpc<size, big_endian>*
997 { return this->brlt_section_; }
1000 add_branch_lookup_table(Address to)
1002 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1003 this->branch_lookup_table_.insert(std::make_pair(to, off));
1007 find_branch_lookup_table(Address to)
1009 typename Branch_lookup_table::const_iterator p
1010 = this->branch_lookup_table_.find(to);
1011 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1015 write_branch_lookup_table(unsigned char *oview)
1017 for (typename Branch_lookup_table::const_iterator p
1018 = this->branch_lookup_table_.begin();
1019 p != this->branch_lookup_table_.end();
1022 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
1026 // Wrapper used after relax to define a local symbol in output data,
1027 // from the end if value < 0.
1029 define_local(Symbol_table* symtab, const char* name,
1030 Output_data* od, Address value, unsigned int symsize)
1033 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1034 od, value, symsize, elfcpp::STT_NOTYPE,
1035 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1036 static_cast<Signed_address>(value) < 0,
1038 // We are creating this symbol late, so need to fix up things
1039 // done early in Layout::finalize.
1040 sym->set_dynsym_index(-1U);
1044 plt_thread_safe() const
1045 { return this->plt_thread_safe_; }
1048 plt_localentry0() const
1049 { return this->plt_localentry0_; }
1052 set_has_localentry0()
1054 this->has_localentry0_ = true;
1058 is_elfv2_localentry0(const Symbol* gsym) const
1061 && this->abiversion() >= 2
1062 && this->plt_localentry0()
1063 && gsym->type() == elfcpp::STT_FUNC
1064 && gsym->is_defined()
1065 && gsym->nonvis() >> 3 == 0
1066 && !gsym->non_zero_localentry());
1070 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1071 unsigned int r_sym) const
1073 const Powerpc_relobj<size, big_endian>* ppc_object
1074 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1077 && this->abiversion() >= 2
1078 && this->plt_localentry0()
1079 && ppc_object->st_other(r_sym) >> 5 == 0)
1081 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1083 if (!psymval->is_ifunc_symbol()
1084 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1091 // Remember any symbols seen with non-zero localentry, even those
1092 // not providing a definition
1094 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1099 unsigned char st_other = sym.get_st_other();
1100 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1101 to->set_non_zero_localentry();
1103 // We haven't resolved anything, continue normal processing.
1109 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1112 set_abiversion(int ver)
1114 elfcpp::Elf_Word flags = this->processor_specific_flags();
1115 flags &= ~elfcpp::EF_PPC64_ABI;
1116 flags |= ver & elfcpp::EF_PPC64_ABI;
1117 this->set_processor_specific_flags(flags);
1121 tls_get_addr_opt() const
1122 { return this->tls_get_addr_opt_; }
1125 tls_get_addr() const
1126 { return this->tls_get_addr_; }
1128 // If optimizing __tls_get_addr calls, whether this is the
1129 // "__tls_get_addr" symbol.
1131 is_tls_get_addr_opt(const Symbol* gsym) const
1133 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1134 || gsym == this->tls_get_addr_opt_);
1138 replace_tls_get_addr(const Symbol* gsym) const
1139 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1142 set_has_tls_get_addr_opt()
1143 { this->has_tls_get_addr_opt_ = true; }
1145 // Offset to toc save stack slot
1148 { return this->abiversion() < 2 ? 40 : 24; }
1150 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1151 // so use the CR save slot. Used only by __tls_get_addr call stub,
1152 // relying on __tls_get_addr not saving CR itself.
1155 { return this->abiversion() < 2 ? 32 : 8; }
1171 : tls_get_addr_state_(NOT_EXPECTED),
1172 relinfo_(NULL), relnum_(0), r_offset_(0)
1177 if (this->tls_get_addr_state_ != NOT_EXPECTED)
1184 if (this->relinfo_ != NULL)
1185 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1186 _("missing expected __tls_get_addr call"));
1190 expect_tls_get_addr_call(
1191 const Relocate_info<size, big_endian>* relinfo,
1195 this->tls_get_addr_state_ = EXPECTED;
1196 this->relinfo_ = relinfo;
1197 this->relnum_ = relnum;
1198 this->r_offset_ = r_offset;
1202 expect_tls_get_addr_call()
1203 { this->tls_get_addr_state_ = EXPECTED; }
1206 skip_next_tls_get_addr_call()
1207 {this->tls_get_addr_state_ = SKIP; }
1210 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1211 unsigned int r_type, const Symbol* gsym)
1213 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
1214 || r_type == elfcpp::R_PPC_PLTREL24)
1216 && (gsym == target->tls_get_addr()
1217 || gsym == target->tls_get_addr_opt()));
1218 Tls_get_addr last_tls = this->tls_get_addr_state_;
1219 this->tls_get_addr_state_ = NOT_EXPECTED;
1220 if (is_tls_call && last_tls != EXPECTED)
1222 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1231 // What we're up to regarding calls to __tls_get_addr.
1232 // On powerpc, the branch and link insn making a call to
1233 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1234 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1235 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
1236 // The marker relocation always comes first, and has the same
1237 // symbol as the reloc on the insn setting up the __tls_get_addr
1238 // argument. This ties the arg setup insn with the call insn,
1239 // allowing ld to safely optimize away the call. We check that
1240 // every call to __tls_get_addr has a marker relocation, and that
1241 // every marker relocation is on a call to __tls_get_addr.
1242 Tls_get_addr tls_get_addr_state_;
1243 // Info about the last reloc for error message.
1244 const Relocate_info<size, big_endian>* relinfo_;
1249 // The class which scans relocations.
1250 class Scan : protected Track_tls
1253 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1256 : Track_tls(), issued_non_pic_error_(false)
1260 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
1263 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1264 Sized_relobj_file<size, big_endian>* object,
1265 unsigned int data_shndx,
1266 Output_section* output_section,
1267 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1268 const elfcpp::Sym<size, big_endian>& lsym,
1272 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1273 Sized_relobj_file<size, big_endian>* object,
1274 unsigned int data_shndx,
1275 Output_section* output_section,
1276 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1280 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1282 Sized_relobj_file<size, big_endian>* relobj,
1285 const elfcpp::Rela<size, big_endian>& ,
1286 unsigned int r_type,
1287 const elfcpp::Sym<size, big_endian>&)
1289 // PowerPC64 .opd is not folded, so any identical function text
1290 // may be folded and we'll still keep function addresses distinct.
1291 // That means no reloc is of concern here.
1294 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1295 <Powerpc_relobj<size, big_endian>*>(relobj);
1296 if (ppcobj->abiversion() == 1)
1299 // For 32-bit and ELFv2, conservatively assume anything but calls to
1300 // function code might be taking the address of the function.
1301 return !is_branch_reloc(r_type);
1305 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1307 Sized_relobj_file<size, big_endian>* relobj,
1310 const elfcpp::Rela<size, big_endian>& ,
1311 unsigned int r_type,
1317 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1318 <Powerpc_relobj<size, big_endian>*>(relobj);
1319 if (ppcobj->abiversion() == 1)
1322 return !is_branch_reloc(r_type);
1326 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1327 Sized_relobj_file<size, big_endian>* object,
1328 unsigned int r_type, bool report_err);
1332 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1333 unsigned int r_type);
1336 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1337 unsigned int r_type, Symbol*);
1340 generate_tls_call(Symbol_table* symtab, Layout* layout,
1341 Target_powerpc* target);
1344 check_non_pic(Relobj*, unsigned int r_type);
1346 // Whether we have issued an error about a non-PIC compilation.
1347 bool issued_non_pic_error_;
1351 symval_for_branch(const Symbol_table* symtab,
1352 const Sized_symbol<size>* gsym,
1353 Powerpc_relobj<size, big_endian>* object,
1354 Address *value, unsigned int *dest_shndx);
1356 // The class which implements relocation.
1357 class Relocate : protected Track_tls
1360 // Use 'at' branch hints when true, 'y' when false.
1361 // FIXME maybe: set this with an option.
1362 static const bool is_isa_v2 = true;
1368 // Do a relocation. Return false if the caller should not issue
1369 // any warnings about this relocation.
1371 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1372 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1373 const Sized_symbol<size>*, const Symbol_value<size>*,
1374 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1378 class Relocate_comdat_behavior
1381 // Decide what the linker should do for relocations that refer to
1382 // discarded comdat sections.
1383 inline Comdat_behavior
1384 get(const char* name)
1386 gold::Default_comdat_behavior default_behavior;
1387 Comdat_behavior ret = default_behavior.get(name);
1388 if (ret == CB_ERROR)
1391 && (strcmp(name, ".fixup") == 0
1392 || strcmp(name, ".got2") == 0))
1395 && (strcmp(name, ".opd") == 0
1396 || strcmp(name, ".toc") == 0
1397 || strcmp(name, ".toc1") == 0))
1404 // Optimize the TLS relocation type based on what we know about the
1405 // symbol. IS_FINAL is true if the final address of this symbol is
1406 // known at link time.
1408 tls::Tls_optimization
1409 optimize_tls_gd(bool is_final)
1411 // If we are generating a shared library, then we can't do anything
1413 if (parameters->options().shared()
1414 || !parameters->options().tls_optimize())
1415 return tls::TLSOPT_NONE;
1418 return tls::TLSOPT_TO_IE;
1419 return tls::TLSOPT_TO_LE;
1422 tls::Tls_optimization
1425 if (parameters->options().shared()
1426 || !parameters->options().tls_optimize())
1427 return tls::TLSOPT_NONE;
1429 return tls::TLSOPT_TO_LE;
1432 tls::Tls_optimization
1433 optimize_tls_ie(bool is_final)
1436 || parameters->options().shared()
1437 || !parameters->options().tls_optimize())
1438 return tls::TLSOPT_NONE;
1440 return tls::TLSOPT_TO_LE;
1445 make_glink_section(Layout*);
1447 // Create the PLT section.
1449 make_plt_section(Symbol_table*, Layout*);
1452 make_iplt_section(Symbol_table*, Layout*);
1455 make_lplt_section(Layout*);
1458 make_brlt_section(Layout*);
1460 // Create a PLT entry for a global symbol.
1462 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1464 // Create a PLT entry for a local IFUNC symbol.
1466 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1467 Sized_relobj_file<size, big_endian>*,
1470 // Create a PLT entry for a local non-IFUNC symbol.
1472 make_local_plt_entry(Layout*,
1473 Sized_relobj_file<size, big_endian>*,
1477 // Create a GOT entry for local dynamic __tls_get_addr.
1479 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1480 Sized_relobj_file<size, big_endian>* object);
1483 tlsld_got_offset() const
1485 return this->tlsld_got_offset_;
1488 // Get the dynamic reloc section, creating it if necessary.
1490 rela_dyn_section(Layout*);
1492 // Similarly, but for ifunc symbols get the one for ifunc.
1494 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1496 // Copy a relocation against a global symbol.
1498 copy_reloc(Symbol_table* symtab, Layout* layout,
1499 Sized_relobj_file<size, big_endian>* object,
1500 unsigned int shndx, Output_section* output_section,
1501 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1503 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1504 this->copy_relocs_.copy_reloc(symtab, layout,
1505 symtab->get_sized_symbol<size>(sym),
1506 object, shndx, output_section,
1507 r_type, reloc.get_r_offset(),
1508 reloc.get_r_addend(),
1509 this->rela_dyn_section(layout));
1512 // Look over all the input sections, deciding where to place stubs.
1514 group_sections(Layout*, const Task*, bool);
1516 // Sort output sections by address.
1517 struct Sort_sections
1520 operator()(const Output_section* sec1, const Output_section* sec2)
1521 { return sec1->address() < sec2->address(); }
1527 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1528 unsigned int data_shndx,
1530 unsigned int r_type,
1533 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1534 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
1540 // Return whether this branch is going via a plt call stub, and if
1541 // so, mark it as having an R_PPC64_TOCSAVE.
1543 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1544 unsigned int shndx, Address offset,
1545 Target_powerpc* target, Symbol_table* symtab);
1547 // If this branch needs a plt call stub, or a long branch stub, make one.
1549 make_stub(Stub_table<size, big_endian>*,
1550 Stub_table<size, big_endian>*,
1551 Symbol_table*) const;
1554 // The branch location..
1555 Powerpc_relobj<size, big_endian>* object_;
1556 unsigned int shndx_;
1558 // ..and the branch type and destination.
1559 unsigned int r_type_ : 31;
1560 unsigned int tocsave_ : 1;
1561 unsigned int r_sym_;
1565 // Information about this specific target which we pass to the
1566 // general Target structure.
1567 static Target::Target_info powerpc_info;
1569 // The types of GOT entries needed for this platform.
1570 // These values are exposed to the ABI in an incremental link.
1571 // Do not renumber existing values without changing the version
1572 // number of the .gnu_incremental_inputs section.
1576 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1577 GOT_TYPE_DTPREL, // entry for @got@dtprel
1578 GOT_TYPE_TPREL // entry for @got@tprel
1582 Output_data_got_powerpc<size, big_endian>* got_;
1583 // The PLT section. This is a container for a table of addresses,
1584 // and their relocations. Each address in the PLT has a dynamic
1585 // relocation (R_*_JMP_SLOT) and each address will have a
1586 // corresponding entry in .glink for lazy resolution of the PLT.
1587 // ppc32 initialises the PLT to point at the .glink entry, while
1588 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1589 // linker adds a stub that loads the PLT entry into ctr then
1590 // branches to ctr. There may be more than one stub for each PLT
1591 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1592 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1593 Output_data_plt_powerpc<size, big_endian>* plt_;
1594 // The IPLT section. Like plt_, this is a container for a table of
1595 // addresses and their relocations, specifically for STT_GNU_IFUNC
1596 // functions that resolve locally (STT_GNU_IFUNC functions that
1597 // don't resolve locally go in PLT). Unlike plt_, these have no
1598 // entry in .glink for lazy resolution, and the relocation section
1599 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1600 // the relocation section may contain relocations against
1601 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1602 // relocation section will appear at the end of other dynamic
1603 // relocations, so that ld.so applies these relocations after other
1604 // dynamic relocations. In a static executable, the relocation
1605 // section is emitted and marked with __rela_iplt_start and
1606 // __rela_iplt_end symbols.
1607 Output_data_plt_powerpc<size, big_endian>* iplt_;
1608 // A PLT style section for local, non-ifunc symbols
1609 Output_data_plt_powerpc<size, big_endian>* lplt_;
1610 // Section holding long branch destinations.
1611 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1612 // The .glink section.
1613 Output_data_glink<size, big_endian>* glink_;
1614 // The dynamic reloc section.
1615 Reloc_section* rela_dyn_;
1616 // Relocs saved to avoid a COPY reloc.
1617 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1618 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1619 unsigned int tlsld_got_offset_;
1621 Stub_tables stub_tables_;
1622 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1623 Branch_lookup_table branch_lookup_table_;
1625 typedef std::vector<Branch_info> Branches;
1626 Branches branch_info_;
1627 Tocsave_loc tocsave_loc_;
1629 bool plt_thread_safe_;
1630 bool plt_localentry0_;
1631 bool plt_localentry0_init_;
1632 bool has_localentry0_;
1633 bool has_tls_get_addr_opt_;
1636 int relax_fail_count_;
1637 int32_t stub_group_size_;
1639 Output_data_save_res<size, big_endian> *savres_section_;
1641 // The "__tls_get_addr" symbol, if present
1642 Symbol* tls_get_addr_;
1643 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1644 Symbol* tls_get_addr_opt_;
1648 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1651 true, // is_big_endian
1652 elfcpp::EM_PPC, // machine_code
1653 false, // has_make_symbol
1654 false, // has_resolve
1655 false, // has_code_fill
1656 true, // is_default_stack_executable
1657 false, // can_icf_inline_merge_sections
1659 "/usr/lib/ld.so.1", // dynamic_linker
1660 0x10000000, // default_text_segment_address
1661 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1662 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1663 false, // isolate_execinstr
1665 elfcpp::SHN_UNDEF, // small_common_shndx
1666 elfcpp::SHN_UNDEF, // large_common_shndx
1667 0, // small_common_section_flags
1668 0, // large_common_section_flags
1669 NULL, // attributes_section
1670 NULL, // attributes_vendor
1671 "_start", // entry_symbol_name
1672 32, // hash_entry_size
1673 elfcpp::SHT_PROGBITS, // unwind_section_type
1677 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1680 false, // is_big_endian
1681 elfcpp::EM_PPC, // machine_code
1682 false, // has_make_symbol
1683 false, // has_resolve
1684 false, // has_code_fill
1685 true, // is_default_stack_executable
1686 false, // can_icf_inline_merge_sections
1688 "/usr/lib/ld.so.1", // dynamic_linker
1689 0x10000000, // default_text_segment_address
1690 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1691 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1692 false, // isolate_execinstr
1694 elfcpp::SHN_UNDEF, // small_common_shndx
1695 elfcpp::SHN_UNDEF, // large_common_shndx
1696 0, // small_common_section_flags
1697 0, // large_common_section_flags
1698 NULL, // attributes_section
1699 NULL, // attributes_vendor
1700 "_start", // entry_symbol_name
1701 32, // hash_entry_size
1702 elfcpp::SHT_PROGBITS, // unwind_section_type
1706 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1709 true, // is_big_endian
1710 elfcpp::EM_PPC64, // machine_code
1711 false, // has_make_symbol
1712 true, // has_resolve
1713 false, // has_code_fill
1714 false, // is_default_stack_executable
1715 false, // can_icf_inline_merge_sections
1717 "/usr/lib/ld.so.1", // dynamic_linker
1718 0x10000000, // default_text_segment_address
1719 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1720 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1721 false, // isolate_execinstr
1723 elfcpp::SHN_UNDEF, // small_common_shndx
1724 elfcpp::SHN_UNDEF, // large_common_shndx
1725 0, // small_common_section_flags
1726 0, // large_common_section_flags
1727 NULL, // attributes_section
1728 NULL, // attributes_vendor
1729 "_start", // entry_symbol_name
1730 32, // hash_entry_size
1731 elfcpp::SHT_PROGBITS, // unwind_section_type
1735 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1738 false, // is_big_endian
1739 elfcpp::EM_PPC64, // machine_code
1740 false, // has_make_symbol
1741 true, // has_resolve
1742 false, // has_code_fill
1743 false, // is_default_stack_executable
1744 false, // can_icf_inline_merge_sections
1746 "/usr/lib/ld.so.1", // dynamic_linker
1747 0x10000000, // default_text_segment_address
1748 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1749 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1750 false, // isolate_execinstr
1752 elfcpp::SHN_UNDEF, // small_common_shndx
1753 elfcpp::SHN_UNDEF, // large_common_shndx
1754 0, // small_common_section_flags
1755 0, // large_common_section_flags
1756 NULL, // attributes_section
1757 NULL, // attributes_vendor
1758 "_start", // entry_symbol_name
1759 32, // hash_entry_size
1760 elfcpp::SHT_PROGBITS, // unwind_section_type
1764 is_branch_reloc(unsigned int r_type)
1766 return (r_type == elfcpp::R_POWERPC_REL24
1767 || r_type == elfcpp::R_PPC_PLTREL24
1768 || r_type == elfcpp::R_PPC_LOCAL24PC
1769 || r_type == elfcpp::R_POWERPC_REL14
1770 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1771 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1772 || r_type == elfcpp::R_POWERPC_ADDR24
1773 || r_type == elfcpp::R_POWERPC_ADDR14
1774 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1775 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1778 // Reloc resolves to plt entry.
1781 is_plt16_reloc(unsigned int r_type)
1783 return (r_type == elfcpp::R_POWERPC_PLT16_LO
1784 || r_type == elfcpp::R_POWERPC_PLT16_HI
1785 || r_type == elfcpp::R_POWERPC_PLT16_HA
1786 || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1789 // If INSN is an opcode that may be used with an @tls operand, return
1790 // the transformed insn for TLS optimisation, otherwise return 0. If
1791 // REG is non-zero only match an insn with RB or RA equal to REG.
1793 at_tls_transform(uint32_t insn, unsigned int reg)
1795 if ((insn & (0x3f << 26)) != 31 << 26)
1799 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1800 rtra = insn & ((1 << 26) - (1 << 16));
1801 else if (((insn >> 16) & 0x1f) == reg)
1802 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1806 if ((insn & (0x3ff << 1)) == 266 << 1)
1809 else if ((insn & (0x1f << 1)) == 23 << 1
1810 && ((insn & (0x1f << 6)) < 14 << 6
1811 || ((insn & (0x1f << 6)) >= 16 << 6
1812 && (insn & (0x1f << 6)) < 24 << 6)))
1813 // load and store indexed -> dform
1814 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1815 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1816 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1817 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1818 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1820 insn = (58 << 26) | 2;
1828 template<int size, bool big_endian>
1829 class Powerpc_relocate_functions
1849 typedef Powerpc_relocate_functions<size, big_endian> This;
1850 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1851 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
1853 template<int valsize>
1855 has_overflow_signed(Address value)
1857 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1858 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1859 limit <<= ((valsize - 1) >> 1);
1860 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1861 return value + limit > (limit << 1) - 1;
1864 template<int valsize>
1866 has_overflow_unsigned(Address value)
1868 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1869 limit <<= ((valsize - 1) >> 1);
1870 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1871 return value > (limit << 1) - 1;
1874 template<int valsize>
1876 has_overflow_bitfield(Address value)
1878 return (has_overflow_unsigned<valsize>(value)
1879 && has_overflow_signed<valsize>(value));
1882 template<int valsize>
1883 static inline Status
1884 overflowed(Address value, Overflow_check overflow)
1886 if (overflow == CHECK_SIGNED)
1888 if (has_overflow_signed<valsize>(value))
1889 return STATUS_OVERFLOW;
1891 else if (overflow == CHECK_UNSIGNED)
1893 if (has_overflow_unsigned<valsize>(value))
1894 return STATUS_OVERFLOW;
1896 else if (overflow == CHECK_BITFIELD)
1898 if (has_overflow_bitfield<valsize>(value))
1899 return STATUS_OVERFLOW;
1904 // Do a simple RELA relocation
1905 template<int fieldsize, int valsize>
1906 static inline Status
1907 rela(unsigned char* view, Address value, Overflow_check overflow)
1909 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1910 Valtype* wv = reinterpret_cast<Valtype*>(view);
1911 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
1912 return overflowed<valsize>(value, overflow);
1915 template<int fieldsize, int valsize>
1916 static inline Status
1917 rela(unsigned char* view,
1918 unsigned int right_shift,
1919 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1921 Overflow_check overflow)
1923 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1924 Valtype* wv = reinterpret_cast<Valtype*>(view);
1925 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
1926 Valtype reloc = value >> right_shift;
1929 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
1930 return overflowed<valsize>(value >> right_shift, overflow);
1933 // Do a simple RELA relocation, unaligned.
1934 template<int fieldsize, int valsize>
1935 static inline Status
1936 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1938 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
1939 return overflowed<valsize>(value, overflow);
1942 template<int fieldsize, int valsize>
1943 static inline Status
1944 rela_ua(unsigned char* view,
1945 unsigned int right_shift,
1946 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1948 Overflow_check overflow)
1950 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
1952 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
1953 Valtype reloc = value >> right_shift;
1956 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
1957 return overflowed<valsize>(value >> right_shift, overflow);
1961 // R_PPC64_ADDR64: (Symbol + Addend)
1963 addr64(unsigned char* view, Address value)
1964 { This::template rela<64,64>(view, value, CHECK_NONE); }
1966 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1968 addr64_u(unsigned char* view, Address value)
1969 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
1971 // R_POWERPC_ADDR32: (Symbol + Addend)
1972 static inline Status
1973 addr32(unsigned char* view, Address value, Overflow_check overflow)
1974 { return This::template rela<32,32>(view, value, overflow); }
1976 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1977 static inline Status
1978 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1979 { return This::template rela_ua<32,32>(view, value, overflow); }
1981 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1982 static inline Status
1983 addr24(unsigned char* view, Address value, Overflow_check overflow)
1985 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
1987 if (overflow != CHECK_NONE && (value & 3) != 0)
1988 stat = STATUS_OVERFLOW;
1992 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1993 static inline Status
1994 addr16(unsigned char* view, Address value, Overflow_check overflow)
1995 { return This::template rela<16,16>(view, value, overflow); }
1997 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1998 static inline Status
1999 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
2000 { return This::template rela_ua<16,16>(view, value, overflow); }
2002 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
2003 static inline Status
2004 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
2006 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
2007 if ((value & 3) != 0)
2008 stat = STATUS_OVERFLOW;
2012 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2013 static inline Status
2014 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2016 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2017 if ((value & 15) != 0)
2018 stat = STATUS_OVERFLOW;
2022 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2024 addr16_hi(unsigned char* view, Address value)
2025 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
2027 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
2029 addr16_ha(unsigned char* view, Address value)
2030 { This::addr16_hi(view, value + 0x8000); }
2032 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
2034 addr16_hi2(unsigned char* view, Address value)
2035 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
2037 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
2039 addr16_ha2(unsigned char* view, Address value)
2040 { This::addr16_hi2(view, value + 0x8000); }
2042 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
2044 addr16_hi3(unsigned char* view, Address value)
2045 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
2047 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
2049 addr16_ha3(unsigned char* view, Address value)
2050 { This::addr16_hi3(view, value + 0x8000); }
2052 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
2053 static inline Status
2054 addr14(unsigned char* view, Address value, Overflow_check overflow)
2056 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
2057 if (overflow != CHECK_NONE && (value & 3) != 0)
2058 stat = STATUS_OVERFLOW;
2062 // R_POWERPC_REL16DX_HA
2063 static inline Status
2064 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2066 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2067 Valtype* wv = reinterpret_cast<Valtype*>(view);
2068 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2070 value = static_cast<SignedAddress>(value) >> 16;
2071 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2072 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2073 return overflowed<16>(value, overflow);
2077 // Set ABI version for input and output.
2079 template<int size, bool big_endian>
2081 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2083 this->e_flags_ |= ver;
2084 if (this->abiversion() != 0)
2086 Target_powerpc<size, big_endian>* target =
2087 static_cast<Target_powerpc<size, big_endian>*>(
2088 parameters->sized_target<size, big_endian>());
2089 if (target->abiversion() == 0)
2090 target->set_abiversion(this->abiversion());
2091 else if (target->abiversion() != this->abiversion())
2092 gold_error(_("%s: ABI version %d is not compatible "
2093 "with ABI version %d output"),
2094 this->name().c_str(),
2095 this->abiversion(), target->abiversion());
2100 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2101 // relocatable object, if such sections exists.
2103 template<int size, bool big_endian>
2105 Powerpc_relobj<size, big_endian>::do_find_special_sections(
2106 Read_symbols_data* sd)
2108 const unsigned char* const pshdrs = sd->section_headers->data();
2109 const unsigned char* namesu = sd->section_names->data();
2110 const char* names = reinterpret_cast<const char*>(namesu);
2111 section_size_type names_size = sd->section_names_size;
2112 const unsigned char* s;
2114 s = this->template find_shdr<size, big_endian>(pshdrs,
2115 size == 32 ? ".got2" : ".opd",
2116 names, names_size, NULL);
2119 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2120 this->special_ = ndx;
2123 if (this->abiversion() == 0)
2124 this->set_abiversion(1);
2125 else if (this->abiversion() > 1)
2126 gold_error(_("%s: .opd invalid in abiv%d"),
2127 this->name().c_str(), this->abiversion());
2132 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2133 names, names_size, NULL);
2136 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2137 this->relatoc_ = ndx;
2138 typename elfcpp::Shdr<size, big_endian> shdr(s);
2139 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2142 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2145 // Examine .rela.opd to build info about function entry points.
2147 template<int size, bool big_endian>
2149 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2151 const unsigned char* prelocs,
2152 const unsigned char* plocal_syms)
2156 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2157 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2158 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2159 Address expected_off = 0;
2160 bool regular = true;
2161 unsigned int opd_ent_size = 0;
2163 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2165 Reltype reloc(prelocs);
2166 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2167 = reloc.get_r_info();
2168 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2169 if (r_type == elfcpp::R_PPC64_ADDR64)
2171 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2172 typename elfcpp::Elf_types<size>::Elf_Addr value;
2175 if (r_sym < this->local_symbol_count())
2177 typename elfcpp::Sym<size, big_endian>
2178 lsym(plocal_syms + r_sym * sym_size);
2179 shndx = lsym.get_st_shndx();
2180 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2181 value = lsym.get_st_value();
2184 shndx = this->symbol_section_and_value(r_sym, &value,
2186 this->set_opd_ent(reloc.get_r_offset(), shndx,
2187 value + reloc.get_r_addend());
2190 expected_off = reloc.get_r_offset();
2191 opd_ent_size = expected_off;
2193 else if (expected_off != reloc.get_r_offset())
2195 expected_off += opd_ent_size;
2197 else if (r_type == elfcpp::R_PPC64_TOC)
2199 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2204 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2205 this->name().c_str(), r_type);
2209 if (reloc_count <= 2)
2210 opd_ent_size = this->section_size(this->opd_shndx());
2211 if (opd_ent_size != 24 && opd_ent_size != 16)
2215 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2216 this->name().c_str());
2222 // Returns true if a code sequence loading the TOC entry at VALUE
2223 // relative to the TOC pointer can be converted into code calculating
2224 // a TOC pointer relative offset.
2225 // If so, the TOC pointer relative offset is stored to VALUE.
2227 template<int size, bool big_endian>
2229 Powerpc_relobj<size, big_endian>::make_toc_relative(
2230 Target_powerpc<size, big_endian>* target,
2236 // With -mcmodel=medium code it is quite possible to have
2237 // toc-relative relocs referring to objects outside the TOC.
2238 // Don't try to look at a non-existent TOC.
2239 if (this->toc_shndx() == 0)
2242 // Convert VALUE back to an address by adding got_base (see below),
2243 // then to an offset in the TOC by subtracting the TOC output
2244 // section address and the TOC output offset. Since this TOC output
2245 // section and the got output section are one and the same, we can
2246 // omit adding and subtracting the output section address.
2247 Address off = (*value + this->toc_base_offset()
2248 - this->output_section_offset(this->toc_shndx()));
2249 // Is this offset in the TOC? -mcmodel=medium code may be using
2250 // TOC relative access to variables outside the TOC. Those of
2251 // course can't be optimized. We also don't try to optimize code
2252 // that is using a different object's TOC.
2253 if (off >= this->section_size(this->toc_shndx()))
2256 if (this->no_toc_opt(off))
2259 section_size_type vlen;
2260 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2261 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2263 Address got_base = (target->got_section()->output_section()->address()
2264 + this->toc_base_offset());
2266 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2273 // Perform the Sized_relobj_file method, then set up opd info from
2276 template<int size, bool big_endian>
2278 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2280 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2283 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2284 p != rd->relocs.end();
2287 if (p->data_shndx == this->opd_shndx())
2289 uint64_t opd_size = this->section_size(this->opd_shndx());
2290 gold_assert(opd_size == static_cast<size_t>(opd_size));
2293 this->init_opd(opd_size);
2294 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2295 rd->local_symbols->data());
2303 // Read the symbols then set up st_other vector.
2305 template<int size, bool big_endian>
2307 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2309 this->base_read_symbols(sd);
2312 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2313 const unsigned char* const pshdrs = sd->section_headers->data();
2314 const unsigned int loccount = this->do_local_symbol_count();
2317 this->st_other_.resize(loccount);
2318 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2319 off_t locsize = loccount * sym_size;
2320 const unsigned int symtab_shndx = this->symtab_shndx();
2321 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2322 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2323 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2324 locsize, true, false);
2326 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2328 elfcpp::Sym<size, big_endian> sym(psyms);
2329 unsigned char st_other = sym.get_st_other();
2330 this->st_other_[i] = st_other;
2331 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2333 if (this->abiversion() == 0)
2334 this->set_abiversion(2);
2335 else if (this->abiversion() < 2)
2336 gold_error(_("%s: local symbol %d has invalid st_other"
2337 " for ABI version 1"),
2338 this->name().c_str(), i);
2345 template<int size, bool big_endian>
2347 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2349 this->e_flags_ |= ver;
2350 if (this->abiversion() != 0)
2352 Target_powerpc<size, big_endian>* target =
2353 static_cast<Target_powerpc<size, big_endian>*>(
2354 parameters->sized_target<size, big_endian>());
2355 if (target->abiversion() == 0)
2356 target->set_abiversion(this->abiversion());
2357 else if (target->abiversion() != this->abiversion())
2358 gold_error(_("%s: ABI version %d is not compatible "
2359 "with ABI version %d output"),
2360 this->name().c_str(),
2361 this->abiversion(), target->abiversion());
2366 // Call Sized_dynobj::base_read_symbols to read the symbols then
2367 // read .opd from a dynamic object, filling in opd_ent_ vector,
2369 template<int size, bool big_endian>
2371 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2373 this->base_read_symbols(sd);
2376 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2377 const unsigned char* const pshdrs = sd->section_headers->data();
2378 const unsigned char* namesu = sd->section_names->data();
2379 const char* names = reinterpret_cast<const char*>(namesu);
2380 const unsigned char* s = NULL;
2381 const unsigned char* opd;
2382 section_size_type opd_size;
2384 // Find and read .opd section.
2387 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2388 sd->section_names_size,
2393 typename elfcpp::Shdr<size, big_endian> shdr(s);
2394 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2395 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2397 if (this->abiversion() == 0)
2398 this->set_abiversion(1);
2399 else if (this->abiversion() > 1)
2400 gold_error(_("%s: .opd invalid in abiv%d"),
2401 this->name().c_str(), this->abiversion());
2403 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2404 this->opd_address_ = shdr.get_sh_addr();
2405 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2406 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2412 // Build set of executable sections.
2413 // Using a set is probably overkill. There is likely to be only
2414 // a few executable sections, typically .init, .text and .fini,
2415 // and they are generally grouped together.
2416 typedef std::set<Sec_info> Exec_sections;
2417 Exec_sections exec_sections;
2419 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2421 typename elfcpp::Shdr<size, big_endian> shdr(s);
2422 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2423 && ((shdr.get_sh_flags()
2424 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2425 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2426 && shdr.get_sh_size() != 0)
2428 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2429 shdr.get_sh_size(), i));
2432 if (exec_sections.empty())
2435 // Look over the OPD entries. This is complicated by the fact
2436 // that some binaries will use two-word entries while others
2437 // will use the standard three-word entries. In most cases
2438 // the third word (the environment pointer for languages like
2439 // Pascal) is unused and will be zero. If the third word is
2440 // used it should not be pointing into executable sections,
2442 this->init_opd(opd_size);
2443 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2445 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2446 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2447 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2449 // Chances are that this is the third word of an OPD entry.
2451 typename Exec_sections::const_iterator e
2452 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2453 if (e != exec_sections.begin())
2456 if (e->start <= val && val < e->start + e->len)
2458 // We have an address in an executable section.
2459 // VAL ought to be the function entry, set it up.
2460 this->set_opd_ent(p - opd, e->shndx, val);
2461 // Skip second word of OPD entry, the TOC pointer.
2465 // If we didn't match any executable sections, we likely
2466 // have a non-zero third word in the OPD entry.
2471 // Relocate sections.
2473 template<int size, bool big_endian>
2475 Powerpc_relobj<size, big_endian>::do_relocate_sections(
2476 const Symbol_table* symtab, const Layout* layout,
2477 const unsigned char* pshdrs, Output_file* of,
2478 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2480 unsigned int start = 1;
2482 && this->relatoc_ != 0
2483 && !parameters->options().relocatable())
2485 // Relocate .toc first.
2486 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2487 this->relatoc_, this->relatoc_);
2488 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2489 1, this->relatoc_ - 1);
2490 start = this->relatoc_ + 1;
2492 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2493 start, this->shnum() - 1);
2495 if (!parameters->options().output_is_position_independent())
2497 Target_powerpc<size, big_endian>* target
2498 = static_cast<Target_powerpc<size, big_endian>*>(
2499 parameters->sized_target<size, big_endian>());
2500 if (target->lplt_section() && target->lplt_section()->data_size() != 0)
2502 const section_size_type offset = target->lplt_section()->offset();
2503 const section_size_type oview_size
2504 = convert_to_section_size_type(target->lplt_section()->data_size());
2505 unsigned char* const oview = of->get_output_view(offset, oview_size);
2507 bool modified = false;
2508 unsigned int nsyms = this->local_symbol_count();
2509 for (unsigned int i = 0; i < nsyms; i++)
2510 if (this->local_has_plt_offset(i))
2512 Address value = this->local_symbol_value(i, 0);
2514 value += ppc64_local_entry_offset(i);
2515 size_t off = this->local_plt_offset(i);
2516 elfcpp::Swap<size, big_endian>::writeval(oview + off, value);
2520 of->write_output_view(offset, oview_size, oview);
2525 // Set up some symbols.
2527 template<int size, bool big_endian>
2529 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2530 Symbol_table* symtab,
2535 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2536 // undefined when scanning relocs (and thus requires
2537 // non-relative dynamic relocs). The proper value will be
2539 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2540 if (gotsym != NULL && gotsym->is_undefined())
2542 Target_powerpc<size, big_endian>* target =
2543 static_cast<Target_powerpc<size, big_endian>*>(
2544 parameters->sized_target<size, big_endian>());
2545 Output_data_got_powerpc<size, big_endian>* got
2546 = target->got_section(symtab, layout);
2547 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2548 Symbol_table::PREDEFINED,
2552 elfcpp::STV_HIDDEN, 0,
2556 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2557 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2558 if (sdasym != NULL && sdasym->is_undefined())
2560 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2562 = layout->add_output_section_data(".sdata", 0,
2564 | elfcpp::SHF_WRITE,
2565 sdata, ORDER_SMALL_DATA, false);
2566 symtab->define_in_output_data("_SDA_BASE_", NULL,
2567 Symbol_table::PREDEFINED,
2568 os, 32768, 0, elfcpp::STT_OBJECT,
2569 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2575 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2576 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2577 if (gotsym != NULL && gotsym->is_undefined())
2579 Target_powerpc<size, big_endian>* target =
2580 static_cast<Target_powerpc<size, big_endian>*>(
2581 parameters->sized_target<size, big_endian>());
2582 Output_data_got_powerpc<size, big_endian>* got
2583 = target->got_section(symtab, layout);
2584 symtab->define_in_output_data(".TOC.", NULL,
2585 Symbol_table::PREDEFINED,
2589 elfcpp::STV_HIDDEN, 0,
2594 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2595 if (parameters->options().tls_get_addr_optimize()
2596 && this->tls_get_addr_ != NULL
2597 && this->tls_get_addr_->in_reg())
2598 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2599 if (this->tls_get_addr_opt_ != NULL)
2601 if (this->tls_get_addr_->is_undefined()
2602 || this->tls_get_addr_->is_from_dynobj())
2604 // Make it seem as if references to __tls_get_addr are
2605 // really to __tls_get_addr_opt, so the latter symbol is
2606 // made dynamic, not the former.
2607 this->tls_get_addr_->clear_in_reg();
2608 this->tls_get_addr_opt_->set_in_reg();
2610 // We have a non-dynamic definition for __tls_get_addr.
2611 // Make __tls_get_addr_opt the same, if it does not already have
2612 // a non-dynamic definition.
2613 else if (this->tls_get_addr_opt_->is_undefined()
2614 || this->tls_get_addr_opt_->is_from_dynobj())
2616 Sized_symbol<size>* from
2617 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2618 Sized_symbol<size>* to
2619 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2620 symtab->clone<size>(to, from);
2625 // Set up PowerPC target specific relobj.
2627 template<int size, bool big_endian>
2629 Target_powerpc<size, big_endian>::do_make_elf_object(
2630 const std::string& name,
2631 Input_file* input_file,
2632 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2634 int et = ehdr.get_e_type();
2635 // ET_EXEC files are valid input for --just-symbols/-R,
2636 // and we treat them as relocatable objects.
2637 if (et == elfcpp::ET_REL
2638 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2640 Powerpc_relobj<size, big_endian>* obj =
2641 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2645 else if (et == elfcpp::ET_DYN)
2647 Powerpc_dynobj<size, big_endian>* obj =
2648 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2654 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2659 template<int size, bool big_endian>
2660 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2663 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2664 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2666 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2667 : Output_data_got<size, big_endian>(),
2668 symtab_(symtab), layout_(layout),
2669 header_ent_cnt_(size == 32 ? 3 : 1),
2670 header_index_(size == 32 ? 0x2000 : 0)
2673 this->set_addralign(256);
2676 // Override all the Output_data_got methods we use so as to first call
2679 add_global(Symbol* gsym, unsigned int got_type)
2681 this->reserve_ent();
2682 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2686 add_global_plt(Symbol* gsym, unsigned int got_type)
2688 this->reserve_ent();
2689 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2693 add_global_tls(Symbol* gsym, unsigned int got_type)
2694 { return this->add_global_plt(gsym, got_type); }
2697 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2698 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2700 this->reserve_ent();
2701 Output_data_got<size, big_endian>::
2702 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2706 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2707 Output_data_reloc_generic* rel_dyn,
2708 unsigned int r_type_1, unsigned int r_type_2)
2710 if (gsym->has_got_offset(got_type))
2713 this->reserve_ent(2);
2714 Output_data_got<size, big_endian>::
2715 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2719 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2721 this->reserve_ent();
2722 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2727 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2729 this->reserve_ent();
2730 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2735 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2736 { return this->add_local_plt(object, sym_index, got_type); }
2739 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2740 unsigned int got_type,
2741 Output_data_reloc_generic* rel_dyn,
2742 unsigned int r_type)
2744 if (object->local_has_got_offset(sym_index, got_type))
2747 this->reserve_ent(2);
2748 Output_data_got<size, big_endian>::
2749 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2753 add_constant(Valtype constant)
2755 this->reserve_ent();
2756 return Output_data_got<size, big_endian>::add_constant(constant);
2760 add_constant_pair(Valtype c1, Valtype c2)
2762 this->reserve_ent(2);
2763 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
2766 // Offset of _GLOBAL_OFFSET_TABLE_.
2770 return this->got_offset(this->header_index_);
2773 // Offset of base used to access the GOT/TOC.
2774 // The got/toc pointer reg will be set to this value.
2776 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2779 return this->g_o_t();
2781 return (this->output_section()->address()
2782 + object->toc_base_offset()
2786 // Ensure our GOT has a header.
2788 set_final_data_size()
2790 if (this->header_ent_cnt_ != 0)
2791 this->make_header();
2792 Output_data_got<size, big_endian>::set_final_data_size();
2795 // First word of GOT header needs some values that are not
2796 // handled by Output_data_got so poke them in here.
2797 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2799 do_write(Output_file* of)
2802 if (size == 32 && this->layout_->dynamic_data() != NULL)
2803 val = this->layout_->dynamic_section()->address();
2805 val = this->output_section()->address() + 0x8000;
2806 this->replace_constant(this->header_index_, val);
2807 Output_data_got<size, big_endian>::do_write(of);
2812 reserve_ent(unsigned int cnt = 1)
2814 if (this->header_ent_cnt_ == 0)
2816 if (this->num_entries() + cnt > this->header_index_)
2817 this->make_header();
2823 this->header_ent_cnt_ = 0;
2824 this->header_index_ = this->num_entries();
2827 Output_data_got<size, big_endian>::add_constant(0);
2828 Output_data_got<size, big_endian>::add_constant(0);
2829 Output_data_got<size, big_endian>::add_constant(0);
2831 // Define _GLOBAL_OFFSET_TABLE_ at the header
2832 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2835 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
2836 sym->set_value(this->g_o_t());
2839 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2840 Symbol_table::PREDEFINED,
2841 this, this->g_o_t(), 0,
2844 elfcpp::STV_HIDDEN, 0,
2848 Output_data_got<size, big_endian>::add_constant(0);
2851 // Stashed pointers.
2852 Symbol_table* symtab_;
2856 unsigned int header_ent_cnt_;
2857 // GOT header index.
2858 unsigned int header_index_;
2861 // Get the GOT section, creating it if necessary.
2863 template<int size, bool big_endian>
2864 Output_data_got_powerpc<size, big_endian>*
2865 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
2868 if (this->got_ == NULL)
2870 gold_assert(symtab != NULL && layout != NULL);
2873 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
2875 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2876 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2877 this->got_, ORDER_DATA, false);
2883 // Get the dynamic reloc section, creating it if necessary.
2885 template<int size, bool big_endian>
2886 typename Target_powerpc<size, big_endian>::Reloc_section*
2887 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
2889 if (this->rela_dyn_ == NULL)
2891 gold_assert(layout != NULL);
2892 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2893 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2894 elfcpp::SHF_ALLOC, this->rela_dyn_,
2895 ORDER_DYNAMIC_RELOCS, false);
2897 return this->rela_dyn_;
2900 // Similarly, but for ifunc symbols get the one for ifunc.
2902 template<int size, bool big_endian>
2903 typename Target_powerpc<size, big_endian>::Reloc_section*
2904 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
2909 return this->rela_dyn_section(layout);
2911 if (this->iplt_ == NULL)
2912 this->make_iplt_section(symtab, layout);
2913 return this->iplt_->rel_plt();
2919 // Determine the stub group size. The group size is the absolute
2920 // value of the parameter --stub-group-size. If --stub-group-size
2921 // is passed a negative value, we restrict stubs to be always after
2922 // the stubbed branches.
2923 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
2924 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
2925 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
2926 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
2927 owner_(NULL), output_section_(NULL)
2931 // Return true iff input section can be handled by current stub
2934 can_add_to_stub_group(Output_section* o,
2935 const Output_section::Input_section* i,
2938 const Output_section::Input_section*
2944 { return output_section_; }
2947 set_output_and_owner(Output_section* o,
2948 const Output_section::Input_section* i)
2950 this->output_section_ = o;
2959 // Adding group sections before the stubs.
2960 FINDING_STUB_SECTION,
2961 // Adding group sections after the stubs.
2965 uint32_t stub_group_size_;
2966 bool stubs_always_after_branch_;
2967 bool suppress_size_errors_;
2968 // True if a stub group can serve multiple output sections.
2971 // Current max size of group. Starts at stub_group_size_ but is
2972 // reduced to stub_group_size_/1024 on seeing a section with
2973 // external conditional branches.
2974 uint32_t group_size_;
2975 uint64_t group_start_addr_;
2976 // owner_ and output_section_ specify the section to which stubs are
2977 // attached. The stubs are placed at the end of this section.
2978 const Output_section::Input_section* owner_;
2979 Output_section* output_section_;
2982 // Return true iff input section can be handled by current stub
2983 // group. Sections are presented to this function in order,
2984 // so the first section is the head of the group.
2987 Stub_control::can_add_to_stub_group(Output_section* o,
2988 const Output_section::Input_section* i,
2991 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
2993 uint64_t start_addr = o->address();
2996 // .init and .fini sections are pasted together to form a single
2997 // function. We can't be adding stubs in the middle of the function.
2998 this_size = o->data_size();
3001 start_addr += i->relobj()->output_section_offset(i->shndx());
3002 this_size = i->data_size();
3005 uint64_t end_addr = start_addr + this_size;
3006 uint32_t group_size = this->stub_group_size_;
3008 this->group_size_ = group_size = group_size >> 10;
3010 if (this_size > group_size && !this->suppress_size_errors_)
3011 gold_warning(_("%s:%s exceeds group size"),
3012 i->relobj()->name().c_str(),
3013 i->relobj()->section_name(i->shndx()).c_str());
3015 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3016 has14 ? " 14bit" : "",
3017 i->relobj()->name().c_str(),
3018 i->relobj()->section_name(i->shndx()).c_str(),
3019 (long long) this_size,
3020 (this->state_ == NO_GROUP
3022 : (long long) end_addr - this->group_start_addr_));
3024 if (this->state_ == NO_GROUP)
3026 // Only here on very first use of Stub_control
3028 this->output_section_ = o;
3029 this->state_ = FINDING_STUB_SECTION;
3030 this->group_size_ = group_size;
3031 this->group_start_addr_ = start_addr;
3034 else if (!this->multi_os_ && this->output_section_ != o)
3036 else if (this->state_ == HAS_STUB_SECTION)
3038 // Can we add this section, which is after the stubs, to the
3040 if (end_addr - this->group_start_addr_ <= this->group_size_)
3043 else if (this->state_ == FINDING_STUB_SECTION)
3045 if ((whole_sec && this->output_section_ == o)
3046 || end_addr - this->group_start_addr_ <= this->group_size_)
3048 // Stubs are added at the end of "owner_".
3050 this->output_section_ = o;
3053 // The group before the stubs has reached maximum size.
3054 // Now see about adding sections after the stubs to the
3055 // group. If the current section has a 14-bit branch and
3056 // the group before the stubs exceeds group_size_ (because
3057 // they didn't have 14-bit branches), don't add sections
3058 // after the stubs: The size of stubs for such a large
3059 // group may exceed the reach of a 14-bit branch.
3060 if (!this->stubs_always_after_branch_
3061 && this_size <= this->group_size_
3062 && start_addr - this->group_start_addr_ <= this->group_size_)
3064 gold_debug(DEBUG_TARGET, "adding after stubs");
3065 this->state_ = HAS_STUB_SECTION;
3066 this->group_start_addr_ = start_addr;
3073 gold_debug(DEBUG_TARGET,
3074 !this->multi_os_ && this->output_section_ != o
3075 ? "nope, new output section\n"
3076 : "nope, didn't fit\n");
3078 // The section fails to fit in the current group. Set up a few
3079 // things for the next group. owner_ and output_section_ will be
3080 // set later after we've retrieved those values for the current
3082 this->state_ = FINDING_STUB_SECTION;
3083 this->group_size_ = group_size;
3084 this->group_start_addr_ = start_addr;
3088 // Look over all the input sections, deciding where to place stubs.
3090 template<int size, bool big_endian>
3092 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
3094 bool no_size_errors)
3096 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3097 parameters->options().stub_group_multi());
3099 // Group input sections and insert stub table
3100 Stub_table_owner* table_owner = NULL;
3101 std::vector<Stub_table_owner*> tables;
3102 Layout::Section_list section_list;
3103 layout->get_executable_sections(§ion_list);
3104 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
3105 for (Layout::Section_list::iterator o = section_list.begin();
3106 o != section_list.end();
3109 typedef Output_section::Input_section_list Input_section_list;
3110 for (Input_section_list::const_iterator i
3111 = (*o)->input_sections().begin();
3112 i != (*o)->input_sections().end();
3115 if (i->is_input_section()
3116 || i->is_relaxed_input_section())
3118 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3119 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3120 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3121 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3123 table_owner->output_section = stub_control.output_section();
3124 table_owner->owner = stub_control.owner();
3125 stub_control.set_output_and_owner(*o, &*i);
3128 if (table_owner == NULL)
3130 table_owner = new Stub_table_owner;
3131 tables.push_back(table_owner);
3133 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
3137 if (table_owner != NULL)
3139 table_owner->output_section = stub_control.output_section();
3140 table_owner->owner = stub_control.owner();;
3142 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3146 Stub_table<size, big_endian>* stub_table;
3148 if ((*t)->owner->is_input_section())
3149 stub_table = new Stub_table<size, big_endian>(this,
3150 (*t)->output_section,
3152 this->stub_tables_.size());
3153 else if ((*t)->owner->is_relaxed_input_section())
3154 stub_table = static_cast<Stub_table<size, big_endian>*>(
3155 (*t)->owner->relaxed_input_section());
3158 this->stub_tables_.push_back(stub_table);
3163 static unsigned long
3164 max_branch_delta (unsigned int r_type)
3166 if (r_type == elfcpp::R_POWERPC_REL14
3167 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3168 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3170 if (r_type == elfcpp::R_POWERPC_REL24
3171 || r_type == elfcpp::R_PPC_PLTREL24
3172 || r_type == elfcpp::R_PPC_LOCAL24PC)
3177 // Return whether this branch is going via a plt call stub.
3179 template<int size, bool big_endian>
3181 Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3182 Powerpc_relobj<size, big_endian>* ppc_object,
3185 Target_powerpc* target,
3186 Symbol_table* symtab)
3188 if (this->object_ != ppc_object
3189 || this->shndx_ != shndx
3190 || this->offset_ != offset)
3193 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3194 if (sym != NULL && sym->is_forwarder())
3195 sym = symtab->resolve_forwards(sym);
3196 if (target->replace_tls_get_addr(sym))
3197 sym = target->tls_get_addr_opt();
3198 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3200 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3201 && !target->is_elfv2_localentry0(gsym))
3202 : (this->object_->local_has_plt_offset(this->r_sym_)
3203 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
3211 // If this branch needs a plt call stub, or a long branch stub, make one.
3213 template<int size, bool big_endian>
3215 Target_powerpc<size, big_endian>::Branch_info::make_stub(
3216 Stub_table<size, big_endian>* stub_table,
3217 Stub_table<size, big_endian>* ifunc_stub_table,
3218 Symbol_table* symtab) const
3220 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3221 Target_powerpc<size, big_endian>* target =
3222 static_cast<Target_powerpc<size, big_endian>*>(
3223 parameters->sized_target<size, big_endian>());
3224 if (sym != NULL && sym->is_forwarder())
3225 sym = symtab->resolve_forwards(sym);
3226 if (target->replace_tls_get_addr(sym))
3227 sym = target->tls_get_addr_opt();
3228 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3232 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3233 : this->object_->local_has_plt_offset(this->r_sym_))
3237 && target->abiversion() >= 2
3238 && !parameters->options().output_is_position_independent()
3239 && !is_branch_reloc(this->r_type_))
3240 target->glink_section()->add_global_entry(gsym);
3243 if (stub_table == NULL
3246 && !parameters->options().output_is_position_independent()
3247 && !is_branch_reloc(this->r_type_)))
3248 stub_table = this->object_->stub_table(this->shndx_);
3249 if (stub_table == NULL)
3251 // This is a ref from a data section to an ifunc symbol,
3252 // or a non-branch reloc for which we always want to use
3253 // one set of stubs for resolving function addresses.
3254 stub_table = ifunc_stub_table;
3256 gold_assert(stub_table != NULL);
3257 Address from = this->object_->get_output_section_offset(this->shndx_);
3258 if (from != invalid_address)
3259 from += (this->object_->output_section(this->shndx_)->address()
3262 ok = stub_table->add_plt_call_entry(from,
3263 this->object_, gsym,
3264 this->r_type_, this->addend_,
3267 ok = stub_table->add_plt_call_entry(from,
3268 this->object_, this->r_sym_,
3269 this->r_type_, this->addend_,
3275 Address max_branch_offset = max_branch_delta(this->r_type_);
3276 if (max_branch_offset == 0)
3278 Address from = this->object_->get_output_section_offset(this->shndx_);
3279 gold_assert(from != invalid_address);
3280 from += (this->object_->output_section(this->shndx_)->address()
3285 switch (gsym->source())
3287 case Symbol::FROM_OBJECT:
3289 Object* symobj = gsym->object();
3290 if (symobj->is_dynamic()
3291 || symobj->pluginobj() != NULL)
3294 unsigned int shndx = gsym->shndx(&is_ordinary);
3295 if (shndx == elfcpp::SHN_UNDEF)
3300 case Symbol::IS_UNDEFINED:
3306 Symbol_table::Compute_final_value_status status;
3307 to = symtab->compute_final_value<size>(gsym, &status);
3308 if (status != Symbol_table::CFVS_OK)
3311 to += this->object_->ppc64_local_entry_offset(gsym);
3315 const Symbol_value<size>* psymval
3316 = this->object_->local_symbol(this->r_sym_);
3317 Symbol_value<size> symval;
3318 if (psymval->is_section_symbol())
3319 symval.set_is_section_symbol();
3320 typedef Sized_relobj_file<size, big_endian> ObjType;
3321 typename ObjType::Compute_final_local_value_status status
3322 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3324 if (status != ObjType::CFLV_OK
3325 || !symval.has_output_value())
3327 to = symval.value(this->object_, 0);
3329 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
3331 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3332 to += this->addend_;
3333 if (stub_table == NULL)
3334 stub_table = this->object_->stub_table(this->shndx_);
3335 if (size == 64 && target->abiversion() < 2)
3337 unsigned int dest_shndx;
3338 if (!target->symval_for_branch(symtab, gsym, this->object_,
3342 Address delta = to - from;
3343 if (delta + max_branch_offset >= 2 * max_branch_offset)
3345 if (stub_table == NULL)
3347 gold_warning(_("%s:%s: branch in non-executable section,"
3348 " no long branch stub for you"),
3349 this->object_->name().c_str(),
3350 this->object_->section_name(this->shndx_).c_str());
3353 bool save_res = (size == 64
3355 && gsym->source() == Symbol::IN_OUTPUT_DATA
3356 && gsym->output_data() == target->savres_section());
3357 ok = stub_table->add_long_branch_entry(this->object_,
3359 from, to, save_res);
3363 gold_debug(DEBUG_TARGET,
3364 "branch at %s:%s+%#lx\n"
3365 "can't reach stub attached to %s:%s",
3366 this->object_->name().c_str(),
3367 this->object_->section_name(this->shndx_).c_str(),
3368 (unsigned long) this->offset_,
3369 stub_table->relobj()->name().c_str(),
3370 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3375 // Relaxation hook. This is where we do stub generation.
3377 template<int size, bool big_endian>
3379 Target_powerpc<size, big_endian>::do_relax(int pass,
3380 const Input_objects*,
3381 Symbol_table* symtab,
3385 unsigned int prev_brlt_size = 0;
3389 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3391 && this->abiversion() < 2
3393 && !parameters->options().user_set_plt_thread_safe())
3395 static const char* const thread_starter[] =
3399 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3401 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3402 "mq_notify", "create_timer",
3407 "GOMP_parallel_start",
3408 "GOMP_parallel_loop_static",
3409 "GOMP_parallel_loop_static_start",
3410 "GOMP_parallel_loop_dynamic",
3411 "GOMP_parallel_loop_dynamic_start",
3412 "GOMP_parallel_loop_guided",
3413 "GOMP_parallel_loop_guided_start",
3414 "GOMP_parallel_loop_runtime",
3415 "GOMP_parallel_loop_runtime_start",
3416 "GOMP_parallel_sections",
3417 "GOMP_parallel_sections_start",
3422 if (parameters->options().shared())
3426 for (unsigned int i = 0;
3427 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3430 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3431 thread_safe = (sym != NULL
3433 && sym->in_real_elf());
3439 this->plt_thread_safe_ = thread_safe;
3444 this->stub_group_size_ = parameters->options().stub_group_size();
3445 bool no_size_errors = true;
3446 if (this->stub_group_size_ == 1)
3447 this->stub_group_size_ = 0x1c00000;
3448 else if (this->stub_group_size_ == -1)
3449 this->stub_group_size_ = -0x1e00000;
3451 no_size_errors = false;
3452 this->group_sections(layout, task, no_size_errors);
3454 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3456 this->branch_lookup_table_.clear();
3457 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3458 p != this->stub_tables_.end();
3461 (*p)->clear_stubs(true);
3463 this->stub_tables_.clear();
3464 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3465 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3466 program_name, this->stub_group_size_);
3467 this->group_sections(layout, task, true);
3470 // We need address of stub tables valid for make_stub.
3471 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3472 p != this->stub_tables_.end();
3475 const Powerpc_relobj<size, big_endian>* object
3476 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3477 Address off = object->get_output_section_offset((*p)->shndx());
3478 gold_assert(off != invalid_address);
3479 Output_section* os = (*p)->output_section();
3480 (*p)->set_address_and_size(os, off);
3485 // Clear plt call stubs, long branch stubs and branch lookup table.
3486 prev_brlt_size = this->branch_lookup_table_.size();
3487 this->branch_lookup_table_.clear();
3488 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3489 p != this->stub_tables_.end();
3492 (*p)->clear_stubs(false);
3496 // Build all the stubs.
3497 this->relax_failed_ = false;
3498 Stub_table<size, big_endian>* ifunc_stub_table
3499 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3500 Stub_table<size, big_endian>* one_stub_table
3501 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3502 for (typename Branches::const_iterator b = this->branch_info_.begin();
3503 b != this->branch_info_.end();
3506 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3507 && !this->relax_failed_)
3509 this->relax_failed_ = true;
3510 this->relax_fail_count_++;
3511 if (this->relax_fail_count_ < 3)
3516 // Did anything change size?
3517 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3518 bool again = num_huge_branches != prev_brlt_size;
3519 if (size == 64 && num_huge_branches != 0)
3520 this->make_brlt_section(layout);
3521 if (size == 64 && again)
3522 this->brlt_section_->set_current_size(num_huge_branches);
3524 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3525 p != this->stub_tables_.rend();
3527 (*p)->remove_eh_frame(layout);
3529 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3530 p != this->stub_tables_.end();
3532 (*p)->add_eh_frame(layout);
3534 typedef Unordered_set<Output_section*> Output_sections;
3535 Output_sections os_need_update;
3536 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3537 p != this->stub_tables_.end();
3540 if ((*p)->size_update())
3543 os_need_update.insert((*p)->output_section());
3547 // Set output section offsets for all input sections in an output
3548 // section that just changed size. Anything past the stubs will
3550 for (typename Output_sections::iterator p = os_need_update.begin();
3551 p != os_need_update.end();
3554 Output_section* os = *p;
3556 typedef Output_section::Input_section_list Input_section_list;
3557 for (Input_section_list::const_iterator i = os->input_sections().begin();
3558 i != os->input_sections().end();
3561 off = align_address(off, i->addralign());
3562 if (i->is_input_section() || i->is_relaxed_input_section())
3563 i->relobj()->set_section_offset(i->shndx(), off);
3564 if (i->is_relaxed_input_section())
3566 Stub_table<size, big_endian>* stub_table
3567 = static_cast<Stub_table<size, big_endian>*>(
3568 i->relaxed_input_section());
3569 Address stub_table_size = stub_table->set_address_and_size(os, off);
3570 off += stub_table_size;
3571 // After a few iterations, set current stub table size
3572 // as min size threshold, so later stub tables can only
3575 stub_table->set_min_size_threshold(stub_table_size);
3578 off += i->data_size();
3580 // If .branch_lt is part of this output section, then we have
3581 // just done the offset adjustment.
3582 os->clear_section_offsets_need_adjustment();
3587 && num_huge_branches != 0
3588 && parameters->options().output_is_position_independent())
3590 // Fill in the BRLT relocs.
3591 this->brlt_section_->reset_brlt_sizes();
3592 for (typename Branch_lookup_table::const_iterator p
3593 = this->branch_lookup_table_.begin();
3594 p != this->branch_lookup_table_.end();
3597 this->brlt_section_->add_reloc(p->first, p->second);
3599 this->brlt_section_->finalize_brlt_sizes();
3603 && (parameters->options().user_set_emit_stub_syms()
3604 ? parameters->options().emit_stub_syms()
3606 || parameters->options().output_is_position_independent()
3607 || parameters->options().emit_relocs())))
3609 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3610 p != this->stub_tables_.end();
3612 (*p)->define_stub_syms(symtab);
3614 if (this->glink_ != NULL)
3616 int stub_size = this->glink_->pltresolve_size();
3617 Address value = -stub_size;
3623 this->define_local(symtab, "__glink_PLTresolve",
3624 this->glink_, value, stub_size);
3627 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3634 template<int size, bool big_endian>
3636 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3637 unsigned char* oview,
3641 uint64_t address = plt->address();
3642 off_t len = plt->data_size();
3644 if (plt == this->glink_)
3646 // See Output_data_glink::do_write() for glink contents.
3649 gold_assert(parameters->doing_static_link());
3650 // Static linking may need stubs, to support ifunc and long
3651 // branches. We need to create an output section for
3652 // .eh_frame early in the link process, to have a place to
3653 // attach stub .eh_frame info. We also need to have
3654 // registered a CIE that matches the stub CIE. Both of
3655 // these requirements are satisfied by creating an FDE and
3656 // CIE for .glink, even though static linking will leave
3657 // .glink zero length.
3658 // ??? Hopefully generating an FDE with a zero address range
3659 // won't confuse anything that consumes .eh_frame info.
3661 else if (size == 64)
3663 // There is one word before __glink_PLTresolve
3667 else if (parameters->options().output_is_position_independent())
3669 // There are two FDEs for a position independent glink.
3670 // The first covers the branch table, the second
3671 // __glink_PLTresolve at the end of glink.
3672 off_t resolve_size = this->glink_->pltresolve_size();
3673 if (oview[9] == elfcpp::DW_CFA_nop)
3674 len -= resolve_size;
3677 address += len - resolve_size;
3684 // Must be a stub table.
3685 const Stub_table<size, big_endian>* stub_table
3686 = static_cast<const Stub_table<size, big_endian>*>(plt);
3687 uint64_t stub_address = stub_table->stub_address();
3688 len -= stub_address - address;
3689 address = stub_address;
3692 *paddress = address;
3696 // A class to handle the PLT data.
3698 template<int size, bool big_endian>
3699 class Output_data_plt_powerpc : public Output_section_data_build
3702 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3703 size, big_endian> Reloc_section;
3705 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
3706 Reloc_section* plt_rel,
3708 : Output_section_data_build(size == 32 ? 4 : 8),
3714 // Add an entry to the PLT.
3719 add_ifunc_entry(Symbol*);
3722 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3725 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3727 // Return the .rela.plt section data.
3734 // Return the number of PLT entries.
3738 if (this->current_data_size() == 0)
3740 return ((this->current_data_size() - this->first_plt_entry_offset())
3741 / this->plt_entry_size());
3746 do_adjust_output_section(Output_section* os)
3751 // Write to a map file.
3753 do_print_to_mapfile(Mapfile* mapfile) const
3754 { mapfile->print_output_data(this, this->name_); }
3757 // Return the offset of the first non-reserved PLT entry.
3759 first_plt_entry_offset() const
3761 // IPLT and LPLT have no reserved entry.
3762 if (this->name_[3] == 'I' || this->name_[3] == 'L')
3764 return this->targ_->first_plt_entry_offset();
3767 // Return the size of each PLT entry.
3769 plt_entry_size() const
3771 return this->targ_->plt_entry_size();
3774 // Write out the PLT data.
3776 do_write(Output_file*);
3778 // The reloc section.
3779 Reloc_section* rel_;
3780 // Allows access to .glink for do_write.
3781 Target_powerpc<size, big_endian>* targ_;
3782 // What to report in map file.
3786 // Add an entry to the PLT.
3788 template<int size, bool big_endian>
3790 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
3792 if (!gsym->has_plt_offset())
3794 section_size_type off = this->current_data_size();
3796 off += this->first_plt_entry_offset();
3797 gsym->set_plt_offset(off);
3798 gsym->set_needs_dynsym_entry();
3799 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3800 this->rel_->add_global(gsym, dynrel, this, off, 0);
3801 off += this->plt_entry_size();
3802 this->set_current_data_size(off);
3806 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3808 template<int size, bool big_endian>
3810 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
3812 if (!gsym->has_plt_offset())
3814 section_size_type off = this->current_data_size();
3815 gsym->set_plt_offset(off);
3816 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3817 if (size == 64 && this->targ_->abiversion() < 2)
3818 dynrel = elfcpp::R_PPC64_JMP_IREL;
3819 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
3820 off += this->plt_entry_size();
3821 this->set_current_data_size(off);
3825 // Add an entry for a local symbol to the PLT.
3827 template<int size, bool big_endian>
3829 Output_data_plt_powerpc<size, big_endian>::add_local_entry(
3830 Sized_relobj_file<size, big_endian>* relobj,
3831 unsigned int local_sym_index)
3833 if (!relobj->local_has_plt_offset(local_sym_index))
3835 section_size_type off = this->current_data_size();
3836 relobj->set_local_plt_offset(local_sym_index, off);
3839 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3840 if (size == 64 && this->targ_->abiversion() < 2)
3841 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3842 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
3843 dynrel, this, off, 0);
3845 off += this->plt_entry_size();
3846 this->set_current_data_size(off);
3850 // Add an entry for a local ifunc symbol to the IPLT.
3852 template<int size, bool big_endian>
3854 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
3855 Sized_relobj_file<size, big_endian>* relobj,
3856 unsigned int local_sym_index)
3858 if (!relobj->local_has_plt_offset(local_sym_index))
3860 section_size_type off = this->current_data_size();
3861 relobj->set_local_plt_offset(local_sym_index, off);
3862 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3863 if (size == 64 && this->targ_->abiversion() < 2)
3864 dynrel = elfcpp::R_PPC64_JMP_IREL;
3865 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
3867 off += this->plt_entry_size();
3868 this->set_current_data_size(off);
3872 static const uint32_t add_0_11_11 = 0x7c0b5a14;
3873 static const uint32_t add_2_2_11 = 0x7c425a14;
3874 static const uint32_t add_2_2_12 = 0x7c426214;
3875 static const uint32_t add_3_3_2 = 0x7c631214;
3876 static const uint32_t add_3_3_13 = 0x7c636a14;
3877 static const uint32_t add_3_12_2 = 0x7c6c1214;
3878 static const uint32_t add_3_12_13 = 0x7c6c6a14;
3879 static const uint32_t add_11_0_11 = 0x7d605a14;
3880 static const uint32_t add_11_2_11 = 0x7d625a14;
3881 static const uint32_t add_11_11_2 = 0x7d6b1214;
3882 static const uint32_t addi_0_12 = 0x380c0000;
3883 static const uint32_t addi_2_2 = 0x38420000;
3884 static const uint32_t addi_3_3 = 0x38630000;
3885 static const uint32_t addi_11_11 = 0x396b0000;
3886 static const uint32_t addi_12_1 = 0x39810000;
3887 static const uint32_t addi_12_12 = 0x398c0000;
3888 static const uint32_t addis_0_2 = 0x3c020000;
3889 static const uint32_t addis_0_13 = 0x3c0d0000;
3890 static const uint32_t addis_2_12 = 0x3c4c0000;
3891 static const uint32_t addis_11_2 = 0x3d620000;
3892 static const uint32_t addis_11_11 = 0x3d6b0000;
3893 static const uint32_t addis_11_30 = 0x3d7e0000;
3894 static const uint32_t addis_12_1 = 0x3d810000;
3895 static const uint32_t addis_12_2 = 0x3d820000;
3896 static const uint32_t addis_12_12 = 0x3d8c0000;
3897 static const uint32_t b = 0x48000000;
3898 static const uint32_t bcl_20_31 = 0x429f0005;
3899 static const uint32_t bctr = 0x4e800420;
3900 static const uint32_t bctrl = 0x4e800421;
3901 static const uint32_t beqlr = 0x4d820020;
3902 static const uint32_t blr = 0x4e800020;
3903 static const uint32_t bnectr_p4 = 0x4ce20420;
3904 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
3905 static const uint32_t cmpldi_2_0 = 0x28220000;
3906 static const uint32_t cmpdi_11_0 = 0x2c2b0000;
3907 static const uint32_t cmpwi_11_0 = 0x2c0b0000;
3908 static const uint32_t cror_15_15_15 = 0x4def7b82;
3909 static const uint32_t cror_31_31_31 = 0x4ffffb82;
3910 static const uint32_t ld_0_1 = 0xe8010000;
3911 static const uint32_t ld_0_12 = 0xe80c0000;
3912 static const uint32_t ld_2_1 = 0xe8410000;
3913 static const uint32_t ld_2_2 = 0xe8420000;
3914 static const uint32_t ld_2_11 = 0xe84b0000;
3915 static const uint32_t ld_2_12 = 0xe84c0000;
3916 static const uint32_t ld_11_1 = 0xe9610000;
3917 static const uint32_t ld_11_2 = 0xe9620000;
3918 static const uint32_t ld_11_3 = 0xe9630000;
3919 static const uint32_t ld_11_11 = 0xe96b0000;
3920 static const uint32_t ld_12_2 = 0xe9820000;
3921 static const uint32_t ld_12_3 = 0xe9830000;
3922 static const uint32_t ld_12_11 = 0xe98b0000;
3923 static const uint32_t ld_12_12 = 0xe98c0000;
3924 static const uint32_t lfd_0_1 = 0xc8010000;
3925 static const uint32_t li_0_0 = 0x38000000;
3926 static const uint32_t li_12_0 = 0x39800000;
3927 static const uint32_t lis_0 = 0x3c000000;
3928 static const uint32_t lis_2 = 0x3c400000;
3929 static const uint32_t lis_11 = 0x3d600000;
3930 static const uint32_t lis_12 = 0x3d800000;
3931 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
3932 static const uint32_t lwz_0_12 = 0x800c0000;
3933 static const uint32_t lwz_11_3 = 0x81630000;
3934 static const uint32_t lwz_11_11 = 0x816b0000;
3935 static const uint32_t lwz_11_30 = 0x817e0000;
3936 static const uint32_t lwz_12_3 = 0x81830000;
3937 static const uint32_t lwz_12_12 = 0x818c0000;
3938 static const uint32_t lwzu_0_12 = 0x840c0000;
3939 static const uint32_t mflr_0 = 0x7c0802a6;
3940 static const uint32_t mflr_11 = 0x7d6802a6;
3941 static const uint32_t mflr_12 = 0x7d8802a6;
3942 static const uint32_t mr_0_3 = 0x7c601b78;
3943 static const uint32_t mr_3_0 = 0x7c030378;
3944 static const uint32_t mtctr_0 = 0x7c0903a6;
3945 static const uint32_t mtctr_11 = 0x7d6903a6;
3946 static const uint32_t mtctr_12 = 0x7d8903a6;
3947 static const uint32_t mtlr_0 = 0x7c0803a6;
3948 static const uint32_t mtlr_11 = 0x7d6803a6;
3949 static const uint32_t mtlr_12 = 0x7d8803a6;
3950 static const uint32_t nop = 0x60000000;
3951 static const uint32_t ori_0_0_0 = 0x60000000;
3952 static const uint32_t srdi_0_0_2 = 0x7800f082;
3953 static const uint32_t std_0_1 = 0xf8010000;
3954 static const uint32_t std_0_12 = 0xf80c0000;
3955 static const uint32_t std_2_1 = 0xf8410000;
3956 static const uint32_t std_11_1 = 0xf9610000;
3957 static const uint32_t stfd_0_1 = 0xd8010000;
3958 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
3959 static const uint32_t sub_11_11_12 = 0x7d6c5850;
3960 static const uint32_t sub_12_12_11 = 0x7d8b6050;
3961 static const uint32_t xor_2_12_12 = 0x7d826278;
3962 static const uint32_t xor_11_12_12 = 0x7d8b6278;
3964 // Write out the PLT.
3966 template<int size, bool big_endian>
3968 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
3970 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
3972 const section_size_type offset = this->offset();
3973 const section_size_type oview_size
3974 = convert_to_section_size_type(this->data_size());
3975 unsigned char* const oview = of->get_output_view(offset, oview_size);
3976 unsigned char* pov = oview;
3977 unsigned char* endpov = oview + oview_size;
3979 // The address of the .glink branch table
3980 const Output_data_glink<size, big_endian>* glink
3981 = this->targ_->glink_section();
3982 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
3984 while (pov < endpov)
3986 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
3991 of->write_output_view(offset, oview_size, oview);
3995 // Create the PLT section.
3997 template<int size, bool big_endian>
3999 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4002 if (this->plt_ == NULL)
4004 if (this->got_ == NULL)
4005 this->got_section(symtab, layout);
4007 if (this->glink_ == NULL)
4008 make_glink_section(layout);
4010 // Ensure that .rela.dyn always appears before .rela.plt This is
4011 // necessary due to how, on PowerPC and some other targets, .rela.dyn
4012 // needs to include .rela.plt in its range.
4013 this->rela_dyn_section(layout);
4015 Reloc_section* plt_rel = new Reloc_section(false);
4016 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4017 elfcpp::SHF_ALLOC, plt_rel,
4018 ORDER_DYNAMIC_PLT_RELOCS, false);
4020 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
4022 layout->add_output_section_data(".plt",
4024 ? elfcpp::SHT_PROGBITS
4025 : elfcpp::SHT_NOBITS),
4026 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4033 Output_section* rela_plt_os = plt_rel->output_section();
4034 rela_plt_os->set_info_section(this->plt_->output_section());
4038 // Create the IPLT section.
4040 template<int size, bool big_endian>
4042 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4045 if (this->iplt_ == NULL)
4047 this->make_plt_section(symtab, layout);
4048 this->make_lplt_section(layout);
4050 Reloc_section* iplt_rel = new Reloc_section(false);
4051 if (this->rela_dyn_->output_section())
4052 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
4054 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
4056 if (this->plt_->output_section())
4057 this->plt_->output_section()->add_output_section_data(this->iplt_);
4061 // Create the LPLT section.
4063 template<int size, bool big_endian>
4065 Target_powerpc<size, big_endian>::make_lplt_section(Layout* layout)
4067 if (this->lplt_ == NULL)
4069 Reloc_section* lplt_rel = NULL;
4070 if (parameters->options().output_is_position_independent())
4072 lplt_rel = new Reloc_section(false);
4073 this->rela_dyn_section(layout);
4074 if (this->rela_dyn_->output_section())
4075 this->rela_dyn_->output_section()
4076 ->add_output_section_data(lplt_rel);
4079 = new Output_data_plt_powerpc<size, big_endian>(this, lplt_rel,
4081 this->make_brlt_section(layout);
4082 if (this->brlt_section_ && this->brlt_section_->output_section())
4083 this->brlt_section_->output_section()
4084 ->add_output_section_data(this->lplt_);
4086 layout->add_output_section_data(".branch_lt",
4087 elfcpp::SHT_PROGBITS,
4088 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4095 // A section for huge long branch addresses, similar to plt section.
4097 template<int size, bool big_endian>
4098 class Output_data_brlt_powerpc : public Output_section_data_build
4101 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4102 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4103 size, big_endian> Reloc_section;
4105 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4106 Reloc_section* brlt_rel)
4107 : Output_section_data_build(size == 32 ? 4 : 8),
4115 this->reset_data_size();
4116 this->rel_->reset_data_size();
4120 finalize_brlt_sizes()
4122 this->finalize_data_size();
4123 this->rel_->finalize_data_size();
4126 // Add a reloc for an entry in the BRLT.
4128 add_reloc(Address to, unsigned int off)
4129 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
4131 // Update section and reloc section size.
4133 set_current_size(unsigned int num_branches)
4135 this->reset_address_and_file_offset();
4136 this->set_current_data_size(num_branches * 16);
4137 this->finalize_data_size();
4138 Output_section* os = this->output_section();
4139 os->set_section_offsets_need_adjustment();
4140 if (this->rel_ != NULL)
4142 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4143 this->rel_->reset_address_and_file_offset();
4144 this->rel_->set_current_data_size(num_branches * reloc_size);
4145 this->rel_->finalize_data_size();
4146 Output_section* os = this->rel_->output_section();
4147 os->set_section_offsets_need_adjustment();
4153 do_adjust_output_section(Output_section* os)
4158 // Write to a map file.
4160 do_print_to_mapfile(Mapfile* mapfile) const
4161 { mapfile->print_output_data(this, "** BRLT"); }
4164 // Write out the BRLT data.
4166 do_write(Output_file*);
4168 // The reloc section.
4169 Reloc_section* rel_;
4170 Target_powerpc<size, big_endian>* targ_;
4173 // Make the branch lookup table section.
4175 template<int size, bool big_endian>
4177 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4179 if (size == 64 && this->brlt_section_ == NULL)
4181 Reloc_section* brlt_rel = NULL;
4182 bool is_pic = parameters->options().output_is_position_independent();
4185 // When PIC we can't fill in .branch_lt but must initialise at
4186 // runtime via dynamic relocations.
4187 this->rela_dyn_section(layout);
4188 brlt_rel = new Reloc_section(false);
4189 if (this->rela_dyn_->output_section())
4190 this->rela_dyn_->output_section()
4191 ->add_output_section_data(brlt_rel);
4194 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
4195 if (this->plt_ && is_pic && this->plt_->output_section())
4196 this->plt_->output_section()
4197 ->add_output_section_data(this->brlt_section_);
4199 layout->add_output_section_data(".branch_lt",
4200 elfcpp::SHT_PROGBITS,
4201 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4202 this->brlt_section_,
4208 // Write out .branch_lt when non-PIC.
4210 template<int size, bool big_endian>
4212 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4214 if (size == 64 && !parameters->options().output_is_position_independent())
4216 const section_size_type offset = this->offset();
4217 const section_size_type oview_size
4218 = convert_to_section_size_type(this->data_size());
4219 unsigned char* const oview = of->get_output_view(offset, oview_size);
4221 this->targ_->write_branch_lookup_table(oview);
4222 of->write_output_view(offset, oview_size, oview);
4226 static inline uint32_t
4232 static inline uint32_t
4238 static inline uint32_t
4241 return hi(a + 0x8000);
4247 static const unsigned char eh_frame_cie[12];
4251 const unsigned char Eh_cie<size>::eh_frame_cie[] =
4254 'z', 'R', 0, // Augmentation string.
4255 4, // Code alignment.
4256 0x80 - size / 8 , // Data alignment.
4258 1, // Augmentation size.
4259 (elfcpp::DW_EH_PE_pcrel
4260 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4261 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4264 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4265 static const unsigned char glink_eh_frame_fde_64v1[] =
4267 0, 0, 0, 0, // Replaced with offset to .glink.
4268 0, 0, 0, 0, // Replaced with size of .glink.
4269 0, // Augmentation size.
4270 elfcpp::DW_CFA_advance_loc + 1,
4271 elfcpp::DW_CFA_register, 65, 12,
4272 elfcpp::DW_CFA_advance_loc + 5,
4273 elfcpp::DW_CFA_restore_extended, 65
4276 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4277 static const unsigned char glink_eh_frame_fde_64v2[] =
4279 0, 0, 0, 0, // Replaced with offset to .glink.
4280 0, 0, 0, 0, // Replaced with size of .glink.
4281 0, // Augmentation size.
4282 elfcpp::DW_CFA_advance_loc + 1,
4283 elfcpp::DW_CFA_register, 65, 0,
4284 elfcpp::DW_CFA_advance_loc + 7,
4285 elfcpp::DW_CFA_restore_extended, 65
4288 // Describe __glink_PLTresolve use of LR, 32-bit version.
4289 static const unsigned char glink_eh_frame_fde_32[] =
4291 0, 0, 0, 0, // Replaced with offset to .glink.
4292 0, 0, 0, 0, // Replaced with size of .glink.
4293 0, // Augmentation size.
4294 elfcpp::DW_CFA_advance_loc + 2,
4295 elfcpp::DW_CFA_register, 65, 0,
4296 elfcpp::DW_CFA_advance_loc + 4,
4297 elfcpp::DW_CFA_restore_extended, 65
4300 static const unsigned char default_fde[] =
4302 0, 0, 0, 0, // Replaced with offset to stubs.
4303 0, 0, 0, 0, // Replaced with size of stubs.
4304 0, // Augmentation size.
4305 elfcpp::DW_CFA_nop, // Pad.
4310 template<bool big_endian>
4312 write_insn(unsigned char* p, uint32_t v)
4314 elfcpp::Swap<32, big_endian>::writeval(p, v);
4318 static inline unsigned int
4321 if (!parameters->options().user_set_plt_align())
4322 return size == 64 ? 32 : 8;
4323 return 1 << parameters->options().plt_align();
4326 // Stub_table holds information about plt and long branch stubs.
4327 // Stubs are built in an area following some input section determined
4328 // by group_sections(). This input section is converted to a relaxed
4329 // input section allowing it to be resized to accommodate the stubs
4331 template<int size, bool big_endian>
4332 class Stub_table : public Output_relaxed_input_section
4337 Plt_stub_ent(unsigned int off, unsigned int indx)
4338 : off_(off), indx_(indx), r2save_(0), localentry0_(0)
4342 unsigned int indx_ : 30;
4343 unsigned int r2save_ : 1;
4344 unsigned int localentry0_ : 1;
4346 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4347 static const Address invalid_address = static_cast<Address>(0) - 1;
4349 Stub_table(Target_powerpc<size, big_endian>* targ,
4350 Output_section* output_section,
4351 const Output_section::Input_section* owner,
4353 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4355 ->section_addralign(owner->shndx())),
4356 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4357 orig_data_size_(owner->current_data_size()),
4358 plt_size_(0), last_plt_size_(0),
4359 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4360 need_save_res_(false), uniq_(id), tls_get_addr_opt_bctrl_(-1u),
4363 this->set_output_section(output_section);
4365 std::vector<Output_relaxed_input_section*> new_relaxed;
4366 new_relaxed.push_back(this);
4367 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4370 // Add a plt call stub.
4372 add_plt_call_entry(Address,
4373 const Sized_relobj_file<size, big_endian>*,
4380 add_plt_call_entry(Address,
4381 const Sized_relobj_file<size, big_endian>*,
4387 // Find a given plt call stub.
4389 find_plt_call_entry(const Symbol*) const;
4392 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4393 unsigned int) const;
4396 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4402 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4407 // Add a long branch stub.
4409 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4410 unsigned int, Address, Address, bool);
4413 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4417 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4419 Address max_branch_offset = max_branch_delta(r_type);
4420 if (max_branch_offset == 0)
4422 gold_assert(from != invalid_address);
4423 Address loc = off + this->stub_address();
4424 return loc - from + max_branch_offset < 2 * max_branch_offset;
4428 clear_stubs(bool all)
4430 this->plt_call_stubs_.clear();
4431 this->plt_size_ = 0;
4432 this->long_branch_stubs_.clear();
4433 this->branch_size_ = 0;
4434 this->need_save_res_ = false;
4437 this->last_plt_size_ = 0;
4438 this->last_branch_size_ = 0;
4443 set_address_and_size(const Output_section* os, Address off)
4445 Address start_off = off;
4446 off += this->orig_data_size_;
4447 Address my_size = this->plt_size_ + this->branch_size_;
4448 if (this->need_save_res_)
4449 my_size += this->targ_->savres_section()->data_size();
4451 off = align_address(off, this->stub_align());
4452 // Include original section size and alignment padding in size
4453 my_size += off - start_off;
4454 // Ensure new size is always larger than min size
4455 // threshold. Alignment requirement is included in "my_size", so
4456 // increase "my_size" does not invalidate alignment.
4457 if (my_size < this->min_size_threshold_)
4458 my_size = this->min_size_threshold_;
4459 this->reset_address_and_file_offset();
4460 this->set_current_data_size(my_size);
4461 this->set_address_and_file_offset(os->address() + start_off,
4462 os->offset() + start_off);
4467 stub_address() const
4469 return align_address(this->address() + this->orig_data_size_,
4470 this->stub_align());
4476 return align_address(this->offset() + this->orig_data_size_,
4477 this->stub_align());
4482 { return this->plt_size_; }
4485 set_min_size_threshold(Address min_size)
4486 { this->min_size_threshold_ = min_size; }
4489 define_stub_syms(Symbol_table*);
4494 Output_section* os = this->output_section();
4495 if (os->addralign() < this->stub_align())
4497 os->set_addralign(this->stub_align());
4498 // FIXME: get rid of the insane checkpointing.
4499 // We can't increase alignment of the input section to which
4500 // stubs are attached; The input section may be .init which
4501 // is pasted together with other .init sections to form a
4502 // function. Aligning might insert zero padding resulting in
4503 // sigill. However we do need to increase alignment of the
4504 // output section so that the align_address() on offset in
4505 // set_address_and_size() adds the same padding as the
4506 // align_address() on address in stub_address().
4507 // What's more, we need this alignment for the layout done in
4508 // relaxation_loop_body() so that the output section starts at
4509 // a suitably aligned address.
4510 os->checkpoint_set_addralign(this->stub_align());
4512 if (this->last_plt_size_ != this->plt_size_
4513 || this->last_branch_size_ != this->branch_size_)
4515 this->last_plt_size_ = this->plt_size_;
4516 this->last_branch_size_ = this->branch_size_;
4522 // Generate a suitable FDE to describe code in this stub group.
4526 // Add .eh_frame info for this stub section.
4528 add_eh_frame(Layout* layout);
4530 // Remove .eh_frame info for this stub section.
4532 remove_eh_frame(Layout* layout);
4534 Target_powerpc<size, big_endian>*
4540 class Plt_stub_key_hash;
4541 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4542 Plt_stub_key_hash> Plt_stub_entries;
4543 class Branch_stub_ent;
4544 class Branch_stub_ent_hash;
4545 typedef Unordered_map<Branch_stub_ent, unsigned int,
4546 Branch_stub_ent_hash> Branch_stub_entries;
4548 // Alignment of stub section.
4552 unsigned int min_align = size == 64 ? 32 : 16;
4553 unsigned int user_align = 1 << parameters->options().plt_align();
4554 return std::max(user_align, min_align);
4557 // Return the plt offset for the given call stub.
4559 plt_off(typename Plt_stub_entries::const_iterator p,
4560 const Output_data_plt_powerpc<size, big_endian>** sec) const
4562 const Symbol* gsym = p->first.sym_;
4564 return this->targ_->plt_off(gsym, sec);
4567 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4568 unsigned int local_sym_index = p->first.locsym_;
4569 return this->targ_->plt_off(relobj, local_sym_index, sec);
4573 // Size of a given plt call stub.
4575 plt_call_size(typename Plt_stub_entries::const_iterator p) const
4579 const Symbol* gsym = p->first.sym_;
4581 + (this->targ_->is_tls_get_addr_opt(gsym) ? 8 * 4 : 0));
4584 const Output_data_plt_powerpc<size, big_endian>* plt;
4585 Address plt_addr = this->plt_off(p, &plt);
4586 plt_addr += plt->address();
4587 Address got_addr = this->targ_->got_section()->output_section()->address();
4588 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4589 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
4590 got_addr += ppcobj->toc_base_offset();
4591 Address off = plt_addr - got_addr;
4592 unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
4593 const Symbol* gsym = p->first.sym_;
4594 if (this->targ_->is_tls_get_addr_opt(gsym))
4596 if (this->targ_->abiversion() < 2)
4598 bool static_chain = parameters->options().plt_static_chain();
4599 bool thread_safe = this->targ_->plt_thread_safe();
4603 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
4609 plt_call_align(unsigned int bytes) const
4611 unsigned int align = param_plt_align<size>();
4612 return (bytes + align - 1) & -align;
4615 // Return long branch stub size.
4617 branch_stub_size(typename Branch_stub_entries::const_iterator p)
4619 Address loc = this->stub_address() + this->last_plt_size_ + p->second;
4620 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
4622 unsigned int bytes = 16;
4623 if (size == 32 && parameters->options().output_is_position_independent())
4630 do_write(Output_file*);
4632 // Plt call stub keys.
4636 Plt_stub_key(const Symbol* sym)
4637 : sym_(sym), object_(0), addend_(0), locsym_(0)
4640 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4641 unsigned int locsym_index)
4642 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4645 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4647 unsigned int r_type,
4649 : sym_(sym), object_(0), addend_(0), locsym_(0)
4652 this->addend_ = addend;
4653 else if (parameters->options().output_is_position_independent()
4654 && r_type == elfcpp::R_PPC_PLTREL24)
4656 this->addend_ = addend;
4657 if (this->addend_ >= 32768)
4658 this->object_ = object;
4662 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4663 unsigned int locsym_index,
4664 unsigned int r_type,
4666 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4669 this->addend_ = addend;
4670 else if (parameters->options().output_is_position_independent()
4671 && r_type == elfcpp::R_PPC_PLTREL24)
4672 this->addend_ = addend;
4675 bool operator==(const Plt_stub_key& that) const
4677 return (this->sym_ == that.sym_
4678 && this->object_ == that.object_
4679 && this->addend_ == that.addend_
4680 && this->locsym_ == that.locsym_);
4684 const Sized_relobj_file<size, big_endian>* object_;
4685 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4686 unsigned int locsym_;
4689 class Plt_stub_key_hash
4692 size_t operator()(const Plt_stub_key& ent) const
4694 return (reinterpret_cast<uintptr_t>(ent.sym_)
4695 ^ reinterpret_cast<uintptr_t>(ent.object_)
4701 // Long branch stub keys.
4702 class Branch_stub_ent
4705 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj,
4706 Address to, bool save_res)
4707 : dest_(to), toc_base_off_(0), save_res_(save_res)
4710 toc_base_off_ = obj->toc_base_offset();
4713 bool operator==(const Branch_stub_ent& that) const
4715 return (this->dest_ == that.dest_
4717 || this->toc_base_off_ == that.toc_base_off_));
4721 unsigned int toc_base_off_;
4725 class Branch_stub_ent_hash
4728 size_t operator()(const Branch_stub_ent& ent) const
4729 { return ent.dest_ ^ ent.toc_base_off_; }
4732 // In a sane world this would be a global.
4733 Target_powerpc<size, big_endian>* targ_;
4734 // Map sym/object/addend to stub offset.
4735 Plt_stub_entries plt_call_stubs_;
4736 // Map destination address to stub offset.
4737 Branch_stub_entries long_branch_stubs_;
4738 // size of input section
4739 section_size_type orig_data_size_;
4741 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
4742 // Some rare cases cause (PR/20529) fluctuation in stub table
4743 // size, which leads to an endless relax loop. This is to be fixed
4744 // by, after the first few iterations, allowing only increase of
4745 // stub table size. This variable sets the minimal possible size of
4746 // a stub table, it is zero for the first few iterations, then
4747 // increases monotonically.
4748 Address min_size_threshold_;
4749 // Set if this stub group needs a copy of out-of-line register
4750 // save/restore functions.
4751 bool need_save_res_;
4752 // Per stub table unique identifier.
4754 // The bctrl in the __tls_get_addr_opt stub, if present.
4755 unsigned int tls_get_addr_opt_bctrl_;
4756 // FDE unwind info for this stub group.
4757 unsigned int plt_fde_len_;
4758 unsigned char plt_fde_[20];
4761 // Add a plt call stub, if we do not already have one for this
4762 // sym/object/addend combo.
4764 template<int size, bool big_endian>
4766 Stub_table<size, big_endian>::add_plt_call_entry(
4768 const Sized_relobj_file<size, big_endian>* object,
4770 unsigned int r_type,
4774 Plt_stub_key key(object, gsym, r_type, addend);
4775 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
4776 std::pair<typename Plt_stub_entries::iterator, bool> p
4777 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
4780 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
4782 && this->targ_->is_elfv2_localentry0(gsym))
4784 p.first->second.localentry0_ = 1;
4785 this->targ_->set_has_localentry0();
4787 if (this->targ_->is_tls_get_addr_opt(gsym))
4789 this->targ_->set_has_tls_get_addr_opt();
4790 this->tls_get_addr_opt_bctrl_ = this->plt_size_ - 5 * 4;
4792 this->plt_size_ = this->plt_call_align(this->plt_size_);
4796 && !p.first->second.localentry0_)
4797 p.first->second.r2save_ = 1;
4798 return this->can_reach_stub(from, ent.off_, r_type);
4801 template<int size, bool big_endian>
4803 Stub_table<size, big_endian>::add_plt_call_entry(
4805 const Sized_relobj_file<size, big_endian>* object,
4806 unsigned int locsym_index,
4807 unsigned int r_type,
4811 Plt_stub_key key(object, locsym_index, r_type, addend);
4812 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
4813 std::pair<typename Plt_stub_entries::iterator, bool> p
4814 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
4817 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
4818 this->plt_size_ = this->plt_call_align(this->plt_size_);
4820 && this->targ_->is_elfv2_localentry0(object, locsym_index))
4822 p.first->second.localentry0_ = 1;
4823 this->targ_->set_has_localentry0();
4828 && !p.first->second.localentry0_)
4829 p.first->second.r2save_ = 1;
4830 return this->can_reach_stub(from, ent.off_, r_type);
4833 // Find a plt call stub.
4835 template<int size, bool big_endian>
4836 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4837 Stub_table<size, big_endian>::find_plt_call_entry(
4838 const Sized_relobj_file<size, big_endian>* object,
4840 unsigned int r_type,
4841 Address addend) const
4843 Plt_stub_key key(object, gsym, r_type, addend);
4844 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4845 if (p == this->plt_call_stubs_.end())
4850 template<int size, bool big_endian>
4851 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4852 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
4854 Plt_stub_key key(gsym);
4855 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4856 if (p == this->plt_call_stubs_.end())
4861 template<int size, bool big_endian>
4862 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4863 Stub_table<size, big_endian>::find_plt_call_entry(
4864 const Sized_relobj_file<size, big_endian>* object,
4865 unsigned int locsym_index,
4866 unsigned int r_type,
4867 Address addend) const
4869 Plt_stub_key key(object, locsym_index, r_type, addend);
4870 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4871 if (p == this->plt_call_stubs_.end())
4876 template<int size, bool big_endian>
4877 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4878 Stub_table<size, big_endian>::find_plt_call_entry(
4879 const Sized_relobj_file<size, big_endian>* object,
4880 unsigned int locsym_index) const
4882 Plt_stub_key key(object, locsym_index);
4883 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4884 if (p == this->plt_call_stubs_.end())
4889 // Add a long branch stub if we don't already have one to given
4892 template<int size, bool big_endian>
4894 Stub_table<size, big_endian>::add_long_branch_entry(
4895 const Powerpc_relobj<size, big_endian>* object,
4896 unsigned int r_type,
4901 Branch_stub_ent ent(object, to, save_res);
4902 Address off = this->branch_size_;
4903 std::pair<typename Branch_stub_entries::iterator, bool> p
4904 = this->long_branch_stubs_.insert(std::make_pair(ent, off));
4908 this->need_save_res_ = true;
4911 unsigned int stub_size = this->branch_stub_size(p.first);
4912 this->branch_size_ = off + stub_size;
4913 if (size == 64 && stub_size != 4)
4914 this->targ_->add_branch_lookup_table(to);
4917 return this->can_reach_stub(from, off, r_type);
4920 // Find long branch stub offset.
4922 template<int size, bool big_endian>
4923 typename Stub_table<size, big_endian>::Address
4924 Stub_table<size, big_endian>::find_long_branch_entry(
4925 const Powerpc_relobj<size, big_endian>* object,
4928 Branch_stub_ent ent(object, to, false);
4929 typename Branch_stub_entries::const_iterator p
4930 = this->long_branch_stubs_.find(ent);
4931 if (p == this->long_branch_stubs_.end())
4932 return invalid_address;
4933 if (p->first.save_res_)
4934 return to - this->targ_->savres_section()->address() + this->branch_size_;
4938 // Generate a suitable FDE to describe code in this stub group.
4939 // The __tls_get_addr_opt call stub needs to describe where it saves
4940 // LR, to support exceptions that might be thrown from __tls_get_addr.
4942 template<int size, bool big_endian>
4944 Stub_table<size, big_endian>::init_plt_fde()
4946 unsigned char* p = this->plt_fde_;
4947 // offset pcrel sdata4, size udata4, and augmentation size byte.
4950 if (this->tls_get_addr_opt_bctrl_ != -1u)
4952 unsigned int to_bctrl = this->tls_get_addr_opt_bctrl_ / 4;
4954 *p++ = elfcpp::DW_CFA_advance_loc + to_bctrl;
4955 else if (to_bctrl < 256)
4957 *p++ = elfcpp::DW_CFA_advance_loc1;
4960 else if (to_bctrl < 65536)
4962 *p++ = elfcpp::DW_CFA_advance_loc2;
4963 elfcpp::Swap<16, big_endian>::writeval(p, to_bctrl);
4968 *p++ = elfcpp::DW_CFA_advance_loc4;
4969 elfcpp::Swap<32, big_endian>::writeval(p, to_bctrl);
4972 *p++ = elfcpp::DW_CFA_offset_extended_sf;
4974 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
4975 *p++ = elfcpp::DW_CFA_advance_loc + 4;
4976 *p++ = elfcpp::DW_CFA_restore_extended;
4979 this->plt_fde_len_ = p - this->plt_fde_;
4982 // Add .eh_frame info for this stub section. Unlike other linker
4983 // generated .eh_frame this is added late in the link, because we
4984 // only want the .eh_frame info if this particular stub section is
4987 template<int size, bool big_endian>
4989 Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
4991 if (!parameters->options().ld_generated_unwind_info())
4994 // Since we add stub .eh_frame info late, it must be placed
4995 // after all other linker generated .eh_frame info so that
4996 // merge mapping need not be updated for input sections.
4997 // There is no provision to use a different CIE to that used
4999 if (!this->targ_->has_glink())
5002 if (this->plt_size_ + this->branch_size_ + this->need_save_res_ == 0)
5005 this->init_plt_fde();
5006 layout->add_eh_frame_for_plt(this,
5007 Eh_cie<size>::eh_frame_cie,
5008 sizeof (Eh_cie<size>::eh_frame_cie),
5009 this->plt_fde_, this->plt_fde_len_);
5012 template<int size, bool big_endian>
5014 Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5016 if (this->plt_fde_len_ != 0)
5018 layout->remove_eh_frame_for_plt(this,
5019 Eh_cie<size>::eh_frame_cie,
5020 sizeof (Eh_cie<size>::eh_frame_cie),
5021 this->plt_fde_, this->plt_fde_len_);
5022 this->plt_fde_len_ = 0;
5026 // A class to handle .glink.
5028 template<int size, bool big_endian>
5029 class Output_data_glink : public Output_section_data
5032 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5033 static const Address invalid_address = static_cast<Address>(0) - 1;
5035 Output_data_glink(Target_powerpc<size, big_endian>* targ)
5036 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5037 end_branch_table_(), ge_size_(0)
5041 add_eh_frame(Layout* layout);
5044 add_global_entry(const Symbol*);
5047 find_global_entry(const Symbol*) const;
5050 global_entry_align(unsigned int off) const
5052 unsigned int align = param_plt_align<size>();
5053 return (off + align - 1) & -align;
5057 global_entry_off() const
5059 return this->global_entry_align(this->end_branch_table_);
5063 global_entry_address() const
5065 gold_assert(this->is_data_size_valid());
5066 return this->address() + this->global_entry_off();
5070 pltresolve_size() const
5074 + (this->targ_->abiversion() < 2 ? 11 * 4 : 14 * 4));
5079 // Write to a map file.
5081 do_print_to_mapfile(Mapfile* mapfile) const
5082 { mapfile->print_output_data(this, _("** glink")); }
5086 set_final_data_size();
5090 do_write(Output_file*);
5092 // Allows access to .got and .plt for do_write.
5093 Target_powerpc<size, big_endian>* targ_;
5095 // Map sym to stub offset.
5096 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5097 Global_entry_stub_entries global_entry_stubs_;
5099 unsigned int end_branch_table_, ge_size_;
5102 template<int size, bool big_endian>
5104 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5106 if (!parameters->options().ld_generated_unwind_info())
5111 if (this->targ_->abiversion() < 2)
5112 layout->add_eh_frame_for_plt(this,
5113 Eh_cie<64>::eh_frame_cie,
5114 sizeof (Eh_cie<64>::eh_frame_cie),
5115 glink_eh_frame_fde_64v1,
5116 sizeof (glink_eh_frame_fde_64v1));
5118 layout->add_eh_frame_for_plt(this,
5119 Eh_cie<64>::eh_frame_cie,
5120 sizeof (Eh_cie<64>::eh_frame_cie),
5121 glink_eh_frame_fde_64v2,
5122 sizeof (glink_eh_frame_fde_64v2));
5126 // 32-bit .glink can use the default since the CIE return
5127 // address reg, LR, is valid.
5128 layout->add_eh_frame_for_plt(this,
5129 Eh_cie<32>::eh_frame_cie,
5130 sizeof (Eh_cie<32>::eh_frame_cie),
5132 sizeof (default_fde));
5133 // Except where LR is used in a PIC __glink_PLTresolve.
5134 if (parameters->options().output_is_position_independent())
5135 layout->add_eh_frame_for_plt(this,
5136 Eh_cie<32>::eh_frame_cie,
5137 sizeof (Eh_cie<32>::eh_frame_cie),
5138 glink_eh_frame_fde_32,
5139 sizeof (glink_eh_frame_fde_32));
5143 template<int size, bool big_endian>
5145 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5147 unsigned int off = this->global_entry_align(this->ge_size_);
5148 std::pair<typename Global_entry_stub_entries::iterator, bool> p
5149 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
5151 this->ge_size_ = off + 16;
5154 template<int size, bool big_endian>
5155 typename Output_data_glink<size, big_endian>::Address
5156 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5158 typename Global_entry_stub_entries::const_iterator p
5159 = this->global_entry_stubs_.find(gsym);
5160 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5163 template<int size, bool big_endian>
5165 Output_data_glink<size, big_endian>::set_final_data_size()
5167 unsigned int count = this->targ_->plt_entry_count();
5168 section_size_type total = 0;
5174 // space for branch table
5175 total += 4 * (count - 1);
5177 total += -total & 15;
5178 total += this->pltresolve_size();
5182 total += this->pltresolve_size();
5184 // space for branch table
5186 if (this->targ_->abiversion() < 2)
5190 total += 4 * (count - 0x8000);
5194 this->end_branch_table_ = total;
5195 total = this->global_entry_align(total);
5196 total += this->ge_size_;
5198 this->set_data_size(total);
5201 // Define symbols on stubs, identifying the stub.
5203 template<int size, bool big_endian>
5205 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5207 if (!this->plt_call_stubs_.empty())
5209 // The key for the plt call stub hash table includes addresses,
5210 // therefore traversal order depends on those addresses, which
5211 // can change between runs if gold is a PIE. Unfortunately the
5212 // output .symtab ordering depends on the order in which symbols
5213 // are added to the linker symtab. We want reproducible output
5214 // so must sort the call stub symbols.
5215 typedef typename Plt_stub_entries::const_iterator plt_iter;
5216 std::vector<plt_iter> sorted;
5217 sorted.resize(this->plt_call_stubs_.size());
5219 for (plt_iter cs = this->plt_call_stubs_.begin();
5220 cs != this->plt_call_stubs_.end();
5222 sorted[cs->second.indx_] = cs;
5224 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5226 plt_iter cs = sorted[i];
5229 if (cs->first.addend_ != 0)
5230 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
5233 if (cs->first.object_)
5235 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5236 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5237 sprintf(obj, "%x:", ppcobj->uniq());
5240 const char *symname;
5241 if (cs->first.sym_ == NULL)
5243 sprintf(localname, "%x", cs->first.locsym_);
5244 symname = localname;
5246 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5247 symname = this->targ_->tls_get_addr_opt()->name();
5249 symname = cs->first.sym_->name();
5250 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5251 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
5253 = this->stub_address() - this->address() + cs->second.off_;
5254 unsigned int stub_size = this->plt_call_align(this->plt_call_size(cs));
5255 this->targ_->define_local(symtab, name, this, value, stub_size);
5259 typedef typename Branch_stub_entries::const_iterator branch_iter;
5260 for (branch_iter bs = this->long_branch_stubs_.begin();
5261 bs != this->long_branch_stubs_.end();
5264 if (bs->first.save_res_)
5267 char* name = new char[8 + 13 + 16 + 1];
5268 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5269 static_cast<unsigned long long>(bs->first.dest_));
5270 Address value = (this->stub_address() - this->address()
5271 + this->plt_size_ + bs->second);
5272 unsigned int stub_size = this->branch_stub_size(bs);
5273 this->targ_->define_local(symtab, name, this, value, stub_size);
5277 // Write out plt and long branch stub code.
5279 template<int size, bool big_endian>
5281 Stub_table<size, big_endian>::do_write(Output_file* of)
5283 if (this->plt_call_stubs_.empty()
5284 && this->long_branch_stubs_.empty())
5287 const section_size_type start_off = this->offset();
5288 const section_size_type off = this->stub_offset();
5289 const section_size_type oview_size =
5290 convert_to_section_size_type(this->data_size() - (off - start_off));
5291 unsigned char* const oview = of->get_output_view(off, oview_size);
5296 const Output_data_got_powerpc<size, big_endian>* got
5297 = this->targ_->got_section();
5298 Address got_os_addr = got->output_section()->address();
5300 if (!this->plt_call_stubs_.empty())
5302 // Write out plt call stubs.
5303 typename Plt_stub_entries::const_iterator cs;
5304 for (cs = this->plt_call_stubs_.begin();
5305 cs != this->plt_call_stubs_.end();
5308 const Output_data_plt_powerpc<size, big_endian>* plt;
5309 Address pltoff = this->plt_off(cs, &plt);
5310 Address plt_addr = pltoff + plt->address();
5311 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5312 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5313 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
5314 Address off = plt_addr - got_addr;
5316 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5317 gold_error(_("%s: linkage table error against `%s'"),
5318 cs->first.object_->name().c_str(),
5319 cs->first.sym_->demangled_name().c_str());
5321 bool plt_load_toc = this->targ_->abiversion() < 2;
5323 = plt_load_toc && parameters->options().plt_static_chain();
5325 = plt_load_toc && this->targ_->plt_thread_safe();
5326 bool use_fake_dep = false;
5327 Address cmp_branch_off = 0;
5330 unsigned int pltindex
5331 = ((pltoff - this->targ_->first_plt_entry_offset())
5332 / this->targ_->plt_entry_size());
5334 = (this->targ_->glink_section()->pltresolve_size()
5336 if (pltindex > 32768)
5337 glinkoff += (pltindex - 32768) * 4;
5339 = this->targ_->glink_section()->address() + glinkoff;
5341 = (this->stub_address() + cs->second.off_ + 20
5342 + 4 * cs->second.r2save_
5343 + 4 * (ha(off) != 0)
5344 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
5345 + 4 * static_chain);
5346 cmp_branch_off = to - from;
5347 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
5350 p = oview + cs->second.off_;
5351 const Symbol* gsym = cs->first.sym_;
5352 if (this->targ_->is_tls_get_addr_opt(gsym))
5354 write_insn<big_endian>(p, ld_11_3 + 0);
5356 write_insn<big_endian>(p, ld_12_3 + 8);
5358 write_insn<big_endian>(p, mr_0_3);
5360 write_insn<big_endian>(p, cmpdi_11_0);
5362 write_insn<big_endian>(p, add_3_12_13);
5364 write_insn<big_endian>(p, beqlr);
5366 write_insn<big_endian>(p, mr_3_0);
5368 if (!cs->second.localentry0_)
5370 write_insn<big_endian>(p, mflr_11);
5372 write_insn<big_endian>(p, (std_11_1
5373 + this->targ_->stk_linker()));
5376 use_fake_dep = thread_safe;
5380 if (cs->second.r2save_)
5382 write_insn<big_endian>(p,
5383 std_2_1 + this->targ_->stk_toc());
5388 write_insn<big_endian>(p, addis_11_2 + ha(off));
5390 write_insn<big_endian>(p, ld_12_11 + l(off));
5395 write_insn<big_endian>(p, addis_12_2 + ha(off));
5397 write_insn<big_endian>(p, ld_12_12 + l(off));
5401 && ha(off + 8 + 8 * static_chain) != ha(off))
5403 write_insn<big_endian>(p, addi_11_11 + l(off));
5407 write_insn<big_endian>(p, mtctr_12);
5413 write_insn<big_endian>(p, xor_2_12_12);
5415 write_insn<big_endian>(p, add_11_11_2);
5418 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
5422 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
5429 if (cs->second.r2save_)
5431 write_insn<big_endian>(p,
5432 std_2_1 + this->targ_->stk_toc());
5435 write_insn<big_endian>(p, ld_12_2 + l(off));
5438 && ha(off + 8 + 8 * static_chain) != ha(off))
5440 write_insn<big_endian>(p, addi_2_2 + l(off));
5444 write_insn<big_endian>(p, mtctr_12);
5450 write_insn<big_endian>(p, xor_11_12_12);
5452 write_insn<big_endian>(p, add_2_2_11);
5457 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
5460 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
5464 if (!cs->second.localentry0_
5465 && this->targ_->is_tls_get_addr_opt(gsym))
5467 write_insn<big_endian>(p, bctrl);
5469 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5471 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5473 write_insn<big_endian>(p, mtlr_11);
5475 write_insn<big_endian>(p, blr);
5477 else if (thread_safe && !use_fake_dep)
5479 write_insn<big_endian>(p, cmpldi_2_0);
5481 write_insn<big_endian>(p, bnectr_p4);
5483 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
5486 write_insn<big_endian>(p, bctr);
5490 // Write out long branch stubs.
5491 typename Branch_stub_entries::const_iterator bs;
5492 for (bs = this->long_branch_stubs_.begin();
5493 bs != this->long_branch_stubs_.end();
5496 if (bs->first.save_res_)
5498 p = oview + this->plt_size_ + bs->second;
5499 Address loc = this->stub_address() + this->plt_size_ + bs->second;
5500 Address delta = bs->first.dest_ - loc;
5501 if (delta + (1 << 25) < 2 << 25)
5502 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
5506 = this->targ_->find_branch_lookup_table(bs->first.dest_);
5507 gold_assert(brlt_addr != invalid_address);
5508 brlt_addr += this->targ_->brlt_section()->address();
5509 Address got_addr = got_os_addr + bs->first.toc_base_off_;
5510 Address brltoff = brlt_addr - got_addr;
5511 if (ha(brltoff) == 0)
5513 write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4;
5517 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
5518 write_insn<big_endian>(p, ld_12_12 + l(brltoff)), p += 4;
5520 write_insn<big_endian>(p, mtctr_12), p += 4;
5521 write_insn<big_endian>(p, bctr);
5527 if (!this->plt_call_stubs_.empty())
5529 // The address of _GLOBAL_OFFSET_TABLE_.
5530 Address g_o_t = invalid_address;
5532 // Write out plt call stubs.
5533 typename Plt_stub_entries::const_iterator cs;
5534 for (cs = this->plt_call_stubs_.begin();
5535 cs != this->plt_call_stubs_.end();
5538 const Output_data_plt_powerpc<size, big_endian>* plt;
5539 Address plt_addr = this->plt_off(cs, &plt);
5540 plt_addr += plt->address();
5542 p = oview + cs->second.off_;
5543 const Symbol* gsym = cs->first.sym_;
5544 if (this->targ_->is_tls_get_addr_opt(gsym))
5546 write_insn<big_endian>(p, lwz_11_3 + 0);
5548 write_insn<big_endian>(p, lwz_12_3 + 4);
5550 write_insn<big_endian>(p, mr_0_3);
5552 write_insn<big_endian>(p, cmpwi_11_0);
5554 write_insn<big_endian>(p, add_3_12_2);
5556 write_insn<big_endian>(p, beqlr);
5558 write_insn<big_endian>(p, mr_3_0);
5560 write_insn<big_endian>(p, nop);
5563 if (parameters->options().output_is_position_independent())
5566 const Powerpc_relobj<size, big_endian>* ppcobj
5567 = (static_cast<const Powerpc_relobj<size, big_endian>*>
5568 (cs->first.object_));
5569 if (ppcobj != NULL && cs->first.addend_ >= 32768)
5571 unsigned int got2 = ppcobj->got2_shndx();
5572 got_addr = ppcobj->get_output_section_offset(got2);
5573 gold_assert(got_addr != invalid_address);
5574 got_addr += (ppcobj->output_section(got2)->address()
5575 + cs->first.addend_);
5579 if (g_o_t == invalid_address)
5581 const Output_data_got_powerpc<size, big_endian>* got
5582 = this->targ_->got_section();
5583 g_o_t = got->address() + got->g_o_t();
5588 Address off = plt_addr - got_addr;
5590 write_insn<big_endian>(p, lwz_11_30 + l(off));
5593 write_insn<big_endian>(p, addis_11_30 + ha(off));
5595 write_insn<big_endian>(p, lwz_11_11 + l(off));
5600 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
5602 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
5605 write_insn<big_endian>(p, mtctr_11);
5607 write_insn<big_endian>(p, bctr);
5611 // Write out long branch stubs.
5612 typename Branch_stub_entries::const_iterator bs;
5613 for (bs = this->long_branch_stubs_.begin();
5614 bs != this->long_branch_stubs_.end();
5617 if (bs->first.save_res_)
5619 p = oview + this->plt_size_ + bs->second;
5620 Address loc = this->stub_address() + this->plt_size_ + bs->second;
5621 Address delta = bs->first.dest_ - loc;
5622 if (delta + (1 << 25) < 2 << 25)
5623 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
5624 else if (!parameters->options().output_is_position_independent())
5626 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
5628 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
5633 write_insn<big_endian>(p, mflr_0);
5635 write_insn<big_endian>(p, bcl_20_31);
5637 write_insn<big_endian>(p, mflr_12);
5639 write_insn<big_endian>(p, addis_12_12 + ha(delta));
5641 write_insn<big_endian>(p, addi_12_12 + l(delta));
5643 write_insn<big_endian>(p, mtlr_0);
5646 write_insn<big_endian>(p, mtctr_12);
5648 write_insn<big_endian>(p, bctr);
5651 if (this->need_save_res_)
5653 p = oview + this->plt_size_ + this->branch_size_;
5654 memcpy (p, this->targ_->savres_section()->contents(),
5655 this->targ_->savres_section()->data_size());
5659 // Write out .glink.
5661 template<int size, bool big_endian>
5663 Output_data_glink<size, big_endian>::do_write(Output_file* of)
5665 const section_size_type off = this->offset();
5666 const section_size_type oview_size =
5667 convert_to_section_size_type(this->data_size());
5668 unsigned char* const oview = of->get_output_view(off, oview_size);
5671 // The base address of the .plt section.
5672 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5673 Address plt_base = this->targ_->plt_section()->address();
5677 if (this->end_branch_table_ != 0)
5679 // Write pltresolve stub.
5681 Address after_bcl = this->address() + 16;
5682 Address pltoff = plt_base - after_bcl;
5684 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
5686 if (this->targ_->abiversion() < 2)
5688 write_insn<big_endian>(p, mflr_12), p += 4;
5689 write_insn<big_endian>(p, bcl_20_31), p += 4;
5690 write_insn<big_endian>(p, mflr_11), p += 4;
5691 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5692 write_insn<big_endian>(p, mtlr_12), p += 4;
5693 write_insn<big_endian>(p, add_11_2_11), p += 4;
5694 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5695 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
5696 write_insn<big_endian>(p, mtctr_12), p += 4;
5697 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
5701 write_insn<big_endian>(p, mflr_0), p += 4;
5702 write_insn<big_endian>(p, bcl_20_31), p += 4;
5703 write_insn<big_endian>(p, mflr_11), p += 4;
5704 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
5705 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5706 write_insn<big_endian>(p, mtlr_0), p += 4;
5707 write_insn<big_endian>(p, sub_12_12_11), p += 4;
5708 write_insn<big_endian>(p, add_11_2_11), p += 4;
5709 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
5710 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5711 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
5712 write_insn<big_endian>(p, mtctr_12), p += 4;
5713 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
5715 write_insn<big_endian>(p, bctr), p += 4;
5716 gold_assert(p == oview + this->pltresolve_size());
5718 // Write lazy link call stubs.
5720 while (p < oview + this->end_branch_table_)
5722 if (this->targ_->abiversion() < 2)
5726 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
5730 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
5731 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
5734 uint32_t branch_off = 8 - (p - oview);
5735 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
5740 Address plt_base = this->targ_->plt_section()->address();
5741 Address iplt_base = invalid_address;
5742 unsigned int global_entry_off = this->global_entry_off();
5743 Address global_entry_base = this->address() + global_entry_off;
5744 typename Global_entry_stub_entries::const_iterator ge;
5745 for (ge = this->global_entry_stubs_.begin();
5746 ge != this->global_entry_stubs_.end();
5749 p = oview + global_entry_off + ge->second;
5750 Address plt_addr = ge->first->plt_offset();
5751 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
5752 && ge->first->can_use_relative_reloc(false))
5754 if (iplt_base == invalid_address)
5755 iplt_base = this->targ_->iplt_section()->address();
5756 plt_addr += iplt_base;
5759 plt_addr += plt_base;
5760 Address my_addr = global_entry_base + ge->second;
5761 Address off = plt_addr - my_addr;
5763 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
5764 gold_error(_("%s: linkage table error against `%s'"),
5765 ge->first->object()->name().c_str(),
5766 ge->first->demangled_name().c_str());
5768 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
5769 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
5770 write_insn<big_endian>(p, mtctr_12), p += 4;
5771 write_insn<big_endian>(p, bctr);
5776 const Output_data_got_powerpc<size, big_endian>* got
5777 = this->targ_->got_section();
5778 // The address of _GLOBAL_OFFSET_TABLE_.
5779 Address g_o_t = got->address() + got->g_o_t();
5781 // Write out pltresolve branch table.
5783 unsigned int the_end = oview_size - this->pltresolve_size();
5784 unsigned char* end_p = oview + the_end;
5785 while (p < end_p - 8 * 4)
5786 write_insn<big_endian>(p, b + end_p - p), p += 4;
5788 write_insn<big_endian>(p, nop), p += 4;
5790 // Write out pltresolve call stub.
5791 end_p = oview + oview_size;
5792 if (parameters->options().output_is_position_independent())
5794 Address res0_off = 0;
5795 Address after_bcl_off = the_end + 12;
5796 Address bcl_res0 = after_bcl_off - res0_off;
5798 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
5800 write_insn<big_endian>(p, mflr_0);
5802 write_insn<big_endian>(p, bcl_20_31);
5804 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
5806 write_insn<big_endian>(p, mflr_12);
5808 write_insn<big_endian>(p, mtlr_0);
5810 write_insn<big_endian>(p, sub_11_11_12);
5813 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
5815 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
5817 if (ha(got_bcl) == ha(got_bcl + 4))
5819 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
5821 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
5825 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
5827 write_insn<big_endian>(p, lwz_12_12 + 4);
5830 write_insn<big_endian>(p, mtctr_0);
5832 write_insn<big_endian>(p, add_0_11_11);
5834 write_insn<big_endian>(p, add_11_0_11);
5838 Address res0 = this->address();
5840 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
5842 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
5844 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5845 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
5847 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
5849 write_insn<big_endian>(p, addi_11_11 + l(-res0));
5851 write_insn<big_endian>(p, mtctr_0);
5853 write_insn<big_endian>(p, add_0_11_11);
5855 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5856 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
5858 write_insn<big_endian>(p, lwz_12_12 + 4);
5860 write_insn<big_endian>(p, add_11_0_11);
5863 write_insn<big_endian>(p, bctr);
5867 write_insn<big_endian>(p, nop);
5872 of->write_output_view(off, oview_size, oview);
5876 // A class to handle linker generated save/restore functions.
5878 template<int size, bool big_endian>
5879 class Output_data_save_res : public Output_section_data_build
5882 Output_data_save_res(Symbol_table* symtab);
5884 const unsigned char*
5891 // Write to a map file.
5893 do_print_to_mapfile(Mapfile* mapfile) const
5894 { mapfile->print_output_data(this, _("** save/restore")); }
5897 do_write(Output_file*);
5900 // The maximum size of save/restore contents.
5901 static const unsigned int savres_max = 218*4;
5904 savres_define(Symbol_table* symtab,
5906 unsigned int lo, unsigned int hi,
5907 unsigned char* write_ent(unsigned char*, int),
5908 unsigned char* write_tail(unsigned char*, int));
5910 unsigned char *contents_;
5913 template<bool big_endian>
5914 static unsigned char*
5915 savegpr0(unsigned char* p, int r)
5917 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5918 write_insn<big_endian>(p, insn);
5922 template<bool big_endian>
5923 static unsigned char*
5924 savegpr0_tail(unsigned char* p, int r)
5926 p = savegpr0<big_endian>(p, r);
5927 uint32_t insn = std_0_1 + 16;
5928 write_insn<big_endian>(p, insn);
5930 write_insn<big_endian>(p, blr);
5934 template<bool big_endian>
5935 static unsigned char*
5936 restgpr0(unsigned char* p, int r)
5938 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5939 write_insn<big_endian>(p, insn);
5943 template<bool big_endian>
5944 static unsigned char*
5945 restgpr0_tail(unsigned char* p, int r)
5947 uint32_t insn = ld_0_1 + 16;
5948 write_insn<big_endian>(p, insn);
5950 p = restgpr0<big_endian>(p, r);
5951 write_insn<big_endian>(p, mtlr_0);
5955 p = restgpr0<big_endian>(p, 30);
5956 p = restgpr0<big_endian>(p, 31);
5958 write_insn<big_endian>(p, blr);
5962 template<bool big_endian>
5963 static unsigned char*
5964 savegpr1(unsigned char* p, int r)
5966 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5967 write_insn<big_endian>(p, insn);
5971 template<bool big_endian>
5972 static unsigned char*
5973 savegpr1_tail(unsigned char* p, int r)
5975 p = savegpr1<big_endian>(p, r);
5976 write_insn<big_endian>(p, blr);
5980 template<bool big_endian>
5981 static unsigned char*
5982 restgpr1(unsigned char* p, int r)
5984 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5985 write_insn<big_endian>(p, insn);
5989 template<bool big_endian>
5990 static unsigned char*
5991 restgpr1_tail(unsigned char* p, int r)
5993 p = restgpr1<big_endian>(p, r);
5994 write_insn<big_endian>(p, blr);
5998 template<bool big_endian>
5999 static unsigned char*
6000 savefpr(unsigned char* p, int r)
6002 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6003 write_insn<big_endian>(p, insn);
6007 template<bool big_endian>
6008 static unsigned char*
6009 savefpr0_tail(unsigned char* p, int r)
6011 p = savefpr<big_endian>(p, r);
6012 write_insn<big_endian>(p, std_0_1 + 16);
6014 write_insn<big_endian>(p, blr);
6018 template<bool big_endian>
6019 static unsigned char*
6020 restfpr(unsigned char* p, int r)
6022 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6023 write_insn<big_endian>(p, insn);
6027 template<bool big_endian>
6028 static unsigned char*
6029 restfpr0_tail(unsigned char* p, int r)
6031 write_insn<big_endian>(p, ld_0_1 + 16);
6033 p = restfpr<big_endian>(p, r);
6034 write_insn<big_endian>(p, mtlr_0);
6038 p = restfpr<big_endian>(p, 30);
6039 p = restfpr<big_endian>(p, 31);
6041 write_insn<big_endian>(p, blr);
6045 template<bool big_endian>
6046 static unsigned char*
6047 savefpr1_tail(unsigned char* p, int r)
6049 p = savefpr<big_endian>(p, r);
6050 write_insn<big_endian>(p, blr);
6054 template<bool big_endian>
6055 static unsigned char*
6056 restfpr1_tail(unsigned char* p, int r)
6058 p = restfpr<big_endian>(p, r);
6059 write_insn<big_endian>(p, blr);
6063 template<bool big_endian>
6064 static unsigned char*
6065 savevr(unsigned char* p, int r)
6067 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
6068 write_insn<big_endian>(p, insn);
6070 insn = stvx_0_12_0 + (r << 21);
6071 write_insn<big_endian>(p, insn);
6075 template<bool big_endian>
6076 static unsigned char*
6077 savevr_tail(unsigned char* p, int r)
6079 p = savevr<big_endian>(p, r);
6080 write_insn<big_endian>(p, blr);
6084 template<bool big_endian>
6085 static unsigned char*
6086 restvr(unsigned char* p, int r)
6088 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
6089 write_insn<big_endian>(p, insn);
6091 insn = lvx_0_12_0 + (r << 21);
6092 write_insn<big_endian>(p, insn);
6096 template<bool big_endian>
6097 static unsigned char*
6098 restvr_tail(unsigned char* p, int r)
6100 p = restvr<big_endian>(p, r);
6101 write_insn<big_endian>(p, blr);
6106 template<int size, bool big_endian>
6107 Output_data_save_res<size, big_endian>::Output_data_save_res(
6108 Symbol_table* symtab)
6109 : Output_section_data_build(4),
6112 this->savres_define(symtab,
6113 "_savegpr0_", 14, 31,
6114 savegpr0<big_endian>, savegpr0_tail<big_endian>);
6115 this->savres_define(symtab,
6116 "_restgpr0_", 14, 29,
6117 restgpr0<big_endian>, restgpr0_tail<big_endian>);
6118 this->savres_define(symtab,
6119 "_restgpr0_", 30, 31,
6120 restgpr0<big_endian>, restgpr0_tail<big_endian>);
6121 this->savres_define(symtab,
6122 "_savegpr1_", 14, 31,
6123 savegpr1<big_endian>, savegpr1_tail<big_endian>);
6124 this->savres_define(symtab,
6125 "_restgpr1_", 14, 31,
6126 restgpr1<big_endian>, restgpr1_tail<big_endian>);
6127 this->savres_define(symtab,
6128 "_savefpr_", 14, 31,
6129 savefpr<big_endian>, savefpr0_tail<big_endian>);
6130 this->savres_define(symtab,
6131 "_restfpr_", 14, 29,
6132 restfpr<big_endian>, restfpr0_tail<big_endian>);
6133 this->savres_define(symtab,
6134 "_restfpr_", 30, 31,
6135 restfpr<big_endian>, restfpr0_tail<big_endian>);
6136 this->savres_define(symtab,
6138 savefpr<big_endian>, savefpr1_tail<big_endian>);
6139 this->savres_define(symtab,
6141 restfpr<big_endian>, restfpr1_tail<big_endian>);
6142 this->savres_define(symtab,
6144 savevr<big_endian>, savevr_tail<big_endian>);
6145 this->savres_define(symtab,
6147 restvr<big_endian>, restvr_tail<big_endian>);
6150 template<int size, bool big_endian>
6152 Output_data_save_res<size, big_endian>::savres_define(
6153 Symbol_table* symtab,
6155 unsigned int lo, unsigned int hi,
6156 unsigned char* write_ent(unsigned char*, int),
6157 unsigned char* write_tail(unsigned char*, int))
6159 size_t len = strlen(name);
6160 bool writing = false;
6163 memcpy(sym, name, len);
6166 for (unsigned int i = lo; i <= hi; i++)
6168 sym[len + 0] = i / 10 + '0';
6169 sym[len + 1] = i % 10 + '0';
6170 Symbol* gsym = symtab->lookup(sym);
6171 bool refd = gsym != NULL && gsym->is_undefined();
6172 writing = writing || refd;
6175 if (this->contents_ == NULL)
6176 this->contents_ = new unsigned char[this->savres_max];
6178 section_size_type value = this->current_data_size();
6179 unsigned char* p = this->contents_ + value;
6181 p = write_ent(p, i);
6183 p = write_tail(p, i);
6184 section_size_type cur_size = p - this->contents_;
6185 this->set_current_data_size(cur_size);
6187 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
6188 this, value, cur_size - value,
6189 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
6190 elfcpp::STV_HIDDEN, 0, false, false);
6195 // Write out save/restore.
6197 template<int size, bool big_endian>
6199 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
6201 const section_size_type off = this->offset();
6202 const section_size_type oview_size =
6203 convert_to_section_size_type(this->data_size());
6204 unsigned char* const oview = of->get_output_view(off, oview_size);
6205 memcpy(oview, this->contents_, oview_size);
6206 of->write_output_view(off, oview_size, oview);
6210 // Create the glink section.
6212 template<int size, bool big_endian>
6214 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
6216 if (this->glink_ == NULL)
6218 this->glink_ = new Output_data_glink<size, big_endian>(this);
6219 this->glink_->add_eh_frame(layout);
6220 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
6221 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
6222 this->glink_, ORDER_TEXT, false);
6226 // Create a PLT entry for a global symbol.
6228 template<int size, bool big_endian>
6230 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
6234 if (gsym->type() == elfcpp::STT_GNU_IFUNC
6235 && gsym->can_use_relative_reloc(false))
6237 if (this->iplt_ == NULL)
6238 this->make_iplt_section(symtab, layout);
6239 this->iplt_->add_ifunc_entry(gsym);
6243 if (this->plt_ == NULL)
6244 this->make_plt_section(symtab, layout);
6245 this->plt_->add_entry(gsym);
6249 // Make a PLT entry for a local symbol.
6251 template<int size, bool big_endian>
6253 Target_powerpc<size, big_endian>::make_local_plt_entry(
6255 Sized_relobj_file<size, big_endian>* relobj,
6258 if (this->lplt_ == NULL)
6259 this->make_lplt_section(layout);
6260 this->lplt_->add_local_entry(relobj, r_sym);
6263 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
6265 template<int size, bool big_endian>
6267 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
6268 Symbol_table* symtab,
6270 Sized_relobj_file<size, big_endian>* relobj,
6273 if (this->iplt_ == NULL)
6274 this->make_iplt_section(symtab, layout);
6275 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
6278 // Return the number of entries in the PLT.
6280 template<int size, bool big_endian>
6282 Target_powerpc<size, big_endian>::plt_entry_count() const
6284 if (this->plt_ == NULL)
6286 return this->plt_->entry_count();
6289 // Create a GOT entry for local dynamic __tls_get_addr calls.
6291 template<int size, bool big_endian>
6293 Target_powerpc<size, big_endian>::tlsld_got_offset(
6294 Symbol_table* symtab,
6296 Sized_relobj_file<size, big_endian>* object)
6298 if (this->tlsld_got_offset_ == -1U)
6300 gold_assert(symtab != NULL && layout != NULL && object != NULL);
6301 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
6302 Output_data_got_powerpc<size, big_endian>* got
6303 = this->got_section(symtab, layout);
6304 unsigned int got_offset = got->add_constant_pair(0, 0);
6305 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
6307 this->tlsld_got_offset_ = got_offset;
6309 return this->tlsld_got_offset_;
6312 // Get the Reference_flags for a particular relocation.
6314 template<int size, bool big_endian>
6316 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
6317 unsigned int r_type,
6318 const Target_powerpc* target)
6324 case elfcpp::R_POWERPC_NONE:
6325 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6326 case elfcpp::R_POWERPC_GNU_VTENTRY:
6327 case elfcpp::R_PPC64_TOC:
6328 // No symbol reference.
6331 case elfcpp::R_PPC64_ADDR64:
6332 case elfcpp::R_PPC64_UADDR64:
6333 case elfcpp::R_POWERPC_ADDR32:
6334 case elfcpp::R_POWERPC_UADDR32:
6335 case elfcpp::R_POWERPC_ADDR16:
6336 case elfcpp::R_POWERPC_UADDR16:
6337 case elfcpp::R_POWERPC_ADDR16_LO:
6338 case elfcpp::R_POWERPC_ADDR16_HI:
6339 case elfcpp::R_POWERPC_ADDR16_HA:
6340 ref = Symbol::ABSOLUTE_REF;
6343 case elfcpp::R_POWERPC_ADDR24:
6344 case elfcpp::R_POWERPC_ADDR14:
6345 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6346 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6347 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
6350 case elfcpp::R_PPC64_REL64:
6351 case elfcpp::R_POWERPC_REL32:
6352 case elfcpp::R_PPC_LOCAL24PC:
6353 case elfcpp::R_POWERPC_REL16:
6354 case elfcpp::R_POWERPC_REL16_LO:
6355 case elfcpp::R_POWERPC_REL16_HI:
6356 case elfcpp::R_POWERPC_REL16_HA:
6357 ref = Symbol::RELATIVE_REF;
6360 case elfcpp::R_POWERPC_REL24:
6361 case elfcpp::R_PPC_PLTREL24:
6362 case elfcpp::R_POWERPC_REL14:
6363 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6364 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6365 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
6368 case elfcpp::R_POWERPC_GOT16:
6369 case elfcpp::R_POWERPC_GOT16_LO:
6370 case elfcpp::R_POWERPC_GOT16_HI:
6371 case elfcpp::R_POWERPC_GOT16_HA:
6372 case elfcpp::R_PPC64_GOT16_DS:
6373 case elfcpp::R_PPC64_GOT16_LO_DS:
6374 case elfcpp::R_PPC64_TOC16:
6375 case elfcpp::R_PPC64_TOC16_LO:
6376 case elfcpp::R_PPC64_TOC16_HI:
6377 case elfcpp::R_PPC64_TOC16_HA:
6378 case elfcpp::R_PPC64_TOC16_DS:
6379 case elfcpp::R_PPC64_TOC16_LO_DS:
6380 case elfcpp::R_POWERPC_PLT16_LO:
6381 case elfcpp::R_POWERPC_PLT16_HI:
6382 case elfcpp::R_POWERPC_PLT16_HA:
6383 case elfcpp::R_PPC64_PLT16_LO_DS:
6384 ref = Symbol::RELATIVE_REF;
6387 case elfcpp::R_POWERPC_GOT_TPREL16:
6388 case elfcpp::R_POWERPC_TLS:
6389 ref = Symbol::TLS_REF;
6392 case elfcpp::R_POWERPC_COPY:
6393 case elfcpp::R_POWERPC_GLOB_DAT:
6394 case elfcpp::R_POWERPC_JMP_SLOT:
6395 case elfcpp::R_POWERPC_RELATIVE:
6396 case elfcpp::R_POWERPC_DTPMOD:
6398 // Not expected. We will give an error later.
6402 if (size == 64 && target->abiversion() < 2)
6403 ref |= Symbol::FUNC_DESC_ABI;
6407 // Report an unsupported relocation against a local symbol.
6409 template<int size, bool big_endian>
6411 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
6412 Sized_relobj_file<size, big_endian>* object,
6413 unsigned int r_type)
6415 gold_error(_("%s: unsupported reloc %u against local symbol"),
6416 object->name().c_str(), r_type);
6419 // We are about to emit a dynamic relocation of type R_TYPE. If the
6420 // dynamic linker does not support it, issue an error.
6422 template<int size, bool big_endian>
6424 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
6425 unsigned int r_type)
6427 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
6429 // These are the relocation types supported by glibc for both 32-bit
6430 // and 64-bit powerpc.
6433 case elfcpp::R_POWERPC_NONE:
6434 case elfcpp::R_POWERPC_RELATIVE:
6435 case elfcpp::R_POWERPC_GLOB_DAT:
6436 case elfcpp::R_POWERPC_DTPMOD:
6437 case elfcpp::R_POWERPC_DTPREL:
6438 case elfcpp::R_POWERPC_TPREL:
6439 case elfcpp::R_POWERPC_JMP_SLOT:
6440 case elfcpp::R_POWERPC_COPY:
6441 case elfcpp::R_POWERPC_IRELATIVE:
6442 case elfcpp::R_POWERPC_ADDR32:
6443 case elfcpp::R_POWERPC_UADDR32:
6444 case elfcpp::R_POWERPC_ADDR24:
6445 case elfcpp::R_POWERPC_ADDR16:
6446 case elfcpp::R_POWERPC_UADDR16:
6447 case elfcpp::R_POWERPC_ADDR16_LO:
6448 case elfcpp::R_POWERPC_ADDR16_HI:
6449 case elfcpp::R_POWERPC_ADDR16_HA:
6450 case elfcpp::R_POWERPC_ADDR14:
6451 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6452 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6453 case elfcpp::R_POWERPC_REL32:
6454 case elfcpp::R_POWERPC_REL24:
6455 case elfcpp::R_POWERPC_TPREL16:
6456 case elfcpp::R_POWERPC_TPREL16_LO:
6457 case elfcpp::R_POWERPC_TPREL16_HI:
6458 case elfcpp::R_POWERPC_TPREL16_HA:
6469 // These are the relocation types supported only on 64-bit.
6470 case elfcpp::R_PPC64_ADDR64:
6471 case elfcpp::R_PPC64_UADDR64:
6472 case elfcpp::R_PPC64_JMP_IREL:
6473 case elfcpp::R_PPC64_ADDR16_DS:
6474 case elfcpp::R_PPC64_ADDR16_LO_DS:
6475 case elfcpp::R_PPC64_ADDR16_HIGH:
6476 case elfcpp::R_PPC64_ADDR16_HIGHA:
6477 case elfcpp::R_PPC64_ADDR16_HIGHER:
6478 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6479 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6480 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6481 case elfcpp::R_PPC64_REL64:
6482 case elfcpp::R_POWERPC_ADDR30:
6483 case elfcpp::R_PPC64_TPREL16_DS:
6484 case elfcpp::R_PPC64_TPREL16_LO_DS:
6485 case elfcpp::R_PPC64_TPREL16_HIGH:
6486 case elfcpp::R_PPC64_TPREL16_HIGHA:
6487 case elfcpp::R_PPC64_TPREL16_HIGHER:
6488 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6489 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6490 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6501 // These are the relocation types supported only on 32-bit.
6502 // ??? glibc ld.so doesn't need to support these.
6503 case elfcpp::R_POWERPC_DTPREL16:
6504 case elfcpp::R_POWERPC_DTPREL16_LO:
6505 case elfcpp::R_POWERPC_DTPREL16_HI:
6506 case elfcpp::R_POWERPC_DTPREL16_HA:
6514 // This prevents us from issuing more than one error per reloc
6515 // section. But we can still wind up issuing more than one
6516 // error per object file.
6517 if (this->issued_non_pic_error_)
6519 gold_assert(parameters->options().output_is_position_independent());
6520 object->error(_("requires unsupported dynamic reloc; "
6521 "recompile with -fPIC"));
6522 this->issued_non_pic_error_ = true;
6526 // Return whether we need to make a PLT entry for a relocation of the
6527 // given type against a STT_GNU_IFUNC symbol.
6529 template<int size, bool big_endian>
6531 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
6532 Target_powerpc<size, big_endian>* target,
6533 Sized_relobj_file<size, big_endian>* object,
6534 unsigned int r_type,
6537 // In non-pic code any reference will resolve to the plt call stub
6538 // for the ifunc symbol.
6539 if ((size == 32 || target->abiversion() >= 2)
6540 && !parameters->options().output_is_position_independent())
6545 // Word size refs from data sections are OK, but don't need a PLT entry.
6546 case elfcpp::R_POWERPC_ADDR32:
6547 case elfcpp::R_POWERPC_UADDR32:
6552 case elfcpp::R_PPC64_ADDR64:
6553 case elfcpp::R_PPC64_UADDR64:
6558 // GOT refs are good, but also don't need a PLT entry.
6559 case elfcpp::R_POWERPC_GOT16:
6560 case elfcpp::R_POWERPC_GOT16_LO:
6561 case elfcpp::R_POWERPC_GOT16_HI:
6562 case elfcpp::R_POWERPC_GOT16_HA:
6563 case elfcpp::R_PPC64_GOT16_DS:
6564 case elfcpp::R_PPC64_GOT16_LO_DS:
6567 // PLT relocs are OK and need a PLT entry.
6568 case elfcpp::R_POWERPC_PLT16_LO:
6569 case elfcpp::R_POWERPC_PLT16_HI:
6570 case elfcpp::R_POWERPC_PLT16_HA:
6571 case elfcpp::R_PPC64_PLT16_LO_DS:
6575 // Function calls are good, and these do need a PLT entry.
6576 case elfcpp::R_POWERPC_ADDR24:
6577 case elfcpp::R_POWERPC_ADDR14:
6578 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6579 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6580 case elfcpp::R_POWERPC_REL24:
6581 case elfcpp::R_PPC_PLTREL24:
6582 case elfcpp::R_POWERPC_REL14:
6583 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6584 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6591 // Anything else is a problem.
6592 // If we are building a static executable, the libc startup function
6593 // responsible for applying indirect function relocations is going
6594 // to complain about the reloc type.
6595 // If we are building a dynamic executable, we will have a text
6596 // relocation. The dynamic loader will set the text segment
6597 // writable and non-executable to apply text relocations. So we'll
6598 // segfault when trying to run the indirection function to resolve
6601 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
6602 object->name().c_str(), r_type);
6606 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6610 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
6612 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
6613 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
6614 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6615 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6616 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6617 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6618 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6619 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6620 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6621 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6622 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6623 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6624 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6625 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6626 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6627 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
6628 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
6629 /* Exclude lfqu by testing reloc. If relocs are ever
6630 defined for the reduced D field in psq_lu then those
6631 will need testing too. */
6632 && r_type != elfcpp::R_PPC64_TOC16_LO
6633 && r_type != elfcpp::R_POWERPC_GOT16_LO)
6634 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
6636 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
6637 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
6638 /* Exclude stfqu. psq_stu as above for psq_lu. */
6639 && r_type != elfcpp::R_PPC64_TOC16_LO
6640 && r_type != elfcpp::R_POWERPC_GOT16_LO)
6641 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
6642 && (insn & 1) == 0));
6645 // Scan a relocation for a local symbol.
6647 template<int size, bool big_endian>
6649 Target_powerpc<size, big_endian>::Scan::local(
6650 Symbol_table* symtab,
6652 Target_powerpc<size, big_endian>* target,
6653 Sized_relobj_file<size, big_endian>* object,
6654 unsigned int data_shndx,
6655 Output_section* output_section,
6656 const elfcpp::Rela<size, big_endian>& reloc,
6657 unsigned int r_type,
6658 const elfcpp::Sym<size, big_endian>& lsym,
6661 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
6663 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6664 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6666 this->expect_tls_get_addr_call();
6667 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6668 if (tls_type != tls::TLSOPT_NONE)
6669 this->skip_next_tls_get_addr_call();
6671 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6672 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6674 this->expect_tls_get_addr_call();
6675 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6676 if (tls_type != tls::TLSOPT_NONE)
6677 this->skip_next_tls_get_addr_call();
6680 Powerpc_relobj<size, big_endian>* ppc_object
6681 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6686 && data_shndx == ppc_object->opd_shndx()
6687 && r_type == elfcpp::R_PPC64_ADDR64)
6688 ppc_object->set_opd_discard(reloc.get_r_offset());
6692 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6693 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
6694 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
6696 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6697 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6698 r_type, r_sym, reloc.get_r_addend());
6699 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
6704 case elfcpp::R_POWERPC_NONE:
6705 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6706 case elfcpp::R_POWERPC_GNU_VTENTRY:
6707 case elfcpp::R_POWERPC_TLS:
6708 case elfcpp::R_PPC64_ENTRY:
6711 case elfcpp::R_PPC64_TOC:
6713 Output_data_got_powerpc<size, big_endian>* got
6714 = target->got_section(symtab, layout);
6715 if (parameters->options().output_is_position_independent())
6717 Address off = reloc.get_r_offset();
6719 && target->abiversion() < 2
6720 && data_shndx == ppc_object->opd_shndx()
6721 && ppc_object->get_opd_discard(off - 8))
6724 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6725 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
6726 rela_dyn->add_output_section_relative(got->output_section(),
6727 elfcpp::R_POWERPC_RELATIVE,
6729 object, data_shndx, off,
6730 symobj->toc_base_offset());
6735 case elfcpp::R_PPC64_ADDR64:
6736 case elfcpp::R_PPC64_UADDR64:
6737 case elfcpp::R_POWERPC_ADDR32:
6738 case elfcpp::R_POWERPC_UADDR32:
6739 case elfcpp::R_POWERPC_ADDR24:
6740 case elfcpp::R_POWERPC_ADDR16:
6741 case elfcpp::R_POWERPC_ADDR16_LO:
6742 case elfcpp::R_POWERPC_ADDR16_HI:
6743 case elfcpp::R_POWERPC_ADDR16_HA:
6744 case elfcpp::R_POWERPC_UADDR16:
6745 case elfcpp::R_PPC64_ADDR16_HIGH:
6746 case elfcpp::R_PPC64_ADDR16_HIGHA:
6747 case elfcpp::R_PPC64_ADDR16_HIGHER:
6748 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6749 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6750 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6751 case elfcpp::R_PPC64_ADDR16_DS:
6752 case elfcpp::R_PPC64_ADDR16_LO_DS:
6753 case elfcpp::R_POWERPC_ADDR14:
6754 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6755 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6756 // If building a shared library (or a position-independent
6757 // executable), we need to create a dynamic relocation for
6759 if (parameters->options().output_is_position_independent()
6760 || (size == 64 && is_ifunc && target->abiversion() < 2))
6762 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6764 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6765 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
6766 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
6768 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6769 : elfcpp::R_POWERPC_RELATIVE);
6770 rela_dyn->add_local_relative(object, r_sym, dynrel,
6771 output_section, data_shndx,
6772 reloc.get_r_offset(),
6773 reloc.get_r_addend(), false);
6775 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
6777 check_non_pic(object, r_type);
6778 rela_dyn->add_local(object, r_sym, r_type, output_section,
6779 data_shndx, reloc.get_r_offset(),
6780 reloc.get_r_addend());
6784 gold_assert(lsym.get_st_value() == 0);
6785 unsigned int shndx = lsym.get_st_shndx();
6787 shndx = object->adjust_sym_shndx(r_sym, shndx,
6790 object->error(_("section symbol %u has bad shndx %u"),
6793 rela_dyn->add_local_section(object, shndx, r_type,
6794 output_section, data_shndx,
6795 reloc.get_r_offset());
6800 case elfcpp::R_POWERPC_PLT16_LO:
6801 case elfcpp::R_POWERPC_PLT16_HI:
6802 case elfcpp::R_POWERPC_PLT16_HA:
6803 case elfcpp::R_PPC64_PLT16_LO_DS:
6806 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6807 target->make_local_plt_entry(layout, object, r_sym);
6811 case elfcpp::R_POWERPC_REL24:
6812 case elfcpp::R_PPC_PLTREL24:
6813 case elfcpp::R_PPC_LOCAL24PC:
6814 case elfcpp::R_POWERPC_REL14:
6815 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6816 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6819 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6820 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6821 r_type, r_sym, reloc.get_r_addend());
6825 case elfcpp::R_PPC64_TOCSAVE:
6826 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6827 // caller has already saved r2 and thus a plt call stub need not
6830 && target->mark_pltcall(ppc_object, data_shndx,
6831 reloc.get_r_offset() - 4, symtab))
6833 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6834 unsigned int shndx = lsym.get_st_shndx();
6836 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6838 object->error(_("tocsave symbol %u has bad shndx %u"),
6841 target->add_tocsave(ppc_object, shndx,
6842 lsym.get_st_value() + reloc.get_r_addend());
6846 case elfcpp::R_PPC64_REL64:
6847 case elfcpp::R_POWERPC_REL32:
6848 case elfcpp::R_POWERPC_REL16:
6849 case elfcpp::R_POWERPC_REL16_LO:
6850 case elfcpp::R_POWERPC_REL16_HI:
6851 case elfcpp::R_POWERPC_REL16_HA:
6852 case elfcpp::R_POWERPC_REL16DX_HA:
6853 case elfcpp::R_POWERPC_SECTOFF:
6854 case elfcpp::R_POWERPC_SECTOFF_LO:
6855 case elfcpp::R_POWERPC_SECTOFF_HI:
6856 case elfcpp::R_POWERPC_SECTOFF_HA:
6857 case elfcpp::R_PPC64_SECTOFF_DS:
6858 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6859 case elfcpp::R_POWERPC_TPREL16:
6860 case elfcpp::R_POWERPC_TPREL16_LO:
6861 case elfcpp::R_POWERPC_TPREL16_HI:
6862 case elfcpp::R_POWERPC_TPREL16_HA:
6863 case elfcpp::R_PPC64_TPREL16_DS:
6864 case elfcpp::R_PPC64_TPREL16_LO_DS:
6865 case elfcpp::R_PPC64_TPREL16_HIGH:
6866 case elfcpp::R_PPC64_TPREL16_HIGHA:
6867 case elfcpp::R_PPC64_TPREL16_HIGHER:
6868 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6869 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6870 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6871 case elfcpp::R_POWERPC_DTPREL16:
6872 case elfcpp::R_POWERPC_DTPREL16_LO:
6873 case elfcpp::R_POWERPC_DTPREL16_HI:
6874 case elfcpp::R_POWERPC_DTPREL16_HA:
6875 case elfcpp::R_PPC64_DTPREL16_DS:
6876 case elfcpp::R_PPC64_DTPREL16_LO_DS:
6877 case elfcpp::R_PPC64_DTPREL16_HIGH:
6878 case elfcpp::R_PPC64_DTPREL16_HIGHA:
6879 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6880 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6881 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6882 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6883 case elfcpp::R_PPC64_TLSGD:
6884 case elfcpp::R_PPC64_TLSLD:
6885 case elfcpp::R_PPC64_ADDR64_LOCAL:
6888 case elfcpp::R_POWERPC_GOT16:
6889 case elfcpp::R_POWERPC_GOT16_LO:
6890 case elfcpp::R_POWERPC_GOT16_HI:
6891 case elfcpp::R_POWERPC_GOT16_HA:
6892 case elfcpp::R_PPC64_GOT16_DS:
6893 case elfcpp::R_PPC64_GOT16_LO_DS:
6895 // The symbol requires a GOT entry.
6896 Output_data_got_powerpc<size, big_endian>* got
6897 = target->got_section(symtab, layout);
6898 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6900 if (!parameters->options().output_is_position_independent())
6903 && (size == 32 || target->abiversion() >= 2))
6904 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
6906 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
6908 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
6910 // If we are generating a shared object or a pie, this
6911 // symbol's GOT entry will be set by a dynamic relocation.
6913 off = got->add_constant(0);
6914 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
6916 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6918 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6919 : elfcpp::R_POWERPC_RELATIVE);
6920 rela_dyn->add_local_relative(object, r_sym, dynrel,
6921 got, off, 0, false);
6926 case elfcpp::R_PPC64_TOC16:
6927 case elfcpp::R_PPC64_TOC16_LO:
6928 case elfcpp::R_PPC64_TOC16_HI:
6929 case elfcpp::R_PPC64_TOC16_HA:
6930 case elfcpp::R_PPC64_TOC16_DS:
6931 case elfcpp::R_PPC64_TOC16_LO_DS:
6932 // We need a GOT section.
6933 target->got_section(symtab, layout);
6936 case elfcpp::R_POWERPC_GOT_TLSGD16:
6937 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6938 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6939 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6941 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6942 if (tls_type == tls::TLSOPT_NONE)
6944 Output_data_got_powerpc<size, big_endian>* got
6945 = target->got_section(symtab, layout);
6946 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6947 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6948 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
6949 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
6951 else if (tls_type == tls::TLSOPT_TO_LE)
6953 // no GOT relocs needed for Local Exec.
6960 case elfcpp::R_POWERPC_GOT_TLSLD16:
6961 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6962 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6963 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6965 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6966 if (tls_type == tls::TLSOPT_NONE)
6967 target->tlsld_got_offset(symtab, layout, object);
6968 else if (tls_type == tls::TLSOPT_TO_LE)
6970 // no GOT relocs needed for Local Exec.
6971 if (parameters->options().emit_relocs())
6973 Output_section* os = layout->tls_segment()->first_section();
6974 gold_assert(os != NULL);
6975 os->set_needs_symtab_index();
6983 case elfcpp::R_POWERPC_GOT_DTPREL16:
6984 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6985 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6986 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6988 Output_data_got_powerpc<size, big_endian>* got
6989 = target->got_section(symtab, layout);
6990 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6991 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
6995 case elfcpp::R_POWERPC_GOT_TPREL16:
6996 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6997 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6998 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7000 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
7001 if (tls_type == tls::TLSOPT_NONE)
7003 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7004 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
7006 Output_data_got_powerpc<size, big_endian>* got
7007 = target->got_section(symtab, layout);
7008 unsigned int off = got->add_constant(0);
7009 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
7011 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7012 rela_dyn->add_symbolless_local_addend(object, r_sym,
7013 elfcpp::R_POWERPC_TPREL,
7017 else if (tls_type == tls::TLSOPT_TO_LE)
7019 // no GOT relocs needed for Local Exec.
7027 unsupported_reloc_local(object, r_type);
7032 && parameters->options().toc_optimize())
7034 if (data_shndx == ppc_object->toc_shndx())
7037 if (r_type != elfcpp::R_PPC64_ADDR64
7038 || (is_ifunc && target->abiversion() < 2))
7040 else if (parameters->options().output_is_position_independent())
7046 unsigned int shndx = lsym.get_st_shndx();
7047 if (shndx >= elfcpp::SHN_LORESERVE
7048 && shndx != elfcpp::SHN_XINDEX)
7053 ppc_object->set_no_toc_opt(reloc.get_r_offset());
7056 enum {no_check, check_lo, check_ha} insn_check;
7060 insn_check = no_check;
7063 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7064 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7065 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7066 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7067 case elfcpp::R_POWERPC_GOT16_HA:
7068 case elfcpp::R_PPC64_TOC16_HA:
7069 insn_check = check_ha;
7072 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7073 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7074 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7075 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7076 case elfcpp::R_POWERPC_GOT16_LO:
7077 case elfcpp::R_PPC64_GOT16_LO_DS:
7078 case elfcpp::R_PPC64_TOC16_LO:
7079 case elfcpp::R_PPC64_TOC16_LO_DS:
7080 insn_check = check_lo;
7084 section_size_type slen;
7085 const unsigned char* view = NULL;
7086 if (insn_check != no_check)
7088 view = ppc_object->section_contents(data_shndx, &slen, false);
7089 section_size_type off =
7090 convert_to_section_size_type(reloc.get_r_offset()) & -4;
7093 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
7094 if (insn_check == check_lo
7095 ? !ok_lo_toc_insn(insn, r_type)
7096 : ((insn & ((0x3f << 26) | 0x1f << 16))
7097 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7099 ppc_object->set_no_toc_opt();
7100 gold_warning(_("%s: toc optimization is not supported "
7101 "for %#08x instruction"),
7102 ppc_object->name().c_str(), insn);
7111 case elfcpp::R_PPC64_TOC16:
7112 case elfcpp::R_PPC64_TOC16_LO:
7113 case elfcpp::R_PPC64_TOC16_HI:
7114 case elfcpp::R_PPC64_TOC16_HA:
7115 case elfcpp::R_PPC64_TOC16_DS:
7116 case elfcpp::R_PPC64_TOC16_LO_DS:
7117 unsigned int shndx = lsym.get_st_shndx();
7118 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7120 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7121 if (is_ordinary && shndx == ppc_object->toc_shndx())
7123 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
7124 if (dst_off < ppc_object->section_size(shndx))
7127 if (r_type == elfcpp::R_PPC64_TOC16_HA)
7129 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
7131 // Need to check that the insn is a ld
7133 view = ppc_object->section_contents(data_shndx,
7136 section_size_type off =
7137 (convert_to_section_size_type(reloc.get_r_offset())
7138 + (big_endian ? -2 : 3));
7140 && (view[off] & (0x3f << 2)) == 58u << 2)
7144 ppc_object->set_no_toc_opt(dst_off);
7155 case elfcpp::R_POWERPC_REL32:
7156 if (ppc_object->got2_shndx() != 0
7157 && parameters->options().output_is_position_independent())
7159 unsigned int shndx = lsym.get_st_shndx();
7160 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7162 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7163 if (is_ordinary && shndx == ppc_object->got2_shndx()
7164 && (ppc_object->section_flags(data_shndx)
7165 & elfcpp::SHF_EXECINSTR) != 0)
7166 gold_error(_("%s: unsupported -mbss-plt code"),
7167 ppc_object->name().c_str());
7177 case elfcpp::R_POWERPC_GOT_TLSLD16:
7178 case elfcpp::R_POWERPC_GOT_TLSGD16:
7179 case elfcpp::R_POWERPC_GOT_TPREL16:
7180 case elfcpp::R_POWERPC_GOT_DTPREL16:
7181 case elfcpp::R_POWERPC_GOT16:
7182 case elfcpp::R_PPC64_GOT16_DS:
7183 case elfcpp::R_PPC64_TOC16:
7184 case elfcpp::R_PPC64_TOC16_DS:
7185 ppc_object->set_has_small_toc_reloc();
7191 // Report an unsupported relocation against a global symbol.
7193 template<int size, bool big_endian>
7195 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
7196 Sized_relobj_file<size, big_endian>* object,
7197 unsigned int r_type,
7200 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7201 object->name().c_str(), r_type, gsym->demangled_name().c_str());
7204 // Scan a relocation for a global symbol.
7206 template<int size, bool big_endian>
7208 Target_powerpc<size, big_endian>::Scan::global(
7209 Symbol_table* symtab,
7211 Target_powerpc<size, big_endian>* target,
7212 Sized_relobj_file<size, big_endian>* object,
7213 unsigned int data_shndx,
7214 Output_section* output_section,
7215 const elfcpp::Rela<size, big_endian>& reloc,
7216 unsigned int r_type,
7219 if (this->maybe_skip_tls_get_addr_call(target, r_type, gsym)
7223 if (target->replace_tls_get_addr(gsym))
7224 // Change a __tls_get_addr reference to __tls_get_addr_opt
7225 // so dynamic relocs are emitted against the latter symbol.
7226 gsym = target->tls_get_addr_opt();
7228 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7229 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7231 this->expect_tls_get_addr_call();
7232 const bool final = gsym->final_value_is_known();
7233 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7234 if (tls_type != tls::TLSOPT_NONE)
7235 this->skip_next_tls_get_addr_call();
7237 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7238 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7240 this->expect_tls_get_addr_call();
7241 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7242 if (tls_type != tls::TLSOPT_NONE)
7243 this->skip_next_tls_get_addr_call();
7246 Powerpc_relobj<size, big_endian>* ppc_object
7247 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7249 // A STT_GNU_IFUNC symbol may require a PLT entry.
7250 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
7251 bool pushed_ifunc = false;
7252 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
7254 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7255 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7256 r_type, r_sym, reloc.get_r_addend());
7257 target->make_plt_entry(symtab, layout, gsym);
7258 pushed_ifunc = true;
7263 case elfcpp::R_POWERPC_NONE:
7264 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7265 case elfcpp::R_POWERPC_GNU_VTENTRY:
7266 case elfcpp::R_PPC_LOCAL24PC:
7267 case elfcpp::R_POWERPC_TLS:
7268 case elfcpp::R_PPC64_ENTRY:
7271 case elfcpp::R_PPC64_TOC:
7273 Output_data_got_powerpc<size, big_endian>* got
7274 = target->got_section(symtab, layout);
7275 if (parameters->options().output_is_position_independent())
7277 Address off = reloc.get_r_offset();
7279 && data_shndx == ppc_object->opd_shndx()
7280 && ppc_object->get_opd_discard(off - 8))
7283 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7284 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
7285 if (data_shndx != ppc_object->opd_shndx())
7286 symobj = static_cast
7287 <Powerpc_relobj<size, big_endian>*>(gsym->object());
7288 rela_dyn->add_output_section_relative(got->output_section(),
7289 elfcpp::R_POWERPC_RELATIVE,
7291 object, data_shndx, off,
7292 symobj->toc_base_offset());
7297 case elfcpp::R_PPC64_ADDR64:
7299 && target->abiversion() < 2
7300 && data_shndx == ppc_object->opd_shndx()
7301 && (gsym->is_defined_in_discarded_section()
7302 || gsym->object() != object))
7304 ppc_object->set_opd_discard(reloc.get_r_offset());
7308 case elfcpp::R_PPC64_UADDR64:
7309 case elfcpp::R_POWERPC_ADDR32:
7310 case elfcpp::R_POWERPC_UADDR32:
7311 case elfcpp::R_POWERPC_ADDR24:
7312 case elfcpp::R_POWERPC_ADDR16:
7313 case elfcpp::R_POWERPC_ADDR16_LO:
7314 case elfcpp::R_POWERPC_ADDR16_HI:
7315 case elfcpp::R_POWERPC_ADDR16_HA:
7316 case elfcpp::R_POWERPC_UADDR16:
7317 case elfcpp::R_PPC64_ADDR16_HIGH:
7318 case elfcpp::R_PPC64_ADDR16_HIGHA:
7319 case elfcpp::R_PPC64_ADDR16_HIGHER:
7320 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7321 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7322 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7323 case elfcpp::R_PPC64_ADDR16_DS:
7324 case elfcpp::R_PPC64_ADDR16_LO_DS:
7325 case elfcpp::R_POWERPC_ADDR14:
7326 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7327 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7329 // Make a PLT entry if necessary.
7330 if (gsym->needs_plt_entry())
7332 // Since this is not a PC-relative relocation, we may be
7333 // taking the address of a function. In that case we need to
7334 // set the entry in the dynamic symbol table to the address of
7335 // the PLT call stub.
7336 bool need_ifunc_plt = false;
7337 if ((size == 32 || target->abiversion() >= 2)
7338 && gsym->is_from_dynobj()
7339 && !parameters->options().output_is_position_independent())
7341 gsym->set_needs_dynsym_value();
7342 need_ifunc_plt = true;
7344 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
7346 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7347 target->push_branch(ppc_object, data_shndx,
7348 reloc.get_r_offset(), r_type, r_sym,
7349 reloc.get_r_addend());
7350 target->make_plt_entry(symtab, layout, gsym);
7353 // Make a dynamic relocation if necessary.
7354 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
7355 || (size == 64 && is_ifunc && target->abiversion() < 2))
7357 if (!parameters->options().output_is_position_independent()
7358 && gsym->may_need_copy_reloc())
7360 target->copy_reloc(symtab, layout, object,
7361 data_shndx, output_section, gsym, reloc);
7363 else if ((((size == 32
7364 && r_type == elfcpp::R_POWERPC_ADDR32)
7366 && r_type == elfcpp::R_PPC64_ADDR64
7367 && target->abiversion() >= 2))
7368 && gsym->can_use_relative_reloc(false)
7369 && !(gsym->visibility() == elfcpp::STV_PROTECTED
7370 && parameters->options().shared()))
7372 && r_type == elfcpp::R_PPC64_ADDR64
7373 && target->abiversion() < 2
7374 && (gsym->can_use_relative_reloc(false)
7375 || data_shndx == ppc_object->opd_shndx())))
7377 Reloc_section* rela_dyn
7378 = target->rela_dyn_section(symtab, layout, is_ifunc);
7379 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7380 : elfcpp::R_POWERPC_RELATIVE);
7381 rela_dyn->add_symbolless_global_addend(
7382 gsym, dynrel, output_section, object, data_shndx,
7383 reloc.get_r_offset(), reloc.get_r_addend());
7387 Reloc_section* rela_dyn
7388 = target->rela_dyn_section(symtab, layout, is_ifunc);
7389 check_non_pic(object, r_type);
7390 rela_dyn->add_global(gsym, r_type, output_section,
7392 reloc.get_r_offset(),
7393 reloc.get_r_addend());
7396 && parameters->options().toc_optimize()
7397 && data_shndx == ppc_object->toc_shndx())
7398 ppc_object->set_no_toc_opt(reloc.get_r_offset());
7404 case elfcpp::R_POWERPC_PLT16_LO:
7405 case elfcpp::R_POWERPC_PLT16_HI:
7406 case elfcpp::R_POWERPC_PLT16_HA:
7407 case elfcpp::R_PPC64_PLT16_LO_DS:
7409 target->make_plt_entry(symtab, layout, gsym);
7412 case elfcpp::R_PPC_PLTREL24:
7413 case elfcpp::R_POWERPC_REL24:
7416 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7417 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7418 r_type, r_sym, reloc.get_r_addend());
7419 if (gsym->needs_plt_entry()
7420 || (!gsym->final_value_is_known()
7421 && (gsym->is_undefined()
7422 || gsym->is_from_dynobj()
7423 || gsym->is_preemptible())))
7424 target->make_plt_entry(symtab, layout, gsym);
7428 case elfcpp::R_PPC64_REL64:
7429 case elfcpp::R_POWERPC_REL32:
7430 // Make a dynamic relocation if necessary.
7431 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
7433 if (!parameters->options().output_is_position_independent()
7434 && gsym->may_need_copy_reloc())
7436 target->copy_reloc(symtab, layout, object,
7437 data_shndx, output_section, gsym,
7442 Reloc_section* rela_dyn
7443 = target->rela_dyn_section(symtab, layout, is_ifunc);
7444 check_non_pic(object, r_type);
7445 rela_dyn->add_global(gsym, r_type, output_section, object,
7446 data_shndx, reloc.get_r_offset(),
7447 reloc.get_r_addend());
7452 case elfcpp::R_POWERPC_REL14:
7453 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7454 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7457 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7458 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7459 r_type, r_sym, reloc.get_r_addend());
7463 case elfcpp::R_PPC64_TOCSAVE:
7464 // R_PPC64_TOCSAVE follows a call instruction to indicate the
7465 // caller has already saved r2 and thus a plt call stub need not
7468 && target->mark_pltcall(ppc_object, data_shndx,
7469 reloc.get_r_offset() - 4, symtab))
7471 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7473 unsigned int shndx = gsym->shndx(&is_ordinary);
7475 object->error(_("tocsave symbol %u has bad shndx %u"),
7479 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
7480 target->add_tocsave(ppc_object, shndx,
7481 sym->value() + reloc.get_r_addend());
7486 case elfcpp::R_POWERPC_REL16:
7487 case elfcpp::R_POWERPC_REL16_LO:
7488 case elfcpp::R_POWERPC_REL16_HI:
7489 case elfcpp::R_POWERPC_REL16_HA:
7490 case elfcpp::R_POWERPC_REL16DX_HA:
7491 case elfcpp::R_POWERPC_SECTOFF:
7492 case elfcpp::R_POWERPC_SECTOFF_LO:
7493 case elfcpp::R_POWERPC_SECTOFF_HI:
7494 case elfcpp::R_POWERPC_SECTOFF_HA:
7495 case elfcpp::R_PPC64_SECTOFF_DS:
7496 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7497 case elfcpp::R_POWERPC_TPREL16:
7498 case elfcpp::R_POWERPC_TPREL16_LO:
7499 case elfcpp::R_POWERPC_TPREL16_HI:
7500 case elfcpp::R_POWERPC_TPREL16_HA:
7501 case elfcpp::R_PPC64_TPREL16_DS:
7502 case elfcpp::R_PPC64_TPREL16_LO_DS:
7503 case elfcpp::R_PPC64_TPREL16_HIGH:
7504 case elfcpp::R_PPC64_TPREL16_HIGHA:
7505 case elfcpp::R_PPC64_TPREL16_HIGHER:
7506 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7507 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7508 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7509 case elfcpp::R_POWERPC_DTPREL16:
7510 case elfcpp::R_POWERPC_DTPREL16_LO:
7511 case elfcpp::R_POWERPC_DTPREL16_HI:
7512 case elfcpp::R_POWERPC_DTPREL16_HA:
7513 case elfcpp::R_PPC64_DTPREL16_DS:
7514 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7515 case elfcpp::R_PPC64_DTPREL16_HIGH:
7516 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7517 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7518 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7519 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7520 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7521 case elfcpp::R_PPC64_TLSGD:
7522 case elfcpp::R_PPC64_TLSLD:
7523 case elfcpp::R_PPC64_ADDR64_LOCAL:
7526 case elfcpp::R_POWERPC_GOT16:
7527 case elfcpp::R_POWERPC_GOT16_LO:
7528 case elfcpp::R_POWERPC_GOT16_HI:
7529 case elfcpp::R_POWERPC_GOT16_HA:
7530 case elfcpp::R_PPC64_GOT16_DS:
7531 case elfcpp::R_PPC64_GOT16_LO_DS:
7533 // The symbol requires a GOT entry.
7534 Output_data_got_powerpc<size, big_endian>* got;
7536 got = target->got_section(symtab, layout);
7537 if (gsym->final_value_is_known())
7540 && (size == 32 || target->abiversion() >= 2))
7541 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
7543 got->add_global(gsym, GOT_TYPE_STANDARD);
7545 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
7547 // If we are generating a shared object or a pie, this
7548 // symbol's GOT entry will be set by a dynamic relocation.
7549 unsigned int off = got->add_constant(0);
7550 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
7552 Reloc_section* rela_dyn
7553 = target->rela_dyn_section(symtab, layout, is_ifunc);
7555 if (gsym->can_use_relative_reloc(false)
7557 || target->abiversion() >= 2)
7558 && gsym->visibility() == elfcpp::STV_PROTECTED
7559 && parameters->options().shared()))
7561 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7562 : elfcpp::R_POWERPC_RELATIVE);
7563 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
7567 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
7568 rela_dyn->add_global(gsym, dynrel, got, off, 0);
7574 case elfcpp::R_PPC64_TOC16:
7575 case elfcpp::R_PPC64_TOC16_LO:
7576 case elfcpp::R_PPC64_TOC16_HI:
7577 case elfcpp::R_PPC64_TOC16_HA:
7578 case elfcpp::R_PPC64_TOC16_DS:
7579 case elfcpp::R_PPC64_TOC16_LO_DS:
7580 // We need a GOT section.
7581 target->got_section(symtab, layout);
7584 case elfcpp::R_POWERPC_GOT_TLSGD16:
7585 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7586 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7587 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7589 const bool final = gsym->final_value_is_known();
7590 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7591 if (tls_type == tls::TLSOPT_NONE)
7593 Output_data_got_powerpc<size, big_endian>* got
7594 = target->got_section(symtab, layout);
7595 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7596 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
7597 elfcpp::R_POWERPC_DTPMOD,
7598 elfcpp::R_POWERPC_DTPREL);
7600 else if (tls_type == tls::TLSOPT_TO_IE)
7602 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
7604 Output_data_got_powerpc<size, big_endian>* got
7605 = target->got_section(symtab, layout);
7606 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7607 if (gsym->is_undefined()
7608 || gsym->is_from_dynobj())
7610 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
7611 elfcpp::R_POWERPC_TPREL);
7615 unsigned int off = got->add_constant(0);
7616 gsym->set_got_offset(GOT_TYPE_TPREL, off);
7617 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
7618 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
7623 else if (tls_type == tls::TLSOPT_TO_LE)
7625 // no GOT relocs needed for Local Exec.
7632 case elfcpp::R_POWERPC_GOT_TLSLD16:
7633 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7634 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7635 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7637 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7638 if (tls_type == tls::TLSOPT_NONE)
7639 target->tlsld_got_offset(symtab, layout, object);
7640 else if (tls_type == tls::TLSOPT_TO_LE)
7642 // no GOT relocs needed for Local Exec.
7643 if (parameters->options().emit_relocs())
7645 Output_section* os = layout->tls_segment()->first_section();
7646 gold_assert(os != NULL);
7647 os->set_needs_symtab_index();
7655 case elfcpp::R_POWERPC_GOT_DTPREL16:
7656 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7657 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7658 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7660 Output_data_got_powerpc<size, big_endian>* got
7661 = target->got_section(symtab, layout);
7662 if (!gsym->final_value_is_known()
7663 && (gsym->is_from_dynobj()
7664 || gsym->is_undefined()
7665 || gsym->is_preemptible()))
7666 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
7667 target->rela_dyn_section(layout),
7668 elfcpp::R_POWERPC_DTPREL);
7670 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
7674 case elfcpp::R_POWERPC_GOT_TPREL16:
7675 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7676 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7677 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7679 const bool final = gsym->final_value_is_known();
7680 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7681 if (tls_type == tls::TLSOPT_NONE)
7683 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
7685 Output_data_got_powerpc<size, big_endian>* got
7686 = target->got_section(symtab, layout);
7687 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7688 if (gsym->is_undefined()
7689 || gsym->is_from_dynobj())
7691 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
7692 elfcpp::R_POWERPC_TPREL);
7696 unsigned int off = got->add_constant(0);
7697 gsym->set_got_offset(GOT_TYPE_TPREL, off);
7698 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
7699 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
7704 else if (tls_type == tls::TLSOPT_TO_LE)
7706 // no GOT relocs needed for Local Exec.
7714 unsupported_reloc_global(object, r_type, gsym);
7719 && parameters->options().toc_optimize())
7721 if (data_shndx == ppc_object->toc_shndx())
7724 if (r_type != elfcpp::R_PPC64_ADDR64
7725 || (is_ifunc && target->abiversion() < 2))
7727 else if (parameters->options().output_is_position_independent()
7728 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
7731 ppc_object->set_no_toc_opt(reloc.get_r_offset());
7734 enum {no_check, check_lo, check_ha} insn_check;
7738 insn_check = no_check;
7741 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7742 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7743 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7744 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7745 case elfcpp::R_POWERPC_GOT16_HA:
7746 case elfcpp::R_PPC64_TOC16_HA:
7747 insn_check = check_ha;
7750 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7751 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7752 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7753 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7754 case elfcpp::R_POWERPC_GOT16_LO:
7755 case elfcpp::R_PPC64_GOT16_LO_DS:
7756 case elfcpp::R_PPC64_TOC16_LO:
7757 case elfcpp::R_PPC64_TOC16_LO_DS:
7758 insn_check = check_lo;
7762 section_size_type slen;
7763 const unsigned char* view = NULL;
7764 if (insn_check != no_check)
7766 view = ppc_object->section_contents(data_shndx, &slen, false);
7767 section_size_type off =
7768 convert_to_section_size_type(reloc.get_r_offset()) & -4;
7771 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
7772 if (insn_check == check_lo
7773 ? !ok_lo_toc_insn(insn, r_type)
7774 : ((insn & ((0x3f << 26) | 0x1f << 16))
7775 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7777 ppc_object->set_no_toc_opt();
7778 gold_warning(_("%s: toc optimization is not supported "
7779 "for %#08x instruction"),
7780 ppc_object->name().c_str(), insn);
7789 case elfcpp::R_PPC64_TOC16:
7790 case elfcpp::R_PPC64_TOC16_LO:
7791 case elfcpp::R_PPC64_TOC16_HI:
7792 case elfcpp::R_PPC64_TOC16_HA:
7793 case elfcpp::R_PPC64_TOC16_DS:
7794 case elfcpp::R_PPC64_TOC16_LO_DS:
7795 if (gsym->source() == Symbol::FROM_OBJECT
7796 && !gsym->object()->is_dynamic())
7798 Powerpc_relobj<size, big_endian>* sym_object
7799 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
7801 unsigned int shndx = gsym->shndx(&is_ordinary);
7802 if (shndx == sym_object->toc_shndx())
7804 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
7805 Address dst_off = sym->value() + reloc.get_r_addend();
7806 if (dst_off < sym_object->section_size(shndx))
7809 if (r_type == elfcpp::R_PPC64_TOC16_HA)
7811 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
7813 // Need to check that the insn is a ld
7815 view = ppc_object->section_contents(data_shndx,
7818 section_size_type off =
7819 (convert_to_section_size_type(reloc.get_r_offset())
7820 + (big_endian ? -2 : 3));
7822 && (view[off] & (0x3f << 2)) == (58u << 2))
7826 sym_object->set_no_toc_opt(dst_off);
7838 case elfcpp::R_PPC_LOCAL24PC:
7839 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7840 gold_error(_("%s: unsupported -mbss-plt code"),
7841 ppc_object->name().c_str());
7850 case elfcpp::R_POWERPC_GOT_TLSLD16:
7851 case elfcpp::R_POWERPC_GOT_TLSGD16:
7852 case elfcpp::R_POWERPC_GOT_TPREL16:
7853 case elfcpp::R_POWERPC_GOT_DTPREL16:
7854 case elfcpp::R_POWERPC_GOT16:
7855 case elfcpp::R_PPC64_GOT16_DS:
7856 case elfcpp::R_PPC64_TOC16:
7857 case elfcpp::R_PPC64_TOC16_DS:
7858 ppc_object->set_has_small_toc_reloc();
7864 // Process relocations for gc.
7866 template<int size, bool big_endian>
7868 Target_powerpc<size, big_endian>::gc_process_relocs(
7869 Symbol_table* symtab,
7871 Sized_relobj_file<size, big_endian>* object,
7872 unsigned int data_shndx,
7874 const unsigned char* prelocs,
7876 Output_section* output_section,
7877 bool needs_special_offset_handling,
7878 size_t local_symbol_count,
7879 const unsigned char* plocal_symbols)
7881 typedef Target_powerpc<size, big_endian> Powerpc;
7882 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7885 Powerpc_relobj<size, big_endian>* ppc_object
7886 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7888 ppc_object->set_opd_valid();
7889 if (size == 64 && data_shndx == ppc_object->opd_shndx())
7891 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
7892 for (p = ppc_object->access_from_map()->begin();
7893 p != ppc_object->access_from_map()->end();
7896 Address dst_off = p->first;
7897 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
7898 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
7899 for (s = p->second.begin(); s != p->second.end(); ++s)
7901 Relobj* src_obj = s->first;
7902 unsigned int src_indx = s->second;
7903 symtab->gc()->add_reference(src_obj, src_indx,
7904 ppc_object, dst_indx);
7908 ppc_object->access_from_map()->clear();
7909 ppc_object->process_gc_mark(symtab);
7910 // Don't look at .opd relocs as .opd will reference everything.
7914 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
7923 needs_special_offset_handling,
7928 // Handle target specific gc actions when adding a gc reference from
7929 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7930 // and DST_OFF. For powerpc64, this adds a referenc to the code
7931 // section of a function descriptor.
7933 template<int size, bool big_endian>
7935 Target_powerpc<size, big_endian>::do_gc_add_reference(
7936 Symbol_table* symtab,
7938 unsigned int src_shndx,
7940 unsigned int dst_shndx,
7941 Address dst_off) const
7943 if (size != 64 || dst_obj->is_dynamic())
7946 Powerpc_relobj<size, big_endian>* ppc_object
7947 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
7948 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
7950 if (ppc_object->opd_valid())
7952 dst_shndx = ppc_object->get_opd_ent(dst_off);
7953 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
7957 // If we haven't run scan_opd_relocs, we must delay
7958 // processing this function descriptor reference.
7959 ppc_object->add_reference(src_obj, src_shndx, dst_off);
7964 // Add any special sections for this symbol to the gc work list.
7965 // For powerpc64, this adds the code section of a function
7968 template<int size, bool big_endian>
7970 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
7971 Symbol_table* symtab,
7976 Powerpc_relobj<size, big_endian>* ppc_object
7977 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
7979 unsigned int shndx = sym->shndx(&is_ordinary);
7980 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
7982 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
7983 Address dst_off = gsym->value();
7984 if (ppc_object->opd_valid())
7986 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
7987 symtab->gc()->worklist().push_back(Section_id(ppc_object,
7991 ppc_object->add_gc_mark(dst_off);
7996 // For a symbol location in .opd, set LOC to the location of the
7999 template<int size, bool big_endian>
8001 Target_powerpc<size, big_endian>::do_function_location(
8002 Symbol_location* loc) const
8004 if (size == 64 && loc->shndx != 0)
8006 if (loc->object->is_dynamic())
8008 Powerpc_dynobj<size, big_endian>* ppc_object
8009 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
8010 if (loc->shndx == ppc_object->opd_shndx())
8013 Address off = loc->offset - ppc_object->opd_address();
8014 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
8015 loc->offset = dest_off;
8020 const Powerpc_relobj<size, big_endian>* ppc_object
8021 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
8022 if (loc->shndx == ppc_object->opd_shndx())
8025 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
8026 loc->offset = dest_off;
8032 // FNOFFSET in section SHNDX in OBJECT is the start of a function
8033 // compiled with -fsplit-stack. The function calls non-split-stack
8034 // code. Change the function to ensure it has enough stack space to
8035 // call some random function.
8037 template<int size, bool big_endian>
8039 Target_powerpc<size, big_endian>::do_calls_non_split(
8042 section_offset_type fnoffset,
8043 section_size_type fnsize,
8044 const unsigned char* prelocs,
8046 unsigned char* view,
8047 section_size_type view_size,
8049 std::string* to) const
8051 // 32-bit not supported.
8055 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
8056 prelocs, reloc_count, view, view_size,
8061 // The function always starts with
8062 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
8063 // addis %r12,%r1,-allocate@ha
8064 // addi %r12,%r12,-allocate@l
8066 // but note that the addis or addi may be replaced with a nop
8068 unsigned char *entry = view + fnoffset;
8069 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
8071 if ((insn & 0xffff0000) == addis_2_12)
8073 /* Skip ELFv2 global entry code. */
8075 insn = elfcpp::Swap<32, big_endian>::readval(entry);
8078 unsigned char *pinsn = entry;
8080 const uint32_t ld_private_ss = 0xe80d8fc0;
8081 if (insn == ld_private_ss)
8083 int32_t allocate = 0;
8087 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
8088 if ((insn & 0xffff0000) == addis_12_1)
8089 allocate += (insn & 0xffff) << 16;
8090 else if ((insn & 0xffff0000) == addi_12_1
8091 || (insn & 0xffff0000) == addi_12_12)
8092 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
8093 else if (insn != nop)
8096 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
8098 int extra = parameters->options().split_stack_adjust_size();
8100 if (allocate >= 0 || extra < 0)
8102 object->error(_("split-stack stack size overflow at "
8103 "section %u offset %0zx"),
8104 shndx, static_cast<size_t>(fnoffset));
8108 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
8109 if (insn != addis_12_1)
8111 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
8113 insn = addi_12_12 | (allocate & 0xffff);
8114 if (insn != addi_12_12)
8116 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
8122 insn = addi_12_1 | (allocate & 0xffff);
8123 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
8126 if (pinsn != entry + 12)
8127 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
8135 if (!object->has_no_split_stack())
8136 object->error(_("failed to match split-stack sequence at "
8137 "section %u offset %0zx"),
8138 shndx, static_cast<size_t>(fnoffset));
8142 // Scan relocations for a section.
8144 template<int size, bool big_endian>
8146 Target_powerpc<size, big_endian>::scan_relocs(
8147 Symbol_table* symtab,
8149 Sized_relobj_file<size, big_endian>* object,
8150 unsigned int data_shndx,
8151 unsigned int sh_type,
8152 const unsigned char* prelocs,
8154 Output_section* output_section,
8155 bool needs_special_offset_handling,
8156 size_t local_symbol_count,
8157 const unsigned char* plocal_symbols)
8159 typedef Target_powerpc<size, big_endian> Powerpc;
8160 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8163 if (!this->plt_localentry0_init_)
8165 bool plt_localentry0 = false;
8167 && this->abiversion() >= 2)
8169 if (parameters->options().user_set_plt_localentry())
8170 plt_localentry0 = parameters->options().plt_localentry();
8172 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
8173 gold_warning(_("--plt-localentry is especially dangerous without "
8174 "ld.so support to detect ABI violations"));
8176 this->plt_localentry0_ = plt_localentry0;
8177 this->plt_localentry0_init_ = true;
8180 if (sh_type == elfcpp::SHT_REL)
8182 gold_error(_("%s: unsupported REL reloc section"),
8183 object->name().c_str());
8187 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
8196 needs_special_offset_handling,
8201 // Functor class for processing the global symbol table.
8202 // Removes symbols defined on discarded opd entries.
8204 template<bool big_endian>
8205 class Global_symbol_visitor_opd
8208 Global_symbol_visitor_opd()
8212 operator()(Sized_symbol<64>* sym)
8214 if (sym->has_symtab_index()
8215 || sym->source() != Symbol::FROM_OBJECT
8216 || !sym->in_real_elf())
8219 if (sym->object()->is_dynamic())
8222 Powerpc_relobj<64, big_endian>* symobj
8223 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
8224 if (symobj->opd_shndx() == 0)
8228 unsigned int shndx = sym->shndx(&is_ordinary);
8229 if (shndx == symobj->opd_shndx()
8230 && symobj->get_opd_discard(sym->value()))
8232 sym->set_undefined();
8233 sym->set_visibility(elfcpp::STV_DEFAULT);
8234 sym->set_is_defined_in_discarded_section();
8235 sym->set_symtab_index(-1U);
8240 template<int size, bool big_endian>
8242 Target_powerpc<size, big_endian>::define_save_restore_funcs(
8244 Symbol_table* symtab)
8248 Output_data_save_res<size, big_endian>* savres
8249 = new Output_data_save_res<size, big_endian>(symtab);
8250 this->savres_section_ = savres;
8251 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8252 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
8253 savres, ORDER_TEXT, false);
8257 // Sort linker created .got section first (for the header), then input
8258 // sections belonging to files using small model code.
8260 template<bool big_endian>
8261 class Sort_toc_sections
8265 operator()(const Output_section::Input_section& is1,
8266 const Output_section::Input_section& is2) const
8268 if (!is1.is_input_section() && is2.is_input_section())
8271 = (is1.is_input_section()
8272 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
8273 ->has_small_toc_reloc()));
8275 = (is2.is_input_section()
8276 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
8277 ->has_small_toc_reloc()));
8278 return small1 && !small2;
8282 // Finalize the sections.
8284 template<int size, bool big_endian>
8286 Target_powerpc<size, big_endian>::do_finalize_sections(
8288 const Input_objects*,
8289 Symbol_table* symtab)
8291 if (parameters->doing_static_link())
8293 // At least some versions of glibc elf-init.o have a strong
8294 // reference to __rela_iplt marker syms. A weak ref would be
8296 if (this->iplt_ != NULL)
8298 Reloc_section* rel = this->iplt_->rel_plt();
8299 symtab->define_in_output_data("__rela_iplt_start", NULL,
8300 Symbol_table::PREDEFINED, rel, 0, 0,
8301 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8302 elfcpp::STV_HIDDEN, 0, false, true);
8303 symtab->define_in_output_data("__rela_iplt_end", NULL,
8304 Symbol_table::PREDEFINED, rel, 0, 0,
8305 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8306 elfcpp::STV_HIDDEN, 0, true, true);
8310 symtab->define_as_constant("__rela_iplt_start", NULL,
8311 Symbol_table::PREDEFINED, 0, 0,
8312 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8313 elfcpp::STV_HIDDEN, 0, true, false);
8314 symtab->define_as_constant("__rela_iplt_end", NULL,
8315 Symbol_table::PREDEFINED, 0, 0,
8316 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8317 elfcpp::STV_HIDDEN, 0, true, false);
8323 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
8324 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
8326 if (!parameters->options().relocatable())
8328 this->define_save_restore_funcs(layout, symtab);
8330 // Annoyingly, we need to make these sections now whether or
8331 // not we need them. If we delay until do_relax then we
8332 // need to mess with the relaxation machinery checkpointing.
8333 this->got_section(symtab, layout);
8334 this->make_brlt_section(layout);
8336 if (parameters->options().toc_sort())
8338 Output_section* os = this->got_->output_section();
8339 if (os != NULL && os->input_sections().size() > 1)
8340 std::stable_sort(os->input_sections().begin(),
8341 os->input_sections().end(),
8342 Sort_toc_sections<big_endian>());
8347 // Fill in some more dynamic tags.
8348 Output_data_dynamic* odyn = layout->dynamic_data();
8351 const Reloc_section* rel_plt = (this->plt_ == NULL
8353 : this->plt_->rel_plt());
8354 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
8355 this->rela_dyn_, true, size == 32);
8359 if (this->got_ != NULL)
8361 this->got_->finalize_data_size();
8362 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
8363 this->got_, this->got_->g_o_t());
8365 if (this->has_tls_get_addr_opt_)
8366 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
8370 if (this->glink_ != NULL)
8372 this->glink_->finalize_data_size();
8373 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
8375 (this->glink_->pltresolve_size()
8378 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
8379 odyn->add_constant(elfcpp::DT_PPC64_OPT,
8380 ((this->has_localentry0_
8381 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
8382 | (this->has_tls_get_addr_opt_
8383 ? elfcpp::PPC64_OPT_TLS : 0)));
8387 // Emit any relocs we saved in an attempt to avoid generating COPY
8389 if (this->copy_relocs_.any_saved_relocs())
8390 this->copy_relocs_.emit(this->rela_dyn_section(layout));
8393 // Emit any saved relocs, and mark toc entries using any of these
8394 // relocs as not optimizable.
8396 template<int sh_type, int size, bool big_endian>
8398 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
8399 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
8402 && parameters->options().toc_optimize())
8404 for (typename Copy_relocs<sh_type, size, big_endian>::
8405 Copy_reloc_entries::iterator p = this->entries_.begin();
8406 p != this->entries_.end();
8409 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
8412 // If the symbol is no longer defined in a dynamic object,
8413 // then we emitted a COPY relocation. If it is still
8414 // dynamic then we'll need dynamic relocations and thus
8415 // can't optimize toc entries.
8416 if (entry.sym_->is_from_dynobj())
8418 Powerpc_relobj<size, big_endian>* ppc_object
8419 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
8420 if (entry.shndx_ == ppc_object->toc_shndx())
8421 ppc_object->set_no_toc_opt(entry.address_);
8426 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
8429 // Return the value to use for a branch relocation.
8431 template<int size, bool big_endian>
8433 Target_powerpc<size, big_endian>::symval_for_branch(
8434 const Symbol_table* symtab,
8435 const Sized_symbol<size>* gsym,
8436 Powerpc_relobj<size, big_endian>* object,
8438 unsigned int *dest_shndx)
8440 if (size == 32 || this->abiversion() >= 2)
8444 // If the symbol is defined in an opd section, ie. is a function
8445 // descriptor, use the function descriptor code entry address
8446 Powerpc_relobj<size, big_endian>* symobj = object;
8448 && (gsym->source() != Symbol::FROM_OBJECT
8449 || gsym->object()->is_dynamic()))
8452 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
8453 unsigned int shndx = symobj->opd_shndx();
8456 Address opd_addr = symobj->get_output_section_offset(shndx);
8457 if (opd_addr == invalid_address)
8459 opd_addr += symobj->output_section_address(shndx);
8460 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
8463 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
8464 if (symtab->is_section_folded(symobj, *dest_shndx))
8467 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
8468 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
8469 *dest_shndx = folded.second;
8471 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
8472 if (sec_addr == invalid_address)
8475 sec_addr += symobj->output_section(*dest_shndx)->address();
8476 *value = sec_addr + sec_off;
8481 // Perform a relocation.
8483 template<int size, bool big_endian>
8485 Target_powerpc<size, big_endian>::Relocate::relocate(
8486 const Relocate_info<size, big_endian>* relinfo,
8488 Target_powerpc* target,
8491 const unsigned char* preloc,
8492 const Sized_symbol<size>* gsym,
8493 const Symbol_value<size>* psymval,
8494 unsigned char* view,
8496 section_size_type view_size)
8501 if (target->replace_tls_get_addr(gsym))
8502 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
8504 const elfcpp::Rela<size, big_endian> rela(preloc);
8505 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
8506 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
8508 case Track_tls::NOT_EXPECTED:
8509 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8510 _("__tls_get_addr call lacks marker reloc"));
8512 case Track_tls::EXPECTED:
8513 // We have already complained.
8515 case Track_tls::SKIP:
8517 case Track_tls::NORMAL:
8521 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
8522 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
8523 typedef typename elfcpp::Rela<size, big_endian> Reltype;
8524 // Offset from start of insn to d-field reloc.
8525 const int d_offset = big_endian ? 2 : 0;
8527 Powerpc_relobj<size, big_endian>* const object
8528 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
8530 bool has_stub_value = false;
8531 bool localentry0 = false;
8532 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
8535 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
8536 : object->local_has_plt_offset(r_sym));
8538 && !is_plt16_reloc<size>(r_type)
8539 && (!psymval->is_ifunc_symbol()
8540 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
8544 && target->abiversion() >= 2
8545 && !parameters->options().output_is_position_independent()
8546 && !is_branch_reloc(r_type))
8548 Address off = target->glink_section()->find_global_entry(gsym);
8549 if (off != invalid_address)
8551 value = target->glink_section()->global_entry_address() + off;
8552 has_stub_value = true;
8557 Stub_table<size, big_endian>* stub_table = NULL;
8558 if (target->stub_tables().size() == 1)
8559 stub_table = target->stub_tables()[0];
8560 if (stub_table == NULL
8563 && !parameters->options().output_is_position_independent()
8564 && !is_branch_reloc(r_type)))
8565 stub_table = object->stub_table(relinfo->data_shndx);
8566 if (stub_table == NULL)
8568 // This is a ref from a data section to an ifunc symbol,
8569 // or a non-branch reloc for which we always want to use
8570 // one set of stubs for resolving function addresses.
8571 if (target->stub_tables().size() != 0)
8572 stub_table = target->stub_tables()[0];
8574 if (stub_table != NULL)
8576 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
8578 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
8579 rela.get_r_addend());
8581 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
8582 rela.get_r_addend());
8585 value = stub_table->stub_address() + ent->off_;
8586 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
8587 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
8588 size_t reloc_count = shdr.get_sh_size() / reloc_size;
8591 && relnum + 1 < reloc_count)
8593 Reltype next_rela(preloc + reloc_size);
8594 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
8595 == elfcpp::R_PPC64_TOCSAVE
8596 && next_rela.get_r_offset() == rela.get_r_offset() + 4)
8599 localentry0 = ent->localentry0_;
8600 has_stub_value = true;
8604 // We don't care too much about bogus debug references to
8605 // non-local functions, but otherwise there had better be a plt
8606 // call stub or global entry stub as appropriate.
8607 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
8610 if (has_plt_offset && is_plt16_reloc<size>(r_type))
8612 const Output_data_plt_powerpc<size, big_endian>* plt;
8614 value = target->plt_off(gsym, &plt);
8616 value = target->plt_off(object, r_sym, &plt);
8617 value += plt->address();
8620 value -= (target->got_section()->output_section()->address()
8621 + object->toc_base_offset());
8622 else if (parameters->options().output_is_position_independent())
8624 if (rela.get_r_addend() >= 32768)
8626 unsigned int got2 = object->got2_shndx();
8627 value -= (object->get_output_section_offset(got2)
8628 + object->output_section(got2)->address()
8629 + rela.get_r_addend());
8632 value -= (target->got_section()->address()
8633 + target->got_section()->g_o_t());
8636 else if (r_type == elfcpp::R_POWERPC_GOT16
8637 || r_type == elfcpp::R_POWERPC_GOT16_LO
8638 || r_type == elfcpp::R_POWERPC_GOT16_HI
8639 || r_type == elfcpp::R_POWERPC_GOT16_HA
8640 || r_type == elfcpp::R_PPC64_GOT16_DS
8641 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
8645 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
8646 value = gsym->got_offset(GOT_TYPE_STANDARD);
8650 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
8651 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
8653 value -= target->got_section()->got_base_offset(object);
8655 else if (r_type == elfcpp::R_PPC64_TOC)
8657 value = (target->got_section()->output_section()->address()
8658 + object->toc_base_offset());
8660 else if (gsym != NULL
8661 && (r_type == elfcpp::R_POWERPC_REL24
8662 || r_type == elfcpp::R_PPC_PLTREL24)
8667 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
8668 Valtype* wv = reinterpret_cast<Valtype*>(view);
8669 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
8670 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
8672 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
8673 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
8676 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
8678 elfcpp::Swap<32, big_endian>::
8679 writeval(wv + 1, ld_2_1 + target->stk_toc());
8680 can_plt_call = true;
8685 // If we don't have a branch and link followed by a nop,
8686 // we can't go via the plt because there is no place to
8687 // put a toc restoring instruction.
8688 // Unless we know we won't be returning.
8689 if (strcmp(gsym->name(), "__libc_start_main") == 0)
8690 can_plt_call = true;
8694 // g++ as of 20130507 emits self-calls without a
8695 // following nop. This is arguably wrong since we have
8696 // conflicting information. On the one hand a global
8697 // symbol and on the other a local call sequence, but
8698 // don't error for this special case.
8699 // It isn't possible to cheaply verify we have exactly
8700 // such a call. Allow all calls to the same section.
8702 Address code = value;
8703 if (gsym->source() == Symbol::FROM_OBJECT
8704 && gsym->object() == object)
8706 unsigned int dest_shndx = 0;
8707 if (target->abiversion() < 2)
8709 Address addend = rela.get_r_addend();
8710 code = psymval->value(object, addend);
8711 target->symval_for_branch(relinfo->symtab, gsym, object,
8712 &code, &dest_shndx);
8715 if (dest_shndx == 0)
8716 dest_shndx = gsym->shndx(&is_ordinary);
8717 ok = dest_shndx == relinfo->data_shndx;
8721 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8722 _("call lacks nop, can't restore toc; "
8723 "recompile with -fPIC"));
8729 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8730 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
8731 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
8732 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
8734 // First instruction of a global dynamic sequence, arg setup insn.
8735 const bool final = gsym == NULL || gsym->final_value_is_known();
8736 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8737 enum Got_type got_type = GOT_TYPE_STANDARD;
8738 if (tls_type == tls::TLSOPT_NONE)
8739 got_type = GOT_TYPE_TLSGD;
8740 else if (tls_type == tls::TLSOPT_TO_IE)
8741 got_type = GOT_TYPE_TPREL;
8742 if (got_type != GOT_TYPE_STANDARD)
8746 gold_assert(gsym->has_got_offset(got_type));
8747 value = gsym->got_offset(got_type);
8751 gold_assert(object->local_has_got_offset(r_sym, got_type));
8752 value = object->local_got_offset(r_sym, got_type);
8754 value -= target->got_section()->got_base_offset(object);
8756 if (tls_type == tls::TLSOPT_TO_IE)
8758 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8759 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8761 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8762 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8763 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
8765 insn |= 32 << 26; // lwz
8767 insn |= 58 << 26; // ld
8768 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8770 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
8771 - elfcpp::R_POWERPC_GOT_TLSGD16);
8773 else if (tls_type == tls::TLSOPT_TO_LE)
8775 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8776 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8778 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8779 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8780 insn &= (1 << 26) - (1 << 21); // extract rt
8785 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8786 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8787 value = psymval->value(object, rela.get_r_addend());
8791 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8793 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8794 r_type = elfcpp::R_POWERPC_NONE;
8798 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8799 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
8800 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
8801 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
8803 // First instruction of a local dynamic sequence, arg setup insn.
8804 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8805 if (tls_type == tls::TLSOPT_NONE)
8807 value = target->tlsld_got_offset();
8808 value -= target->got_section()->got_base_offset(object);
8812 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8813 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8814 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
8816 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8817 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8818 insn &= (1 << 26) - (1 << 21); // extract rt
8823 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8824 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8829 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8831 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8832 r_type = elfcpp::R_POWERPC_NONE;
8836 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
8837 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
8838 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
8839 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
8841 // Accesses relative to a local dynamic sequence address,
8842 // no optimisation here.
8845 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
8846 value = gsym->got_offset(GOT_TYPE_DTPREL);
8850 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
8851 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
8853 value -= target->got_section()->got_base_offset(object);
8855 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8856 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
8857 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
8858 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
8860 // First instruction of initial exec sequence.
8861 const bool final = gsym == NULL || gsym->final_value_is_known();
8862 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8863 if (tls_type == tls::TLSOPT_NONE)
8867 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
8868 value = gsym->got_offset(GOT_TYPE_TPREL);
8872 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
8873 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
8875 value -= target->got_section()->got_base_offset(object);
8879 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8880 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8881 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
8883 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8884 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8885 insn &= (1 << 26) - (1 << 21); // extract rt from ld
8890 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8891 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8892 value = psymval->value(object, rela.get_r_addend());
8896 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8898 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8899 r_type = elfcpp::R_POWERPC_NONE;
8903 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8904 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8906 // Second instruction of a global dynamic sequence,
8907 // the __tls_get_addr call
8908 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
8909 const bool final = gsym == NULL || gsym->final_value_is_known();
8910 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8911 if (tls_type != tls::TLSOPT_NONE)
8913 if (tls_type == tls::TLSOPT_TO_IE)
8915 Insn* iview = reinterpret_cast<Insn*>(view);
8916 Insn insn = add_3_3_13;
8919 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8920 r_type = elfcpp::R_POWERPC_NONE;
8924 Insn* iview = reinterpret_cast<Insn*>(view);
8925 Insn insn = addi_3_3;
8926 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8927 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8929 value = psymval->value(object, rela.get_r_addend());
8931 this->skip_next_tls_get_addr_call();
8934 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8935 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8937 // Second instruction of a local dynamic sequence,
8938 // the __tls_get_addr call
8939 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
8940 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8941 if (tls_type == tls::TLSOPT_TO_LE)
8943 Insn* iview = reinterpret_cast<Insn*>(view);
8944 Insn insn = addi_3_3;
8945 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8946 this->skip_next_tls_get_addr_call();
8947 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8952 else if (r_type == elfcpp::R_POWERPC_TLS)
8954 // Second instruction of an initial exec sequence
8955 const bool final = gsym == NULL || gsym->final_value_is_known();
8956 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8957 if (tls_type == tls::TLSOPT_TO_LE)
8959 Insn* iview = reinterpret_cast<Insn*>(view);
8960 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8961 unsigned int reg = size == 32 ? 2 : 13;
8962 insn = at_tls_transform(insn, reg);
8963 gold_assert(insn != 0);
8964 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8965 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8967 value = psymval->value(object, rela.get_r_addend());
8970 else if (!has_stub_value)
8974 && (r_type == elfcpp::R_PPC_PLTREL24
8975 || r_type == elfcpp::R_POWERPC_PLT16_LO
8976 || r_type == elfcpp::R_POWERPC_PLT16_HI
8977 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
8978 addend = rela.get_r_addend();
8979 value = psymval->value(object, addend);
8980 if (size == 64 && is_branch_reloc(r_type))
8982 if (target->abiversion() >= 2)
8985 value += object->ppc64_local_entry_offset(gsym);
8987 value += object->ppc64_local_entry_offset(r_sym);
8991 unsigned int dest_shndx;
8992 target->symval_for_branch(relinfo->symtab, gsym, object,
8993 &value, &dest_shndx);
8996 Address max_branch_offset = max_branch_delta(r_type);
8997 if (max_branch_offset != 0
8998 && value - address + max_branch_offset >= 2 * max_branch_offset)
9000 Stub_table<size, big_endian>* stub_table
9001 = object->stub_table(relinfo->data_shndx);
9002 if (stub_table != NULL)
9004 Address off = stub_table->find_long_branch_entry(object, value);
9005 if (off != invalid_address)
9007 value = (stub_table->stub_address() + stub_table->plt_size()
9009 has_stub_value = true;
9017 case elfcpp::R_PPC64_REL64:
9018 case elfcpp::R_POWERPC_REL32:
9019 case elfcpp::R_POWERPC_REL24:
9020 case elfcpp::R_PPC_PLTREL24:
9021 case elfcpp::R_PPC_LOCAL24PC:
9022 case elfcpp::R_POWERPC_REL16:
9023 case elfcpp::R_POWERPC_REL16_LO:
9024 case elfcpp::R_POWERPC_REL16_HI:
9025 case elfcpp::R_POWERPC_REL16_HA:
9026 case elfcpp::R_POWERPC_REL16DX_HA:
9027 case elfcpp::R_POWERPC_REL14:
9028 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9029 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
9033 case elfcpp::R_PPC64_TOC16:
9034 case elfcpp::R_PPC64_TOC16_LO:
9035 case elfcpp::R_PPC64_TOC16_HI:
9036 case elfcpp::R_PPC64_TOC16_HA:
9037 case elfcpp::R_PPC64_TOC16_DS:
9038 case elfcpp::R_PPC64_TOC16_LO_DS:
9039 // Subtract the TOC base address.
9040 value -= (target->got_section()->output_section()->address()
9041 + object->toc_base_offset());
9044 case elfcpp::R_POWERPC_SECTOFF:
9045 case elfcpp::R_POWERPC_SECTOFF_LO:
9046 case elfcpp::R_POWERPC_SECTOFF_HI:
9047 case elfcpp::R_POWERPC_SECTOFF_HA:
9048 case elfcpp::R_PPC64_SECTOFF_DS:
9049 case elfcpp::R_PPC64_SECTOFF_LO_DS:
9051 value -= os->address();
9054 case elfcpp::R_PPC64_TPREL16_DS:
9055 case elfcpp::R_PPC64_TPREL16_LO_DS:
9056 case elfcpp::R_PPC64_TPREL16_HIGH:
9057 case elfcpp::R_PPC64_TPREL16_HIGHA:
9059 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
9062 case elfcpp::R_POWERPC_TPREL16:
9063 case elfcpp::R_POWERPC_TPREL16_LO:
9064 case elfcpp::R_POWERPC_TPREL16_HI:
9065 case elfcpp::R_POWERPC_TPREL16_HA:
9066 case elfcpp::R_POWERPC_TPREL:
9067 case elfcpp::R_PPC64_TPREL16_HIGHER:
9068 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9069 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9070 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9071 // tls symbol values are relative to tls_segment()->vaddr()
9075 case elfcpp::R_PPC64_DTPREL16_DS:
9076 case elfcpp::R_PPC64_DTPREL16_LO_DS:
9077 case elfcpp::R_PPC64_DTPREL16_HIGHER:
9078 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
9079 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
9080 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
9082 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
9083 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
9086 case elfcpp::R_POWERPC_DTPREL16:
9087 case elfcpp::R_POWERPC_DTPREL16_LO:
9088 case elfcpp::R_POWERPC_DTPREL16_HI:
9089 case elfcpp::R_POWERPC_DTPREL16_HA:
9090 case elfcpp::R_POWERPC_DTPREL:
9091 case elfcpp::R_PPC64_DTPREL16_HIGH:
9092 case elfcpp::R_PPC64_DTPREL16_HIGHA:
9093 // tls symbol values are relative to tls_segment()->vaddr()
9094 value -= dtp_offset;
9097 case elfcpp::R_PPC64_ADDR64_LOCAL:
9099 value += object->ppc64_local_entry_offset(gsym);
9101 value += object->ppc64_local_entry_offset(r_sym);
9108 Insn branch_bit = 0;
9111 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
9112 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9113 branch_bit = 1 << 21;
9115 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
9116 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
9118 Insn* iview = reinterpret_cast<Insn*>(view);
9119 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9122 if (this->is_isa_v2)
9124 // Set 'a' bit. This is 0b00010 in BO field for branch
9125 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
9126 // for branch on CTR insns (BO == 1a00t or 1a01t).
9127 if ((insn & (0x14 << 21)) == (0x04 << 21))
9129 else if ((insn & (0x14 << 21)) == (0x10 << 21))
9136 // Invert 'y' bit if not the default.
9137 if (static_cast<Signed_address>(value) < 0)
9140 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9144 case elfcpp::R_POWERPC_PLT16_HA:
9146 && !parameters->options().output_is_position_independent())
9148 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9149 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9151 // Convert addis to lis.
9152 if ((insn & (0x3f << 26)) == 15u << 26
9153 && (insn & (0x1f << 16)) != 0)
9155 insn &= ~(0x1f << 16);
9156 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9172 // Multi-instruction sequences that access the GOT/TOC can
9173 // be optimized, eg.
9174 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
9175 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
9177 // addis ra,r2,0; addi rb,ra,x@toc@l;
9178 // to nop; addi rb,r2,x@toc;
9179 // FIXME: the @got sequence shown above is not yet
9180 // optimized. Note that gcc as of 2017-01-07 doesn't use
9181 // the ELF @got relocs except for TLS, instead using the
9182 // PowerOpen variant of a compiler managed GOT (called TOC).
9183 // The PowerOpen TOC sequence equivalent to the first
9184 // example is optimized.
9185 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9186 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9187 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9188 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9189 case elfcpp::R_POWERPC_GOT16_HA:
9190 case elfcpp::R_PPC64_TOC16_HA:
9191 if (parameters->options().toc_optimize())
9193 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9194 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9195 if (r_type == elfcpp::R_PPC64_TOC16_HA
9196 && object->make_toc_relative(target, &value))
9198 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
9199 == ((15u << 26) | (2 << 16)));
9201 if (((insn & ((0x3f << 26) | 0x1f << 16))
9202 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
9203 && value + 0x8000 < 0x10000)
9205 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
9211 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9212 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9213 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9214 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9215 case elfcpp::R_POWERPC_GOT16_LO:
9216 case elfcpp::R_PPC64_GOT16_LO_DS:
9217 case elfcpp::R_PPC64_TOC16_LO:
9218 case elfcpp::R_PPC64_TOC16_LO_DS:
9219 if (parameters->options().toc_optimize())
9221 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9222 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9223 bool changed = false;
9224 if (r_type == elfcpp::R_PPC64_TOC16_LO_DS
9225 && object->make_toc_relative(target, &value))
9227 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
9228 insn ^= (14u << 26) ^ (58u << 26);
9229 r_type = elfcpp::R_PPC64_TOC16_LO;
9232 if (ok_lo_toc_insn(insn, r_type)
9233 && value + 0x8000 < 0x10000)
9235 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
9237 // Transform addic to addi when we change reg.
9238 insn &= ~((0x3f << 26) | (0x1f << 16));
9239 insn |= (14u << 26) | (2 << 16);
9243 insn &= ~(0x1f << 16);
9249 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9253 case elfcpp::R_POWERPC_TPREL16_HA:
9254 if (parameters->options().tls_optimize() && value + 0x8000 < 0x10000)
9256 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9257 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9258 if ((insn & ((0x3f << 26) | 0x1f << 16))
9259 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
9263 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
9269 case elfcpp::R_PPC64_TPREL16_LO_DS:
9271 // R_PPC_TLSGD, R_PPC_TLSLD
9274 case elfcpp::R_POWERPC_TPREL16_LO:
9275 if (parameters->options().tls_optimize() && value + 0x8000 < 0x10000)
9277 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9278 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
9279 insn &= ~(0x1f << 16);
9280 insn |= (size == 32 ? 2 : 13) << 16;
9281 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
9285 case elfcpp::R_PPC64_ENTRY:
9286 value = (target->got_section()->output_section()->address()
9287 + object->toc_base_offset());
9288 if (value + 0x80008000 <= 0xffffffff
9289 && !parameters->options().output_is_position_independent())
9291 Insn* iview = reinterpret_cast<Insn*>(view);
9292 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
9293 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
9295 if ((insn1 & ~0xfffc) == ld_2_12
9296 && insn2 == add_2_2_12)
9298 insn1 = lis_2 + ha(value);
9299 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
9300 insn2 = addi_2_2 + l(value);
9301 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
9308 if (value + 0x80008000 <= 0xffffffff)
9310 Insn* iview = reinterpret_cast<Insn*>(view);
9311 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
9312 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
9314 if ((insn1 & ~0xfffc) == ld_2_12
9315 && insn2 == add_2_2_12)
9317 insn1 = addis_2_12 + ha(value);
9318 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
9319 insn2 = addi_2_2 + l(value);
9320 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
9327 case elfcpp::R_POWERPC_REL16_LO:
9328 // If we are generating a non-PIC executable, edit
9329 // 0: addis 2,12,.TOC.-0b@ha
9330 // addi 2,2,.TOC.-0b@l
9331 // used by ELFv2 global entry points to set up r2, to
9334 // if .TOC. is in range. */
9335 if (value + address - 4 + 0x80008000 <= 0xffffffff
9338 && target->abiversion() >= 2
9339 && !parameters->options().output_is_position_independent()
9340 && rela.get_r_addend() == d_offset + 4
9342 && strcmp(gsym->name(), ".TOC.") == 0)
9344 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
9345 Reltype prev_rela(preloc - reloc_size);
9346 if ((prev_rela.get_r_info()
9347 == elfcpp::elf_r_info<size>(r_sym,
9348 elfcpp::R_POWERPC_REL16_HA))
9349 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
9350 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
9352 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9353 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
9354 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
9356 if ((insn1 & 0xffff0000) == addis_2_12
9357 && (insn2 & 0xffff0000) == addi_2_2)
9359 insn1 = lis_2 + ha(value + address - 4);
9360 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
9361 insn2 = addi_2_2 + l(value + address - 4);
9362 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
9365 relinfo->rr->set_strategy(relnum - 1,
9366 Relocatable_relocs::RELOC_SPECIAL);
9367 relinfo->rr->set_strategy(relnum,
9368 Relocatable_relocs::RELOC_SPECIAL);
9378 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
9379 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
9382 case elfcpp::R_POWERPC_ADDR32:
9383 case elfcpp::R_POWERPC_UADDR32:
9385 overflow = Reloc::CHECK_BITFIELD;
9388 case elfcpp::R_POWERPC_REL32:
9389 case elfcpp::R_POWERPC_REL16DX_HA:
9391 overflow = Reloc::CHECK_SIGNED;
9394 case elfcpp::R_POWERPC_UADDR16:
9395 overflow = Reloc::CHECK_BITFIELD;
9398 case elfcpp::R_POWERPC_ADDR16:
9399 // We really should have three separate relocations,
9400 // one for 16-bit data, one for insns with 16-bit signed fields,
9401 // and one for insns with 16-bit unsigned fields.
9402 overflow = Reloc::CHECK_BITFIELD;
9403 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
9404 overflow = Reloc::CHECK_LOW_INSN;
9407 case elfcpp::R_POWERPC_ADDR16_HI:
9408 case elfcpp::R_POWERPC_ADDR16_HA:
9409 case elfcpp::R_POWERPC_GOT16_HI:
9410 case elfcpp::R_POWERPC_GOT16_HA:
9411 case elfcpp::R_POWERPC_PLT16_HI:
9412 case elfcpp::R_POWERPC_PLT16_HA:
9413 case elfcpp::R_POWERPC_SECTOFF_HI:
9414 case elfcpp::R_POWERPC_SECTOFF_HA:
9415 case elfcpp::R_PPC64_TOC16_HI:
9416 case elfcpp::R_PPC64_TOC16_HA:
9417 case elfcpp::R_PPC64_PLTGOT16_HI:
9418 case elfcpp::R_PPC64_PLTGOT16_HA:
9419 case elfcpp::R_POWERPC_TPREL16_HI:
9420 case elfcpp::R_POWERPC_TPREL16_HA:
9421 case elfcpp::R_POWERPC_DTPREL16_HI:
9422 case elfcpp::R_POWERPC_DTPREL16_HA:
9423 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
9424 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9425 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9426 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9427 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9428 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9429 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9430 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9431 case elfcpp::R_POWERPC_REL16_HI:
9432 case elfcpp::R_POWERPC_REL16_HA:
9434 overflow = Reloc::CHECK_HIGH_INSN;
9437 case elfcpp::R_POWERPC_REL16:
9438 case elfcpp::R_PPC64_TOC16:
9439 case elfcpp::R_POWERPC_GOT16:
9440 case elfcpp::R_POWERPC_SECTOFF:
9441 case elfcpp::R_POWERPC_TPREL16:
9442 case elfcpp::R_POWERPC_DTPREL16:
9443 case elfcpp::R_POWERPC_GOT_TLSGD16:
9444 case elfcpp::R_POWERPC_GOT_TLSLD16:
9445 case elfcpp::R_POWERPC_GOT_TPREL16:
9446 case elfcpp::R_POWERPC_GOT_DTPREL16:
9447 overflow = Reloc::CHECK_LOW_INSN;
9450 case elfcpp::R_POWERPC_ADDR24:
9451 case elfcpp::R_POWERPC_ADDR14:
9452 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
9453 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
9454 case elfcpp::R_PPC64_ADDR16_DS:
9455 case elfcpp::R_POWERPC_REL24:
9456 case elfcpp::R_PPC_PLTREL24:
9457 case elfcpp::R_PPC_LOCAL24PC:
9458 case elfcpp::R_PPC64_TPREL16_DS:
9459 case elfcpp::R_PPC64_DTPREL16_DS:
9460 case elfcpp::R_PPC64_TOC16_DS:
9461 case elfcpp::R_PPC64_GOT16_DS:
9462 case elfcpp::R_PPC64_SECTOFF_DS:
9463 case elfcpp::R_POWERPC_REL14:
9464 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9465 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
9466 overflow = Reloc::CHECK_SIGNED;
9470 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
9473 if (overflow == Reloc::CHECK_LOW_INSN
9474 || overflow == Reloc::CHECK_HIGH_INSN)
9476 insn = elfcpp::Swap<32, big_endian>::readval(iview);
9478 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
9479 overflow = Reloc::CHECK_BITFIELD;
9480 else if (overflow == Reloc::CHECK_LOW_INSN
9481 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
9482 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
9483 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
9484 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
9485 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
9486 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
9487 overflow = Reloc::CHECK_UNSIGNED;
9489 overflow = Reloc::CHECK_SIGNED;
9492 bool maybe_dq_reloc = false;
9493 typename Powerpc_relocate_functions<size, big_endian>::Status status
9494 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
9497 case elfcpp::R_POWERPC_NONE:
9498 case elfcpp::R_POWERPC_TLS:
9499 case elfcpp::R_POWERPC_GNU_VTINHERIT:
9500 case elfcpp::R_POWERPC_GNU_VTENTRY:
9503 case elfcpp::R_PPC64_ADDR64:
9504 case elfcpp::R_PPC64_REL64:
9505 case elfcpp::R_PPC64_TOC:
9506 case elfcpp::R_PPC64_ADDR64_LOCAL:
9507 Reloc::addr64(view, value);
9510 case elfcpp::R_POWERPC_TPREL:
9511 case elfcpp::R_POWERPC_DTPREL:
9513 Reloc::addr64(view, value);
9515 status = Reloc::addr32(view, value, overflow);
9518 case elfcpp::R_PPC64_UADDR64:
9519 Reloc::addr64_u(view, value);
9522 case elfcpp::R_POWERPC_ADDR32:
9523 status = Reloc::addr32(view, value, overflow);
9526 case elfcpp::R_POWERPC_REL32:
9527 case elfcpp::R_POWERPC_UADDR32:
9528 status = Reloc::addr32_u(view, value, overflow);
9531 case elfcpp::R_POWERPC_ADDR24:
9532 case elfcpp::R_POWERPC_REL24:
9533 case elfcpp::R_PPC_PLTREL24:
9534 case elfcpp::R_PPC_LOCAL24PC:
9535 status = Reloc::addr24(view, value, overflow);
9538 case elfcpp::R_POWERPC_GOT_DTPREL16:
9539 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9540 case elfcpp::R_POWERPC_GOT_TPREL16:
9541 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9544 // On ppc64 these are all ds form
9545 maybe_dq_reloc = true;
9549 case elfcpp::R_POWERPC_ADDR16:
9550 case elfcpp::R_POWERPC_REL16:
9551 case elfcpp::R_PPC64_TOC16:
9552 case elfcpp::R_POWERPC_GOT16:
9553 case elfcpp::R_POWERPC_SECTOFF:
9554 case elfcpp::R_POWERPC_TPREL16:
9555 case elfcpp::R_POWERPC_DTPREL16:
9556 case elfcpp::R_POWERPC_GOT_TLSGD16:
9557 case elfcpp::R_POWERPC_GOT_TLSLD16:
9558 case elfcpp::R_POWERPC_ADDR16_LO:
9559 case elfcpp::R_POWERPC_REL16_LO:
9560 case elfcpp::R_PPC64_TOC16_LO:
9561 case elfcpp::R_POWERPC_GOT16_LO:
9562 case elfcpp::R_POWERPC_PLT16_LO:
9563 case elfcpp::R_POWERPC_SECTOFF_LO:
9564 case elfcpp::R_POWERPC_TPREL16_LO:
9565 case elfcpp::R_POWERPC_DTPREL16_LO:
9566 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9567 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9569 status = Reloc::addr16(view, value, overflow);
9571 maybe_dq_reloc = true;
9574 case elfcpp::R_POWERPC_UADDR16:
9575 status = Reloc::addr16_u(view, value, overflow);
9578 case elfcpp::R_PPC64_ADDR16_HIGH:
9579 case elfcpp::R_PPC64_TPREL16_HIGH:
9580 case elfcpp::R_PPC64_DTPREL16_HIGH:
9582 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
9585 case elfcpp::R_POWERPC_ADDR16_HI:
9586 case elfcpp::R_POWERPC_REL16_HI:
9587 case elfcpp::R_PPC64_TOC16_HI:
9588 case elfcpp::R_POWERPC_GOT16_HI:
9589 case elfcpp::R_POWERPC_PLT16_HI:
9590 case elfcpp::R_POWERPC_SECTOFF_HI:
9591 case elfcpp::R_POWERPC_TPREL16_HI:
9592 case elfcpp::R_POWERPC_DTPREL16_HI:
9593 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
9594 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9595 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9596 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9597 Reloc::addr16_hi(view, value);
9600 case elfcpp::R_PPC64_ADDR16_HIGHA:
9601 case elfcpp::R_PPC64_TPREL16_HIGHA:
9602 case elfcpp::R_PPC64_DTPREL16_HIGHA:
9604 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
9607 case elfcpp::R_POWERPC_ADDR16_HA:
9608 case elfcpp::R_POWERPC_REL16_HA:
9609 case elfcpp::R_PPC64_TOC16_HA:
9610 case elfcpp::R_POWERPC_GOT16_HA:
9611 case elfcpp::R_POWERPC_PLT16_HA:
9612 case elfcpp::R_POWERPC_SECTOFF_HA:
9613 case elfcpp::R_POWERPC_TPREL16_HA:
9614 case elfcpp::R_POWERPC_DTPREL16_HA:
9615 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9616 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9617 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9618 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9619 Reloc::addr16_ha(view, value);
9622 case elfcpp::R_POWERPC_REL16DX_HA:
9623 status = Reloc::addr16dx_ha(view, value, overflow);
9626 case elfcpp::R_PPC64_DTPREL16_HIGHER:
9628 // R_PPC_EMB_NADDR16_LO
9631 case elfcpp::R_PPC64_ADDR16_HIGHER:
9632 case elfcpp::R_PPC64_TPREL16_HIGHER:
9633 Reloc::addr16_hi2(view, value);
9636 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
9638 // R_PPC_EMB_NADDR16_HI
9641 case elfcpp::R_PPC64_ADDR16_HIGHERA:
9642 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9643 Reloc::addr16_ha2(view, value);
9646 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
9648 // R_PPC_EMB_NADDR16_HA
9651 case elfcpp::R_PPC64_ADDR16_HIGHEST:
9652 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9653 Reloc::addr16_hi3(view, value);
9656 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
9661 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
9662 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9663 Reloc::addr16_ha3(view, value);
9666 case elfcpp::R_PPC64_DTPREL16_DS:
9667 case elfcpp::R_PPC64_DTPREL16_LO_DS:
9669 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
9672 case elfcpp::R_PPC64_TPREL16_DS:
9673 case elfcpp::R_PPC64_TPREL16_LO_DS:
9675 // R_PPC_TLSGD, R_PPC_TLSLD
9678 case elfcpp::R_PPC64_ADDR16_DS:
9679 case elfcpp::R_PPC64_ADDR16_LO_DS:
9680 case elfcpp::R_PPC64_TOC16_DS:
9681 case elfcpp::R_PPC64_TOC16_LO_DS:
9682 case elfcpp::R_PPC64_GOT16_DS:
9683 case elfcpp::R_PPC64_GOT16_LO_DS:
9684 case elfcpp::R_PPC64_PLT16_LO_DS:
9685 case elfcpp::R_PPC64_SECTOFF_DS:
9686 case elfcpp::R_PPC64_SECTOFF_LO_DS:
9687 maybe_dq_reloc = true;
9690 case elfcpp::R_POWERPC_ADDR14:
9691 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
9692 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
9693 case elfcpp::R_POWERPC_REL14:
9694 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9695 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
9696 status = Reloc::addr14(view, value, overflow);
9699 case elfcpp::R_POWERPC_COPY:
9700 case elfcpp::R_POWERPC_GLOB_DAT:
9701 case elfcpp::R_POWERPC_JMP_SLOT:
9702 case elfcpp::R_POWERPC_RELATIVE:
9703 case elfcpp::R_POWERPC_DTPMOD:
9704 case elfcpp::R_PPC64_JMP_IREL:
9705 case elfcpp::R_POWERPC_IRELATIVE:
9706 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9707 _("unexpected reloc %u in object file"),
9711 case elfcpp::R_PPC64_TOCSAVE:
9717 Symbol_location loc;
9718 loc.object = relinfo->object;
9719 loc.shndx = relinfo->data_shndx;
9720 loc.offset = rela.get_r_offset();
9721 Tocsave_loc::const_iterator p = target->tocsave_loc().find(loc);
9722 if (p != target->tocsave_loc().end())
9724 // If we've generated plt calls using this tocsave, then
9725 // the nop needs to be changed to save r2.
9726 Insn* iview = reinterpret_cast<Insn*>(view);
9727 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
9728 elfcpp::Swap<32, big_endian>::
9729 writeval(iview, std_2_1 + target->stk_toc());
9734 case elfcpp::R_PPC_EMB_SDA2I16:
9735 case elfcpp::R_PPC_EMB_SDA2REL:
9738 // R_PPC64_TLSGD, R_PPC64_TLSLD
9741 case elfcpp::R_POWERPC_PLT32:
9742 case elfcpp::R_POWERPC_PLTREL32:
9743 case elfcpp::R_PPC_SDAREL16:
9744 case elfcpp::R_POWERPC_ADDR30:
9745 case elfcpp::R_PPC64_PLT64:
9746 case elfcpp::R_PPC64_PLTREL64:
9747 case elfcpp::R_PPC64_PLTGOT16:
9748 case elfcpp::R_PPC64_PLTGOT16_LO:
9749 case elfcpp::R_PPC64_PLTGOT16_HI:
9750 case elfcpp::R_PPC64_PLTGOT16_HA:
9751 case elfcpp::R_PPC64_PLTGOT16_DS:
9752 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
9753 case elfcpp::R_PPC_EMB_RELSDA:
9754 case elfcpp::R_PPC_TOC16:
9757 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9758 _("unsupported reloc %u"),
9766 insn = elfcpp::Swap<32, big_endian>::readval(iview);
9768 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
9769 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
9770 && (insn & 3) == 1))
9771 status = Reloc::addr16_dq(view, value, overflow);
9773 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
9774 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
9775 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
9776 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
9777 status = Reloc::addr16_ds(view, value, overflow);
9779 status = Reloc::addr16(view, value, overflow);
9782 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
9785 && gsym->is_undefined()
9786 && is_branch_reloc(r_type))))
9788 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9789 _("relocation overflow"));
9791 gold_info(_("try relinking with a smaller --stub-group-size"));
9797 // Relocate section data.
9799 template<int size, bool big_endian>
9801 Target_powerpc<size, big_endian>::relocate_section(
9802 const Relocate_info<size, big_endian>* relinfo,
9803 unsigned int sh_type,
9804 const unsigned char* prelocs,
9806 Output_section* output_section,
9807 bool needs_special_offset_handling,
9808 unsigned char* view,
9810 section_size_type view_size,
9811 const Reloc_symbol_changes* reloc_symbol_changes)
9813 typedef Target_powerpc<size, big_endian> Powerpc;
9814 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
9815 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
9816 Powerpc_comdat_behavior;
9817 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9820 gold_assert(sh_type == elfcpp::SHT_RELA);
9822 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
9823 Powerpc_comdat_behavior, Classify_reloc>(
9829 needs_special_offset_handling,
9833 reloc_symbol_changes);
9836 template<int size, bool big_endian>
9837 class Powerpc_scan_relocatable_reloc
9840 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9841 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
9842 static const int sh_type = elfcpp::SHT_RELA;
9844 // Return the symbol referred to by the relocation.
9845 static inline unsigned int
9846 get_r_sym(const Reltype* reloc)
9847 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
9849 // Return the type of the relocation.
9850 static inline unsigned int
9851 get_r_type(const Reltype* reloc)
9852 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
9854 // Return the strategy to use for a local symbol which is not a
9855 // section symbol, given the relocation type.
9856 inline Relocatable_relocs::Reloc_strategy
9857 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
9859 if (r_type == 0 && r_sym == 0)
9860 return Relocatable_relocs::RELOC_DISCARD;
9861 return Relocatable_relocs::RELOC_COPY;
9864 // Return the strategy to use for a local symbol which is a section
9865 // symbol, given the relocation type.
9866 inline Relocatable_relocs::Reloc_strategy
9867 local_section_strategy(unsigned int, Relobj*)
9869 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
9872 // Return the strategy to use for a global symbol, given the
9873 // relocation type, the object, and the symbol index.
9874 inline Relocatable_relocs::Reloc_strategy
9875 global_strategy(unsigned int r_type, Relobj*, unsigned int)
9878 && (r_type == elfcpp::R_PPC_PLTREL24
9879 || r_type == elfcpp::R_POWERPC_PLT16_LO
9880 || r_type == elfcpp::R_POWERPC_PLT16_HI
9881 || r_type == elfcpp::R_POWERPC_PLT16_HA))
9882 return Relocatable_relocs::RELOC_SPECIAL;
9883 return Relocatable_relocs::RELOC_COPY;
9887 // Scan the relocs during a relocatable link.
9889 template<int size, bool big_endian>
9891 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
9892 Symbol_table* symtab,
9894 Sized_relobj_file<size, big_endian>* object,
9895 unsigned int data_shndx,
9896 unsigned int sh_type,
9897 const unsigned char* prelocs,
9899 Output_section* output_section,
9900 bool needs_special_offset_handling,
9901 size_t local_symbol_count,
9902 const unsigned char* plocal_symbols,
9903 Relocatable_relocs* rr)
9905 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
9907 gold_assert(sh_type == elfcpp::SHT_RELA);
9909 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
9917 needs_special_offset_handling,
9923 // Scan the relocs for --emit-relocs.
9925 template<int size, bool big_endian>
9927 Target_powerpc<size, big_endian>::emit_relocs_scan(
9928 Symbol_table* symtab,
9930 Sized_relobj_file<size, big_endian>* object,
9931 unsigned int data_shndx,
9932 unsigned int sh_type,
9933 const unsigned char* prelocs,
9935 Output_section* output_section,
9936 bool needs_special_offset_handling,
9937 size_t local_symbol_count,
9938 const unsigned char* plocal_syms,
9939 Relocatable_relocs* rr)
9941 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9943 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
9944 Emit_relocs_strategy;
9946 gold_assert(sh_type == elfcpp::SHT_RELA);
9948 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
9956 needs_special_offset_handling,
9962 // Emit relocations for a section.
9963 // This is a modified version of the function by the same name in
9964 // target-reloc.h. Using relocate_special_relocatable for
9965 // R_PPC_PLTREL24 would require duplication of the entire body of the
9966 // loop, so we may as well duplicate the whole thing.
9968 template<int size, bool big_endian>
9970 Target_powerpc<size, big_endian>::relocate_relocs(
9971 const Relocate_info<size, big_endian>* relinfo,
9972 unsigned int sh_type,
9973 const unsigned char* prelocs,
9975 Output_section* output_section,
9976 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
9978 Address view_address,
9980 unsigned char* reloc_view,
9981 section_size_type reloc_view_size)
9983 gold_assert(sh_type == elfcpp::SHT_RELA);
9985 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9986 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
9987 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
9988 // Offset from start of insn to d-field reloc.
9989 const int d_offset = big_endian ? 2 : 0;
9991 Powerpc_relobj<size, big_endian>* const object
9992 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
9993 const unsigned int local_count = object->local_symbol_count();
9994 unsigned int got2_shndx = object->got2_shndx();
9995 Address got2_addend = 0;
9996 if (got2_shndx != 0)
9998 got2_addend = object->get_output_section_offset(got2_shndx);
9999 gold_assert(got2_addend != invalid_address);
10002 const bool relocatable = parameters->options().relocatable();
10004 unsigned char* pwrite = reloc_view;
10005 bool zap_next = false;
10006 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
10008 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
10009 if (strategy == Relocatable_relocs::RELOC_DISCARD)
10012 Reltype reloc(prelocs);
10013 Reltype_write reloc_write(pwrite);
10015 Address offset = reloc.get_r_offset();
10016 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
10017 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
10018 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
10019 const unsigned int orig_r_sym = r_sym;
10020 typename elfcpp::Elf_types<size>::Elf_Swxword addend
10021 = reloc.get_r_addend();
10022 const Symbol* gsym = NULL;
10026 // We could arrange to discard these and other relocs for
10027 // tls optimised sequences in the strategy methods, but for
10028 // now do as BFD ld does.
10029 r_type = elfcpp::R_POWERPC_NONE;
10033 // Get the new symbol index.
10034 Output_section* os = NULL;
10035 if (r_sym < local_count)
10039 case Relocatable_relocs::RELOC_COPY:
10040 case Relocatable_relocs::RELOC_SPECIAL:
10043 r_sym = object->symtab_index(r_sym);
10044 gold_assert(r_sym != -1U);
10048 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
10050 // We are adjusting a section symbol. We need to find
10051 // the symbol table index of the section symbol for
10052 // the output section corresponding to input section
10053 // in which this symbol is defined.
10054 gold_assert(r_sym < local_count);
10056 unsigned int shndx =
10057 object->local_symbol_input_shndx(r_sym, &is_ordinary);
10058 gold_assert(is_ordinary);
10059 os = object->output_section(shndx);
10060 gold_assert(os != NULL);
10061 gold_assert(os->needs_symtab_index());
10062 r_sym = os->symtab_index();
10067 gold_unreachable();
10072 gsym = object->global_symbol(r_sym);
10073 gold_assert(gsym != NULL);
10074 if (gsym->is_forwarder())
10075 gsym = relinfo->symtab->resolve_forwards(gsym);
10077 gold_assert(gsym->has_symtab_index());
10078 r_sym = gsym->symtab_index();
10081 // Get the new offset--the location in the output section where
10082 // this relocation should be applied.
10083 if (static_cast<Address>(offset_in_output_section) != invalid_address)
10084 offset += offset_in_output_section;
10087 section_offset_type sot_offset =
10088 convert_types<section_offset_type, Address>(offset);
10089 section_offset_type new_sot_offset =
10090 output_section->output_offset(object, relinfo->data_shndx,
10092 gold_assert(new_sot_offset != -1);
10093 offset = new_sot_offset;
10096 // In an object file, r_offset is an offset within the section.
10097 // In an executable or dynamic object, generated by
10098 // --emit-relocs, r_offset is an absolute address.
10101 offset += view_address;
10102 if (static_cast<Address>(offset_in_output_section) != invalid_address)
10103 offset -= offset_in_output_section;
10106 // Handle the reloc addend based on the strategy.
10107 if (strategy == Relocatable_relocs::RELOC_COPY)
10109 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
10111 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
10112 addend = psymval->value(object, addend);
10113 // In a relocatable link, the symbol value is relative to
10114 // the start of the output section. For a non-relocatable
10115 // link, we need to adjust the addend.
10118 gold_assert(os != NULL);
10119 addend -= os->address();
10122 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
10126 if (addend >= 32768)
10127 addend += got2_addend;
10129 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
10131 r_type = elfcpp::R_POWERPC_ADDR16_HA;
10132 addend -= d_offset;
10134 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
10136 r_type = elfcpp::R_POWERPC_ADDR16_LO;
10137 addend -= d_offset + 4;
10141 gold_unreachable();
10145 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10146 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10147 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
10148 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
10150 // First instruction of a global dynamic sequence,
10152 const bool final = gsym == NULL || gsym->final_value_is_known();
10153 switch (this->optimize_tls_gd(final))
10155 case tls::TLSOPT_TO_IE:
10156 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
10157 - elfcpp::R_POWERPC_GOT_TLSGD16);
10159 case tls::TLSOPT_TO_LE:
10160 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10161 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10162 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10165 r_type = elfcpp::R_POWERPC_NONE;
10166 offset -= d_offset;
10173 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10174 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
10175 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
10176 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
10178 // First instruction of a local dynamic sequence,
10180 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
10182 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10183 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
10185 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10186 const Output_section* os = relinfo->layout->tls_segment()
10188 gold_assert(os != NULL);
10189 gold_assert(os->needs_symtab_index());
10190 r_sym = os->symtab_index();
10191 addend = dtp_offset;
10195 r_type = elfcpp::R_POWERPC_NONE;
10196 offset -= d_offset;
10200 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10201 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
10202 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
10203 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
10205 // First instruction of initial exec sequence.
10206 const bool final = gsym == NULL || gsym->final_value_is_known();
10207 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
10209 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10210 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
10211 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10214 r_type = elfcpp::R_POWERPC_NONE;
10215 offset -= d_offset;
10219 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
10220 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
10222 // Second instruction of a global dynamic sequence,
10223 // the __tls_get_addr call
10224 const bool final = gsym == NULL || gsym->final_value_is_known();
10225 switch (this->optimize_tls_gd(final))
10227 case tls::TLSOPT_TO_IE:
10228 r_type = elfcpp::R_POWERPC_NONE;
10231 case tls::TLSOPT_TO_LE:
10232 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10233 offset += d_offset;
10240 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
10241 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
10243 // Second instruction of a local dynamic sequence,
10244 // the __tls_get_addr call
10245 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
10247 const Output_section* os = relinfo->layout->tls_segment()
10249 gold_assert(os != NULL);
10250 gold_assert(os->needs_symtab_index());
10251 r_sym = os->symtab_index();
10252 addend = dtp_offset;
10253 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10254 offset += d_offset;
10258 else if (r_type == elfcpp::R_POWERPC_TLS)
10260 // Second instruction of an initial exec sequence
10261 const bool final = gsym == NULL || gsym->final_value_is_known();
10262 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
10264 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10265 offset += d_offset;
10270 reloc_write.put_r_offset(offset);
10271 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
10272 reloc_write.put_r_addend(addend);
10274 pwrite += reloc_size;
10277 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
10278 == reloc_view_size);
10281 // Return the value to use for a dynamic symbol which requires special
10282 // treatment. This is how we support equality comparisons of function
10283 // pointers across shared library boundaries, as described in the
10284 // processor specific ABI supplement.
10286 template<int size, bool big_endian>
10288 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
10292 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
10293 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
10294 p != this->stub_tables_.end();
10297 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
10298 = (*p)->find_plt_call_entry(gsym);
10300 return (*p)->stub_address() + ent->off_;
10303 else if (this->abiversion() >= 2)
10305 Address off = this->glink_section()->find_global_entry(gsym);
10306 if (off != invalid_address)
10307 return this->glink_section()->global_entry_address() + off;
10309 gold_unreachable();
10312 // Return the PLT address to use for a local symbol.
10313 template<int size, bool big_endian>
10315 Target_powerpc<size, big_endian>::do_plt_address_for_local(
10316 const Relobj* object,
10317 unsigned int symndx) const
10321 const Sized_relobj<size, big_endian>* relobj
10322 = static_cast<const Sized_relobj<size, big_endian>*>(object);
10323 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
10324 p != this->stub_tables_.end();
10327 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
10328 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
10330 return (*p)->stub_address() + ent->off_;
10333 gold_unreachable();
10336 // Return the PLT address to use for a global symbol.
10337 template<int size, bool big_endian>
10339 Target_powerpc<size, big_endian>::do_plt_address_for_global(
10340 const Symbol* gsym) const
10344 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
10345 p != this->stub_tables_.end();
10348 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
10349 = (*p)->find_plt_call_entry(gsym);
10351 return (*p)->stub_address() + ent->off_;
10354 else if (this->abiversion() >= 2)
10356 Address off = this->glink_section()->find_global_entry(gsym);
10357 if (off != invalid_address)
10358 return this->glink_section()->global_entry_address() + off;
10360 gold_unreachable();
10363 // Return the offset to use for the GOT_INDX'th got entry which is
10364 // for a local tls symbol specified by OBJECT, SYMNDX.
10365 template<int size, bool big_endian>
10367 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
10368 const Relobj* object,
10369 unsigned int symndx,
10370 unsigned int got_indx) const
10372 const Powerpc_relobj<size, big_endian>* ppc_object
10373 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
10374 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
10376 for (Got_type got_type = GOT_TYPE_TLSGD;
10377 got_type <= GOT_TYPE_TPREL;
10378 got_type = Got_type(got_type + 1))
10379 if (ppc_object->local_has_got_offset(symndx, got_type))
10381 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
10382 if (got_type == GOT_TYPE_TLSGD)
10384 if (off == got_indx * (size / 8))
10386 if (got_type == GOT_TYPE_TPREL)
10389 return -dtp_offset;
10393 gold_unreachable();
10396 // Return the offset to use for the GOT_INDX'th got entry which is
10397 // for global tls symbol GSYM.
10398 template<int size, bool big_endian>
10400 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
10402 unsigned int got_indx) const
10404 if (gsym->type() == elfcpp::STT_TLS)
10406 for (Got_type got_type = GOT_TYPE_TLSGD;
10407 got_type <= GOT_TYPE_TPREL;
10408 got_type = Got_type(got_type + 1))
10409 if (gsym->has_got_offset(got_type))
10411 unsigned int off = gsym->got_offset(got_type);
10412 if (got_type == GOT_TYPE_TLSGD)
10414 if (off == got_indx * (size / 8))
10416 if (got_type == GOT_TYPE_TPREL)
10419 return -dtp_offset;
10423 gold_unreachable();
10426 // The selector for powerpc object files.
10428 template<int size, bool big_endian>
10429 class Target_selector_powerpc : public Target_selector
10432 Target_selector_powerpc()
10433 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
10436 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
10437 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
10439 ? (big_endian ? "elf64ppc" : "elf64lppc")
10440 : (big_endian ? "elf32ppc" : "elf32lppc")))
10444 do_instantiate_target()
10445 { return new Target_powerpc<size, big_endian>(); }
10448 Target_selector_powerpc<32, true> target_selector_ppc32;
10449 Target_selector_powerpc<32, false> target_selector_ppc32le;
10450 Target_selector_powerpc<64, true> target_selector_ppc64;
10451 Target_selector_powerpc<64, false> target_selector_ppc64le;
10453 // Instantiate these constants for -O0
10454 template<int size, bool big_endian>
10455 const typename Output_data_glink<size, big_endian>::Address
10456 Output_data_glink<size, big_endian>::invalid_address;
10457 template<int size, bool big_endian>
10458 const typename Stub_table<size, big_endian>::Address
10459 Stub_table<size, big_endian>::invalid_address;
10460 template<int size, bool big_endian>
10461 const typename Target_powerpc<size, big_endian>::Address
10462 Target_powerpc<size, big_endian>::invalid_address;
10464 } // End anonymous namespace.