1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright (C) 2008-2017 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
30 #include "parameters.h"
37 #include "copy-relocs.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
50 template<int size, bool big_endian>
51 class Output_data_plt_powerpc;
53 template<int size, bool big_endian>
54 class Output_data_brlt_powerpc;
56 template<int size, bool big_endian>
57 class Output_data_got_powerpc;
59 template<int size, bool big_endian>
60 class Output_data_glink;
62 template<int size, bool big_endian>
65 template<int size, bool big_endian>
66 class Output_data_save_res;
68 template<int size, bool big_endian>
71 struct Stub_table_owner
74 : output_section(NULL), owner(NULL)
77 Output_section* output_section;
78 const Output_section::Input_section* owner;
82 is_branch_reloc(unsigned int r_type);
84 // Counter incremented on every Powerpc_relobj constructed.
85 static uint32_t object_id = 0;
87 template<int size, bool big_endian>
88 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
91 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
92 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
93 typedef Unordered_map<Address, Section_refs> Access_from;
95 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
96 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
97 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
98 uniq_(object_id++), special_(0), relatoc_(0), toc_(0),
99 has_small_toc_reloc_(false), opd_valid_(false),
100 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
101 access_from_map_(), has14_(), stub_table_index_(), st_other_()
103 this->set_abiversion(0);
109 // Read the symbols then set up st_other vector.
111 do_read_symbols(Read_symbols_data*);
113 // Arrange to always relocate .toc first.
115 do_relocate_sections(
116 const Symbol_table* symtab, const Layout* layout,
117 const unsigned char* pshdrs, Output_file* of,
118 typename Sized_relobj_file<size, big_endian>::Views* pviews);
120 // The .toc section index.
127 // Mark .toc entry at OFF as not optimizable.
129 set_no_toc_opt(Address off)
131 if (this->no_toc_opt_.empty())
132 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
135 if (off < this->no_toc_opt_.size())
136 this->no_toc_opt_[off] = true;
139 // Mark the entire .toc as not optimizable.
143 this->no_toc_opt_.resize(1);
144 this->no_toc_opt_[0] = true;
147 // Return true if code using the .toc entry at OFF should not be edited.
149 no_toc_opt(Address off) const
151 if (this->no_toc_opt_.empty())
154 if (off >= this->no_toc_opt_.size())
156 return this->no_toc_opt_[off];
159 // The .got2 section shndx.
164 return this->special_;
169 // The .opd section shndx.
176 return this->special_;
179 // Init OPD entry arrays.
181 init_opd(size_t opd_size)
183 size_t count = this->opd_ent_ndx(opd_size);
184 this->opd_ent_.resize(count);
187 // Return section and offset of function entry for .opd + R_OFF.
189 get_opd_ent(Address r_off, Address* value = NULL) const
191 size_t ndx = this->opd_ent_ndx(r_off);
192 gold_assert(ndx < this->opd_ent_.size());
193 gold_assert(this->opd_ent_[ndx].shndx != 0);
195 *value = this->opd_ent_[ndx].off;
196 return this->opd_ent_[ndx].shndx;
199 // Set section and offset of function entry for .opd + R_OFF.
201 set_opd_ent(Address r_off, unsigned int shndx, Address value)
203 size_t ndx = this->opd_ent_ndx(r_off);
204 gold_assert(ndx < this->opd_ent_.size());
205 this->opd_ent_[ndx].shndx = shndx;
206 this->opd_ent_[ndx].off = value;
209 // Return discard flag for .opd + R_OFF.
211 get_opd_discard(Address r_off) const
213 size_t ndx = this->opd_ent_ndx(r_off);
214 gold_assert(ndx < this->opd_ent_.size());
215 return this->opd_ent_[ndx].discard;
218 // Set discard flag for .opd + R_OFF.
220 set_opd_discard(Address r_off)
222 size_t ndx = this->opd_ent_ndx(r_off);
223 gold_assert(ndx < this->opd_ent_.size());
224 this->opd_ent_[ndx].discard = true;
229 { return this->opd_valid_; }
233 { this->opd_valid_ = true; }
235 // Examine .rela.opd to build info about function entry points.
237 scan_opd_relocs(size_t reloc_count,
238 const unsigned char* prelocs,
239 const unsigned char* plocal_syms);
241 // Returns true if a code sequence loading a TOC entry can be
242 // converted into code calculating a TOC pointer relative offset.
244 make_toc_relative(Target_powerpc<size, big_endian>* target,
247 // Perform the Sized_relobj_file method, then set up opd info from
250 do_read_relocs(Read_relocs_data*);
253 do_find_special_sections(Read_symbols_data* sd);
255 // Adjust this local symbol value. Return false if the symbol
256 // should be discarded from the output file.
258 do_adjust_local_symbol(Symbol_value<size>* lv) const
260 if (size == 64 && this->opd_shndx() != 0)
263 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
265 if (this->get_opd_discard(lv->input_value()))
273 { return &this->access_from_map_; }
275 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
276 // section at DST_OFF.
278 add_reference(Relobj* src_obj,
279 unsigned int src_indx,
280 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
282 Section_id src_id(src_obj, src_indx);
283 this->access_from_map_[dst_off].insert(src_id);
286 // Add a reference to the code section specified by the .opd entry
289 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
291 size_t ndx = this->opd_ent_ndx(dst_off);
292 if (ndx >= this->opd_ent_.size())
293 this->opd_ent_.resize(ndx + 1);
294 this->opd_ent_[ndx].gc_mark = true;
298 process_gc_mark(Symbol_table* symtab)
300 for (size_t i = 0; i < this->opd_ent_.size(); i++)
301 if (this->opd_ent_[i].gc_mark)
303 unsigned int shndx = this->opd_ent_[i].shndx;
304 symtab->gc()->worklist().push_back(Section_id(this, shndx));
308 // Return offset in output GOT section that this object will use
309 // as a TOC pointer. Won't be just a constant with multi-toc support.
311 toc_base_offset() const
315 set_has_small_toc_reloc()
316 { has_small_toc_reloc_ = true; }
319 has_small_toc_reloc() const
320 { return has_small_toc_reloc_; }
323 set_has_14bit_branch(unsigned int shndx)
325 if (shndx >= this->has14_.size())
326 this->has14_.resize(shndx + 1);
327 this->has14_[shndx] = true;
331 has_14bit_branch(unsigned int shndx) const
332 { return shndx < this->has14_.size() && this->has14_[shndx]; }
335 set_stub_table(unsigned int shndx, unsigned int stub_index)
337 if (shndx >= this->stub_table_index_.size())
338 this->stub_table_index_.resize(shndx + 1, -1);
339 this->stub_table_index_[shndx] = stub_index;
342 Stub_table<size, big_endian>*
343 stub_table(unsigned int shndx)
345 if (shndx < this->stub_table_index_.size())
347 Target_powerpc<size, big_endian>* target
348 = static_cast<Target_powerpc<size, big_endian>*>(
349 parameters->sized_target<size, big_endian>());
350 unsigned int indx = this->stub_table_index_[shndx];
351 if (indx < target->stub_tables().size())
352 return target->stub_tables()[indx];
360 this->stub_table_index_.clear();
365 { return this->uniq_; }
369 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
371 // Set ABI version for input and output
373 set_abiversion(int ver);
376 st_other (unsigned int symndx) const
378 return this->st_other_[symndx];
382 ppc64_local_entry_offset(const Symbol* sym) const
383 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
386 ppc64_local_entry_offset(unsigned int symndx) const
387 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
398 // Return index into opd_ent_ array for .opd entry at OFF.
399 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
400 // apart when the language doesn't use the last 8-byte word, the
401 // environment pointer. Thus dividing the entry section offset by
402 // 16 will give an index into opd_ent_ that works for either layout
403 // of .opd. (It leaves some elements of the vector unused when .opd
404 // entries are spaced 24 bytes apart, but we don't know the spacing
405 // until relocations are processed, and in any case it is possible
406 // for an object to have some entries spaced 16 bytes apart and
407 // others 24 bytes apart.)
409 opd_ent_ndx(size_t off) const
412 // Per object unique identifier
415 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
416 unsigned int special_;
418 // For 64-bit the .rela.toc and .toc section shdnx.
419 unsigned int relatoc_;
422 // For 64-bit, whether this object uses small model relocs to access
424 bool has_small_toc_reloc_;
426 // Set at the start of gc_process_relocs, when we know opd_ent_
427 // vector is valid. The flag could be made atomic and set in
428 // do_read_relocs with memory_order_release and then tested with
429 // memory_order_acquire, potentially resulting in fewer entries in
434 elfcpp::Elf_Word e_flags_;
436 // For 64-bit, an array with one entry per 64-bit word in the .toc
437 // section, set if accesses using that word cannot be optimised.
438 std::vector<bool> no_toc_opt_;
440 // The first 8-byte word of an OPD entry gives the address of the
441 // entry point of the function. Relocatable object files have a
442 // relocation on this word. The following vector records the
443 // section and offset specified by these relocations.
444 std::vector<Opd_ent> opd_ent_;
446 // References made to this object's .opd section when running
447 // gc_process_relocs for another object, before the opd_ent_ vector
448 // is valid for this object.
449 Access_from access_from_map_;
451 // Whether input section has a 14-bit branch reloc.
452 std::vector<bool> has14_;
454 // The stub table to use for a given input section.
455 std::vector<unsigned int> stub_table_index_;
457 // ELF st_other field for local symbols.
458 std::vector<unsigned char> st_other_;
461 template<int size, bool big_endian>
462 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
465 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
467 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
468 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
469 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
470 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_()
472 this->set_abiversion(0);
478 // Call Sized_dynobj::do_read_symbols to read the symbols then
479 // read .opd from a dynamic object, filling in opd_ent_ vector,
481 do_read_symbols(Read_symbols_data*);
483 // The .opd section shndx.
487 return this->opd_shndx_;
490 // The .opd section address.
494 return this->opd_address_;
497 // Init OPD entry arrays.
499 init_opd(size_t opd_size)
501 size_t count = this->opd_ent_ndx(opd_size);
502 this->opd_ent_.resize(count);
505 // Return section and offset of function entry for .opd + R_OFF.
507 get_opd_ent(Address r_off, Address* value = NULL) const
509 size_t ndx = this->opd_ent_ndx(r_off);
510 gold_assert(ndx < this->opd_ent_.size());
511 gold_assert(this->opd_ent_[ndx].shndx != 0);
513 *value = this->opd_ent_[ndx].off;
514 return this->opd_ent_[ndx].shndx;
517 // Set section and offset of function entry for .opd + R_OFF.
519 set_opd_ent(Address r_off, unsigned int shndx, Address value)
521 size_t ndx = this->opd_ent_ndx(r_off);
522 gold_assert(ndx < this->opd_ent_.size());
523 this->opd_ent_[ndx].shndx = shndx;
524 this->opd_ent_[ndx].off = value;
529 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
531 // Set ABI version for input and output.
533 set_abiversion(int ver);
536 // Used to specify extent of executable sections.
539 Sec_info(Address start_, Address len_, unsigned int shndx_)
540 : start(start_), len(len_), shndx(shndx_)
544 operator<(const Sec_info& that) const
545 { return this->start < that.start; }
558 // Return index into opd_ent_ array for .opd entry at OFF.
560 opd_ent_ndx(size_t off) const
563 // For 64-bit the .opd section shndx and address.
564 unsigned int opd_shndx_;
565 Address opd_address_;
568 elfcpp::Elf_Word e_flags_;
570 // The first 8-byte word of an OPD entry gives the address of the
571 // entry point of the function. Records the section and offset
572 // corresponding to the address. Note that in dynamic objects,
573 // offset is *not* relative to the section.
574 std::vector<Opd_ent> opd_ent_;
577 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
578 // base class will emit.
580 template<int sh_type, int size, bool big_endian>
581 class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
584 Powerpc_copy_relocs()
585 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
588 // Emit any saved relocations which turn out to be needed. This is
589 // called after all the relocs have been scanned.
591 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
594 template<int size, bool big_endian>
595 class Target_powerpc : public Sized_target<size, big_endian>
599 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
600 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
601 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
602 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
603 static const Address invalid_address = static_cast<Address>(0) - 1;
604 // Offset of tp and dtp pointers from start of TLS block.
605 static const Address tp_offset = 0x7000;
606 static const Address dtp_offset = 0x8000;
609 : Sized_target<size, big_endian>(&powerpc_info),
610 got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL),
611 glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
612 tlsld_got_offset_(-1U),
613 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
614 plt_thread_safe_(false), plt_localentry0_(false),
615 plt_localentry0_init_(false), has_localentry0_(false),
616 relax_failed_(false), relax_fail_count_(0),
617 stub_group_size_(0), savres_section_(0)
621 // Process the relocations to determine unreferenced sections for
622 // garbage collection.
624 gc_process_relocs(Symbol_table* symtab,
626 Sized_relobj_file<size, big_endian>* object,
627 unsigned int data_shndx,
628 unsigned int sh_type,
629 const unsigned char* prelocs,
631 Output_section* output_section,
632 bool needs_special_offset_handling,
633 size_t local_symbol_count,
634 const unsigned char* plocal_symbols);
636 // Scan the relocations to look for symbol adjustments.
638 scan_relocs(Symbol_table* symtab,
640 Sized_relobj_file<size, big_endian>* object,
641 unsigned int data_shndx,
642 unsigned int sh_type,
643 const unsigned char* prelocs,
645 Output_section* output_section,
646 bool needs_special_offset_handling,
647 size_t local_symbol_count,
648 const unsigned char* plocal_symbols);
650 // Map input .toc section to output .got section.
652 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
654 if (size == 64 && strcmp(name, ".toc") == 0)
662 // Provide linker defined save/restore functions.
664 define_save_restore_funcs(Layout*, Symbol_table*);
666 // No stubs unless a final link.
669 { return !parameters->options().relocatable(); }
672 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
675 do_plt_fde_location(const Output_data*, unsigned char*,
676 uint64_t*, off_t*) const;
678 // Stash info about branches, for stub generation.
680 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
681 unsigned int data_shndx, Address r_offset,
682 unsigned int r_type, unsigned int r_sym, Address addend)
684 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
685 this->branch_info_.push_back(info);
686 if (r_type == elfcpp::R_POWERPC_REL14
687 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
688 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
689 ppc_object->set_has_14bit_branch(data_shndx);
692 // Return whether the last branch is a plt call, and if so, mark the
693 // branch as having an R_PPC64_TOCSAVE.
695 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
696 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
699 && !this->branch_info_.empty()
700 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
701 r_offset, this, symtab));
704 // Say the given location, that of a nop in a function prologue with
705 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
706 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
708 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
709 unsigned int shndx, Address offset)
712 loc.object = ppc_object;
715 this->tocsave_loc_.insert(loc);
722 return this->tocsave_loc_;
726 do_define_standard_symbols(Symbol_table*, Layout*);
728 // Finalize the sections.
730 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
732 // Return the value to use for a dynamic which requires special
735 do_dynsym_value(const Symbol*) const;
737 // Return the PLT address to use for a local symbol.
739 do_plt_address_for_local(const Relobj*, unsigned int) const;
741 // Return the PLT address to use for a global symbol.
743 do_plt_address_for_global(const Symbol*) const;
745 // Return the offset to use for the GOT_INDX'th got entry which is
746 // for a local tls symbol specified by OBJECT, SYMNDX.
748 do_tls_offset_for_local(const Relobj* object,
750 unsigned int got_indx) const;
752 // Return the offset to use for the GOT_INDX'th got entry which is
753 // for global tls symbol GSYM.
755 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
758 do_function_location(Symbol_location*) const;
761 do_can_check_for_function_pointers() const
764 // Adjust -fsplit-stack code which calls non-split-stack code.
766 do_calls_non_split(Relobj* object, unsigned int shndx,
767 section_offset_type fnoffset, section_size_type fnsize,
768 const unsigned char* prelocs, size_t reloc_count,
769 unsigned char* view, section_size_type view_size,
770 std::string* from, std::string* to) const;
772 // Relocate a section.
774 relocate_section(const Relocate_info<size, big_endian>*,
775 unsigned int sh_type,
776 const unsigned char* prelocs,
778 Output_section* output_section,
779 bool needs_special_offset_handling,
781 Address view_address,
782 section_size_type view_size,
783 const Reloc_symbol_changes*);
785 // Scan the relocs during a relocatable link.
787 scan_relocatable_relocs(Symbol_table* symtab,
789 Sized_relobj_file<size, big_endian>* object,
790 unsigned int data_shndx,
791 unsigned int sh_type,
792 const unsigned char* prelocs,
794 Output_section* output_section,
795 bool needs_special_offset_handling,
796 size_t local_symbol_count,
797 const unsigned char* plocal_symbols,
798 Relocatable_relocs*);
800 // Scan the relocs for --emit-relocs.
802 emit_relocs_scan(Symbol_table* symtab,
804 Sized_relobj_file<size, big_endian>* object,
805 unsigned int data_shndx,
806 unsigned int sh_type,
807 const unsigned char* prelocs,
809 Output_section* output_section,
810 bool needs_special_offset_handling,
811 size_t local_symbol_count,
812 const unsigned char* plocal_syms,
813 Relocatable_relocs* rr);
815 // Emit relocations for a section.
817 relocate_relocs(const Relocate_info<size, big_endian>*,
818 unsigned int sh_type,
819 const unsigned char* prelocs,
821 Output_section* output_section,
822 typename elfcpp::Elf_types<size>::Elf_Off
823 offset_in_output_section,
825 Address view_address,
827 unsigned char* reloc_view,
828 section_size_type reloc_view_size);
830 // Return whether SYM is defined by the ABI.
832 do_is_defined_by_abi(const Symbol* sym) const
834 return strcmp(sym->name(), "__tls_get_addr") == 0;
837 // Return the size of the GOT section.
841 gold_assert(this->got_ != NULL);
842 return this->got_->data_size();
845 // Get the PLT section.
846 const Output_data_plt_powerpc<size, big_endian>*
849 gold_assert(this->plt_ != NULL);
853 // Get the IPLT section.
854 const Output_data_plt_powerpc<size, big_endian>*
857 gold_assert(this->iplt_ != NULL);
861 // Get the .glink section.
862 const Output_data_glink<size, big_endian>*
863 glink_section() const
865 gold_assert(this->glink_ != NULL);
869 Output_data_glink<size, big_endian>*
872 gold_assert(this->glink_ != NULL);
876 bool has_glink() const
877 { return this->glink_ != NULL; }
879 // Get the GOT section.
880 const Output_data_got_powerpc<size, big_endian>*
883 gold_assert(this->got_ != NULL);
887 // Get the GOT section, creating it if necessary.
888 Output_data_got_powerpc<size, big_endian>*
889 got_section(Symbol_table*, Layout*);
892 do_make_elf_object(const std::string&, Input_file*, off_t,
893 const elfcpp::Ehdr<size, big_endian>&);
895 // Return the number of entries in the GOT.
897 got_entry_count() const
899 if (this->got_ == NULL)
901 return this->got_size() / (size / 8);
904 // Return the number of entries in the PLT.
906 plt_entry_count() const;
908 // Return the offset of the first non-reserved PLT entry.
910 first_plt_entry_offset() const
914 if (this->abiversion() >= 2)
919 // Return the size of each PLT entry.
921 plt_entry_size() const
925 if (this->abiversion() >= 2)
930 Output_data_save_res<size, big_endian>*
931 savres_section() const
933 return this->savres_section_;
936 // Add any special sections for this symbol to the gc work list.
937 // For powerpc64, this adds the code section of a function
940 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
942 // Handle target specific gc actions when adding a gc reference from
943 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
944 // and DST_OFF. For powerpc64, this adds a referenc to the code
945 // section of a function descriptor.
947 do_gc_add_reference(Symbol_table* symtab,
949 unsigned int src_shndx,
951 unsigned int dst_shndx,
952 Address dst_off) const;
954 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
957 { return this->stub_tables_; }
959 const Output_data_brlt_powerpc<size, big_endian>*
961 { return this->brlt_section_; }
964 add_branch_lookup_table(Address to)
966 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
967 this->branch_lookup_table_.insert(std::make_pair(to, off));
971 find_branch_lookup_table(Address to)
973 typename Branch_lookup_table::const_iterator p
974 = this->branch_lookup_table_.find(to);
975 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
979 write_branch_lookup_table(unsigned char *oview)
981 for (typename Branch_lookup_table::const_iterator p
982 = this->branch_lookup_table_.begin();
983 p != this->branch_lookup_table_.end();
986 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
990 // Wrapper used after relax to define a local symbol in output data,
991 // from the end if value < 0.
993 define_local(Symbol_table* symtab, const char* name,
994 Output_data* od, Address value, unsigned int symsize)
997 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
998 od, value, symsize, elfcpp::STT_NOTYPE,
999 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1000 static_cast<Signed_address>(value) < 0,
1002 // We are creating this symbol late, so need to fix up things
1003 // done early in Layout::finalize.
1004 sym->set_dynsym_index(-1U);
1008 plt_thread_safe() const
1009 { return this->plt_thread_safe_; }
1012 plt_localentry0() const
1013 { return this->plt_localentry0_; }
1016 set_has_localentry0()
1018 this->has_localentry0_ = true;
1022 is_elfv2_localentry0(const Symbol* gsym) const
1025 && this->abiversion() >= 2
1026 && this->plt_localentry0()
1027 && gsym->type() == elfcpp::STT_FUNC
1028 && gsym->is_defined()
1029 && gsym->nonvis() >> 3 == 0);
1033 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1034 unsigned int r_sym) const
1036 const Powerpc_relobj<size, big_endian>* ppc_object
1037 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1040 && this->abiversion() >= 2
1041 && this->plt_localentry0()
1042 && ppc_object->st_other(r_sym) >> 5 == 0)
1044 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1046 if (!psymval->is_ifunc_symbol()
1047 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1056 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1059 set_abiversion (int ver)
1061 elfcpp::Elf_Word flags = this->processor_specific_flags();
1062 flags &= ~elfcpp::EF_PPC64_ABI;
1063 flags |= ver & elfcpp::EF_PPC64_ABI;
1064 this->set_processor_specific_flags(flags);
1067 // Offset to save stack slot
1070 { return this->abiversion() < 2 ? 40 : 24; }
1086 : tls_get_addr_(NOT_EXPECTED),
1087 relinfo_(NULL), relnum_(0), r_offset_(0)
1092 if (this->tls_get_addr_ != NOT_EXPECTED)
1099 if (this->relinfo_ != NULL)
1100 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1101 _("missing expected __tls_get_addr call"));
1105 expect_tls_get_addr_call(
1106 const Relocate_info<size, big_endian>* relinfo,
1110 this->tls_get_addr_ = EXPECTED;
1111 this->relinfo_ = relinfo;
1112 this->relnum_ = relnum;
1113 this->r_offset_ = r_offset;
1117 expect_tls_get_addr_call()
1118 { this->tls_get_addr_ = EXPECTED; }
1121 skip_next_tls_get_addr_call()
1122 {this->tls_get_addr_ = SKIP; }
1125 maybe_skip_tls_get_addr_call(unsigned int r_type, const Symbol* gsym)
1127 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
1128 || r_type == elfcpp::R_PPC_PLTREL24)
1130 && strcmp(gsym->name(), "__tls_get_addr") == 0);
1131 Tls_get_addr last_tls = this->tls_get_addr_;
1132 this->tls_get_addr_ = NOT_EXPECTED;
1133 if (is_tls_call && last_tls != EXPECTED)
1135 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1144 // What we're up to regarding calls to __tls_get_addr.
1145 // On powerpc, the branch and link insn making a call to
1146 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1147 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1148 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
1149 // The marker relocation always comes first, and has the same
1150 // symbol as the reloc on the insn setting up the __tls_get_addr
1151 // argument. This ties the arg setup insn with the call insn,
1152 // allowing ld to safely optimize away the call. We check that
1153 // every call to __tls_get_addr has a marker relocation, and that
1154 // every marker relocation is on a call to __tls_get_addr.
1155 Tls_get_addr tls_get_addr_;
1156 // Info about the last reloc for error message.
1157 const Relocate_info<size, big_endian>* relinfo_;
1162 // The class which scans relocations.
1163 class Scan : protected Track_tls
1166 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1169 : Track_tls(), issued_non_pic_error_(false)
1173 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
1176 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1177 Sized_relobj_file<size, big_endian>* object,
1178 unsigned int data_shndx,
1179 Output_section* output_section,
1180 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1181 const elfcpp::Sym<size, big_endian>& lsym,
1185 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1186 Sized_relobj_file<size, big_endian>* object,
1187 unsigned int data_shndx,
1188 Output_section* output_section,
1189 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1193 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1195 Sized_relobj_file<size, big_endian>* relobj,
1198 const elfcpp::Rela<size, big_endian>& ,
1199 unsigned int r_type,
1200 const elfcpp::Sym<size, big_endian>&)
1202 // PowerPC64 .opd is not folded, so any identical function text
1203 // may be folded and we'll still keep function addresses distinct.
1204 // That means no reloc is of concern here.
1207 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1208 <Powerpc_relobj<size, big_endian>*>(relobj);
1209 if (ppcobj->abiversion() == 1)
1212 // For 32-bit and ELFv2, conservatively assume anything but calls to
1213 // function code might be taking the address of the function.
1214 return !is_branch_reloc(r_type);
1218 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1220 Sized_relobj_file<size, big_endian>* relobj,
1223 const elfcpp::Rela<size, big_endian>& ,
1224 unsigned int r_type,
1230 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1231 <Powerpc_relobj<size, big_endian>*>(relobj);
1232 if (ppcobj->abiversion() == 1)
1235 return !is_branch_reloc(r_type);
1239 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1240 Sized_relobj_file<size, big_endian>* object,
1241 unsigned int r_type, bool report_err);
1245 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1246 unsigned int r_type);
1249 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1250 unsigned int r_type, Symbol*);
1253 generate_tls_call(Symbol_table* symtab, Layout* layout,
1254 Target_powerpc* target);
1257 check_non_pic(Relobj*, unsigned int r_type);
1259 // Whether we have issued an error about a non-PIC compilation.
1260 bool issued_non_pic_error_;
1264 symval_for_branch(const Symbol_table* symtab,
1265 const Sized_symbol<size>* gsym,
1266 Powerpc_relobj<size, big_endian>* object,
1267 Address *value, unsigned int *dest_shndx);
1269 // The class which implements relocation.
1270 class Relocate : protected Track_tls
1273 // Use 'at' branch hints when true, 'y' when false.
1274 // FIXME maybe: set this with an option.
1275 static const bool is_isa_v2 = true;
1281 // Do a relocation. Return false if the caller should not issue
1282 // any warnings about this relocation.
1284 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1285 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1286 const Sized_symbol<size>*, const Symbol_value<size>*,
1287 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1291 class Relocate_comdat_behavior
1294 // Decide what the linker should do for relocations that refer to
1295 // discarded comdat sections.
1296 inline Comdat_behavior
1297 get(const char* name)
1299 gold::Default_comdat_behavior default_behavior;
1300 Comdat_behavior ret = default_behavior.get(name);
1301 if (ret == CB_WARNING)
1304 && (strcmp(name, ".fixup") == 0
1305 || strcmp(name, ".got2") == 0))
1308 && (strcmp(name, ".opd") == 0
1309 || strcmp(name, ".toc") == 0
1310 || strcmp(name, ".toc1") == 0))
1317 // Optimize the TLS relocation type based on what we know about the
1318 // symbol. IS_FINAL is true if the final address of this symbol is
1319 // known at link time.
1321 tls::Tls_optimization
1322 optimize_tls_gd(bool is_final)
1324 // If we are generating a shared library, then we can't do anything
1326 if (parameters->options().shared())
1327 return tls::TLSOPT_NONE;
1330 return tls::TLSOPT_TO_IE;
1331 return tls::TLSOPT_TO_LE;
1334 tls::Tls_optimization
1337 if (parameters->options().shared())
1338 return tls::TLSOPT_NONE;
1340 return tls::TLSOPT_TO_LE;
1343 tls::Tls_optimization
1344 optimize_tls_ie(bool is_final)
1346 if (!is_final || parameters->options().shared())
1347 return tls::TLSOPT_NONE;
1349 return tls::TLSOPT_TO_LE;
1354 make_glink_section(Layout*);
1356 // Create the PLT section.
1358 make_plt_section(Symbol_table*, Layout*);
1361 make_iplt_section(Symbol_table*, Layout*);
1364 make_brlt_section(Layout*);
1366 // Create a PLT entry for a global symbol.
1368 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1370 // Create a PLT entry for a local IFUNC symbol.
1372 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1373 Sized_relobj_file<size, big_endian>*,
1377 // Create a GOT entry for local dynamic __tls_get_addr.
1379 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1380 Sized_relobj_file<size, big_endian>* object);
1383 tlsld_got_offset() const
1385 return this->tlsld_got_offset_;
1388 // Get the dynamic reloc section, creating it if necessary.
1390 rela_dyn_section(Layout*);
1392 // Similarly, but for ifunc symbols get the one for ifunc.
1394 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1396 // Copy a relocation against a global symbol.
1398 copy_reloc(Symbol_table* symtab, Layout* layout,
1399 Sized_relobj_file<size, big_endian>* object,
1400 unsigned int shndx, Output_section* output_section,
1401 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1403 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1404 this->copy_relocs_.copy_reloc(symtab, layout,
1405 symtab->get_sized_symbol<size>(sym),
1406 object, shndx, output_section,
1407 r_type, reloc.get_r_offset(),
1408 reloc.get_r_addend(),
1409 this->rela_dyn_section(layout));
1412 // Look over all the input sections, deciding where to place stubs.
1414 group_sections(Layout*, const Task*, bool);
1416 // Sort output sections by address.
1417 struct Sort_sections
1420 operator()(const Output_section* sec1, const Output_section* sec2)
1421 { return sec1->address() < sec2->address(); }
1427 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1428 unsigned int data_shndx,
1430 unsigned int r_type,
1433 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1434 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
1440 // Return whether this branch is going via a plt call stub, and if
1441 // so, mark it as having an R_PPC64_TOCSAVE.
1443 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1444 unsigned int shndx, Address offset,
1445 Target_powerpc* target, Symbol_table* symtab);
1447 // If this branch needs a plt call stub, or a long branch stub, make one.
1449 make_stub(Stub_table<size, big_endian>*,
1450 Stub_table<size, big_endian>*,
1451 Symbol_table*) const;
1454 // The branch location..
1455 Powerpc_relobj<size, big_endian>* object_;
1456 unsigned int shndx_;
1458 // ..and the branch type and destination.
1459 unsigned int r_type_ : 31;
1460 unsigned int tocsave_ : 1;
1461 unsigned int r_sym_;
1465 // Information about this specific target which we pass to the
1466 // general Target structure.
1467 static Target::Target_info powerpc_info;
1469 // The types of GOT entries needed for this platform.
1470 // These values are exposed to the ABI in an incremental link.
1471 // Do not renumber existing values without changing the version
1472 // number of the .gnu_incremental_inputs section.
1476 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1477 GOT_TYPE_DTPREL, // entry for @got@dtprel
1478 GOT_TYPE_TPREL // entry for @got@tprel
1482 Output_data_got_powerpc<size, big_endian>* got_;
1483 // The PLT section. This is a container for a table of addresses,
1484 // and their relocations. Each address in the PLT has a dynamic
1485 // relocation (R_*_JMP_SLOT) and each address will have a
1486 // corresponding entry in .glink for lazy resolution of the PLT.
1487 // ppc32 initialises the PLT to point at the .glink entry, while
1488 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1489 // linker adds a stub that loads the PLT entry into ctr then
1490 // branches to ctr. There may be more than one stub for each PLT
1491 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1492 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1493 Output_data_plt_powerpc<size, big_endian>* plt_;
1494 // The IPLT section. Like plt_, this is a container for a table of
1495 // addresses and their relocations, specifically for STT_GNU_IFUNC
1496 // functions that resolve locally (STT_GNU_IFUNC functions that
1497 // don't resolve locally go in PLT). Unlike plt_, these have no
1498 // entry in .glink for lazy resolution, and the relocation section
1499 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1500 // the relocation section may contain relocations against
1501 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1502 // relocation section will appear at the end of other dynamic
1503 // relocations, so that ld.so applies these relocations after other
1504 // dynamic relocations. In a static executable, the relocation
1505 // section is emitted and marked with __rela_iplt_start and
1506 // __rela_iplt_end symbols.
1507 Output_data_plt_powerpc<size, big_endian>* iplt_;
1508 // Section holding long branch destinations.
1509 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1510 // The .glink section.
1511 Output_data_glink<size, big_endian>* glink_;
1512 // The dynamic reloc section.
1513 Reloc_section* rela_dyn_;
1514 // Relocs saved to avoid a COPY reloc.
1515 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1516 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1517 unsigned int tlsld_got_offset_;
1519 Stub_tables stub_tables_;
1520 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1521 Branch_lookup_table branch_lookup_table_;
1523 typedef std::vector<Branch_info> Branches;
1524 Branches branch_info_;
1525 Tocsave_loc tocsave_loc_;
1527 bool plt_thread_safe_;
1528 bool plt_localentry0_;
1529 bool plt_localentry0_init_;
1530 bool has_localentry0_;
1533 int relax_fail_count_;
1534 int32_t stub_group_size_;
1536 Output_data_save_res<size, big_endian> *savres_section_;
1540 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1543 true, // is_big_endian
1544 elfcpp::EM_PPC, // machine_code
1545 false, // has_make_symbol
1546 false, // has_resolve
1547 false, // has_code_fill
1548 true, // is_default_stack_executable
1549 false, // can_icf_inline_merge_sections
1551 "/usr/lib/ld.so.1", // dynamic_linker
1552 0x10000000, // default_text_segment_address
1553 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1554 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1555 false, // isolate_execinstr
1557 elfcpp::SHN_UNDEF, // small_common_shndx
1558 elfcpp::SHN_UNDEF, // large_common_shndx
1559 0, // small_common_section_flags
1560 0, // large_common_section_flags
1561 NULL, // attributes_section
1562 NULL, // attributes_vendor
1563 "_start", // entry_symbol_name
1564 32, // hash_entry_size
1568 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1571 false, // is_big_endian
1572 elfcpp::EM_PPC, // machine_code
1573 false, // has_make_symbol
1574 false, // has_resolve
1575 false, // has_code_fill
1576 true, // is_default_stack_executable
1577 false, // can_icf_inline_merge_sections
1579 "/usr/lib/ld.so.1", // dynamic_linker
1580 0x10000000, // default_text_segment_address
1581 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1582 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1583 false, // isolate_execinstr
1585 elfcpp::SHN_UNDEF, // small_common_shndx
1586 elfcpp::SHN_UNDEF, // large_common_shndx
1587 0, // small_common_section_flags
1588 0, // large_common_section_flags
1589 NULL, // attributes_section
1590 NULL, // attributes_vendor
1591 "_start", // entry_symbol_name
1592 32, // hash_entry_size
1596 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1599 true, // is_big_endian
1600 elfcpp::EM_PPC64, // machine_code
1601 false, // has_make_symbol
1602 false, // has_resolve
1603 false, // has_code_fill
1604 true, // is_default_stack_executable
1605 false, // can_icf_inline_merge_sections
1607 "/usr/lib/ld.so.1", // dynamic_linker
1608 0x10000000, // default_text_segment_address
1609 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1610 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1611 false, // isolate_execinstr
1613 elfcpp::SHN_UNDEF, // small_common_shndx
1614 elfcpp::SHN_UNDEF, // large_common_shndx
1615 0, // small_common_section_flags
1616 0, // large_common_section_flags
1617 NULL, // attributes_section
1618 NULL, // attributes_vendor
1619 "_start", // entry_symbol_name
1620 32, // hash_entry_size
1624 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1627 false, // is_big_endian
1628 elfcpp::EM_PPC64, // machine_code
1629 false, // has_make_symbol
1630 false, // has_resolve
1631 false, // has_code_fill
1632 true, // is_default_stack_executable
1633 false, // can_icf_inline_merge_sections
1635 "/usr/lib/ld.so.1", // dynamic_linker
1636 0x10000000, // default_text_segment_address
1637 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1638 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1639 false, // isolate_execinstr
1641 elfcpp::SHN_UNDEF, // small_common_shndx
1642 elfcpp::SHN_UNDEF, // large_common_shndx
1643 0, // small_common_section_flags
1644 0, // large_common_section_flags
1645 NULL, // attributes_section
1646 NULL, // attributes_vendor
1647 "_start", // entry_symbol_name
1648 32, // hash_entry_size
1652 is_branch_reloc(unsigned int r_type)
1654 return (r_type == elfcpp::R_POWERPC_REL24
1655 || r_type == elfcpp::R_PPC_PLTREL24
1656 || r_type == elfcpp::R_PPC_LOCAL24PC
1657 || r_type == elfcpp::R_POWERPC_REL14
1658 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1659 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1660 || r_type == elfcpp::R_POWERPC_ADDR24
1661 || r_type == elfcpp::R_POWERPC_ADDR14
1662 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1663 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1666 // If INSN is an opcode that may be used with an @tls operand, return
1667 // the transformed insn for TLS optimisation, otherwise return 0. If
1668 // REG is non-zero only match an insn with RB or RA equal to REG.
1670 at_tls_transform(uint32_t insn, unsigned int reg)
1672 if ((insn & (0x3f << 26)) != 31 << 26)
1676 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1677 rtra = insn & ((1 << 26) - (1 << 16));
1678 else if (((insn >> 16) & 0x1f) == reg)
1679 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1683 if ((insn & (0x3ff << 1)) == 266 << 1)
1686 else if ((insn & (0x1f << 1)) == 23 << 1
1687 && ((insn & (0x1f << 6)) < 14 << 6
1688 || ((insn & (0x1f << 6)) >= 16 << 6
1689 && (insn & (0x1f << 6)) < 24 << 6)))
1690 // load and store indexed -> dform
1691 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1692 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1693 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1694 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1695 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1697 insn = (58 << 26) | 2;
1705 template<int size, bool big_endian>
1706 class Powerpc_relocate_functions
1726 typedef Powerpc_relocate_functions<size, big_endian> This;
1727 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1728 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
1730 template<int valsize>
1732 has_overflow_signed(Address value)
1734 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1735 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1736 limit <<= ((valsize - 1) >> 1);
1737 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1738 return value + limit > (limit << 1) - 1;
1741 template<int valsize>
1743 has_overflow_unsigned(Address value)
1745 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1746 limit <<= ((valsize - 1) >> 1);
1747 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1748 return value > (limit << 1) - 1;
1751 template<int valsize>
1753 has_overflow_bitfield(Address value)
1755 return (has_overflow_unsigned<valsize>(value)
1756 && has_overflow_signed<valsize>(value));
1759 template<int valsize>
1760 static inline Status
1761 overflowed(Address value, Overflow_check overflow)
1763 if (overflow == CHECK_SIGNED)
1765 if (has_overflow_signed<valsize>(value))
1766 return STATUS_OVERFLOW;
1768 else if (overflow == CHECK_UNSIGNED)
1770 if (has_overflow_unsigned<valsize>(value))
1771 return STATUS_OVERFLOW;
1773 else if (overflow == CHECK_BITFIELD)
1775 if (has_overflow_bitfield<valsize>(value))
1776 return STATUS_OVERFLOW;
1781 // Do a simple RELA relocation
1782 template<int fieldsize, int valsize>
1783 static inline Status
1784 rela(unsigned char* view, Address value, Overflow_check overflow)
1786 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1787 Valtype* wv = reinterpret_cast<Valtype*>(view);
1788 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
1789 return overflowed<valsize>(value, overflow);
1792 template<int fieldsize, int valsize>
1793 static inline Status
1794 rela(unsigned char* view,
1795 unsigned int right_shift,
1796 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1798 Overflow_check overflow)
1800 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1801 Valtype* wv = reinterpret_cast<Valtype*>(view);
1802 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
1803 Valtype reloc = value >> right_shift;
1806 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
1807 return overflowed<valsize>(value >> right_shift, overflow);
1810 // Do a simple RELA relocation, unaligned.
1811 template<int fieldsize, int valsize>
1812 static inline Status
1813 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1815 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
1816 return overflowed<valsize>(value, overflow);
1819 template<int fieldsize, int valsize>
1820 static inline Status
1821 rela_ua(unsigned char* view,
1822 unsigned int right_shift,
1823 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1825 Overflow_check overflow)
1827 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
1829 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
1830 Valtype reloc = value >> right_shift;
1833 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
1834 return overflowed<valsize>(value >> right_shift, overflow);
1838 // R_PPC64_ADDR64: (Symbol + Addend)
1840 addr64(unsigned char* view, Address value)
1841 { This::template rela<64,64>(view, value, CHECK_NONE); }
1843 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1845 addr64_u(unsigned char* view, Address value)
1846 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
1848 // R_POWERPC_ADDR32: (Symbol + Addend)
1849 static inline Status
1850 addr32(unsigned char* view, Address value, Overflow_check overflow)
1851 { return This::template rela<32,32>(view, value, overflow); }
1853 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1854 static inline Status
1855 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1856 { return This::template rela_ua<32,32>(view, value, overflow); }
1858 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1859 static inline Status
1860 addr24(unsigned char* view, Address value, Overflow_check overflow)
1862 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
1864 if (overflow != CHECK_NONE && (value & 3) != 0)
1865 stat = STATUS_OVERFLOW;
1869 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1870 static inline Status
1871 addr16(unsigned char* view, Address value, Overflow_check overflow)
1872 { return This::template rela<16,16>(view, value, overflow); }
1874 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1875 static inline Status
1876 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1877 { return This::template rela_ua<16,16>(view, value, overflow); }
1879 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1880 static inline Status
1881 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1883 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
1884 if ((value & 3) != 0)
1885 stat = STATUS_OVERFLOW;
1889 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
1890 static inline Status
1891 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
1893 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
1894 if ((value & 15) != 0)
1895 stat = STATUS_OVERFLOW;
1899 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1901 addr16_hi(unsigned char* view, Address value)
1902 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
1904 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1906 addr16_ha(unsigned char* view, Address value)
1907 { This::addr16_hi(view, value + 0x8000); }
1909 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1911 addr16_hi2(unsigned char* view, Address value)
1912 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
1914 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1916 addr16_ha2(unsigned char* view, Address value)
1917 { This::addr16_hi2(view, value + 0x8000); }
1919 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1921 addr16_hi3(unsigned char* view, Address value)
1922 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
1924 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1926 addr16_ha3(unsigned char* view, Address value)
1927 { This::addr16_hi3(view, value + 0x8000); }
1929 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1930 static inline Status
1931 addr14(unsigned char* view, Address value, Overflow_check overflow)
1933 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
1934 if (overflow != CHECK_NONE && (value & 3) != 0)
1935 stat = STATUS_OVERFLOW;
1939 // R_POWERPC_REL16DX_HA
1940 static inline Status
1941 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
1943 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1944 Valtype* wv = reinterpret_cast<Valtype*>(view);
1945 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1947 value = static_cast<SignedAddress>(value) >> 16;
1948 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
1949 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1950 return overflowed<16>(value, overflow);
1954 // Set ABI version for input and output.
1956 template<int size, bool big_endian>
1958 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
1960 this->e_flags_ |= ver;
1961 if (this->abiversion() != 0)
1963 Target_powerpc<size, big_endian>* target =
1964 static_cast<Target_powerpc<size, big_endian>*>(
1965 parameters->sized_target<size, big_endian>());
1966 if (target->abiversion() == 0)
1967 target->set_abiversion(this->abiversion());
1968 else if (target->abiversion() != this->abiversion())
1969 gold_error(_("%s: ABI version %d is not compatible "
1970 "with ABI version %d output"),
1971 this->name().c_str(),
1972 this->abiversion(), target->abiversion());
1977 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
1978 // relocatable object, if such sections exists.
1980 template<int size, bool big_endian>
1982 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1983 Read_symbols_data* sd)
1985 const unsigned char* const pshdrs = sd->section_headers->data();
1986 const unsigned char* namesu = sd->section_names->data();
1987 const char* names = reinterpret_cast<const char*>(namesu);
1988 section_size_type names_size = sd->section_names_size;
1989 const unsigned char* s;
1991 s = this->template find_shdr<size, big_endian>(pshdrs,
1992 size == 32 ? ".got2" : ".opd",
1993 names, names_size, NULL);
1996 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1997 this->special_ = ndx;
2000 if (this->abiversion() == 0)
2001 this->set_abiversion(1);
2002 else if (this->abiversion() > 1)
2003 gold_error(_("%s: .opd invalid in abiv%d"),
2004 this->name().c_str(), this->abiversion());
2009 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2010 names, names_size, NULL);
2013 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2014 this->relatoc_ = ndx;
2015 typename elfcpp::Shdr<size, big_endian> shdr(s);
2016 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2019 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2022 // Examine .rela.opd to build info about function entry points.
2024 template<int size, bool big_endian>
2026 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2028 const unsigned char* prelocs,
2029 const unsigned char* plocal_syms)
2033 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2034 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2035 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2036 Address expected_off = 0;
2037 bool regular = true;
2038 unsigned int opd_ent_size = 0;
2040 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2042 Reltype reloc(prelocs);
2043 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2044 = reloc.get_r_info();
2045 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2046 if (r_type == elfcpp::R_PPC64_ADDR64)
2048 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2049 typename elfcpp::Elf_types<size>::Elf_Addr value;
2052 if (r_sym < this->local_symbol_count())
2054 typename elfcpp::Sym<size, big_endian>
2055 lsym(plocal_syms + r_sym * sym_size);
2056 shndx = lsym.get_st_shndx();
2057 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2058 value = lsym.get_st_value();
2061 shndx = this->symbol_section_and_value(r_sym, &value,
2063 this->set_opd_ent(reloc.get_r_offset(), shndx,
2064 value + reloc.get_r_addend());
2067 expected_off = reloc.get_r_offset();
2068 opd_ent_size = expected_off;
2070 else if (expected_off != reloc.get_r_offset())
2072 expected_off += opd_ent_size;
2074 else if (r_type == elfcpp::R_PPC64_TOC)
2076 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2081 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2082 this->name().c_str(), r_type);
2086 if (reloc_count <= 2)
2087 opd_ent_size = this->section_size(this->opd_shndx());
2088 if (opd_ent_size != 24 && opd_ent_size != 16)
2092 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2093 this->name().c_str());
2099 // Returns true if a code sequence loading the TOC entry at VALUE
2100 // relative to the TOC pointer can be converted into code calculating
2101 // a TOC pointer relative offset.
2102 // If so, the TOC pointer relative offset is stored to VALUE.
2104 template<int size, bool big_endian>
2106 Powerpc_relobj<size, big_endian>::make_toc_relative(
2107 Target_powerpc<size, big_endian>* target,
2113 // With -mcmodel=medium code it is quite possible to have
2114 // toc-relative relocs referring to objects outside the TOC.
2115 // Don't try to look at a non-existent TOC.
2116 if (this->toc_shndx() == 0)
2119 // Convert VALUE back to an address by adding got_base (see below),
2120 // then to an offset in the TOC by subtracting the TOC output
2121 // section address and the TOC output offset. Since this TOC output
2122 // section and the got output section are one and the same, we can
2123 // omit adding and subtracting the output section address.
2124 Address off = (*value + this->toc_base_offset()
2125 - this->output_section_offset(this->toc_shndx()));
2126 // Is this offset in the TOC? -mcmodel=medium code may be using
2127 // TOC relative access to variables outside the TOC. Those of
2128 // course can't be optimized. We also don't try to optimize code
2129 // that is using a different object's TOC.
2130 if (off >= this->section_size(this->toc_shndx()))
2133 if (this->no_toc_opt(off))
2136 section_size_type vlen;
2137 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2138 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2140 Address got_base = (target->got_section()->output_section()->address()
2141 + this->toc_base_offset());
2143 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2150 // Perform the Sized_relobj_file method, then set up opd info from
2153 template<int size, bool big_endian>
2155 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2157 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2160 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2161 p != rd->relocs.end();
2164 if (p->data_shndx == this->opd_shndx())
2166 uint64_t opd_size = this->section_size(this->opd_shndx());
2167 gold_assert(opd_size == static_cast<size_t>(opd_size));
2170 this->init_opd(opd_size);
2171 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2172 rd->local_symbols->data());
2180 // Read the symbols then set up st_other vector.
2182 template<int size, bool big_endian>
2184 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2186 this->base_read_symbols(sd);
2189 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2190 const unsigned char* const pshdrs = sd->section_headers->data();
2191 const unsigned int loccount = this->do_local_symbol_count();
2194 this->st_other_.resize(loccount);
2195 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2196 off_t locsize = loccount * sym_size;
2197 const unsigned int symtab_shndx = this->symtab_shndx();
2198 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2199 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2200 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2201 locsize, true, false);
2203 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2205 elfcpp::Sym<size, big_endian> sym(psyms);
2206 unsigned char st_other = sym.get_st_other();
2207 this->st_other_[i] = st_other;
2208 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2210 if (this->abiversion() == 0)
2211 this->set_abiversion(2);
2212 else if (this->abiversion() < 2)
2213 gold_error(_("%s: local symbol %d has invalid st_other"
2214 " for ABI version 1"),
2215 this->name().c_str(), i);
2222 template<int size, bool big_endian>
2224 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2226 this->e_flags_ |= ver;
2227 if (this->abiversion() != 0)
2229 Target_powerpc<size, big_endian>* target =
2230 static_cast<Target_powerpc<size, big_endian>*>(
2231 parameters->sized_target<size, big_endian>());
2232 if (target->abiversion() == 0)
2233 target->set_abiversion(this->abiversion());
2234 else if (target->abiversion() != this->abiversion())
2235 gold_error(_("%s: ABI version %d is not compatible "
2236 "with ABI version %d output"),
2237 this->name().c_str(),
2238 this->abiversion(), target->abiversion());
2243 // Call Sized_dynobj::base_read_symbols to read the symbols then
2244 // read .opd from a dynamic object, filling in opd_ent_ vector,
2246 template<int size, bool big_endian>
2248 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2250 this->base_read_symbols(sd);
2253 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2254 const unsigned char* const pshdrs = sd->section_headers->data();
2255 const unsigned char* namesu = sd->section_names->data();
2256 const char* names = reinterpret_cast<const char*>(namesu);
2257 const unsigned char* s = NULL;
2258 const unsigned char* opd;
2259 section_size_type opd_size;
2261 // Find and read .opd section.
2264 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2265 sd->section_names_size,
2270 typename elfcpp::Shdr<size, big_endian> shdr(s);
2271 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2272 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2274 if (this->abiversion() == 0)
2275 this->set_abiversion(1);
2276 else if (this->abiversion() > 1)
2277 gold_error(_("%s: .opd invalid in abiv%d"),
2278 this->name().c_str(), this->abiversion());
2280 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2281 this->opd_address_ = shdr.get_sh_addr();
2282 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2283 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2289 // Build set of executable sections.
2290 // Using a set is probably overkill. There is likely to be only
2291 // a few executable sections, typically .init, .text and .fini,
2292 // and they are generally grouped together.
2293 typedef std::set<Sec_info> Exec_sections;
2294 Exec_sections exec_sections;
2296 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2298 typename elfcpp::Shdr<size, big_endian> shdr(s);
2299 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2300 && ((shdr.get_sh_flags()
2301 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2302 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2303 && shdr.get_sh_size() != 0)
2305 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2306 shdr.get_sh_size(), i));
2309 if (exec_sections.empty())
2312 // Look over the OPD entries. This is complicated by the fact
2313 // that some binaries will use two-word entries while others
2314 // will use the standard three-word entries. In most cases
2315 // the third word (the environment pointer for languages like
2316 // Pascal) is unused and will be zero. If the third word is
2317 // used it should not be pointing into executable sections,
2319 this->init_opd(opd_size);
2320 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2322 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2323 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2324 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2326 // Chances are that this is the third word of an OPD entry.
2328 typename Exec_sections::const_iterator e
2329 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2330 if (e != exec_sections.begin())
2333 if (e->start <= val && val < e->start + e->len)
2335 // We have an address in an executable section.
2336 // VAL ought to be the function entry, set it up.
2337 this->set_opd_ent(p - opd, e->shndx, val);
2338 // Skip second word of OPD entry, the TOC pointer.
2342 // If we didn't match any executable sections, we likely
2343 // have a non-zero third word in the OPD entry.
2348 // Relocate sections.
2350 template<int size, bool big_endian>
2352 Powerpc_relobj<size, big_endian>::do_relocate_sections(
2353 const Symbol_table* symtab, const Layout* layout,
2354 const unsigned char* pshdrs, Output_file* of,
2355 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2357 unsigned int start = 1;
2359 && this->relatoc_ != 0
2360 && !parameters->options().relocatable())
2362 // Relocate .toc first.
2363 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2364 this->relatoc_, this->relatoc_);
2365 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2366 1, this->relatoc_ - 1);
2367 start = this->relatoc_ + 1;
2369 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2370 start, this->shnum() - 1);
2373 // Set up some symbols.
2375 template<int size, bool big_endian>
2377 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2378 Symbol_table* symtab,
2383 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2384 // undefined when scanning relocs (and thus requires
2385 // non-relative dynamic relocs). The proper value will be
2387 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2388 if (gotsym != NULL && gotsym->is_undefined())
2390 Target_powerpc<size, big_endian>* target =
2391 static_cast<Target_powerpc<size, big_endian>*>(
2392 parameters->sized_target<size, big_endian>());
2393 Output_data_got_powerpc<size, big_endian>* got
2394 = target->got_section(symtab, layout);
2395 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2396 Symbol_table::PREDEFINED,
2400 elfcpp::STV_HIDDEN, 0,
2404 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2405 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2406 if (sdasym != NULL && sdasym->is_undefined())
2408 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2410 = layout->add_output_section_data(".sdata", 0,
2412 | elfcpp::SHF_WRITE,
2413 sdata, ORDER_SMALL_DATA, false);
2414 symtab->define_in_output_data("_SDA_BASE_", NULL,
2415 Symbol_table::PREDEFINED,
2416 os, 32768, 0, elfcpp::STT_OBJECT,
2417 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2423 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2424 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2425 if (gotsym != NULL && gotsym->is_undefined())
2427 Target_powerpc<size, big_endian>* target =
2428 static_cast<Target_powerpc<size, big_endian>*>(
2429 parameters->sized_target<size, big_endian>());
2430 Output_data_got_powerpc<size, big_endian>* got
2431 = target->got_section(symtab, layout);
2432 symtab->define_in_output_data(".TOC.", NULL,
2433 Symbol_table::PREDEFINED,
2437 elfcpp::STV_HIDDEN, 0,
2443 // Set up PowerPC target specific relobj.
2445 template<int size, bool big_endian>
2447 Target_powerpc<size, big_endian>::do_make_elf_object(
2448 const std::string& name,
2449 Input_file* input_file,
2450 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2452 int et = ehdr.get_e_type();
2453 // ET_EXEC files are valid input for --just-symbols/-R,
2454 // and we treat them as relocatable objects.
2455 if (et == elfcpp::ET_REL
2456 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2458 Powerpc_relobj<size, big_endian>* obj =
2459 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2463 else if (et == elfcpp::ET_DYN)
2465 Powerpc_dynobj<size, big_endian>* obj =
2466 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2472 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2477 template<int size, bool big_endian>
2478 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2481 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2482 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2484 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2485 : Output_data_got<size, big_endian>(),
2486 symtab_(symtab), layout_(layout),
2487 header_ent_cnt_(size == 32 ? 3 : 1),
2488 header_index_(size == 32 ? 0x2000 : 0)
2491 this->set_addralign(256);
2494 // Override all the Output_data_got methods we use so as to first call
2497 add_global(Symbol* gsym, unsigned int got_type)
2499 this->reserve_ent();
2500 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2504 add_global_plt(Symbol* gsym, unsigned int got_type)
2506 this->reserve_ent();
2507 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2511 add_global_tls(Symbol* gsym, unsigned int got_type)
2512 { return this->add_global_plt(gsym, got_type); }
2515 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2516 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2518 this->reserve_ent();
2519 Output_data_got<size, big_endian>::
2520 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2524 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2525 Output_data_reloc_generic* rel_dyn,
2526 unsigned int r_type_1, unsigned int r_type_2)
2528 this->reserve_ent(2);
2529 Output_data_got<size, big_endian>::
2530 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2534 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2536 this->reserve_ent();
2537 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2542 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2544 this->reserve_ent();
2545 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2550 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2551 { return this->add_local_plt(object, sym_index, got_type); }
2554 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2555 unsigned int got_type,
2556 Output_data_reloc_generic* rel_dyn,
2557 unsigned int r_type)
2559 this->reserve_ent(2);
2560 Output_data_got<size, big_endian>::
2561 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2565 add_constant(Valtype constant)
2567 this->reserve_ent();
2568 return Output_data_got<size, big_endian>::add_constant(constant);
2572 add_constant_pair(Valtype c1, Valtype c2)
2574 this->reserve_ent(2);
2575 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
2578 // Offset of _GLOBAL_OFFSET_TABLE_.
2582 return this->got_offset(this->header_index_);
2585 // Offset of base used to access the GOT/TOC.
2586 // The got/toc pointer reg will be set to this value.
2588 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2591 return this->g_o_t();
2593 return (this->output_section()->address()
2594 + object->toc_base_offset()
2598 // Ensure our GOT has a header.
2600 set_final_data_size()
2602 if (this->header_ent_cnt_ != 0)
2603 this->make_header();
2604 Output_data_got<size, big_endian>::set_final_data_size();
2607 // First word of GOT header needs some values that are not
2608 // handled by Output_data_got so poke them in here.
2609 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2611 do_write(Output_file* of)
2614 if (size == 32 && this->layout_->dynamic_data() != NULL)
2615 val = this->layout_->dynamic_section()->address();
2617 val = this->output_section()->address() + 0x8000;
2618 this->replace_constant(this->header_index_, val);
2619 Output_data_got<size, big_endian>::do_write(of);
2624 reserve_ent(unsigned int cnt = 1)
2626 if (this->header_ent_cnt_ == 0)
2628 if (this->num_entries() + cnt > this->header_index_)
2629 this->make_header();
2635 this->header_ent_cnt_ = 0;
2636 this->header_index_ = this->num_entries();
2639 Output_data_got<size, big_endian>::add_constant(0);
2640 Output_data_got<size, big_endian>::add_constant(0);
2641 Output_data_got<size, big_endian>::add_constant(0);
2643 // Define _GLOBAL_OFFSET_TABLE_ at the header
2644 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2647 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
2648 sym->set_value(this->g_o_t());
2651 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2652 Symbol_table::PREDEFINED,
2653 this, this->g_o_t(), 0,
2656 elfcpp::STV_HIDDEN, 0,
2660 Output_data_got<size, big_endian>::add_constant(0);
2663 // Stashed pointers.
2664 Symbol_table* symtab_;
2668 unsigned int header_ent_cnt_;
2669 // GOT header index.
2670 unsigned int header_index_;
2673 // Get the GOT section, creating it if necessary.
2675 template<int size, bool big_endian>
2676 Output_data_got_powerpc<size, big_endian>*
2677 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
2680 if (this->got_ == NULL)
2682 gold_assert(symtab != NULL && layout != NULL);
2685 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
2687 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2688 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2689 this->got_, ORDER_DATA, false);
2695 // Get the dynamic reloc section, creating it if necessary.
2697 template<int size, bool big_endian>
2698 typename Target_powerpc<size, big_endian>::Reloc_section*
2699 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
2701 if (this->rela_dyn_ == NULL)
2703 gold_assert(layout != NULL);
2704 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2705 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2706 elfcpp::SHF_ALLOC, this->rela_dyn_,
2707 ORDER_DYNAMIC_RELOCS, false);
2709 return this->rela_dyn_;
2712 // Similarly, but for ifunc symbols get the one for ifunc.
2714 template<int size, bool big_endian>
2715 typename Target_powerpc<size, big_endian>::Reloc_section*
2716 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
2721 return this->rela_dyn_section(layout);
2723 if (this->iplt_ == NULL)
2724 this->make_iplt_section(symtab, layout);
2725 return this->iplt_->rel_plt();
2731 // Determine the stub group size. The group size is the absolute
2732 // value of the parameter --stub-group-size. If --stub-group-size
2733 // is passed a negative value, we restrict stubs to be always after
2734 // the stubbed branches.
2735 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
2736 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
2737 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
2738 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
2739 owner_(NULL), output_section_(NULL)
2743 // Return true iff input section can be handled by current stub
2746 can_add_to_stub_group(Output_section* o,
2747 const Output_section::Input_section* i,
2750 const Output_section::Input_section*
2756 { return output_section_; }
2759 set_output_and_owner(Output_section* o,
2760 const Output_section::Input_section* i)
2762 this->output_section_ = o;
2771 // Adding group sections before the stubs.
2772 FINDING_STUB_SECTION,
2773 // Adding group sections after the stubs.
2777 uint32_t stub_group_size_;
2778 bool stubs_always_after_branch_;
2779 bool suppress_size_errors_;
2780 // True if a stub group can serve multiple output sections.
2783 // Current max size of group. Starts at stub_group_size_ but is
2784 // reduced to stub_group_size_/1024 on seeing a section with
2785 // external conditional branches.
2786 uint32_t group_size_;
2787 uint64_t group_start_addr_;
2788 // owner_ and output_section_ specify the section to which stubs are
2789 // attached. The stubs are placed at the end of this section.
2790 const Output_section::Input_section* owner_;
2791 Output_section* output_section_;
2794 // Return true iff input section can be handled by current stub
2795 // group. Sections are presented to this function in order,
2796 // so the first section is the head of the group.
2799 Stub_control::can_add_to_stub_group(Output_section* o,
2800 const Output_section::Input_section* i,
2803 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
2805 uint64_t start_addr = o->address();
2808 // .init and .fini sections are pasted together to form a single
2809 // function. We can't be adding stubs in the middle of the function.
2810 this_size = o->data_size();
2813 start_addr += i->relobj()->output_section_offset(i->shndx());
2814 this_size = i->data_size();
2817 uint64_t end_addr = start_addr + this_size;
2818 uint32_t group_size = this->stub_group_size_;
2820 this->group_size_ = group_size = group_size >> 10;
2822 if (this_size > group_size && !this->suppress_size_errors_)
2823 gold_warning(_("%s:%s exceeds group size"),
2824 i->relobj()->name().c_str(),
2825 i->relobj()->section_name(i->shndx()).c_str());
2827 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
2828 has14 ? " 14bit" : "",
2829 i->relobj()->name().c_str(),
2830 i->relobj()->section_name(i->shndx()).c_str(),
2831 (long long) this_size,
2832 (this->state_ == NO_GROUP
2834 : (long long) end_addr - this->group_start_addr_));
2836 if (this->state_ == NO_GROUP)
2838 // Only here on very first use of Stub_control
2840 this->output_section_ = o;
2841 this->state_ = FINDING_STUB_SECTION;
2842 this->group_size_ = group_size;
2843 this->group_start_addr_ = start_addr;
2846 else if (!this->multi_os_ && this->output_section_ != o)
2848 else if (this->state_ == HAS_STUB_SECTION)
2850 // Can we add this section, which is after the stubs, to the
2852 if (end_addr - this->group_start_addr_ <= this->group_size_)
2855 else if (this->state_ == FINDING_STUB_SECTION)
2857 if ((whole_sec && this->output_section_ == o)
2858 || end_addr - this->group_start_addr_ <= this->group_size_)
2860 // Stubs are added at the end of "owner_".
2862 this->output_section_ = o;
2865 // The group before the stubs has reached maximum size.
2866 // Now see about adding sections after the stubs to the
2867 // group. If the current section has a 14-bit branch and
2868 // the group before the stubs exceeds group_size_ (because
2869 // they didn't have 14-bit branches), don't add sections
2870 // after the stubs: The size of stubs for such a large
2871 // group may exceed the reach of a 14-bit branch.
2872 if (!this->stubs_always_after_branch_
2873 && this_size <= this->group_size_
2874 && start_addr - this->group_start_addr_ <= this->group_size_)
2876 gold_debug(DEBUG_TARGET, "adding after stubs");
2877 this->state_ = HAS_STUB_SECTION;
2878 this->group_start_addr_ = start_addr;
2885 gold_debug(DEBUG_TARGET,
2886 !this->multi_os_ && this->output_section_ != o
2887 ? "nope, new output section\n"
2888 : "nope, didn't fit\n");
2890 // The section fails to fit in the current group. Set up a few
2891 // things for the next group. owner_ and output_section_ will be
2892 // set later after we've retrieved those values for the current
2894 this->state_ = FINDING_STUB_SECTION;
2895 this->group_size_ = group_size;
2896 this->group_start_addr_ = start_addr;
2900 // Look over all the input sections, deciding where to place stubs.
2902 template<int size, bool big_endian>
2904 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
2906 bool no_size_errors)
2908 Stub_control stub_control(this->stub_group_size_, no_size_errors,
2909 parameters->options().stub_group_multi());
2911 // Group input sections and insert stub table
2912 Stub_table_owner* table_owner = NULL;
2913 std::vector<Stub_table_owner*> tables;
2914 Layout::Section_list section_list;
2915 layout->get_executable_sections(§ion_list);
2916 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
2917 for (Layout::Section_list::iterator o = section_list.begin();
2918 o != section_list.end();
2921 typedef Output_section::Input_section_list Input_section_list;
2922 for (Input_section_list::const_iterator i
2923 = (*o)->input_sections().begin();
2924 i != (*o)->input_sections().end();
2927 if (i->is_input_section()
2928 || i->is_relaxed_input_section())
2930 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2931 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2932 bool has14 = ppcobj->has_14bit_branch(i->shndx());
2933 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
2935 table_owner->output_section = stub_control.output_section();
2936 table_owner->owner = stub_control.owner();
2937 stub_control.set_output_and_owner(*o, &*i);
2940 if (table_owner == NULL)
2942 table_owner = new Stub_table_owner;
2943 tables.push_back(table_owner);
2945 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
2949 if (table_owner != NULL)
2951 table_owner->output_section = stub_control.output_section();
2952 table_owner->owner = stub_control.owner();;
2954 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
2958 Stub_table<size, big_endian>* stub_table;
2960 if ((*t)->owner->is_input_section())
2961 stub_table = new Stub_table<size, big_endian>(this,
2962 (*t)->output_section,
2964 this->stub_tables_.size());
2965 else if ((*t)->owner->is_relaxed_input_section())
2966 stub_table = static_cast<Stub_table<size, big_endian>*>(
2967 (*t)->owner->relaxed_input_section());
2970 this->stub_tables_.push_back(stub_table);
2975 static unsigned long
2976 max_branch_delta (unsigned int r_type)
2978 if (r_type == elfcpp::R_POWERPC_REL14
2979 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
2980 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
2982 if (r_type == elfcpp::R_POWERPC_REL24
2983 || r_type == elfcpp::R_PPC_PLTREL24
2984 || r_type == elfcpp::R_PPC_LOCAL24PC)
2989 // Return whether this branch is going via a plt call stub.
2991 template<int size, bool big_endian>
2993 Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
2994 Powerpc_relobj<size, big_endian>* ppc_object,
2997 Target_powerpc* target,
2998 Symbol_table* symtab)
3000 if (this->object_ != ppc_object
3001 || this->shndx_ != shndx
3002 || this->offset_ != offset)
3005 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3006 if (sym != NULL && sym->is_forwarder())
3007 sym = symtab->resolve_forwards(sym);
3008 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3010 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3011 && !target->is_elfv2_localentry0(gsym))
3012 : (this->object_->local_has_plt_offset(this->r_sym_)
3013 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
3021 // If this branch needs a plt call stub, or a long branch stub, make one.
3023 template<int size, bool big_endian>
3025 Target_powerpc<size, big_endian>::Branch_info::make_stub(
3026 Stub_table<size, big_endian>* stub_table,
3027 Stub_table<size, big_endian>* ifunc_stub_table,
3028 Symbol_table* symtab) const
3030 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3031 if (sym != NULL && sym->is_forwarder())
3032 sym = symtab->resolve_forwards(sym);
3033 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3034 Target_powerpc<size, big_endian>* target =
3035 static_cast<Target_powerpc<size, big_endian>*>(
3036 parameters->sized_target<size, big_endian>());
3040 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3041 : this->object_->local_has_plt_offset(this->r_sym_))
3045 && target->abiversion() >= 2
3046 && !parameters->options().output_is_position_independent()
3047 && !is_branch_reloc(this->r_type_))
3048 target->glink_section()->add_global_entry(gsym);
3051 if (stub_table == NULL)
3052 stub_table = this->object_->stub_table(this->shndx_);
3053 if (stub_table == NULL)
3055 // This is a ref from a data section to an ifunc symbol.
3056 stub_table = ifunc_stub_table;
3058 gold_assert(stub_table != NULL);
3059 Address from = this->object_->get_output_section_offset(this->shndx_);
3060 if (from != invalid_address)
3061 from += (this->object_->output_section(this->shndx_)->address()
3064 ok = stub_table->add_plt_call_entry(from,
3065 this->object_, gsym,
3066 this->r_type_, this->addend_,
3069 ok = stub_table->add_plt_call_entry(from,
3070 this->object_, this->r_sym_,
3071 this->r_type_, this->addend_,
3077 Address max_branch_offset = max_branch_delta(this->r_type_);
3078 if (max_branch_offset == 0)
3080 Address from = this->object_->get_output_section_offset(this->shndx_);
3081 gold_assert(from != invalid_address);
3082 from += (this->object_->output_section(this->shndx_)->address()
3087 switch (gsym->source())
3089 case Symbol::FROM_OBJECT:
3091 Object* symobj = gsym->object();
3092 if (symobj->is_dynamic()
3093 || symobj->pluginobj() != NULL)
3096 unsigned int shndx = gsym->shndx(&is_ordinary);
3097 if (shndx == elfcpp::SHN_UNDEF)
3102 case Symbol::IS_UNDEFINED:
3108 Symbol_table::Compute_final_value_status status;
3109 to = symtab->compute_final_value<size>(gsym, &status);
3110 if (status != Symbol_table::CFVS_OK)
3113 to += this->object_->ppc64_local_entry_offset(gsym);
3117 const Symbol_value<size>* psymval
3118 = this->object_->local_symbol(this->r_sym_);
3119 Symbol_value<size> symval;
3120 if (psymval->is_section_symbol())
3121 symval.set_is_section_symbol();
3122 typedef Sized_relobj_file<size, big_endian> ObjType;
3123 typename ObjType::Compute_final_local_value_status status
3124 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3126 if (status != ObjType::CFLV_OK
3127 || !symval.has_output_value())
3129 to = symval.value(this->object_, 0);
3131 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
3133 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3134 to += this->addend_;
3135 if (stub_table == NULL)
3136 stub_table = this->object_->stub_table(this->shndx_);
3137 if (size == 64 && target->abiversion() < 2)
3139 unsigned int dest_shndx;
3140 if (!target->symval_for_branch(symtab, gsym, this->object_,
3144 Address delta = to - from;
3145 if (delta + max_branch_offset >= 2 * max_branch_offset)
3147 if (stub_table == NULL)
3149 gold_warning(_("%s:%s: branch in non-executable section,"
3150 " no long branch stub for you"),
3151 this->object_->name().c_str(),
3152 this->object_->section_name(this->shndx_).c_str());
3155 bool save_res = (size == 64
3157 && gsym->source() == Symbol::IN_OUTPUT_DATA
3158 && gsym->output_data() == target->savres_section());
3159 ok = stub_table->add_long_branch_entry(this->object_,
3161 from, to, save_res);
3165 gold_debug(DEBUG_TARGET,
3166 "branch at %s:%s+%#lx\n"
3167 "can't reach stub attached to %s:%s",
3168 this->object_->name().c_str(),
3169 this->object_->section_name(this->shndx_).c_str(),
3170 (unsigned long) this->offset_,
3171 stub_table->relobj()->name().c_str(),
3172 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3177 // Relaxation hook. This is where we do stub generation.
3179 template<int size, bool big_endian>
3181 Target_powerpc<size, big_endian>::do_relax(int pass,
3182 const Input_objects*,
3183 Symbol_table* symtab,
3187 unsigned int prev_brlt_size = 0;
3191 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3193 && this->abiversion() < 2
3195 && !parameters->options().user_set_plt_thread_safe())
3197 static const char* const thread_starter[] =
3201 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3203 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3204 "mq_notify", "create_timer",
3209 "GOMP_parallel_start",
3210 "GOMP_parallel_loop_static",
3211 "GOMP_parallel_loop_static_start",
3212 "GOMP_parallel_loop_dynamic",
3213 "GOMP_parallel_loop_dynamic_start",
3214 "GOMP_parallel_loop_guided",
3215 "GOMP_parallel_loop_guided_start",
3216 "GOMP_parallel_loop_runtime",
3217 "GOMP_parallel_loop_runtime_start",
3218 "GOMP_parallel_sections",
3219 "GOMP_parallel_sections_start",
3224 if (parameters->options().shared())
3228 for (unsigned int i = 0;
3229 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3232 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3233 thread_safe = (sym != NULL
3235 && sym->in_real_elf());
3241 this->plt_thread_safe_ = thread_safe;
3246 this->stub_group_size_ = parameters->options().stub_group_size();
3247 bool no_size_errors = true;
3248 if (this->stub_group_size_ == 1)
3249 this->stub_group_size_ = 0x1c00000;
3250 else if (this->stub_group_size_ == -1)
3251 this->stub_group_size_ = -0x1e00000;
3253 no_size_errors = false;
3254 this->group_sections(layout, task, no_size_errors);
3256 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3258 this->branch_lookup_table_.clear();
3259 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3260 p != this->stub_tables_.end();
3263 (*p)->clear_stubs(true);
3265 this->stub_tables_.clear();
3266 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3267 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3268 program_name, this->stub_group_size_);
3269 this->group_sections(layout, task, true);
3272 // We need address of stub tables valid for make_stub.
3273 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3274 p != this->stub_tables_.end();
3277 const Powerpc_relobj<size, big_endian>* object
3278 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3279 Address off = object->get_output_section_offset((*p)->shndx());
3280 gold_assert(off != invalid_address);
3281 Output_section* os = (*p)->output_section();
3282 (*p)->set_address_and_size(os, off);
3287 // Clear plt call stubs, long branch stubs and branch lookup table.
3288 prev_brlt_size = this->branch_lookup_table_.size();
3289 this->branch_lookup_table_.clear();
3290 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3291 p != this->stub_tables_.end();
3294 (*p)->clear_stubs(false);
3298 // Build all the stubs.
3299 this->relax_failed_ = false;
3300 Stub_table<size, big_endian>* ifunc_stub_table
3301 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3302 Stub_table<size, big_endian>* one_stub_table
3303 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3304 for (typename Branches::const_iterator b = this->branch_info_.begin();
3305 b != this->branch_info_.end();
3308 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3309 && !this->relax_failed_)
3311 this->relax_failed_ = true;
3312 this->relax_fail_count_++;
3313 if (this->relax_fail_count_ < 3)
3318 // Did anything change size?
3319 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3320 bool again = num_huge_branches != prev_brlt_size;
3321 if (size == 64 && num_huge_branches != 0)
3322 this->make_brlt_section(layout);
3323 if (size == 64 && again)
3324 this->brlt_section_->set_current_size(num_huge_branches);
3326 typedef Unordered_set<Output_section*> Output_sections;
3327 Output_sections os_need_update;
3328 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3329 p != this->stub_tables_.end();
3332 if ((*p)->size_update())
3335 (*p)->add_eh_frame(layout);
3336 os_need_update.insert((*p)->output_section());
3340 // Set output section offsets for all input sections in an output
3341 // section that just changed size. Anything past the stubs will
3343 for (typename Output_sections::iterator p = os_need_update.begin();
3344 p != os_need_update.end();
3347 Output_section* os = *p;
3349 typedef Output_section::Input_section_list Input_section_list;
3350 for (Input_section_list::const_iterator i = os->input_sections().begin();
3351 i != os->input_sections().end();
3354 off = align_address(off, i->addralign());
3355 if (i->is_input_section() || i->is_relaxed_input_section())
3356 i->relobj()->set_section_offset(i->shndx(), off);
3357 if (i->is_relaxed_input_section())
3359 Stub_table<size, big_endian>* stub_table
3360 = static_cast<Stub_table<size, big_endian>*>(
3361 i->relaxed_input_section());
3362 Address stub_table_size = stub_table->set_address_and_size(os, off);
3363 off += stub_table_size;
3364 // After a few iterations, set current stub table size
3365 // as min size threshold, so later stub tables can only
3368 stub_table->set_min_size_threshold(stub_table_size);
3371 off += i->data_size();
3373 // If .branch_lt is part of this output section, then we have
3374 // just done the offset adjustment.
3375 os->clear_section_offsets_need_adjustment();
3380 && num_huge_branches != 0
3381 && parameters->options().output_is_position_independent())
3383 // Fill in the BRLT relocs.
3384 this->brlt_section_->reset_brlt_sizes();
3385 for (typename Branch_lookup_table::const_iterator p
3386 = this->branch_lookup_table_.begin();
3387 p != this->branch_lookup_table_.end();
3390 this->brlt_section_->add_reloc(p->first, p->second);
3392 this->brlt_section_->finalize_brlt_sizes();
3396 && (parameters->options().user_set_emit_stub_syms()
3397 ? parameters->options().emit_stub_syms()
3399 || parameters->options().output_is_position_independent()
3400 || parameters->options().emit_relocs())))
3402 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3403 p != this->stub_tables_.end();
3405 (*p)->define_stub_syms(symtab);
3407 if (this->glink_ != NULL)
3409 int stub_size = this->glink_->pltresolve_size;
3410 Address value = -stub_size;
3416 this->define_local(symtab, "__glink_PLTresolve",
3417 this->glink_, value, stub_size);
3420 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3427 template<int size, bool big_endian>
3429 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3430 unsigned char* oview,
3434 uint64_t address = plt->address();
3435 off_t len = plt->data_size();
3437 if (plt == this->glink_)
3439 // See Output_data_glink::do_write() for glink contents.
3442 gold_assert(parameters->doing_static_link());
3443 // Static linking may need stubs, to support ifunc and long
3444 // branches. We need to create an output section for
3445 // .eh_frame early in the link process, to have a place to
3446 // attach stub .eh_frame info. We also need to have
3447 // registered a CIE that matches the stub CIE. Both of
3448 // these requirements are satisfied by creating an FDE and
3449 // CIE for .glink, even though static linking will leave
3450 // .glink zero length.
3451 // ??? Hopefully generating an FDE with a zero address range
3452 // won't confuse anything that consumes .eh_frame info.
3454 else if (size == 64)
3456 // There is one word before __glink_PLTresolve
3460 else if (parameters->options().output_is_position_independent())
3462 // There are two FDEs for a position independent glink.
3463 // The first covers the branch table, the second
3464 // __glink_PLTresolve at the end of glink.
3465 off_t resolve_size = this->glink_->pltresolve_size;
3466 if (oview[9] == elfcpp::DW_CFA_nop)
3467 len -= resolve_size;
3470 address += len - resolve_size;
3477 // Must be a stub table.
3478 const Stub_table<size, big_endian>* stub_table
3479 = static_cast<const Stub_table<size, big_endian>*>(plt);
3480 uint64_t stub_address = stub_table->stub_address();
3481 len -= stub_address - address;
3482 address = stub_address;
3485 *paddress = address;
3489 // A class to handle the PLT data.
3491 template<int size, bool big_endian>
3492 class Output_data_plt_powerpc : public Output_section_data_build
3495 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3496 size, big_endian> Reloc_section;
3498 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
3499 Reloc_section* plt_rel,
3501 : Output_section_data_build(size == 32 ? 4 : 8),
3507 // Add an entry to the PLT.
3512 add_ifunc_entry(Symbol*);
3515 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3517 // Return the .rela.plt section data.
3524 // Return the number of PLT entries.
3528 if (this->current_data_size() == 0)
3530 return ((this->current_data_size() - this->first_plt_entry_offset())
3531 / this->plt_entry_size());
3536 do_adjust_output_section(Output_section* os)
3541 // Write to a map file.
3543 do_print_to_mapfile(Mapfile* mapfile) const
3544 { mapfile->print_output_data(this, this->name_); }
3547 // Return the offset of the first non-reserved PLT entry.
3549 first_plt_entry_offset() const
3551 // IPLT has no reserved entry.
3552 if (this->name_[3] == 'I')
3554 return this->targ_->first_plt_entry_offset();
3557 // Return the size of each PLT entry.
3559 plt_entry_size() const
3561 return this->targ_->plt_entry_size();
3564 // Write out the PLT data.
3566 do_write(Output_file*);
3568 // The reloc section.
3569 Reloc_section* rel_;
3570 // Allows access to .glink for do_write.
3571 Target_powerpc<size, big_endian>* targ_;
3572 // What to report in map file.
3576 // Add an entry to the PLT.
3578 template<int size, bool big_endian>
3580 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
3582 if (!gsym->has_plt_offset())
3584 section_size_type off = this->current_data_size();
3586 off += this->first_plt_entry_offset();
3587 gsym->set_plt_offset(off);
3588 gsym->set_needs_dynsym_entry();
3589 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3590 this->rel_->add_global(gsym, dynrel, this, off, 0);
3591 off += this->plt_entry_size();
3592 this->set_current_data_size(off);
3596 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3598 template<int size, bool big_endian>
3600 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
3602 if (!gsym->has_plt_offset())
3604 section_size_type off = this->current_data_size();
3605 gsym->set_plt_offset(off);
3606 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3607 if (size == 64 && this->targ_->abiversion() < 2)
3608 dynrel = elfcpp::R_PPC64_JMP_IREL;
3609 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
3610 off += this->plt_entry_size();
3611 this->set_current_data_size(off);
3615 // Add an entry for a local ifunc symbol to the IPLT.
3617 template<int size, bool big_endian>
3619 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
3620 Sized_relobj_file<size, big_endian>* relobj,
3621 unsigned int local_sym_index)
3623 if (!relobj->local_has_plt_offset(local_sym_index))
3625 section_size_type off = this->current_data_size();
3626 relobj->set_local_plt_offset(local_sym_index, off);
3627 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3628 if (size == 64 && this->targ_->abiversion() < 2)
3629 dynrel = elfcpp::R_PPC64_JMP_IREL;
3630 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
3632 off += this->plt_entry_size();
3633 this->set_current_data_size(off);
3637 static const uint32_t add_0_11_11 = 0x7c0b5a14;
3638 static const uint32_t add_2_2_11 = 0x7c425a14;
3639 static const uint32_t add_2_2_12 = 0x7c426214;
3640 static const uint32_t add_3_3_2 = 0x7c631214;
3641 static const uint32_t add_3_3_13 = 0x7c636a14;
3642 static const uint32_t add_11_0_11 = 0x7d605a14;
3643 static const uint32_t add_11_2_11 = 0x7d625a14;
3644 static const uint32_t add_11_11_2 = 0x7d6b1214;
3645 static const uint32_t addi_0_12 = 0x380c0000;
3646 static const uint32_t addi_2_2 = 0x38420000;
3647 static const uint32_t addi_3_3 = 0x38630000;
3648 static const uint32_t addi_11_11 = 0x396b0000;
3649 static const uint32_t addi_12_1 = 0x39810000;
3650 static const uint32_t addi_12_12 = 0x398c0000;
3651 static const uint32_t addis_0_2 = 0x3c020000;
3652 static const uint32_t addis_0_13 = 0x3c0d0000;
3653 static const uint32_t addis_2_12 = 0x3c4c0000;
3654 static const uint32_t addis_11_2 = 0x3d620000;
3655 static const uint32_t addis_11_11 = 0x3d6b0000;
3656 static const uint32_t addis_11_30 = 0x3d7e0000;
3657 static const uint32_t addis_12_1 = 0x3d810000;
3658 static const uint32_t addis_12_2 = 0x3d820000;
3659 static const uint32_t addis_12_12 = 0x3d8c0000;
3660 static const uint32_t b = 0x48000000;
3661 static const uint32_t bcl_20_31 = 0x429f0005;
3662 static const uint32_t bctr = 0x4e800420;
3663 static const uint32_t blr = 0x4e800020;
3664 static const uint32_t bnectr_p4 = 0x4ce20420;
3665 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
3666 static const uint32_t cmpldi_2_0 = 0x28220000;
3667 static const uint32_t cror_15_15_15 = 0x4def7b82;
3668 static const uint32_t cror_31_31_31 = 0x4ffffb82;
3669 static const uint32_t ld_0_1 = 0xe8010000;
3670 static const uint32_t ld_0_12 = 0xe80c0000;
3671 static const uint32_t ld_2_1 = 0xe8410000;
3672 static const uint32_t ld_2_2 = 0xe8420000;
3673 static const uint32_t ld_2_11 = 0xe84b0000;
3674 static const uint32_t ld_2_12 = 0xe84c0000;
3675 static const uint32_t ld_11_2 = 0xe9620000;
3676 static const uint32_t ld_11_11 = 0xe96b0000;
3677 static const uint32_t ld_12_2 = 0xe9820000;
3678 static const uint32_t ld_12_11 = 0xe98b0000;
3679 static const uint32_t ld_12_12 = 0xe98c0000;
3680 static const uint32_t lfd_0_1 = 0xc8010000;
3681 static const uint32_t li_0_0 = 0x38000000;
3682 static const uint32_t li_12_0 = 0x39800000;
3683 static const uint32_t lis_0 = 0x3c000000;
3684 static const uint32_t lis_2 = 0x3c400000;
3685 static const uint32_t lis_11 = 0x3d600000;
3686 static const uint32_t lis_12 = 0x3d800000;
3687 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
3688 static const uint32_t lwz_0_12 = 0x800c0000;
3689 static const uint32_t lwz_11_11 = 0x816b0000;
3690 static const uint32_t lwz_11_30 = 0x817e0000;
3691 static const uint32_t lwz_12_12 = 0x818c0000;
3692 static const uint32_t lwzu_0_12 = 0x840c0000;
3693 static const uint32_t mflr_0 = 0x7c0802a6;
3694 static const uint32_t mflr_11 = 0x7d6802a6;
3695 static const uint32_t mflr_12 = 0x7d8802a6;
3696 static const uint32_t mtctr_0 = 0x7c0903a6;
3697 static const uint32_t mtctr_11 = 0x7d6903a6;
3698 static const uint32_t mtctr_12 = 0x7d8903a6;
3699 static const uint32_t mtlr_0 = 0x7c0803a6;
3700 static const uint32_t mtlr_12 = 0x7d8803a6;
3701 static const uint32_t nop = 0x60000000;
3702 static const uint32_t ori_0_0_0 = 0x60000000;
3703 static const uint32_t srdi_0_0_2 = 0x7800f082;
3704 static const uint32_t std_0_1 = 0xf8010000;
3705 static const uint32_t std_0_12 = 0xf80c0000;
3706 static const uint32_t std_2_1 = 0xf8410000;
3707 static const uint32_t stfd_0_1 = 0xd8010000;
3708 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
3709 static const uint32_t sub_11_11_12 = 0x7d6c5850;
3710 static const uint32_t sub_12_12_11 = 0x7d8b6050;
3711 static const uint32_t xor_2_12_12 = 0x7d826278;
3712 static const uint32_t xor_11_12_12 = 0x7d8b6278;
3714 // Write out the PLT.
3716 template<int size, bool big_endian>
3718 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
3720 if (size == 32 && this->name_[3] != 'I')
3722 const section_size_type offset = this->offset();
3723 const section_size_type oview_size
3724 = convert_to_section_size_type(this->data_size());
3725 unsigned char* const oview = of->get_output_view(offset, oview_size);
3726 unsigned char* pov = oview;
3727 unsigned char* endpov = oview + oview_size;
3729 // The address of the .glink branch table
3730 const Output_data_glink<size, big_endian>* glink
3731 = this->targ_->glink_section();
3732 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
3734 while (pov < endpov)
3736 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
3741 of->write_output_view(offset, oview_size, oview);
3745 // Create the PLT section.
3747 template<int size, bool big_endian>
3749 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
3752 if (this->plt_ == NULL)
3754 if (this->got_ == NULL)
3755 this->got_section(symtab, layout);
3757 if (this->glink_ == NULL)
3758 make_glink_section(layout);
3760 // Ensure that .rela.dyn always appears before .rela.plt This is
3761 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3762 // needs to include .rela.plt in its range.
3763 this->rela_dyn_section(layout);
3765 Reloc_section* plt_rel = new Reloc_section(false);
3766 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3767 elfcpp::SHF_ALLOC, plt_rel,
3768 ORDER_DYNAMIC_PLT_RELOCS, false);
3770 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
3772 layout->add_output_section_data(".plt",
3774 ? elfcpp::SHT_PROGBITS
3775 : elfcpp::SHT_NOBITS),
3776 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3783 Output_section* rela_plt_os = plt_rel->output_section();
3784 rela_plt_os->set_info_section(this->plt_->output_section());
3788 // Create the IPLT section.
3790 template<int size, bool big_endian>
3792 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
3795 if (this->iplt_ == NULL)
3797 this->make_plt_section(symtab, layout);
3799 Reloc_section* iplt_rel = new Reloc_section(false);
3800 if (this->rela_dyn_->output_section())
3801 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
3803 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
3805 if (this->plt_->output_section())
3806 this->plt_->output_section()->add_output_section_data(this->iplt_);
3810 // A section for huge long branch addresses, similar to plt section.
3812 template<int size, bool big_endian>
3813 class Output_data_brlt_powerpc : public Output_section_data_build
3816 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3817 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3818 size, big_endian> Reloc_section;
3820 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
3821 Reloc_section* brlt_rel)
3822 : Output_section_data_build(size == 32 ? 4 : 8),
3830 this->reset_data_size();
3831 this->rel_->reset_data_size();
3835 finalize_brlt_sizes()
3837 this->finalize_data_size();
3838 this->rel_->finalize_data_size();
3841 // Add a reloc for an entry in the BRLT.
3843 add_reloc(Address to, unsigned int off)
3844 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
3846 // Update section and reloc section size.
3848 set_current_size(unsigned int num_branches)
3850 this->reset_address_and_file_offset();
3851 this->set_current_data_size(num_branches * 16);
3852 this->finalize_data_size();
3853 Output_section* os = this->output_section();
3854 os->set_section_offsets_need_adjustment();
3855 if (this->rel_ != NULL)
3857 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
3858 this->rel_->reset_address_and_file_offset();
3859 this->rel_->set_current_data_size(num_branches * reloc_size);
3860 this->rel_->finalize_data_size();
3861 Output_section* os = this->rel_->output_section();
3862 os->set_section_offsets_need_adjustment();
3868 do_adjust_output_section(Output_section* os)
3873 // Write to a map file.
3875 do_print_to_mapfile(Mapfile* mapfile) const
3876 { mapfile->print_output_data(this, "** BRLT"); }
3879 // Write out the BRLT data.
3881 do_write(Output_file*);
3883 // The reloc section.
3884 Reloc_section* rel_;
3885 Target_powerpc<size, big_endian>* targ_;
3888 // Make the branch lookup table section.
3890 template<int size, bool big_endian>
3892 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
3894 if (size == 64 && this->brlt_section_ == NULL)
3896 Reloc_section* brlt_rel = NULL;
3897 bool is_pic = parameters->options().output_is_position_independent();
3900 // When PIC we can't fill in .branch_lt (like .plt it can be
3901 // a bss style section) but must initialise at runtime via
3902 // dynamic relocations.
3903 this->rela_dyn_section(layout);
3904 brlt_rel = new Reloc_section(false);
3905 if (this->rela_dyn_->output_section())
3906 this->rela_dyn_->output_section()
3907 ->add_output_section_data(brlt_rel);
3910 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
3911 if (this->plt_ && is_pic && this->plt_->output_section())
3912 this->plt_->output_section()
3913 ->add_output_section_data(this->brlt_section_);
3915 layout->add_output_section_data(".branch_lt",
3916 (is_pic ? elfcpp::SHT_NOBITS
3917 : elfcpp::SHT_PROGBITS),
3918 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3919 this->brlt_section_,
3920 (is_pic ? ORDER_SMALL_BSS
3921 : ORDER_SMALL_DATA),
3926 // Write out .branch_lt when non-PIC.
3928 template<int size, bool big_endian>
3930 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
3932 if (size == 64 && !parameters->options().output_is_position_independent())
3934 const section_size_type offset = this->offset();
3935 const section_size_type oview_size
3936 = convert_to_section_size_type(this->data_size());
3937 unsigned char* const oview = of->get_output_view(offset, oview_size);
3939 this->targ_->write_branch_lookup_table(oview);
3940 of->write_output_view(offset, oview_size, oview);
3944 static inline uint32_t
3950 static inline uint32_t
3956 static inline uint32_t
3959 return hi(a + 0x8000);
3965 static const unsigned char eh_frame_cie[12];
3969 const unsigned char Eh_cie<size>::eh_frame_cie[] =
3972 'z', 'R', 0, // Augmentation string.
3973 4, // Code alignment.
3974 0x80 - size / 8 , // Data alignment.
3976 1, // Augmentation size.
3977 (elfcpp::DW_EH_PE_pcrel
3978 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
3979 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
3982 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3983 static const unsigned char glink_eh_frame_fde_64v1[] =
3985 0, 0, 0, 0, // Replaced with offset to .glink.
3986 0, 0, 0, 0, // Replaced with size of .glink.
3987 0, // Augmentation size.
3988 elfcpp::DW_CFA_advance_loc + 1,
3989 elfcpp::DW_CFA_register, 65, 12,
3990 elfcpp::DW_CFA_advance_loc + 5,
3991 elfcpp::DW_CFA_restore_extended, 65
3994 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
3995 static const unsigned char glink_eh_frame_fde_64v2[] =
3997 0, 0, 0, 0, // Replaced with offset to .glink.
3998 0, 0, 0, 0, // Replaced with size of .glink.
3999 0, // Augmentation size.
4000 elfcpp::DW_CFA_advance_loc + 1,
4001 elfcpp::DW_CFA_register, 65, 0,
4002 elfcpp::DW_CFA_advance_loc + 7,
4003 elfcpp::DW_CFA_restore_extended, 65
4006 // Describe __glink_PLTresolve use of LR, 32-bit version.
4007 static const unsigned char glink_eh_frame_fde_32[] =
4009 0, 0, 0, 0, // Replaced with offset to .glink.
4010 0, 0, 0, 0, // Replaced with size of .glink.
4011 0, // Augmentation size.
4012 elfcpp::DW_CFA_advance_loc + 2,
4013 elfcpp::DW_CFA_register, 65, 0,
4014 elfcpp::DW_CFA_advance_loc + 4,
4015 elfcpp::DW_CFA_restore_extended, 65
4018 static const unsigned char default_fde[] =
4020 0, 0, 0, 0, // Replaced with offset to stubs.
4021 0, 0, 0, 0, // Replaced with size of stubs.
4022 0, // Augmentation size.
4023 elfcpp::DW_CFA_nop, // Pad.
4028 template<bool big_endian>
4030 write_insn(unsigned char* p, uint32_t v)
4032 elfcpp::Swap<32, big_endian>::writeval(p, v);
4035 // Stub_table holds information about plt and long branch stubs.
4036 // Stubs are built in an area following some input section determined
4037 // by group_sections(). This input section is converted to a relaxed
4038 // input section allowing it to be resized to accommodate the stubs
4040 template<int size, bool big_endian>
4041 class Stub_table : public Output_relaxed_input_section
4046 Plt_stub_ent(unsigned int off, unsigned int indx)
4047 : off_(off), indx_(indx), r2save_(0), localentry0_(0)
4051 unsigned int indx_ : 30;
4052 unsigned int r2save_ : 1;
4053 unsigned int localentry0_ : 1;
4055 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4056 static const Address invalid_address = static_cast<Address>(0) - 1;
4058 Stub_table(Target_powerpc<size, big_endian>* targ,
4059 Output_section* output_section,
4060 const Output_section::Input_section* owner,
4062 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4064 ->section_addralign(owner->shndx())),
4065 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4066 orig_data_size_(owner->current_data_size()),
4067 plt_size_(0), last_plt_size_(0),
4068 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4069 eh_frame_added_(false), need_save_res_(false), uniq_(id)
4071 this->set_output_section(output_section);
4073 std::vector<Output_relaxed_input_section*> new_relaxed;
4074 new_relaxed.push_back(this);
4075 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4078 // Add a plt call stub.
4080 add_plt_call_entry(Address,
4081 const Sized_relobj_file<size, big_endian>*,
4088 add_plt_call_entry(Address,
4089 const Sized_relobj_file<size, big_endian>*,
4095 // Find a given plt call stub.
4097 find_plt_call_entry(const Symbol*) const;
4100 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4101 unsigned int) const;
4104 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4110 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4115 // Add a long branch stub.
4117 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4118 unsigned int, Address, Address, bool);
4121 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4125 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4127 Address max_branch_offset = max_branch_delta(r_type);
4128 if (max_branch_offset == 0)
4130 gold_assert(from != invalid_address);
4131 Address loc = off + this->stub_address();
4132 return loc - from + max_branch_offset < 2 * max_branch_offset;
4136 clear_stubs(bool all)
4138 this->plt_call_stubs_.clear();
4139 this->plt_size_ = 0;
4140 this->long_branch_stubs_.clear();
4141 this->branch_size_ = 0;
4142 this->need_save_res_ = false;
4145 this->last_plt_size_ = 0;
4146 this->last_branch_size_ = 0;
4151 set_address_and_size(const Output_section* os, Address off)
4153 Address start_off = off;
4154 off += this->orig_data_size_;
4155 Address my_size = this->plt_size_ + this->branch_size_;
4156 if (this->need_save_res_)
4157 my_size += this->targ_->savres_section()->data_size();
4159 off = align_address(off, this->stub_align());
4160 // Include original section size and alignment padding in size
4161 my_size += off - start_off;
4162 // Ensure new size is always larger than min size
4163 // threshold. Alignment requirement is included in "my_size", so
4164 // increase "my_size" does not invalidate alignment.
4165 if (my_size < this->min_size_threshold_)
4166 my_size = this->min_size_threshold_;
4167 this->reset_address_and_file_offset();
4168 this->set_current_data_size(my_size);
4169 this->set_address_and_file_offset(os->address() + start_off,
4170 os->offset() + start_off);
4175 stub_address() const
4177 return align_address(this->address() + this->orig_data_size_,
4178 this->stub_align());
4184 return align_address(this->offset() + this->orig_data_size_,
4185 this->stub_align());
4190 { return this->plt_size_; }
4193 set_min_size_threshold(Address min_size)
4194 { this->min_size_threshold_ = min_size; }
4197 define_stub_syms(Symbol_table*);
4202 Output_section* os = this->output_section();
4203 if (os->addralign() < this->stub_align())
4205 os->set_addralign(this->stub_align());
4206 // FIXME: get rid of the insane checkpointing.
4207 // We can't increase alignment of the input section to which
4208 // stubs are attached; The input section may be .init which
4209 // is pasted together with other .init sections to form a
4210 // function. Aligning might insert zero padding resulting in
4211 // sigill. However we do need to increase alignment of the
4212 // output section so that the align_address() on offset in
4213 // set_address_and_size() adds the same padding as the
4214 // align_address() on address in stub_address().
4215 // What's more, we need this alignment for the layout done in
4216 // relaxation_loop_body() so that the output section starts at
4217 // a suitably aligned address.
4218 os->checkpoint_set_addralign(this->stub_align());
4220 if (this->last_plt_size_ != this->plt_size_
4221 || this->last_branch_size_ != this->branch_size_)
4223 this->last_plt_size_ = this->plt_size_;
4224 this->last_branch_size_ = this->branch_size_;
4230 // Add .eh_frame info for this stub section. Unlike other linker
4231 // generated .eh_frame this is added late in the link, because we
4232 // only want the .eh_frame info if this particular stub section is
4235 add_eh_frame(Layout* layout)
4237 if (!this->eh_frame_added_)
4239 if (!parameters->options().ld_generated_unwind_info())
4242 // Since we add stub .eh_frame info late, it must be placed
4243 // after all other linker generated .eh_frame info so that
4244 // merge mapping need not be updated for input sections.
4245 // There is no provision to use a different CIE to that used
4247 if (!this->targ_->has_glink())
4250 layout->add_eh_frame_for_plt(this,
4251 Eh_cie<size>::eh_frame_cie,
4252 sizeof (Eh_cie<size>::eh_frame_cie),
4254 sizeof (default_fde));
4255 this->eh_frame_added_ = true;
4259 Target_powerpc<size, big_endian>*
4265 class Plt_stub_key_hash;
4266 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4267 Plt_stub_key_hash> Plt_stub_entries;
4268 class Branch_stub_ent;
4269 class Branch_stub_ent_hash;
4270 typedef Unordered_map<Branch_stub_ent, unsigned int,
4271 Branch_stub_ent_hash> Branch_stub_entries;
4273 // Alignment of stub section.
4279 unsigned int min_align = 32;
4280 unsigned int user_align = 1 << parameters->options().plt_align();
4281 return std::max(user_align, min_align);
4284 // Return the plt offset for the given call stub.
4286 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
4288 const Symbol* gsym = p->first.sym_;
4291 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
4292 && gsym->can_use_relative_reloc(false));
4293 return gsym->plt_offset();
4298 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4299 unsigned int local_sym_index = p->first.locsym_;
4300 return relobj->local_plt_offset(local_sym_index);
4304 // Size of a given plt call stub.
4306 plt_call_size(typename Plt_stub_entries::const_iterator p) const
4312 Address plt_addr = this->plt_off(p, &is_iplt);
4314 plt_addr += this->targ_->iplt_section()->address();
4316 plt_addr += this->targ_->plt_section()->address();
4317 Address got_addr = this->targ_->got_section()->output_section()->address();
4318 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4319 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
4320 got_addr += ppcobj->toc_base_offset();
4321 Address off = plt_addr - got_addr;
4322 unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
4323 if (this->targ_->abiversion() < 2)
4325 bool static_chain = parameters->options().plt_static_chain();
4326 bool thread_safe = this->targ_->plt_thread_safe();
4330 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
4332 unsigned int align = 1 << parameters->options().plt_align();
4334 bytes = (bytes + align - 1) & -align;
4338 // Return long branch stub size.
4340 branch_stub_size(typename Branch_stub_entries::const_iterator p)
4342 Address loc = this->stub_address() + this->last_plt_size_ + p->second;
4343 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
4345 if (size == 64 || !parameters->options().output_is_position_independent())
4352 do_write(Output_file*);
4354 // Plt call stub keys.
4358 Plt_stub_key(const Symbol* sym)
4359 : sym_(sym), object_(0), addend_(0), locsym_(0)
4362 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4363 unsigned int locsym_index)
4364 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4367 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4369 unsigned int r_type,
4371 : sym_(sym), object_(0), addend_(0), locsym_(0)
4374 this->addend_ = addend;
4375 else if (parameters->options().output_is_position_independent()
4376 && r_type == elfcpp::R_PPC_PLTREL24)
4378 this->addend_ = addend;
4379 if (this->addend_ >= 32768)
4380 this->object_ = object;
4384 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4385 unsigned int locsym_index,
4386 unsigned int r_type,
4388 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4391 this->addend_ = addend;
4392 else if (parameters->options().output_is_position_independent()
4393 && r_type == elfcpp::R_PPC_PLTREL24)
4394 this->addend_ = addend;
4397 bool operator==(const Plt_stub_key& that) const
4399 return (this->sym_ == that.sym_
4400 && this->object_ == that.object_
4401 && this->addend_ == that.addend_
4402 && this->locsym_ == that.locsym_);
4406 const Sized_relobj_file<size, big_endian>* object_;
4407 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4408 unsigned int locsym_;
4411 class Plt_stub_key_hash
4414 size_t operator()(const Plt_stub_key& ent) const
4416 return (reinterpret_cast<uintptr_t>(ent.sym_)
4417 ^ reinterpret_cast<uintptr_t>(ent.object_)
4423 // Long branch stub keys.
4424 class Branch_stub_ent
4427 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj,
4428 Address to, bool save_res)
4429 : dest_(to), toc_base_off_(0), save_res_(save_res)
4432 toc_base_off_ = obj->toc_base_offset();
4435 bool operator==(const Branch_stub_ent& that) const
4437 return (this->dest_ == that.dest_
4439 || this->toc_base_off_ == that.toc_base_off_));
4443 unsigned int toc_base_off_;
4447 class Branch_stub_ent_hash
4450 size_t operator()(const Branch_stub_ent& ent) const
4451 { return ent.dest_ ^ ent.toc_base_off_; }
4454 // In a sane world this would be a global.
4455 Target_powerpc<size, big_endian>* targ_;
4456 // Map sym/object/addend to stub offset.
4457 Plt_stub_entries plt_call_stubs_;
4458 // Map destination address to stub offset.
4459 Branch_stub_entries long_branch_stubs_;
4460 // size of input section
4461 section_size_type orig_data_size_;
4463 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
4464 // Some rare cases cause (PR/20529) fluctuation in stub table
4465 // size, which leads to an endless relax loop. This is to be fixed
4466 // by, after the first few iterations, allowing only increase of
4467 // stub table size. This variable sets the minimal possible size of
4468 // a stub table, it is zero for the first few iterations, then
4469 // increases monotonically.
4470 Address min_size_threshold_;
4471 // Whether .eh_frame info has been created for this stub section.
4472 bool eh_frame_added_;
4473 // Set if this stub group needs a copy of out-of-line register
4474 // save/restore functions.
4475 bool need_save_res_;
4476 // Per stub table unique identifier.
4480 // Add a plt call stub, if we do not already have one for this
4481 // sym/object/addend combo.
4483 template<int size, bool big_endian>
4485 Stub_table<size, big_endian>::add_plt_call_entry(
4487 const Sized_relobj_file<size, big_endian>* object,
4489 unsigned int r_type,
4493 Plt_stub_key key(object, gsym, r_type, addend);
4494 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
4495 std::pair<typename Plt_stub_entries::iterator, bool> p
4496 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
4499 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
4501 && this->targ_->is_elfv2_localentry0(gsym))
4503 p.first->second.localentry0_ = 1;
4504 this->targ_->set_has_localentry0();
4509 && !p.first->second.localentry0_)
4510 p.first->second.r2save_ = 1;
4511 return this->can_reach_stub(from, ent.off_, r_type);
4514 template<int size, bool big_endian>
4516 Stub_table<size, big_endian>::add_plt_call_entry(
4518 const Sized_relobj_file<size, big_endian>* object,
4519 unsigned int locsym_index,
4520 unsigned int r_type,
4524 Plt_stub_key key(object, locsym_index, r_type, addend);
4525 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
4526 std::pair<typename Plt_stub_entries::iterator, bool> p
4527 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
4530 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
4532 && this->targ_->is_elfv2_localentry0(object, locsym_index))
4534 p.first->second.localentry0_ = 1;
4535 this->targ_->set_has_localentry0();
4540 && !p.first->second.localentry0_)
4541 p.first->second.r2save_ = 1;
4542 return this->can_reach_stub(from, ent.off_, r_type);
4545 // Find a plt call stub.
4547 template<int size, bool big_endian>
4548 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4549 Stub_table<size, big_endian>::find_plt_call_entry(
4550 const Sized_relobj_file<size, big_endian>* object,
4552 unsigned int r_type,
4553 Address addend) const
4555 Plt_stub_key key(object, gsym, r_type, addend);
4556 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4557 if (p == this->plt_call_stubs_.end())
4562 template<int size, bool big_endian>
4563 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4564 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
4566 Plt_stub_key key(gsym);
4567 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4568 if (p == this->plt_call_stubs_.end())
4573 template<int size, bool big_endian>
4574 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4575 Stub_table<size, big_endian>::find_plt_call_entry(
4576 const Sized_relobj_file<size, big_endian>* object,
4577 unsigned int locsym_index,
4578 unsigned int r_type,
4579 Address addend) const
4581 Plt_stub_key key(object, locsym_index, r_type, addend);
4582 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4583 if (p == this->plt_call_stubs_.end())
4588 template<int size, bool big_endian>
4589 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4590 Stub_table<size, big_endian>::find_plt_call_entry(
4591 const Sized_relobj_file<size, big_endian>* object,
4592 unsigned int locsym_index) const
4594 Plt_stub_key key(object, locsym_index);
4595 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4596 if (p == this->plt_call_stubs_.end())
4601 // Add a long branch stub if we don't already have one to given
4604 template<int size, bool big_endian>
4606 Stub_table<size, big_endian>::add_long_branch_entry(
4607 const Powerpc_relobj<size, big_endian>* object,
4608 unsigned int r_type,
4613 Branch_stub_ent ent(object, to, save_res);
4614 Address off = this->branch_size_;
4615 std::pair<typename Branch_stub_entries::iterator, bool> p
4616 = this->long_branch_stubs_.insert(std::make_pair(ent, off));
4620 this->need_save_res_ = true;
4623 unsigned int stub_size = this->branch_stub_size(p.first);
4624 this->branch_size_ = off + stub_size;
4625 if (size == 64 && stub_size != 4)
4626 this->targ_->add_branch_lookup_table(to);
4629 return this->can_reach_stub(from, off, r_type);
4632 // Find long branch stub offset.
4634 template<int size, bool big_endian>
4635 typename Stub_table<size, big_endian>::Address
4636 Stub_table<size, big_endian>::find_long_branch_entry(
4637 const Powerpc_relobj<size, big_endian>* object,
4640 Branch_stub_ent ent(object, to, false);
4641 typename Branch_stub_entries::const_iterator p
4642 = this->long_branch_stubs_.find(ent);
4643 if (p == this->long_branch_stubs_.end())
4644 return invalid_address;
4645 if (p->first.save_res_)
4646 return to - this->targ_->savres_section()->address() + this->branch_size_;
4650 // A class to handle .glink.
4652 template<int size, bool big_endian>
4653 class Output_data_glink : public Output_section_data
4656 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4657 static const Address invalid_address = static_cast<Address>(0) - 1;
4658 static const int pltresolve_size = 16*4;
4660 Output_data_glink(Target_powerpc<size, big_endian>* targ)
4661 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
4662 end_branch_table_(), ge_size_(0)
4666 add_eh_frame(Layout* layout);
4669 add_global_entry(const Symbol*);
4672 find_global_entry(const Symbol*) const;
4675 global_entry_address() const
4677 gold_assert(this->is_data_size_valid());
4678 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4679 return this->address() + global_entry_off;
4683 // Write to a map file.
4685 do_print_to_mapfile(Mapfile* mapfile) const
4686 { mapfile->print_output_data(this, _("** glink")); }
4690 set_final_data_size();
4694 do_write(Output_file*);
4696 // Allows access to .got and .plt for do_write.
4697 Target_powerpc<size, big_endian>* targ_;
4699 // Map sym to stub offset.
4700 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
4701 Global_entry_stub_entries global_entry_stubs_;
4703 unsigned int end_branch_table_, ge_size_;
4706 template<int size, bool big_endian>
4708 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
4710 if (!parameters->options().ld_generated_unwind_info())
4715 if (this->targ_->abiversion() < 2)
4716 layout->add_eh_frame_for_plt(this,
4717 Eh_cie<64>::eh_frame_cie,
4718 sizeof (Eh_cie<64>::eh_frame_cie),
4719 glink_eh_frame_fde_64v1,
4720 sizeof (glink_eh_frame_fde_64v1));
4722 layout->add_eh_frame_for_plt(this,
4723 Eh_cie<64>::eh_frame_cie,
4724 sizeof (Eh_cie<64>::eh_frame_cie),
4725 glink_eh_frame_fde_64v2,
4726 sizeof (glink_eh_frame_fde_64v2));
4730 // 32-bit .glink can use the default since the CIE return
4731 // address reg, LR, is valid.
4732 layout->add_eh_frame_for_plt(this,
4733 Eh_cie<32>::eh_frame_cie,
4734 sizeof (Eh_cie<32>::eh_frame_cie),
4736 sizeof (default_fde));
4737 // Except where LR is used in a PIC __glink_PLTresolve.
4738 if (parameters->options().output_is_position_independent())
4739 layout->add_eh_frame_for_plt(this,
4740 Eh_cie<32>::eh_frame_cie,
4741 sizeof (Eh_cie<32>::eh_frame_cie),
4742 glink_eh_frame_fde_32,
4743 sizeof (glink_eh_frame_fde_32));
4747 template<int size, bool big_endian>
4749 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
4751 std::pair<typename Global_entry_stub_entries::iterator, bool> p
4752 = this->global_entry_stubs_.insert(std::make_pair(gsym, this->ge_size_));
4754 this->ge_size_ += 16;
4757 template<int size, bool big_endian>
4758 typename Output_data_glink<size, big_endian>::Address
4759 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
4761 typename Global_entry_stub_entries::const_iterator p
4762 = this->global_entry_stubs_.find(gsym);
4763 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
4766 template<int size, bool big_endian>
4768 Output_data_glink<size, big_endian>::set_final_data_size()
4770 unsigned int count = this->targ_->plt_entry_count();
4771 section_size_type total = 0;
4777 // space for branch table
4778 total += 4 * (count - 1);
4780 total += -total & 15;
4781 total += this->pltresolve_size;
4785 total += this->pltresolve_size;
4787 // space for branch table
4789 if (this->targ_->abiversion() < 2)
4793 total += 4 * (count - 0x8000);
4797 this->end_branch_table_ = total;
4798 total = (total + 15) & -16;
4799 total += this->ge_size_;
4801 this->set_data_size(total);
4804 // Define symbols on stubs, identifying the stub.
4806 template<int size, bool big_endian>
4808 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
4810 if (!this->plt_call_stubs_.empty())
4812 // The key for the plt call stub hash table includes addresses,
4813 // therefore traversal order depends on those addresses, which
4814 // can change between runs if gold is a PIE. Unfortunately the
4815 // output .symtab ordering depends on the order in which symbols
4816 // are added to the linker symtab. We want reproducible output
4817 // so must sort the call stub symbols.
4818 typedef typename Plt_stub_entries::const_iterator plt_iter;
4819 std::vector<plt_iter> sorted;
4820 sorted.resize(this->plt_call_stubs_.size());
4822 for (plt_iter cs = this->plt_call_stubs_.begin();
4823 cs != this->plt_call_stubs_.end();
4825 sorted[cs->second.indx_] = cs;
4827 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
4829 plt_iter cs = sorted[i];
4832 if (cs->first.addend_ != 0)
4833 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
4836 if (cs->first.object_)
4838 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4839 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
4840 sprintf(obj, "%x:", ppcobj->uniq());
4843 const char *symname;
4844 if (cs->first.sym_ == NULL)
4846 sprintf(localname, "%x", cs->first.locsym_);
4847 symname = localname;
4850 symname = cs->first.sym_->name();
4851 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
4852 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
4854 = this->stub_address() - this->address() + cs->second.off_;
4855 unsigned int stub_size = this->plt_call_size(cs);
4856 this->targ_->define_local(symtab, name, this, value, stub_size);
4860 typedef typename Branch_stub_entries::const_iterator branch_iter;
4861 for (branch_iter bs = this->long_branch_stubs_.begin();
4862 bs != this->long_branch_stubs_.end();
4865 if (bs->first.save_res_)
4868 char* name = new char[8 + 13 + 16 + 1];
4869 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
4870 static_cast<unsigned long long>(bs->first.dest_));
4871 Address value = (this->stub_address() - this->address()
4872 + this->plt_size_ + bs->second);
4873 unsigned int stub_size = this->branch_stub_size(bs);
4874 this->targ_->define_local(symtab, name, this, value, stub_size);
4878 // Write out plt and long branch stub code.
4880 template<int size, bool big_endian>
4882 Stub_table<size, big_endian>::do_write(Output_file* of)
4884 if (this->plt_call_stubs_.empty()
4885 && this->long_branch_stubs_.empty())
4888 const section_size_type start_off = this->offset();
4889 const section_size_type off = this->stub_offset();
4890 const section_size_type oview_size =
4891 convert_to_section_size_type(this->data_size() - (off - start_off));
4892 unsigned char* const oview = of->get_output_view(off, oview_size);
4897 const Output_data_got_powerpc<size, big_endian>* got
4898 = this->targ_->got_section();
4899 Address got_os_addr = got->output_section()->address();
4901 if (!this->plt_call_stubs_.empty())
4903 // The base address of the .plt section.
4904 Address plt_base = this->targ_->plt_section()->address();
4905 Address iplt_base = invalid_address;
4907 // Write out plt call stubs.
4908 typename Plt_stub_entries::const_iterator cs;
4909 for (cs = this->plt_call_stubs_.begin();
4910 cs != this->plt_call_stubs_.end();
4914 Address pltoff = this->plt_off(cs, &is_iplt);
4915 Address plt_addr = pltoff;
4918 if (iplt_base == invalid_address)
4919 iplt_base = this->targ_->iplt_section()->address();
4920 plt_addr += iplt_base;
4923 plt_addr += plt_base;
4924 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4925 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
4926 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
4927 Address off = plt_addr - got_addr;
4929 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
4930 gold_error(_("%s: linkage table error against `%s'"),
4931 cs->first.object_->name().c_str(),
4932 cs->first.sym_->demangled_name().c_str());
4934 bool plt_load_toc = this->targ_->abiversion() < 2;
4936 = plt_load_toc && parameters->options().plt_static_chain();
4938 = plt_load_toc && this->targ_->plt_thread_safe();
4939 bool use_fake_dep = false;
4940 Address cmp_branch_off = 0;
4943 unsigned int pltindex
4944 = ((pltoff - this->targ_->first_plt_entry_offset())
4945 / this->targ_->plt_entry_size());
4947 = (this->targ_->glink_section()->pltresolve_size
4949 if (pltindex > 32768)
4950 glinkoff += (pltindex - 32768) * 4;
4952 = this->targ_->glink_section()->address() + glinkoff;
4954 = (this->stub_address() + cs->second.off_ + 20
4955 + 4 * cs->second.r2save_
4956 + 4 * (ha(off) != 0)
4957 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
4958 + 4 * static_chain);
4959 cmp_branch_off = to - from;
4960 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
4963 p = oview + cs->second.off_;
4966 if (cs->second.r2save_)
4968 write_insn<big_endian>(p,
4969 std_2_1 + this->targ_->stk_toc());
4974 write_insn<big_endian>(p, addis_11_2 + ha(off));
4976 write_insn<big_endian>(p, ld_12_11 + l(off));
4981 write_insn<big_endian>(p, addis_12_2 + ha(off));
4983 write_insn<big_endian>(p, ld_12_12 + l(off));
4987 && ha(off + 8 + 8 * static_chain) != ha(off))
4989 write_insn<big_endian>(p, addi_11_11 + l(off));
4993 write_insn<big_endian>(p, mtctr_12);
4999 write_insn<big_endian>(p, xor_2_12_12);
5001 write_insn<big_endian>(p, add_11_11_2);
5004 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
5008 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
5015 if (cs->second.r2save_)
5017 write_insn<big_endian>(p,
5018 std_2_1 + this->targ_->stk_toc());
5021 write_insn<big_endian>(p, ld_12_2 + l(off));
5024 && ha(off + 8 + 8 * static_chain) != ha(off))
5026 write_insn<big_endian>(p, addi_2_2 + l(off));
5030 write_insn<big_endian>(p, mtctr_12);
5036 write_insn<big_endian>(p, xor_11_12_12);
5038 write_insn<big_endian>(p, add_2_2_11);
5043 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
5046 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
5050 if (thread_safe && !use_fake_dep)
5052 write_insn<big_endian>(p, cmpldi_2_0);
5054 write_insn<big_endian>(p, bnectr_p4);
5056 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
5059 write_insn<big_endian>(p, bctr);
5063 // Write out long branch stubs.
5064 typename Branch_stub_entries::const_iterator bs;
5065 for (bs = this->long_branch_stubs_.begin();
5066 bs != this->long_branch_stubs_.end();
5069 if (bs->first.save_res_)
5071 p = oview + this->plt_size_ + bs->second;
5072 Address loc = this->stub_address() + this->plt_size_ + bs->second;
5073 Address delta = bs->first.dest_ - loc;
5074 if (delta + (1 << 25) < 2 << 25)
5075 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
5079 = this->targ_->find_branch_lookup_table(bs->first.dest_);
5080 gold_assert(brlt_addr != invalid_address);
5081 brlt_addr += this->targ_->brlt_section()->address();
5082 Address got_addr = got_os_addr + bs->first.toc_base_off_;
5083 Address brltoff = brlt_addr - got_addr;
5084 if (ha(brltoff) == 0)
5086 write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4;
5090 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
5091 write_insn<big_endian>(p, ld_12_12 + l(brltoff)), p += 4;
5093 write_insn<big_endian>(p, mtctr_12), p += 4;
5094 write_insn<big_endian>(p, bctr);
5100 if (!this->plt_call_stubs_.empty())
5102 // The base address of the .plt section.
5103 Address plt_base = this->targ_->plt_section()->address();
5104 Address iplt_base = invalid_address;
5105 // The address of _GLOBAL_OFFSET_TABLE_.
5106 Address g_o_t = invalid_address;
5108 // Write out plt call stubs.
5109 typename Plt_stub_entries::const_iterator cs;
5110 for (cs = this->plt_call_stubs_.begin();
5111 cs != this->plt_call_stubs_.end();
5115 Address plt_addr = this->plt_off(cs, &is_iplt);
5118 if (iplt_base == invalid_address)
5119 iplt_base = this->targ_->iplt_section()->address();
5120 plt_addr += iplt_base;
5123 plt_addr += plt_base;
5125 p = oview + cs->second.off_;
5126 if (parameters->options().output_is_position_independent())
5129 const Powerpc_relobj<size, big_endian>* ppcobj
5130 = (static_cast<const Powerpc_relobj<size, big_endian>*>
5131 (cs->first.object_));
5132 if (ppcobj != NULL && cs->first.addend_ >= 32768)
5134 unsigned int got2 = ppcobj->got2_shndx();
5135 got_addr = ppcobj->get_output_section_offset(got2);
5136 gold_assert(got_addr != invalid_address);
5137 got_addr += (ppcobj->output_section(got2)->address()
5138 + cs->first.addend_);
5142 if (g_o_t == invalid_address)
5144 const Output_data_got_powerpc<size, big_endian>* got
5145 = this->targ_->got_section();
5146 g_o_t = got->address() + got->g_o_t();
5151 Address off = plt_addr - got_addr;
5154 write_insn<big_endian>(p + 0, lwz_11_30 + l(off));
5155 write_insn<big_endian>(p + 4, mtctr_11);
5156 write_insn<big_endian>(p + 8, bctr);
5160 write_insn<big_endian>(p + 0, addis_11_30 + ha(off));
5161 write_insn<big_endian>(p + 4, lwz_11_11 + l(off));
5162 write_insn<big_endian>(p + 8, mtctr_11);
5163 write_insn<big_endian>(p + 12, bctr);
5168 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
5169 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
5170 write_insn<big_endian>(p + 8, mtctr_11);
5171 write_insn<big_endian>(p + 12, bctr);
5176 // Write out long branch stubs.
5177 typename Branch_stub_entries::const_iterator bs;
5178 for (bs = this->long_branch_stubs_.begin();
5179 bs != this->long_branch_stubs_.end();
5182 if (bs->first.save_res_)
5184 p = oview + this->plt_size_ + bs->second;
5185 Address loc = this->stub_address() + this->plt_size_ + bs->second;
5186 Address delta = bs->first.dest_ - loc;
5187 if (delta + (1 << 25) < 2 << 25)
5188 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
5189 else if (!parameters->options().output_is_position_independent())
5191 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
5192 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
5193 write_insn<big_endian>(p + 8, mtctr_12);
5194 write_insn<big_endian>(p + 12, bctr);
5199 write_insn<big_endian>(p + 0, mflr_0);
5200 write_insn<big_endian>(p + 4, bcl_20_31);
5201 write_insn<big_endian>(p + 8, mflr_12);
5202 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
5203 write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
5204 write_insn<big_endian>(p + 20, mtlr_0);
5205 write_insn<big_endian>(p + 24, mtctr_12);
5206 write_insn<big_endian>(p + 28, bctr);
5210 if (this->need_save_res_)
5212 p = oview + this->plt_size_ + this->branch_size_;
5213 memcpy (p, this->targ_->savres_section()->contents(),
5214 this->targ_->savres_section()->data_size());
5218 // Write out .glink.
5220 template<int size, bool big_endian>
5222 Output_data_glink<size, big_endian>::do_write(Output_file* of)
5224 const section_size_type off = this->offset();
5225 const section_size_type oview_size =
5226 convert_to_section_size_type(this->data_size());
5227 unsigned char* const oview = of->get_output_view(off, oview_size);
5230 // The base address of the .plt section.
5231 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5232 Address plt_base = this->targ_->plt_section()->address();
5236 if (this->end_branch_table_ != 0)
5238 // Write pltresolve stub.
5240 Address after_bcl = this->address() + 16;
5241 Address pltoff = plt_base - after_bcl;
5243 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
5245 if (this->targ_->abiversion() < 2)
5247 write_insn<big_endian>(p, mflr_12), p += 4;
5248 write_insn<big_endian>(p, bcl_20_31), p += 4;
5249 write_insn<big_endian>(p, mflr_11), p += 4;
5250 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5251 write_insn<big_endian>(p, mtlr_12), p += 4;
5252 write_insn<big_endian>(p, add_11_2_11), p += 4;
5253 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5254 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
5255 write_insn<big_endian>(p, mtctr_12), p += 4;
5256 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
5260 write_insn<big_endian>(p, mflr_0), p += 4;
5261 write_insn<big_endian>(p, bcl_20_31), p += 4;
5262 write_insn<big_endian>(p, mflr_11), p += 4;
5263 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
5264 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5265 write_insn<big_endian>(p, mtlr_0), p += 4;
5266 write_insn<big_endian>(p, sub_12_12_11), p += 4;
5267 write_insn<big_endian>(p, add_11_2_11), p += 4;
5268 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
5269 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5270 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
5271 write_insn<big_endian>(p, mtctr_12), p += 4;
5272 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
5274 write_insn<big_endian>(p, bctr), p += 4;
5275 while (p < oview + this->pltresolve_size)
5276 write_insn<big_endian>(p, nop), p += 4;
5278 // Write lazy link call stubs.
5280 while (p < oview + this->end_branch_table_)
5282 if (this->targ_->abiversion() < 2)
5286 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
5290 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
5291 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
5294 uint32_t branch_off = 8 - (p - oview);
5295 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
5300 Address plt_base = this->targ_->plt_section()->address();
5301 Address iplt_base = invalid_address;
5302 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
5303 Address global_entry_base = this->address() + global_entry_off;
5304 typename Global_entry_stub_entries::const_iterator ge;
5305 for (ge = this->global_entry_stubs_.begin();
5306 ge != this->global_entry_stubs_.end();
5309 p = oview + global_entry_off + ge->second;
5310 Address plt_addr = ge->first->plt_offset();
5311 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
5312 && ge->first->can_use_relative_reloc(false))
5314 if (iplt_base == invalid_address)
5315 iplt_base = this->targ_->iplt_section()->address();
5316 plt_addr += iplt_base;
5319 plt_addr += plt_base;
5320 Address my_addr = global_entry_base + ge->second;
5321 Address off = plt_addr - my_addr;
5323 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
5324 gold_error(_("%s: linkage table error against `%s'"),
5325 ge->first->object()->name().c_str(),
5326 ge->first->demangled_name().c_str());
5328 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
5329 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
5330 write_insn<big_endian>(p, mtctr_12), p += 4;
5331 write_insn<big_endian>(p, bctr);
5336 const Output_data_got_powerpc<size, big_endian>* got
5337 = this->targ_->got_section();
5338 // The address of _GLOBAL_OFFSET_TABLE_.
5339 Address g_o_t = got->address() + got->g_o_t();
5341 // Write out pltresolve branch table.
5343 unsigned int the_end = oview_size - this->pltresolve_size;
5344 unsigned char* end_p = oview + the_end;
5345 while (p < end_p - 8 * 4)
5346 write_insn<big_endian>(p, b + end_p - p), p += 4;
5348 write_insn<big_endian>(p, nop), p += 4;
5350 // Write out pltresolve call stub.
5351 if (parameters->options().output_is_position_independent())
5353 Address res0_off = 0;
5354 Address after_bcl_off = the_end + 12;
5355 Address bcl_res0 = after_bcl_off - res0_off;
5357 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
5358 write_insn<big_endian>(p + 4, mflr_0);
5359 write_insn<big_endian>(p + 8, bcl_20_31);
5360 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
5361 write_insn<big_endian>(p + 16, mflr_12);
5362 write_insn<big_endian>(p + 20, mtlr_0);
5363 write_insn<big_endian>(p + 24, sub_11_11_12);
5365 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
5367 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
5368 if (ha(got_bcl) == ha(got_bcl + 4))
5370 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
5371 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
5375 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
5376 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
5378 write_insn<big_endian>(p + 40, mtctr_0);
5379 write_insn<big_endian>(p + 44, add_0_11_11);
5380 write_insn<big_endian>(p + 48, add_11_0_11);
5381 write_insn<big_endian>(p + 52, bctr);
5382 write_insn<big_endian>(p + 56, nop);
5383 write_insn<big_endian>(p + 60, nop);
5387 Address res0 = this->address();
5389 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
5390 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
5391 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5392 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
5394 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
5395 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
5396 write_insn<big_endian>(p + 16, mtctr_0);
5397 write_insn<big_endian>(p + 20, add_0_11_11);
5398 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5399 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
5401 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
5402 write_insn<big_endian>(p + 28, add_11_0_11);
5403 write_insn<big_endian>(p + 32, bctr);
5404 write_insn<big_endian>(p + 36, nop);
5405 write_insn<big_endian>(p + 40, nop);
5406 write_insn<big_endian>(p + 44, nop);
5407 write_insn<big_endian>(p + 48, nop);
5408 write_insn<big_endian>(p + 52, nop);
5409 write_insn<big_endian>(p + 56, nop);
5410 write_insn<big_endian>(p + 60, nop);
5415 of->write_output_view(off, oview_size, oview);
5419 // A class to handle linker generated save/restore functions.
5421 template<int size, bool big_endian>
5422 class Output_data_save_res : public Output_section_data_build
5425 Output_data_save_res(Symbol_table* symtab);
5427 const unsigned char*
5434 // Write to a map file.
5436 do_print_to_mapfile(Mapfile* mapfile) const
5437 { mapfile->print_output_data(this, _("** save/restore")); }
5440 do_write(Output_file*);
5443 // The maximum size of save/restore contents.
5444 static const unsigned int savres_max = 218*4;
5447 savres_define(Symbol_table* symtab,
5449 unsigned int lo, unsigned int hi,
5450 unsigned char* write_ent(unsigned char*, int),
5451 unsigned char* write_tail(unsigned char*, int));
5453 unsigned char *contents_;
5456 template<bool big_endian>
5457 static unsigned char*
5458 savegpr0(unsigned char* p, int r)
5460 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5461 write_insn<big_endian>(p, insn);
5465 template<bool big_endian>
5466 static unsigned char*
5467 savegpr0_tail(unsigned char* p, int r)
5469 p = savegpr0<big_endian>(p, r);
5470 uint32_t insn = std_0_1 + 16;
5471 write_insn<big_endian>(p, insn);
5473 write_insn<big_endian>(p, blr);
5477 template<bool big_endian>
5478 static unsigned char*
5479 restgpr0(unsigned char* p, int r)
5481 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5482 write_insn<big_endian>(p, insn);
5486 template<bool big_endian>
5487 static unsigned char*
5488 restgpr0_tail(unsigned char* p, int r)
5490 uint32_t insn = ld_0_1 + 16;
5491 write_insn<big_endian>(p, insn);
5493 p = restgpr0<big_endian>(p, r);
5494 write_insn<big_endian>(p, mtlr_0);
5498 p = restgpr0<big_endian>(p, 30);
5499 p = restgpr0<big_endian>(p, 31);
5501 write_insn<big_endian>(p, blr);
5505 template<bool big_endian>
5506 static unsigned char*
5507 savegpr1(unsigned char* p, int r)
5509 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5510 write_insn<big_endian>(p, insn);
5514 template<bool big_endian>
5515 static unsigned char*
5516 savegpr1_tail(unsigned char* p, int r)
5518 p = savegpr1<big_endian>(p, r);
5519 write_insn<big_endian>(p, blr);
5523 template<bool big_endian>
5524 static unsigned char*
5525 restgpr1(unsigned char* p, int r)
5527 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5528 write_insn<big_endian>(p, insn);
5532 template<bool big_endian>
5533 static unsigned char*
5534 restgpr1_tail(unsigned char* p, int r)
5536 p = restgpr1<big_endian>(p, r);
5537 write_insn<big_endian>(p, blr);
5541 template<bool big_endian>
5542 static unsigned char*
5543 savefpr(unsigned char* p, int r)
5545 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5546 write_insn<big_endian>(p, insn);
5550 template<bool big_endian>
5551 static unsigned char*
5552 savefpr0_tail(unsigned char* p, int r)
5554 p = savefpr<big_endian>(p, r);
5555 write_insn<big_endian>(p, std_0_1 + 16);
5557 write_insn<big_endian>(p, blr);
5561 template<bool big_endian>
5562 static unsigned char*
5563 restfpr(unsigned char* p, int r)
5565 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5566 write_insn<big_endian>(p, insn);
5570 template<bool big_endian>
5571 static unsigned char*
5572 restfpr0_tail(unsigned char* p, int r)
5574 write_insn<big_endian>(p, ld_0_1 + 16);
5576 p = restfpr<big_endian>(p, r);
5577 write_insn<big_endian>(p, mtlr_0);
5581 p = restfpr<big_endian>(p, 30);
5582 p = restfpr<big_endian>(p, 31);
5584 write_insn<big_endian>(p, blr);
5588 template<bool big_endian>
5589 static unsigned char*
5590 savefpr1_tail(unsigned char* p, int r)
5592 p = savefpr<big_endian>(p, r);
5593 write_insn<big_endian>(p, blr);
5597 template<bool big_endian>
5598 static unsigned char*
5599 restfpr1_tail(unsigned char* p, int r)
5601 p = restfpr<big_endian>(p, r);
5602 write_insn<big_endian>(p, blr);
5606 template<bool big_endian>
5607 static unsigned char*
5608 savevr(unsigned char* p, int r)
5610 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5611 write_insn<big_endian>(p, insn);
5613 insn = stvx_0_12_0 + (r << 21);
5614 write_insn<big_endian>(p, insn);
5618 template<bool big_endian>
5619 static unsigned char*
5620 savevr_tail(unsigned char* p, int r)
5622 p = savevr<big_endian>(p, r);
5623 write_insn<big_endian>(p, blr);
5627 template<bool big_endian>
5628 static unsigned char*
5629 restvr(unsigned char* p, int r)
5631 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5632 write_insn<big_endian>(p, insn);
5634 insn = lvx_0_12_0 + (r << 21);
5635 write_insn<big_endian>(p, insn);
5639 template<bool big_endian>
5640 static unsigned char*
5641 restvr_tail(unsigned char* p, int r)
5643 p = restvr<big_endian>(p, r);
5644 write_insn<big_endian>(p, blr);
5649 template<int size, bool big_endian>
5650 Output_data_save_res<size, big_endian>::Output_data_save_res(
5651 Symbol_table* symtab)
5652 : Output_section_data_build(4),
5655 this->savres_define(symtab,
5656 "_savegpr0_", 14, 31,
5657 savegpr0<big_endian>, savegpr0_tail<big_endian>);
5658 this->savres_define(symtab,
5659 "_restgpr0_", 14, 29,
5660 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5661 this->savres_define(symtab,
5662 "_restgpr0_", 30, 31,
5663 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5664 this->savres_define(symtab,
5665 "_savegpr1_", 14, 31,
5666 savegpr1<big_endian>, savegpr1_tail<big_endian>);
5667 this->savres_define(symtab,
5668 "_restgpr1_", 14, 31,
5669 restgpr1<big_endian>, restgpr1_tail<big_endian>);
5670 this->savres_define(symtab,
5671 "_savefpr_", 14, 31,
5672 savefpr<big_endian>, savefpr0_tail<big_endian>);
5673 this->savres_define(symtab,
5674 "_restfpr_", 14, 29,
5675 restfpr<big_endian>, restfpr0_tail<big_endian>);
5676 this->savres_define(symtab,
5677 "_restfpr_", 30, 31,
5678 restfpr<big_endian>, restfpr0_tail<big_endian>);
5679 this->savres_define(symtab,
5681 savefpr<big_endian>, savefpr1_tail<big_endian>);
5682 this->savres_define(symtab,
5684 restfpr<big_endian>, restfpr1_tail<big_endian>);
5685 this->savres_define(symtab,
5687 savevr<big_endian>, savevr_tail<big_endian>);
5688 this->savres_define(symtab,
5690 restvr<big_endian>, restvr_tail<big_endian>);
5693 template<int size, bool big_endian>
5695 Output_data_save_res<size, big_endian>::savres_define(
5696 Symbol_table* symtab,
5698 unsigned int lo, unsigned int hi,
5699 unsigned char* write_ent(unsigned char*, int),
5700 unsigned char* write_tail(unsigned char*, int))
5702 size_t len = strlen(name);
5703 bool writing = false;
5706 memcpy(sym, name, len);
5709 for (unsigned int i = lo; i <= hi; i++)
5711 sym[len + 0] = i / 10 + '0';
5712 sym[len + 1] = i % 10 + '0';
5713 Symbol* gsym = symtab->lookup(sym);
5714 bool refd = gsym != NULL && gsym->is_undefined();
5715 writing = writing || refd;
5718 if (this->contents_ == NULL)
5719 this->contents_ = new unsigned char[this->savres_max];
5721 section_size_type value = this->current_data_size();
5722 unsigned char* p = this->contents_ + value;
5724 p = write_ent(p, i);
5726 p = write_tail(p, i);
5727 section_size_type cur_size = p - this->contents_;
5728 this->set_current_data_size(cur_size);
5730 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
5731 this, value, cur_size - value,
5732 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
5733 elfcpp::STV_HIDDEN, 0, false, false);
5738 // Write out save/restore.
5740 template<int size, bool big_endian>
5742 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
5744 const section_size_type off = this->offset();
5745 const section_size_type oview_size =
5746 convert_to_section_size_type(this->data_size());
5747 unsigned char* const oview = of->get_output_view(off, oview_size);
5748 memcpy(oview, this->contents_, oview_size);
5749 of->write_output_view(off, oview_size, oview);
5753 // Create the glink section.
5755 template<int size, bool big_endian>
5757 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
5759 if (this->glink_ == NULL)
5761 this->glink_ = new Output_data_glink<size, big_endian>(this);
5762 this->glink_->add_eh_frame(layout);
5763 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
5764 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
5765 this->glink_, ORDER_TEXT, false);
5769 // Create a PLT entry for a global symbol.
5771 template<int size, bool big_endian>
5773 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
5777 if (gsym->type() == elfcpp::STT_GNU_IFUNC
5778 && gsym->can_use_relative_reloc(false))
5780 if (this->iplt_ == NULL)
5781 this->make_iplt_section(symtab, layout);
5782 this->iplt_->add_ifunc_entry(gsym);
5786 if (this->plt_ == NULL)
5787 this->make_plt_section(symtab, layout);
5788 this->plt_->add_entry(gsym);
5792 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5794 template<int size, bool big_endian>
5796 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
5797 Symbol_table* symtab,
5799 Sized_relobj_file<size, big_endian>* relobj,
5802 if (this->iplt_ == NULL)
5803 this->make_iplt_section(symtab, layout);
5804 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
5807 // Return the number of entries in the PLT.
5809 template<int size, bool big_endian>
5811 Target_powerpc<size, big_endian>::plt_entry_count() const
5813 if (this->plt_ == NULL)
5815 return this->plt_->entry_count();
5818 // Create a GOT entry for local dynamic __tls_get_addr calls.
5820 template<int size, bool big_endian>
5822 Target_powerpc<size, big_endian>::tlsld_got_offset(
5823 Symbol_table* symtab,
5825 Sized_relobj_file<size, big_endian>* object)
5827 if (this->tlsld_got_offset_ == -1U)
5829 gold_assert(symtab != NULL && layout != NULL && object != NULL);
5830 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
5831 Output_data_got_powerpc<size, big_endian>* got
5832 = this->got_section(symtab, layout);
5833 unsigned int got_offset = got->add_constant_pair(0, 0);
5834 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
5836 this->tlsld_got_offset_ = got_offset;
5838 return this->tlsld_got_offset_;
5841 // Get the Reference_flags for a particular relocation.
5843 template<int size, bool big_endian>
5845 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
5846 unsigned int r_type,
5847 const Target_powerpc* target)
5853 case elfcpp::R_POWERPC_NONE:
5854 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5855 case elfcpp::R_POWERPC_GNU_VTENTRY:
5856 case elfcpp::R_PPC64_TOC:
5857 // No symbol reference.
5860 case elfcpp::R_PPC64_ADDR64:
5861 case elfcpp::R_PPC64_UADDR64:
5862 case elfcpp::R_POWERPC_ADDR32:
5863 case elfcpp::R_POWERPC_UADDR32:
5864 case elfcpp::R_POWERPC_ADDR16:
5865 case elfcpp::R_POWERPC_UADDR16:
5866 case elfcpp::R_POWERPC_ADDR16_LO:
5867 case elfcpp::R_POWERPC_ADDR16_HI:
5868 case elfcpp::R_POWERPC_ADDR16_HA:
5869 ref = Symbol::ABSOLUTE_REF;
5872 case elfcpp::R_POWERPC_ADDR24:
5873 case elfcpp::R_POWERPC_ADDR14:
5874 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5875 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5876 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
5879 case elfcpp::R_PPC64_REL64:
5880 case elfcpp::R_POWERPC_REL32:
5881 case elfcpp::R_PPC_LOCAL24PC:
5882 case elfcpp::R_POWERPC_REL16:
5883 case elfcpp::R_POWERPC_REL16_LO:
5884 case elfcpp::R_POWERPC_REL16_HI:
5885 case elfcpp::R_POWERPC_REL16_HA:
5886 ref = Symbol::RELATIVE_REF;
5889 case elfcpp::R_POWERPC_REL24:
5890 case elfcpp::R_PPC_PLTREL24:
5891 case elfcpp::R_POWERPC_REL14:
5892 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5893 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5894 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
5897 case elfcpp::R_POWERPC_GOT16:
5898 case elfcpp::R_POWERPC_GOT16_LO:
5899 case elfcpp::R_POWERPC_GOT16_HI:
5900 case elfcpp::R_POWERPC_GOT16_HA:
5901 case elfcpp::R_PPC64_GOT16_DS:
5902 case elfcpp::R_PPC64_GOT16_LO_DS:
5903 case elfcpp::R_PPC64_TOC16:
5904 case elfcpp::R_PPC64_TOC16_LO:
5905 case elfcpp::R_PPC64_TOC16_HI:
5906 case elfcpp::R_PPC64_TOC16_HA:
5907 case elfcpp::R_PPC64_TOC16_DS:
5908 case elfcpp::R_PPC64_TOC16_LO_DS:
5909 ref = Symbol::RELATIVE_REF;
5912 case elfcpp::R_POWERPC_GOT_TPREL16:
5913 case elfcpp::R_POWERPC_TLS:
5914 ref = Symbol::TLS_REF;
5917 case elfcpp::R_POWERPC_COPY:
5918 case elfcpp::R_POWERPC_GLOB_DAT:
5919 case elfcpp::R_POWERPC_JMP_SLOT:
5920 case elfcpp::R_POWERPC_RELATIVE:
5921 case elfcpp::R_POWERPC_DTPMOD:
5923 // Not expected. We will give an error later.
5927 if (size == 64 && target->abiversion() < 2)
5928 ref |= Symbol::FUNC_DESC_ABI;
5932 // Report an unsupported relocation against a local symbol.
5934 template<int size, bool big_endian>
5936 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
5937 Sized_relobj_file<size, big_endian>* object,
5938 unsigned int r_type)
5940 gold_error(_("%s: unsupported reloc %u against local symbol"),
5941 object->name().c_str(), r_type);
5944 // We are about to emit a dynamic relocation of type R_TYPE. If the
5945 // dynamic linker does not support it, issue an error.
5947 template<int size, bool big_endian>
5949 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
5950 unsigned int r_type)
5952 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
5954 // These are the relocation types supported by glibc for both 32-bit
5955 // and 64-bit powerpc.
5958 case elfcpp::R_POWERPC_NONE:
5959 case elfcpp::R_POWERPC_RELATIVE:
5960 case elfcpp::R_POWERPC_GLOB_DAT:
5961 case elfcpp::R_POWERPC_DTPMOD:
5962 case elfcpp::R_POWERPC_DTPREL:
5963 case elfcpp::R_POWERPC_TPREL:
5964 case elfcpp::R_POWERPC_JMP_SLOT:
5965 case elfcpp::R_POWERPC_COPY:
5966 case elfcpp::R_POWERPC_IRELATIVE:
5967 case elfcpp::R_POWERPC_ADDR32:
5968 case elfcpp::R_POWERPC_UADDR32:
5969 case elfcpp::R_POWERPC_ADDR24:
5970 case elfcpp::R_POWERPC_ADDR16:
5971 case elfcpp::R_POWERPC_UADDR16:
5972 case elfcpp::R_POWERPC_ADDR16_LO:
5973 case elfcpp::R_POWERPC_ADDR16_HI:
5974 case elfcpp::R_POWERPC_ADDR16_HA:
5975 case elfcpp::R_POWERPC_ADDR14:
5976 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5977 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5978 case elfcpp::R_POWERPC_REL32:
5979 case elfcpp::R_POWERPC_REL24:
5980 case elfcpp::R_POWERPC_TPREL16:
5981 case elfcpp::R_POWERPC_TPREL16_LO:
5982 case elfcpp::R_POWERPC_TPREL16_HI:
5983 case elfcpp::R_POWERPC_TPREL16_HA:
5994 // These are the relocation types supported only on 64-bit.
5995 case elfcpp::R_PPC64_ADDR64:
5996 case elfcpp::R_PPC64_UADDR64:
5997 case elfcpp::R_PPC64_JMP_IREL:
5998 case elfcpp::R_PPC64_ADDR16_DS:
5999 case elfcpp::R_PPC64_ADDR16_LO_DS:
6000 case elfcpp::R_PPC64_ADDR16_HIGH:
6001 case elfcpp::R_PPC64_ADDR16_HIGHA:
6002 case elfcpp::R_PPC64_ADDR16_HIGHER:
6003 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6004 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6005 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6006 case elfcpp::R_PPC64_REL64:
6007 case elfcpp::R_POWERPC_ADDR30:
6008 case elfcpp::R_PPC64_TPREL16_DS:
6009 case elfcpp::R_PPC64_TPREL16_LO_DS:
6010 case elfcpp::R_PPC64_TPREL16_HIGH:
6011 case elfcpp::R_PPC64_TPREL16_HIGHA:
6012 case elfcpp::R_PPC64_TPREL16_HIGHER:
6013 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6014 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6015 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6026 // These are the relocation types supported only on 32-bit.
6027 // ??? glibc ld.so doesn't need to support these.
6028 case elfcpp::R_POWERPC_DTPREL16:
6029 case elfcpp::R_POWERPC_DTPREL16_LO:
6030 case elfcpp::R_POWERPC_DTPREL16_HI:
6031 case elfcpp::R_POWERPC_DTPREL16_HA:
6039 // This prevents us from issuing more than one error per reloc
6040 // section. But we can still wind up issuing more than one
6041 // error per object file.
6042 if (this->issued_non_pic_error_)
6044 gold_assert(parameters->options().output_is_position_independent());
6045 object->error(_("requires unsupported dynamic reloc; "
6046 "recompile with -fPIC"));
6047 this->issued_non_pic_error_ = true;
6051 // Return whether we need to make a PLT entry for a relocation of the
6052 // given type against a STT_GNU_IFUNC symbol.
6054 template<int size, bool big_endian>
6056 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
6057 Target_powerpc<size, big_endian>* target,
6058 Sized_relobj_file<size, big_endian>* object,
6059 unsigned int r_type,
6062 // In non-pic code any reference will resolve to the plt call stub
6063 // for the ifunc symbol.
6064 if ((size == 32 || target->abiversion() >= 2)
6065 && !parameters->options().output_is_position_independent())
6070 // Word size refs from data sections are OK, but don't need a PLT entry.
6071 case elfcpp::R_POWERPC_ADDR32:
6072 case elfcpp::R_POWERPC_UADDR32:
6077 case elfcpp::R_PPC64_ADDR64:
6078 case elfcpp::R_PPC64_UADDR64:
6083 // GOT refs are good, but also don't need a PLT entry.
6084 case elfcpp::R_POWERPC_GOT16:
6085 case elfcpp::R_POWERPC_GOT16_LO:
6086 case elfcpp::R_POWERPC_GOT16_HI:
6087 case elfcpp::R_POWERPC_GOT16_HA:
6088 case elfcpp::R_PPC64_GOT16_DS:
6089 case elfcpp::R_PPC64_GOT16_LO_DS:
6092 // Function calls are good, and these do need a PLT entry.
6093 case elfcpp::R_POWERPC_ADDR24:
6094 case elfcpp::R_POWERPC_ADDR14:
6095 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6096 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6097 case elfcpp::R_POWERPC_REL24:
6098 case elfcpp::R_PPC_PLTREL24:
6099 case elfcpp::R_POWERPC_REL14:
6100 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6101 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6108 // Anything else is a problem.
6109 // If we are building a static executable, the libc startup function
6110 // responsible for applying indirect function relocations is going
6111 // to complain about the reloc type.
6112 // If we are building a dynamic executable, we will have a text
6113 // relocation. The dynamic loader will set the text segment
6114 // writable and non-executable to apply text relocations. So we'll
6115 // segfault when trying to run the indirection function to resolve
6118 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
6119 object->name().c_str(), r_type);
6123 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6127 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
6129 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
6130 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
6131 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6132 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6133 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6134 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6135 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6136 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6137 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6138 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6139 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6140 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6141 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6142 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6143 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6144 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
6145 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
6146 /* Exclude lfqu by testing reloc. If relocs are ever
6147 defined for the reduced D field in psq_lu then those
6148 will need testing too. */
6149 && r_type != elfcpp::R_PPC64_TOC16_LO
6150 && r_type != elfcpp::R_POWERPC_GOT16_LO)
6151 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
6153 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
6154 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
6155 /* Exclude stfqu. psq_stu as above for psq_lu. */
6156 && r_type != elfcpp::R_PPC64_TOC16_LO
6157 && r_type != elfcpp::R_POWERPC_GOT16_LO)
6158 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
6159 && (insn & 1) == 0));
6162 // Scan a relocation for a local symbol.
6164 template<int size, bool big_endian>
6166 Target_powerpc<size, big_endian>::Scan::local(
6167 Symbol_table* symtab,
6169 Target_powerpc<size, big_endian>* target,
6170 Sized_relobj_file<size, big_endian>* object,
6171 unsigned int data_shndx,
6172 Output_section* output_section,
6173 const elfcpp::Rela<size, big_endian>& reloc,
6174 unsigned int r_type,
6175 const elfcpp::Sym<size, big_endian>& lsym,
6178 this->maybe_skip_tls_get_addr_call(r_type, NULL);
6180 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6181 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6183 this->expect_tls_get_addr_call();
6184 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6185 if (tls_type != tls::TLSOPT_NONE)
6186 this->skip_next_tls_get_addr_call();
6188 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6189 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6191 this->expect_tls_get_addr_call();
6192 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6193 if (tls_type != tls::TLSOPT_NONE)
6194 this->skip_next_tls_get_addr_call();
6197 Powerpc_relobj<size, big_endian>* ppc_object
6198 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6203 && data_shndx == ppc_object->opd_shndx()
6204 && r_type == elfcpp::R_PPC64_ADDR64)
6205 ppc_object->set_opd_discard(reloc.get_r_offset());
6209 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6210 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
6211 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
6213 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6214 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6215 r_type, r_sym, reloc.get_r_addend());
6216 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
6221 case elfcpp::R_POWERPC_NONE:
6222 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6223 case elfcpp::R_POWERPC_GNU_VTENTRY:
6224 case elfcpp::R_POWERPC_TLS:
6225 case elfcpp::R_PPC64_ENTRY:
6228 case elfcpp::R_PPC64_TOC:
6230 Output_data_got_powerpc<size, big_endian>* got
6231 = target->got_section(symtab, layout);
6232 if (parameters->options().output_is_position_independent())
6234 Address off = reloc.get_r_offset();
6236 && target->abiversion() < 2
6237 && data_shndx == ppc_object->opd_shndx()
6238 && ppc_object->get_opd_discard(off - 8))
6241 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6242 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
6243 rela_dyn->add_output_section_relative(got->output_section(),
6244 elfcpp::R_POWERPC_RELATIVE,
6246 object, data_shndx, off,
6247 symobj->toc_base_offset());
6252 case elfcpp::R_PPC64_ADDR64:
6253 case elfcpp::R_PPC64_UADDR64:
6254 case elfcpp::R_POWERPC_ADDR32:
6255 case elfcpp::R_POWERPC_UADDR32:
6256 case elfcpp::R_POWERPC_ADDR24:
6257 case elfcpp::R_POWERPC_ADDR16:
6258 case elfcpp::R_POWERPC_ADDR16_LO:
6259 case elfcpp::R_POWERPC_ADDR16_HI:
6260 case elfcpp::R_POWERPC_ADDR16_HA:
6261 case elfcpp::R_POWERPC_UADDR16:
6262 case elfcpp::R_PPC64_ADDR16_HIGH:
6263 case elfcpp::R_PPC64_ADDR16_HIGHA:
6264 case elfcpp::R_PPC64_ADDR16_HIGHER:
6265 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6266 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6267 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6268 case elfcpp::R_PPC64_ADDR16_DS:
6269 case elfcpp::R_PPC64_ADDR16_LO_DS:
6270 case elfcpp::R_POWERPC_ADDR14:
6271 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6272 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6273 // If building a shared library (or a position-independent
6274 // executable), we need to create a dynamic relocation for
6276 if (parameters->options().output_is_position_independent()
6277 || (size == 64 && is_ifunc && target->abiversion() < 2))
6279 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6281 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6282 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
6283 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
6285 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6286 : elfcpp::R_POWERPC_RELATIVE);
6287 rela_dyn->add_local_relative(object, r_sym, dynrel,
6288 output_section, data_shndx,
6289 reloc.get_r_offset(),
6290 reloc.get_r_addend(), false);
6292 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
6294 check_non_pic(object, r_type);
6295 rela_dyn->add_local(object, r_sym, r_type, output_section,
6296 data_shndx, reloc.get_r_offset(),
6297 reloc.get_r_addend());
6301 gold_assert(lsym.get_st_value() == 0);
6302 unsigned int shndx = lsym.get_st_shndx();
6304 shndx = object->adjust_sym_shndx(r_sym, shndx,
6307 object->error(_("section symbol %u has bad shndx %u"),
6310 rela_dyn->add_local_section(object, shndx, r_type,
6311 output_section, data_shndx,
6312 reloc.get_r_offset());
6317 case elfcpp::R_POWERPC_REL24:
6318 case elfcpp::R_PPC_PLTREL24:
6319 case elfcpp::R_PPC_LOCAL24PC:
6320 case elfcpp::R_POWERPC_REL14:
6321 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6322 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6325 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6326 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6327 r_type, r_sym, reloc.get_r_addend());
6331 case elfcpp::R_PPC64_TOCSAVE:
6332 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6333 // caller has already saved r2 and thus a plt call stub need not
6336 && target->mark_pltcall(ppc_object, data_shndx,
6337 reloc.get_r_offset() - 4, symtab))
6339 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6340 unsigned int shndx = lsym.get_st_shndx();
6342 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6344 object->error(_("tocsave symbol %u has bad shndx %u"),
6347 target->add_tocsave(ppc_object, shndx,
6348 lsym.get_st_value() + reloc.get_r_addend());
6352 case elfcpp::R_PPC64_REL64:
6353 case elfcpp::R_POWERPC_REL32:
6354 case elfcpp::R_POWERPC_REL16:
6355 case elfcpp::R_POWERPC_REL16_LO:
6356 case elfcpp::R_POWERPC_REL16_HI:
6357 case elfcpp::R_POWERPC_REL16_HA:
6358 case elfcpp::R_POWERPC_REL16DX_HA:
6359 case elfcpp::R_POWERPC_SECTOFF:
6360 case elfcpp::R_POWERPC_SECTOFF_LO:
6361 case elfcpp::R_POWERPC_SECTOFF_HI:
6362 case elfcpp::R_POWERPC_SECTOFF_HA:
6363 case elfcpp::R_PPC64_SECTOFF_DS:
6364 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6365 case elfcpp::R_POWERPC_TPREL16:
6366 case elfcpp::R_POWERPC_TPREL16_LO:
6367 case elfcpp::R_POWERPC_TPREL16_HI:
6368 case elfcpp::R_POWERPC_TPREL16_HA:
6369 case elfcpp::R_PPC64_TPREL16_DS:
6370 case elfcpp::R_PPC64_TPREL16_LO_DS:
6371 case elfcpp::R_PPC64_TPREL16_HIGH:
6372 case elfcpp::R_PPC64_TPREL16_HIGHA:
6373 case elfcpp::R_PPC64_TPREL16_HIGHER:
6374 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6375 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6376 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6377 case elfcpp::R_POWERPC_DTPREL16:
6378 case elfcpp::R_POWERPC_DTPREL16_LO:
6379 case elfcpp::R_POWERPC_DTPREL16_HI:
6380 case elfcpp::R_POWERPC_DTPREL16_HA:
6381 case elfcpp::R_PPC64_DTPREL16_DS:
6382 case elfcpp::R_PPC64_DTPREL16_LO_DS:
6383 case elfcpp::R_PPC64_DTPREL16_HIGH:
6384 case elfcpp::R_PPC64_DTPREL16_HIGHA:
6385 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6386 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6387 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6388 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6389 case elfcpp::R_PPC64_TLSGD:
6390 case elfcpp::R_PPC64_TLSLD:
6391 case elfcpp::R_PPC64_ADDR64_LOCAL:
6394 case elfcpp::R_POWERPC_GOT16:
6395 case elfcpp::R_POWERPC_GOT16_LO:
6396 case elfcpp::R_POWERPC_GOT16_HI:
6397 case elfcpp::R_POWERPC_GOT16_HA:
6398 case elfcpp::R_PPC64_GOT16_DS:
6399 case elfcpp::R_PPC64_GOT16_LO_DS:
6401 // The symbol requires a GOT entry.
6402 Output_data_got_powerpc<size, big_endian>* got
6403 = target->got_section(symtab, layout);
6404 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6406 if (!parameters->options().output_is_position_independent())
6409 && (size == 32 || target->abiversion() >= 2))
6410 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
6412 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
6414 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
6416 // If we are generating a shared object or a pie, this
6417 // symbol's GOT entry will be set by a dynamic relocation.
6419 off = got->add_constant(0);
6420 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
6422 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6424 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6425 : elfcpp::R_POWERPC_RELATIVE);
6426 rela_dyn->add_local_relative(object, r_sym, dynrel,
6427 got, off, 0, false);
6432 case elfcpp::R_PPC64_TOC16:
6433 case elfcpp::R_PPC64_TOC16_LO:
6434 case elfcpp::R_PPC64_TOC16_HI:
6435 case elfcpp::R_PPC64_TOC16_HA:
6436 case elfcpp::R_PPC64_TOC16_DS:
6437 case elfcpp::R_PPC64_TOC16_LO_DS:
6438 // We need a GOT section.
6439 target->got_section(symtab, layout);
6442 case elfcpp::R_POWERPC_GOT_TLSGD16:
6443 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6444 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6445 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6447 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6448 if (tls_type == tls::TLSOPT_NONE)
6450 Output_data_got_powerpc<size, big_endian>* got
6451 = target->got_section(symtab, layout);
6452 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6453 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6454 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
6455 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
6457 else if (tls_type == tls::TLSOPT_TO_LE)
6459 // no GOT relocs needed for Local Exec.
6466 case elfcpp::R_POWERPC_GOT_TLSLD16:
6467 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6468 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6469 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6471 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6472 if (tls_type == tls::TLSOPT_NONE)
6473 target->tlsld_got_offset(symtab, layout, object);
6474 else if (tls_type == tls::TLSOPT_TO_LE)
6476 // no GOT relocs needed for Local Exec.
6477 if (parameters->options().emit_relocs())
6479 Output_section* os = layout->tls_segment()->first_section();
6480 gold_assert(os != NULL);
6481 os->set_needs_symtab_index();
6489 case elfcpp::R_POWERPC_GOT_DTPREL16:
6490 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6491 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6492 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6494 Output_data_got_powerpc<size, big_endian>* got
6495 = target->got_section(symtab, layout);
6496 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6497 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
6501 case elfcpp::R_POWERPC_GOT_TPREL16:
6502 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6503 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6504 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6506 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
6507 if (tls_type == tls::TLSOPT_NONE)
6509 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6510 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
6512 Output_data_got_powerpc<size, big_endian>* got
6513 = target->got_section(symtab, layout);
6514 unsigned int off = got->add_constant(0);
6515 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
6517 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6518 rela_dyn->add_symbolless_local_addend(object, r_sym,
6519 elfcpp::R_POWERPC_TPREL,
6523 else if (tls_type == tls::TLSOPT_TO_LE)
6525 // no GOT relocs needed for Local Exec.
6533 unsupported_reloc_local(object, r_type);
6538 && parameters->options().toc_optimize())
6540 if (data_shndx == ppc_object->toc_shndx())
6543 if (r_type != elfcpp::R_PPC64_ADDR64
6544 || (is_ifunc && target->abiversion() < 2))
6546 else if (parameters->options().output_is_position_independent())
6552 unsigned int shndx = lsym.get_st_shndx();
6553 if (shndx >= elfcpp::SHN_LORESERVE
6554 && shndx != elfcpp::SHN_XINDEX)
6559 ppc_object->set_no_toc_opt(reloc.get_r_offset());
6562 enum {no_check, check_lo, check_ha} insn_check;
6566 insn_check = no_check;
6569 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6570 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6571 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6572 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6573 case elfcpp::R_POWERPC_GOT16_HA:
6574 case elfcpp::R_PPC64_TOC16_HA:
6575 insn_check = check_ha;
6578 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6579 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6580 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6581 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6582 case elfcpp::R_POWERPC_GOT16_LO:
6583 case elfcpp::R_PPC64_GOT16_LO_DS:
6584 case elfcpp::R_PPC64_TOC16_LO:
6585 case elfcpp::R_PPC64_TOC16_LO_DS:
6586 insn_check = check_lo;
6590 section_size_type slen;
6591 const unsigned char* view = NULL;
6592 if (insn_check != no_check)
6594 view = ppc_object->section_contents(data_shndx, &slen, false);
6595 section_size_type off =
6596 convert_to_section_size_type(reloc.get_r_offset()) & -4;
6599 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
6600 if (insn_check == check_lo
6601 ? !ok_lo_toc_insn(insn, r_type)
6602 : ((insn & ((0x3f << 26) | 0x1f << 16))
6603 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
6605 ppc_object->set_no_toc_opt();
6606 gold_warning(_("%s: toc optimization is not supported "
6607 "for %#08x instruction"),
6608 ppc_object->name().c_str(), insn);
6617 case elfcpp::R_PPC64_TOC16:
6618 case elfcpp::R_PPC64_TOC16_LO:
6619 case elfcpp::R_PPC64_TOC16_HI:
6620 case elfcpp::R_PPC64_TOC16_HA:
6621 case elfcpp::R_PPC64_TOC16_DS:
6622 case elfcpp::R_PPC64_TOC16_LO_DS:
6623 unsigned int shndx = lsym.get_st_shndx();
6624 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6626 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6627 if (is_ordinary && shndx == ppc_object->toc_shndx())
6629 Address dst_off = lsym.get_st_value() + reloc.get_r_offset();
6630 if (dst_off < ppc_object->section_size(shndx))
6633 if (r_type == elfcpp::R_PPC64_TOC16_HA)
6635 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
6637 // Need to check that the insn is a ld
6639 view = ppc_object->section_contents(data_shndx,
6642 section_size_type off =
6643 (convert_to_section_size_type(reloc.get_r_offset())
6644 + (big_endian ? -2 : 3));
6646 && (view[off] & (0x3f << 2)) == 58u << 2)
6650 ppc_object->set_no_toc_opt(dst_off);
6661 case elfcpp::R_POWERPC_REL32:
6662 if (ppc_object->got2_shndx() != 0
6663 && parameters->options().output_is_position_independent())
6665 unsigned int shndx = lsym.get_st_shndx();
6666 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6668 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6669 if (is_ordinary && shndx == ppc_object->got2_shndx()
6670 && (ppc_object->section_flags(data_shndx)
6671 & elfcpp::SHF_EXECINSTR) != 0)
6672 gold_error(_("%s: unsupported -mbss-plt code"),
6673 ppc_object->name().c_str());
6683 case elfcpp::R_POWERPC_GOT_TLSLD16:
6684 case elfcpp::R_POWERPC_GOT_TLSGD16:
6685 case elfcpp::R_POWERPC_GOT_TPREL16:
6686 case elfcpp::R_POWERPC_GOT_DTPREL16:
6687 case elfcpp::R_POWERPC_GOT16:
6688 case elfcpp::R_PPC64_GOT16_DS:
6689 case elfcpp::R_PPC64_TOC16:
6690 case elfcpp::R_PPC64_TOC16_DS:
6691 ppc_object->set_has_small_toc_reloc();
6697 // Report an unsupported relocation against a global symbol.
6699 template<int size, bool big_endian>
6701 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
6702 Sized_relobj_file<size, big_endian>* object,
6703 unsigned int r_type,
6706 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6707 object->name().c_str(), r_type, gsym->demangled_name().c_str());
6710 // Scan a relocation for a global symbol.
6712 template<int size, bool big_endian>
6714 Target_powerpc<size, big_endian>::Scan::global(
6715 Symbol_table* symtab,
6717 Target_powerpc<size, big_endian>* target,
6718 Sized_relobj_file<size, big_endian>* object,
6719 unsigned int data_shndx,
6720 Output_section* output_section,
6721 const elfcpp::Rela<size, big_endian>& reloc,
6722 unsigned int r_type,
6725 if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
6728 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6729 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6731 this->expect_tls_get_addr_call();
6732 const bool final = gsym->final_value_is_known();
6733 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6734 if (tls_type != tls::TLSOPT_NONE)
6735 this->skip_next_tls_get_addr_call();
6737 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6738 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6740 this->expect_tls_get_addr_call();
6741 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6742 if (tls_type != tls::TLSOPT_NONE)
6743 this->skip_next_tls_get_addr_call();
6746 Powerpc_relobj<size, big_endian>* ppc_object
6747 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6749 // A STT_GNU_IFUNC symbol may require a PLT entry.
6750 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
6751 bool pushed_ifunc = false;
6752 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
6754 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6755 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6756 r_type, r_sym, reloc.get_r_addend());
6757 target->make_plt_entry(symtab, layout, gsym);
6758 pushed_ifunc = true;
6763 case elfcpp::R_POWERPC_NONE:
6764 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6765 case elfcpp::R_POWERPC_GNU_VTENTRY:
6766 case elfcpp::R_PPC_LOCAL24PC:
6767 case elfcpp::R_POWERPC_TLS:
6768 case elfcpp::R_PPC64_ENTRY:
6771 case elfcpp::R_PPC64_TOC:
6773 Output_data_got_powerpc<size, big_endian>* got
6774 = target->got_section(symtab, layout);
6775 if (parameters->options().output_is_position_independent())
6777 Address off = reloc.get_r_offset();
6779 && data_shndx == ppc_object->opd_shndx()
6780 && ppc_object->get_opd_discard(off - 8))
6783 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6784 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
6785 if (data_shndx != ppc_object->opd_shndx())
6786 symobj = static_cast
6787 <Powerpc_relobj<size, big_endian>*>(gsym->object());
6788 rela_dyn->add_output_section_relative(got->output_section(),
6789 elfcpp::R_POWERPC_RELATIVE,
6791 object, data_shndx, off,
6792 symobj->toc_base_offset());
6797 case elfcpp::R_PPC64_ADDR64:
6799 && target->abiversion() < 2
6800 && data_shndx == ppc_object->opd_shndx()
6801 && (gsym->is_defined_in_discarded_section()
6802 || gsym->object() != object))
6804 ppc_object->set_opd_discard(reloc.get_r_offset());
6808 case elfcpp::R_PPC64_UADDR64:
6809 case elfcpp::R_POWERPC_ADDR32:
6810 case elfcpp::R_POWERPC_UADDR32:
6811 case elfcpp::R_POWERPC_ADDR24:
6812 case elfcpp::R_POWERPC_ADDR16:
6813 case elfcpp::R_POWERPC_ADDR16_LO:
6814 case elfcpp::R_POWERPC_ADDR16_HI:
6815 case elfcpp::R_POWERPC_ADDR16_HA:
6816 case elfcpp::R_POWERPC_UADDR16:
6817 case elfcpp::R_PPC64_ADDR16_HIGH:
6818 case elfcpp::R_PPC64_ADDR16_HIGHA:
6819 case elfcpp::R_PPC64_ADDR16_HIGHER:
6820 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6821 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6822 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6823 case elfcpp::R_PPC64_ADDR16_DS:
6824 case elfcpp::R_PPC64_ADDR16_LO_DS:
6825 case elfcpp::R_POWERPC_ADDR14:
6826 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6827 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6829 // Make a PLT entry if necessary.
6830 if (gsym->needs_plt_entry())
6832 // Since this is not a PC-relative relocation, we may be
6833 // taking the address of a function. In that case we need to
6834 // set the entry in the dynamic symbol table to the address of
6835 // the PLT call stub.
6836 bool need_ifunc_plt = false;
6837 if ((size == 32 || target->abiversion() >= 2)
6838 && gsym->is_from_dynobj()
6839 && !parameters->options().output_is_position_independent())
6841 gsym->set_needs_dynsym_value();
6842 need_ifunc_plt = true;
6844 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
6846 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6847 target->push_branch(ppc_object, data_shndx,
6848 reloc.get_r_offset(), r_type, r_sym,
6849 reloc.get_r_addend());
6850 target->make_plt_entry(symtab, layout, gsym);
6853 // Make a dynamic relocation if necessary.
6854 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
6855 || (size == 64 && is_ifunc && target->abiversion() < 2))
6857 if (!parameters->options().output_is_position_independent()
6858 && gsym->may_need_copy_reloc())
6860 target->copy_reloc(symtab, layout, object,
6861 data_shndx, output_section, gsym, reloc);
6863 else if ((((size == 32
6864 && r_type == elfcpp::R_POWERPC_ADDR32)
6866 && r_type == elfcpp::R_PPC64_ADDR64
6867 && target->abiversion() >= 2))
6868 && gsym->can_use_relative_reloc(false)
6869 && !(gsym->visibility() == elfcpp::STV_PROTECTED
6870 && parameters->options().shared()))
6872 && r_type == elfcpp::R_PPC64_ADDR64
6873 && target->abiversion() < 2
6874 && (gsym->can_use_relative_reloc(false)
6875 || data_shndx == ppc_object->opd_shndx())))
6877 Reloc_section* rela_dyn
6878 = target->rela_dyn_section(symtab, layout, is_ifunc);
6879 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6880 : elfcpp::R_POWERPC_RELATIVE);
6881 rela_dyn->add_symbolless_global_addend(
6882 gsym, dynrel, output_section, object, data_shndx,
6883 reloc.get_r_offset(), reloc.get_r_addend());
6887 Reloc_section* rela_dyn
6888 = target->rela_dyn_section(symtab, layout, is_ifunc);
6889 check_non_pic(object, r_type);
6890 rela_dyn->add_global(gsym, r_type, output_section,
6892 reloc.get_r_offset(),
6893 reloc.get_r_addend());
6896 && parameters->options().toc_optimize()
6897 && data_shndx == ppc_object->toc_shndx())
6898 ppc_object->set_no_toc_opt(reloc.get_r_offset());
6904 case elfcpp::R_PPC_PLTREL24:
6905 case elfcpp::R_POWERPC_REL24:
6908 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6909 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6910 r_type, r_sym, reloc.get_r_addend());
6911 if (gsym->needs_plt_entry()
6912 || (!gsym->final_value_is_known()
6913 && (gsym->is_undefined()
6914 || gsym->is_from_dynobj()
6915 || gsym->is_preemptible())))
6916 target->make_plt_entry(symtab, layout, gsym);
6920 case elfcpp::R_PPC64_REL64:
6921 case elfcpp::R_POWERPC_REL32:
6922 // Make a dynamic relocation if necessary.
6923 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
6925 if (!parameters->options().output_is_position_independent()
6926 && gsym->may_need_copy_reloc())
6928 target->copy_reloc(symtab, layout, object,
6929 data_shndx, output_section, gsym,
6934 Reloc_section* rela_dyn
6935 = target->rela_dyn_section(symtab, layout, is_ifunc);
6936 check_non_pic(object, r_type);
6937 rela_dyn->add_global(gsym, r_type, output_section, object,
6938 data_shndx, reloc.get_r_offset(),
6939 reloc.get_r_addend());
6944 case elfcpp::R_POWERPC_REL14:
6945 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6946 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6949 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6950 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6951 r_type, r_sym, reloc.get_r_addend());
6955 case elfcpp::R_PPC64_TOCSAVE:
6956 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6957 // caller has already saved r2 and thus a plt call stub need not
6960 && target->mark_pltcall(ppc_object, data_shndx,
6961 reloc.get_r_offset() - 4, symtab))
6963 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6965 unsigned int shndx = gsym->shndx(&is_ordinary);
6967 object->error(_("tocsave symbol %u has bad shndx %u"),
6971 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
6972 target->add_tocsave(ppc_object, shndx,
6973 sym->value() + reloc.get_r_addend());
6978 case elfcpp::R_POWERPC_REL16:
6979 case elfcpp::R_POWERPC_REL16_LO:
6980 case elfcpp::R_POWERPC_REL16_HI:
6981 case elfcpp::R_POWERPC_REL16_HA:
6982 case elfcpp::R_POWERPC_REL16DX_HA:
6983 case elfcpp::R_POWERPC_SECTOFF:
6984 case elfcpp::R_POWERPC_SECTOFF_LO:
6985 case elfcpp::R_POWERPC_SECTOFF_HI:
6986 case elfcpp::R_POWERPC_SECTOFF_HA:
6987 case elfcpp::R_PPC64_SECTOFF_DS:
6988 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6989 case elfcpp::R_POWERPC_TPREL16:
6990 case elfcpp::R_POWERPC_TPREL16_LO:
6991 case elfcpp::R_POWERPC_TPREL16_HI:
6992 case elfcpp::R_POWERPC_TPREL16_HA:
6993 case elfcpp::R_PPC64_TPREL16_DS:
6994 case elfcpp::R_PPC64_TPREL16_LO_DS:
6995 case elfcpp::R_PPC64_TPREL16_HIGH:
6996 case elfcpp::R_PPC64_TPREL16_HIGHA:
6997 case elfcpp::R_PPC64_TPREL16_HIGHER:
6998 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6999 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7000 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7001 case elfcpp::R_POWERPC_DTPREL16:
7002 case elfcpp::R_POWERPC_DTPREL16_LO:
7003 case elfcpp::R_POWERPC_DTPREL16_HI:
7004 case elfcpp::R_POWERPC_DTPREL16_HA:
7005 case elfcpp::R_PPC64_DTPREL16_DS:
7006 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7007 case elfcpp::R_PPC64_DTPREL16_HIGH:
7008 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7009 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7010 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7011 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7012 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7013 case elfcpp::R_PPC64_TLSGD:
7014 case elfcpp::R_PPC64_TLSLD:
7015 case elfcpp::R_PPC64_ADDR64_LOCAL:
7018 case elfcpp::R_POWERPC_GOT16:
7019 case elfcpp::R_POWERPC_GOT16_LO:
7020 case elfcpp::R_POWERPC_GOT16_HI:
7021 case elfcpp::R_POWERPC_GOT16_HA:
7022 case elfcpp::R_PPC64_GOT16_DS:
7023 case elfcpp::R_PPC64_GOT16_LO_DS:
7025 // The symbol requires a GOT entry.
7026 Output_data_got_powerpc<size, big_endian>* got;
7028 got = target->got_section(symtab, layout);
7029 if (gsym->final_value_is_known())
7032 && (size == 32 || target->abiversion() >= 2))
7033 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
7035 got->add_global(gsym, GOT_TYPE_STANDARD);
7037 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
7039 // If we are generating a shared object or a pie, this
7040 // symbol's GOT entry will be set by a dynamic relocation.
7041 unsigned int off = got->add_constant(0);
7042 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
7044 Reloc_section* rela_dyn
7045 = target->rela_dyn_section(symtab, layout, is_ifunc);
7047 if (gsym->can_use_relative_reloc(false)
7049 || target->abiversion() >= 2)
7050 && gsym->visibility() == elfcpp::STV_PROTECTED
7051 && parameters->options().shared()))
7053 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7054 : elfcpp::R_POWERPC_RELATIVE);
7055 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
7059 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
7060 rela_dyn->add_global(gsym, dynrel, got, off, 0);
7066 case elfcpp::R_PPC64_TOC16:
7067 case elfcpp::R_PPC64_TOC16_LO:
7068 case elfcpp::R_PPC64_TOC16_HI:
7069 case elfcpp::R_PPC64_TOC16_HA:
7070 case elfcpp::R_PPC64_TOC16_DS:
7071 case elfcpp::R_PPC64_TOC16_LO_DS:
7072 // We need a GOT section.
7073 target->got_section(symtab, layout);
7076 case elfcpp::R_POWERPC_GOT_TLSGD16:
7077 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7078 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7079 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7081 const bool final = gsym->final_value_is_known();
7082 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7083 if (tls_type == tls::TLSOPT_NONE)
7085 Output_data_got_powerpc<size, big_endian>* got
7086 = target->got_section(symtab, layout);
7087 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7088 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
7089 elfcpp::R_POWERPC_DTPMOD,
7090 elfcpp::R_POWERPC_DTPREL);
7092 else if (tls_type == tls::TLSOPT_TO_IE)
7094 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
7096 Output_data_got_powerpc<size, big_endian>* got
7097 = target->got_section(symtab, layout);
7098 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7099 if (gsym->is_undefined()
7100 || gsym->is_from_dynobj())
7102 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
7103 elfcpp::R_POWERPC_TPREL);
7107 unsigned int off = got->add_constant(0);
7108 gsym->set_got_offset(GOT_TYPE_TPREL, off);
7109 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
7110 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
7115 else if (tls_type == tls::TLSOPT_TO_LE)
7117 // no GOT relocs needed for Local Exec.
7124 case elfcpp::R_POWERPC_GOT_TLSLD16:
7125 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7126 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7127 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7129 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7130 if (tls_type == tls::TLSOPT_NONE)
7131 target->tlsld_got_offset(symtab, layout, object);
7132 else if (tls_type == tls::TLSOPT_TO_LE)
7134 // no GOT relocs needed for Local Exec.
7135 if (parameters->options().emit_relocs())
7137 Output_section* os = layout->tls_segment()->first_section();
7138 gold_assert(os != NULL);
7139 os->set_needs_symtab_index();
7147 case elfcpp::R_POWERPC_GOT_DTPREL16:
7148 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7149 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7150 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7152 Output_data_got_powerpc<size, big_endian>* got
7153 = target->got_section(symtab, layout);
7154 if (!gsym->final_value_is_known()
7155 && (gsym->is_from_dynobj()
7156 || gsym->is_undefined()
7157 || gsym->is_preemptible()))
7158 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
7159 target->rela_dyn_section(layout),
7160 elfcpp::R_POWERPC_DTPREL);
7162 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
7166 case elfcpp::R_POWERPC_GOT_TPREL16:
7167 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7168 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7169 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7171 const bool final = gsym->final_value_is_known();
7172 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7173 if (tls_type == tls::TLSOPT_NONE)
7175 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
7177 Output_data_got_powerpc<size, big_endian>* got
7178 = target->got_section(symtab, layout);
7179 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7180 if (gsym->is_undefined()
7181 || gsym->is_from_dynobj())
7183 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
7184 elfcpp::R_POWERPC_TPREL);
7188 unsigned int off = got->add_constant(0);
7189 gsym->set_got_offset(GOT_TYPE_TPREL, off);
7190 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
7191 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
7196 else if (tls_type == tls::TLSOPT_TO_LE)
7198 // no GOT relocs needed for Local Exec.
7206 unsupported_reloc_global(object, r_type, gsym);
7211 && parameters->options().toc_optimize())
7213 if (data_shndx == ppc_object->toc_shndx())
7216 if (r_type != elfcpp::R_PPC64_ADDR64
7217 || (is_ifunc && target->abiversion() < 2))
7219 else if (parameters->options().output_is_position_independent()
7220 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
7223 ppc_object->set_no_toc_opt(reloc.get_r_offset());
7226 enum {no_check, check_lo, check_ha} insn_check;
7230 insn_check = no_check;
7233 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7234 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7235 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7236 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7237 case elfcpp::R_POWERPC_GOT16_HA:
7238 case elfcpp::R_PPC64_TOC16_HA:
7239 insn_check = check_ha;
7242 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7243 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7244 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7245 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7246 case elfcpp::R_POWERPC_GOT16_LO:
7247 case elfcpp::R_PPC64_GOT16_LO_DS:
7248 case elfcpp::R_PPC64_TOC16_LO:
7249 case elfcpp::R_PPC64_TOC16_LO_DS:
7250 insn_check = check_lo;
7254 section_size_type slen;
7255 const unsigned char* view = NULL;
7256 if (insn_check != no_check)
7258 view = ppc_object->section_contents(data_shndx, &slen, false);
7259 section_size_type off =
7260 convert_to_section_size_type(reloc.get_r_offset()) & -4;
7263 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
7264 if (insn_check == check_lo
7265 ? !ok_lo_toc_insn(insn, r_type)
7266 : ((insn & ((0x3f << 26) | 0x1f << 16))
7267 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7269 ppc_object->set_no_toc_opt();
7270 gold_warning(_("%s: toc optimization is not supported "
7271 "for %#08x instruction"),
7272 ppc_object->name().c_str(), insn);
7281 case elfcpp::R_PPC64_TOC16:
7282 case elfcpp::R_PPC64_TOC16_LO:
7283 case elfcpp::R_PPC64_TOC16_HI:
7284 case elfcpp::R_PPC64_TOC16_HA:
7285 case elfcpp::R_PPC64_TOC16_DS:
7286 case elfcpp::R_PPC64_TOC16_LO_DS:
7287 if (gsym->source() == Symbol::FROM_OBJECT
7288 && !gsym->object()->is_dynamic())
7290 Powerpc_relobj<size, big_endian>* sym_object
7291 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
7293 unsigned int shndx = gsym->shndx(&is_ordinary);
7294 if (shndx == sym_object->toc_shndx())
7296 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
7297 Address dst_off = sym->value() + reloc.get_r_offset();
7298 if (dst_off < sym_object->section_size(shndx))
7301 if (r_type == elfcpp::R_PPC64_TOC16_HA)
7303 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
7305 // Need to check that the insn is a ld
7307 view = ppc_object->section_contents(data_shndx,
7310 section_size_type off =
7311 (convert_to_section_size_type(reloc.get_r_offset())
7312 + (big_endian ? -2 : 3));
7314 && (view[off] & (0x3f << 2)) == (58u << 2))
7318 sym_object->set_no_toc_opt(dst_off);
7330 case elfcpp::R_PPC_LOCAL24PC:
7331 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7332 gold_error(_("%s: unsupported -mbss-plt code"),
7333 ppc_object->name().c_str());
7342 case elfcpp::R_POWERPC_GOT_TLSLD16:
7343 case elfcpp::R_POWERPC_GOT_TLSGD16:
7344 case elfcpp::R_POWERPC_GOT_TPREL16:
7345 case elfcpp::R_POWERPC_GOT_DTPREL16:
7346 case elfcpp::R_POWERPC_GOT16:
7347 case elfcpp::R_PPC64_GOT16_DS:
7348 case elfcpp::R_PPC64_TOC16:
7349 case elfcpp::R_PPC64_TOC16_DS:
7350 ppc_object->set_has_small_toc_reloc();
7356 // Process relocations for gc.
7358 template<int size, bool big_endian>
7360 Target_powerpc<size, big_endian>::gc_process_relocs(
7361 Symbol_table* symtab,
7363 Sized_relobj_file<size, big_endian>* object,
7364 unsigned int data_shndx,
7366 const unsigned char* prelocs,
7368 Output_section* output_section,
7369 bool needs_special_offset_handling,
7370 size_t local_symbol_count,
7371 const unsigned char* plocal_symbols)
7373 typedef Target_powerpc<size, big_endian> Powerpc;
7374 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7377 Powerpc_relobj<size, big_endian>* ppc_object
7378 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7380 ppc_object->set_opd_valid();
7381 if (size == 64 && data_shndx == ppc_object->opd_shndx())
7383 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
7384 for (p = ppc_object->access_from_map()->begin();
7385 p != ppc_object->access_from_map()->end();
7388 Address dst_off = p->first;
7389 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
7390 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
7391 for (s = p->second.begin(); s != p->second.end(); ++s)
7393 Relobj* src_obj = s->first;
7394 unsigned int src_indx = s->second;
7395 symtab->gc()->add_reference(src_obj, src_indx,
7396 ppc_object, dst_indx);
7400 ppc_object->access_from_map()->clear();
7401 ppc_object->process_gc_mark(symtab);
7402 // Don't look at .opd relocs as .opd will reference everything.
7406 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
7415 needs_special_offset_handling,
7420 // Handle target specific gc actions when adding a gc reference from
7421 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7422 // and DST_OFF. For powerpc64, this adds a referenc to the code
7423 // section of a function descriptor.
7425 template<int size, bool big_endian>
7427 Target_powerpc<size, big_endian>::do_gc_add_reference(
7428 Symbol_table* symtab,
7430 unsigned int src_shndx,
7432 unsigned int dst_shndx,
7433 Address dst_off) const
7435 if (size != 64 || dst_obj->is_dynamic())
7438 Powerpc_relobj<size, big_endian>* ppc_object
7439 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
7440 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
7442 if (ppc_object->opd_valid())
7444 dst_shndx = ppc_object->get_opd_ent(dst_off);
7445 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
7449 // If we haven't run scan_opd_relocs, we must delay
7450 // processing this function descriptor reference.
7451 ppc_object->add_reference(src_obj, src_shndx, dst_off);
7456 // Add any special sections for this symbol to the gc work list.
7457 // For powerpc64, this adds the code section of a function
7460 template<int size, bool big_endian>
7462 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
7463 Symbol_table* symtab,
7468 Powerpc_relobj<size, big_endian>* ppc_object
7469 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
7471 unsigned int shndx = sym->shndx(&is_ordinary);
7472 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
7474 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
7475 Address dst_off = gsym->value();
7476 if (ppc_object->opd_valid())
7478 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
7479 symtab->gc()->worklist().push_back(Section_id(ppc_object,
7483 ppc_object->add_gc_mark(dst_off);
7488 // For a symbol location in .opd, set LOC to the location of the
7491 template<int size, bool big_endian>
7493 Target_powerpc<size, big_endian>::do_function_location(
7494 Symbol_location* loc) const
7496 if (size == 64 && loc->shndx != 0)
7498 if (loc->object->is_dynamic())
7500 Powerpc_dynobj<size, big_endian>* ppc_object
7501 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
7502 if (loc->shndx == ppc_object->opd_shndx())
7505 Address off = loc->offset - ppc_object->opd_address();
7506 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
7507 loc->offset = dest_off;
7512 const Powerpc_relobj<size, big_endian>* ppc_object
7513 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
7514 if (loc->shndx == ppc_object->opd_shndx())
7517 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
7518 loc->offset = dest_off;
7524 // FNOFFSET in section SHNDX in OBJECT is the start of a function
7525 // compiled with -fsplit-stack. The function calls non-split-stack
7526 // code. Change the function to ensure it has enough stack space to
7527 // call some random function.
7529 template<int size, bool big_endian>
7531 Target_powerpc<size, big_endian>::do_calls_non_split(
7534 section_offset_type fnoffset,
7535 section_size_type fnsize,
7536 const unsigned char* prelocs,
7538 unsigned char* view,
7539 section_size_type view_size,
7541 std::string* to) const
7543 // 32-bit not supported.
7547 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
7548 prelocs, reloc_count, view, view_size,
7553 // The function always starts with
7554 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
7555 // addis %r12,%r1,-allocate@ha
7556 // addi %r12,%r12,-allocate@l
7558 // but note that the addis or addi may be replaced with a nop
7560 unsigned char *entry = view + fnoffset;
7561 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
7563 if ((insn & 0xffff0000) == addis_2_12)
7565 /* Skip ELFv2 global entry code. */
7567 insn = elfcpp::Swap<32, big_endian>::readval(entry);
7570 unsigned char *pinsn = entry;
7572 const uint32_t ld_private_ss = 0xe80d8fc0;
7573 if (insn == ld_private_ss)
7575 int32_t allocate = 0;
7579 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
7580 if ((insn & 0xffff0000) == addis_12_1)
7581 allocate += (insn & 0xffff) << 16;
7582 else if ((insn & 0xffff0000) == addi_12_1
7583 || (insn & 0xffff0000) == addi_12_12)
7584 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
7585 else if (insn != nop)
7588 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
7590 int extra = parameters->options().split_stack_adjust_size();
7592 if (allocate >= 0 || extra < 0)
7594 object->error(_("split-stack stack size overflow at "
7595 "section %u offset %0zx"),
7596 shndx, static_cast<size_t>(fnoffset));
7600 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
7601 if (insn != addis_12_1)
7603 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7605 insn = addi_12_12 | (allocate & 0xffff);
7606 if (insn != addi_12_12)
7608 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7614 insn = addi_12_1 | (allocate & 0xffff);
7615 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7618 if (pinsn != entry + 12)
7619 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
7627 if (!object->has_no_split_stack())
7628 object->error(_("failed to match split-stack sequence at "
7629 "section %u offset %0zx"),
7630 shndx, static_cast<size_t>(fnoffset));
7634 // Scan relocations for a section.
7636 template<int size, bool big_endian>
7638 Target_powerpc<size, big_endian>::scan_relocs(
7639 Symbol_table* symtab,
7641 Sized_relobj_file<size, big_endian>* object,
7642 unsigned int data_shndx,
7643 unsigned int sh_type,
7644 const unsigned char* prelocs,
7646 Output_section* output_section,
7647 bool needs_special_offset_handling,
7648 size_t local_symbol_count,
7649 const unsigned char* plocal_symbols)
7651 typedef Target_powerpc<size, big_endian> Powerpc;
7652 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7655 if (!this->plt_localentry0_init_)
7657 bool plt_localentry0 = false;
7659 && this->abiversion() >= 2)
7661 if (parameters->options().user_set_plt_localentry())
7662 plt_localentry0 = parameters->options().plt_localentry();
7664 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
7665 gold_warning(_("--plt-localentry is especially dangerous without "
7666 "ld.so support to detect ABI violations"));
7668 this->plt_localentry0_ = plt_localentry0;
7669 this->plt_localentry0_init_ = true;
7672 if (sh_type == elfcpp::SHT_REL)
7674 gold_error(_("%s: unsupported REL reloc section"),
7675 object->name().c_str());
7679 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
7688 needs_special_offset_handling,
7693 // Functor class for processing the global symbol table.
7694 // Removes symbols defined on discarded opd entries.
7696 template<bool big_endian>
7697 class Global_symbol_visitor_opd
7700 Global_symbol_visitor_opd()
7704 operator()(Sized_symbol<64>* sym)
7706 if (sym->has_symtab_index()
7707 || sym->source() != Symbol::FROM_OBJECT
7708 || !sym->in_real_elf())
7711 if (sym->object()->is_dynamic())
7714 Powerpc_relobj<64, big_endian>* symobj
7715 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
7716 if (symobj->opd_shndx() == 0)
7720 unsigned int shndx = sym->shndx(&is_ordinary);
7721 if (shndx == symobj->opd_shndx()
7722 && symobj->get_opd_discard(sym->value()))
7724 sym->set_undefined();
7725 sym->set_visibility(elfcpp::STV_DEFAULT);
7726 sym->set_is_defined_in_discarded_section();
7727 sym->set_symtab_index(-1U);
7732 template<int size, bool big_endian>
7734 Target_powerpc<size, big_endian>::define_save_restore_funcs(
7736 Symbol_table* symtab)
7740 Output_data_save_res<size, big_endian>* savres
7741 = new Output_data_save_res<size, big_endian>(symtab);
7742 this->savres_section_ = savres;
7743 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7744 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7745 savres, ORDER_TEXT, false);
7749 // Sort linker created .got section first (for the header), then input
7750 // sections belonging to files using small model code.
7752 template<bool big_endian>
7753 class Sort_toc_sections
7757 operator()(const Output_section::Input_section& is1,
7758 const Output_section::Input_section& is2) const
7760 if (!is1.is_input_section() && is2.is_input_section())
7763 = (is1.is_input_section()
7764 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
7765 ->has_small_toc_reloc()));
7767 = (is2.is_input_section()
7768 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
7769 ->has_small_toc_reloc()));
7770 return small1 && !small2;
7774 // Finalize the sections.
7776 template<int size, bool big_endian>
7778 Target_powerpc<size, big_endian>::do_finalize_sections(
7780 const Input_objects*,
7781 Symbol_table* symtab)
7783 if (parameters->doing_static_link())
7785 // At least some versions of glibc elf-init.o have a strong
7786 // reference to __rela_iplt marker syms. A weak ref would be
7788 if (this->iplt_ != NULL)
7790 Reloc_section* rel = this->iplt_->rel_plt();
7791 symtab->define_in_output_data("__rela_iplt_start", NULL,
7792 Symbol_table::PREDEFINED, rel, 0, 0,
7793 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7794 elfcpp::STV_HIDDEN, 0, false, true);
7795 symtab->define_in_output_data("__rela_iplt_end", NULL,
7796 Symbol_table::PREDEFINED, rel, 0, 0,
7797 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7798 elfcpp::STV_HIDDEN, 0, true, true);
7802 symtab->define_as_constant("__rela_iplt_start", NULL,
7803 Symbol_table::PREDEFINED, 0, 0,
7804 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7805 elfcpp::STV_HIDDEN, 0, true, false);
7806 symtab->define_as_constant("__rela_iplt_end", NULL,
7807 Symbol_table::PREDEFINED, 0, 0,
7808 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7809 elfcpp::STV_HIDDEN, 0, true, false);
7815 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
7816 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
7818 if (!parameters->options().relocatable())
7820 this->define_save_restore_funcs(layout, symtab);
7822 // Annoyingly, we need to make these sections now whether or
7823 // not we need them. If we delay until do_relax then we
7824 // need to mess with the relaxation machinery checkpointing.
7825 this->got_section(symtab, layout);
7826 this->make_brlt_section(layout);
7828 if (parameters->options().toc_sort())
7830 Output_section* os = this->got_->output_section();
7831 if (os != NULL && os->input_sections().size() > 1)
7832 std::stable_sort(os->input_sections().begin(),
7833 os->input_sections().end(),
7834 Sort_toc_sections<big_endian>());
7839 // Fill in some more dynamic tags.
7840 Output_data_dynamic* odyn = layout->dynamic_data();
7843 const Reloc_section* rel_plt = (this->plt_ == NULL
7845 : this->plt_->rel_plt());
7846 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
7847 this->rela_dyn_, true, size == 32);
7851 if (this->got_ != NULL)
7853 this->got_->finalize_data_size();
7854 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
7855 this->got_, this->got_->g_o_t());
7860 if (this->glink_ != NULL)
7862 this->glink_->finalize_data_size();
7863 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
7865 (this->glink_->pltresolve_size
7868 if (this->has_localentry0_)
7869 odyn->add_constant(elfcpp::DT_PPC64_OPT,
7870 elfcpp::PPC64_OPT_LOCALENTRY);
7874 // Emit any relocs we saved in an attempt to avoid generating COPY
7876 if (this->copy_relocs_.any_saved_relocs())
7877 this->copy_relocs_.emit(this->rela_dyn_section(layout));
7880 // Emit any saved relocs, and mark toc entries using any of these
7881 // relocs as not optimizable.
7883 template<int sh_type, int size, bool big_endian>
7885 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
7886 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
7889 && parameters->options().toc_optimize())
7891 for (typename Copy_relocs<sh_type, size, big_endian>::
7892 Copy_reloc_entries::iterator p = this->entries_.begin();
7893 p != this->entries_.end();
7896 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
7899 // If the symbol is no longer defined in a dynamic object,
7900 // then we emitted a COPY relocation. If it is still
7901 // dynamic then we'll need dynamic relocations and thus
7902 // can't optimize toc entries.
7903 if (entry.sym_->is_from_dynobj())
7905 Powerpc_relobj<size, big_endian>* ppc_object
7906 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
7907 if (entry.shndx_ == ppc_object->toc_shndx())
7908 ppc_object->set_no_toc_opt(entry.address_);
7913 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
7916 // Return the value to use for a branch relocation.
7918 template<int size, bool big_endian>
7920 Target_powerpc<size, big_endian>::symval_for_branch(
7921 const Symbol_table* symtab,
7922 const Sized_symbol<size>* gsym,
7923 Powerpc_relobj<size, big_endian>* object,
7925 unsigned int *dest_shndx)
7927 if (size == 32 || this->abiversion() >= 2)
7931 // If the symbol is defined in an opd section, ie. is a function
7932 // descriptor, use the function descriptor code entry address
7933 Powerpc_relobj<size, big_endian>* symobj = object;
7935 && (gsym->source() != Symbol::FROM_OBJECT
7936 || gsym->object()->is_dynamic()))
7939 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
7940 unsigned int shndx = symobj->opd_shndx();
7943 Address opd_addr = symobj->get_output_section_offset(shndx);
7944 if (opd_addr == invalid_address)
7946 opd_addr += symobj->output_section_address(shndx);
7947 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
7950 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
7951 if (symtab->is_section_folded(symobj, *dest_shndx))
7954 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
7955 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
7956 *dest_shndx = folded.second;
7958 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
7959 if (sec_addr == invalid_address)
7962 sec_addr += symobj->output_section(*dest_shndx)->address();
7963 *value = sec_addr + sec_off;
7968 // Perform a relocation.
7970 template<int size, bool big_endian>
7972 Target_powerpc<size, big_endian>::Relocate::relocate(
7973 const Relocate_info<size, big_endian>* relinfo,
7975 Target_powerpc* target,
7978 const unsigned char* preloc,
7979 const Sized_symbol<size>* gsym,
7980 const Symbol_value<size>* psymval,
7981 unsigned char* view,
7983 section_size_type view_size)
7988 const elfcpp::Rela<size, big_endian> rela(preloc);
7989 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
7990 switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
7992 case Track_tls::NOT_EXPECTED:
7993 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7994 _("__tls_get_addr call lacks marker reloc"));
7996 case Track_tls::EXPECTED:
7997 // We have already complained.
7999 case Track_tls::SKIP:
8001 case Track_tls::NORMAL:
8005 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
8006 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
8007 typedef typename elfcpp::Rela<size, big_endian> Reltype;
8008 // Offset from start of insn to d-field reloc.
8009 const int d_offset = big_endian ? 2 : 0;
8011 Powerpc_relobj<size, big_endian>* const object
8012 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
8014 bool has_stub_value = false;
8015 bool localentry0 = false;
8016 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
8018 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
8019 : object->local_has_plt_offset(r_sym))
8020 && (!psymval->is_ifunc_symbol()
8021 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
8025 && target->abiversion() >= 2
8026 && !parameters->options().output_is_position_independent()
8027 && !is_branch_reloc(r_type))
8029 Address off = target->glink_section()->find_global_entry(gsym);
8030 if (off != invalid_address)
8032 value = target->glink_section()->global_entry_address() + off;
8033 has_stub_value = true;
8038 Stub_table<size, big_endian>* stub_table
8039 = object->stub_table(relinfo->data_shndx);
8040 if (stub_table == NULL)
8042 // This is a ref from a data section to an ifunc symbol.
8043 if (target->stub_tables().size() != 0)
8044 stub_table = target->stub_tables()[0];
8046 if (stub_table != NULL)
8048 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
8050 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
8051 rela.get_r_addend());
8053 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
8054 rela.get_r_addend());
8057 value = stub_table->stub_address() + ent->off_;
8058 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
8059 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
8060 size_t reloc_count = shdr.get_sh_size() / reloc_size;
8063 && relnum + 1 < reloc_count)
8065 Reltype next_rela(preloc + reloc_size);
8066 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
8067 == elfcpp::R_PPC64_TOCSAVE
8068 && next_rela.get_r_offset() == rela.get_r_offset() + 4)
8071 localentry0 = ent->localentry0_;
8072 has_stub_value = true;
8076 // We don't care too much about bogus debug references to
8077 // non-local functions, but otherwise there had better be a plt
8078 // call stub or global entry stub as appropriate.
8079 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
8082 if (r_type == elfcpp::R_POWERPC_GOT16
8083 || r_type == elfcpp::R_POWERPC_GOT16_LO
8084 || r_type == elfcpp::R_POWERPC_GOT16_HI
8085 || r_type == elfcpp::R_POWERPC_GOT16_HA
8086 || r_type == elfcpp::R_PPC64_GOT16_DS
8087 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
8091 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
8092 value = gsym->got_offset(GOT_TYPE_STANDARD);
8096 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
8097 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
8099 value -= target->got_section()->got_base_offset(object);
8101 else if (r_type == elfcpp::R_PPC64_TOC)
8103 value = (target->got_section()->output_section()->address()
8104 + object->toc_base_offset());
8106 else if (gsym != NULL
8107 && (r_type == elfcpp::R_POWERPC_REL24
8108 || r_type == elfcpp::R_PPC_PLTREL24)
8113 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
8114 Valtype* wv = reinterpret_cast<Valtype*>(view);
8115 bool can_plt_call = localentry0;
8116 if (!localentry0 && rela.get_r_offset() + 8 <= view_size)
8118 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
8119 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
8122 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
8124 elfcpp::Swap<32, big_endian>::
8125 writeval(wv + 1, ld_2_1 + target->stk_toc());
8126 can_plt_call = true;
8131 // If we don't have a branch and link followed by a nop,
8132 // we can't go via the plt because there is no place to
8133 // put a toc restoring instruction.
8134 // Unless we know we won't be returning.
8135 if (strcmp(gsym->name(), "__libc_start_main") == 0)
8136 can_plt_call = true;
8140 // g++ as of 20130507 emits self-calls without a
8141 // following nop. This is arguably wrong since we have
8142 // conflicting information. On the one hand a global
8143 // symbol and on the other a local call sequence, but
8144 // don't error for this special case.
8145 // It isn't possible to cheaply verify we have exactly
8146 // such a call. Allow all calls to the same section.
8148 Address code = value;
8149 if (gsym->source() == Symbol::FROM_OBJECT
8150 && gsym->object() == object)
8152 unsigned int dest_shndx = 0;
8153 if (target->abiversion() < 2)
8155 Address addend = rela.get_r_addend();
8156 code = psymval->value(object, addend);
8157 target->symval_for_branch(relinfo->symtab, gsym, object,
8158 &code, &dest_shndx);
8161 if (dest_shndx == 0)
8162 dest_shndx = gsym->shndx(&is_ordinary);
8163 ok = dest_shndx == relinfo->data_shndx;
8167 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8168 _("call lacks nop, can't restore toc; "
8169 "recompile with -fPIC"));
8175 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8176 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
8177 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
8178 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
8180 // First instruction of a global dynamic sequence, arg setup insn.
8181 const bool final = gsym == NULL || gsym->final_value_is_known();
8182 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8183 enum Got_type got_type = GOT_TYPE_STANDARD;
8184 if (tls_type == tls::TLSOPT_NONE)
8185 got_type = GOT_TYPE_TLSGD;
8186 else if (tls_type == tls::TLSOPT_TO_IE)
8187 got_type = GOT_TYPE_TPREL;
8188 if (got_type != GOT_TYPE_STANDARD)
8192 gold_assert(gsym->has_got_offset(got_type));
8193 value = gsym->got_offset(got_type);
8197 gold_assert(object->local_has_got_offset(r_sym, got_type));
8198 value = object->local_got_offset(r_sym, got_type);
8200 value -= target->got_section()->got_base_offset(object);
8202 if (tls_type == tls::TLSOPT_TO_IE)
8204 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8205 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8207 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8208 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8209 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
8211 insn |= 32 << 26; // lwz
8213 insn |= 58 << 26; // ld
8214 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8216 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
8217 - elfcpp::R_POWERPC_GOT_TLSGD16);
8219 else if (tls_type == tls::TLSOPT_TO_LE)
8221 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8222 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8224 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8225 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8226 insn &= (1 << 26) - (1 << 21); // extract rt
8231 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8232 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8233 value = psymval->value(object, rela.get_r_addend());
8237 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8239 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8240 r_type = elfcpp::R_POWERPC_NONE;
8244 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8245 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
8246 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
8247 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
8249 // First instruction of a local dynamic sequence, arg setup insn.
8250 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8251 if (tls_type == tls::TLSOPT_NONE)
8253 value = target->tlsld_got_offset();
8254 value -= target->got_section()->got_base_offset(object);
8258 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8259 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8260 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
8262 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8263 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8264 insn &= (1 << 26) - (1 << 21); // extract rt
8269 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8270 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8275 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8277 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8278 r_type = elfcpp::R_POWERPC_NONE;
8282 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
8283 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
8284 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
8285 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
8287 // Accesses relative to a local dynamic sequence address,
8288 // no optimisation here.
8291 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
8292 value = gsym->got_offset(GOT_TYPE_DTPREL);
8296 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
8297 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
8299 value -= target->got_section()->got_base_offset(object);
8301 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8302 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
8303 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
8304 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
8306 // First instruction of initial exec sequence.
8307 const bool final = gsym == NULL || gsym->final_value_is_known();
8308 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8309 if (tls_type == tls::TLSOPT_NONE)
8313 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
8314 value = gsym->got_offset(GOT_TYPE_TPREL);
8318 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
8319 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
8321 value -= target->got_section()->got_base_offset(object);
8325 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8326 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8327 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
8329 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8330 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8331 insn &= (1 << 26) - (1 << 21); // extract rt from ld
8336 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8337 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8338 value = psymval->value(object, rela.get_r_addend());
8342 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8344 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8345 r_type = elfcpp::R_POWERPC_NONE;
8349 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8350 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8352 // Second instruction of a global dynamic sequence,
8353 // the __tls_get_addr call
8354 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
8355 const bool final = gsym == NULL || gsym->final_value_is_known();
8356 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8357 if (tls_type != tls::TLSOPT_NONE)
8359 if (tls_type == tls::TLSOPT_TO_IE)
8361 Insn* iview = reinterpret_cast<Insn*>(view);
8362 Insn insn = add_3_3_13;
8365 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8366 r_type = elfcpp::R_POWERPC_NONE;
8370 Insn* iview = reinterpret_cast<Insn*>(view);
8371 Insn insn = addi_3_3;
8372 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8373 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8375 value = psymval->value(object, rela.get_r_addend());
8377 this->skip_next_tls_get_addr_call();
8380 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8381 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8383 // Second instruction of a local dynamic sequence,
8384 // the __tls_get_addr call
8385 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
8386 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8387 if (tls_type == tls::TLSOPT_TO_LE)
8389 Insn* iview = reinterpret_cast<Insn*>(view);
8390 Insn insn = addi_3_3;
8391 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8392 this->skip_next_tls_get_addr_call();
8393 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8398 else if (r_type == elfcpp::R_POWERPC_TLS)
8400 // Second instruction of an initial exec sequence
8401 const bool final = gsym == NULL || gsym->final_value_is_known();
8402 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8403 if (tls_type == tls::TLSOPT_TO_LE)
8405 Insn* iview = reinterpret_cast<Insn*>(view);
8406 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8407 unsigned int reg = size == 32 ? 2 : 13;
8408 insn = at_tls_transform(insn, reg);
8409 gold_assert(insn != 0);
8410 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8411 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8413 value = psymval->value(object, rela.get_r_addend());
8416 else if (!has_stub_value)
8419 if (!(size == 32 && r_type == elfcpp::R_PPC_PLTREL24))
8420 addend = rela.get_r_addend();
8421 value = psymval->value(object, addend);
8422 if (size == 64 && is_branch_reloc(r_type))
8424 if (target->abiversion() >= 2)
8427 value += object->ppc64_local_entry_offset(gsym);
8429 value += object->ppc64_local_entry_offset(r_sym);
8433 unsigned int dest_shndx;
8434 target->symval_for_branch(relinfo->symtab, gsym, object,
8435 &value, &dest_shndx);
8438 Address max_branch_offset = max_branch_delta(r_type);
8439 if (max_branch_offset != 0
8440 && value - address + max_branch_offset >= 2 * max_branch_offset)
8442 Stub_table<size, big_endian>* stub_table
8443 = object->stub_table(relinfo->data_shndx);
8444 if (stub_table != NULL)
8446 Address off = stub_table->find_long_branch_entry(object, value);
8447 if (off != invalid_address)
8449 value = (stub_table->stub_address() + stub_table->plt_size()
8451 has_stub_value = true;
8459 case elfcpp::R_PPC64_REL64:
8460 case elfcpp::R_POWERPC_REL32:
8461 case elfcpp::R_POWERPC_REL24:
8462 case elfcpp::R_PPC_PLTREL24:
8463 case elfcpp::R_PPC_LOCAL24PC:
8464 case elfcpp::R_POWERPC_REL16:
8465 case elfcpp::R_POWERPC_REL16_LO:
8466 case elfcpp::R_POWERPC_REL16_HI:
8467 case elfcpp::R_POWERPC_REL16_HA:
8468 case elfcpp::R_POWERPC_REL16DX_HA:
8469 case elfcpp::R_POWERPC_REL14:
8470 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8471 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8475 case elfcpp::R_PPC64_TOC16:
8476 case elfcpp::R_PPC64_TOC16_LO:
8477 case elfcpp::R_PPC64_TOC16_HI:
8478 case elfcpp::R_PPC64_TOC16_HA:
8479 case elfcpp::R_PPC64_TOC16_DS:
8480 case elfcpp::R_PPC64_TOC16_LO_DS:
8481 // Subtract the TOC base address.
8482 value -= (target->got_section()->output_section()->address()
8483 + object->toc_base_offset());
8486 case elfcpp::R_POWERPC_SECTOFF:
8487 case elfcpp::R_POWERPC_SECTOFF_LO:
8488 case elfcpp::R_POWERPC_SECTOFF_HI:
8489 case elfcpp::R_POWERPC_SECTOFF_HA:
8490 case elfcpp::R_PPC64_SECTOFF_DS:
8491 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8493 value -= os->address();
8496 case elfcpp::R_PPC64_TPREL16_DS:
8497 case elfcpp::R_PPC64_TPREL16_LO_DS:
8498 case elfcpp::R_PPC64_TPREL16_HIGH:
8499 case elfcpp::R_PPC64_TPREL16_HIGHA:
8501 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
8504 case elfcpp::R_POWERPC_TPREL16:
8505 case elfcpp::R_POWERPC_TPREL16_LO:
8506 case elfcpp::R_POWERPC_TPREL16_HI:
8507 case elfcpp::R_POWERPC_TPREL16_HA:
8508 case elfcpp::R_POWERPC_TPREL:
8509 case elfcpp::R_PPC64_TPREL16_HIGHER:
8510 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8511 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8512 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8513 // tls symbol values are relative to tls_segment()->vaddr()
8517 case elfcpp::R_PPC64_DTPREL16_DS:
8518 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8519 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8520 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8521 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8522 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8524 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
8525 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
8528 case elfcpp::R_POWERPC_DTPREL16:
8529 case elfcpp::R_POWERPC_DTPREL16_LO:
8530 case elfcpp::R_POWERPC_DTPREL16_HI:
8531 case elfcpp::R_POWERPC_DTPREL16_HA:
8532 case elfcpp::R_POWERPC_DTPREL:
8533 case elfcpp::R_PPC64_DTPREL16_HIGH:
8534 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8535 // tls symbol values are relative to tls_segment()->vaddr()
8536 value -= dtp_offset;
8539 case elfcpp::R_PPC64_ADDR64_LOCAL:
8541 value += object->ppc64_local_entry_offset(gsym);
8543 value += object->ppc64_local_entry_offset(r_sym);
8550 Insn branch_bit = 0;
8553 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8554 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8555 branch_bit = 1 << 21;
8557 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8558 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8560 Insn* iview = reinterpret_cast<Insn*>(view);
8561 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8564 if (this->is_isa_v2)
8566 // Set 'a' bit. This is 0b00010 in BO field for branch
8567 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
8568 // for branch on CTR insns (BO == 1a00t or 1a01t).
8569 if ((insn & (0x14 << 21)) == (0x04 << 21))
8571 else if ((insn & (0x14 << 21)) == (0x10 << 21))
8578 // Invert 'y' bit if not the default.
8579 if (static_cast<Signed_address>(value) < 0)
8582 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8597 // Multi-instruction sequences that access the GOT/TOC can
8598 // be optimized, eg.
8599 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
8600 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
8602 // addis ra,r2,0; addi rb,ra,x@toc@l;
8603 // to nop; addi rb,r2,x@toc;
8604 // FIXME: the @got sequence shown above is not yet
8605 // optimized. Note that gcc as of 2017-01-07 doesn't use
8606 // the ELF @got relocs except for TLS, instead using the
8607 // PowerOpen variant of a compiler managed GOT (called TOC).
8608 // The PowerOpen TOC sequence equivalent to the first
8609 // example is optimized.
8610 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8611 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8612 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8613 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8614 case elfcpp::R_POWERPC_GOT16_HA:
8615 case elfcpp::R_PPC64_TOC16_HA:
8616 if (parameters->options().toc_optimize())
8618 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8619 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8620 if (r_type == elfcpp::R_PPC64_TOC16_HA
8621 && object->make_toc_relative(target, &value))
8623 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
8624 == ((15u << 26) | (2 << 16)));
8626 if (((insn & ((0x3f << 26) | 0x1f << 16))
8627 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8628 && value + 0x8000 < 0x10000)
8630 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
8636 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8637 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8638 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8639 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8640 case elfcpp::R_POWERPC_GOT16_LO:
8641 case elfcpp::R_PPC64_GOT16_LO_DS:
8642 case elfcpp::R_PPC64_TOC16_LO:
8643 case elfcpp::R_PPC64_TOC16_LO_DS:
8644 if (parameters->options().toc_optimize())
8646 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8647 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8648 bool changed = false;
8649 if (r_type == elfcpp::R_PPC64_TOC16_LO_DS
8650 && object->make_toc_relative(target, &value))
8652 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
8653 insn ^= (14u << 26) ^ (58u << 26);
8654 r_type = elfcpp::R_PPC64_TOC16_LO;
8657 if (ok_lo_toc_insn(insn, r_type)
8658 && value + 0x8000 < 0x10000)
8660 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
8662 // Transform addic to addi when we change reg.
8663 insn &= ~((0x3f << 26) | (0x1f << 16));
8664 insn |= (14u << 26) | (2 << 16);
8668 insn &= ~(0x1f << 16);
8674 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8678 case elfcpp::R_PPC64_ENTRY:
8679 value = (target->got_section()->output_section()->address()
8680 + object->toc_base_offset());
8681 if (value + 0x80008000 <= 0xffffffff
8682 && !parameters->options().output_is_position_independent())
8684 Insn* iview = reinterpret_cast<Insn*>(view);
8685 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
8686 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
8688 if ((insn1 & ~0xfffc) == ld_2_12
8689 && insn2 == add_2_2_12)
8691 insn1 = lis_2 + ha(value);
8692 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
8693 insn2 = addi_2_2 + l(value);
8694 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
8701 if (value + 0x80008000 <= 0xffffffff)
8703 Insn* iview = reinterpret_cast<Insn*>(view);
8704 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
8705 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
8707 if ((insn1 & ~0xfffc) == ld_2_12
8708 && insn2 == add_2_2_12)
8710 insn1 = addis_2_12 + ha(value);
8711 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
8712 insn2 = addi_2_2 + l(value);
8713 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
8720 case elfcpp::R_POWERPC_REL16_LO:
8721 // If we are generating a non-PIC executable, edit
8722 // 0: addis 2,12,.TOC.-0b@ha
8723 // addi 2,2,.TOC.-0b@l
8724 // used by ELFv2 global entry points to set up r2, to
8727 // if .TOC. is in range. */
8728 if (value + address - 4 + 0x80008000 <= 0xffffffff
8731 && target->abiversion() >= 2
8732 && !parameters->options().output_is_position_independent()
8733 && rela.get_r_addend() == d_offset + 4
8735 && strcmp(gsym->name(), ".TOC.") == 0)
8737 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
8738 Reltype prev_rela(preloc - reloc_size);
8739 if ((prev_rela.get_r_info()
8740 == elfcpp::elf_r_info<size>(r_sym,
8741 elfcpp::R_POWERPC_REL16_HA))
8742 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
8743 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
8745 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8746 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
8747 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
8749 if ((insn1 & 0xffff0000) == addis_2_12
8750 && (insn2 & 0xffff0000) == addi_2_2)
8752 insn1 = lis_2 + ha(value + address - 4);
8753 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
8754 insn2 = addi_2_2 + l(value + address - 4);
8755 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
8758 relinfo->rr->set_strategy(relnum - 1,
8759 Relocatable_relocs::RELOC_SPECIAL);
8760 relinfo->rr->set_strategy(relnum,
8761 Relocatable_relocs::RELOC_SPECIAL);
8771 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
8772 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
8775 case elfcpp::R_POWERPC_ADDR32:
8776 case elfcpp::R_POWERPC_UADDR32:
8778 overflow = Reloc::CHECK_BITFIELD;
8781 case elfcpp::R_POWERPC_REL32:
8782 case elfcpp::R_POWERPC_REL16DX_HA:
8784 overflow = Reloc::CHECK_SIGNED;
8787 case elfcpp::R_POWERPC_UADDR16:
8788 overflow = Reloc::CHECK_BITFIELD;
8791 case elfcpp::R_POWERPC_ADDR16:
8792 // We really should have three separate relocations,
8793 // one for 16-bit data, one for insns with 16-bit signed fields,
8794 // and one for insns with 16-bit unsigned fields.
8795 overflow = Reloc::CHECK_BITFIELD;
8796 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
8797 overflow = Reloc::CHECK_LOW_INSN;
8800 case elfcpp::R_POWERPC_ADDR16_HI:
8801 case elfcpp::R_POWERPC_ADDR16_HA:
8802 case elfcpp::R_POWERPC_GOT16_HI:
8803 case elfcpp::R_POWERPC_GOT16_HA:
8804 case elfcpp::R_POWERPC_PLT16_HI:
8805 case elfcpp::R_POWERPC_PLT16_HA:
8806 case elfcpp::R_POWERPC_SECTOFF_HI:
8807 case elfcpp::R_POWERPC_SECTOFF_HA:
8808 case elfcpp::R_PPC64_TOC16_HI:
8809 case elfcpp::R_PPC64_TOC16_HA:
8810 case elfcpp::R_PPC64_PLTGOT16_HI:
8811 case elfcpp::R_PPC64_PLTGOT16_HA:
8812 case elfcpp::R_POWERPC_TPREL16_HI:
8813 case elfcpp::R_POWERPC_TPREL16_HA:
8814 case elfcpp::R_POWERPC_DTPREL16_HI:
8815 case elfcpp::R_POWERPC_DTPREL16_HA:
8816 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8817 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8818 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8819 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8820 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8821 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8822 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8823 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8824 case elfcpp::R_POWERPC_REL16_HI:
8825 case elfcpp::R_POWERPC_REL16_HA:
8827 overflow = Reloc::CHECK_HIGH_INSN;
8830 case elfcpp::R_POWERPC_REL16:
8831 case elfcpp::R_PPC64_TOC16:
8832 case elfcpp::R_POWERPC_GOT16:
8833 case elfcpp::R_POWERPC_SECTOFF:
8834 case elfcpp::R_POWERPC_TPREL16:
8835 case elfcpp::R_POWERPC_DTPREL16:
8836 case elfcpp::R_POWERPC_GOT_TLSGD16:
8837 case elfcpp::R_POWERPC_GOT_TLSLD16:
8838 case elfcpp::R_POWERPC_GOT_TPREL16:
8839 case elfcpp::R_POWERPC_GOT_DTPREL16:
8840 overflow = Reloc::CHECK_LOW_INSN;
8843 case elfcpp::R_POWERPC_ADDR24:
8844 case elfcpp::R_POWERPC_ADDR14:
8845 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8846 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8847 case elfcpp::R_PPC64_ADDR16_DS:
8848 case elfcpp::R_POWERPC_REL24:
8849 case elfcpp::R_PPC_PLTREL24:
8850 case elfcpp::R_PPC_LOCAL24PC:
8851 case elfcpp::R_PPC64_TPREL16_DS:
8852 case elfcpp::R_PPC64_DTPREL16_DS:
8853 case elfcpp::R_PPC64_TOC16_DS:
8854 case elfcpp::R_PPC64_GOT16_DS:
8855 case elfcpp::R_PPC64_SECTOFF_DS:
8856 case elfcpp::R_POWERPC_REL14:
8857 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8858 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8859 overflow = Reloc::CHECK_SIGNED;
8863 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8866 if (overflow == Reloc::CHECK_LOW_INSN
8867 || overflow == Reloc::CHECK_HIGH_INSN)
8869 insn = elfcpp::Swap<32, big_endian>::readval(iview);
8871 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
8872 overflow = Reloc::CHECK_BITFIELD;
8873 else if (overflow == Reloc::CHECK_LOW_INSN
8874 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
8875 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
8876 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
8877 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
8878 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
8879 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
8880 overflow = Reloc::CHECK_UNSIGNED;
8882 overflow = Reloc::CHECK_SIGNED;
8885 bool maybe_dq_reloc = false;
8886 typename Powerpc_relocate_functions<size, big_endian>::Status status
8887 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
8890 case elfcpp::R_POWERPC_NONE:
8891 case elfcpp::R_POWERPC_TLS:
8892 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8893 case elfcpp::R_POWERPC_GNU_VTENTRY:
8896 case elfcpp::R_PPC64_ADDR64:
8897 case elfcpp::R_PPC64_REL64:
8898 case elfcpp::R_PPC64_TOC:
8899 case elfcpp::R_PPC64_ADDR64_LOCAL:
8900 Reloc::addr64(view, value);
8903 case elfcpp::R_POWERPC_TPREL:
8904 case elfcpp::R_POWERPC_DTPREL:
8906 Reloc::addr64(view, value);
8908 status = Reloc::addr32(view, value, overflow);
8911 case elfcpp::R_PPC64_UADDR64:
8912 Reloc::addr64_u(view, value);
8915 case elfcpp::R_POWERPC_ADDR32:
8916 status = Reloc::addr32(view, value, overflow);
8919 case elfcpp::R_POWERPC_REL32:
8920 case elfcpp::R_POWERPC_UADDR32:
8921 status = Reloc::addr32_u(view, value, overflow);
8924 case elfcpp::R_POWERPC_ADDR24:
8925 case elfcpp::R_POWERPC_REL24:
8926 case elfcpp::R_PPC_PLTREL24:
8927 case elfcpp::R_PPC_LOCAL24PC:
8928 status = Reloc::addr24(view, value, overflow);
8931 case elfcpp::R_POWERPC_GOT_DTPREL16:
8932 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8933 case elfcpp::R_POWERPC_GOT_TPREL16:
8934 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8937 // On ppc64 these are all ds form
8938 maybe_dq_reloc = true;
8942 case elfcpp::R_POWERPC_ADDR16:
8943 case elfcpp::R_POWERPC_REL16:
8944 case elfcpp::R_PPC64_TOC16:
8945 case elfcpp::R_POWERPC_GOT16:
8946 case elfcpp::R_POWERPC_SECTOFF:
8947 case elfcpp::R_POWERPC_TPREL16:
8948 case elfcpp::R_POWERPC_DTPREL16:
8949 case elfcpp::R_POWERPC_GOT_TLSGD16:
8950 case elfcpp::R_POWERPC_GOT_TLSLD16:
8951 case elfcpp::R_POWERPC_ADDR16_LO:
8952 case elfcpp::R_POWERPC_REL16_LO:
8953 case elfcpp::R_PPC64_TOC16_LO:
8954 case elfcpp::R_POWERPC_GOT16_LO:
8955 case elfcpp::R_POWERPC_SECTOFF_LO:
8956 case elfcpp::R_POWERPC_TPREL16_LO:
8957 case elfcpp::R_POWERPC_DTPREL16_LO:
8958 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8959 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8961 status = Reloc::addr16(view, value, overflow);
8963 maybe_dq_reloc = true;
8966 case elfcpp::R_POWERPC_UADDR16:
8967 status = Reloc::addr16_u(view, value, overflow);
8970 case elfcpp::R_PPC64_ADDR16_HIGH:
8971 case elfcpp::R_PPC64_TPREL16_HIGH:
8972 case elfcpp::R_PPC64_DTPREL16_HIGH:
8974 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
8977 case elfcpp::R_POWERPC_ADDR16_HI:
8978 case elfcpp::R_POWERPC_REL16_HI:
8979 case elfcpp::R_PPC64_TOC16_HI:
8980 case elfcpp::R_POWERPC_GOT16_HI:
8981 case elfcpp::R_POWERPC_SECTOFF_HI:
8982 case elfcpp::R_POWERPC_TPREL16_HI:
8983 case elfcpp::R_POWERPC_DTPREL16_HI:
8984 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8985 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8986 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8987 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8988 Reloc::addr16_hi(view, value);
8991 case elfcpp::R_PPC64_ADDR16_HIGHA:
8992 case elfcpp::R_PPC64_TPREL16_HIGHA:
8993 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8995 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
8998 case elfcpp::R_POWERPC_ADDR16_HA:
8999 case elfcpp::R_POWERPC_REL16_HA:
9000 case elfcpp::R_PPC64_TOC16_HA:
9001 case elfcpp::R_POWERPC_GOT16_HA:
9002 case elfcpp::R_POWERPC_SECTOFF_HA:
9003 case elfcpp::R_POWERPC_TPREL16_HA:
9004 case elfcpp::R_POWERPC_DTPREL16_HA:
9005 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9006 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9007 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9008 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9009 Reloc::addr16_ha(view, value);
9012 case elfcpp::R_POWERPC_REL16DX_HA:
9013 status = Reloc::addr16dx_ha(view, value, overflow);
9016 case elfcpp::R_PPC64_DTPREL16_HIGHER:
9018 // R_PPC_EMB_NADDR16_LO
9021 case elfcpp::R_PPC64_ADDR16_HIGHER:
9022 case elfcpp::R_PPC64_TPREL16_HIGHER:
9023 Reloc::addr16_hi2(view, value);
9026 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
9028 // R_PPC_EMB_NADDR16_HI
9031 case elfcpp::R_PPC64_ADDR16_HIGHERA:
9032 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9033 Reloc::addr16_ha2(view, value);
9036 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
9038 // R_PPC_EMB_NADDR16_HA
9041 case elfcpp::R_PPC64_ADDR16_HIGHEST:
9042 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9043 Reloc::addr16_hi3(view, value);
9046 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
9051 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
9052 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9053 Reloc::addr16_ha3(view, value);
9056 case elfcpp::R_PPC64_DTPREL16_DS:
9057 case elfcpp::R_PPC64_DTPREL16_LO_DS:
9059 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
9062 case elfcpp::R_PPC64_TPREL16_DS:
9063 case elfcpp::R_PPC64_TPREL16_LO_DS:
9065 // R_PPC_TLSGD, R_PPC_TLSLD
9068 case elfcpp::R_PPC64_ADDR16_DS:
9069 case elfcpp::R_PPC64_ADDR16_LO_DS:
9070 case elfcpp::R_PPC64_TOC16_DS:
9071 case elfcpp::R_PPC64_TOC16_LO_DS:
9072 case elfcpp::R_PPC64_GOT16_DS:
9073 case elfcpp::R_PPC64_GOT16_LO_DS:
9074 case elfcpp::R_PPC64_SECTOFF_DS:
9075 case elfcpp::R_PPC64_SECTOFF_LO_DS:
9076 maybe_dq_reloc = true;
9079 case elfcpp::R_POWERPC_ADDR14:
9080 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
9081 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
9082 case elfcpp::R_POWERPC_REL14:
9083 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9084 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
9085 status = Reloc::addr14(view, value, overflow);
9088 case elfcpp::R_POWERPC_COPY:
9089 case elfcpp::R_POWERPC_GLOB_DAT:
9090 case elfcpp::R_POWERPC_JMP_SLOT:
9091 case elfcpp::R_POWERPC_RELATIVE:
9092 case elfcpp::R_POWERPC_DTPMOD:
9093 case elfcpp::R_PPC64_JMP_IREL:
9094 case elfcpp::R_POWERPC_IRELATIVE:
9095 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9096 _("unexpected reloc %u in object file"),
9100 case elfcpp::R_PPC64_TOCSAVE:
9106 Symbol_location loc;
9107 loc.object = relinfo->object;
9108 loc.shndx = relinfo->data_shndx;
9109 loc.offset = rela.get_r_offset();
9110 Tocsave_loc::const_iterator p = target->tocsave_loc().find(loc);
9111 if (p != target->tocsave_loc().end())
9113 // If we've generated plt calls using this tocsave, then
9114 // the nop needs to be changed to save r2.
9115 Insn* iview = reinterpret_cast<Insn*>(view);
9116 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
9117 elfcpp::Swap<32, big_endian>::
9118 writeval(iview, std_2_1 + target->stk_toc());
9123 case elfcpp::R_PPC_EMB_SDA2I16:
9124 case elfcpp::R_PPC_EMB_SDA2REL:
9127 // R_PPC64_TLSGD, R_PPC64_TLSLD
9130 case elfcpp::R_POWERPC_PLT32:
9131 case elfcpp::R_POWERPC_PLTREL32:
9132 case elfcpp::R_POWERPC_PLT16_LO:
9133 case elfcpp::R_POWERPC_PLT16_HI:
9134 case elfcpp::R_POWERPC_PLT16_HA:
9135 case elfcpp::R_PPC_SDAREL16:
9136 case elfcpp::R_POWERPC_ADDR30:
9137 case elfcpp::R_PPC64_PLT64:
9138 case elfcpp::R_PPC64_PLTREL64:
9139 case elfcpp::R_PPC64_PLTGOT16:
9140 case elfcpp::R_PPC64_PLTGOT16_LO:
9141 case elfcpp::R_PPC64_PLTGOT16_HI:
9142 case elfcpp::R_PPC64_PLTGOT16_HA:
9143 case elfcpp::R_PPC64_PLT16_LO_DS:
9144 case elfcpp::R_PPC64_PLTGOT16_DS:
9145 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
9146 case elfcpp::R_PPC_EMB_RELSDA:
9147 case elfcpp::R_PPC_TOC16:
9150 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9151 _("unsupported reloc %u"),
9159 insn = elfcpp::Swap<32, big_endian>::readval(iview);
9161 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
9162 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
9163 && (insn & 3) == 1))
9164 status = Reloc::addr16_dq(view, value, overflow);
9166 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
9167 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
9168 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
9169 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
9170 status = Reloc::addr16_ds(view, value, overflow);
9172 status = Reloc::addr16(view, value, overflow);
9175 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
9178 && gsym->is_undefined()
9179 && is_branch_reloc(r_type))))
9181 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9182 _("relocation overflow"));
9184 gold_info(_("try relinking with a smaller --stub-group-size"));
9190 // Relocate section data.
9192 template<int size, bool big_endian>
9194 Target_powerpc<size, big_endian>::relocate_section(
9195 const Relocate_info<size, big_endian>* relinfo,
9196 unsigned int sh_type,
9197 const unsigned char* prelocs,
9199 Output_section* output_section,
9200 bool needs_special_offset_handling,
9201 unsigned char* view,
9203 section_size_type view_size,
9204 const Reloc_symbol_changes* reloc_symbol_changes)
9206 typedef Target_powerpc<size, big_endian> Powerpc;
9207 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
9208 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
9209 Powerpc_comdat_behavior;
9210 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9213 gold_assert(sh_type == elfcpp::SHT_RELA);
9215 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
9216 Powerpc_comdat_behavior, Classify_reloc>(
9222 needs_special_offset_handling,
9226 reloc_symbol_changes);
9229 template<int size, bool big_endian>
9230 class Powerpc_scan_relocatable_reloc
9233 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9234 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
9235 static const int sh_type = elfcpp::SHT_RELA;
9237 // Return the symbol referred to by the relocation.
9238 static inline unsigned int
9239 get_r_sym(const Reltype* reloc)
9240 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
9242 // Return the type of the relocation.
9243 static inline unsigned int
9244 get_r_type(const Reltype* reloc)
9245 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
9247 // Return the strategy to use for a local symbol which is not a
9248 // section symbol, given the relocation type.
9249 inline Relocatable_relocs::Reloc_strategy
9250 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
9252 if (r_type == 0 && r_sym == 0)
9253 return Relocatable_relocs::RELOC_DISCARD;
9254 return Relocatable_relocs::RELOC_COPY;
9257 // Return the strategy to use for a local symbol which is a section
9258 // symbol, given the relocation type.
9259 inline Relocatable_relocs::Reloc_strategy
9260 local_section_strategy(unsigned int, Relobj*)
9262 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
9265 // Return the strategy to use for a global symbol, given the
9266 // relocation type, the object, and the symbol index.
9267 inline Relocatable_relocs::Reloc_strategy
9268 global_strategy(unsigned int r_type, Relobj*, unsigned int)
9270 if (r_type == elfcpp::R_PPC_PLTREL24)
9271 return Relocatable_relocs::RELOC_SPECIAL;
9272 return Relocatable_relocs::RELOC_COPY;
9276 // Scan the relocs during a relocatable link.
9278 template<int size, bool big_endian>
9280 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
9281 Symbol_table* symtab,
9283 Sized_relobj_file<size, big_endian>* object,
9284 unsigned int data_shndx,
9285 unsigned int sh_type,
9286 const unsigned char* prelocs,
9288 Output_section* output_section,
9289 bool needs_special_offset_handling,
9290 size_t local_symbol_count,
9291 const unsigned char* plocal_symbols,
9292 Relocatable_relocs* rr)
9294 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
9296 gold_assert(sh_type == elfcpp::SHT_RELA);
9298 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
9306 needs_special_offset_handling,
9312 // Scan the relocs for --emit-relocs.
9314 template<int size, bool big_endian>
9316 Target_powerpc<size, big_endian>::emit_relocs_scan(
9317 Symbol_table* symtab,
9319 Sized_relobj_file<size, big_endian>* object,
9320 unsigned int data_shndx,
9321 unsigned int sh_type,
9322 const unsigned char* prelocs,
9324 Output_section* output_section,
9325 bool needs_special_offset_handling,
9326 size_t local_symbol_count,
9327 const unsigned char* plocal_syms,
9328 Relocatable_relocs* rr)
9330 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9332 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
9333 Emit_relocs_strategy;
9335 gold_assert(sh_type == elfcpp::SHT_RELA);
9337 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
9345 needs_special_offset_handling,
9351 // Emit relocations for a section.
9352 // This is a modified version of the function by the same name in
9353 // target-reloc.h. Using relocate_special_relocatable for
9354 // R_PPC_PLTREL24 would require duplication of the entire body of the
9355 // loop, so we may as well duplicate the whole thing.
9357 template<int size, bool big_endian>
9359 Target_powerpc<size, big_endian>::relocate_relocs(
9360 const Relocate_info<size, big_endian>* relinfo,
9361 unsigned int sh_type,
9362 const unsigned char* prelocs,
9364 Output_section* output_section,
9365 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
9367 Address view_address,
9369 unsigned char* reloc_view,
9370 section_size_type reloc_view_size)
9372 gold_assert(sh_type == elfcpp::SHT_RELA);
9374 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9375 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
9376 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
9377 // Offset from start of insn to d-field reloc.
9378 const int d_offset = big_endian ? 2 : 0;
9380 Powerpc_relobj<size, big_endian>* const object
9381 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
9382 const unsigned int local_count = object->local_symbol_count();
9383 unsigned int got2_shndx = object->got2_shndx();
9384 Address got2_addend = 0;
9385 if (got2_shndx != 0)
9387 got2_addend = object->get_output_section_offset(got2_shndx);
9388 gold_assert(got2_addend != invalid_address);
9391 unsigned char* pwrite = reloc_view;
9392 bool zap_next = false;
9393 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
9395 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
9396 if (strategy == Relocatable_relocs::RELOC_DISCARD)
9399 Reltype reloc(prelocs);
9400 Reltype_write reloc_write(pwrite);
9402 Address offset = reloc.get_r_offset();
9403 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
9404 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
9405 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
9406 const unsigned int orig_r_sym = r_sym;
9407 typename elfcpp::Elf_types<size>::Elf_Swxword addend
9408 = reloc.get_r_addend();
9409 const Symbol* gsym = NULL;
9413 // We could arrange to discard these and other relocs for
9414 // tls optimised sequences in the strategy methods, but for
9415 // now do as BFD ld does.
9416 r_type = elfcpp::R_POWERPC_NONE;
9420 // Get the new symbol index.
9421 Output_section* os = NULL;
9422 if (r_sym < local_count)
9426 case Relocatable_relocs::RELOC_COPY:
9427 case Relocatable_relocs::RELOC_SPECIAL:
9430 r_sym = object->symtab_index(r_sym);
9431 gold_assert(r_sym != -1U);
9435 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
9437 // We are adjusting a section symbol. We need to find
9438 // the symbol table index of the section symbol for
9439 // the output section corresponding to input section
9440 // in which this symbol is defined.
9441 gold_assert(r_sym < local_count);
9443 unsigned int shndx =
9444 object->local_symbol_input_shndx(r_sym, &is_ordinary);
9445 gold_assert(is_ordinary);
9446 os = object->output_section(shndx);
9447 gold_assert(os != NULL);
9448 gold_assert(os->needs_symtab_index());
9449 r_sym = os->symtab_index();
9459 gsym = object->global_symbol(r_sym);
9460 gold_assert(gsym != NULL);
9461 if (gsym->is_forwarder())
9462 gsym = relinfo->symtab->resolve_forwards(gsym);
9464 gold_assert(gsym->has_symtab_index());
9465 r_sym = gsym->symtab_index();
9468 // Get the new offset--the location in the output section where
9469 // this relocation should be applied.
9470 if (static_cast<Address>(offset_in_output_section) != invalid_address)
9471 offset += offset_in_output_section;
9474 section_offset_type sot_offset =
9475 convert_types<section_offset_type, Address>(offset);
9476 section_offset_type new_sot_offset =
9477 output_section->output_offset(object, relinfo->data_shndx,
9479 gold_assert(new_sot_offset != -1);
9480 offset = new_sot_offset;
9483 // In an object file, r_offset is an offset within the section.
9484 // In an executable or dynamic object, generated by
9485 // --emit-relocs, r_offset is an absolute address.
9486 if (!parameters->options().relocatable())
9488 offset += view_address;
9489 if (static_cast<Address>(offset_in_output_section) != invalid_address)
9490 offset -= offset_in_output_section;
9493 // Handle the reloc addend based on the strategy.
9494 if (strategy == Relocatable_relocs::RELOC_COPY)
9496 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
9498 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
9499 gold_assert(os != NULL);
9500 addend = psymval->value(object, addend) - os->address();
9502 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
9506 if (addend >= 32768)
9507 addend += got2_addend;
9509 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
9511 r_type = elfcpp::R_POWERPC_ADDR16_HA;
9514 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
9516 r_type = elfcpp::R_POWERPC_ADDR16_LO;
9517 addend -= d_offset + 4;
9523 if (!parameters->options().relocatable())
9525 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9526 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
9527 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
9528 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
9530 // First instruction of a global dynamic sequence,
9532 const bool final = gsym == NULL || gsym->final_value_is_known();
9533 switch (this->optimize_tls_gd(final))
9535 case tls::TLSOPT_TO_IE:
9536 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
9537 - elfcpp::R_POWERPC_GOT_TLSGD16);
9539 case tls::TLSOPT_TO_LE:
9540 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9541 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
9542 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9545 r_type = elfcpp::R_POWERPC_NONE;
9553 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9554 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
9555 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
9556 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
9558 // First instruction of a local dynamic sequence,
9560 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
9562 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9563 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
9565 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9566 const Output_section* os = relinfo->layout->tls_segment()
9568 gold_assert(os != NULL);
9569 gold_assert(os->needs_symtab_index());
9570 r_sym = os->symtab_index();
9571 addend = dtp_offset;
9575 r_type = elfcpp::R_POWERPC_NONE;
9580 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9581 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
9582 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
9583 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
9585 // First instruction of initial exec sequence.
9586 const bool final = gsym == NULL || gsym->final_value_is_known();
9587 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
9589 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9590 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
9591 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9594 r_type = elfcpp::R_POWERPC_NONE;
9599 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
9600 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
9602 // Second instruction of a global dynamic sequence,
9603 // the __tls_get_addr call
9604 const bool final = gsym == NULL || gsym->final_value_is_known();
9605 switch (this->optimize_tls_gd(final))
9607 case tls::TLSOPT_TO_IE:
9608 r_type = elfcpp::R_POWERPC_NONE;
9611 case tls::TLSOPT_TO_LE:
9612 r_type = elfcpp::R_POWERPC_TPREL16_LO;
9620 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
9621 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
9623 // Second instruction of a local dynamic sequence,
9624 // the __tls_get_addr call
9625 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
9627 const Output_section* os = relinfo->layout->tls_segment()
9629 gold_assert(os != NULL);
9630 gold_assert(os->needs_symtab_index());
9631 r_sym = os->symtab_index();
9632 addend = dtp_offset;
9633 r_type = elfcpp::R_POWERPC_TPREL16_LO;
9638 else if (r_type == elfcpp::R_POWERPC_TLS)
9640 // Second instruction of an initial exec sequence
9641 const bool final = gsym == NULL || gsym->final_value_is_known();
9642 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
9644 r_type = elfcpp::R_POWERPC_TPREL16_LO;
9650 reloc_write.put_r_offset(offset);
9651 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
9652 reloc_write.put_r_addend(addend);
9654 pwrite += reloc_size;
9657 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
9658 == reloc_view_size);
9661 // Return the value to use for a dynamic symbol which requires special
9662 // treatment. This is how we support equality comparisons of function
9663 // pointers across shared library boundaries, as described in the
9664 // processor specific ABI supplement.
9666 template<int size, bool big_endian>
9668 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
9672 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
9673 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9674 p != this->stub_tables_.end();
9677 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
9678 = (*p)->find_plt_call_entry(gsym);
9680 return (*p)->stub_address() + ent->off_;
9683 else if (this->abiversion() >= 2)
9685 Address off = this->glink_section()->find_global_entry(gsym);
9686 if (off != invalid_address)
9687 return this->glink_section()->global_entry_address() + off;
9692 // Return the PLT address to use for a local symbol.
9693 template<int size, bool big_endian>
9695 Target_powerpc<size, big_endian>::do_plt_address_for_local(
9696 const Relobj* object,
9697 unsigned int symndx) const
9701 const Sized_relobj<size, big_endian>* relobj
9702 = static_cast<const Sized_relobj<size, big_endian>*>(object);
9703 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9704 p != this->stub_tables_.end();
9707 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
9708 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
9710 return (*p)->stub_address() + ent->off_;
9716 // Return the PLT address to use for a global symbol.
9717 template<int size, bool big_endian>
9719 Target_powerpc<size, big_endian>::do_plt_address_for_global(
9720 const Symbol* gsym) const
9724 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9725 p != this->stub_tables_.end();
9728 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
9729 = (*p)->find_plt_call_entry(gsym);
9731 return (*p)->stub_address() + ent->off_;
9734 else if (this->abiversion() >= 2)
9736 Address off = this->glink_section()->find_global_entry(gsym);
9737 if (off != invalid_address)
9738 return this->glink_section()->global_entry_address() + off;
9743 // Return the offset to use for the GOT_INDX'th got entry which is
9744 // for a local tls symbol specified by OBJECT, SYMNDX.
9745 template<int size, bool big_endian>
9747 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
9748 const Relobj* object,
9749 unsigned int symndx,
9750 unsigned int got_indx) const
9752 const Powerpc_relobj<size, big_endian>* ppc_object
9753 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
9754 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
9756 for (Got_type got_type = GOT_TYPE_TLSGD;
9757 got_type <= GOT_TYPE_TPREL;
9758 got_type = Got_type(got_type + 1))
9759 if (ppc_object->local_has_got_offset(symndx, got_type))
9761 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
9762 if (got_type == GOT_TYPE_TLSGD)
9764 if (off == got_indx * (size / 8))
9766 if (got_type == GOT_TYPE_TPREL)
9776 // Return the offset to use for the GOT_INDX'th got entry which is
9777 // for global tls symbol GSYM.
9778 template<int size, bool big_endian>
9780 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
9782 unsigned int got_indx) const
9784 if (gsym->type() == elfcpp::STT_TLS)
9786 for (Got_type got_type = GOT_TYPE_TLSGD;
9787 got_type <= GOT_TYPE_TPREL;
9788 got_type = Got_type(got_type + 1))
9789 if (gsym->has_got_offset(got_type))
9791 unsigned int off = gsym->got_offset(got_type);
9792 if (got_type == GOT_TYPE_TLSGD)
9794 if (off == got_indx * (size / 8))
9796 if (got_type == GOT_TYPE_TPREL)
9806 // The selector for powerpc object files.
9808 template<int size, bool big_endian>
9809 class Target_selector_powerpc : public Target_selector
9812 Target_selector_powerpc()
9813 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
9816 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
9817 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
9819 ? (big_endian ? "elf64ppc" : "elf64lppc")
9820 : (big_endian ? "elf32ppc" : "elf32lppc")))
9824 do_instantiate_target()
9825 { return new Target_powerpc<size, big_endian>(); }
9828 Target_selector_powerpc<32, true> target_selector_ppc32;
9829 Target_selector_powerpc<32, false> target_selector_ppc32le;
9830 Target_selector_powerpc<64, true> target_selector_ppc64;
9831 Target_selector_powerpc<64, false> target_selector_ppc64le;
9833 // Instantiate these constants for -O0
9834 template<int size, bool big_endian>
9835 const int Output_data_glink<size, big_endian>::pltresolve_size;
9836 template<int size, bool big_endian>
9837 const typename Output_data_glink<size, big_endian>::Address
9838 Output_data_glink<size, big_endian>::invalid_address;
9839 template<int size, bool big_endian>
9840 const typename Stub_table<size, big_endian>::Address
9841 Stub_table<size, big_endian>::invalid_address;
9842 template<int size, bool big_endian>
9843 const typename Target_powerpc<size, big_endian>::Address
9844 Target_powerpc<size, big_endian>::invalid_address;
9846 } // End anonymous namespace.