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 toc save stack slot
1070 { return this->abiversion() < 2 ? 40 : 24; }
1086 : tls_get_addr_state_(NOT_EXPECTED),
1087 relinfo_(NULL), relnum_(0), r_offset_(0)
1092 if (this->tls_get_addr_state_ != 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_state_ = 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_state_ = EXPECTED; }
1121 skip_next_tls_get_addr_call()
1122 {this->tls_get_addr_state_ = 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_state_;
1132 this->tls_get_addr_state_ = 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_state_;
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 || !parameters->options().tls_optimize())
1328 return tls::TLSOPT_NONE;
1331 return tls::TLSOPT_TO_IE;
1332 return tls::TLSOPT_TO_LE;
1335 tls::Tls_optimization
1338 if (parameters->options().shared()
1339 || !parameters->options().tls_optimize())
1340 return tls::TLSOPT_NONE;
1342 return tls::TLSOPT_TO_LE;
1345 tls::Tls_optimization
1346 optimize_tls_ie(bool is_final)
1349 || parameters->options().shared()
1350 || !parameters->options().tls_optimize())
1351 return tls::TLSOPT_NONE;
1353 return tls::TLSOPT_TO_LE;
1358 make_glink_section(Layout*);
1360 // Create the PLT section.
1362 make_plt_section(Symbol_table*, Layout*);
1365 make_iplt_section(Symbol_table*, Layout*);
1368 make_brlt_section(Layout*);
1370 // Create a PLT entry for a global symbol.
1372 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1374 // Create a PLT entry for a local IFUNC symbol.
1376 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1377 Sized_relobj_file<size, big_endian>*,
1381 // Create a GOT entry for local dynamic __tls_get_addr.
1383 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1384 Sized_relobj_file<size, big_endian>* object);
1387 tlsld_got_offset() const
1389 return this->tlsld_got_offset_;
1392 // Get the dynamic reloc section, creating it if necessary.
1394 rela_dyn_section(Layout*);
1396 // Similarly, but for ifunc symbols get the one for ifunc.
1398 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1400 // Copy a relocation against a global symbol.
1402 copy_reloc(Symbol_table* symtab, Layout* layout,
1403 Sized_relobj_file<size, big_endian>* object,
1404 unsigned int shndx, Output_section* output_section,
1405 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1407 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1408 this->copy_relocs_.copy_reloc(symtab, layout,
1409 symtab->get_sized_symbol<size>(sym),
1410 object, shndx, output_section,
1411 r_type, reloc.get_r_offset(),
1412 reloc.get_r_addend(),
1413 this->rela_dyn_section(layout));
1416 // Look over all the input sections, deciding where to place stubs.
1418 group_sections(Layout*, const Task*, bool);
1420 // Sort output sections by address.
1421 struct Sort_sections
1424 operator()(const Output_section* sec1, const Output_section* sec2)
1425 { return sec1->address() < sec2->address(); }
1431 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1432 unsigned int data_shndx,
1434 unsigned int r_type,
1437 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1438 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
1444 // Return whether this branch is going via a plt call stub, and if
1445 // so, mark it as having an R_PPC64_TOCSAVE.
1447 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1448 unsigned int shndx, Address offset,
1449 Target_powerpc* target, Symbol_table* symtab);
1451 // If this branch needs a plt call stub, or a long branch stub, make one.
1453 make_stub(Stub_table<size, big_endian>*,
1454 Stub_table<size, big_endian>*,
1455 Symbol_table*) const;
1458 // The branch location..
1459 Powerpc_relobj<size, big_endian>* object_;
1460 unsigned int shndx_;
1462 // ..and the branch type and destination.
1463 unsigned int r_type_ : 31;
1464 unsigned int tocsave_ : 1;
1465 unsigned int r_sym_;
1469 // Information about this specific target which we pass to the
1470 // general Target structure.
1471 static Target::Target_info powerpc_info;
1473 // The types of GOT entries needed for this platform.
1474 // These values are exposed to the ABI in an incremental link.
1475 // Do not renumber existing values without changing the version
1476 // number of the .gnu_incremental_inputs section.
1480 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1481 GOT_TYPE_DTPREL, // entry for @got@dtprel
1482 GOT_TYPE_TPREL // entry for @got@tprel
1486 Output_data_got_powerpc<size, big_endian>* got_;
1487 // The PLT section. This is a container for a table of addresses,
1488 // and their relocations. Each address in the PLT has a dynamic
1489 // relocation (R_*_JMP_SLOT) and each address will have a
1490 // corresponding entry in .glink for lazy resolution of the PLT.
1491 // ppc32 initialises the PLT to point at the .glink entry, while
1492 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1493 // linker adds a stub that loads the PLT entry into ctr then
1494 // branches to ctr. There may be more than one stub for each PLT
1495 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1496 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1497 Output_data_plt_powerpc<size, big_endian>* plt_;
1498 // The IPLT section. Like plt_, this is a container for a table of
1499 // addresses and their relocations, specifically for STT_GNU_IFUNC
1500 // functions that resolve locally (STT_GNU_IFUNC functions that
1501 // don't resolve locally go in PLT). Unlike plt_, these have no
1502 // entry in .glink for lazy resolution, and the relocation section
1503 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1504 // the relocation section may contain relocations against
1505 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1506 // relocation section will appear at the end of other dynamic
1507 // relocations, so that ld.so applies these relocations after other
1508 // dynamic relocations. In a static executable, the relocation
1509 // section is emitted and marked with __rela_iplt_start and
1510 // __rela_iplt_end symbols.
1511 Output_data_plt_powerpc<size, big_endian>* iplt_;
1512 // Section holding long branch destinations.
1513 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1514 // The .glink section.
1515 Output_data_glink<size, big_endian>* glink_;
1516 // The dynamic reloc section.
1517 Reloc_section* rela_dyn_;
1518 // Relocs saved to avoid a COPY reloc.
1519 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1520 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1521 unsigned int tlsld_got_offset_;
1523 Stub_tables stub_tables_;
1524 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1525 Branch_lookup_table branch_lookup_table_;
1527 typedef std::vector<Branch_info> Branches;
1528 Branches branch_info_;
1529 Tocsave_loc tocsave_loc_;
1531 bool plt_thread_safe_;
1532 bool plt_localentry0_;
1533 bool plt_localentry0_init_;
1534 bool has_localentry0_;
1537 int relax_fail_count_;
1538 int32_t stub_group_size_;
1540 Output_data_save_res<size, big_endian> *savres_section_;
1544 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1547 true, // is_big_endian
1548 elfcpp::EM_PPC, // machine_code
1549 false, // has_make_symbol
1550 false, // has_resolve
1551 false, // has_code_fill
1552 true, // is_default_stack_executable
1553 false, // can_icf_inline_merge_sections
1555 "/usr/lib/ld.so.1", // dynamic_linker
1556 0x10000000, // default_text_segment_address
1557 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1558 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1559 false, // isolate_execinstr
1561 elfcpp::SHN_UNDEF, // small_common_shndx
1562 elfcpp::SHN_UNDEF, // large_common_shndx
1563 0, // small_common_section_flags
1564 0, // large_common_section_flags
1565 NULL, // attributes_section
1566 NULL, // attributes_vendor
1567 "_start", // entry_symbol_name
1568 32, // hash_entry_size
1572 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1575 false, // is_big_endian
1576 elfcpp::EM_PPC, // machine_code
1577 false, // has_make_symbol
1578 false, // has_resolve
1579 false, // has_code_fill
1580 true, // is_default_stack_executable
1581 false, // can_icf_inline_merge_sections
1583 "/usr/lib/ld.so.1", // dynamic_linker
1584 0x10000000, // default_text_segment_address
1585 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1586 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1587 false, // isolate_execinstr
1589 elfcpp::SHN_UNDEF, // small_common_shndx
1590 elfcpp::SHN_UNDEF, // large_common_shndx
1591 0, // small_common_section_flags
1592 0, // large_common_section_flags
1593 NULL, // attributes_section
1594 NULL, // attributes_vendor
1595 "_start", // entry_symbol_name
1596 32, // hash_entry_size
1600 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1603 true, // is_big_endian
1604 elfcpp::EM_PPC64, // machine_code
1605 false, // has_make_symbol
1606 false, // has_resolve
1607 false, // has_code_fill
1608 true, // is_default_stack_executable
1609 false, // can_icf_inline_merge_sections
1611 "/usr/lib/ld.so.1", // dynamic_linker
1612 0x10000000, // default_text_segment_address
1613 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1614 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1615 false, // isolate_execinstr
1617 elfcpp::SHN_UNDEF, // small_common_shndx
1618 elfcpp::SHN_UNDEF, // large_common_shndx
1619 0, // small_common_section_flags
1620 0, // large_common_section_flags
1621 NULL, // attributes_section
1622 NULL, // attributes_vendor
1623 "_start", // entry_symbol_name
1624 32, // hash_entry_size
1628 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1631 false, // is_big_endian
1632 elfcpp::EM_PPC64, // machine_code
1633 false, // has_make_symbol
1634 false, // has_resolve
1635 false, // has_code_fill
1636 true, // is_default_stack_executable
1637 false, // can_icf_inline_merge_sections
1639 "/usr/lib/ld.so.1", // dynamic_linker
1640 0x10000000, // default_text_segment_address
1641 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1642 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1643 false, // isolate_execinstr
1645 elfcpp::SHN_UNDEF, // small_common_shndx
1646 elfcpp::SHN_UNDEF, // large_common_shndx
1647 0, // small_common_section_flags
1648 0, // large_common_section_flags
1649 NULL, // attributes_section
1650 NULL, // attributes_vendor
1651 "_start", // entry_symbol_name
1652 32, // hash_entry_size
1656 is_branch_reloc(unsigned int r_type)
1658 return (r_type == elfcpp::R_POWERPC_REL24
1659 || r_type == elfcpp::R_PPC_PLTREL24
1660 || r_type == elfcpp::R_PPC_LOCAL24PC
1661 || r_type == elfcpp::R_POWERPC_REL14
1662 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1663 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1664 || r_type == elfcpp::R_POWERPC_ADDR24
1665 || r_type == elfcpp::R_POWERPC_ADDR14
1666 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1667 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1670 // If INSN is an opcode that may be used with an @tls operand, return
1671 // the transformed insn for TLS optimisation, otherwise return 0. If
1672 // REG is non-zero only match an insn with RB or RA equal to REG.
1674 at_tls_transform(uint32_t insn, unsigned int reg)
1676 if ((insn & (0x3f << 26)) != 31 << 26)
1680 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1681 rtra = insn & ((1 << 26) - (1 << 16));
1682 else if (((insn >> 16) & 0x1f) == reg)
1683 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1687 if ((insn & (0x3ff << 1)) == 266 << 1)
1690 else if ((insn & (0x1f << 1)) == 23 << 1
1691 && ((insn & (0x1f << 6)) < 14 << 6
1692 || ((insn & (0x1f << 6)) >= 16 << 6
1693 && (insn & (0x1f << 6)) < 24 << 6)))
1694 // load and store indexed -> dform
1695 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1696 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1697 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1698 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1699 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1701 insn = (58 << 26) | 2;
1709 template<int size, bool big_endian>
1710 class Powerpc_relocate_functions
1730 typedef Powerpc_relocate_functions<size, big_endian> This;
1731 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1732 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
1734 template<int valsize>
1736 has_overflow_signed(Address value)
1738 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1739 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1740 limit <<= ((valsize - 1) >> 1);
1741 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1742 return value + limit > (limit << 1) - 1;
1745 template<int valsize>
1747 has_overflow_unsigned(Address value)
1749 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1750 limit <<= ((valsize - 1) >> 1);
1751 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1752 return value > (limit << 1) - 1;
1755 template<int valsize>
1757 has_overflow_bitfield(Address value)
1759 return (has_overflow_unsigned<valsize>(value)
1760 && has_overflow_signed<valsize>(value));
1763 template<int valsize>
1764 static inline Status
1765 overflowed(Address value, Overflow_check overflow)
1767 if (overflow == CHECK_SIGNED)
1769 if (has_overflow_signed<valsize>(value))
1770 return STATUS_OVERFLOW;
1772 else if (overflow == CHECK_UNSIGNED)
1774 if (has_overflow_unsigned<valsize>(value))
1775 return STATUS_OVERFLOW;
1777 else if (overflow == CHECK_BITFIELD)
1779 if (has_overflow_bitfield<valsize>(value))
1780 return STATUS_OVERFLOW;
1785 // Do a simple RELA relocation
1786 template<int fieldsize, int valsize>
1787 static inline Status
1788 rela(unsigned char* view, Address value, Overflow_check overflow)
1790 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1791 Valtype* wv = reinterpret_cast<Valtype*>(view);
1792 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
1793 return overflowed<valsize>(value, overflow);
1796 template<int fieldsize, int valsize>
1797 static inline Status
1798 rela(unsigned char* view,
1799 unsigned int right_shift,
1800 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1802 Overflow_check overflow)
1804 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1805 Valtype* wv = reinterpret_cast<Valtype*>(view);
1806 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
1807 Valtype reloc = value >> right_shift;
1810 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
1811 return overflowed<valsize>(value >> right_shift, overflow);
1814 // Do a simple RELA relocation, unaligned.
1815 template<int fieldsize, int valsize>
1816 static inline Status
1817 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1819 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
1820 return overflowed<valsize>(value, overflow);
1823 template<int fieldsize, int valsize>
1824 static inline Status
1825 rela_ua(unsigned char* view,
1826 unsigned int right_shift,
1827 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1829 Overflow_check overflow)
1831 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
1833 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
1834 Valtype reloc = value >> right_shift;
1837 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
1838 return overflowed<valsize>(value >> right_shift, overflow);
1842 // R_PPC64_ADDR64: (Symbol + Addend)
1844 addr64(unsigned char* view, Address value)
1845 { This::template rela<64,64>(view, value, CHECK_NONE); }
1847 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1849 addr64_u(unsigned char* view, Address value)
1850 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
1852 // R_POWERPC_ADDR32: (Symbol + Addend)
1853 static inline Status
1854 addr32(unsigned char* view, Address value, Overflow_check overflow)
1855 { return This::template rela<32,32>(view, value, overflow); }
1857 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1858 static inline Status
1859 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1860 { return This::template rela_ua<32,32>(view, value, overflow); }
1862 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1863 static inline Status
1864 addr24(unsigned char* view, Address value, Overflow_check overflow)
1866 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
1868 if (overflow != CHECK_NONE && (value & 3) != 0)
1869 stat = STATUS_OVERFLOW;
1873 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1874 static inline Status
1875 addr16(unsigned char* view, Address value, Overflow_check overflow)
1876 { return This::template rela<16,16>(view, value, overflow); }
1878 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1879 static inline Status
1880 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1881 { return This::template rela_ua<16,16>(view, value, overflow); }
1883 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1884 static inline Status
1885 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1887 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
1888 if ((value & 3) != 0)
1889 stat = STATUS_OVERFLOW;
1893 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
1894 static inline Status
1895 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
1897 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
1898 if ((value & 15) != 0)
1899 stat = STATUS_OVERFLOW;
1903 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1905 addr16_hi(unsigned char* view, Address value)
1906 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
1908 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1910 addr16_ha(unsigned char* view, Address value)
1911 { This::addr16_hi(view, value + 0x8000); }
1913 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1915 addr16_hi2(unsigned char* view, Address value)
1916 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
1918 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1920 addr16_ha2(unsigned char* view, Address value)
1921 { This::addr16_hi2(view, value + 0x8000); }
1923 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1925 addr16_hi3(unsigned char* view, Address value)
1926 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
1928 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1930 addr16_ha3(unsigned char* view, Address value)
1931 { This::addr16_hi3(view, value + 0x8000); }
1933 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1934 static inline Status
1935 addr14(unsigned char* view, Address value, Overflow_check overflow)
1937 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
1938 if (overflow != CHECK_NONE && (value & 3) != 0)
1939 stat = STATUS_OVERFLOW;
1943 // R_POWERPC_REL16DX_HA
1944 static inline Status
1945 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
1947 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1948 Valtype* wv = reinterpret_cast<Valtype*>(view);
1949 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1951 value = static_cast<SignedAddress>(value) >> 16;
1952 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
1953 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1954 return overflowed<16>(value, overflow);
1958 // Set ABI version for input and output.
1960 template<int size, bool big_endian>
1962 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
1964 this->e_flags_ |= ver;
1965 if (this->abiversion() != 0)
1967 Target_powerpc<size, big_endian>* target =
1968 static_cast<Target_powerpc<size, big_endian>*>(
1969 parameters->sized_target<size, big_endian>());
1970 if (target->abiversion() == 0)
1971 target->set_abiversion(this->abiversion());
1972 else if (target->abiversion() != this->abiversion())
1973 gold_error(_("%s: ABI version %d is not compatible "
1974 "with ABI version %d output"),
1975 this->name().c_str(),
1976 this->abiversion(), target->abiversion());
1981 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
1982 // relocatable object, if such sections exists.
1984 template<int size, bool big_endian>
1986 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1987 Read_symbols_data* sd)
1989 const unsigned char* const pshdrs = sd->section_headers->data();
1990 const unsigned char* namesu = sd->section_names->data();
1991 const char* names = reinterpret_cast<const char*>(namesu);
1992 section_size_type names_size = sd->section_names_size;
1993 const unsigned char* s;
1995 s = this->template find_shdr<size, big_endian>(pshdrs,
1996 size == 32 ? ".got2" : ".opd",
1997 names, names_size, NULL);
2000 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2001 this->special_ = ndx;
2004 if (this->abiversion() == 0)
2005 this->set_abiversion(1);
2006 else if (this->abiversion() > 1)
2007 gold_error(_("%s: .opd invalid in abiv%d"),
2008 this->name().c_str(), this->abiversion());
2013 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2014 names, names_size, NULL);
2017 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2018 this->relatoc_ = ndx;
2019 typename elfcpp::Shdr<size, big_endian> shdr(s);
2020 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2023 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2026 // Examine .rela.opd to build info about function entry points.
2028 template<int size, bool big_endian>
2030 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2032 const unsigned char* prelocs,
2033 const unsigned char* plocal_syms)
2037 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2038 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2039 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2040 Address expected_off = 0;
2041 bool regular = true;
2042 unsigned int opd_ent_size = 0;
2044 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2046 Reltype reloc(prelocs);
2047 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2048 = reloc.get_r_info();
2049 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2050 if (r_type == elfcpp::R_PPC64_ADDR64)
2052 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2053 typename elfcpp::Elf_types<size>::Elf_Addr value;
2056 if (r_sym < this->local_symbol_count())
2058 typename elfcpp::Sym<size, big_endian>
2059 lsym(plocal_syms + r_sym * sym_size);
2060 shndx = lsym.get_st_shndx();
2061 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2062 value = lsym.get_st_value();
2065 shndx = this->symbol_section_and_value(r_sym, &value,
2067 this->set_opd_ent(reloc.get_r_offset(), shndx,
2068 value + reloc.get_r_addend());
2071 expected_off = reloc.get_r_offset();
2072 opd_ent_size = expected_off;
2074 else if (expected_off != reloc.get_r_offset())
2076 expected_off += opd_ent_size;
2078 else if (r_type == elfcpp::R_PPC64_TOC)
2080 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2085 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2086 this->name().c_str(), r_type);
2090 if (reloc_count <= 2)
2091 opd_ent_size = this->section_size(this->opd_shndx());
2092 if (opd_ent_size != 24 && opd_ent_size != 16)
2096 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2097 this->name().c_str());
2103 // Returns true if a code sequence loading the TOC entry at VALUE
2104 // relative to the TOC pointer can be converted into code calculating
2105 // a TOC pointer relative offset.
2106 // If so, the TOC pointer relative offset is stored to VALUE.
2108 template<int size, bool big_endian>
2110 Powerpc_relobj<size, big_endian>::make_toc_relative(
2111 Target_powerpc<size, big_endian>* target,
2117 // With -mcmodel=medium code it is quite possible to have
2118 // toc-relative relocs referring to objects outside the TOC.
2119 // Don't try to look at a non-existent TOC.
2120 if (this->toc_shndx() == 0)
2123 // Convert VALUE back to an address by adding got_base (see below),
2124 // then to an offset in the TOC by subtracting the TOC output
2125 // section address and the TOC output offset. Since this TOC output
2126 // section and the got output section are one and the same, we can
2127 // omit adding and subtracting the output section address.
2128 Address off = (*value + this->toc_base_offset()
2129 - this->output_section_offset(this->toc_shndx()));
2130 // Is this offset in the TOC? -mcmodel=medium code may be using
2131 // TOC relative access to variables outside the TOC. Those of
2132 // course can't be optimized. We also don't try to optimize code
2133 // that is using a different object's TOC.
2134 if (off >= this->section_size(this->toc_shndx()))
2137 if (this->no_toc_opt(off))
2140 section_size_type vlen;
2141 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2142 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2144 Address got_base = (target->got_section()->output_section()->address()
2145 + this->toc_base_offset());
2147 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2154 // Perform the Sized_relobj_file method, then set up opd info from
2157 template<int size, bool big_endian>
2159 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2161 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2164 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2165 p != rd->relocs.end();
2168 if (p->data_shndx == this->opd_shndx())
2170 uint64_t opd_size = this->section_size(this->opd_shndx());
2171 gold_assert(opd_size == static_cast<size_t>(opd_size));
2174 this->init_opd(opd_size);
2175 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2176 rd->local_symbols->data());
2184 // Read the symbols then set up st_other vector.
2186 template<int size, bool big_endian>
2188 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2190 this->base_read_symbols(sd);
2193 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2194 const unsigned char* const pshdrs = sd->section_headers->data();
2195 const unsigned int loccount = this->do_local_symbol_count();
2198 this->st_other_.resize(loccount);
2199 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2200 off_t locsize = loccount * sym_size;
2201 const unsigned int symtab_shndx = this->symtab_shndx();
2202 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2203 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2204 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2205 locsize, true, false);
2207 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2209 elfcpp::Sym<size, big_endian> sym(psyms);
2210 unsigned char st_other = sym.get_st_other();
2211 this->st_other_[i] = st_other;
2212 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2214 if (this->abiversion() == 0)
2215 this->set_abiversion(2);
2216 else if (this->abiversion() < 2)
2217 gold_error(_("%s: local symbol %d has invalid st_other"
2218 " for ABI version 1"),
2219 this->name().c_str(), i);
2226 template<int size, bool big_endian>
2228 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2230 this->e_flags_ |= ver;
2231 if (this->abiversion() != 0)
2233 Target_powerpc<size, big_endian>* target =
2234 static_cast<Target_powerpc<size, big_endian>*>(
2235 parameters->sized_target<size, big_endian>());
2236 if (target->abiversion() == 0)
2237 target->set_abiversion(this->abiversion());
2238 else if (target->abiversion() != this->abiversion())
2239 gold_error(_("%s: ABI version %d is not compatible "
2240 "with ABI version %d output"),
2241 this->name().c_str(),
2242 this->abiversion(), target->abiversion());
2247 // Call Sized_dynobj::base_read_symbols to read the symbols then
2248 // read .opd from a dynamic object, filling in opd_ent_ vector,
2250 template<int size, bool big_endian>
2252 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2254 this->base_read_symbols(sd);
2257 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2258 const unsigned char* const pshdrs = sd->section_headers->data();
2259 const unsigned char* namesu = sd->section_names->data();
2260 const char* names = reinterpret_cast<const char*>(namesu);
2261 const unsigned char* s = NULL;
2262 const unsigned char* opd;
2263 section_size_type opd_size;
2265 // Find and read .opd section.
2268 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2269 sd->section_names_size,
2274 typename elfcpp::Shdr<size, big_endian> shdr(s);
2275 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2276 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2278 if (this->abiversion() == 0)
2279 this->set_abiversion(1);
2280 else if (this->abiversion() > 1)
2281 gold_error(_("%s: .opd invalid in abiv%d"),
2282 this->name().c_str(), this->abiversion());
2284 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2285 this->opd_address_ = shdr.get_sh_addr();
2286 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2287 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2293 // Build set of executable sections.
2294 // Using a set is probably overkill. There is likely to be only
2295 // a few executable sections, typically .init, .text and .fini,
2296 // and they are generally grouped together.
2297 typedef std::set<Sec_info> Exec_sections;
2298 Exec_sections exec_sections;
2300 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2302 typename elfcpp::Shdr<size, big_endian> shdr(s);
2303 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2304 && ((shdr.get_sh_flags()
2305 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2306 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2307 && shdr.get_sh_size() != 0)
2309 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2310 shdr.get_sh_size(), i));
2313 if (exec_sections.empty())
2316 // Look over the OPD entries. This is complicated by the fact
2317 // that some binaries will use two-word entries while others
2318 // will use the standard three-word entries. In most cases
2319 // the third word (the environment pointer for languages like
2320 // Pascal) is unused and will be zero. If the third word is
2321 // used it should not be pointing into executable sections,
2323 this->init_opd(opd_size);
2324 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2326 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2327 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2328 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2330 // Chances are that this is the third word of an OPD entry.
2332 typename Exec_sections::const_iterator e
2333 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2334 if (e != exec_sections.begin())
2337 if (e->start <= val && val < e->start + e->len)
2339 // We have an address in an executable section.
2340 // VAL ought to be the function entry, set it up.
2341 this->set_opd_ent(p - opd, e->shndx, val);
2342 // Skip second word of OPD entry, the TOC pointer.
2346 // If we didn't match any executable sections, we likely
2347 // have a non-zero third word in the OPD entry.
2352 // Relocate sections.
2354 template<int size, bool big_endian>
2356 Powerpc_relobj<size, big_endian>::do_relocate_sections(
2357 const Symbol_table* symtab, const Layout* layout,
2358 const unsigned char* pshdrs, Output_file* of,
2359 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2361 unsigned int start = 1;
2363 && this->relatoc_ != 0
2364 && !parameters->options().relocatable())
2366 // Relocate .toc first.
2367 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2368 this->relatoc_, this->relatoc_);
2369 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2370 1, this->relatoc_ - 1);
2371 start = this->relatoc_ + 1;
2373 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2374 start, this->shnum() - 1);
2377 // Set up some symbols.
2379 template<int size, bool big_endian>
2381 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2382 Symbol_table* symtab,
2387 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2388 // undefined when scanning relocs (and thus requires
2389 // non-relative dynamic relocs). The proper value will be
2391 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2392 if (gotsym != NULL && gotsym->is_undefined())
2394 Target_powerpc<size, big_endian>* target =
2395 static_cast<Target_powerpc<size, big_endian>*>(
2396 parameters->sized_target<size, big_endian>());
2397 Output_data_got_powerpc<size, big_endian>* got
2398 = target->got_section(symtab, layout);
2399 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2400 Symbol_table::PREDEFINED,
2404 elfcpp::STV_HIDDEN, 0,
2408 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2409 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2410 if (sdasym != NULL && sdasym->is_undefined())
2412 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2414 = layout->add_output_section_data(".sdata", 0,
2416 | elfcpp::SHF_WRITE,
2417 sdata, ORDER_SMALL_DATA, false);
2418 symtab->define_in_output_data("_SDA_BASE_", NULL,
2419 Symbol_table::PREDEFINED,
2420 os, 32768, 0, elfcpp::STT_OBJECT,
2421 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2427 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2428 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2429 if (gotsym != NULL && gotsym->is_undefined())
2431 Target_powerpc<size, big_endian>* target =
2432 static_cast<Target_powerpc<size, big_endian>*>(
2433 parameters->sized_target<size, big_endian>());
2434 Output_data_got_powerpc<size, big_endian>* got
2435 = target->got_section(symtab, layout);
2436 symtab->define_in_output_data(".TOC.", NULL,
2437 Symbol_table::PREDEFINED,
2441 elfcpp::STV_HIDDEN, 0,
2447 // Set up PowerPC target specific relobj.
2449 template<int size, bool big_endian>
2451 Target_powerpc<size, big_endian>::do_make_elf_object(
2452 const std::string& name,
2453 Input_file* input_file,
2454 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2456 int et = ehdr.get_e_type();
2457 // ET_EXEC files are valid input for --just-symbols/-R,
2458 // and we treat them as relocatable objects.
2459 if (et == elfcpp::ET_REL
2460 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2462 Powerpc_relobj<size, big_endian>* obj =
2463 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2467 else if (et == elfcpp::ET_DYN)
2469 Powerpc_dynobj<size, big_endian>* obj =
2470 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2476 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2481 template<int size, bool big_endian>
2482 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2485 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2486 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2488 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2489 : Output_data_got<size, big_endian>(),
2490 symtab_(symtab), layout_(layout),
2491 header_ent_cnt_(size == 32 ? 3 : 1),
2492 header_index_(size == 32 ? 0x2000 : 0)
2495 this->set_addralign(256);
2498 // Override all the Output_data_got methods we use so as to first call
2501 add_global(Symbol* gsym, unsigned int got_type)
2503 this->reserve_ent();
2504 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2508 add_global_plt(Symbol* gsym, unsigned int got_type)
2510 this->reserve_ent();
2511 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2515 add_global_tls(Symbol* gsym, unsigned int got_type)
2516 { return this->add_global_plt(gsym, got_type); }
2519 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2520 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2522 this->reserve_ent();
2523 Output_data_got<size, big_endian>::
2524 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2528 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2529 Output_data_reloc_generic* rel_dyn,
2530 unsigned int r_type_1, unsigned int r_type_2)
2532 if (gsym->has_got_offset(got_type))
2535 this->reserve_ent(2);
2536 Output_data_got<size, big_endian>::
2537 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2541 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2543 this->reserve_ent();
2544 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2549 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2551 this->reserve_ent();
2552 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2557 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2558 { return this->add_local_plt(object, sym_index, got_type); }
2561 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2562 unsigned int got_type,
2563 Output_data_reloc_generic* rel_dyn,
2564 unsigned int r_type)
2566 if (object->local_has_got_offset(sym_index, got_type))
2569 this->reserve_ent(2);
2570 Output_data_got<size, big_endian>::
2571 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2575 add_constant(Valtype constant)
2577 this->reserve_ent();
2578 return Output_data_got<size, big_endian>::add_constant(constant);
2582 add_constant_pair(Valtype c1, Valtype c2)
2584 this->reserve_ent(2);
2585 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
2588 // Offset of _GLOBAL_OFFSET_TABLE_.
2592 return this->got_offset(this->header_index_);
2595 // Offset of base used to access the GOT/TOC.
2596 // The got/toc pointer reg will be set to this value.
2598 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2601 return this->g_o_t();
2603 return (this->output_section()->address()
2604 + object->toc_base_offset()
2608 // Ensure our GOT has a header.
2610 set_final_data_size()
2612 if (this->header_ent_cnt_ != 0)
2613 this->make_header();
2614 Output_data_got<size, big_endian>::set_final_data_size();
2617 // First word of GOT header needs some values that are not
2618 // handled by Output_data_got so poke them in here.
2619 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2621 do_write(Output_file* of)
2624 if (size == 32 && this->layout_->dynamic_data() != NULL)
2625 val = this->layout_->dynamic_section()->address();
2627 val = this->output_section()->address() + 0x8000;
2628 this->replace_constant(this->header_index_, val);
2629 Output_data_got<size, big_endian>::do_write(of);
2634 reserve_ent(unsigned int cnt = 1)
2636 if (this->header_ent_cnt_ == 0)
2638 if (this->num_entries() + cnt > this->header_index_)
2639 this->make_header();
2645 this->header_ent_cnt_ = 0;
2646 this->header_index_ = this->num_entries();
2649 Output_data_got<size, big_endian>::add_constant(0);
2650 Output_data_got<size, big_endian>::add_constant(0);
2651 Output_data_got<size, big_endian>::add_constant(0);
2653 // Define _GLOBAL_OFFSET_TABLE_ at the header
2654 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2657 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
2658 sym->set_value(this->g_o_t());
2661 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2662 Symbol_table::PREDEFINED,
2663 this, this->g_o_t(), 0,
2666 elfcpp::STV_HIDDEN, 0,
2670 Output_data_got<size, big_endian>::add_constant(0);
2673 // Stashed pointers.
2674 Symbol_table* symtab_;
2678 unsigned int header_ent_cnt_;
2679 // GOT header index.
2680 unsigned int header_index_;
2683 // Get the GOT section, creating it if necessary.
2685 template<int size, bool big_endian>
2686 Output_data_got_powerpc<size, big_endian>*
2687 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
2690 if (this->got_ == NULL)
2692 gold_assert(symtab != NULL && layout != NULL);
2695 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
2697 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2698 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2699 this->got_, ORDER_DATA, false);
2705 // Get the dynamic reloc section, creating it if necessary.
2707 template<int size, bool big_endian>
2708 typename Target_powerpc<size, big_endian>::Reloc_section*
2709 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
2711 if (this->rela_dyn_ == NULL)
2713 gold_assert(layout != NULL);
2714 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2715 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2716 elfcpp::SHF_ALLOC, this->rela_dyn_,
2717 ORDER_DYNAMIC_RELOCS, false);
2719 return this->rela_dyn_;
2722 // Similarly, but for ifunc symbols get the one for ifunc.
2724 template<int size, bool big_endian>
2725 typename Target_powerpc<size, big_endian>::Reloc_section*
2726 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
2731 return this->rela_dyn_section(layout);
2733 if (this->iplt_ == NULL)
2734 this->make_iplt_section(symtab, layout);
2735 return this->iplt_->rel_plt();
2741 // Determine the stub group size. The group size is the absolute
2742 // value of the parameter --stub-group-size. If --stub-group-size
2743 // is passed a negative value, we restrict stubs to be always after
2744 // the stubbed branches.
2745 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
2746 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
2747 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
2748 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
2749 owner_(NULL), output_section_(NULL)
2753 // Return true iff input section can be handled by current stub
2756 can_add_to_stub_group(Output_section* o,
2757 const Output_section::Input_section* i,
2760 const Output_section::Input_section*
2766 { return output_section_; }
2769 set_output_and_owner(Output_section* o,
2770 const Output_section::Input_section* i)
2772 this->output_section_ = o;
2781 // Adding group sections before the stubs.
2782 FINDING_STUB_SECTION,
2783 // Adding group sections after the stubs.
2787 uint32_t stub_group_size_;
2788 bool stubs_always_after_branch_;
2789 bool suppress_size_errors_;
2790 // True if a stub group can serve multiple output sections.
2793 // Current max size of group. Starts at stub_group_size_ but is
2794 // reduced to stub_group_size_/1024 on seeing a section with
2795 // external conditional branches.
2796 uint32_t group_size_;
2797 uint64_t group_start_addr_;
2798 // owner_ and output_section_ specify the section to which stubs are
2799 // attached. The stubs are placed at the end of this section.
2800 const Output_section::Input_section* owner_;
2801 Output_section* output_section_;
2804 // Return true iff input section can be handled by current stub
2805 // group. Sections are presented to this function in order,
2806 // so the first section is the head of the group.
2809 Stub_control::can_add_to_stub_group(Output_section* o,
2810 const Output_section::Input_section* i,
2813 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
2815 uint64_t start_addr = o->address();
2818 // .init and .fini sections are pasted together to form a single
2819 // function. We can't be adding stubs in the middle of the function.
2820 this_size = o->data_size();
2823 start_addr += i->relobj()->output_section_offset(i->shndx());
2824 this_size = i->data_size();
2827 uint64_t end_addr = start_addr + this_size;
2828 uint32_t group_size = this->stub_group_size_;
2830 this->group_size_ = group_size = group_size >> 10;
2832 if (this_size > group_size && !this->suppress_size_errors_)
2833 gold_warning(_("%s:%s exceeds group size"),
2834 i->relobj()->name().c_str(),
2835 i->relobj()->section_name(i->shndx()).c_str());
2837 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
2838 has14 ? " 14bit" : "",
2839 i->relobj()->name().c_str(),
2840 i->relobj()->section_name(i->shndx()).c_str(),
2841 (long long) this_size,
2842 (this->state_ == NO_GROUP
2844 : (long long) end_addr - this->group_start_addr_));
2846 if (this->state_ == NO_GROUP)
2848 // Only here on very first use of Stub_control
2850 this->output_section_ = o;
2851 this->state_ = FINDING_STUB_SECTION;
2852 this->group_size_ = group_size;
2853 this->group_start_addr_ = start_addr;
2856 else if (!this->multi_os_ && this->output_section_ != o)
2858 else if (this->state_ == HAS_STUB_SECTION)
2860 // Can we add this section, which is after the stubs, to the
2862 if (end_addr - this->group_start_addr_ <= this->group_size_)
2865 else if (this->state_ == FINDING_STUB_SECTION)
2867 if ((whole_sec && this->output_section_ == o)
2868 || end_addr - this->group_start_addr_ <= this->group_size_)
2870 // Stubs are added at the end of "owner_".
2872 this->output_section_ = o;
2875 // The group before the stubs has reached maximum size.
2876 // Now see about adding sections after the stubs to the
2877 // group. If the current section has a 14-bit branch and
2878 // the group before the stubs exceeds group_size_ (because
2879 // they didn't have 14-bit branches), don't add sections
2880 // after the stubs: The size of stubs for such a large
2881 // group may exceed the reach of a 14-bit branch.
2882 if (!this->stubs_always_after_branch_
2883 && this_size <= this->group_size_
2884 && start_addr - this->group_start_addr_ <= this->group_size_)
2886 gold_debug(DEBUG_TARGET, "adding after stubs");
2887 this->state_ = HAS_STUB_SECTION;
2888 this->group_start_addr_ = start_addr;
2895 gold_debug(DEBUG_TARGET,
2896 !this->multi_os_ && this->output_section_ != o
2897 ? "nope, new output section\n"
2898 : "nope, didn't fit\n");
2900 // The section fails to fit in the current group. Set up a few
2901 // things for the next group. owner_ and output_section_ will be
2902 // set later after we've retrieved those values for the current
2904 this->state_ = FINDING_STUB_SECTION;
2905 this->group_size_ = group_size;
2906 this->group_start_addr_ = start_addr;
2910 // Look over all the input sections, deciding where to place stubs.
2912 template<int size, bool big_endian>
2914 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
2916 bool no_size_errors)
2918 Stub_control stub_control(this->stub_group_size_, no_size_errors,
2919 parameters->options().stub_group_multi());
2921 // Group input sections and insert stub table
2922 Stub_table_owner* table_owner = NULL;
2923 std::vector<Stub_table_owner*> tables;
2924 Layout::Section_list section_list;
2925 layout->get_executable_sections(§ion_list);
2926 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
2927 for (Layout::Section_list::iterator o = section_list.begin();
2928 o != section_list.end();
2931 typedef Output_section::Input_section_list Input_section_list;
2932 for (Input_section_list::const_iterator i
2933 = (*o)->input_sections().begin();
2934 i != (*o)->input_sections().end();
2937 if (i->is_input_section()
2938 || i->is_relaxed_input_section())
2940 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2941 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2942 bool has14 = ppcobj->has_14bit_branch(i->shndx());
2943 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
2945 table_owner->output_section = stub_control.output_section();
2946 table_owner->owner = stub_control.owner();
2947 stub_control.set_output_and_owner(*o, &*i);
2950 if (table_owner == NULL)
2952 table_owner = new Stub_table_owner;
2953 tables.push_back(table_owner);
2955 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
2959 if (table_owner != NULL)
2961 table_owner->output_section = stub_control.output_section();
2962 table_owner->owner = stub_control.owner();;
2964 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
2968 Stub_table<size, big_endian>* stub_table;
2970 if ((*t)->owner->is_input_section())
2971 stub_table = new Stub_table<size, big_endian>(this,
2972 (*t)->output_section,
2974 this->stub_tables_.size());
2975 else if ((*t)->owner->is_relaxed_input_section())
2976 stub_table = static_cast<Stub_table<size, big_endian>*>(
2977 (*t)->owner->relaxed_input_section());
2980 this->stub_tables_.push_back(stub_table);
2985 static unsigned long
2986 max_branch_delta (unsigned int r_type)
2988 if (r_type == elfcpp::R_POWERPC_REL14
2989 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
2990 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
2992 if (r_type == elfcpp::R_POWERPC_REL24
2993 || r_type == elfcpp::R_PPC_PLTREL24
2994 || r_type == elfcpp::R_PPC_LOCAL24PC)
2999 // Return whether this branch is going via a plt call stub.
3001 template<int size, bool big_endian>
3003 Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3004 Powerpc_relobj<size, big_endian>* ppc_object,
3007 Target_powerpc* target,
3008 Symbol_table* symtab)
3010 if (this->object_ != ppc_object
3011 || this->shndx_ != shndx
3012 || this->offset_ != offset)
3015 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3016 if (sym != NULL && sym->is_forwarder())
3017 sym = symtab->resolve_forwards(sym);
3018 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3020 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3021 && !target->is_elfv2_localentry0(gsym))
3022 : (this->object_->local_has_plt_offset(this->r_sym_)
3023 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
3031 // If this branch needs a plt call stub, or a long branch stub, make one.
3033 template<int size, bool big_endian>
3035 Target_powerpc<size, big_endian>::Branch_info::make_stub(
3036 Stub_table<size, big_endian>* stub_table,
3037 Stub_table<size, big_endian>* ifunc_stub_table,
3038 Symbol_table* symtab) const
3040 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3041 if (sym != NULL && sym->is_forwarder())
3042 sym = symtab->resolve_forwards(sym);
3043 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3044 Target_powerpc<size, big_endian>* target =
3045 static_cast<Target_powerpc<size, big_endian>*>(
3046 parameters->sized_target<size, big_endian>());
3050 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3051 : this->object_->local_has_plt_offset(this->r_sym_))
3055 && target->abiversion() >= 2
3056 && !parameters->options().output_is_position_independent()
3057 && !is_branch_reloc(this->r_type_))
3058 target->glink_section()->add_global_entry(gsym);
3061 if (stub_table == NULL)
3062 stub_table = this->object_->stub_table(this->shndx_);
3063 if (stub_table == NULL)
3065 // This is a ref from a data section to an ifunc symbol.
3066 stub_table = ifunc_stub_table;
3068 gold_assert(stub_table != NULL);
3069 Address from = this->object_->get_output_section_offset(this->shndx_);
3070 if (from != invalid_address)
3071 from += (this->object_->output_section(this->shndx_)->address()
3074 ok = stub_table->add_plt_call_entry(from,
3075 this->object_, gsym,
3076 this->r_type_, this->addend_,
3079 ok = stub_table->add_plt_call_entry(from,
3080 this->object_, this->r_sym_,
3081 this->r_type_, this->addend_,
3087 Address max_branch_offset = max_branch_delta(this->r_type_);
3088 if (max_branch_offset == 0)
3090 Address from = this->object_->get_output_section_offset(this->shndx_);
3091 gold_assert(from != invalid_address);
3092 from += (this->object_->output_section(this->shndx_)->address()
3097 switch (gsym->source())
3099 case Symbol::FROM_OBJECT:
3101 Object* symobj = gsym->object();
3102 if (symobj->is_dynamic()
3103 || symobj->pluginobj() != NULL)
3106 unsigned int shndx = gsym->shndx(&is_ordinary);
3107 if (shndx == elfcpp::SHN_UNDEF)
3112 case Symbol::IS_UNDEFINED:
3118 Symbol_table::Compute_final_value_status status;
3119 to = symtab->compute_final_value<size>(gsym, &status);
3120 if (status != Symbol_table::CFVS_OK)
3123 to += this->object_->ppc64_local_entry_offset(gsym);
3127 const Symbol_value<size>* psymval
3128 = this->object_->local_symbol(this->r_sym_);
3129 Symbol_value<size> symval;
3130 if (psymval->is_section_symbol())
3131 symval.set_is_section_symbol();
3132 typedef Sized_relobj_file<size, big_endian> ObjType;
3133 typename ObjType::Compute_final_local_value_status status
3134 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3136 if (status != ObjType::CFLV_OK
3137 || !symval.has_output_value())
3139 to = symval.value(this->object_, 0);
3141 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
3143 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3144 to += this->addend_;
3145 if (stub_table == NULL)
3146 stub_table = this->object_->stub_table(this->shndx_);
3147 if (size == 64 && target->abiversion() < 2)
3149 unsigned int dest_shndx;
3150 if (!target->symval_for_branch(symtab, gsym, this->object_,
3154 Address delta = to - from;
3155 if (delta + max_branch_offset >= 2 * max_branch_offset)
3157 if (stub_table == NULL)
3159 gold_warning(_("%s:%s: branch in non-executable section,"
3160 " no long branch stub for you"),
3161 this->object_->name().c_str(),
3162 this->object_->section_name(this->shndx_).c_str());
3165 bool save_res = (size == 64
3167 && gsym->source() == Symbol::IN_OUTPUT_DATA
3168 && gsym->output_data() == target->savres_section());
3169 ok = stub_table->add_long_branch_entry(this->object_,
3171 from, to, save_res);
3175 gold_debug(DEBUG_TARGET,
3176 "branch at %s:%s+%#lx\n"
3177 "can't reach stub attached to %s:%s",
3178 this->object_->name().c_str(),
3179 this->object_->section_name(this->shndx_).c_str(),
3180 (unsigned long) this->offset_,
3181 stub_table->relobj()->name().c_str(),
3182 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3187 // Relaxation hook. This is where we do stub generation.
3189 template<int size, bool big_endian>
3191 Target_powerpc<size, big_endian>::do_relax(int pass,
3192 const Input_objects*,
3193 Symbol_table* symtab,
3197 unsigned int prev_brlt_size = 0;
3201 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3203 && this->abiversion() < 2
3205 && !parameters->options().user_set_plt_thread_safe())
3207 static const char* const thread_starter[] =
3211 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3213 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3214 "mq_notify", "create_timer",
3219 "GOMP_parallel_start",
3220 "GOMP_parallel_loop_static",
3221 "GOMP_parallel_loop_static_start",
3222 "GOMP_parallel_loop_dynamic",
3223 "GOMP_parallel_loop_dynamic_start",
3224 "GOMP_parallel_loop_guided",
3225 "GOMP_parallel_loop_guided_start",
3226 "GOMP_parallel_loop_runtime",
3227 "GOMP_parallel_loop_runtime_start",
3228 "GOMP_parallel_sections",
3229 "GOMP_parallel_sections_start",
3234 if (parameters->options().shared())
3238 for (unsigned int i = 0;
3239 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3242 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3243 thread_safe = (sym != NULL
3245 && sym->in_real_elf());
3251 this->plt_thread_safe_ = thread_safe;
3256 this->stub_group_size_ = parameters->options().stub_group_size();
3257 bool no_size_errors = true;
3258 if (this->stub_group_size_ == 1)
3259 this->stub_group_size_ = 0x1c00000;
3260 else if (this->stub_group_size_ == -1)
3261 this->stub_group_size_ = -0x1e00000;
3263 no_size_errors = false;
3264 this->group_sections(layout, task, no_size_errors);
3266 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3268 this->branch_lookup_table_.clear();
3269 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3270 p != this->stub_tables_.end();
3273 (*p)->clear_stubs(true);
3275 this->stub_tables_.clear();
3276 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3277 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3278 program_name, this->stub_group_size_);
3279 this->group_sections(layout, task, true);
3282 // We need address of stub tables valid for make_stub.
3283 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3284 p != this->stub_tables_.end();
3287 const Powerpc_relobj<size, big_endian>* object
3288 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3289 Address off = object->get_output_section_offset((*p)->shndx());
3290 gold_assert(off != invalid_address);
3291 Output_section* os = (*p)->output_section();
3292 (*p)->set_address_and_size(os, off);
3297 // Clear plt call stubs, long branch stubs and branch lookup table.
3298 prev_brlt_size = this->branch_lookup_table_.size();
3299 this->branch_lookup_table_.clear();
3300 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3301 p != this->stub_tables_.end();
3304 (*p)->clear_stubs(false);
3308 // Build all the stubs.
3309 this->relax_failed_ = false;
3310 Stub_table<size, big_endian>* ifunc_stub_table
3311 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3312 Stub_table<size, big_endian>* one_stub_table
3313 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3314 for (typename Branches::const_iterator b = this->branch_info_.begin();
3315 b != this->branch_info_.end();
3318 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3319 && !this->relax_failed_)
3321 this->relax_failed_ = true;
3322 this->relax_fail_count_++;
3323 if (this->relax_fail_count_ < 3)
3328 // Did anything change size?
3329 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3330 bool again = num_huge_branches != prev_brlt_size;
3331 if (size == 64 && num_huge_branches != 0)
3332 this->make_brlt_section(layout);
3333 if (size == 64 && again)
3334 this->brlt_section_->set_current_size(num_huge_branches);
3336 typedef Unordered_set<Output_section*> Output_sections;
3337 Output_sections os_need_update;
3338 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3339 p != this->stub_tables_.end();
3342 if ((*p)->size_update())
3345 (*p)->add_eh_frame(layout);
3346 os_need_update.insert((*p)->output_section());
3350 // Set output section offsets for all input sections in an output
3351 // section that just changed size. Anything past the stubs will
3353 for (typename Output_sections::iterator p = os_need_update.begin();
3354 p != os_need_update.end();
3357 Output_section* os = *p;
3359 typedef Output_section::Input_section_list Input_section_list;
3360 for (Input_section_list::const_iterator i = os->input_sections().begin();
3361 i != os->input_sections().end();
3364 off = align_address(off, i->addralign());
3365 if (i->is_input_section() || i->is_relaxed_input_section())
3366 i->relobj()->set_section_offset(i->shndx(), off);
3367 if (i->is_relaxed_input_section())
3369 Stub_table<size, big_endian>* stub_table
3370 = static_cast<Stub_table<size, big_endian>*>(
3371 i->relaxed_input_section());
3372 Address stub_table_size = stub_table->set_address_and_size(os, off);
3373 off += stub_table_size;
3374 // After a few iterations, set current stub table size
3375 // as min size threshold, so later stub tables can only
3378 stub_table->set_min_size_threshold(stub_table_size);
3381 off += i->data_size();
3383 // If .branch_lt is part of this output section, then we have
3384 // just done the offset adjustment.
3385 os->clear_section_offsets_need_adjustment();
3390 && num_huge_branches != 0
3391 && parameters->options().output_is_position_independent())
3393 // Fill in the BRLT relocs.
3394 this->brlt_section_->reset_brlt_sizes();
3395 for (typename Branch_lookup_table::const_iterator p
3396 = this->branch_lookup_table_.begin();
3397 p != this->branch_lookup_table_.end();
3400 this->brlt_section_->add_reloc(p->first, p->second);
3402 this->brlt_section_->finalize_brlt_sizes();
3406 && (parameters->options().user_set_emit_stub_syms()
3407 ? parameters->options().emit_stub_syms()
3409 || parameters->options().output_is_position_independent()
3410 || parameters->options().emit_relocs())))
3412 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3413 p != this->stub_tables_.end();
3415 (*p)->define_stub_syms(symtab);
3417 if (this->glink_ != NULL)
3419 int stub_size = this->glink_->pltresolve_size;
3420 Address value = -stub_size;
3426 this->define_local(symtab, "__glink_PLTresolve",
3427 this->glink_, value, stub_size);
3430 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3437 template<int size, bool big_endian>
3439 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3440 unsigned char* oview,
3444 uint64_t address = plt->address();
3445 off_t len = plt->data_size();
3447 if (plt == this->glink_)
3449 // See Output_data_glink::do_write() for glink contents.
3452 gold_assert(parameters->doing_static_link());
3453 // Static linking may need stubs, to support ifunc and long
3454 // branches. We need to create an output section for
3455 // .eh_frame early in the link process, to have a place to
3456 // attach stub .eh_frame info. We also need to have
3457 // registered a CIE that matches the stub CIE. Both of
3458 // these requirements are satisfied by creating an FDE and
3459 // CIE for .glink, even though static linking will leave
3460 // .glink zero length.
3461 // ??? Hopefully generating an FDE with a zero address range
3462 // won't confuse anything that consumes .eh_frame info.
3464 else if (size == 64)
3466 // There is one word before __glink_PLTresolve
3470 else if (parameters->options().output_is_position_independent())
3472 // There are two FDEs for a position independent glink.
3473 // The first covers the branch table, the second
3474 // __glink_PLTresolve at the end of glink.
3475 off_t resolve_size = this->glink_->pltresolve_size;
3476 if (oview[9] == elfcpp::DW_CFA_nop)
3477 len -= resolve_size;
3480 address += len - resolve_size;
3487 // Must be a stub table.
3488 const Stub_table<size, big_endian>* stub_table
3489 = static_cast<const Stub_table<size, big_endian>*>(plt);
3490 uint64_t stub_address = stub_table->stub_address();
3491 len -= stub_address - address;
3492 address = stub_address;
3495 *paddress = address;
3499 // A class to handle the PLT data.
3501 template<int size, bool big_endian>
3502 class Output_data_plt_powerpc : public Output_section_data_build
3505 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3506 size, big_endian> Reloc_section;
3508 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
3509 Reloc_section* plt_rel,
3511 : Output_section_data_build(size == 32 ? 4 : 8),
3517 // Add an entry to the PLT.
3522 add_ifunc_entry(Symbol*);
3525 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3527 // Return the .rela.plt section data.
3534 // Return the number of PLT entries.
3538 if (this->current_data_size() == 0)
3540 return ((this->current_data_size() - this->first_plt_entry_offset())
3541 / this->plt_entry_size());
3546 do_adjust_output_section(Output_section* os)
3551 // Write to a map file.
3553 do_print_to_mapfile(Mapfile* mapfile) const
3554 { mapfile->print_output_data(this, this->name_); }
3557 // Return the offset of the first non-reserved PLT entry.
3559 first_plt_entry_offset() const
3561 // IPLT has no reserved entry.
3562 if (this->name_[3] == 'I')
3564 return this->targ_->first_plt_entry_offset();
3567 // Return the size of each PLT entry.
3569 plt_entry_size() const
3571 return this->targ_->plt_entry_size();
3574 // Write out the PLT data.
3576 do_write(Output_file*);
3578 // The reloc section.
3579 Reloc_section* rel_;
3580 // Allows access to .glink for do_write.
3581 Target_powerpc<size, big_endian>* targ_;
3582 // What to report in map file.
3586 // Add an entry to the PLT.
3588 template<int size, bool big_endian>
3590 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
3592 if (!gsym->has_plt_offset())
3594 section_size_type off = this->current_data_size();
3596 off += this->first_plt_entry_offset();
3597 gsym->set_plt_offset(off);
3598 gsym->set_needs_dynsym_entry();
3599 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3600 this->rel_->add_global(gsym, dynrel, this, off, 0);
3601 off += this->plt_entry_size();
3602 this->set_current_data_size(off);
3606 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3608 template<int size, bool big_endian>
3610 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
3612 if (!gsym->has_plt_offset())
3614 section_size_type off = this->current_data_size();
3615 gsym->set_plt_offset(off);
3616 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3617 if (size == 64 && this->targ_->abiversion() < 2)
3618 dynrel = elfcpp::R_PPC64_JMP_IREL;
3619 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
3620 off += this->plt_entry_size();
3621 this->set_current_data_size(off);
3625 // Add an entry for a local ifunc symbol to the IPLT.
3627 template<int size, bool big_endian>
3629 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
3630 Sized_relobj_file<size, big_endian>* relobj,
3631 unsigned int local_sym_index)
3633 if (!relobj->local_has_plt_offset(local_sym_index))
3635 section_size_type off = this->current_data_size();
3636 relobj->set_local_plt_offset(local_sym_index, off);
3637 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3638 if (size == 64 && this->targ_->abiversion() < 2)
3639 dynrel = elfcpp::R_PPC64_JMP_IREL;
3640 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
3642 off += this->plt_entry_size();
3643 this->set_current_data_size(off);
3647 static const uint32_t add_0_11_11 = 0x7c0b5a14;
3648 static const uint32_t add_2_2_11 = 0x7c425a14;
3649 static const uint32_t add_2_2_12 = 0x7c426214;
3650 static const uint32_t add_3_3_2 = 0x7c631214;
3651 static const uint32_t add_3_3_13 = 0x7c636a14;
3652 static const uint32_t add_11_0_11 = 0x7d605a14;
3653 static const uint32_t add_11_2_11 = 0x7d625a14;
3654 static const uint32_t add_11_11_2 = 0x7d6b1214;
3655 static const uint32_t addi_0_12 = 0x380c0000;
3656 static const uint32_t addi_2_2 = 0x38420000;
3657 static const uint32_t addi_3_3 = 0x38630000;
3658 static const uint32_t addi_11_11 = 0x396b0000;
3659 static const uint32_t addi_12_1 = 0x39810000;
3660 static const uint32_t addi_12_12 = 0x398c0000;
3661 static const uint32_t addis_0_2 = 0x3c020000;
3662 static const uint32_t addis_0_13 = 0x3c0d0000;
3663 static const uint32_t addis_2_12 = 0x3c4c0000;
3664 static const uint32_t addis_11_2 = 0x3d620000;
3665 static const uint32_t addis_11_11 = 0x3d6b0000;
3666 static const uint32_t addis_11_30 = 0x3d7e0000;
3667 static const uint32_t addis_12_1 = 0x3d810000;
3668 static const uint32_t addis_12_2 = 0x3d820000;
3669 static const uint32_t addis_12_12 = 0x3d8c0000;
3670 static const uint32_t b = 0x48000000;
3671 static const uint32_t bcl_20_31 = 0x429f0005;
3672 static const uint32_t bctr = 0x4e800420;
3673 static const uint32_t blr = 0x4e800020;
3674 static const uint32_t bnectr_p4 = 0x4ce20420;
3675 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
3676 static const uint32_t cmpldi_2_0 = 0x28220000;
3677 static const uint32_t cror_15_15_15 = 0x4def7b82;
3678 static const uint32_t cror_31_31_31 = 0x4ffffb82;
3679 static const uint32_t ld_0_1 = 0xe8010000;
3680 static const uint32_t ld_0_12 = 0xe80c0000;
3681 static const uint32_t ld_2_1 = 0xe8410000;
3682 static const uint32_t ld_2_2 = 0xe8420000;
3683 static const uint32_t ld_2_11 = 0xe84b0000;
3684 static const uint32_t ld_2_12 = 0xe84c0000;
3685 static const uint32_t ld_11_2 = 0xe9620000;
3686 static const uint32_t ld_11_11 = 0xe96b0000;
3687 static const uint32_t ld_12_2 = 0xe9820000;
3688 static const uint32_t ld_12_11 = 0xe98b0000;
3689 static const uint32_t ld_12_12 = 0xe98c0000;
3690 static const uint32_t lfd_0_1 = 0xc8010000;
3691 static const uint32_t li_0_0 = 0x38000000;
3692 static const uint32_t li_12_0 = 0x39800000;
3693 static const uint32_t lis_0 = 0x3c000000;
3694 static const uint32_t lis_2 = 0x3c400000;
3695 static const uint32_t lis_11 = 0x3d600000;
3696 static const uint32_t lis_12 = 0x3d800000;
3697 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
3698 static const uint32_t lwz_0_12 = 0x800c0000;
3699 static const uint32_t lwz_11_11 = 0x816b0000;
3700 static const uint32_t lwz_11_30 = 0x817e0000;
3701 static const uint32_t lwz_12_12 = 0x818c0000;
3702 static const uint32_t lwzu_0_12 = 0x840c0000;
3703 static const uint32_t mflr_0 = 0x7c0802a6;
3704 static const uint32_t mflr_11 = 0x7d6802a6;
3705 static const uint32_t mflr_12 = 0x7d8802a6;
3706 static const uint32_t mtctr_0 = 0x7c0903a6;
3707 static const uint32_t mtctr_11 = 0x7d6903a6;
3708 static const uint32_t mtctr_12 = 0x7d8903a6;
3709 static const uint32_t mtlr_0 = 0x7c0803a6;
3710 static const uint32_t mtlr_12 = 0x7d8803a6;
3711 static const uint32_t nop = 0x60000000;
3712 static const uint32_t ori_0_0_0 = 0x60000000;
3713 static const uint32_t srdi_0_0_2 = 0x7800f082;
3714 static const uint32_t std_0_1 = 0xf8010000;
3715 static const uint32_t std_0_12 = 0xf80c0000;
3716 static const uint32_t std_2_1 = 0xf8410000;
3717 static const uint32_t stfd_0_1 = 0xd8010000;
3718 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
3719 static const uint32_t sub_11_11_12 = 0x7d6c5850;
3720 static const uint32_t sub_12_12_11 = 0x7d8b6050;
3721 static const uint32_t xor_2_12_12 = 0x7d826278;
3722 static const uint32_t xor_11_12_12 = 0x7d8b6278;
3724 // Write out the PLT.
3726 template<int size, bool big_endian>
3728 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
3730 if (size == 32 && this->name_[3] != 'I')
3732 const section_size_type offset = this->offset();
3733 const section_size_type oview_size
3734 = convert_to_section_size_type(this->data_size());
3735 unsigned char* const oview = of->get_output_view(offset, oview_size);
3736 unsigned char* pov = oview;
3737 unsigned char* endpov = oview + oview_size;
3739 // The address of the .glink branch table
3740 const Output_data_glink<size, big_endian>* glink
3741 = this->targ_->glink_section();
3742 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
3744 while (pov < endpov)
3746 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
3751 of->write_output_view(offset, oview_size, oview);
3755 // Create the PLT section.
3757 template<int size, bool big_endian>
3759 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
3762 if (this->plt_ == NULL)
3764 if (this->got_ == NULL)
3765 this->got_section(symtab, layout);
3767 if (this->glink_ == NULL)
3768 make_glink_section(layout);
3770 // Ensure that .rela.dyn always appears before .rela.plt This is
3771 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3772 // needs to include .rela.plt in its range.
3773 this->rela_dyn_section(layout);
3775 Reloc_section* plt_rel = new Reloc_section(false);
3776 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3777 elfcpp::SHF_ALLOC, plt_rel,
3778 ORDER_DYNAMIC_PLT_RELOCS, false);
3780 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
3782 layout->add_output_section_data(".plt",
3784 ? elfcpp::SHT_PROGBITS
3785 : elfcpp::SHT_NOBITS),
3786 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3793 Output_section* rela_plt_os = plt_rel->output_section();
3794 rela_plt_os->set_info_section(this->plt_->output_section());
3798 // Create the IPLT section.
3800 template<int size, bool big_endian>
3802 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
3805 if (this->iplt_ == NULL)
3807 this->make_plt_section(symtab, layout);
3809 Reloc_section* iplt_rel = new Reloc_section(false);
3810 if (this->rela_dyn_->output_section())
3811 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
3813 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
3815 if (this->plt_->output_section())
3816 this->plt_->output_section()->add_output_section_data(this->iplt_);
3820 // A section for huge long branch addresses, similar to plt section.
3822 template<int size, bool big_endian>
3823 class Output_data_brlt_powerpc : public Output_section_data_build
3826 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3827 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3828 size, big_endian> Reloc_section;
3830 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
3831 Reloc_section* brlt_rel)
3832 : Output_section_data_build(size == 32 ? 4 : 8),
3840 this->reset_data_size();
3841 this->rel_->reset_data_size();
3845 finalize_brlt_sizes()
3847 this->finalize_data_size();
3848 this->rel_->finalize_data_size();
3851 // Add a reloc for an entry in the BRLT.
3853 add_reloc(Address to, unsigned int off)
3854 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
3856 // Update section and reloc section size.
3858 set_current_size(unsigned int num_branches)
3860 this->reset_address_and_file_offset();
3861 this->set_current_data_size(num_branches * 16);
3862 this->finalize_data_size();
3863 Output_section* os = this->output_section();
3864 os->set_section_offsets_need_adjustment();
3865 if (this->rel_ != NULL)
3867 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
3868 this->rel_->reset_address_and_file_offset();
3869 this->rel_->set_current_data_size(num_branches * reloc_size);
3870 this->rel_->finalize_data_size();
3871 Output_section* os = this->rel_->output_section();
3872 os->set_section_offsets_need_adjustment();
3878 do_adjust_output_section(Output_section* os)
3883 // Write to a map file.
3885 do_print_to_mapfile(Mapfile* mapfile) const
3886 { mapfile->print_output_data(this, "** BRLT"); }
3889 // Write out the BRLT data.
3891 do_write(Output_file*);
3893 // The reloc section.
3894 Reloc_section* rel_;
3895 Target_powerpc<size, big_endian>* targ_;
3898 // Make the branch lookup table section.
3900 template<int size, bool big_endian>
3902 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
3904 if (size == 64 && this->brlt_section_ == NULL)
3906 Reloc_section* brlt_rel = NULL;
3907 bool is_pic = parameters->options().output_is_position_independent();
3910 // When PIC we can't fill in .branch_lt (like .plt it can be
3911 // a bss style section) but must initialise at runtime via
3912 // dynamic relocations.
3913 this->rela_dyn_section(layout);
3914 brlt_rel = new Reloc_section(false);
3915 if (this->rela_dyn_->output_section())
3916 this->rela_dyn_->output_section()
3917 ->add_output_section_data(brlt_rel);
3920 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
3921 if (this->plt_ && is_pic && this->plt_->output_section())
3922 this->plt_->output_section()
3923 ->add_output_section_data(this->brlt_section_);
3925 layout->add_output_section_data(".branch_lt",
3926 (is_pic ? elfcpp::SHT_NOBITS
3927 : elfcpp::SHT_PROGBITS),
3928 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3929 this->brlt_section_,
3930 (is_pic ? ORDER_SMALL_BSS
3931 : ORDER_SMALL_DATA),
3936 // Write out .branch_lt when non-PIC.
3938 template<int size, bool big_endian>
3940 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
3942 if (size == 64 && !parameters->options().output_is_position_independent())
3944 const section_size_type offset = this->offset();
3945 const section_size_type oview_size
3946 = convert_to_section_size_type(this->data_size());
3947 unsigned char* const oview = of->get_output_view(offset, oview_size);
3949 this->targ_->write_branch_lookup_table(oview);
3950 of->write_output_view(offset, oview_size, oview);
3954 static inline uint32_t
3960 static inline uint32_t
3966 static inline uint32_t
3969 return hi(a + 0x8000);
3975 static const unsigned char eh_frame_cie[12];
3979 const unsigned char Eh_cie<size>::eh_frame_cie[] =
3982 'z', 'R', 0, // Augmentation string.
3983 4, // Code alignment.
3984 0x80 - size / 8 , // Data alignment.
3986 1, // Augmentation size.
3987 (elfcpp::DW_EH_PE_pcrel
3988 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
3989 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
3992 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3993 static const unsigned char glink_eh_frame_fde_64v1[] =
3995 0, 0, 0, 0, // Replaced with offset to .glink.
3996 0, 0, 0, 0, // Replaced with size of .glink.
3997 0, // Augmentation size.
3998 elfcpp::DW_CFA_advance_loc + 1,
3999 elfcpp::DW_CFA_register, 65, 12,
4000 elfcpp::DW_CFA_advance_loc + 5,
4001 elfcpp::DW_CFA_restore_extended, 65
4004 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4005 static const unsigned char glink_eh_frame_fde_64v2[] =
4007 0, 0, 0, 0, // Replaced with offset to .glink.
4008 0, 0, 0, 0, // Replaced with size of .glink.
4009 0, // Augmentation size.
4010 elfcpp::DW_CFA_advance_loc + 1,
4011 elfcpp::DW_CFA_register, 65, 0,
4012 elfcpp::DW_CFA_advance_loc + 7,
4013 elfcpp::DW_CFA_restore_extended, 65
4016 // Describe __glink_PLTresolve use of LR, 32-bit version.
4017 static const unsigned char glink_eh_frame_fde_32[] =
4019 0, 0, 0, 0, // Replaced with offset to .glink.
4020 0, 0, 0, 0, // Replaced with size of .glink.
4021 0, // Augmentation size.
4022 elfcpp::DW_CFA_advance_loc + 2,
4023 elfcpp::DW_CFA_register, 65, 0,
4024 elfcpp::DW_CFA_advance_loc + 4,
4025 elfcpp::DW_CFA_restore_extended, 65
4028 static const unsigned char default_fde[] =
4030 0, 0, 0, 0, // Replaced with offset to stubs.
4031 0, 0, 0, 0, // Replaced with size of stubs.
4032 0, // Augmentation size.
4033 elfcpp::DW_CFA_nop, // Pad.
4038 template<bool big_endian>
4040 write_insn(unsigned char* p, uint32_t v)
4042 elfcpp::Swap<32, big_endian>::writeval(p, v);
4045 // Stub_table holds information about plt and long branch stubs.
4046 // Stubs are built in an area following some input section determined
4047 // by group_sections(). This input section is converted to a relaxed
4048 // input section allowing it to be resized to accommodate the stubs
4050 template<int size, bool big_endian>
4051 class Stub_table : public Output_relaxed_input_section
4056 Plt_stub_ent(unsigned int off, unsigned int indx)
4057 : off_(off), indx_(indx), r2save_(0), localentry0_(0)
4061 unsigned int indx_ : 30;
4062 unsigned int r2save_ : 1;
4063 unsigned int localentry0_ : 1;
4065 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4066 static const Address invalid_address = static_cast<Address>(0) - 1;
4068 Stub_table(Target_powerpc<size, big_endian>* targ,
4069 Output_section* output_section,
4070 const Output_section::Input_section* owner,
4072 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4074 ->section_addralign(owner->shndx())),
4075 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4076 orig_data_size_(owner->current_data_size()),
4077 plt_size_(0), last_plt_size_(0),
4078 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4079 eh_frame_added_(false), need_save_res_(false), uniq_(id)
4081 this->set_output_section(output_section);
4083 std::vector<Output_relaxed_input_section*> new_relaxed;
4084 new_relaxed.push_back(this);
4085 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4088 // Add a plt call stub.
4090 add_plt_call_entry(Address,
4091 const Sized_relobj_file<size, big_endian>*,
4098 add_plt_call_entry(Address,
4099 const Sized_relobj_file<size, big_endian>*,
4105 // Find a given plt call stub.
4107 find_plt_call_entry(const Symbol*) const;
4110 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4111 unsigned int) const;
4114 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4120 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4125 // Add a long branch stub.
4127 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4128 unsigned int, Address, Address, bool);
4131 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4135 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4137 Address max_branch_offset = max_branch_delta(r_type);
4138 if (max_branch_offset == 0)
4140 gold_assert(from != invalid_address);
4141 Address loc = off + this->stub_address();
4142 return loc - from + max_branch_offset < 2 * max_branch_offset;
4146 clear_stubs(bool all)
4148 this->plt_call_stubs_.clear();
4149 this->plt_size_ = 0;
4150 this->long_branch_stubs_.clear();
4151 this->branch_size_ = 0;
4152 this->need_save_res_ = false;
4155 this->last_plt_size_ = 0;
4156 this->last_branch_size_ = 0;
4161 set_address_and_size(const Output_section* os, Address off)
4163 Address start_off = off;
4164 off += this->orig_data_size_;
4165 Address my_size = this->plt_size_ + this->branch_size_;
4166 if (this->need_save_res_)
4167 my_size += this->targ_->savres_section()->data_size();
4169 off = align_address(off, this->stub_align());
4170 // Include original section size and alignment padding in size
4171 my_size += off - start_off;
4172 // Ensure new size is always larger than min size
4173 // threshold. Alignment requirement is included in "my_size", so
4174 // increase "my_size" does not invalidate alignment.
4175 if (my_size < this->min_size_threshold_)
4176 my_size = this->min_size_threshold_;
4177 this->reset_address_and_file_offset();
4178 this->set_current_data_size(my_size);
4179 this->set_address_and_file_offset(os->address() + start_off,
4180 os->offset() + start_off);
4185 stub_address() const
4187 return align_address(this->address() + this->orig_data_size_,
4188 this->stub_align());
4194 return align_address(this->offset() + this->orig_data_size_,
4195 this->stub_align());
4200 { return this->plt_size_; }
4203 set_min_size_threshold(Address min_size)
4204 { this->min_size_threshold_ = min_size; }
4207 define_stub_syms(Symbol_table*);
4212 Output_section* os = this->output_section();
4213 if (os->addralign() < this->stub_align())
4215 os->set_addralign(this->stub_align());
4216 // FIXME: get rid of the insane checkpointing.
4217 // We can't increase alignment of the input section to which
4218 // stubs are attached; The input section may be .init which
4219 // is pasted together with other .init sections to form a
4220 // function. Aligning might insert zero padding resulting in
4221 // sigill. However we do need to increase alignment of the
4222 // output section so that the align_address() on offset in
4223 // set_address_and_size() adds the same padding as the
4224 // align_address() on address in stub_address().
4225 // What's more, we need this alignment for the layout done in
4226 // relaxation_loop_body() so that the output section starts at
4227 // a suitably aligned address.
4228 os->checkpoint_set_addralign(this->stub_align());
4230 if (this->last_plt_size_ != this->plt_size_
4231 || this->last_branch_size_ != this->branch_size_)
4233 this->last_plt_size_ = this->plt_size_;
4234 this->last_branch_size_ = this->branch_size_;
4240 // Add .eh_frame info for this stub section. Unlike other linker
4241 // generated .eh_frame this is added late in the link, because we
4242 // only want the .eh_frame info if this particular stub section is
4245 add_eh_frame(Layout* layout)
4247 if (!this->eh_frame_added_)
4249 if (!parameters->options().ld_generated_unwind_info())
4252 // Since we add stub .eh_frame info late, it must be placed
4253 // after all other linker generated .eh_frame info so that
4254 // merge mapping need not be updated for input sections.
4255 // There is no provision to use a different CIE to that used
4257 if (!this->targ_->has_glink())
4260 layout->add_eh_frame_for_plt(this,
4261 Eh_cie<size>::eh_frame_cie,
4262 sizeof (Eh_cie<size>::eh_frame_cie),
4264 sizeof (default_fde));
4265 this->eh_frame_added_ = true;
4269 Target_powerpc<size, big_endian>*
4275 class Plt_stub_key_hash;
4276 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4277 Plt_stub_key_hash> Plt_stub_entries;
4278 class Branch_stub_ent;
4279 class Branch_stub_ent_hash;
4280 typedef Unordered_map<Branch_stub_ent, unsigned int,
4281 Branch_stub_ent_hash> Branch_stub_entries;
4283 // Alignment of stub section.
4289 unsigned int min_align = 32;
4290 unsigned int user_align = 1 << parameters->options().plt_align();
4291 return std::max(user_align, min_align);
4294 // Return the plt offset for the given call stub.
4296 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
4298 const Symbol* gsym = p->first.sym_;
4301 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
4302 && gsym->can_use_relative_reloc(false));
4303 return gsym->plt_offset();
4308 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4309 unsigned int local_sym_index = p->first.locsym_;
4310 return relobj->local_plt_offset(local_sym_index);
4314 // Size of a given plt call stub.
4316 plt_call_size(typename Plt_stub_entries::const_iterator p) const
4322 Address plt_addr = this->plt_off(p, &is_iplt);
4324 plt_addr += this->targ_->iplt_section()->address();
4326 plt_addr += this->targ_->plt_section()->address();
4327 Address got_addr = this->targ_->got_section()->output_section()->address();
4328 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4329 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
4330 got_addr += ppcobj->toc_base_offset();
4331 Address off = plt_addr - got_addr;
4332 unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
4333 if (this->targ_->abiversion() < 2)
4335 bool static_chain = parameters->options().plt_static_chain();
4336 bool thread_safe = this->targ_->plt_thread_safe();
4340 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
4342 unsigned int align = 1 << parameters->options().plt_align();
4344 bytes = (bytes + align - 1) & -align;
4348 // Return long branch stub size.
4350 branch_stub_size(typename Branch_stub_entries::const_iterator p)
4352 Address loc = this->stub_address() + this->last_plt_size_ + p->second;
4353 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
4355 if (size == 64 || !parameters->options().output_is_position_independent())
4362 do_write(Output_file*);
4364 // Plt call stub keys.
4368 Plt_stub_key(const Symbol* sym)
4369 : sym_(sym), object_(0), addend_(0), locsym_(0)
4372 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4373 unsigned int locsym_index)
4374 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4377 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4379 unsigned int r_type,
4381 : sym_(sym), object_(0), addend_(0), locsym_(0)
4384 this->addend_ = addend;
4385 else if (parameters->options().output_is_position_independent()
4386 && r_type == elfcpp::R_PPC_PLTREL24)
4388 this->addend_ = addend;
4389 if (this->addend_ >= 32768)
4390 this->object_ = object;
4394 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4395 unsigned int locsym_index,
4396 unsigned int r_type,
4398 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4401 this->addend_ = addend;
4402 else if (parameters->options().output_is_position_independent()
4403 && r_type == elfcpp::R_PPC_PLTREL24)
4404 this->addend_ = addend;
4407 bool operator==(const Plt_stub_key& that) const
4409 return (this->sym_ == that.sym_
4410 && this->object_ == that.object_
4411 && this->addend_ == that.addend_
4412 && this->locsym_ == that.locsym_);
4416 const Sized_relobj_file<size, big_endian>* object_;
4417 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4418 unsigned int locsym_;
4421 class Plt_stub_key_hash
4424 size_t operator()(const Plt_stub_key& ent) const
4426 return (reinterpret_cast<uintptr_t>(ent.sym_)
4427 ^ reinterpret_cast<uintptr_t>(ent.object_)
4433 // Long branch stub keys.
4434 class Branch_stub_ent
4437 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj,
4438 Address to, bool save_res)
4439 : dest_(to), toc_base_off_(0), save_res_(save_res)
4442 toc_base_off_ = obj->toc_base_offset();
4445 bool operator==(const Branch_stub_ent& that) const
4447 return (this->dest_ == that.dest_
4449 || this->toc_base_off_ == that.toc_base_off_));
4453 unsigned int toc_base_off_;
4457 class Branch_stub_ent_hash
4460 size_t operator()(const Branch_stub_ent& ent) const
4461 { return ent.dest_ ^ ent.toc_base_off_; }
4464 // In a sane world this would be a global.
4465 Target_powerpc<size, big_endian>* targ_;
4466 // Map sym/object/addend to stub offset.
4467 Plt_stub_entries plt_call_stubs_;
4468 // Map destination address to stub offset.
4469 Branch_stub_entries long_branch_stubs_;
4470 // size of input section
4471 section_size_type orig_data_size_;
4473 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
4474 // Some rare cases cause (PR/20529) fluctuation in stub table
4475 // size, which leads to an endless relax loop. This is to be fixed
4476 // by, after the first few iterations, allowing only increase of
4477 // stub table size. This variable sets the minimal possible size of
4478 // a stub table, it is zero for the first few iterations, then
4479 // increases monotonically.
4480 Address min_size_threshold_;
4481 // Whether .eh_frame info has been created for this stub section.
4482 bool eh_frame_added_;
4483 // Set if this stub group needs a copy of out-of-line register
4484 // save/restore functions.
4485 bool need_save_res_;
4486 // Per stub table unique identifier.
4490 // Add a plt call stub, if we do not already have one for this
4491 // sym/object/addend combo.
4493 template<int size, bool big_endian>
4495 Stub_table<size, big_endian>::add_plt_call_entry(
4497 const Sized_relobj_file<size, big_endian>* object,
4499 unsigned int r_type,
4503 Plt_stub_key key(object, gsym, r_type, addend);
4504 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
4505 std::pair<typename Plt_stub_entries::iterator, bool> p
4506 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
4509 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
4511 && this->targ_->is_elfv2_localentry0(gsym))
4513 p.first->second.localentry0_ = 1;
4514 this->targ_->set_has_localentry0();
4519 && !p.first->second.localentry0_)
4520 p.first->second.r2save_ = 1;
4521 return this->can_reach_stub(from, ent.off_, r_type);
4524 template<int size, bool big_endian>
4526 Stub_table<size, big_endian>::add_plt_call_entry(
4528 const Sized_relobj_file<size, big_endian>* object,
4529 unsigned int locsym_index,
4530 unsigned int r_type,
4534 Plt_stub_key key(object, locsym_index, r_type, addend);
4535 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
4536 std::pair<typename Plt_stub_entries::iterator, bool> p
4537 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
4540 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
4542 && this->targ_->is_elfv2_localentry0(object, locsym_index))
4544 p.first->second.localentry0_ = 1;
4545 this->targ_->set_has_localentry0();
4550 && !p.first->second.localentry0_)
4551 p.first->second.r2save_ = 1;
4552 return this->can_reach_stub(from, ent.off_, r_type);
4555 // Find a plt call stub.
4557 template<int size, bool big_endian>
4558 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4559 Stub_table<size, big_endian>::find_plt_call_entry(
4560 const Sized_relobj_file<size, big_endian>* object,
4562 unsigned int r_type,
4563 Address addend) const
4565 Plt_stub_key key(object, gsym, r_type, addend);
4566 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4567 if (p == this->plt_call_stubs_.end())
4572 template<int size, bool big_endian>
4573 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4574 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
4576 Plt_stub_key key(gsym);
4577 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4578 if (p == this->plt_call_stubs_.end())
4583 template<int size, bool big_endian>
4584 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4585 Stub_table<size, big_endian>::find_plt_call_entry(
4586 const Sized_relobj_file<size, big_endian>* object,
4587 unsigned int locsym_index,
4588 unsigned int r_type,
4589 Address addend) const
4591 Plt_stub_key key(object, locsym_index, r_type, addend);
4592 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4593 if (p == this->plt_call_stubs_.end())
4598 template<int size, bool big_endian>
4599 const typename Stub_table<size, big_endian>::Plt_stub_ent*
4600 Stub_table<size, big_endian>::find_plt_call_entry(
4601 const Sized_relobj_file<size, big_endian>* object,
4602 unsigned int locsym_index) const
4604 Plt_stub_key key(object, locsym_index);
4605 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4606 if (p == this->plt_call_stubs_.end())
4611 // Add a long branch stub if we don't already have one to given
4614 template<int size, bool big_endian>
4616 Stub_table<size, big_endian>::add_long_branch_entry(
4617 const Powerpc_relobj<size, big_endian>* object,
4618 unsigned int r_type,
4623 Branch_stub_ent ent(object, to, save_res);
4624 Address off = this->branch_size_;
4625 std::pair<typename Branch_stub_entries::iterator, bool> p
4626 = this->long_branch_stubs_.insert(std::make_pair(ent, off));
4630 this->need_save_res_ = true;
4633 unsigned int stub_size = this->branch_stub_size(p.first);
4634 this->branch_size_ = off + stub_size;
4635 if (size == 64 && stub_size != 4)
4636 this->targ_->add_branch_lookup_table(to);
4639 return this->can_reach_stub(from, off, r_type);
4642 // Find long branch stub offset.
4644 template<int size, bool big_endian>
4645 typename Stub_table<size, big_endian>::Address
4646 Stub_table<size, big_endian>::find_long_branch_entry(
4647 const Powerpc_relobj<size, big_endian>* object,
4650 Branch_stub_ent ent(object, to, false);
4651 typename Branch_stub_entries::const_iterator p
4652 = this->long_branch_stubs_.find(ent);
4653 if (p == this->long_branch_stubs_.end())
4654 return invalid_address;
4655 if (p->first.save_res_)
4656 return to - this->targ_->savres_section()->address() + this->branch_size_;
4660 // A class to handle .glink.
4662 template<int size, bool big_endian>
4663 class Output_data_glink : public Output_section_data
4666 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4667 static const Address invalid_address = static_cast<Address>(0) - 1;
4668 static const int pltresolve_size = 16*4;
4670 Output_data_glink(Target_powerpc<size, big_endian>* targ)
4671 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
4672 end_branch_table_(), ge_size_(0)
4676 add_eh_frame(Layout* layout);
4679 add_global_entry(const Symbol*);
4682 find_global_entry(const Symbol*) const;
4685 global_entry_address() const
4687 gold_assert(this->is_data_size_valid());
4688 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4689 return this->address() + global_entry_off;
4693 // Write to a map file.
4695 do_print_to_mapfile(Mapfile* mapfile) const
4696 { mapfile->print_output_data(this, _("** glink")); }
4700 set_final_data_size();
4704 do_write(Output_file*);
4706 // Allows access to .got and .plt for do_write.
4707 Target_powerpc<size, big_endian>* targ_;
4709 // Map sym to stub offset.
4710 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
4711 Global_entry_stub_entries global_entry_stubs_;
4713 unsigned int end_branch_table_, ge_size_;
4716 template<int size, bool big_endian>
4718 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
4720 if (!parameters->options().ld_generated_unwind_info())
4725 if (this->targ_->abiversion() < 2)
4726 layout->add_eh_frame_for_plt(this,
4727 Eh_cie<64>::eh_frame_cie,
4728 sizeof (Eh_cie<64>::eh_frame_cie),
4729 glink_eh_frame_fde_64v1,
4730 sizeof (glink_eh_frame_fde_64v1));
4732 layout->add_eh_frame_for_plt(this,
4733 Eh_cie<64>::eh_frame_cie,
4734 sizeof (Eh_cie<64>::eh_frame_cie),
4735 glink_eh_frame_fde_64v2,
4736 sizeof (glink_eh_frame_fde_64v2));
4740 // 32-bit .glink can use the default since the CIE return
4741 // address reg, LR, is valid.
4742 layout->add_eh_frame_for_plt(this,
4743 Eh_cie<32>::eh_frame_cie,
4744 sizeof (Eh_cie<32>::eh_frame_cie),
4746 sizeof (default_fde));
4747 // Except where LR is used in a PIC __glink_PLTresolve.
4748 if (parameters->options().output_is_position_independent())
4749 layout->add_eh_frame_for_plt(this,
4750 Eh_cie<32>::eh_frame_cie,
4751 sizeof (Eh_cie<32>::eh_frame_cie),
4752 glink_eh_frame_fde_32,
4753 sizeof (glink_eh_frame_fde_32));
4757 template<int size, bool big_endian>
4759 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
4761 std::pair<typename Global_entry_stub_entries::iterator, bool> p
4762 = this->global_entry_stubs_.insert(std::make_pair(gsym, this->ge_size_));
4764 this->ge_size_ += 16;
4767 template<int size, bool big_endian>
4768 typename Output_data_glink<size, big_endian>::Address
4769 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
4771 typename Global_entry_stub_entries::const_iterator p
4772 = this->global_entry_stubs_.find(gsym);
4773 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
4776 template<int size, bool big_endian>
4778 Output_data_glink<size, big_endian>::set_final_data_size()
4780 unsigned int count = this->targ_->plt_entry_count();
4781 section_size_type total = 0;
4787 // space for branch table
4788 total += 4 * (count - 1);
4790 total += -total & 15;
4791 total += this->pltresolve_size;
4795 total += this->pltresolve_size;
4797 // space for branch table
4799 if (this->targ_->abiversion() < 2)
4803 total += 4 * (count - 0x8000);
4807 this->end_branch_table_ = total;
4808 total = (total + 15) & -16;
4809 total += this->ge_size_;
4811 this->set_data_size(total);
4814 // Define symbols on stubs, identifying the stub.
4816 template<int size, bool big_endian>
4818 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
4820 if (!this->plt_call_stubs_.empty())
4822 // The key for the plt call stub hash table includes addresses,
4823 // therefore traversal order depends on those addresses, which
4824 // can change between runs if gold is a PIE. Unfortunately the
4825 // output .symtab ordering depends on the order in which symbols
4826 // are added to the linker symtab. We want reproducible output
4827 // so must sort the call stub symbols.
4828 typedef typename Plt_stub_entries::const_iterator plt_iter;
4829 std::vector<plt_iter> sorted;
4830 sorted.resize(this->plt_call_stubs_.size());
4832 for (plt_iter cs = this->plt_call_stubs_.begin();
4833 cs != this->plt_call_stubs_.end();
4835 sorted[cs->second.indx_] = cs;
4837 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
4839 plt_iter cs = sorted[i];
4842 if (cs->first.addend_ != 0)
4843 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
4846 if (cs->first.object_)
4848 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4849 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
4850 sprintf(obj, "%x:", ppcobj->uniq());
4853 const char *symname;
4854 if (cs->first.sym_ == NULL)
4856 sprintf(localname, "%x", cs->first.locsym_);
4857 symname = localname;
4860 symname = cs->first.sym_->name();
4861 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
4862 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
4864 = this->stub_address() - this->address() + cs->second.off_;
4865 unsigned int stub_size = this->plt_call_size(cs);
4866 this->targ_->define_local(symtab, name, this, value, stub_size);
4870 typedef typename Branch_stub_entries::const_iterator branch_iter;
4871 for (branch_iter bs = this->long_branch_stubs_.begin();
4872 bs != this->long_branch_stubs_.end();
4875 if (bs->first.save_res_)
4878 char* name = new char[8 + 13 + 16 + 1];
4879 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
4880 static_cast<unsigned long long>(bs->first.dest_));
4881 Address value = (this->stub_address() - this->address()
4882 + this->plt_size_ + bs->second);
4883 unsigned int stub_size = this->branch_stub_size(bs);
4884 this->targ_->define_local(symtab, name, this, value, stub_size);
4888 // Write out plt and long branch stub code.
4890 template<int size, bool big_endian>
4892 Stub_table<size, big_endian>::do_write(Output_file* of)
4894 if (this->plt_call_stubs_.empty()
4895 && this->long_branch_stubs_.empty())
4898 const section_size_type start_off = this->offset();
4899 const section_size_type off = this->stub_offset();
4900 const section_size_type oview_size =
4901 convert_to_section_size_type(this->data_size() - (off - start_off));
4902 unsigned char* const oview = of->get_output_view(off, oview_size);
4907 const Output_data_got_powerpc<size, big_endian>* got
4908 = this->targ_->got_section();
4909 Address got_os_addr = got->output_section()->address();
4911 if (!this->plt_call_stubs_.empty())
4913 // The base address of the .plt section.
4914 Address plt_base = this->targ_->plt_section()->address();
4915 Address iplt_base = invalid_address;
4917 // Write out plt call stubs.
4918 typename Plt_stub_entries::const_iterator cs;
4919 for (cs = this->plt_call_stubs_.begin();
4920 cs != this->plt_call_stubs_.end();
4924 Address pltoff = this->plt_off(cs, &is_iplt);
4925 Address plt_addr = pltoff;
4928 if (iplt_base == invalid_address)
4929 iplt_base = this->targ_->iplt_section()->address();
4930 plt_addr += iplt_base;
4933 plt_addr += plt_base;
4934 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4935 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
4936 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
4937 Address off = plt_addr - got_addr;
4939 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
4940 gold_error(_("%s: linkage table error against `%s'"),
4941 cs->first.object_->name().c_str(),
4942 cs->first.sym_->demangled_name().c_str());
4944 bool plt_load_toc = this->targ_->abiversion() < 2;
4946 = plt_load_toc && parameters->options().plt_static_chain();
4948 = plt_load_toc && this->targ_->plt_thread_safe();
4949 bool use_fake_dep = false;
4950 Address cmp_branch_off = 0;
4953 unsigned int pltindex
4954 = ((pltoff - this->targ_->first_plt_entry_offset())
4955 / this->targ_->plt_entry_size());
4957 = (this->targ_->glink_section()->pltresolve_size
4959 if (pltindex > 32768)
4960 glinkoff += (pltindex - 32768) * 4;
4962 = this->targ_->glink_section()->address() + glinkoff;
4964 = (this->stub_address() + cs->second.off_ + 20
4965 + 4 * cs->second.r2save_
4966 + 4 * (ha(off) != 0)
4967 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
4968 + 4 * static_chain);
4969 cmp_branch_off = to - from;
4970 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
4973 p = oview + cs->second.off_;
4976 if (cs->second.r2save_)
4978 write_insn<big_endian>(p,
4979 std_2_1 + this->targ_->stk_toc());
4984 write_insn<big_endian>(p, addis_11_2 + ha(off));
4986 write_insn<big_endian>(p, ld_12_11 + l(off));
4991 write_insn<big_endian>(p, addis_12_2 + ha(off));
4993 write_insn<big_endian>(p, ld_12_12 + l(off));
4997 && ha(off + 8 + 8 * static_chain) != ha(off))
4999 write_insn<big_endian>(p, addi_11_11 + l(off));
5003 write_insn<big_endian>(p, mtctr_12);
5009 write_insn<big_endian>(p, xor_2_12_12);
5011 write_insn<big_endian>(p, add_11_11_2);
5014 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
5018 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
5025 if (cs->second.r2save_)
5027 write_insn<big_endian>(p,
5028 std_2_1 + this->targ_->stk_toc());
5031 write_insn<big_endian>(p, ld_12_2 + l(off));
5034 && ha(off + 8 + 8 * static_chain) != ha(off))
5036 write_insn<big_endian>(p, addi_2_2 + l(off));
5040 write_insn<big_endian>(p, mtctr_12);
5046 write_insn<big_endian>(p, xor_11_12_12);
5048 write_insn<big_endian>(p, add_2_2_11);
5053 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
5056 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
5060 if (thread_safe && !use_fake_dep)
5062 write_insn<big_endian>(p, cmpldi_2_0);
5064 write_insn<big_endian>(p, bnectr_p4);
5066 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
5069 write_insn<big_endian>(p, bctr);
5073 // Write out long branch stubs.
5074 typename Branch_stub_entries::const_iterator bs;
5075 for (bs = this->long_branch_stubs_.begin();
5076 bs != this->long_branch_stubs_.end();
5079 if (bs->first.save_res_)
5081 p = oview + this->plt_size_ + bs->second;
5082 Address loc = this->stub_address() + this->plt_size_ + bs->second;
5083 Address delta = bs->first.dest_ - loc;
5084 if (delta + (1 << 25) < 2 << 25)
5085 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
5089 = this->targ_->find_branch_lookup_table(bs->first.dest_);
5090 gold_assert(brlt_addr != invalid_address);
5091 brlt_addr += this->targ_->brlt_section()->address();
5092 Address got_addr = got_os_addr + bs->first.toc_base_off_;
5093 Address brltoff = brlt_addr - got_addr;
5094 if (ha(brltoff) == 0)
5096 write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4;
5100 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
5101 write_insn<big_endian>(p, ld_12_12 + l(brltoff)), p += 4;
5103 write_insn<big_endian>(p, mtctr_12), p += 4;
5104 write_insn<big_endian>(p, bctr);
5110 if (!this->plt_call_stubs_.empty())
5112 // The base address of the .plt section.
5113 Address plt_base = this->targ_->plt_section()->address();
5114 Address iplt_base = invalid_address;
5115 // The address of _GLOBAL_OFFSET_TABLE_.
5116 Address g_o_t = invalid_address;
5118 // Write out plt call stubs.
5119 typename Plt_stub_entries::const_iterator cs;
5120 for (cs = this->plt_call_stubs_.begin();
5121 cs != this->plt_call_stubs_.end();
5125 Address plt_addr = this->plt_off(cs, &is_iplt);
5128 if (iplt_base == invalid_address)
5129 iplt_base = this->targ_->iplt_section()->address();
5130 plt_addr += iplt_base;
5133 plt_addr += plt_base;
5135 p = oview + cs->second.off_;
5136 if (parameters->options().output_is_position_independent())
5139 const Powerpc_relobj<size, big_endian>* ppcobj
5140 = (static_cast<const Powerpc_relobj<size, big_endian>*>
5141 (cs->first.object_));
5142 if (ppcobj != NULL && cs->first.addend_ >= 32768)
5144 unsigned int got2 = ppcobj->got2_shndx();
5145 got_addr = ppcobj->get_output_section_offset(got2);
5146 gold_assert(got_addr != invalid_address);
5147 got_addr += (ppcobj->output_section(got2)->address()
5148 + cs->first.addend_);
5152 if (g_o_t == invalid_address)
5154 const Output_data_got_powerpc<size, big_endian>* got
5155 = this->targ_->got_section();
5156 g_o_t = got->address() + got->g_o_t();
5161 Address off = plt_addr - got_addr;
5164 write_insn<big_endian>(p + 0, lwz_11_30 + l(off));
5165 write_insn<big_endian>(p + 4, mtctr_11);
5166 write_insn<big_endian>(p + 8, bctr);
5170 write_insn<big_endian>(p + 0, addis_11_30 + ha(off));
5171 write_insn<big_endian>(p + 4, lwz_11_11 + l(off));
5172 write_insn<big_endian>(p + 8, mtctr_11);
5173 write_insn<big_endian>(p + 12, bctr);
5178 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
5179 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
5180 write_insn<big_endian>(p + 8, mtctr_11);
5181 write_insn<big_endian>(p + 12, bctr);
5186 // Write out long branch stubs.
5187 typename Branch_stub_entries::const_iterator bs;
5188 for (bs = this->long_branch_stubs_.begin();
5189 bs != this->long_branch_stubs_.end();
5192 if (bs->first.save_res_)
5194 p = oview + this->plt_size_ + bs->second;
5195 Address loc = this->stub_address() + this->plt_size_ + bs->second;
5196 Address delta = bs->first.dest_ - loc;
5197 if (delta + (1 << 25) < 2 << 25)
5198 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
5199 else if (!parameters->options().output_is_position_independent())
5201 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
5202 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
5203 write_insn<big_endian>(p + 8, mtctr_12);
5204 write_insn<big_endian>(p + 12, bctr);
5209 write_insn<big_endian>(p + 0, mflr_0);
5210 write_insn<big_endian>(p + 4, bcl_20_31);
5211 write_insn<big_endian>(p + 8, mflr_12);
5212 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
5213 write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
5214 write_insn<big_endian>(p + 20, mtlr_0);
5215 write_insn<big_endian>(p + 24, mtctr_12);
5216 write_insn<big_endian>(p + 28, bctr);
5220 if (this->need_save_res_)
5222 p = oview + this->plt_size_ + this->branch_size_;
5223 memcpy (p, this->targ_->savres_section()->contents(),
5224 this->targ_->savres_section()->data_size());
5228 // Write out .glink.
5230 template<int size, bool big_endian>
5232 Output_data_glink<size, big_endian>::do_write(Output_file* of)
5234 const section_size_type off = this->offset();
5235 const section_size_type oview_size =
5236 convert_to_section_size_type(this->data_size());
5237 unsigned char* const oview = of->get_output_view(off, oview_size);
5240 // The base address of the .plt section.
5241 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5242 Address plt_base = this->targ_->plt_section()->address();
5246 if (this->end_branch_table_ != 0)
5248 // Write pltresolve stub.
5250 Address after_bcl = this->address() + 16;
5251 Address pltoff = plt_base - after_bcl;
5253 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
5255 if (this->targ_->abiversion() < 2)
5257 write_insn<big_endian>(p, mflr_12), p += 4;
5258 write_insn<big_endian>(p, bcl_20_31), p += 4;
5259 write_insn<big_endian>(p, mflr_11), p += 4;
5260 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5261 write_insn<big_endian>(p, mtlr_12), p += 4;
5262 write_insn<big_endian>(p, add_11_2_11), p += 4;
5263 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5264 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
5265 write_insn<big_endian>(p, mtctr_12), p += 4;
5266 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
5270 write_insn<big_endian>(p, mflr_0), p += 4;
5271 write_insn<big_endian>(p, bcl_20_31), p += 4;
5272 write_insn<big_endian>(p, mflr_11), p += 4;
5273 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
5274 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5275 write_insn<big_endian>(p, mtlr_0), p += 4;
5276 write_insn<big_endian>(p, sub_12_12_11), p += 4;
5277 write_insn<big_endian>(p, add_11_2_11), p += 4;
5278 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
5279 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5280 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
5281 write_insn<big_endian>(p, mtctr_12), p += 4;
5282 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
5284 write_insn<big_endian>(p, bctr), p += 4;
5285 while (p < oview + this->pltresolve_size)
5286 write_insn<big_endian>(p, nop), p += 4;
5288 // Write lazy link call stubs.
5290 while (p < oview + this->end_branch_table_)
5292 if (this->targ_->abiversion() < 2)
5296 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
5300 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
5301 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
5304 uint32_t branch_off = 8 - (p - oview);
5305 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
5310 Address plt_base = this->targ_->plt_section()->address();
5311 Address iplt_base = invalid_address;
5312 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
5313 Address global_entry_base = this->address() + global_entry_off;
5314 typename Global_entry_stub_entries::const_iterator ge;
5315 for (ge = this->global_entry_stubs_.begin();
5316 ge != this->global_entry_stubs_.end();
5319 p = oview + global_entry_off + ge->second;
5320 Address plt_addr = ge->first->plt_offset();
5321 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
5322 && ge->first->can_use_relative_reloc(false))
5324 if (iplt_base == invalid_address)
5325 iplt_base = this->targ_->iplt_section()->address();
5326 plt_addr += iplt_base;
5329 plt_addr += plt_base;
5330 Address my_addr = global_entry_base + ge->second;
5331 Address off = plt_addr - my_addr;
5333 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
5334 gold_error(_("%s: linkage table error against `%s'"),
5335 ge->first->object()->name().c_str(),
5336 ge->first->demangled_name().c_str());
5338 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
5339 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
5340 write_insn<big_endian>(p, mtctr_12), p += 4;
5341 write_insn<big_endian>(p, bctr);
5346 const Output_data_got_powerpc<size, big_endian>* got
5347 = this->targ_->got_section();
5348 // The address of _GLOBAL_OFFSET_TABLE_.
5349 Address g_o_t = got->address() + got->g_o_t();
5351 // Write out pltresolve branch table.
5353 unsigned int the_end = oview_size - this->pltresolve_size;
5354 unsigned char* end_p = oview + the_end;
5355 while (p < end_p - 8 * 4)
5356 write_insn<big_endian>(p, b + end_p - p), p += 4;
5358 write_insn<big_endian>(p, nop), p += 4;
5360 // Write out pltresolve call stub.
5361 if (parameters->options().output_is_position_independent())
5363 Address res0_off = 0;
5364 Address after_bcl_off = the_end + 12;
5365 Address bcl_res0 = after_bcl_off - res0_off;
5367 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
5368 write_insn<big_endian>(p + 4, mflr_0);
5369 write_insn<big_endian>(p + 8, bcl_20_31);
5370 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
5371 write_insn<big_endian>(p + 16, mflr_12);
5372 write_insn<big_endian>(p + 20, mtlr_0);
5373 write_insn<big_endian>(p + 24, sub_11_11_12);
5375 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
5377 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
5378 if (ha(got_bcl) == ha(got_bcl + 4))
5380 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
5381 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
5385 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
5386 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
5388 write_insn<big_endian>(p + 40, mtctr_0);
5389 write_insn<big_endian>(p + 44, add_0_11_11);
5390 write_insn<big_endian>(p + 48, add_11_0_11);
5391 write_insn<big_endian>(p + 52, bctr);
5392 write_insn<big_endian>(p + 56, nop);
5393 write_insn<big_endian>(p + 60, nop);
5397 Address res0 = this->address();
5399 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
5400 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
5401 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5402 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
5404 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
5405 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
5406 write_insn<big_endian>(p + 16, mtctr_0);
5407 write_insn<big_endian>(p + 20, add_0_11_11);
5408 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5409 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
5411 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
5412 write_insn<big_endian>(p + 28, add_11_0_11);
5413 write_insn<big_endian>(p + 32, bctr);
5414 write_insn<big_endian>(p + 36, nop);
5415 write_insn<big_endian>(p + 40, nop);
5416 write_insn<big_endian>(p + 44, nop);
5417 write_insn<big_endian>(p + 48, nop);
5418 write_insn<big_endian>(p + 52, nop);
5419 write_insn<big_endian>(p + 56, nop);
5420 write_insn<big_endian>(p + 60, nop);
5425 of->write_output_view(off, oview_size, oview);
5429 // A class to handle linker generated save/restore functions.
5431 template<int size, bool big_endian>
5432 class Output_data_save_res : public Output_section_data_build
5435 Output_data_save_res(Symbol_table* symtab);
5437 const unsigned char*
5444 // Write to a map file.
5446 do_print_to_mapfile(Mapfile* mapfile) const
5447 { mapfile->print_output_data(this, _("** save/restore")); }
5450 do_write(Output_file*);
5453 // The maximum size of save/restore contents.
5454 static const unsigned int savres_max = 218*4;
5457 savres_define(Symbol_table* symtab,
5459 unsigned int lo, unsigned int hi,
5460 unsigned char* write_ent(unsigned char*, int),
5461 unsigned char* write_tail(unsigned char*, int));
5463 unsigned char *contents_;
5466 template<bool big_endian>
5467 static unsigned char*
5468 savegpr0(unsigned char* p, int r)
5470 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5471 write_insn<big_endian>(p, insn);
5475 template<bool big_endian>
5476 static unsigned char*
5477 savegpr0_tail(unsigned char* p, int r)
5479 p = savegpr0<big_endian>(p, r);
5480 uint32_t insn = std_0_1 + 16;
5481 write_insn<big_endian>(p, insn);
5483 write_insn<big_endian>(p, blr);
5487 template<bool big_endian>
5488 static unsigned char*
5489 restgpr0(unsigned char* p, int r)
5491 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5492 write_insn<big_endian>(p, insn);
5496 template<bool big_endian>
5497 static unsigned char*
5498 restgpr0_tail(unsigned char* p, int r)
5500 uint32_t insn = ld_0_1 + 16;
5501 write_insn<big_endian>(p, insn);
5503 p = restgpr0<big_endian>(p, r);
5504 write_insn<big_endian>(p, mtlr_0);
5508 p = restgpr0<big_endian>(p, 30);
5509 p = restgpr0<big_endian>(p, 31);
5511 write_insn<big_endian>(p, blr);
5515 template<bool big_endian>
5516 static unsigned char*
5517 savegpr1(unsigned char* p, int r)
5519 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5520 write_insn<big_endian>(p, insn);
5524 template<bool big_endian>
5525 static unsigned char*
5526 savegpr1_tail(unsigned char* p, int r)
5528 p = savegpr1<big_endian>(p, r);
5529 write_insn<big_endian>(p, blr);
5533 template<bool big_endian>
5534 static unsigned char*
5535 restgpr1(unsigned char* p, int r)
5537 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5538 write_insn<big_endian>(p, insn);
5542 template<bool big_endian>
5543 static unsigned char*
5544 restgpr1_tail(unsigned char* p, int r)
5546 p = restgpr1<big_endian>(p, r);
5547 write_insn<big_endian>(p, blr);
5551 template<bool big_endian>
5552 static unsigned char*
5553 savefpr(unsigned char* p, int r)
5555 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5556 write_insn<big_endian>(p, insn);
5560 template<bool big_endian>
5561 static unsigned char*
5562 savefpr0_tail(unsigned char* p, int r)
5564 p = savefpr<big_endian>(p, r);
5565 write_insn<big_endian>(p, std_0_1 + 16);
5567 write_insn<big_endian>(p, blr);
5571 template<bool big_endian>
5572 static unsigned char*
5573 restfpr(unsigned char* p, int r)
5575 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5576 write_insn<big_endian>(p, insn);
5580 template<bool big_endian>
5581 static unsigned char*
5582 restfpr0_tail(unsigned char* p, int r)
5584 write_insn<big_endian>(p, ld_0_1 + 16);
5586 p = restfpr<big_endian>(p, r);
5587 write_insn<big_endian>(p, mtlr_0);
5591 p = restfpr<big_endian>(p, 30);
5592 p = restfpr<big_endian>(p, 31);
5594 write_insn<big_endian>(p, blr);
5598 template<bool big_endian>
5599 static unsigned char*
5600 savefpr1_tail(unsigned char* p, int r)
5602 p = savefpr<big_endian>(p, r);
5603 write_insn<big_endian>(p, blr);
5607 template<bool big_endian>
5608 static unsigned char*
5609 restfpr1_tail(unsigned char* p, int r)
5611 p = restfpr<big_endian>(p, r);
5612 write_insn<big_endian>(p, blr);
5616 template<bool big_endian>
5617 static unsigned char*
5618 savevr(unsigned char* p, int r)
5620 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5621 write_insn<big_endian>(p, insn);
5623 insn = stvx_0_12_0 + (r << 21);
5624 write_insn<big_endian>(p, insn);
5628 template<bool big_endian>
5629 static unsigned char*
5630 savevr_tail(unsigned char* p, int r)
5632 p = savevr<big_endian>(p, r);
5633 write_insn<big_endian>(p, blr);
5637 template<bool big_endian>
5638 static unsigned char*
5639 restvr(unsigned char* p, int r)
5641 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5642 write_insn<big_endian>(p, insn);
5644 insn = lvx_0_12_0 + (r << 21);
5645 write_insn<big_endian>(p, insn);
5649 template<bool big_endian>
5650 static unsigned char*
5651 restvr_tail(unsigned char* p, int r)
5653 p = restvr<big_endian>(p, r);
5654 write_insn<big_endian>(p, blr);
5659 template<int size, bool big_endian>
5660 Output_data_save_res<size, big_endian>::Output_data_save_res(
5661 Symbol_table* symtab)
5662 : Output_section_data_build(4),
5665 this->savres_define(symtab,
5666 "_savegpr0_", 14, 31,
5667 savegpr0<big_endian>, savegpr0_tail<big_endian>);
5668 this->savres_define(symtab,
5669 "_restgpr0_", 14, 29,
5670 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5671 this->savres_define(symtab,
5672 "_restgpr0_", 30, 31,
5673 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5674 this->savres_define(symtab,
5675 "_savegpr1_", 14, 31,
5676 savegpr1<big_endian>, savegpr1_tail<big_endian>);
5677 this->savres_define(symtab,
5678 "_restgpr1_", 14, 31,
5679 restgpr1<big_endian>, restgpr1_tail<big_endian>);
5680 this->savres_define(symtab,
5681 "_savefpr_", 14, 31,
5682 savefpr<big_endian>, savefpr0_tail<big_endian>);
5683 this->savres_define(symtab,
5684 "_restfpr_", 14, 29,
5685 restfpr<big_endian>, restfpr0_tail<big_endian>);
5686 this->savres_define(symtab,
5687 "_restfpr_", 30, 31,
5688 restfpr<big_endian>, restfpr0_tail<big_endian>);
5689 this->savres_define(symtab,
5691 savefpr<big_endian>, savefpr1_tail<big_endian>);
5692 this->savres_define(symtab,
5694 restfpr<big_endian>, restfpr1_tail<big_endian>);
5695 this->savres_define(symtab,
5697 savevr<big_endian>, savevr_tail<big_endian>);
5698 this->savres_define(symtab,
5700 restvr<big_endian>, restvr_tail<big_endian>);
5703 template<int size, bool big_endian>
5705 Output_data_save_res<size, big_endian>::savres_define(
5706 Symbol_table* symtab,
5708 unsigned int lo, unsigned int hi,
5709 unsigned char* write_ent(unsigned char*, int),
5710 unsigned char* write_tail(unsigned char*, int))
5712 size_t len = strlen(name);
5713 bool writing = false;
5716 memcpy(sym, name, len);
5719 for (unsigned int i = lo; i <= hi; i++)
5721 sym[len + 0] = i / 10 + '0';
5722 sym[len + 1] = i % 10 + '0';
5723 Symbol* gsym = symtab->lookup(sym);
5724 bool refd = gsym != NULL && gsym->is_undefined();
5725 writing = writing || refd;
5728 if (this->contents_ == NULL)
5729 this->contents_ = new unsigned char[this->savres_max];
5731 section_size_type value = this->current_data_size();
5732 unsigned char* p = this->contents_ + value;
5734 p = write_ent(p, i);
5736 p = write_tail(p, i);
5737 section_size_type cur_size = p - this->contents_;
5738 this->set_current_data_size(cur_size);
5740 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
5741 this, value, cur_size - value,
5742 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
5743 elfcpp::STV_HIDDEN, 0, false, false);
5748 // Write out save/restore.
5750 template<int size, bool big_endian>
5752 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
5754 const section_size_type off = this->offset();
5755 const section_size_type oview_size =
5756 convert_to_section_size_type(this->data_size());
5757 unsigned char* const oview = of->get_output_view(off, oview_size);
5758 memcpy(oview, this->contents_, oview_size);
5759 of->write_output_view(off, oview_size, oview);
5763 // Create the glink section.
5765 template<int size, bool big_endian>
5767 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
5769 if (this->glink_ == NULL)
5771 this->glink_ = new Output_data_glink<size, big_endian>(this);
5772 this->glink_->add_eh_frame(layout);
5773 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
5774 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
5775 this->glink_, ORDER_TEXT, false);
5779 // Create a PLT entry for a global symbol.
5781 template<int size, bool big_endian>
5783 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
5787 if (gsym->type() == elfcpp::STT_GNU_IFUNC
5788 && gsym->can_use_relative_reloc(false))
5790 if (this->iplt_ == NULL)
5791 this->make_iplt_section(symtab, layout);
5792 this->iplt_->add_ifunc_entry(gsym);
5796 if (this->plt_ == NULL)
5797 this->make_plt_section(symtab, layout);
5798 this->plt_->add_entry(gsym);
5802 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5804 template<int size, bool big_endian>
5806 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
5807 Symbol_table* symtab,
5809 Sized_relobj_file<size, big_endian>* relobj,
5812 if (this->iplt_ == NULL)
5813 this->make_iplt_section(symtab, layout);
5814 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
5817 // Return the number of entries in the PLT.
5819 template<int size, bool big_endian>
5821 Target_powerpc<size, big_endian>::plt_entry_count() const
5823 if (this->plt_ == NULL)
5825 return this->plt_->entry_count();
5828 // Create a GOT entry for local dynamic __tls_get_addr calls.
5830 template<int size, bool big_endian>
5832 Target_powerpc<size, big_endian>::tlsld_got_offset(
5833 Symbol_table* symtab,
5835 Sized_relobj_file<size, big_endian>* object)
5837 if (this->tlsld_got_offset_ == -1U)
5839 gold_assert(symtab != NULL && layout != NULL && object != NULL);
5840 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
5841 Output_data_got_powerpc<size, big_endian>* got
5842 = this->got_section(symtab, layout);
5843 unsigned int got_offset = got->add_constant_pair(0, 0);
5844 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
5846 this->tlsld_got_offset_ = got_offset;
5848 return this->tlsld_got_offset_;
5851 // Get the Reference_flags for a particular relocation.
5853 template<int size, bool big_endian>
5855 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
5856 unsigned int r_type,
5857 const Target_powerpc* target)
5863 case elfcpp::R_POWERPC_NONE:
5864 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5865 case elfcpp::R_POWERPC_GNU_VTENTRY:
5866 case elfcpp::R_PPC64_TOC:
5867 // No symbol reference.
5870 case elfcpp::R_PPC64_ADDR64:
5871 case elfcpp::R_PPC64_UADDR64:
5872 case elfcpp::R_POWERPC_ADDR32:
5873 case elfcpp::R_POWERPC_UADDR32:
5874 case elfcpp::R_POWERPC_ADDR16:
5875 case elfcpp::R_POWERPC_UADDR16:
5876 case elfcpp::R_POWERPC_ADDR16_LO:
5877 case elfcpp::R_POWERPC_ADDR16_HI:
5878 case elfcpp::R_POWERPC_ADDR16_HA:
5879 ref = Symbol::ABSOLUTE_REF;
5882 case elfcpp::R_POWERPC_ADDR24:
5883 case elfcpp::R_POWERPC_ADDR14:
5884 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5885 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5886 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
5889 case elfcpp::R_PPC64_REL64:
5890 case elfcpp::R_POWERPC_REL32:
5891 case elfcpp::R_PPC_LOCAL24PC:
5892 case elfcpp::R_POWERPC_REL16:
5893 case elfcpp::R_POWERPC_REL16_LO:
5894 case elfcpp::R_POWERPC_REL16_HI:
5895 case elfcpp::R_POWERPC_REL16_HA:
5896 ref = Symbol::RELATIVE_REF;
5899 case elfcpp::R_POWERPC_REL24:
5900 case elfcpp::R_PPC_PLTREL24:
5901 case elfcpp::R_POWERPC_REL14:
5902 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5903 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5904 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
5907 case elfcpp::R_POWERPC_GOT16:
5908 case elfcpp::R_POWERPC_GOT16_LO:
5909 case elfcpp::R_POWERPC_GOT16_HI:
5910 case elfcpp::R_POWERPC_GOT16_HA:
5911 case elfcpp::R_PPC64_GOT16_DS:
5912 case elfcpp::R_PPC64_GOT16_LO_DS:
5913 case elfcpp::R_PPC64_TOC16:
5914 case elfcpp::R_PPC64_TOC16_LO:
5915 case elfcpp::R_PPC64_TOC16_HI:
5916 case elfcpp::R_PPC64_TOC16_HA:
5917 case elfcpp::R_PPC64_TOC16_DS:
5918 case elfcpp::R_PPC64_TOC16_LO_DS:
5919 ref = Symbol::RELATIVE_REF;
5922 case elfcpp::R_POWERPC_GOT_TPREL16:
5923 case elfcpp::R_POWERPC_TLS:
5924 ref = Symbol::TLS_REF;
5927 case elfcpp::R_POWERPC_COPY:
5928 case elfcpp::R_POWERPC_GLOB_DAT:
5929 case elfcpp::R_POWERPC_JMP_SLOT:
5930 case elfcpp::R_POWERPC_RELATIVE:
5931 case elfcpp::R_POWERPC_DTPMOD:
5933 // Not expected. We will give an error later.
5937 if (size == 64 && target->abiversion() < 2)
5938 ref |= Symbol::FUNC_DESC_ABI;
5942 // Report an unsupported relocation against a local symbol.
5944 template<int size, bool big_endian>
5946 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
5947 Sized_relobj_file<size, big_endian>* object,
5948 unsigned int r_type)
5950 gold_error(_("%s: unsupported reloc %u against local symbol"),
5951 object->name().c_str(), r_type);
5954 // We are about to emit a dynamic relocation of type R_TYPE. If the
5955 // dynamic linker does not support it, issue an error.
5957 template<int size, bool big_endian>
5959 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
5960 unsigned int r_type)
5962 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
5964 // These are the relocation types supported by glibc for both 32-bit
5965 // and 64-bit powerpc.
5968 case elfcpp::R_POWERPC_NONE:
5969 case elfcpp::R_POWERPC_RELATIVE:
5970 case elfcpp::R_POWERPC_GLOB_DAT:
5971 case elfcpp::R_POWERPC_DTPMOD:
5972 case elfcpp::R_POWERPC_DTPREL:
5973 case elfcpp::R_POWERPC_TPREL:
5974 case elfcpp::R_POWERPC_JMP_SLOT:
5975 case elfcpp::R_POWERPC_COPY:
5976 case elfcpp::R_POWERPC_IRELATIVE:
5977 case elfcpp::R_POWERPC_ADDR32:
5978 case elfcpp::R_POWERPC_UADDR32:
5979 case elfcpp::R_POWERPC_ADDR24:
5980 case elfcpp::R_POWERPC_ADDR16:
5981 case elfcpp::R_POWERPC_UADDR16:
5982 case elfcpp::R_POWERPC_ADDR16_LO:
5983 case elfcpp::R_POWERPC_ADDR16_HI:
5984 case elfcpp::R_POWERPC_ADDR16_HA:
5985 case elfcpp::R_POWERPC_ADDR14:
5986 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5987 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5988 case elfcpp::R_POWERPC_REL32:
5989 case elfcpp::R_POWERPC_REL24:
5990 case elfcpp::R_POWERPC_TPREL16:
5991 case elfcpp::R_POWERPC_TPREL16_LO:
5992 case elfcpp::R_POWERPC_TPREL16_HI:
5993 case elfcpp::R_POWERPC_TPREL16_HA:
6004 // These are the relocation types supported only on 64-bit.
6005 case elfcpp::R_PPC64_ADDR64:
6006 case elfcpp::R_PPC64_UADDR64:
6007 case elfcpp::R_PPC64_JMP_IREL:
6008 case elfcpp::R_PPC64_ADDR16_DS:
6009 case elfcpp::R_PPC64_ADDR16_LO_DS:
6010 case elfcpp::R_PPC64_ADDR16_HIGH:
6011 case elfcpp::R_PPC64_ADDR16_HIGHA:
6012 case elfcpp::R_PPC64_ADDR16_HIGHER:
6013 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6014 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6015 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6016 case elfcpp::R_PPC64_REL64:
6017 case elfcpp::R_POWERPC_ADDR30:
6018 case elfcpp::R_PPC64_TPREL16_DS:
6019 case elfcpp::R_PPC64_TPREL16_LO_DS:
6020 case elfcpp::R_PPC64_TPREL16_HIGH:
6021 case elfcpp::R_PPC64_TPREL16_HIGHA:
6022 case elfcpp::R_PPC64_TPREL16_HIGHER:
6023 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6024 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6025 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6036 // These are the relocation types supported only on 32-bit.
6037 // ??? glibc ld.so doesn't need to support these.
6038 case elfcpp::R_POWERPC_DTPREL16:
6039 case elfcpp::R_POWERPC_DTPREL16_LO:
6040 case elfcpp::R_POWERPC_DTPREL16_HI:
6041 case elfcpp::R_POWERPC_DTPREL16_HA:
6049 // This prevents us from issuing more than one error per reloc
6050 // section. But we can still wind up issuing more than one
6051 // error per object file.
6052 if (this->issued_non_pic_error_)
6054 gold_assert(parameters->options().output_is_position_independent());
6055 object->error(_("requires unsupported dynamic reloc; "
6056 "recompile with -fPIC"));
6057 this->issued_non_pic_error_ = true;
6061 // Return whether we need to make a PLT entry for a relocation of the
6062 // given type against a STT_GNU_IFUNC symbol.
6064 template<int size, bool big_endian>
6066 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
6067 Target_powerpc<size, big_endian>* target,
6068 Sized_relobj_file<size, big_endian>* object,
6069 unsigned int r_type,
6072 // In non-pic code any reference will resolve to the plt call stub
6073 // for the ifunc symbol.
6074 if ((size == 32 || target->abiversion() >= 2)
6075 && !parameters->options().output_is_position_independent())
6080 // Word size refs from data sections are OK, but don't need a PLT entry.
6081 case elfcpp::R_POWERPC_ADDR32:
6082 case elfcpp::R_POWERPC_UADDR32:
6087 case elfcpp::R_PPC64_ADDR64:
6088 case elfcpp::R_PPC64_UADDR64:
6093 // GOT refs are good, but also don't need a PLT entry.
6094 case elfcpp::R_POWERPC_GOT16:
6095 case elfcpp::R_POWERPC_GOT16_LO:
6096 case elfcpp::R_POWERPC_GOT16_HI:
6097 case elfcpp::R_POWERPC_GOT16_HA:
6098 case elfcpp::R_PPC64_GOT16_DS:
6099 case elfcpp::R_PPC64_GOT16_LO_DS:
6102 // Function calls are good, and these do need a PLT entry.
6103 case elfcpp::R_POWERPC_ADDR24:
6104 case elfcpp::R_POWERPC_ADDR14:
6105 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6106 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6107 case elfcpp::R_POWERPC_REL24:
6108 case elfcpp::R_PPC_PLTREL24:
6109 case elfcpp::R_POWERPC_REL14:
6110 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6111 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6118 // Anything else is a problem.
6119 // If we are building a static executable, the libc startup function
6120 // responsible for applying indirect function relocations is going
6121 // to complain about the reloc type.
6122 // If we are building a dynamic executable, we will have a text
6123 // relocation. The dynamic loader will set the text segment
6124 // writable and non-executable to apply text relocations. So we'll
6125 // segfault when trying to run the indirection function to resolve
6128 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
6129 object->name().c_str(), r_type);
6133 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6137 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
6139 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
6140 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
6141 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6142 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6143 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6144 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6145 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6146 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6147 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6148 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6149 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6150 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6151 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6152 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6153 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6154 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
6155 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
6156 /* Exclude lfqu by testing reloc. If relocs are ever
6157 defined for the reduced D field in psq_lu then those
6158 will need testing too. */
6159 && r_type != elfcpp::R_PPC64_TOC16_LO
6160 && r_type != elfcpp::R_POWERPC_GOT16_LO)
6161 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
6163 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
6164 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
6165 /* Exclude stfqu. psq_stu as above for psq_lu. */
6166 && r_type != elfcpp::R_PPC64_TOC16_LO
6167 && r_type != elfcpp::R_POWERPC_GOT16_LO)
6168 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
6169 && (insn & 1) == 0));
6172 // Scan a relocation for a local symbol.
6174 template<int size, bool big_endian>
6176 Target_powerpc<size, big_endian>::Scan::local(
6177 Symbol_table* symtab,
6179 Target_powerpc<size, big_endian>* target,
6180 Sized_relobj_file<size, big_endian>* object,
6181 unsigned int data_shndx,
6182 Output_section* output_section,
6183 const elfcpp::Rela<size, big_endian>& reloc,
6184 unsigned int r_type,
6185 const elfcpp::Sym<size, big_endian>& lsym,
6188 this->maybe_skip_tls_get_addr_call(r_type, NULL);
6190 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6191 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6193 this->expect_tls_get_addr_call();
6194 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6195 if (tls_type != tls::TLSOPT_NONE)
6196 this->skip_next_tls_get_addr_call();
6198 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6199 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6201 this->expect_tls_get_addr_call();
6202 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6203 if (tls_type != tls::TLSOPT_NONE)
6204 this->skip_next_tls_get_addr_call();
6207 Powerpc_relobj<size, big_endian>* ppc_object
6208 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6213 && data_shndx == ppc_object->opd_shndx()
6214 && r_type == elfcpp::R_PPC64_ADDR64)
6215 ppc_object->set_opd_discard(reloc.get_r_offset());
6219 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6220 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
6221 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
6223 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6224 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6225 r_type, r_sym, reloc.get_r_addend());
6226 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
6231 case elfcpp::R_POWERPC_NONE:
6232 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6233 case elfcpp::R_POWERPC_GNU_VTENTRY:
6234 case elfcpp::R_POWERPC_TLS:
6235 case elfcpp::R_PPC64_ENTRY:
6238 case elfcpp::R_PPC64_TOC:
6240 Output_data_got_powerpc<size, big_endian>* got
6241 = target->got_section(symtab, layout);
6242 if (parameters->options().output_is_position_independent())
6244 Address off = reloc.get_r_offset();
6246 && target->abiversion() < 2
6247 && data_shndx == ppc_object->opd_shndx()
6248 && ppc_object->get_opd_discard(off - 8))
6251 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6252 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
6253 rela_dyn->add_output_section_relative(got->output_section(),
6254 elfcpp::R_POWERPC_RELATIVE,
6256 object, data_shndx, off,
6257 symobj->toc_base_offset());
6262 case elfcpp::R_PPC64_ADDR64:
6263 case elfcpp::R_PPC64_UADDR64:
6264 case elfcpp::R_POWERPC_ADDR32:
6265 case elfcpp::R_POWERPC_UADDR32:
6266 case elfcpp::R_POWERPC_ADDR24:
6267 case elfcpp::R_POWERPC_ADDR16:
6268 case elfcpp::R_POWERPC_ADDR16_LO:
6269 case elfcpp::R_POWERPC_ADDR16_HI:
6270 case elfcpp::R_POWERPC_ADDR16_HA:
6271 case elfcpp::R_POWERPC_UADDR16:
6272 case elfcpp::R_PPC64_ADDR16_HIGH:
6273 case elfcpp::R_PPC64_ADDR16_HIGHA:
6274 case elfcpp::R_PPC64_ADDR16_HIGHER:
6275 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6276 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6277 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6278 case elfcpp::R_PPC64_ADDR16_DS:
6279 case elfcpp::R_PPC64_ADDR16_LO_DS:
6280 case elfcpp::R_POWERPC_ADDR14:
6281 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6282 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6283 // If building a shared library (or a position-independent
6284 // executable), we need to create a dynamic relocation for
6286 if (parameters->options().output_is_position_independent()
6287 || (size == 64 && is_ifunc && target->abiversion() < 2))
6289 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6291 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6292 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
6293 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
6295 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6296 : elfcpp::R_POWERPC_RELATIVE);
6297 rela_dyn->add_local_relative(object, r_sym, dynrel,
6298 output_section, data_shndx,
6299 reloc.get_r_offset(),
6300 reloc.get_r_addend(), false);
6302 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
6304 check_non_pic(object, r_type);
6305 rela_dyn->add_local(object, r_sym, r_type, output_section,
6306 data_shndx, reloc.get_r_offset(),
6307 reloc.get_r_addend());
6311 gold_assert(lsym.get_st_value() == 0);
6312 unsigned int shndx = lsym.get_st_shndx();
6314 shndx = object->adjust_sym_shndx(r_sym, shndx,
6317 object->error(_("section symbol %u has bad shndx %u"),
6320 rela_dyn->add_local_section(object, shndx, r_type,
6321 output_section, data_shndx,
6322 reloc.get_r_offset());
6327 case elfcpp::R_POWERPC_REL24:
6328 case elfcpp::R_PPC_PLTREL24:
6329 case elfcpp::R_PPC_LOCAL24PC:
6330 case elfcpp::R_POWERPC_REL14:
6331 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6332 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6335 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6336 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6337 r_type, r_sym, reloc.get_r_addend());
6341 case elfcpp::R_PPC64_TOCSAVE:
6342 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6343 // caller has already saved r2 and thus a plt call stub need not
6346 && target->mark_pltcall(ppc_object, data_shndx,
6347 reloc.get_r_offset() - 4, symtab))
6349 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6350 unsigned int shndx = lsym.get_st_shndx();
6352 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6354 object->error(_("tocsave symbol %u has bad shndx %u"),
6357 target->add_tocsave(ppc_object, shndx,
6358 lsym.get_st_value() + reloc.get_r_addend());
6362 case elfcpp::R_PPC64_REL64:
6363 case elfcpp::R_POWERPC_REL32:
6364 case elfcpp::R_POWERPC_REL16:
6365 case elfcpp::R_POWERPC_REL16_LO:
6366 case elfcpp::R_POWERPC_REL16_HI:
6367 case elfcpp::R_POWERPC_REL16_HA:
6368 case elfcpp::R_POWERPC_REL16DX_HA:
6369 case elfcpp::R_POWERPC_SECTOFF:
6370 case elfcpp::R_POWERPC_SECTOFF_LO:
6371 case elfcpp::R_POWERPC_SECTOFF_HI:
6372 case elfcpp::R_POWERPC_SECTOFF_HA:
6373 case elfcpp::R_PPC64_SECTOFF_DS:
6374 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6375 case elfcpp::R_POWERPC_TPREL16:
6376 case elfcpp::R_POWERPC_TPREL16_LO:
6377 case elfcpp::R_POWERPC_TPREL16_HI:
6378 case elfcpp::R_POWERPC_TPREL16_HA:
6379 case elfcpp::R_PPC64_TPREL16_DS:
6380 case elfcpp::R_PPC64_TPREL16_LO_DS:
6381 case elfcpp::R_PPC64_TPREL16_HIGH:
6382 case elfcpp::R_PPC64_TPREL16_HIGHA:
6383 case elfcpp::R_PPC64_TPREL16_HIGHER:
6384 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6385 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6386 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6387 case elfcpp::R_POWERPC_DTPREL16:
6388 case elfcpp::R_POWERPC_DTPREL16_LO:
6389 case elfcpp::R_POWERPC_DTPREL16_HI:
6390 case elfcpp::R_POWERPC_DTPREL16_HA:
6391 case elfcpp::R_PPC64_DTPREL16_DS:
6392 case elfcpp::R_PPC64_DTPREL16_LO_DS:
6393 case elfcpp::R_PPC64_DTPREL16_HIGH:
6394 case elfcpp::R_PPC64_DTPREL16_HIGHA:
6395 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6396 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6397 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6398 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6399 case elfcpp::R_PPC64_TLSGD:
6400 case elfcpp::R_PPC64_TLSLD:
6401 case elfcpp::R_PPC64_ADDR64_LOCAL:
6404 case elfcpp::R_POWERPC_GOT16:
6405 case elfcpp::R_POWERPC_GOT16_LO:
6406 case elfcpp::R_POWERPC_GOT16_HI:
6407 case elfcpp::R_POWERPC_GOT16_HA:
6408 case elfcpp::R_PPC64_GOT16_DS:
6409 case elfcpp::R_PPC64_GOT16_LO_DS:
6411 // The symbol requires a GOT entry.
6412 Output_data_got_powerpc<size, big_endian>* got
6413 = target->got_section(symtab, layout);
6414 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6416 if (!parameters->options().output_is_position_independent())
6419 && (size == 32 || target->abiversion() >= 2))
6420 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
6422 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
6424 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
6426 // If we are generating a shared object or a pie, this
6427 // symbol's GOT entry will be set by a dynamic relocation.
6429 off = got->add_constant(0);
6430 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
6432 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6434 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6435 : elfcpp::R_POWERPC_RELATIVE);
6436 rela_dyn->add_local_relative(object, r_sym, dynrel,
6437 got, off, 0, false);
6442 case elfcpp::R_PPC64_TOC16:
6443 case elfcpp::R_PPC64_TOC16_LO:
6444 case elfcpp::R_PPC64_TOC16_HI:
6445 case elfcpp::R_PPC64_TOC16_HA:
6446 case elfcpp::R_PPC64_TOC16_DS:
6447 case elfcpp::R_PPC64_TOC16_LO_DS:
6448 // We need a GOT section.
6449 target->got_section(symtab, layout);
6452 case elfcpp::R_POWERPC_GOT_TLSGD16:
6453 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6454 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6455 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6457 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6458 if (tls_type == tls::TLSOPT_NONE)
6460 Output_data_got_powerpc<size, big_endian>* got
6461 = target->got_section(symtab, layout);
6462 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6463 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6464 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
6465 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
6467 else if (tls_type == tls::TLSOPT_TO_LE)
6469 // no GOT relocs needed for Local Exec.
6476 case elfcpp::R_POWERPC_GOT_TLSLD16:
6477 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6478 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6479 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6481 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6482 if (tls_type == tls::TLSOPT_NONE)
6483 target->tlsld_got_offset(symtab, layout, object);
6484 else if (tls_type == tls::TLSOPT_TO_LE)
6486 // no GOT relocs needed for Local Exec.
6487 if (parameters->options().emit_relocs())
6489 Output_section* os = layout->tls_segment()->first_section();
6490 gold_assert(os != NULL);
6491 os->set_needs_symtab_index();
6499 case elfcpp::R_POWERPC_GOT_DTPREL16:
6500 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6501 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6502 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6504 Output_data_got_powerpc<size, big_endian>* got
6505 = target->got_section(symtab, layout);
6506 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6507 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
6511 case elfcpp::R_POWERPC_GOT_TPREL16:
6512 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6513 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6514 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6516 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
6517 if (tls_type == tls::TLSOPT_NONE)
6519 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6520 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
6522 Output_data_got_powerpc<size, big_endian>* got
6523 = target->got_section(symtab, layout);
6524 unsigned int off = got->add_constant(0);
6525 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
6527 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6528 rela_dyn->add_symbolless_local_addend(object, r_sym,
6529 elfcpp::R_POWERPC_TPREL,
6533 else if (tls_type == tls::TLSOPT_TO_LE)
6535 // no GOT relocs needed for Local Exec.
6543 unsupported_reloc_local(object, r_type);
6548 && parameters->options().toc_optimize())
6550 if (data_shndx == ppc_object->toc_shndx())
6553 if (r_type != elfcpp::R_PPC64_ADDR64
6554 || (is_ifunc && target->abiversion() < 2))
6556 else if (parameters->options().output_is_position_independent())
6562 unsigned int shndx = lsym.get_st_shndx();
6563 if (shndx >= elfcpp::SHN_LORESERVE
6564 && shndx != elfcpp::SHN_XINDEX)
6569 ppc_object->set_no_toc_opt(reloc.get_r_offset());
6572 enum {no_check, check_lo, check_ha} insn_check;
6576 insn_check = no_check;
6579 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6580 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6581 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6582 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6583 case elfcpp::R_POWERPC_GOT16_HA:
6584 case elfcpp::R_PPC64_TOC16_HA:
6585 insn_check = check_ha;
6588 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6589 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6590 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6591 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6592 case elfcpp::R_POWERPC_GOT16_LO:
6593 case elfcpp::R_PPC64_GOT16_LO_DS:
6594 case elfcpp::R_PPC64_TOC16_LO:
6595 case elfcpp::R_PPC64_TOC16_LO_DS:
6596 insn_check = check_lo;
6600 section_size_type slen;
6601 const unsigned char* view = NULL;
6602 if (insn_check != no_check)
6604 view = ppc_object->section_contents(data_shndx, &slen, false);
6605 section_size_type off =
6606 convert_to_section_size_type(reloc.get_r_offset()) & -4;
6609 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
6610 if (insn_check == check_lo
6611 ? !ok_lo_toc_insn(insn, r_type)
6612 : ((insn & ((0x3f << 26) | 0x1f << 16))
6613 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
6615 ppc_object->set_no_toc_opt();
6616 gold_warning(_("%s: toc optimization is not supported "
6617 "for %#08x instruction"),
6618 ppc_object->name().c_str(), insn);
6627 case elfcpp::R_PPC64_TOC16:
6628 case elfcpp::R_PPC64_TOC16_LO:
6629 case elfcpp::R_PPC64_TOC16_HI:
6630 case elfcpp::R_PPC64_TOC16_HA:
6631 case elfcpp::R_PPC64_TOC16_DS:
6632 case elfcpp::R_PPC64_TOC16_LO_DS:
6633 unsigned int shndx = lsym.get_st_shndx();
6634 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6636 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6637 if (is_ordinary && shndx == ppc_object->toc_shndx())
6639 Address dst_off = lsym.get_st_value() + reloc.get_r_offset();
6640 if (dst_off < ppc_object->section_size(shndx))
6643 if (r_type == elfcpp::R_PPC64_TOC16_HA)
6645 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
6647 // Need to check that the insn is a ld
6649 view = ppc_object->section_contents(data_shndx,
6652 section_size_type off =
6653 (convert_to_section_size_type(reloc.get_r_offset())
6654 + (big_endian ? -2 : 3));
6656 && (view[off] & (0x3f << 2)) == 58u << 2)
6660 ppc_object->set_no_toc_opt(dst_off);
6671 case elfcpp::R_POWERPC_REL32:
6672 if (ppc_object->got2_shndx() != 0
6673 && parameters->options().output_is_position_independent())
6675 unsigned int shndx = lsym.get_st_shndx();
6676 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6678 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6679 if (is_ordinary && shndx == ppc_object->got2_shndx()
6680 && (ppc_object->section_flags(data_shndx)
6681 & elfcpp::SHF_EXECINSTR) != 0)
6682 gold_error(_("%s: unsupported -mbss-plt code"),
6683 ppc_object->name().c_str());
6693 case elfcpp::R_POWERPC_GOT_TLSLD16:
6694 case elfcpp::R_POWERPC_GOT_TLSGD16:
6695 case elfcpp::R_POWERPC_GOT_TPREL16:
6696 case elfcpp::R_POWERPC_GOT_DTPREL16:
6697 case elfcpp::R_POWERPC_GOT16:
6698 case elfcpp::R_PPC64_GOT16_DS:
6699 case elfcpp::R_PPC64_TOC16:
6700 case elfcpp::R_PPC64_TOC16_DS:
6701 ppc_object->set_has_small_toc_reloc();
6707 // Report an unsupported relocation against a global symbol.
6709 template<int size, bool big_endian>
6711 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
6712 Sized_relobj_file<size, big_endian>* object,
6713 unsigned int r_type,
6716 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6717 object->name().c_str(), r_type, gsym->demangled_name().c_str());
6720 // Scan a relocation for a global symbol.
6722 template<int size, bool big_endian>
6724 Target_powerpc<size, big_endian>::Scan::global(
6725 Symbol_table* symtab,
6727 Target_powerpc<size, big_endian>* target,
6728 Sized_relobj_file<size, big_endian>* object,
6729 unsigned int data_shndx,
6730 Output_section* output_section,
6731 const elfcpp::Rela<size, big_endian>& reloc,
6732 unsigned int r_type,
6735 if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
6738 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6739 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6741 this->expect_tls_get_addr_call();
6742 const bool final = gsym->final_value_is_known();
6743 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6744 if (tls_type != tls::TLSOPT_NONE)
6745 this->skip_next_tls_get_addr_call();
6747 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6748 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6750 this->expect_tls_get_addr_call();
6751 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6752 if (tls_type != tls::TLSOPT_NONE)
6753 this->skip_next_tls_get_addr_call();
6756 Powerpc_relobj<size, big_endian>* ppc_object
6757 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6759 // A STT_GNU_IFUNC symbol may require a PLT entry.
6760 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
6761 bool pushed_ifunc = false;
6762 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
6764 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6765 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6766 r_type, r_sym, reloc.get_r_addend());
6767 target->make_plt_entry(symtab, layout, gsym);
6768 pushed_ifunc = true;
6773 case elfcpp::R_POWERPC_NONE:
6774 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6775 case elfcpp::R_POWERPC_GNU_VTENTRY:
6776 case elfcpp::R_PPC_LOCAL24PC:
6777 case elfcpp::R_POWERPC_TLS:
6778 case elfcpp::R_PPC64_ENTRY:
6781 case elfcpp::R_PPC64_TOC:
6783 Output_data_got_powerpc<size, big_endian>* got
6784 = target->got_section(symtab, layout);
6785 if (parameters->options().output_is_position_independent())
6787 Address off = reloc.get_r_offset();
6789 && data_shndx == ppc_object->opd_shndx()
6790 && ppc_object->get_opd_discard(off - 8))
6793 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6794 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
6795 if (data_shndx != ppc_object->opd_shndx())
6796 symobj = static_cast
6797 <Powerpc_relobj<size, big_endian>*>(gsym->object());
6798 rela_dyn->add_output_section_relative(got->output_section(),
6799 elfcpp::R_POWERPC_RELATIVE,
6801 object, data_shndx, off,
6802 symobj->toc_base_offset());
6807 case elfcpp::R_PPC64_ADDR64:
6809 && target->abiversion() < 2
6810 && data_shndx == ppc_object->opd_shndx()
6811 && (gsym->is_defined_in_discarded_section()
6812 || gsym->object() != object))
6814 ppc_object->set_opd_discard(reloc.get_r_offset());
6818 case elfcpp::R_PPC64_UADDR64:
6819 case elfcpp::R_POWERPC_ADDR32:
6820 case elfcpp::R_POWERPC_UADDR32:
6821 case elfcpp::R_POWERPC_ADDR24:
6822 case elfcpp::R_POWERPC_ADDR16:
6823 case elfcpp::R_POWERPC_ADDR16_LO:
6824 case elfcpp::R_POWERPC_ADDR16_HI:
6825 case elfcpp::R_POWERPC_ADDR16_HA:
6826 case elfcpp::R_POWERPC_UADDR16:
6827 case elfcpp::R_PPC64_ADDR16_HIGH:
6828 case elfcpp::R_PPC64_ADDR16_HIGHA:
6829 case elfcpp::R_PPC64_ADDR16_HIGHER:
6830 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6831 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6832 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6833 case elfcpp::R_PPC64_ADDR16_DS:
6834 case elfcpp::R_PPC64_ADDR16_LO_DS:
6835 case elfcpp::R_POWERPC_ADDR14:
6836 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6837 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6839 // Make a PLT entry if necessary.
6840 if (gsym->needs_plt_entry())
6842 // Since this is not a PC-relative relocation, we may be
6843 // taking the address of a function. In that case we need to
6844 // set the entry in the dynamic symbol table to the address of
6845 // the PLT call stub.
6846 bool need_ifunc_plt = false;
6847 if ((size == 32 || target->abiversion() >= 2)
6848 && gsym->is_from_dynobj()
6849 && !parameters->options().output_is_position_independent())
6851 gsym->set_needs_dynsym_value();
6852 need_ifunc_plt = true;
6854 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
6856 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6857 target->push_branch(ppc_object, data_shndx,
6858 reloc.get_r_offset(), r_type, r_sym,
6859 reloc.get_r_addend());
6860 target->make_plt_entry(symtab, layout, gsym);
6863 // Make a dynamic relocation if necessary.
6864 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
6865 || (size == 64 && is_ifunc && target->abiversion() < 2))
6867 if (!parameters->options().output_is_position_independent()
6868 && gsym->may_need_copy_reloc())
6870 target->copy_reloc(symtab, layout, object,
6871 data_shndx, output_section, gsym, reloc);
6873 else if ((((size == 32
6874 && r_type == elfcpp::R_POWERPC_ADDR32)
6876 && r_type == elfcpp::R_PPC64_ADDR64
6877 && target->abiversion() >= 2))
6878 && gsym->can_use_relative_reloc(false)
6879 && !(gsym->visibility() == elfcpp::STV_PROTECTED
6880 && parameters->options().shared()))
6882 && r_type == elfcpp::R_PPC64_ADDR64
6883 && target->abiversion() < 2
6884 && (gsym->can_use_relative_reloc(false)
6885 || data_shndx == ppc_object->opd_shndx())))
6887 Reloc_section* rela_dyn
6888 = target->rela_dyn_section(symtab, layout, is_ifunc);
6889 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6890 : elfcpp::R_POWERPC_RELATIVE);
6891 rela_dyn->add_symbolless_global_addend(
6892 gsym, dynrel, output_section, object, data_shndx,
6893 reloc.get_r_offset(), reloc.get_r_addend());
6897 Reloc_section* rela_dyn
6898 = target->rela_dyn_section(symtab, layout, is_ifunc);
6899 check_non_pic(object, r_type);
6900 rela_dyn->add_global(gsym, r_type, output_section,
6902 reloc.get_r_offset(),
6903 reloc.get_r_addend());
6906 && parameters->options().toc_optimize()
6907 && data_shndx == ppc_object->toc_shndx())
6908 ppc_object->set_no_toc_opt(reloc.get_r_offset());
6914 case elfcpp::R_PPC_PLTREL24:
6915 case elfcpp::R_POWERPC_REL24:
6918 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6919 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6920 r_type, r_sym, reloc.get_r_addend());
6921 if (gsym->needs_plt_entry()
6922 || (!gsym->final_value_is_known()
6923 && (gsym->is_undefined()
6924 || gsym->is_from_dynobj()
6925 || gsym->is_preemptible())))
6926 target->make_plt_entry(symtab, layout, gsym);
6930 case elfcpp::R_PPC64_REL64:
6931 case elfcpp::R_POWERPC_REL32:
6932 // Make a dynamic relocation if necessary.
6933 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
6935 if (!parameters->options().output_is_position_independent()
6936 && gsym->may_need_copy_reloc())
6938 target->copy_reloc(symtab, layout, object,
6939 data_shndx, output_section, gsym,
6944 Reloc_section* rela_dyn
6945 = target->rela_dyn_section(symtab, layout, is_ifunc);
6946 check_non_pic(object, r_type);
6947 rela_dyn->add_global(gsym, r_type, output_section, object,
6948 data_shndx, reloc.get_r_offset(),
6949 reloc.get_r_addend());
6954 case elfcpp::R_POWERPC_REL14:
6955 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6956 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6959 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6960 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6961 r_type, r_sym, reloc.get_r_addend());
6965 case elfcpp::R_PPC64_TOCSAVE:
6966 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6967 // caller has already saved r2 and thus a plt call stub need not
6970 && target->mark_pltcall(ppc_object, data_shndx,
6971 reloc.get_r_offset() - 4, symtab))
6973 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6975 unsigned int shndx = gsym->shndx(&is_ordinary);
6977 object->error(_("tocsave symbol %u has bad shndx %u"),
6981 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
6982 target->add_tocsave(ppc_object, shndx,
6983 sym->value() + reloc.get_r_addend());
6988 case elfcpp::R_POWERPC_REL16:
6989 case elfcpp::R_POWERPC_REL16_LO:
6990 case elfcpp::R_POWERPC_REL16_HI:
6991 case elfcpp::R_POWERPC_REL16_HA:
6992 case elfcpp::R_POWERPC_REL16DX_HA:
6993 case elfcpp::R_POWERPC_SECTOFF:
6994 case elfcpp::R_POWERPC_SECTOFF_LO:
6995 case elfcpp::R_POWERPC_SECTOFF_HI:
6996 case elfcpp::R_POWERPC_SECTOFF_HA:
6997 case elfcpp::R_PPC64_SECTOFF_DS:
6998 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6999 case elfcpp::R_POWERPC_TPREL16:
7000 case elfcpp::R_POWERPC_TPREL16_LO:
7001 case elfcpp::R_POWERPC_TPREL16_HI:
7002 case elfcpp::R_POWERPC_TPREL16_HA:
7003 case elfcpp::R_PPC64_TPREL16_DS:
7004 case elfcpp::R_PPC64_TPREL16_LO_DS:
7005 case elfcpp::R_PPC64_TPREL16_HIGH:
7006 case elfcpp::R_PPC64_TPREL16_HIGHA:
7007 case elfcpp::R_PPC64_TPREL16_HIGHER:
7008 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7009 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7010 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7011 case elfcpp::R_POWERPC_DTPREL16:
7012 case elfcpp::R_POWERPC_DTPREL16_LO:
7013 case elfcpp::R_POWERPC_DTPREL16_HI:
7014 case elfcpp::R_POWERPC_DTPREL16_HA:
7015 case elfcpp::R_PPC64_DTPREL16_DS:
7016 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7017 case elfcpp::R_PPC64_DTPREL16_HIGH:
7018 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7019 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7020 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7021 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7022 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7023 case elfcpp::R_PPC64_TLSGD:
7024 case elfcpp::R_PPC64_TLSLD:
7025 case elfcpp::R_PPC64_ADDR64_LOCAL:
7028 case elfcpp::R_POWERPC_GOT16:
7029 case elfcpp::R_POWERPC_GOT16_LO:
7030 case elfcpp::R_POWERPC_GOT16_HI:
7031 case elfcpp::R_POWERPC_GOT16_HA:
7032 case elfcpp::R_PPC64_GOT16_DS:
7033 case elfcpp::R_PPC64_GOT16_LO_DS:
7035 // The symbol requires a GOT entry.
7036 Output_data_got_powerpc<size, big_endian>* got;
7038 got = target->got_section(symtab, layout);
7039 if (gsym->final_value_is_known())
7042 && (size == 32 || target->abiversion() >= 2))
7043 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
7045 got->add_global(gsym, GOT_TYPE_STANDARD);
7047 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
7049 // If we are generating a shared object or a pie, this
7050 // symbol's GOT entry will be set by a dynamic relocation.
7051 unsigned int off = got->add_constant(0);
7052 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
7054 Reloc_section* rela_dyn
7055 = target->rela_dyn_section(symtab, layout, is_ifunc);
7057 if (gsym->can_use_relative_reloc(false)
7059 || target->abiversion() >= 2)
7060 && gsym->visibility() == elfcpp::STV_PROTECTED
7061 && parameters->options().shared()))
7063 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7064 : elfcpp::R_POWERPC_RELATIVE);
7065 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
7069 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
7070 rela_dyn->add_global(gsym, dynrel, got, off, 0);
7076 case elfcpp::R_PPC64_TOC16:
7077 case elfcpp::R_PPC64_TOC16_LO:
7078 case elfcpp::R_PPC64_TOC16_HI:
7079 case elfcpp::R_PPC64_TOC16_HA:
7080 case elfcpp::R_PPC64_TOC16_DS:
7081 case elfcpp::R_PPC64_TOC16_LO_DS:
7082 // We need a GOT section.
7083 target->got_section(symtab, layout);
7086 case elfcpp::R_POWERPC_GOT_TLSGD16:
7087 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7088 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7089 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7091 const bool final = gsym->final_value_is_known();
7092 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7093 if (tls_type == tls::TLSOPT_NONE)
7095 Output_data_got_powerpc<size, big_endian>* got
7096 = target->got_section(symtab, layout);
7097 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7098 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
7099 elfcpp::R_POWERPC_DTPMOD,
7100 elfcpp::R_POWERPC_DTPREL);
7102 else if (tls_type == tls::TLSOPT_TO_IE)
7104 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
7106 Output_data_got_powerpc<size, big_endian>* got
7107 = target->got_section(symtab, layout);
7108 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7109 if (gsym->is_undefined()
7110 || gsym->is_from_dynobj())
7112 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
7113 elfcpp::R_POWERPC_TPREL);
7117 unsigned int off = got->add_constant(0);
7118 gsym->set_got_offset(GOT_TYPE_TPREL, off);
7119 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
7120 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
7125 else if (tls_type == tls::TLSOPT_TO_LE)
7127 // no GOT relocs needed for Local Exec.
7134 case elfcpp::R_POWERPC_GOT_TLSLD16:
7135 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7136 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7137 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7139 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7140 if (tls_type == tls::TLSOPT_NONE)
7141 target->tlsld_got_offset(symtab, layout, object);
7142 else if (tls_type == tls::TLSOPT_TO_LE)
7144 // no GOT relocs needed for Local Exec.
7145 if (parameters->options().emit_relocs())
7147 Output_section* os = layout->tls_segment()->first_section();
7148 gold_assert(os != NULL);
7149 os->set_needs_symtab_index();
7157 case elfcpp::R_POWERPC_GOT_DTPREL16:
7158 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7159 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7160 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7162 Output_data_got_powerpc<size, big_endian>* got
7163 = target->got_section(symtab, layout);
7164 if (!gsym->final_value_is_known()
7165 && (gsym->is_from_dynobj()
7166 || gsym->is_undefined()
7167 || gsym->is_preemptible()))
7168 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
7169 target->rela_dyn_section(layout),
7170 elfcpp::R_POWERPC_DTPREL);
7172 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
7176 case elfcpp::R_POWERPC_GOT_TPREL16:
7177 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7178 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7179 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7181 const bool final = gsym->final_value_is_known();
7182 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7183 if (tls_type == tls::TLSOPT_NONE)
7185 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
7187 Output_data_got_powerpc<size, big_endian>* got
7188 = target->got_section(symtab, layout);
7189 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7190 if (gsym->is_undefined()
7191 || gsym->is_from_dynobj())
7193 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
7194 elfcpp::R_POWERPC_TPREL);
7198 unsigned int off = got->add_constant(0);
7199 gsym->set_got_offset(GOT_TYPE_TPREL, off);
7200 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
7201 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
7206 else if (tls_type == tls::TLSOPT_TO_LE)
7208 // no GOT relocs needed for Local Exec.
7216 unsupported_reloc_global(object, r_type, gsym);
7221 && parameters->options().toc_optimize())
7223 if (data_shndx == ppc_object->toc_shndx())
7226 if (r_type != elfcpp::R_PPC64_ADDR64
7227 || (is_ifunc && target->abiversion() < 2))
7229 else if (parameters->options().output_is_position_independent()
7230 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
7233 ppc_object->set_no_toc_opt(reloc.get_r_offset());
7236 enum {no_check, check_lo, check_ha} insn_check;
7240 insn_check = no_check;
7243 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7244 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7245 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7246 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7247 case elfcpp::R_POWERPC_GOT16_HA:
7248 case elfcpp::R_PPC64_TOC16_HA:
7249 insn_check = check_ha;
7252 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7253 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7254 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7255 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7256 case elfcpp::R_POWERPC_GOT16_LO:
7257 case elfcpp::R_PPC64_GOT16_LO_DS:
7258 case elfcpp::R_PPC64_TOC16_LO:
7259 case elfcpp::R_PPC64_TOC16_LO_DS:
7260 insn_check = check_lo;
7264 section_size_type slen;
7265 const unsigned char* view = NULL;
7266 if (insn_check != no_check)
7268 view = ppc_object->section_contents(data_shndx, &slen, false);
7269 section_size_type off =
7270 convert_to_section_size_type(reloc.get_r_offset()) & -4;
7273 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
7274 if (insn_check == check_lo
7275 ? !ok_lo_toc_insn(insn, r_type)
7276 : ((insn & ((0x3f << 26) | 0x1f << 16))
7277 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7279 ppc_object->set_no_toc_opt();
7280 gold_warning(_("%s: toc optimization is not supported "
7281 "for %#08x instruction"),
7282 ppc_object->name().c_str(), insn);
7291 case elfcpp::R_PPC64_TOC16:
7292 case elfcpp::R_PPC64_TOC16_LO:
7293 case elfcpp::R_PPC64_TOC16_HI:
7294 case elfcpp::R_PPC64_TOC16_HA:
7295 case elfcpp::R_PPC64_TOC16_DS:
7296 case elfcpp::R_PPC64_TOC16_LO_DS:
7297 if (gsym->source() == Symbol::FROM_OBJECT
7298 && !gsym->object()->is_dynamic())
7300 Powerpc_relobj<size, big_endian>* sym_object
7301 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
7303 unsigned int shndx = gsym->shndx(&is_ordinary);
7304 if (shndx == sym_object->toc_shndx())
7306 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
7307 Address dst_off = sym->value() + reloc.get_r_offset();
7308 if (dst_off < sym_object->section_size(shndx))
7311 if (r_type == elfcpp::R_PPC64_TOC16_HA)
7313 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
7315 // Need to check that the insn is a ld
7317 view = ppc_object->section_contents(data_shndx,
7320 section_size_type off =
7321 (convert_to_section_size_type(reloc.get_r_offset())
7322 + (big_endian ? -2 : 3));
7324 && (view[off] & (0x3f << 2)) == (58u << 2))
7328 sym_object->set_no_toc_opt(dst_off);
7340 case elfcpp::R_PPC_LOCAL24PC:
7341 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7342 gold_error(_("%s: unsupported -mbss-plt code"),
7343 ppc_object->name().c_str());
7352 case elfcpp::R_POWERPC_GOT_TLSLD16:
7353 case elfcpp::R_POWERPC_GOT_TLSGD16:
7354 case elfcpp::R_POWERPC_GOT_TPREL16:
7355 case elfcpp::R_POWERPC_GOT_DTPREL16:
7356 case elfcpp::R_POWERPC_GOT16:
7357 case elfcpp::R_PPC64_GOT16_DS:
7358 case elfcpp::R_PPC64_TOC16:
7359 case elfcpp::R_PPC64_TOC16_DS:
7360 ppc_object->set_has_small_toc_reloc();
7366 // Process relocations for gc.
7368 template<int size, bool big_endian>
7370 Target_powerpc<size, big_endian>::gc_process_relocs(
7371 Symbol_table* symtab,
7373 Sized_relobj_file<size, big_endian>* object,
7374 unsigned int data_shndx,
7376 const unsigned char* prelocs,
7378 Output_section* output_section,
7379 bool needs_special_offset_handling,
7380 size_t local_symbol_count,
7381 const unsigned char* plocal_symbols)
7383 typedef Target_powerpc<size, big_endian> Powerpc;
7384 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7387 Powerpc_relobj<size, big_endian>* ppc_object
7388 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7390 ppc_object->set_opd_valid();
7391 if (size == 64 && data_shndx == ppc_object->opd_shndx())
7393 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
7394 for (p = ppc_object->access_from_map()->begin();
7395 p != ppc_object->access_from_map()->end();
7398 Address dst_off = p->first;
7399 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
7400 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
7401 for (s = p->second.begin(); s != p->second.end(); ++s)
7403 Relobj* src_obj = s->first;
7404 unsigned int src_indx = s->second;
7405 symtab->gc()->add_reference(src_obj, src_indx,
7406 ppc_object, dst_indx);
7410 ppc_object->access_from_map()->clear();
7411 ppc_object->process_gc_mark(symtab);
7412 // Don't look at .opd relocs as .opd will reference everything.
7416 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
7425 needs_special_offset_handling,
7430 // Handle target specific gc actions when adding a gc reference from
7431 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7432 // and DST_OFF. For powerpc64, this adds a referenc to the code
7433 // section of a function descriptor.
7435 template<int size, bool big_endian>
7437 Target_powerpc<size, big_endian>::do_gc_add_reference(
7438 Symbol_table* symtab,
7440 unsigned int src_shndx,
7442 unsigned int dst_shndx,
7443 Address dst_off) const
7445 if (size != 64 || dst_obj->is_dynamic())
7448 Powerpc_relobj<size, big_endian>* ppc_object
7449 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
7450 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
7452 if (ppc_object->opd_valid())
7454 dst_shndx = ppc_object->get_opd_ent(dst_off);
7455 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
7459 // If we haven't run scan_opd_relocs, we must delay
7460 // processing this function descriptor reference.
7461 ppc_object->add_reference(src_obj, src_shndx, dst_off);
7466 // Add any special sections for this symbol to the gc work list.
7467 // For powerpc64, this adds the code section of a function
7470 template<int size, bool big_endian>
7472 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
7473 Symbol_table* symtab,
7478 Powerpc_relobj<size, big_endian>* ppc_object
7479 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
7481 unsigned int shndx = sym->shndx(&is_ordinary);
7482 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
7484 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
7485 Address dst_off = gsym->value();
7486 if (ppc_object->opd_valid())
7488 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
7489 symtab->gc()->worklist().push_back(Section_id(ppc_object,
7493 ppc_object->add_gc_mark(dst_off);
7498 // For a symbol location in .opd, set LOC to the location of the
7501 template<int size, bool big_endian>
7503 Target_powerpc<size, big_endian>::do_function_location(
7504 Symbol_location* loc) const
7506 if (size == 64 && loc->shndx != 0)
7508 if (loc->object->is_dynamic())
7510 Powerpc_dynobj<size, big_endian>* ppc_object
7511 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
7512 if (loc->shndx == ppc_object->opd_shndx())
7515 Address off = loc->offset - ppc_object->opd_address();
7516 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
7517 loc->offset = dest_off;
7522 const Powerpc_relobj<size, big_endian>* ppc_object
7523 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
7524 if (loc->shndx == ppc_object->opd_shndx())
7527 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
7528 loc->offset = dest_off;
7534 // FNOFFSET in section SHNDX in OBJECT is the start of a function
7535 // compiled with -fsplit-stack. The function calls non-split-stack
7536 // code. Change the function to ensure it has enough stack space to
7537 // call some random function.
7539 template<int size, bool big_endian>
7541 Target_powerpc<size, big_endian>::do_calls_non_split(
7544 section_offset_type fnoffset,
7545 section_size_type fnsize,
7546 const unsigned char* prelocs,
7548 unsigned char* view,
7549 section_size_type view_size,
7551 std::string* to) const
7553 // 32-bit not supported.
7557 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
7558 prelocs, reloc_count, view, view_size,
7563 // The function always starts with
7564 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
7565 // addis %r12,%r1,-allocate@ha
7566 // addi %r12,%r12,-allocate@l
7568 // but note that the addis or addi may be replaced with a nop
7570 unsigned char *entry = view + fnoffset;
7571 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
7573 if ((insn & 0xffff0000) == addis_2_12)
7575 /* Skip ELFv2 global entry code. */
7577 insn = elfcpp::Swap<32, big_endian>::readval(entry);
7580 unsigned char *pinsn = entry;
7582 const uint32_t ld_private_ss = 0xe80d8fc0;
7583 if (insn == ld_private_ss)
7585 int32_t allocate = 0;
7589 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
7590 if ((insn & 0xffff0000) == addis_12_1)
7591 allocate += (insn & 0xffff) << 16;
7592 else if ((insn & 0xffff0000) == addi_12_1
7593 || (insn & 0xffff0000) == addi_12_12)
7594 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
7595 else if (insn != nop)
7598 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
7600 int extra = parameters->options().split_stack_adjust_size();
7602 if (allocate >= 0 || extra < 0)
7604 object->error(_("split-stack stack size overflow at "
7605 "section %u offset %0zx"),
7606 shndx, static_cast<size_t>(fnoffset));
7610 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
7611 if (insn != addis_12_1)
7613 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7615 insn = addi_12_12 | (allocate & 0xffff);
7616 if (insn != addi_12_12)
7618 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7624 insn = addi_12_1 | (allocate & 0xffff);
7625 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7628 if (pinsn != entry + 12)
7629 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
7637 if (!object->has_no_split_stack())
7638 object->error(_("failed to match split-stack sequence at "
7639 "section %u offset %0zx"),
7640 shndx, static_cast<size_t>(fnoffset));
7644 // Scan relocations for a section.
7646 template<int size, bool big_endian>
7648 Target_powerpc<size, big_endian>::scan_relocs(
7649 Symbol_table* symtab,
7651 Sized_relobj_file<size, big_endian>* object,
7652 unsigned int data_shndx,
7653 unsigned int sh_type,
7654 const unsigned char* prelocs,
7656 Output_section* output_section,
7657 bool needs_special_offset_handling,
7658 size_t local_symbol_count,
7659 const unsigned char* plocal_symbols)
7661 typedef Target_powerpc<size, big_endian> Powerpc;
7662 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7665 if (!this->plt_localentry0_init_)
7667 bool plt_localentry0 = false;
7669 && this->abiversion() >= 2)
7671 if (parameters->options().user_set_plt_localentry())
7672 plt_localentry0 = parameters->options().plt_localentry();
7674 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
7675 gold_warning(_("--plt-localentry is especially dangerous without "
7676 "ld.so support to detect ABI violations"));
7678 this->plt_localentry0_ = plt_localentry0;
7679 this->plt_localentry0_init_ = true;
7682 if (sh_type == elfcpp::SHT_REL)
7684 gold_error(_("%s: unsupported REL reloc section"),
7685 object->name().c_str());
7689 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
7698 needs_special_offset_handling,
7703 // Functor class for processing the global symbol table.
7704 // Removes symbols defined on discarded opd entries.
7706 template<bool big_endian>
7707 class Global_symbol_visitor_opd
7710 Global_symbol_visitor_opd()
7714 operator()(Sized_symbol<64>* sym)
7716 if (sym->has_symtab_index()
7717 || sym->source() != Symbol::FROM_OBJECT
7718 || !sym->in_real_elf())
7721 if (sym->object()->is_dynamic())
7724 Powerpc_relobj<64, big_endian>* symobj
7725 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
7726 if (symobj->opd_shndx() == 0)
7730 unsigned int shndx = sym->shndx(&is_ordinary);
7731 if (shndx == symobj->opd_shndx()
7732 && symobj->get_opd_discard(sym->value()))
7734 sym->set_undefined();
7735 sym->set_visibility(elfcpp::STV_DEFAULT);
7736 sym->set_is_defined_in_discarded_section();
7737 sym->set_symtab_index(-1U);
7742 template<int size, bool big_endian>
7744 Target_powerpc<size, big_endian>::define_save_restore_funcs(
7746 Symbol_table* symtab)
7750 Output_data_save_res<size, big_endian>* savres
7751 = new Output_data_save_res<size, big_endian>(symtab);
7752 this->savres_section_ = savres;
7753 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7754 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7755 savres, ORDER_TEXT, false);
7759 // Sort linker created .got section first (for the header), then input
7760 // sections belonging to files using small model code.
7762 template<bool big_endian>
7763 class Sort_toc_sections
7767 operator()(const Output_section::Input_section& is1,
7768 const Output_section::Input_section& is2) const
7770 if (!is1.is_input_section() && is2.is_input_section())
7773 = (is1.is_input_section()
7774 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
7775 ->has_small_toc_reloc()));
7777 = (is2.is_input_section()
7778 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
7779 ->has_small_toc_reloc()));
7780 return small1 && !small2;
7784 // Finalize the sections.
7786 template<int size, bool big_endian>
7788 Target_powerpc<size, big_endian>::do_finalize_sections(
7790 const Input_objects*,
7791 Symbol_table* symtab)
7793 if (parameters->doing_static_link())
7795 // At least some versions of glibc elf-init.o have a strong
7796 // reference to __rela_iplt marker syms. A weak ref would be
7798 if (this->iplt_ != NULL)
7800 Reloc_section* rel = this->iplt_->rel_plt();
7801 symtab->define_in_output_data("__rela_iplt_start", NULL,
7802 Symbol_table::PREDEFINED, rel, 0, 0,
7803 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7804 elfcpp::STV_HIDDEN, 0, false, true);
7805 symtab->define_in_output_data("__rela_iplt_end", NULL,
7806 Symbol_table::PREDEFINED, rel, 0, 0,
7807 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7808 elfcpp::STV_HIDDEN, 0, true, true);
7812 symtab->define_as_constant("__rela_iplt_start", NULL,
7813 Symbol_table::PREDEFINED, 0, 0,
7814 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7815 elfcpp::STV_HIDDEN, 0, true, false);
7816 symtab->define_as_constant("__rela_iplt_end", NULL,
7817 Symbol_table::PREDEFINED, 0, 0,
7818 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7819 elfcpp::STV_HIDDEN, 0, true, false);
7825 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
7826 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
7828 if (!parameters->options().relocatable())
7830 this->define_save_restore_funcs(layout, symtab);
7832 // Annoyingly, we need to make these sections now whether or
7833 // not we need them. If we delay until do_relax then we
7834 // need to mess with the relaxation machinery checkpointing.
7835 this->got_section(symtab, layout);
7836 this->make_brlt_section(layout);
7838 if (parameters->options().toc_sort())
7840 Output_section* os = this->got_->output_section();
7841 if (os != NULL && os->input_sections().size() > 1)
7842 std::stable_sort(os->input_sections().begin(),
7843 os->input_sections().end(),
7844 Sort_toc_sections<big_endian>());
7849 // Fill in some more dynamic tags.
7850 Output_data_dynamic* odyn = layout->dynamic_data();
7853 const Reloc_section* rel_plt = (this->plt_ == NULL
7855 : this->plt_->rel_plt());
7856 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
7857 this->rela_dyn_, true, size == 32);
7861 if (this->got_ != NULL)
7863 this->got_->finalize_data_size();
7864 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
7865 this->got_, this->got_->g_o_t());
7870 if (this->glink_ != NULL)
7872 this->glink_->finalize_data_size();
7873 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
7875 (this->glink_->pltresolve_size
7878 if (this->has_localentry0_)
7879 odyn->add_constant(elfcpp::DT_PPC64_OPT,
7880 elfcpp::PPC64_OPT_LOCALENTRY);
7884 // Emit any relocs we saved in an attempt to avoid generating COPY
7886 if (this->copy_relocs_.any_saved_relocs())
7887 this->copy_relocs_.emit(this->rela_dyn_section(layout));
7890 // Emit any saved relocs, and mark toc entries using any of these
7891 // relocs as not optimizable.
7893 template<int sh_type, int size, bool big_endian>
7895 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
7896 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
7899 && parameters->options().toc_optimize())
7901 for (typename Copy_relocs<sh_type, size, big_endian>::
7902 Copy_reloc_entries::iterator p = this->entries_.begin();
7903 p != this->entries_.end();
7906 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
7909 // If the symbol is no longer defined in a dynamic object,
7910 // then we emitted a COPY relocation. If it is still
7911 // dynamic then we'll need dynamic relocations and thus
7912 // can't optimize toc entries.
7913 if (entry.sym_->is_from_dynobj())
7915 Powerpc_relobj<size, big_endian>* ppc_object
7916 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
7917 if (entry.shndx_ == ppc_object->toc_shndx())
7918 ppc_object->set_no_toc_opt(entry.address_);
7923 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
7926 // Return the value to use for a branch relocation.
7928 template<int size, bool big_endian>
7930 Target_powerpc<size, big_endian>::symval_for_branch(
7931 const Symbol_table* symtab,
7932 const Sized_symbol<size>* gsym,
7933 Powerpc_relobj<size, big_endian>* object,
7935 unsigned int *dest_shndx)
7937 if (size == 32 || this->abiversion() >= 2)
7941 // If the symbol is defined in an opd section, ie. is a function
7942 // descriptor, use the function descriptor code entry address
7943 Powerpc_relobj<size, big_endian>* symobj = object;
7945 && (gsym->source() != Symbol::FROM_OBJECT
7946 || gsym->object()->is_dynamic()))
7949 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
7950 unsigned int shndx = symobj->opd_shndx();
7953 Address opd_addr = symobj->get_output_section_offset(shndx);
7954 if (opd_addr == invalid_address)
7956 opd_addr += symobj->output_section_address(shndx);
7957 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
7960 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
7961 if (symtab->is_section_folded(symobj, *dest_shndx))
7964 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
7965 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
7966 *dest_shndx = folded.second;
7968 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
7969 if (sec_addr == invalid_address)
7972 sec_addr += symobj->output_section(*dest_shndx)->address();
7973 *value = sec_addr + sec_off;
7978 // Perform a relocation.
7980 template<int size, bool big_endian>
7982 Target_powerpc<size, big_endian>::Relocate::relocate(
7983 const Relocate_info<size, big_endian>* relinfo,
7985 Target_powerpc* target,
7988 const unsigned char* preloc,
7989 const Sized_symbol<size>* gsym,
7990 const Symbol_value<size>* psymval,
7991 unsigned char* view,
7993 section_size_type view_size)
7998 const elfcpp::Rela<size, big_endian> rela(preloc);
7999 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
8000 switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
8002 case Track_tls::NOT_EXPECTED:
8003 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8004 _("__tls_get_addr call lacks marker reloc"));
8006 case Track_tls::EXPECTED:
8007 // We have already complained.
8009 case Track_tls::SKIP:
8011 case Track_tls::NORMAL:
8015 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
8016 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
8017 typedef typename elfcpp::Rela<size, big_endian> Reltype;
8018 // Offset from start of insn to d-field reloc.
8019 const int d_offset = big_endian ? 2 : 0;
8021 Powerpc_relobj<size, big_endian>* const object
8022 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
8024 bool has_stub_value = false;
8025 bool localentry0 = false;
8026 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
8028 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
8029 : object->local_has_plt_offset(r_sym))
8030 && (!psymval->is_ifunc_symbol()
8031 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
8035 && target->abiversion() >= 2
8036 && !parameters->options().output_is_position_independent()
8037 && !is_branch_reloc(r_type))
8039 Address off = target->glink_section()->find_global_entry(gsym);
8040 if (off != invalid_address)
8042 value = target->glink_section()->global_entry_address() + off;
8043 has_stub_value = true;
8048 Stub_table<size, big_endian>* stub_table
8049 = object->stub_table(relinfo->data_shndx);
8050 if (stub_table == NULL)
8052 // This is a ref from a data section to an ifunc symbol.
8053 if (target->stub_tables().size() != 0)
8054 stub_table = target->stub_tables()[0];
8056 if (stub_table != NULL)
8058 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
8060 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
8061 rela.get_r_addend());
8063 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
8064 rela.get_r_addend());
8067 value = stub_table->stub_address() + ent->off_;
8068 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
8069 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
8070 size_t reloc_count = shdr.get_sh_size() / reloc_size;
8073 && relnum + 1 < reloc_count)
8075 Reltype next_rela(preloc + reloc_size);
8076 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
8077 == elfcpp::R_PPC64_TOCSAVE
8078 && next_rela.get_r_offset() == rela.get_r_offset() + 4)
8081 localentry0 = ent->localentry0_;
8082 has_stub_value = true;
8086 // We don't care too much about bogus debug references to
8087 // non-local functions, but otherwise there had better be a plt
8088 // call stub or global entry stub as appropriate.
8089 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
8092 if (r_type == elfcpp::R_POWERPC_GOT16
8093 || r_type == elfcpp::R_POWERPC_GOT16_LO
8094 || r_type == elfcpp::R_POWERPC_GOT16_HI
8095 || r_type == elfcpp::R_POWERPC_GOT16_HA
8096 || r_type == elfcpp::R_PPC64_GOT16_DS
8097 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
8101 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
8102 value = gsym->got_offset(GOT_TYPE_STANDARD);
8106 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
8107 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
8109 value -= target->got_section()->got_base_offset(object);
8111 else if (r_type == elfcpp::R_PPC64_TOC)
8113 value = (target->got_section()->output_section()->address()
8114 + object->toc_base_offset());
8116 else if (gsym != NULL
8117 && (r_type == elfcpp::R_POWERPC_REL24
8118 || r_type == elfcpp::R_PPC_PLTREL24)
8123 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
8124 Valtype* wv = reinterpret_cast<Valtype*>(view);
8125 bool can_plt_call = localentry0;
8126 if (!localentry0 && rela.get_r_offset() + 8 <= view_size)
8128 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
8129 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
8132 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
8134 elfcpp::Swap<32, big_endian>::
8135 writeval(wv + 1, ld_2_1 + target->stk_toc());
8136 can_plt_call = true;
8141 // If we don't have a branch and link followed by a nop,
8142 // we can't go via the plt because there is no place to
8143 // put a toc restoring instruction.
8144 // Unless we know we won't be returning.
8145 if (strcmp(gsym->name(), "__libc_start_main") == 0)
8146 can_plt_call = true;
8150 // g++ as of 20130507 emits self-calls without a
8151 // following nop. This is arguably wrong since we have
8152 // conflicting information. On the one hand a global
8153 // symbol and on the other a local call sequence, but
8154 // don't error for this special case.
8155 // It isn't possible to cheaply verify we have exactly
8156 // such a call. Allow all calls to the same section.
8158 Address code = value;
8159 if (gsym->source() == Symbol::FROM_OBJECT
8160 && gsym->object() == object)
8162 unsigned int dest_shndx = 0;
8163 if (target->abiversion() < 2)
8165 Address addend = rela.get_r_addend();
8166 code = psymval->value(object, addend);
8167 target->symval_for_branch(relinfo->symtab, gsym, object,
8168 &code, &dest_shndx);
8171 if (dest_shndx == 0)
8172 dest_shndx = gsym->shndx(&is_ordinary);
8173 ok = dest_shndx == relinfo->data_shndx;
8177 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8178 _("call lacks nop, can't restore toc; "
8179 "recompile with -fPIC"));
8185 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8186 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
8187 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
8188 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
8190 // First instruction of a global dynamic sequence, arg setup insn.
8191 const bool final = gsym == NULL || gsym->final_value_is_known();
8192 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8193 enum Got_type got_type = GOT_TYPE_STANDARD;
8194 if (tls_type == tls::TLSOPT_NONE)
8195 got_type = GOT_TYPE_TLSGD;
8196 else if (tls_type == tls::TLSOPT_TO_IE)
8197 got_type = GOT_TYPE_TPREL;
8198 if (got_type != GOT_TYPE_STANDARD)
8202 gold_assert(gsym->has_got_offset(got_type));
8203 value = gsym->got_offset(got_type);
8207 gold_assert(object->local_has_got_offset(r_sym, got_type));
8208 value = object->local_got_offset(r_sym, got_type);
8210 value -= target->got_section()->got_base_offset(object);
8212 if (tls_type == tls::TLSOPT_TO_IE)
8214 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8215 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8217 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8218 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8219 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
8221 insn |= 32 << 26; // lwz
8223 insn |= 58 << 26; // ld
8224 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8226 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
8227 - elfcpp::R_POWERPC_GOT_TLSGD16);
8229 else if (tls_type == tls::TLSOPT_TO_LE)
8231 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8232 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8234 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8235 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8236 insn &= (1 << 26) - (1 << 21); // extract rt
8241 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8242 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8243 value = psymval->value(object, rela.get_r_addend());
8247 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8249 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8250 r_type = elfcpp::R_POWERPC_NONE;
8254 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8255 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
8256 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
8257 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
8259 // First instruction of a local dynamic sequence, arg setup insn.
8260 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8261 if (tls_type == tls::TLSOPT_NONE)
8263 value = target->tlsld_got_offset();
8264 value -= target->got_section()->got_base_offset(object);
8268 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8269 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8270 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
8272 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8273 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8274 insn &= (1 << 26) - (1 << 21); // extract rt
8279 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8280 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8285 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8287 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8288 r_type = elfcpp::R_POWERPC_NONE;
8292 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
8293 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
8294 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
8295 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
8297 // Accesses relative to a local dynamic sequence address,
8298 // no optimisation here.
8301 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
8302 value = gsym->got_offset(GOT_TYPE_DTPREL);
8306 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
8307 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
8309 value -= target->got_section()->got_base_offset(object);
8311 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8312 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
8313 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
8314 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
8316 // First instruction of initial exec sequence.
8317 const bool final = gsym == NULL || gsym->final_value_is_known();
8318 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8319 if (tls_type == tls::TLSOPT_NONE)
8323 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
8324 value = gsym->got_offset(GOT_TYPE_TPREL);
8328 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
8329 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
8331 value -= target->got_section()->got_base_offset(object);
8335 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8336 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8337 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
8339 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8340 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8341 insn &= (1 << 26) - (1 << 21); // extract rt from ld
8346 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8347 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8348 value = psymval->value(object, rela.get_r_addend());
8352 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8354 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8355 r_type = elfcpp::R_POWERPC_NONE;
8359 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8360 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8362 // Second instruction of a global dynamic sequence,
8363 // the __tls_get_addr call
8364 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
8365 const bool final = gsym == NULL || gsym->final_value_is_known();
8366 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8367 if (tls_type != tls::TLSOPT_NONE)
8369 if (tls_type == tls::TLSOPT_TO_IE)
8371 Insn* iview = reinterpret_cast<Insn*>(view);
8372 Insn insn = add_3_3_13;
8375 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8376 r_type = elfcpp::R_POWERPC_NONE;
8380 Insn* iview = reinterpret_cast<Insn*>(view);
8381 Insn insn = addi_3_3;
8382 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8383 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8385 value = psymval->value(object, rela.get_r_addend());
8387 this->skip_next_tls_get_addr_call();
8390 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8391 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8393 // Second instruction of a local dynamic sequence,
8394 // the __tls_get_addr call
8395 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
8396 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8397 if (tls_type == tls::TLSOPT_TO_LE)
8399 Insn* iview = reinterpret_cast<Insn*>(view);
8400 Insn insn = addi_3_3;
8401 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8402 this->skip_next_tls_get_addr_call();
8403 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8408 else if (r_type == elfcpp::R_POWERPC_TLS)
8410 // Second instruction of an initial exec sequence
8411 const bool final = gsym == NULL || gsym->final_value_is_known();
8412 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8413 if (tls_type == tls::TLSOPT_TO_LE)
8415 Insn* iview = reinterpret_cast<Insn*>(view);
8416 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8417 unsigned int reg = size == 32 ? 2 : 13;
8418 insn = at_tls_transform(insn, reg);
8419 gold_assert(insn != 0);
8420 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8421 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8423 value = psymval->value(object, rela.get_r_addend());
8426 else if (!has_stub_value)
8429 if (!(size == 32 && r_type == elfcpp::R_PPC_PLTREL24))
8430 addend = rela.get_r_addend();
8431 value = psymval->value(object, addend);
8432 if (size == 64 && is_branch_reloc(r_type))
8434 if (target->abiversion() >= 2)
8437 value += object->ppc64_local_entry_offset(gsym);
8439 value += object->ppc64_local_entry_offset(r_sym);
8443 unsigned int dest_shndx;
8444 target->symval_for_branch(relinfo->symtab, gsym, object,
8445 &value, &dest_shndx);
8448 Address max_branch_offset = max_branch_delta(r_type);
8449 if (max_branch_offset != 0
8450 && value - address + max_branch_offset >= 2 * max_branch_offset)
8452 Stub_table<size, big_endian>* stub_table
8453 = object->stub_table(relinfo->data_shndx);
8454 if (stub_table != NULL)
8456 Address off = stub_table->find_long_branch_entry(object, value);
8457 if (off != invalid_address)
8459 value = (stub_table->stub_address() + stub_table->plt_size()
8461 has_stub_value = true;
8469 case elfcpp::R_PPC64_REL64:
8470 case elfcpp::R_POWERPC_REL32:
8471 case elfcpp::R_POWERPC_REL24:
8472 case elfcpp::R_PPC_PLTREL24:
8473 case elfcpp::R_PPC_LOCAL24PC:
8474 case elfcpp::R_POWERPC_REL16:
8475 case elfcpp::R_POWERPC_REL16_LO:
8476 case elfcpp::R_POWERPC_REL16_HI:
8477 case elfcpp::R_POWERPC_REL16_HA:
8478 case elfcpp::R_POWERPC_REL16DX_HA:
8479 case elfcpp::R_POWERPC_REL14:
8480 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8481 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8485 case elfcpp::R_PPC64_TOC16:
8486 case elfcpp::R_PPC64_TOC16_LO:
8487 case elfcpp::R_PPC64_TOC16_HI:
8488 case elfcpp::R_PPC64_TOC16_HA:
8489 case elfcpp::R_PPC64_TOC16_DS:
8490 case elfcpp::R_PPC64_TOC16_LO_DS:
8491 // Subtract the TOC base address.
8492 value -= (target->got_section()->output_section()->address()
8493 + object->toc_base_offset());
8496 case elfcpp::R_POWERPC_SECTOFF:
8497 case elfcpp::R_POWERPC_SECTOFF_LO:
8498 case elfcpp::R_POWERPC_SECTOFF_HI:
8499 case elfcpp::R_POWERPC_SECTOFF_HA:
8500 case elfcpp::R_PPC64_SECTOFF_DS:
8501 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8503 value -= os->address();
8506 case elfcpp::R_PPC64_TPREL16_DS:
8507 case elfcpp::R_PPC64_TPREL16_LO_DS:
8508 case elfcpp::R_PPC64_TPREL16_HIGH:
8509 case elfcpp::R_PPC64_TPREL16_HIGHA:
8511 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
8514 case elfcpp::R_POWERPC_TPREL16:
8515 case elfcpp::R_POWERPC_TPREL16_LO:
8516 case elfcpp::R_POWERPC_TPREL16_HI:
8517 case elfcpp::R_POWERPC_TPREL16_HA:
8518 case elfcpp::R_POWERPC_TPREL:
8519 case elfcpp::R_PPC64_TPREL16_HIGHER:
8520 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8521 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8522 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8523 // tls symbol values are relative to tls_segment()->vaddr()
8527 case elfcpp::R_PPC64_DTPREL16_DS:
8528 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8529 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8530 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8531 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8532 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8534 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
8535 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
8538 case elfcpp::R_POWERPC_DTPREL16:
8539 case elfcpp::R_POWERPC_DTPREL16_LO:
8540 case elfcpp::R_POWERPC_DTPREL16_HI:
8541 case elfcpp::R_POWERPC_DTPREL16_HA:
8542 case elfcpp::R_POWERPC_DTPREL:
8543 case elfcpp::R_PPC64_DTPREL16_HIGH:
8544 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8545 // tls symbol values are relative to tls_segment()->vaddr()
8546 value -= dtp_offset;
8549 case elfcpp::R_PPC64_ADDR64_LOCAL:
8551 value += object->ppc64_local_entry_offset(gsym);
8553 value += object->ppc64_local_entry_offset(r_sym);
8560 Insn branch_bit = 0;
8563 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8564 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8565 branch_bit = 1 << 21;
8567 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8568 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8570 Insn* iview = reinterpret_cast<Insn*>(view);
8571 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8574 if (this->is_isa_v2)
8576 // Set 'a' bit. This is 0b00010 in BO field for branch
8577 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
8578 // for branch on CTR insns (BO == 1a00t or 1a01t).
8579 if ((insn & (0x14 << 21)) == (0x04 << 21))
8581 else if ((insn & (0x14 << 21)) == (0x10 << 21))
8588 // Invert 'y' bit if not the default.
8589 if (static_cast<Signed_address>(value) < 0)
8592 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8607 // Multi-instruction sequences that access the GOT/TOC can
8608 // be optimized, eg.
8609 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
8610 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
8612 // addis ra,r2,0; addi rb,ra,x@toc@l;
8613 // to nop; addi rb,r2,x@toc;
8614 // FIXME: the @got sequence shown above is not yet
8615 // optimized. Note that gcc as of 2017-01-07 doesn't use
8616 // the ELF @got relocs except for TLS, instead using the
8617 // PowerOpen variant of a compiler managed GOT (called TOC).
8618 // The PowerOpen TOC sequence equivalent to the first
8619 // example is optimized.
8620 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8621 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8622 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8623 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8624 case elfcpp::R_POWERPC_GOT16_HA:
8625 case elfcpp::R_PPC64_TOC16_HA:
8626 if (parameters->options().toc_optimize())
8628 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8629 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8630 if (r_type == elfcpp::R_PPC64_TOC16_HA
8631 && object->make_toc_relative(target, &value))
8633 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
8634 == ((15u << 26) | (2 << 16)));
8636 if (((insn & ((0x3f << 26) | 0x1f << 16))
8637 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8638 && value + 0x8000 < 0x10000)
8640 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
8646 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8647 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8648 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8649 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8650 case elfcpp::R_POWERPC_GOT16_LO:
8651 case elfcpp::R_PPC64_GOT16_LO_DS:
8652 case elfcpp::R_PPC64_TOC16_LO:
8653 case elfcpp::R_PPC64_TOC16_LO_DS:
8654 if (parameters->options().toc_optimize())
8656 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8657 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8658 bool changed = false;
8659 if (r_type == elfcpp::R_PPC64_TOC16_LO_DS
8660 && object->make_toc_relative(target, &value))
8662 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
8663 insn ^= (14u << 26) ^ (58u << 26);
8664 r_type = elfcpp::R_PPC64_TOC16_LO;
8667 if (ok_lo_toc_insn(insn, r_type)
8668 && value + 0x8000 < 0x10000)
8670 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
8672 // Transform addic to addi when we change reg.
8673 insn &= ~((0x3f << 26) | (0x1f << 16));
8674 insn |= (14u << 26) | (2 << 16);
8678 insn &= ~(0x1f << 16);
8684 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8688 case elfcpp::R_PPC64_ENTRY:
8689 value = (target->got_section()->output_section()->address()
8690 + object->toc_base_offset());
8691 if (value + 0x80008000 <= 0xffffffff
8692 && !parameters->options().output_is_position_independent())
8694 Insn* iview = reinterpret_cast<Insn*>(view);
8695 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
8696 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
8698 if ((insn1 & ~0xfffc) == ld_2_12
8699 && insn2 == add_2_2_12)
8701 insn1 = lis_2 + ha(value);
8702 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
8703 insn2 = addi_2_2 + l(value);
8704 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
8711 if (value + 0x80008000 <= 0xffffffff)
8713 Insn* iview = reinterpret_cast<Insn*>(view);
8714 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
8715 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
8717 if ((insn1 & ~0xfffc) == ld_2_12
8718 && insn2 == add_2_2_12)
8720 insn1 = addis_2_12 + ha(value);
8721 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
8722 insn2 = addi_2_2 + l(value);
8723 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
8730 case elfcpp::R_POWERPC_REL16_LO:
8731 // If we are generating a non-PIC executable, edit
8732 // 0: addis 2,12,.TOC.-0b@ha
8733 // addi 2,2,.TOC.-0b@l
8734 // used by ELFv2 global entry points to set up r2, to
8737 // if .TOC. is in range. */
8738 if (value + address - 4 + 0x80008000 <= 0xffffffff
8741 && target->abiversion() >= 2
8742 && !parameters->options().output_is_position_independent()
8743 && rela.get_r_addend() == d_offset + 4
8745 && strcmp(gsym->name(), ".TOC.") == 0)
8747 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
8748 Reltype prev_rela(preloc - reloc_size);
8749 if ((prev_rela.get_r_info()
8750 == elfcpp::elf_r_info<size>(r_sym,
8751 elfcpp::R_POWERPC_REL16_HA))
8752 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
8753 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
8755 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8756 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
8757 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
8759 if ((insn1 & 0xffff0000) == addis_2_12
8760 && (insn2 & 0xffff0000) == addi_2_2)
8762 insn1 = lis_2 + ha(value + address - 4);
8763 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
8764 insn2 = addi_2_2 + l(value + address - 4);
8765 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
8768 relinfo->rr->set_strategy(relnum - 1,
8769 Relocatable_relocs::RELOC_SPECIAL);
8770 relinfo->rr->set_strategy(relnum,
8771 Relocatable_relocs::RELOC_SPECIAL);
8781 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
8782 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
8785 case elfcpp::R_POWERPC_ADDR32:
8786 case elfcpp::R_POWERPC_UADDR32:
8788 overflow = Reloc::CHECK_BITFIELD;
8791 case elfcpp::R_POWERPC_REL32:
8792 case elfcpp::R_POWERPC_REL16DX_HA:
8794 overflow = Reloc::CHECK_SIGNED;
8797 case elfcpp::R_POWERPC_UADDR16:
8798 overflow = Reloc::CHECK_BITFIELD;
8801 case elfcpp::R_POWERPC_ADDR16:
8802 // We really should have three separate relocations,
8803 // one for 16-bit data, one for insns with 16-bit signed fields,
8804 // and one for insns with 16-bit unsigned fields.
8805 overflow = Reloc::CHECK_BITFIELD;
8806 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
8807 overflow = Reloc::CHECK_LOW_INSN;
8810 case elfcpp::R_POWERPC_ADDR16_HI:
8811 case elfcpp::R_POWERPC_ADDR16_HA:
8812 case elfcpp::R_POWERPC_GOT16_HI:
8813 case elfcpp::R_POWERPC_GOT16_HA:
8814 case elfcpp::R_POWERPC_PLT16_HI:
8815 case elfcpp::R_POWERPC_PLT16_HA:
8816 case elfcpp::R_POWERPC_SECTOFF_HI:
8817 case elfcpp::R_POWERPC_SECTOFF_HA:
8818 case elfcpp::R_PPC64_TOC16_HI:
8819 case elfcpp::R_PPC64_TOC16_HA:
8820 case elfcpp::R_PPC64_PLTGOT16_HI:
8821 case elfcpp::R_PPC64_PLTGOT16_HA:
8822 case elfcpp::R_POWERPC_TPREL16_HI:
8823 case elfcpp::R_POWERPC_TPREL16_HA:
8824 case elfcpp::R_POWERPC_DTPREL16_HI:
8825 case elfcpp::R_POWERPC_DTPREL16_HA:
8826 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8827 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8828 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8829 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8830 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8831 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8832 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8833 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8834 case elfcpp::R_POWERPC_REL16_HI:
8835 case elfcpp::R_POWERPC_REL16_HA:
8837 overflow = Reloc::CHECK_HIGH_INSN;
8840 case elfcpp::R_POWERPC_REL16:
8841 case elfcpp::R_PPC64_TOC16:
8842 case elfcpp::R_POWERPC_GOT16:
8843 case elfcpp::R_POWERPC_SECTOFF:
8844 case elfcpp::R_POWERPC_TPREL16:
8845 case elfcpp::R_POWERPC_DTPREL16:
8846 case elfcpp::R_POWERPC_GOT_TLSGD16:
8847 case elfcpp::R_POWERPC_GOT_TLSLD16:
8848 case elfcpp::R_POWERPC_GOT_TPREL16:
8849 case elfcpp::R_POWERPC_GOT_DTPREL16:
8850 overflow = Reloc::CHECK_LOW_INSN;
8853 case elfcpp::R_POWERPC_ADDR24:
8854 case elfcpp::R_POWERPC_ADDR14:
8855 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8856 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8857 case elfcpp::R_PPC64_ADDR16_DS:
8858 case elfcpp::R_POWERPC_REL24:
8859 case elfcpp::R_PPC_PLTREL24:
8860 case elfcpp::R_PPC_LOCAL24PC:
8861 case elfcpp::R_PPC64_TPREL16_DS:
8862 case elfcpp::R_PPC64_DTPREL16_DS:
8863 case elfcpp::R_PPC64_TOC16_DS:
8864 case elfcpp::R_PPC64_GOT16_DS:
8865 case elfcpp::R_PPC64_SECTOFF_DS:
8866 case elfcpp::R_POWERPC_REL14:
8867 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8868 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8869 overflow = Reloc::CHECK_SIGNED;
8873 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8876 if (overflow == Reloc::CHECK_LOW_INSN
8877 || overflow == Reloc::CHECK_HIGH_INSN)
8879 insn = elfcpp::Swap<32, big_endian>::readval(iview);
8881 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
8882 overflow = Reloc::CHECK_BITFIELD;
8883 else if (overflow == Reloc::CHECK_LOW_INSN
8884 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
8885 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
8886 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
8887 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
8888 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
8889 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
8890 overflow = Reloc::CHECK_UNSIGNED;
8892 overflow = Reloc::CHECK_SIGNED;
8895 bool maybe_dq_reloc = false;
8896 typename Powerpc_relocate_functions<size, big_endian>::Status status
8897 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
8900 case elfcpp::R_POWERPC_NONE:
8901 case elfcpp::R_POWERPC_TLS:
8902 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8903 case elfcpp::R_POWERPC_GNU_VTENTRY:
8906 case elfcpp::R_PPC64_ADDR64:
8907 case elfcpp::R_PPC64_REL64:
8908 case elfcpp::R_PPC64_TOC:
8909 case elfcpp::R_PPC64_ADDR64_LOCAL:
8910 Reloc::addr64(view, value);
8913 case elfcpp::R_POWERPC_TPREL:
8914 case elfcpp::R_POWERPC_DTPREL:
8916 Reloc::addr64(view, value);
8918 status = Reloc::addr32(view, value, overflow);
8921 case elfcpp::R_PPC64_UADDR64:
8922 Reloc::addr64_u(view, value);
8925 case elfcpp::R_POWERPC_ADDR32:
8926 status = Reloc::addr32(view, value, overflow);
8929 case elfcpp::R_POWERPC_REL32:
8930 case elfcpp::R_POWERPC_UADDR32:
8931 status = Reloc::addr32_u(view, value, overflow);
8934 case elfcpp::R_POWERPC_ADDR24:
8935 case elfcpp::R_POWERPC_REL24:
8936 case elfcpp::R_PPC_PLTREL24:
8937 case elfcpp::R_PPC_LOCAL24PC:
8938 status = Reloc::addr24(view, value, overflow);
8941 case elfcpp::R_POWERPC_GOT_DTPREL16:
8942 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8943 case elfcpp::R_POWERPC_GOT_TPREL16:
8944 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8947 // On ppc64 these are all ds form
8948 maybe_dq_reloc = true;
8952 case elfcpp::R_POWERPC_ADDR16:
8953 case elfcpp::R_POWERPC_REL16:
8954 case elfcpp::R_PPC64_TOC16:
8955 case elfcpp::R_POWERPC_GOT16:
8956 case elfcpp::R_POWERPC_SECTOFF:
8957 case elfcpp::R_POWERPC_TPREL16:
8958 case elfcpp::R_POWERPC_DTPREL16:
8959 case elfcpp::R_POWERPC_GOT_TLSGD16:
8960 case elfcpp::R_POWERPC_GOT_TLSLD16:
8961 case elfcpp::R_POWERPC_ADDR16_LO:
8962 case elfcpp::R_POWERPC_REL16_LO:
8963 case elfcpp::R_PPC64_TOC16_LO:
8964 case elfcpp::R_POWERPC_GOT16_LO:
8965 case elfcpp::R_POWERPC_SECTOFF_LO:
8966 case elfcpp::R_POWERPC_TPREL16_LO:
8967 case elfcpp::R_POWERPC_DTPREL16_LO:
8968 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8969 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8971 status = Reloc::addr16(view, value, overflow);
8973 maybe_dq_reloc = true;
8976 case elfcpp::R_POWERPC_UADDR16:
8977 status = Reloc::addr16_u(view, value, overflow);
8980 case elfcpp::R_PPC64_ADDR16_HIGH:
8981 case elfcpp::R_PPC64_TPREL16_HIGH:
8982 case elfcpp::R_PPC64_DTPREL16_HIGH:
8984 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
8987 case elfcpp::R_POWERPC_ADDR16_HI:
8988 case elfcpp::R_POWERPC_REL16_HI:
8989 case elfcpp::R_PPC64_TOC16_HI:
8990 case elfcpp::R_POWERPC_GOT16_HI:
8991 case elfcpp::R_POWERPC_SECTOFF_HI:
8992 case elfcpp::R_POWERPC_TPREL16_HI:
8993 case elfcpp::R_POWERPC_DTPREL16_HI:
8994 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8995 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8996 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8997 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8998 Reloc::addr16_hi(view, value);
9001 case elfcpp::R_PPC64_ADDR16_HIGHA:
9002 case elfcpp::R_PPC64_TPREL16_HIGHA:
9003 case elfcpp::R_PPC64_DTPREL16_HIGHA:
9005 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
9008 case elfcpp::R_POWERPC_ADDR16_HA:
9009 case elfcpp::R_POWERPC_REL16_HA:
9010 case elfcpp::R_PPC64_TOC16_HA:
9011 case elfcpp::R_POWERPC_GOT16_HA:
9012 case elfcpp::R_POWERPC_SECTOFF_HA:
9013 case elfcpp::R_POWERPC_TPREL16_HA:
9014 case elfcpp::R_POWERPC_DTPREL16_HA:
9015 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9016 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9017 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9018 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9019 Reloc::addr16_ha(view, value);
9022 case elfcpp::R_POWERPC_REL16DX_HA:
9023 status = Reloc::addr16dx_ha(view, value, overflow);
9026 case elfcpp::R_PPC64_DTPREL16_HIGHER:
9028 // R_PPC_EMB_NADDR16_LO
9031 case elfcpp::R_PPC64_ADDR16_HIGHER:
9032 case elfcpp::R_PPC64_TPREL16_HIGHER:
9033 Reloc::addr16_hi2(view, value);
9036 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
9038 // R_PPC_EMB_NADDR16_HI
9041 case elfcpp::R_PPC64_ADDR16_HIGHERA:
9042 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9043 Reloc::addr16_ha2(view, value);
9046 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
9048 // R_PPC_EMB_NADDR16_HA
9051 case elfcpp::R_PPC64_ADDR16_HIGHEST:
9052 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9053 Reloc::addr16_hi3(view, value);
9056 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
9061 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
9062 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9063 Reloc::addr16_ha3(view, value);
9066 case elfcpp::R_PPC64_DTPREL16_DS:
9067 case elfcpp::R_PPC64_DTPREL16_LO_DS:
9069 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
9072 case elfcpp::R_PPC64_TPREL16_DS:
9073 case elfcpp::R_PPC64_TPREL16_LO_DS:
9075 // R_PPC_TLSGD, R_PPC_TLSLD
9078 case elfcpp::R_PPC64_ADDR16_DS:
9079 case elfcpp::R_PPC64_ADDR16_LO_DS:
9080 case elfcpp::R_PPC64_TOC16_DS:
9081 case elfcpp::R_PPC64_TOC16_LO_DS:
9082 case elfcpp::R_PPC64_GOT16_DS:
9083 case elfcpp::R_PPC64_GOT16_LO_DS:
9084 case elfcpp::R_PPC64_SECTOFF_DS:
9085 case elfcpp::R_PPC64_SECTOFF_LO_DS:
9086 maybe_dq_reloc = true;
9089 case elfcpp::R_POWERPC_ADDR14:
9090 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
9091 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
9092 case elfcpp::R_POWERPC_REL14:
9093 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9094 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
9095 status = Reloc::addr14(view, value, overflow);
9098 case elfcpp::R_POWERPC_COPY:
9099 case elfcpp::R_POWERPC_GLOB_DAT:
9100 case elfcpp::R_POWERPC_JMP_SLOT:
9101 case elfcpp::R_POWERPC_RELATIVE:
9102 case elfcpp::R_POWERPC_DTPMOD:
9103 case elfcpp::R_PPC64_JMP_IREL:
9104 case elfcpp::R_POWERPC_IRELATIVE:
9105 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9106 _("unexpected reloc %u in object file"),
9110 case elfcpp::R_PPC64_TOCSAVE:
9116 Symbol_location loc;
9117 loc.object = relinfo->object;
9118 loc.shndx = relinfo->data_shndx;
9119 loc.offset = rela.get_r_offset();
9120 Tocsave_loc::const_iterator p = target->tocsave_loc().find(loc);
9121 if (p != target->tocsave_loc().end())
9123 // If we've generated plt calls using this tocsave, then
9124 // the nop needs to be changed to save r2.
9125 Insn* iview = reinterpret_cast<Insn*>(view);
9126 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
9127 elfcpp::Swap<32, big_endian>::
9128 writeval(iview, std_2_1 + target->stk_toc());
9133 case elfcpp::R_PPC_EMB_SDA2I16:
9134 case elfcpp::R_PPC_EMB_SDA2REL:
9137 // R_PPC64_TLSGD, R_PPC64_TLSLD
9140 case elfcpp::R_POWERPC_PLT32:
9141 case elfcpp::R_POWERPC_PLTREL32:
9142 case elfcpp::R_POWERPC_PLT16_LO:
9143 case elfcpp::R_POWERPC_PLT16_HI:
9144 case elfcpp::R_POWERPC_PLT16_HA:
9145 case elfcpp::R_PPC_SDAREL16:
9146 case elfcpp::R_POWERPC_ADDR30:
9147 case elfcpp::R_PPC64_PLT64:
9148 case elfcpp::R_PPC64_PLTREL64:
9149 case elfcpp::R_PPC64_PLTGOT16:
9150 case elfcpp::R_PPC64_PLTGOT16_LO:
9151 case elfcpp::R_PPC64_PLTGOT16_HI:
9152 case elfcpp::R_PPC64_PLTGOT16_HA:
9153 case elfcpp::R_PPC64_PLT16_LO_DS:
9154 case elfcpp::R_PPC64_PLTGOT16_DS:
9155 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
9156 case elfcpp::R_PPC_EMB_RELSDA:
9157 case elfcpp::R_PPC_TOC16:
9160 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9161 _("unsupported reloc %u"),
9169 insn = elfcpp::Swap<32, big_endian>::readval(iview);
9171 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
9172 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
9173 && (insn & 3) == 1))
9174 status = Reloc::addr16_dq(view, value, overflow);
9176 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
9177 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
9178 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
9179 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
9180 status = Reloc::addr16_ds(view, value, overflow);
9182 status = Reloc::addr16(view, value, overflow);
9185 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
9188 && gsym->is_undefined()
9189 && is_branch_reloc(r_type))))
9191 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9192 _("relocation overflow"));
9194 gold_info(_("try relinking with a smaller --stub-group-size"));
9200 // Relocate section data.
9202 template<int size, bool big_endian>
9204 Target_powerpc<size, big_endian>::relocate_section(
9205 const Relocate_info<size, big_endian>* relinfo,
9206 unsigned int sh_type,
9207 const unsigned char* prelocs,
9209 Output_section* output_section,
9210 bool needs_special_offset_handling,
9211 unsigned char* view,
9213 section_size_type view_size,
9214 const Reloc_symbol_changes* reloc_symbol_changes)
9216 typedef Target_powerpc<size, big_endian> Powerpc;
9217 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
9218 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
9219 Powerpc_comdat_behavior;
9220 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9223 gold_assert(sh_type == elfcpp::SHT_RELA);
9225 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
9226 Powerpc_comdat_behavior, Classify_reloc>(
9232 needs_special_offset_handling,
9236 reloc_symbol_changes);
9239 template<int size, bool big_endian>
9240 class Powerpc_scan_relocatable_reloc
9243 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9244 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
9245 static const int sh_type = elfcpp::SHT_RELA;
9247 // Return the symbol referred to by the relocation.
9248 static inline unsigned int
9249 get_r_sym(const Reltype* reloc)
9250 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
9252 // Return the type of the relocation.
9253 static inline unsigned int
9254 get_r_type(const Reltype* reloc)
9255 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
9257 // Return the strategy to use for a local symbol which is not a
9258 // section symbol, given the relocation type.
9259 inline Relocatable_relocs::Reloc_strategy
9260 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
9262 if (r_type == 0 && r_sym == 0)
9263 return Relocatable_relocs::RELOC_DISCARD;
9264 return Relocatable_relocs::RELOC_COPY;
9267 // Return the strategy to use for a local symbol which is a section
9268 // symbol, given the relocation type.
9269 inline Relocatable_relocs::Reloc_strategy
9270 local_section_strategy(unsigned int, Relobj*)
9272 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
9275 // Return the strategy to use for a global symbol, given the
9276 // relocation type, the object, and the symbol index.
9277 inline Relocatable_relocs::Reloc_strategy
9278 global_strategy(unsigned int r_type, Relobj*, unsigned int)
9280 if (r_type == elfcpp::R_PPC_PLTREL24)
9281 return Relocatable_relocs::RELOC_SPECIAL;
9282 return Relocatable_relocs::RELOC_COPY;
9286 // Scan the relocs during a relocatable link.
9288 template<int size, bool big_endian>
9290 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
9291 Symbol_table* symtab,
9293 Sized_relobj_file<size, big_endian>* object,
9294 unsigned int data_shndx,
9295 unsigned int sh_type,
9296 const unsigned char* prelocs,
9298 Output_section* output_section,
9299 bool needs_special_offset_handling,
9300 size_t local_symbol_count,
9301 const unsigned char* plocal_symbols,
9302 Relocatable_relocs* rr)
9304 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
9306 gold_assert(sh_type == elfcpp::SHT_RELA);
9308 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
9316 needs_special_offset_handling,
9322 // Scan the relocs for --emit-relocs.
9324 template<int size, bool big_endian>
9326 Target_powerpc<size, big_endian>::emit_relocs_scan(
9327 Symbol_table* symtab,
9329 Sized_relobj_file<size, big_endian>* object,
9330 unsigned int data_shndx,
9331 unsigned int sh_type,
9332 const unsigned char* prelocs,
9334 Output_section* output_section,
9335 bool needs_special_offset_handling,
9336 size_t local_symbol_count,
9337 const unsigned char* plocal_syms,
9338 Relocatable_relocs* rr)
9340 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9342 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
9343 Emit_relocs_strategy;
9345 gold_assert(sh_type == elfcpp::SHT_RELA);
9347 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
9355 needs_special_offset_handling,
9361 // Emit relocations for a section.
9362 // This is a modified version of the function by the same name in
9363 // target-reloc.h. Using relocate_special_relocatable for
9364 // R_PPC_PLTREL24 would require duplication of the entire body of the
9365 // loop, so we may as well duplicate the whole thing.
9367 template<int size, bool big_endian>
9369 Target_powerpc<size, big_endian>::relocate_relocs(
9370 const Relocate_info<size, big_endian>* relinfo,
9371 unsigned int sh_type,
9372 const unsigned char* prelocs,
9374 Output_section* output_section,
9375 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
9377 Address view_address,
9379 unsigned char* reloc_view,
9380 section_size_type reloc_view_size)
9382 gold_assert(sh_type == elfcpp::SHT_RELA);
9384 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9385 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
9386 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
9387 // Offset from start of insn to d-field reloc.
9388 const int d_offset = big_endian ? 2 : 0;
9390 Powerpc_relobj<size, big_endian>* const object
9391 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
9392 const unsigned int local_count = object->local_symbol_count();
9393 unsigned int got2_shndx = object->got2_shndx();
9394 Address got2_addend = 0;
9395 if (got2_shndx != 0)
9397 got2_addend = object->get_output_section_offset(got2_shndx);
9398 gold_assert(got2_addend != invalid_address);
9401 unsigned char* pwrite = reloc_view;
9402 bool zap_next = false;
9403 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
9405 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
9406 if (strategy == Relocatable_relocs::RELOC_DISCARD)
9409 Reltype reloc(prelocs);
9410 Reltype_write reloc_write(pwrite);
9412 Address offset = reloc.get_r_offset();
9413 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
9414 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
9415 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
9416 const unsigned int orig_r_sym = r_sym;
9417 typename elfcpp::Elf_types<size>::Elf_Swxword addend
9418 = reloc.get_r_addend();
9419 const Symbol* gsym = NULL;
9423 // We could arrange to discard these and other relocs for
9424 // tls optimised sequences in the strategy methods, but for
9425 // now do as BFD ld does.
9426 r_type = elfcpp::R_POWERPC_NONE;
9430 // Get the new symbol index.
9431 Output_section* os = NULL;
9432 if (r_sym < local_count)
9436 case Relocatable_relocs::RELOC_COPY:
9437 case Relocatable_relocs::RELOC_SPECIAL:
9440 r_sym = object->symtab_index(r_sym);
9441 gold_assert(r_sym != -1U);
9445 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
9447 // We are adjusting a section symbol. We need to find
9448 // the symbol table index of the section symbol for
9449 // the output section corresponding to input section
9450 // in which this symbol is defined.
9451 gold_assert(r_sym < local_count);
9453 unsigned int shndx =
9454 object->local_symbol_input_shndx(r_sym, &is_ordinary);
9455 gold_assert(is_ordinary);
9456 os = object->output_section(shndx);
9457 gold_assert(os != NULL);
9458 gold_assert(os->needs_symtab_index());
9459 r_sym = os->symtab_index();
9469 gsym = object->global_symbol(r_sym);
9470 gold_assert(gsym != NULL);
9471 if (gsym->is_forwarder())
9472 gsym = relinfo->symtab->resolve_forwards(gsym);
9474 gold_assert(gsym->has_symtab_index());
9475 r_sym = gsym->symtab_index();
9478 // Get the new offset--the location in the output section where
9479 // this relocation should be applied.
9480 if (static_cast<Address>(offset_in_output_section) != invalid_address)
9481 offset += offset_in_output_section;
9484 section_offset_type sot_offset =
9485 convert_types<section_offset_type, Address>(offset);
9486 section_offset_type new_sot_offset =
9487 output_section->output_offset(object, relinfo->data_shndx,
9489 gold_assert(new_sot_offset != -1);
9490 offset = new_sot_offset;
9493 // In an object file, r_offset is an offset within the section.
9494 // In an executable or dynamic object, generated by
9495 // --emit-relocs, r_offset is an absolute address.
9496 if (!parameters->options().relocatable())
9498 offset += view_address;
9499 if (static_cast<Address>(offset_in_output_section) != invalid_address)
9500 offset -= offset_in_output_section;
9503 // Handle the reloc addend based on the strategy.
9504 if (strategy == Relocatable_relocs::RELOC_COPY)
9506 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
9508 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
9509 gold_assert(os != NULL);
9510 addend = psymval->value(object, addend) - os->address();
9512 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
9516 if (addend >= 32768)
9517 addend += got2_addend;
9519 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
9521 r_type = elfcpp::R_POWERPC_ADDR16_HA;
9524 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
9526 r_type = elfcpp::R_POWERPC_ADDR16_LO;
9527 addend -= d_offset + 4;
9533 if (!parameters->options().relocatable())
9535 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9536 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
9537 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
9538 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
9540 // First instruction of a global dynamic sequence,
9542 const bool final = gsym == NULL || gsym->final_value_is_known();
9543 switch (this->optimize_tls_gd(final))
9545 case tls::TLSOPT_TO_IE:
9546 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
9547 - elfcpp::R_POWERPC_GOT_TLSGD16);
9549 case tls::TLSOPT_TO_LE:
9550 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9551 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
9552 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9555 r_type = elfcpp::R_POWERPC_NONE;
9563 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9564 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
9565 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
9566 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
9568 // First instruction of a local dynamic sequence,
9570 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
9572 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9573 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
9575 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9576 const Output_section* os = relinfo->layout->tls_segment()
9578 gold_assert(os != NULL);
9579 gold_assert(os->needs_symtab_index());
9580 r_sym = os->symtab_index();
9581 addend = dtp_offset;
9585 r_type = elfcpp::R_POWERPC_NONE;
9590 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9591 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
9592 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
9593 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
9595 // First instruction of initial exec sequence.
9596 const bool final = gsym == NULL || gsym->final_value_is_known();
9597 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
9599 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9600 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
9601 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9604 r_type = elfcpp::R_POWERPC_NONE;
9609 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
9610 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
9612 // Second instruction of a global dynamic sequence,
9613 // the __tls_get_addr call
9614 const bool final = gsym == NULL || gsym->final_value_is_known();
9615 switch (this->optimize_tls_gd(final))
9617 case tls::TLSOPT_TO_IE:
9618 r_type = elfcpp::R_POWERPC_NONE;
9621 case tls::TLSOPT_TO_LE:
9622 r_type = elfcpp::R_POWERPC_TPREL16_LO;
9630 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
9631 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
9633 // Second instruction of a local dynamic sequence,
9634 // the __tls_get_addr call
9635 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
9637 const Output_section* os = relinfo->layout->tls_segment()
9639 gold_assert(os != NULL);
9640 gold_assert(os->needs_symtab_index());
9641 r_sym = os->symtab_index();
9642 addend = dtp_offset;
9643 r_type = elfcpp::R_POWERPC_TPREL16_LO;
9648 else if (r_type == elfcpp::R_POWERPC_TLS)
9650 // Second instruction of an initial exec sequence
9651 const bool final = gsym == NULL || gsym->final_value_is_known();
9652 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
9654 r_type = elfcpp::R_POWERPC_TPREL16_LO;
9660 reloc_write.put_r_offset(offset);
9661 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
9662 reloc_write.put_r_addend(addend);
9664 pwrite += reloc_size;
9667 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
9668 == reloc_view_size);
9671 // Return the value to use for a dynamic symbol which requires special
9672 // treatment. This is how we support equality comparisons of function
9673 // pointers across shared library boundaries, as described in the
9674 // processor specific ABI supplement.
9676 template<int size, bool big_endian>
9678 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
9682 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
9683 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9684 p != this->stub_tables_.end();
9687 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
9688 = (*p)->find_plt_call_entry(gsym);
9690 return (*p)->stub_address() + ent->off_;
9693 else if (this->abiversion() >= 2)
9695 Address off = this->glink_section()->find_global_entry(gsym);
9696 if (off != invalid_address)
9697 return this->glink_section()->global_entry_address() + off;
9702 // Return the PLT address to use for a local symbol.
9703 template<int size, bool big_endian>
9705 Target_powerpc<size, big_endian>::do_plt_address_for_local(
9706 const Relobj* object,
9707 unsigned int symndx) const
9711 const Sized_relobj<size, big_endian>* relobj
9712 = static_cast<const Sized_relobj<size, big_endian>*>(object);
9713 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9714 p != this->stub_tables_.end();
9717 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
9718 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
9720 return (*p)->stub_address() + ent->off_;
9726 // Return the PLT address to use for a global symbol.
9727 template<int size, bool big_endian>
9729 Target_powerpc<size, big_endian>::do_plt_address_for_global(
9730 const Symbol* gsym) const
9734 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9735 p != this->stub_tables_.end();
9738 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
9739 = (*p)->find_plt_call_entry(gsym);
9741 return (*p)->stub_address() + ent->off_;
9744 else if (this->abiversion() >= 2)
9746 Address off = this->glink_section()->find_global_entry(gsym);
9747 if (off != invalid_address)
9748 return this->glink_section()->global_entry_address() + off;
9753 // Return the offset to use for the GOT_INDX'th got entry which is
9754 // for a local tls symbol specified by OBJECT, SYMNDX.
9755 template<int size, bool big_endian>
9757 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
9758 const Relobj* object,
9759 unsigned int symndx,
9760 unsigned int got_indx) const
9762 const Powerpc_relobj<size, big_endian>* ppc_object
9763 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
9764 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
9766 for (Got_type got_type = GOT_TYPE_TLSGD;
9767 got_type <= GOT_TYPE_TPREL;
9768 got_type = Got_type(got_type + 1))
9769 if (ppc_object->local_has_got_offset(symndx, got_type))
9771 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
9772 if (got_type == GOT_TYPE_TLSGD)
9774 if (off == got_indx * (size / 8))
9776 if (got_type == GOT_TYPE_TPREL)
9786 // Return the offset to use for the GOT_INDX'th got entry which is
9787 // for global tls symbol GSYM.
9788 template<int size, bool big_endian>
9790 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
9792 unsigned int got_indx) const
9794 if (gsym->type() == elfcpp::STT_TLS)
9796 for (Got_type got_type = GOT_TYPE_TLSGD;
9797 got_type <= GOT_TYPE_TPREL;
9798 got_type = Got_type(got_type + 1))
9799 if (gsym->has_got_offset(got_type))
9801 unsigned int off = gsym->got_offset(got_type);
9802 if (got_type == GOT_TYPE_TLSGD)
9804 if (off == got_indx * (size / 8))
9806 if (got_type == GOT_TYPE_TPREL)
9816 // The selector for powerpc object files.
9818 template<int size, bool big_endian>
9819 class Target_selector_powerpc : public Target_selector
9822 Target_selector_powerpc()
9823 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
9826 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
9827 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
9829 ? (big_endian ? "elf64ppc" : "elf64lppc")
9830 : (big_endian ? "elf32ppc" : "elf32lppc")))
9834 do_instantiate_target()
9835 { return new Target_powerpc<size, big_endian>(); }
9838 Target_selector_powerpc<32, true> target_selector_ppc32;
9839 Target_selector_powerpc<32, false> target_selector_ppc32le;
9840 Target_selector_powerpc<64, true> target_selector_ppc64;
9841 Target_selector_powerpc<64, false> target_selector_ppc64le;
9843 // Instantiate these constants for -O0
9844 template<int size, bool big_endian>
9845 const int Output_data_glink<size, big_endian>::pltresolve_size;
9846 template<int size, bool big_endian>
9847 const typename Output_data_glink<size, big_endian>::Address
9848 Output_data_glink<size, big_endian>::invalid_address;
9849 template<int size, bool big_endian>
9850 const typename Stub_table<size, big_endian>::Address
9851 Stub_table<size, big_endian>::invalid_address;
9852 template<int size, bool big_endian>
9853 const typename Target_powerpc<size, big_endian>::Address
9854 Target_powerpc<size, big_endian>::invalid_address;
9856 } // End anonymous namespace.