1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright (C) 2008-2019 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"
44 #include "attributes.h"
51 template<int size, bool big_endian>
52 class Output_data_plt_powerpc;
54 template<int size, bool big_endian>
55 class Output_data_brlt_powerpc;
57 template<int size, bool big_endian>
58 class Output_data_got_powerpc;
60 template<int size, bool big_endian>
61 class Output_data_glink;
63 template<int size, bool big_endian>
66 template<int size, bool big_endian>
67 class Output_data_save_res;
69 template<int size, bool big_endian>
72 struct Stub_table_owner
75 : output_section(NULL), owner(NULL)
78 Output_section* output_section;
79 const Output_section::Input_section* owner;
83 inline bool is_branch_reloc(unsigned int);
86 inline bool is_plt16_reloc(unsigned int);
88 // Counter incremented on every Powerpc_relobj constructed.
89 static uint32_t object_id = 0;
91 template<int size, bool big_endian>
92 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
95 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
96 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
97 typedef Unordered_map<Address, Section_refs> Access_from;
99 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
100 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
101 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
102 uniq_(object_id++), special_(0), relatoc_(0), toc_(0),
103 has_small_toc_reloc_(false), opd_valid_(false),
104 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
105 access_from_map_(), has14_(), stub_table_index_(), st_other_(),
106 attributes_section_data_(NULL)
108 this->set_abiversion(0);
112 { delete this->attributes_section_data_; }
114 // Read the symbols then set up st_other vector.
116 do_read_symbols(Read_symbols_data*);
118 // Arrange to always relocate .toc first.
120 do_relocate_sections(
121 const Symbol_table* symtab, const Layout* layout,
122 const unsigned char* pshdrs, Output_file* of,
123 typename Sized_relobj_file<size, big_endian>::Views* pviews);
125 // The .toc section index.
132 // Mark .toc entry at OFF as not optimizable.
134 set_no_toc_opt(Address off)
136 if (this->no_toc_opt_.empty())
137 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
140 if (off < this->no_toc_opt_.size())
141 this->no_toc_opt_[off] = true;
144 // Mark the entire .toc as not optimizable.
148 this->no_toc_opt_.resize(1);
149 this->no_toc_opt_[0] = true;
152 // Return true if code using the .toc entry at OFF should not be edited.
154 no_toc_opt(Address off) const
156 if (this->no_toc_opt_.empty())
159 if (off >= this->no_toc_opt_.size())
161 return this->no_toc_opt_[off];
164 // The .got2 section shndx.
169 return this->special_;
174 // The .opd section shndx.
181 return this->special_;
184 // Init OPD entry arrays.
186 init_opd(size_t opd_size)
188 size_t count = this->opd_ent_ndx(opd_size);
189 this->opd_ent_.resize(count);
192 // Return section and offset of function entry for .opd + R_OFF.
194 get_opd_ent(Address r_off, Address* value = NULL) const
196 size_t ndx = this->opd_ent_ndx(r_off);
197 gold_assert(ndx < this->opd_ent_.size());
198 gold_assert(this->opd_ent_[ndx].shndx != 0);
200 *value = this->opd_ent_[ndx].off;
201 return this->opd_ent_[ndx].shndx;
204 // Set section and offset of function entry for .opd + R_OFF.
206 set_opd_ent(Address r_off, unsigned int shndx, Address value)
208 size_t ndx = this->opd_ent_ndx(r_off);
209 gold_assert(ndx < this->opd_ent_.size());
210 this->opd_ent_[ndx].shndx = shndx;
211 this->opd_ent_[ndx].off = value;
214 // Return discard flag for .opd + R_OFF.
216 get_opd_discard(Address r_off) const
218 size_t ndx = this->opd_ent_ndx(r_off);
219 gold_assert(ndx < this->opd_ent_.size());
220 return this->opd_ent_[ndx].discard;
223 // Set discard flag for .opd + R_OFF.
225 set_opd_discard(Address r_off)
227 size_t ndx = this->opd_ent_ndx(r_off);
228 gold_assert(ndx < this->opd_ent_.size());
229 this->opd_ent_[ndx].discard = true;
234 { return this->opd_valid_; }
238 { this->opd_valid_ = true; }
240 // Examine .rela.opd to build info about function entry points.
242 scan_opd_relocs(size_t reloc_count,
243 const unsigned char* prelocs,
244 const unsigned char* plocal_syms);
246 // Returns true if a code sequence loading a TOC entry can be
247 // converted into code calculating a TOC pointer relative offset.
249 make_toc_relative(Target_powerpc<size, big_endian>* target,
253 make_got_relative(Target_powerpc<size, big_endian>* target,
254 const Symbol_value<size>* psymval,
258 // Perform the Sized_relobj_file method, then set up opd info from
261 do_read_relocs(Read_relocs_data*);
264 do_find_special_sections(Read_symbols_data* sd);
266 // Adjust this local symbol value. Return false if the symbol
267 // should be discarded from the output file.
269 do_adjust_local_symbol(Symbol_value<size>* lv) const
271 if (size == 64 && this->opd_shndx() != 0)
274 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
276 if (this->get_opd_discard(lv->input_value()))
284 { return &this->access_from_map_; }
286 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
287 // section at DST_OFF.
289 add_reference(Relobj* src_obj,
290 unsigned int src_indx,
291 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
293 Section_id src_id(src_obj, src_indx);
294 this->access_from_map_[dst_off].insert(src_id);
297 // Add a reference to the code section specified by the .opd entry
300 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
302 size_t ndx = this->opd_ent_ndx(dst_off);
303 if (ndx >= this->opd_ent_.size())
304 this->opd_ent_.resize(ndx + 1);
305 this->opd_ent_[ndx].gc_mark = true;
309 process_gc_mark(Symbol_table* symtab)
311 for (size_t i = 0; i < this->opd_ent_.size(); i++)
312 if (this->opd_ent_[i].gc_mark)
314 unsigned int shndx = this->opd_ent_[i].shndx;
315 symtab->gc()->worklist().push_back(Section_id(this, shndx));
319 // Return offset in output GOT section that this object will use
320 // as a TOC pointer. Won't be just a constant with multi-toc support.
322 toc_base_offset() const
326 set_has_small_toc_reloc()
327 { has_small_toc_reloc_ = true; }
330 has_small_toc_reloc() const
331 { return has_small_toc_reloc_; }
334 set_has_14bit_branch(unsigned int shndx)
336 if (shndx >= this->has14_.size())
337 this->has14_.resize(shndx + 1);
338 this->has14_[shndx] = true;
342 has_14bit_branch(unsigned int shndx) const
343 { return shndx < this->has14_.size() && this->has14_[shndx]; }
346 set_stub_table(unsigned int shndx, unsigned int stub_index)
348 if (shndx >= this->stub_table_index_.size())
349 this->stub_table_index_.resize(shndx + 1, -1);
350 this->stub_table_index_[shndx] = stub_index;
353 Stub_table<size, big_endian>*
354 stub_table(unsigned int shndx)
356 if (shndx < this->stub_table_index_.size())
358 Target_powerpc<size, big_endian>* target
359 = static_cast<Target_powerpc<size, big_endian>*>(
360 parameters->sized_target<size, big_endian>());
361 unsigned int indx = this->stub_table_index_[shndx];
362 if (indx < target->stub_tables().size())
363 return target->stub_tables()[indx];
371 this->stub_table_index_.clear();
376 { return this->uniq_; }
380 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
382 // Set ABI version for input and output
384 set_abiversion(int ver);
387 st_other (unsigned int symndx) const
389 return this->st_other_[symndx];
393 ppc64_local_entry_offset(const Symbol* sym) const
394 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
397 ppc64_local_entry_offset(unsigned int symndx) const
398 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
401 ppc64_needs_toc(const Symbol* sym) const
402 { return sym->nonvis() > 1 << 3; }
405 ppc64_needs_toc(unsigned int symndx) const
406 { return this->st_other_[symndx] > 1 << 5; }
408 // The contents of the .gnu.attributes section if there is one.
409 const Attributes_section_data*
410 attributes_section_data() const
411 { return this->attributes_section_data_; }
422 // Return index into opd_ent_ array for .opd entry at OFF.
423 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
424 // apart when the language doesn't use the last 8-byte word, the
425 // environment pointer. Thus dividing the entry section offset by
426 // 16 will give an index into opd_ent_ that works for either layout
427 // of .opd. (It leaves some elements of the vector unused when .opd
428 // entries are spaced 24 bytes apart, but we don't know the spacing
429 // until relocations are processed, and in any case it is possible
430 // for an object to have some entries spaced 16 bytes apart and
431 // others 24 bytes apart.)
433 opd_ent_ndx(size_t off) const
436 // Per object unique identifier
439 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
440 unsigned int special_;
442 // For 64-bit the .rela.toc and .toc section shdnx.
443 unsigned int relatoc_;
446 // For 64-bit, whether this object uses small model relocs to access
448 bool has_small_toc_reloc_;
450 // Set at the start of gc_process_relocs, when we know opd_ent_
451 // vector is valid. The flag could be made atomic and set in
452 // do_read_relocs with memory_order_release and then tested with
453 // memory_order_acquire, potentially resulting in fewer entries in
458 elfcpp::Elf_Word e_flags_;
460 // For 64-bit, an array with one entry per 64-bit word in the .toc
461 // section, set if accesses using that word cannot be optimised.
462 std::vector<bool> no_toc_opt_;
464 // The first 8-byte word of an OPD entry gives the address of the
465 // entry point of the function. Relocatable object files have a
466 // relocation on this word. The following vector records the
467 // section and offset specified by these relocations.
468 std::vector<Opd_ent> opd_ent_;
470 // References made to this object's .opd section when running
471 // gc_process_relocs for another object, before the opd_ent_ vector
472 // is valid for this object.
473 Access_from access_from_map_;
475 // Whether input section has a 14-bit branch reloc.
476 std::vector<bool> has14_;
478 // The stub table to use for a given input section.
479 std::vector<unsigned int> stub_table_index_;
481 // ELF st_other field for local symbols.
482 std::vector<unsigned char> st_other_;
484 // Object attributes if there is a .gnu.attributes section.
485 Attributes_section_data* attributes_section_data_;
488 template<int size, bool big_endian>
489 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
492 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
494 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
495 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
496 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
497 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
498 attributes_section_data_(NULL)
500 this->set_abiversion(0);
504 { delete this->attributes_section_data_; }
506 // Call Sized_dynobj::do_read_symbols to read the symbols then
507 // read .opd from a dynamic object, filling in opd_ent_ vector,
509 do_read_symbols(Read_symbols_data*);
511 // The .opd section shndx.
515 return this->opd_shndx_;
518 // The .opd section address.
522 return this->opd_address_;
525 // Init OPD entry arrays.
527 init_opd(size_t opd_size)
529 size_t count = this->opd_ent_ndx(opd_size);
530 this->opd_ent_.resize(count);
533 // Return section and offset of function entry for .opd + R_OFF.
535 get_opd_ent(Address r_off, Address* value = NULL) const
537 size_t ndx = this->opd_ent_ndx(r_off);
538 gold_assert(ndx < this->opd_ent_.size());
539 gold_assert(this->opd_ent_[ndx].shndx != 0);
541 *value = this->opd_ent_[ndx].off;
542 return this->opd_ent_[ndx].shndx;
545 // Set section and offset of function entry for .opd + R_OFF.
547 set_opd_ent(Address r_off, unsigned int shndx, Address value)
549 size_t ndx = this->opd_ent_ndx(r_off);
550 gold_assert(ndx < this->opd_ent_.size());
551 this->opd_ent_[ndx].shndx = shndx;
552 this->opd_ent_[ndx].off = value;
557 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
559 // Set ABI version for input and output.
561 set_abiversion(int ver);
563 // The contents of the .gnu.attributes section if there is one.
564 const Attributes_section_data*
565 attributes_section_data() const
566 { return this->attributes_section_data_; }
569 // Used to specify extent of executable sections.
572 Sec_info(Address start_, Address len_, unsigned int shndx_)
573 : start(start_), len(len_), shndx(shndx_)
577 operator<(const Sec_info& that) const
578 { return this->start < that.start; }
591 // Return index into opd_ent_ array for .opd entry at OFF.
593 opd_ent_ndx(size_t off) const
596 // For 64-bit the .opd section shndx and address.
597 unsigned int opd_shndx_;
598 Address opd_address_;
601 elfcpp::Elf_Word e_flags_;
603 // The first 8-byte word of an OPD entry gives the address of the
604 // entry point of the function. Records the section and offset
605 // corresponding to the address. Note that in dynamic objects,
606 // offset is *not* relative to the section.
607 std::vector<Opd_ent> opd_ent_;
609 // Object attributes if there is a .gnu.attributes section.
610 Attributes_section_data* attributes_section_data_;
613 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
614 // base class will emit.
616 template<int sh_type, int size, bool big_endian>
617 class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
620 Powerpc_copy_relocs()
621 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
624 // Emit any saved relocations which turn out to be needed. This is
625 // called after all the relocs have been scanned.
627 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
630 template<int size, bool big_endian>
631 class Target_powerpc : public Sized_target<size, big_endian>
635 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
636 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
637 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
638 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
639 static const Address invalid_address = static_cast<Address>(0) - 1;
640 // Offset of tp and dtp pointers from start of TLS block.
641 static const Address tp_offset = 0x7000;
642 static const Address dtp_offset = 0x8000;
645 : Sized_target<size, big_endian>(&powerpc_info),
646 got_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL), brlt_section_(NULL),
647 glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
648 tlsld_got_offset_(-1U),
649 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
650 powerxx_stubs_(false), plt_thread_safe_(false), plt_localentry0_(false),
651 plt_localentry0_init_(false), has_localentry0_(false),
652 has_tls_get_addr_opt_(false),
653 relax_failed_(false), relax_fail_count_(0),
654 stub_group_size_(0), savres_section_(0),
655 tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
656 attributes_section_data_(NULL),
657 last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
661 // Process the relocations to determine unreferenced sections for
662 // garbage collection.
664 gc_process_relocs(Symbol_table* symtab,
666 Sized_relobj_file<size, big_endian>* object,
667 unsigned int data_shndx,
668 unsigned int sh_type,
669 const unsigned char* prelocs,
671 Output_section* output_section,
672 bool needs_special_offset_handling,
673 size_t local_symbol_count,
674 const unsigned char* plocal_symbols);
676 // Scan the relocations to look for symbol adjustments.
678 scan_relocs(Symbol_table* symtab,
680 Sized_relobj_file<size, big_endian>* object,
681 unsigned int data_shndx,
682 unsigned int sh_type,
683 const unsigned char* prelocs,
685 Output_section* output_section,
686 bool needs_special_offset_handling,
687 size_t local_symbol_count,
688 const unsigned char* plocal_symbols);
690 // Map input .toc section to output .got section.
692 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
694 if (size == 64 && strcmp(name, ".toc") == 0)
702 // Provide linker defined save/restore functions.
704 define_save_restore_funcs(Layout*, Symbol_table*);
706 // No stubs unless a final link.
709 { return !parameters->options().relocatable(); }
712 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
715 do_plt_fde_location(const Output_data*, unsigned char*,
716 uint64_t*, off_t*) const;
718 // Stash info about branches, for stub generation.
720 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
721 unsigned int data_shndx, Address r_offset,
722 unsigned int r_type, unsigned int r_sym, Address addend)
724 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
725 this->branch_info_.push_back(info);
726 if (r_type == elfcpp::R_POWERPC_REL14
727 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
728 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
729 ppc_object->set_has_14bit_branch(data_shndx);
732 // Return whether the last branch is a plt call, and if so, mark the
733 // branch as having an R_PPC64_TOCSAVE.
735 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
736 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
739 && !this->branch_info_.empty()
740 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
741 r_offset, this, symtab));
744 // Say the given location, that of a nop in a function prologue with
745 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
746 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
748 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
749 unsigned int shndx, Address offset)
752 loc.object = ppc_object;
755 this->tocsave_loc_.insert(loc);
762 return this->tocsave_loc_;
766 do_define_standard_symbols(Symbol_table*, Layout*);
768 // Finalize the sections.
770 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
772 // Return the value to use for a dynamic which requires special
775 do_dynsym_value(const Symbol*) const;
777 // Return the PLT address to use for a local symbol.
779 do_plt_address_for_local(const Relobj*, unsigned int) const;
781 // Return the PLT address to use for a global symbol.
783 do_plt_address_for_global(const Symbol*) const;
785 // Return the offset to use for the GOT_INDX'th got entry which is
786 // for a local tls symbol specified by OBJECT, SYMNDX.
788 do_tls_offset_for_local(const Relobj* object,
790 unsigned int got_indx) const;
792 // Return the offset to use for the GOT_INDX'th got entry which is
793 // for global tls symbol GSYM.
795 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
798 do_function_location(Symbol_location*) const;
801 do_can_check_for_function_pointers() const
804 // Adjust -fsplit-stack code which calls non-split-stack code.
806 do_calls_non_split(Relobj* object, unsigned int shndx,
807 section_offset_type fnoffset, section_size_type fnsize,
808 const unsigned char* prelocs, size_t reloc_count,
809 unsigned char* view, section_size_type view_size,
810 std::string* from, std::string* to) const;
812 // Relocate a section.
814 relocate_section(const Relocate_info<size, big_endian>*,
815 unsigned int sh_type,
816 const unsigned char* prelocs,
818 Output_section* output_section,
819 bool needs_special_offset_handling,
821 Address view_address,
822 section_size_type view_size,
823 const Reloc_symbol_changes*);
825 // Scan the relocs during a relocatable link.
827 scan_relocatable_relocs(Symbol_table* symtab,
829 Sized_relobj_file<size, big_endian>* object,
830 unsigned int data_shndx,
831 unsigned int sh_type,
832 const unsigned char* prelocs,
834 Output_section* output_section,
835 bool needs_special_offset_handling,
836 size_t local_symbol_count,
837 const unsigned char* plocal_symbols,
838 Relocatable_relocs*);
840 // Scan the relocs for --emit-relocs.
842 emit_relocs_scan(Symbol_table* symtab,
844 Sized_relobj_file<size, big_endian>* object,
845 unsigned int data_shndx,
846 unsigned int sh_type,
847 const unsigned char* prelocs,
849 Output_section* output_section,
850 bool needs_special_offset_handling,
851 size_t local_symbol_count,
852 const unsigned char* plocal_syms,
853 Relocatable_relocs* rr);
855 // Emit relocations for a section.
857 relocate_relocs(const Relocate_info<size, big_endian>*,
858 unsigned int sh_type,
859 const unsigned char* prelocs,
861 Output_section* output_section,
862 typename elfcpp::Elf_types<size>::Elf_Off
863 offset_in_output_section,
865 Address view_address,
867 unsigned char* reloc_view,
868 section_size_type reloc_view_size);
870 // Return whether SYM is defined by the ABI.
872 do_is_defined_by_abi(const Symbol* sym) const
874 return strcmp(sym->name(), "__tls_get_addr") == 0;
877 // Return the size of the GOT section.
881 gold_assert(this->got_ != NULL);
882 return this->got_->data_size();
885 // Get the PLT section.
886 const Output_data_plt_powerpc<size, big_endian>*
889 gold_assert(this->plt_ != NULL);
893 // Get the IPLT section.
894 const Output_data_plt_powerpc<size, big_endian>*
897 gold_assert(this->iplt_ != NULL);
901 // Get the LPLT section.
902 const Output_data_plt_powerpc<size, big_endian>*
908 // Return the plt offset and section for the given global sym.
910 plt_off(const Symbol* gsym,
911 const Output_data_plt_powerpc<size, big_endian>** sec) const
913 if (gsym->type() == elfcpp::STT_GNU_IFUNC
914 && gsym->can_use_relative_reloc(false))
915 *sec = this->iplt_section();
917 *sec = this->plt_section();
918 return gsym->plt_offset();
921 // Return the plt offset and section for the given local sym.
923 plt_off(const Sized_relobj_file<size, big_endian>* relobj,
924 unsigned int local_sym_index,
925 const Output_data_plt_powerpc<size, big_endian>** sec) const
927 const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
928 if (lsym->is_ifunc_symbol())
929 *sec = this->iplt_section();
931 *sec = this->lplt_section();
932 return relobj->local_plt_offset(local_sym_index);
935 // Get the .glink section.
936 const Output_data_glink<size, big_endian>*
937 glink_section() const
939 gold_assert(this->glink_ != NULL);
943 Output_data_glink<size, big_endian>*
946 gold_assert(this->glink_ != NULL);
950 bool has_glink() const
951 { return this->glink_ != NULL; }
953 // Get the GOT section.
954 const Output_data_got_powerpc<size, big_endian>*
957 gold_assert(this->got_ != NULL);
961 // Get the GOT section, creating it if necessary.
962 Output_data_got_powerpc<size, big_endian>*
963 got_section(Symbol_table*, Layout*);
966 do_make_elf_object(const std::string&, Input_file*, off_t,
967 const elfcpp::Ehdr<size, big_endian>&);
969 // Return the number of entries in the GOT.
971 got_entry_count() const
973 if (this->got_ == NULL)
975 return this->got_size() / (size / 8);
978 // Return the number of entries in the PLT.
980 plt_entry_count() const;
982 // Return the offset of the first non-reserved PLT entry.
984 first_plt_entry_offset() const
988 if (this->abiversion() >= 2)
993 // Return the size of each PLT entry.
995 plt_entry_size() const
999 if (this->abiversion() >= 2)
1004 Output_data_save_res<size, big_endian>*
1005 savres_section() const
1007 return this->savres_section_;
1010 // Add any special sections for this symbol to the gc work list.
1011 // For powerpc64, this adds the code section of a function
1014 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
1016 // Handle target specific gc actions when adding a gc reference from
1017 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1018 // and DST_OFF. For powerpc64, this adds a referenc to the code
1019 // section of a function descriptor.
1021 do_gc_add_reference(Symbol_table* symtab,
1023 unsigned int src_shndx,
1025 unsigned int dst_shndx,
1026 Address dst_off) const;
1028 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
1031 { return this->stub_tables_; }
1033 const Output_data_brlt_powerpc<size, big_endian>*
1034 brlt_section() const
1035 { return this->brlt_section_; }
1038 add_branch_lookup_table(Address to)
1040 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1041 this->branch_lookup_table_.insert(std::make_pair(to, off));
1045 find_branch_lookup_table(Address to)
1047 typename Branch_lookup_table::const_iterator p
1048 = this->branch_lookup_table_.find(to);
1049 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1053 write_branch_lookup_table(unsigned char *oview)
1055 for (typename Branch_lookup_table::const_iterator p
1056 = this->branch_lookup_table_.begin();
1057 p != this->branch_lookup_table_.end();
1060 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
1064 // Wrapper used after relax to define a local symbol in output data,
1065 // from the end if value < 0.
1067 define_local(Symbol_table* symtab, const char* name,
1068 Output_data* od, Address value, unsigned int symsize)
1071 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1072 od, value, symsize, elfcpp::STT_NOTYPE,
1073 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1074 static_cast<Signed_address>(value) < 0,
1076 // We are creating this symbol late, so need to fix up things
1077 // done early in Layout::finalize.
1078 sym->set_dynsym_index(-1U);
1082 powerxx_stubs() const
1083 { return this->powerxx_stubs_; }
1088 this->powerxx_stubs_ = true;
1092 plt_thread_safe() const
1093 { return this->plt_thread_safe_; }
1096 plt_localentry0() const
1097 { return this->plt_localentry0_; }
1100 set_has_localentry0()
1102 this->has_localentry0_ = true;
1106 is_elfv2_localentry0(const Symbol* gsym) const
1109 && this->abiversion() >= 2
1110 && this->plt_localentry0()
1111 && gsym->type() == elfcpp::STT_FUNC
1112 && gsym->is_defined()
1113 && gsym->nonvis() >> 3 == 0
1114 && !gsym->non_zero_localentry());
1118 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1119 unsigned int r_sym) const
1121 const Powerpc_relobj<size, big_endian>* ppc_object
1122 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1125 && this->abiversion() >= 2
1126 && this->plt_localentry0()
1127 && ppc_object->st_other(r_sym) >> 5 == 0)
1129 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1131 if (!psymval->is_ifunc_symbol()
1132 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1139 // Remember any symbols seen with non-zero localentry, even those
1140 // not providing a definition
1142 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1147 unsigned char st_other = sym.get_st_other();
1148 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1149 to->set_non_zero_localentry();
1151 // We haven't resolved anything, continue normal processing.
1157 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1160 set_abiversion(int ver)
1162 elfcpp::Elf_Word flags = this->processor_specific_flags();
1163 flags &= ~elfcpp::EF_PPC64_ABI;
1164 flags |= ver & elfcpp::EF_PPC64_ABI;
1165 this->set_processor_specific_flags(flags);
1169 tls_get_addr_opt() const
1170 { return this->tls_get_addr_opt_; }
1173 tls_get_addr() const
1174 { return this->tls_get_addr_; }
1176 // If optimizing __tls_get_addr calls, whether this is the
1177 // "__tls_get_addr" symbol.
1179 is_tls_get_addr_opt(const Symbol* gsym) const
1181 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1182 || gsym == this->tls_get_addr_opt_);
1186 replace_tls_get_addr(const Symbol* gsym) const
1187 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1190 set_has_tls_get_addr_opt()
1191 { this->has_tls_get_addr_opt_ = true; }
1193 // Offset to toc save stack slot
1196 { return this->abiversion() < 2 ? 40 : 24; }
1198 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1199 // so use the CR save slot. Used only by __tls_get_addr call stub,
1200 // relying on __tls_get_addr not saving CR itself.
1203 { return this->abiversion() < 2 ? 32 : 8; }
1205 // Merge object attributes from input object with those in the output.
1207 merge_object_attributes(const char*, const Attributes_section_data*);
1223 : tls_get_addr_state_(NOT_EXPECTED),
1224 relinfo_(NULL), relnum_(0), r_offset_(0)
1229 if (this->tls_get_addr_state_ != NOT_EXPECTED)
1236 if (this->relinfo_ != NULL)
1237 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1238 _("missing expected __tls_get_addr call"));
1242 expect_tls_get_addr_call(
1243 const Relocate_info<size, big_endian>* relinfo,
1247 this->tls_get_addr_state_ = EXPECTED;
1248 this->relinfo_ = relinfo;
1249 this->relnum_ = relnum;
1250 this->r_offset_ = r_offset;
1254 expect_tls_get_addr_call()
1255 { this->tls_get_addr_state_ = EXPECTED; }
1258 skip_next_tls_get_addr_call()
1259 {this->tls_get_addr_state_ = SKIP; }
1262 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1263 unsigned int r_type, const Symbol* gsym)
1266 = ((r_type == elfcpp::R_POWERPC_REL24
1267 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1268 || r_type == elfcpp::R_PPC_PLTREL24
1269 || is_plt16_reloc<size>(r_type)
1270 || r_type == elfcpp::R_PPC64_PLT_PCREL34
1271 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
1272 || r_type == elfcpp::R_POWERPC_PLTSEQ
1273 || r_type == elfcpp::R_POWERPC_PLTCALL
1274 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1275 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1277 && (gsym == target->tls_get_addr()
1278 || gsym == target->tls_get_addr_opt()));
1279 Tls_get_addr last_tls = this->tls_get_addr_state_;
1280 this->tls_get_addr_state_ = NOT_EXPECTED;
1281 if (is_tls_call && last_tls != EXPECTED)
1283 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1292 // What we're up to regarding calls to __tls_get_addr.
1293 // On powerpc, the branch and link insn making a call to
1294 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1295 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1296 // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
1297 // The marker relocation always comes first, and has the same
1298 // symbol as the reloc on the insn setting up the __tls_get_addr
1299 // argument. This ties the arg setup insn with the call insn,
1300 // allowing ld to safely optimize away the call. We check that
1301 // every call to __tls_get_addr has a marker relocation, and that
1302 // every marker relocation is on a call to __tls_get_addr.
1303 Tls_get_addr tls_get_addr_state_;
1304 // Info about the last reloc for error message.
1305 const Relocate_info<size, big_endian>* relinfo_;
1310 // The class which scans relocations.
1311 class Scan : protected Track_tls
1314 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1317 : Track_tls(), issued_non_pic_error_(false)
1321 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
1324 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1325 Sized_relobj_file<size, big_endian>* object,
1326 unsigned int data_shndx,
1327 Output_section* output_section,
1328 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1329 const elfcpp::Sym<size, big_endian>& lsym,
1333 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1334 Sized_relobj_file<size, big_endian>* object,
1335 unsigned int data_shndx,
1336 Output_section* output_section,
1337 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1341 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1343 Sized_relobj_file<size, big_endian>* relobj,
1346 const elfcpp::Rela<size, big_endian>& ,
1347 unsigned int r_type,
1348 const elfcpp::Sym<size, big_endian>&)
1350 // PowerPC64 .opd is not folded, so any identical function text
1351 // may be folded and we'll still keep function addresses distinct.
1352 // That means no reloc is of concern here.
1355 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1356 <Powerpc_relobj<size, big_endian>*>(relobj);
1357 if (ppcobj->abiversion() == 1)
1360 // For 32-bit and ELFv2, conservatively assume anything but calls to
1361 // function code might be taking the address of the function.
1362 return !is_branch_reloc<size>(r_type);
1366 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1368 Sized_relobj_file<size, big_endian>* relobj,
1371 const elfcpp::Rela<size, big_endian>& ,
1372 unsigned int r_type,
1378 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1379 <Powerpc_relobj<size, big_endian>*>(relobj);
1380 if (ppcobj->abiversion() == 1)
1383 return !is_branch_reloc<size>(r_type);
1387 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1388 Sized_relobj_file<size, big_endian>* object,
1389 unsigned int r_type, bool report_err);
1393 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1394 unsigned int r_type);
1397 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1398 unsigned int r_type, Symbol*);
1401 generate_tls_call(Symbol_table* symtab, Layout* layout,
1402 Target_powerpc* target);
1405 check_non_pic(Relobj*, unsigned int r_type);
1407 // Whether we have issued an error about a non-PIC compilation.
1408 bool issued_non_pic_error_;
1412 symval_for_branch(const Symbol_table* symtab,
1413 const Sized_symbol<size>* gsym,
1414 Powerpc_relobj<size, big_endian>* object,
1415 Address *value, unsigned int *dest_shndx);
1417 // The class which implements relocation.
1418 class Relocate : protected Track_tls
1421 // Use 'at' branch hints when true, 'y' when false.
1422 // FIXME maybe: set this with an option.
1423 static const bool is_isa_v2 = true;
1429 // Do a relocation. Return false if the caller should not issue
1430 // any warnings about this relocation.
1432 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1433 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1434 const Sized_symbol<size>*, const Symbol_value<size>*,
1435 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1439 class Relocate_comdat_behavior
1442 // Decide what the linker should do for relocations that refer to
1443 // discarded comdat sections.
1444 inline Comdat_behavior
1445 get(const char* name)
1447 gold::Default_comdat_behavior default_behavior;
1448 Comdat_behavior ret = default_behavior.get(name);
1449 if (ret == CB_ERROR)
1452 && (strcmp(name, ".fixup") == 0
1453 || strcmp(name, ".got2") == 0))
1456 && (strcmp(name, ".opd") == 0
1457 || strcmp(name, ".toc") == 0
1458 || strcmp(name, ".toc1") == 0))
1465 // Optimize the TLS relocation type based on what we know about the
1466 // symbol. IS_FINAL is true if the final address of this symbol is
1467 // known at link time.
1469 tls::Tls_optimization
1470 optimize_tls_gd(bool is_final)
1472 // If we are generating a shared library, then we can't do anything
1474 if (parameters->options().shared()
1475 || !parameters->options().tls_optimize())
1476 return tls::TLSOPT_NONE;
1479 return tls::TLSOPT_TO_IE;
1480 return tls::TLSOPT_TO_LE;
1483 tls::Tls_optimization
1486 if (parameters->options().shared()
1487 || !parameters->options().tls_optimize())
1488 return tls::TLSOPT_NONE;
1490 return tls::TLSOPT_TO_LE;
1493 tls::Tls_optimization
1494 optimize_tls_ie(bool is_final)
1497 || parameters->options().shared()
1498 || !parameters->options().tls_optimize())
1499 return tls::TLSOPT_NONE;
1501 return tls::TLSOPT_TO_LE;
1506 make_glink_section(Layout*);
1508 // Create the PLT section.
1510 make_plt_section(Symbol_table*, Layout*);
1513 make_iplt_section(Symbol_table*, Layout*);
1516 make_lplt_section(Layout*);
1519 make_brlt_section(Layout*);
1521 // Create a PLT entry for a global symbol.
1523 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1525 // Create a PLT entry for a local IFUNC symbol.
1527 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1528 Sized_relobj_file<size, big_endian>*,
1531 // Create a PLT entry for a local non-IFUNC symbol.
1533 make_local_plt_entry(Layout*,
1534 Sized_relobj_file<size, big_endian>*,
1538 // Create a GOT entry for local dynamic __tls_get_addr.
1540 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1541 Sized_relobj_file<size, big_endian>* object);
1544 tlsld_got_offset() const
1546 return this->tlsld_got_offset_;
1549 // Get the dynamic reloc section, creating it if necessary.
1551 rela_dyn_section(Layout*);
1553 // Similarly, but for ifunc symbols get the one for ifunc.
1555 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1557 // Copy a relocation against a global symbol.
1559 copy_reloc(Symbol_table* symtab, Layout* layout,
1560 Sized_relobj_file<size, big_endian>* object,
1561 unsigned int shndx, Output_section* output_section,
1562 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1564 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1565 this->copy_relocs_.copy_reloc(symtab, layout,
1566 symtab->get_sized_symbol<size>(sym),
1567 object, shndx, output_section,
1568 r_type, reloc.get_r_offset(),
1569 reloc.get_r_addend(),
1570 this->rela_dyn_section(layout));
1573 // Look over all the input sections, deciding where to place stubs.
1575 group_sections(Layout*, const Task*, bool);
1577 // Sort output sections by address.
1578 struct Sort_sections
1581 operator()(const Output_section* sec1, const Output_section* sec2)
1582 { return sec1->address() < sec2->address(); }
1588 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1589 unsigned int data_shndx,
1591 unsigned int r_type,
1594 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1595 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
1601 // Return whether this branch is going via a plt call stub, and if
1602 // so, mark it as having an R_PPC64_TOCSAVE.
1604 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1605 unsigned int shndx, Address offset,
1606 Target_powerpc* target, Symbol_table* symtab);
1608 // If this branch needs a plt call stub, or a long branch stub, make one.
1610 make_stub(Stub_table<size, big_endian>*,
1611 Stub_table<size, big_endian>*,
1612 Symbol_table*) const;
1615 // The branch location..
1616 Powerpc_relobj<size, big_endian>* object_;
1617 unsigned int shndx_;
1619 // ..and the branch type and destination.
1620 unsigned int r_type_ : 31;
1621 unsigned int tocsave_ : 1;
1622 unsigned int r_sym_;
1626 // Information about this specific target which we pass to the
1627 // general Target structure.
1628 static Target::Target_info powerpc_info;
1630 // The types of GOT entries needed for this platform.
1631 // These values are exposed to the ABI in an incremental link.
1632 // Do not renumber existing values without changing the version
1633 // number of the .gnu_incremental_inputs section.
1637 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1638 GOT_TYPE_DTPREL, // entry for @got@dtprel
1639 GOT_TYPE_TPREL // entry for @got@tprel
1643 Output_data_got_powerpc<size, big_endian>* got_;
1644 // The PLT section. This is a container for a table of addresses,
1645 // and their relocations. Each address in the PLT has a dynamic
1646 // relocation (R_*_JMP_SLOT) and each address will have a
1647 // corresponding entry in .glink for lazy resolution of the PLT.
1648 // ppc32 initialises the PLT to point at the .glink entry, while
1649 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1650 // linker adds a stub that loads the PLT entry into ctr then
1651 // branches to ctr. There may be more than one stub for each PLT
1652 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1653 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1654 Output_data_plt_powerpc<size, big_endian>* plt_;
1655 // The IPLT section. Like plt_, this is a container for a table of
1656 // addresses and their relocations, specifically for STT_GNU_IFUNC
1657 // functions that resolve locally (STT_GNU_IFUNC functions that
1658 // don't resolve locally go in PLT). Unlike plt_, these have no
1659 // entry in .glink for lazy resolution, and the relocation section
1660 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1661 // the relocation section may contain relocations against
1662 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1663 // relocation section will appear at the end of other dynamic
1664 // relocations, so that ld.so applies these relocations after other
1665 // dynamic relocations. In a static executable, the relocation
1666 // section is emitted and marked with __rela_iplt_start and
1667 // __rela_iplt_end symbols.
1668 Output_data_plt_powerpc<size, big_endian>* iplt_;
1669 // A PLT style section for local, non-ifunc symbols
1670 Output_data_plt_powerpc<size, big_endian>* lplt_;
1671 // Section holding long branch destinations.
1672 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1673 // The .glink section.
1674 Output_data_glink<size, big_endian>* glink_;
1675 // The dynamic reloc section.
1676 Reloc_section* rela_dyn_;
1677 // Relocs saved to avoid a COPY reloc.
1678 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1679 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1680 unsigned int tlsld_got_offset_;
1682 Stub_tables stub_tables_;
1683 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1684 Branch_lookup_table branch_lookup_table_;
1686 typedef std::vector<Branch_info> Branches;
1687 Branches branch_info_;
1688 Tocsave_loc tocsave_loc_;
1690 bool powerxx_stubs_;
1691 bool plt_thread_safe_;
1692 bool plt_localentry0_;
1693 bool plt_localentry0_init_;
1694 bool has_localentry0_;
1695 bool has_tls_get_addr_opt_;
1698 int relax_fail_count_;
1699 int32_t stub_group_size_;
1701 Output_data_save_res<size, big_endian> *savres_section_;
1703 // The "__tls_get_addr" symbol, if present
1704 Symbol* tls_get_addr_;
1705 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1706 Symbol* tls_get_addr_opt_;
1708 // Attributes in output.
1709 Attributes_section_data* attributes_section_data_;
1711 // Last input file to change various attribute tags
1712 const char* last_fp_;
1713 const char* last_ld_;
1714 const char* last_vec_;
1715 const char* last_struct_;
1719 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1722 true, // is_big_endian
1723 elfcpp::EM_PPC, // machine_code
1724 false, // has_make_symbol
1725 false, // has_resolve
1726 false, // has_code_fill
1727 true, // is_default_stack_executable
1728 false, // can_icf_inline_merge_sections
1730 "/usr/lib/ld.so.1", // dynamic_linker
1731 0x10000000, // default_text_segment_address
1732 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1733 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1734 false, // isolate_execinstr
1736 elfcpp::SHN_UNDEF, // small_common_shndx
1737 elfcpp::SHN_UNDEF, // large_common_shndx
1738 0, // small_common_section_flags
1739 0, // large_common_section_flags
1740 NULL, // attributes_section
1741 NULL, // attributes_vendor
1742 "_start", // entry_symbol_name
1743 32, // hash_entry_size
1744 elfcpp::SHT_PROGBITS, // unwind_section_type
1748 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1751 false, // is_big_endian
1752 elfcpp::EM_PPC, // machine_code
1753 false, // has_make_symbol
1754 false, // has_resolve
1755 false, // has_code_fill
1756 true, // is_default_stack_executable
1757 false, // can_icf_inline_merge_sections
1759 "/usr/lib/ld.so.1", // dynamic_linker
1760 0x10000000, // default_text_segment_address
1761 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1762 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1763 false, // isolate_execinstr
1765 elfcpp::SHN_UNDEF, // small_common_shndx
1766 elfcpp::SHN_UNDEF, // large_common_shndx
1767 0, // small_common_section_flags
1768 0, // large_common_section_flags
1769 NULL, // attributes_section
1770 NULL, // attributes_vendor
1771 "_start", // entry_symbol_name
1772 32, // hash_entry_size
1773 elfcpp::SHT_PROGBITS, // unwind_section_type
1777 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1780 true, // is_big_endian
1781 elfcpp::EM_PPC64, // machine_code
1782 false, // has_make_symbol
1783 true, // has_resolve
1784 false, // has_code_fill
1785 false, // is_default_stack_executable
1786 false, // can_icf_inline_merge_sections
1788 "/usr/lib/ld.so.1", // dynamic_linker
1789 0x10000000, // default_text_segment_address
1790 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1791 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1792 false, // isolate_execinstr
1794 elfcpp::SHN_UNDEF, // small_common_shndx
1795 elfcpp::SHN_UNDEF, // large_common_shndx
1796 0, // small_common_section_flags
1797 0, // large_common_section_flags
1798 NULL, // attributes_section
1799 NULL, // attributes_vendor
1800 "_start", // entry_symbol_name
1801 32, // hash_entry_size
1802 elfcpp::SHT_PROGBITS, // unwind_section_type
1806 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1809 false, // is_big_endian
1810 elfcpp::EM_PPC64, // machine_code
1811 false, // has_make_symbol
1812 true, // has_resolve
1813 false, // has_code_fill
1814 false, // is_default_stack_executable
1815 false, // can_icf_inline_merge_sections
1817 "/usr/lib/ld.so.1", // dynamic_linker
1818 0x10000000, // default_text_segment_address
1819 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1820 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1821 false, // isolate_execinstr
1823 elfcpp::SHN_UNDEF, // small_common_shndx
1824 elfcpp::SHN_UNDEF, // large_common_shndx
1825 0, // small_common_section_flags
1826 0, // large_common_section_flags
1827 NULL, // attributes_section
1828 NULL, // attributes_vendor
1829 "_start", // entry_symbol_name
1830 32, // hash_entry_size
1831 elfcpp::SHT_PROGBITS, // unwind_section_type
1836 is_branch_reloc(unsigned int r_type)
1838 return (r_type == elfcpp::R_POWERPC_REL24
1839 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1840 || r_type == elfcpp::R_PPC_PLTREL24
1841 || r_type == elfcpp::R_PPC_LOCAL24PC
1842 || r_type == elfcpp::R_POWERPC_REL14
1843 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1844 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1845 || r_type == elfcpp::R_POWERPC_ADDR24
1846 || r_type == elfcpp::R_POWERPC_ADDR14
1847 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1848 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1851 // Reloc resolves to plt entry.
1854 is_plt16_reloc(unsigned int r_type)
1856 return (r_type == elfcpp::R_POWERPC_PLT16_LO
1857 || r_type == elfcpp::R_POWERPC_PLT16_HI
1858 || r_type == elfcpp::R_POWERPC_PLT16_HA
1859 || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1862 // If INSN is an opcode that may be used with an @tls operand, return
1863 // the transformed insn for TLS optimisation, otherwise return 0. If
1864 // REG is non-zero only match an insn with RB or RA equal to REG.
1866 at_tls_transform(uint32_t insn, unsigned int reg)
1868 if ((insn & (0x3f << 26)) != 31 << 26)
1872 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1873 rtra = insn & ((1 << 26) - (1 << 16));
1874 else if (((insn >> 16) & 0x1f) == reg)
1875 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1879 if ((insn & (0x3ff << 1)) == 266 << 1)
1882 else if ((insn & (0x1f << 1)) == 23 << 1
1883 && ((insn & (0x1f << 6)) < 14 << 6
1884 || ((insn & (0x1f << 6)) >= 16 << 6
1885 && (insn & (0x1f << 6)) < 24 << 6)))
1886 // load and store indexed -> dform
1887 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1888 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1889 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1890 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1891 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1893 insn = (58 << 26) | 2;
1901 template<int size, bool big_endian>
1902 class Powerpc_relocate_functions
1922 typedef Powerpc_relocate_functions<size, big_endian> This;
1923 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1924 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
1926 template<int valsize>
1928 has_overflow_signed(Address value)
1930 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1931 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1932 limit <<= ((valsize - 1) >> 1);
1933 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1934 return value + limit > (limit << 1) - 1;
1937 template<int valsize>
1939 has_overflow_unsigned(Address value)
1941 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1942 limit <<= ((valsize - 1) >> 1);
1943 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1944 return value > (limit << 1) - 1;
1947 template<int valsize>
1949 has_overflow_bitfield(Address value)
1951 return (has_overflow_unsigned<valsize>(value)
1952 && has_overflow_signed<valsize>(value));
1955 template<int valsize>
1956 static inline Status
1957 overflowed(Address value, Overflow_check overflow)
1959 if (overflow == CHECK_SIGNED)
1961 if (has_overflow_signed<valsize>(value))
1962 return STATUS_OVERFLOW;
1964 else if (overflow == CHECK_UNSIGNED)
1966 if (has_overflow_unsigned<valsize>(value))
1967 return STATUS_OVERFLOW;
1969 else if (overflow == CHECK_BITFIELD)
1971 if (has_overflow_bitfield<valsize>(value))
1972 return STATUS_OVERFLOW;
1977 // Do a simple RELA relocation
1978 template<int fieldsize, int valsize>
1979 static inline Status
1980 rela(unsigned char* view, Address value, Overflow_check overflow)
1982 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1983 Valtype* wv = reinterpret_cast<Valtype*>(view);
1984 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
1985 return overflowed<valsize>(value, overflow);
1988 template<int fieldsize, int valsize>
1989 static inline Status
1990 rela(unsigned char* view,
1991 unsigned int right_shift,
1992 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1994 Overflow_check overflow)
1996 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1997 Valtype* wv = reinterpret_cast<Valtype*>(view);
1998 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
1999 Valtype reloc = value >> right_shift;
2002 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
2003 return overflowed<valsize>(value >> right_shift, overflow);
2006 // Do a simple RELA relocation, unaligned.
2007 template<int fieldsize, int valsize>
2008 static inline Status
2009 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
2011 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
2012 return overflowed<valsize>(value, overflow);
2015 template<int fieldsize, int valsize>
2016 static inline Status
2017 rela_ua(unsigned char* view,
2018 unsigned int right_shift,
2019 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2021 Overflow_check overflow)
2023 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
2025 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
2026 Valtype reloc = value >> right_shift;
2029 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
2030 return overflowed<valsize>(value >> right_shift, overflow);
2034 // R_PPC64_ADDR64: (Symbol + Addend)
2036 addr64(unsigned char* view, Address value)
2037 { This::template rela<64,64>(view, value, CHECK_NONE); }
2039 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
2041 addr64_u(unsigned char* view, Address value)
2042 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
2044 // R_POWERPC_ADDR32: (Symbol + Addend)
2045 static inline Status
2046 addr32(unsigned char* view, Address value, Overflow_check overflow)
2047 { return This::template rela<32,32>(view, value, overflow); }
2049 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
2050 static inline Status
2051 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
2052 { return This::template rela_ua<32,32>(view, value, overflow); }
2054 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
2055 static inline Status
2056 addr24(unsigned char* view, Address value, Overflow_check overflow)
2058 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2060 if (overflow != CHECK_NONE && (value & 3) != 0)
2061 stat = STATUS_OVERFLOW;
2065 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
2066 static inline Status
2067 addr16(unsigned char* view, Address value, Overflow_check overflow)
2068 { return This::template rela<16,16>(view, value, overflow); }
2070 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
2071 static inline Status
2072 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
2073 { return This::template rela_ua<16,16>(view, value, overflow); }
2075 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
2076 static inline Status
2077 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
2079 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
2080 if ((value & 3) != 0)
2081 stat = STATUS_OVERFLOW;
2085 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2086 static inline Status
2087 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2089 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2090 if ((value & 15) != 0)
2091 stat = STATUS_OVERFLOW;
2095 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2097 addr16_hi(unsigned char* view, Address value)
2098 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
2100 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
2102 addr16_ha(unsigned char* view, Address value)
2103 { This::addr16_hi(view, value + 0x8000); }
2105 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
2107 addr16_hi2(unsigned char* view, Address value)
2108 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
2110 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
2112 addr16_ha2(unsigned char* view, Address value)
2113 { This::addr16_hi2(view, value + 0x8000); }
2115 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
2117 addr16_hi3(unsigned char* view, Address value)
2118 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
2120 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
2122 addr16_ha3(unsigned char* view, Address value)
2123 { This::addr16_hi3(view, value + 0x8000); }
2125 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
2126 static inline Status
2127 addr14(unsigned char* view, Address value, Overflow_check overflow)
2129 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
2130 if (overflow != CHECK_NONE && (value & 3) != 0)
2131 stat = STATUS_OVERFLOW;
2135 // R_POWERPC_REL16DX_HA
2136 static inline Status
2137 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2139 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2140 Valtype* wv = reinterpret_cast<Valtype*>(view);
2141 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2143 value = static_cast<SignedAddress>(value) >> 16;
2144 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2145 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2146 return overflowed<16>(value, overflow);
2150 static inline Status
2151 addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
2153 Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
2155 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2161 addr34_hi(unsigned char *view, uint64_t value)
2162 { This::addr34(view, value >> 34, CHECK_NONE);}
2166 addr34_ha(unsigned char *view, uint64_t value)
2167 { This::addr34_hi(view, value + (1ULL << 33));}
2170 static inline Status
2171 addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
2173 Status stat = This::template rela<32,12>(view, 16, 0xfff,
2175 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2179 // R_PPC64_ADDR16_HIGHER34
2181 addr16_higher34(unsigned char* view, uint64_t value)
2182 { This::addr16(view, value >> 34, CHECK_NONE); }
2184 // R_PPC64_ADDR16_HIGHERA34
2186 addr16_highera34(unsigned char* view, uint64_t value)
2187 { This::addr16_higher34(view, value + (1ULL << 33)); }
2189 // R_PPC64_ADDR16_HIGHEST34
2191 addr16_highest34(unsigned char* view, uint64_t value)
2192 { This::addr16(view, value >> 50, CHECK_NONE); }
2194 // R_PPC64_ADDR16_HIGHESTA34
2196 addr16_highesta34(unsigned char* view, uint64_t value)
2197 { This::addr16_highest34(view, value + (1ULL << 33)); }
2200 // Set ABI version for input and output.
2202 template<int size, bool big_endian>
2204 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2206 this->e_flags_ |= ver;
2207 if (this->abiversion() != 0)
2209 Target_powerpc<size, big_endian>* target =
2210 static_cast<Target_powerpc<size, big_endian>*>(
2211 parameters->sized_target<size, big_endian>());
2212 if (target->abiversion() == 0)
2213 target->set_abiversion(this->abiversion());
2214 else if (target->abiversion() != this->abiversion())
2215 gold_error(_("%s: ABI version %d is not compatible "
2216 "with ABI version %d output"),
2217 this->name().c_str(),
2218 this->abiversion(), target->abiversion());
2223 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2224 // relocatable object, if such sections exists.
2226 template<int size, bool big_endian>
2228 Powerpc_relobj<size, big_endian>::do_find_special_sections(
2229 Read_symbols_data* sd)
2231 const unsigned char* const pshdrs = sd->section_headers->data();
2232 const unsigned char* namesu = sd->section_names->data();
2233 const char* names = reinterpret_cast<const char*>(namesu);
2234 section_size_type names_size = sd->section_names_size;
2235 const unsigned char* s;
2237 s = this->template find_shdr<size, big_endian>(pshdrs,
2238 size == 32 ? ".got2" : ".opd",
2239 names, names_size, NULL);
2242 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2243 this->special_ = ndx;
2246 if (this->abiversion() == 0)
2247 this->set_abiversion(1);
2248 else if (this->abiversion() > 1)
2249 gold_error(_("%s: .opd invalid in abiv%d"),
2250 this->name().c_str(), this->abiversion());
2255 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2256 names, names_size, NULL);
2259 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2260 this->relatoc_ = ndx;
2261 typename elfcpp::Shdr<size, big_endian> shdr(s);
2262 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2265 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2268 // Examine .rela.opd to build info about function entry points.
2270 template<int size, bool big_endian>
2272 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2274 const unsigned char* prelocs,
2275 const unsigned char* plocal_syms)
2279 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2280 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2281 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2282 Address expected_off = 0;
2283 bool regular = true;
2284 unsigned int opd_ent_size = 0;
2286 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2288 Reltype reloc(prelocs);
2289 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2290 = reloc.get_r_info();
2291 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2292 if (r_type == elfcpp::R_PPC64_ADDR64)
2294 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2295 typename elfcpp::Elf_types<size>::Elf_Addr value;
2298 if (r_sym < this->local_symbol_count())
2300 typename elfcpp::Sym<size, big_endian>
2301 lsym(plocal_syms + r_sym * sym_size);
2302 shndx = lsym.get_st_shndx();
2303 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2304 value = lsym.get_st_value();
2307 shndx = this->symbol_section_and_value(r_sym, &value,
2309 this->set_opd_ent(reloc.get_r_offset(), shndx,
2310 value + reloc.get_r_addend());
2313 expected_off = reloc.get_r_offset();
2314 opd_ent_size = expected_off;
2316 else if (expected_off != reloc.get_r_offset())
2318 expected_off += opd_ent_size;
2320 else if (r_type == elfcpp::R_PPC64_TOC)
2322 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2327 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2328 this->name().c_str(), r_type);
2332 if (reloc_count <= 2)
2333 opd_ent_size = this->section_size(this->opd_shndx());
2334 if (opd_ent_size != 24 && opd_ent_size != 16)
2338 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2339 this->name().c_str());
2345 // Returns true if a code sequence loading the TOC entry at VALUE
2346 // relative to the TOC pointer can be converted into code calculating
2347 // a TOC pointer relative offset.
2348 // If so, the TOC pointer relative offset is stored to VALUE.
2350 template<int size, bool big_endian>
2352 Powerpc_relobj<size, big_endian>::make_toc_relative(
2353 Target_powerpc<size, big_endian>* target,
2359 // With -mcmodel=medium code it is quite possible to have
2360 // toc-relative relocs referring to objects outside the TOC.
2361 // Don't try to look at a non-existent TOC.
2362 if (this->toc_shndx() == 0)
2365 // Convert VALUE back to an address by adding got_base (see below),
2366 // then to an offset in the TOC by subtracting the TOC output
2367 // section address and the TOC output offset. Since this TOC output
2368 // section and the got output section are one and the same, we can
2369 // omit adding and subtracting the output section address.
2370 Address off = (*value + this->toc_base_offset()
2371 - this->output_section_offset(this->toc_shndx()));
2372 // Is this offset in the TOC? -mcmodel=medium code may be using
2373 // TOC relative access to variables outside the TOC. Those of
2374 // course can't be optimized. We also don't try to optimize code
2375 // that is using a different object's TOC.
2376 if (off >= this->section_size(this->toc_shndx()))
2379 if (this->no_toc_opt(off))
2382 section_size_type vlen;
2383 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2384 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2386 Address got_base = (target->got_section()->output_section()->address()
2387 + this->toc_base_offset());
2389 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2396 template<int size, bool big_endian>
2398 Powerpc_relobj<size, big_endian>::make_got_relative(
2399 Target_powerpc<size, big_endian>* target,
2400 const Symbol_value<size>* psymval,
2404 Address addr = psymval->value(this, addend);
2405 Address got_base = (target->got_section()->output_section()->address()
2406 + this->toc_base_offset());
2408 if (addr + 0x80008000 > 0xffffffff)
2415 // Perform the Sized_relobj_file method, then set up opd info from
2418 template<int size, bool big_endian>
2420 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2422 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2425 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2426 p != rd->relocs.end();
2429 if (p->data_shndx == this->opd_shndx())
2431 uint64_t opd_size = this->section_size(this->opd_shndx());
2432 gold_assert(opd_size == static_cast<size_t>(opd_size));
2435 this->init_opd(opd_size);
2436 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2437 rd->local_symbols->data());
2445 // Read the symbols then set up st_other vector.
2447 template<int size, bool big_endian>
2449 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2451 this->base_read_symbols(sd);
2452 if (this->input_file()->format() != Input_file::FORMAT_ELF)
2456 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2457 const unsigned char* const pshdrs = sd->section_headers->data();
2458 const unsigned int loccount = this->do_local_symbol_count();
2461 this->st_other_.resize(loccount);
2462 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2463 off_t locsize = loccount * sym_size;
2464 const unsigned int symtab_shndx = this->symtab_shndx();
2465 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2466 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2467 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2468 locsize, true, false);
2470 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2472 elfcpp::Sym<size, big_endian> sym(psyms);
2473 unsigned char st_other = sym.get_st_other();
2474 this->st_other_[i] = st_other;
2475 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2477 if (this->abiversion() == 0)
2478 this->set_abiversion(2);
2479 else if (this->abiversion() < 2)
2480 gold_error(_("%s: local symbol %d has invalid st_other"
2481 " for ABI version 1"),
2482 this->name().c_str(), i);
2488 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2489 const unsigned char* ps = sd->section_headers->data() + shdr_size;
2490 bool merge_attributes = false;
2491 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2493 elfcpp::Shdr<size, big_endian> shdr(ps);
2494 switch (shdr.get_sh_type())
2496 case elfcpp::SHT_GNU_ATTRIBUTES:
2498 gold_assert(this->attributes_section_data_ == NULL);
2499 section_offset_type section_offset = shdr.get_sh_offset();
2500 section_size_type section_size =
2501 convert_to_section_size_type(shdr.get_sh_size());
2502 const unsigned char* view =
2503 this->get_view(section_offset, section_size, true, false);
2504 this->attributes_section_data_ =
2505 new Attributes_section_data(view, section_size);
2509 case elfcpp::SHT_SYMTAB:
2511 // Sometimes an object has no contents except the section
2512 // name string table and an empty symbol table with the
2513 // undefined symbol. We don't want to merge
2514 // processor-specific flags from such an object.
2515 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2516 elfcpp::Elf_sizes<size>::sym_size;
2517 if (shdr.get_sh_size() > sym_size)
2518 merge_attributes = true;
2522 case elfcpp::SHT_STRTAB:
2526 merge_attributes = true;
2531 if (!merge_attributes)
2533 // Should rarely happen.
2534 delete this->attributes_section_data_;
2535 this->attributes_section_data_ = NULL;
2539 template<int size, bool big_endian>
2541 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2543 this->e_flags_ |= ver;
2544 if (this->abiversion() != 0)
2546 Target_powerpc<size, big_endian>* target =
2547 static_cast<Target_powerpc<size, big_endian>*>(
2548 parameters->sized_target<size, big_endian>());
2549 if (target->abiversion() == 0)
2550 target->set_abiversion(this->abiversion());
2551 else if (target->abiversion() != this->abiversion())
2552 gold_error(_("%s: ABI version %d is not compatible "
2553 "with ABI version %d output"),
2554 this->name().c_str(),
2555 this->abiversion(), target->abiversion());
2560 // Call Sized_dynobj::base_read_symbols to read the symbols then
2561 // read .opd from a dynamic object, filling in opd_ent_ vector,
2563 template<int size, bool big_endian>
2565 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2567 this->base_read_symbols(sd);
2568 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2569 const unsigned char* ps =
2570 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2571 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2573 elfcpp::Shdr<size, big_endian> shdr(ps);
2574 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2576 section_offset_type section_offset = shdr.get_sh_offset();
2577 section_size_type section_size =
2578 convert_to_section_size_type(shdr.get_sh_size());
2579 const unsigned char* view =
2580 this->get_view(section_offset, section_size, true, false);
2581 this->attributes_section_data_ =
2582 new Attributes_section_data(view, section_size);
2588 const unsigned char* const pshdrs = sd->section_headers->data();
2589 const unsigned char* namesu = sd->section_names->data();
2590 const char* names = reinterpret_cast<const char*>(namesu);
2591 const unsigned char* s = NULL;
2592 const unsigned char* opd;
2593 section_size_type opd_size;
2595 // Find and read .opd section.
2598 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2599 sd->section_names_size,
2604 typename elfcpp::Shdr<size, big_endian> shdr(s);
2605 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2606 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2608 if (this->abiversion() == 0)
2609 this->set_abiversion(1);
2610 else if (this->abiversion() > 1)
2611 gold_error(_("%s: .opd invalid in abiv%d"),
2612 this->name().c_str(), this->abiversion());
2614 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2615 this->opd_address_ = shdr.get_sh_addr();
2616 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2617 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2623 // Build set of executable sections.
2624 // Using a set is probably overkill. There is likely to be only
2625 // a few executable sections, typically .init, .text and .fini,
2626 // and they are generally grouped together.
2627 typedef std::set<Sec_info> Exec_sections;
2628 Exec_sections exec_sections;
2630 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2632 typename elfcpp::Shdr<size, big_endian> shdr(s);
2633 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2634 && ((shdr.get_sh_flags()
2635 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2636 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2637 && shdr.get_sh_size() != 0)
2639 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2640 shdr.get_sh_size(), i));
2643 if (exec_sections.empty())
2646 // Look over the OPD entries. This is complicated by the fact
2647 // that some binaries will use two-word entries while others
2648 // will use the standard three-word entries. In most cases
2649 // the third word (the environment pointer for languages like
2650 // Pascal) is unused and will be zero. If the third word is
2651 // used it should not be pointing into executable sections,
2653 this->init_opd(opd_size);
2654 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2656 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2657 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2658 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2660 // Chances are that this is the third word of an OPD entry.
2662 typename Exec_sections::const_iterator e
2663 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2664 if (e != exec_sections.begin())
2667 if (e->start <= val && val < e->start + e->len)
2669 // We have an address in an executable section.
2670 // VAL ought to be the function entry, set it up.
2671 this->set_opd_ent(p - opd, e->shndx, val);
2672 // Skip second word of OPD entry, the TOC pointer.
2676 // If we didn't match any executable sections, we likely
2677 // have a non-zero third word in the OPD entry.
2682 // Relocate sections.
2684 template<int size, bool big_endian>
2686 Powerpc_relobj<size, big_endian>::do_relocate_sections(
2687 const Symbol_table* symtab, const Layout* layout,
2688 const unsigned char* pshdrs, Output_file* of,
2689 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2691 unsigned int start = 1;
2693 && this->relatoc_ != 0
2694 && !parameters->options().relocatable())
2696 // Relocate .toc first.
2697 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2698 this->relatoc_, this->relatoc_);
2699 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2700 1, this->relatoc_ - 1);
2701 start = this->relatoc_ + 1;
2703 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2704 start, this->shnum() - 1);
2706 if (!parameters->options().output_is_position_independent())
2708 Target_powerpc<size, big_endian>* target
2709 = static_cast<Target_powerpc<size, big_endian>*>(
2710 parameters->sized_target<size, big_endian>());
2711 if (target->lplt_section() && target->lplt_section()->data_size() != 0)
2713 const section_size_type offset = target->lplt_section()->offset();
2714 const section_size_type oview_size
2715 = convert_to_section_size_type(target->lplt_section()->data_size());
2716 unsigned char* const oview = of->get_output_view(offset, oview_size);
2718 bool modified = false;
2719 unsigned int nsyms = this->local_symbol_count();
2720 for (unsigned int i = 0; i < nsyms; i++)
2721 if (this->local_has_plt_offset(i))
2723 Address value = this->local_symbol_value(i, 0);
2725 value += ppc64_local_entry_offset(i);
2726 size_t off = this->local_plt_offset(i);
2727 elfcpp::Swap<size, big_endian>::writeval(oview + off, value);
2731 of->write_output_view(offset, oview_size, oview);
2736 // Set up some symbols.
2738 template<int size, bool big_endian>
2740 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2741 Symbol_table* symtab,
2746 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2747 // undefined when scanning relocs (and thus requires
2748 // non-relative dynamic relocs). The proper value will be
2750 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2751 if (gotsym != NULL && gotsym->is_undefined())
2753 Target_powerpc<size, big_endian>* target =
2754 static_cast<Target_powerpc<size, big_endian>*>(
2755 parameters->sized_target<size, big_endian>());
2756 Output_data_got_powerpc<size, big_endian>* got
2757 = target->got_section(symtab, layout);
2758 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2759 Symbol_table::PREDEFINED,
2763 elfcpp::STV_HIDDEN, 0,
2767 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2768 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2769 if (sdasym != NULL && sdasym->is_undefined())
2771 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2773 = layout->add_output_section_data(".sdata", 0,
2775 | elfcpp::SHF_WRITE,
2776 sdata, ORDER_SMALL_DATA, false);
2777 symtab->define_in_output_data("_SDA_BASE_", NULL,
2778 Symbol_table::PREDEFINED,
2779 os, 32768, 0, elfcpp::STT_OBJECT,
2780 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2786 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2787 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2788 if (gotsym != NULL && gotsym->is_undefined())
2790 Target_powerpc<size, big_endian>* target =
2791 static_cast<Target_powerpc<size, big_endian>*>(
2792 parameters->sized_target<size, big_endian>());
2793 Output_data_got_powerpc<size, big_endian>* got
2794 = target->got_section(symtab, layout);
2795 symtab->define_in_output_data(".TOC.", NULL,
2796 Symbol_table::PREDEFINED,
2800 elfcpp::STV_HIDDEN, 0,
2805 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2806 if (parameters->options().tls_get_addr_optimize()
2807 && this->tls_get_addr_ != NULL
2808 && this->tls_get_addr_->in_reg())
2809 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2810 if (this->tls_get_addr_opt_ != NULL)
2812 if (this->tls_get_addr_->is_undefined()
2813 || this->tls_get_addr_->is_from_dynobj())
2815 // Make it seem as if references to __tls_get_addr are
2816 // really to __tls_get_addr_opt, so the latter symbol is
2817 // made dynamic, not the former.
2818 this->tls_get_addr_->clear_in_reg();
2819 this->tls_get_addr_opt_->set_in_reg();
2821 // We have a non-dynamic definition for __tls_get_addr.
2822 // Make __tls_get_addr_opt the same, if it does not already have
2823 // a non-dynamic definition.
2824 else if (this->tls_get_addr_opt_->is_undefined()
2825 || this->tls_get_addr_opt_->is_from_dynobj())
2827 Sized_symbol<size>* from
2828 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2829 Sized_symbol<size>* to
2830 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2831 symtab->clone<size>(to, from);
2836 // Set up PowerPC target specific relobj.
2838 template<int size, bool big_endian>
2840 Target_powerpc<size, big_endian>::do_make_elf_object(
2841 const std::string& name,
2842 Input_file* input_file,
2843 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2845 int et = ehdr.get_e_type();
2846 // ET_EXEC files are valid input for --just-symbols/-R,
2847 // and we treat them as relocatable objects.
2848 if (et == elfcpp::ET_REL
2849 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2851 Powerpc_relobj<size, big_endian>* obj =
2852 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2856 else if (et == elfcpp::ET_DYN)
2858 Powerpc_dynobj<size, big_endian>* obj =
2859 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2865 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2870 template<int size, bool big_endian>
2871 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2874 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2875 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2877 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2878 : Output_data_got<size, big_endian>(),
2879 symtab_(symtab), layout_(layout),
2880 header_ent_cnt_(size == 32 ? 3 : 1),
2881 header_index_(size == 32 ? 0x2000 : 0)
2884 this->set_addralign(256);
2887 // Override all the Output_data_got methods we use so as to first call
2890 add_global(Symbol* gsym, unsigned int got_type)
2892 this->reserve_ent();
2893 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2897 add_global_plt(Symbol* gsym, unsigned int got_type)
2899 this->reserve_ent();
2900 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2904 add_global_tls(Symbol* gsym, unsigned int got_type)
2905 { return this->add_global_plt(gsym, got_type); }
2908 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2909 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2911 this->reserve_ent();
2912 Output_data_got<size, big_endian>::
2913 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2917 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2918 Output_data_reloc_generic* rel_dyn,
2919 unsigned int r_type_1, unsigned int r_type_2)
2921 if (gsym->has_got_offset(got_type))
2924 this->reserve_ent(2);
2925 Output_data_got<size, big_endian>::
2926 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2930 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2932 this->reserve_ent();
2933 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2938 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2940 this->reserve_ent();
2941 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2946 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2947 { return this->add_local_plt(object, sym_index, got_type); }
2950 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2951 unsigned int got_type,
2952 Output_data_reloc_generic* rel_dyn,
2953 unsigned int r_type)
2955 if (object->local_has_got_offset(sym_index, got_type))
2958 this->reserve_ent(2);
2959 Output_data_got<size, big_endian>::
2960 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2964 add_constant(Valtype constant)
2966 this->reserve_ent();
2967 return Output_data_got<size, big_endian>::add_constant(constant);
2971 add_constant_pair(Valtype c1, Valtype c2)
2973 this->reserve_ent(2);
2974 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
2977 // Offset of _GLOBAL_OFFSET_TABLE_.
2981 return this->got_offset(this->header_index_);
2984 // Offset of base used to access the GOT/TOC.
2985 // The got/toc pointer reg will be set to this value.
2987 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2990 return this->g_o_t();
2992 return (this->output_section()->address()
2993 + object->toc_base_offset()
2997 // Ensure our GOT has a header.
2999 set_final_data_size()
3001 if (this->header_ent_cnt_ != 0)
3002 this->make_header();
3003 Output_data_got<size, big_endian>::set_final_data_size();
3006 // First word of GOT header needs some values that are not
3007 // handled by Output_data_got so poke them in here.
3008 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
3010 do_write(Output_file* of)
3013 if (size == 32 && this->layout_->dynamic_data() != NULL)
3014 val = this->layout_->dynamic_section()->address();
3016 val = this->output_section()->address() + 0x8000;
3017 this->replace_constant(this->header_index_, val);
3018 Output_data_got<size, big_endian>::do_write(of);
3023 reserve_ent(unsigned int cnt = 1)
3025 if (this->header_ent_cnt_ == 0)
3027 if (this->num_entries() + cnt > this->header_index_)
3028 this->make_header();
3034 this->header_ent_cnt_ = 0;
3035 this->header_index_ = this->num_entries();
3038 Output_data_got<size, big_endian>::add_constant(0);
3039 Output_data_got<size, big_endian>::add_constant(0);
3040 Output_data_got<size, big_endian>::add_constant(0);
3042 // Define _GLOBAL_OFFSET_TABLE_ at the header
3043 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
3046 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
3047 sym->set_value(this->g_o_t());
3050 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3051 Symbol_table::PREDEFINED,
3052 this, this->g_o_t(), 0,
3055 elfcpp::STV_HIDDEN, 0,
3059 Output_data_got<size, big_endian>::add_constant(0);
3062 // Stashed pointers.
3063 Symbol_table* symtab_;
3067 unsigned int header_ent_cnt_;
3068 // GOT header index.
3069 unsigned int header_index_;
3072 // Get the GOT section, creating it if necessary.
3074 template<int size, bool big_endian>
3075 Output_data_got_powerpc<size, big_endian>*
3076 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
3079 if (this->got_ == NULL)
3081 gold_assert(symtab != NULL && layout != NULL);
3084 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
3086 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3087 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3088 this->got_, ORDER_DATA, false);
3094 // Get the dynamic reloc section, creating it if necessary.
3096 template<int size, bool big_endian>
3097 typename Target_powerpc<size, big_endian>::Reloc_section*
3098 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3100 if (this->rela_dyn_ == NULL)
3102 gold_assert(layout != NULL);
3103 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3104 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3105 elfcpp::SHF_ALLOC, this->rela_dyn_,
3106 ORDER_DYNAMIC_RELOCS, false);
3108 return this->rela_dyn_;
3111 // Similarly, but for ifunc symbols get the one for ifunc.
3113 template<int size, bool big_endian>
3114 typename Target_powerpc<size, big_endian>::Reloc_section*
3115 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3120 return this->rela_dyn_section(layout);
3122 if (this->iplt_ == NULL)
3123 this->make_iplt_section(symtab, layout);
3124 return this->iplt_->rel_plt();
3130 // Determine the stub group size. The group size is the absolute
3131 // value of the parameter --stub-group-size. If --stub-group-size
3132 // is passed a negative value, we restrict stubs to be always after
3133 // the stubbed branches.
3134 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3135 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3136 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3137 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3138 owner_(NULL), output_section_(NULL)
3142 // Return true iff input section can be handled by current stub
3145 can_add_to_stub_group(Output_section* o,
3146 const Output_section::Input_section* i,
3149 const Output_section::Input_section*
3155 { return output_section_; }
3158 set_output_and_owner(Output_section* o,
3159 const Output_section::Input_section* i)
3161 this->output_section_ = o;
3170 // Adding group sections before the stubs.
3171 FINDING_STUB_SECTION,
3172 // Adding group sections after the stubs.
3176 uint32_t stub_group_size_;
3177 bool stubs_always_after_branch_;
3178 bool suppress_size_errors_;
3179 // True if a stub group can serve multiple output sections.
3182 // Current max size of group. Starts at stub_group_size_ but is
3183 // reduced to stub_group_size_/1024 on seeing a section with
3184 // external conditional branches.
3185 uint32_t group_size_;
3186 uint64_t group_start_addr_;
3187 // owner_ and output_section_ specify the section to which stubs are
3188 // attached. The stubs are placed at the end of this section.
3189 const Output_section::Input_section* owner_;
3190 Output_section* output_section_;
3193 // Return true iff input section can be handled by current stub
3194 // group. Sections are presented to this function in order,
3195 // so the first section is the head of the group.
3198 Stub_control::can_add_to_stub_group(Output_section* o,
3199 const Output_section::Input_section* i,
3202 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3204 uint64_t start_addr = o->address();
3207 // .init and .fini sections are pasted together to form a single
3208 // function. We can't be adding stubs in the middle of the function.
3209 this_size = o->data_size();
3212 start_addr += i->relobj()->output_section_offset(i->shndx());
3213 this_size = i->data_size();
3216 uint64_t end_addr = start_addr + this_size;
3217 uint32_t group_size = this->stub_group_size_;
3219 this->group_size_ = group_size = group_size >> 10;
3221 if (this_size > group_size && !this->suppress_size_errors_)
3222 gold_warning(_("%s:%s exceeds group size"),
3223 i->relobj()->name().c_str(),
3224 i->relobj()->section_name(i->shndx()).c_str());
3226 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3227 has14 ? " 14bit" : "",
3228 i->relobj()->name().c_str(),
3229 i->relobj()->section_name(i->shndx()).c_str(),
3230 (long long) this_size,
3231 (this->state_ == NO_GROUP
3233 : (long long) end_addr - this->group_start_addr_));
3235 if (this->state_ == NO_GROUP)
3237 // Only here on very first use of Stub_control
3239 this->output_section_ = o;
3240 this->state_ = FINDING_STUB_SECTION;
3241 this->group_size_ = group_size;
3242 this->group_start_addr_ = start_addr;
3245 else if (!this->multi_os_ && this->output_section_ != o)
3247 else if (this->state_ == HAS_STUB_SECTION)
3249 // Can we add this section, which is after the stubs, to the
3251 if (end_addr - this->group_start_addr_ <= this->group_size_)
3254 else if (this->state_ == FINDING_STUB_SECTION)
3256 if ((whole_sec && this->output_section_ == o)
3257 || end_addr - this->group_start_addr_ <= this->group_size_)
3259 // Stubs are added at the end of "owner_".
3261 this->output_section_ = o;
3264 // The group before the stubs has reached maximum size.
3265 // Now see about adding sections after the stubs to the
3266 // group. If the current section has a 14-bit branch and
3267 // the group before the stubs exceeds group_size_ (because
3268 // they didn't have 14-bit branches), don't add sections
3269 // after the stubs: The size of stubs for such a large
3270 // group may exceed the reach of a 14-bit branch.
3271 if (!this->stubs_always_after_branch_
3272 && this_size <= this->group_size_
3273 && start_addr - this->group_start_addr_ <= this->group_size_)
3275 gold_debug(DEBUG_TARGET, "adding after stubs");
3276 this->state_ = HAS_STUB_SECTION;
3277 this->group_start_addr_ = start_addr;
3284 gold_debug(DEBUG_TARGET,
3285 !this->multi_os_ && this->output_section_ != o
3286 ? "nope, new output section\n"
3287 : "nope, didn't fit\n");
3289 // The section fails to fit in the current group. Set up a few
3290 // things for the next group. owner_ and output_section_ will be
3291 // set later after we've retrieved those values for the current
3293 this->state_ = FINDING_STUB_SECTION;
3294 this->group_size_ = group_size;
3295 this->group_start_addr_ = start_addr;
3299 // Look over all the input sections, deciding where to place stubs.
3301 template<int size, bool big_endian>
3303 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
3305 bool no_size_errors)
3307 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3308 parameters->options().stub_group_multi());
3310 // Group input sections and insert stub table
3311 Stub_table_owner* table_owner = NULL;
3312 std::vector<Stub_table_owner*> tables;
3313 Layout::Section_list section_list;
3314 layout->get_executable_sections(§ion_list);
3315 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
3316 for (Layout::Section_list::iterator o = section_list.begin();
3317 o != section_list.end();
3320 typedef Output_section::Input_section_list Input_section_list;
3321 for (Input_section_list::const_iterator i
3322 = (*o)->input_sections().begin();
3323 i != (*o)->input_sections().end();
3326 if (i->is_input_section()
3327 || i->is_relaxed_input_section())
3329 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3330 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3331 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3332 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3334 table_owner->output_section = stub_control.output_section();
3335 table_owner->owner = stub_control.owner();
3336 stub_control.set_output_and_owner(*o, &*i);
3339 if (table_owner == NULL)
3341 table_owner = new Stub_table_owner;
3342 tables.push_back(table_owner);
3344 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
3348 if (table_owner != NULL)
3350 table_owner->output_section = stub_control.output_section();
3351 table_owner->owner = stub_control.owner();;
3353 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3357 Stub_table<size, big_endian>* stub_table;
3359 if ((*t)->owner->is_input_section())
3360 stub_table = new Stub_table<size, big_endian>(this,
3361 (*t)->output_section,
3363 this->stub_tables_.size());
3364 else if ((*t)->owner->is_relaxed_input_section())
3365 stub_table = static_cast<Stub_table<size, big_endian>*>(
3366 (*t)->owner->relaxed_input_section());
3369 this->stub_tables_.push_back(stub_table);
3375 static unsigned long
3376 max_branch_delta (unsigned int r_type)
3378 if (r_type == elfcpp::R_POWERPC_REL14
3379 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3380 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3382 if (r_type == elfcpp::R_POWERPC_REL24
3383 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
3384 || r_type == elfcpp::R_PPC_PLTREL24
3385 || r_type == elfcpp::R_PPC_LOCAL24PC)
3390 // Return whether this branch is going via a plt call stub.
3392 template<int size, bool big_endian>
3394 Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3395 Powerpc_relobj<size, big_endian>* ppc_object,
3398 Target_powerpc* target,
3399 Symbol_table* symtab)
3401 if (this->object_ != ppc_object
3402 || this->shndx_ != shndx
3403 || this->offset_ != offset)
3406 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3407 if (sym != NULL && sym->is_forwarder())
3408 sym = symtab->resolve_forwards(sym);
3409 if (target->replace_tls_get_addr(sym))
3410 sym = target->tls_get_addr_opt();
3411 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3413 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3414 && !target->is_elfv2_localentry0(gsym))
3415 : (this->object_->local_has_plt_offset(this->r_sym_)
3416 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
3424 // If this branch needs a plt call stub, or a long branch stub, make one.
3426 template<int size, bool big_endian>
3428 Target_powerpc<size, big_endian>::Branch_info::make_stub(
3429 Stub_table<size, big_endian>* stub_table,
3430 Stub_table<size, big_endian>* ifunc_stub_table,
3431 Symbol_table* symtab) const
3433 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3434 Target_powerpc<size, big_endian>* target =
3435 static_cast<Target_powerpc<size, big_endian>*>(
3436 parameters->sized_target<size, big_endian>());
3437 if (sym != NULL && sym->is_forwarder())
3438 sym = symtab->resolve_forwards(sym);
3439 if (target->replace_tls_get_addr(sym))
3440 sym = target->tls_get_addr_opt();
3441 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3445 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3446 : this->object_->local_has_plt_offset(this->r_sym_))
3450 && target->abiversion() >= 2
3451 && !parameters->options().output_is_position_independent()
3452 && !is_branch_reloc<size>(this->r_type_))
3453 target->glink_section()->add_global_entry(gsym);
3456 if (stub_table == NULL
3459 && !parameters->options().output_is_position_independent()
3460 && !is_branch_reloc<size>(this->r_type_)))
3461 stub_table = this->object_->stub_table(this->shndx_);
3462 if (stub_table == NULL)
3464 // This is a ref from a data section to an ifunc symbol,
3465 // or a non-branch reloc for which we always want to use
3466 // one set of stubs for resolving function addresses.
3467 stub_table = ifunc_stub_table;
3469 gold_assert(stub_table != NULL);
3470 Address from = this->object_->get_output_section_offset(this->shndx_);
3471 if (from != invalid_address)
3472 from += (this->object_->output_section(this->shndx_)->address()
3475 ok = stub_table->add_plt_call_entry(from,
3476 this->object_, gsym,
3477 this->r_type_, this->addend_,
3480 ok = stub_table->add_plt_call_entry(from,
3481 this->object_, this->r_sym_,
3482 this->r_type_, this->addend_,
3488 Address max_branch_offset = max_branch_delta<size>(this->r_type_);
3489 if (max_branch_offset == 0)
3491 Address from = this->object_->get_output_section_offset(this->shndx_);
3492 gold_assert(from != invalid_address);
3493 from += (this->object_->output_section(this->shndx_)->address()
3498 switch (gsym->source())
3500 case Symbol::FROM_OBJECT:
3502 Object* symobj = gsym->object();
3503 if (symobj->is_dynamic()
3504 || symobj->pluginobj() != NULL)
3507 unsigned int shndx = gsym->shndx(&is_ordinary);
3508 if (shndx == elfcpp::SHN_UNDEF)
3513 case Symbol::IS_UNDEFINED:
3519 Symbol_table::Compute_final_value_status status;
3520 to = symtab->compute_final_value<size>(gsym, &status);
3521 if (status != Symbol_table::CFVS_OK)
3524 to += this->object_->ppc64_local_entry_offset(gsym);
3528 const Symbol_value<size>* psymval
3529 = this->object_->local_symbol(this->r_sym_);
3530 Symbol_value<size> symval;
3531 if (psymval->is_section_symbol())
3532 symval.set_is_section_symbol();
3533 typedef Sized_relobj_file<size, big_endian> ObjType;
3534 typename ObjType::Compute_final_local_value_status status
3535 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3537 if (status != ObjType::CFLV_OK
3538 || !symval.has_output_value())
3540 to = symval.value(this->object_, 0);
3542 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
3544 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3545 to += this->addend_;
3546 if (stub_table == NULL)
3547 stub_table = this->object_->stub_table(this->shndx_);
3548 if (size == 64 && target->abiversion() < 2)
3550 unsigned int dest_shndx;
3551 if (!target->symval_for_branch(symtab, gsym, this->object_,
3555 Address delta = to - from;
3556 if (delta + max_branch_offset >= 2 * max_branch_offset
3558 && this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3560 ? this->object_->ppc64_needs_toc(gsym)
3561 : this->object_->ppc64_needs_toc(this->r_sym_))))
3563 if (stub_table == NULL)
3565 gold_warning(_("%s:%s: branch in non-executable section,"
3566 " no long branch stub for you"),
3567 this->object_->name().c_str(),
3568 this->object_->section_name(this->shndx_).c_str());
3571 bool save_res = (size == 64
3573 && gsym->source() == Symbol::IN_OUTPUT_DATA
3574 && gsym->output_data() == target->savres_section());
3575 ok = stub_table->add_long_branch_entry(this->object_,
3577 from, to, save_res);
3581 gold_debug(DEBUG_TARGET,
3582 "branch at %s:%s+%#lx\n"
3583 "can't reach stub attached to %s:%s",
3584 this->object_->name().c_str(),
3585 this->object_->section_name(this->shndx_).c_str(),
3586 (unsigned long) this->offset_,
3587 stub_table->relobj()->name().c_str(),
3588 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3593 // Relaxation hook. This is where we do stub generation.
3595 template<int size, bool big_endian>
3597 Target_powerpc<size, big_endian>::do_relax(int pass,
3598 const Input_objects*,
3599 Symbol_table* symtab,
3603 unsigned int prev_brlt_size = 0;
3607 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3609 && this->abiversion() < 2
3611 && !parameters->options().user_set_plt_thread_safe())
3613 static const char* const thread_starter[] =
3617 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3619 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3620 "mq_notify", "create_timer",
3625 "GOMP_parallel_start",
3626 "GOMP_parallel_loop_static",
3627 "GOMP_parallel_loop_static_start",
3628 "GOMP_parallel_loop_dynamic",
3629 "GOMP_parallel_loop_dynamic_start",
3630 "GOMP_parallel_loop_guided",
3631 "GOMP_parallel_loop_guided_start",
3632 "GOMP_parallel_loop_runtime",
3633 "GOMP_parallel_loop_runtime_start",
3634 "GOMP_parallel_sections",
3635 "GOMP_parallel_sections_start",
3640 if (parameters->options().shared())
3644 for (unsigned int i = 0;
3645 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3648 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3649 thread_safe = (sym != NULL
3651 && sym->in_real_elf());
3657 this->plt_thread_safe_ = thread_safe;
3662 this->stub_group_size_ = parameters->options().stub_group_size();
3663 bool no_size_errors = true;
3664 if (this->stub_group_size_ == 1)
3665 this->stub_group_size_ = 0x1c00000;
3666 else if (this->stub_group_size_ == -1)
3667 this->stub_group_size_ = -0x1e00000;
3669 no_size_errors = false;
3670 this->group_sections(layout, task, no_size_errors);
3672 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3674 this->branch_lookup_table_.clear();
3675 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3676 p != this->stub_tables_.end();
3679 (*p)->clear_stubs(true);
3681 this->stub_tables_.clear();
3682 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3683 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3684 program_name, this->stub_group_size_);
3685 this->group_sections(layout, task, true);
3688 // We need address of stub tables valid for make_stub.
3689 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3690 p != this->stub_tables_.end();
3693 const Powerpc_relobj<size, big_endian>* object
3694 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3695 Address off = object->get_output_section_offset((*p)->shndx());
3696 gold_assert(off != invalid_address);
3697 Output_section* os = (*p)->output_section();
3698 (*p)->set_address_and_size(os, off);
3703 // Clear plt call stubs, long branch stubs and branch lookup table.
3704 prev_brlt_size = this->branch_lookup_table_.size();
3705 this->branch_lookup_table_.clear();
3706 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3707 p != this->stub_tables_.end();
3710 (*p)->clear_stubs(false);
3714 // Build all the stubs.
3715 this->relax_failed_ = false;
3716 Stub_table<size, big_endian>* ifunc_stub_table
3717 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3718 Stub_table<size, big_endian>* one_stub_table
3719 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3720 for (typename Branches::const_iterator b = this->branch_info_.begin();
3721 b != this->branch_info_.end();
3724 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3725 && !this->relax_failed_)
3727 this->relax_failed_ = true;
3728 this->relax_fail_count_++;
3729 if (this->relax_fail_count_ < 3)
3733 bool do_resize = false;
3734 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3735 p != this->stub_tables_.end();
3737 if ((*p)->need_resize())
3744 this->branch_lookup_table_.clear();
3745 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3746 p != this->stub_tables_.end();
3748 (*p)->set_resizing(true);
3749 for (typename Branches::const_iterator b = this->branch_info_.begin();
3750 b != this->branch_info_.end();
3753 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3754 && !this->relax_failed_)
3756 this->relax_failed_ = true;
3757 this->relax_fail_count_++;
3758 if (this->relax_fail_count_ < 3)
3762 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3763 p != this->stub_tables_.end();
3765 (*p)->set_resizing(false);
3768 // Did anything change size?
3769 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3770 bool again = num_huge_branches != prev_brlt_size;
3771 if (size == 64 && num_huge_branches != 0)
3772 this->make_brlt_section(layout);
3773 if (size == 64 && again)
3774 this->brlt_section_->set_current_size(num_huge_branches);
3776 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3777 p != this->stub_tables_.rend();
3779 (*p)->remove_eh_frame(layout);
3781 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3782 p != this->stub_tables_.end();
3784 (*p)->add_eh_frame(layout);
3786 typedef Unordered_set<Output_section*> Output_sections;
3787 Output_sections os_need_update;
3788 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3789 p != this->stub_tables_.end();
3792 if ((*p)->size_update())
3795 os_need_update.insert((*p)->output_section());
3799 // Set output section offsets for all input sections in an output
3800 // section that just changed size. Anything past the stubs will
3802 for (typename Output_sections::iterator p = os_need_update.begin();
3803 p != os_need_update.end();
3806 Output_section* os = *p;
3808 typedef Output_section::Input_section_list Input_section_list;
3809 for (Input_section_list::const_iterator i = os->input_sections().begin();
3810 i != os->input_sections().end();
3813 off = align_address(off, i->addralign());
3814 if (i->is_input_section() || i->is_relaxed_input_section())
3815 i->relobj()->set_section_offset(i->shndx(), off);
3816 if (i->is_relaxed_input_section())
3818 Stub_table<size, big_endian>* stub_table
3819 = static_cast<Stub_table<size, big_endian>*>(
3820 i->relaxed_input_section());
3821 Address stub_table_size = stub_table->set_address_and_size(os, off);
3822 off += stub_table_size;
3823 // After a few iterations, set current stub table size
3824 // as min size threshold, so later stub tables can only
3827 stub_table->set_min_size_threshold(stub_table_size);
3830 off += i->data_size();
3832 // If .branch_lt is part of this output section, then we have
3833 // just done the offset adjustment.
3834 os->clear_section_offsets_need_adjustment();
3839 && num_huge_branches != 0
3840 && parameters->options().output_is_position_independent())
3842 // Fill in the BRLT relocs.
3843 this->brlt_section_->reset_brlt_sizes();
3844 for (typename Branch_lookup_table::const_iterator p
3845 = this->branch_lookup_table_.begin();
3846 p != this->branch_lookup_table_.end();
3849 this->brlt_section_->add_reloc(p->first, p->second);
3851 this->brlt_section_->finalize_brlt_sizes();
3855 && (parameters->options().user_set_emit_stub_syms()
3856 ? parameters->options().emit_stub_syms()
3858 || parameters->options().output_is_position_independent()
3859 || parameters->options().emit_relocs())))
3861 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3862 p != this->stub_tables_.end();
3864 (*p)->define_stub_syms(symtab);
3866 if (this->glink_ != NULL)
3868 int stub_size = this->glink_->pltresolve_size();
3869 Address value = -stub_size;
3875 this->define_local(symtab, "__glink_PLTresolve",
3876 this->glink_, value, stub_size);
3879 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3886 template<int size, bool big_endian>
3888 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3889 unsigned char* oview,
3893 uint64_t address = plt->address();
3894 off_t len = plt->data_size();
3896 if (plt == this->glink_)
3898 // See Output_data_glink::do_write() for glink contents.
3901 gold_assert(parameters->doing_static_link());
3902 // Static linking may need stubs, to support ifunc and long
3903 // branches. We need to create an output section for
3904 // .eh_frame early in the link process, to have a place to
3905 // attach stub .eh_frame info. We also need to have
3906 // registered a CIE that matches the stub CIE. Both of
3907 // these requirements are satisfied by creating an FDE and
3908 // CIE for .glink, even though static linking will leave
3909 // .glink zero length.
3910 // ??? Hopefully generating an FDE with a zero address range
3911 // won't confuse anything that consumes .eh_frame info.
3913 else if (size == 64)
3915 // There is one word before __glink_PLTresolve
3919 else if (parameters->options().output_is_position_independent())
3921 // There are two FDEs for a position independent glink.
3922 // The first covers the branch table, the second
3923 // __glink_PLTresolve at the end of glink.
3924 off_t resolve_size = this->glink_->pltresolve_size();
3925 if (oview[9] == elfcpp::DW_CFA_nop)
3926 len -= resolve_size;
3929 address += len - resolve_size;
3936 // Must be a stub table.
3937 const Stub_table<size, big_endian>* stub_table
3938 = static_cast<const Stub_table<size, big_endian>*>(plt);
3939 uint64_t stub_address = stub_table->stub_address();
3940 len -= stub_address - address;
3941 address = stub_address;
3944 *paddress = address;
3948 // A class to handle the PLT data.
3950 template<int size, bool big_endian>
3951 class Output_data_plt_powerpc : public Output_section_data_build
3954 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3955 size, big_endian> Reloc_section;
3957 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
3958 Reloc_section* plt_rel,
3960 : Output_section_data_build(size == 32 ? 4 : 8),
3966 // Add an entry to the PLT.
3971 add_ifunc_entry(Symbol*);
3974 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3977 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3979 // Return the .rela.plt section data.
3986 // Return the number of PLT entries.
3990 if (this->current_data_size() == 0)
3992 return ((this->current_data_size() - this->first_plt_entry_offset())
3993 / this->plt_entry_size());
3998 do_adjust_output_section(Output_section* os)
4003 // Write to a map file.
4005 do_print_to_mapfile(Mapfile* mapfile) const
4006 { mapfile->print_output_data(this, this->name_); }
4009 // Return the offset of the first non-reserved PLT entry.
4011 first_plt_entry_offset() const
4013 // IPLT and LPLT have no reserved entry.
4014 if (this->name_[3] == 'I' || this->name_[3] == 'L')
4016 return this->targ_->first_plt_entry_offset();
4019 // Return the size of each PLT entry.
4021 plt_entry_size() const
4023 return this->targ_->plt_entry_size();
4026 // Write out the PLT data.
4028 do_write(Output_file*);
4030 // The reloc section.
4031 Reloc_section* rel_;
4032 // Allows access to .glink for do_write.
4033 Target_powerpc<size, big_endian>* targ_;
4034 // What to report in map file.
4038 // Add an entry to the PLT.
4040 template<int size, bool big_endian>
4042 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
4044 if (!gsym->has_plt_offset())
4046 section_size_type off = this->current_data_size();
4048 off += this->first_plt_entry_offset();
4049 gsym->set_plt_offset(off);
4050 gsym->set_needs_dynsym_entry();
4051 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4052 this->rel_->add_global(gsym, dynrel, this, off, 0);
4053 off += this->plt_entry_size();
4054 this->set_current_data_size(off);
4058 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
4060 template<int size, bool big_endian>
4062 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
4064 if (!gsym->has_plt_offset())
4066 section_size_type off = this->current_data_size();
4067 gsym->set_plt_offset(off);
4068 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4069 if (size == 64 && this->targ_->abiversion() < 2)
4070 dynrel = elfcpp::R_PPC64_JMP_IREL;
4071 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
4072 off += this->plt_entry_size();
4073 this->set_current_data_size(off);
4077 // Add an entry for a local symbol to the PLT.
4079 template<int size, bool big_endian>
4081 Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4082 Sized_relobj_file<size, big_endian>* relobj,
4083 unsigned int local_sym_index)
4085 if (!relobj->local_has_plt_offset(local_sym_index))
4087 section_size_type off = this->current_data_size();
4088 relobj->set_local_plt_offset(local_sym_index, off);
4091 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4092 if (size == 64 && this->targ_->abiversion() < 2)
4093 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4094 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4095 dynrel, this, off, 0);
4097 off += this->plt_entry_size();
4098 this->set_current_data_size(off);
4102 // Add an entry for a local ifunc symbol to the IPLT.
4104 template<int size, bool big_endian>
4106 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4107 Sized_relobj_file<size, big_endian>* relobj,
4108 unsigned int local_sym_index)
4110 if (!relobj->local_has_plt_offset(local_sym_index))
4112 section_size_type off = this->current_data_size();
4113 relobj->set_local_plt_offset(local_sym_index, off);
4114 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4115 if (size == 64 && this->targ_->abiversion() < 2)
4116 dynrel = elfcpp::R_PPC64_JMP_IREL;
4117 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4119 off += this->plt_entry_size();
4120 this->set_current_data_size(off);
4124 static const uint32_t add_0_11_11 = 0x7c0b5a14;
4125 static const uint32_t add_2_2_11 = 0x7c425a14;
4126 static const uint32_t add_2_2_12 = 0x7c426214;
4127 static const uint32_t add_3_3_2 = 0x7c631214;
4128 static const uint32_t add_3_3_13 = 0x7c636a14;
4129 static const uint32_t add_3_12_2 = 0x7c6c1214;
4130 static const uint32_t add_3_12_13 = 0x7c6c6a14;
4131 static const uint32_t add_11_0_11 = 0x7d605a14;
4132 static const uint32_t add_11_2_11 = 0x7d625a14;
4133 static const uint32_t add_11_11_2 = 0x7d6b1214;
4134 static const uint32_t add_12_11_12 = 0x7d8b6214;
4135 static const uint32_t addi_0_12 = 0x380c0000;
4136 static const uint32_t addi_2_2 = 0x38420000;
4137 static const uint32_t addi_3_3 = 0x38630000;
4138 static const uint32_t addi_11_11 = 0x396b0000;
4139 static const uint32_t addi_12_1 = 0x39810000;
4140 static const uint32_t addi_12_11 = 0x398b0000;
4141 static const uint32_t addi_12_12 = 0x398c0000;
4142 static const uint32_t addis_0_2 = 0x3c020000;
4143 static const uint32_t addis_0_13 = 0x3c0d0000;
4144 static const uint32_t addis_2_12 = 0x3c4c0000;
4145 static const uint32_t addis_11_2 = 0x3d620000;
4146 static const uint32_t addis_11_11 = 0x3d6b0000;
4147 static const uint32_t addis_11_30 = 0x3d7e0000;
4148 static const uint32_t addis_12_1 = 0x3d810000;
4149 static const uint32_t addis_12_2 = 0x3d820000;
4150 static const uint32_t addis_12_11 = 0x3d8b0000;
4151 static const uint32_t addis_12_12 = 0x3d8c0000;
4152 static const uint32_t b = 0x48000000;
4153 static const uint32_t bcl_20_31 = 0x429f0005;
4154 static const uint32_t bctr = 0x4e800420;
4155 static const uint32_t bctrl = 0x4e800421;
4156 static const uint32_t beqlr = 0x4d820020;
4157 static const uint32_t blr = 0x4e800020;
4158 static const uint32_t bnectr_p4 = 0x4ce20420;
4159 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
4160 static const uint32_t cmpldi_2_0 = 0x28220000;
4161 static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4162 static const uint32_t cmpwi_11_0 = 0x2c0b0000;
4163 static const uint32_t cror_15_15_15 = 0x4def7b82;
4164 static const uint32_t cror_31_31_31 = 0x4ffffb82;
4165 static const uint32_t ld_0_1 = 0xe8010000;
4166 static const uint32_t ld_0_12 = 0xe80c0000;
4167 static const uint32_t ld_2_1 = 0xe8410000;
4168 static const uint32_t ld_2_2 = 0xe8420000;
4169 static const uint32_t ld_2_11 = 0xe84b0000;
4170 static const uint32_t ld_2_12 = 0xe84c0000;
4171 static const uint32_t ld_11_1 = 0xe9610000;
4172 static const uint32_t ld_11_2 = 0xe9620000;
4173 static const uint32_t ld_11_3 = 0xe9630000;
4174 static const uint32_t ld_11_11 = 0xe96b0000;
4175 static const uint32_t ld_12_2 = 0xe9820000;
4176 static const uint32_t ld_12_3 = 0xe9830000;
4177 static const uint32_t ld_12_11 = 0xe98b0000;
4178 static const uint32_t ld_12_12 = 0xe98c0000;
4179 static const uint32_t ldx_12_11_12 = 0x7d8b602a;
4180 static const uint32_t lfd_0_1 = 0xc8010000;
4181 static const uint32_t li_0_0 = 0x38000000;
4182 static const uint32_t li_11_0 = 0x39600000;
4183 static const uint32_t li_12_0 = 0x39800000;
4184 static const uint32_t lis_0 = 0x3c000000;
4185 static const uint32_t lis_2 = 0x3c400000;
4186 static const uint32_t lis_11 = 0x3d600000;
4187 static const uint32_t lis_12 = 0x3d800000;
4188 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
4189 static const uint32_t lwz_0_12 = 0x800c0000;
4190 static const uint32_t lwz_11_3 = 0x81630000;
4191 static const uint32_t lwz_11_11 = 0x816b0000;
4192 static const uint32_t lwz_11_30 = 0x817e0000;
4193 static const uint32_t lwz_12_3 = 0x81830000;
4194 static const uint32_t lwz_12_12 = 0x818c0000;
4195 static const uint32_t lwzu_0_12 = 0x840c0000;
4196 static const uint32_t mflr_0 = 0x7c0802a6;
4197 static const uint32_t mflr_11 = 0x7d6802a6;
4198 static const uint32_t mflr_12 = 0x7d8802a6;
4199 static const uint32_t mr_0_3 = 0x7c601b78;
4200 static const uint32_t mr_3_0 = 0x7c030378;
4201 static const uint32_t mtctr_0 = 0x7c0903a6;
4202 static const uint32_t mtctr_11 = 0x7d6903a6;
4203 static const uint32_t mtctr_12 = 0x7d8903a6;
4204 static const uint32_t mtlr_0 = 0x7c0803a6;
4205 static const uint32_t mtlr_11 = 0x7d6803a6;
4206 static const uint32_t mtlr_12 = 0x7d8803a6;
4207 static const uint32_t nop = 0x60000000;
4208 static const uint32_t ori_0_0_0 = 0x60000000;
4209 static const uint32_t ori_11_11_0 = 0x616b0000;
4210 static const uint32_t ori_12_12_0 = 0x618c0000;
4211 static const uint32_t oris_12_12_0 = 0x658c0000;
4212 static const uint32_t sldi_11_11_34 = 0x796b1746;
4213 static const uint32_t sldi_12_12_32 = 0x799c07c6;
4214 static const uint32_t srdi_0_0_2 = 0x7800f082;
4215 static const uint32_t std_0_1 = 0xf8010000;
4216 static const uint32_t std_0_12 = 0xf80c0000;
4217 static const uint32_t std_2_1 = 0xf8410000;
4218 static const uint32_t std_11_1 = 0xf9610000;
4219 static const uint32_t stfd_0_1 = 0xd8010000;
4220 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
4221 static const uint32_t sub_11_11_12 = 0x7d6c5850;
4222 static const uint32_t sub_12_12_11 = 0x7d8b6050;
4223 static const uint32_t xor_2_12_12 = 0x7d826278;
4224 static const uint32_t xor_11_12_12 = 0x7d8b6278;
4226 static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
4227 static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
4228 static const uint64_t pnop = 0x0700000000000000ULL;
4230 // Write out the PLT.
4232 template<int size, bool big_endian>
4234 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4236 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
4238 const section_size_type offset = this->offset();
4239 const section_size_type oview_size
4240 = convert_to_section_size_type(this->data_size());
4241 unsigned char* const oview = of->get_output_view(offset, oview_size);
4242 unsigned char* pov = oview;
4243 unsigned char* endpov = oview + oview_size;
4245 // The address of the .glink branch table
4246 const Output_data_glink<size, big_endian>* glink
4247 = this->targ_->glink_section();
4248 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
4250 while (pov < endpov)
4252 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4257 of->write_output_view(offset, oview_size, oview);
4261 // Create the PLT section.
4263 template<int size, bool big_endian>
4265 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4268 if (this->plt_ == NULL)
4270 if (this->got_ == NULL)
4271 this->got_section(symtab, layout);
4273 if (this->glink_ == NULL)
4274 make_glink_section(layout);
4276 // Ensure that .rela.dyn always appears before .rela.plt This is
4277 // necessary due to how, on PowerPC and some other targets, .rela.dyn
4278 // needs to include .rela.plt in its range.
4279 this->rela_dyn_section(layout);
4281 Reloc_section* plt_rel = new Reloc_section(false);
4282 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4283 elfcpp::SHF_ALLOC, plt_rel,
4284 ORDER_DYNAMIC_PLT_RELOCS, false);
4286 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
4288 layout->add_output_section_data(".plt",
4290 ? elfcpp::SHT_PROGBITS
4291 : elfcpp::SHT_NOBITS),
4292 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4299 Output_section* rela_plt_os = plt_rel->output_section();
4300 rela_plt_os->set_info_section(this->plt_->output_section());
4304 // Create the IPLT section.
4306 template<int size, bool big_endian>
4308 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4311 if (this->iplt_ == NULL)
4313 this->make_plt_section(symtab, layout);
4314 this->make_lplt_section(layout);
4316 Reloc_section* iplt_rel = new Reloc_section(false);
4317 if (this->rela_dyn_->output_section())
4318 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
4320 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
4322 if (this->plt_->output_section())
4323 this->plt_->output_section()->add_output_section_data(this->iplt_);
4327 // Create the LPLT section.
4329 template<int size, bool big_endian>
4331 Target_powerpc<size, big_endian>::make_lplt_section(Layout* layout)
4333 if (this->lplt_ == NULL)
4335 Reloc_section* lplt_rel = NULL;
4336 if (parameters->options().output_is_position_independent())
4338 lplt_rel = new Reloc_section(false);
4339 this->rela_dyn_section(layout);
4340 if (this->rela_dyn_->output_section())
4341 this->rela_dyn_->output_section()
4342 ->add_output_section_data(lplt_rel);
4345 = new Output_data_plt_powerpc<size, big_endian>(this, lplt_rel,
4347 this->make_brlt_section(layout);
4348 if (this->brlt_section_ && this->brlt_section_->output_section())
4349 this->brlt_section_->output_section()
4350 ->add_output_section_data(this->lplt_);
4352 layout->add_output_section_data(".branch_lt",
4353 elfcpp::SHT_PROGBITS,
4354 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4361 // A section for huge long branch addresses, similar to plt section.
4363 template<int size, bool big_endian>
4364 class Output_data_brlt_powerpc : public Output_section_data_build
4367 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4368 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4369 size, big_endian> Reloc_section;
4371 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4372 Reloc_section* brlt_rel)
4373 : Output_section_data_build(size == 32 ? 4 : 8),
4381 this->reset_data_size();
4382 this->rel_->reset_data_size();
4386 finalize_brlt_sizes()
4388 this->finalize_data_size();
4389 this->rel_->finalize_data_size();
4392 // Add a reloc for an entry in the BRLT.
4394 add_reloc(Address to, unsigned int off)
4395 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
4397 // Update section and reloc section size.
4399 set_current_size(unsigned int num_branches)
4401 this->reset_address_and_file_offset();
4402 this->set_current_data_size(num_branches * 16);
4403 this->finalize_data_size();
4404 Output_section* os = this->output_section();
4405 os->set_section_offsets_need_adjustment();
4406 if (this->rel_ != NULL)
4408 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4409 this->rel_->reset_address_and_file_offset();
4410 this->rel_->set_current_data_size(num_branches * reloc_size);
4411 this->rel_->finalize_data_size();
4412 Output_section* os = this->rel_->output_section();
4413 os->set_section_offsets_need_adjustment();
4419 do_adjust_output_section(Output_section* os)
4424 // Write to a map file.
4426 do_print_to_mapfile(Mapfile* mapfile) const
4427 { mapfile->print_output_data(this, "** BRLT"); }
4430 // Write out the BRLT data.
4432 do_write(Output_file*);
4434 // The reloc section.
4435 Reloc_section* rel_;
4436 Target_powerpc<size, big_endian>* targ_;
4439 // Make the branch lookup table section.
4441 template<int size, bool big_endian>
4443 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4445 if (size == 64 && this->brlt_section_ == NULL)
4447 Reloc_section* brlt_rel = NULL;
4448 bool is_pic = parameters->options().output_is_position_independent();
4451 // When PIC we can't fill in .branch_lt but must initialise at
4452 // runtime via dynamic relocations.
4453 this->rela_dyn_section(layout);
4454 brlt_rel = new Reloc_section(false);
4455 if (this->rela_dyn_->output_section())
4456 this->rela_dyn_->output_section()
4457 ->add_output_section_data(brlt_rel);
4460 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
4461 if (this->plt_ && is_pic && this->plt_->output_section())
4462 this->plt_->output_section()
4463 ->add_output_section_data(this->brlt_section_);
4465 layout->add_output_section_data(".branch_lt",
4466 elfcpp::SHT_PROGBITS,
4467 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4468 this->brlt_section_,
4474 // Write out .branch_lt when non-PIC.
4476 template<int size, bool big_endian>
4478 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4480 if (size == 64 && !parameters->options().output_is_position_independent())
4482 const section_size_type offset = this->offset();
4483 const section_size_type oview_size
4484 = convert_to_section_size_type(this->data_size());
4485 unsigned char* const oview = of->get_output_view(offset, oview_size);
4487 this->targ_->write_branch_lookup_table(oview);
4488 of->write_output_view(offset, oview_size, oview);
4492 static inline uint32_t
4498 static inline uint32_t
4504 static inline uint32_t
4507 return hi(a + 0x8000);
4510 static inline uint64_t
4513 return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4516 static inline uint64_t
4519 return (v + (1ULL << 33)) >> 34;
4525 static const unsigned char eh_frame_cie[12];
4529 const unsigned char Eh_cie<size>::eh_frame_cie[] =
4532 'z', 'R', 0, // Augmentation string.
4533 4, // Code alignment.
4534 0x80 - size / 8 , // Data alignment.
4536 1, // Augmentation size.
4537 (elfcpp::DW_EH_PE_pcrel
4538 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4539 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4542 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4543 static const unsigned char glink_eh_frame_fde_64v1[] =
4545 0, 0, 0, 0, // Replaced with offset to .glink.
4546 0, 0, 0, 0, // Replaced with size of .glink.
4547 0, // Augmentation size.
4548 elfcpp::DW_CFA_advance_loc + 1,
4549 elfcpp::DW_CFA_register, 65, 12,
4550 elfcpp::DW_CFA_advance_loc + 5,
4551 elfcpp::DW_CFA_restore_extended, 65
4554 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4555 static const unsigned char glink_eh_frame_fde_64v2[] =
4557 0, 0, 0, 0, // Replaced with offset to .glink.
4558 0, 0, 0, 0, // Replaced with size of .glink.
4559 0, // Augmentation size.
4560 elfcpp::DW_CFA_advance_loc + 1,
4561 elfcpp::DW_CFA_register, 65, 0,
4562 elfcpp::DW_CFA_advance_loc + 7,
4563 elfcpp::DW_CFA_restore_extended, 65
4566 // Describe __glink_PLTresolve use of LR, 32-bit version.
4567 static const unsigned char glink_eh_frame_fde_32[] =
4569 0, 0, 0, 0, // Replaced with offset to .glink.
4570 0, 0, 0, 0, // Replaced with size of .glink.
4571 0, // Augmentation size.
4572 elfcpp::DW_CFA_advance_loc + 2,
4573 elfcpp::DW_CFA_register, 65, 0,
4574 elfcpp::DW_CFA_advance_loc + 4,
4575 elfcpp::DW_CFA_restore_extended, 65
4578 static const unsigned char default_fde[] =
4580 0, 0, 0, 0, // Replaced with offset to stubs.
4581 0, 0, 0, 0, // Replaced with size of stubs.
4582 0, // Augmentation size.
4583 elfcpp::DW_CFA_nop, // Pad.
4588 template<bool big_endian>
4590 write_insn(unsigned char* p, uint32_t v)
4592 elfcpp::Swap<32, big_endian>::writeval(p, v);
4596 static inline unsigned int
4599 if (!parameters->options().user_set_plt_align())
4600 return size == 64 ? 32 : 8;
4601 return 1 << parameters->options().plt_align();
4604 // Stub_table holds information about plt and long branch stubs.
4605 // Stubs are built in an area following some input section determined
4606 // by group_sections(). This input section is converted to a relaxed
4607 // input section allowing it to be resized to accommodate the stubs
4609 template<int size, bool big_endian>
4610 class Stub_table : public Output_relaxed_input_section
4615 Plt_stub_ent(unsigned int off, unsigned int indx)
4616 : off_(off), indx_(indx), iter_(0), notoc_(0), r2save_(0), localentry0_(0)
4620 unsigned int indx_ : 28;
4621 unsigned int iter_ : 1;
4622 unsigned int notoc_ : 1;
4623 unsigned int r2save_ : 1;
4624 unsigned int localentry0_ : 1;
4626 struct Branch_stub_ent
4628 Branch_stub_ent(unsigned int off, bool notoc, bool save_res)
4629 : off_(off), iter_(false), notoc_(notoc), save_res_(save_res)
4637 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4638 static const Address invalid_address = static_cast<Address>(0) - 1;
4640 Stub_table(Target_powerpc<size, big_endian>* targ,
4641 Output_section* output_section,
4642 const Output_section::Input_section* owner,
4644 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4646 ->section_addralign(owner->shndx())),
4647 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4648 orig_data_size_(owner->current_data_size()),
4649 plt_size_(0), last_plt_size_(0),
4650 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4651 need_save_res_(false), need_resize_(false), resizing_(false),
4654 this->set_output_section(output_section);
4656 std::vector<Output_relaxed_input_section*> new_relaxed;
4657 new_relaxed.push_back(this);
4658 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4661 // Add a plt call stub.
4663 add_plt_call_entry(Address,
4664 const Sized_relobj_file<size, big_endian>*,
4671 add_plt_call_entry(Address,
4672 const Sized_relobj_file<size, big_endian>*,
4678 // Find a given plt call stub.
4680 find_plt_call_entry(const Symbol*) const;
4683 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4684 unsigned int) const;
4687 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4693 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4698 // Add a long branch stub.
4700 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4701 unsigned int, Address, Address, bool);
4703 const Branch_stub_ent*
4704 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4708 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4710 Address max_branch_offset = max_branch_delta<size>(r_type);
4711 if (max_branch_offset == 0)
4713 gold_assert(from != invalid_address);
4714 Address loc = off + this->stub_address();
4715 return loc - from + max_branch_offset < 2 * max_branch_offset;
4719 clear_stubs(bool all)
4721 this->plt_call_stubs_.clear();
4722 this->plt_size_ = 0;
4723 this->long_branch_stubs_.clear();
4724 this->branch_size_ = 0;
4725 this->need_save_res_ = false;
4728 this->last_plt_size_ = 0;
4729 this->last_branch_size_ = 0;
4735 { return need_resize_; }
4738 set_resizing(bool val)
4740 this->resizing_ = val;
4743 this->need_resize_ = false;
4744 this->plt_size_ = 0;
4745 this->branch_size_ = 0;
4746 this->need_save_res_ = false;
4751 set_address_and_size(const Output_section* os, Address off)
4753 Address start_off = off;
4754 off += this->orig_data_size_;
4755 Address my_size = this->plt_size_ + this->branch_size_;
4756 if (this->need_save_res_)
4757 my_size += this->targ_->savres_section()->data_size();
4759 off = align_address(off, this->stub_align());
4760 // Include original section size and alignment padding in size
4761 my_size += off - start_off;
4762 // Ensure new size is always larger than min size
4763 // threshold. Alignment requirement is included in "my_size", so
4764 // increase "my_size" does not invalidate alignment.
4765 if (my_size < this->min_size_threshold_)
4766 my_size = this->min_size_threshold_;
4767 this->reset_address_and_file_offset();
4768 this->set_current_data_size(my_size);
4769 this->set_address_and_file_offset(os->address() + start_off,
4770 os->offset() + start_off);
4775 stub_address() const
4777 return align_address(this->address() + this->orig_data_size_,
4778 this->stub_align());
4784 return align_address(this->offset() + this->orig_data_size_,
4785 this->stub_align());
4790 { return this->plt_size_; }
4794 { return this->branch_size_; }
4797 set_min_size_threshold(Address min_size)
4798 { this->min_size_threshold_ = min_size; }
4801 define_stub_syms(Symbol_table*);
4806 Output_section* os = this->output_section();
4807 if (os->addralign() < this->stub_align())
4809 os->set_addralign(this->stub_align());
4810 // FIXME: get rid of the insane checkpointing.
4811 // We can't increase alignment of the input section to which
4812 // stubs are attached; The input section may be .init which
4813 // is pasted together with other .init sections to form a
4814 // function. Aligning might insert zero padding resulting in
4815 // sigill. However we do need to increase alignment of the
4816 // output section so that the align_address() on offset in
4817 // set_address_and_size() adds the same padding as the
4818 // align_address() on address in stub_address().
4819 // What's more, we need this alignment for the layout done in
4820 // relaxation_loop_body() so that the output section starts at
4821 // a suitably aligned address.
4822 os->checkpoint_set_addralign(this->stub_align());
4824 if (this->last_plt_size_ != this->plt_size_
4825 || this->last_branch_size_ != this->branch_size_)
4827 this->last_plt_size_ = this->plt_size_;
4828 this->last_branch_size_ = this->branch_size_;
4834 // Add .eh_frame info for this stub section.
4836 add_eh_frame(Layout* layout);
4838 // Remove .eh_frame info for this stub section.
4840 remove_eh_frame(Layout* layout);
4842 Target_powerpc<size, big_endian>*
4848 class Plt_stub_key_hash;
4849 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4850 Plt_stub_key_hash> Plt_stub_entries;
4851 class Branch_stub_key;
4852 class Branch_stub_key_hash;
4853 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
4854 Branch_stub_key_hash> Branch_stub_entries;
4856 // Alignment of stub section.
4860 unsigned int min_align = size == 64 ? 32 : 16;
4861 unsigned int user_align = 1 << parameters->options().plt_align();
4862 return std::max(user_align, min_align);
4865 // Return the plt offset for the given call stub.
4867 plt_off(typename Plt_stub_entries::const_iterator p,
4868 const Output_data_plt_powerpc<size, big_endian>** sec) const
4870 const Symbol* gsym = p->first.sym_;
4872 return this->targ_->plt_off(gsym, sec);
4875 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4876 unsigned int local_sym_index = p->first.locsym_;
4877 return this->targ_->plt_off(relobj, local_sym_index, sec);
4881 // Size of a given plt call stub.
4883 plt_call_size(typename Plt_stub_entries::const_iterator p) const;
4886 plt_call_align(unsigned int bytes) const
4888 unsigned int align = param_plt_align<size>();
4889 return (bytes + align - 1) & -align;
4892 // Return long branch stub size.
4894 branch_stub_size(typename Branch_stub_entries::const_iterator p,
4898 build_tls_opt_head(unsigned char** pp,
4899 typename Plt_stub_entries::const_iterator cs);
4902 build_tls_opt_tail(unsigned char* p,
4903 typename Plt_stub_entries::const_iterator cs);
4906 plt_error(const Plt_stub_key& p);
4910 do_write(Output_file*);
4912 // Plt call stub keys.
4916 Plt_stub_key(const Symbol* sym)
4917 : sym_(sym), object_(0), addend_(0), locsym_(0)
4920 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4921 unsigned int locsym_index)
4922 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4925 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4927 unsigned int r_type,
4929 : sym_(sym), object_(0), addend_(0), locsym_(0)
4932 this->addend_ = addend;
4933 else if (parameters->options().output_is_position_independent()
4934 && (r_type == elfcpp::R_PPC_PLTREL24
4935 || r_type == elfcpp::R_POWERPC_PLTCALL))
4937 this->addend_ = addend;
4938 if (this->addend_ >= 32768)
4939 this->object_ = object;
4943 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4944 unsigned int locsym_index,
4945 unsigned int r_type,
4947 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4950 this->addend_ = addend;
4951 else if (parameters->options().output_is_position_independent()
4952 && (r_type == elfcpp::R_PPC_PLTREL24
4953 || r_type == elfcpp::R_POWERPC_PLTCALL))
4954 this->addend_ = addend;
4957 bool operator==(const Plt_stub_key& that) const
4959 return (this->sym_ == that.sym_
4960 && this->object_ == that.object_
4961 && this->addend_ == that.addend_
4962 && this->locsym_ == that.locsym_);
4966 const Sized_relobj_file<size, big_endian>* object_;
4967 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4968 unsigned int locsym_;
4971 class Plt_stub_key_hash
4974 size_t operator()(const Plt_stub_key& ent) const
4976 return (reinterpret_cast<uintptr_t>(ent.sym_)
4977 ^ reinterpret_cast<uintptr_t>(ent.object_)
4983 // Long branch stub keys.
4984 class Branch_stub_key
4987 Branch_stub_key(const Powerpc_relobj<size, big_endian>* obj, Address to)
4988 : dest_(to), toc_base_off_(0)
4991 toc_base_off_ = obj->toc_base_offset();
4994 bool operator==(const Branch_stub_key& that) const
4996 return (this->dest_ == that.dest_
4998 || this->toc_base_off_ == that.toc_base_off_));
5002 unsigned int toc_base_off_;
5005 class Branch_stub_key_hash
5008 size_t operator()(const Branch_stub_key& key) const
5009 { return key.dest_ ^ key.toc_base_off_; }
5012 // In a sane world this would be a global.
5013 Target_powerpc<size, big_endian>* targ_;
5014 // Map sym/object/addend to stub offset.
5015 Plt_stub_entries plt_call_stubs_;
5016 // Map destination address to stub offset.
5017 Branch_stub_entries long_branch_stubs_;
5018 // size of input section
5019 section_size_type orig_data_size_;
5021 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
5022 // Some rare cases cause (PR/20529) fluctuation in stub table
5023 // size, which leads to an endless relax loop. This is to be fixed
5024 // by, after the first few iterations, allowing only increase of
5025 // stub table size. This variable sets the minimal possible size of
5026 // a stub table, it is zero for the first few iterations, then
5027 // increases monotonically.
5028 Address min_size_threshold_;
5029 // Set if this stub group needs a copy of out-of-line register
5030 // save/restore functions.
5031 bool need_save_res_;
5032 // Set when notoc_/r2save_ changes after sizing a stub
5034 // Set when resizing stubs
5036 // Per stub table unique identifier.
5040 // Add a plt call stub, if we do not already have one for this
5041 // sym/object/addend combo.
5043 template<int size, bool big_endian>
5045 Stub_table<size, big_endian>::add_plt_call_entry(
5047 const Sized_relobj_file<size, big_endian>* object,
5049 unsigned int r_type,
5053 Plt_stub_key key(object, gsym, r_type, addend);
5054 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5055 std::pair<typename Plt_stub_entries::iterator, bool> p
5056 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5060 && this->targ_->is_elfv2_localentry0(gsym))
5062 p.first->second.localentry0_ = 1;
5063 this->targ_->set_has_localentry0();
5065 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5067 if (!p.second && !p.first->second.notoc_
5068 && !this->targ_->powerxx_stubs())
5069 this->need_resize_ = true;
5070 p.first->second.notoc_ = 1;
5072 else if (!tocsave && !p.first->second.localentry0_)
5074 if (!p.second && !p.first->second.r2save_)
5075 this->need_resize_ = true;
5076 p.first->second.r2save_ = 1;
5079 if (p.second || (this->resizing_ && !p.first->second.iter_))
5081 if (this->resizing_)
5083 p.first->second.iter_ = 1;
5084 p.first->second.off_ = this->plt_size_;
5086 this->plt_size_ += this->plt_call_size(p.first);
5087 if (this->targ_->is_tls_get_addr_opt(gsym))
5088 this->targ_->set_has_tls_get_addr_opt();
5089 this->plt_size_ = this->plt_call_align(this->plt_size_);
5091 return this->can_reach_stub(from, p.first->second.off_, r_type);
5094 template<int size, bool big_endian>
5096 Stub_table<size, big_endian>::add_plt_call_entry(
5098 const Sized_relobj_file<size, big_endian>* object,
5099 unsigned int locsym_index,
5100 unsigned int r_type,
5104 Plt_stub_key key(object, locsym_index, r_type, addend);
5105 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5106 std::pair<typename Plt_stub_entries::iterator, bool> p
5107 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5111 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5113 p.first->second.localentry0_ = 1;
5114 this->targ_->set_has_localentry0();
5116 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5118 if (!p.second && !p.first->second.notoc_
5119 && !this->targ_->powerxx_stubs())
5120 this->need_resize_ = true;
5121 p.first->second.notoc_ = 1;
5123 else if (!tocsave && !p.first->second.localentry0_)
5125 if (!p.second && !p.first->second.r2save_)
5126 this->need_resize_ = true;
5127 p.first->second.r2save_ = 1;
5130 if (p.second || (this->resizing_ && !p.first->second.iter_))
5132 if (this->resizing_)
5134 p.first->second.iter_ = 1;
5135 p.first->second.off_ = this->plt_size_;
5137 this->plt_size_ += this->plt_call_size(p.first);
5138 this->plt_size_ = this->plt_call_align(this->plt_size_);
5140 return this->can_reach_stub(from, p.first->second.off_, r_type);
5143 // Find a plt call stub.
5145 template<int size, bool big_endian>
5146 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5147 Stub_table<size, big_endian>::find_plt_call_entry(
5148 const Sized_relobj_file<size, big_endian>* object,
5150 unsigned int r_type,
5151 Address addend) const
5153 Plt_stub_key key(object, gsym, r_type, addend);
5154 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5155 if (p == this->plt_call_stubs_.end())
5160 template<int size, bool big_endian>
5161 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5162 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
5164 Plt_stub_key key(gsym);
5165 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5166 if (p == this->plt_call_stubs_.end())
5171 template<int size, bool big_endian>
5172 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5173 Stub_table<size, big_endian>::find_plt_call_entry(
5174 const Sized_relobj_file<size, big_endian>* object,
5175 unsigned int locsym_index,
5176 unsigned int r_type,
5177 Address addend) const
5179 Plt_stub_key key(object, locsym_index, r_type, addend);
5180 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5181 if (p == this->plt_call_stubs_.end())
5186 template<int size, bool big_endian>
5187 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5188 Stub_table<size, big_endian>::find_plt_call_entry(
5189 const Sized_relobj_file<size, big_endian>* object,
5190 unsigned int locsym_index) const
5192 Plt_stub_key key(object, locsym_index);
5193 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5194 if (p == this->plt_call_stubs_.end())
5199 // Add a long branch stub if we don't already have one to given
5202 template<int size, bool big_endian>
5204 Stub_table<size, big_endian>::add_long_branch_entry(
5205 const Powerpc_relobj<size, big_endian>* object,
5206 unsigned int r_type,
5211 Branch_stub_key key(object, to);
5212 bool notoc = (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC);
5213 Branch_stub_ent ent(this->branch_size_, notoc, save_res);
5214 std::pair<typename Branch_stub_entries::iterator, bool> p
5215 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
5216 if (notoc && !p.first->second.notoc_)
5218 this->need_resize_ = true;
5219 p.first->second.notoc_ = true;
5221 gold_assert(save_res == p.first->second.save_res_);
5222 if (p.second || (this->resizing_ && !p.first->second.iter_))
5224 if (this->resizing_)
5226 p.first->second.iter_ = 1;
5227 p.first->second.off_ = this->branch_size_;
5230 this->need_save_res_ = true;
5233 bool need_lt = false;
5234 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5235 this->branch_size_ += stub_size;
5236 if (size == 64 && need_lt)
5237 this->targ_->add_branch_lookup_table(to);
5240 return this->can_reach_stub(from, p.first->second.off_, r_type);
5243 // Find long branch stub offset.
5245 template<int size, bool big_endian>
5246 const typename Stub_table<size, big_endian>::Branch_stub_ent*
5247 Stub_table<size, big_endian>::find_long_branch_entry(
5248 const Powerpc_relobj<size, big_endian>* object,
5251 Branch_stub_key key(object, to);
5252 typename Branch_stub_entries::const_iterator p
5253 = this->long_branch_stubs_.find(key);
5254 if (p == this->long_branch_stubs_.end())
5259 template<bool big_endian>
5261 eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
5265 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5266 else if (delta < 256)
5268 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5269 fde.push_back(delta);
5271 else if (delta < 65536)
5273 fde.resize(fde.size() + 3);
5274 unsigned char *p = &*fde.end() - 3;
5275 *p++ = elfcpp::DW_CFA_advance_loc2;
5276 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5280 fde.resize(fde.size() + 5);
5281 unsigned char *p = &*fde.end() - 5;
5282 *p++ = elfcpp::DW_CFA_advance_loc4;
5283 elfcpp::Swap<32, big_endian>::writeval(p, delta);
5287 template<typename T>
5289 stub_sort(T s1, T s2)
5291 return s1->second.off_ < s2->second.off_;
5294 // Add .eh_frame info for this stub section. Unlike other linker
5295 // generated .eh_frame this is added late in the link, because we
5296 // only want the .eh_frame info if this particular stub section is
5299 template<int size, bool big_endian>
5301 Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5304 || !parameters->options().ld_generated_unwind_info())
5307 // Since we add stub .eh_frame info late, it must be placed
5308 // after all other linker generated .eh_frame info so that
5309 // merge mapping need not be updated for input sections.
5310 // There is no provision to use a different CIE to that used
5312 if (!this->targ_->has_glink())
5315 typedef typename Plt_stub_entries::const_iterator plt_iter;
5316 std::vector<plt_iter> calls;
5317 if (!this->plt_call_stubs_.empty())
5318 for (plt_iter cs = this->plt_call_stubs_.begin();
5319 cs != this->plt_call_stubs_.end();
5321 if ((this->targ_->is_tls_get_addr_opt(cs->first.sym_)
5322 && cs->second.r2save_
5323 && !cs->second.localentry0_)
5324 || (cs->second.notoc_
5325 && !this->targ_->powerxx_stubs()))
5326 calls.push_back(cs);
5327 if (calls.size() > 1)
5328 std::stable_sort(calls.begin(), calls.end(),
5329 stub_sort<plt_iter>);
5331 typedef typename Branch_stub_entries::const_iterator branch_iter;
5332 std::vector<branch_iter> branches;
5333 if (!this->long_branch_stubs_.empty()
5334 && !this->targ_->powerxx_stubs())
5335 for (branch_iter bs = this->long_branch_stubs_.begin();
5336 bs != this->long_branch_stubs_.end();
5338 if (bs->second.notoc_)
5339 branches.push_back(bs);
5340 if (branches.size() > 1)
5341 std::stable_sort(branches.begin(), branches.end(),
5342 stub_sort<branch_iter>);
5344 if (calls.empty() && branches.empty())
5347 unsigned int last_eh_loc = 0;
5348 // offset pcrel sdata4, size udata4, and augmentation size byte.
5349 std::vector<unsigned char> fde(9, 0);
5351 for (unsigned int i = 0; i < calls.size(); i++)
5353 plt_iter cs = calls[i];
5354 unsigned int off = cs->second.off_;
5355 // The __tls_get_addr_opt call stub needs to describe where
5356 // it saves LR, to support exceptions that might be thrown
5357 // from __tls_get_addr, and to support asynchronous exceptions.
5358 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5361 if (cs->second.r2save_
5362 && !cs->second.localentry0_)
5365 eh_advance<big_endian>(fde, off - last_eh_loc);
5366 fde.resize(fde.size() + 6);
5367 unsigned char* p = &*fde.end() - 6;
5368 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5370 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5371 unsigned int delta = this->plt_call_size(cs) - 4 - 9 * 4;
5372 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5373 *p++ = elfcpp::DW_CFA_restore_extended;
5375 last_eh_loc = off + delta;
5379 // notoc stubs also should describe LR changes, to support
5380 // asynchronous exceptions.
5381 off += (cs->second.r2save_ ? 4 : 0) + 8;
5382 eh_advance<big_endian>(fde, off - last_eh_loc);
5383 fde.resize(fde.size() + 6);
5384 unsigned char* p = &*fde.end() - 6;
5385 *p++ = elfcpp::DW_CFA_register;
5388 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5389 *p++ = elfcpp::DW_CFA_restore_extended;
5391 last_eh_loc = off + 8;
5394 for (unsigned int i = 0; i < branches.size(); i++)
5396 branch_iter bs = branches[i];
5397 unsigned int off = bs->second.off_ + 8;
5398 eh_advance<big_endian>(fde, off - last_eh_loc);
5399 fde.resize(fde.size() + 6);
5400 unsigned char* p = &*fde.end() - 6;
5401 *p++ = elfcpp::DW_CFA_register;
5404 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5405 *p++ = elfcpp::DW_CFA_restore_extended;
5407 last_eh_loc = off + 8;
5410 layout->add_eh_frame_for_plt(this,
5411 Eh_cie<size>::eh_frame_cie,
5412 sizeof (Eh_cie<size>::eh_frame_cie),
5413 &*fde.begin(), fde.size());
5416 template<int size, bool big_endian>
5418 Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5421 && parameters->options().ld_generated_unwind_info()
5422 && this->targ_->has_glink())
5423 layout->remove_eh_frame_for_plt(this,
5424 Eh_cie<size>::eh_frame_cie,
5425 sizeof (Eh_cie<size>::eh_frame_cie));
5428 // A class to handle .glink.
5430 template<int size, bool big_endian>
5431 class Output_data_glink : public Output_section_data
5434 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5435 static const Address invalid_address = static_cast<Address>(0) - 1;
5437 Output_data_glink(Target_powerpc<size, big_endian>* targ)
5438 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5439 end_branch_table_(), ge_size_(0)
5443 add_eh_frame(Layout* layout);
5446 add_global_entry(const Symbol*);
5449 find_global_entry(const Symbol*) const;
5452 global_entry_align(unsigned int off) const
5454 unsigned int align = param_plt_align<size>();
5455 return (off + align - 1) & -align;
5459 global_entry_off() const
5461 return this->global_entry_align(this->end_branch_table_);
5465 global_entry_address() const
5467 gold_assert(this->is_data_size_valid());
5468 return this->address() + this->global_entry_off();
5472 pltresolve_size() const
5476 + (this->targ_->abiversion() < 2 ? 11 * 4 : 14 * 4));
5481 // Write to a map file.
5483 do_print_to_mapfile(Mapfile* mapfile) const
5484 { mapfile->print_output_data(this, _("** glink")); }
5488 set_final_data_size();
5492 do_write(Output_file*);
5494 // Allows access to .got and .plt for do_write.
5495 Target_powerpc<size, big_endian>* targ_;
5497 // Map sym to stub offset.
5498 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5499 Global_entry_stub_entries global_entry_stubs_;
5501 unsigned int end_branch_table_, ge_size_;
5504 template<int size, bool big_endian>
5506 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5508 if (!parameters->options().ld_generated_unwind_info())
5513 if (this->targ_->abiversion() < 2)
5514 layout->add_eh_frame_for_plt(this,
5515 Eh_cie<64>::eh_frame_cie,
5516 sizeof (Eh_cie<64>::eh_frame_cie),
5517 glink_eh_frame_fde_64v1,
5518 sizeof (glink_eh_frame_fde_64v1));
5520 layout->add_eh_frame_for_plt(this,
5521 Eh_cie<64>::eh_frame_cie,
5522 sizeof (Eh_cie<64>::eh_frame_cie),
5523 glink_eh_frame_fde_64v2,
5524 sizeof (glink_eh_frame_fde_64v2));
5528 // 32-bit .glink can use the default since the CIE return
5529 // address reg, LR, is valid.
5530 layout->add_eh_frame_for_plt(this,
5531 Eh_cie<32>::eh_frame_cie,
5532 sizeof (Eh_cie<32>::eh_frame_cie),
5534 sizeof (default_fde));
5535 // Except where LR is used in a PIC __glink_PLTresolve.
5536 if (parameters->options().output_is_position_independent())
5537 layout->add_eh_frame_for_plt(this,
5538 Eh_cie<32>::eh_frame_cie,
5539 sizeof (Eh_cie<32>::eh_frame_cie),
5540 glink_eh_frame_fde_32,
5541 sizeof (glink_eh_frame_fde_32));
5545 template<int size, bool big_endian>
5547 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5549 unsigned int off = this->global_entry_align(this->ge_size_);
5550 std::pair<typename Global_entry_stub_entries::iterator, bool> p
5551 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
5553 this->ge_size_ = off + 16;
5556 template<int size, bool big_endian>
5557 typename Output_data_glink<size, big_endian>::Address
5558 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5560 typename Global_entry_stub_entries::const_iterator p
5561 = this->global_entry_stubs_.find(gsym);
5562 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5565 template<int size, bool big_endian>
5567 Output_data_glink<size, big_endian>::set_final_data_size()
5569 unsigned int count = this->targ_->plt_entry_count();
5570 section_size_type total = 0;
5576 // space for branch table
5577 total += 4 * (count - 1);
5579 total += -total & 15;
5580 total += this->pltresolve_size();
5584 total += this->pltresolve_size();
5586 // space for branch table
5588 if (this->targ_->abiversion() < 2)
5592 total += 4 * (count - 0x8000);
5596 this->end_branch_table_ = total;
5597 total = this->global_entry_align(total);
5598 total += this->ge_size_;
5600 this->set_data_size(total);
5603 // Define symbols on stubs, identifying the stub.
5605 template<int size, bool big_endian>
5607 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5609 if (!this->plt_call_stubs_.empty())
5611 // The key for the plt call stub hash table includes addresses,
5612 // therefore traversal order depends on those addresses, which
5613 // can change between runs if gold is a PIE. Unfortunately the
5614 // output .symtab ordering depends on the order in which symbols
5615 // are added to the linker symtab. We want reproducible output
5616 // so must sort the call stub symbols.
5617 typedef typename Plt_stub_entries::const_iterator plt_iter;
5618 std::vector<plt_iter> sorted;
5619 sorted.resize(this->plt_call_stubs_.size());
5621 for (plt_iter cs = this->plt_call_stubs_.begin();
5622 cs != this->plt_call_stubs_.end();
5624 sorted[cs->second.indx_] = cs;
5626 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5628 plt_iter cs = sorted[i];
5631 if (cs->first.addend_ != 0)
5632 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
5635 if (cs->first.object_)
5637 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5638 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5639 sprintf(obj, "%x:", ppcobj->uniq());
5642 const char *symname;
5643 if (cs->first.sym_ == NULL)
5645 sprintf(localname, "%x", cs->first.locsym_);
5646 symname = localname;
5648 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5649 symname = this->targ_->tls_get_addr_opt()->name();
5651 symname = cs->first.sym_->name();
5652 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5653 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
5655 = this->stub_address() - this->address() + cs->second.off_;
5656 unsigned int stub_size = this->plt_call_align(this->plt_call_size(cs));
5657 this->targ_->define_local(symtab, name, this, value, stub_size);
5661 typedef typename Branch_stub_entries::const_iterator branch_iter;
5662 for (branch_iter bs = this->long_branch_stubs_.begin();
5663 bs != this->long_branch_stubs_.end();
5666 if (bs->second.save_res_)
5669 char* name = new char[8 + 13 + 16 + 1];
5670 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5671 static_cast<unsigned long long>(bs->first.dest_));
5672 Address value = (this->stub_address() - this->address()
5673 + this->plt_size_ + bs->second.off_);
5674 bool need_lt = false;
5675 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
5676 this->targ_->define_local(symtab, name, this, value, stub_size);
5680 // Emit the start of a __tls_get_addr_opt plt call stub.
5682 template<int size, bool big_endian>
5684 Stub_table<size, big_endian>::build_tls_opt_head(
5686 typename Plt_stub_entries::const_iterator cs)
5688 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5690 unsigned char* p = *pp;
5693 write_insn<big_endian>(p, ld_11_3 + 0);
5695 write_insn<big_endian>(p, ld_12_3 + 8);
5697 write_insn<big_endian>(p, mr_0_3);
5699 write_insn<big_endian>(p, cmpdi_11_0);
5701 write_insn<big_endian>(p, add_3_12_13);
5703 write_insn<big_endian>(p, beqlr);
5705 write_insn<big_endian>(p, mr_3_0);
5707 if (cs->second.r2save_ && !cs->second.localentry0_)
5709 write_insn<big_endian>(p, mflr_11);
5711 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
5717 write_insn<big_endian>(p, lwz_11_3 + 0);
5719 write_insn<big_endian>(p, lwz_12_3 + 4);
5721 write_insn<big_endian>(p, mr_0_3);
5723 write_insn<big_endian>(p, cmpwi_11_0);
5725 write_insn<big_endian>(p, add_3_12_2);
5727 write_insn<big_endian>(p, beqlr);
5729 write_insn<big_endian>(p, mr_3_0);
5731 write_insn<big_endian>(p, nop);
5740 // Emit the tail of a __tls_get_addr_opt plt call stub.
5742 template<int size, bool big_endian>
5744 Stub_table<size, big_endian>::build_tls_opt_tail(
5746 typename Plt_stub_entries::const_iterator cs)
5749 && cs->second.r2save_
5750 && !cs->second.localentry0_
5751 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5753 write_insn<big_endian>(p, bctrl);
5755 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5757 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5759 write_insn<big_endian>(p, mtlr_11);
5761 write_insn<big_endian>(p, blr);
5767 // Emit pc-relative plt call stub code.
5769 template<bool big_endian>
5770 static unsigned char*
5771 build_powerxx_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
5774 if (off - odd + (1ULL << 33) < 1ULL << 34)
5779 write_insn<big_endian>(p, nop);
5787 write_insn<big_endian>(p, insn >> 32);
5789 write_insn<big_endian>(p, insn & 0xffffffff);
5791 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
5794 write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
5798 write_insn<big_endian>(p, sldi_11_11_34);
5801 insn = paddi_12_pc | d34(off);
5802 write_insn<big_endian>(p, insn >> 32);
5804 write_insn<big_endian>(p, insn & 0xffffffff);
5808 write_insn<big_endian>(p, sldi_11_11_34);
5812 write_insn<big_endian>(p, ldx_12_11_12);
5814 write_insn<big_endian>(p, add_12_11_12);
5819 write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
5821 write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
5825 write_insn<big_endian>(p, sldi_11_11_34);
5828 insn = paddi_12_pc | d34(off);
5829 write_insn<big_endian>(p, insn >> 32);
5831 write_insn<big_endian>(p, insn & 0xffffffff);
5835 write_insn<big_endian>(p, sldi_11_11_34);
5839 write_insn<big_endian>(p, ldx_12_11_12);
5841 write_insn<big_endian>(p, add_12_11_12);
5847 // Gets the address of a label (1:) in r11 and builds an offset in r12,
5848 // then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
5853 // lis %r12,xxx-1b@highest
5854 // ori %r12,%r12,xxx-1b@higher
5855 // sldi %r12,%r12,32
5856 // oris %r12,%r12,xxx-1b@high
5857 // ori %r12,%r12,xxx-1b@l
5858 // add/ldx %r12,%r11,%r12
5860 template<bool big_endian>
5861 static unsigned char*
5862 build_notoc_offset(unsigned char* p, uint64_t off, bool load)
5864 write_insn<big_endian>(p, mflr_12);
5866 write_insn<big_endian>(p, bcl_20_31);
5868 write_insn<big_endian>(p, mflr_11);
5870 write_insn<big_endian>(p, mtlr_12);
5872 if (off + 0x8000 < 0x10000)
5875 write_insn<big_endian>(p, ld_12_11 + l(off));
5877 write_insn<big_endian>(p, addi_12_11 + l(off));
5879 else if (off + 0x80008000ULL < 0x100000000ULL)
5881 write_insn<big_endian>(p, addis_12_11 + ha(off));
5884 write_insn<big_endian>(p, ld_12_12 + l(off));
5886 write_insn<big_endian>(p, addi_12_12 + l(off));
5890 if (off + 0x800000000000ULL < 0x1000000000000ULL)
5892 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
5897 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
5899 if (((off >> 32) & 0xffff) != 0)
5901 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
5905 if (((off >> 32) & 0xffffffffULL) != 0)
5907 write_insn<big_endian>(p, sldi_12_12_32);
5912 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
5917 write_insn<big_endian>(p, ori_12_12_0 + l(off));
5921 write_insn<big_endian>(p, ldx_12_11_12);
5923 write_insn<big_endian>(p, add_12_11_12);
5929 // Size of a given plt call stub.
5931 template<int size, bool big_endian>
5933 Stub_table<size, big_endian>::plt_call_size(
5934 typename Plt_stub_entries::const_iterator p) const
5938 const Symbol* gsym = p->first.sym_;
5940 + (this->targ_->is_tls_get_addr_opt(gsym) ? 8 * 4 : 0));
5943 const Output_data_plt_powerpc<size, big_endian>* plt;
5944 uint64_t plt_addr = this->plt_off(p, &plt);
5945 plt_addr += plt->address();
5946 unsigned int bytes = 0;
5947 const Symbol* gsym = p->first.sym_;
5948 if (this->targ_->is_tls_get_addr_opt(gsym))
5950 if (p->second.r2save_ && !p->second.localentry0_)
5956 if (p->second.r2save_)
5959 if (this->targ_->powerxx_stubs())
5961 uint64_t from = this->stub_address() + p->second.off_ + bytes;
5964 uint64_t odd = from & 4;
5965 uint64_t off = plt_addr - from;
5966 if (off - odd + (1ULL << 33) < 1ULL << 34)
5967 bytes += odd + 4 * 4;
5968 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
5975 if (p->second.notoc_)
5977 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
5980 uint64_t off = plt_addr - from;
5981 if (off + 0x8000 < 0x10000)
5983 else if (off + 0x80008000ULL < 0x100000000ULL)
5988 if (off + 0x800000000000ULL >= 0x1000000000000ULL
5989 && ((off >> 32) & 0xffff) != 0)
5991 if (((off >> 32) & 0xffffffffULL) != 0)
6001 uint64_t got_addr = this->targ_->got_section()->output_section()->address();
6002 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6003 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
6004 got_addr += ppcobj->toc_base_offset();
6005 uint64_t off = plt_addr - got_addr;
6006 bytes += 3 * 4 + 4 * (ha(off) != 0);
6007 if (this->targ_->abiversion() < 2)
6009 bool static_chain = parameters->options().plt_static_chain();
6010 bool thread_safe = this->targ_->plt_thread_safe();
6014 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6019 // Return long branch stub size.
6021 template<int size, bool big_endian>
6023 Stub_table<size, big_endian>::branch_stub_size(
6024 typename Branch_stub_entries::const_iterator p,
6027 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6030 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6032 if (parameters->options().output_is_position_independent())
6037 uint64_t off = p->first.dest_ - loc;
6038 if (p->second.notoc_)
6040 if (this->targ_->powerxx_stubs())
6042 Address odd = loc & 4;
6043 if (off + (1 << 25) < 2 << 25)
6045 if (off - odd + (1ULL << 33) < 1ULL << 34)
6047 if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6052 if (off + 0x8000 < 0x10000)
6054 if (off + 0x80008000ULL < 0x100000000ULL)
6056 if (off + 24 + (1 << 25) < 2 << 25)
6060 unsigned int bytes = 32;
6061 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6062 && ((off >> 32) & 0xffff) != 0)
6064 if (((off >> 32) & 0xffffffffULL) != 0)
6073 if (off + (1 << 25) < 2 << 25)
6075 if (!this->targ_->powerxx_stubs())
6080 template<int size, bool big_endian>
6082 Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6085 gold_error(_("linkage table error against `%s'"),
6086 p.sym_->demangled_name().c_str());
6088 gold_error(_("linkage table error against `%s:[local %u]'"),
6089 p.object_->name().c_str(),
6093 // Write out plt and long branch stub code.
6095 template<int size, bool big_endian>
6097 Stub_table<size, big_endian>::do_write(Output_file* of)
6099 if (this->plt_call_stubs_.empty()
6100 && this->long_branch_stubs_.empty())
6103 const section_size_type start_off = this->offset();
6104 const section_size_type off = this->stub_offset();
6105 const section_size_type oview_size =
6106 convert_to_section_size_type(this->data_size() - (off - start_off));
6107 unsigned char* const oview = of->get_output_view(off, oview_size);
6111 && this->targ_->powerxx_stubs())
6113 if (!this->plt_call_stubs_.empty())
6115 // Write out plt call stubs.
6116 typename Plt_stub_entries::const_iterator cs;
6117 for (cs = this->plt_call_stubs_.begin();
6118 cs != this->plt_call_stubs_.end();
6121 p = oview + cs->second.off_;
6122 this->build_tls_opt_head(&p, cs);
6123 if (cs->second.r2save_)
6125 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6128 const Output_data_plt_powerpc<size, big_endian>* plt;
6129 Address pltoff = this->plt_off(cs, &plt);
6130 Address plt_addr = pltoff + plt->address();
6131 Address from = this->stub_address() + (p - oview);
6132 Address delta = plt_addr - from;
6133 p = build_powerxx_offset<big_endian>(p, delta, from & 4, true);
6134 write_insn<big_endian>(p, mtctr_12);
6136 if (!this->build_tls_opt_tail(p, cs))
6137 write_insn<big_endian>(p, bctr);
6141 // Write out long branch stubs.
6142 typename Branch_stub_entries::const_iterator bs;
6143 for (bs = this->long_branch_stubs_.begin();
6144 bs != this->long_branch_stubs_.end();
6147 if (bs->second.save_res_)
6149 Address off = this->plt_size_ + bs->second.off_;
6151 Address loc = this->stub_address() + off;
6152 Address delta = bs->first.dest_ - loc;
6153 if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6155 unsigned char* startp = p;
6156 p = build_powerxx_offset<big_endian>(p, delta, loc & 4, false);
6157 delta -= p - startp;
6159 if (delta + (1 << 25) < 2 << 25)
6160 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6163 write_insn<big_endian>(p, mtctr_12);
6165 write_insn<big_endian>(p, bctr);
6169 else if (size == 64)
6171 const Output_data_got_powerpc<size, big_endian>* got
6172 = this->targ_->got_section();
6173 Address got_os_addr = got->output_section()->address();
6175 if (!this->plt_call_stubs_.empty()
6176 && this->targ_->abiversion() >= 2)
6178 // Write out plt call stubs for ELFv2.
6179 typename Plt_stub_entries::const_iterator cs;
6180 for (cs = this->plt_call_stubs_.begin();
6181 cs != this->plt_call_stubs_.end();
6184 const Output_data_plt_powerpc<size, big_endian>* plt;
6185 Address pltoff = this->plt_off(cs, &plt);
6186 Address plt_addr = pltoff + plt->address();
6188 p = oview + cs->second.off_;
6189 this->build_tls_opt_head(&p, cs);
6190 if (cs->second.r2save_)
6192 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6195 if (cs->second.notoc_)
6197 Address from = this->stub_address() + (p - oview) + 8;
6198 Address off = plt_addr - from;
6199 p = build_notoc_offset<big_endian>(p, off, true);
6203 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6204 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6205 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6206 Address off = plt_addr - got_addr;
6208 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6209 this->plt_error(cs->first);
6213 write_insn<big_endian>(p, addis_12_2 + ha(off));
6215 write_insn<big_endian>(p, ld_12_12 + l(off));
6220 write_insn<big_endian>(p, ld_12_2 + l(off));
6224 write_insn<big_endian>(p, mtctr_12);
6226 if (!this->build_tls_opt_tail(p, cs))
6227 write_insn<big_endian>(p, bctr);
6230 else if (!this->plt_call_stubs_.empty())
6232 // Write out plt call stubs for ELFv1.
6233 typename Plt_stub_entries::const_iterator cs;
6234 for (cs = this->plt_call_stubs_.begin();
6235 cs != this->plt_call_stubs_.end();
6238 const Output_data_plt_powerpc<size, big_endian>* plt;
6239 Address pltoff = this->plt_off(cs, &plt);
6240 Address plt_addr = pltoff + plt->address();
6241 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6242 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6243 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6244 Address off = plt_addr - got_addr;
6246 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6247 || cs->second.notoc_)
6248 this->plt_error(cs->first);
6250 bool static_chain = parameters->options().plt_static_chain();
6251 bool thread_safe = this->targ_->plt_thread_safe();
6252 bool use_fake_dep = false;
6253 Address cmp_branch_off = 0;
6256 unsigned int pltindex
6257 = ((pltoff - this->targ_->first_plt_entry_offset())
6258 / this->targ_->plt_entry_size());
6260 = (this->targ_->glink_section()->pltresolve_size()
6262 if (pltindex > 32768)
6263 glinkoff += (pltindex - 32768) * 4;
6265 = this->targ_->glink_section()->address() + glinkoff;
6267 = (this->stub_address() + cs->second.off_ + 20
6268 + 4 * cs->second.r2save_
6269 + 4 * (ha(off) != 0)
6270 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6271 + 4 * static_chain);
6272 cmp_branch_off = to - from;
6273 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6276 p = oview + cs->second.off_;
6277 if (this->build_tls_opt_head(&p, cs))
6278 use_fake_dep = thread_safe;
6279 if (cs->second.r2save_)
6281 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6286 write_insn<big_endian>(p, addis_11_2 + ha(off));
6288 write_insn<big_endian>(p, ld_12_11 + l(off));
6290 if (ha(off + 8 + 8 * static_chain) != ha(off))
6292 write_insn<big_endian>(p, addi_11_11 + l(off));
6296 write_insn<big_endian>(p, mtctr_12);
6300 write_insn<big_endian>(p, xor_2_12_12);
6302 write_insn<big_endian>(p, add_11_11_2);
6305 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
6309 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
6315 write_insn<big_endian>(p, ld_12_2 + l(off));
6317 if (ha(off + 8 + 8 * static_chain) != ha(off))
6319 write_insn<big_endian>(p, addi_2_2 + l(off));
6323 write_insn<big_endian>(p, mtctr_12);
6327 write_insn<big_endian>(p, xor_11_12_12);
6329 write_insn<big_endian>(p, add_2_2_11);
6334 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6337 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
6340 if (this->build_tls_opt_tail(p, cs))
6342 else if (thread_safe && !use_fake_dep)
6344 write_insn<big_endian>(p, cmpldi_2_0);
6346 write_insn<big_endian>(p, bnectr_p4);
6348 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6351 write_insn<big_endian>(p, bctr);
6355 // Write out long branch stubs.
6356 typename Branch_stub_entries::const_iterator bs;
6357 for (bs = this->long_branch_stubs_.begin();
6358 bs != this->long_branch_stubs_.end();
6361 if (bs->second.save_res_)
6363 Address off = this->plt_size_ + bs->second.off_;
6365 Address loc = this->stub_address() + off;
6366 Address delta = bs->first.dest_ - loc;
6367 if (bs->second.notoc_)
6369 unsigned char* startp = p;
6370 p = build_notoc_offset<big_endian>(p, off, false);
6371 delta -= p - startp;
6373 else if (delta + (1 << 25) >= 2 << 25)
6376 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6377 gold_assert(brlt_addr != invalid_address);
6378 brlt_addr += this->targ_->brlt_section()->address();
6379 Address got_addr = got_os_addr + bs->first.toc_base_off_;
6380 Address brltoff = brlt_addr - got_addr;
6381 if (ha(brltoff) == 0)
6383 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6388 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6390 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6394 if (delta + (1 << 25) < 2 << 25)
6395 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6398 write_insn<big_endian>(p, mtctr_12);
6400 write_insn<big_endian>(p, bctr);
6406 if (!this->plt_call_stubs_.empty())
6408 // The address of _GLOBAL_OFFSET_TABLE_.
6409 Address g_o_t = invalid_address;
6411 // Write out plt call stubs.
6412 typename Plt_stub_entries::const_iterator cs;
6413 for (cs = this->plt_call_stubs_.begin();
6414 cs != this->plt_call_stubs_.end();
6417 const Output_data_plt_powerpc<size, big_endian>* plt;
6418 Address plt_addr = this->plt_off(cs, &plt);
6419 plt_addr += plt->address();
6421 p = oview + cs->second.off_;
6422 this->build_tls_opt_head(&p, cs);
6423 if (parameters->options().output_is_position_independent())
6426 const Powerpc_relobj<size, big_endian>* ppcobj
6427 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6428 (cs->first.object_));
6429 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6431 unsigned int got2 = ppcobj->got2_shndx();
6432 got_addr = ppcobj->get_output_section_offset(got2);
6433 gold_assert(got_addr != invalid_address);
6434 got_addr += (ppcobj->output_section(got2)->address()
6435 + cs->first.addend_);
6439 if (g_o_t == invalid_address)
6441 const Output_data_got_powerpc<size, big_endian>* got
6442 = this->targ_->got_section();
6443 g_o_t = got->address() + got->g_o_t();
6448 Address off = plt_addr - got_addr;
6450 write_insn<big_endian>(p, lwz_11_30 + l(off));
6453 write_insn<big_endian>(p, addis_11_30 + ha(off));
6455 write_insn<big_endian>(p, lwz_11_11 + l(off));
6460 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6462 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
6465 write_insn<big_endian>(p, mtctr_11);
6467 write_insn<big_endian>(p, bctr);
6471 // Write out long branch stubs.
6472 typename Branch_stub_entries::const_iterator bs;
6473 for (bs = this->long_branch_stubs_.begin();
6474 bs != this->long_branch_stubs_.end();
6477 if (bs->second.save_res_)
6479 Address off = this->plt_size_ + bs->second.off_;
6481 Address loc = this->stub_address() + off;
6482 Address delta = bs->first.dest_ - loc;
6483 if (delta + (1 << 25) < 2 << 25)
6484 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6485 else if (!parameters->options().output_is_position_independent())
6487 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6489 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
6494 write_insn<big_endian>(p, mflr_0);
6496 write_insn<big_endian>(p, bcl_20_31);
6498 write_insn<big_endian>(p, mflr_12);
6500 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6502 write_insn<big_endian>(p, addi_12_12 + l(delta));
6504 write_insn<big_endian>(p, mtlr_0);
6507 write_insn<big_endian>(p, mtctr_12);
6509 write_insn<big_endian>(p, bctr);
6512 if (this->need_save_res_)
6514 p = oview + this->plt_size_ + this->branch_size_;
6515 memcpy (p, this->targ_->savres_section()->contents(),
6516 this->targ_->savres_section()->data_size());
6520 // Write out .glink.
6522 template<int size, bool big_endian>
6524 Output_data_glink<size, big_endian>::do_write(Output_file* of)
6526 const section_size_type off = this->offset();
6527 const section_size_type oview_size =
6528 convert_to_section_size_type(this->data_size());
6529 unsigned char* const oview = of->get_output_view(off, oview_size);
6532 // The base address of the .plt section.
6533 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
6534 Address plt_base = this->targ_->plt_section()->address();
6538 if (this->end_branch_table_ != 0)
6540 // Write pltresolve stub.
6542 Address after_bcl = this->address() + 16;
6543 Address pltoff = plt_base - after_bcl;
6545 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
6547 if (this->targ_->abiversion() < 2)
6549 write_insn<big_endian>(p, mflr_12), p += 4;
6550 write_insn<big_endian>(p, bcl_20_31), p += 4;
6551 write_insn<big_endian>(p, mflr_11), p += 4;
6552 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
6553 write_insn<big_endian>(p, mtlr_12), p += 4;
6554 write_insn<big_endian>(p, add_11_2_11), p += 4;
6555 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6556 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
6557 write_insn<big_endian>(p, mtctr_12), p += 4;
6558 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
6562 write_insn<big_endian>(p, mflr_0), p += 4;
6563 write_insn<big_endian>(p, bcl_20_31), p += 4;
6564 write_insn<big_endian>(p, mflr_11), p += 4;
6565 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
6566 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
6567 write_insn<big_endian>(p, mtlr_0), p += 4;
6568 write_insn<big_endian>(p, sub_12_12_11), p += 4;
6569 write_insn<big_endian>(p, add_11_2_11), p += 4;
6570 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
6571 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6572 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
6573 write_insn<big_endian>(p, mtctr_12), p += 4;
6574 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
6576 write_insn<big_endian>(p, bctr), p += 4;
6577 gold_assert(p == oview + this->pltresolve_size());
6579 // Write lazy link call stubs.
6581 while (p < oview + this->end_branch_table_)
6583 if (this->targ_->abiversion() < 2)
6587 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
6591 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
6592 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
6595 uint32_t branch_off = 8 - (p - oview);
6596 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
6601 Address plt_base = this->targ_->plt_section()->address();
6602 Address iplt_base = invalid_address;
6603 unsigned int global_entry_off = this->global_entry_off();
6604 Address global_entry_base = this->address() + global_entry_off;
6605 typename Global_entry_stub_entries::const_iterator ge;
6606 for (ge = this->global_entry_stubs_.begin();
6607 ge != this->global_entry_stubs_.end();
6610 p = oview + global_entry_off + ge->second;
6611 Address plt_addr = ge->first->plt_offset();
6612 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
6613 && ge->first->can_use_relative_reloc(false))
6615 if (iplt_base == invalid_address)
6616 iplt_base = this->targ_->iplt_section()->address();
6617 plt_addr += iplt_base;
6620 plt_addr += plt_base;
6621 Address my_addr = global_entry_base + ge->second;
6622 Address off = plt_addr - my_addr;
6624 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
6625 gold_error(_("linkage table error against `%s'"),
6626 ge->first->demangled_name().c_str());
6628 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
6629 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
6630 write_insn<big_endian>(p, mtctr_12), p += 4;
6631 write_insn<big_endian>(p, bctr);
6636 const Output_data_got_powerpc<size, big_endian>* got
6637 = this->targ_->got_section();
6638 // The address of _GLOBAL_OFFSET_TABLE_.
6639 Address g_o_t = got->address() + got->g_o_t();
6641 // Write out pltresolve branch table.
6643 unsigned int the_end = oview_size - this->pltresolve_size();
6644 unsigned char* end_p = oview + the_end;
6645 while (p < end_p - 8 * 4)
6646 write_insn<big_endian>(p, b + end_p - p), p += 4;
6648 write_insn<big_endian>(p, nop), p += 4;
6650 // Write out pltresolve call stub.
6651 end_p = oview + oview_size;
6652 if (parameters->options().output_is_position_independent())
6654 Address res0_off = 0;
6655 Address after_bcl_off = the_end + 12;
6656 Address bcl_res0 = after_bcl_off - res0_off;
6658 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
6660 write_insn<big_endian>(p, mflr_0);
6662 write_insn<big_endian>(p, bcl_20_31);
6664 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
6666 write_insn<big_endian>(p, mflr_12);
6668 write_insn<big_endian>(p, mtlr_0);
6670 write_insn<big_endian>(p, sub_11_11_12);
6673 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
6675 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
6677 if (ha(got_bcl) == ha(got_bcl + 4))
6679 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
6681 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
6685 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
6687 write_insn<big_endian>(p, lwz_12_12 + 4);
6690 write_insn<big_endian>(p, mtctr_0);
6692 write_insn<big_endian>(p, add_0_11_11);
6694 write_insn<big_endian>(p, add_11_0_11);
6698 Address res0 = this->address();
6700 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
6702 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
6704 if (ha(g_o_t + 4) == ha(g_o_t + 8))
6705 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
6707 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
6709 write_insn<big_endian>(p, addi_11_11 + l(-res0));
6711 write_insn<big_endian>(p, mtctr_0);
6713 write_insn<big_endian>(p, add_0_11_11);
6715 if (ha(g_o_t + 4) == ha(g_o_t + 8))
6716 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
6718 write_insn<big_endian>(p, lwz_12_12 + 4);
6720 write_insn<big_endian>(p, add_11_0_11);
6723 write_insn<big_endian>(p, bctr);
6727 write_insn<big_endian>(p, nop);
6732 of->write_output_view(off, oview_size, oview);
6736 // A class to handle linker generated save/restore functions.
6738 template<int size, bool big_endian>
6739 class Output_data_save_res : public Output_section_data_build
6742 Output_data_save_res(Symbol_table* symtab);
6744 const unsigned char*
6751 // Write to a map file.
6753 do_print_to_mapfile(Mapfile* mapfile) const
6754 { mapfile->print_output_data(this, _("** save/restore")); }
6757 do_write(Output_file*);
6760 // The maximum size of save/restore contents.
6761 static const unsigned int savres_max = 218*4;
6764 savres_define(Symbol_table* symtab,
6766 unsigned int lo, unsigned int hi,
6767 unsigned char* write_ent(unsigned char*, int),
6768 unsigned char* write_tail(unsigned char*, int));
6770 unsigned char *contents_;
6773 template<bool big_endian>
6774 static unsigned char*
6775 savegpr0(unsigned char* p, int r)
6777 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6778 write_insn<big_endian>(p, insn);
6782 template<bool big_endian>
6783 static unsigned char*
6784 savegpr0_tail(unsigned char* p, int r)
6786 p = savegpr0<big_endian>(p, r);
6787 uint32_t insn = std_0_1 + 16;
6788 write_insn<big_endian>(p, insn);
6790 write_insn<big_endian>(p, blr);
6794 template<bool big_endian>
6795 static unsigned char*
6796 restgpr0(unsigned char* p, int r)
6798 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6799 write_insn<big_endian>(p, insn);
6803 template<bool big_endian>
6804 static unsigned char*
6805 restgpr0_tail(unsigned char* p, int r)
6807 uint32_t insn = ld_0_1 + 16;
6808 write_insn<big_endian>(p, insn);
6810 p = restgpr0<big_endian>(p, r);
6811 write_insn<big_endian>(p, mtlr_0);
6815 p = restgpr0<big_endian>(p, 30);
6816 p = restgpr0<big_endian>(p, 31);
6818 write_insn<big_endian>(p, blr);
6822 template<bool big_endian>
6823 static unsigned char*
6824 savegpr1(unsigned char* p, int r)
6826 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
6827 write_insn<big_endian>(p, insn);
6831 template<bool big_endian>
6832 static unsigned char*
6833 savegpr1_tail(unsigned char* p, int r)
6835 p = savegpr1<big_endian>(p, r);
6836 write_insn<big_endian>(p, blr);
6840 template<bool big_endian>
6841 static unsigned char*
6842 restgpr1(unsigned char* p, int r)
6844 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
6845 write_insn<big_endian>(p, insn);
6849 template<bool big_endian>
6850 static unsigned char*
6851 restgpr1_tail(unsigned char* p, int r)
6853 p = restgpr1<big_endian>(p, r);
6854 write_insn<big_endian>(p, blr);
6858 template<bool big_endian>
6859 static unsigned char*
6860 savefpr(unsigned char* p, int r)
6862 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6863 write_insn<big_endian>(p, insn);
6867 template<bool big_endian>
6868 static unsigned char*
6869 savefpr0_tail(unsigned char* p, int r)
6871 p = savefpr<big_endian>(p, r);
6872 write_insn<big_endian>(p, std_0_1 + 16);
6874 write_insn<big_endian>(p, blr);
6878 template<bool big_endian>
6879 static unsigned char*
6880 restfpr(unsigned char* p, int r)
6882 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6883 write_insn<big_endian>(p, insn);
6887 template<bool big_endian>
6888 static unsigned char*
6889 restfpr0_tail(unsigned char* p, int r)
6891 write_insn<big_endian>(p, ld_0_1 + 16);
6893 p = restfpr<big_endian>(p, r);
6894 write_insn<big_endian>(p, mtlr_0);
6898 p = restfpr<big_endian>(p, 30);
6899 p = restfpr<big_endian>(p, 31);
6901 write_insn<big_endian>(p, blr);
6905 template<bool big_endian>
6906 static unsigned char*
6907 savefpr1_tail(unsigned char* p, int r)
6909 p = savefpr<big_endian>(p, r);
6910 write_insn<big_endian>(p, blr);
6914 template<bool big_endian>
6915 static unsigned char*
6916 restfpr1_tail(unsigned char* p, int r)
6918 p = restfpr<big_endian>(p, r);
6919 write_insn<big_endian>(p, blr);
6923 template<bool big_endian>
6924 static unsigned char*
6925 savevr(unsigned char* p, int r)
6927 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
6928 write_insn<big_endian>(p, insn);
6930 insn = stvx_0_12_0 + (r << 21);
6931 write_insn<big_endian>(p, insn);
6935 template<bool big_endian>
6936 static unsigned char*
6937 savevr_tail(unsigned char* p, int r)
6939 p = savevr<big_endian>(p, r);
6940 write_insn<big_endian>(p, blr);
6944 template<bool big_endian>
6945 static unsigned char*
6946 restvr(unsigned char* p, int r)
6948 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
6949 write_insn<big_endian>(p, insn);
6951 insn = lvx_0_12_0 + (r << 21);
6952 write_insn<big_endian>(p, insn);
6956 template<bool big_endian>
6957 static unsigned char*
6958 restvr_tail(unsigned char* p, int r)
6960 p = restvr<big_endian>(p, r);
6961 write_insn<big_endian>(p, blr);
6966 template<int size, bool big_endian>
6967 Output_data_save_res<size, big_endian>::Output_data_save_res(
6968 Symbol_table* symtab)
6969 : Output_section_data_build(4),
6972 this->savres_define(symtab,
6973 "_savegpr0_", 14, 31,
6974 savegpr0<big_endian>, savegpr0_tail<big_endian>);
6975 this->savres_define(symtab,
6976 "_restgpr0_", 14, 29,
6977 restgpr0<big_endian>, restgpr0_tail<big_endian>);
6978 this->savres_define(symtab,
6979 "_restgpr0_", 30, 31,
6980 restgpr0<big_endian>, restgpr0_tail<big_endian>);
6981 this->savres_define(symtab,
6982 "_savegpr1_", 14, 31,
6983 savegpr1<big_endian>, savegpr1_tail<big_endian>);
6984 this->savres_define(symtab,
6985 "_restgpr1_", 14, 31,
6986 restgpr1<big_endian>, restgpr1_tail<big_endian>);
6987 this->savres_define(symtab,
6988 "_savefpr_", 14, 31,
6989 savefpr<big_endian>, savefpr0_tail<big_endian>);
6990 this->savres_define(symtab,
6991 "_restfpr_", 14, 29,
6992 restfpr<big_endian>, restfpr0_tail<big_endian>);
6993 this->savres_define(symtab,
6994 "_restfpr_", 30, 31,
6995 restfpr<big_endian>, restfpr0_tail<big_endian>);
6996 this->savres_define(symtab,
6998 savefpr<big_endian>, savefpr1_tail<big_endian>);
6999 this->savres_define(symtab,
7001 restfpr<big_endian>, restfpr1_tail<big_endian>);
7002 this->savres_define(symtab,
7004 savevr<big_endian>, savevr_tail<big_endian>);
7005 this->savres_define(symtab,
7007 restvr<big_endian>, restvr_tail<big_endian>);
7010 template<int size, bool big_endian>
7012 Output_data_save_res<size, big_endian>::savres_define(
7013 Symbol_table* symtab,
7015 unsigned int lo, unsigned int hi,
7016 unsigned char* write_ent(unsigned char*, int),
7017 unsigned char* write_tail(unsigned char*, int))
7019 size_t len = strlen(name);
7020 bool writing = false;
7023 memcpy(sym, name, len);
7026 for (unsigned int i = lo; i <= hi; i++)
7028 sym[len + 0] = i / 10 + '0';
7029 sym[len + 1] = i % 10 + '0';
7030 Symbol* gsym = symtab->lookup(sym);
7031 bool refd = gsym != NULL && gsym->is_undefined();
7032 writing = writing || refd;
7035 if (this->contents_ == NULL)
7036 this->contents_ = new unsigned char[this->savres_max];
7038 section_size_type value = this->current_data_size();
7039 unsigned char* p = this->contents_ + value;
7041 p = write_ent(p, i);
7043 p = write_tail(p, i);
7044 section_size_type cur_size = p - this->contents_;
7045 this->set_current_data_size(cur_size);
7047 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7048 this, value, cur_size - value,
7049 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7050 elfcpp::STV_HIDDEN, 0, false, false);
7055 // Write out save/restore.
7057 template<int size, bool big_endian>
7059 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7061 const section_size_type off = this->offset();
7062 const section_size_type oview_size =
7063 convert_to_section_size_type(this->data_size());
7064 unsigned char* const oview = of->get_output_view(off, oview_size);
7065 memcpy(oview, this->contents_, oview_size);
7066 of->write_output_view(off, oview_size, oview);
7070 // Create the glink section.
7072 template<int size, bool big_endian>
7074 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7076 if (this->glink_ == NULL)
7078 this->glink_ = new Output_data_glink<size, big_endian>(this);
7079 this->glink_->add_eh_frame(layout);
7080 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7081 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7082 this->glink_, ORDER_TEXT, false);
7086 // Create a PLT entry for a global symbol.
7088 template<int size, bool big_endian>
7090 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7094 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7095 && gsym->can_use_relative_reloc(false))
7097 if (this->iplt_ == NULL)
7098 this->make_iplt_section(symtab, layout);
7099 this->iplt_->add_ifunc_entry(gsym);
7103 if (this->plt_ == NULL)
7104 this->make_plt_section(symtab, layout);
7105 this->plt_->add_entry(gsym);
7109 // Make a PLT entry for a local symbol.
7111 template<int size, bool big_endian>
7113 Target_powerpc<size, big_endian>::make_local_plt_entry(
7115 Sized_relobj_file<size, big_endian>* relobj,
7118 if (this->lplt_ == NULL)
7119 this->make_lplt_section(layout);
7120 this->lplt_->add_local_entry(relobj, r_sym);
7123 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
7125 template<int size, bool big_endian>
7127 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
7128 Symbol_table* symtab,
7130 Sized_relobj_file<size, big_endian>* relobj,
7133 if (this->iplt_ == NULL)
7134 this->make_iplt_section(symtab, layout);
7135 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
7138 // Return the number of entries in the PLT.
7140 template<int size, bool big_endian>
7142 Target_powerpc<size, big_endian>::plt_entry_count() const
7144 if (this->plt_ == NULL)
7146 return this->plt_->entry_count();
7149 // Create a GOT entry for local dynamic __tls_get_addr calls.
7151 template<int size, bool big_endian>
7153 Target_powerpc<size, big_endian>::tlsld_got_offset(
7154 Symbol_table* symtab,
7156 Sized_relobj_file<size, big_endian>* object)
7158 if (this->tlsld_got_offset_ == -1U)
7160 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7161 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
7162 Output_data_got_powerpc<size, big_endian>* got
7163 = this->got_section(symtab, layout);
7164 unsigned int got_offset = got->add_constant_pair(0, 0);
7165 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7167 this->tlsld_got_offset_ = got_offset;
7169 return this->tlsld_got_offset_;
7172 // Get the Reference_flags for a particular relocation.
7174 template<int size, bool big_endian>
7176 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7177 unsigned int r_type,
7178 const Target_powerpc* target)
7184 case elfcpp::R_POWERPC_NONE:
7185 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7186 case elfcpp::R_POWERPC_GNU_VTENTRY:
7187 case elfcpp::R_PPC64_TOC:
7188 // No symbol reference.
7191 case elfcpp::R_PPC64_ADDR64:
7192 case elfcpp::R_PPC64_UADDR64:
7193 case elfcpp::R_POWERPC_ADDR32:
7194 case elfcpp::R_POWERPC_UADDR32:
7195 case elfcpp::R_POWERPC_ADDR16:
7196 case elfcpp::R_POWERPC_UADDR16:
7197 case elfcpp::R_POWERPC_ADDR16_LO:
7198 case elfcpp::R_POWERPC_ADDR16_HI:
7199 case elfcpp::R_POWERPC_ADDR16_HA:
7200 ref = Symbol::ABSOLUTE_REF;
7203 case elfcpp::R_POWERPC_ADDR24:
7204 case elfcpp::R_POWERPC_ADDR14:
7205 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7206 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7207 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7210 case elfcpp::R_PPC64_REL64:
7211 case elfcpp::R_POWERPC_REL32:
7212 case elfcpp::R_PPC_LOCAL24PC:
7213 case elfcpp::R_POWERPC_REL16:
7214 case elfcpp::R_POWERPC_REL16_LO:
7215 case elfcpp::R_POWERPC_REL16_HI:
7216 case elfcpp::R_POWERPC_REL16_HA:
7217 case elfcpp::R_PPC64_REL16_HIGH:
7218 case elfcpp::R_PPC64_REL16_HIGHA:
7219 case elfcpp::R_PPC64_REL16_HIGHER:
7220 case elfcpp::R_PPC64_REL16_HIGHERA:
7221 case elfcpp::R_PPC64_REL16_HIGHEST:
7222 case elfcpp::R_PPC64_REL16_HIGHESTA:
7223 case elfcpp::R_PPC64_PCREL34:
7224 case elfcpp::R_PPC64_REL16_HIGHER34:
7225 case elfcpp::R_PPC64_REL16_HIGHERA34:
7226 case elfcpp::R_PPC64_REL16_HIGHEST34:
7227 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7228 case elfcpp::R_PPC64_PCREL28:
7229 ref = Symbol::RELATIVE_REF;
7232 case elfcpp::R_PPC64_REL24_NOTOC:
7236 case elfcpp::R_POWERPC_REL24:
7237 case elfcpp::R_PPC_PLTREL24:
7238 case elfcpp::R_POWERPC_REL14:
7239 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7240 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7241 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7244 case elfcpp::R_POWERPC_GOT16:
7245 case elfcpp::R_POWERPC_GOT16_LO:
7246 case elfcpp::R_POWERPC_GOT16_HI:
7247 case elfcpp::R_POWERPC_GOT16_HA:
7248 case elfcpp::R_PPC64_GOT16_DS:
7249 case elfcpp::R_PPC64_GOT16_LO_DS:
7250 case elfcpp::R_PPC64_GOT_PCREL34:
7251 case elfcpp::R_PPC64_TOC16:
7252 case elfcpp::R_PPC64_TOC16_LO:
7253 case elfcpp::R_PPC64_TOC16_HI:
7254 case elfcpp::R_PPC64_TOC16_HA:
7255 case elfcpp::R_PPC64_TOC16_DS:
7256 case elfcpp::R_PPC64_TOC16_LO_DS:
7257 case elfcpp::R_POWERPC_PLT16_LO:
7258 case elfcpp::R_POWERPC_PLT16_HI:
7259 case elfcpp::R_POWERPC_PLT16_HA:
7260 case elfcpp::R_PPC64_PLT16_LO_DS:
7261 case elfcpp::R_PPC64_PLT_PCREL34:
7262 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7263 ref = Symbol::RELATIVE_REF;
7266 case elfcpp::R_POWERPC_GOT_TPREL16:
7267 case elfcpp::R_POWERPC_TLS:
7268 ref = Symbol::TLS_REF;
7271 case elfcpp::R_POWERPC_COPY:
7272 case elfcpp::R_POWERPC_GLOB_DAT:
7273 case elfcpp::R_POWERPC_JMP_SLOT:
7274 case elfcpp::R_POWERPC_RELATIVE:
7275 case elfcpp::R_POWERPC_DTPMOD:
7277 // Not expected. We will give an error later.
7281 if (size == 64 && target->abiversion() < 2)
7282 ref |= Symbol::FUNC_DESC_ABI;
7286 // Report an unsupported relocation against a local symbol.
7288 template<int size, bool big_endian>
7290 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
7291 Sized_relobj_file<size, big_endian>* object,
7292 unsigned int r_type)
7294 gold_error(_("%s: unsupported reloc %u against local symbol"),
7295 object->name().c_str(), r_type);
7298 // We are about to emit a dynamic relocation of type R_TYPE. If the
7299 // dynamic linker does not support it, issue an error.
7301 template<int size, bool big_endian>
7303 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7304 unsigned int r_type)
7306 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7308 // These are the relocation types supported by glibc for both 32-bit
7309 // and 64-bit powerpc.
7312 case elfcpp::R_POWERPC_NONE:
7313 case elfcpp::R_POWERPC_RELATIVE:
7314 case elfcpp::R_POWERPC_GLOB_DAT:
7315 case elfcpp::R_POWERPC_DTPMOD:
7316 case elfcpp::R_POWERPC_DTPREL:
7317 case elfcpp::R_POWERPC_TPREL:
7318 case elfcpp::R_POWERPC_JMP_SLOT:
7319 case elfcpp::R_POWERPC_COPY:
7320 case elfcpp::R_POWERPC_IRELATIVE:
7321 case elfcpp::R_POWERPC_ADDR32:
7322 case elfcpp::R_POWERPC_UADDR32:
7323 case elfcpp::R_POWERPC_ADDR24:
7324 case elfcpp::R_POWERPC_ADDR16:
7325 case elfcpp::R_POWERPC_UADDR16:
7326 case elfcpp::R_POWERPC_ADDR16_LO:
7327 case elfcpp::R_POWERPC_ADDR16_HI:
7328 case elfcpp::R_POWERPC_ADDR16_HA:
7329 case elfcpp::R_POWERPC_ADDR14:
7330 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7331 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7332 case elfcpp::R_POWERPC_REL32:
7333 case elfcpp::R_POWERPC_REL24:
7334 case elfcpp::R_POWERPC_TPREL16:
7335 case elfcpp::R_POWERPC_TPREL16_LO:
7336 case elfcpp::R_POWERPC_TPREL16_HI:
7337 case elfcpp::R_POWERPC_TPREL16_HA:
7348 // These are the relocation types supported only on 64-bit.
7349 case elfcpp::R_PPC64_ADDR64:
7350 case elfcpp::R_PPC64_UADDR64:
7351 case elfcpp::R_PPC64_JMP_IREL:
7352 case elfcpp::R_PPC64_ADDR16_DS:
7353 case elfcpp::R_PPC64_ADDR16_LO_DS:
7354 case elfcpp::R_PPC64_ADDR16_HIGH:
7355 case elfcpp::R_PPC64_ADDR16_HIGHA:
7356 case elfcpp::R_PPC64_ADDR16_HIGHER:
7357 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7358 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7359 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7360 case elfcpp::R_PPC64_REL64:
7361 case elfcpp::R_POWERPC_ADDR30:
7362 case elfcpp::R_PPC64_TPREL16_DS:
7363 case elfcpp::R_PPC64_TPREL16_LO_DS:
7364 case elfcpp::R_PPC64_TPREL16_HIGH:
7365 case elfcpp::R_PPC64_TPREL16_HIGHA:
7366 case elfcpp::R_PPC64_TPREL16_HIGHER:
7367 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7368 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7369 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7380 // These are the relocation types supported only on 32-bit.
7381 // ??? glibc ld.so doesn't need to support these.
7382 case elfcpp::R_POWERPC_DTPREL16:
7383 case elfcpp::R_POWERPC_DTPREL16_LO:
7384 case elfcpp::R_POWERPC_DTPREL16_HI:
7385 case elfcpp::R_POWERPC_DTPREL16_HA:
7393 // This prevents us from issuing more than one error per reloc
7394 // section. But we can still wind up issuing more than one
7395 // error per object file.
7396 if (this->issued_non_pic_error_)
7398 gold_assert(parameters->options().output_is_position_independent());
7399 object->error(_("requires unsupported dynamic reloc; "
7400 "recompile with -fPIC"));
7401 this->issued_non_pic_error_ = true;
7405 // Return whether we need to make a PLT entry for a relocation of the
7406 // given type against a STT_GNU_IFUNC symbol.
7408 template<int size, bool big_endian>
7410 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
7411 Target_powerpc<size, big_endian>* target,
7412 Sized_relobj_file<size, big_endian>* object,
7413 unsigned int r_type,
7416 // In non-pic code any reference will resolve to the plt call stub
7417 // for the ifunc symbol.
7418 if ((size == 32 || target->abiversion() >= 2)
7419 && !parameters->options().output_is_position_independent())
7424 // Word size refs from data sections are OK, but don't need a PLT entry.
7425 case elfcpp::R_POWERPC_ADDR32:
7426 case elfcpp::R_POWERPC_UADDR32:
7431 case elfcpp::R_PPC64_ADDR64:
7432 case elfcpp::R_PPC64_UADDR64:
7437 // GOT refs are good, but also don't need a PLT entry.
7438 case elfcpp::R_POWERPC_GOT16:
7439 case elfcpp::R_POWERPC_GOT16_LO:
7440 case elfcpp::R_POWERPC_GOT16_HI:
7441 case elfcpp::R_POWERPC_GOT16_HA:
7442 case elfcpp::R_PPC64_GOT16_DS:
7443 case elfcpp::R_PPC64_GOT16_LO_DS:
7444 case elfcpp::R_PPC64_GOT_PCREL34:
7447 // PLT relocs are OK and need a PLT entry.
7448 case elfcpp::R_POWERPC_PLT16_LO:
7449 case elfcpp::R_POWERPC_PLT16_HI:
7450 case elfcpp::R_POWERPC_PLT16_HA:
7451 case elfcpp::R_PPC64_PLT16_LO_DS:
7452 case elfcpp::R_POWERPC_PLTSEQ:
7453 case elfcpp::R_POWERPC_PLTCALL:
7454 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7455 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7456 case elfcpp::R_PPC64_PLT_PCREL34:
7457 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7461 // Function calls are good, and these do need a PLT entry.
7462 case elfcpp::R_PPC64_REL24_NOTOC:
7466 case elfcpp::R_POWERPC_ADDR24:
7467 case elfcpp::R_POWERPC_ADDR14:
7468 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7469 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7470 case elfcpp::R_POWERPC_REL24:
7471 case elfcpp::R_PPC_PLTREL24:
7472 case elfcpp::R_POWERPC_REL14:
7473 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7474 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7481 // Anything else is a problem.
7482 // If we are building a static executable, the libc startup function
7483 // responsible for applying indirect function relocations is going
7484 // to complain about the reloc type.
7485 // If we are building a dynamic executable, we will have a text
7486 // relocation. The dynamic loader will set the text segment
7487 // writable and non-executable to apply text relocations. So we'll
7488 // segfault when trying to run the indirection function to resolve
7491 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
7492 object->name().c_str(), r_type);
7496 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
7500 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
7502 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7503 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7504 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7505 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7506 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7507 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7508 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7509 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7510 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7511 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7512 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7513 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7514 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7515 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7516 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7517 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7518 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7519 /* Exclude lfqu by testing reloc. If relocs are ever
7520 defined for the reduced D field in psq_lu then those
7521 will need testing too. */
7522 && r_type != elfcpp::R_PPC64_TOC16_LO
7523 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7524 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7526 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7527 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7528 /* Exclude stfqu. psq_stu as above for psq_lu. */
7529 && r_type != elfcpp::R_PPC64_TOC16_LO
7530 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7531 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7532 && (insn & 1) == 0));
7535 // Scan a relocation for a local symbol.
7537 template<int size, bool big_endian>
7539 Target_powerpc<size, big_endian>::Scan::local(
7540 Symbol_table* symtab,
7542 Target_powerpc<size, big_endian>* target,
7543 Sized_relobj_file<size, big_endian>* object,
7544 unsigned int data_shndx,
7545 Output_section* output_section,
7546 const elfcpp::Rela<size, big_endian>& reloc,
7547 unsigned int r_type,
7548 const elfcpp::Sym<size, big_endian>& lsym,
7551 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
7553 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7554 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7556 this->expect_tls_get_addr_call();
7557 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7558 if (tls_type != tls::TLSOPT_NONE)
7559 this->skip_next_tls_get_addr_call();
7561 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7562 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7564 this->expect_tls_get_addr_call();
7565 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7566 if (tls_type != tls::TLSOPT_NONE)
7567 this->skip_next_tls_get_addr_call();
7570 Powerpc_relobj<size, big_endian>* ppc_object
7571 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7576 && data_shndx == ppc_object->opd_shndx()
7577 && r_type == elfcpp::R_PPC64_ADDR64)
7578 ppc_object->set_opd_discard(reloc.get_r_offset());
7582 // A local STT_GNU_IFUNC symbol may require a PLT entry.
7583 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
7584 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
7586 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7587 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7588 r_type, r_sym, reloc.get_r_addend());
7589 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
7594 case elfcpp::R_POWERPC_NONE:
7595 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7596 case elfcpp::R_POWERPC_GNU_VTENTRY:
7597 case elfcpp::R_POWERPC_TLS:
7598 case elfcpp::R_PPC64_ENTRY:
7599 case elfcpp::R_POWERPC_PLTSEQ:
7600 case elfcpp::R_POWERPC_PLTCALL:
7601 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7602 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7603 case elfcpp::R_PPC64_PCREL_OPT:
7604 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7605 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7606 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7607 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7608 case elfcpp::R_PPC64_REL16_HIGHER34:
7609 case elfcpp::R_PPC64_REL16_HIGHERA34:
7610 case elfcpp::R_PPC64_REL16_HIGHEST34:
7611 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7614 case elfcpp::R_PPC64_D34:
7615 case elfcpp::R_PPC64_D34_LO:
7616 case elfcpp::R_PPC64_D34_HI30:
7617 case elfcpp::R_PPC64_D34_HA30:
7618 case elfcpp::R_PPC64_D28:
7619 case elfcpp::R_PPC64_PCREL34:
7620 case elfcpp::R_PPC64_PCREL28:
7621 target->set_powerxx_stubs();
7624 case elfcpp::R_PPC64_TOC:
7626 Output_data_got_powerpc<size, big_endian>* got
7627 = target->got_section(symtab, layout);
7628 if (parameters->options().output_is_position_independent())
7630 Address off = reloc.get_r_offset();
7632 && target->abiversion() < 2
7633 && data_shndx == ppc_object->opd_shndx()
7634 && ppc_object->get_opd_discard(off - 8))
7637 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7638 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
7639 rela_dyn->add_output_section_relative(got->output_section(),
7640 elfcpp::R_POWERPC_RELATIVE,
7642 object, data_shndx, off,
7643 symobj->toc_base_offset());
7648 case elfcpp::R_PPC64_ADDR64:
7649 case elfcpp::R_PPC64_UADDR64:
7650 case elfcpp::R_POWERPC_ADDR32:
7651 case elfcpp::R_POWERPC_UADDR32:
7652 case elfcpp::R_POWERPC_ADDR24:
7653 case elfcpp::R_POWERPC_ADDR16:
7654 case elfcpp::R_POWERPC_ADDR16_LO:
7655 case elfcpp::R_POWERPC_ADDR16_HI:
7656 case elfcpp::R_POWERPC_ADDR16_HA:
7657 case elfcpp::R_POWERPC_UADDR16:
7658 case elfcpp::R_PPC64_ADDR16_HIGH:
7659 case elfcpp::R_PPC64_ADDR16_HIGHA:
7660 case elfcpp::R_PPC64_ADDR16_HIGHER:
7661 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7662 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7663 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7664 case elfcpp::R_PPC64_ADDR16_DS:
7665 case elfcpp::R_PPC64_ADDR16_LO_DS:
7666 case elfcpp::R_POWERPC_ADDR14:
7667 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7668 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7669 // If building a shared library (or a position-independent
7670 // executable), we need to create a dynamic relocation for
7672 if (parameters->options().output_is_position_independent()
7673 || (size == 64 && is_ifunc && target->abiversion() < 2))
7675 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7677 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7678 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
7679 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
7681 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7682 : elfcpp::R_POWERPC_RELATIVE);
7683 rela_dyn->add_local_relative(object, r_sym, dynrel,
7684 output_section, data_shndx,
7685 reloc.get_r_offset(),
7686 reloc.get_r_addend(), false);
7688 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
7690 check_non_pic(object, r_type);
7691 rela_dyn->add_local(object, r_sym, r_type, output_section,
7692 data_shndx, reloc.get_r_offset(),
7693 reloc.get_r_addend());
7697 gold_assert(lsym.get_st_value() == 0);
7698 unsigned int shndx = lsym.get_st_shndx();
7700 shndx = object->adjust_sym_shndx(r_sym, shndx,
7703 object->error(_("section symbol %u has bad shndx %u"),
7706 rela_dyn->add_local_section(object, shndx, r_type,
7707 output_section, data_shndx,
7708 reloc.get_r_offset());
7713 case elfcpp::R_PPC64_PLT_PCREL34:
7714 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7715 target->set_powerxx_stubs();
7717 case elfcpp::R_POWERPC_PLT16_LO:
7718 case elfcpp::R_POWERPC_PLT16_HI:
7719 case elfcpp::R_POWERPC_PLT16_HA:
7720 case elfcpp::R_PPC64_PLT16_LO_DS:
7723 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7724 target->make_local_plt_entry(layout, object, r_sym);
7728 case elfcpp::R_PPC64_REL24_NOTOC:
7732 case elfcpp::R_POWERPC_REL24:
7733 case elfcpp::R_PPC_PLTREL24:
7734 case elfcpp::R_PPC_LOCAL24PC:
7735 case elfcpp::R_POWERPC_REL14:
7736 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7737 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7740 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7741 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7742 r_type, r_sym, reloc.get_r_addend());
7746 case elfcpp::R_PPC64_TOCSAVE:
7747 // R_PPC64_TOCSAVE follows a call instruction to indicate the
7748 // caller has already saved r2 and thus a plt call stub need not
7751 && target->mark_pltcall(ppc_object, data_shndx,
7752 reloc.get_r_offset() - 4, symtab))
7754 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7755 unsigned int shndx = lsym.get_st_shndx();
7757 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7759 object->error(_("tocsave symbol %u has bad shndx %u"),
7762 target->add_tocsave(ppc_object, shndx,
7763 lsym.get_st_value() + reloc.get_r_addend());
7767 case elfcpp::R_PPC64_REL64:
7768 case elfcpp::R_POWERPC_REL32:
7769 case elfcpp::R_POWERPC_REL16:
7770 case elfcpp::R_POWERPC_REL16_LO:
7771 case elfcpp::R_POWERPC_REL16_HI:
7772 case elfcpp::R_POWERPC_REL16_HA:
7773 case elfcpp::R_POWERPC_REL16DX_HA:
7774 case elfcpp::R_PPC64_REL16_HIGH:
7775 case elfcpp::R_PPC64_REL16_HIGHA:
7776 case elfcpp::R_PPC64_REL16_HIGHER:
7777 case elfcpp::R_PPC64_REL16_HIGHERA:
7778 case elfcpp::R_PPC64_REL16_HIGHEST:
7779 case elfcpp::R_PPC64_REL16_HIGHESTA:
7780 case elfcpp::R_POWERPC_SECTOFF:
7781 case elfcpp::R_POWERPC_SECTOFF_LO:
7782 case elfcpp::R_POWERPC_SECTOFF_HI:
7783 case elfcpp::R_POWERPC_SECTOFF_HA:
7784 case elfcpp::R_PPC64_SECTOFF_DS:
7785 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7786 case elfcpp::R_POWERPC_TPREL16:
7787 case elfcpp::R_POWERPC_TPREL16_LO:
7788 case elfcpp::R_POWERPC_TPREL16_HI:
7789 case elfcpp::R_POWERPC_TPREL16_HA:
7790 case elfcpp::R_PPC64_TPREL16_DS:
7791 case elfcpp::R_PPC64_TPREL16_LO_DS:
7792 case elfcpp::R_PPC64_TPREL16_HIGH:
7793 case elfcpp::R_PPC64_TPREL16_HIGHA:
7794 case elfcpp::R_PPC64_TPREL16_HIGHER:
7795 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7796 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7797 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7798 case elfcpp::R_POWERPC_DTPREL16:
7799 case elfcpp::R_POWERPC_DTPREL16_LO:
7800 case elfcpp::R_POWERPC_DTPREL16_HI:
7801 case elfcpp::R_POWERPC_DTPREL16_HA:
7802 case elfcpp::R_PPC64_DTPREL16_DS:
7803 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7804 case elfcpp::R_PPC64_DTPREL16_HIGH:
7805 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7806 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7807 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7808 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7809 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7810 case elfcpp::R_PPC64_TLSGD:
7811 case elfcpp::R_PPC64_TLSLD:
7812 case elfcpp::R_PPC64_ADDR64_LOCAL:
7815 case elfcpp::R_PPC64_GOT_PCREL34:
7816 target->set_powerxx_stubs();
7818 case elfcpp::R_POWERPC_GOT16:
7819 case elfcpp::R_POWERPC_GOT16_LO:
7820 case elfcpp::R_POWERPC_GOT16_HI:
7821 case elfcpp::R_POWERPC_GOT16_HA:
7822 case elfcpp::R_PPC64_GOT16_DS:
7823 case elfcpp::R_PPC64_GOT16_LO_DS:
7825 // The symbol requires a GOT entry.
7826 Output_data_got_powerpc<size, big_endian>* got
7827 = target->got_section(symtab, layout);
7828 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7830 if (!parameters->options().output_is_position_independent())
7833 && (size == 32 || target->abiversion() >= 2))
7834 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
7836 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
7838 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
7840 // If we are generating a shared object or a pie, this
7841 // symbol's GOT entry will be set by a dynamic relocation.
7843 off = got->add_constant(0);
7844 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
7846 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7848 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7849 : elfcpp::R_POWERPC_RELATIVE);
7850 rela_dyn->add_local_relative(object, r_sym, dynrel,
7851 got, off, 0, false);
7856 case elfcpp::R_PPC64_TOC16:
7857 case elfcpp::R_PPC64_TOC16_LO:
7858 case elfcpp::R_PPC64_TOC16_HI:
7859 case elfcpp::R_PPC64_TOC16_HA:
7860 case elfcpp::R_PPC64_TOC16_DS:
7861 case elfcpp::R_PPC64_TOC16_LO_DS:
7862 // We need a GOT section.
7863 target->got_section(symtab, layout);
7866 case elfcpp::R_POWERPC_GOT_TLSGD16:
7867 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7868 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7869 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7871 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7872 if (tls_type == tls::TLSOPT_NONE)
7874 Output_data_got_powerpc<size, big_endian>* got
7875 = target->got_section(symtab, layout);
7876 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7877 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7878 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
7879 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
7881 else if (tls_type == tls::TLSOPT_TO_LE)
7883 // no GOT relocs needed for Local Exec.
7890 case elfcpp::R_POWERPC_GOT_TLSLD16:
7891 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7892 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7893 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7895 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7896 if (tls_type == tls::TLSOPT_NONE)
7897 target->tlsld_got_offset(symtab, layout, object);
7898 else if (tls_type == tls::TLSOPT_TO_LE)
7900 // no GOT relocs needed for Local Exec.
7901 if (parameters->options().emit_relocs())
7903 Output_section* os = layout->tls_segment()->first_section();
7904 gold_assert(os != NULL);
7905 os->set_needs_symtab_index();
7913 case elfcpp::R_POWERPC_GOT_DTPREL16:
7914 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7915 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7916 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7918 Output_data_got_powerpc<size, big_endian>* got
7919 = target->got_section(symtab, layout);
7920 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7921 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
7925 case elfcpp::R_POWERPC_GOT_TPREL16:
7926 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7927 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7928 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7930 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
7931 if (tls_type == tls::TLSOPT_NONE)
7933 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7934 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
7936 Output_data_got_powerpc<size, big_endian>* got
7937 = target->got_section(symtab, layout);
7938 unsigned int off = got->add_constant(0);
7939 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
7941 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7942 rela_dyn->add_symbolless_local_addend(object, r_sym,
7943 elfcpp::R_POWERPC_TPREL,
7947 else if (tls_type == tls::TLSOPT_TO_LE)
7949 // no GOT relocs needed for Local Exec.
7957 unsupported_reloc_local(object, r_type);
7962 && parameters->options().toc_optimize())
7964 if (data_shndx == ppc_object->toc_shndx())
7967 if (r_type != elfcpp::R_PPC64_ADDR64
7968 || (is_ifunc && target->abiversion() < 2))
7970 else if (parameters->options().output_is_position_independent())
7976 unsigned int shndx = lsym.get_st_shndx();
7977 if (shndx >= elfcpp::SHN_LORESERVE
7978 && shndx != elfcpp::SHN_XINDEX)
7983 ppc_object->set_no_toc_opt(reloc.get_r_offset());
7986 enum {no_check, check_lo, check_ha} insn_check;
7990 insn_check = no_check;
7993 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7994 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7995 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7996 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7997 case elfcpp::R_POWERPC_GOT16_HA:
7998 case elfcpp::R_PPC64_TOC16_HA:
7999 insn_check = check_ha;
8002 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8003 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8004 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8005 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8006 case elfcpp::R_POWERPC_GOT16_LO:
8007 case elfcpp::R_PPC64_GOT16_LO_DS:
8008 case elfcpp::R_PPC64_TOC16_LO:
8009 case elfcpp::R_PPC64_TOC16_LO_DS:
8010 insn_check = check_lo;
8014 section_size_type slen;
8015 const unsigned char* view = NULL;
8016 if (insn_check != no_check)
8018 view = ppc_object->section_contents(data_shndx, &slen, false);
8019 section_size_type off =
8020 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8023 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8024 if (insn_check == check_lo
8025 ? !ok_lo_toc_insn(insn, r_type)
8026 : ((insn & ((0x3f << 26) | 0x1f << 16))
8027 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8029 ppc_object->set_no_toc_opt();
8030 gold_warning(_("%s: toc optimization is not supported "
8031 "for %#08x instruction"),
8032 ppc_object->name().c_str(), insn);
8041 case elfcpp::R_PPC64_TOC16:
8042 case elfcpp::R_PPC64_TOC16_LO:
8043 case elfcpp::R_PPC64_TOC16_HI:
8044 case elfcpp::R_PPC64_TOC16_HA:
8045 case elfcpp::R_PPC64_TOC16_DS:
8046 case elfcpp::R_PPC64_TOC16_LO_DS:
8047 unsigned int shndx = lsym.get_st_shndx();
8048 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8050 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8051 if (is_ordinary && shndx == ppc_object->toc_shndx())
8053 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
8054 if (dst_off < ppc_object->section_size(shndx))
8057 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8059 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8061 // Need to check that the insn is a ld
8063 view = ppc_object->section_contents(data_shndx,
8066 section_size_type off =
8067 (convert_to_section_size_type(reloc.get_r_offset())
8068 + (big_endian ? -2 : 3));
8070 && (view[off] & (0x3f << 2)) == 58u << 2)
8074 ppc_object->set_no_toc_opt(dst_off);
8085 case elfcpp::R_POWERPC_REL32:
8086 if (ppc_object->got2_shndx() != 0
8087 && parameters->options().output_is_position_independent())
8089 unsigned int shndx = lsym.get_st_shndx();
8090 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8092 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8093 if (is_ordinary && shndx == ppc_object->got2_shndx()
8094 && (ppc_object->section_flags(data_shndx)
8095 & elfcpp::SHF_EXECINSTR) != 0)
8096 gold_error(_("%s: unsupported -mbss-plt code"),
8097 ppc_object->name().c_str());
8107 case elfcpp::R_POWERPC_GOT_TLSLD16:
8108 case elfcpp::R_POWERPC_GOT_TLSGD16:
8109 case elfcpp::R_POWERPC_GOT_TPREL16:
8110 case elfcpp::R_POWERPC_GOT_DTPREL16:
8111 case elfcpp::R_POWERPC_GOT16:
8112 case elfcpp::R_PPC64_GOT16_DS:
8113 case elfcpp::R_PPC64_TOC16:
8114 case elfcpp::R_PPC64_TOC16_DS:
8115 ppc_object->set_has_small_toc_reloc();
8121 // Report an unsupported relocation against a global symbol.
8123 template<int size, bool big_endian>
8125 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8126 Sized_relobj_file<size, big_endian>* object,
8127 unsigned int r_type,
8130 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8131 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8134 // Scan a relocation for a global symbol.
8136 template<int size, bool big_endian>
8138 Target_powerpc<size, big_endian>::Scan::global(
8139 Symbol_table* symtab,
8141 Target_powerpc<size, big_endian>* target,
8142 Sized_relobj_file<size, big_endian>* object,
8143 unsigned int data_shndx,
8144 Output_section* output_section,
8145 const elfcpp::Rela<size, big_endian>& reloc,
8146 unsigned int r_type,
8149 if (this->maybe_skip_tls_get_addr_call(target, r_type, gsym)
8153 if (target->replace_tls_get_addr(gsym))
8154 // Change a __tls_get_addr reference to __tls_get_addr_opt
8155 // so dynamic relocs are emitted against the latter symbol.
8156 gsym = target->tls_get_addr_opt();
8158 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8159 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8161 this->expect_tls_get_addr_call();
8162 const bool final = gsym->final_value_is_known();
8163 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8164 if (tls_type != tls::TLSOPT_NONE)
8165 this->skip_next_tls_get_addr_call();
8167 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8168 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8170 this->expect_tls_get_addr_call();
8171 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8172 if (tls_type != tls::TLSOPT_NONE)
8173 this->skip_next_tls_get_addr_call();
8176 Powerpc_relobj<size, big_endian>* ppc_object
8177 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8179 // A STT_GNU_IFUNC symbol may require a PLT entry.
8180 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
8181 bool pushed_ifunc = false;
8182 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
8184 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8185 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8186 r_type, r_sym, reloc.get_r_addend());
8187 target->make_plt_entry(symtab, layout, gsym);
8188 pushed_ifunc = true;
8193 case elfcpp::R_POWERPC_NONE:
8194 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8195 case elfcpp::R_POWERPC_GNU_VTENTRY:
8196 case elfcpp::R_PPC_LOCAL24PC:
8197 case elfcpp::R_POWERPC_TLS:
8198 case elfcpp::R_PPC64_ENTRY:
8199 case elfcpp::R_POWERPC_PLTSEQ:
8200 case elfcpp::R_POWERPC_PLTCALL:
8201 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8202 case elfcpp::R_PPC64_PLTCALL_NOTOC:
8203 case elfcpp::R_PPC64_PCREL_OPT:
8204 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8205 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8206 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8207 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8208 case elfcpp::R_PPC64_REL16_HIGHER34:
8209 case elfcpp::R_PPC64_REL16_HIGHERA34:
8210 case elfcpp::R_PPC64_REL16_HIGHEST34:
8211 case elfcpp::R_PPC64_REL16_HIGHESTA34:
8214 case elfcpp::R_PPC64_D34:
8215 case elfcpp::R_PPC64_D34_LO:
8216 case elfcpp::R_PPC64_D34_HI30:
8217 case elfcpp::R_PPC64_D34_HA30:
8218 case elfcpp::R_PPC64_D28:
8219 case elfcpp::R_PPC64_PCREL34:
8220 case elfcpp::R_PPC64_PCREL28:
8221 target->set_powerxx_stubs();
8224 case elfcpp::R_PPC64_TOC:
8226 Output_data_got_powerpc<size, big_endian>* got
8227 = target->got_section(symtab, layout);
8228 if (parameters->options().output_is_position_independent())
8230 Address off = reloc.get_r_offset();
8232 && data_shndx == ppc_object->opd_shndx()
8233 && ppc_object->get_opd_discard(off - 8))
8236 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8237 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
8238 if (data_shndx != ppc_object->opd_shndx())
8239 symobj = static_cast
8240 <Powerpc_relobj<size, big_endian>*>(gsym->object());
8241 rela_dyn->add_output_section_relative(got->output_section(),
8242 elfcpp::R_POWERPC_RELATIVE,
8244 object, data_shndx, off,
8245 symobj->toc_base_offset());
8250 case elfcpp::R_PPC64_ADDR64:
8252 && target->abiversion() < 2
8253 && data_shndx == ppc_object->opd_shndx()
8254 && (gsym->is_defined_in_discarded_section()
8255 || gsym->object() != object))
8257 ppc_object->set_opd_discard(reloc.get_r_offset());
8261 case elfcpp::R_PPC64_UADDR64:
8262 case elfcpp::R_POWERPC_ADDR32:
8263 case elfcpp::R_POWERPC_UADDR32:
8264 case elfcpp::R_POWERPC_ADDR24:
8265 case elfcpp::R_POWERPC_ADDR16:
8266 case elfcpp::R_POWERPC_ADDR16_LO:
8267 case elfcpp::R_POWERPC_ADDR16_HI:
8268 case elfcpp::R_POWERPC_ADDR16_HA:
8269 case elfcpp::R_POWERPC_UADDR16:
8270 case elfcpp::R_PPC64_ADDR16_HIGH:
8271 case elfcpp::R_PPC64_ADDR16_HIGHA:
8272 case elfcpp::R_PPC64_ADDR16_HIGHER:
8273 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8274 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8275 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8276 case elfcpp::R_PPC64_ADDR16_DS:
8277 case elfcpp::R_PPC64_ADDR16_LO_DS:
8278 case elfcpp::R_POWERPC_ADDR14:
8279 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8280 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8282 // Make a PLT entry if necessary.
8283 if (gsym->needs_plt_entry())
8285 // Since this is not a PC-relative relocation, we may be
8286 // taking the address of a function. In that case we need to
8287 // set the entry in the dynamic symbol table to the address of
8288 // the PLT call stub.
8289 bool need_ifunc_plt = false;
8290 if ((size == 32 || target->abiversion() >= 2)
8291 && gsym->is_from_dynobj()
8292 && !parameters->options().output_is_position_independent())
8294 gsym->set_needs_dynsym_value();
8295 need_ifunc_plt = true;
8297 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
8299 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8300 target->push_branch(ppc_object, data_shndx,
8301 reloc.get_r_offset(), r_type, r_sym,
8302 reloc.get_r_addend());
8303 target->make_plt_entry(symtab, layout, gsym);
8306 // Make a dynamic relocation if necessary.
8307 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
8308 || (size == 64 && is_ifunc && target->abiversion() < 2))
8310 if (!parameters->options().output_is_position_independent()
8311 && gsym->may_need_copy_reloc())
8313 target->copy_reloc(symtab, layout, object,
8314 data_shndx, output_section, gsym, reloc);
8316 else if ((((size == 32
8317 && r_type == elfcpp::R_POWERPC_ADDR32)
8319 && r_type == elfcpp::R_PPC64_ADDR64
8320 && target->abiversion() >= 2))
8321 && gsym->can_use_relative_reloc(false)
8322 && !(gsym->visibility() == elfcpp::STV_PROTECTED
8323 && parameters->options().shared()))
8325 && r_type == elfcpp::R_PPC64_ADDR64
8326 && target->abiversion() < 2
8327 && (gsym->can_use_relative_reloc(false)
8328 || data_shndx == ppc_object->opd_shndx())))
8330 Reloc_section* rela_dyn
8331 = target->rela_dyn_section(symtab, layout, is_ifunc);
8332 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8333 : elfcpp::R_POWERPC_RELATIVE);
8334 rela_dyn->add_symbolless_global_addend(
8335 gsym, dynrel, output_section, object, data_shndx,
8336 reloc.get_r_offset(), reloc.get_r_addend());
8340 Reloc_section* rela_dyn
8341 = target->rela_dyn_section(symtab, layout, is_ifunc);
8342 check_non_pic(object, r_type);
8343 rela_dyn->add_global(gsym, r_type, output_section,
8345 reloc.get_r_offset(),
8346 reloc.get_r_addend());
8349 && parameters->options().toc_optimize()
8350 && data_shndx == ppc_object->toc_shndx())
8351 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8357 case elfcpp::R_PPC64_PLT_PCREL34:
8358 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8359 target->set_powerxx_stubs();
8361 case elfcpp::R_POWERPC_PLT16_LO:
8362 case elfcpp::R_POWERPC_PLT16_HI:
8363 case elfcpp::R_POWERPC_PLT16_HA:
8364 case elfcpp::R_PPC64_PLT16_LO_DS:
8366 target->make_plt_entry(symtab, layout, gsym);
8369 case elfcpp::R_PPC64_REL24_NOTOC:
8373 case elfcpp::R_PPC_PLTREL24:
8374 case elfcpp::R_POWERPC_REL24:
8377 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8378 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8379 r_type, r_sym, reloc.get_r_addend());
8380 if (gsym->needs_plt_entry()
8381 || (!gsym->final_value_is_known()
8382 && (gsym->is_undefined()
8383 || gsym->is_from_dynobj()
8384 || gsym->is_preemptible())))
8385 target->make_plt_entry(symtab, layout, gsym);
8389 case elfcpp::R_PPC64_REL64:
8390 case elfcpp::R_POWERPC_REL32:
8391 // Make a dynamic relocation if necessary.
8392 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
8394 if (!parameters->options().output_is_position_independent()
8395 && gsym->may_need_copy_reloc())
8397 target->copy_reloc(symtab, layout, object,
8398 data_shndx, output_section, gsym,
8403 Reloc_section* rela_dyn
8404 = target->rela_dyn_section(symtab, layout, is_ifunc);
8405 check_non_pic(object, r_type);
8406 rela_dyn->add_global(gsym, r_type, output_section, object,
8407 data_shndx, reloc.get_r_offset(),
8408 reloc.get_r_addend());
8413 case elfcpp::R_POWERPC_REL14:
8414 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8415 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8418 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8419 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8420 r_type, r_sym, reloc.get_r_addend());
8424 case elfcpp::R_PPC64_TOCSAVE:
8425 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8426 // caller has already saved r2 and thus a plt call stub need not
8429 && target->mark_pltcall(ppc_object, data_shndx,
8430 reloc.get_r_offset() - 4, symtab))
8432 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8434 unsigned int shndx = gsym->shndx(&is_ordinary);
8436 object->error(_("tocsave symbol %u has bad shndx %u"),
8440 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
8441 target->add_tocsave(ppc_object, shndx,
8442 sym->value() + reloc.get_r_addend());
8447 case elfcpp::R_POWERPC_REL16:
8448 case elfcpp::R_POWERPC_REL16_LO:
8449 case elfcpp::R_POWERPC_REL16_HI:
8450 case elfcpp::R_POWERPC_REL16_HA:
8451 case elfcpp::R_POWERPC_REL16DX_HA:
8452 case elfcpp::R_PPC64_REL16_HIGH:
8453 case elfcpp::R_PPC64_REL16_HIGHA:
8454 case elfcpp::R_PPC64_REL16_HIGHER:
8455 case elfcpp::R_PPC64_REL16_HIGHERA:
8456 case elfcpp::R_PPC64_REL16_HIGHEST:
8457 case elfcpp::R_PPC64_REL16_HIGHESTA:
8458 case elfcpp::R_POWERPC_SECTOFF:
8459 case elfcpp::R_POWERPC_SECTOFF_LO:
8460 case elfcpp::R_POWERPC_SECTOFF_HI:
8461 case elfcpp::R_POWERPC_SECTOFF_HA:
8462 case elfcpp::R_PPC64_SECTOFF_DS:
8463 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8464 case elfcpp::R_POWERPC_TPREL16:
8465 case elfcpp::R_POWERPC_TPREL16_LO:
8466 case elfcpp::R_POWERPC_TPREL16_HI:
8467 case elfcpp::R_POWERPC_TPREL16_HA:
8468 case elfcpp::R_PPC64_TPREL16_DS:
8469 case elfcpp::R_PPC64_TPREL16_LO_DS:
8470 case elfcpp::R_PPC64_TPREL16_HIGH:
8471 case elfcpp::R_PPC64_TPREL16_HIGHA:
8472 case elfcpp::R_PPC64_TPREL16_HIGHER:
8473 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8474 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8475 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8476 case elfcpp::R_POWERPC_DTPREL16:
8477 case elfcpp::R_POWERPC_DTPREL16_LO:
8478 case elfcpp::R_POWERPC_DTPREL16_HI:
8479 case elfcpp::R_POWERPC_DTPREL16_HA:
8480 case elfcpp::R_PPC64_DTPREL16_DS:
8481 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8482 case elfcpp::R_PPC64_DTPREL16_HIGH:
8483 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8484 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8485 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8486 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8487 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8488 case elfcpp::R_PPC64_TLSGD:
8489 case elfcpp::R_PPC64_TLSLD:
8490 case elfcpp::R_PPC64_ADDR64_LOCAL:
8493 case elfcpp::R_PPC64_GOT_PCREL34:
8494 target->set_powerxx_stubs();
8496 case elfcpp::R_POWERPC_GOT16:
8497 case elfcpp::R_POWERPC_GOT16_LO:
8498 case elfcpp::R_POWERPC_GOT16_HI:
8499 case elfcpp::R_POWERPC_GOT16_HA:
8500 case elfcpp::R_PPC64_GOT16_DS:
8501 case elfcpp::R_PPC64_GOT16_LO_DS:
8503 // The symbol requires a GOT entry.
8504 Output_data_got_powerpc<size, big_endian>* got;
8506 got = target->got_section(symtab, layout);
8507 if (gsym->final_value_is_known())
8510 && (size == 32 || target->abiversion() >= 2))
8511 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
8513 got->add_global(gsym, GOT_TYPE_STANDARD);
8515 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
8517 // If we are generating a shared object or a pie, this
8518 // symbol's GOT entry will be set by a dynamic relocation.
8519 unsigned int off = got->add_constant(0);
8520 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
8522 Reloc_section* rela_dyn
8523 = target->rela_dyn_section(symtab, layout, is_ifunc);
8525 if (gsym->can_use_relative_reloc(false)
8527 || target->abiversion() >= 2)
8528 && gsym->visibility() == elfcpp::STV_PROTECTED
8529 && parameters->options().shared()))
8531 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8532 : elfcpp::R_POWERPC_RELATIVE);
8533 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
8537 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
8538 rela_dyn->add_global(gsym, dynrel, got, off, 0);
8544 case elfcpp::R_PPC64_TOC16:
8545 case elfcpp::R_PPC64_TOC16_LO:
8546 case elfcpp::R_PPC64_TOC16_HI:
8547 case elfcpp::R_PPC64_TOC16_HA:
8548 case elfcpp::R_PPC64_TOC16_DS:
8549 case elfcpp::R_PPC64_TOC16_LO_DS:
8550 // We need a GOT section.
8551 target->got_section(symtab, layout);
8554 case elfcpp::R_POWERPC_GOT_TLSGD16:
8555 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8556 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8557 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8559 const bool final = gsym->final_value_is_known();
8560 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8561 if (tls_type == tls::TLSOPT_NONE)
8563 Output_data_got_powerpc<size, big_endian>* got
8564 = target->got_section(symtab, layout);
8565 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8566 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
8567 elfcpp::R_POWERPC_DTPMOD,
8568 elfcpp::R_POWERPC_DTPREL);
8570 else if (tls_type == tls::TLSOPT_TO_IE)
8572 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
8574 Output_data_got_powerpc<size, big_endian>* got
8575 = target->got_section(symtab, layout);
8576 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8577 if (gsym->is_undefined()
8578 || gsym->is_from_dynobj())
8580 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
8581 elfcpp::R_POWERPC_TPREL);
8585 unsigned int off = got->add_constant(0);
8586 gsym->set_got_offset(GOT_TYPE_TPREL, off);
8587 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
8588 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
8593 else if (tls_type == tls::TLSOPT_TO_LE)
8595 // no GOT relocs needed for Local Exec.
8602 case elfcpp::R_POWERPC_GOT_TLSLD16:
8603 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8604 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8605 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8607 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8608 if (tls_type == tls::TLSOPT_NONE)
8609 target->tlsld_got_offset(symtab, layout, object);
8610 else if (tls_type == tls::TLSOPT_TO_LE)
8612 // no GOT relocs needed for Local Exec.
8613 if (parameters->options().emit_relocs())
8615 Output_section* os = layout->tls_segment()->first_section();
8616 gold_assert(os != NULL);
8617 os->set_needs_symtab_index();
8625 case elfcpp::R_POWERPC_GOT_DTPREL16:
8626 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8627 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8628 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8630 Output_data_got_powerpc<size, big_endian>* got
8631 = target->got_section(symtab, layout);
8632 if (!gsym->final_value_is_known()
8633 && (gsym->is_from_dynobj()
8634 || gsym->is_undefined()
8635 || gsym->is_preemptible()))
8636 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
8637 target->rela_dyn_section(layout),
8638 elfcpp::R_POWERPC_DTPREL);
8640 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
8644 case elfcpp::R_POWERPC_GOT_TPREL16:
8645 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8646 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8647 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8649 const bool final = gsym->final_value_is_known();
8650 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8651 if (tls_type == tls::TLSOPT_NONE)
8653 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
8655 Output_data_got_powerpc<size, big_endian>* got
8656 = target->got_section(symtab, layout);
8657 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8658 if (gsym->is_undefined()
8659 || gsym->is_from_dynobj())
8661 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
8662 elfcpp::R_POWERPC_TPREL);
8666 unsigned int off = got->add_constant(0);
8667 gsym->set_got_offset(GOT_TYPE_TPREL, off);
8668 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
8669 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
8674 else if (tls_type == tls::TLSOPT_TO_LE)
8676 // no GOT relocs needed for Local Exec.
8684 unsupported_reloc_global(object, r_type, gsym);
8689 && parameters->options().toc_optimize())
8691 if (data_shndx == ppc_object->toc_shndx())
8694 if (r_type != elfcpp::R_PPC64_ADDR64
8695 || (is_ifunc && target->abiversion() < 2))
8697 else if (parameters->options().output_is_position_independent()
8698 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
8701 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8704 enum {no_check, check_lo, check_ha} insn_check;
8708 insn_check = no_check;
8711 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8712 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8713 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8714 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8715 case elfcpp::R_POWERPC_GOT16_HA:
8716 case elfcpp::R_PPC64_TOC16_HA:
8717 insn_check = check_ha;
8720 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8721 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8722 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8723 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8724 case elfcpp::R_POWERPC_GOT16_LO:
8725 case elfcpp::R_PPC64_GOT16_LO_DS:
8726 case elfcpp::R_PPC64_TOC16_LO:
8727 case elfcpp::R_PPC64_TOC16_LO_DS:
8728 insn_check = check_lo;
8732 section_size_type slen;
8733 const unsigned char* view = NULL;
8734 if (insn_check != no_check)
8736 view = ppc_object->section_contents(data_shndx, &slen, false);
8737 section_size_type off =
8738 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8741 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8742 if (insn_check == check_lo
8743 ? !ok_lo_toc_insn(insn, r_type)
8744 : ((insn & ((0x3f << 26) | 0x1f << 16))
8745 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8747 ppc_object->set_no_toc_opt();
8748 gold_warning(_("%s: toc optimization is not supported "
8749 "for %#08x instruction"),
8750 ppc_object->name().c_str(), insn);
8759 case elfcpp::R_PPC64_TOC16:
8760 case elfcpp::R_PPC64_TOC16_LO:
8761 case elfcpp::R_PPC64_TOC16_HI:
8762 case elfcpp::R_PPC64_TOC16_HA:
8763 case elfcpp::R_PPC64_TOC16_DS:
8764 case elfcpp::R_PPC64_TOC16_LO_DS:
8765 if (gsym->source() == Symbol::FROM_OBJECT
8766 && !gsym->object()->is_dynamic())
8768 Powerpc_relobj<size, big_endian>* sym_object
8769 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
8771 unsigned int shndx = gsym->shndx(&is_ordinary);
8772 if (shndx == sym_object->toc_shndx())
8774 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
8775 Address dst_off = sym->value() + reloc.get_r_addend();
8776 if (dst_off < sym_object->section_size(shndx))
8779 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8781 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8783 // Need to check that the insn is a ld
8785 view = ppc_object->section_contents(data_shndx,
8788 section_size_type off =
8789 (convert_to_section_size_type(reloc.get_r_offset())
8790 + (big_endian ? -2 : 3));
8792 && (view[off] & (0x3f << 2)) == (58u << 2))
8796 sym_object->set_no_toc_opt(dst_off);
8808 case elfcpp::R_PPC_LOCAL24PC:
8809 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
8810 gold_error(_("%s: unsupported -mbss-plt code"),
8811 ppc_object->name().c_str());
8820 case elfcpp::R_POWERPC_GOT_TLSLD16:
8821 case elfcpp::R_POWERPC_GOT_TLSGD16:
8822 case elfcpp::R_POWERPC_GOT_TPREL16:
8823 case elfcpp::R_POWERPC_GOT_DTPREL16:
8824 case elfcpp::R_POWERPC_GOT16:
8825 case elfcpp::R_PPC64_GOT16_DS:
8826 case elfcpp::R_PPC64_TOC16:
8827 case elfcpp::R_PPC64_TOC16_DS:
8828 ppc_object->set_has_small_toc_reloc();
8834 // Process relocations for gc.
8836 template<int size, bool big_endian>
8838 Target_powerpc<size, big_endian>::gc_process_relocs(
8839 Symbol_table* symtab,
8841 Sized_relobj_file<size, big_endian>* object,
8842 unsigned int data_shndx,
8844 const unsigned char* prelocs,
8846 Output_section* output_section,
8847 bool needs_special_offset_handling,
8848 size_t local_symbol_count,
8849 const unsigned char* plocal_symbols)
8851 typedef Target_powerpc<size, big_endian> Powerpc;
8852 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8855 Powerpc_relobj<size, big_endian>* ppc_object
8856 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8858 ppc_object->set_opd_valid();
8859 if (size == 64 && data_shndx == ppc_object->opd_shndx())
8861 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
8862 for (p = ppc_object->access_from_map()->begin();
8863 p != ppc_object->access_from_map()->end();
8866 Address dst_off = p->first;
8867 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
8868 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
8869 for (s = p->second.begin(); s != p->second.end(); ++s)
8871 Relobj* src_obj = s->first;
8872 unsigned int src_indx = s->second;
8873 symtab->gc()->add_reference(src_obj, src_indx,
8874 ppc_object, dst_indx);
8878 ppc_object->access_from_map()->clear();
8879 ppc_object->process_gc_mark(symtab);
8880 // Don't look at .opd relocs as .opd will reference everything.
8884 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
8893 needs_special_offset_handling,
8898 // Handle target specific gc actions when adding a gc reference from
8899 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
8900 // and DST_OFF. For powerpc64, this adds a referenc to the code
8901 // section of a function descriptor.
8903 template<int size, bool big_endian>
8905 Target_powerpc<size, big_endian>::do_gc_add_reference(
8906 Symbol_table* symtab,
8908 unsigned int src_shndx,
8910 unsigned int dst_shndx,
8911 Address dst_off) const
8913 if (size != 64 || dst_obj->is_dynamic())
8916 Powerpc_relobj<size, big_endian>* ppc_object
8917 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
8918 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
8920 if (ppc_object->opd_valid())
8922 dst_shndx = ppc_object->get_opd_ent(dst_off);
8923 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
8927 // If we haven't run scan_opd_relocs, we must delay
8928 // processing this function descriptor reference.
8929 ppc_object->add_reference(src_obj, src_shndx, dst_off);
8934 // Add any special sections for this symbol to the gc work list.
8935 // For powerpc64, this adds the code section of a function
8938 template<int size, bool big_endian>
8940 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
8941 Symbol_table* symtab,
8946 Powerpc_relobj<size, big_endian>* ppc_object
8947 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
8949 unsigned int shndx = sym->shndx(&is_ordinary);
8950 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
8952 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
8953 Address dst_off = gsym->value();
8954 if (ppc_object->opd_valid())
8956 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
8957 symtab->gc()->worklist().push_back(Section_id(ppc_object,
8961 ppc_object->add_gc_mark(dst_off);
8966 // For a symbol location in .opd, set LOC to the location of the
8969 template<int size, bool big_endian>
8971 Target_powerpc<size, big_endian>::do_function_location(
8972 Symbol_location* loc) const
8974 if (size == 64 && loc->shndx != 0)
8976 if (loc->object->is_dynamic())
8978 Powerpc_dynobj<size, big_endian>* ppc_object
8979 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
8980 if (loc->shndx == ppc_object->opd_shndx())
8983 Address off = loc->offset - ppc_object->opd_address();
8984 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
8985 loc->offset = dest_off;
8990 const Powerpc_relobj<size, big_endian>* ppc_object
8991 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
8992 if (loc->shndx == ppc_object->opd_shndx())
8995 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
8996 loc->offset = dest_off;
9002 // FNOFFSET in section SHNDX in OBJECT is the start of a function
9003 // compiled with -fsplit-stack. The function calls non-split-stack
9004 // code. Change the function to ensure it has enough stack space to
9005 // call some random function.
9007 template<int size, bool big_endian>
9009 Target_powerpc<size, big_endian>::do_calls_non_split(
9012 section_offset_type fnoffset,
9013 section_size_type fnsize,
9014 const unsigned char* prelocs,
9016 unsigned char* view,
9017 section_size_type view_size,
9019 std::string* to) const
9021 // 32-bit not supported.
9025 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
9026 prelocs, reloc_count, view, view_size,
9031 // The function always starts with
9032 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
9033 // addis %r12,%r1,-allocate@ha
9034 // addi %r12,%r12,-allocate@l
9036 // but note that the addis or addi may be replaced with a nop
9038 unsigned char *entry = view + fnoffset;
9039 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9041 if ((insn & 0xffff0000) == addis_2_12)
9043 /* Skip ELFv2 global entry code. */
9045 insn = elfcpp::Swap<32, big_endian>::readval(entry);
9048 unsigned char *pinsn = entry;
9050 const uint32_t ld_private_ss = 0xe80d8fc0;
9051 if (insn == ld_private_ss)
9053 int32_t allocate = 0;
9057 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9058 if ((insn & 0xffff0000) == addis_12_1)
9059 allocate += (insn & 0xffff) << 16;
9060 else if ((insn & 0xffff0000) == addi_12_1
9061 || (insn & 0xffff0000) == addi_12_12)
9062 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9063 else if (insn != nop)
9066 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9068 int extra = parameters->options().split_stack_adjust_size();
9070 if (allocate >= 0 || extra < 0)
9072 object->error(_("split-stack stack size overflow at "
9073 "section %u offset %0zx"),
9074 shndx, static_cast<size_t>(fnoffset));
9078 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9079 if (insn != addis_12_1)
9081 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9083 insn = addi_12_12 | (allocate & 0xffff);
9084 if (insn != addi_12_12)
9086 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9092 insn = addi_12_1 | (allocate & 0xffff);
9093 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9096 if (pinsn != entry + 12)
9097 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9105 if (!object->has_no_split_stack())
9106 object->error(_("failed to match split-stack sequence at "
9107 "section %u offset %0zx"),
9108 shndx, static_cast<size_t>(fnoffset));
9112 // Scan relocations for a section.
9114 template<int size, bool big_endian>
9116 Target_powerpc<size, big_endian>::scan_relocs(
9117 Symbol_table* symtab,
9119 Sized_relobj_file<size, big_endian>* object,
9120 unsigned int data_shndx,
9121 unsigned int sh_type,
9122 const unsigned char* prelocs,
9124 Output_section* output_section,
9125 bool needs_special_offset_handling,
9126 size_t local_symbol_count,
9127 const unsigned char* plocal_symbols)
9129 typedef Target_powerpc<size, big_endian> Powerpc;
9130 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9133 if (!this->plt_localentry0_init_)
9135 bool plt_localentry0 = false;
9137 && this->abiversion() >= 2)
9139 if (parameters->options().user_set_plt_localentry())
9140 plt_localentry0 = parameters->options().plt_localentry();
9142 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9143 gold_warning(_("--plt-localentry is especially dangerous without "
9144 "ld.so support to detect ABI violations"));
9146 this->plt_localentry0_ = plt_localentry0;
9147 this->plt_localentry0_init_ = true;
9150 if (sh_type == elfcpp::SHT_REL)
9152 gold_error(_("%s: unsupported REL reloc section"),
9153 object->name().c_str());
9157 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9166 needs_special_offset_handling,
9171 // Functor class for processing the global symbol table.
9172 // Removes symbols defined on discarded opd entries.
9174 template<bool big_endian>
9175 class Global_symbol_visitor_opd
9178 Global_symbol_visitor_opd()
9182 operator()(Sized_symbol<64>* sym)
9184 if (sym->has_symtab_index()
9185 || sym->source() != Symbol::FROM_OBJECT
9186 || !sym->in_real_elf())
9189 if (sym->object()->is_dynamic())
9192 Powerpc_relobj<64, big_endian>* symobj
9193 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
9194 if (symobj->opd_shndx() == 0)
9198 unsigned int shndx = sym->shndx(&is_ordinary);
9199 if (shndx == symobj->opd_shndx()
9200 && symobj->get_opd_discard(sym->value()))
9202 sym->set_undefined();
9203 sym->set_visibility(elfcpp::STV_DEFAULT);
9204 sym->set_is_defined_in_discarded_section();
9205 sym->set_symtab_index(-1U);
9210 template<int size, bool big_endian>
9212 Target_powerpc<size, big_endian>::define_save_restore_funcs(
9214 Symbol_table* symtab)
9218 Output_data_save_res<size, big_endian>* savres
9219 = new Output_data_save_res<size, big_endian>(symtab);
9220 this->savres_section_ = savres;
9221 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
9222 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
9223 savres, ORDER_TEXT, false);
9227 // Sort linker created .got section first (for the header), then input
9228 // sections belonging to files using small model code.
9230 template<bool big_endian>
9231 class Sort_toc_sections
9235 operator()(const Output_section::Input_section& is1,
9236 const Output_section::Input_section& is2) const
9238 if (!is1.is_input_section() && is2.is_input_section())
9241 = (is1.is_input_section()
9242 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
9243 ->has_small_toc_reloc()));
9245 = (is2.is_input_section()
9246 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
9247 ->has_small_toc_reloc()));
9248 return small1 && !small2;
9252 // Finalize the sections.
9254 template<int size, bool big_endian>
9256 Target_powerpc<size, big_endian>::do_finalize_sections(
9258 const Input_objects* input_objects,
9259 Symbol_table* symtab)
9261 if (parameters->doing_static_link())
9263 // At least some versions of glibc elf-init.o have a strong
9264 // reference to __rela_iplt marker syms. A weak ref would be
9266 if (this->iplt_ != NULL)
9268 Reloc_section* rel = this->iplt_->rel_plt();
9269 symtab->define_in_output_data("__rela_iplt_start", NULL,
9270 Symbol_table::PREDEFINED, rel, 0, 0,
9271 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9272 elfcpp::STV_HIDDEN, 0, false, true);
9273 symtab->define_in_output_data("__rela_iplt_end", NULL,
9274 Symbol_table::PREDEFINED, rel, 0, 0,
9275 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9276 elfcpp::STV_HIDDEN, 0, true, true);
9280 symtab->define_as_constant("__rela_iplt_start", NULL,
9281 Symbol_table::PREDEFINED, 0, 0,
9282 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9283 elfcpp::STV_HIDDEN, 0, true, false);
9284 symtab->define_as_constant("__rela_iplt_end", NULL,
9285 Symbol_table::PREDEFINED, 0, 0,
9286 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9287 elfcpp::STV_HIDDEN, 0, true, false);
9293 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
9294 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
9296 if (!parameters->options().relocatable())
9298 this->define_save_restore_funcs(layout, symtab);
9300 // Annoyingly, we need to make these sections now whether or
9301 // not we need them. If we delay until do_relax then we
9302 // need to mess with the relaxation machinery checkpointing.
9303 this->got_section(symtab, layout);
9304 this->make_brlt_section(layout);
9306 if (parameters->options().toc_sort())
9308 Output_section* os = this->got_->output_section();
9309 if (os != NULL && os->input_sections().size() > 1)
9310 std::stable_sort(os->input_sections().begin(),
9311 os->input_sections().end(),
9312 Sort_toc_sections<big_endian>());
9317 // Fill in some more dynamic tags.
9318 Output_data_dynamic* odyn = layout->dynamic_data();
9321 const Reloc_section* rel_plt = (this->plt_ == NULL
9323 : this->plt_->rel_plt());
9324 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
9325 this->rela_dyn_, true, size == 32);
9329 if (this->got_ != NULL)
9331 this->got_->finalize_data_size();
9332 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
9333 this->got_, this->got_->g_o_t());
9335 if (this->has_tls_get_addr_opt_)
9336 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
9340 if (this->glink_ != NULL)
9342 this->glink_->finalize_data_size();
9343 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
9345 (this->glink_->pltresolve_size()
9348 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
9349 odyn->add_constant(elfcpp::DT_PPC64_OPT,
9350 ((this->has_localentry0_
9351 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
9352 | (this->has_tls_get_addr_opt_
9353 ? elfcpp::PPC64_OPT_TLS : 0)));
9357 // Emit any relocs we saved in an attempt to avoid generating COPY
9359 if (this->copy_relocs_.any_saved_relocs())
9360 this->copy_relocs_.emit(this->rela_dyn_section(layout));
9362 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9363 p != input_objects->relobj_end();
9366 Powerpc_relobj<size, big_endian>* ppc_relobj
9367 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
9368 if (ppc_relobj->attributes_section_data())
9369 this->merge_object_attributes(ppc_relobj->name().c_str(),
9370 ppc_relobj->attributes_section_data());
9372 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
9373 p != input_objects->dynobj_end();
9376 Powerpc_dynobj<size, big_endian>* ppc_dynobj
9377 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
9378 if (ppc_dynobj->attributes_section_data())
9379 this->merge_object_attributes(ppc_dynobj->name().c_str(),
9380 ppc_dynobj->attributes_section_data());
9383 // Create a .gnu.attributes section if we have merged any attributes
9385 if (this->attributes_section_data_ != NULL
9386 && this->attributes_section_data_->size() != 0)
9388 Output_attributes_section_data* attributes_section
9389 = new Output_attributes_section_data(*this->attributes_section_data_);
9390 layout->add_output_section_data(".gnu.attributes",
9391 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9392 attributes_section, ORDER_INVALID, false);
9396 // Merge object attributes from input file called NAME with those of the
9397 // output. The input object attributes are in the object pointed by PASD.
9399 template<int size, bool big_endian>
9401 Target_powerpc<size, big_endian>::merge_object_attributes(
9403 const Attributes_section_data* pasd)
9405 // Return if there is no attributes section data.
9409 // Create output object attributes.
9410 if (this->attributes_section_data_ == NULL)
9411 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
9413 const int vendor = Object_attribute::OBJ_ATTR_GNU;
9414 const Object_attribute* in_attr = pasd->known_attributes(vendor);
9415 Object_attribute* out_attr
9416 = this->attributes_section_data_->known_attributes(vendor);
9421 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
9422 int in_fp = in_attr[tag].int_value() & 0xf;
9423 int out_fp = out_attr[tag].int_value() & 0xf;
9424 if (in_fp != out_fp)
9427 if ((in_fp & 3) == 0)
9429 else if ((out_fp & 3) == 0)
9431 out_fp |= in_fp & 3;
9432 out_attr[tag].set_int_value(out_fp);
9433 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9434 this->last_fp_ = name;
9436 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
9438 err = N_("%s uses hard float, %s uses soft float");
9439 first = this->last_fp_;
9442 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
9444 err = N_("%s uses hard float, %s uses soft float");
9446 second = this->last_fp_;
9448 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
9450 err = N_("%s uses double-precision hard float, "
9451 "%s uses single-precision hard float");
9452 first = this->last_fp_;
9455 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
9457 err = N_("%s uses double-precision hard float, "
9458 "%s uses single-precision hard float");
9460 second = this->last_fp_;
9463 if (err || (in_fp & 0xc) == 0)
9465 else if ((out_fp & 0xc) == 0)
9467 out_fp |= in_fp & 0xc;
9468 out_attr[tag].set_int_value(out_fp);
9469 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9470 this->last_ld_ = name;
9472 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
9474 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9476 second = this->last_ld_;
9478 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
9480 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9481 first = this->last_ld_;
9484 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
9486 err = N_("%s uses IBM long double, %s uses IEEE long double");
9487 first = this->last_ld_;
9490 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
9492 err = N_("%s uses IBM long double, %s uses IEEE long double");
9494 second = this->last_ld_;
9499 if (parameters->options().warn_mismatch())
9500 gold_error(_(err), first, second);
9501 // Arrange for this attribute to be deleted. It's better to
9502 // say "don't know" about a file than to wrongly claim compliance.
9503 out_attr[tag].set_type(0);
9509 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
9510 int in_vec = in_attr[tag].int_value() & 3;
9511 int out_vec = out_attr[tag].int_value() & 3;
9512 if (in_vec != out_vec)
9517 else if (out_vec == 0)
9520 out_attr[tag].set_int_value(out_vec);
9521 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9522 this->last_vec_ = name;
9524 // For now, allow generic to transition to AltiVec or SPE
9525 // without a warning. If GCC marked files with their stack
9526 // alignment and used don't-care markings for files which are
9527 // not affected by the vector ABI, we could warn about this
9529 else if (in_vec == 1)
9531 else if (out_vec == 1)
9534 out_attr[tag].set_int_value(out_vec);
9535 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9536 this->last_vec_ = name;
9538 else if (out_vec < in_vec)
9540 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
9541 first = this->last_vec_;
9544 else if (out_vec > in_vec)
9546 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
9548 second = this->last_vec_;
9552 if (parameters->options().warn_mismatch())
9553 gold_error(_(err), first, second);
9554 out_attr[tag].set_type(0);
9558 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
9559 int in_struct = in_attr[tag].int_value() & 3;
9560 int out_struct = out_attr[tag].int_value() & 3;
9561 if (in_struct != out_struct)
9564 if (in_struct == 0 || in_struct == 3)
9566 else if (out_struct == 0)
9568 out_struct = in_struct;
9569 out_attr[tag].set_int_value(out_struct);
9570 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9571 this->last_struct_ = name;
9573 else if (out_struct < in_struct)
9575 err = N_("%s uses r3/r4 for small structure returns, "
9577 first = this->last_struct_;
9580 else if (out_struct > in_struct)
9582 err = N_("%s uses r3/r4 for small structure returns, "
9585 second = this->last_struct_;
9589 if (parameters->options().warn_mismatch())
9590 gold_error(_(err), first, second);
9591 out_attr[tag].set_type(0);
9596 // Merge Tag_compatibility attributes and any common GNU ones.
9597 this->attributes_section_data_->merge(name, pasd);
9600 // Emit any saved relocs, and mark toc entries using any of these
9601 // relocs as not optimizable.
9603 template<int sh_type, int size, bool big_endian>
9605 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
9606 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
9609 && parameters->options().toc_optimize())
9611 for (typename Copy_relocs<sh_type, size, big_endian>::
9612 Copy_reloc_entries::iterator p = this->entries_.begin();
9613 p != this->entries_.end();
9616 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
9619 // If the symbol is no longer defined in a dynamic object,
9620 // then we emitted a COPY relocation. If it is still
9621 // dynamic then we'll need dynamic relocations and thus
9622 // can't optimize toc entries.
9623 if (entry.sym_->is_from_dynobj())
9625 Powerpc_relobj<size, big_endian>* ppc_object
9626 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
9627 if (entry.shndx_ == ppc_object->toc_shndx())
9628 ppc_object->set_no_toc_opt(entry.address_);
9633 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
9636 // Return the value to use for a branch relocation.
9638 template<int size, bool big_endian>
9640 Target_powerpc<size, big_endian>::symval_for_branch(
9641 const Symbol_table* symtab,
9642 const Sized_symbol<size>* gsym,
9643 Powerpc_relobj<size, big_endian>* object,
9645 unsigned int *dest_shndx)
9647 if (size == 32 || this->abiversion() >= 2)
9651 // If the symbol is defined in an opd section, ie. is a function
9652 // descriptor, use the function descriptor code entry address
9653 Powerpc_relobj<size, big_endian>* symobj = object;
9655 && (gsym->source() != Symbol::FROM_OBJECT
9656 || gsym->object()->is_dynamic()))
9659 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9660 unsigned int shndx = symobj->opd_shndx();
9663 Address opd_addr = symobj->get_output_section_offset(shndx);
9664 if (opd_addr == invalid_address)
9666 opd_addr += symobj->output_section_address(shndx);
9667 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
9670 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
9671 if (symtab->is_section_folded(symobj, *dest_shndx))
9674 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
9675 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
9676 *dest_shndx = folded.second;
9678 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
9679 if (sec_addr == invalid_address)
9682 sec_addr += symobj->output_section(*dest_shndx)->address();
9683 *value = sec_addr + sec_off;
9690 relative_value_is_known(const Sized_symbol<size>* gsym)
9692 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
9695 if (gsym->is_from_dynobj()
9696 || gsym->is_undefined()
9697 || gsym->is_preemptible())
9700 if (gsym->is_absolute())
9701 return !parameters->options().output_is_position_independent();
9708 relative_value_is_known(const Symbol_value<size>* psymval)
9710 if (psymval->is_ifunc_symbol())
9714 unsigned int shndx = psymval->input_shndx(&is_ordinary);
9716 return is_ordinary && shndx != elfcpp::SHN_UNDEF;
9719 // PCREL_OPT in one instance flags to the linker that a pair of insns:
9720 // pld ra,symbol@got@pcrel
9721 // load/store rt,0(ra)
9723 // pla ra,symbol@pcrel
9724 // load/store rt,0(ra)
9725 // may be translated to
9726 // pload/pstore rt,symbol@pcrel
9728 // This function returns true if the optimization is possible, placing
9729 // the prefix insn in *PINSN1 and a NOP in *PINSN2.
9731 // On entry to this function, the linker has already determined that
9732 // the pld can be replaced with pla: *PINSN1 is that pla insn,
9733 // while *PINSN2 is the second instruction.
9736 xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
9738 uint32_t insn2 = *pinsn2 >> 32;
9741 // Check that regs match.
9742 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
9745 switch ((insn2 >> 26) & 63)
9761 // These are the PMLS cases, where we just need to tack a prefix
9762 // on the insn. Check that the D field is zero.
9763 if ((insn2 & 0xffff) != 0)
9765 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
9766 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
9770 if ((insn2 & 0xfffd) != 0)
9772 i1new = ((1ULL << 58) | (1ULL << 52)
9773 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
9774 | (insn2 & (31ULL << 21)));
9777 case 57: // lxsd, lxssp
9778 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
9780 i1new = ((1ULL << 58) | (1ULL << 52)
9781 | ((40ULL | (insn2 & 3)) << 26)
9782 | (insn2 & (31ULL << 21)));
9785 case 61: // stxsd, stxssp, lxv, stxv
9786 if ((insn2 & 3) == 0)
9788 else if ((insn2 & 3) >= 2)
9790 if ((insn2 & 0xfffc) != 0)
9792 i1new = ((1ULL << 58) | (1ULL << 52)
9793 | ((44ULL | (insn2 & 3)) << 26)
9794 | (insn2 & (31ULL << 21)));
9798 if ((insn2 & 0xfff0) != 0)
9800 i1new = ((1ULL << 58) | (1ULL << 52)
9801 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
9802 | (insn2 & (31ULL << 21)));
9807 if ((insn2 & 0xffff) != 0)
9809 i1new = ((1ULL << 58) | (1ULL << 52)
9810 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
9813 case 62: // std, stq
9814 if ((insn2 & 0xfffd) != 0)
9816 i1new = ((1ULL << 58) | (1ULL << 52)
9817 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
9818 | (insn2 & (31ULL << 21)));
9823 *pinsn2 = (uint64_t) nop << 32;
9827 // Perform a relocation.
9829 template<int size, bool big_endian>
9831 Target_powerpc<size, big_endian>::Relocate::relocate(
9832 const Relocate_info<size, big_endian>* relinfo,
9834 Target_powerpc* target,
9837 const unsigned char* preloc,
9838 const Sized_symbol<size>* gsym,
9839 const Symbol_value<size>* psymval,
9840 unsigned char* view,
9842 section_size_type view_size)
9844 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
9845 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
9846 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9851 if (target->replace_tls_get_addr(gsym))
9852 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
9854 const elfcpp::Rela<size, big_endian> rela(preloc);
9855 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
9856 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
9858 case Track_tls::NOT_EXPECTED:
9859 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9860 _("__tls_get_addr call lacks marker reloc"));
9862 case Track_tls::EXPECTED:
9863 // We have already complained.
9865 case Track_tls::SKIP:
9866 if (is_plt16_reloc<size>(r_type)
9867 || r_type == elfcpp::R_POWERPC_PLTSEQ
9868 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
9870 Insn* iview = reinterpret_cast<Insn*>(view);
9871 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
9873 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
9875 Insn* iview = reinterpret_cast<Insn*>(view);
9876 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
9878 else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
9879 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
9881 Insn* iview = reinterpret_cast<Insn*>(view);
9882 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
9883 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
9886 case Track_tls::NORMAL:
9890 // Offset from start of insn to d-field reloc.
9891 const int d_offset = big_endian ? 2 : 0;
9893 Powerpc_relobj<size, big_endian>* const object
9894 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
9896 bool has_stub_value = false;
9897 bool localentry0 = false;
9898 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
9901 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
9902 : object->local_has_plt_offset(r_sym));
9904 && !is_plt16_reloc<size>(r_type)
9905 && r_type != elfcpp::R_PPC64_PLT_PCREL34
9906 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC
9907 && r_type != elfcpp::R_POWERPC_PLTSEQ
9908 && r_type != elfcpp::R_POWERPC_PLTCALL
9909 && r_type != elfcpp::R_PPC64_PLTSEQ_NOTOC
9910 && r_type != elfcpp::R_PPC64_PLTCALL_NOTOC
9911 && (!psymval->is_ifunc_symbol()
9912 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
9916 && target->abiversion() >= 2
9917 && !parameters->options().output_is_position_independent()
9918 && !is_branch_reloc<size>(r_type))
9920 Address off = target->glink_section()->find_global_entry(gsym);
9921 if (off != invalid_address)
9923 value = target->glink_section()->global_entry_address() + off;
9924 has_stub_value = true;
9929 Stub_table<size, big_endian>* stub_table = NULL;
9930 if (target->stub_tables().size() == 1)
9931 stub_table = target->stub_tables()[0];
9932 if (stub_table == NULL
9935 && !parameters->options().output_is_position_independent()
9936 && !is_branch_reloc<size>(r_type)))
9937 stub_table = object->stub_table(relinfo->data_shndx);
9938 if (stub_table == NULL)
9940 // This is a ref from a data section to an ifunc symbol,
9941 // or a non-branch reloc for which we always want to use
9942 // one set of stubs for resolving function addresses.
9943 if (target->stub_tables().size() != 0)
9944 stub_table = target->stub_tables()[0];
9946 if (stub_table != NULL)
9948 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
9950 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
9951 rela.get_r_addend());
9953 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
9954 rela.get_r_addend());
9957 value = stub_table->stub_address() + ent->off_;
9958 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
9959 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
9960 size_t reloc_count = shdr.get_sh_size() / reloc_size;
9963 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
9967 && relnum < reloc_count - 1)
9969 Reltype next_rela(preloc + reloc_size);
9970 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
9971 == elfcpp::R_PPC64_TOCSAVE
9972 && next_rela.get_r_offset() == rela.get_r_offset() + 4)
9975 localentry0 = ent->localentry0_;
9976 has_stub_value = true;
9980 // We don't care too much about bogus debug references to
9981 // non-local functions, but otherwise there had better be a plt
9982 // call stub or global entry stub as appropriate.
9983 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
9986 if (has_plt_offset && (is_plt16_reloc<size>(r_type)
9987 || r_type == elfcpp::R_PPC64_PLT_PCREL34
9988 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
9990 const Output_data_plt_powerpc<size, big_endian>* plt;
9992 value = target->plt_off(gsym, &plt);
9994 value = target->plt_off(object, r_sym, &plt);
9995 value += plt->address();
9999 if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10000 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
10001 value -= (target->got_section()->output_section()->address()
10002 + object->toc_base_offset());
10004 else if (parameters->options().output_is_position_independent())
10006 if (rela.get_r_addend() >= 32768)
10008 unsigned int got2 = object->got2_shndx();
10009 value -= (object->get_output_section_offset(got2)
10010 + object->output_section(got2)->address()
10011 + rela.get_r_addend());
10014 value -= (target->got_section()->address()
10015 + target->got_section()->g_o_t());
10018 else if (!has_plt_offset
10019 && (is_plt16_reloc<size>(r_type)
10020 || r_type == elfcpp::R_POWERPC_PLTSEQ
10021 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
10023 Insn* iview = reinterpret_cast<Insn*>(view);
10024 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10025 r_type = elfcpp::R_POWERPC_NONE;
10027 else if (!has_plt_offset
10028 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10029 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10031 Insn* iview = reinterpret_cast<Insn*>(view);
10032 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10033 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10034 r_type = elfcpp::R_POWERPC_NONE;
10036 else if (r_type == elfcpp::R_POWERPC_GOT16
10037 || r_type == elfcpp::R_POWERPC_GOT16_LO
10038 || r_type == elfcpp::R_POWERPC_GOT16_HI
10039 || r_type == elfcpp::R_POWERPC_GOT16_HA
10040 || r_type == elfcpp::R_PPC64_GOT16_DS
10041 || r_type == elfcpp::R_PPC64_GOT16_LO_DS
10042 || r_type == elfcpp::R_PPC64_GOT_PCREL34)
10046 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
10047 value = gsym->got_offset(GOT_TYPE_STANDARD);
10051 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
10052 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
10054 if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
10055 value += target->got_section()->address();
10057 value -= target->got_section()->got_base_offset(object);
10059 else if (r_type == elfcpp::R_PPC64_TOC)
10061 value = (target->got_section()->output_section()->address()
10062 + object->toc_base_offset());
10064 else if (gsym != NULL
10065 && (r_type == elfcpp::R_POWERPC_REL24
10066 || r_type == elfcpp::R_PPC_PLTREL24)
10071 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10072 Valtype* wv = reinterpret_cast<Valtype*>(view);
10073 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10074 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
10076 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
10077 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
10078 if ((insn & 1) != 0
10080 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
10082 elfcpp::Swap<32, big_endian>::
10083 writeval(wv + 1, ld_2_1 + target->stk_toc());
10084 can_plt_call = true;
10089 // If we don't have a branch and link followed by a nop,
10090 // we can't go via the plt because there is no place to
10091 // put a toc restoring instruction.
10092 // Unless we know we won't be returning.
10093 if (strcmp(gsym->name(), "__libc_start_main") == 0)
10094 can_plt_call = true;
10098 // g++ as of 20130507 emits self-calls without a
10099 // following nop. This is arguably wrong since we have
10100 // conflicting information. On the one hand a global
10101 // symbol and on the other a local call sequence, but
10102 // don't error for this special case.
10103 // It isn't possible to cheaply verify we have exactly
10104 // such a call. Allow all calls to the same section.
10106 Address code = value;
10107 if (gsym->source() == Symbol::FROM_OBJECT
10108 && gsym->object() == object)
10110 unsigned int dest_shndx = 0;
10111 if (target->abiversion() < 2)
10113 Address addend = rela.get_r_addend();
10114 code = psymval->value(object, addend);
10115 target->symval_for_branch(relinfo->symtab, gsym, object,
10116 &code, &dest_shndx);
10119 if (dest_shndx == 0)
10120 dest_shndx = gsym->shndx(&is_ordinary);
10121 ok = dest_shndx == relinfo->data_shndx;
10125 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10126 _("call lacks nop, can't restore toc; "
10127 "recompile with -fPIC"));
10133 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10134 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10135 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
10136 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
10138 // First instruction of a global dynamic sequence, arg setup insn.
10139 const bool final = gsym == NULL || gsym->final_value_is_known();
10140 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10141 enum Got_type got_type = GOT_TYPE_STANDARD;
10142 if (tls_type == tls::TLSOPT_NONE)
10143 got_type = GOT_TYPE_TLSGD;
10144 else if (tls_type == tls::TLSOPT_TO_IE)
10145 got_type = GOT_TYPE_TPREL;
10146 if (got_type != GOT_TYPE_STANDARD)
10150 gold_assert(gsym->has_got_offset(got_type));
10151 value = gsym->got_offset(got_type);
10155 gold_assert(object->local_has_got_offset(r_sym, got_type));
10156 value = object->local_got_offset(r_sym, got_type);
10158 value -= target->got_section()->got_base_offset(object);
10160 if (tls_type == tls::TLSOPT_TO_IE)
10162 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10163 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10165 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10166 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10167 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
10169 insn |= 32 << 26; // lwz
10171 insn |= 58 << 26; // ld
10172 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10174 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
10175 - elfcpp::R_POWERPC_GOT_TLSGD16);
10177 else if (tls_type == tls::TLSOPT_TO_LE)
10179 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10180 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10182 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10183 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10184 insn &= (1 << 26) - (1 << 21); // extract rt
10188 insn |= addis_0_13;
10189 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10190 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10191 value = psymval->value(object, rela.get_r_addend());
10195 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10197 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10198 r_type = elfcpp::R_POWERPC_NONE;
10202 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10203 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
10204 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
10205 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
10207 // First instruction of a local dynamic sequence, arg setup insn.
10208 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10209 if (tls_type == tls::TLSOPT_NONE)
10211 value = target->tlsld_got_offset();
10212 value -= target->got_section()->got_base_offset(object);
10216 gold_assert(tls_type == tls::TLSOPT_TO_LE);
10217 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10218 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
10220 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10221 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10222 insn &= (1 << 26) - (1 << 21); // extract rt
10226 insn |= addis_0_13;
10227 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10228 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10229 value = dtp_offset;
10233 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10235 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10236 r_type = elfcpp::R_POWERPC_NONE;
10240 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
10241 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
10242 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
10243 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
10245 // Accesses relative to a local dynamic sequence address,
10246 // no optimisation here.
10249 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
10250 value = gsym->got_offset(GOT_TYPE_DTPREL);
10254 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
10255 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
10257 value -= target->got_section()->got_base_offset(object);
10259 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10260 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
10261 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
10262 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
10264 // First instruction of initial exec sequence.
10265 const bool final = gsym == NULL || gsym->final_value_is_known();
10266 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10267 if (tls_type == tls::TLSOPT_NONE)
10271 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
10272 value = gsym->got_offset(GOT_TYPE_TPREL);
10276 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
10277 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
10279 value -= target->got_section()->got_base_offset(object);
10283 gold_assert(tls_type == tls::TLSOPT_TO_LE);
10284 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10285 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
10287 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10288 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10289 insn &= (1 << 26) - (1 << 21); // extract rt from ld
10293 insn |= addis_0_13;
10294 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10295 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10296 value = psymval->value(object, rela.get_r_addend());
10300 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10302 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10303 r_type = elfcpp::R_POWERPC_NONE;
10307 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
10308 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
10310 // Second instruction of a global dynamic sequence,
10311 // the __tls_get_addr call
10312 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
10313 const bool final = gsym == NULL || gsym->final_value_is_known();
10314 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10315 if (tls_type != tls::TLSOPT_NONE)
10317 if (tls_type == tls::TLSOPT_TO_IE)
10319 Insn* iview = reinterpret_cast<Insn*>(view);
10320 Insn insn = add_3_3_13;
10323 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10324 r_type = elfcpp::R_POWERPC_NONE;
10328 Insn* iview = reinterpret_cast<Insn*>(view);
10329 Insn insn = addi_3_3;
10330 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10331 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10333 value = psymval->value(object, rela.get_r_addend());
10335 this->skip_next_tls_get_addr_call();
10338 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
10339 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
10341 // Second instruction of a local dynamic sequence,
10342 // the __tls_get_addr call
10343 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
10344 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10345 if (tls_type == tls::TLSOPT_TO_LE)
10347 Insn* iview = reinterpret_cast<Insn*>(view);
10348 Insn insn = addi_3_3;
10349 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10350 this->skip_next_tls_get_addr_call();
10351 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10353 value = dtp_offset;
10356 else if (r_type == elfcpp::R_POWERPC_TLS)
10358 // Second instruction of an initial exec sequence
10359 const bool final = gsym == NULL || gsym->final_value_is_known();
10360 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10361 if (tls_type == tls::TLSOPT_TO_LE)
10363 Insn* iview = reinterpret_cast<Insn*>(view);
10364 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10365 unsigned int reg = size == 32 ? 2 : 13;
10366 insn = at_tls_transform(insn, reg);
10367 gold_assert(insn != 0);
10368 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10369 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10371 value = psymval->value(object, rela.get_r_addend());
10374 else if (!has_stub_value)
10376 if (!has_plt_offset && (r_type == elfcpp::R_POWERPC_PLTCALL
10377 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
10379 // PLTCALL without plt entry => convert to direct call
10380 Insn* iview = reinterpret_cast<Insn*>(view);
10381 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10382 insn = (insn & 1) | b;
10383 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10385 r_type = elfcpp::R_PPC_PLTREL24;
10386 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
10387 r_type = elfcpp::R_PPC64_REL24_NOTOC;
10389 r_type = elfcpp::R_POWERPC_REL24;
10391 Address addend = 0;
10393 && (r_type == elfcpp::R_PPC_PLTREL24
10394 || r_type == elfcpp::R_POWERPC_PLT16_LO
10395 || r_type == elfcpp::R_POWERPC_PLT16_HI
10396 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
10397 addend = rela.get_r_addend();
10398 value = psymval->value(object, addend);
10399 if (size == 64 && is_branch_reloc<size>(r_type))
10401 if (target->abiversion() >= 2)
10404 value += object->ppc64_local_entry_offset(gsym);
10406 value += object->ppc64_local_entry_offset(r_sym);
10410 unsigned int dest_shndx;
10411 target->symval_for_branch(relinfo->symtab, gsym, object,
10412 &value, &dest_shndx);
10415 Address max_branch_offset = max_branch_delta<size>(r_type);
10416 if (max_branch_offset != 0
10417 && (value - address + max_branch_offset >= 2 * max_branch_offset
10419 && r_type == elfcpp::R_PPC64_REL24_NOTOC
10421 ? object->ppc64_needs_toc(gsym)
10422 : object->ppc64_needs_toc(r_sym)))))
10424 Stub_table<size, big_endian>* stub_table
10425 = object->stub_table(relinfo->data_shndx);
10426 if (stub_table != NULL)
10428 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
10429 = stub_table->find_long_branch_entry(object, value);
10432 if (ent->save_res_)
10433 value = (value - target->savres_section()->address()
10434 + stub_table->branch_size());
10436 value = (stub_table->stub_address() + stub_table->plt_size()
10438 has_stub_value = true;
10446 case elfcpp::R_PPC64_REL24_NOTOC:
10450 case elfcpp::R_PPC64_REL64:
10451 case elfcpp::R_POWERPC_REL32:
10452 case elfcpp::R_POWERPC_REL24:
10453 case elfcpp::R_PPC_PLTREL24:
10454 case elfcpp::R_PPC_LOCAL24PC:
10455 case elfcpp::R_POWERPC_REL16:
10456 case elfcpp::R_POWERPC_REL16_LO:
10457 case elfcpp::R_POWERPC_REL16_HI:
10458 case elfcpp::R_POWERPC_REL16_HA:
10459 case elfcpp::R_POWERPC_REL16DX_HA:
10460 case elfcpp::R_PPC64_REL16_HIGH:
10461 case elfcpp::R_PPC64_REL16_HIGHA:
10462 case elfcpp::R_PPC64_REL16_HIGHER:
10463 case elfcpp::R_PPC64_REL16_HIGHERA:
10464 case elfcpp::R_PPC64_REL16_HIGHEST:
10465 case elfcpp::R_PPC64_REL16_HIGHESTA:
10466 case elfcpp::R_POWERPC_REL14:
10467 case elfcpp::R_POWERPC_REL14_BRTAKEN:
10468 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
10469 case elfcpp::R_PPC64_PCREL34:
10470 case elfcpp::R_PPC64_GOT_PCREL34:
10471 case elfcpp::R_PPC64_PLT_PCREL34:
10472 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
10473 case elfcpp::R_PPC64_PCREL28:
10474 case elfcpp::R_PPC64_REL16_HIGHER34:
10475 case elfcpp::R_PPC64_REL16_HIGHERA34:
10476 case elfcpp::R_PPC64_REL16_HIGHEST34:
10477 case elfcpp::R_PPC64_REL16_HIGHESTA34:
10481 case elfcpp::R_PPC64_TOC16:
10482 case elfcpp::R_PPC64_TOC16_LO:
10483 case elfcpp::R_PPC64_TOC16_HI:
10484 case elfcpp::R_PPC64_TOC16_HA:
10485 case elfcpp::R_PPC64_TOC16_DS:
10486 case elfcpp::R_PPC64_TOC16_LO_DS:
10487 // Subtract the TOC base address.
10488 value -= (target->got_section()->output_section()->address()
10489 + object->toc_base_offset());
10492 case elfcpp::R_POWERPC_SECTOFF:
10493 case elfcpp::R_POWERPC_SECTOFF_LO:
10494 case elfcpp::R_POWERPC_SECTOFF_HI:
10495 case elfcpp::R_POWERPC_SECTOFF_HA:
10496 case elfcpp::R_PPC64_SECTOFF_DS:
10497 case elfcpp::R_PPC64_SECTOFF_LO_DS:
10499 value -= os->address();
10502 case elfcpp::R_PPC64_TPREL16_DS:
10503 case elfcpp::R_PPC64_TPREL16_LO_DS:
10504 case elfcpp::R_PPC64_TPREL16_HIGH:
10505 case elfcpp::R_PPC64_TPREL16_HIGHA:
10507 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
10510 case elfcpp::R_POWERPC_TPREL16:
10511 case elfcpp::R_POWERPC_TPREL16_LO:
10512 case elfcpp::R_POWERPC_TPREL16_HI:
10513 case elfcpp::R_POWERPC_TPREL16_HA:
10514 case elfcpp::R_POWERPC_TPREL:
10515 case elfcpp::R_PPC64_TPREL16_HIGHER:
10516 case elfcpp::R_PPC64_TPREL16_HIGHERA:
10517 case elfcpp::R_PPC64_TPREL16_HIGHEST:
10518 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
10519 // tls symbol values are relative to tls_segment()->vaddr()
10520 value -= tp_offset;
10523 case elfcpp::R_PPC64_DTPREL16_DS:
10524 case elfcpp::R_PPC64_DTPREL16_LO_DS:
10525 case elfcpp::R_PPC64_DTPREL16_HIGHER:
10526 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
10527 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
10528 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
10530 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
10531 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
10534 case elfcpp::R_POWERPC_DTPREL16:
10535 case elfcpp::R_POWERPC_DTPREL16_LO:
10536 case elfcpp::R_POWERPC_DTPREL16_HI:
10537 case elfcpp::R_POWERPC_DTPREL16_HA:
10538 case elfcpp::R_POWERPC_DTPREL:
10539 case elfcpp::R_PPC64_DTPREL16_HIGH:
10540 case elfcpp::R_PPC64_DTPREL16_HIGHA:
10541 // tls symbol values are relative to tls_segment()->vaddr()
10542 value -= dtp_offset;
10545 case elfcpp::R_PPC64_ADDR64_LOCAL:
10547 value += object->ppc64_local_entry_offset(gsym);
10549 value += object->ppc64_local_entry_offset(r_sym);
10556 Insn branch_bit = 0;
10559 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
10560 case elfcpp::R_POWERPC_REL14_BRTAKEN:
10561 branch_bit = 1 << 21;
10563 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
10564 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
10566 Insn* iview = reinterpret_cast<Insn*>(view);
10567 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10568 insn &= ~(1 << 21);
10569 insn |= branch_bit;
10570 if (this->is_isa_v2)
10572 // Set 'a' bit. This is 0b00010 in BO field for branch
10573 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
10574 // for branch on CTR insns (BO == 1a00t or 1a01t).
10575 if ((insn & (0x14 << 21)) == (0x04 << 21))
10576 insn |= 0x02 << 21;
10577 else if ((insn & (0x14 << 21)) == (0x10 << 21))
10578 insn |= 0x08 << 21;
10584 // Invert 'y' bit if not the default.
10585 if (static_cast<Signed_address>(value) < 0)
10588 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10592 case elfcpp::R_POWERPC_PLT16_HA:
10594 && !parameters->options().output_is_position_independent())
10596 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10597 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10599 // Convert addis to lis.
10600 if ((insn & (0x3f << 26)) == 15u << 26
10601 && (insn & (0x1f << 16)) != 0)
10603 insn &= ~(0x1f << 16);
10604 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10615 ? relative_value_is_known(gsym)
10616 : relative_value_is_known(psymval)))
10621 uint64_t pinsn, pinsn2;
10628 // Multi-instruction sequences that access the GOT/TOC can
10629 // be optimized, eg.
10630 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
10631 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
10633 // addis ra,r2,0; addi rb,ra,x@toc@l;
10634 // to nop; addi rb,r2,x@toc;
10635 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
10636 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
10637 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
10638 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
10639 case elfcpp::R_POWERPC_GOT16_HA:
10640 case elfcpp::R_PPC64_TOC16_HA:
10641 if (parameters->options().toc_optimize())
10643 iview = reinterpret_cast<Insn*>(view - d_offset);
10644 insn = elfcpp::Swap<32, big_endian>::readval(iview);
10645 if ((r_type == elfcpp::R_PPC64_TOC16_HA
10646 && object->make_toc_relative(target, &value))
10647 || (r_type == elfcpp::R_POWERPC_GOT16_HA
10648 && object->make_got_relative(target, psymval,
10649 rela.get_r_addend(),
10652 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
10653 == ((15u << 26) | (2 << 16)));
10655 if (((insn & ((0x3f << 26) | 0x1f << 16))
10656 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
10657 && value + 0x8000 < 0x10000)
10659 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10665 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
10666 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
10667 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
10668 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
10669 case elfcpp::R_POWERPC_GOT16_LO:
10670 case elfcpp::R_PPC64_GOT16_LO_DS:
10671 case elfcpp::R_PPC64_TOC16_LO:
10672 case elfcpp::R_PPC64_TOC16_LO_DS:
10673 if (parameters->options().toc_optimize())
10675 iview = reinterpret_cast<Insn*>(view - d_offset);
10676 insn = elfcpp::Swap<32, big_endian>::readval(iview);
10677 bool changed = false;
10678 if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
10679 && object->make_toc_relative(target, &value))
10680 || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
10681 && object->make_got_relative(target, psymval,
10682 rela.get_r_addend(),
10685 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
10686 insn ^= (14u << 26) ^ (58u << 26);
10687 r_type = elfcpp::R_PPC64_TOC16_LO;
10690 if (ok_lo_toc_insn(insn, r_type)
10691 && value + 0x8000 < 0x10000)
10693 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
10695 // Transform addic to addi when we change reg.
10696 insn &= ~((0x3f << 26) | (0x1f << 16));
10697 insn |= (14u << 26) | (2 << 16);
10701 insn &= ~(0x1f << 16);
10707 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10711 case elfcpp::R_PPC64_GOT_PCREL34:
10712 if (parameters->options().toc_optimize())
10714 iview = reinterpret_cast<Insn*>(view);
10715 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10717 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10718 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
10719 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
10722 Address relval = psymval->value(object, rela.get_r_addend());
10724 if (relval + (1ULL << 33) < 1ULL << 34)
10727 // Replace with paddi
10728 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
10729 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10730 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10731 pinsn & 0xffffffff);
10737 case elfcpp::R_PPC64_PCREL34:
10739 iview = reinterpret_cast<Insn*>(view);
10740 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10742 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10743 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
10744 != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
10745 | (14ULL << 26) /* paddi */))
10749 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10750 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10751 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10752 if (relnum >= reloc_count - 1)
10755 Reltype next_rela(preloc + reloc_size);
10756 if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
10757 != elfcpp::R_PPC64_PCREL_OPT)
10758 || next_rela.get_r_offset() != rela.get_r_offset())
10761 Address off = next_rela.get_r_addend();
10763 off = 8; // zero means next insn.
10764 if (off + rela.get_r_offset() + 4 > view_size)
10767 iview2 = reinterpret_cast<Insn*>(view + off);
10768 pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
10770 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
10772 if (xlate_pcrel_opt(&pinsn, &pinsn2))
10774 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10775 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10776 pinsn & 0xffffffff);
10777 elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
10782 case elfcpp::R_POWERPC_TPREL16_HA:
10783 if (parameters->options().tls_optimize() && value + 0x8000 < 0x10000)
10785 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10786 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10787 if ((insn & ((0x3f << 26) | 0x1f << 16))
10788 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
10792 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10798 case elfcpp::R_PPC64_TPREL16_LO_DS:
10800 // R_PPC_TLSGD, R_PPC_TLSLD
10803 case elfcpp::R_POWERPC_TPREL16_LO:
10804 if (parameters->options().tls_optimize() && value + 0x8000 < 0x10000)
10806 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10807 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10808 insn &= ~(0x1f << 16);
10809 insn |= (size == 32 ? 2 : 13) << 16;
10810 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10814 case elfcpp::R_PPC64_ENTRY:
10815 value = (target->got_section()->output_section()->address()
10816 + object->toc_base_offset());
10817 if (value + 0x80008000 <= 0xffffffff
10818 && !parameters->options().output_is_position_independent())
10820 Insn* iview = reinterpret_cast<Insn*>(view);
10821 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
10822 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
10824 if ((insn1 & ~0xfffc) == ld_2_12
10825 && insn2 == add_2_2_12)
10827 insn1 = lis_2 + ha(value);
10828 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
10829 insn2 = addi_2_2 + l(value);
10830 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
10837 if (value + 0x80008000 <= 0xffffffff)
10839 Insn* iview = reinterpret_cast<Insn*>(view);
10840 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
10841 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
10843 if ((insn1 & ~0xfffc) == ld_2_12
10844 && insn2 == add_2_2_12)
10846 insn1 = addis_2_12 + ha(value);
10847 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
10848 insn2 = addi_2_2 + l(value);
10849 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
10856 case elfcpp::R_POWERPC_REL16_LO:
10857 // If we are generating a non-PIC executable, edit
10858 // 0: addis 2,12,.TOC.-0b@ha
10859 // addi 2,2,.TOC.-0b@l
10860 // used by ELFv2 global entry points to set up r2, to
10862 // addi 2,2,.TOC.@l
10863 // if .TOC. is in range. */
10864 if (value + address - 4 + 0x80008000 <= 0xffffffff
10867 && target->abiversion() >= 2
10868 && !parameters->options().output_is_position_independent()
10869 && rela.get_r_addend() == d_offset + 4
10871 && strcmp(gsym->name(), ".TOC.") == 0)
10873 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10874 Reltype prev_rela(preloc - reloc_size);
10875 if ((prev_rela.get_r_info()
10876 == elfcpp::elf_r_info<size>(r_sym,
10877 elfcpp::R_POWERPC_REL16_HA))
10878 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
10879 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
10881 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10882 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
10883 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
10885 if ((insn1 & 0xffff0000) == addis_2_12
10886 && (insn2 & 0xffff0000) == addi_2_2)
10888 insn1 = lis_2 + ha(value + address - 4);
10889 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
10890 insn2 = addi_2_2 + l(value + address - 4);
10891 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
10894 relinfo->rr->set_strategy(relnum - 1,
10895 Relocatable_relocs::RELOC_SPECIAL);
10896 relinfo->rr->set_strategy(relnum,
10897 Relocatable_relocs::RELOC_SPECIAL);
10907 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
10908 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
10911 case elfcpp::R_POWERPC_ADDR32:
10912 case elfcpp::R_POWERPC_UADDR32:
10914 overflow = Reloc::CHECK_BITFIELD;
10917 case elfcpp::R_POWERPC_REL32:
10918 case elfcpp::R_POWERPC_REL16DX_HA:
10920 overflow = Reloc::CHECK_SIGNED;
10923 case elfcpp::R_POWERPC_UADDR16:
10924 overflow = Reloc::CHECK_BITFIELD;
10927 case elfcpp::R_POWERPC_ADDR16:
10928 // We really should have three separate relocations,
10929 // one for 16-bit data, one for insns with 16-bit signed fields,
10930 // and one for insns with 16-bit unsigned fields.
10931 overflow = Reloc::CHECK_BITFIELD;
10932 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
10933 overflow = Reloc::CHECK_LOW_INSN;
10936 case elfcpp::R_POWERPC_ADDR16_HI:
10937 case elfcpp::R_POWERPC_ADDR16_HA:
10938 case elfcpp::R_POWERPC_GOT16_HI:
10939 case elfcpp::R_POWERPC_GOT16_HA:
10940 case elfcpp::R_POWERPC_PLT16_HI:
10941 case elfcpp::R_POWERPC_PLT16_HA:
10942 case elfcpp::R_POWERPC_SECTOFF_HI:
10943 case elfcpp::R_POWERPC_SECTOFF_HA:
10944 case elfcpp::R_PPC64_TOC16_HI:
10945 case elfcpp::R_PPC64_TOC16_HA:
10946 case elfcpp::R_PPC64_PLTGOT16_HI:
10947 case elfcpp::R_PPC64_PLTGOT16_HA:
10948 case elfcpp::R_POWERPC_TPREL16_HI:
10949 case elfcpp::R_POWERPC_TPREL16_HA:
10950 case elfcpp::R_POWERPC_DTPREL16_HI:
10951 case elfcpp::R_POWERPC_DTPREL16_HA:
10952 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
10953 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
10954 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
10955 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
10956 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
10957 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
10958 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
10959 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
10960 case elfcpp::R_POWERPC_REL16_HI:
10961 case elfcpp::R_POWERPC_REL16_HA:
10963 overflow = Reloc::CHECK_HIGH_INSN;
10966 case elfcpp::R_POWERPC_REL16:
10967 case elfcpp::R_PPC64_TOC16:
10968 case elfcpp::R_POWERPC_GOT16:
10969 case elfcpp::R_POWERPC_SECTOFF:
10970 case elfcpp::R_POWERPC_TPREL16:
10971 case elfcpp::R_POWERPC_DTPREL16:
10972 case elfcpp::R_POWERPC_GOT_TLSGD16:
10973 case elfcpp::R_POWERPC_GOT_TLSLD16:
10974 case elfcpp::R_POWERPC_GOT_TPREL16:
10975 case elfcpp::R_POWERPC_GOT_DTPREL16:
10976 overflow = Reloc::CHECK_LOW_INSN;
10979 case elfcpp::R_PPC64_REL24_NOTOC:
10983 case elfcpp::R_POWERPC_ADDR24:
10984 case elfcpp::R_POWERPC_ADDR14:
10985 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
10986 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
10987 case elfcpp::R_PPC64_ADDR16_DS:
10988 case elfcpp::R_POWERPC_REL24:
10989 case elfcpp::R_PPC_PLTREL24:
10990 case elfcpp::R_PPC_LOCAL24PC:
10991 case elfcpp::R_PPC64_TPREL16_DS:
10992 case elfcpp::R_PPC64_DTPREL16_DS:
10993 case elfcpp::R_PPC64_TOC16_DS:
10994 case elfcpp::R_PPC64_GOT16_DS:
10995 case elfcpp::R_PPC64_SECTOFF_DS:
10996 case elfcpp::R_POWERPC_REL14:
10997 case elfcpp::R_POWERPC_REL14_BRTAKEN:
10998 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
10999 case elfcpp::R_PPC64_D34:
11000 case elfcpp::R_PPC64_PCREL34:
11001 case elfcpp::R_PPC64_GOT_PCREL34:
11002 case elfcpp::R_PPC64_PLT_PCREL34:
11003 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11004 case elfcpp::R_PPC64_D28:
11005 case elfcpp::R_PPC64_PCREL28:
11006 overflow = Reloc::CHECK_SIGNED;
11010 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11013 if (overflow == Reloc::CHECK_LOW_INSN
11014 || overflow == Reloc::CHECK_HIGH_INSN)
11016 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11018 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
11019 overflow = Reloc::CHECK_BITFIELD;
11020 else if (overflow == Reloc::CHECK_LOW_INSN
11021 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
11022 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
11023 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
11024 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
11025 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
11026 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
11027 overflow = Reloc::CHECK_UNSIGNED;
11029 overflow = Reloc::CHECK_SIGNED;
11032 bool maybe_dq_reloc = false;
11033 typename Powerpc_relocate_functions<size, big_endian>::Status status
11034 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
11037 case elfcpp::R_POWERPC_NONE:
11038 case elfcpp::R_POWERPC_TLS:
11039 case elfcpp::R_POWERPC_GNU_VTINHERIT:
11040 case elfcpp::R_POWERPC_GNU_VTENTRY:
11041 case elfcpp::R_POWERPC_PLTSEQ:
11042 case elfcpp::R_POWERPC_PLTCALL:
11043 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
11044 case elfcpp::R_PPC64_PLTCALL_NOTOC:
11045 case elfcpp::R_PPC64_PCREL_OPT:
11048 case elfcpp::R_PPC64_ADDR64:
11049 case elfcpp::R_PPC64_REL64:
11050 case elfcpp::R_PPC64_TOC:
11051 case elfcpp::R_PPC64_ADDR64_LOCAL:
11052 Reloc::addr64(view, value);
11055 case elfcpp::R_POWERPC_TPREL:
11056 case elfcpp::R_POWERPC_DTPREL:
11058 Reloc::addr64(view, value);
11060 status = Reloc::addr32(view, value, overflow);
11063 case elfcpp::R_PPC64_UADDR64:
11064 Reloc::addr64_u(view, value);
11067 case elfcpp::R_POWERPC_ADDR32:
11068 status = Reloc::addr32(view, value, overflow);
11071 case elfcpp::R_POWERPC_REL32:
11072 case elfcpp::R_POWERPC_UADDR32:
11073 status = Reloc::addr32_u(view, value, overflow);
11076 case elfcpp::R_PPC64_REL24_NOTOC:
11078 goto unsupp; // R_PPC_EMB_RELSDA
11080 case elfcpp::R_POWERPC_ADDR24:
11081 case elfcpp::R_POWERPC_REL24:
11082 case elfcpp::R_PPC_PLTREL24:
11083 case elfcpp::R_PPC_LOCAL24PC:
11084 status = Reloc::addr24(view, value, overflow);
11087 case elfcpp::R_POWERPC_GOT_DTPREL16:
11088 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11089 case elfcpp::R_POWERPC_GOT_TPREL16:
11090 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11093 // On ppc64 these are all ds form
11094 maybe_dq_reloc = true;
11098 case elfcpp::R_POWERPC_ADDR16:
11099 case elfcpp::R_POWERPC_REL16:
11100 case elfcpp::R_PPC64_TOC16:
11101 case elfcpp::R_POWERPC_GOT16:
11102 case elfcpp::R_POWERPC_SECTOFF:
11103 case elfcpp::R_POWERPC_TPREL16:
11104 case elfcpp::R_POWERPC_DTPREL16:
11105 case elfcpp::R_POWERPC_GOT_TLSGD16:
11106 case elfcpp::R_POWERPC_GOT_TLSLD16:
11107 case elfcpp::R_POWERPC_ADDR16_LO:
11108 case elfcpp::R_POWERPC_REL16_LO:
11109 case elfcpp::R_PPC64_TOC16_LO:
11110 case elfcpp::R_POWERPC_GOT16_LO:
11111 case elfcpp::R_POWERPC_PLT16_LO:
11112 case elfcpp::R_POWERPC_SECTOFF_LO:
11113 case elfcpp::R_POWERPC_TPREL16_LO:
11114 case elfcpp::R_POWERPC_DTPREL16_LO:
11115 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11116 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11118 status = Reloc::addr16(view, value, overflow);
11120 maybe_dq_reloc = true;
11123 case elfcpp::R_POWERPC_UADDR16:
11124 status = Reloc::addr16_u(view, value, overflow);
11127 case elfcpp::R_PPC64_ADDR16_HIGH:
11128 case elfcpp::R_PPC64_TPREL16_HIGH:
11129 case elfcpp::R_PPC64_DTPREL16_HIGH:
11131 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
11134 case elfcpp::R_POWERPC_ADDR16_HI:
11135 case elfcpp::R_POWERPC_REL16_HI:
11136 case elfcpp::R_PPC64_REL16_HIGH:
11137 case elfcpp::R_PPC64_TOC16_HI:
11138 case elfcpp::R_POWERPC_GOT16_HI:
11139 case elfcpp::R_POWERPC_PLT16_HI:
11140 case elfcpp::R_POWERPC_SECTOFF_HI:
11141 case elfcpp::R_POWERPC_TPREL16_HI:
11142 case elfcpp::R_POWERPC_DTPREL16_HI:
11143 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11144 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11145 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11146 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11147 Reloc::addr16_hi(view, value);
11150 case elfcpp::R_PPC64_ADDR16_HIGHA:
11151 case elfcpp::R_PPC64_TPREL16_HIGHA:
11152 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11154 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
11157 case elfcpp::R_POWERPC_ADDR16_HA:
11158 case elfcpp::R_POWERPC_REL16_HA:
11159 case elfcpp::R_PPC64_REL16_HIGHA:
11160 case elfcpp::R_PPC64_TOC16_HA:
11161 case elfcpp::R_POWERPC_GOT16_HA:
11162 case elfcpp::R_POWERPC_PLT16_HA:
11163 case elfcpp::R_POWERPC_SECTOFF_HA:
11164 case elfcpp::R_POWERPC_TPREL16_HA:
11165 case elfcpp::R_POWERPC_DTPREL16_HA:
11166 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11167 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11168 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11169 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11170 Reloc::addr16_ha(view, value);
11173 case elfcpp::R_POWERPC_REL16DX_HA:
11174 status = Reloc::addr16dx_ha(view, value, overflow);
11177 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11179 // R_PPC_EMB_NADDR16_LO
11182 case elfcpp::R_PPC64_ADDR16_HIGHER:
11183 case elfcpp::R_PPC64_REL16_HIGHER:
11184 case elfcpp::R_PPC64_TPREL16_HIGHER:
11185 Reloc::addr16_hi2(view, value);
11188 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11190 // R_PPC_EMB_NADDR16_HI
11193 case elfcpp::R_PPC64_ADDR16_HIGHERA:
11194 case elfcpp::R_PPC64_REL16_HIGHERA:
11195 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11196 Reloc::addr16_ha2(view, value);
11199 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11201 // R_PPC_EMB_NADDR16_HA
11204 case elfcpp::R_PPC64_ADDR16_HIGHEST:
11205 case elfcpp::R_PPC64_REL16_HIGHEST:
11206 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11207 Reloc::addr16_hi3(view, value);
11210 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11212 // R_PPC_EMB_SDAI16
11215 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
11216 case elfcpp::R_PPC64_REL16_HIGHESTA:
11217 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11218 Reloc::addr16_ha3(view, value);
11221 case elfcpp::R_PPC64_DTPREL16_DS:
11222 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11224 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
11227 case elfcpp::R_PPC64_TPREL16_DS:
11228 case elfcpp::R_PPC64_TPREL16_LO_DS:
11230 // R_PPC_TLSGD, R_PPC_TLSLD
11233 case elfcpp::R_PPC64_ADDR16_DS:
11234 case elfcpp::R_PPC64_ADDR16_LO_DS:
11235 case elfcpp::R_PPC64_TOC16_DS:
11236 case elfcpp::R_PPC64_TOC16_LO_DS:
11237 case elfcpp::R_PPC64_GOT16_DS:
11238 case elfcpp::R_PPC64_GOT16_LO_DS:
11239 case elfcpp::R_PPC64_PLT16_LO_DS:
11240 case elfcpp::R_PPC64_SECTOFF_DS:
11241 case elfcpp::R_PPC64_SECTOFF_LO_DS:
11242 maybe_dq_reloc = true;
11245 case elfcpp::R_POWERPC_ADDR14:
11246 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11247 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11248 case elfcpp::R_POWERPC_REL14:
11249 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11250 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11251 status = Reloc::addr14(view, value, overflow);
11254 case elfcpp::R_POWERPC_COPY:
11255 case elfcpp::R_POWERPC_GLOB_DAT:
11256 case elfcpp::R_POWERPC_JMP_SLOT:
11257 case elfcpp::R_POWERPC_RELATIVE:
11258 case elfcpp::R_POWERPC_DTPMOD:
11259 case elfcpp::R_PPC64_JMP_IREL:
11260 case elfcpp::R_POWERPC_IRELATIVE:
11261 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11262 _("unexpected reloc %u in object file"),
11266 case elfcpp::R_PPC64_TOCSAVE:
11272 Symbol_location loc;
11273 loc.object = relinfo->object;
11274 loc.shndx = relinfo->data_shndx;
11275 loc.offset = rela.get_r_offset();
11276 Tocsave_loc::const_iterator p = target->tocsave_loc().find(loc);
11277 if (p != target->tocsave_loc().end())
11279 // If we've generated plt calls using this tocsave, then
11280 // the nop needs to be changed to save r2.
11281 Insn* iview = reinterpret_cast<Insn*>(view);
11282 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
11283 elfcpp::Swap<32, big_endian>::
11284 writeval(iview, std_2_1 + target->stk_toc());
11289 case elfcpp::R_PPC_EMB_SDA2I16:
11290 case elfcpp::R_PPC_EMB_SDA2REL:
11293 // R_PPC64_TLSGD, R_PPC64_TLSLD
11296 case elfcpp::R_PPC64_D34:
11297 case elfcpp::R_PPC64_D34_LO:
11298 case elfcpp::R_PPC64_PCREL34:
11299 case elfcpp::R_PPC64_GOT_PCREL34:
11300 case elfcpp::R_PPC64_PLT_PCREL34:
11301 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11304 status = Reloc::addr34(view, value, overflow);
11307 case elfcpp::R_PPC64_D34_HI30:
11310 Reloc::addr34_hi(view, value);
11313 case elfcpp::R_PPC64_D34_HA30:
11316 Reloc::addr34_ha(view, value);
11319 case elfcpp::R_PPC64_D28:
11320 case elfcpp::R_PPC64_PCREL28:
11323 status = Reloc::addr28(view, value, overflow);
11326 case elfcpp::R_PPC64_ADDR16_HIGHER34:
11327 case elfcpp::R_PPC64_REL16_HIGHER34:
11330 Reloc::addr16_higher34(view, value);
11333 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
11334 case elfcpp::R_PPC64_REL16_HIGHERA34:
11337 Reloc::addr16_highera34(view, value);
11340 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
11341 case elfcpp::R_PPC64_REL16_HIGHEST34:
11344 Reloc::addr16_highest34(view, value);
11347 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
11348 case elfcpp::R_PPC64_REL16_HIGHESTA34:
11351 Reloc::addr16_highesta34(view, value);
11354 case elfcpp::R_POWERPC_PLT32:
11355 case elfcpp::R_POWERPC_PLTREL32:
11356 case elfcpp::R_PPC_SDAREL16:
11357 case elfcpp::R_POWERPC_ADDR30:
11358 case elfcpp::R_PPC64_PLT64:
11359 case elfcpp::R_PPC64_PLTREL64:
11360 case elfcpp::R_PPC64_PLTGOT16:
11361 case elfcpp::R_PPC64_PLTGOT16_LO:
11362 case elfcpp::R_PPC64_PLTGOT16_HI:
11363 case elfcpp::R_PPC64_PLTGOT16_HA:
11364 case elfcpp::R_PPC64_PLTGOT16_DS:
11365 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
11366 case elfcpp::R_PPC_TOC16:
11369 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11370 _("unsupported reloc %u"),
11375 if (maybe_dq_reloc)
11378 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11380 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
11381 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
11382 && (insn & 3) == 1))
11383 status = Reloc::addr16_dq(view, value, overflow);
11384 else if (size == 64
11385 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
11386 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
11387 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
11388 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
11389 status = Reloc::addr16_ds(view, value, overflow);
11391 status = Reloc::addr16(view, value, overflow);
11394 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
11397 && gsym->is_undefined()
11398 && is_branch_reloc<size>(r_type))))
11400 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11401 _("relocation overflow"));
11402 if (has_stub_value)
11403 gold_info(_("try relinking with a smaller --stub-group-size"));
11409 // Relocate section data.
11411 template<int size, bool big_endian>
11413 Target_powerpc<size, big_endian>::relocate_section(
11414 const Relocate_info<size, big_endian>* relinfo,
11415 unsigned int sh_type,
11416 const unsigned char* prelocs,
11417 size_t reloc_count,
11418 Output_section* output_section,
11419 bool needs_special_offset_handling,
11420 unsigned char* view,
11422 section_size_type view_size,
11423 const Reloc_symbol_changes* reloc_symbol_changes)
11425 typedef Target_powerpc<size, big_endian> Powerpc;
11426 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
11427 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
11428 Powerpc_comdat_behavior;
11429 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
11432 gold_assert(sh_type == elfcpp::SHT_RELA);
11434 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
11435 Powerpc_comdat_behavior, Classify_reloc>(
11441 needs_special_offset_handling,
11445 reloc_symbol_changes);
11448 template<int size, bool big_endian>
11449 class Powerpc_scan_relocatable_reloc
11452 typedef typename elfcpp::Rela<size, big_endian> Reltype;
11453 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11454 static const int sh_type = elfcpp::SHT_RELA;
11456 // Return the symbol referred to by the relocation.
11457 static inline unsigned int
11458 get_r_sym(const Reltype* reloc)
11459 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
11461 // Return the type of the relocation.
11462 static inline unsigned int
11463 get_r_type(const Reltype* reloc)
11464 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
11466 // Return the strategy to use for a local symbol which is not a
11467 // section symbol, given the relocation type.
11468 inline Relocatable_relocs::Reloc_strategy
11469 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
11471 if (r_type == 0 && r_sym == 0)
11472 return Relocatable_relocs::RELOC_DISCARD;
11473 return Relocatable_relocs::RELOC_COPY;
11476 // Return the strategy to use for a local symbol which is a section
11477 // symbol, given the relocation type.
11478 inline Relocatable_relocs::Reloc_strategy
11479 local_section_strategy(unsigned int, Relobj*)
11481 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
11484 // Return the strategy to use for a global symbol, given the
11485 // relocation type, the object, and the symbol index.
11486 inline Relocatable_relocs::Reloc_strategy
11487 global_strategy(unsigned int r_type, Relobj*, unsigned int)
11490 && (r_type == elfcpp::R_PPC_PLTREL24
11491 || r_type == elfcpp::R_POWERPC_PLT16_LO
11492 || r_type == elfcpp::R_POWERPC_PLT16_HI
11493 || r_type == elfcpp::R_POWERPC_PLT16_HA))
11494 return Relocatable_relocs::RELOC_SPECIAL;
11495 return Relocatable_relocs::RELOC_COPY;
11499 // Scan the relocs during a relocatable link.
11501 template<int size, bool big_endian>
11503 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
11504 Symbol_table* symtab,
11506 Sized_relobj_file<size, big_endian>* object,
11507 unsigned int data_shndx,
11508 unsigned int sh_type,
11509 const unsigned char* prelocs,
11510 size_t reloc_count,
11511 Output_section* output_section,
11512 bool needs_special_offset_handling,
11513 size_t local_symbol_count,
11514 const unsigned char* plocal_symbols,
11515 Relocatable_relocs* rr)
11517 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
11519 gold_assert(sh_type == elfcpp::SHT_RELA);
11521 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
11529 needs_special_offset_handling,
11530 local_symbol_count,
11535 // Scan the relocs for --emit-relocs.
11537 template<int size, bool big_endian>
11539 Target_powerpc<size, big_endian>::emit_relocs_scan(
11540 Symbol_table* symtab,
11542 Sized_relobj_file<size, big_endian>* object,
11543 unsigned int data_shndx,
11544 unsigned int sh_type,
11545 const unsigned char* prelocs,
11546 size_t reloc_count,
11547 Output_section* output_section,
11548 bool needs_special_offset_handling,
11549 size_t local_symbol_count,
11550 const unsigned char* plocal_syms,
11551 Relocatable_relocs* rr)
11553 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
11555 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
11556 Emit_relocs_strategy;
11558 gold_assert(sh_type == elfcpp::SHT_RELA);
11560 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
11568 needs_special_offset_handling,
11569 local_symbol_count,
11574 // Emit relocations for a section.
11575 // This is a modified version of the function by the same name in
11576 // target-reloc.h. Using relocate_special_relocatable for
11577 // R_PPC_PLTREL24 would require duplication of the entire body of the
11578 // loop, so we may as well duplicate the whole thing.
11580 template<int size, bool big_endian>
11582 Target_powerpc<size, big_endian>::relocate_relocs(
11583 const Relocate_info<size, big_endian>* relinfo,
11584 unsigned int sh_type,
11585 const unsigned char* prelocs,
11586 size_t reloc_count,
11587 Output_section* output_section,
11588 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
11590 Address view_address,
11592 unsigned char* reloc_view,
11593 section_size_type reloc_view_size)
11595 gold_assert(sh_type == elfcpp::SHT_RELA);
11597 typedef typename elfcpp::Rela<size, big_endian> Reltype;
11598 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
11599 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11600 // Offset from start of insn to d-field reloc.
11601 const int d_offset = big_endian ? 2 : 0;
11603 Powerpc_relobj<size, big_endian>* const object
11604 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
11605 const unsigned int local_count = object->local_symbol_count();
11606 unsigned int got2_shndx = object->got2_shndx();
11607 Address got2_addend = 0;
11608 if (got2_shndx != 0)
11610 got2_addend = object->get_output_section_offset(got2_shndx);
11611 gold_assert(got2_addend != invalid_address);
11614 const bool relocatable = parameters->options().relocatable();
11616 unsigned char* pwrite = reloc_view;
11617 bool zap_next = false;
11618 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
11620 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
11621 if (strategy == Relocatable_relocs::RELOC_DISCARD)
11624 Reltype reloc(prelocs);
11625 Reltype_write reloc_write(pwrite);
11627 Address offset = reloc.get_r_offset();
11628 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
11629 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
11630 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
11631 const unsigned int orig_r_sym = r_sym;
11632 typename elfcpp::Elf_types<size>::Elf_Swxword addend
11633 = reloc.get_r_addend();
11634 const Symbol* gsym = NULL;
11638 // We could arrange to discard these and other relocs for
11639 // tls optimised sequences in the strategy methods, but for
11640 // now do as BFD ld does.
11641 r_type = elfcpp::R_POWERPC_NONE;
11645 // Get the new symbol index.
11646 Output_section* os = NULL;
11647 if (r_sym < local_count)
11651 case Relocatable_relocs::RELOC_COPY:
11652 case Relocatable_relocs::RELOC_SPECIAL:
11655 r_sym = object->symtab_index(r_sym);
11656 gold_assert(r_sym != -1U);
11660 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
11662 // We are adjusting a section symbol. We need to find
11663 // the symbol table index of the section symbol for
11664 // the output section corresponding to input section
11665 // in which this symbol is defined.
11666 gold_assert(r_sym < local_count);
11668 unsigned int shndx =
11669 object->local_symbol_input_shndx(r_sym, &is_ordinary);
11670 gold_assert(is_ordinary);
11671 os = object->output_section(shndx);
11672 gold_assert(os != NULL);
11673 gold_assert(os->needs_symtab_index());
11674 r_sym = os->symtab_index();
11679 gold_unreachable();
11684 gsym = object->global_symbol(r_sym);
11685 gold_assert(gsym != NULL);
11686 if (gsym->is_forwarder())
11687 gsym = relinfo->symtab->resolve_forwards(gsym);
11689 gold_assert(gsym->has_symtab_index());
11690 r_sym = gsym->symtab_index();
11693 // Get the new offset--the location in the output section where
11694 // this relocation should be applied.
11695 if (static_cast<Address>(offset_in_output_section) != invalid_address)
11696 offset += offset_in_output_section;
11699 section_offset_type sot_offset =
11700 convert_types<section_offset_type, Address>(offset);
11701 section_offset_type new_sot_offset =
11702 output_section->output_offset(object, relinfo->data_shndx,
11704 gold_assert(new_sot_offset != -1);
11705 offset = new_sot_offset;
11708 // In an object file, r_offset is an offset within the section.
11709 // In an executable or dynamic object, generated by
11710 // --emit-relocs, r_offset is an absolute address.
11713 offset += view_address;
11714 if (static_cast<Address>(offset_in_output_section) != invalid_address)
11715 offset -= offset_in_output_section;
11718 // Handle the reloc addend based on the strategy.
11719 if (strategy == Relocatable_relocs::RELOC_COPY)
11721 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
11723 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
11724 addend = psymval->value(object, addend);
11725 // In a relocatable link, the symbol value is relative to
11726 // the start of the output section. For a non-relocatable
11727 // link, we need to adjust the addend.
11730 gold_assert(os != NULL);
11731 addend -= os->address();
11734 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
11738 if (addend >= 32768)
11739 addend += got2_addend;
11741 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
11743 r_type = elfcpp::R_POWERPC_ADDR16_HA;
11744 addend -= d_offset;
11746 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
11748 r_type = elfcpp::R_POWERPC_ADDR16_LO;
11749 addend -= d_offset + 4;
11753 gold_unreachable();
11757 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11758 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
11759 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
11760 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
11762 // First instruction of a global dynamic sequence,
11764 const bool final = gsym == NULL || gsym->final_value_is_known();
11765 switch (this->optimize_tls_gd(final))
11767 case tls::TLSOPT_TO_IE:
11768 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
11769 - elfcpp::R_POWERPC_GOT_TLSGD16);
11771 case tls::TLSOPT_TO_LE:
11772 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11773 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
11774 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11777 r_type = elfcpp::R_POWERPC_NONE;
11778 offset -= d_offset;
11785 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11786 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
11787 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
11788 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
11790 // First instruction of a local dynamic sequence,
11792 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
11794 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11795 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
11797 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11798 const Output_section* os = relinfo->layout->tls_segment()
11800 gold_assert(os != NULL);
11801 gold_assert(os->needs_symtab_index());
11802 r_sym = os->symtab_index();
11803 addend = dtp_offset;
11807 r_type = elfcpp::R_POWERPC_NONE;
11808 offset -= d_offset;
11812 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11813 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
11814 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
11815 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
11817 // First instruction of initial exec sequence.
11818 const bool final = gsym == NULL || gsym->final_value_is_known();
11819 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
11821 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11822 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
11823 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11826 r_type = elfcpp::R_POWERPC_NONE;
11827 offset -= d_offset;
11831 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
11832 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
11834 // Second instruction of a global dynamic sequence,
11835 // the __tls_get_addr call
11836 const bool final = gsym == NULL || gsym->final_value_is_known();
11837 switch (this->optimize_tls_gd(final))
11839 case tls::TLSOPT_TO_IE:
11840 r_type = elfcpp::R_POWERPC_NONE;
11843 case tls::TLSOPT_TO_LE:
11844 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11845 offset += d_offset;
11852 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
11853 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
11855 // Second instruction of a local dynamic sequence,
11856 // the __tls_get_addr call
11857 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
11859 const Output_section* os = relinfo->layout->tls_segment()
11861 gold_assert(os != NULL);
11862 gold_assert(os->needs_symtab_index());
11863 r_sym = os->symtab_index();
11864 addend = dtp_offset;
11865 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11866 offset += d_offset;
11870 else if (r_type == elfcpp::R_POWERPC_TLS)
11872 // Second instruction of an initial exec sequence
11873 const bool final = gsym == NULL || gsym->final_value_is_known();
11874 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
11876 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11877 offset += d_offset;
11882 reloc_write.put_r_offset(offset);
11883 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
11884 reloc_write.put_r_addend(addend);
11886 pwrite += reloc_size;
11889 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
11890 == reloc_view_size);
11893 // Return the value to use for a dynamic symbol which requires special
11894 // treatment. This is how we support equality comparisons of function
11895 // pointers across shared library boundaries, as described in the
11896 // processor specific ABI supplement.
11898 template<int size, bool big_endian>
11900 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
11904 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
11905 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
11906 p != this->stub_tables_.end();
11909 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
11910 = (*p)->find_plt_call_entry(gsym);
11912 return (*p)->stub_address() + ent->off_;
11915 else if (this->abiversion() >= 2)
11917 Address off = this->glink_section()->find_global_entry(gsym);
11918 if (off != invalid_address)
11919 return this->glink_section()->global_entry_address() + off;
11921 gold_unreachable();
11924 // Return the PLT address to use for a local symbol.
11925 template<int size, bool big_endian>
11927 Target_powerpc<size, big_endian>::do_plt_address_for_local(
11928 const Relobj* object,
11929 unsigned int symndx) const
11933 const Sized_relobj<size, big_endian>* relobj
11934 = static_cast<const Sized_relobj<size, big_endian>*>(object);
11935 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
11936 p != this->stub_tables_.end();
11939 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
11940 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
11942 return (*p)->stub_address() + ent->off_;
11945 gold_unreachable();
11948 // Return the PLT address to use for a global symbol.
11949 template<int size, bool big_endian>
11951 Target_powerpc<size, big_endian>::do_plt_address_for_global(
11952 const Symbol* gsym) const
11956 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
11957 p != this->stub_tables_.end();
11960 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
11961 = (*p)->find_plt_call_entry(gsym);
11963 return (*p)->stub_address() + ent->off_;
11966 else if (this->abiversion() >= 2)
11968 Address off = this->glink_section()->find_global_entry(gsym);
11969 if (off != invalid_address)
11970 return this->glink_section()->global_entry_address() + off;
11972 gold_unreachable();
11975 // Return the offset to use for the GOT_INDX'th got entry which is
11976 // for a local tls symbol specified by OBJECT, SYMNDX.
11977 template<int size, bool big_endian>
11979 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
11980 const Relobj* object,
11981 unsigned int symndx,
11982 unsigned int got_indx) const
11984 const Powerpc_relobj<size, big_endian>* ppc_object
11985 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
11986 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
11988 for (Got_type got_type = GOT_TYPE_TLSGD;
11989 got_type <= GOT_TYPE_TPREL;
11990 got_type = Got_type(got_type + 1))
11991 if (ppc_object->local_has_got_offset(symndx, got_type))
11993 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
11994 if (got_type == GOT_TYPE_TLSGD)
11996 if (off == got_indx * (size / 8))
11998 if (got_type == GOT_TYPE_TPREL)
12001 return -dtp_offset;
12005 gold_unreachable();
12008 // Return the offset to use for the GOT_INDX'th got entry which is
12009 // for global tls symbol GSYM.
12010 template<int size, bool big_endian>
12012 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
12014 unsigned int got_indx) const
12016 if (gsym->type() == elfcpp::STT_TLS)
12018 for (Got_type got_type = GOT_TYPE_TLSGD;
12019 got_type <= GOT_TYPE_TPREL;
12020 got_type = Got_type(got_type + 1))
12021 if (gsym->has_got_offset(got_type))
12023 unsigned int off = gsym->got_offset(got_type);
12024 if (got_type == GOT_TYPE_TLSGD)
12026 if (off == got_indx * (size / 8))
12028 if (got_type == GOT_TYPE_TPREL)
12031 return -dtp_offset;
12035 gold_unreachable();
12038 // The selector for powerpc object files.
12040 template<int size, bool big_endian>
12041 class Target_selector_powerpc : public Target_selector
12044 Target_selector_powerpc()
12045 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
12048 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
12049 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
12051 ? (big_endian ? "elf64ppc" : "elf64lppc")
12052 : (big_endian ? "elf32ppc" : "elf32lppc")))
12056 do_instantiate_target()
12057 { return new Target_powerpc<size, big_endian>(); }
12060 Target_selector_powerpc<32, true> target_selector_ppc32;
12061 Target_selector_powerpc<32, false> target_selector_ppc32le;
12062 Target_selector_powerpc<64, true> target_selector_ppc64;
12063 Target_selector_powerpc<64, false> target_selector_ppc64le;
12065 // Instantiate these constants for -O0
12066 template<int size, bool big_endian>
12067 const typename Output_data_glink<size, big_endian>::Address
12068 Output_data_glink<size, big_endian>::invalid_address;
12069 template<int size, bool big_endian>
12070 const typename Stub_table<size, big_endian>::Address
12071 Stub_table<size, big_endian>::invalid_address;
12072 template<int size, bool big_endian>
12073 const typename Target_powerpc<size, big_endian>::Address
12074 Target_powerpc<size, big_endian>::invalid_address;
12076 } // End anonymous namespace.