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 typename elfcpp::Elf_types<size>::Elf_Off Offset;
62 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
63 typedef Unordered_map<Address, Section_refs> Access_from;
65 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
66 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
67 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
68 special_(0), opd_valid_(false), opd_ent_(), access_from_map_()
74 // The .got2 section shndx.
79 return this->special_;
84 // The .opd section shndx.
91 return this->special_;
94 // Init OPD entry arrays.
96 init_opd(size_t opd_size)
98 size_t count = this->opd_ent_ndx(opd_size);
99 this->opd_ent_.resize(count);
102 // Return section and offset of function entry for .opd + R_OFF.
104 get_opd_ent(Address r_off, Address* value = NULL) const
106 size_t ndx = this->opd_ent_ndx(r_off);
107 gold_assert(ndx < this->opd_ent_.size());
108 gold_assert(this->opd_ent_[ndx].shndx != 0);
110 *value = this->opd_ent_[ndx].off;
111 return this->opd_ent_[ndx].shndx;
114 // Set section and offset of function entry for .opd + R_OFF.
116 set_opd_ent(Address r_off, unsigned int shndx, Address value)
118 size_t ndx = this->opd_ent_ndx(r_off);
119 gold_assert(ndx < this->opd_ent_.size());
120 this->opd_ent_[ndx].shndx = shndx;
121 this->opd_ent_[ndx].off = value;
124 // Return discard flag for .opd + R_OFF.
126 get_opd_discard(Address r_off) const
128 size_t ndx = this->opd_ent_ndx(r_off);
129 gold_assert(ndx < this->opd_ent_.size());
130 return this->opd_ent_[ndx].discard;
133 // Set discard flag for .opd + R_OFF.
135 set_opd_discard(Address r_off)
137 size_t ndx = this->opd_ent_ndx(r_off);
138 gold_assert(ndx < this->opd_ent_.size());
139 this->opd_ent_[ndx].discard = true;
144 { return &this->access_from_map_; }
146 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
147 // section at DST_OFF.
149 add_reference(Object* src_obj,
150 unsigned int src_indx,
151 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
153 Section_id src_id(src_obj, src_indx);
154 this->access_from_map_[dst_off].insert(src_id);
157 // Add a reference to the code section specified by the .opd entry
160 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
162 size_t ndx = this->opd_ent_ndx(dst_off);
163 if (ndx >= this->opd_ent_.size())
164 this->opd_ent_.resize(ndx + 1);
165 this->opd_ent_[ndx].gc_mark = true;
169 process_gc_mark(Symbol_table* symtab)
171 for (size_t i = 0; i < this->opd_ent_.size(); i++)
172 if (this->opd_ent_[i].gc_mark)
174 unsigned int shndx = this->opd_ent_[i].shndx;
175 symtab->gc()->worklist().push(Section_id(this, shndx));
181 { return this->opd_valid_; }
185 { this->opd_valid_ = true; }
187 // Examine .rela.opd to build info about function entry points.
189 scan_opd_relocs(size_t reloc_count,
190 const unsigned char* prelocs,
191 const unsigned char* plocal_syms);
194 do_read_relocs(Read_relocs_data*);
197 do_find_special_sections(Read_symbols_data* sd);
199 // Adjust this local symbol value. Return false if the symbol
200 // should be discarded from the output file.
202 do_adjust_local_symbol(Symbol_value<size>* lv) const
204 if (size == 64 && this->opd_shndx() != 0)
207 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
209 if (this->get_opd_discard(lv->input_value()))
215 // Return offset in output GOT section that this object will use
216 // as a TOC pointer. Won't be just a constant with multi-toc support.
218 toc_base_offset() const
230 // Return index into opd_ent_ array for .opd entry at OFF.
231 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
232 // apart when the language doesn't use the last 8-byte word, the
233 // environment pointer. Thus dividing the entry section offset by
234 // 16 will give an index into opd_ent_ that works for either layout
235 // of .opd. (It leaves some elements of the vector unused when .opd
236 // entries are spaced 24 bytes apart, but we don't know the spacing
237 // until relocations are processed, and in any case it is possible
238 // for an object to have some entries spaced 16 bytes apart and
239 // others 24 bytes apart.)
241 opd_ent_ndx(size_t off) const
244 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
245 unsigned int special_;
247 // Set at the start of gc_process_relocs, when we know opd_ent_
248 // vector is valid. The flag could be made atomic and set in
249 // do_read_relocs with memory_order_release and then tested with
250 // memory_order_acquire, potentially resulting in fewer entries in
254 // The first 8-byte word of an OPD entry gives the address of the
255 // entry point of the function. Relocatable object files have a
256 // relocation on this word. The following vector records the
257 // section and offset specified by these relocations.
258 std::vector<Opd_ent> opd_ent_;
260 // References made to this object's .opd section when running
261 // gc_process_relocs for another object, before the opd_ent_ vector
262 // is valid for this object.
263 Access_from access_from_map_;
266 template<int size, bool big_endian>
267 class Target_powerpc : public Sized_target<size, big_endian>
271 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
272 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
273 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
274 static const Address invalid_address = static_cast<Address>(0) - 1;
275 // Offset of tp and dtp pointers from start of TLS block.
276 static const Address tp_offset = 0x7000;
277 static const Address dtp_offset = 0x8000;
280 : Sized_target<size, big_endian>(&powerpc_info),
281 got_(NULL), plt_(NULL), iplt_(NULL), glink_(NULL), rela_dyn_(NULL),
282 copy_relocs_(elfcpp::R_POWERPC_COPY),
283 dynbss_(NULL), tlsld_got_offset_(-1U)
287 // Process the relocations to determine unreferenced sections for
288 // garbage collection.
290 gc_process_relocs(Symbol_table* symtab,
292 Sized_relobj_file<size, big_endian>* object,
293 unsigned int data_shndx,
294 unsigned int sh_type,
295 const unsigned char* prelocs,
297 Output_section* output_section,
298 bool needs_special_offset_handling,
299 size_t local_symbol_count,
300 const unsigned char* plocal_symbols);
302 // Scan the relocations to look for symbol adjustments.
304 scan_relocs(Symbol_table* symtab,
306 Sized_relobj_file<size, big_endian>* object,
307 unsigned int data_shndx,
308 unsigned int sh_type,
309 const unsigned char* prelocs,
311 Output_section* output_section,
312 bool needs_special_offset_handling,
313 size_t local_symbol_count,
314 const unsigned char* plocal_symbols);
316 // Map input .toc section to output .got section.
318 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
320 if (size == 64 && strcmp(name, ".toc") == 0)
328 // Provide linker defined save/restore functions.
330 define_save_restore_funcs(Layout*, Symbol_table*);
332 // Finalize the sections.
334 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
336 // Return the value to use for a dynamic which requires special
339 do_dynsym_value(const Symbol*) const;
341 // Return the PLT address to use for a local symbol.
343 do_plt_address_for_local(const Relobj*, unsigned int) const;
345 // Return the PLT address to use for a global symbol.
347 do_plt_address_for_global(const Symbol*) const;
349 // Return the offset to use for the GOT_INDX'th got entry which is
350 // for a local tls symbol specified by OBJECT, SYMNDX.
352 do_tls_offset_for_local(const Relobj* object,
354 unsigned int got_indx) const;
356 // Return the offset to use for the GOT_INDX'th got entry which is
357 // for global tls symbol GSYM.
359 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
361 // Relocate a section.
363 relocate_section(const Relocate_info<size, big_endian>*,
364 unsigned int sh_type,
365 const unsigned char* prelocs,
367 Output_section* output_section,
368 bool needs_special_offset_handling,
370 Address view_address,
371 section_size_type view_size,
372 const Reloc_symbol_changes*);
374 // Scan the relocs during a relocatable link.
376 scan_relocatable_relocs(Symbol_table* symtab,
378 Sized_relobj_file<size, big_endian>* object,
379 unsigned int data_shndx,
380 unsigned int sh_type,
381 const unsigned char* prelocs,
383 Output_section* output_section,
384 bool needs_special_offset_handling,
385 size_t local_symbol_count,
386 const unsigned char* plocal_symbols,
387 Relocatable_relocs*);
389 // Emit relocations for a section.
391 relocate_relocs(const Relocate_info<size, big_endian>*,
392 unsigned int sh_type,
393 const unsigned char* prelocs,
395 Output_section* output_section,
396 typename elfcpp::Elf_types<size>::Elf_Off
397 offset_in_output_section,
398 const Relocatable_relocs*,
400 Address view_address,
402 unsigned char* reloc_view,
403 section_size_type reloc_view_size);
405 // Return whether SYM is defined by the ABI.
407 do_is_defined_by_abi(const Symbol* sym) const
409 return strcmp(sym->name(), "__tls_get_addr") == 0;
412 // Return the size of the GOT section.
416 gold_assert(this->got_ != NULL);
417 return this->got_->data_size();
420 // Get the PLT section.
421 const Output_data_plt_powerpc<size, big_endian>*
424 gold_assert(this->plt_ != NULL);
428 // Get the IPLT section.
429 const Output_data_plt_powerpc<size, big_endian>*
432 gold_assert(this->iplt_ != NULL);
436 // Get the .glink section.
437 const Output_data_glink<size, big_endian>*
438 glink_section() const
440 gold_assert(this->glink_ != NULL);
444 // Get the GOT section.
445 const Output_data_got_powerpc<size, big_endian>*
448 gold_assert(this->got_ != NULL);
453 do_make_elf_object(const std::string&, Input_file*, off_t,
454 const elfcpp::Ehdr<size, big_endian>&);
456 // Return the number of entries in the GOT.
458 got_entry_count() const
460 if (this->got_ == NULL)
462 return this->got_size() / (size / 8);
465 // Return the number of entries in the PLT.
467 plt_entry_count() const;
469 // Return the offset of the first non-reserved PLT entry.
471 first_plt_entry_offset() const;
473 // Return the size of each PLT entry.
475 plt_entry_size() const;
477 // Add any special sections for this symbol to the gc work list.
478 // For powerpc64, this adds the code section of a function
481 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
483 // Handle target specific gc actions when adding a gc reference from
484 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
485 // and DST_OFF. For powerpc64, this adds a referenc to the code
486 // section of a function descriptor.
488 do_gc_add_reference(Symbol_table* symtab,
490 unsigned int src_shndx,
492 unsigned int dst_shndx,
493 Address dst_off) const;
497 // The class which scans relocations.
501 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
504 : issued_non_pic_error_(false)
508 get_reference_flags(unsigned int r_type);
511 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
512 Sized_relobj_file<size, big_endian>* object,
513 unsigned int data_shndx,
514 Output_section* output_section,
515 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
516 const elfcpp::Sym<size, big_endian>& lsym,
520 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
521 Sized_relobj_file<size, big_endian>* object,
522 unsigned int data_shndx,
523 Output_section* output_section,
524 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
528 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
530 Sized_relobj_file<size, big_endian>* ,
533 const elfcpp::Rela<size, big_endian>& ,
535 const elfcpp::Sym<size, big_endian>&)
539 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
541 Sized_relobj_file<size, big_endian>* ,
544 const elfcpp::Rela<size,
546 unsigned int , Symbol*)
551 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
552 unsigned int r_type);
555 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
556 unsigned int r_type, Symbol*);
559 generate_tls_call(Symbol_table* symtab, Layout* layout,
560 Target_powerpc* target);
563 check_non_pic(Relobj*, unsigned int r_type);
566 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>* object,
567 unsigned int r_type);
569 // Whether we have issued an error about a non-PIC compilation.
570 bool issued_non_pic_error_;
574 symval_for_branch(Address value, const Sized_symbol<size>* gsym,
575 Powerpc_relobj<size, big_endian>* object,
576 unsigned int *dest_shndx);
578 // The class which implements relocation.
582 // Use 'at' branch hints when true, 'y' when false.
583 // FIXME maybe: set this with an option.
584 static const bool is_isa_v2 = true;
588 CALL_NOT_EXPECTED = 0,
594 : call_tls_get_addr_(CALL_NOT_EXPECTED)
599 if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
601 // FIXME: This needs to specify the location somehow.
602 gold_error(_("missing expected __tls_get_addr call"));
606 // Do a relocation. Return false if the caller should not issue
607 // any warnings about this relocation.
609 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
610 Output_section*, size_t relnum,
611 const elfcpp::Rela<size, big_endian>&,
612 unsigned int r_type, const Sized_symbol<size>*,
613 const Symbol_value<size>*,
615 typename elfcpp::Elf_types<size>::Elf_Addr,
618 // This is set if we should skip the next reloc, which should be a
619 // call to __tls_get_addr.
620 enum skip_tls call_tls_get_addr_;
623 class Relocate_comdat_behavior
626 // Decide what the linker should do for relocations that refer to
627 // discarded comdat sections.
628 inline Comdat_behavior
629 get(const char* name)
631 gold::Default_comdat_behavior default_behavior;
632 Comdat_behavior ret = default_behavior.get(name);
633 if (ret == CB_WARNING)
636 && (strcmp(name, ".fixup") == 0
637 || strcmp(name, ".got2") == 0))
640 && (strcmp(name, ".opd") == 0
641 || strcmp(name, ".toc") == 0
642 || strcmp(name, ".toc1") == 0))
649 // A class which returns the size required for a relocation type,
650 // used while scanning relocs during a relocatable link.
651 class Relocatable_size_for_reloc
655 get_size_for_reloc(unsigned int, Relobj*)
662 // Optimize the TLS relocation type based on what we know about the
663 // symbol. IS_FINAL is true if the final address of this symbol is
664 // known at link time.
666 tls::Tls_optimization
667 optimize_tls_gd(bool is_final)
669 // If we are generating a shared library, then we can't do anything
671 if (parameters->options().shared())
672 return tls::TLSOPT_NONE;
675 return tls::TLSOPT_TO_IE;
676 return tls::TLSOPT_TO_LE;
679 tls::Tls_optimization
682 if (parameters->options().shared())
683 return tls::TLSOPT_NONE;
685 return tls::TLSOPT_TO_LE;
688 tls::Tls_optimization
689 optimize_tls_ie(bool is_final)
691 if (!is_final || parameters->options().shared())
692 return tls::TLSOPT_NONE;
694 return tls::TLSOPT_TO_LE;
697 // Get the GOT section, creating it if necessary.
698 Output_data_got_powerpc<size, big_endian>*
699 got_section(Symbol_table*, Layout*);
703 make_glink_section(Layout*);
705 // Create the PLT section.
707 make_plt_section(Layout*);
710 make_iplt_section(Layout*);
712 // Create a PLT entry for a global symbol.
714 make_plt_entry(Layout*, Symbol*,
715 const elfcpp::Rela<size, big_endian>&,
716 const Sized_relobj_file<size, big_endian>* object);
718 // Create a PLT entry for a local IFUNC symbol.
720 make_local_ifunc_plt_entry(Layout*,
721 const elfcpp::Rela<size, big_endian>&,
722 Sized_relobj_file<size, big_endian>*);
724 // Create a GOT entry for local dynamic __tls_get_addr.
726 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
727 Sized_relobj_file<size, big_endian>* object);
730 tlsld_got_offset() const
732 return this->tlsld_got_offset_;
735 // Get the dynamic reloc section, creating it if necessary.
737 rela_dyn_section(Layout*);
739 // Copy a relocation against a global symbol.
741 copy_reloc(Symbol_table* symtab, Layout* layout,
742 Sized_relobj_file<size, big_endian>* object,
743 unsigned int shndx, Output_section* output_section,
744 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
746 this->copy_relocs_.copy_reloc(symtab, layout,
747 symtab->get_sized_symbol<size>(sym),
748 object, shndx, output_section,
749 reloc, this->rela_dyn_section(layout));
752 // Information about this specific target which we pass to the
753 // general Target structure.
754 static Target::Target_info powerpc_info;
756 // The types of GOT entries needed for this platform.
757 // These values are exposed to the ABI in an incremental link.
758 // Do not renumber existing values without changing the version
759 // number of the .gnu_incremental_inputs section.
763 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
764 GOT_TYPE_DTPREL, // entry for @got@dtprel
765 GOT_TYPE_TPREL // entry for @got@tprel
768 // The GOT output section.
769 Output_data_got_powerpc<size, big_endian>* got_;
770 // The PLT output section.
771 Output_data_plt_powerpc<size, big_endian>* plt_;
772 // The IPLT output section.
773 Output_data_plt_powerpc<size, big_endian>* iplt_;
774 // The .glink output section.
775 Output_data_glink<size, big_endian>* glink_;
776 // The dynamic reloc output section.
777 Reloc_section* rela_dyn_;
778 // Relocs saved to avoid a COPY reloc.
779 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
780 // Space for variables copied with a COPY reloc.
781 Output_data_space* dynbss_;
782 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
783 unsigned int tlsld_got_offset_;
787 Target::Target_info Target_powerpc<32, true>::powerpc_info =
790 true, // is_big_endian
791 elfcpp::EM_PPC, // machine_code
792 false, // has_make_symbol
793 false, // has_resolve
794 false, // has_code_fill
795 true, // is_default_stack_executable
796 false, // can_icf_inline_merge_sections
798 "/usr/lib/ld.so.1", // dynamic_linker
799 0x10000000, // default_text_segment_address
800 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
801 4 * 1024, // common_pagesize (overridable by -z common-page-size)
802 false, // isolate_execinstr
804 elfcpp::SHN_UNDEF, // small_common_shndx
805 elfcpp::SHN_UNDEF, // large_common_shndx
806 0, // small_common_section_flags
807 0, // large_common_section_flags
808 NULL, // attributes_section
809 NULL // attributes_vendor
813 Target::Target_info Target_powerpc<32, false>::powerpc_info =
816 false, // is_big_endian
817 elfcpp::EM_PPC, // machine_code
818 false, // has_make_symbol
819 false, // has_resolve
820 false, // has_code_fill
821 true, // is_default_stack_executable
822 false, // can_icf_inline_merge_sections
824 "/usr/lib/ld.so.1", // dynamic_linker
825 0x10000000, // default_text_segment_address
826 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
827 4 * 1024, // common_pagesize (overridable by -z common-page-size)
828 false, // isolate_execinstr
830 elfcpp::SHN_UNDEF, // small_common_shndx
831 elfcpp::SHN_UNDEF, // large_common_shndx
832 0, // small_common_section_flags
833 0, // large_common_section_flags
834 NULL, // attributes_section
835 NULL // attributes_vendor
839 Target::Target_info Target_powerpc<64, true>::powerpc_info =
842 true, // is_big_endian
843 elfcpp::EM_PPC64, // machine_code
844 false, // has_make_symbol
845 false, // has_resolve
846 false, // has_code_fill
847 true, // is_default_stack_executable
848 false, // can_icf_inline_merge_sections
850 "/usr/lib/ld.so.1", // dynamic_linker
851 0x10000000, // default_text_segment_address
852 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
853 4 * 1024, // common_pagesize (overridable by -z common-page-size)
854 false, // isolate_execinstr
856 elfcpp::SHN_UNDEF, // small_common_shndx
857 elfcpp::SHN_UNDEF, // large_common_shndx
858 0, // small_common_section_flags
859 0, // large_common_section_flags
860 NULL, // attributes_section
861 NULL // attributes_vendor
865 Target::Target_info Target_powerpc<64, false>::powerpc_info =
868 false, // is_big_endian
869 elfcpp::EM_PPC64, // machine_code
870 false, // has_make_symbol
871 false, // has_resolve
872 false, // has_code_fill
873 true, // is_default_stack_executable
874 false, // can_icf_inline_merge_sections
876 "/usr/lib/ld.so.1", // dynamic_linker
877 0x10000000, // default_text_segment_address
878 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
879 4 * 1024, // common_pagesize (overridable by -z common-page-size)
880 false, // isolate_execinstr
882 elfcpp::SHN_UNDEF, // small_common_shndx
883 elfcpp::SHN_UNDEF, // large_common_shndx
884 0, // small_common_section_flags
885 0, // large_common_section_flags
886 NULL, // attributes_section
887 NULL // attributes_vendor
891 is_branch_reloc(unsigned int r_type)
893 return (r_type == elfcpp::R_POWERPC_REL24
894 || r_type == elfcpp::R_PPC_PLTREL24
895 || r_type == elfcpp::R_PPC_LOCAL24PC
896 || r_type == elfcpp::R_POWERPC_REL14
897 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
898 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
899 || r_type == elfcpp::R_POWERPC_ADDR24
900 || r_type == elfcpp::R_POWERPC_ADDR14
901 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
902 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
905 // If INSN is an opcode that may be used with an @tls operand, return
906 // the transformed insn for TLS optimisation, otherwise return 0. If
907 // REG is non-zero only match an insn with RB or RA equal to REG.
909 at_tls_transform(uint32_t insn, unsigned int reg)
911 if ((insn & (0x3f << 26)) != 31 << 26)
915 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
916 rtra = insn & ((1 << 26) - (1 << 16));
917 else if (((insn >> 16) & 0x1f) == reg)
918 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
922 if ((insn & (0x3ff << 1)) == 266 << 1)
925 else if ((insn & (0x1f << 1)) == 23 << 1
926 && ((insn & (0x1f << 6)) < 14 << 6
927 || ((insn & (0x1f << 6)) >= 16 << 6
928 && (insn & (0x1f << 6)) < 24 << 6)))
929 // load and store indexed -> dform
930 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
931 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
932 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
933 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
934 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
936 insn = (58 << 26) | 2;
943 // Modified version of symtab.h class Symbol member
944 // Given a direct absolute or pc-relative static relocation against
945 // the global symbol, this function returns whether a dynamic relocation
950 needs_dynamic_reloc(const Symbol* gsym, int flags)
952 // No dynamic relocations in a static link!
953 if (parameters->doing_static_link())
956 // A reference to an undefined symbol from an executable should be
957 // statically resolved to 0, and does not need a dynamic relocation.
958 // This matches gnu ld behavior.
959 if (gsym->is_undefined() && !parameters->options().shared())
962 // A reference to an absolute symbol does not need a dynamic relocation.
963 if (gsym->is_absolute())
966 // An absolute reference within a position-independent output file
967 // will need a dynamic relocation.
968 if ((flags & Symbol::ABSOLUTE_REF)
969 && parameters->options().output_is_position_independent())
972 // A function call that can branch to a local PLT entry does not need
973 // a dynamic relocation.
974 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
977 // A reference to any PLT entry in a non-position-independent executable
978 // does not need a dynamic relocation.
979 // Except due to having function descriptors on powerpc64 we don't define
980 // functions to their plt code in an executable, so this doesn't apply.
982 && !parameters->options().output_is_position_independent()
983 && gsym->has_plt_offset())
986 // A reference to a symbol defined in a dynamic object or to a
987 // symbol that is preemptible will need a dynamic relocation.
988 if (gsym->is_from_dynobj()
989 || gsym->is_undefined()
990 || gsym->is_preemptible())
993 // For all other cases, return FALSE.
997 // Modified version of symtab.h class Symbol member
998 // Whether we should use the PLT offset associated with a symbol for
999 // a relocation. FLAGS is a set of Reference_flags.
1003 use_plt_offset(const Symbol* gsym, int flags)
1005 // If the symbol doesn't have a PLT offset, then naturally we
1006 // don't want to use it.
1007 if (!gsym->has_plt_offset())
1010 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
1011 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1014 // If we are going to generate a dynamic relocation, then we will
1015 // wind up using that, so no need to use the PLT entry.
1016 if (needs_dynamic_reloc<size>(gsym, flags))
1019 // If the symbol is from a dynamic object, we need to use the PLT
1021 if (gsym->is_from_dynobj())
1024 // If we are generating a shared object, and gsym symbol is
1025 // undefined or preemptible, we need to use the PLT entry.
1026 if (parameters->options().shared()
1027 && (gsym->is_undefined() || gsym->is_preemptible()))
1030 // If gsym is a call to a weak undefined symbol, we need to use
1031 // the PLT entry; the symbol may be defined by a library loaded
1033 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
1036 // Otherwise we can use the regular definition.
1040 template<int size, bool big_endian>
1041 class Powerpc_relocate_functions
1058 typedef Powerpc_relocate_functions<size, big_endian> This;
1059 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1061 template<int valsize>
1063 has_overflow_signed(Address value)
1065 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1066 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1067 limit <<= ((valsize - 1) >> 1);
1068 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1069 return value + limit > (limit << 1) - 1;
1072 template<int valsize>
1074 has_overflow_bitfield(Address value)
1076 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1077 limit <<= ((valsize - 1) >> 1);
1078 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1079 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
1082 template<int valsize>
1083 static inline Status
1084 overflowed(Address value, Overflow_check overflow)
1086 if (overflow == CHECK_SIGNED)
1088 if (has_overflow_signed<valsize>(value))
1089 return STATUS_OVERFLOW;
1091 else if (overflow == CHECK_BITFIELD)
1093 if (has_overflow_bitfield<valsize>(value))
1094 return STATUS_OVERFLOW;
1099 // Do a simple RELA relocation
1100 template<int valsize>
1101 static inline Status
1102 rela(unsigned char* view, Address value, Overflow_check overflow)
1104 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1105 Valtype* wv = reinterpret_cast<Valtype*>(view);
1106 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
1107 return overflowed<valsize>(value, overflow);
1110 template<int valsize>
1111 static inline Status
1112 rela(unsigned char* view,
1113 unsigned int right_shift,
1114 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1116 Overflow_check overflow)
1118 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1119 Valtype* wv = reinterpret_cast<Valtype*>(view);
1120 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1121 Valtype reloc = value >> right_shift;
1124 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
1125 return overflowed<valsize>(value >> right_shift, overflow);
1128 // Do a simple RELA relocation, unaligned.
1129 template<int valsize>
1130 static inline Status
1131 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1133 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
1134 return overflowed<valsize>(value, overflow);
1137 template<int valsize>
1138 static inline Status
1139 rela_ua(unsigned char* view,
1140 unsigned int right_shift,
1141 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1143 Overflow_check overflow)
1145 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1147 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
1148 Valtype reloc = value >> right_shift;
1151 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
1152 return overflowed<valsize>(value >> right_shift, overflow);
1156 // R_PPC64_ADDR64: (Symbol + Addend)
1158 addr64(unsigned char* view, Address value)
1159 { This::template rela<64>(view, value, CHECK_NONE); }
1161 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1163 addr64_u(unsigned char* view, Address value)
1164 { This::template rela_ua<64>(view, value, CHECK_NONE); }
1166 // R_POWERPC_ADDR32: (Symbol + Addend)
1167 static inline Status
1168 addr32(unsigned char* view, Address value, Overflow_check overflow)
1169 { return This::template rela<32>(view, value, overflow); }
1171 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1172 static inline Status
1173 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1174 { return This::template rela_ua<32>(view, value, overflow); }
1176 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1177 static inline Status
1178 addr24(unsigned char* view, Address value, Overflow_check overflow)
1180 Status stat = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
1181 if (overflow != CHECK_NONE && (value & 3) != 0)
1182 stat = STATUS_OVERFLOW;
1186 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1187 static inline Status
1188 addr16(unsigned char* view, Address value, Overflow_check overflow)
1189 { return This::template rela<16>(view, value, overflow); }
1191 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1192 static inline Status
1193 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1194 { return This::template rela_ua<16>(view, value, overflow); }
1196 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1197 static inline Status
1198 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1200 Status stat = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1201 if (overflow != CHECK_NONE && (value & 3) != 0)
1202 stat = STATUS_OVERFLOW;
1206 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1208 addr16_hi(unsigned char* view, Address value)
1209 { This::template rela<16>(view, 16, 0xffff, value, CHECK_NONE); }
1211 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1213 addr16_ha(unsigned char* view, Address value)
1214 { This::addr16_hi(view, value + 0x8000); }
1216 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1218 addr16_hi2(unsigned char* view, Address value)
1219 { This::template rela<16>(view, 32, 0xffff, value, CHECK_NONE); }
1221 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1223 addr16_ha2(unsigned char* view, Address value)
1224 { This::addr16_hi2(view, value + 0x8000); }
1226 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1228 addr16_hi3(unsigned char* view, Address value)
1229 { This::template rela<16>(view, 48, 0xffff, value, CHECK_NONE); }
1231 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1233 addr16_ha3(unsigned char* view, Address value)
1234 { This::addr16_hi3(view, value + 0x8000); }
1236 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1237 static inline Status
1238 addr14(unsigned char* view, Address value, Overflow_check overflow)
1240 Status stat = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1241 if (overflow != CHECK_NONE && (value & 3) != 0)
1242 stat = STATUS_OVERFLOW;
1247 // Stash away the index of .got2 or .opd in a relocatable object, if
1248 // such a section exists.
1250 template<int size, bool big_endian>
1252 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1253 Read_symbols_data* sd)
1255 const unsigned char* const pshdrs = sd->section_headers->data();
1256 const unsigned char* namesu = sd->section_names->data();
1257 const char* names = reinterpret_cast<const char*>(namesu);
1258 section_size_type names_size = sd->section_names_size;
1259 const unsigned char* s;
1261 s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1262 names, names_size, NULL);
1265 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1266 this->special_ = ndx;
1268 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1271 // Examine .rela.opd to build info about function entry points.
1273 template<int size, bool big_endian>
1275 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1277 const unsigned char* prelocs,
1278 const unsigned char* plocal_syms)
1282 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1284 const int reloc_size
1285 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1286 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1287 Address expected_off = 0;
1288 bool regular = true;
1289 unsigned int opd_ent_size = 0;
1291 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1293 Reltype reloc(prelocs);
1294 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1295 = reloc.get_r_info();
1296 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1297 if (r_type == elfcpp::R_PPC64_ADDR64)
1299 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1300 typename elfcpp::Elf_types<size>::Elf_Addr value;
1303 if (r_sym < this->local_symbol_count())
1305 typename elfcpp::Sym<size, big_endian>
1306 lsym(plocal_syms + r_sym * sym_size);
1307 shndx = lsym.get_st_shndx();
1308 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1309 value = lsym.get_st_value();
1312 shndx = this->symbol_section_and_value(r_sym, &value,
1314 this->set_opd_ent(reloc.get_r_offset(), shndx,
1315 value + reloc.get_r_addend());
1318 expected_off = reloc.get_r_offset();
1319 opd_ent_size = expected_off;
1321 else if (expected_off != reloc.get_r_offset())
1323 expected_off += opd_ent_size;
1325 else if (r_type == elfcpp::R_PPC64_TOC)
1327 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
1332 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1333 this->name().c_str(), r_type);
1337 if (reloc_count <= 2)
1338 opd_ent_size = this->section_size(this->opd_shndx());
1339 if (opd_ent_size != 24 && opd_ent_size != 16)
1343 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1344 this->name().c_str());
1350 template<int size, bool big_endian>
1352 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1354 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1357 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1358 p != rd->relocs.end();
1361 if (p->data_shndx == this->opd_shndx())
1363 uint64_t opd_size = this->section_size(this->opd_shndx());
1364 gold_assert(opd_size == static_cast<size_t>(opd_size));
1367 this->init_opd(opd_size);
1368 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1369 rd->local_symbols->data());
1377 // Set up PowerPC target specific relobj.
1379 template<int size, bool big_endian>
1381 Target_powerpc<size, big_endian>::do_make_elf_object(
1382 const std::string& name,
1383 Input_file* input_file,
1384 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1386 int et = ehdr.get_e_type();
1387 // ET_EXEC files are valid input for --just-symbols/-R,
1388 // and we treat them as relocatable objects.
1389 if (et == elfcpp::ET_REL
1390 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1392 Powerpc_relobj<size, big_endian>* obj =
1393 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1397 else if (et == elfcpp::ET_DYN)
1399 Sized_dynobj<size, big_endian>* obj =
1400 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1406 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1411 template<int size, bool big_endian>
1412 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1415 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1416 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1418 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1419 : Output_data_got<size, big_endian>(),
1420 symtab_(symtab), layout_(layout),
1421 header_ent_cnt_(size == 32 ? 3 : 1),
1422 header_index_(size == 32 ? 0x2000 : 0)
1427 // Create a new GOT entry and return its offset.
1429 add_got_entry(Got_entry got_entry)
1431 this->reserve_ent();
1432 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1435 // Create a pair of new GOT entries and return the offset of the first.
1437 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1439 this->reserve_ent(2);
1440 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1445 add_constant_pair(Valtype c1, Valtype c2)
1447 this->reserve_ent(2);
1448 unsigned int got_offset = this->add_constant(c1);
1449 this->add_constant(c2);
1453 // Offset of _GLOBAL_OFFSET_TABLE_.
1457 return this->got_offset(this->header_index_);
1460 // Offset of base used to access the GOT/TOC.
1461 // The got/toc pointer reg will be set to this value.
1462 typename elfcpp::Elf_types<size>::Elf_Off
1463 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1466 return this->g_o_t();
1468 return (this->output_section()->address()
1469 + object->toc_base_offset()
1473 // Ensure our GOT has a header.
1475 set_final_data_size()
1477 if (this->header_ent_cnt_ != 0)
1478 this->make_header();
1479 Output_data_got<size, big_endian>::set_final_data_size();
1482 // First word of GOT header needs some values that are not
1483 // handled by Output_data_got so poke them in here.
1484 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1486 do_write(Output_file* of)
1489 if (size == 32 && this->layout_->dynamic_data() != NULL)
1490 val = this->layout_->dynamic_section()->address();
1492 val = this->output_section()->address() + 0x8000;
1493 this->replace_constant(this->header_index_, val);
1494 Output_data_got<size, big_endian>::do_write(of);
1499 reserve_ent(unsigned int cnt = 1)
1501 if (this->header_ent_cnt_ == 0)
1503 if (this->num_entries() + cnt > this->header_index_)
1504 this->make_header();
1510 this->header_ent_cnt_ = 0;
1511 this->header_index_ = this->num_entries();
1514 Output_data_got<size, big_endian>::add_constant(0);
1515 Output_data_got<size, big_endian>::add_constant(0);
1516 Output_data_got<size, big_endian>::add_constant(0);
1518 // Define _GLOBAL_OFFSET_TABLE_ at the header
1519 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1520 Symbol_table::PREDEFINED,
1521 this, this->g_o_t(), 0,
1528 Output_data_got<size, big_endian>::add_constant(0);
1531 // Stashed pointers.
1532 Symbol_table* symtab_;
1536 unsigned int header_ent_cnt_;
1537 // GOT header index.
1538 unsigned int header_index_;
1541 // Get the GOT section, creating it if necessary.
1543 template<int size, bool big_endian>
1544 Output_data_got_powerpc<size, big_endian>*
1545 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1548 if (this->got_ == NULL)
1550 gold_assert(symtab != NULL && layout != NULL);
1553 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
1555 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1556 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1557 this->got_, ORDER_DATA, false);
1563 // Get the dynamic reloc section, creating it if necessary.
1565 template<int size, bool big_endian>
1566 typename Target_powerpc<size, big_endian>::Reloc_section*
1567 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1569 if (this->rela_dyn_ == NULL)
1571 gold_assert(layout != NULL);
1572 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1573 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1574 elfcpp::SHF_ALLOC, this->rela_dyn_,
1575 ORDER_DYNAMIC_RELOCS, false);
1577 return this->rela_dyn_;
1580 // A class to handle the PLT data.
1582 template<int size, bool big_endian>
1583 class Output_data_plt_powerpc : public Output_section_data_build
1586 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1587 size, big_endian> Reloc_section;
1589 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
1590 Reloc_section* plt_rel,
1591 unsigned int reserved_size,
1593 : Output_section_data_build(size == 32 ? 4 : 8),
1596 initial_plt_entry_size_(reserved_size),
1600 // Add an entry to the PLT.
1605 add_ifunc_entry(Symbol*);
1608 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
1610 // Return the .rela.plt section data.
1617 // Return the number of PLT entries.
1621 return ((this->current_data_size() - this->initial_plt_entry_size_)
1625 // Return the offset of the first non-reserved PLT entry.
1627 first_plt_entry_offset()
1628 { return this->initial_plt_entry_size_; }
1630 // Return the size of a PLT entry.
1632 get_plt_entry_size()
1633 { return plt_entry_size; }
1637 do_adjust_output_section(Output_section* os)
1642 // Write to a map file.
1644 do_print_to_mapfile(Mapfile* mapfile) const
1645 { mapfile->print_output_data(this, this->name_); }
1648 // The size of an entry in the PLT.
1649 static const int plt_entry_size = size == 32 ? 4 : 24;
1651 // Write out the PLT data.
1653 do_write(Output_file*);
1655 // The reloc section.
1656 Reloc_section* rel_;
1657 // Allows access to .glink for do_write.
1658 Target_powerpc<size, big_endian>* targ_;
1659 // The size of the first reserved entry.
1660 int initial_plt_entry_size_;
1661 // What to report in map file.
1665 // Add an entry to the PLT.
1667 template<int size, bool big_endian>
1669 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1671 if (!gsym->has_plt_offset())
1673 off_t off = this->current_data_size();
1675 off += this->first_plt_entry_offset();
1676 gsym->set_plt_offset(off);
1677 gsym->set_needs_dynsym_entry();
1678 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
1679 this->rel_->add_global(gsym, dynrel, this, off, 0);
1680 off += plt_entry_size;
1681 this->set_current_data_size(off);
1685 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
1687 template<int size, bool big_endian>
1689 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
1691 if (!gsym->has_plt_offset())
1693 off_t off = this->current_data_size();
1694 gsym->set_plt_offset(off);
1695 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
1697 dynrel = elfcpp::R_PPC64_JMP_IREL;
1698 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
1699 off += plt_entry_size;
1700 this->set_current_data_size(off);
1704 // Add an entry for a local ifunc symbol to the IPLT.
1706 template<int size, bool big_endian>
1708 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
1709 Sized_relobj_file<size, big_endian>* relobj,
1710 unsigned int local_sym_index)
1712 if (!relobj->local_has_plt_offset(local_sym_index))
1714 off_t off = this->current_data_size();
1715 relobj->set_local_plt_offset(local_sym_index, off);
1716 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
1718 dynrel = elfcpp::R_PPC64_JMP_IREL;
1719 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
1721 off += plt_entry_size;
1722 this->set_current_data_size(off);
1726 static const uint32_t add_0_11_11 = 0x7c0b5a14;
1727 static const uint32_t add_3_3_2 = 0x7c631214;
1728 static const uint32_t add_3_3_13 = 0x7c636a14;
1729 static const uint32_t add_11_0_11 = 0x7d605a14;
1730 static const uint32_t add_12_2_11 = 0x7d825a14;
1731 static const uint32_t addi_11_11 = 0x396b0000;
1732 static const uint32_t addi_12_12 = 0x398c0000;
1733 static const uint32_t addi_2_2 = 0x38420000;
1734 static const uint32_t addi_3_2 = 0x38620000;
1735 static const uint32_t addi_3_3 = 0x38630000;
1736 static const uint32_t addis_0_2 = 0x3c020000;
1737 static const uint32_t addis_0_13 = 0x3c0d0000;
1738 static const uint32_t addis_11_11 = 0x3d6b0000;
1739 static const uint32_t addis_11_30 = 0x3d7e0000;
1740 static const uint32_t addis_12_12 = 0x3d8c0000;
1741 static const uint32_t addis_12_2 = 0x3d820000;
1742 static const uint32_t addis_3_2 = 0x3c620000;
1743 static const uint32_t addis_3_13 = 0x3c6d0000;
1744 static const uint32_t b = 0x48000000;
1745 static const uint32_t bcl_20_31 = 0x429f0005;
1746 static const uint32_t bctr = 0x4e800420;
1747 static const uint32_t blr = 0x4e800020;
1748 static const uint32_t blrl = 0x4e800021;
1749 static const uint32_t cror_15_15_15 = 0x4def7b82;
1750 static const uint32_t cror_31_31_31 = 0x4ffffb82;
1751 static const uint32_t ld_0_1 = 0xe8010000;
1752 static const uint32_t ld_0_12 = 0xe80c0000;
1753 static const uint32_t ld_11_12 = 0xe96c0000;
1754 static const uint32_t ld_11_2 = 0xe9620000;
1755 static const uint32_t ld_2_1 = 0xe8410000;
1756 static const uint32_t ld_2_11 = 0xe84b0000;
1757 static const uint32_t ld_2_12 = 0xe84c0000;
1758 static const uint32_t ld_2_2 = 0xe8420000;
1759 static const uint32_t lfd_0_1 = 0xc8010000;
1760 static const uint32_t li_0_0 = 0x38000000;
1761 static const uint32_t li_12_0 = 0x39800000;
1762 static const uint32_t lis_0_0 = 0x3c000000;
1763 static const uint32_t lis_11 = 0x3d600000;
1764 static const uint32_t lis_12 = 0x3d800000;
1765 static const uint32_t lwz_0_12 = 0x800c0000;
1766 static const uint32_t lwz_11_11 = 0x816b0000;
1767 static const uint32_t lwz_11_30 = 0x817e0000;
1768 static const uint32_t lwz_12_12 = 0x818c0000;
1769 static const uint32_t lwzu_0_12 = 0x840c0000;
1770 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
1771 static const uint32_t mflr_0 = 0x7c0802a6;
1772 static const uint32_t mflr_11 = 0x7d6802a6;
1773 static const uint32_t mflr_12 = 0x7d8802a6;
1774 static const uint32_t mtctr_0 = 0x7c0903a6;
1775 static const uint32_t mtctr_11 = 0x7d6903a6;
1776 static const uint32_t mtlr_0 = 0x7c0803a6;
1777 static const uint32_t mtlr_12 = 0x7d8803a6;
1778 static const uint32_t nop = 0x60000000;
1779 static const uint32_t ori_0_0_0 = 0x60000000;
1780 static const uint32_t std_0_1 = 0xf8010000;
1781 static const uint32_t std_0_12 = 0xf80c0000;
1782 static const uint32_t std_2_1 = 0xf8410000;
1783 static const uint32_t stfd_0_1 = 0xd8010000;
1784 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
1785 static const uint32_t sub_11_11_12 = 0x7d6c5850;
1787 // Write out the PLT.
1789 template<int size, bool big_endian>
1791 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1795 const off_t offset = this->offset();
1796 const section_size_type oview_size
1797 = convert_to_section_size_type(this->data_size());
1798 unsigned char* const oview = of->get_output_view(offset, oview_size);
1799 unsigned char* pov = oview;
1800 unsigned char* endpov = oview + oview_size;
1802 // The address of the .glink branch table
1803 const Output_data_glink<size, big_endian>* glink
1804 = this->targ_->glink_section();
1805 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1806 = glink->address() + glink->pltresolve();
1808 while (pov < endpov)
1810 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1815 of->write_output_view(offset, oview_size, oview);
1819 // Create the PLT section.
1821 template<int size, bool big_endian>
1823 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1825 if (this->plt_ == NULL)
1827 if (this->glink_ == NULL)
1828 make_glink_section(layout);
1830 // Ensure that .rela.dyn always appears before .rela.plt This is
1831 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1832 // needs to include .rela.plt in it's range.
1833 this->rela_dyn_section(layout);
1835 Reloc_section* plt_rel = new Reloc_section(false);
1836 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1837 elfcpp::SHF_ALLOC, plt_rel,
1838 ORDER_DYNAMIC_PLT_RELOCS, false);
1840 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
1841 size == 32 ? 0 : 24,
1843 layout->add_output_section_data(".plt",
1845 ? elfcpp::SHT_PROGBITS
1846 : elfcpp::SHT_NOBITS),
1847 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1856 // Create the IPLT section.
1858 template<int size, bool big_endian>
1860 Target_powerpc<size, big_endian>::make_iplt_section(Layout* layout)
1862 if (this->iplt_ == NULL)
1864 this->make_plt_section(layout);
1866 Reloc_section* iplt_rel = new Reloc_section(false);
1867 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
1869 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
1871 this->plt_->output_section()->add_output_section_data(this->iplt_);
1875 // A class to handle .glink.
1877 template<int size, bool big_endian>
1878 class Output_data_glink : public Output_section_data
1881 static const int pltresolve_size = 16*4;
1883 Output_data_glink(Target_powerpc<size, big_endian>*);
1887 add_entry(const Sized_relobj_file<size, big_endian>*,
1889 const elfcpp::Rela<size, big_endian>&);
1892 add_entry(const Sized_relobj_file<size, big_endian>*,
1894 const elfcpp::Rela<size, big_endian>&);
1897 find_entry(const Symbol*) const;
1900 find_entry(const Sized_relobj_file<size, big_endian>*, unsigned int) const;
1903 find_entry(const Sized_relobj_file<size, big_endian>*,
1905 const elfcpp::Rela<size, big_endian>&) const;
1908 find_entry(const Sized_relobj_file<size, big_endian>*,
1910 const elfcpp::Rela<size, big_endian>&) const;
1913 glink_entry_size() const
1918 // FIXME: We should be using multiple glink sections for
1919 // stubs to support > 33M applications.
1926 return this->pltresolve_;
1930 // Write to a map file.
1932 do_print_to_mapfile(Mapfile* mapfile) const
1933 { mapfile->print_output_data(this, _("** glink")); }
1937 set_final_data_size();
1941 do_write(Output_file*);
1946 Glink_sym_ent(const Symbol* sym)
1947 : sym_(sym), object_(0), addend_(0), locsym_(0)
1950 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
1951 unsigned int locsym_index)
1952 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
1955 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
1957 const elfcpp::Rela<size, big_endian>& reloc)
1958 : sym_(sym), object_(0), addend_(0), locsym_(0)
1961 this->addend_ = reloc.get_r_addend();
1962 else if (parameters->options().output_is_position_independent()
1963 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1964 == elfcpp::R_PPC_PLTREL24))
1966 this->addend_ = reloc.get_r_addend();
1967 if (this->addend_ >= 32768)
1968 this->object_ = object;
1972 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
1973 unsigned int locsym_index,
1974 const elfcpp::Rela<size, big_endian>& reloc)
1975 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
1978 this->addend_ = reloc.get_r_addend();
1979 else if (parameters->options().output_is_position_independent()
1980 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1981 == elfcpp::R_PPC_PLTREL24))
1982 this->addend_ = reloc.get_r_addend();
1985 bool operator==(const Glink_sym_ent& that) const
1987 return (this->sym_ == that.sym_
1988 && this->object_ == that.object_
1989 && this->addend_ == that.addend_
1990 && this->locsym_ == that.locsym_);
1994 const Sized_relobj_file<size, big_endian>* object_;
1995 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
1996 unsigned int locsym_;
1999 class Glink_sym_ent_hash
2002 size_t operator()(const Glink_sym_ent& ent) const
2004 return (reinterpret_cast<uintptr_t>(ent.sym_)
2005 ^ reinterpret_cast<uintptr_t>(ent.object_)
2011 // Map sym/object/addend to index.
2012 typedef Unordered_map<Glink_sym_ent, unsigned int,
2013 Glink_sym_ent_hash> Glink_entries;
2014 Glink_entries glink_entries_;
2016 // Offset of pltresolve stub (actually, branch table for 32-bit)
2019 // Allows access to .got and .plt for do_write.
2020 Target_powerpc<size, big_endian>* targ_;
2023 // Create the glink section.
2025 template<int size, bool big_endian>
2026 Output_data_glink<size, big_endian>::Output_data_glink(
2027 Target_powerpc<size, big_endian>* targ)
2028 : Output_section_data(16),
2029 pltresolve_(0), targ_(targ)
2033 // Add an entry to glink, if we do not already have one for this
2034 // sym/object/addend combo.
2036 template<int size, bool big_endian>
2038 Output_data_glink<size, big_endian>::add_entry(
2039 const Sized_relobj_file<size, big_endian>* object,
2041 const elfcpp::Rela<size, big_endian>& reloc)
2043 Glink_sym_ent ent(object, gsym, reloc);
2044 unsigned int indx = this->glink_entries_.size();
2045 this->glink_entries_.insert(std::make_pair(ent, indx));
2048 template<int size, bool big_endian>
2050 Output_data_glink<size, big_endian>::add_entry(
2051 const Sized_relobj_file<size, big_endian>* object,
2052 unsigned int locsym_index,
2053 const elfcpp::Rela<size, big_endian>& reloc)
2055 Glink_sym_ent ent(object, locsym_index, reloc);
2056 unsigned int indx = this->glink_entries_.size();
2057 this->glink_entries_.insert(std::make_pair(ent, indx));
2060 template<int size, bool big_endian>
2062 Output_data_glink<size, big_endian>::find_entry(
2063 const Sized_relobj_file<size, big_endian>* object,
2065 const elfcpp::Rela<size, big_endian>& reloc) const
2067 Glink_sym_ent ent(object, gsym, reloc);
2068 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2069 gold_assert(p != this->glink_entries_.end());
2073 template<int size, bool big_endian>
2075 Output_data_glink<size, big_endian>::find_entry(const Symbol* gsym) const
2077 Glink_sym_ent ent(gsym);
2078 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2079 gold_assert(p != this->glink_entries_.end());
2083 template<int size, bool big_endian>
2085 Output_data_glink<size, big_endian>::find_entry(
2086 const Sized_relobj_file<size, big_endian>* object,
2087 unsigned int locsym_index,
2088 const elfcpp::Rela<size, big_endian>& reloc) const
2090 Glink_sym_ent ent(object, locsym_index, reloc);
2091 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2092 gold_assert(p != this->glink_entries_.end());
2096 template<int size, bool big_endian>
2098 Output_data_glink<size, big_endian>::find_entry(
2099 const Sized_relobj_file<size, big_endian>* object,
2100 unsigned int locsym_index) const
2102 Glink_sym_ent ent(object, locsym_index);
2103 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2104 gold_assert(p != this->glink_entries_.end());
2108 template<int size, bool big_endian>
2110 Output_data_glink<size, big_endian>::set_final_data_size()
2112 unsigned int count = this->glink_entries_.size();
2113 off_t total = count;
2120 this->pltresolve_ = total;
2122 // space for branch table
2123 total += 4 * (count - 1);
2125 total += -total & 15;
2126 total += this->pltresolve_size;
2131 this->pltresolve_ = total;
2132 total += this->pltresolve_size;
2134 // space for branch table
2137 total += 4 * (count - 0x8000);
2141 this->set_data_size(total);
2144 static inline uint32_t
2150 static inline uint32_t
2156 static inline uint32_t
2159 return hi(a + 0x8000);
2162 template<bool big_endian>
2164 write_insn(unsigned char* p, uint32_t v)
2166 elfcpp::Swap<32, big_endian>::writeval(p, v);
2169 // Write out .glink.
2171 template<int size, bool big_endian>
2173 Output_data_glink<size, big_endian>::do_write(Output_file* of)
2175 const off_t off = this->offset();
2176 const section_size_type oview_size =
2177 convert_to_section_size_type(this->data_size());
2178 unsigned char* const oview = of->get_output_view(off, oview_size);
2181 // The base address of the .plt section.
2182 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2183 static const Address invalid_address = static_cast<Address>(0) - 1;
2184 Address plt_base = this->targ_->plt_section()->address();
2185 Address iplt_base = invalid_address;
2187 const Output_data_got_powerpc<size, big_endian>* got
2188 = this->targ_->got_section();
2192 Address got_os_addr = got->output_section()->address();
2194 // Write out call stubs.
2195 typename Glink_entries::const_iterator g;
2196 for (g = this->glink_entries_.begin();
2197 g != this->glink_entries_.end();
2202 const Symbol* gsym = g->first.sym_;
2205 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
2206 && gsym->can_use_relative_reloc(false));
2207 plt_addr = gsym->plt_offset();
2212 const Sized_relobj_file<size, big_endian>* relobj
2214 unsigned int local_sym_index = g->first.locsym_;
2215 plt_addr = relobj->local_plt_offset(local_sym_index);
2219 if (iplt_base == invalid_address)
2220 iplt_base = this->targ_->iplt_section()->address();
2221 plt_addr += iplt_base;
2224 plt_addr += plt_base;
2225 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2226 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
2227 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
2228 Address pltoff = plt_addr - got_addr;
2230 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
2231 gold_error(_("%s: linkage table error against `%s'"),
2232 g->first.object_->name().c_str(),
2233 g->first.sym_->demangled_name().c_str());
2235 p = oview + g->second * this->glink_entry_size();
2236 if (ha(pltoff) != 0)
2238 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
2239 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
2240 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
2241 if (ha(pltoff + 16) != ha(pltoff))
2243 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
2246 write_insn<big_endian>(p, mtctr_11), p += 4;
2247 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
2248 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
2249 write_insn<big_endian>(p, bctr), p += 4;
2253 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
2254 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
2255 if (ha(pltoff + 16) != ha(pltoff))
2257 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
2260 write_insn<big_endian>(p, mtctr_11), p += 4;
2261 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
2262 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
2263 write_insn<big_endian>(p, bctr), p += 4;
2267 // Write pltresolve stub.
2268 p = oview + this->pltresolve_;
2269 Address after_bcl = this->address() + this->pltresolve_ + 16;
2270 Address pltoff = plt_base - after_bcl;
2272 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
2274 write_insn<big_endian>(p, mflr_12), p += 4;
2275 write_insn<big_endian>(p, bcl_20_31), p += 4;
2276 write_insn<big_endian>(p, mflr_11), p += 4;
2277 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
2278 write_insn<big_endian>(p, mtlr_12), p += 4;
2279 write_insn<big_endian>(p, add_12_2_11), p += 4;
2280 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
2281 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
2282 write_insn<big_endian>(p, mtctr_11), p += 4;
2283 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
2284 write_insn<big_endian>(p, bctr), p += 4;
2285 while (p < oview + this->pltresolve_ + this->pltresolve_size)
2286 write_insn<big_endian>(p, nop), p += 4;
2288 // Write lazy link call stubs.
2290 while (p < oview + oview_size)
2294 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
2298 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
2299 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
2301 uint32_t branch_off = this->pltresolve_ + 8 - (p - oview);
2302 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
2308 // The address of _GLOBAL_OFFSET_TABLE_.
2309 Address g_o_t = got->address() + got->g_o_t();
2311 // Write out call stubs.
2312 typename Glink_entries::const_iterator g;
2313 for (g = this->glink_entries_.begin();
2314 g != this->glink_entries_.end();
2319 const Symbol* gsym = g->first.sym_;
2322 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
2323 && gsym->can_use_relative_reloc(false));
2324 plt_addr = gsym->plt_offset();
2329 const Sized_relobj_file<size, big_endian>* relobj
2331 unsigned int local_sym_index = g->first.locsym_;
2332 plt_addr = relobj->local_plt_offset(local_sym_index);
2336 if (iplt_base == invalid_address)
2337 iplt_base = this->targ_->iplt_section()->address();
2338 plt_addr += iplt_base;
2341 plt_addr += plt_base;
2343 p = oview + g->second * this->glink_entry_size();
2344 if (parameters->options().output_is_position_independent())
2347 const Powerpc_relobj<size, big_endian>* object = static_cast
2348 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
2349 if (object != NULL && g->first.addend_ >= 32768)
2351 unsigned int got2 = object->got2_shndx();
2352 got_addr = g->first.object_->get_output_section_offset(got2);
2353 gold_assert(got_addr != invalid_address);
2354 got_addr += (g->first.object_->output_section(got2)->address()
2355 + g->first.addend_);
2360 Address pltoff = plt_addr - got_addr;
2361 if (ha(pltoff) == 0)
2363 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
2364 write_insn<big_endian>(p + 4, mtctr_11);
2365 write_insn<big_endian>(p + 8, bctr);
2369 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
2370 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
2371 write_insn<big_endian>(p + 8, mtctr_11);
2372 write_insn<big_endian>(p + 12, bctr);
2377 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
2378 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
2379 write_insn<big_endian>(p + 8, mtctr_11);
2380 write_insn<big_endian>(p + 12, bctr);
2384 // Write out pltresolve branch table.
2385 p = oview + this->pltresolve_;
2386 unsigned int the_end = oview_size - this->pltresolve_size;
2387 unsigned char* end_p = oview + the_end;
2388 while (p < end_p - 8 * 4)
2389 write_insn<big_endian>(p, b + end_p - p), p += 4;
2391 write_insn<big_endian>(p, nop), p += 4;
2393 // Write out pltresolve call stub.
2394 if (parameters->options().output_is_position_independent())
2396 Address res0_off = this->pltresolve_;
2397 Address after_bcl_off = the_end + 12;
2398 Address bcl_res0 = after_bcl_off - res0_off;
2400 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
2401 write_insn<big_endian>(p + 4, mflr_0);
2402 write_insn<big_endian>(p + 8, bcl_20_31);
2403 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
2404 write_insn<big_endian>(p + 16, mflr_12);
2405 write_insn<big_endian>(p + 20, mtlr_0);
2406 write_insn<big_endian>(p + 24, sub_11_11_12);
2408 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
2410 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
2411 if (ha(got_bcl) == ha(got_bcl + 4))
2413 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
2414 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
2418 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
2419 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
2421 write_insn<big_endian>(p + 40, mtctr_0);
2422 write_insn<big_endian>(p + 44, add_0_11_11);
2423 write_insn<big_endian>(p + 48, add_11_0_11);
2424 write_insn<big_endian>(p + 52, bctr);
2425 write_insn<big_endian>(p + 56, nop);
2426 write_insn<big_endian>(p + 60, nop);
2430 Address res0 = this->pltresolve_ + this->address();
2432 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
2433 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
2434 if (ha(g_o_t + 4) == ha(g_o_t + 8))
2435 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
2437 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
2438 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
2439 write_insn<big_endian>(p + 16, mtctr_0);
2440 write_insn<big_endian>(p + 20, add_0_11_11);
2441 if (ha(g_o_t + 4) == ha(g_o_t + 8))
2442 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
2444 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
2445 write_insn<big_endian>(p + 28, add_11_0_11);
2446 write_insn<big_endian>(p + 32, bctr);
2447 write_insn<big_endian>(p + 36, nop);
2448 write_insn<big_endian>(p + 40, nop);
2449 write_insn<big_endian>(p + 44, nop);
2450 write_insn<big_endian>(p + 48, nop);
2451 write_insn<big_endian>(p + 52, nop);
2452 write_insn<big_endian>(p + 56, nop);
2453 write_insn<big_endian>(p + 60, nop);
2458 of->write_output_view(off, oview_size, oview);
2462 // A class to handle linker generated save/restore functions.
2464 template<int size, bool big_endian>
2465 class Output_data_save_res : public Output_section_data_build
2468 Output_data_save_res(Symbol_table* symtab);
2471 // Write to a map file.
2473 do_print_to_mapfile(Mapfile* mapfile) const
2474 { mapfile->print_output_data(this, _("** save/restore")); }
2477 do_write(Output_file*);
2480 // The maximum size of save/restore contents.
2481 static const unsigned int savres_max = 218*4;
2484 savres_define(Symbol_table* symtab,
2486 unsigned int lo, unsigned int hi,
2487 unsigned char* write_ent(unsigned char*, int),
2488 unsigned char* write_tail(unsigned char*, int));
2490 unsigned char *contents_;
2493 template<bool big_endian>
2494 static unsigned char*
2495 savegpr0(unsigned char* p, int r)
2497 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2498 write_insn<big_endian>(p, insn);
2502 template<bool big_endian>
2503 static unsigned char*
2504 savegpr0_tail(unsigned char* p, int r)
2506 p = savegpr0<big_endian>(p, r);
2507 uint32_t insn = std_0_1 + 16;
2508 write_insn<big_endian>(p, insn);
2510 write_insn<big_endian>(p, blr);
2514 template<bool big_endian>
2515 static unsigned char*
2516 restgpr0(unsigned char* p, int r)
2518 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2519 write_insn<big_endian>(p, insn);
2523 template<bool big_endian>
2524 static unsigned char*
2525 restgpr0_tail(unsigned char* p, int r)
2527 uint32_t insn = ld_0_1 + 16;
2528 write_insn<big_endian>(p, insn);
2530 p = restgpr0<big_endian>(p, r);
2531 write_insn<big_endian>(p, mtlr_0);
2535 p = restgpr0<big_endian>(p, 30);
2536 p = restgpr0<big_endian>(p, 31);
2538 write_insn<big_endian>(p, blr);
2542 template<bool big_endian>
2543 static unsigned char*
2544 savegpr1(unsigned char* p, int r)
2546 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
2547 write_insn<big_endian>(p, insn);
2551 template<bool big_endian>
2552 static unsigned char*
2553 savegpr1_tail(unsigned char* p, int r)
2555 p = savegpr1<big_endian>(p, r);
2556 write_insn<big_endian>(p, blr);
2560 template<bool big_endian>
2561 static unsigned char*
2562 restgpr1(unsigned char* p, int r)
2564 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
2565 write_insn<big_endian>(p, insn);
2569 template<bool big_endian>
2570 static unsigned char*
2571 restgpr1_tail(unsigned char* p, int r)
2573 p = restgpr1<big_endian>(p, r);
2574 write_insn<big_endian>(p, blr);
2578 template<bool big_endian>
2579 static unsigned char*
2580 savefpr(unsigned char* p, int r)
2582 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2583 write_insn<big_endian>(p, insn);
2587 template<bool big_endian>
2588 static unsigned char*
2589 savefpr0_tail(unsigned char* p, int r)
2591 p = savefpr<big_endian>(p, r);
2592 write_insn<big_endian>(p, std_0_1 + 16);
2594 write_insn<big_endian>(p, blr);
2598 template<bool big_endian>
2599 static unsigned char*
2600 restfpr(unsigned char* p, int r)
2602 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2603 write_insn<big_endian>(p, insn);
2607 template<bool big_endian>
2608 static unsigned char*
2609 restfpr0_tail(unsigned char* p, int r)
2611 write_insn<big_endian>(p, ld_0_1 + 16);
2613 p = restfpr<big_endian>(p, r);
2614 write_insn<big_endian>(p, mtlr_0);
2618 p = restfpr<big_endian>(p, 30);
2619 p = restfpr<big_endian>(p, 31);
2621 write_insn<big_endian>(p, blr);
2625 template<bool big_endian>
2626 static unsigned char*
2627 savefpr1_tail(unsigned char* p, int r)
2629 p = savefpr<big_endian>(p, r);
2630 write_insn<big_endian>(p, blr);
2634 template<bool big_endian>
2635 static unsigned char*
2636 restfpr1_tail(unsigned char* p, int r)
2638 p = restfpr<big_endian>(p, r);
2639 write_insn<big_endian>(p, blr);
2643 template<bool big_endian>
2644 static unsigned char*
2645 savevr(unsigned char* p, int r)
2647 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
2648 write_insn<big_endian>(p, insn);
2650 insn = stvx_0_12_0 + (r << 21);
2651 write_insn<big_endian>(p, insn);
2655 template<bool big_endian>
2656 static unsigned char*
2657 savevr_tail(unsigned char* p, int r)
2659 p = savevr<big_endian>(p, r);
2660 write_insn<big_endian>(p, blr);
2664 template<bool big_endian>
2665 static unsigned char*
2666 restvr(unsigned char* p, int r)
2668 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
2669 write_insn<big_endian>(p, insn);
2671 insn = lvx_0_12_0 + (r << 21);
2672 write_insn<big_endian>(p, insn);
2676 template<bool big_endian>
2677 static unsigned char*
2678 restvr_tail(unsigned char* p, int r)
2680 p = restvr<big_endian>(p, r);
2681 write_insn<big_endian>(p, blr);
2686 template<int size, bool big_endian>
2687 Output_data_save_res<size, big_endian>::Output_data_save_res(
2688 Symbol_table* symtab)
2689 : Output_section_data_build(4),
2692 this->savres_define(symtab,
2693 "_savegpr0_", 14, 31,
2694 savegpr0<big_endian>, savegpr0_tail<big_endian>);
2695 this->savres_define(symtab,
2696 "_restgpr0_", 14, 29,
2697 restgpr0<big_endian>, restgpr0_tail<big_endian>);
2698 this->savres_define(symtab,
2699 "_restgpr0_", 30, 31,
2700 restgpr0<big_endian>, restgpr0_tail<big_endian>);
2701 this->savres_define(symtab,
2702 "_savegpr1_", 14, 31,
2703 savegpr1<big_endian>, savegpr1_tail<big_endian>);
2704 this->savres_define(symtab,
2705 "_restgpr1_", 14, 31,
2706 restgpr1<big_endian>, restgpr1_tail<big_endian>);
2707 this->savres_define(symtab,
2708 "_savefpr_", 14, 31,
2709 savefpr<big_endian>, savefpr0_tail<big_endian>);
2710 this->savres_define(symtab,
2711 "_restfpr_", 14, 29,
2712 restfpr<big_endian>, restfpr0_tail<big_endian>);
2713 this->savres_define(symtab,
2714 "_restfpr_", 30, 31,
2715 restfpr<big_endian>, restfpr0_tail<big_endian>);
2716 this->savres_define(symtab,
2718 savefpr<big_endian>, savefpr1_tail<big_endian>);
2719 this->savres_define(symtab,
2721 restfpr<big_endian>, restfpr1_tail<big_endian>);
2722 this->savres_define(symtab,
2724 savevr<big_endian>, savevr_tail<big_endian>);
2725 this->savres_define(symtab,
2727 restvr<big_endian>, restvr_tail<big_endian>);
2730 template<int size, bool big_endian>
2732 Output_data_save_res<size, big_endian>::savres_define(
2733 Symbol_table* symtab,
2735 unsigned int lo, unsigned int hi,
2736 unsigned char* write_ent(unsigned char*, int),
2737 unsigned char* write_tail(unsigned char*, int))
2739 size_t len = strlen(name);
2740 bool writing = false;
2743 memcpy(sym, name, len);
2746 for (unsigned int i = lo; i <= hi; i++)
2748 sym[len + 0] = i / 10 + '0';
2749 sym[len + 1] = i % 10 + '0';
2750 Symbol* gsym = symtab->lookup(sym);
2751 bool refd = gsym != NULL && gsym->is_undefined();
2752 writing = writing || refd;
2755 if (this->contents_ == NULL)
2756 this->contents_ = new unsigned char[this->savres_max];
2758 off_t value = this->current_data_size();
2759 unsigned char* p = this->contents_ + value;
2761 p = write_ent(p, i);
2763 p = write_tail(p, i);
2764 off_t cur_size = p - this->contents_;
2765 this->set_current_data_size(cur_size);
2767 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
2768 this, value, cur_size - value,
2769 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
2770 elfcpp::STV_HIDDEN, 0, false, false);
2775 // Write out save/restore.
2777 template<int size, bool big_endian>
2779 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
2781 const off_t off = this->offset();
2782 const section_size_type oview_size =
2783 convert_to_section_size_type(this->data_size());
2784 unsigned char* const oview = of->get_output_view(off, oview_size);
2785 memcpy(oview, this->contents_, oview_size);
2786 of->write_output_view(off, oview_size, oview);
2790 // Create the glink section.
2792 template<int size, bool big_endian>
2794 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
2796 if (this->glink_ == NULL)
2798 this->glink_ = new Output_data_glink<size, big_endian>(this);
2799 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2800 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2801 this->glink_, ORDER_TEXT, false);
2805 // Create a PLT entry for a global symbol.
2807 template<int size, bool big_endian>
2809 Target_powerpc<size, big_endian>::make_plt_entry(
2812 const elfcpp::Rela<size, big_endian>& reloc,
2813 const Sized_relobj_file<size, big_endian>* object)
2815 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2816 && gsym->can_use_relative_reloc(false))
2818 if (this->iplt_ == NULL)
2819 this->make_iplt_section(layout);
2820 this->iplt_->add_ifunc_entry(gsym);
2824 if (this->plt_ == NULL)
2825 this->make_plt_section(layout);
2826 this->plt_->add_entry(gsym);
2828 this->glink_->add_entry(object, gsym, reloc);
2831 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2833 template<int size, bool big_endian>
2835 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
2837 const elfcpp::Rela<size, big_endian>& reloc,
2838 Sized_relobj_file<size, big_endian>* relobj)
2840 if (this->iplt_ == NULL)
2841 this->make_iplt_section(layout);
2842 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2843 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
2844 this->glink_->add_entry(relobj, r_sym, reloc);
2847 // Return the number of entries in the PLT.
2849 template<int size, bool big_endian>
2851 Target_powerpc<size, big_endian>::plt_entry_count() const
2853 if (this->plt_ == NULL)
2855 unsigned int count = this->plt_->entry_count();
2856 if (this->iplt_ != NULL)
2857 count += this->iplt_->entry_count();
2861 // Return the offset of the first non-reserved PLT entry.
2863 template<int size, bool big_endian>
2865 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
2867 return this->plt_->first_plt_entry_offset();
2870 // Return the size of each PLT entry.
2872 template<int size, bool big_endian>
2874 Target_powerpc<size, big_endian>::plt_entry_size() const
2876 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
2879 // Create a GOT entry for local dynamic __tls_get_addr calls.
2881 template<int size, bool big_endian>
2883 Target_powerpc<size, big_endian>::tlsld_got_offset(
2884 Symbol_table* symtab,
2886 Sized_relobj_file<size, big_endian>* object)
2888 if (this->tlsld_got_offset_ == -1U)
2890 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2891 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2892 Output_data_got_powerpc<size, big_endian>* got
2893 = this->got_section(symtab, layout);
2894 unsigned int got_offset = got->add_constant_pair(0, 0);
2895 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
2897 this->tlsld_got_offset_ = got_offset;
2899 return this->tlsld_got_offset_;
2902 // Get the Reference_flags for a particular relocation.
2904 template<int size, bool big_endian>
2906 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
2910 case elfcpp::R_POWERPC_NONE:
2911 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2912 case elfcpp::R_POWERPC_GNU_VTENTRY:
2913 case elfcpp::R_PPC64_TOC:
2914 // No symbol reference.
2917 case elfcpp::R_PPC64_ADDR64:
2918 case elfcpp::R_PPC64_UADDR64:
2919 case elfcpp::R_POWERPC_ADDR32:
2920 case elfcpp::R_POWERPC_UADDR32:
2921 case elfcpp::R_POWERPC_ADDR16:
2922 case elfcpp::R_POWERPC_UADDR16:
2923 case elfcpp::R_POWERPC_ADDR16_LO:
2924 case elfcpp::R_POWERPC_ADDR16_HI:
2925 case elfcpp::R_POWERPC_ADDR16_HA:
2926 return Symbol::ABSOLUTE_REF;
2928 case elfcpp::R_POWERPC_ADDR24:
2929 case elfcpp::R_POWERPC_ADDR14:
2930 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2931 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2932 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2934 case elfcpp::R_PPC64_REL64:
2935 case elfcpp::R_POWERPC_REL32:
2936 case elfcpp::R_PPC_LOCAL24PC:
2937 case elfcpp::R_POWERPC_REL16:
2938 case elfcpp::R_POWERPC_REL16_LO:
2939 case elfcpp::R_POWERPC_REL16_HI:
2940 case elfcpp::R_POWERPC_REL16_HA:
2941 return Symbol::RELATIVE_REF;
2943 case elfcpp::R_POWERPC_REL24:
2944 case elfcpp::R_PPC_PLTREL24:
2945 case elfcpp::R_POWERPC_REL14:
2946 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2947 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2948 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2950 case elfcpp::R_POWERPC_GOT16:
2951 case elfcpp::R_POWERPC_GOT16_LO:
2952 case elfcpp::R_POWERPC_GOT16_HI:
2953 case elfcpp::R_POWERPC_GOT16_HA:
2954 case elfcpp::R_PPC64_GOT16_DS:
2955 case elfcpp::R_PPC64_GOT16_LO_DS:
2956 case elfcpp::R_PPC64_TOC16:
2957 case elfcpp::R_PPC64_TOC16_LO:
2958 case elfcpp::R_PPC64_TOC16_HI:
2959 case elfcpp::R_PPC64_TOC16_HA:
2960 case elfcpp::R_PPC64_TOC16_DS:
2961 case elfcpp::R_PPC64_TOC16_LO_DS:
2963 return Symbol::ABSOLUTE_REF;
2965 case elfcpp::R_POWERPC_GOT_TPREL16:
2966 case elfcpp::R_POWERPC_TLS:
2967 return Symbol::TLS_REF;
2969 case elfcpp::R_POWERPC_COPY:
2970 case elfcpp::R_POWERPC_GLOB_DAT:
2971 case elfcpp::R_POWERPC_JMP_SLOT:
2972 case elfcpp::R_POWERPC_RELATIVE:
2973 case elfcpp::R_POWERPC_DTPMOD:
2975 // Not expected. We will give an error later.
2980 // Report an unsupported relocation against a local symbol.
2982 template<int size, bool big_endian>
2984 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
2985 Sized_relobj_file<size, big_endian>* object,
2986 unsigned int r_type)
2988 gold_error(_("%s: unsupported reloc %u against local symbol"),
2989 object->name().c_str(), r_type);
2992 // We are about to emit a dynamic relocation of type R_TYPE. If the
2993 // dynamic linker does not support it, issue an error.
2995 template<int size, bool big_endian>
2997 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
2998 unsigned int r_type)
3000 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
3002 // These are the relocation types supported by glibc for both 32-bit
3003 // and 64-bit powerpc.
3006 case elfcpp::R_POWERPC_NONE:
3007 case elfcpp::R_POWERPC_RELATIVE:
3008 case elfcpp::R_POWERPC_GLOB_DAT:
3009 case elfcpp::R_POWERPC_DTPMOD:
3010 case elfcpp::R_POWERPC_DTPREL:
3011 case elfcpp::R_POWERPC_TPREL:
3012 case elfcpp::R_POWERPC_JMP_SLOT:
3013 case elfcpp::R_POWERPC_COPY:
3014 case elfcpp::R_POWERPC_IRELATIVE:
3015 case elfcpp::R_POWERPC_ADDR32:
3016 case elfcpp::R_POWERPC_UADDR32:
3017 case elfcpp::R_POWERPC_ADDR24:
3018 case elfcpp::R_POWERPC_ADDR16:
3019 case elfcpp::R_POWERPC_UADDR16:
3020 case elfcpp::R_POWERPC_ADDR16_LO:
3021 case elfcpp::R_POWERPC_ADDR16_HI:
3022 case elfcpp::R_POWERPC_ADDR16_HA:
3023 case elfcpp::R_POWERPC_ADDR14:
3024 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3025 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3026 case elfcpp::R_POWERPC_REL32:
3027 case elfcpp::R_POWERPC_REL24:
3028 case elfcpp::R_POWERPC_TPREL16:
3029 case elfcpp::R_POWERPC_TPREL16_LO:
3030 case elfcpp::R_POWERPC_TPREL16_HI:
3031 case elfcpp::R_POWERPC_TPREL16_HA:
3042 // These are the relocation types supported only on 64-bit.
3043 case elfcpp::R_PPC64_ADDR64:
3044 case elfcpp::R_PPC64_UADDR64:
3045 case elfcpp::R_PPC64_JMP_IREL:
3046 case elfcpp::R_PPC64_ADDR16_DS:
3047 case elfcpp::R_PPC64_ADDR16_LO_DS:
3048 case elfcpp::R_PPC64_ADDR16_HIGHER:
3049 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3050 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3051 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3052 case elfcpp::R_PPC64_REL64:
3053 case elfcpp::R_POWERPC_ADDR30:
3054 case elfcpp::R_PPC64_TPREL16_DS:
3055 case elfcpp::R_PPC64_TPREL16_LO_DS:
3056 case elfcpp::R_PPC64_TPREL16_HIGHER:
3057 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3058 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3059 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3070 // These are the relocation types supported only on 32-bit.
3071 // ??? glibc ld.so doesn't need to support these.
3072 case elfcpp::R_POWERPC_DTPREL16:
3073 case elfcpp::R_POWERPC_DTPREL16_LO:
3074 case elfcpp::R_POWERPC_DTPREL16_HI:
3075 case elfcpp::R_POWERPC_DTPREL16_HA:
3083 // This prevents us from issuing more than one error per reloc
3084 // section. But we can still wind up issuing more than one
3085 // error per object file.
3086 if (this->issued_non_pic_error_)
3088 gold_assert(parameters->options().output_is_position_independent());
3089 object->error(_("requires unsupported dynamic reloc; "
3090 "recompile with -fPIC"));
3091 this->issued_non_pic_error_ = true;
3095 // Return whether we need to make a PLT entry for a relocation of the
3096 // given type against a STT_GNU_IFUNC symbol.
3098 template<int size, bool big_endian>
3100 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
3101 Sized_relobj_file<size, big_endian>* object,
3102 unsigned int r_type)
3104 // In non-pic code any reference will resolve to the plt call stub
3105 // for the ifunc symbol.
3106 if (size == 32 && !parameters->options().output_is_position_independent())
3111 // Word size refs from data sections are OK.
3112 case elfcpp::R_POWERPC_ADDR32:
3113 case elfcpp::R_POWERPC_UADDR32:
3118 case elfcpp::R_PPC64_ADDR64:
3119 case elfcpp::R_PPC64_UADDR64:
3124 // GOT refs are good.
3125 case elfcpp::R_POWERPC_GOT16:
3126 case elfcpp::R_POWERPC_GOT16_LO:
3127 case elfcpp::R_POWERPC_GOT16_HI:
3128 case elfcpp::R_POWERPC_GOT16_HA:
3129 case elfcpp::R_PPC64_GOT16_DS:
3130 case elfcpp::R_PPC64_GOT16_LO_DS:
3133 // So are function calls.
3134 case elfcpp::R_POWERPC_ADDR24:
3135 case elfcpp::R_POWERPC_ADDR14:
3136 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3137 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3138 case elfcpp::R_POWERPC_REL24:
3139 case elfcpp::R_PPC_PLTREL24:
3140 case elfcpp::R_POWERPC_REL14:
3141 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3142 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3149 // Anything else is a problem.
3150 // If we are building a static executable, the libc startup function
3151 // responsible for applying indirect function relocations is going
3152 // to complain about the reloc type.
3153 // If we are building a dynamic executable, we will have a text
3154 // relocation. The dynamic loader will set the text segment
3155 // writable and non-executable to apply text relocations. So we'll
3156 // segfault when trying to run the indirection function to resolve
3158 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
3159 object->name().c_str(), r_type);
3163 // Scan a relocation for a local symbol.
3165 template<int size, bool big_endian>
3167 Target_powerpc<size, big_endian>::Scan::local(
3168 Symbol_table* symtab,
3170 Target_powerpc<size, big_endian>* target,
3171 Sized_relobj_file<size, big_endian>* object,
3172 unsigned int data_shndx,
3173 Output_section* output_section,
3174 const elfcpp::Rela<size, big_endian>& reloc,
3175 unsigned int r_type,
3176 const elfcpp::Sym<size, big_endian>& lsym,
3179 Powerpc_relobj<size, big_endian>* ppc_object
3180 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3185 && data_shndx == ppc_object->opd_shndx()
3186 && r_type == elfcpp::R_PPC64_ADDR64)
3187 ppc_object->set_opd_discard(reloc.get_r_offset());
3191 // A local STT_GNU_IFUNC symbol may require a PLT entry.
3192 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
3193 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
3194 target->make_local_ifunc_plt_entry(layout, reloc, object);
3198 case elfcpp::R_POWERPC_NONE:
3199 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3200 case elfcpp::R_POWERPC_GNU_VTENTRY:
3201 case elfcpp::R_PPC64_TOCSAVE:
3202 case elfcpp::R_PPC_EMB_MRKREF:
3203 case elfcpp::R_POWERPC_TLS:
3206 case elfcpp::R_PPC64_TOC:
3208 Output_data_got_powerpc<size, big_endian>* got
3209 = target->got_section(symtab, layout);
3210 if (parameters->options().output_is_position_independent())
3212 Address off = reloc.get_r_offset();
3214 && data_shndx == ppc_object->opd_shndx()
3215 && ppc_object->get_opd_discard(off - 8))
3218 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3219 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
3220 rela_dyn->add_output_section_relative(got->output_section(),
3221 elfcpp::R_POWERPC_RELATIVE,
3223 object, data_shndx, off,
3224 symobj->toc_base_offset());
3229 case elfcpp::R_PPC64_ADDR64:
3230 case elfcpp::R_PPC64_UADDR64:
3231 case elfcpp::R_POWERPC_ADDR32:
3232 case elfcpp::R_POWERPC_UADDR32:
3233 case elfcpp::R_POWERPC_ADDR24:
3234 case elfcpp::R_POWERPC_ADDR16:
3235 case elfcpp::R_POWERPC_ADDR16_LO:
3236 case elfcpp::R_POWERPC_ADDR16_HI:
3237 case elfcpp::R_POWERPC_ADDR16_HA:
3238 case elfcpp::R_POWERPC_UADDR16:
3239 case elfcpp::R_PPC64_ADDR16_HIGHER:
3240 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3241 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3242 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3243 case elfcpp::R_PPC64_ADDR16_DS:
3244 case elfcpp::R_PPC64_ADDR16_LO_DS:
3245 case elfcpp::R_POWERPC_ADDR14:
3246 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3247 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3248 // If building a shared library (or a position-independent
3249 // executable), we need to create a dynamic relocation for
3251 if (parameters->options().output_is_position_independent()
3252 || (size == 64 && is_ifunc))
3254 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3256 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
3257 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
3259 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3260 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3263 rela_dyn = target->iplt_section()->rel_plt();
3264 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3266 rela_dyn->add_local_relative(object, r_sym, dynrel,
3267 output_section, data_shndx,
3268 reloc.get_r_offset(),
3269 reloc.get_r_addend(), false);
3273 check_non_pic(object, r_type);
3274 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3275 rela_dyn->add_local(object, r_sym, r_type, output_section,
3276 data_shndx, reloc.get_r_offset(),
3277 reloc.get_r_addend());
3282 case elfcpp::R_PPC64_REL64:
3283 case elfcpp::R_POWERPC_REL32:
3284 case elfcpp::R_POWERPC_REL24:
3285 case elfcpp::R_PPC_PLTREL24:
3286 case elfcpp::R_PPC_LOCAL24PC:
3287 case elfcpp::R_POWERPC_REL16:
3288 case elfcpp::R_POWERPC_REL16_LO:
3289 case elfcpp::R_POWERPC_REL16_HI:
3290 case elfcpp::R_POWERPC_REL16_HA:
3291 case elfcpp::R_POWERPC_REL14:
3292 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3293 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3294 case elfcpp::R_POWERPC_SECTOFF:
3295 case elfcpp::R_POWERPC_TPREL16:
3296 case elfcpp::R_POWERPC_DTPREL16:
3297 case elfcpp::R_POWERPC_SECTOFF_LO:
3298 case elfcpp::R_POWERPC_TPREL16_LO:
3299 case elfcpp::R_POWERPC_DTPREL16_LO:
3300 case elfcpp::R_POWERPC_SECTOFF_HI:
3301 case elfcpp::R_POWERPC_TPREL16_HI:
3302 case elfcpp::R_POWERPC_DTPREL16_HI:
3303 case elfcpp::R_POWERPC_SECTOFF_HA:
3304 case elfcpp::R_POWERPC_TPREL16_HA:
3305 case elfcpp::R_POWERPC_DTPREL16_HA:
3306 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3307 case elfcpp::R_PPC64_TPREL16_HIGHER:
3308 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3309 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3310 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3311 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3312 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3313 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3314 case elfcpp::R_PPC64_TPREL16_DS:
3315 case elfcpp::R_PPC64_TPREL16_LO_DS:
3316 case elfcpp::R_PPC64_DTPREL16_DS:
3317 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3318 case elfcpp::R_PPC64_SECTOFF_DS:
3319 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3320 case elfcpp::R_PPC64_TLSGD:
3321 case elfcpp::R_PPC64_TLSLD:
3324 case elfcpp::R_POWERPC_GOT16:
3325 case elfcpp::R_POWERPC_GOT16_LO:
3326 case elfcpp::R_POWERPC_GOT16_HI:
3327 case elfcpp::R_POWERPC_GOT16_HA:
3328 case elfcpp::R_PPC64_GOT16_DS:
3329 case elfcpp::R_PPC64_GOT16_LO_DS:
3331 // The symbol requires a GOT entry.
3332 Output_data_got_powerpc<size, big_endian>* got
3333 = target->got_section(symtab, layout);
3334 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3336 if (!parameters->options().output_is_position_independent())
3338 if (size == 32 && is_ifunc)
3339 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
3341 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3343 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
3345 // If we are generating a shared object or a pie, this
3346 // symbol's GOT entry will be set by a dynamic relocation.
3348 off = got->add_constant(0);
3349 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
3351 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3352 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3355 rela_dyn = target->iplt_section()->rel_plt();
3356 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3358 rela_dyn->add_local_relative(object, r_sym, dynrel,
3359 got, off, 0, false);
3364 case elfcpp::R_PPC64_TOC16:
3365 case elfcpp::R_PPC64_TOC16_LO:
3366 case elfcpp::R_PPC64_TOC16_HI:
3367 case elfcpp::R_PPC64_TOC16_HA:
3368 case elfcpp::R_PPC64_TOC16_DS:
3369 case elfcpp::R_PPC64_TOC16_LO_DS:
3370 // We need a GOT section.
3371 target->got_section(symtab, layout);
3374 case elfcpp::R_POWERPC_GOT_TLSGD16:
3375 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3376 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3377 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3379 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
3380 if (tls_type == tls::TLSOPT_NONE)
3382 Output_data_got_powerpc<size, big_endian>* got
3383 = target->got_section(symtab, layout);
3384 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3385 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3386 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
3387 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
3389 else if (tls_type == tls::TLSOPT_TO_LE)
3391 // no GOT relocs needed for Local Exec.
3398 case elfcpp::R_POWERPC_GOT_TLSLD16:
3399 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3400 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3401 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3403 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3404 if (tls_type == tls::TLSOPT_NONE)
3405 target->tlsld_got_offset(symtab, layout, object);
3406 else if (tls_type == tls::TLSOPT_TO_LE)
3408 // no GOT relocs needed for Local Exec.
3409 if (parameters->options().emit_relocs())
3411 Output_section* os = layout->tls_segment()->first_section();
3412 gold_assert(os != NULL);
3413 os->set_needs_symtab_index();
3421 case elfcpp::R_POWERPC_GOT_DTPREL16:
3422 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3423 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3424 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3426 Output_data_got_powerpc<size, big_endian>* got
3427 = target->got_section(symtab, layout);
3428 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3429 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
3433 case elfcpp::R_POWERPC_GOT_TPREL16:
3434 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3435 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3436 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3438 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
3439 if (tls_type == tls::TLSOPT_NONE)
3441 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3442 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
3444 Output_data_got_powerpc<size, big_endian>* got
3445 = target->got_section(symtab, layout);
3446 unsigned int off = got->add_constant(0);
3447 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
3449 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3450 rela_dyn->add_symbolless_local_addend(object, r_sym,
3451 elfcpp::R_POWERPC_TPREL,
3455 else if (tls_type == tls::TLSOPT_TO_LE)
3457 // no GOT relocs needed for Local Exec.
3465 unsupported_reloc_local(object, r_type);
3470 // Report an unsupported relocation against a global symbol.
3472 template<int size, bool big_endian>
3474 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
3475 Sized_relobj_file<size, big_endian>* object,
3476 unsigned int r_type,
3479 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3480 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3483 // Scan a relocation for a global symbol.
3485 template<int size, bool big_endian>
3487 Target_powerpc<size, big_endian>::Scan::global(
3488 Symbol_table* symtab,
3490 Target_powerpc<size, big_endian>* target,
3491 Sized_relobj_file<size, big_endian>* object,
3492 unsigned int data_shndx,
3493 Output_section* output_section,
3494 const elfcpp::Rela<size, big_endian>& reloc,
3495 unsigned int r_type,
3498 Powerpc_relobj<size, big_endian>* ppc_object
3499 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3501 // A STT_GNU_IFUNC symbol may require a PLT entry.
3502 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3503 && this->reloc_needs_plt_for_ifunc(object, r_type))
3504 target->make_plt_entry(layout, gsym, reloc, object);
3508 case elfcpp::R_POWERPC_NONE:
3509 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3510 case elfcpp::R_POWERPC_GNU_VTENTRY:
3511 case elfcpp::R_PPC_LOCAL24PC:
3512 case elfcpp::R_PPC_EMB_MRKREF:
3513 case elfcpp::R_POWERPC_TLS:
3516 case elfcpp::R_PPC64_TOC:
3518 Output_data_got_powerpc<size, big_endian>* got
3519 = target->got_section(symtab, layout);
3520 if (parameters->options().output_is_position_independent())
3522 Address off = reloc.get_r_offset();
3524 && data_shndx == ppc_object->opd_shndx()
3525 && ppc_object->get_opd_discard(off - 8))
3528 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3529 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
3530 if (data_shndx != ppc_object->opd_shndx())
3531 symobj = static_cast
3532 <Powerpc_relobj<size, big_endian>*>(gsym->object());
3533 rela_dyn->add_output_section_relative(got->output_section(),
3534 elfcpp::R_POWERPC_RELATIVE,
3536 object, data_shndx, off,
3537 symobj->toc_base_offset());
3542 case elfcpp::R_PPC64_ADDR64:
3544 && data_shndx == ppc_object->opd_shndx()
3545 && (gsym->is_defined_in_discarded_section()
3546 || gsym->object() != object))
3548 ppc_object->set_opd_discard(reloc.get_r_offset());
3552 case elfcpp::R_PPC64_UADDR64:
3553 case elfcpp::R_POWERPC_ADDR32:
3554 case elfcpp::R_POWERPC_UADDR32:
3555 case elfcpp::R_POWERPC_ADDR24:
3556 case elfcpp::R_POWERPC_ADDR16:
3557 case elfcpp::R_POWERPC_ADDR16_LO:
3558 case elfcpp::R_POWERPC_ADDR16_HI:
3559 case elfcpp::R_POWERPC_ADDR16_HA:
3560 case elfcpp::R_POWERPC_UADDR16:
3561 case elfcpp::R_PPC64_ADDR16_HIGHER:
3562 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3563 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3564 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3565 case elfcpp::R_PPC64_ADDR16_DS:
3566 case elfcpp::R_PPC64_ADDR16_LO_DS:
3567 case elfcpp::R_POWERPC_ADDR14:
3568 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3569 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3571 // Make a PLT entry if necessary.
3572 if (gsym->needs_plt_entry())
3574 target->make_plt_entry(layout, gsym, reloc, 0);
3575 // Since this is not a PC-relative relocation, we may be
3576 // taking the address of a function. In that case we need to
3577 // set the entry in the dynamic symbol table to the address of
3578 // the PLT call stub.
3580 && gsym->is_from_dynobj()
3581 && !parameters->options().output_is_position_independent())
3582 gsym->set_needs_dynsym_value();
3584 // Make a dynamic relocation if necessary.
3585 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
3586 || (size == 64 && gsym->type() == elfcpp::STT_GNU_IFUNC))
3588 if (gsym->may_need_copy_reloc())
3590 target->copy_reloc(symtab, layout, object,
3591 data_shndx, output_section, gsym, reloc);
3593 else if (((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
3594 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
3595 && (gsym->can_use_relative_reloc(false)
3597 && data_shndx == ppc_object->opd_shndx())))
3599 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3600 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3601 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3603 rela_dyn = target->iplt_section()->rel_plt();
3604 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3606 rela_dyn->add_symbolless_global_addend(
3607 gsym, dynrel, output_section, object, data_shndx,
3608 reloc.get_r_offset(), reloc.get_r_addend());
3612 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3613 check_non_pic(object, r_type);
3614 rela_dyn->add_global(gsym, r_type, output_section,
3616 reloc.get_r_offset(),
3617 reloc.get_r_addend());
3623 case elfcpp::R_PPC_PLTREL24:
3624 case elfcpp::R_POWERPC_REL24:
3625 if (gsym->needs_plt_entry()
3626 || (!gsym->final_value_is_known()
3627 && (gsym->is_undefined()
3628 || gsym->is_from_dynobj()
3629 || gsym->is_preemptible())))
3630 target->make_plt_entry(layout, gsym, reloc, object);
3633 case elfcpp::R_PPC64_REL64:
3634 case elfcpp::R_POWERPC_REL32:
3635 // Make a dynamic relocation if necessary.
3636 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
3638 if (gsym->may_need_copy_reloc())
3640 target->copy_reloc(symtab, layout, object,
3641 data_shndx, output_section, gsym,
3646 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3647 check_non_pic(object, r_type);
3648 rela_dyn->add_global(gsym, r_type, output_section, object,
3649 data_shndx, reloc.get_r_offset(),
3650 reloc.get_r_addend());
3655 case elfcpp::R_POWERPC_REL16:
3656 case elfcpp::R_POWERPC_REL16_LO:
3657 case elfcpp::R_POWERPC_REL16_HI:
3658 case elfcpp::R_POWERPC_REL16_HA:
3659 case elfcpp::R_POWERPC_REL14:
3660 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3661 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3662 case elfcpp::R_POWERPC_SECTOFF:
3663 case elfcpp::R_POWERPC_TPREL16:
3664 case elfcpp::R_POWERPC_DTPREL16:
3665 case elfcpp::R_POWERPC_SECTOFF_LO:
3666 case elfcpp::R_POWERPC_TPREL16_LO:
3667 case elfcpp::R_POWERPC_DTPREL16_LO:
3668 case elfcpp::R_POWERPC_SECTOFF_HI:
3669 case elfcpp::R_POWERPC_TPREL16_HI:
3670 case elfcpp::R_POWERPC_DTPREL16_HI:
3671 case elfcpp::R_POWERPC_SECTOFF_HA:
3672 case elfcpp::R_POWERPC_TPREL16_HA:
3673 case elfcpp::R_POWERPC_DTPREL16_HA:
3674 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3675 case elfcpp::R_PPC64_TPREL16_HIGHER:
3676 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3677 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3678 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3679 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3680 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3681 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3682 case elfcpp::R_PPC64_TPREL16_DS:
3683 case elfcpp::R_PPC64_TPREL16_LO_DS:
3684 case elfcpp::R_PPC64_DTPREL16_DS:
3685 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3686 case elfcpp::R_PPC64_SECTOFF_DS:
3687 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3688 case elfcpp::R_PPC64_TLSGD:
3689 case elfcpp::R_PPC64_TLSLD:
3692 case elfcpp::R_POWERPC_GOT16:
3693 case elfcpp::R_POWERPC_GOT16_LO:
3694 case elfcpp::R_POWERPC_GOT16_HI:
3695 case elfcpp::R_POWERPC_GOT16_HA:
3696 case elfcpp::R_PPC64_GOT16_DS:
3697 case elfcpp::R_PPC64_GOT16_LO_DS:
3699 // The symbol requires a GOT entry.
3700 Output_data_got_powerpc<size, big_endian>* got;
3702 got = target->got_section(symtab, layout);
3703 if (gsym->final_value_is_known())
3705 if (size == 32 && gsym->type() == elfcpp::STT_GNU_IFUNC)
3706 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3708 got->add_global(gsym, GOT_TYPE_STANDARD);
3710 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
3712 // If we are generating a shared object or a pie, this
3713 // symbol's GOT entry will be set by a dynamic relocation.
3714 unsigned int off = got->add_constant(0);
3715 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
3717 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3718 if (gsym->can_use_relative_reloc(false)
3720 && gsym->visibility() == elfcpp::STV_PROTECTED
3721 && parameters->options().shared()))
3723 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3724 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3726 rela_dyn = target->iplt_section()->rel_plt();
3727 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3729 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
3733 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
3734 rela_dyn->add_global(gsym, dynrel, got, off, 0);
3740 case elfcpp::R_PPC64_TOC16:
3741 case elfcpp::R_PPC64_TOC16_LO:
3742 case elfcpp::R_PPC64_TOC16_HI:
3743 case elfcpp::R_PPC64_TOC16_HA:
3744 case elfcpp::R_PPC64_TOC16_DS:
3745 case elfcpp::R_PPC64_TOC16_LO_DS:
3746 // We need a GOT section.
3747 target->got_section(symtab, layout);
3750 case elfcpp::R_POWERPC_GOT_TLSGD16:
3751 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3752 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3753 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3755 const bool final = gsym->final_value_is_known();
3756 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3757 if (tls_type == tls::TLSOPT_NONE)
3759 Output_data_got_powerpc<size, big_endian>* got
3760 = target->got_section(symtab, layout);
3761 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
3762 target->rela_dyn_section(layout),
3763 elfcpp::R_POWERPC_DTPMOD,
3764 elfcpp::R_POWERPC_DTPREL);
3766 else if (tls_type == tls::TLSOPT_TO_IE)
3768 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
3770 Output_data_got_powerpc<size, big_endian>* got
3771 = target->got_section(symtab, layout);
3772 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3773 if (gsym->is_undefined()
3774 || gsym->is_from_dynobj())
3776 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
3777 elfcpp::R_POWERPC_TPREL);
3781 unsigned int off = got->add_constant(0);
3782 gsym->set_got_offset(GOT_TYPE_TPREL, off);
3783 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
3784 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
3789 else if (tls_type == tls::TLSOPT_TO_LE)
3791 // no GOT relocs needed for Local Exec.
3798 case elfcpp::R_POWERPC_GOT_TLSLD16:
3799 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3800 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3801 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3803 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3804 if (tls_type == tls::TLSOPT_NONE)
3805 target->tlsld_got_offset(symtab, layout, object);
3806 else if (tls_type == tls::TLSOPT_TO_LE)
3808 // no GOT relocs needed for Local Exec.
3809 if (parameters->options().emit_relocs())
3811 Output_section* os = layout->tls_segment()->first_section();
3812 gold_assert(os != NULL);
3813 os->set_needs_symtab_index();
3821 case elfcpp::R_POWERPC_GOT_DTPREL16:
3822 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3823 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3824 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3826 Output_data_got_powerpc<size, big_endian>* got
3827 = target->got_section(symtab, layout);
3828 if (!gsym->final_value_is_known()
3829 && (gsym->is_from_dynobj()
3830 || gsym->is_undefined()
3831 || gsym->is_preemptible()))
3832 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
3833 target->rela_dyn_section(layout),
3834 elfcpp::R_POWERPC_DTPREL);
3836 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
3840 case elfcpp::R_POWERPC_GOT_TPREL16:
3841 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3842 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3843 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3845 const bool final = gsym->final_value_is_known();
3846 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3847 if (tls_type == tls::TLSOPT_NONE)
3849 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
3851 Output_data_got_powerpc<size, big_endian>* got
3852 = target->got_section(symtab, layout);
3853 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3854 if (gsym->is_undefined()
3855 || gsym->is_from_dynobj())
3857 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
3858 elfcpp::R_POWERPC_TPREL);
3862 unsigned int off = got->add_constant(0);
3863 gsym->set_got_offset(GOT_TYPE_TPREL, off);
3864 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
3865 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
3870 else if (tls_type == tls::TLSOPT_TO_LE)
3872 // no GOT relocs needed for Local Exec.
3880 unsupported_reloc_global(object, r_type, gsym);
3885 // Process relocations for gc.
3887 template<int size, bool big_endian>
3889 Target_powerpc<size, big_endian>::gc_process_relocs(
3890 Symbol_table* symtab,
3892 Sized_relobj_file<size, big_endian>* object,
3893 unsigned int data_shndx,
3895 const unsigned char* prelocs,
3897 Output_section* output_section,
3898 bool needs_special_offset_handling,
3899 size_t local_symbol_count,
3900 const unsigned char* plocal_symbols)
3902 typedef Target_powerpc<size, big_endian> Powerpc;
3903 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
3904 Powerpc_relobj<size, big_endian>* ppc_object
3905 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3907 ppc_object->set_opd_valid();
3908 if (size == 64 && data_shndx == ppc_object->opd_shndx())
3910 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
3911 for (p = ppc_object->access_from_map()->begin();
3912 p != ppc_object->access_from_map()->end();
3915 Address dst_off = p->first;
3916 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
3917 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
3918 for (s = p->second.begin(); s != p->second.end(); ++s)
3920 Object* src_obj = s->first;
3921 unsigned int src_indx = s->second;
3922 symtab->gc()->add_reference(src_obj, src_indx,
3923 ppc_object, dst_indx);
3927 ppc_object->access_from_map()->clear();
3928 ppc_object->process_gc_mark(symtab);
3929 // Don't look at .opd relocs as .opd will reference everything.
3933 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
3934 typename Target_powerpc::Relocatable_size_for_reloc>(
3943 needs_special_offset_handling,
3948 // Handle target specific gc actions when adding a gc reference from
3949 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
3950 // and DST_OFF. For powerpc64, this adds a referenc to the code
3951 // section of a function descriptor.
3953 template<int size, bool big_endian>
3955 Target_powerpc<size, big_endian>::do_gc_add_reference(
3956 Symbol_table* symtab,
3958 unsigned int src_shndx,
3960 unsigned int dst_shndx,
3961 Address dst_off) const
3963 Powerpc_relobj<size, big_endian>* ppc_object
3964 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
3966 && !ppc_object->is_dynamic()
3967 && dst_shndx == ppc_object->opd_shndx())
3969 if (ppc_object->opd_valid())
3971 dst_shndx = ppc_object->get_opd_ent(dst_off);
3972 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
3976 // If we haven't run scan_opd_relocs, we must delay
3977 // processing this function descriptor reference.
3978 ppc_object->add_reference(src_obj, src_shndx, dst_off);
3983 // Add any special sections for this symbol to the gc work list.
3984 // For powerpc64, this adds the code section of a function
3987 template<int size, bool big_endian>
3989 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
3990 Symbol_table* symtab,
3995 Powerpc_relobj<size, big_endian>* ppc_object
3996 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
3998 unsigned int shndx = sym->shndx(&is_ordinary);
3999 if (is_ordinary && shndx == ppc_object->opd_shndx())
4001 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
4002 Address dst_off = gsym->value();
4003 if (ppc_object->opd_valid())
4005 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
4006 symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
4009 ppc_object->add_gc_mark(dst_off);
4014 // Scan relocations for a section.
4016 template<int size, bool big_endian>
4018 Target_powerpc<size, big_endian>::scan_relocs(
4019 Symbol_table* symtab,
4021 Sized_relobj_file<size, big_endian>* object,
4022 unsigned int data_shndx,
4023 unsigned int sh_type,
4024 const unsigned char* prelocs,
4026 Output_section* output_section,
4027 bool needs_special_offset_handling,
4028 size_t local_symbol_count,
4029 const unsigned char* plocal_symbols)
4031 typedef Target_powerpc<size, big_endian> Powerpc;
4032 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
4034 if (sh_type == elfcpp::SHT_REL)
4036 gold_error(_("%s: unsupported REL reloc section"),
4037 object->name().c_str());
4043 // Define a weak hidden _GLOBAL_OFFSET_TABLE_ to ensure it isn't
4044 // seen as undefined when scanning relocs (and thus requires
4045 // non-relative dynamic relocs). The proper value will be
4047 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
4048 if (gotsym != NULL && gotsym->is_undefined())
4049 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
4050 Symbol_table::PREDEFINED,
4051 this->got_section(symtab, layout), 0, 0,
4054 elfcpp::STV_HIDDEN, 0,
4057 static Output_data_space* sdata;
4059 // Define _SDA_BASE_ at the start of the .sdata section.
4062 // layout->find_output_section(".sdata") == NULL
4063 sdata = new Output_data_space(4, "** sdata");
4065 = layout->add_output_section_data(".sdata", 0,
4067 | elfcpp::SHF_WRITE,
4068 sdata, ORDER_SMALL_DATA, false);
4069 symtab->define_in_output_data("_SDA_BASE_", NULL,
4070 Symbol_table::PREDEFINED,
4071 os, 32768, 0, elfcpp::STT_OBJECT,
4072 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
4077 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
4086 needs_special_offset_handling,
4091 // Functor class for processing the global symbol table.
4092 // Removes symbols defined on discarded opd entries.
4094 template<bool big_endian>
4095 class Global_symbol_visitor_opd
4098 Global_symbol_visitor_opd()
4102 operator()(Sized_symbol<64>* sym)
4104 if (sym->has_symtab_index()
4105 || sym->source() != Symbol::FROM_OBJECT
4106 || !sym->in_real_elf())
4109 Powerpc_relobj<64, big_endian>* symobj
4110 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
4111 if (symobj->is_dynamic()
4112 || symobj->opd_shndx() == 0)
4116 unsigned int shndx = sym->shndx(&is_ordinary);
4117 if (shndx == symobj->opd_shndx()
4118 && symobj->get_opd_discard(sym->value()))
4119 sym->set_symtab_index(-1U);
4123 template<int size, bool big_endian>
4125 Target_powerpc<size, big_endian>::define_save_restore_funcs(
4127 Symbol_table* symtab)
4131 Output_data_save_res<64, big_endian>* savres
4132 = new Output_data_save_res<64, big_endian>(symtab);
4133 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
4134 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
4135 savres, ORDER_TEXT, false);
4139 // Finalize the sections.
4141 template<int size, bool big_endian>
4143 Target_powerpc<size, big_endian>::do_finalize_sections(
4145 const Input_objects*,
4146 Symbol_table* symtab)
4148 if (parameters->doing_static_link())
4150 // At least some versions of glibc elf-init.o have a strong
4151 // reference to __rela_iplt marker syms. A weak ref would be
4153 if (this->iplt_ != NULL)
4155 Reloc_section* rel = this->iplt_->rel_plt();
4156 symtab->define_in_output_data("__rela_iplt_start", NULL,
4157 Symbol_table::PREDEFINED, rel, 0, 0,
4158 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4159 elfcpp::STV_HIDDEN, 0, false, true);
4160 symtab->define_in_output_data("__rela_iplt_end", NULL,
4161 Symbol_table::PREDEFINED, rel, 0, 0,
4162 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4163 elfcpp::STV_HIDDEN, 0, true, true);
4167 symtab->define_as_constant("__rela_iplt_start", NULL,
4168 Symbol_table::PREDEFINED, 0, 0,
4169 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4170 elfcpp::STV_HIDDEN, 0, true, false);
4171 symtab->define_as_constant("__rela_iplt_end", NULL,
4172 Symbol_table::PREDEFINED, 0, 0,
4173 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4174 elfcpp::STV_HIDDEN, 0, true, false);
4180 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
4181 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
4182 this->define_save_restore_funcs(layout, symtab);
4185 // Fill in some more dynamic tags.
4186 Output_data_dynamic* odyn = layout->dynamic_data();
4189 const Reloc_section* rel_plt = (this->plt_ == NULL
4191 : this->plt_->rel_plt());
4192 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
4193 this->rela_dyn_, true, size == 32);
4197 if (this->got_ != NULL)
4199 this->got_->finalize_data_size();
4200 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
4201 this->got_, this->got_->g_o_t());
4206 if (this->glink_ != NULL)
4208 this->glink_->finalize_data_size();
4209 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
4211 (this->glink_->pltresolve()
4212 + this->glink_->pltresolve_size
4218 // Emit any relocs we saved in an attempt to avoid generating COPY
4220 if (this->copy_relocs_.any_saved_relocs())
4221 this->copy_relocs_.emit(this->rela_dyn_section(layout));
4224 // Return the value to use for a branch relocation.
4226 template<int size, bool big_endian>
4227 typename elfcpp::Elf_types<size>::Elf_Addr
4228 Target_powerpc<size, big_endian>::symval_for_branch(
4230 const Sized_symbol<size>* gsym,
4231 Powerpc_relobj<size, big_endian>* object,
4232 unsigned int *dest_shndx)
4238 // If the symbol is defined in an opd section, ie. is a function
4239 // descriptor, use the function descriptor code entry address
4240 Powerpc_relobj<size, big_endian>* symobj = object;
4242 && gsym->source() != Symbol::FROM_OBJECT)
4245 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
4246 unsigned int shndx = symobj->opd_shndx();
4249 Address opd_addr = symobj->get_output_section_offset(shndx);
4250 gold_assert(opd_addr != invalid_address);
4251 opd_addr += symobj->output_section(shndx)->address();
4252 if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
4255 *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
4256 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
4257 gold_assert(sec_addr != invalid_address);
4258 sec_addr += symobj->output_section(*dest_shndx)->address();
4259 value = sec_addr + sec_off;
4264 // Perform a relocation.
4266 template<int size, bool big_endian>
4268 Target_powerpc<size, big_endian>::Relocate::relocate(
4269 const Relocate_info<size, big_endian>* relinfo,
4270 Target_powerpc* target,
4273 const elfcpp::Rela<size, big_endian>& rela,
4274 unsigned int r_type,
4275 const Sized_symbol<size>* gsym,
4276 const Symbol_value<size>* psymval,
4277 unsigned char* view,
4279 section_size_type view_size)
4282 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
4283 || r_type == elfcpp::R_PPC_PLTREL24)
4285 && strcmp(gsym->name(), "__tls_get_addr") == 0);
4286 enum skip_tls last_tls = this->call_tls_get_addr_;
4287 this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
4290 if (last_tls == CALL_NOT_EXPECTED)
4291 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4292 _("__tls_get_addr call lacks marker reloc"));
4293 else if (last_tls == CALL_SKIP)
4296 else if (last_tls != CALL_NOT_EXPECTED)
4297 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4298 _("missing expected __tls_get_addr call"));
4300 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
4301 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
4302 Powerpc_relobj<size, big_endian>* const object
4303 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
4305 bool has_plt_value = false;
4306 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4308 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
4309 : object->local_has_plt_offset(r_sym))
4311 const Output_data_glink<size, big_endian>* glink
4312 = target->glink_section();
4313 unsigned int glink_index;
4315 glink_index = glink->find_entry(object, gsym, rela);
4317 glink_index = glink->find_entry(object, r_sym, rela);
4318 value = glink->address() + glink_index * glink->glink_entry_size();
4319 has_plt_value = true;
4322 if (r_type == elfcpp::R_POWERPC_GOT16
4323 || r_type == elfcpp::R_POWERPC_GOT16_LO
4324 || r_type == elfcpp::R_POWERPC_GOT16_HI
4325 || r_type == elfcpp::R_POWERPC_GOT16_HA
4326 || r_type == elfcpp::R_PPC64_GOT16_DS
4327 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
4331 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4332 value = gsym->got_offset(GOT_TYPE_STANDARD);
4336 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4337 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4338 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
4340 value -= target->got_section()->got_base_offset(object);
4342 else if (r_type == elfcpp::R_PPC64_TOC)
4344 value = (target->got_section()->output_section()->address()
4345 + object->toc_base_offset());
4347 else if (gsym != NULL
4348 && (r_type == elfcpp::R_POWERPC_REL24
4349 || r_type == elfcpp::R_PPC_PLTREL24)
4354 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4355 Valtype* wv = reinterpret_cast<Valtype*>(view);
4356 bool can_plt_call = false;
4357 if (rela.get_r_offset() + 8 <= view_size)
4359 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
4360 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
4363 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
4365 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
4366 can_plt_call = true;
4371 // If we don't have a branch and link followed by a nop,
4372 // we can't go via the plt because there is no place to
4373 // put a toc restoring instruction.
4374 // Unless we know we won't be returning.
4375 if (strcmp(gsym->name(), "__libc_start_main") == 0)
4376 can_plt_call = true;
4380 // This is not an error in one special case: A self
4381 // call. It isn't possible to cheaply verify we have
4382 // such a call so just check for a call to the same
4385 Address code = value;
4386 if (gsym->source() == Symbol::FROM_OBJECT
4387 && gsym->object() == object)
4389 Address addend = rela.get_r_addend();
4390 unsigned int dest_shndx;
4391 Address opdent = psymval->value(object, addend);
4392 code = target->symval_for_branch(opdent, gsym, object,
4395 if (dest_shndx == 0)
4396 dest_shndx = gsym->shndx(&is_ordinary);
4397 ok = dest_shndx == relinfo->data_shndx;
4401 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4402 _("call lacks nop, can't restore toc; "
4403 "recompile with -fPIC"));
4409 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4410 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
4411 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
4412 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
4414 // First instruction of a global dynamic sequence, arg setup insn.
4415 const bool final = gsym == NULL || gsym->final_value_is_known();
4416 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
4417 enum Got_type got_type = GOT_TYPE_STANDARD;
4418 if (tls_type == tls::TLSOPT_NONE)
4419 got_type = GOT_TYPE_TLSGD;
4420 else if (tls_type == tls::TLSOPT_TO_IE)
4421 got_type = GOT_TYPE_TPREL;
4422 if (got_type != GOT_TYPE_STANDARD)
4426 gold_assert(gsym->has_got_offset(got_type));
4427 value = gsym->got_offset(got_type);
4431 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4432 gold_assert(object->local_has_got_offset(r_sym, got_type));
4433 value = object->local_got_offset(r_sym, got_type);
4435 value -= target->got_section()->got_base_offset(object);
4437 if (tls_type == tls::TLSOPT_TO_IE)
4439 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4440 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4442 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4443 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4444 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
4446 insn |= 32 << 26; // lwz
4448 insn |= 58 << 26; // ld
4449 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4451 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
4452 - elfcpp::R_POWERPC_GOT_TLSGD16);
4454 else if (tls_type == tls::TLSOPT_TO_LE)
4456 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4457 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4459 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4460 Insn insn = addis_3_13;
4463 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4464 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4465 value = psymval->value(object, rela.get_r_addend());
4469 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4471 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4472 r_type = elfcpp::R_POWERPC_NONE;
4476 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4477 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
4478 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
4479 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
4481 // First instruction of a local dynamic sequence, arg setup insn.
4482 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4483 if (tls_type == tls::TLSOPT_NONE)
4485 value = target->tlsld_got_offset();
4486 value -= target->got_section()->got_base_offset(object);
4490 gold_assert(tls_type == tls::TLSOPT_TO_LE);
4491 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4492 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
4494 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4495 Insn insn = addis_3_13;
4498 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4499 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4504 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4506 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4507 r_type = elfcpp::R_POWERPC_NONE;
4511 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
4512 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
4513 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
4514 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
4516 // Accesses relative to a local dynamic sequence address,
4517 // no optimisation here.
4520 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
4521 value = gsym->got_offset(GOT_TYPE_DTPREL);
4525 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4526 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
4527 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
4529 value -= target->got_section()->got_base_offset(object);
4531 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4532 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
4533 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
4534 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
4536 // First instruction of initial exec sequence.
4537 const bool final = gsym == NULL || gsym->final_value_is_known();
4538 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
4539 if (tls_type == tls::TLSOPT_NONE)
4543 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
4544 value = gsym->got_offset(GOT_TYPE_TPREL);
4548 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4549 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
4550 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
4552 value -= target->got_section()->got_base_offset(object);
4556 gold_assert(tls_type == tls::TLSOPT_TO_LE);
4557 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4558 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
4560 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4561 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4562 insn &= (1 << 26) - (1 << 21); // extract rt from ld
4567 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4568 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4569 value = psymval->value(object, rela.get_r_addend());
4573 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4575 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4576 r_type = elfcpp::R_POWERPC_NONE;
4580 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4581 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4583 // Second instruction of a global dynamic sequence,
4584 // the __tls_get_addr call
4585 this->call_tls_get_addr_ = CALL_EXPECTED;
4586 const bool final = gsym == NULL || gsym->final_value_is_known();
4587 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
4588 if (tls_type != tls::TLSOPT_NONE)
4590 if (tls_type == tls::TLSOPT_TO_IE)
4592 Insn* iview = reinterpret_cast<Insn*>(view);
4593 Insn insn = add_3_3_13;
4596 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4597 r_type = elfcpp::R_POWERPC_NONE;
4601 Insn* iview = reinterpret_cast<Insn*>(view);
4602 Insn insn = addi_3_3;
4603 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4604 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4605 view += 2 * big_endian;
4606 value = psymval->value(object, rela.get_r_addend());
4608 this->call_tls_get_addr_ = CALL_SKIP;
4611 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4612 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4614 // Second instruction of a local dynamic sequence,
4615 // the __tls_get_addr call
4616 this->call_tls_get_addr_ = CALL_EXPECTED;
4617 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4618 if (tls_type == tls::TLSOPT_TO_LE)
4620 Insn* iview = reinterpret_cast<Insn*>(view);
4621 Insn insn = addi_3_3;
4622 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4623 this->call_tls_get_addr_ = CALL_SKIP;
4624 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4625 view += 2 * big_endian;
4629 else if (r_type == elfcpp::R_POWERPC_TLS)
4631 // Second instruction of an initial exec sequence
4632 const bool final = gsym == NULL || gsym->final_value_is_known();
4633 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
4634 if (tls_type == tls::TLSOPT_TO_LE)
4636 Insn* iview = reinterpret_cast<Insn*>(view);
4637 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4638 unsigned int reg = size == 32 ? 2 : 13;
4639 insn = at_tls_transform(insn, reg);
4640 gold_assert(insn != 0);
4641 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4642 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4643 view += 2 * big_endian;
4644 value = psymval->value(object, rela.get_r_addend());
4647 else if (!has_plt_value)
4650 unsigned int dest_shndx;
4651 if (r_type != elfcpp::R_PPC_PLTREL24)
4652 addend = rela.get_r_addend();
4653 value = psymval->value(object, addend);
4654 if (size == 64 && is_branch_reloc(r_type))
4655 value = target->symval_for_branch(value, gsym, object, &dest_shndx);
4660 case elfcpp::R_PPC64_REL64:
4661 case elfcpp::R_POWERPC_REL32:
4662 case elfcpp::R_POWERPC_REL24:
4663 case elfcpp::R_PPC_PLTREL24:
4664 case elfcpp::R_PPC_LOCAL24PC:
4665 case elfcpp::R_POWERPC_REL16:
4666 case elfcpp::R_POWERPC_REL16_LO:
4667 case elfcpp::R_POWERPC_REL16_HI:
4668 case elfcpp::R_POWERPC_REL16_HA:
4669 case elfcpp::R_POWERPC_REL14:
4670 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4671 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4675 case elfcpp::R_PPC64_TOC16:
4676 case elfcpp::R_PPC64_TOC16_LO:
4677 case elfcpp::R_PPC64_TOC16_HI:
4678 case elfcpp::R_PPC64_TOC16_HA:
4679 case elfcpp::R_PPC64_TOC16_DS:
4680 case elfcpp::R_PPC64_TOC16_LO_DS:
4681 // Subtract the TOC base address.
4682 value -= (target->got_section()->output_section()->address()
4683 + object->toc_base_offset());
4686 case elfcpp::R_POWERPC_SECTOFF:
4687 case elfcpp::R_POWERPC_SECTOFF_LO:
4688 case elfcpp::R_POWERPC_SECTOFF_HI:
4689 case elfcpp::R_POWERPC_SECTOFF_HA:
4690 case elfcpp::R_PPC64_SECTOFF_DS:
4691 case elfcpp::R_PPC64_SECTOFF_LO_DS:
4693 value -= os->address();
4696 case elfcpp::R_PPC64_TPREL16_DS:
4697 case elfcpp::R_PPC64_TPREL16_LO_DS:
4699 // R_PPC_TLSGD and R_PPC_TLSLD
4701 case elfcpp::R_POWERPC_TPREL16:
4702 case elfcpp::R_POWERPC_TPREL16_LO:
4703 case elfcpp::R_POWERPC_TPREL16_HI:
4704 case elfcpp::R_POWERPC_TPREL16_HA:
4705 case elfcpp::R_POWERPC_TPREL:
4706 case elfcpp::R_PPC64_TPREL16_HIGHER:
4707 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4708 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4709 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4710 // tls symbol values are relative to tls_segment()->vaddr()
4714 case elfcpp::R_PPC64_DTPREL16_DS:
4715 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4716 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4717 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4718 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4719 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4721 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
4722 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
4724 case elfcpp::R_POWERPC_DTPREL16:
4725 case elfcpp::R_POWERPC_DTPREL16_LO:
4726 case elfcpp::R_POWERPC_DTPREL16_HI:
4727 case elfcpp::R_POWERPC_DTPREL16_HA:
4728 case elfcpp::R_POWERPC_DTPREL:
4729 // tls symbol values are relative to tls_segment()->vaddr()
4730 value -= dtp_offset;
4737 Insn branch_bit = 0;
4740 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4741 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4742 branch_bit = 1 << 21;
4743 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4744 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4746 Insn* iview = reinterpret_cast<Insn*>(view);
4747 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4750 if (this->is_isa_v2)
4752 // Set 'a' bit. This is 0b00010 in BO field for branch
4753 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
4754 // for branch on CTR insns (BO == 1a00t or 1a01t).
4755 if ((insn & (0x14 << 21)) == (0x04 << 21))
4757 else if ((insn & (0x14 << 21)) == (0x10 << 21))
4764 // Invert 'y' bit if not the default.
4765 if (static_cast<Signed_address>(value) < 0)
4768 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4776 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
4779 case elfcpp::R_POWERPC_ADDR32:
4780 case elfcpp::R_POWERPC_UADDR32:
4782 overflow = Reloc::CHECK_BITFIELD;
4785 case elfcpp::R_POWERPC_REL32:
4787 overflow = Reloc::CHECK_SIGNED;
4790 case elfcpp::R_POWERPC_ADDR24:
4791 case elfcpp::R_POWERPC_ADDR16:
4792 case elfcpp::R_POWERPC_UADDR16:
4793 case elfcpp::R_PPC64_ADDR16_DS:
4794 case elfcpp::R_POWERPC_ADDR14:
4795 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4796 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4797 overflow = Reloc::CHECK_BITFIELD;
4800 case elfcpp::R_POWERPC_REL24:
4801 case elfcpp::R_PPC_PLTREL24:
4802 case elfcpp::R_PPC_LOCAL24PC:
4803 case elfcpp::R_POWERPC_REL16:
4804 case elfcpp::R_PPC64_TOC16:
4805 case elfcpp::R_POWERPC_GOT16:
4806 case elfcpp::R_POWERPC_SECTOFF:
4807 case elfcpp::R_POWERPC_TPREL16:
4808 case elfcpp::R_POWERPC_DTPREL16:
4809 case elfcpp::R_PPC64_TPREL16_DS:
4810 case elfcpp::R_PPC64_DTPREL16_DS:
4811 case elfcpp::R_PPC64_TOC16_DS:
4812 case elfcpp::R_PPC64_GOT16_DS:
4813 case elfcpp::R_PPC64_SECTOFF_DS:
4814 case elfcpp::R_POWERPC_REL14:
4815 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4816 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4817 case elfcpp::R_POWERPC_GOT_TLSGD16:
4818 case elfcpp::R_POWERPC_GOT_TLSLD16:
4819 case elfcpp::R_POWERPC_GOT_TPREL16:
4820 case elfcpp::R_POWERPC_GOT_DTPREL16:
4821 overflow = Reloc::CHECK_SIGNED;
4825 typename Powerpc_relocate_functions<size, big_endian>::Status status
4826 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
4829 case elfcpp::R_POWERPC_NONE:
4830 case elfcpp::R_POWERPC_TLS:
4831 case elfcpp::R_POWERPC_GNU_VTINHERIT:
4832 case elfcpp::R_POWERPC_GNU_VTENTRY:
4833 case elfcpp::R_PPC_EMB_MRKREF:
4836 case elfcpp::R_PPC64_ADDR64:
4837 case elfcpp::R_PPC64_REL64:
4838 case elfcpp::R_PPC64_TOC:
4839 Reloc::addr64(view, value);
4842 case elfcpp::R_POWERPC_TPREL:
4843 case elfcpp::R_POWERPC_DTPREL:
4845 Reloc::addr64(view, value);
4847 status = Reloc::addr32(view, value, overflow);
4850 case elfcpp::R_PPC64_UADDR64:
4851 Reloc::addr64_u(view, value);
4854 case elfcpp::R_POWERPC_ADDR32:
4855 status = Reloc::addr32(view, value, overflow);
4858 case elfcpp::R_POWERPC_REL32:
4859 case elfcpp::R_POWERPC_UADDR32:
4860 status = Reloc::addr32_u(view, value, overflow);
4863 case elfcpp::R_POWERPC_ADDR24:
4864 case elfcpp::R_POWERPC_REL24:
4865 case elfcpp::R_PPC_PLTREL24:
4866 case elfcpp::R_PPC_LOCAL24PC:
4867 status = Reloc::addr24(view, value, overflow);
4870 case elfcpp::R_POWERPC_GOT_DTPREL16:
4871 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
4874 status = Reloc::addr16_ds(view, value, overflow);
4877 case elfcpp::R_POWERPC_ADDR16:
4878 case elfcpp::R_POWERPC_REL16:
4879 case elfcpp::R_PPC64_TOC16:
4880 case elfcpp::R_POWERPC_GOT16:
4881 case elfcpp::R_POWERPC_SECTOFF:
4882 case elfcpp::R_POWERPC_TPREL16:
4883 case elfcpp::R_POWERPC_DTPREL16:
4884 case elfcpp::R_POWERPC_GOT_TLSGD16:
4885 case elfcpp::R_POWERPC_GOT_TLSLD16:
4886 case elfcpp::R_POWERPC_GOT_TPREL16:
4887 case elfcpp::R_POWERPC_ADDR16_LO:
4888 case elfcpp::R_POWERPC_REL16_LO:
4889 case elfcpp::R_PPC64_TOC16_LO:
4890 case elfcpp::R_POWERPC_GOT16_LO:
4891 case elfcpp::R_POWERPC_SECTOFF_LO:
4892 case elfcpp::R_POWERPC_TPREL16_LO:
4893 case elfcpp::R_POWERPC_DTPREL16_LO:
4894 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
4895 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
4896 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
4897 status = Reloc::addr16(view, value, overflow);
4900 case elfcpp::R_POWERPC_UADDR16:
4901 status = Reloc::addr16_u(view, value, overflow);
4904 case elfcpp::R_POWERPC_ADDR16_HI:
4905 case elfcpp::R_POWERPC_REL16_HI:
4906 case elfcpp::R_PPC64_TOC16_HI:
4907 case elfcpp::R_POWERPC_GOT16_HI:
4908 case elfcpp::R_POWERPC_SECTOFF_HI:
4909 case elfcpp::R_POWERPC_TPREL16_HI:
4910 case elfcpp::R_POWERPC_DTPREL16_HI:
4911 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
4912 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
4913 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
4914 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
4915 Reloc::addr16_hi(view, value);
4918 case elfcpp::R_POWERPC_ADDR16_HA:
4919 case elfcpp::R_POWERPC_REL16_HA:
4920 case elfcpp::R_PPC64_TOC16_HA:
4921 case elfcpp::R_POWERPC_GOT16_HA:
4922 case elfcpp::R_POWERPC_SECTOFF_HA:
4923 case elfcpp::R_POWERPC_TPREL16_HA:
4924 case elfcpp::R_POWERPC_DTPREL16_HA:
4925 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
4926 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
4927 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
4928 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
4929 Reloc::addr16_ha(view, value);
4932 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4934 // R_PPC_EMB_NADDR16_LO
4936 case elfcpp::R_PPC64_ADDR16_HIGHER:
4937 case elfcpp::R_PPC64_TPREL16_HIGHER:
4938 Reloc::addr16_hi2(view, value);
4941 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4943 // R_PPC_EMB_NADDR16_HI
4945 case elfcpp::R_PPC64_ADDR16_HIGHERA:
4946 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4947 Reloc::addr16_ha2(view, value);
4950 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4952 // R_PPC_EMB_NADDR16_HA
4954 case elfcpp::R_PPC64_ADDR16_HIGHEST:
4955 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4956 Reloc::addr16_hi3(view, value);
4959 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4963 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4964 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4965 Reloc::addr16_ha3(view, value);
4968 case elfcpp::R_PPC64_DTPREL16_DS:
4969 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4971 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
4973 case elfcpp::R_PPC64_TPREL16_DS:
4974 case elfcpp::R_PPC64_TPREL16_LO_DS:
4976 // R_PPC_TLSGD, R_PPC_TLSLD
4978 case elfcpp::R_PPC64_ADDR16_DS:
4979 case elfcpp::R_PPC64_ADDR16_LO_DS:
4980 case elfcpp::R_PPC64_TOC16_DS:
4981 case elfcpp::R_PPC64_TOC16_LO_DS:
4982 case elfcpp::R_PPC64_GOT16_DS:
4983 case elfcpp::R_PPC64_GOT16_LO_DS:
4984 case elfcpp::R_PPC64_SECTOFF_DS:
4985 case elfcpp::R_PPC64_SECTOFF_LO_DS:
4986 status = Reloc::addr16_ds(view, value, overflow);
4989 case elfcpp::R_POWERPC_ADDR14:
4990 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4991 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4992 case elfcpp::R_POWERPC_REL14:
4993 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4994 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4995 status = Reloc::addr14(view, value, overflow);
4998 case elfcpp::R_POWERPC_COPY:
4999 case elfcpp::R_POWERPC_GLOB_DAT:
5000 case elfcpp::R_POWERPC_JMP_SLOT:
5001 case elfcpp::R_POWERPC_RELATIVE:
5002 case elfcpp::R_POWERPC_DTPMOD:
5003 case elfcpp::R_PPC64_JMP_IREL:
5004 case elfcpp::R_POWERPC_IRELATIVE:
5005 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5006 _("unexpected reloc %u in object file"),
5010 case elfcpp::R_PPC_EMB_SDA21:
5015 // R_PPC64_TOCSAVE. For the time being this can be ignored.
5019 case elfcpp::R_PPC_EMB_SDA2I16:
5020 case elfcpp::R_PPC_EMB_SDA2REL:
5023 // R_PPC64_TLSGD, R_PPC64_TLSLD
5026 case elfcpp::R_POWERPC_PLT32:
5027 case elfcpp::R_POWERPC_PLTREL32:
5028 case elfcpp::R_POWERPC_PLT16_LO:
5029 case elfcpp::R_POWERPC_PLT16_HI:
5030 case elfcpp::R_POWERPC_PLT16_HA:
5031 case elfcpp::R_PPC_SDAREL16:
5032 case elfcpp::R_POWERPC_ADDR30:
5033 case elfcpp::R_PPC64_PLT64:
5034 case elfcpp::R_PPC64_PLTREL64:
5035 case elfcpp::R_PPC64_PLTGOT16:
5036 case elfcpp::R_PPC64_PLTGOT16_LO:
5037 case elfcpp::R_PPC64_PLTGOT16_HI:
5038 case elfcpp::R_PPC64_PLTGOT16_HA:
5039 case elfcpp::R_PPC64_PLT16_LO_DS:
5040 case elfcpp::R_PPC64_PLTGOT16_DS:
5041 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
5042 case elfcpp::R_PPC_EMB_RELSEC16:
5043 case elfcpp::R_PPC_EMB_RELST_LO:
5044 case elfcpp::R_PPC_EMB_RELST_HI:
5045 case elfcpp::R_PPC_EMB_RELST_HA:
5046 case elfcpp::R_PPC_EMB_BIT_FLD:
5047 case elfcpp::R_PPC_EMB_RELSDA:
5048 case elfcpp::R_PPC_TOC16:
5051 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5052 _("unsupported reloc %u"),
5056 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
5057 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5058 _("relocation overflow"));
5063 // Relocate section data.
5065 template<int size, bool big_endian>
5067 Target_powerpc<size, big_endian>::relocate_section(
5068 const Relocate_info<size, big_endian>* relinfo,
5069 unsigned int sh_type,
5070 const unsigned char* prelocs,
5072 Output_section* output_section,
5073 bool needs_special_offset_handling,
5074 unsigned char* view,
5076 section_size_type view_size,
5077 const Reloc_symbol_changes* reloc_symbol_changes)
5079 typedef Target_powerpc<size, big_endian> Powerpc;
5080 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
5081 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
5082 Powerpc_comdat_behavior;
5084 gold_assert(sh_type == elfcpp::SHT_RELA);
5086 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
5087 Powerpc_relocate, Powerpc_comdat_behavior>(
5093 needs_special_offset_handling,
5097 reloc_symbol_changes);
5100 class Powerpc_scan_relocatable_reloc
5103 // Return the strategy to use for a local symbol which is not a
5104 // section symbol, given the relocation type.
5105 inline Relocatable_relocs::Reloc_strategy
5106 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
5108 if (r_type == 0 && r_sym == 0)
5109 return Relocatable_relocs::RELOC_DISCARD;
5110 return Relocatable_relocs::RELOC_COPY;
5113 // Return the strategy to use for a local symbol which is a section
5114 // symbol, given the relocation type.
5115 inline Relocatable_relocs::Reloc_strategy
5116 local_section_strategy(unsigned int, Relobj*)
5118 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
5121 // Return the strategy to use for a global symbol, given the
5122 // relocation type, the object, and the symbol index.
5123 inline Relocatable_relocs::Reloc_strategy
5124 global_strategy(unsigned int r_type, Relobj*, unsigned int)
5126 if (r_type == elfcpp::R_PPC_PLTREL24)
5127 return Relocatable_relocs::RELOC_SPECIAL;
5128 return Relocatable_relocs::RELOC_COPY;
5132 // Scan the relocs during a relocatable link.
5134 template<int size, bool big_endian>
5136 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
5137 Symbol_table* symtab,
5139 Sized_relobj_file<size, big_endian>* object,
5140 unsigned int data_shndx,
5141 unsigned int sh_type,
5142 const unsigned char* prelocs,
5144 Output_section* output_section,
5145 bool needs_special_offset_handling,
5146 size_t local_symbol_count,
5147 const unsigned char* plocal_symbols,
5148 Relocatable_relocs* rr)
5150 gold_assert(sh_type == elfcpp::SHT_RELA);
5152 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
5153 Powerpc_scan_relocatable_reloc>(
5161 needs_special_offset_handling,
5167 // Emit relocations for a section.
5168 // This is a modified version of the function by the same name in
5169 // target-reloc.h. Using relocate_special_relocatable for
5170 // R_PPC_PLTREL24 would require duplication of the entire body of the
5171 // loop, so we may as well duplicate the whole thing.
5173 template<int size, bool big_endian>
5175 Target_powerpc<size, big_endian>::relocate_relocs(
5176 const Relocate_info<size, big_endian>* relinfo,
5177 unsigned int sh_type,
5178 const unsigned char* prelocs,
5180 Output_section* output_section,
5181 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
5182 const Relocatable_relocs* rr,
5184 Address view_address,
5186 unsigned char* reloc_view,
5187 section_size_type reloc_view_size)
5189 gold_assert(sh_type == elfcpp::SHT_RELA);
5191 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
5193 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
5195 const int reloc_size
5196 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
5198 Powerpc_relobj<size, big_endian>* const object
5199 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
5200 const unsigned int local_count = object->local_symbol_count();
5201 unsigned int got2_shndx = object->got2_shndx();
5202 Address got2_addend = 0;
5203 if (got2_shndx != 0)
5205 got2_addend = object->get_output_section_offset(got2_shndx);
5206 gold_assert(got2_addend != invalid_address);
5209 unsigned char* pwrite = reloc_view;
5210 bool zap_next = false;
5211 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
5213 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
5214 if (strategy == Relocatable_relocs::RELOC_DISCARD)
5217 Reltype reloc(prelocs);
5218 Reltype_write reloc_write(pwrite);
5220 Address offset = reloc.get_r_offset();
5221 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
5222 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
5223 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
5224 const unsigned int orig_r_sym = r_sym;
5225 typename elfcpp::Elf_types<size>::Elf_Swxword addend
5226 = reloc.get_r_addend();
5227 const Symbol* gsym = NULL;
5231 // We could arrange to discard these and other relocs for
5232 // tls optimised sequences in the strategy methods, but for
5233 // now do as BFD ld does.
5234 r_type = elfcpp::R_POWERPC_NONE;
5238 // Get the new symbol index.
5239 if (r_sym < local_count)
5243 case Relocatable_relocs::RELOC_COPY:
5244 case Relocatable_relocs::RELOC_SPECIAL:
5247 r_sym = object->symtab_index(r_sym);
5248 gold_assert(r_sym != -1U);
5252 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
5254 // We are adjusting a section symbol. We need to find
5255 // the symbol table index of the section symbol for
5256 // the output section corresponding to input section
5257 // in which this symbol is defined.
5258 gold_assert(r_sym < local_count);
5260 unsigned int shndx =
5261 object->local_symbol_input_shndx(r_sym, &is_ordinary);
5262 gold_assert(is_ordinary);
5263 Output_section* os = object->output_section(shndx);
5264 gold_assert(os != NULL);
5265 gold_assert(os->needs_symtab_index());
5266 r_sym = os->symtab_index();
5276 gsym = object->global_symbol(r_sym);
5277 gold_assert(gsym != NULL);
5278 if (gsym->is_forwarder())
5279 gsym = relinfo->symtab->resolve_forwards(gsym);
5281 gold_assert(gsym->has_symtab_index());
5282 r_sym = gsym->symtab_index();
5285 // Get the new offset--the location in the output section where
5286 // this relocation should be applied.
5287 if (static_cast<Address>(offset_in_output_section) != invalid_address)
5288 offset += offset_in_output_section;
5291 section_offset_type sot_offset =
5292 convert_types<section_offset_type, Address>(offset);
5293 section_offset_type new_sot_offset =
5294 output_section->output_offset(object, relinfo->data_shndx,
5296 gold_assert(new_sot_offset != -1);
5297 offset = new_sot_offset;
5300 // In an object file, r_offset is an offset within the section.
5301 // In an executable or dynamic object, generated by
5302 // --emit-relocs, r_offset is an absolute address.
5303 if (!parameters->options().relocatable())
5305 offset += view_address;
5306 if (static_cast<Address>(offset_in_output_section) != invalid_address)
5307 offset -= offset_in_output_section;
5310 // Handle the reloc addend based on the strategy.
5311 if (strategy == Relocatable_relocs::RELOC_COPY)
5313 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
5315 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
5316 addend = psymval->value(object, addend);
5318 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
5320 if (addend >= 32768)
5321 addend += got2_addend;
5326 if (!parameters->options().relocatable())
5328 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5329 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
5330 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
5331 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
5333 // First instruction of a global dynamic sequence,
5335 const bool final = gsym == NULL || gsym->final_value_is_known();
5336 switch (this->optimize_tls_gd(final))
5338 case tls::TLSOPT_TO_IE:
5339 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
5340 - elfcpp::R_POWERPC_GOT_TLSGD16);
5342 case tls::TLSOPT_TO_LE:
5343 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5344 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
5345 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5348 r_type = elfcpp::R_POWERPC_NONE;
5349 offset -= 2 * big_endian;
5356 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5357 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
5358 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
5359 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
5361 // First instruction of a local dynamic sequence,
5363 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
5365 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5366 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
5368 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5369 const Output_section* os = relinfo->layout->tls_segment()
5371 gold_assert(os != NULL);
5372 gold_assert(os->needs_symtab_index());
5373 r_sym = os->symtab_index();
5374 addend = dtp_offset;
5378 r_type = elfcpp::R_POWERPC_NONE;
5379 offset -= 2 * big_endian;
5383 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5384 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
5385 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
5386 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
5388 // First instruction of initial exec sequence.
5389 const bool final = gsym == NULL || gsym->final_value_is_known();
5390 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
5392 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5393 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
5394 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5397 r_type = elfcpp::R_POWERPC_NONE;
5398 offset -= 2 * big_endian;
5402 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5403 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5405 // Second instruction of a global dynamic sequence,
5406 // the __tls_get_addr call
5407 const bool final = gsym == NULL || gsym->final_value_is_known();
5408 switch (this->optimize_tls_gd(final))
5410 case tls::TLSOPT_TO_IE:
5411 r_type = elfcpp::R_POWERPC_NONE;
5414 case tls::TLSOPT_TO_LE:
5415 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5416 offset += 2 * big_endian;
5423 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5424 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5426 // Second instruction of a local dynamic sequence,
5427 // the __tls_get_addr call
5428 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
5430 const Output_section* os = relinfo->layout->tls_segment()
5432 gold_assert(os != NULL);
5433 gold_assert(os->needs_symtab_index());
5434 r_sym = os->symtab_index();
5435 addend = dtp_offset;
5436 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5437 offset += 2 * big_endian;
5441 else if (r_type == elfcpp::R_POWERPC_TLS)
5443 // Second instruction of an initial exec sequence
5444 const bool final = gsym == NULL || gsym->final_value_is_known();
5445 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
5447 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5448 offset += 2 * big_endian;
5453 reloc_write.put_r_offset(offset);
5454 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
5455 reloc_write.put_r_addend(addend);
5457 pwrite += reloc_size;
5460 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
5461 == reloc_view_size);
5464 // Return the value to use for a dynamic which requires special
5465 // treatment. This is how we support equality comparisons of function
5466 // pointers across shared library boundaries, as described in the
5467 // processor specific ABI supplement.
5469 template<int size, bool big_endian>
5471 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
5475 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5476 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5477 unsigned int glink_index = glink->find_entry(gsym);
5478 return glink->address() + glink_index * glink->glink_entry_size();
5484 // Return the PLT address to use for a local symbol.
5485 template<int size, bool big_endian>
5487 Target_powerpc<size, big_endian>::do_plt_address_for_local(
5488 const Relobj* object,
5489 unsigned int symndx) const
5493 const Sized_relobj<size, big_endian>* relobj
5494 = static_cast<const Sized_relobj<size, big_endian>*>(object);
5495 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5496 unsigned int glink_index = glink->find_entry(relobj->sized_relobj(),
5498 return glink->address() + glink_index * glink->glink_entry_size();
5504 // Return the PLT address to use for a global symbol.
5505 template<int size, bool big_endian>
5507 Target_powerpc<size, big_endian>::do_plt_address_for_global(
5508 const Symbol* gsym) const
5512 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5513 unsigned int glink_index = glink->find_entry(gsym);
5514 return glink->address() + glink_index * glink->glink_entry_size();
5520 // Return the offset to use for the GOT_INDX'th got entry which is
5521 // for a local tls symbol specified by OBJECT, SYMNDX.
5522 template<int size, bool big_endian>
5524 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
5525 const Relobj* object,
5526 unsigned int symndx,
5527 unsigned int got_indx) const
5529 const Powerpc_relobj<size, big_endian>* ppc_object
5530 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
5531 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
5533 for (Got_type got_type = GOT_TYPE_TLSGD;
5534 got_type <= GOT_TYPE_TPREL;
5535 got_type = Got_type(got_type + 1))
5536 if (ppc_object->local_has_got_offset(symndx, got_type))
5538 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
5539 if (got_type == GOT_TYPE_TLSGD)
5541 if (off == got_indx * (size / 8))
5543 if (got_type == GOT_TYPE_TPREL)
5553 // Return the offset to use for the GOT_INDX'th got entry which is
5554 // for global tls symbol GSYM.
5555 template<int size, bool big_endian>
5557 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
5559 unsigned int got_indx) const
5561 if (gsym->type() == elfcpp::STT_TLS)
5563 for (Got_type got_type = GOT_TYPE_TLSGD;
5564 got_type <= GOT_TYPE_TPREL;
5565 got_type = Got_type(got_type + 1))
5566 if (gsym->has_got_offset(got_type))
5568 unsigned int off = gsym->got_offset(got_type);
5569 if (got_type == GOT_TYPE_TLSGD)
5571 if (off == got_indx * (size / 8))
5573 if (got_type == GOT_TYPE_TPREL)
5583 // The selector for powerpc object files.
5585 template<int size, bool big_endian>
5586 class Target_selector_powerpc : public Target_selector
5589 Target_selector_powerpc()
5590 : Target_selector(elfcpp::EM_NONE, size, big_endian,
5592 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
5593 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
5595 ? (big_endian ? "elf64ppc" : "elf64lppc")
5596 : (big_endian ? "elf32ppc" : "elf32lppc")))
5600 do_recognize(Input_file*, off_t, int machine, int, int)
5605 if (machine != elfcpp::EM_PPC64)
5610 if (machine != elfcpp::EM_PPC)
5618 return this->instantiate_target();
5622 do_instantiate_target()
5623 { return new Target_powerpc<size, big_endian>(); }
5626 Target_selector_powerpc<32, true> target_selector_ppc32;
5627 Target_selector_powerpc<32, false> target_selector_ppc32le;
5628 Target_selector_powerpc<64, true> target_selector_ppc64;
5629 Target_selector_powerpc<64, false> target_selector_ppc64le;
5631 } // End anonymous namespace.