1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009, 2010, 2011, 2012 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.
27 #include "parameters.h"
34 #include "copy-relocs.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
47 template<int size, bool big_endian>
48 class Output_data_plt_powerpc;
50 template<int size, bool big_endian>
51 class Output_data_got_powerpc;
53 template<int size, bool big_endian>
54 class Output_data_glink;
56 template<int size, bool big_endian>
57 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
60 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
61 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
62 typedef Unordered_map<Address, Section_refs> Access_from;
64 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
65 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
66 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
67 special_(0), opd_valid_(false), opd_ent_(), access_from_map_()
73 // The .got2 section shndx.
78 return this->special_;
83 // The .opd section shndx.
90 return this->special_;
93 // Init OPD entry arrays.
95 init_opd(size_t opd_size)
97 size_t count = this->opd_ent_ndx(opd_size);
98 this->opd_ent_.resize(count);
101 // Return section and offset of function entry for .opd + R_OFF.
103 get_opd_ent(Address r_off, Address* value = NULL) const
105 size_t ndx = this->opd_ent_ndx(r_off);
106 gold_assert(ndx < this->opd_ent_.size());
107 gold_assert(this->opd_ent_[ndx].shndx != 0);
109 *value = this->opd_ent_[ndx].off;
110 return this->opd_ent_[ndx].shndx;
113 // Set section and offset of function entry for .opd + R_OFF.
115 set_opd_ent(Address r_off, unsigned int shndx, Address value)
117 size_t ndx = this->opd_ent_ndx(r_off);
118 gold_assert(ndx < this->opd_ent_.size());
119 this->opd_ent_[ndx].shndx = shndx;
120 this->opd_ent_[ndx].off = value;
123 // Return discard flag for .opd + R_OFF.
125 get_opd_discard(Address r_off) const
127 size_t ndx = this->opd_ent_ndx(r_off);
128 gold_assert(ndx < this->opd_ent_.size());
129 return this->opd_ent_[ndx].discard;
132 // Set discard flag for .opd + R_OFF.
134 set_opd_discard(Address r_off)
136 size_t ndx = this->opd_ent_ndx(r_off);
137 gold_assert(ndx < this->opd_ent_.size());
138 this->opd_ent_[ndx].discard = true;
143 { return &this->access_from_map_; }
145 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
146 // section at DST_OFF.
148 add_reference(Object* src_obj,
149 unsigned int src_indx,
150 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
152 Section_id src_id(src_obj, src_indx);
153 this->access_from_map_[dst_off].insert(src_id);
156 // Add a reference to the code section specified by the .opd entry
159 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
161 size_t ndx = this->opd_ent_ndx(dst_off);
162 if (ndx >= this->opd_ent_.size())
163 this->opd_ent_.resize(ndx + 1);
164 this->opd_ent_[ndx].gc_mark = true;
168 process_gc_mark(Symbol_table* symtab)
170 for (size_t i = 0; i < this->opd_ent_.size(); i++)
171 if (this->opd_ent_[i].gc_mark)
173 unsigned int shndx = this->opd_ent_[i].shndx;
174 symtab->gc()->worklist().push(Section_id(this, shndx));
180 { return this->opd_valid_; }
184 { this->opd_valid_ = true; }
186 // Examine .rela.opd to build info about function entry points.
188 scan_opd_relocs(size_t reloc_count,
189 const unsigned char* prelocs,
190 const unsigned char* plocal_syms);
192 // Perform the Sized_relobj_file method, then set up opd info from
195 do_read_relocs(Read_relocs_data*);
197 // Set up some symbols, then perform Sized_relobj_file method.
198 // Occurs after garbage collection, which is why opd info can't be
201 do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
204 do_find_special_sections(Read_symbols_data* sd);
206 // Adjust this local symbol value. Return false if the symbol
207 // should be discarded from the output file.
209 do_adjust_local_symbol(Symbol_value<size>* lv) const
211 if (size == 64 && this->opd_shndx() != 0)
214 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
216 if (this->get_opd_discard(lv->input_value()))
222 // Return offset in output GOT section that this object will use
223 // as a TOC pointer. Won't be just a constant with multi-toc support.
225 toc_base_offset() const
237 // Return index into opd_ent_ array for .opd entry at OFF.
238 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
239 // apart when the language doesn't use the last 8-byte word, the
240 // environment pointer. Thus dividing the entry section offset by
241 // 16 will give an index into opd_ent_ that works for either layout
242 // of .opd. (It leaves some elements of the vector unused when .opd
243 // entries are spaced 24 bytes apart, but we don't know the spacing
244 // until relocations are processed, and in any case it is possible
245 // for an object to have some entries spaced 16 bytes apart and
246 // others 24 bytes apart.)
248 opd_ent_ndx(size_t off) const
251 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
252 unsigned int special_;
254 // Set at the start of gc_process_relocs, when we know opd_ent_
255 // vector is valid. The flag could be made atomic and set in
256 // do_read_relocs with memory_order_release and then tested with
257 // memory_order_acquire, potentially resulting in fewer entries in
261 // The first 8-byte word of an OPD entry gives the address of the
262 // entry point of the function. Relocatable object files have a
263 // relocation on this word. The following vector records the
264 // section and offset specified by these relocations.
265 std::vector<Opd_ent> opd_ent_;
267 // References made to this object's .opd section when running
268 // gc_process_relocs for another object, before the opd_ent_ vector
269 // is valid for this object.
270 Access_from access_from_map_;
273 template<int size, bool big_endian>
274 class Target_powerpc : public Sized_target<size, big_endian>
278 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
279 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
280 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
281 static const Address invalid_address = static_cast<Address>(0) - 1;
282 // Offset of tp and dtp pointers from start of TLS block.
283 static const Address tp_offset = 0x7000;
284 static const Address dtp_offset = 0x8000;
287 : Sized_target<size, big_endian>(&powerpc_info),
288 got_(NULL), plt_(NULL), iplt_(NULL), glink_(NULL), rela_dyn_(NULL),
289 copy_relocs_(elfcpp::R_POWERPC_COPY),
290 dynbss_(NULL), tlsld_got_offset_(-1U)
294 // Process the relocations to determine unreferenced sections for
295 // garbage collection.
297 gc_process_relocs(Symbol_table* symtab,
299 Sized_relobj_file<size, big_endian>* object,
300 unsigned int data_shndx,
301 unsigned int sh_type,
302 const unsigned char* prelocs,
304 Output_section* output_section,
305 bool needs_special_offset_handling,
306 size_t local_symbol_count,
307 const unsigned char* plocal_symbols);
309 // Scan the relocations to look for symbol adjustments.
311 scan_relocs(Symbol_table* symtab,
313 Sized_relobj_file<size, big_endian>* object,
314 unsigned int data_shndx,
315 unsigned int sh_type,
316 const unsigned char* prelocs,
318 Output_section* output_section,
319 bool needs_special_offset_handling,
320 size_t local_symbol_count,
321 const unsigned char* plocal_symbols);
323 // Map input .toc section to output .got section.
325 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
327 if (size == 64 && strcmp(name, ".toc") == 0)
335 // Provide linker defined save/restore functions.
337 define_save_restore_funcs(Layout*, Symbol_table*);
339 // Finalize the sections.
341 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
343 // Return the value to use for a dynamic which requires special
346 do_dynsym_value(const Symbol*) const;
348 // Return the PLT address to use for a local symbol.
350 do_plt_address_for_local(const Relobj*, unsigned int) const;
352 // Return the PLT address to use for a global symbol.
354 do_plt_address_for_global(const Symbol*) const;
356 // Return the offset to use for the GOT_INDX'th got entry which is
357 // for a local tls symbol specified by OBJECT, SYMNDX.
359 do_tls_offset_for_local(const Relobj* object,
361 unsigned int got_indx) const;
363 // Return the offset to use for the GOT_INDX'th got entry which is
364 // for global tls symbol GSYM.
366 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
368 // Relocate a section.
370 relocate_section(const Relocate_info<size, big_endian>*,
371 unsigned int sh_type,
372 const unsigned char* prelocs,
374 Output_section* output_section,
375 bool needs_special_offset_handling,
377 Address view_address,
378 section_size_type view_size,
379 const Reloc_symbol_changes*);
381 // Scan the relocs during a relocatable link.
383 scan_relocatable_relocs(Symbol_table* symtab,
385 Sized_relobj_file<size, big_endian>* object,
386 unsigned int data_shndx,
387 unsigned int sh_type,
388 const unsigned char* prelocs,
390 Output_section* output_section,
391 bool needs_special_offset_handling,
392 size_t local_symbol_count,
393 const unsigned char* plocal_symbols,
394 Relocatable_relocs*);
396 // Emit relocations for a section.
398 relocate_relocs(const Relocate_info<size, big_endian>*,
399 unsigned int sh_type,
400 const unsigned char* prelocs,
402 Output_section* output_section,
403 typename elfcpp::Elf_types<size>::Elf_Off
404 offset_in_output_section,
405 const Relocatable_relocs*,
407 Address view_address,
409 unsigned char* reloc_view,
410 section_size_type reloc_view_size);
412 // Return whether SYM is defined by the ABI.
414 do_is_defined_by_abi(const Symbol* sym) const
416 return strcmp(sym->name(), "__tls_get_addr") == 0;
419 // Return the size of the GOT section.
423 gold_assert(this->got_ != NULL);
424 return this->got_->data_size();
427 // Get the PLT section.
428 const Output_data_plt_powerpc<size, big_endian>*
431 gold_assert(this->plt_ != NULL);
435 // Get the IPLT section.
436 const Output_data_plt_powerpc<size, big_endian>*
439 gold_assert(this->iplt_ != NULL);
443 // Get the .glink section.
444 const Output_data_glink<size, big_endian>*
445 glink_section() const
447 gold_assert(this->glink_ != NULL);
451 // Get the GOT section.
452 const Output_data_got_powerpc<size, big_endian>*
455 gold_assert(this->got_ != NULL);
459 // Get the GOT section, creating it if necessary.
460 Output_data_got_powerpc<size, big_endian>*
461 got_section(Symbol_table*, Layout*);
464 do_make_elf_object(const std::string&, Input_file*, off_t,
465 const elfcpp::Ehdr<size, big_endian>&);
467 // Return the number of entries in the GOT.
469 got_entry_count() const
471 if (this->got_ == NULL)
473 return this->got_size() / (size / 8);
476 // Return the number of entries in the PLT.
478 plt_entry_count() const;
480 // Return the offset of the first non-reserved PLT entry.
482 first_plt_entry_offset() const;
484 // Return the size of each PLT entry.
486 plt_entry_size() const;
488 // Add any special sections for this symbol to the gc work list.
489 // For powerpc64, this adds the code section of a function
492 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
494 // Handle target specific gc actions when adding a gc reference from
495 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
496 // and DST_OFF. For powerpc64, this adds a referenc to the code
497 // section of a function descriptor.
499 do_gc_add_reference(Symbol_table* symtab,
501 unsigned int src_shndx,
503 unsigned int dst_shndx,
504 Address dst_off) const;
508 // The class which scans relocations.
512 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
515 : issued_non_pic_error_(false)
519 get_reference_flags(unsigned int r_type);
522 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
523 Sized_relobj_file<size, big_endian>* object,
524 unsigned int data_shndx,
525 Output_section* output_section,
526 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
527 const elfcpp::Sym<size, big_endian>& lsym,
531 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
532 Sized_relobj_file<size, big_endian>* object,
533 unsigned int data_shndx,
534 Output_section* output_section,
535 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
539 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
541 Sized_relobj_file<size, big_endian>* ,
544 const elfcpp::Rela<size, big_endian>& ,
546 const elfcpp::Sym<size, big_endian>&)
550 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
552 Sized_relobj_file<size, big_endian>* ,
555 const elfcpp::Rela<size,
557 unsigned int , Symbol*)
562 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
563 unsigned int r_type);
566 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
567 unsigned int r_type, Symbol*);
570 generate_tls_call(Symbol_table* symtab, Layout* layout,
571 Target_powerpc* target);
574 check_non_pic(Relobj*, unsigned int r_type);
577 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>* object,
578 unsigned int r_type);
580 // Whether we have issued an error about a non-PIC compilation.
581 bool issued_non_pic_error_;
585 symval_for_branch(Address value, const Sized_symbol<size>* gsym,
586 Powerpc_relobj<size, big_endian>* object,
587 unsigned int *dest_shndx);
589 // The class which implements relocation.
593 // Use 'at' branch hints when true, 'y' when false.
594 // FIXME maybe: set this with an option.
595 static const bool is_isa_v2 = true;
599 CALL_NOT_EXPECTED = 0,
605 : call_tls_get_addr_(CALL_NOT_EXPECTED)
610 if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
612 // FIXME: This needs to specify the location somehow.
613 gold_error(_("missing expected __tls_get_addr call"));
617 // Do a relocation. Return false if the caller should not issue
618 // any warnings about this relocation.
620 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
621 Output_section*, size_t relnum,
622 const elfcpp::Rela<size, big_endian>&,
623 unsigned int r_type, const Sized_symbol<size>*,
624 const Symbol_value<size>*,
626 typename elfcpp::Elf_types<size>::Elf_Addr,
629 // This is set if we should skip the next reloc, which should be a
630 // call to __tls_get_addr.
631 enum skip_tls call_tls_get_addr_;
634 class Relocate_comdat_behavior
637 // Decide what the linker should do for relocations that refer to
638 // discarded comdat sections.
639 inline Comdat_behavior
640 get(const char* name)
642 gold::Default_comdat_behavior default_behavior;
643 Comdat_behavior ret = default_behavior.get(name);
644 if (ret == CB_WARNING)
647 && (strcmp(name, ".fixup") == 0
648 || strcmp(name, ".got2") == 0))
651 && (strcmp(name, ".opd") == 0
652 || strcmp(name, ".toc") == 0
653 || strcmp(name, ".toc1") == 0))
660 // A class which returns the size required for a relocation type,
661 // used while scanning relocs during a relocatable link.
662 class Relocatable_size_for_reloc
666 get_size_for_reloc(unsigned int, Relobj*)
673 // Optimize the TLS relocation type based on what we know about the
674 // symbol. IS_FINAL is true if the final address of this symbol is
675 // known at link time.
677 tls::Tls_optimization
678 optimize_tls_gd(bool is_final)
680 // If we are generating a shared library, then we can't do anything
682 if (parameters->options().shared())
683 return tls::TLSOPT_NONE;
686 return tls::TLSOPT_TO_IE;
687 return tls::TLSOPT_TO_LE;
690 tls::Tls_optimization
693 if (parameters->options().shared())
694 return tls::TLSOPT_NONE;
696 return tls::TLSOPT_TO_LE;
699 tls::Tls_optimization
700 optimize_tls_ie(bool is_final)
702 if (!is_final || parameters->options().shared())
703 return tls::TLSOPT_NONE;
705 return tls::TLSOPT_TO_LE;
710 make_glink_section(Layout*);
712 // Create the PLT section.
714 make_plt_section(Layout*);
717 make_iplt_section(Layout*);
719 // Create a PLT entry for a global symbol.
721 make_plt_entry(Layout*, Symbol*,
722 const elfcpp::Rela<size, big_endian>&,
723 const Sized_relobj_file<size, big_endian>* object);
725 // Create a PLT entry for a local IFUNC symbol.
727 make_local_ifunc_plt_entry(Layout*,
728 const elfcpp::Rela<size, big_endian>&,
729 Sized_relobj_file<size, big_endian>*);
731 // Create a GOT entry for local dynamic __tls_get_addr.
733 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
734 Sized_relobj_file<size, big_endian>* object);
737 tlsld_got_offset() const
739 return this->tlsld_got_offset_;
742 // Get the dynamic reloc section, creating it if necessary.
744 rela_dyn_section(Layout*);
746 // Copy a relocation against a global symbol.
748 copy_reloc(Symbol_table* symtab, Layout* layout,
749 Sized_relobj_file<size, big_endian>* object,
750 unsigned int shndx, Output_section* output_section,
751 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
753 this->copy_relocs_.copy_reloc(symtab, layout,
754 symtab->get_sized_symbol<size>(sym),
755 object, shndx, output_section,
756 reloc, this->rela_dyn_section(layout));
759 // Information about this specific target which we pass to the
760 // general Target structure.
761 static Target::Target_info powerpc_info;
763 // The types of GOT entries needed for this platform.
764 // These values are exposed to the ABI in an incremental link.
765 // Do not renumber existing values without changing the version
766 // number of the .gnu_incremental_inputs section.
770 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
771 GOT_TYPE_DTPREL, // entry for @got@dtprel
772 GOT_TYPE_TPREL // entry for @got@tprel
775 // The GOT output section.
776 Output_data_got_powerpc<size, big_endian>* got_;
777 // The PLT output section.
778 Output_data_plt_powerpc<size, big_endian>* plt_;
779 // The IPLT output section.
780 Output_data_plt_powerpc<size, big_endian>* iplt_;
781 // The .glink output section.
782 Output_data_glink<size, big_endian>* glink_;
783 // The dynamic reloc output section.
784 Reloc_section* rela_dyn_;
785 // Relocs saved to avoid a COPY reloc.
786 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
787 // Space for variables copied with a COPY reloc.
788 Output_data_space* dynbss_;
789 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
790 unsigned int tlsld_got_offset_;
794 Target::Target_info Target_powerpc<32, true>::powerpc_info =
797 true, // is_big_endian
798 elfcpp::EM_PPC, // machine_code
799 false, // has_make_symbol
800 false, // has_resolve
801 false, // has_code_fill
802 true, // is_default_stack_executable
803 false, // can_icf_inline_merge_sections
805 "/usr/lib/ld.so.1", // dynamic_linker
806 0x10000000, // default_text_segment_address
807 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
808 4 * 1024, // common_pagesize (overridable by -z common-page-size)
809 false, // isolate_execinstr
811 elfcpp::SHN_UNDEF, // small_common_shndx
812 elfcpp::SHN_UNDEF, // large_common_shndx
813 0, // small_common_section_flags
814 0, // large_common_section_flags
815 NULL, // attributes_section
816 NULL // attributes_vendor
820 Target::Target_info Target_powerpc<32, false>::powerpc_info =
823 false, // is_big_endian
824 elfcpp::EM_PPC, // machine_code
825 false, // has_make_symbol
826 false, // has_resolve
827 false, // has_code_fill
828 true, // is_default_stack_executable
829 false, // can_icf_inline_merge_sections
831 "/usr/lib/ld.so.1", // dynamic_linker
832 0x10000000, // default_text_segment_address
833 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
834 4 * 1024, // common_pagesize (overridable by -z common-page-size)
835 false, // isolate_execinstr
837 elfcpp::SHN_UNDEF, // small_common_shndx
838 elfcpp::SHN_UNDEF, // large_common_shndx
839 0, // small_common_section_flags
840 0, // large_common_section_flags
841 NULL, // attributes_section
842 NULL // attributes_vendor
846 Target::Target_info Target_powerpc<64, true>::powerpc_info =
849 true, // is_big_endian
850 elfcpp::EM_PPC64, // machine_code
851 false, // has_make_symbol
852 false, // has_resolve
853 false, // has_code_fill
854 true, // is_default_stack_executable
855 false, // can_icf_inline_merge_sections
857 "/usr/lib/ld.so.1", // dynamic_linker
858 0x10000000, // default_text_segment_address
859 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
860 4 * 1024, // common_pagesize (overridable by -z common-page-size)
861 false, // isolate_execinstr
863 elfcpp::SHN_UNDEF, // small_common_shndx
864 elfcpp::SHN_UNDEF, // large_common_shndx
865 0, // small_common_section_flags
866 0, // large_common_section_flags
867 NULL, // attributes_section
868 NULL // attributes_vendor
872 Target::Target_info Target_powerpc<64, false>::powerpc_info =
875 false, // is_big_endian
876 elfcpp::EM_PPC64, // machine_code
877 false, // has_make_symbol
878 false, // has_resolve
879 false, // has_code_fill
880 true, // is_default_stack_executable
881 false, // can_icf_inline_merge_sections
883 "/usr/lib/ld.so.1", // dynamic_linker
884 0x10000000, // default_text_segment_address
885 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
886 4 * 1024, // common_pagesize (overridable by -z common-page-size)
887 false, // isolate_execinstr
889 elfcpp::SHN_UNDEF, // small_common_shndx
890 elfcpp::SHN_UNDEF, // large_common_shndx
891 0, // small_common_section_flags
892 0, // large_common_section_flags
893 NULL, // attributes_section
894 NULL // attributes_vendor
898 is_branch_reloc(unsigned int r_type)
900 return (r_type == elfcpp::R_POWERPC_REL24
901 || r_type == elfcpp::R_PPC_PLTREL24
902 || r_type == elfcpp::R_PPC_LOCAL24PC
903 || r_type == elfcpp::R_POWERPC_REL14
904 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
905 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
906 || r_type == elfcpp::R_POWERPC_ADDR24
907 || r_type == elfcpp::R_POWERPC_ADDR14
908 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
909 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
912 // If INSN is an opcode that may be used with an @tls operand, return
913 // the transformed insn for TLS optimisation, otherwise return 0. If
914 // REG is non-zero only match an insn with RB or RA equal to REG.
916 at_tls_transform(uint32_t insn, unsigned int reg)
918 if ((insn & (0x3f << 26)) != 31 << 26)
922 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
923 rtra = insn & ((1 << 26) - (1 << 16));
924 else if (((insn >> 16) & 0x1f) == reg)
925 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
929 if ((insn & (0x3ff << 1)) == 266 << 1)
932 else if ((insn & (0x1f << 1)) == 23 << 1
933 && ((insn & (0x1f << 6)) < 14 << 6
934 || ((insn & (0x1f << 6)) >= 16 << 6
935 && (insn & (0x1f << 6)) < 24 << 6)))
936 // load and store indexed -> dform
937 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
938 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
939 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
940 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
941 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
943 insn = (58 << 26) | 2;
950 // Modified version of symtab.h class Symbol member
951 // Given a direct absolute or pc-relative static relocation against
952 // the global symbol, this function returns whether a dynamic relocation
957 needs_dynamic_reloc(const Symbol* gsym, int flags)
959 // No dynamic relocations in a static link!
960 if (parameters->doing_static_link())
963 // A reference to an undefined symbol from an executable should be
964 // statically resolved to 0, and does not need a dynamic relocation.
965 // This matches gnu ld behavior.
966 if (gsym->is_undefined() && !parameters->options().shared())
969 // A reference to an absolute symbol does not need a dynamic relocation.
970 if (gsym->is_absolute())
973 // An absolute reference within a position-independent output file
974 // will need a dynamic relocation.
975 if ((flags & Symbol::ABSOLUTE_REF)
976 && parameters->options().output_is_position_independent())
979 // A function call that can branch to a local PLT entry does not need
980 // a dynamic relocation.
981 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
984 // A reference to any PLT entry in a non-position-independent executable
985 // does not need a dynamic relocation.
986 // Except due to having function descriptors on powerpc64 we don't define
987 // functions to their plt code in an executable, so this doesn't apply.
989 && !parameters->options().output_is_position_independent()
990 && gsym->has_plt_offset())
993 // A reference to a symbol defined in a dynamic object or to a
994 // symbol that is preemptible will need a dynamic relocation.
995 if (gsym->is_from_dynobj()
996 || gsym->is_undefined()
997 || gsym->is_preemptible())
1000 // For all other cases, return FALSE.
1004 // Modified version of symtab.h class Symbol member
1005 // Whether we should use the PLT offset associated with a symbol for
1006 // a relocation. FLAGS is a set of Reference_flags.
1010 use_plt_offset(const Symbol* gsym, int flags)
1012 // If the symbol doesn't have a PLT offset, then naturally we
1013 // don't want to use it.
1014 if (!gsym->has_plt_offset())
1017 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
1018 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1021 // If we are going to generate a dynamic relocation, then we will
1022 // wind up using that, so no need to use the PLT entry.
1023 if (needs_dynamic_reloc<size>(gsym, flags))
1026 // If the symbol is from a dynamic object, we need to use the PLT
1028 if (gsym->is_from_dynobj())
1031 // If we are generating a shared object, and gsym symbol is
1032 // undefined or preemptible, we need to use the PLT entry.
1033 if (parameters->options().shared()
1034 && (gsym->is_undefined() || gsym->is_preemptible()))
1037 // If gsym is a call to a weak undefined symbol, we need to use
1038 // the PLT entry; the symbol may be defined by a library loaded
1040 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
1043 // Otherwise we can use the regular definition.
1047 template<int size, bool big_endian>
1048 class Powerpc_relocate_functions
1065 typedef Powerpc_relocate_functions<size, big_endian> This;
1066 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1068 template<int valsize>
1070 has_overflow_signed(Address value)
1072 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1073 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1074 limit <<= ((valsize - 1) >> 1);
1075 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1076 return value + limit > (limit << 1) - 1;
1079 template<int valsize>
1081 has_overflow_bitfield(Address value)
1083 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1084 limit <<= ((valsize - 1) >> 1);
1085 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1086 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
1089 template<int valsize>
1090 static inline Status
1091 overflowed(Address value, Overflow_check overflow)
1093 if (overflow == CHECK_SIGNED)
1095 if (has_overflow_signed<valsize>(value))
1096 return STATUS_OVERFLOW;
1098 else if (overflow == CHECK_BITFIELD)
1100 if (has_overflow_bitfield<valsize>(value))
1101 return STATUS_OVERFLOW;
1106 // Do a simple RELA relocation
1107 template<int valsize>
1108 static inline Status
1109 rela(unsigned char* view, Address value, Overflow_check overflow)
1111 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1112 Valtype* wv = reinterpret_cast<Valtype*>(view);
1113 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
1114 return overflowed<valsize>(value, overflow);
1117 template<int valsize>
1118 static inline Status
1119 rela(unsigned char* view,
1120 unsigned int right_shift,
1121 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1123 Overflow_check overflow)
1125 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1126 Valtype* wv = reinterpret_cast<Valtype*>(view);
1127 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1128 Valtype reloc = value >> right_shift;
1131 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
1132 return overflowed<valsize>(value >> right_shift, overflow);
1135 // Do a simple RELA relocation, unaligned.
1136 template<int valsize>
1137 static inline Status
1138 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1140 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
1141 return overflowed<valsize>(value, overflow);
1144 template<int valsize>
1145 static inline Status
1146 rela_ua(unsigned char* view,
1147 unsigned int right_shift,
1148 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1150 Overflow_check overflow)
1152 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1154 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
1155 Valtype reloc = value >> right_shift;
1158 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
1159 return overflowed<valsize>(value >> right_shift, overflow);
1163 // R_PPC64_ADDR64: (Symbol + Addend)
1165 addr64(unsigned char* view, Address value)
1166 { This::template rela<64>(view, value, CHECK_NONE); }
1168 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1170 addr64_u(unsigned char* view, Address value)
1171 { This::template rela_ua<64>(view, value, CHECK_NONE); }
1173 // R_POWERPC_ADDR32: (Symbol + Addend)
1174 static inline Status
1175 addr32(unsigned char* view, Address value, Overflow_check overflow)
1176 { return This::template rela<32>(view, value, overflow); }
1178 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1179 static inline Status
1180 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1181 { return This::template rela_ua<32>(view, value, overflow); }
1183 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1184 static inline Status
1185 addr24(unsigned char* view, Address value, Overflow_check overflow)
1187 Status stat = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
1188 if (overflow != CHECK_NONE && (value & 3) != 0)
1189 stat = STATUS_OVERFLOW;
1193 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1194 static inline Status
1195 addr16(unsigned char* view, Address value, Overflow_check overflow)
1196 { return This::template rela<16>(view, value, overflow); }
1198 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1199 static inline Status
1200 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1201 { return This::template rela_ua<16>(view, value, overflow); }
1203 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1204 static inline Status
1205 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1207 Status stat = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1208 if (overflow != CHECK_NONE && (value & 3) != 0)
1209 stat = STATUS_OVERFLOW;
1213 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1215 addr16_hi(unsigned char* view, Address value)
1216 { This::template rela<16>(view, 16, 0xffff, value, CHECK_NONE); }
1218 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1220 addr16_ha(unsigned char* view, Address value)
1221 { This::addr16_hi(view, value + 0x8000); }
1223 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1225 addr16_hi2(unsigned char* view, Address value)
1226 { This::template rela<16>(view, 32, 0xffff, value, CHECK_NONE); }
1228 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1230 addr16_ha2(unsigned char* view, Address value)
1231 { This::addr16_hi2(view, value + 0x8000); }
1233 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1235 addr16_hi3(unsigned char* view, Address value)
1236 { This::template rela<16>(view, 48, 0xffff, value, CHECK_NONE); }
1238 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1240 addr16_ha3(unsigned char* view, Address value)
1241 { This::addr16_hi3(view, value + 0x8000); }
1243 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1244 static inline Status
1245 addr14(unsigned char* view, Address value, Overflow_check overflow)
1247 Status stat = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1248 if (overflow != CHECK_NONE && (value & 3) != 0)
1249 stat = STATUS_OVERFLOW;
1254 // Stash away the index of .got2 or .opd in a relocatable object, if
1255 // such a section exists.
1257 template<int size, bool big_endian>
1259 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1260 Read_symbols_data* sd)
1262 const unsigned char* const pshdrs = sd->section_headers->data();
1263 const unsigned char* namesu = sd->section_names->data();
1264 const char* names = reinterpret_cast<const char*>(namesu);
1265 section_size_type names_size = sd->section_names_size;
1266 const unsigned char* s;
1268 s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1269 names, names_size, NULL);
1272 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1273 this->special_ = ndx;
1275 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1278 // Examine .rela.opd to build info about function entry points.
1280 template<int size, bool big_endian>
1282 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1284 const unsigned char* prelocs,
1285 const unsigned char* plocal_syms)
1289 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1291 const int reloc_size
1292 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1293 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1294 Address expected_off = 0;
1295 bool regular = true;
1296 unsigned int opd_ent_size = 0;
1298 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1300 Reltype reloc(prelocs);
1301 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1302 = reloc.get_r_info();
1303 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1304 if (r_type == elfcpp::R_PPC64_ADDR64)
1306 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1307 typename elfcpp::Elf_types<size>::Elf_Addr value;
1310 if (r_sym < this->local_symbol_count())
1312 typename elfcpp::Sym<size, big_endian>
1313 lsym(plocal_syms + r_sym * sym_size);
1314 shndx = lsym.get_st_shndx();
1315 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1316 value = lsym.get_st_value();
1319 shndx = this->symbol_section_and_value(r_sym, &value,
1321 this->set_opd_ent(reloc.get_r_offset(), shndx,
1322 value + reloc.get_r_addend());
1325 expected_off = reloc.get_r_offset();
1326 opd_ent_size = expected_off;
1328 else if (expected_off != reloc.get_r_offset())
1330 expected_off += opd_ent_size;
1332 else if (r_type == elfcpp::R_PPC64_TOC)
1334 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
1339 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1340 this->name().c_str(), r_type);
1344 if (reloc_count <= 2)
1345 opd_ent_size = this->section_size(this->opd_shndx());
1346 if (opd_ent_size != 24 && opd_ent_size != 16)
1350 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1351 this->name().c_str());
1357 template<int size, bool big_endian>
1359 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1361 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1364 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1365 p != rd->relocs.end();
1368 if (p->data_shndx == this->opd_shndx())
1370 uint64_t opd_size = this->section_size(this->opd_shndx());
1371 gold_assert(opd_size == static_cast<size_t>(opd_size));
1374 this->init_opd(opd_size);
1375 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1376 rd->local_symbols->data());
1384 // Set up some symbols, then perform Sized_relobj_file method.
1386 template<int size, bool big_endian>
1388 Powerpc_relobj<size, big_endian>::do_scan_relocs(Symbol_table* symtab,
1390 Read_relocs_data* rd)
1394 // Define a weak hidden _GLOBAL_OFFSET_TABLE_ to ensure it isn't
1395 // seen as undefined when scanning relocs (and thus requires
1396 // non-relative dynamic relocs). The proper value will be
1398 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
1399 if (gotsym != NULL && gotsym->is_undefined())
1401 Target_powerpc<size, big_endian>* target =
1402 static_cast<Target_powerpc<size, big_endian>*>(
1403 parameters->sized_target<size, big_endian>());
1404 Output_data_got_powerpc<size, big_endian>* got
1405 = target->got_section(symtab, layout);
1406 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1407 Symbol_table::PREDEFINED,
1411 elfcpp::STV_HIDDEN, 0,
1415 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
1416 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
1417 if (sdasym != NULL && sdasym->is_undefined())
1419 Output_data_space* sdata = new Output_data_space(4, "** sdata");
1421 = layout->add_output_section_data(".sdata", 0,
1423 | elfcpp::SHF_WRITE,
1424 sdata, ORDER_SMALL_DATA, false);
1425 symtab->define_in_output_data("_SDA_BASE_", NULL,
1426 Symbol_table::PREDEFINED,
1427 os, 32768, 0, elfcpp::STT_OBJECT,
1428 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
1432 Sized_relobj_file<size, big_endian>::do_scan_relocs(symtab, layout, rd);
1435 // Set up PowerPC target specific relobj.
1437 template<int size, bool big_endian>
1439 Target_powerpc<size, big_endian>::do_make_elf_object(
1440 const std::string& name,
1441 Input_file* input_file,
1442 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1444 int et = ehdr.get_e_type();
1445 // ET_EXEC files are valid input for --just-symbols/-R,
1446 // and we treat them as relocatable objects.
1447 if (et == elfcpp::ET_REL
1448 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1450 Powerpc_relobj<size, big_endian>* obj =
1451 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1455 else if (et == elfcpp::ET_DYN)
1457 Sized_dynobj<size, big_endian>* obj =
1458 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1464 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1469 template<int size, bool big_endian>
1470 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1473 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1474 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1476 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1477 : Output_data_got<size, big_endian>(),
1478 symtab_(symtab), layout_(layout),
1479 header_ent_cnt_(size == 32 ? 3 : 1),
1480 header_index_(size == 32 ? 0x2000 : 0)
1485 // Create a new GOT entry and return its offset.
1487 add_got_entry(Got_entry got_entry)
1489 this->reserve_ent();
1490 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1493 // Create a pair of new GOT entries and return the offset of the first.
1495 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1497 this->reserve_ent(2);
1498 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1503 add_constant_pair(Valtype c1, Valtype c2)
1505 this->reserve_ent(2);
1506 unsigned int got_offset = this->add_constant(c1);
1507 this->add_constant(c2);
1511 // Offset of _GLOBAL_OFFSET_TABLE_.
1515 return this->got_offset(this->header_index_);
1518 // Offset of base used to access the GOT/TOC.
1519 // The got/toc pointer reg will be set to this value.
1521 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1524 return this->g_o_t();
1526 return (this->output_section()->address()
1527 + object->toc_base_offset()
1531 // Ensure our GOT has a header.
1533 set_final_data_size()
1535 if (this->header_ent_cnt_ != 0)
1536 this->make_header();
1537 Output_data_got<size, big_endian>::set_final_data_size();
1540 // First word of GOT header needs some values that are not
1541 // handled by Output_data_got so poke them in here.
1542 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1544 do_write(Output_file* of)
1547 if (size == 32 && this->layout_->dynamic_data() != NULL)
1548 val = this->layout_->dynamic_section()->address();
1550 val = this->output_section()->address() + 0x8000;
1551 this->replace_constant(this->header_index_, val);
1552 Output_data_got<size, big_endian>::do_write(of);
1557 reserve_ent(unsigned int cnt = 1)
1559 if (this->header_ent_cnt_ == 0)
1561 if (this->num_entries() + cnt > this->header_index_)
1562 this->make_header();
1568 this->header_ent_cnt_ = 0;
1569 this->header_index_ = this->num_entries();
1572 Output_data_got<size, big_endian>::add_constant(0);
1573 Output_data_got<size, big_endian>::add_constant(0);
1574 Output_data_got<size, big_endian>::add_constant(0);
1576 // Define _GLOBAL_OFFSET_TABLE_ at the header
1577 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1578 Symbol_table::PREDEFINED,
1579 this, this->g_o_t(), 0,
1586 Output_data_got<size, big_endian>::add_constant(0);
1589 // Stashed pointers.
1590 Symbol_table* symtab_;
1594 unsigned int header_ent_cnt_;
1595 // GOT header index.
1596 unsigned int header_index_;
1599 // Get the GOT section, creating it if necessary.
1601 template<int size, bool big_endian>
1602 Output_data_got_powerpc<size, big_endian>*
1603 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1606 if (this->got_ == NULL)
1608 gold_assert(symtab != NULL && layout != NULL);
1611 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
1613 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1614 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1615 this->got_, ORDER_DATA, false);
1621 // Get the dynamic reloc section, creating it if necessary.
1623 template<int size, bool big_endian>
1624 typename Target_powerpc<size, big_endian>::Reloc_section*
1625 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1627 if (this->rela_dyn_ == NULL)
1629 gold_assert(layout != NULL);
1630 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1631 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1632 elfcpp::SHF_ALLOC, this->rela_dyn_,
1633 ORDER_DYNAMIC_RELOCS, false);
1635 return this->rela_dyn_;
1638 // A class to handle the PLT data.
1640 template<int size, bool big_endian>
1641 class Output_data_plt_powerpc : public Output_section_data_build
1644 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1645 size, big_endian> Reloc_section;
1647 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
1648 Reloc_section* plt_rel,
1649 unsigned int reserved_size,
1651 : Output_section_data_build(size == 32 ? 4 : 8),
1654 initial_plt_entry_size_(reserved_size),
1658 // Add an entry to the PLT.
1663 add_ifunc_entry(Symbol*);
1666 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
1668 // Return the .rela.plt section data.
1675 // Return the number of PLT entries.
1679 return ((this->current_data_size() - this->initial_plt_entry_size_)
1683 // Return the offset of the first non-reserved PLT entry.
1685 first_plt_entry_offset()
1686 { return this->initial_plt_entry_size_; }
1688 // Return the size of a PLT entry.
1690 get_plt_entry_size()
1691 { return plt_entry_size; }
1695 do_adjust_output_section(Output_section* os)
1700 // Write to a map file.
1702 do_print_to_mapfile(Mapfile* mapfile) const
1703 { mapfile->print_output_data(this, this->name_); }
1706 // The size of an entry in the PLT.
1707 static const int plt_entry_size = size == 32 ? 4 : 24;
1709 // Write out the PLT data.
1711 do_write(Output_file*);
1713 // The reloc section.
1714 Reloc_section* rel_;
1715 // Allows access to .glink for do_write.
1716 Target_powerpc<size, big_endian>* targ_;
1717 // The size of the first reserved entry.
1718 int initial_plt_entry_size_;
1719 // What to report in map file.
1723 // Add an entry to the PLT.
1725 template<int size, bool big_endian>
1727 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1729 if (!gsym->has_plt_offset())
1731 off_t off = this->current_data_size();
1733 off += this->first_plt_entry_offset();
1734 gsym->set_plt_offset(off);
1735 gsym->set_needs_dynsym_entry();
1736 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
1737 this->rel_->add_global(gsym, dynrel, this, off, 0);
1738 off += plt_entry_size;
1739 this->set_current_data_size(off);
1743 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
1745 template<int size, bool big_endian>
1747 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
1749 if (!gsym->has_plt_offset())
1751 off_t off = this->current_data_size();
1752 gsym->set_plt_offset(off);
1753 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
1755 dynrel = elfcpp::R_PPC64_JMP_IREL;
1756 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
1757 off += plt_entry_size;
1758 this->set_current_data_size(off);
1762 // Add an entry for a local ifunc symbol to the IPLT.
1764 template<int size, bool big_endian>
1766 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
1767 Sized_relobj_file<size, big_endian>* relobj,
1768 unsigned int local_sym_index)
1770 if (!relobj->local_has_plt_offset(local_sym_index))
1772 off_t off = this->current_data_size();
1773 relobj->set_local_plt_offset(local_sym_index, off);
1774 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
1776 dynrel = elfcpp::R_PPC64_JMP_IREL;
1777 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
1779 off += plt_entry_size;
1780 this->set_current_data_size(off);
1784 static const uint32_t add_0_11_11 = 0x7c0b5a14;
1785 static const uint32_t add_3_3_2 = 0x7c631214;
1786 static const uint32_t add_3_3_13 = 0x7c636a14;
1787 static const uint32_t add_11_0_11 = 0x7d605a14;
1788 static const uint32_t add_12_2_11 = 0x7d825a14;
1789 static const uint32_t addi_11_11 = 0x396b0000;
1790 static const uint32_t addi_12_12 = 0x398c0000;
1791 static const uint32_t addi_2_2 = 0x38420000;
1792 static const uint32_t addi_3_2 = 0x38620000;
1793 static const uint32_t addi_3_3 = 0x38630000;
1794 static const uint32_t addis_0_2 = 0x3c020000;
1795 static const uint32_t addis_0_13 = 0x3c0d0000;
1796 static const uint32_t addis_11_11 = 0x3d6b0000;
1797 static const uint32_t addis_11_30 = 0x3d7e0000;
1798 static const uint32_t addis_12_12 = 0x3d8c0000;
1799 static const uint32_t addis_12_2 = 0x3d820000;
1800 static const uint32_t addis_3_2 = 0x3c620000;
1801 static const uint32_t addis_3_13 = 0x3c6d0000;
1802 static const uint32_t b = 0x48000000;
1803 static const uint32_t bcl_20_31 = 0x429f0005;
1804 static const uint32_t bctr = 0x4e800420;
1805 static const uint32_t blr = 0x4e800020;
1806 static const uint32_t blrl = 0x4e800021;
1807 static const uint32_t cror_15_15_15 = 0x4def7b82;
1808 static const uint32_t cror_31_31_31 = 0x4ffffb82;
1809 static const uint32_t ld_0_1 = 0xe8010000;
1810 static const uint32_t ld_0_12 = 0xe80c0000;
1811 static const uint32_t ld_11_12 = 0xe96c0000;
1812 static const uint32_t ld_11_2 = 0xe9620000;
1813 static const uint32_t ld_2_1 = 0xe8410000;
1814 static const uint32_t ld_2_11 = 0xe84b0000;
1815 static const uint32_t ld_2_12 = 0xe84c0000;
1816 static const uint32_t ld_2_2 = 0xe8420000;
1817 static const uint32_t lfd_0_1 = 0xc8010000;
1818 static const uint32_t li_0_0 = 0x38000000;
1819 static const uint32_t li_12_0 = 0x39800000;
1820 static const uint32_t lis_0_0 = 0x3c000000;
1821 static const uint32_t lis_11 = 0x3d600000;
1822 static const uint32_t lis_12 = 0x3d800000;
1823 static const uint32_t lwz_0_12 = 0x800c0000;
1824 static const uint32_t lwz_11_11 = 0x816b0000;
1825 static const uint32_t lwz_11_30 = 0x817e0000;
1826 static const uint32_t lwz_12_12 = 0x818c0000;
1827 static const uint32_t lwzu_0_12 = 0x840c0000;
1828 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
1829 static const uint32_t mflr_0 = 0x7c0802a6;
1830 static const uint32_t mflr_11 = 0x7d6802a6;
1831 static const uint32_t mflr_12 = 0x7d8802a6;
1832 static const uint32_t mtctr_0 = 0x7c0903a6;
1833 static const uint32_t mtctr_11 = 0x7d6903a6;
1834 static const uint32_t mtlr_0 = 0x7c0803a6;
1835 static const uint32_t mtlr_12 = 0x7d8803a6;
1836 static const uint32_t nop = 0x60000000;
1837 static const uint32_t ori_0_0_0 = 0x60000000;
1838 static const uint32_t std_0_1 = 0xf8010000;
1839 static const uint32_t std_0_12 = 0xf80c0000;
1840 static const uint32_t std_2_1 = 0xf8410000;
1841 static const uint32_t stfd_0_1 = 0xd8010000;
1842 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
1843 static const uint32_t sub_11_11_12 = 0x7d6c5850;
1845 // Write out the PLT.
1847 template<int size, bool big_endian>
1849 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1853 const off_t offset = this->offset();
1854 const section_size_type oview_size
1855 = convert_to_section_size_type(this->data_size());
1856 unsigned char* const oview = of->get_output_view(offset, oview_size);
1857 unsigned char* pov = oview;
1858 unsigned char* endpov = oview + oview_size;
1860 // The address of the .glink branch table
1861 const Output_data_glink<size, big_endian>* glink
1862 = this->targ_->glink_section();
1863 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1864 = glink->address() + glink->pltresolve();
1866 while (pov < endpov)
1868 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1873 of->write_output_view(offset, oview_size, oview);
1877 // Create the PLT section.
1879 template<int size, bool big_endian>
1881 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1883 if (this->plt_ == NULL)
1885 if (this->glink_ == NULL)
1886 make_glink_section(layout);
1888 // Ensure that .rela.dyn always appears before .rela.plt This is
1889 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1890 // needs to include .rela.plt in it's range.
1891 this->rela_dyn_section(layout);
1893 Reloc_section* plt_rel = new Reloc_section(false);
1894 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1895 elfcpp::SHF_ALLOC, plt_rel,
1896 ORDER_DYNAMIC_PLT_RELOCS, false);
1898 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
1899 size == 32 ? 0 : 24,
1901 layout->add_output_section_data(".plt",
1903 ? elfcpp::SHT_PROGBITS
1904 : elfcpp::SHT_NOBITS),
1905 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1914 // Create the IPLT section.
1916 template<int size, bool big_endian>
1918 Target_powerpc<size, big_endian>::make_iplt_section(Layout* layout)
1920 if (this->iplt_ == NULL)
1922 this->make_plt_section(layout);
1924 Reloc_section* iplt_rel = new Reloc_section(false);
1925 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
1927 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
1929 this->plt_->output_section()->add_output_section_data(this->iplt_);
1933 // A class to handle .glink.
1935 template<int size, bool big_endian>
1936 class Output_data_glink : public Output_section_data
1939 static const int pltresolve_size = 16*4;
1941 Output_data_glink(Target_powerpc<size, big_endian>*);
1945 add_entry(const Sized_relobj_file<size, big_endian>*,
1947 const elfcpp::Rela<size, big_endian>&);
1950 add_entry(const Sized_relobj_file<size, big_endian>*,
1952 const elfcpp::Rela<size, big_endian>&);
1955 find_entry(const Symbol*) const;
1958 find_entry(const Sized_relobj_file<size, big_endian>*, unsigned int) const;
1961 find_entry(const Sized_relobj_file<size, big_endian>*,
1963 const elfcpp::Rela<size, big_endian>&) const;
1966 find_entry(const Sized_relobj_file<size, big_endian>*,
1968 const elfcpp::Rela<size, big_endian>&) const;
1971 glink_entry_size() const
1976 // FIXME: We should be using multiple glink sections for
1977 // stubs to support > 33M applications.
1984 return this->pltresolve_;
1988 // Write to a map file.
1990 do_print_to_mapfile(Mapfile* mapfile) const
1991 { mapfile->print_output_data(this, _("** glink")); }
1995 set_final_data_size();
1999 do_write(Output_file*);
2004 Glink_sym_ent(const Symbol* sym)
2005 : sym_(sym), object_(0), addend_(0), locsym_(0)
2008 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
2009 unsigned int locsym_index)
2010 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
2013 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
2015 const elfcpp::Rela<size, big_endian>& reloc)
2016 : sym_(sym), object_(0), addend_(0), locsym_(0)
2019 this->addend_ = reloc.get_r_addend();
2020 else if (parameters->options().output_is_position_independent()
2021 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
2022 == elfcpp::R_PPC_PLTREL24))
2024 this->addend_ = reloc.get_r_addend();
2025 if (this->addend_ >= 32768)
2026 this->object_ = object;
2030 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
2031 unsigned int locsym_index,
2032 const elfcpp::Rela<size, big_endian>& reloc)
2033 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
2036 this->addend_ = reloc.get_r_addend();
2037 else if (parameters->options().output_is_position_independent()
2038 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
2039 == elfcpp::R_PPC_PLTREL24))
2040 this->addend_ = reloc.get_r_addend();
2043 bool operator==(const Glink_sym_ent& that) const
2045 return (this->sym_ == that.sym_
2046 && this->object_ == that.object_
2047 && this->addend_ == that.addend_
2048 && this->locsym_ == that.locsym_);
2052 const Sized_relobj_file<size, big_endian>* object_;
2053 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
2054 unsigned int locsym_;
2057 class Glink_sym_ent_hash
2060 size_t operator()(const Glink_sym_ent& ent) const
2062 return (reinterpret_cast<uintptr_t>(ent.sym_)
2063 ^ reinterpret_cast<uintptr_t>(ent.object_)
2069 // Map sym/object/addend to index.
2070 typedef Unordered_map<Glink_sym_ent, unsigned int,
2071 Glink_sym_ent_hash> Glink_entries;
2072 Glink_entries glink_entries_;
2074 // Offset of pltresolve stub (actually, branch table for 32-bit)
2077 // Allows access to .got and .plt for do_write.
2078 Target_powerpc<size, big_endian>* targ_;
2081 // Create the glink section.
2083 template<int size, bool big_endian>
2084 Output_data_glink<size, big_endian>::Output_data_glink(
2085 Target_powerpc<size, big_endian>* targ)
2086 : Output_section_data(16),
2087 pltresolve_(0), targ_(targ)
2091 // Add an entry to glink, if we do not already have one for this
2092 // sym/object/addend combo.
2094 template<int size, bool big_endian>
2096 Output_data_glink<size, big_endian>::add_entry(
2097 const Sized_relobj_file<size, big_endian>* object,
2099 const elfcpp::Rela<size, big_endian>& reloc)
2101 Glink_sym_ent ent(object, gsym, reloc);
2102 unsigned int indx = this->glink_entries_.size();
2103 this->glink_entries_.insert(std::make_pair(ent, indx));
2106 template<int size, bool big_endian>
2108 Output_data_glink<size, big_endian>::add_entry(
2109 const Sized_relobj_file<size, big_endian>* object,
2110 unsigned int locsym_index,
2111 const elfcpp::Rela<size, big_endian>& reloc)
2113 Glink_sym_ent ent(object, locsym_index, reloc);
2114 unsigned int indx = this->glink_entries_.size();
2115 this->glink_entries_.insert(std::make_pair(ent, indx));
2118 template<int size, bool big_endian>
2120 Output_data_glink<size, big_endian>::find_entry(
2121 const Sized_relobj_file<size, big_endian>* object,
2123 const elfcpp::Rela<size, big_endian>& reloc) const
2125 Glink_sym_ent ent(object, gsym, reloc);
2126 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2127 gold_assert(p != this->glink_entries_.end());
2131 template<int size, bool big_endian>
2133 Output_data_glink<size, big_endian>::find_entry(const Symbol* gsym) const
2135 Glink_sym_ent ent(gsym);
2136 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2137 gold_assert(p != this->glink_entries_.end());
2141 template<int size, bool big_endian>
2143 Output_data_glink<size, big_endian>::find_entry(
2144 const Sized_relobj_file<size, big_endian>* object,
2145 unsigned int locsym_index,
2146 const elfcpp::Rela<size, big_endian>& reloc) const
2148 Glink_sym_ent ent(object, locsym_index, reloc);
2149 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2150 gold_assert(p != this->glink_entries_.end());
2154 template<int size, bool big_endian>
2156 Output_data_glink<size, big_endian>::find_entry(
2157 const Sized_relobj_file<size, big_endian>* object,
2158 unsigned int locsym_index) const
2160 Glink_sym_ent ent(object, locsym_index);
2161 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2162 gold_assert(p != this->glink_entries_.end());
2166 template<int size, bool big_endian>
2168 Output_data_glink<size, big_endian>::set_final_data_size()
2170 unsigned int count = this->glink_entries_.size();
2171 off_t total = count;
2178 this->pltresolve_ = total;
2180 // space for branch table
2181 total += 4 * (count - 1);
2183 total += -total & 15;
2184 total += this->pltresolve_size;
2189 this->pltresolve_ = total;
2190 total += this->pltresolve_size;
2192 // space for branch table
2195 total += 4 * (count - 0x8000);
2199 this->set_data_size(total);
2202 static inline uint32_t
2208 static inline uint32_t
2214 static inline uint32_t
2217 return hi(a + 0x8000);
2220 template<bool big_endian>
2222 write_insn(unsigned char* p, uint32_t v)
2224 elfcpp::Swap<32, big_endian>::writeval(p, v);
2227 // Write out .glink.
2229 template<int size, bool big_endian>
2231 Output_data_glink<size, big_endian>::do_write(Output_file* of)
2233 const off_t off = this->offset();
2234 const section_size_type oview_size =
2235 convert_to_section_size_type(this->data_size());
2236 unsigned char* const oview = of->get_output_view(off, oview_size);
2239 // The base address of the .plt section.
2240 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2241 static const Address invalid_address = static_cast<Address>(0) - 1;
2242 Address plt_base = this->targ_->plt_section()->address();
2243 Address iplt_base = invalid_address;
2245 const Output_data_got_powerpc<size, big_endian>* got
2246 = this->targ_->got_section();
2250 Address got_os_addr = got->output_section()->address();
2252 // Write out call stubs.
2253 typename Glink_entries::const_iterator g;
2254 for (g = this->glink_entries_.begin();
2255 g != this->glink_entries_.end();
2260 const Symbol* gsym = g->first.sym_;
2263 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
2264 && gsym->can_use_relative_reloc(false));
2265 plt_addr = gsym->plt_offset();
2270 const Sized_relobj_file<size, big_endian>* relobj
2272 unsigned int local_sym_index = g->first.locsym_;
2273 plt_addr = relobj->local_plt_offset(local_sym_index);
2277 if (iplt_base == invalid_address)
2278 iplt_base = this->targ_->iplt_section()->address();
2279 plt_addr += iplt_base;
2282 plt_addr += plt_base;
2283 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2284 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
2285 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
2286 Address pltoff = plt_addr - got_addr;
2288 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
2289 gold_error(_("%s: linkage table error against `%s'"),
2290 g->first.object_->name().c_str(),
2291 g->first.sym_->demangled_name().c_str());
2293 p = oview + g->second * this->glink_entry_size();
2294 if (ha(pltoff) != 0)
2296 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
2297 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
2298 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
2299 if (ha(pltoff + 16) != ha(pltoff))
2301 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
2304 write_insn<big_endian>(p, mtctr_11), p += 4;
2305 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
2306 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
2307 write_insn<big_endian>(p, bctr), p += 4;
2311 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
2312 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
2313 if (ha(pltoff + 16) != ha(pltoff))
2315 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
2318 write_insn<big_endian>(p, mtctr_11), p += 4;
2319 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
2320 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
2321 write_insn<big_endian>(p, bctr), p += 4;
2325 // Write pltresolve stub.
2326 p = oview + this->pltresolve_;
2327 Address after_bcl = this->address() + this->pltresolve_ + 16;
2328 Address pltoff = plt_base - after_bcl;
2330 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
2332 write_insn<big_endian>(p, mflr_12), p += 4;
2333 write_insn<big_endian>(p, bcl_20_31), p += 4;
2334 write_insn<big_endian>(p, mflr_11), p += 4;
2335 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
2336 write_insn<big_endian>(p, mtlr_12), p += 4;
2337 write_insn<big_endian>(p, add_12_2_11), p += 4;
2338 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
2339 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
2340 write_insn<big_endian>(p, mtctr_11), p += 4;
2341 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
2342 write_insn<big_endian>(p, bctr), p += 4;
2343 while (p < oview + this->pltresolve_ + this->pltresolve_size)
2344 write_insn<big_endian>(p, nop), p += 4;
2346 // Write lazy link call stubs.
2348 while (p < oview + oview_size)
2352 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
2356 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
2357 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
2359 uint32_t branch_off = this->pltresolve_ + 8 - (p - oview);
2360 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
2366 // The address of _GLOBAL_OFFSET_TABLE_.
2367 Address g_o_t = got->address() + got->g_o_t();
2369 // Write out call stubs.
2370 typename Glink_entries::const_iterator g;
2371 for (g = this->glink_entries_.begin();
2372 g != this->glink_entries_.end();
2377 const Symbol* gsym = g->first.sym_;
2380 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
2381 && gsym->can_use_relative_reloc(false));
2382 plt_addr = gsym->plt_offset();
2387 const Sized_relobj_file<size, big_endian>* relobj
2389 unsigned int local_sym_index = g->first.locsym_;
2390 plt_addr = relobj->local_plt_offset(local_sym_index);
2394 if (iplt_base == invalid_address)
2395 iplt_base = this->targ_->iplt_section()->address();
2396 plt_addr += iplt_base;
2399 plt_addr += plt_base;
2401 p = oview + g->second * this->glink_entry_size();
2402 if (parameters->options().output_is_position_independent())
2405 const Powerpc_relobj<size, big_endian>* object = static_cast
2406 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
2407 if (object != NULL && g->first.addend_ >= 32768)
2409 unsigned int got2 = object->got2_shndx();
2410 got_addr = g->first.object_->get_output_section_offset(got2);
2411 gold_assert(got_addr != invalid_address);
2412 got_addr += (g->first.object_->output_section(got2)->address()
2413 + g->first.addend_);
2418 Address pltoff = plt_addr - got_addr;
2419 if (ha(pltoff) == 0)
2421 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
2422 write_insn<big_endian>(p + 4, mtctr_11);
2423 write_insn<big_endian>(p + 8, bctr);
2427 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
2428 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
2429 write_insn<big_endian>(p + 8, mtctr_11);
2430 write_insn<big_endian>(p + 12, bctr);
2435 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
2436 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
2437 write_insn<big_endian>(p + 8, mtctr_11);
2438 write_insn<big_endian>(p + 12, bctr);
2442 // Write out pltresolve branch table.
2443 p = oview + this->pltresolve_;
2444 unsigned int the_end = oview_size - this->pltresolve_size;
2445 unsigned char* end_p = oview + the_end;
2446 while (p < end_p - 8 * 4)
2447 write_insn<big_endian>(p, b + end_p - p), p += 4;
2449 write_insn<big_endian>(p, nop), p += 4;
2451 // Write out pltresolve call stub.
2452 if (parameters->options().output_is_position_independent())
2454 Address res0_off = this->pltresolve_;
2455 Address after_bcl_off = the_end + 12;
2456 Address bcl_res0 = after_bcl_off - res0_off;
2458 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
2459 write_insn<big_endian>(p + 4, mflr_0);
2460 write_insn<big_endian>(p + 8, bcl_20_31);
2461 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
2462 write_insn<big_endian>(p + 16, mflr_12);
2463 write_insn<big_endian>(p + 20, mtlr_0);
2464 write_insn<big_endian>(p + 24, sub_11_11_12);
2466 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
2468 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
2469 if (ha(got_bcl) == ha(got_bcl + 4))
2471 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
2472 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
2476 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
2477 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
2479 write_insn<big_endian>(p + 40, mtctr_0);
2480 write_insn<big_endian>(p + 44, add_0_11_11);
2481 write_insn<big_endian>(p + 48, add_11_0_11);
2482 write_insn<big_endian>(p + 52, bctr);
2483 write_insn<big_endian>(p + 56, nop);
2484 write_insn<big_endian>(p + 60, nop);
2488 Address res0 = this->pltresolve_ + this->address();
2490 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
2491 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
2492 if (ha(g_o_t + 4) == ha(g_o_t + 8))
2493 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
2495 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
2496 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
2497 write_insn<big_endian>(p + 16, mtctr_0);
2498 write_insn<big_endian>(p + 20, add_0_11_11);
2499 if (ha(g_o_t + 4) == ha(g_o_t + 8))
2500 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
2502 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
2503 write_insn<big_endian>(p + 28, add_11_0_11);
2504 write_insn<big_endian>(p + 32, bctr);
2505 write_insn<big_endian>(p + 36, nop);
2506 write_insn<big_endian>(p + 40, nop);
2507 write_insn<big_endian>(p + 44, nop);
2508 write_insn<big_endian>(p + 48, nop);
2509 write_insn<big_endian>(p + 52, nop);
2510 write_insn<big_endian>(p + 56, nop);
2511 write_insn<big_endian>(p + 60, nop);
2516 of->write_output_view(off, oview_size, oview);
2520 // A class to handle linker generated save/restore functions.
2522 template<int size, bool big_endian>
2523 class Output_data_save_res : public Output_section_data_build
2526 Output_data_save_res(Symbol_table* symtab);
2529 // Write to a map file.
2531 do_print_to_mapfile(Mapfile* mapfile) const
2532 { mapfile->print_output_data(this, _("** save/restore")); }
2535 do_write(Output_file*);
2538 // The maximum size of save/restore contents.
2539 static const unsigned int savres_max = 218*4;
2542 savres_define(Symbol_table* symtab,
2544 unsigned int lo, unsigned int hi,
2545 unsigned char* write_ent(unsigned char*, int),
2546 unsigned char* write_tail(unsigned char*, int));
2548 unsigned char *contents_;
2551 template<bool big_endian>
2552 static unsigned char*
2553 savegpr0(unsigned char* p, int r)
2555 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2556 write_insn<big_endian>(p, insn);
2560 template<bool big_endian>
2561 static unsigned char*
2562 savegpr0_tail(unsigned char* p, int r)
2564 p = savegpr0<big_endian>(p, r);
2565 uint32_t insn = std_0_1 + 16;
2566 write_insn<big_endian>(p, insn);
2568 write_insn<big_endian>(p, blr);
2572 template<bool big_endian>
2573 static unsigned char*
2574 restgpr0(unsigned char* p, int r)
2576 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2577 write_insn<big_endian>(p, insn);
2581 template<bool big_endian>
2582 static unsigned char*
2583 restgpr0_tail(unsigned char* p, int r)
2585 uint32_t insn = ld_0_1 + 16;
2586 write_insn<big_endian>(p, insn);
2588 p = restgpr0<big_endian>(p, r);
2589 write_insn<big_endian>(p, mtlr_0);
2593 p = restgpr0<big_endian>(p, 30);
2594 p = restgpr0<big_endian>(p, 31);
2596 write_insn<big_endian>(p, blr);
2600 template<bool big_endian>
2601 static unsigned char*
2602 savegpr1(unsigned char* p, int r)
2604 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
2605 write_insn<big_endian>(p, insn);
2609 template<bool big_endian>
2610 static unsigned char*
2611 savegpr1_tail(unsigned char* p, int r)
2613 p = savegpr1<big_endian>(p, r);
2614 write_insn<big_endian>(p, blr);
2618 template<bool big_endian>
2619 static unsigned char*
2620 restgpr1(unsigned char* p, int r)
2622 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
2623 write_insn<big_endian>(p, insn);
2627 template<bool big_endian>
2628 static unsigned char*
2629 restgpr1_tail(unsigned char* p, int r)
2631 p = restgpr1<big_endian>(p, r);
2632 write_insn<big_endian>(p, blr);
2636 template<bool big_endian>
2637 static unsigned char*
2638 savefpr(unsigned char* p, int r)
2640 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2641 write_insn<big_endian>(p, insn);
2645 template<bool big_endian>
2646 static unsigned char*
2647 savefpr0_tail(unsigned char* p, int r)
2649 p = savefpr<big_endian>(p, r);
2650 write_insn<big_endian>(p, std_0_1 + 16);
2652 write_insn<big_endian>(p, blr);
2656 template<bool big_endian>
2657 static unsigned char*
2658 restfpr(unsigned char* p, int r)
2660 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2661 write_insn<big_endian>(p, insn);
2665 template<bool big_endian>
2666 static unsigned char*
2667 restfpr0_tail(unsigned char* p, int r)
2669 write_insn<big_endian>(p, ld_0_1 + 16);
2671 p = restfpr<big_endian>(p, r);
2672 write_insn<big_endian>(p, mtlr_0);
2676 p = restfpr<big_endian>(p, 30);
2677 p = restfpr<big_endian>(p, 31);
2679 write_insn<big_endian>(p, blr);
2683 template<bool big_endian>
2684 static unsigned char*
2685 savefpr1_tail(unsigned char* p, int r)
2687 p = savefpr<big_endian>(p, r);
2688 write_insn<big_endian>(p, blr);
2692 template<bool big_endian>
2693 static unsigned char*
2694 restfpr1_tail(unsigned char* p, int r)
2696 p = restfpr<big_endian>(p, r);
2697 write_insn<big_endian>(p, blr);
2701 template<bool big_endian>
2702 static unsigned char*
2703 savevr(unsigned char* p, int r)
2705 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
2706 write_insn<big_endian>(p, insn);
2708 insn = stvx_0_12_0 + (r << 21);
2709 write_insn<big_endian>(p, insn);
2713 template<bool big_endian>
2714 static unsigned char*
2715 savevr_tail(unsigned char* p, int r)
2717 p = savevr<big_endian>(p, r);
2718 write_insn<big_endian>(p, blr);
2722 template<bool big_endian>
2723 static unsigned char*
2724 restvr(unsigned char* p, int r)
2726 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
2727 write_insn<big_endian>(p, insn);
2729 insn = lvx_0_12_0 + (r << 21);
2730 write_insn<big_endian>(p, insn);
2734 template<bool big_endian>
2735 static unsigned char*
2736 restvr_tail(unsigned char* p, int r)
2738 p = restvr<big_endian>(p, r);
2739 write_insn<big_endian>(p, blr);
2744 template<int size, bool big_endian>
2745 Output_data_save_res<size, big_endian>::Output_data_save_res(
2746 Symbol_table* symtab)
2747 : Output_section_data_build(4),
2750 this->savres_define(symtab,
2751 "_savegpr0_", 14, 31,
2752 savegpr0<big_endian>, savegpr0_tail<big_endian>);
2753 this->savres_define(symtab,
2754 "_restgpr0_", 14, 29,
2755 restgpr0<big_endian>, restgpr0_tail<big_endian>);
2756 this->savres_define(symtab,
2757 "_restgpr0_", 30, 31,
2758 restgpr0<big_endian>, restgpr0_tail<big_endian>);
2759 this->savres_define(symtab,
2760 "_savegpr1_", 14, 31,
2761 savegpr1<big_endian>, savegpr1_tail<big_endian>);
2762 this->savres_define(symtab,
2763 "_restgpr1_", 14, 31,
2764 restgpr1<big_endian>, restgpr1_tail<big_endian>);
2765 this->savres_define(symtab,
2766 "_savefpr_", 14, 31,
2767 savefpr<big_endian>, savefpr0_tail<big_endian>);
2768 this->savres_define(symtab,
2769 "_restfpr_", 14, 29,
2770 restfpr<big_endian>, restfpr0_tail<big_endian>);
2771 this->savres_define(symtab,
2772 "_restfpr_", 30, 31,
2773 restfpr<big_endian>, restfpr0_tail<big_endian>);
2774 this->savres_define(symtab,
2776 savefpr<big_endian>, savefpr1_tail<big_endian>);
2777 this->savres_define(symtab,
2779 restfpr<big_endian>, restfpr1_tail<big_endian>);
2780 this->savres_define(symtab,
2782 savevr<big_endian>, savevr_tail<big_endian>);
2783 this->savres_define(symtab,
2785 restvr<big_endian>, restvr_tail<big_endian>);
2788 template<int size, bool big_endian>
2790 Output_data_save_res<size, big_endian>::savres_define(
2791 Symbol_table* symtab,
2793 unsigned int lo, unsigned int hi,
2794 unsigned char* write_ent(unsigned char*, int),
2795 unsigned char* write_tail(unsigned char*, int))
2797 size_t len = strlen(name);
2798 bool writing = false;
2801 memcpy(sym, name, len);
2804 for (unsigned int i = lo; i <= hi; i++)
2806 sym[len + 0] = i / 10 + '0';
2807 sym[len + 1] = i % 10 + '0';
2808 Symbol* gsym = symtab->lookup(sym);
2809 bool refd = gsym != NULL && gsym->is_undefined();
2810 writing = writing || refd;
2813 if (this->contents_ == NULL)
2814 this->contents_ = new unsigned char[this->savres_max];
2816 off_t value = this->current_data_size();
2817 unsigned char* p = this->contents_ + value;
2819 p = write_ent(p, i);
2821 p = write_tail(p, i);
2822 off_t cur_size = p - this->contents_;
2823 this->set_current_data_size(cur_size);
2825 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
2826 this, value, cur_size - value,
2827 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
2828 elfcpp::STV_HIDDEN, 0, false, false);
2833 // Write out save/restore.
2835 template<int size, bool big_endian>
2837 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
2839 const off_t off = this->offset();
2840 const section_size_type oview_size =
2841 convert_to_section_size_type(this->data_size());
2842 unsigned char* const oview = of->get_output_view(off, oview_size);
2843 memcpy(oview, this->contents_, oview_size);
2844 of->write_output_view(off, oview_size, oview);
2848 // Create the glink section.
2850 template<int size, bool big_endian>
2852 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
2854 if (this->glink_ == NULL)
2856 this->glink_ = new Output_data_glink<size, big_endian>(this);
2857 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2858 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2859 this->glink_, ORDER_TEXT, false);
2863 // Create a PLT entry for a global symbol.
2865 template<int size, bool big_endian>
2867 Target_powerpc<size, big_endian>::make_plt_entry(
2870 const elfcpp::Rela<size, big_endian>& reloc,
2871 const Sized_relobj_file<size, big_endian>* object)
2873 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2874 && gsym->can_use_relative_reloc(false))
2876 if (this->iplt_ == NULL)
2877 this->make_iplt_section(layout);
2878 this->iplt_->add_ifunc_entry(gsym);
2882 if (this->plt_ == NULL)
2883 this->make_plt_section(layout);
2884 this->plt_->add_entry(gsym);
2886 this->glink_->add_entry(object, gsym, reloc);
2889 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2891 template<int size, bool big_endian>
2893 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
2895 const elfcpp::Rela<size, big_endian>& reloc,
2896 Sized_relobj_file<size, big_endian>* relobj)
2898 if (this->iplt_ == NULL)
2899 this->make_iplt_section(layout);
2900 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2901 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
2902 this->glink_->add_entry(relobj, r_sym, reloc);
2905 // Return the number of entries in the PLT.
2907 template<int size, bool big_endian>
2909 Target_powerpc<size, big_endian>::plt_entry_count() const
2911 if (this->plt_ == NULL)
2913 unsigned int count = this->plt_->entry_count();
2914 if (this->iplt_ != NULL)
2915 count += this->iplt_->entry_count();
2919 // Return the offset of the first non-reserved PLT entry.
2921 template<int size, bool big_endian>
2923 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
2925 return this->plt_->first_plt_entry_offset();
2928 // Return the size of each PLT entry.
2930 template<int size, bool big_endian>
2932 Target_powerpc<size, big_endian>::plt_entry_size() const
2934 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
2937 // Create a GOT entry for local dynamic __tls_get_addr calls.
2939 template<int size, bool big_endian>
2941 Target_powerpc<size, big_endian>::tlsld_got_offset(
2942 Symbol_table* symtab,
2944 Sized_relobj_file<size, big_endian>* object)
2946 if (this->tlsld_got_offset_ == -1U)
2948 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2949 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2950 Output_data_got_powerpc<size, big_endian>* got
2951 = this->got_section(symtab, layout);
2952 unsigned int got_offset = got->add_constant_pair(0, 0);
2953 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
2955 this->tlsld_got_offset_ = got_offset;
2957 return this->tlsld_got_offset_;
2960 // Get the Reference_flags for a particular relocation.
2962 template<int size, bool big_endian>
2964 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
2968 case elfcpp::R_POWERPC_NONE:
2969 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2970 case elfcpp::R_POWERPC_GNU_VTENTRY:
2971 case elfcpp::R_PPC64_TOC:
2972 // No symbol reference.
2975 case elfcpp::R_PPC64_ADDR64:
2976 case elfcpp::R_PPC64_UADDR64:
2977 case elfcpp::R_POWERPC_ADDR32:
2978 case elfcpp::R_POWERPC_UADDR32:
2979 case elfcpp::R_POWERPC_ADDR16:
2980 case elfcpp::R_POWERPC_UADDR16:
2981 case elfcpp::R_POWERPC_ADDR16_LO:
2982 case elfcpp::R_POWERPC_ADDR16_HI:
2983 case elfcpp::R_POWERPC_ADDR16_HA:
2984 return Symbol::ABSOLUTE_REF;
2986 case elfcpp::R_POWERPC_ADDR24:
2987 case elfcpp::R_POWERPC_ADDR14:
2988 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2989 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2990 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2992 case elfcpp::R_PPC64_REL64:
2993 case elfcpp::R_POWERPC_REL32:
2994 case elfcpp::R_PPC_LOCAL24PC:
2995 case elfcpp::R_POWERPC_REL16:
2996 case elfcpp::R_POWERPC_REL16_LO:
2997 case elfcpp::R_POWERPC_REL16_HI:
2998 case elfcpp::R_POWERPC_REL16_HA:
2999 return Symbol::RELATIVE_REF;
3001 case elfcpp::R_POWERPC_REL24:
3002 case elfcpp::R_PPC_PLTREL24:
3003 case elfcpp::R_POWERPC_REL14:
3004 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3005 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3006 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
3008 case elfcpp::R_POWERPC_GOT16:
3009 case elfcpp::R_POWERPC_GOT16_LO:
3010 case elfcpp::R_POWERPC_GOT16_HI:
3011 case elfcpp::R_POWERPC_GOT16_HA:
3012 case elfcpp::R_PPC64_GOT16_DS:
3013 case elfcpp::R_PPC64_GOT16_LO_DS:
3014 case elfcpp::R_PPC64_TOC16:
3015 case elfcpp::R_PPC64_TOC16_LO:
3016 case elfcpp::R_PPC64_TOC16_HI:
3017 case elfcpp::R_PPC64_TOC16_HA:
3018 case elfcpp::R_PPC64_TOC16_DS:
3019 case elfcpp::R_PPC64_TOC16_LO_DS:
3021 return Symbol::ABSOLUTE_REF;
3023 case elfcpp::R_POWERPC_GOT_TPREL16:
3024 case elfcpp::R_POWERPC_TLS:
3025 return Symbol::TLS_REF;
3027 case elfcpp::R_POWERPC_COPY:
3028 case elfcpp::R_POWERPC_GLOB_DAT:
3029 case elfcpp::R_POWERPC_JMP_SLOT:
3030 case elfcpp::R_POWERPC_RELATIVE:
3031 case elfcpp::R_POWERPC_DTPMOD:
3033 // Not expected. We will give an error later.
3038 // Report an unsupported relocation against a local symbol.
3040 template<int size, bool big_endian>
3042 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
3043 Sized_relobj_file<size, big_endian>* object,
3044 unsigned int r_type)
3046 gold_error(_("%s: unsupported reloc %u against local symbol"),
3047 object->name().c_str(), r_type);
3050 // We are about to emit a dynamic relocation of type R_TYPE. If the
3051 // dynamic linker does not support it, issue an error.
3053 template<int size, bool big_endian>
3055 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
3056 unsigned int r_type)
3058 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
3060 // These are the relocation types supported by glibc for both 32-bit
3061 // and 64-bit powerpc.
3064 case elfcpp::R_POWERPC_NONE:
3065 case elfcpp::R_POWERPC_RELATIVE:
3066 case elfcpp::R_POWERPC_GLOB_DAT:
3067 case elfcpp::R_POWERPC_DTPMOD:
3068 case elfcpp::R_POWERPC_DTPREL:
3069 case elfcpp::R_POWERPC_TPREL:
3070 case elfcpp::R_POWERPC_JMP_SLOT:
3071 case elfcpp::R_POWERPC_COPY:
3072 case elfcpp::R_POWERPC_IRELATIVE:
3073 case elfcpp::R_POWERPC_ADDR32:
3074 case elfcpp::R_POWERPC_UADDR32:
3075 case elfcpp::R_POWERPC_ADDR24:
3076 case elfcpp::R_POWERPC_ADDR16:
3077 case elfcpp::R_POWERPC_UADDR16:
3078 case elfcpp::R_POWERPC_ADDR16_LO:
3079 case elfcpp::R_POWERPC_ADDR16_HI:
3080 case elfcpp::R_POWERPC_ADDR16_HA:
3081 case elfcpp::R_POWERPC_ADDR14:
3082 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3083 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3084 case elfcpp::R_POWERPC_REL32:
3085 case elfcpp::R_POWERPC_REL24:
3086 case elfcpp::R_POWERPC_TPREL16:
3087 case elfcpp::R_POWERPC_TPREL16_LO:
3088 case elfcpp::R_POWERPC_TPREL16_HI:
3089 case elfcpp::R_POWERPC_TPREL16_HA:
3100 // These are the relocation types supported only on 64-bit.
3101 case elfcpp::R_PPC64_ADDR64:
3102 case elfcpp::R_PPC64_UADDR64:
3103 case elfcpp::R_PPC64_JMP_IREL:
3104 case elfcpp::R_PPC64_ADDR16_DS:
3105 case elfcpp::R_PPC64_ADDR16_LO_DS:
3106 case elfcpp::R_PPC64_ADDR16_HIGHER:
3107 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3108 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3109 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3110 case elfcpp::R_PPC64_REL64:
3111 case elfcpp::R_POWERPC_ADDR30:
3112 case elfcpp::R_PPC64_TPREL16_DS:
3113 case elfcpp::R_PPC64_TPREL16_LO_DS:
3114 case elfcpp::R_PPC64_TPREL16_HIGHER:
3115 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3116 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3117 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3128 // These are the relocation types supported only on 32-bit.
3129 // ??? glibc ld.so doesn't need to support these.
3130 case elfcpp::R_POWERPC_DTPREL16:
3131 case elfcpp::R_POWERPC_DTPREL16_LO:
3132 case elfcpp::R_POWERPC_DTPREL16_HI:
3133 case elfcpp::R_POWERPC_DTPREL16_HA:
3141 // This prevents us from issuing more than one error per reloc
3142 // section. But we can still wind up issuing more than one
3143 // error per object file.
3144 if (this->issued_non_pic_error_)
3146 gold_assert(parameters->options().output_is_position_independent());
3147 object->error(_("requires unsupported dynamic reloc; "
3148 "recompile with -fPIC"));
3149 this->issued_non_pic_error_ = true;
3153 // Return whether we need to make a PLT entry for a relocation of the
3154 // given type against a STT_GNU_IFUNC symbol.
3156 template<int size, bool big_endian>
3158 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
3159 Sized_relobj_file<size, big_endian>* object,
3160 unsigned int r_type)
3162 // In non-pic code any reference will resolve to the plt call stub
3163 // for the ifunc symbol.
3164 if (size == 32 && !parameters->options().output_is_position_independent())
3169 // Word size refs from data sections are OK.
3170 case elfcpp::R_POWERPC_ADDR32:
3171 case elfcpp::R_POWERPC_UADDR32:
3176 case elfcpp::R_PPC64_ADDR64:
3177 case elfcpp::R_PPC64_UADDR64:
3182 // GOT refs are good.
3183 case elfcpp::R_POWERPC_GOT16:
3184 case elfcpp::R_POWERPC_GOT16_LO:
3185 case elfcpp::R_POWERPC_GOT16_HI:
3186 case elfcpp::R_POWERPC_GOT16_HA:
3187 case elfcpp::R_PPC64_GOT16_DS:
3188 case elfcpp::R_PPC64_GOT16_LO_DS:
3191 // So are function calls.
3192 case elfcpp::R_POWERPC_ADDR24:
3193 case elfcpp::R_POWERPC_ADDR14:
3194 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3195 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3196 case elfcpp::R_POWERPC_REL24:
3197 case elfcpp::R_PPC_PLTREL24:
3198 case elfcpp::R_POWERPC_REL14:
3199 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3200 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3207 // Anything else is a problem.
3208 // If we are building a static executable, the libc startup function
3209 // responsible for applying indirect function relocations is going
3210 // to complain about the reloc type.
3211 // If we are building a dynamic executable, we will have a text
3212 // relocation. The dynamic loader will set the text segment
3213 // writable and non-executable to apply text relocations. So we'll
3214 // segfault when trying to run the indirection function to resolve
3216 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
3217 object->name().c_str(), r_type);
3221 // Scan a relocation for a local symbol.
3223 template<int size, bool big_endian>
3225 Target_powerpc<size, big_endian>::Scan::local(
3226 Symbol_table* symtab,
3228 Target_powerpc<size, big_endian>* target,
3229 Sized_relobj_file<size, big_endian>* object,
3230 unsigned int data_shndx,
3231 Output_section* output_section,
3232 const elfcpp::Rela<size, big_endian>& reloc,
3233 unsigned int r_type,
3234 const elfcpp::Sym<size, big_endian>& lsym,
3237 Powerpc_relobj<size, big_endian>* ppc_object
3238 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3243 && data_shndx == ppc_object->opd_shndx()
3244 && r_type == elfcpp::R_PPC64_ADDR64)
3245 ppc_object->set_opd_discard(reloc.get_r_offset());
3249 // A local STT_GNU_IFUNC symbol may require a PLT entry.
3250 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
3251 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
3252 target->make_local_ifunc_plt_entry(layout, reloc, object);
3256 case elfcpp::R_POWERPC_NONE:
3257 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3258 case elfcpp::R_POWERPC_GNU_VTENTRY:
3259 case elfcpp::R_PPC64_TOCSAVE:
3260 case elfcpp::R_PPC_EMB_MRKREF:
3261 case elfcpp::R_POWERPC_TLS:
3264 case elfcpp::R_PPC64_TOC:
3266 Output_data_got_powerpc<size, big_endian>* got
3267 = target->got_section(symtab, layout);
3268 if (parameters->options().output_is_position_independent())
3270 Address off = reloc.get_r_offset();
3272 && data_shndx == ppc_object->opd_shndx()
3273 && ppc_object->get_opd_discard(off - 8))
3276 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3277 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
3278 rela_dyn->add_output_section_relative(got->output_section(),
3279 elfcpp::R_POWERPC_RELATIVE,
3281 object, data_shndx, off,
3282 symobj->toc_base_offset());
3287 case elfcpp::R_PPC64_ADDR64:
3288 case elfcpp::R_PPC64_UADDR64:
3289 case elfcpp::R_POWERPC_ADDR32:
3290 case elfcpp::R_POWERPC_UADDR32:
3291 case elfcpp::R_POWERPC_ADDR24:
3292 case elfcpp::R_POWERPC_ADDR16:
3293 case elfcpp::R_POWERPC_ADDR16_LO:
3294 case elfcpp::R_POWERPC_ADDR16_HI:
3295 case elfcpp::R_POWERPC_ADDR16_HA:
3296 case elfcpp::R_POWERPC_UADDR16:
3297 case elfcpp::R_PPC64_ADDR16_HIGHER:
3298 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3299 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3300 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3301 case elfcpp::R_PPC64_ADDR16_DS:
3302 case elfcpp::R_PPC64_ADDR16_LO_DS:
3303 case elfcpp::R_POWERPC_ADDR14:
3304 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3305 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3306 // If building a shared library (or a position-independent
3307 // executable), we need to create a dynamic relocation for
3309 if (parameters->options().output_is_position_independent()
3310 || (size == 64 && is_ifunc))
3312 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3314 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
3315 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
3317 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3318 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3321 rela_dyn = target->iplt_section()->rel_plt();
3322 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3324 rela_dyn->add_local_relative(object, r_sym, dynrel,
3325 output_section, data_shndx,
3326 reloc.get_r_offset(),
3327 reloc.get_r_addend(), false);
3331 check_non_pic(object, r_type);
3332 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3333 rela_dyn->add_local(object, r_sym, r_type, output_section,
3334 data_shndx, reloc.get_r_offset(),
3335 reloc.get_r_addend());
3340 case elfcpp::R_PPC64_REL64:
3341 case elfcpp::R_POWERPC_REL32:
3342 case elfcpp::R_POWERPC_REL24:
3343 case elfcpp::R_PPC_PLTREL24:
3344 case elfcpp::R_PPC_LOCAL24PC:
3345 case elfcpp::R_POWERPC_REL16:
3346 case elfcpp::R_POWERPC_REL16_LO:
3347 case elfcpp::R_POWERPC_REL16_HI:
3348 case elfcpp::R_POWERPC_REL16_HA:
3349 case elfcpp::R_POWERPC_REL14:
3350 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3351 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3352 case elfcpp::R_POWERPC_SECTOFF:
3353 case elfcpp::R_POWERPC_TPREL16:
3354 case elfcpp::R_POWERPC_DTPREL16:
3355 case elfcpp::R_POWERPC_SECTOFF_LO:
3356 case elfcpp::R_POWERPC_TPREL16_LO:
3357 case elfcpp::R_POWERPC_DTPREL16_LO:
3358 case elfcpp::R_POWERPC_SECTOFF_HI:
3359 case elfcpp::R_POWERPC_TPREL16_HI:
3360 case elfcpp::R_POWERPC_DTPREL16_HI:
3361 case elfcpp::R_POWERPC_SECTOFF_HA:
3362 case elfcpp::R_POWERPC_TPREL16_HA:
3363 case elfcpp::R_POWERPC_DTPREL16_HA:
3364 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3365 case elfcpp::R_PPC64_TPREL16_HIGHER:
3366 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3367 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3368 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3369 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3370 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3371 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3372 case elfcpp::R_PPC64_TPREL16_DS:
3373 case elfcpp::R_PPC64_TPREL16_LO_DS:
3374 case elfcpp::R_PPC64_DTPREL16_DS:
3375 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3376 case elfcpp::R_PPC64_SECTOFF_DS:
3377 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3378 case elfcpp::R_PPC64_TLSGD:
3379 case elfcpp::R_PPC64_TLSLD:
3382 case elfcpp::R_POWERPC_GOT16:
3383 case elfcpp::R_POWERPC_GOT16_LO:
3384 case elfcpp::R_POWERPC_GOT16_HI:
3385 case elfcpp::R_POWERPC_GOT16_HA:
3386 case elfcpp::R_PPC64_GOT16_DS:
3387 case elfcpp::R_PPC64_GOT16_LO_DS:
3389 // The symbol requires a GOT entry.
3390 Output_data_got_powerpc<size, big_endian>* got
3391 = target->got_section(symtab, layout);
3392 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3394 if (!parameters->options().output_is_position_independent())
3396 if (size == 32 && is_ifunc)
3397 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
3399 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3401 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
3403 // If we are generating a shared object or a pie, this
3404 // symbol's GOT entry will be set by a dynamic relocation.
3406 off = got->add_constant(0);
3407 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
3409 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3410 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3413 rela_dyn = target->iplt_section()->rel_plt();
3414 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3416 rela_dyn->add_local_relative(object, r_sym, dynrel,
3417 got, off, 0, false);
3422 case elfcpp::R_PPC64_TOC16:
3423 case elfcpp::R_PPC64_TOC16_LO:
3424 case elfcpp::R_PPC64_TOC16_HI:
3425 case elfcpp::R_PPC64_TOC16_HA:
3426 case elfcpp::R_PPC64_TOC16_DS:
3427 case elfcpp::R_PPC64_TOC16_LO_DS:
3428 // We need a GOT section.
3429 target->got_section(symtab, layout);
3432 case elfcpp::R_POWERPC_GOT_TLSGD16:
3433 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3434 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3435 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3437 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
3438 if (tls_type == tls::TLSOPT_NONE)
3440 Output_data_got_powerpc<size, big_endian>* got
3441 = target->got_section(symtab, layout);
3442 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3443 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3444 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
3445 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
3447 else if (tls_type == tls::TLSOPT_TO_LE)
3449 // no GOT relocs needed for Local Exec.
3456 case elfcpp::R_POWERPC_GOT_TLSLD16:
3457 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3458 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3459 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3461 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3462 if (tls_type == tls::TLSOPT_NONE)
3463 target->tlsld_got_offset(symtab, layout, object);
3464 else if (tls_type == tls::TLSOPT_TO_LE)
3466 // no GOT relocs needed for Local Exec.
3467 if (parameters->options().emit_relocs())
3469 Output_section* os = layout->tls_segment()->first_section();
3470 gold_assert(os != NULL);
3471 os->set_needs_symtab_index();
3479 case elfcpp::R_POWERPC_GOT_DTPREL16:
3480 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3481 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3482 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3484 Output_data_got_powerpc<size, big_endian>* got
3485 = target->got_section(symtab, layout);
3486 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3487 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
3491 case elfcpp::R_POWERPC_GOT_TPREL16:
3492 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3493 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3494 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3496 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
3497 if (tls_type == tls::TLSOPT_NONE)
3499 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3500 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
3502 Output_data_got_powerpc<size, big_endian>* got
3503 = target->got_section(symtab, layout);
3504 unsigned int off = got->add_constant(0);
3505 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
3507 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3508 rela_dyn->add_symbolless_local_addend(object, r_sym,
3509 elfcpp::R_POWERPC_TPREL,
3513 else if (tls_type == tls::TLSOPT_TO_LE)
3515 // no GOT relocs needed for Local Exec.
3523 unsupported_reloc_local(object, r_type);
3528 // Report an unsupported relocation against a global symbol.
3530 template<int size, bool big_endian>
3532 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
3533 Sized_relobj_file<size, big_endian>* object,
3534 unsigned int r_type,
3537 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3538 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3541 // Scan a relocation for a global symbol.
3543 template<int size, bool big_endian>
3545 Target_powerpc<size, big_endian>::Scan::global(
3546 Symbol_table* symtab,
3548 Target_powerpc<size, big_endian>* target,
3549 Sized_relobj_file<size, big_endian>* object,
3550 unsigned int data_shndx,
3551 Output_section* output_section,
3552 const elfcpp::Rela<size, big_endian>& reloc,
3553 unsigned int r_type,
3556 Powerpc_relobj<size, big_endian>* ppc_object
3557 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3559 // A STT_GNU_IFUNC symbol may require a PLT entry.
3560 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3561 && this->reloc_needs_plt_for_ifunc(object, r_type))
3562 target->make_plt_entry(layout, gsym, reloc, object);
3566 case elfcpp::R_POWERPC_NONE:
3567 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3568 case elfcpp::R_POWERPC_GNU_VTENTRY:
3569 case elfcpp::R_PPC_LOCAL24PC:
3570 case elfcpp::R_PPC_EMB_MRKREF:
3571 case elfcpp::R_POWERPC_TLS:
3574 case elfcpp::R_PPC64_TOC:
3576 Output_data_got_powerpc<size, big_endian>* got
3577 = target->got_section(symtab, layout);
3578 if (parameters->options().output_is_position_independent())
3580 Address off = reloc.get_r_offset();
3582 && data_shndx == ppc_object->opd_shndx()
3583 && ppc_object->get_opd_discard(off - 8))
3586 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3587 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
3588 if (data_shndx != ppc_object->opd_shndx())
3589 symobj = static_cast
3590 <Powerpc_relobj<size, big_endian>*>(gsym->object());
3591 rela_dyn->add_output_section_relative(got->output_section(),
3592 elfcpp::R_POWERPC_RELATIVE,
3594 object, data_shndx, off,
3595 symobj->toc_base_offset());
3600 case elfcpp::R_PPC64_ADDR64:
3602 && data_shndx == ppc_object->opd_shndx()
3603 && (gsym->is_defined_in_discarded_section()
3604 || gsym->object() != object))
3606 ppc_object->set_opd_discard(reloc.get_r_offset());
3610 case elfcpp::R_PPC64_UADDR64:
3611 case elfcpp::R_POWERPC_ADDR32:
3612 case elfcpp::R_POWERPC_UADDR32:
3613 case elfcpp::R_POWERPC_ADDR24:
3614 case elfcpp::R_POWERPC_ADDR16:
3615 case elfcpp::R_POWERPC_ADDR16_LO:
3616 case elfcpp::R_POWERPC_ADDR16_HI:
3617 case elfcpp::R_POWERPC_ADDR16_HA:
3618 case elfcpp::R_POWERPC_UADDR16:
3619 case elfcpp::R_PPC64_ADDR16_HIGHER:
3620 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3621 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3622 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3623 case elfcpp::R_PPC64_ADDR16_DS:
3624 case elfcpp::R_PPC64_ADDR16_LO_DS:
3625 case elfcpp::R_POWERPC_ADDR14:
3626 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3627 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3629 // Make a PLT entry if necessary.
3630 if (gsym->needs_plt_entry())
3632 target->make_plt_entry(layout, gsym, reloc, 0);
3633 // Since this is not a PC-relative relocation, we may be
3634 // taking the address of a function. In that case we need to
3635 // set the entry in the dynamic symbol table to the address of
3636 // the PLT call stub.
3638 && gsym->is_from_dynobj()
3639 && !parameters->options().output_is_position_independent())
3640 gsym->set_needs_dynsym_value();
3642 // Make a dynamic relocation if necessary.
3643 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
3644 || (size == 64 && gsym->type() == elfcpp::STT_GNU_IFUNC))
3646 if (gsym->may_need_copy_reloc())
3648 target->copy_reloc(symtab, layout, object,
3649 data_shndx, output_section, gsym, reloc);
3651 else if (((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
3652 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
3653 && (gsym->can_use_relative_reloc(false)
3655 && data_shndx == ppc_object->opd_shndx())))
3657 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3658 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3659 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3661 rela_dyn = target->iplt_section()->rel_plt();
3662 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3664 rela_dyn->add_symbolless_global_addend(
3665 gsym, dynrel, output_section, object, data_shndx,
3666 reloc.get_r_offset(), reloc.get_r_addend());
3670 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3671 check_non_pic(object, r_type);
3672 rela_dyn->add_global(gsym, r_type, output_section,
3674 reloc.get_r_offset(),
3675 reloc.get_r_addend());
3681 case elfcpp::R_PPC_PLTREL24:
3682 case elfcpp::R_POWERPC_REL24:
3683 if (gsym->needs_plt_entry()
3684 || (!gsym->final_value_is_known()
3685 && (gsym->is_undefined()
3686 || gsym->is_from_dynobj()
3687 || gsym->is_preemptible())))
3688 target->make_plt_entry(layout, gsym, reloc, object);
3691 case elfcpp::R_PPC64_REL64:
3692 case elfcpp::R_POWERPC_REL32:
3693 // Make a dynamic relocation if necessary.
3694 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
3696 if (gsym->may_need_copy_reloc())
3698 target->copy_reloc(symtab, layout, object,
3699 data_shndx, output_section, gsym,
3704 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3705 check_non_pic(object, r_type);
3706 rela_dyn->add_global(gsym, r_type, output_section, object,
3707 data_shndx, reloc.get_r_offset(),
3708 reloc.get_r_addend());
3713 case elfcpp::R_POWERPC_REL16:
3714 case elfcpp::R_POWERPC_REL16_LO:
3715 case elfcpp::R_POWERPC_REL16_HI:
3716 case elfcpp::R_POWERPC_REL16_HA:
3717 case elfcpp::R_POWERPC_REL14:
3718 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3719 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3720 case elfcpp::R_POWERPC_SECTOFF:
3721 case elfcpp::R_POWERPC_TPREL16:
3722 case elfcpp::R_POWERPC_DTPREL16:
3723 case elfcpp::R_POWERPC_SECTOFF_LO:
3724 case elfcpp::R_POWERPC_TPREL16_LO:
3725 case elfcpp::R_POWERPC_DTPREL16_LO:
3726 case elfcpp::R_POWERPC_SECTOFF_HI:
3727 case elfcpp::R_POWERPC_TPREL16_HI:
3728 case elfcpp::R_POWERPC_DTPREL16_HI:
3729 case elfcpp::R_POWERPC_SECTOFF_HA:
3730 case elfcpp::R_POWERPC_TPREL16_HA:
3731 case elfcpp::R_POWERPC_DTPREL16_HA:
3732 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3733 case elfcpp::R_PPC64_TPREL16_HIGHER:
3734 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3735 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3736 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3737 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3738 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3739 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3740 case elfcpp::R_PPC64_TPREL16_DS:
3741 case elfcpp::R_PPC64_TPREL16_LO_DS:
3742 case elfcpp::R_PPC64_DTPREL16_DS:
3743 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3744 case elfcpp::R_PPC64_SECTOFF_DS:
3745 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3746 case elfcpp::R_PPC64_TLSGD:
3747 case elfcpp::R_PPC64_TLSLD:
3750 case elfcpp::R_POWERPC_GOT16:
3751 case elfcpp::R_POWERPC_GOT16_LO:
3752 case elfcpp::R_POWERPC_GOT16_HI:
3753 case elfcpp::R_POWERPC_GOT16_HA:
3754 case elfcpp::R_PPC64_GOT16_DS:
3755 case elfcpp::R_PPC64_GOT16_LO_DS:
3757 // The symbol requires a GOT entry.
3758 Output_data_got_powerpc<size, big_endian>* got;
3760 got = target->got_section(symtab, layout);
3761 if (gsym->final_value_is_known())
3763 if (size == 32 && gsym->type() == elfcpp::STT_GNU_IFUNC)
3764 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3766 got->add_global(gsym, GOT_TYPE_STANDARD);
3768 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
3770 // If we are generating a shared object or a pie, this
3771 // symbol's GOT entry will be set by a dynamic relocation.
3772 unsigned int off = got->add_constant(0);
3773 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
3775 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3776 if (gsym->can_use_relative_reloc(false)
3778 && gsym->visibility() == elfcpp::STV_PROTECTED
3779 && parameters->options().shared()))
3781 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3782 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3784 rela_dyn = target->iplt_section()->rel_plt();
3785 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3787 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
3791 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
3792 rela_dyn->add_global(gsym, dynrel, got, off, 0);
3798 case elfcpp::R_PPC64_TOC16:
3799 case elfcpp::R_PPC64_TOC16_LO:
3800 case elfcpp::R_PPC64_TOC16_HI:
3801 case elfcpp::R_PPC64_TOC16_HA:
3802 case elfcpp::R_PPC64_TOC16_DS:
3803 case elfcpp::R_PPC64_TOC16_LO_DS:
3804 // We need a GOT section.
3805 target->got_section(symtab, layout);
3808 case elfcpp::R_POWERPC_GOT_TLSGD16:
3809 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3810 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3811 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3813 const bool final = gsym->final_value_is_known();
3814 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3815 if (tls_type == tls::TLSOPT_NONE)
3817 Output_data_got_powerpc<size, big_endian>* got
3818 = target->got_section(symtab, layout);
3819 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
3820 target->rela_dyn_section(layout),
3821 elfcpp::R_POWERPC_DTPMOD,
3822 elfcpp::R_POWERPC_DTPREL);
3824 else if (tls_type == tls::TLSOPT_TO_IE)
3826 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
3828 Output_data_got_powerpc<size, big_endian>* got
3829 = target->got_section(symtab, layout);
3830 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3831 if (gsym->is_undefined()
3832 || gsym->is_from_dynobj())
3834 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
3835 elfcpp::R_POWERPC_TPREL);
3839 unsigned int off = got->add_constant(0);
3840 gsym->set_got_offset(GOT_TYPE_TPREL, off);
3841 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
3842 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
3847 else if (tls_type == tls::TLSOPT_TO_LE)
3849 // no GOT relocs needed for Local Exec.
3856 case elfcpp::R_POWERPC_GOT_TLSLD16:
3857 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3858 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3859 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3861 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3862 if (tls_type == tls::TLSOPT_NONE)
3863 target->tlsld_got_offset(symtab, layout, object);
3864 else if (tls_type == tls::TLSOPT_TO_LE)
3866 // no GOT relocs needed for Local Exec.
3867 if (parameters->options().emit_relocs())
3869 Output_section* os = layout->tls_segment()->first_section();
3870 gold_assert(os != NULL);
3871 os->set_needs_symtab_index();
3879 case elfcpp::R_POWERPC_GOT_DTPREL16:
3880 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3881 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3882 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3884 Output_data_got_powerpc<size, big_endian>* got
3885 = target->got_section(symtab, layout);
3886 if (!gsym->final_value_is_known()
3887 && (gsym->is_from_dynobj()
3888 || gsym->is_undefined()
3889 || gsym->is_preemptible()))
3890 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
3891 target->rela_dyn_section(layout),
3892 elfcpp::R_POWERPC_DTPREL);
3894 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
3898 case elfcpp::R_POWERPC_GOT_TPREL16:
3899 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3900 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3901 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3903 const bool final = gsym->final_value_is_known();
3904 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3905 if (tls_type == tls::TLSOPT_NONE)
3907 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
3909 Output_data_got_powerpc<size, big_endian>* got
3910 = target->got_section(symtab, layout);
3911 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3912 if (gsym->is_undefined()
3913 || gsym->is_from_dynobj())
3915 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
3916 elfcpp::R_POWERPC_TPREL);
3920 unsigned int off = got->add_constant(0);
3921 gsym->set_got_offset(GOT_TYPE_TPREL, off);
3922 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
3923 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
3928 else if (tls_type == tls::TLSOPT_TO_LE)
3930 // no GOT relocs needed for Local Exec.
3938 unsupported_reloc_global(object, r_type, gsym);
3943 // Process relocations for gc.
3945 template<int size, bool big_endian>
3947 Target_powerpc<size, big_endian>::gc_process_relocs(
3948 Symbol_table* symtab,
3950 Sized_relobj_file<size, big_endian>* object,
3951 unsigned int data_shndx,
3953 const unsigned char* prelocs,
3955 Output_section* output_section,
3956 bool needs_special_offset_handling,
3957 size_t local_symbol_count,
3958 const unsigned char* plocal_symbols)
3960 typedef Target_powerpc<size, big_endian> Powerpc;
3961 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
3962 Powerpc_relobj<size, big_endian>* ppc_object
3963 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3965 ppc_object->set_opd_valid();
3966 if (size == 64 && data_shndx == ppc_object->opd_shndx())
3968 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
3969 for (p = ppc_object->access_from_map()->begin();
3970 p != ppc_object->access_from_map()->end();
3973 Address dst_off = p->first;
3974 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
3975 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
3976 for (s = p->second.begin(); s != p->second.end(); ++s)
3978 Object* src_obj = s->first;
3979 unsigned int src_indx = s->second;
3980 symtab->gc()->add_reference(src_obj, src_indx,
3981 ppc_object, dst_indx);
3985 ppc_object->access_from_map()->clear();
3986 ppc_object->process_gc_mark(symtab);
3987 // Don't look at .opd relocs as .opd will reference everything.
3991 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
3992 typename Target_powerpc::Relocatable_size_for_reloc>(
4001 needs_special_offset_handling,
4006 // Handle target specific gc actions when adding a gc reference from
4007 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
4008 // and DST_OFF. For powerpc64, this adds a referenc to the code
4009 // section of a function descriptor.
4011 template<int size, bool big_endian>
4013 Target_powerpc<size, big_endian>::do_gc_add_reference(
4014 Symbol_table* symtab,
4016 unsigned int src_shndx,
4018 unsigned int dst_shndx,
4019 Address dst_off) const
4021 Powerpc_relobj<size, big_endian>* ppc_object
4022 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
4024 && !ppc_object->is_dynamic()
4025 && dst_shndx == ppc_object->opd_shndx())
4027 if (ppc_object->opd_valid())
4029 dst_shndx = ppc_object->get_opd_ent(dst_off);
4030 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
4034 // If we haven't run scan_opd_relocs, we must delay
4035 // processing this function descriptor reference.
4036 ppc_object->add_reference(src_obj, src_shndx, dst_off);
4041 // Add any special sections for this symbol to the gc work list.
4042 // For powerpc64, this adds the code section of a function
4045 template<int size, bool big_endian>
4047 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
4048 Symbol_table* symtab,
4053 Powerpc_relobj<size, big_endian>* ppc_object
4054 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
4056 unsigned int shndx = sym->shndx(&is_ordinary);
4057 if (is_ordinary && shndx == ppc_object->opd_shndx())
4059 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
4060 Address dst_off = gsym->value();
4061 if (ppc_object->opd_valid())
4063 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
4064 symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
4067 ppc_object->add_gc_mark(dst_off);
4072 // Scan relocations for a section.
4074 template<int size, bool big_endian>
4076 Target_powerpc<size, big_endian>::scan_relocs(
4077 Symbol_table* symtab,
4079 Sized_relobj_file<size, big_endian>* object,
4080 unsigned int data_shndx,
4081 unsigned int sh_type,
4082 const unsigned char* prelocs,
4084 Output_section* output_section,
4085 bool needs_special_offset_handling,
4086 size_t local_symbol_count,
4087 const unsigned char* plocal_symbols)
4089 typedef Target_powerpc<size, big_endian> Powerpc;
4090 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
4092 if (sh_type == elfcpp::SHT_REL)
4094 gold_error(_("%s: unsupported REL reloc section"),
4095 object->name().c_str());
4099 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
4108 needs_special_offset_handling,
4113 // Functor class for processing the global symbol table.
4114 // Removes symbols defined on discarded opd entries.
4116 template<bool big_endian>
4117 class Global_symbol_visitor_opd
4120 Global_symbol_visitor_opd()
4124 operator()(Sized_symbol<64>* sym)
4126 if (sym->has_symtab_index()
4127 || sym->source() != Symbol::FROM_OBJECT
4128 || !sym->in_real_elf())
4131 Powerpc_relobj<64, big_endian>* symobj
4132 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
4133 if (symobj->is_dynamic()
4134 || symobj->opd_shndx() == 0)
4138 unsigned int shndx = sym->shndx(&is_ordinary);
4139 if (shndx == symobj->opd_shndx()
4140 && symobj->get_opd_discard(sym->value()))
4141 sym->set_symtab_index(-1U);
4145 template<int size, bool big_endian>
4147 Target_powerpc<size, big_endian>::define_save_restore_funcs(
4149 Symbol_table* symtab)
4153 Output_data_save_res<64, big_endian>* savres
4154 = new Output_data_save_res<64, big_endian>(symtab);
4155 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
4156 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
4157 savres, ORDER_TEXT, false);
4161 // Finalize the sections.
4163 template<int size, bool big_endian>
4165 Target_powerpc<size, big_endian>::do_finalize_sections(
4167 const Input_objects*,
4168 Symbol_table* symtab)
4170 if (parameters->doing_static_link())
4172 // At least some versions of glibc elf-init.o have a strong
4173 // reference to __rela_iplt marker syms. A weak ref would be
4175 if (this->iplt_ != NULL)
4177 Reloc_section* rel = this->iplt_->rel_plt();
4178 symtab->define_in_output_data("__rela_iplt_start", NULL,
4179 Symbol_table::PREDEFINED, rel, 0, 0,
4180 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4181 elfcpp::STV_HIDDEN, 0, false, true);
4182 symtab->define_in_output_data("__rela_iplt_end", NULL,
4183 Symbol_table::PREDEFINED, rel, 0, 0,
4184 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4185 elfcpp::STV_HIDDEN, 0, true, true);
4189 symtab->define_as_constant("__rela_iplt_start", NULL,
4190 Symbol_table::PREDEFINED, 0, 0,
4191 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4192 elfcpp::STV_HIDDEN, 0, true, false);
4193 symtab->define_as_constant("__rela_iplt_end", NULL,
4194 Symbol_table::PREDEFINED, 0, 0,
4195 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4196 elfcpp::STV_HIDDEN, 0, true, false);
4202 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
4203 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
4204 this->define_save_restore_funcs(layout, symtab);
4207 // Fill in some more dynamic tags.
4208 Output_data_dynamic* odyn = layout->dynamic_data();
4211 const Reloc_section* rel_plt = (this->plt_ == NULL
4213 : this->plt_->rel_plt());
4214 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
4215 this->rela_dyn_, true, size == 32);
4219 if (this->got_ != NULL)
4221 this->got_->finalize_data_size();
4222 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
4223 this->got_, this->got_->g_o_t());
4228 if (this->glink_ != NULL)
4230 this->glink_->finalize_data_size();
4231 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
4233 (this->glink_->pltresolve()
4234 + this->glink_->pltresolve_size
4240 // Emit any relocs we saved in an attempt to avoid generating COPY
4242 if (this->copy_relocs_.any_saved_relocs())
4243 this->copy_relocs_.emit(this->rela_dyn_section(layout));
4246 // Return the value to use for a branch relocation.
4248 template<int size, bool big_endian>
4249 typename elfcpp::Elf_types<size>::Elf_Addr
4250 Target_powerpc<size, big_endian>::symval_for_branch(
4252 const Sized_symbol<size>* gsym,
4253 Powerpc_relobj<size, big_endian>* object,
4254 unsigned int *dest_shndx)
4260 // If the symbol is defined in an opd section, ie. is a function
4261 // descriptor, use the function descriptor code entry address
4262 Powerpc_relobj<size, big_endian>* symobj = object;
4264 && gsym->source() != Symbol::FROM_OBJECT)
4267 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
4268 unsigned int shndx = symobj->opd_shndx();
4271 Address opd_addr = symobj->get_output_section_offset(shndx);
4272 gold_assert(opd_addr != invalid_address);
4273 opd_addr += symobj->output_section(shndx)->address();
4274 if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
4277 *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
4278 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
4279 gold_assert(sec_addr != invalid_address);
4280 sec_addr += symobj->output_section(*dest_shndx)->address();
4281 value = sec_addr + sec_off;
4286 // Perform a relocation.
4288 template<int size, bool big_endian>
4290 Target_powerpc<size, big_endian>::Relocate::relocate(
4291 const Relocate_info<size, big_endian>* relinfo,
4292 Target_powerpc* target,
4295 const elfcpp::Rela<size, big_endian>& rela,
4296 unsigned int r_type,
4297 const Sized_symbol<size>* gsym,
4298 const Symbol_value<size>* psymval,
4299 unsigned char* view,
4301 section_size_type view_size)
4304 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
4305 || r_type == elfcpp::R_PPC_PLTREL24)
4307 && strcmp(gsym->name(), "__tls_get_addr") == 0);
4308 enum skip_tls last_tls = this->call_tls_get_addr_;
4309 this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
4312 if (last_tls == CALL_NOT_EXPECTED)
4313 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4314 _("__tls_get_addr call lacks marker reloc"));
4315 else if (last_tls == CALL_SKIP)
4318 else if (last_tls != CALL_NOT_EXPECTED)
4319 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4320 _("missing expected __tls_get_addr call"));
4322 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
4323 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
4324 Powerpc_relobj<size, big_endian>* const object
4325 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
4327 bool has_plt_value = false;
4328 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4330 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
4331 : object->local_has_plt_offset(r_sym))
4333 const Output_data_glink<size, big_endian>* glink
4334 = target->glink_section();
4335 unsigned int glink_index;
4337 glink_index = glink->find_entry(object, gsym, rela);
4339 glink_index = glink->find_entry(object, r_sym, rela);
4340 value = glink->address() + glink_index * glink->glink_entry_size();
4341 has_plt_value = true;
4344 if (r_type == elfcpp::R_POWERPC_GOT16
4345 || r_type == elfcpp::R_POWERPC_GOT16_LO
4346 || r_type == elfcpp::R_POWERPC_GOT16_HI
4347 || r_type == elfcpp::R_POWERPC_GOT16_HA
4348 || r_type == elfcpp::R_PPC64_GOT16_DS
4349 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
4353 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4354 value = gsym->got_offset(GOT_TYPE_STANDARD);
4358 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4359 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4360 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
4362 value -= target->got_section()->got_base_offset(object);
4364 else if (r_type == elfcpp::R_PPC64_TOC)
4366 value = (target->got_section()->output_section()->address()
4367 + object->toc_base_offset());
4369 else if (gsym != NULL
4370 && (r_type == elfcpp::R_POWERPC_REL24
4371 || r_type == elfcpp::R_PPC_PLTREL24)
4376 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4377 Valtype* wv = reinterpret_cast<Valtype*>(view);
4378 bool can_plt_call = false;
4379 if (rela.get_r_offset() + 8 <= view_size)
4381 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
4382 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
4385 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
4387 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
4388 can_plt_call = true;
4393 // If we don't have a branch and link followed by a nop,
4394 // we can't go via the plt because there is no place to
4395 // put a toc restoring instruction.
4396 // Unless we know we won't be returning.
4397 if (strcmp(gsym->name(), "__libc_start_main") == 0)
4398 can_plt_call = true;
4402 // This is not an error in one special case: A self
4403 // call. It isn't possible to cheaply verify we have
4404 // such a call so just check for a call to the same
4407 Address code = value;
4408 if (gsym->source() == Symbol::FROM_OBJECT
4409 && gsym->object() == object)
4411 Address addend = rela.get_r_addend();
4412 unsigned int dest_shndx;
4413 Address opdent = psymval->value(object, addend);
4414 code = target->symval_for_branch(opdent, gsym, object,
4417 if (dest_shndx == 0)
4418 dest_shndx = gsym->shndx(&is_ordinary);
4419 ok = dest_shndx == relinfo->data_shndx;
4423 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4424 _("call lacks nop, can't restore toc; "
4425 "recompile with -fPIC"));
4431 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4432 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
4433 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
4434 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
4436 // First instruction of a global dynamic sequence, arg setup insn.
4437 const bool final = gsym == NULL || gsym->final_value_is_known();
4438 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
4439 enum Got_type got_type = GOT_TYPE_STANDARD;
4440 if (tls_type == tls::TLSOPT_NONE)
4441 got_type = GOT_TYPE_TLSGD;
4442 else if (tls_type == tls::TLSOPT_TO_IE)
4443 got_type = GOT_TYPE_TPREL;
4444 if (got_type != GOT_TYPE_STANDARD)
4448 gold_assert(gsym->has_got_offset(got_type));
4449 value = gsym->got_offset(got_type);
4453 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4454 gold_assert(object->local_has_got_offset(r_sym, got_type));
4455 value = object->local_got_offset(r_sym, got_type);
4457 value -= target->got_section()->got_base_offset(object);
4459 if (tls_type == tls::TLSOPT_TO_IE)
4461 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4462 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4464 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4465 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4466 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
4468 insn |= 32 << 26; // lwz
4470 insn |= 58 << 26; // ld
4471 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4473 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
4474 - elfcpp::R_POWERPC_GOT_TLSGD16);
4476 else if (tls_type == tls::TLSOPT_TO_LE)
4478 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4479 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4481 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4482 Insn insn = addis_3_13;
4485 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4486 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4487 value = psymval->value(object, rela.get_r_addend());
4491 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4493 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4494 r_type = elfcpp::R_POWERPC_NONE;
4498 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4499 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
4500 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
4501 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
4503 // First instruction of a local dynamic sequence, arg setup insn.
4504 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4505 if (tls_type == tls::TLSOPT_NONE)
4507 value = target->tlsld_got_offset();
4508 value -= target->got_section()->got_base_offset(object);
4512 gold_assert(tls_type == tls::TLSOPT_TO_LE);
4513 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4514 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
4516 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4517 Insn insn = addis_3_13;
4520 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4521 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4526 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4528 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4529 r_type = elfcpp::R_POWERPC_NONE;
4533 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
4534 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
4535 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
4536 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
4538 // Accesses relative to a local dynamic sequence address,
4539 // no optimisation here.
4542 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
4543 value = gsym->got_offset(GOT_TYPE_DTPREL);
4547 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4548 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
4549 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
4551 value -= target->got_section()->got_base_offset(object);
4553 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4554 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
4555 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
4556 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
4558 // First instruction of initial exec sequence.
4559 const bool final = gsym == NULL || gsym->final_value_is_known();
4560 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
4561 if (tls_type == tls::TLSOPT_NONE)
4565 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
4566 value = gsym->got_offset(GOT_TYPE_TPREL);
4570 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4571 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
4572 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
4574 value -= target->got_section()->got_base_offset(object);
4578 gold_assert(tls_type == tls::TLSOPT_TO_LE);
4579 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4580 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
4582 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4583 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4584 insn &= (1 << 26) - (1 << 21); // extract rt from ld
4589 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4590 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4591 value = psymval->value(object, rela.get_r_addend());
4595 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4597 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4598 r_type = elfcpp::R_POWERPC_NONE;
4602 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4603 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4605 // Second instruction of a global dynamic sequence,
4606 // the __tls_get_addr call
4607 this->call_tls_get_addr_ = CALL_EXPECTED;
4608 const bool final = gsym == NULL || gsym->final_value_is_known();
4609 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
4610 if (tls_type != tls::TLSOPT_NONE)
4612 if (tls_type == tls::TLSOPT_TO_IE)
4614 Insn* iview = reinterpret_cast<Insn*>(view);
4615 Insn insn = add_3_3_13;
4618 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4619 r_type = elfcpp::R_POWERPC_NONE;
4623 Insn* iview = reinterpret_cast<Insn*>(view);
4624 Insn insn = addi_3_3;
4625 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4626 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4627 view += 2 * big_endian;
4628 value = psymval->value(object, rela.get_r_addend());
4630 this->call_tls_get_addr_ = CALL_SKIP;
4633 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4634 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4636 // Second instruction of a local dynamic sequence,
4637 // the __tls_get_addr call
4638 this->call_tls_get_addr_ = CALL_EXPECTED;
4639 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4640 if (tls_type == tls::TLSOPT_TO_LE)
4642 Insn* iview = reinterpret_cast<Insn*>(view);
4643 Insn insn = addi_3_3;
4644 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4645 this->call_tls_get_addr_ = CALL_SKIP;
4646 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4647 view += 2 * big_endian;
4651 else if (r_type == elfcpp::R_POWERPC_TLS)
4653 // Second instruction of an initial exec sequence
4654 const bool final = gsym == NULL || gsym->final_value_is_known();
4655 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
4656 if (tls_type == tls::TLSOPT_TO_LE)
4658 Insn* iview = reinterpret_cast<Insn*>(view);
4659 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4660 unsigned int reg = size == 32 ? 2 : 13;
4661 insn = at_tls_transform(insn, reg);
4662 gold_assert(insn != 0);
4663 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4664 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4665 view += 2 * big_endian;
4666 value = psymval->value(object, rela.get_r_addend());
4669 else if (!has_plt_value)
4672 unsigned int dest_shndx;
4673 if (r_type != elfcpp::R_PPC_PLTREL24)
4674 addend = rela.get_r_addend();
4675 value = psymval->value(object, addend);
4676 if (size == 64 && is_branch_reloc(r_type))
4677 value = target->symval_for_branch(value, gsym, object, &dest_shndx);
4682 case elfcpp::R_PPC64_REL64:
4683 case elfcpp::R_POWERPC_REL32:
4684 case elfcpp::R_POWERPC_REL24:
4685 case elfcpp::R_PPC_PLTREL24:
4686 case elfcpp::R_PPC_LOCAL24PC:
4687 case elfcpp::R_POWERPC_REL16:
4688 case elfcpp::R_POWERPC_REL16_LO:
4689 case elfcpp::R_POWERPC_REL16_HI:
4690 case elfcpp::R_POWERPC_REL16_HA:
4691 case elfcpp::R_POWERPC_REL14:
4692 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4693 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4697 case elfcpp::R_PPC64_TOC16:
4698 case elfcpp::R_PPC64_TOC16_LO:
4699 case elfcpp::R_PPC64_TOC16_HI:
4700 case elfcpp::R_PPC64_TOC16_HA:
4701 case elfcpp::R_PPC64_TOC16_DS:
4702 case elfcpp::R_PPC64_TOC16_LO_DS:
4703 // Subtract the TOC base address.
4704 value -= (target->got_section()->output_section()->address()
4705 + object->toc_base_offset());
4708 case elfcpp::R_POWERPC_SECTOFF:
4709 case elfcpp::R_POWERPC_SECTOFF_LO:
4710 case elfcpp::R_POWERPC_SECTOFF_HI:
4711 case elfcpp::R_POWERPC_SECTOFF_HA:
4712 case elfcpp::R_PPC64_SECTOFF_DS:
4713 case elfcpp::R_PPC64_SECTOFF_LO_DS:
4715 value -= os->address();
4718 case elfcpp::R_PPC64_TPREL16_DS:
4719 case elfcpp::R_PPC64_TPREL16_LO_DS:
4721 // R_PPC_TLSGD and R_PPC_TLSLD
4723 case elfcpp::R_POWERPC_TPREL16:
4724 case elfcpp::R_POWERPC_TPREL16_LO:
4725 case elfcpp::R_POWERPC_TPREL16_HI:
4726 case elfcpp::R_POWERPC_TPREL16_HA:
4727 case elfcpp::R_POWERPC_TPREL:
4728 case elfcpp::R_PPC64_TPREL16_HIGHER:
4729 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4730 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4731 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4732 // tls symbol values are relative to tls_segment()->vaddr()
4736 case elfcpp::R_PPC64_DTPREL16_DS:
4737 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4738 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4739 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4740 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4741 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4743 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
4744 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
4746 case elfcpp::R_POWERPC_DTPREL16:
4747 case elfcpp::R_POWERPC_DTPREL16_LO:
4748 case elfcpp::R_POWERPC_DTPREL16_HI:
4749 case elfcpp::R_POWERPC_DTPREL16_HA:
4750 case elfcpp::R_POWERPC_DTPREL:
4751 // tls symbol values are relative to tls_segment()->vaddr()
4752 value -= dtp_offset;
4759 Insn branch_bit = 0;
4762 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4763 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4764 branch_bit = 1 << 21;
4765 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4766 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4768 Insn* iview = reinterpret_cast<Insn*>(view);
4769 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4772 if (this->is_isa_v2)
4774 // Set 'a' bit. This is 0b00010 in BO field for branch
4775 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
4776 // for branch on CTR insns (BO == 1a00t or 1a01t).
4777 if ((insn & (0x14 << 21)) == (0x04 << 21))
4779 else if ((insn & (0x14 << 21)) == (0x10 << 21))
4786 // Invert 'y' bit if not the default.
4787 if (static_cast<Signed_address>(value) < 0)
4790 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4798 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
4801 case elfcpp::R_POWERPC_ADDR32:
4802 case elfcpp::R_POWERPC_UADDR32:
4804 overflow = Reloc::CHECK_BITFIELD;
4807 case elfcpp::R_POWERPC_REL32:
4809 overflow = Reloc::CHECK_SIGNED;
4812 case elfcpp::R_POWERPC_ADDR24:
4813 case elfcpp::R_POWERPC_ADDR16:
4814 case elfcpp::R_POWERPC_UADDR16:
4815 case elfcpp::R_PPC64_ADDR16_DS:
4816 case elfcpp::R_POWERPC_ADDR14:
4817 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4818 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4819 overflow = Reloc::CHECK_BITFIELD;
4822 case elfcpp::R_POWERPC_REL24:
4823 case elfcpp::R_PPC_PLTREL24:
4824 case elfcpp::R_PPC_LOCAL24PC:
4825 case elfcpp::R_POWERPC_REL16:
4826 case elfcpp::R_PPC64_TOC16:
4827 case elfcpp::R_POWERPC_GOT16:
4828 case elfcpp::R_POWERPC_SECTOFF:
4829 case elfcpp::R_POWERPC_TPREL16:
4830 case elfcpp::R_POWERPC_DTPREL16:
4831 case elfcpp::R_PPC64_TPREL16_DS:
4832 case elfcpp::R_PPC64_DTPREL16_DS:
4833 case elfcpp::R_PPC64_TOC16_DS:
4834 case elfcpp::R_PPC64_GOT16_DS:
4835 case elfcpp::R_PPC64_SECTOFF_DS:
4836 case elfcpp::R_POWERPC_REL14:
4837 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4838 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4839 case elfcpp::R_POWERPC_GOT_TLSGD16:
4840 case elfcpp::R_POWERPC_GOT_TLSLD16:
4841 case elfcpp::R_POWERPC_GOT_TPREL16:
4842 case elfcpp::R_POWERPC_GOT_DTPREL16:
4843 overflow = Reloc::CHECK_SIGNED;
4847 typename Powerpc_relocate_functions<size, big_endian>::Status status
4848 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
4851 case elfcpp::R_POWERPC_NONE:
4852 case elfcpp::R_POWERPC_TLS:
4853 case elfcpp::R_POWERPC_GNU_VTINHERIT:
4854 case elfcpp::R_POWERPC_GNU_VTENTRY:
4855 case elfcpp::R_PPC_EMB_MRKREF:
4858 case elfcpp::R_PPC64_ADDR64:
4859 case elfcpp::R_PPC64_REL64:
4860 case elfcpp::R_PPC64_TOC:
4861 Reloc::addr64(view, value);
4864 case elfcpp::R_POWERPC_TPREL:
4865 case elfcpp::R_POWERPC_DTPREL:
4867 Reloc::addr64(view, value);
4869 status = Reloc::addr32(view, value, overflow);
4872 case elfcpp::R_PPC64_UADDR64:
4873 Reloc::addr64_u(view, value);
4876 case elfcpp::R_POWERPC_ADDR32:
4877 status = Reloc::addr32(view, value, overflow);
4880 case elfcpp::R_POWERPC_REL32:
4881 case elfcpp::R_POWERPC_UADDR32:
4882 status = Reloc::addr32_u(view, value, overflow);
4885 case elfcpp::R_POWERPC_ADDR24:
4886 case elfcpp::R_POWERPC_REL24:
4887 case elfcpp::R_PPC_PLTREL24:
4888 case elfcpp::R_PPC_LOCAL24PC:
4889 status = Reloc::addr24(view, value, overflow);
4892 case elfcpp::R_POWERPC_GOT_DTPREL16:
4893 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
4896 status = Reloc::addr16_ds(view, value, overflow);
4899 case elfcpp::R_POWERPC_ADDR16:
4900 case elfcpp::R_POWERPC_REL16:
4901 case elfcpp::R_PPC64_TOC16:
4902 case elfcpp::R_POWERPC_GOT16:
4903 case elfcpp::R_POWERPC_SECTOFF:
4904 case elfcpp::R_POWERPC_TPREL16:
4905 case elfcpp::R_POWERPC_DTPREL16:
4906 case elfcpp::R_POWERPC_GOT_TLSGD16:
4907 case elfcpp::R_POWERPC_GOT_TLSLD16:
4908 case elfcpp::R_POWERPC_GOT_TPREL16:
4909 case elfcpp::R_POWERPC_ADDR16_LO:
4910 case elfcpp::R_POWERPC_REL16_LO:
4911 case elfcpp::R_PPC64_TOC16_LO:
4912 case elfcpp::R_POWERPC_GOT16_LO:
4913 case elfcpp::R_POWERPC_SECTOFF_LO:
4914 case elfcpp::R_POWERPC_TPREL16_LO:
4915 case elfcpp::R_POWERPC_DTPREL16_LO:
4916 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
4917 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
4918 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
4919 status = Reloc::addr16(view, value, overflow);
4922 case elfcpp::R_POWERPC_UADDR16:
4923 status = Reloc::addr16_u(view, value, overflow);
4926 case elfcpp::R_POWERPC_ADDR16_HI:
4927 case elfcpp::R_POWERPC_REL16_HI:
4928 case elfcpp::R_PPC64_TOC16_HI:
4929 case elfcpp::R_POWERPC_GOT16_HI:
4930 case elfcpp::R_POWERPC_SECTOFF_HI:
4931 case elfcpp::R_POWERPC_TPREL16_HI:
4932 case elfcpp::R_POWERPC_DTPREL16_HI:
4933 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
4934 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
4935 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
4936 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
4937 Reloc::addr16_hi(view, value);
4940 case elfcpp::R_POWERPC_ADDR16_HA:
4941 case elfcpp::R_POWERPC_REL16_HA:
4942 case elfcpp::R_PPC64_TOC16_HA:
4943 case elfcpp::R_POWERPC_GOT16_HA:
4944 case elfcpp::R_POWERPC_SECTOFF_HA:
4945 case elfcpp::R_POWERPC_TPREL16_HA:
4946 case elfcpp::R_POWERPC_DTPREL16_HA:
4947 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
4948 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
4949 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
4950 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
4951 Reloc::addr16_ha(view, value);
4954 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4956 // R_PPC_EMB_NADDR16_LO
4958 case elfcpp::R_PPC64_ADDR16_HIGHER:
4959 case elfcpp::R_PPC64_TPREL16_HIGHER:
4960 Reloc::addr16_hi2(view, value);
4963 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4965 // R_PPC_EMB_NADDR16_HI
4967 case elfcpp::R_PPC64_ADDR16_HIGHERA:
4968 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4969 Reloc::addr16_ha2(view, value);
4972 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4974 // R_PPC_EMB_NADDR16_HA
4976 case elfcpp::R_PPC64_ADDR16_HIGHEST:
4977 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4978 Reloc::addr16_hi3(view, value);
4981 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4985 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4986 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4987 Reloc::addr16_ha3(view, value);
4990 case elfcpp::R_PPC64_DTPREL16_DS:
4991 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4993 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
4995 case elfcpp::R_PPC64_TPREL16_DS:
4996 case elfcpp::R_PPC64_TPREL16_LO_DS:
4998 // R_PPC_TLSGD, R_PPC_TLSLD
5000 case elfcpp::R_PPC64_ADDR16_DS:
5001 case elfcpp::R_PPC64_ADDR16_LO_DS:
5002 case elfcpp::R_PPC64_TOC16_DS:
5003 case elfcpp::R_PPC64_TOC16_LO_DS:
5004 case elfcpp::R_PPC64_GOT16_DS:
5005 case elfcpp::R_PPC64_GOT16_LO_DS:
5006 case elfcpp::R_PPC64_SECTOFF_DS:
5007 case elfcpp::R_PPC64_SECTOFF_LO_DS:
5008 status = Reloc::addr16_ds(view, value, overflow);
5011 case elfcpp::R_POWERPC_ADDR14:
5012 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5013 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5014 case elfcpp::R_POWERPC_REL14:
5015 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5016 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5017 status = Reloc::addr14(view, value, overflow);
5020 case elfcpp::R_POWERPC_COPY:
5021 case elfcpp::R_POWERPC_GLOB_DAT:
5022 case elfcpp::R_POWERPC_JMP_SLOT:
5023 case elfcpp::R_POWERPC_RELATIVE:
5024 case elfcpp::R_POWERPC_DTPMOD:
5025 case elfcpp::R_PPC64_JMP_IREL:
5026 case elfcpp::R_POWERPC_IRELATIVE:
5027 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5028 _("unexpected reloc %u in object file"),
5032 case elfcpp::R_PPC_EMB_SDA21:
5037 // R_PPC64_TOCSAVE. For the time being this can be ignored.
5041 case elfcpp::R_PPC_EMB_SDA2I16:
5042 case elfcpp::R_PPC_EMB_SDA2REL:
5045 // R_PPC64_TLSGD, R_PPC64_TLSLD
5048 case elfcpp::R_POWERPC_PLT32:
5049 case elfcpp::R_POWERPC_PLTREL32:
5050 case elfcpp::R_POWERPC_PLT16_LO:
5051 case elfcpp::R_POWERPC_PLT16_HI:
5052 case elfcpp::R_POWERPC_PLT16_HA:
5053 case elfcpp::R_PPC_SDAREL16:
5054 case elfcpp::R_POWERPC_ADDR30:
5055 case elfcpp::R_PPC64_PLT64:
5056 case elfcpp::R_PPC64_PLTREL64:
5057 case elfcpp::R_PPC64_PLTGOT16:
5058 case elfcpp::R_PPC64_PLTGOT16_LO:
5059 case elfcpp::R_PPC64_PLTGOT16_HI:
5060 case elfcpp::R_PPC64_PLTGOT16_HA:
5061 case elfcpp::R_PPC64_PLT16_LO_DS:
5062 case elfcpp::R_PPC64_PLTGOT16_DS:
5063 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
5064 case elfcpp::R_PPC_EMB_RELSEC16:
5065 case elfcpp::R_PPC_EMB_RELST_LO:
5066 case elfcpp::R_PPC_EMB_RELST_HI:
5067 case elfcpp::R_PPC_EMB_RELST_HA:
5068 case elfcpp::R_PPC_EMB_BIT_FLD:
5069 case elfcpp::R_PPC_EMB_RELSDA:
5070 case elfcpp::R_PPC_TOC16:
5073 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5074 _("unsupported reloc %u"),
5078 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
5079 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5080 _("relocation overflow"));
5085 // Relocate section data.
5087 template<int size, bool big_endian>
5089 Target_powerpc<size, big_endian>::relocate_section(
5090 const Relocate_info<size, big_endian>* relinfo,
5091 unsigned int sh_type,
5092 const unsigned char* prelocs,
5094 Output_section* output_section,
5095 bool needs_special_offset_handling,
5096 unsigned char* view,
5098 section_size_type view_size,
5099 const Reloc_symbol_changes* reloc_symbol_changes)
5101 typedef Target_powerpc<size, big_endian> Powerpc;
5102 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
5103 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
5104 Powerpc_comdat_behavior;
5106 gold_assert(sh_type == elfcpp::SHT_RELA);
5108 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
5109 Powerpc_relocate, Powerpc_comdat_behavior>(
5115 needs_special_offset_handling,
5119 reloc_symbol_changes);
5122 class Powerpc_scan_relocatable_reloc
5125 // Return the strategy to use for a local symbol which is not a
5126 // section symbol, given the relocation type.
5127 inline Relocatable_relocs::Reloc_strategy
5128 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
5130 if (r_type == 0 && r_sym == 0)
5131 return Relocatable_relocs::RELOC_DISCARD;
5132 return Relocatable_relocs::RELOC_COPY;
5135 // Return the strategy to use for a local symbol which is a section
5136 // symbol, given the relocation type.
5137 inline Relocatable_relocs::Reloc_strategy
5138 local_section_strategy(unsigned int, Relobj*)
5140 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
5143 // Return the strategy to use for a global symbol, given the
5144 // relocation type, the object, and the symbol index.
5145 inline Relocatable_relocs::Reloc_strategy
5146 global_strategy(unsigned int r_type, Relobj*, unsigned int)
5148 if (r_type == elfcpp::R_PPC_PLTREL24)
5149 return Relocatable_relocs::RELOC_SPECIAL;
5150 return Relocatable_relocs::RELOC_COPY;
5154 // Scan the relocs during a relocatable link.
5156 template<int size, bool big_endian>
5158 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
5159 Symbol_table* symtab,
5161 Sized_relobj_file<size, big_endian>* object,
5162 unsigned int data_shndx,
5163 unsigned int sh_type,
5164 const unsigned char* prelocs,
5166 Output_section* output_section,
5167 bool needs_special_offset_handling,
5168 size_t local_symbol_count,
5169 const unsigned char* plocal_symbols,
5170 Relocatable_relocs* rr)
5172 gold_assert(sh_type == elfcpp::SHT_RELA);
5174 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
5175 Powerpc_scan_relocatable_reloc>(
5183 needs_special_offset_handling,
5189 // Emit relocations for a section.
5190 // This is a modified version of the function by the same name in
5191 // target-reloc.h. Using relocate_special_relocatable for
5192 // R_PPC_PLTREL24 would require duplication of the entire body of the
5193 // loop, so we may as well duplicate the whole thing.
5195 template<int size, bool big_endian>
5197 Target_powerpc<size, big_endian>::relocate_relocs(
5198 const Relocate_info<size, big_endian>* relinfo,
5199 unsigned int sh_type,
5200 const unsigned char* prelocs,
5202 Output_section* output_section,
5203 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
5204 const Relocatable_relocs* rr,
5206 Address view_address,
5208 unsigned char* reloc_view,
5209 section_size_type reloc_view_size)
5211 gold_assert(sh_type == elfcpp::SHT_RELA);
5213 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
5215 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
5217 const int reloc_size
5218 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
5220 Powerpc_relobj<size, big_endian>* const object
5221 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
5222 const unsigned int local_count = object->local_symbol_count();
5223 unsigned int got2_shndx = object->got2_shndx();
5224 Address got2_addend = 0;
5225 if (got2_shndx != 0)
5227 got2_addend = object->get_output_section_offset(got2_shndx);
5228 gold_assert(got2_addend != invalid_address);
5231 unsigned char* pwrite = reloc_view;
5232 bool zap_next = false;
5233 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
5235 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
5236 if (strategy == Relocatable_relocs::RELOC_DISCARD)
5239 Reltype reloc(prelocs);
5240 Reltype_write reloc_write(pwrite);
5242 Address offset = reloc.get_r_offset();
5243 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
5244 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
5245 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
5246 const unsigned int orig_r_sym = r_sym;
5247 typename elfcpp::Elf_types<size>::Elf_Swxword addend
5248 = reloc.get_r_addend();
5249 const Symbol* gsym = NULL;
5253 // We could arrange to discard these and other relocs for
5254 // tls optimised sequences in the strategy methods, but for
5255 // now do as BFD ld does.
5256 r_type = elfcpp::R_POWERPC_NONE;
5260 // Get the new symbol index.
5261 if (r_sym < local_count)
5265 case Relocatable_relocs::RELOC_COPY:
5266 case Relocatable_relocs::RELOC_SPECIAL:
5269 r_sym = object->symtab_index(r_sym);
5270 gold_assert(r_sym != -1U);
5274 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
5276 // We are adjusting a section symbol. We need to find
5277 // the symbol table index of the section symbol for
5278 // the output section corresponding to input section
5279 // in which this symbol is defined.
5280 gold_assert(r_sym < local_count);
5282 unsigned int shndx =
5283 object->local_symbol_input_shndx(r_sym, &is_ordinary);
5284 gold_assert(is_ordinary);
5285 Output_section* os = object->output_section(shndx);
5286 gold_assert(os != NULL);
5287 gold_assert(os->needs_symtab_index());
5288 r_sym = os->symtab_index();
5298 gsym = object->global_symbol(r_sym);
5299 gold_assert(gsym != NULL);
5300 if (gsym->is_forwarder())
5301 gsym = relinfo->symtab->resolve_forwards(gsym);
5303 gold_assert(gsym->has_symtab_index());
5304 r_sym = gsym->symtab_index();
5307 // Get the new offset--the location in the output section where
5308 // this relocation should be applied.
5309 if (static_cast<Address>(offset_in_output_section) != invalid_address)
5310 offset += offset_in_output_section;
5313 section_offset_type sot_offset =
5314 convert_types<section_offset_type, Address>(offset);
5315 section_offset_type new_sot_offset =
5316 output_section->output_offset(object, relinfo->data_shndx,
5318 gold_assert(new_sot_offset != -1);
5319 offset = new_sot_offset;
5322 // In an object file, r_offset is an offset within the section.
5323 // In an executable or dynamic object, generated by
5324 // --emit-relocs, r_offset is an absolute address.
5325 if (!parameters->options().relocatable())
5327 offset += view_address;
5328 if (static_cast<Address>(offset_in_output_section) != invalid_address)
5329 offset -= offset_in_output_section;
5332 // Handle the reloc addend based on the strategy.
5333 if (strategy == Relocatable_relocs::RELOC_COPY)
5335 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
5337 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
5338 addend = psymval->value(object, addend);
5340 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
5342 if (addend >= 32768)
5343 addend += got2_addend;
5348 if (!parameters->options().relocatable())
5350 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5351 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
5352 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
5353 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
5355 // First instruction of a global dynamic sequence,
5357 const bool final = gsym == NULL || gsym->final_value_is_known();
5358 switch (this->optimize_tls_gd(final))
5360 case tls::TLSOPT_TO_IE:
5361 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
5362 - elfcpp::R_POWERPC_GOT_TLSGD16);
5364 case tls::TLSOPT_TO_LE:
5365 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5366 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
5367 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5370 r_type = elfcpp::R_POWERPC_NONE;
5371 offset -= 2 * big_endian;
5378 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5379 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
5380 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
5381 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
5383 // First instruction of a local dynamic sequence,
5385 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
5387 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5388 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
5390 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5391 const Output_section* os = relinfo->layout->tls_segment()
5393 gold_assert(os != NULL);
5394 gold_assert(os->needs_symtab_index());
5395 r_sym = os->symtab_index();
5396 addend = dtp_offset;
5400 r_type = elfcpp::R_POWERPC_NONE;
5401 offset -= 2 * big_endian;
5405 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5406 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
5407 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
5408 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
5410 // First instruction of initial exec sequence.
5411 const bool final = gsym == NULL || gsym->final_value_is_known();
5412 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
5414 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5415 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
5416 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5419 r_type = elfcpp::R_POWERPC_NONE;
5420 offset -= 2 * big_endian;
5424 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5425 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5427 // Second instruction of a global dynamic sequence,
5428 // the __tls_get_addr call
5429 const bool final = gsym == NULL || gsym->final_value_is_known();
5430 switch (this->optimize_tls_gd(final))
5432 case tls::TLSOPT_TO_IE:
5433 r_type = elfcpp::R_POWERPC_NONE;
5436 case tls::TLSOPT_TO_LE:
5437 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5438 offset += 2 * big_endian;
5445 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5446 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5448 // Second instruction of a local dynamic sequence,
5449 // the __tls_get_addr call
5450 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
5452 const Output_section* os = relinfo->layout->tls_segment()
5454 gold_assert(os != NULL);
5455 gold_assert(os->needs_symtab_index());
5456 r_sym = os->symtab_index();
5457 addend = dtp_offset;
5458 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5459 offset += 2 * big_endian;
5463 else if (r_type == elfcpp::R_POWERPC_TLS)
5465 // Second instruction of an initial exec sequence
5466 const bool final = gsym == NULL || gsym->final_value_is_known();
5467 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
5469 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5470 offset += 2 * big_endian;
5475 reloc_write.put_r_offset(offset);
5476 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
5477 reloc_write.put_r_addend(addend);
5479 pwrite += reloc_size;
5482 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
5483 == reloc_view_size);
5486 // Return the value to use for a dynamic which requires special
5487 // treatment. This is how we support equality comparisons of function
5488 // pointers across shared library boundaries, as described in the
5489 // processor specific ABI supplement.
5491 template<int size, bool big_endian>
5493 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
5497 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5498 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5499 unsigned int glink_index = glink->find_entry(gsym);
5500 return glink->address() + glink_index * glink->glink_entry_size();
5506 // Return the PLT address to use for a local symbol.
5507 template<int size, bool big_endian>
5509 Target_powerpc<size, big_endian>::do_plt_address_for_local(
5510 const Relobj* object,
5511 unsigned int symndx) const
5515 const Sized_relobj<size, big_endian>* relobj
5516 = static_cast<const Sized_relobj<size, big_endian>*>(object);
5517 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5518 unsigned int glink_index = glink->find_entry(relobj->sized_relobj(),
5520 return glink->address() + glink_index * glink->glink_entry_size();
5526 // Return the PLT address to use for a global symbol.
5527 template<int size, bool big_endian>
5529 Target_powerpc<size, big_endian>::do_plt_address_for_global(
5530 const Symbol* gsym) const
5534 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5535 unsigned int glink_index = glink->find_entry(gsym);
5536 return glink->address() + glink_index * glink->glink_entry_size();
5542 // Return the offset to use for the GOT_INDX'th got entry which is
5543 // for a local tls symbol specified by OBJECT, SYMNDX.
5544 template<int size, bool big_endian>
5546 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
5547 const Relobj* object,
5548 unsigned int symndx,
5549 unsigned int got_indx) const
5551 const Powerpc_relobj<size, big_endian>* ppc_object
5552 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
5553 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
5555 for (Got_type got_type = GOT_TYPE_TLSGD;
5556 got_type <= GOT_TYPE_TPREL;
5557 got_type = Got_type(got_type + 1))
5558 if (ppc_object->local_has_got_offset(symndx, got_type))
5560 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
5561 if (got_type == GOT_TYPE_TLSGD)
5563 if (off == got_indx * (size / 8))
5565 if (got_type == GOT_TYPE_TPREL)
5575 // Return the offset to use for the GOT_INDX'th got entry which is
5576 // for global tls symbol GSYM.
5577 template<int size, bool big_endian>
5579 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
5581 unsigned int got_indx) const
5583 if (gsym->type() == elfcpp::STT_TLS)
5585 for (Got_type got_type = GOT_TYPE_TLSGD;
5586 got_type <= GOT_TYPE_TPREL;
5587 got_type = Got_type(got_type + 1))
5588 if (gsym->has_got_offset(got_type))
5590 unsigned int off = gsym->got_offset(got_type);
5591 if (got_type == GOT_TYPE_TLSGD)
5593 if (off == got_indx * (size / 8))
5595 if (got_type == GOT_TYPE_TPREL)
5605 // The selector for powerpc object files.
5607 template<int size, bool big_endian>
5608 class Target_selector_powerpc : public Target_selector
5611 Target_selector_powerpc()
5612 : Target_selector(elfcpp::EM_NONE, size, big_endian,
5614 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
5615 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
5617 ? (big_endian ? "elf64ppc" : "elf64lppc")
5618 : (big_endian ? "elf32ppc" : "elf32lppc")))
5622 do_recognize(Input_file*, off_t, int machine, int, int)
5627 if (machine != elfcpp::EM_PPC64)
5632 if (machine != elfcpp::EM_PPC)
5640 return this->instantiate_target();
5644 do_instantiate_target()
5645 { return new Target_powerpc<size, big_endian>(); }
5648 Target_selector_powerpc<32, true> target_selector_ppc32;
5649 Target_selector_powerpc<32, false> target_selector_ppc32le;
5650 Target_selector_powerpc<64, true> target_selector_ppc64;
5651 Target_selector_powerpc<64, false> target_selector_ppc64le;
5653 } // End anonymous namespace.