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 off_t offset_in_output_section,
397 const Relocatable_relocs*,
399 Address view_address,
401 unsigned char* reloc_view,
402 section_size_type reloc_view_size);
404 // Return whether SYM is defined by the ABI.
406 do_is_defined_by_abi(const Symbol* sym) const
408 return strcmp(sym->name(), "__tls_get_addr") == 0;
411 // Return the size of the GOT section.
415 gold_assert(this->got_ != NULL);
416 return this->got_->data_size();
419 // Get the PLT section.
420 const Output_data_plt_powerpc<size, big_endian>*
423 gold_assert(this->plt_ != NULL);
427 // Get the IPLT section.
428 const Output_data_plt_powerpc<size, big_endian>*
431 gold_assert(this->iplt_ != NULL);
435 // Get the .glink section.
436 const Output_data_glink<size, big_endian>*
437 glink_section() const
439 gold_assert(this->glink_ != NULL);
443 // Get the GOT section.
444 const Output_data_got_powerpc<size, big_endian>*
447 gold_assert(this->got_ != NULL);
452 do_make_elf_object(const std::string&, Input_file*, off_t,
453 const elfcpp::Ehdr<size, big_endian>&);
455 // Return the number of entries in the GOT.
457 got_entry_count() const
459 if (this->got_ == NULL)
461 return this->got_size() / (size / 8);
464 // Return the number of entries in the PLT.
466 plt_entry_count() const;
468 // Return the offset of the first non-reserved PLT entry.
470 first_plt_entry_offset() const;
472 // Return the size of each PLT entry.
474 plt_entry_size() const;
476 // Add any special sections for this symbol to the gc work list.
477 // For powerpc64, this adds the code section of a function
480 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
482 // Handle target specific gc actions when adding a gc reference from
483 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
484 // and DST_OFF. For powerpc64, this adds a referenc to the code
485 // section of a function descriptor.
487 do_gc_add_reference(Symbol_table* symtab,
489 unsigned int src_shndx,
491 unsigned int dst_shndx,
492 Address dst_off) const;
496 // The class which scans relocations.
500 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
503 : issued_non_pic_error_(false)
507 get_reference_flags(unsigned int r_type);
510 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
511 Sized_relobj_file<size, big_endian>* object,
512 unsigned int data_shndx,
513 Output_section* output_section,
514 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
515 const elfcpp::Sym<size, big_endian>& lsym,
519 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
520 Sized_relobj_file<size, big_endian>* object,
521 unsigned int data_shndx,
522 Output_section* output_section,
523 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
527 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
529 Sized_relobj_file<size, big_endian>* ,
532 const elfcpp::Rela<size, big_endian>& ,
534 const elfcpp::Sym<size, big_endian>&)
538 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
540 Sized_relobj_file<size, big_endian>* ,
543 const elfcpp::Rela<size,
545 unsigned int , Symbol*)
550 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
551 unsigned int r_type);
554 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
555 unsigned int r_type, Symbol*);
558 generate_tls_call(Symbol_table* symtab, Layout* layout,
559 Target_powerpc* target);
562 check_non_pic(Relobj*, unsigned int r_type);
565 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>* object,
566 unsigned int r_type);
568 // Whether we have issued an error about a non-PIC compilation.
569 bool issued_non_pic_error_;
573 symval_for_branch(Address value, const Sized_symbol<size>* gsym,
574 Powerpc_relobj<size, big_endian>* object,
575 unsigned int *dest_shndx);
577 // The class which implements relocation.
581 // Use 'at' branch hints when true, 'y' when false.
582 // FIXME maybe: set this with an option.
583 static const bool is_isa_v2 = true;
587 CALL_NOT_EXPECTED = 0,
593 : call_tls_get_addr_(CALL_NOT_EXPECTED)
598 if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
600 // FIXME: This needs to specify the location somehow.
601 gold_error(_("missing expected __tls_get_addr call"));
605 // Do a relocation. Return false if the caller should not issue
606 // any warnings about this relocation.
608 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
609 Output_section*, size_t relnum,
610 const elfcpp::Rela<size, big_endian>&,
611 unsigned int r_type, const Sized_symbol<size>*,
612 const Symbol_value<size>*,
614 typename elfcpp::Elf_types<size>::Elf_Addr,
617 // This is set if we should skip the next reloc, which should be a
618 // call to __tls_get_addr.
619 enum skip_tls call_tls_get_addr_;
622 // A class which returns the size required for a relocation type,
623 // used while scanning relocs during a relocatable link.
624 class Relocatable_size_for_reloc
628 get_size_for_reloc(unsigned int, Relobj*)
635 // Optimize the TLS relocation type based on what we know about the
636 // symbol. IS_FINAL is true if the final address of this symbol is
637 // known at link time.
639 tls::Tls_optimization
640 optimize_tls_gd(bool is_final)
642 // If we are generating a shared library, then we can't do anything
644 if (parameters->options().shared())
645 return tls::TLSOPT_NONE;
648 return tls::TLSOPT_TO_IE;
649 return tls::TLSOPT_TO_LE;
652 tls::Tls_optimization
655 if (parameters->options().shared())
656 return tls::TLSOPT_NONE;
658 return tls::TLSOPT_TO_LE;
661 tls::Tls_optimization
662 optimize_tls_ie(bool is_final)
664 if (!is_final || parameters->options().shared())
665 return tls::TLSOPT_NONE;
667 return tls::TLSOPT_TO_LE;
670 // Get the GOT section, creating it if necessary.
671 Output_data_got_powerpc<size, big_endian>*
672 got_section(Symbol_table*, Layout*);
676 make_glink_section(Layout*);
678 // Create the PLT section.
680 make_plt_section(Layout*);
683 make_iplt_section(Layout*);
685 // Create a PLT entry for a global symbol.
687 make_plt_entry(Layout*, Symbol*,
688 const elfcpp::Rela<size, big_endian>&,
689 const Sized_relobj_file<size, big_endian>* object);
691 // Create a PLT entry for a local IFUNC symbol.
693 make_local_ifunc_plt_entry(Layout*,
694 const elfcpp::Rela<size, big_endian>&,
695 Sized_relobj_file<size, big_endian>*);
697 // Create a GOT entry for local dynamic __tls_get_addr.
699 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
700 Sized_relobj_file<size, big_endian>* object);
703 tlsld_got_offset() const
705 return this->tlsld_got_offset_;
708 // Get the dynamic reloc section, creating it if necessary.
710 rela_dyn_section(Layout*);
712 // Copy a relocation against a global symbol.
714 copy_reloc(Symbol_table* symtab, Layout* layout,
715 Sized_relobj_file<size, big_endian>* object,
716 unsigned int shndx, Output_section* output_section,
717 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
719 this->copy_relocs_.copy_reloc(symtab, layout,
720 symtab->get_sized_symbol<size>(sym),
721 object, shndx, output_section,
722 reloc, this->rela_dyn_section(layout));
725 // Information about this specific target which we pass to the
726 // general Target structure.
727 static Target::Target_info powerpc_info;
729 // The types of GOT entries needed for this platform.
730 // These values are exposed to the ABI in an incremental link.
731 // Do not renumber existing values without changing the version
732 // number of the .gnu_incremental_inputs section.
736 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
737 GOT_TYPE_DTPREL, // entry for @got@dtprel
738 GOT_TYPE_TPREL // entry for @got@tprel
741 // The GOT output section.
742 Output_data_got_powerpc<size, big_endian>* got_;
743 // The PLT output section.
744 Output_data_plt_powerpc<size, big_endian>* plt_;
745 // The IPLT output section.
746 Output_data_plt_powerpc<size, big_endian>* iplt_;
747 // The .glink output section.
748 Output_data_glink<size, big_endian>* glink_;
749 // The dynamic reloc output section.
750 Reloc_section* rela_dyn_;
751 // Relocs saved to avoid a COPY reloc.
752 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
753 // Space for variables copied with a COPY reloc.
754 Output_data_space* dynbss_;
755 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
756 unsigned int tlsld_got_offset_;
760 Target::Target_info Target_powerpc<32, true>::powerpc_info =
763 true, // is_big_endian
764 elfcpp::EM_PPC, // machine_code
765 false, // has_make_symbol
766 false, // has_resolve
767 false, // has_code_fill
768 true, // is_default_stack_executable
769 false, // can_icf_inline_merge_sections
771 "/usr/lib/ld.so.1", // dynamic_linker
772 0x10000000, // default_text_segment_address
773 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
774 4 * 1024, // common_pagesize (overridable by -z common-page-size)
775 false, // isolate_execinstr
777 elfcpp::SHN_UNDEF, // small_common_shndx
778 elfcpp::SHN_UNDEF, // large_common_shndx
779 0, // small_common_section_flags
780 0, // large_common_section_flags
781 NULL, // attributes_section
782 NULL // attributes_vendor
786 Target::Target_info Target_powerpc<32, false>::powerpc_info =
789 false, // is_big_endian
790 elfcpp::EM_PPC, // machine_code
791 false, // has_make_symbol
792 false, // has_resolve
793 false, // has_code_fill
794 true, // is_default_stack_executable
795 false, // can_icf_inline_merge_sections
797 "/usr/lib/ld.so.1", // dynamic_linker
798 0x10000000, // default_text_segment_address
799 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
800 4 * 1024, // common_pagesize (overridable by -z common-page-size)
801 false, // isolate_execinstr
803 elfcpp::SHN_UNDEF, // small_common_shndx
804 elfcpp::SHN_UNDEF, // large_common_shndx
805 0, // small_common_section_flags
806 0, // large_common_section_flags
807 NULL, // attributes_section
808 NULL // attributes_vendor
812 Target::Target_info Target_powerpc<64, true>::powerpc_info =
815 true, // is_big_endian
816 elfcpp::EM_PPC64, // machine_code
817 false, // has_make_symbol
818 false, // has_resolve
819 false, // has_code_fill
820 true, // is_default_stack_executable
821 false, // can_icf_inline_merge_sections
823 "/usr/lib/ld.so.1", // dynamic_linker
824 0x10000000, // default_text_segment_address
825 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
826 4 * 1024, // common_pagesize (overridable by -z common-page-size)
827 false, // isolate_execinstr
829 elfcpp::SHN_UNDEF, // small_common_shndx
830 elfcpp::SHN_UNDEF, // large_common_shndx
831 0, // small_common_section_flags
832 0, // large_common_section_flags
833 NULL, // attributes_section
834 NULL // attributes_vendor
838 Target::Target_info Target_powerpc<64, false>::powerpc_info =
841 false, // is_big_endian
842 elfcpp::EM_PPC64, // machine_code
843 false, // has_make_symbol
844 false, // has_resolve
845 false, // has_code_fill
846 true, // is_default_stack_executable
847 false, // can_icf_inline_merge_sections
849 "/usr/lib/ld.so.1", // dynamic_linker
850 0x10000000, // default_text_segment_address
851 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
852 4 * 1024, // common_pagesize (overridable by -z common-page-size)
853 false, // isolate_execinstr
855 elfcpp::SHN_UNDEF, // small_common_shndx
856 elfcpp::SHN_UNDEF, // large_common_shndx
857 0, // small_common_section_flags
858 0, // large_common_section_flags
859 NULL, // attributes_section
860 NULL // attributes_vendor
864 is_branch_reloc(unsigned int r_type)
866 return (r_type == elfcpp::R_POWERPC_REL24
867 || r_type == elfcpp::R_PPC_PLTREL24
868 || r_type == elfcpp::R_PPC_LOCAL24PC
869 || r_type == elfcpp::R_POWERPC_REL14
870 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
871 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
872 || r_type == elfcpp::R_POWERPC_ADDR24
873 || r_type == elfcpp::R_POWERPC_ADDR14
874 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
875 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
878 // If INSN is an opcode that may be used with an @tls operand, return
879 // the transformed insn for TLS optimisation, otherwise return 0. If
880 // REG is non-zero only match an insn with RB or RA equal to REG.
882 at_tls_transform(uint32_t insn, unsigned int reg)
884 if ((insn & (0x3f << 26)) != 31 << 26)
888 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
889 rtra = insn & ((1 << 26) - (1 << 16));
890 else if (((insn >> 16) & 0x1f) == reg)
891 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
895 if ((insn & (0x3ff << 1)) == 266 << 1)
898 else if ((insn & (0x1f << 1)) == 23 << 1
899 && ((insn & (0x1f << 6)) < 14 << 6
900 || ((insn & (0x1f << 6)) >= 16 << 6
901 && (insn & (0x1f << 6)) < 24 << 6)))
902 // load and store indexed -> dform
903 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
904 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
905 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
906 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
907 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
909 insn = (58 << 26) | 2;
916 // Modified version of symtab.h class Symbol member
917 // Given a direct absolute or pc-relative static relocation against
918 // the global symbol, this function returns whether a dynamic relocation
923 needs_dynamic_reloc(const Symbol* gsym, int flags)
925 // No dynamic relocations in a static link!
926 if (parameters->doing_static_link())
929 // A reference to an undefined symbol from an executable should be
930 // statically resolved to 0, and does not need a dynamic relocation.
931 // This matches gnu ld behavior.
932 if (gsym->is_undefined() && !parameters->options().shared())
935 // A reference to an absolute symbol does not need a dynamic relocation.
936 if (gsym->is_absolute())
939 // An absolute reference within a position-independent output file
940 // will need a dynamic relocation.
941 if ((flags & Symbol::ABSOLUTE_REF)
942 && parameters->options().output_is_position_independent())
945 // A function call that can branch to a local PLT entry does not need
946 // a dynamic relocation.
947 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
950 // A reference to any PLT entry in a non-position-independent executable
951 // does not need a dynamic relocation.
952 // Except due to having function descriptors on powerpc64 we don't define
953 // functions to their plt code in an executable, so this doesn't apply.
955 && !parameters->options().output_is_position_independent()
956 && gsym->has_plt_offset())
959 // A reference to a symbol defined in a dynamic object or to a
960 // symbol that is preemptible will need a dynamic relocation.
961 if (gsym->is_from_dynobj()
962 || gsym->is_undefined()
963 || gsym->is_preemptible())
966 // For all other cases, return FALSE.
970 // Modified version of symtab.h class Symbol member
971 // Whether we should use the PLT offset associated with a symbol for
972 // a relocation. FLAGS is a set of Reference_flags.
976 use_plt_offset(const Symbol* gsym, int flags)
978 // If the symbol doesn't have a PLT offset, then naturally we
979 // don't want to use it.
980 if (!gsym->has_plt_offset())
983 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
984 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
987 // If we are going to generate a dynamic relocation, then we will
988 // wind up using that, so no need to use the PLT entry.
989 if (needs_dynamic_reloc<size>(gsym, flags))
992 // If the symbol is from a dynamic object, we need to use the PLT
994 if (gsym->is_from_dynobj())
997 // If we are generating a shared object, and gsym symbol is
998 // undefined or preemptible, we need to use the PLT entry.
999 if (parameters->options().shared()
1000 && (gsym->is_undefined() || gsym->is_preemptible()))
1003 // If gsym is a call to a weak undefined symbol, we need to use
1004 // the PLT entry; the symbol may be defined by a library loaded
1006 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
1009 // Otherwise we can use the regular definition.
1013 template<int size, bool big_endian>
1014 class Powerpc_relocate_functions
1031 typedef Powerpc_relocate_functions<size, big_endian> This;
1032 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1034 template<int valsize>
1036 has_overflow_signed(Address value)
1038 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1039 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1040 limit <<= ((valsize - 1) >> 1);
1041 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1042 return value + limit > (limit << 1) - 1;
1045 template<int valsize>
1047 has_overflow_bitfield(Address value)
1049 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1050 limit <<= ((valsize - 1) >> 1);
1051 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1052 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
1055 template<int valsize>
1056 static inline Status
1057 overflowed(Address value, Overflow_check overflow)
1059 if (overflow == CHECK_SIGNED)
1061 if (has_overflow_signed<valsize>(value))
1062 return STATUS_OVERFLOW;
1064 else if (overflow == CHECK_BITFIELD)
1066 if (has_overflow_bitfield<valsize>(value))
1067 return STATUS_OVERFLOW;
1072 // Do a simple RELA relocation
1073 template<int valsize>
1074 static inline Status
1075 rela(unsigned char* view, Address value, Overflow_check overflow)
1077 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1078 Valtype* wv = reinterpret_cast<Valtype*>(view);
1079 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
1080 return overflowed<valsize>(value, overflow);
1083 template<int valsize>
1084 static inline Status
1085 rela(unsigned char* view,
1086 unsigned int right_shift,
1087 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1089 Overflow_check overflow)
1091 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1092 Valtype* wv = reinterpret_cast<Valtype*>(view);
1093 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1094 Valtype reloc = value >> right_shift;
1097 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
1098 return overflowed<valsize>(value >> right_shift, overflow);
1101 // Do a simple RELA relocation, unaligned.
1102 template<int valsize>
1103 static inline Status
1104 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1106 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
1107 return overflowed<valsize>(value, overflow);
1110 template<int valsize>
1111 static inline Status
1112 rela_ua(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_unaligned<valsize, big_endian>::Valtype
1120 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
1121 Valtype reloc = value >> right_shift;
1124 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
1125 return overflowed<valsize>(value >> right_shift, overflow);
1129 // R_PPC64_ADDR64: (Symbol + Addend)
1131 addr64(unsigned char* view, Address value)
1132 { This::template rela<64>(view, value, CHECK_NONE); }
1134 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1136 addr64_u(unsigned char* view, Address value)
1137 { This::template rela_ua<64>(view, value, CHECK_NONE); }
1139 // R_POWERPC_ADDR32: (Symbol + Addend)
1140 static inline Status
1141 addr32(unsigned char* view, Address value, Overflow_check overflow)
1142 { return This::template rela<32>(view, value, overflow); }
1144 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1145 static inline Status
1146 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1147 { return This::template rela_ua<32>(view, value, overflow); }
1149 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1150 static inline Status
1151 addr24(unsigned char* view, Address value, Overflow_check overflow)
1153 Status stat = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
1154 if (overflow != CHECK_NONE && (value & 3) != 0)
1155 stat = STATUS_OVERFLOW;
1159 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1160 static inline Status
1161 addr16(unsigned char* view, Address value, Overflow_check overflow)
1162 { return This::template rela<16>(view, value, overflow); }
1164 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1165 static inline Status
1166 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1167 { return This::template rela_ua<16>(view, value, overflow); }
1169 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1170 static inline Status
1171 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1173 Status stat = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1174 if (overflow != CHECK_NONE && (value & 3) != 0)
1175 stat = STATUS_OVERFLOW;
1179 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1181 addr16_hi(unsigned char* view, Address value)
1182 { This::template rela<16>(view, 16, 0xffff, value, CHECK_NONE); }
1184 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1186 addr16_ha(unsigned char* view, Address value)
1187 { This::addr16_hi(view, value + 0x8000); }
1189 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1191 addr16_hi2(unsigned char* view, Address value)
1192 { This::template rela<16>(view, 32, 0xffff, value, CHECK_NONE); }
1194 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1196 addr16_ha2(unsigned char* view, Address value)
1197 { This::addr16_hi2(view, value + 0x8000); }
1199 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1201 addr16_hi3(unsigned char* view, Address value)
1202 { This::template rela<16>(view, 48, 0xffff, value, CHECK_NONE); }
1204 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1206 addr16_ha3(unsigned char* view, Address value)
1207 { This::addr16_hi3(view, value + 0x8000); }
1209 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1210 static inline Status
1211 addr14(unsigned char* view, Address value, Overflow_check overflow)
1213 Status stat = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1214 if (overflow != CHECK_NONE && (value & 3) != 0)
1215 stat = STATUS_OVERFLOW;
1220 // Stash away the index of .got2 or .opd in a relocatable object, if
1221 // such a section exists.
1223 template<int size, bool big_endian>
1225 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1226 Read_symbols_data* sd)
1228 const unsigned char* const pshdrs = sd->section_headers->data();
1229 const unsigned char* namesu = sd->section_names->data();
1230 const char* names = reinterpret_cast<const char*>(namesu);
1231 section_size_type names_size = sd->section_names_size;
1232 const unsigned char* s;
1234 s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1235 names, names_size, NULL);
1238 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1239 this->special_ = ndx;
1241 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1244 // Examine .rela.opd to build info about function entry points.
1246 template<int size, bool big_endian>
1248 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1250 const unsigned char* prelocs,
1251 const unsigned char* plocal_syms)
1255 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1257 const int reloc_size
1258 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1259 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1260 Address expected_off = 0;
1261 bool regular = true;
1262 unsigned int opd_ent_size = 0;
1264 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1266 Reltype reloc(prelocs);
1267 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1268 = reloc.get_r_info();
1269 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1270 if (r_type == elfcpp::R_PPC64_ADDR64)
1272 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1273 typename elfcpp::Elf_types<size>::Elf_Addr value;
1276 if (r_sym < this->local_symbol_count())
1278 typename elfcpp::Sym<size, big_endian>
1279 lsym(plocal_syms + r_sym * sym_size);
1280 shndx = lsym.get_st_shndx();
1281 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1282 value = lsym.get_st_value();
1285 shndx = this->symbol_section_and_value(r_sym, &value,
1287 this->set_opd_ent(reloc.get_r_offset(), shndx,
1288 value + reloc.get_r_addend());
1291 expected_off = reloc.get_r_offset();
1292 opd_ent_size = expected_off;
1294 else if (expected_off != reloc.get_r_offset())
1296 expected_off += opd_ent_size;
1298 else if (r_type == elfcpp::R_PPC64_TOC)
1300 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
1305 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1306 this->name().c_str(), r_type);
1310 if (reloc_count <= 2)
1311 opd_ent_size = this->section_size(this->opd_shndx());
1312 if (opd_ent_size != 24 && opd_ent_size != 16)
1316 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1317 this->name().c_str());
1323 template<int size, bool big_endian>
1325 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1327 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1330 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1331 p != rd->relocs.end();
1334 if (p->data_shndx == this->opd_shndx())
1336 uint64_t opd_size = this->section_size(this->opd_shndx());
1337 gold_assert(opd_size == static_cast<size_t>(opd_size));
1340 this->init_opd(opd_size);
1341 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1342 rd->local_symbols->data());
1350 // Set up PowerPC target specific relobj.
1352 template<int size, bool big_endian>
1354 Target_powerpc<size, big_endian>::do_make_elf_object(
1355 const std::string& name,
1356 Input_file* input_file,
1357 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1359 int et = ehdr.get_e_type();
1360 // ET_EXEC files are valid input for --just-symbols/-R,
1361 // and we treat them as relocatable objects.
1362 if (et == elfcpp::ET_REL
1363 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1365 Powerpc_relobj<size, big_endian>* obj =
1366 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1370 else if (et == elfcpp::ET_DYN)
1372 Sized_dynobj<size, big_endian>* obj =
1373 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1379 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1384 template<int size, bool big_endian>
1385 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1388 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1389 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1391 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1392 : Output_data_got<size, big_endian>(),
1393 symtab_(symtab), layout_(layout),
1394 header_ent_cnt_(size == 32 ? 3 : 1),
1395 header_index_(size == 32 ? 0x2000 : 0)
1400 // Create a new GOT entry and return its offset.
1402 add_got_entry(Got_entry got_entry)
1404 this->reserve_ent();
1405 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1408 // Create a pair of new GOT entries and return the offset of the first.
1410 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1412 this->reserve_ent(2);
1413 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1418 add_constant_pair(Valtype c1, Valtype c2)
1420 this->reserve_ent(2);
1421 unsigned int got_offset = this->add_constant(c1);
1422 this->add_constant(c2);
1426 // Offset of _GLOBAL_OFFSET_TABLE_.
1430 return this->got_offset(this->header_index_);
1433 // Offset of base used to access the GOT/TOC.
1434 // The got/toc pointer reg will be set to this value.
1435 typename elfcpp::Elf_types<size>::Elf_Off
1436 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1439 return this->g_o_t();
1441 return (this->output_section()->address()
1442 + object->toc_base_offset()
1446 // Ensure our GOT has a header.
1448 set_final_data_size()
1450 if (this->header_ent_cnt_ != 0)
1451 this->make_header();
1452 Output_data_got<size, big_endian>::set_final_data_size();
1455 // First word of GOT header needs some values that are not
1456 // handled by Output_data_got so poke them in here.
1457 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1459 do_write(Output_file* of)
1462 if (size == 32 && this->layout_->dynamic_data() != NULL)
1463 val = this->layout_->dynamic_section()->address();
1465 val = this->output_section()->address() + 0x8000;
1466 this->replace_constant(this->header_index_, val);
1467 Output_data_got<size, big_endian>::do_write(of);
1472 reserve_ent(unsigned int cnt = 1)
1474 if (this->header_ent_cnt_ == 0)
1476 if (this->num_entries() + cnt > this->header_index_)
1477 this->make_header();
1483 this->header_ent_cnt_ = 0;
1484 this->header_index_ = this->num_entries();
1487 Output_data_got<size, big_endian>::add_constant(0);
1488 Output_data_got<size, big_endian>::add_constant(0);
1489 Output_data_got<size, big_endian>::add_constant(0);
1491 // Define _GLOBAL_OFFSET_TABLE_ at the header
1492 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1493 Symbol_table::PREDEFINED,
1494 this, this->g_o_t(), 0,
1501 Output_data_got<size, big_endian>::add_constant(0);
1504 // Stashed pointers.
1505 Symbol_table* symtab_;
1509 unsigned int header_ent_cnt_;
1510 // GOT header index.
1511 unsigned int header_index_;
1514 // Get the GOT section, creating it if necessary.
1516 template<int size, bool big_endian>
1517 Output_data_got_powerpc<size, big_endian>*
1518 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1521 if (this->got_ == NULL)
1523 gold_assert(symtab != NULL && layout != NULL);
1526 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
1528 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1529 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1530 this->got_, ORDER_DATA, false);
1536 // Get the dynamic reloc section, creating it if necessary.
1538 template<int size, bool big_endian>
1539 typename Target_powerpc<size, big_endian>::Reloc_section*
1540 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1542 if (this->rela_dyn_ == NULL)
1544 gold_assert(layout != NULL);
1545 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1546 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1547 elfcpp::SHF_ALLOC, this->rela_dyn_,
1548 ORDER_DYNAMIC_RELOCS, false);
1550 return this->rela_dyn_;
1553 // A class to handle the PLT data.
1555 template<int size, bool big_endian>
1556 class Output_data_plt_powerpc : public Output_section_data_build
1559 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1560 size, big_endian> Reloc_section;
1562 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
1563 Reloc_section* plt_rel,
1564 unsigned int reserved_size,
1566 : Output_section_data_build(size == 32 ? 4 : 8),
1569 initial_plt_entry_size_(reserved_size),
1573 // Add an entry to the PLT.
1578 add_ifunc_entry(Symbol*);
1581 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
1583 // Return the .rela.plt section data.
1590 // Return the number of PLT entries.
1594 return ((this->current_data_size() - this->initial_plt_entry_size_)
1598 // Return the offset of the first non-reserved PLT entry.
1600 first_plt_entry_offset()
1601 { return this->initial_plt_entry_size_; }
1603 // Return the size of a PLT entry.
1605 get_plt_entry_size()
1606 { return plt_entry_size; }
1610 do_adjust_output_section(Output_section* os)
1615 // Write to a map file.
1617 do_print_to_mapfile(Mapfile* mapfile) const
1618 { mapfile->print_output_data(this, this->name_); }
1621 // The size of an entry in the PLT.
1622 static const int plt_entry_size = size == 32 ? 4 : 24;
1624 // Write out the PLT data.
1626 do_write(Output_file*);
1628 // The reloc section.
1629 Reloc_section* rel_;
1630 // Allows access to .glink for do_write.
1631 Target_powerpc<size, big_endian>* targ_;
1632 // The size of the first reserved entry.
1633 int initial_plt_entry_size_;
1634 // What to report in map file.
1638 // Add an entry to the PLT.
1640 template<int size, bool big_endian>
1642 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1644 if (!gsym->has_plt_offset())
1646 off_t off = this->current_data_size();
1648 off += this->first_plt_entry_offset();
1649 gsym->set_plt_offset(off);
1650 gsym->set_needs_dynsym_entry();
1651 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
1652 this->rel_->add_global(gsym, dynrel, this, off, 0);
1653 off += plt_entry_size;
1654 this->set_current_data_size(off);
1658 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
1660 template<int size, bool big_endian>
1662 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
1664 if (!gsym->has_plt_offset())
1666 off_t off = this->current_data_size();
1667 gsym->set_plt_offset(off);
1668 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
1670 dynrel = elfcpp::R_PPC64_JMP_IREL;
1671 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
1672 off += plt_entry_size;
1673 this->set_current_data_size(off);
1677 // Add an entry for a local ifunc symbol to the IPLT.
1679 template<int size, bool big_endian>
1681 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
1682 Sized_relobj_file<size, big_endian>* relobj,
1683 unsigned int local_sym_index)
1685 if (!relobj->local_has_plt_offset(local_sym_index))
1687 off_t off = this->current_data_size();
1688 relobj->set_local_plt_offset(local_sym_index, off);
1689 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
1691 dynrel = elfcpp::R_PPC64_JMP_IREL;
1692 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
1694 off += plt_entry_size;
1695 this->set_current_data_size(off);
1699 static const uint32_t add_0_11_11 = 0x7c0b5a14;
1700 static const uint32_t add_3_3_2 = 0x7c631214;
1701 static const uint32_t add_3_3_13 = 0x7c636a14;
1702 static const uint32_t add_11_0_11 = 0x7d605a14;
1703 static const uint32_t add_12_2_11 = 0x7d825a14;
1704 static const uint32_t addi_11_11 = 0x396b0000;
1705 static const uint32_t addi_12_12 = 0x398c0000;
1706 static const uint32_t addi_2_2 = 0x38420000;
1707 static const uint32_t addi_3_2 = 0x38620000;
1708 static const uint32_t addi_3_3 = 0x38630000;
1709 static const uint32_t addis_0_2 = 0x3c020000;
1710 static const uint32_t addis_0_13 = 0x3c0d0000;
1711 static const uint32_t addis_11_11 = 0x3d6b0000;
1712 static const uint32_t addis_11_30 = 0x3d7e0000;
1713 static const uint32_t addis_12_12 = 0x3d8c0000;
1714 static const uint32_t addis_12_2 = 0x3d820000;
1715 static const uint32_t addis_3_2 = 0x3c620000;
1716 static const uint32_t addis_3_13 = 0x3c6d0000;
1717 static const uint32_t b = 0x48000000;
1718 static const uint32_t bcl_20_31 = 0x429f0005;
1719 static const uint32_t bctr = 0x4e800420;
1720 static const uint32_t blr = 0x4e800020;
1721 static const uint32_t blrl = 0x4e800021;
1722 static const uint32_t cror_15_15_15 = 0x4def7b82;
1723 static const uint32_t cror_31_31_31 = 0x4ffffb82;
1724 static const uint32_t ld_0_1 = 0xe8010000;
1725 static const uint32_t ld_0_12 = 0xe80c0000;
1726 static const uint32_t ld_11_12 = 0xe96c0000;
1727 static const uint32_t ld_11_2 = 0xe9620000;
1728 static const uint32_t ld_2_1 = 0xe8410000;
1729 static const uint32_t ld_2_11 = 0xe84b0000;
1730 static const uint32_t ld_2_12 = 0xe84c0000;
1731 static const uint32_t ld_2_2 = 0xe8420000;
1732 static const uint32_t lfd_0_1 = 0xc8010000;
1733 static const uint32_t li_0_0 = 0x38000000;
1734 static const uint32_t li_12_0 = 0x39800000;
1735 static const uint32_t lis_0_0 = 0x3c000000;
1736 static const uint32_t lis_11 = 0x3d600000;
1737 static const uint32_t lis_12 = 0x3d800000;
1738 static const uint32_t lwz_0_12 = 0x800c0000;
1739 static const uint32_t lwz_11_11 = 0x816b0000;
1740 static const uint32_t lwz_11_30 = 0x817e0000;
1741 static const uint32_t lwz_12_12 = 0x818c0000;
1742 static const uint32_t lwzu_0_12 = 0x840c0000;
1743 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
1744 static const uint32_t mflr_0 = 0x7c0802a6;
1745 static const uint32_t mflr_11 = 0x7d6802a6;
1746 static const uint32_t mflr_12 = 0x7d8802a6;
1747 static const uint32_t mtctr_0 = 0x7c0903a6;
1748 static const uint32_t mtctr_11 = 0x7d6903a6;
1749 static const uint32_t mtlr_0 = 0x7c0803a6;
1750 static const uint32_t mtlr_12 = 0x7d8803a6;
1751 static const uint32_t nop = 0x60000000;
1752 static const uint32_t ori_0_0_0 = 0x60000000;
1753 static const uint32_t std_0_1 = 0xf8010000;
1754 static const uint32_t std_0_12 = 0xf80c0000;
1755 static const uint32_t std_2_1 = 0xf8410000;
1756 static const uint32_t stfd_0_1 = 0xd8010000;
1757 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
1758 static const uint32_t sub_11_11_12 = 0x7d6c5850;
1760 // Write out the PLT.
1762 template<int size, bool big_endian>
1764 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1768 const off_t offset = this->offset();
1769 const section_size_type oview_size
1770 = convert_to_section_size_type(this->data_size());
1771 unsigned char* const oview = of->get_output_view(offset, oview_size);
1772 unsigned char* pov = oview;
1773 unsigned char* endpov = oview + oview_size;
1775 // The address of the .glink branch table
1776 const Output_data_glink<size, big_endian>* glink
1777 = this->targ_->glink_section();
1778 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1779 = glink->address() + glink->pltresolve();
1781 while (pov < endpov)
1783 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1788 of->write_output_view(offset, oview_size, oview);
1792 // Create the PLT section.
1794 template<int size, bool big_endian>
1796 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1798 if (this->plt_ == NULL)
1800 if (this->glink_ == NULL)
1801 make_glink_section(layout);
1803 // Ensure that .rela.dyn always appears before .rela.plt This is
1804 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1805 // needs to include .rela.plt in it's range.
1806 this->rela_dyn_section(layout);
1808 Reloc_section* plt_rel = new Reloc_section(false);
1809 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1810 elfcpp::SHF_ALLOC, plt_rel,
1811 ORDER_DYNAMIC_PLT_RELOCS, false);
1813 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
1814 size == 32 ? 0 : 24,
1816 layout->add_output_section_data(".plt",
1818 ? elfcpp::SHT_PROGBITS
1819 : elfcpp::SHT_NOBITS),
1820 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1829 // Create the IPLT section.
1831 template<int size, bool big_endian>
1833 Target_powerpc<size, big_endian>::make_iplt_section(Layout* layout)
1835 if (this->iplt_ == NULL)
1837 this->make_plt_section(layout);
1839 Reloc_section* iplt_rel = new Reloc_section(false);
1840 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
1842 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
1844 this->plt_->output_section()->add_output_section_data(this->iplt_);
1848 // A class to handle .glink.
1850 template<int size, bool big_endian>
1851 class Output_data_glink : public Output_section_data
1854 static const int pltresolve_size = 16*4;
1856 Output_data_glink(Target_powerpc<size, big_endian>*);
1860 add_entry(const Sized_relobj_file<size, big_endian>*,
1862 const elfcpp::Rela<size, big_endian>&);
1865 add_entry(const Sized_relobj_file<size, big_endian>*,
1867 const elfcpp::Rela<size, big_endian>&);
1870 find_entry(const Symbol*) const;
1873 find_entry(const Sized_relobj_file<size, big_endian>*, unsigned int) const;
1876 find_entry(const Sized_relobj_file<size, big_endian>*,
1878 const elfcpp::Rela<size, big_endian>&) const;
1881 find_entry(const Sized_relobj_file<size, big_endian>*,
1883 const elfcpp::Rela<size, big_endian>&) const;
1886 glink_entry_size() const
1891 // FIXME: We should be using multiple glink sections for
1892 // stubs to support > 33M applications.
1899 return this->pltresolve_;
1903 // Write to a map file.
1905 do_print_to_mapfile(Mapfile* mapfile) const
1906 { mapfile->print_output_data(this, _("** glink")); }
1910 set_final_data_size();
1914 do_write(Output_file*);
1919 Glink_sym_ent(const Symbol* sym)
1920 : sym_(sym), object_(0), addend_(0), locsym_(0)
1923 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
1924 unsigned int locsym_index)
1925 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
1928 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
1930 const elfcpp::Rela<size, big_endian>& reloc)
1931 : sym_(sym), object_(0), addend_(0), locsym_(0)
1934 this->addend_ = reloc.get_r_addend();
1935 else if (parameters->options().output_is_position_independent()
1936 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1937 == elfcpp::R_PPC_PLTREL24))
1939 this->addend_ = reloc.get_r_addend();
1940 if (this->addend_ >= 32768)
1941 this->object_ = object;
1945 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
1946 unsigned int locsym_index,
1947 const elfcpp::Rela<size, big_endian>& reloc)
1948 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
1951 this->addend_ = reloc.get_r_addend();
1952 else if (parameters->options().output_is_position_independent()
1953 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1954 == elfcpp::R_PPC_PLTREL24))
1955 this->addend_ = reloc.get_r_addend();
1958 bool operator==(const Glink_sym_ent& that) const
1960 return (this->sym_ == that.sym_
1961 && this->object_ == that.object_
1962 && this->addend_ == that.addend_
1963 && this->locsym_ == that.locsym_);
1967 const Sized_relobj_file<size, big_endian>* object_;
1968 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
1969 unsigned int locsym_;
1972 class Glink_sym_ent_hash
1975 size_t operator()(const Glink_sym_ent& ent) const
1977 return (reinterpret_cast<uintptr_t>(ent.sym_)
1978 ^ reinterpret_cast<uintptr_t>(ent.object_)
1984 // Map sym/object/addend to index.
1985 typedef Unordered_map<Glink_sym_ent, unsigned int,
1986 Glink_sym_ent_hash> Glink_entries;
1987 Glink_entries glink_entries_;
1989 // Offset of pltresolve stub (actually, branch table for 32-bit)
1992 // Allows access to .got and .plt for do_write.
1993 Target_powerpc<size, big_endian>* targ_;
1996 // Create the glink section.
1998 template<int size, bool big_endian>
1999 Output_data_glink<size, big_endian>::Output_data_glink(
2000 Target_powerpc<size, big_endian>* targ)
2001 : Output_section_data(16),
2002 pltresolve_(0), targ_(targ)
2006 // Add an entry to glink, if we do not already have one for this
2007 // sym/object/addend combo.
2009 template<int size, bool big_endian>
2011 Output_data_glink<size, big_endian>::add_entry(
2012 const Sized_relobj_file<size, big_endian>* object,
2014 const elfcpp::Rela<size, big_endian>& reloc)
2016 Glink_sym_ent ent(object, gsym, reloc);
2017 unsigned int indx = this->glink_entries_.size();
2018 this->glink_entries_.insert(std::make_pair(ent, indx));
2021 template<int size, bool big_endian>
2023 Output_data_glink<size, big_endian>::add_entry(
2024 const Sized_relobj_file<size, big_endian>* object,
2025 unsigned int locsym_index,
2026 const elfcpp::Rela<size, big_endian>& reloc)
2028 Glink_sym_ent ent(object, locsym_index, reloc);
2029 unsigned int indx = this->glink_entries_.size();
2030 this->glink_entries_.insert(std::make_pair(ent, indx));
2033 template<int size, bool big_endian>
2035 Output_data_glink<size, big_endian>::find_entry(
2036 const Sized_relobj_file<size, big_endian>* object,
2038 const elfcpp::Rela<size, big_endian>& reloc) const
2040 Glink_sym_ent ent(object, gsym, reloc);
2041 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2042 gold_assert(p != this->glink_entries_.end());
2046 template<int size, bool big_endian>
2048 Output_data_glink<size, big_endian>::find_entry(const Symbol* gsym) const
2050 Glink_sym_ent ent(gsym);
2051 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2052 gold_assert(p != this->glink_entries_.end());
2056 template<int size, bool big_endian>
2058 Output_data_glink<size, big_endian>::find_entry(
2059 const Sized_relobj_file<size, big_endian>* object,
2060 unsigned int locsym_index,
2061 const elfcpp::Rela<size, big_endian>& reloc) const
2063 Glink_sym_ent ent(object, locsym_index, reloc);
2064 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2065 gold_assert(p != this->glink_entries_.end());
2069 template<int size, bool big_endian>
2071 Output_data_glink<size, big_endian>::find_entry(
2072 const Sized_relobj_file<size, big_endian>* object,
2073 unsigned int locsym_index) const
2075 Glink_sym_ent ent(object, locsym_index);
2076 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2077 gold_assert(p != this->glink_entries_.end());
2081 template<int size, bool big_endian>
2083 Output_data_glink<size, big_endian>::set_final_data_size()
2085 unsigned int count = this->glink_entries_.size();
2086 off_t total = count;
2093 this->pltresolve_ = total;
2095 // space for branch table
2096 total += 4 * (count - 1);
2098 total += -total & 15;
2099 total += this->pltresolve_size;
2104 this->pltresolve_ = total;
2105 total += this->pltresolve_size;
2107 // space for branch table
2110 total += 4 * (count - 0x8000);
2114 this->set_data_size(total);
2117 static inline uint32_t
2123 static inline uint32_t
2129 static inline uint32_t
2132 return hi(a + 0x8000);
2135 template<bool big_endian>
2137 write_insn(unsigned char* p, uint32_t v)
2139 elfcpp::Swap<32, big_endian>::writeval(p, v);
2142 // Write out .glink.
2144 template<int size, bool big_endian>
2146 Output_data_glink<size, big_endian>::do_write(Output_file* of)
2148 const off_t off = this->offset();
2149 const section_size_type oview_size =
2150 convert_to_section_size_type(this->data_size());
2151 unsigned char* const oview = of->get_output_view(off, oview_size);
2154 // The base address of the .plt section.
2155 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2156 static const Address invalid_address = static_cast<Address>(0) - 1;
2157 Address plt_base = this->targ_->plt_section()->address();
2158 Address iplt_base = invalid_address;
2160 const Output_data_got_powerpc<size, big_endian>* got
2161 = this->targ_->got_section();
2165 Address got_os_addr = got->output_section()->address();
2167 // Write out call stubs.
2168 typename Glink_entries::const_iterator g;
2169 for (g = this->glink_entries_.begin();
2170 g != this->glink_entries_.end();
2175 const Symbol* gsym = g->first.sym_;
2178 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
2179 && gsym->can_use_relative_reloc(false));
2180 plt_addr = gsym->plt_offset();
2185 const Sized_relobj_file<size, big_endian>* relobj
2187 unsigned int local_sym_index = g->first.locsym_;
2188 plt_addr = relobj->local_plt_offset(local_sym_index);
2192 if (iplt_base == invalid_address)
2193 iplt_base = this->targ_->iplt_section()->address();
2194 plt_addr += iplt_base;
2197 plt_addr += plt_base;
2198 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2199 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
2200 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
2201 Address pltoff = plt_addr - got_addr;
2203 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
2204 gold_error(_("%s: linkage table error against `%s'"),
2205 g->first.object_->name().c_str(),
2206 g->first.sym_->demangled_name().c_str());
2208 p = oview + g->second * this->glink_entry_size();
2209 if (ha(pltoff) != 0)
2211 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
2212 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
2213 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
2214 if (ha(pltoff + 16) != ha(pltoff))
2216 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
2219 write_insn<big_endian>(p, mtctr_11), p += 4;
2220 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
2221 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
2222 write_insn<big_endian>(p, bctr), p += 4;
2226 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
2227 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
2228 if (ha(pltoff + 16) != ha(pltoff))
2230 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
2233 write_insn<big_endian>(p, mtctr_11), p += 4;
2234 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
2235 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
2236 write_insn<big_endian>(p, bctr), p += 4;
2240 // Write pltresolve stub.
2241 p = oview + this->pltresolve_;
2242 Address after_bcl = this->address() + this->pltresolve_ + 16;
2243 Address pltoff = plt_base - after_bcl;
2245 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
2247 write_insn<big_endian>(p, mflr_12), p += 4;
2248 write_insn<big_endian>(p, bcl_20_31), p += 4;
2249 write_insn<big_endian>(p, mflr_11), p += 4;
2250 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
2251 write_insn<big_endian>(p, mtlr_12), p += 4;
2252 write_insn<big_endian>(p, add_12_2_11), p += 4;
2253 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
2254 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
2255 write_insn<big_endian>(p, mtctr_11), p += 4;
2256 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
2257 write_insn<big_endian>(p, bctr), p += 4;
2258 while (p < oview + this->pltresolve_ + this->pltresolve_size)
2259 write_insn<big_endian>(p, nop), p += 4;
2261 // Write lazy link call stubs.
2263 while (p < oview + oview_size)
2267 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
2271 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
2272 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
2274 uint32_t branch_off = this->pltresolve_ + 8 - (p - oview);
2275 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
2281 // The address of _GLOBAL_OFFSET_TABLE_.
2282 Address g_o_t = got->address() + got->g_o_t();
2284 // Write out call stubs.
2285 typename Glink_entries::const_iterator g;
2286 for (g = this->glink_entries_.begin();
2287 g != this->glink_entries_.end();
2292 const Symbol* gsym = g->first.sym_;
2295 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
2296 && gsym->can_use_relative_reloc(false));
2297 plt_addr = gsym->plt_offset();
2302 const Sized_relobj_file<size, big_endian>* relobj
2304 unsigned int local_sym_index = g->first.locsym_;
2305 plt_addr = relobj->local_plt_offset(local_sym_index);
2309 if (iplt_base == invalid_address)
2310 iplt_base = this->targ_->iplt_section()->address();
2311 plt_addr += iplt_base;
2314 plt_addr += plt_base;
2316 p = oview + g->second * this->glink_entry_size();
2317 if (parameters->options().output_is_position_independent())
2320 const Powerpc_relobj<size, big_endian>* object = static_cast
2321 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
2322 if (object != NULL && g->first.addend_ >= 32768)
2324 unsigned int got2 = object->got2_shndx();
2325 got_addr = g->first.object_->get_output_section_offset(got2);
2326 gold_assert(got_addr != invalid_address);
2327 got_addr += (g->first.object_->output_section(got2)->address()
2328 + g->first.addend_);
2333 Address pltoff = plt_addr - got_addr;
2334 if (ha(pltoff) == 0)
2336 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
2337 write_insn<big_endian>(p + 4, mtctr_11);
2338 write_insn<big_endian>(p + 8, bctr);
2342 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
2343 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
2344 write_insn<big_endian>(p + 8, mtctr_11);
2345 write_insn<big_endian>(p + 12, bctr);
2350 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
2351 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
2352 write_insn<big_endian>(p + 8, mtctr_11);
2353 write_insn<big_endian>(p + 12, bctr);
2357 // Write out pltresolve branch table.
2358 p = oview + this->pltresolve_;
2359 unsigned int the_end = oview_size - this->pltresolve_size;
2360 unsigned char* end_p = oview + the_end;
2361 while (p < end_p - 8 * 4)
2362 write_insn<big_endian>(p, b + end_p - p), p += 4;
2364 write_insn<big_endian>(p, nop), p += 4;
2366 // Write out pltresolve call stub.
2367 if (parameters->options().output_is_position_independent())
2369 Address res0_off = this->pltresolve_;
2370 Address after_bcl_off = the_end + 12;
2371 Address bcl_res0 = after_bcl_off - res0_off;
2373 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
2374 write_insn<big_endian>(p + 4, mflr_0);
2375 write_insn<big_endian>(p + 8, bcl_20_31);
2376 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
2377 write_insn<big_endian>(p + 16, mflr_12);
2378 write_insn<big_endian>(p + 20, mtlr_0);
2379 write_insn<big_endian>(p + 24, sub_11_11_12);
2381 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
2383 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
2384 if (ha(got_bcl) == ha(got_bcl + 4))
2386 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
2387 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
2391 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
2392 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
2394 write_insn<big_endian>(p + 40, mtctr_0);
2395 write_insn<big_endian>(p + 44, add_0_11_11);
2396 write_insn<big_endian>(p + 48, add_11_0_11);
2397 write_insn<big_endian>(p + 52, bctr);
2398 write_insn<big_endian>(p + 56, nop);
2399 write_insn<big_endian>(p + 60, nop);
2403 Address res0 = this->pltresolve_ + this->address();
2405 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
2406 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
2407 if (ha(g_o_t + 4) == ha(g_o_t + 8))
2408 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
2410 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
2411 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
2412 write_insn<big_endian>(p + 16, mtctr_0);
2413 write_insn<big_endian>(p + 20, add_0_11_11);
2414 if (ha(g_o_t + 4) == ha(g_o_t + 8))
2415 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
2417 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
2418 write_insn<big_endian>(p + 28, add_11_0_11);
2419 write_insn<big_endian>(p + 32, bctr);
2420 write_insn<big_endian>(p + 36, nop);
2421 write_insn<big_endian>(p + 40, nop);
2422 write_insn<big_endian>(p + 44, nop);
2423 write_insn<big_endian>(p + 48, nop);
2424 write_insn<big_endian>(p + 52, nop);
2425 write_insn<big_endian>(p + 56, nop);
2426 write_insn<big_endian>(p + 60, nop);
2431 of->write_output_view(off, oview_size, oview);
2435 // A class to handle linker generated save/restore functions.
2437 template<int size, bool big_endian>
2438 class Output_data_save_res : public Output_section_data_build
2441 Output_data_save_res(Symbol_table* symtab);
2444 // Write to a map file.
2446 do_print_to_mapfile(Mapfile* mapfile) const
2447 { mapfile->print_output_data(this, _("** save/restore")); }
2450 do_write(Output_file*);
2453 // The maximum size of save/restore contents.
2454 static const unsigned int savres_max = 218*4;
2457 savres_define(Symbol_table* symtab,
2459 unsigned int lo, unsigned int hi,
2460 unsigned char* write_ent(unsigned char*, int),
2461 unsigned char* write_tail(unsigned char*, int));
2463 unsigned char *contents_;
2466 template<bool big_endian>
2467 static unsigned char*
2468 savegpr0(unsigned char* p, int r)
2470 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2471 write_insn<big_endian>(p, insn);
2475 template<bool big_endian>
2476 static unsigned char*
2477 savegpr0_tail(unsigned char* p, int r)
2479 p = savegpr0<big_endian>(p, r);
2480 uint32_t insn = std_0_1 + 16;
2481 write_insn<big_endian>(p, insn);
2483 write_insn<big_endian>(p, blr);
2487 template<bool big_endian>
2488 static unsigned char*
2489 restgpr0(unsigned char* p, int r)
2491 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2492 write_insn<big_endian>(p, insn);
2496 template<bool big_endian>
2497 static unsigned char*
2498 restgpr0_tail(unsigned char* p, int r)
2500 uint32_t insn = ld_0_1 + 16;
2501 write_insn<big_endian>(p, insn);
2503 p = restgpr0<big_endian>(p, r);
2504 write_insn<big_endian>(p, mtlr_0);
2508 p = restgpr0<big_endian>(p, 30);
2509 p = restgpr0<big_endian>(p, 31);
2511 write_insn<big_endian>(p, blr);
2515 template<bool big_endian>
2516 static unsigned char*
2517 savegpr1(unsigned char* p, int r)
2519 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
2520 write_insn<big_endian>(p, insn);
2524 template<bool big_endian>
2525 static unsigned char*
2526 savegpr1_tail(unsigned char* p, int r)
2528 p = savegpr1<big_endian>(p, r);
2529 write_insn<big_endian>(p, blr);
2533 template<bool big_endian>
2534 static unsigned char*
2535 restgpr1(unsigned char* p, int r)
2537 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
2538 write_insn<big_endian>(p, insn);
2542 template<bool big_endian>
2543 static unsigned char*
2544 restgpr1_tail(unsigned char* p, int r)
2546 p = restgpr1<big_endian>(p, r);
2547 write_insn<big_endian>(p, blr);
2551 template<bool big_endian>
2552 static unsigned char*
2553 savefpr(unsigned char* p, int r)
2555 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2556 write_insn<big_endian>(p, insn);
2560 template<bool big_endian>
2561 static unsigned char*
2562 savefpr0_tail(unsigned char* p, int r)
2564 p = savefpr<big_endian>(p, r);
2565 write_insn<big_endian>(p, std_0_1 + 16);
2567 write_insn<big_endian>(p, blr);
2571 template<bool big_endian>
2572 static unsigned char*
2573 restfpr(unsigned char* p, int r)
2575 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2576 write_insn<big_endian>(p, insn);
2580 template<bool big_endian>
2581 static unsigned char*
2582 restfpr0_tail(unsigned char* p, int r)
2584 write_insn<big_endian>(p, ld_0_1 + 16);
2586 p = restfpr<big_endian>(p, r);
2587 write_insn<big_endian>(p, mtlr_0);
2591 p = restfpr<big_endian>(p, 30);
2592 p = restfpr<big_endian>(p, 31);
2594 write_insn<big_endian>(p, blr);
2598 template<bool big_endian>
2599 static unsigned char*
2600 savefpr1_tail(unsigned char* p, int r)
2602 p = savefpr<big_endian>(p, r);
2603 write_insn<big_endian>(p, blr);
2607 template<bool big_endian>
2608 static unsigned char*
2609 restfpr1_tail(unsigned char* p, int r)
2611 p = restfpr<big_endian>(p, r);
2612 write_insn<big_endian>(p, blr);
2616 template<bool big_endian>
2617 static unsigned char*
2618 savevr(unsigned char* p, int r)
2620 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
2621 write_insn<big_endian>(p, insn);
2623 insn = stvx_0_12_0 + (r << 21);
2624 write_insn<big_endian>(p, insn);
2628 template<bool big_endian>
2629 static unsigned char*
2630 savevr_tail(unsigned char* p, int r)
2632 p = savevr<big_endian>(p, r);
2633 write_insn<big_endian>(p, blr);
2637 template<bool big_endian>
2638 static unsigned char*
2639 restvr(unsigned char* p, int r)
2641 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
2642 write_insn<big_endian>(p, insn);
2644 insn = lvx_0_12_0 + (r << 21);
2645 write_insn<big_endian>(p, insn);
2649 template<bool big_endian>
2650 static unsigned char*
2651 restvr_tail(unsigned char* p, int r)
2653 p = restvr<big_endian>(p, r);
2654 write_insn<big_endian>(p, blr);
2659 template<int size, bool big_endian>
2660 Output_data_save_res<size, big_endian>::Output_data_save_res(
2661 Symbol_table* symtab)
2662 : Output_section_data_build(4),
2665 this->savres_define(symtab,
2666 "_savegpr0_", 14, 31,
2667 savegpr0<big_endian>, savegpr0_tail<big_endian>);
2668 this->savres_define(symtab,
2669 "_restgpr0_", 14, 29,
2670 restgpr0<big_endian>, restgpr0_tail<big_endian>);
2671 this->savres_define(symtab,
2672 "_restgpr0_", 30, 31,
2673 restgpr0<big_endian>, restgpr0_tail<big_endian>);
2674 this->savres_define(symtab,
2675 "_savegpr1_", 14, 31,
2676 savegpr1<big_endian>, savegpr1_tail<big_endian>);
2677 this->savres_define(symtab,
2678 "_restgpr1_", 14, 31,
2679 restgpr1<big_endian>, restgpr1_tail<big_endian>);
2680 this->savres_define(symtab,
2681 "_savefpr_", 14, 31,
2682 savefpr<big_endian>, savefpr0_tail<big_endian>);
2683 this->savres_define(symtab,
2684 "_restfpr_", 14, 29,
2685 restfpr<big_endian>, restfpr0_tail<big_endian>);
2686 this->savres_define(symtab,
2687 "_restfpr_", 30, 31,
2688 restfpr<big_endian>, restfpr0_tail<big_endian>);
2689 this->savres_define(symtab,
2691 savefpr<big_endian>, savefpr1_tail<big_endian>);
2692 this->savres_define(symtab,
2694 restfpr<big_endian>, restfpr1_tail<big_endian>);
2695 this->savres_define(symtab,
2697 savevr<big_endian>, savevr_tail<big_endian>);
2698 this->savres_define(symtab,
2700 restvr<big_endian>, restvr_tail<big_endian>);
2703 template<int size, bool big_endian>
2705 Output_data_save_res<size, big_endian>::savres_define(
2706 Symbol_table* symtab,
2708 unsigned int lo, unsigned int hi,
2709 unsigned char* write_ent(unsigned char*, int),
2710 unsigned char* write_tail(unsigned char*, int))
2712 size_t len = strlen(name);
2713 bool writing = false;
2716 memcpy(sym, name, len);
2719 for (unsigned int i = lo; i <= hi; i++)
2721 sym[len + 0] = i / 10 + '0';
2722 sym[len + 1] = i % 10 + '0';
2723 Symbol* gsym = symtab->lookup(sym);
2724 bool refd = gsym != NULL && gsym->is_undefined();
2725 writing = writing || refd;
2728 if (this->contents_ == NULL)
2729 this->contents_ = new unsigned char[this->savres_max];
2731 off_t value = this->current_data_size();
2732 unsigned char* p = this->contents_ + value;
2734 p = write_ent(p, i);
2736 p = write_tail(p, i);
2737 off_t cur_size = p - this->contents_;
2738 this->set_current_data_size(cur_size);
2740 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
2741 this, value, cur_size - value,
2742 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
2743 elfcpp::STV_HIDDEN, 0, false, false);
2748 // Write out save/restore.
2750 template<int size, bool big_endian>
2752 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
2754 const off_t off = this->offset();
2755 const section_size_type oview_size =
2756 convert_to_section_size_type(this->data_size());
2757 unsigned char* const oview = of->get_output_view(off, oview_size);
2758 memcpy(oview, this->contents_, oview_size);
2759 of->write_output_view(off, oview_size, oview);
2763 // Create the glink section.
2765 template<int size, bool big_endian>
2767 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
2769 if (this->glink_ == NULL)
2771 this->glink_ = new Output_data_glink<size, big_endian>(this);
2772 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2773 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2774 this->glink_, ORDER_TEXT, false);
2778 // Create a PLT entry for a global symbol.
2780 template<int size, bool big_endian>
2782 Target_powerpc<size, big_endian>::make_plt_entry(
2785 const elfcpp::Rela<size, big_endian>& reloc,
2786 const Sized_relobj_file<size, big_endian>* object)
2788 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2789 && gsym->can_use_relative_reloc(false))
2791 if (this->iplt_ == NULL)
2792 this->make_iplt_section(layout);
2793 this->iplt_->add_ifunc_entry(gsym);
2797 if (this->plt_ == NULL)
2798 this->make_plt_section(layout);
2799 this->plt_->add_entry(gsym);
2801 this->glink_->add_entry(object, gsym, reloc);
2804 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2806 template<int size, bool big_endian>
2808 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
2810 const elfcpp::Rela<size, big_endian>& reloc,
2811 Sized_relobj_file<size, big_endian>* relobj)
2813 if (this->iplt_ == NULL)
2814 this->make_iplt_section(layout);
2815 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2816 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
2817 this->glink_->add_entry(relobj, r_sym, reloc);
2820 // Return the number of entries in the PLT.
2822 template<int size, bool big_endian>
2824 Target_powerpc<size, big_endian>::plt_entry_count() const
2826 if (this->plt_ == NULL)
2828 unsigned int count = this->plt_->entry_count();
2829 if (this->iplt_ != NULL)
2830 count += this->iplt_->entry_count();
2834 // Return the offset of the first non-reserved PLT entry.
2836 template<int size, bool big_endian>
2838 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
2840 return this->plt_->first_plt_entry_offset();
2843 // Return the size of each PLT entry.
2845 template<int size, bool big_endian>
2847 Target_powerpc<size, big_endian>::plt_entry_size() const
2849 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
2852 // Create a GOT entry for local dynamic __tls_get_addr calls.
2854 template<int size, bool big_endian>
2856 Target_powerpc<size, big_endian>::tlsld_got_offset(
2857 Symbol_table* symtab,
2859 Sized_relobj_file<size, big_endian>* object)
2861 if (this->tlsld_got_offset_ == -1U)
2863 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2864 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2865 Output_data_got_powerpc<size, big_endian>* got
2866 = this->got_section(symtab, layout);
2867 unsigned int got_offset = got->add_constant_pair(0, 0);
2868 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
2870 this->tlsld_got_offset_ = got_offset;
2872 return this->tlsld_got_offset_;
2875 // Get the Reference_flags for a particular relocation.
2877 template<int size, bool big_endian>
2879 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
2883 case elfcpp::R_POWERPC_NONE:
2884 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2885 case elfcpp::R_POWERPC_GNU_VTENTRY:
2886 case elfcpp::R_PPC64_TOC:
2887 // No symbol reference.
2890 case elfcpp::R_PPC64_ADDR64:
2891 case elfcpp::R_PPC64_UADDR64:
2892 case elfcpp::R_POWERPC_ADDR32:
2893 case elfcpp::R_POWERPC_UADDR32:
2894 case elfcpp::R_POWERPC_ADDR16:
2895 case elfcpp::R_POWERPC_UADDR16:
2896 case elfcpp::R_POWERPC_ADDR16_LO:
2897 case elfcpp::R_POWERPC_ADDR16_HI:
2898 case elfcpp::R_POWERPC_ADDR16_HA:
2899 return Symbol::ABSOLUTE_REF;
2901 case elfcpp::R_POWERPC_ADDR24:
2902 case elfcpp::R_POWERPC_ADDR14:
2903 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2904 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2905 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2907 case elfcpp::R_PPC64_REL64:
2908 case elfcpp::R_POWERPC_REL32:
2909 case elfcpp::R_PPC_LOCAL24PC:
2910 case elfcpp::R_POWERPC_REL16:
2911 case elfcpp::R_POWERPC_REL16_LO:
2912 case elfcpp::R_POWERPC_REL16_HI:
2913 case elfcpp::R_POWERPC_REL16_HA:
2914 return Symbol::RELATIVE_REF;
2916 case elfcpp::R_POWERPC_REL24:
2917 case elfcpp::R_PPC_PLTREL24:
2918 case elfcpp::R_POWERPC_REL14:
2919 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2920 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2921 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2923 case elfcpp::R_POWERPC_GOT16:
2924 case elfcpp::R_POWERPC_GOT16_LO:
2925 case elfcpp::R_POWERPC_GOT16_HI:
2926 case elfcpp::R_POWERPC_GOT16_HA:
2927 case elfcpp::R_PPC64_GOT16_DS:
2928 case elfcpp::R_PPC64_GOT16_LO_DS:
2929 case elfcpp::R_PPC64_TOC16:
2930 case elfcpp::R_PPC64_TOC16_LO:
2931 case elfcpp::R_PPC64_TOC16_HI:
2932 case elfcpp::R_PPC64_TOC16_HA:
2933 case elfcpp::R_PPC64_TOC16_DS:
2934 case elfcpp::R_PPC64_TOC16_LO_DS:
2936 return Symbol::ABSOLUTE_REF;
2938 case elfcpp::R_POWERPC_GOT_TPREL16:
2939 case elfcpp::R_POWERPC_TLS:
2940 return Symbol::TLS_REF;
2942 case elfcpp::R_POWERPC_COPY:
2943 case elfcpp::R_POWERPC_GLOB_DAT:
2944 case elfcpp::R_POWERPC_JMP_SLOT:
2945 case elfcpp::R_POWERPC_RELATIVE:
2946 case elfcpp::R_POWERPC_DTPMOD:
2948 // Not expected. We will give an error later.
2953 // Report an unsupported relocation against a local symbol.
2955 template<int size, bool big_endian>
2957 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
2958 Sized_relobj_file<size, big_endian>* object,
2959 unsigned int r_type)
2961 gold_error(_("%s: unsupported reloc %u against local symbol"),
2962 object->name().c_str(), r_type);
2965 // We are about to emit a dynamic relocation of type R_TYPE. If the
2966 // dynamic linker does not support it, issue an error.
2968 template<int size, bool big_endian>
2970 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
2971 unsigned int r_type)
2973 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
2975 // These are the relocation types supported by glibc for both 32-bit
2976 // and 64-bit powerpc.
2979 case elfcpp::R_POWERPC_NONE:
2980 case elfcpp::R_POWERPC_RELATIVE:
2981 case elfcpp::R_POWERPC_GLOB_DAT:
2982 case elfcpp::R_POWERPC_DTPMOD:
2983 case elfcpp::R_POWERPC_DTPREL:
2984 case elfcpp::R_POWERPC_TPREL:
2985 case elfcpp::R_POWERPC_JMP_SLOT:
2986 case elfcpp::R_POWERPC_COPY:
2987 case elfcpp::R_POWERPC_IRELATIVE:
2988 case elfcpp::R_POWERPC_ADDR32:
2989 case elfcpp::R_POWERPC_UADDR32:
2990 case elfcpp::R_POWERPC_ADDR24:
2991 case elfcpp::R_POWERPC_ADDR16:
2992 case elfcpp::R_POWERPC_UADDR16:
2993 case elfcpp::R_POWERPC_ADDR16_LO:
2994 case elfcpp::R_POWERPC_ADDR16_HI:
2995 case elfcpp::R_POWERPC_ADDR16_HA:
2996 case elfcpp::R_POWERPC_ADDR14:
2997 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2998 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2999 case elfcpp::R_POWERPC_REL32:
3000 case elfcpp::R_POWERPC_REL24:
3001 case elfcpp::R_POWERPC_TPREL16:
3002 case elfcpp::R_POWERPC_TPREL16_LO:
3003 case elfcpp::R_POWERPC_TPREL16_HI:
3004 case elfcpp::R_POWERPC_TPREL16_HA:
3015 // These are the relocation types supported only on 64-bit.
3016 case elfcpp::R_PPC64_ADDR64:
3017 case elfcpp::R_PPC64_UADDR64:
3018 case elfcpp::R_PPC64_JMP_IREL:
3019 case elfcpp::R_PPC64_ADDR16_DS:
3020 case elfcpp::R_PPC64_ADDR16_LO_DS:
3021 case elfcpp::R_PPC64_ADDR16_HIGHER:
3022 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3023 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3024 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3025 case elfcpp::R_PPC64_REL64:
3026 case elfcpp::R_POWERPC_ADDR30:
3027 case elfcpp::R_PPC64_TPREL16_DS:
3028 case elfcpp::R_PPC64_TPREL16_LO_DS:
3029 case elfcpp::R_PPC64_TPREL16_HIGHER:
3030 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3031 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3032 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3043 // These are the relocation types supported only on 32-bit.
3044 // ??? glibc ld.so doesn't need to support these.
3045 case elfcpp::R_POWERPC_DTPREL16:
3046 case elfcpp::R_POWERPC_DTPREL16_LO:
3047 case elfcpp::R_POWERPC_DTPREL16_HI:
3048 case elfcpp::R_POWERPC_DTPREL16_HA:
3056 // This prevents us from issuing more than one error per reloc
3057 // section. But we can still wind up issuing more than one
3058 // error per object file.
3059 if (this->issued_non_pic_error_)
3061 gold_assert(parameters->options().output_is_position_independent());
3062 object->error(_("requires unsupported dynamic reloc; "
3063 "recompile with -fPIC"));
3064 this->issued_non_pic_error_ = true;
3068 // Return whether we need to make a PLT entry for a relocation of the
3069 // given type against a STT_GNU_IFUNC symbol.
3071 template<int size, bool big_endian>
3073 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
3074 Sized_relobj_file<size, big_endian>* object,
3075 unsigned int r_type)
3077 // In non-pic code any reference will resolve to the plt call stub
3078 // for the ifunc symbol.
3079 if (size == 32 && !parameters->options().output_is_position_independent())
3084 // Word size refs from data sections are OK.
3085 case elfcpp::R_POWERPC_ADDR32:
3086 case elfcpp::R_POWERPC_UADDR32:
3091 case elfcpp::R_PPC64_ADDR64:
3092 case elfcpp::R_PPC64_UADDR64:
3097 // GOT refs are good.
3098 case elfcpp::R_POWERPC_GOT16:
3099 case elfcpp::R_POWERPC_GOT16_LO:
3100 case elfcpp::R_POWERPC_GOT16_HI:
3101 case elfcpp::R_POWERPC_GOT16_HA:
3102 case elfcpp::R_PPC64_GOT16_DS:
3103 case elfcpp::R_PPC64_GOT16_LO_DS:
3106 // So are function calls.
3107 case elfcpp::R_POWERPC_ADDR24:
3108 case elfcpp::R_POWERPC_ADDR14:
3109 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3110 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3111 case elfcpp::R_POWERPC_REL24:
3112 case elfcpp::R_PPC_PLTREL24:
3113 case elfcpp::R_POWERPC_REL14:
3114 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3115 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3122 // Anything else is a problem.
3123 // If we are building a static executable, the libc startup function
3124 // responsible for applying indirect function relocations is going
3125 // to complain about the reloc type.
3126 // If we are building a dynamic executable, we will have a text
3127 // relocation. The dynamic loader will set the text segment
3128 // writable and non-executable to apply text relocations. So we'll
3129 // segfault when trying to run the indirection function to resolve
3131 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
3132 object->name().c_str(), r_type);
3136 // Scan a relocation for a local symbol.
3138 template<int size, bool big_endian>
3140 Target_powerpc<size, big_endian>::Scan::local(
3141 Symbol_table* symtab,
3143 Target_powerpc<size, big_endian>* target,
3144 Sized_relobj_file<size, big_endian>* object,
3145 unsigned int data_shndx,
3146 Output_section* output_section,
3147 const elfcpp::Rela<size, big_endian>& reloc,
3148 unsigned int r_type,
3149 const elfcpp::Sym<size, big_endian>& lsym,
3152 Powerpc_relobj<size, big_endian>* ppc_object
3153 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3158 && data_shndx == ppc_object->opd_shndx()
3159 && r_type == elfcpp::R_PPC64_ADDR64)
3160 ppc_object->set_opd_discard(reloc.get_r_offset());
3164 // A local STT_GNU_IFUNC symbol may require a PLT entry.
3165 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
3166 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
3167 target->make_local_ifunc_plt_entry(layout, reloc, object);
3171 case elfcpp::R_POWERPC_NONE:
3172 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3173 case elfcpp::R_POWERPC_GNU_VTENTRY:
3174 case elfcpp::R_PPC64_TOCSAVE:
3175 case elfcpp::R_PPC_EMB_MRKREF:
3176 case elfcpp::R_POWERPC_TLS:
3179 case elfcpp::R_PPC64_TOC:
3181 Output_data_got_powerpc<size, big_endian>* got
3182 = target->got_section(symtab, layout);
3183 if (parameters->options().output_is_position_independent())
3185 Address off = reloc.get_r_offset();
3187 && data_shndx == ppc_object->opd_shndx()
3188 && ppc_object->get_opd_discard(off - 8))
3191 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3192 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
3193 rela_dyn->add_output_section_relative(got->output_section(),
3194 elfcpp::R_POWERPC_RELATIVE,
3196 object, data_shndx, off,
3197 symobj->toc_base_offset());
3202 case elfcpp::R_PPC64_ADDR64:
3203 case elfcpp::R_PPC64_UADDR64:
3204 case elfcpp::R_POWERPC_ADDR32:
3205 case elfcpp::R_POWERPC_UADDR32:
3206 case elfcpp::R_POWERPC_ADDR24:
3207 case elfcpp::R_POWERPC_ADDR16:
3208 case elfcpp::R_POWERPC_ADDR16_LO:
3209 case elfcpp::R_POWERPC_ADDR16_HI:
3210 case elfcpp::R_POWERPC_ADDR16_HA:
3211 case elfcpp::R_POWERPC_UADDR16:
3212 case elfcpp::R_PPC64_ADDR16_HIGHER:
3213 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3214 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3215 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3216 case elfcpp::R_PPC64_ADDR16_DS:
3217 case elfcpp::R_PPC64_ADDR16_LO_DS:
3218 case elfcpp::R_POWERPC_ADDR14:
3219 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3220 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3221 // If building a shared library (or a position-independent
3222 // executable), we need to create a dynamic relocation for
3224 if (parameters->options().output_is_position_independent()
3225 || (size == 64 && is_ifunc))
3227 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3229 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
3230 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
3232 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3233 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3236 rela_dyn = target->iplt_section()->rel_plt();
3237 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3239 rela_dyn->add_local_relative(object, r_sym, dynrel,
3240 output_section, data_shndx,
3241 reloc.get_r_offset(),
3242 reloc.get_r_addend(), false);
3246 check_non_pic(object, r_type);
3247 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3248 rela_dyn->add_local(object, r_sym, r_type, output_section,
3249 data_shndx, reloc.get_r_offset(),
3250 reloc.get_r_addend());
3255 case elfcpp::R_PPC64_REL64:
3256 case elfcpp::R_POWERPC_REL32:
3257 case elfcpp::R_POWERPC_REL24:
3258 case elfcpp::R_PPC_PLTREL24:
3259 case elfcpp::R_PPC_LOCAL24PC:
3260 case elfcpp::R_POWERPC_REL16:
3261 case elfcpp::R_POWERPC_REL16_LO:
3262 case elfcpp::R_POWERPC_REL16_HI:
3263 case elfcpp::R_POWERPC_REL16_HA:
3264 case elfcpp::R_POWERPC_REL14:
3265 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3266 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3267 case elfcpp::R_POWERPC_SECTOFF:
3268 case elfcpp::R_POWERPC_TPREL16:
3269 case elfcpp::R_POWERPC_DTPREL16:
3270 case elfcpp::R_POWERPC_SECTOFF_LO:
3271 case elfcpp::R_POWERPC_TPREL16_LO:
3272 case elfcpp::R_POWERPC_DTPREL16_LO:
3273 case elfcpp::R_POWERPC_SECTOFF_HI:
3274 case elfcpp::R_POWERPC_TPREL16_HI:
3275 case elfcpp::R_POWERPC_DTPREL16_HI:
3276 case elfcpp::R_POWERPC_SECTOFF_HA:
3277 case elfcpp::R_POWERPC_TPREL16_HA:
3278 case elfcpp::R_POWERPC_DTPREL16_HA:
3279 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3280 case elfcpp::R_PPC64_TPREL16_HIGHER:
3281 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3282 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3283 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3284 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3285 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3286 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3287 case elfcpp::R_PPC64_TPREL16_DS:
3288 case elfcpp::R_PPC64_TPREL16_LO_DS:
3289 case elfcpp::R_PPC64_DTPREL16_DS:
3290 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3291 case elfcpp::R_PPC64_SECTOFF_DS:
3292 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3293 case elfcpp::R_PPC64_TLSGD:
3294 case elfcpp::R_PPC64_TLSLD:
3297 case elfcpp::R_POWERPC_GOT16:
3298 case elfcpp::R_POWERPC_GOT16_LO:
3299 case elfcpp::R_POWERPC_GOT16_HI:
3300 case elfcpp::R_POWERPC_GOT16_HA:
3301 case elfcpp::R_PPC64_GOT16_DS:
3302 case elfcpp::R_PPC64_GOT16_LO_DS:
3304 // The symbol requires a GOT entry.
3305 Output_data_got_powerpc<size, big_endian>* got
3306 = target->got_section(symtab, layout);
3307 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3309 if (!parameters->options().output_is_position_independent())
3311 if (size == 32 && is_ifunc)
3312 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
3314 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3316 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
3318 // If we are generating a shared object or a pie, this
3319 // symbol's GOT entry will be set by a dynamic relocation.
3321 off = got->add_constant(0);
3322 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
3324 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3325 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3328 rela_dyn = target->iplt_section()->rel_plt();
3329 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3331 rela_dyn->add_local_relative(object, r_sym, dynrel,
3332 got, off, 0, false);
3337 case elfcpp::R_PPC64_TOC16:
3338 case elfcpp::R_PPC64_TOC16_LO:
3339 case elfcpp::R_PPC64_TOC16_HI:
3340 case elfcpp::R_PPC64_TOC16_HA:
3341 case elfcpp::R_PPC64_TOC16_DS:
3342 case elfcpp::R_PPC64_TOC16_LO_DS:
3343 // We need a GOT section.
3344 target->got_section(symtab, layout);
3347 case elfcpp::R_POWERPC_GOT_TLSGD16:
3348 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3349 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3350 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3352 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
3353 if (tls_type == tls::TLSOPT_NONE)
3355 Output_data_got_powerpc<size, big_endian>* got
3356 = target->got_section(symtab, layout);
3357 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3358 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3359 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
3360 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
3362 else if (tls_type == tls::TLSOPT_TO_LE)
3364 // no GOT relocs needed for Local Exec.
3371 case elfcpp::R_POWERPC_GOT_TLSLD16:
3372 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3373 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3374 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3376 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3377 if (tls_type == tls::TLSOPT_NONE)
3378 target->tlsld_got_offset(symtab, layout, object);
3379 else if (tls_type == tls::TLSOPT_TO_LE)
3381 // no GOT relocs needed for Local Exec.
3382 if (parameters->options().emit_relocs())
3384 Output_section* os = layout->tls_segment()->first_section();
3385 gold_assert(os != NULL);
3386 os->set_needs_symtab_index();
3394 case elfcpp::R_POWERPC_GOT_DTPREL16:
3395 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3396 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3397 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3399 Output_data_got_powerpc<size, big_endian>* got
3400 = target->got_section(symtab, layout);
3401 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3402 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
3406 case elfcpp::R_POWERPC_GOT_TPREL16:
3407 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3408 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3409 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3411 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
3412 if (tls_type == tls::TLSOPT_NONE)
3414 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3415 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
3417 Output_data_got_powerpc<size, big_endian>* got
3418 = target->got_section(symtab, layout);
3419 unsigned int off = got->add_constant(0);
3420 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
3422 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3423 rela_dyn->add_symbolless_local_addend(object, r_sym,
3424 elfcpp::R_POWERPC_TPREL,
3428 else if (tls_type == tls::TLSOPT_TO_LE)
3430 // no GOT relocs needed for Local Exec.
3438 unsupported_reloc_local(object, r_type);
3443 // Report an unsupported relocation against a global symbol.
3445 template<int size, bool big_endian>
3447 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
3448 Sized_relobj_file<size, big_endian>* object,
3449 unsigned int r_type,
3452 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3453 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3456 // Scan a relocation for a global symbol.
3458 template<int size, bool big_endian>
3460 Target_powerpc<size, big_endian>::Scan::global(
3461 Symbol_table* symtab,
3463 Target_powerpc<size, big_endian>* target,
3464 Sized_relobj_file<size, big_endian>* object,
3465 unsigned int data_shndx,
3466 Output_section* output_section,
3467 const elfcpp::Rela<size, big_endian>& reloc,
3468 unsigned int r_type,
3471 Powerpc_relobj<size, big_endian>* ppc_object
3472 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3474 // A STT_GNU_IFUNC symbol may require a PLT entry.
3475 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3476 && this->reloc_needs_plt_for_ifunc(object, r_type))
3477 target->make_plt_entry(layout, gsym, reloc, object);
3481 case elfcpp::R_POWERPC_NONE:
3482 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3483 case elfcpp::R_POWERPC_GNU_VTENTRY:
3484 case elfcpp::R_PPC_LOCAL24PC:
3485 case elfcpp::R_PPC_EMB_MRKREF:
3486 case elfcpp::R_POWERPC_TLS:
3489 case elfcpp::R_PPC64_TOC:
3491 Output_data_got_powerpc<size, big_endian>* got
3492 = target->got_section(symtab, layout);
3493 if (parameters->options().output_is_position_independent())
3495 Address off = reloc.get_r_offset();
3497 && data_shndx == ppc_object->opd_shndx()
3498 && ppc_object->get_opd_discard(off - 8))
3501 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3502 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
3503 if (data_shndx != ppc_object->opd_shndx())
3504 symobj = static_cast
3505 <Powerpc_relobj<size, big_endian>*>(gsym->object());
3506 rela_dyn->add_output_section_relative(got->output_section(),
3507 elfcpp::R_POWERPC_RELATIVE,
3509 object, data_shndx, off,
3510 symobj->toc_base_offset());
3515 case elfcpp::R_PPC64_ADDR64:
3517 && data_shndx == ppc_object->opd_shndx()
3518 && (gsym->is_defined_in_discarded_section()
3519 || gsym->object() != object))
3521 ppc_object->set_opd_discard(reloc.get_r_offset());
3525 case elfcpp::R_PPC64_UADDR64:
3526 case elfcpp::R_POWERPC_ADDR32:
3527 case elfcpp::R_POWERPC_UADDR32:
3528 case elfcpp::R_POWERPC_ADDR24:
3529 case elfcpp::R_POWERPC_ADDR16:
3530 case elfcpp::R_POWERPC_ADDR16_LO:
3531 case elfcpp::R_POWERPC_ADDR16_HI:
3532 case elfcpp::R_POWERPC_ADDR16_HA:
3533 case elfcpp::R_POWERPC_UADDR16:
3534 case elfcpp::R_PPC64_ADDR16_HIGHER:
3535 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3536 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3537 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3538 case elfcpp::R_PPC64_ADDR16_DS:
3539 case elfcpp::R_PPC64_ADDR16_LO_DS:
3540 case elfcpp::R_POWERPC_ADDR14:
3541 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3542 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3544 // Make a PLT entry if necessary.
3545 if (gsym->needs_plt_entry())
3547 target->make_plt_entry(layout, gsym, reloc, 0);
3548 // Since this is not a PC-relative relocation, we may be
3549 // taking the address of a function. In that case we need to
3550 // set the entry in the dynamic symbol table to the address of
3551 // the PLT call stub.
3553 && gsym->is_from_dynobj()
3554 && !parameters->options().output_is_position_independent())
3555 gsym->set_needs_dynsym_value();
3557 // Make a dynamic relocation if necessary.
3558 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
3559 || (size == 64 && gsym->type() == elfcpp::STT_GNU_IFUNC))
3561 if (gsym->may_need_copy_reloc())
3563 target->copy_reloc(symtab, layout, object,
3564 data_shndx, output_section, gsym, reloc);
3566 else if (((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
3567 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
3568 && (gsym->can_use_relative_reloc(false)
3570 && data_shndx == ppc_object->opd_shndx())))
3572 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3573 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3574 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3576 rela_dyn = target->iplt_section()->rel_plt();
3577 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3579 rela_dyn->add_symbolless_global_addend(
3580 gsym, dynrel, output_section, object, data_shndx,
3581 reloc.get_r_offset(), reloc.get_r_addend());
3585 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3586 check_non_pic(object, r_type);
3587 rela_dyn->add_global(gsym, r_type, output_section,
3589 reloc.get_r_offset(),
3590 reloc.get_r_addend());
3596 case elfcpp::R_PPC_PLTREL24:
3597 case elfcpp::R_POWERPC_REL24:
3598 if (gsym->needs_plt_entry()
3599 || (!gsym->final_value_is_known()
3600 && (gsym->is_undefined()
3601 || gsym->is_from_dynobj()
3602 || gsym->is_preemptible())))
3603 target->make_plt_entry(layout, gsym, reloc, object);
3606 case elfcpp::R_PPC64_REL64:
3607 case elfcpp::R_POWERPC_REL32:
3608 // Make a dynamic relocation if necessary.
3609 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
3611 if (gsym->may_need_copy_reloc())
3613 target->copy_reloc(symtab, layout, object,
3614 data_shndx, output_section, gsym,
3619 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3620 check_non_pic(object, r_type);
3621 rela_dyn->add_global(gsym, r_type, output_section, object,
3622 data_shndx, reloc.get_r_offset(),
3623 reloc.get_r_addend());
3628 case elfcpp::R_POWERPC_REL16:
3629 case elfcpp::R_POWERPC_REL16_LO:
3630 case elfcpp::R_POWERPC_REL16_HI:
3631 case elfcpp::R_POWERPC_REL16_HA:
3632 case elfcpp::R_POWERPC_REL14:
3633 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3634 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3635 case elfcpp::R_POWERPC_SECTOFF:
3636 case elfcpp::R_POWERPC_TPREL16:
3637 case elfcpp::R_POWERPC_DTPREL16:
3638 case elfcpp::R_POWERPC_SECTOFF_LO:
3639 case elfcpp::R_POWERPC_TPREL16_LO:
3640 case elfcpp::R_POWERPC_DTPREL16_LO:
3641 case elfcpp::R_POWERPC_SECTOFF_HI:
3642 case elfcpp::R_POWERPC_TPREL16_HI:
3643 case elfcpp::R_POWERPC_DTPREL16_HI:
3644 case elfcpp::R_POWERPC_SECTOFF_HA:
3645 case elfcpp::R_POWERPC_TPREL16_HA:
3646 case elfcpp::R_POWERPC_DTPREL16_HA:
3647 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3648 case elfcpp::R_PPC64_TPREL16_HIGHER:
3649 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3650 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3651 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3652 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3653 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3654 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3655 case elfcpp::R_PPC64_TPREL16_DS:
3656 case elfcpp::R_PPC64_TPREL16_LO_DS:
3657 case elfcpp::R_PPC64_DTPREL16_DS:
3658 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3659 case elfcpp::R_PPC64_SECTOFF_DS:
3660 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3661 case elfcpp::R_PPC64_TLSGD:
3662 case elfcpp::R_PPC64_TLSLD:
3665 case elfcpp::R_POWERPC_GOT16:
3666 case elfcpp::R_POWERPC_GOT16_LO:
3667 case elfcpp::R_POWERPC_GOT16_HI:
3668 case elfcpp::R_POWERPC_GOT16_HA:
3669 case elfcpp::R_PPC64_GOT16_DS:
3670 case elfcpp::R_PPC64_GOT16_LO_DS:
3672 // The symbol requires a GOT entry.
3673 Output_data_got_powerpc<size, big_endian>* got;
3675 got = target->got_section(symtab, layout);
3676 if (gsym->final_value_is_known())
3678 if (size == 32 && gsym->type() == elfcpp::STT_GNU_IFUNC)
3679 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3681 got->add_global(gsym, GOT_TYPE_STANDARD);
3683 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
3685 // If we are generating a shared object or a pie, this
3686 // symbol's GOT entry will be set by a dynamic relocation.
3687 unsigned int off = got->add_constant(0);
3688 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
3690 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3691 if (gsym->can_use_relative_reloc(false)
3693 && gsym->visibility() == elfcpp::STV_PROTECTED
3694 && parameters->options().shared()))
3696 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3697 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3699 rela_dyn = target->iplt_section()->rel_plt();
3700 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3702 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
3706 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
3707 rela_dyn->add_global(gsym, dynrel, got, off, 0);
3713 case elfcpp::R_PPC64_TOC16:
3714 case elfcpp::R_PPC64_TOC16_LO:
3715 case elfcpp::R_PPC64_TOC16_HI:
3716 case elfcpp::R_PPC64_TOC16_HA:
3717 case elfcpp::R_PPC64_TOC16_DS:
3718 case elfcpp::R_PPC64_TOC16_LO_DS:
3719 // We need a GOT section.
3720 target->got_section(symtab, layout);
3723 case elfcpp::R_POWERPC_GOT_TLSGD16:
3724 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3725 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3726 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3728 const bool final = gsym->final_value_is_known();
3729 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3730 if (tls_type == tls::TLSOPT_NONE)
3732 Output_data_got_powerpc<size, big_endian>* got
3733 = target->got_section(symtab, layout);
3734 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
3735 target->rela_dyn_section(layout),
3736 elfcpp::R_POWERPC_DTPMOD,
3737 elfcpp::R_POWERPC_DTPREL);
3739 else if (tls_type == tls::TLSOPT_TO_IE)
3741 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
3743 Output_data_got_powerpc<size, big_endian>* got
3744 = target->got_section(symtab, layout);
3745 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3746 if (gsym->is_undefined()
3747 || gsym->is_from_dynobj())
3749 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
3750 elfcpp::R_POWERPC_TPREL);
3754 unsigned int off = got->add_constant(0);
3755 gsym->set_got_offset(GOT_TYPE_TPREL, off);
3756 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
3757 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
3762 else if (tls_type == tls::TLSOPT_TO_LE)
3764 // no GOT relocs needed for Local Exec.
3771 case elfcpp::R_POWERPC_GOT_TLSLD16:
3772 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3773 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3774 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3776 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3777 if (tls_type == tls::TLSOPT_NONE)
3778 target->tlsld_got_offset(symtab, layout, object);
3779 else if (tls_type == tls::TLSOPT_TO_LE)
3781 // no GOT relocs needed for Local Exec.
3782 if (parameters->options().emit_relocs())
3784 Output_section* os = layout->tls_segment()->first_section();
3785 gold_assert(os != NULL);
3786 os->set_needs_symtab_index();
3794 case elfcpp::R_POWERPC_GOT_DTPREL16:
3795 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3796 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3797 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3799 Output_data_got_powerpc<size, big_endian>* got
3800 = target->got_section(symtab, layout);
3801 if (!gsym->final_value_is_known()
3802 && (gsym->is_from_dynobj()
3803 || gsym->is_undefined()
3804 || gsym->is_preemptible()))
3805 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
3806 target->rela_dyn_section(layout),
3807 elfcpp::R_POWERPC_DTPREL);
3809 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
3813 case elfcpp::R_POWERPC_GOT_TPREL16:
3814 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3815 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3816 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3818 const bool final = gsym->final_value_is_known();
3819 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3820 if (tls_type == tls::TLSOPT_NONE)
3822 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
3824 Output_data_got_powerpc<size, big_endian>* got
3825 = target->got_section(symtab, layout);
3826 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3827 if (gsym->is_undefined()
3828 || gsym->is_from_dynobj())
3830 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
3831 elfcpp::R_POWERPC_TPREL);
3835 unsigned int off = got->add_constant(0);
3836 gsym->set_got_offset(GOT_TYPE_TPREL, off);
3837 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
3838 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
3843 else if (tls_type == tls::TLSOPT_TO_LE)
3845 // no GOT relocs needed for Local Exec.
3853 unsupported_reloc_global(object, r_type, gsym);
3858 // Process relocations for gc.
3860 template<int size, bool big_endian>
3862 Target_powerpc<size, big_endian>::gc_process_relocs(
3863 Symbol_table* symtab,
3865 Sized_relobj_file<size, big_endian>* object,
3866 unsigned int data_shndx,
3868 const unsigned char* prelocs,
3870 Output_section* output_section,
3871 bool needs_special_offset_handling,
3872 size_t local_symbol_count,
3873 const unsigned char* plocal_symbols)
3875 typedef Target_powerpc<size, big_endian> Powerpc;
3876 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
3877 Powerpc_relobj<size, big_endian>* ppc_object
3878 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3880 ppc_object->set_opd_valid();
3881 if (size == 64 && data_shndx == ppc_object->opd_shndx())
3883 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
3884 for (p = ppc_object->access_from_map()->begin();
3885 p != ppc_object->access_from_map()->end();
3888 Address dst_off = p->first;
3889 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
3890 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
3891 for (s = p->second.begin(); s != p->second.end(); ++s)
3893 Object* src_obj = s->first;
3894 unsigned int src_indx = s->second;
3895 symtab->gc()->add_reference(src_obj, src_indx,
3896 ppc_object, dst_indx);
3900 ppc_object->access_from_map()->clear();
3901 ppc_object->process_gc_mark(symtab);
3902 // Don't look at .opd relocs as .opd will reference everything.
3906 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
3907 typename Target_powerpc::Relocatable_size_for_reloc>(
3916 needs_special_offset_handling,
3921 // Handle target specific gc actions when adding a gc reference from
3922 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
3923 // and DST_OFF. For powerpc64, this adds a referenc to the code
3924 // section of a function descriptor.
3926 template<int size, bool big_endian>
3928 Target_powerpc<size, big_endian>::do_gc_add_reference(
3929 Symbol_table* symtab,
3931 unsigned int src_shndx,
3933 unsigned int dst_shndx,
3934 Address dst_off) const
3936 Powerpc_relobj<size, big_endian>* ppc_object
3937 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
3939 && !ppc_object->is_dynamic()
3940 && dst_shndx == ppc_object->opd_shndx())
3942 if (ppc_object->opd_valid())
3944 dst_shndx = ppc_object->get_opd_ent(dst_off);
3945 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
3949 // If we haven't run scan_opd_relocs, we must delay
3950 // processing this function descriptor reference.
3951 ppc_object->add_reference(src_obj, src_shndx, dst_off);
3956 // Add any special sections for this symbol to the gc work list.
3957 // For powerpc64, this adds the code section of a function
3960 template<int size, bool big_endian>
3962 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
3963 Symbol_table* symtab,
3968 Powerpc_relobj<size, big_endian>* ppc_object
3969 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
3971 unsigned int shndx = sym->shndx(&is_ordinary);
3972 if (is_ordinary && shndx == ppc_object->opd_shndx())
3974 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
3975 Address dst_off = gsym->value();
3976 if (ppc_object->opd_valid())
3978 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
3979 symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
3982 ppc_object->add_gc_mark(dst_off);
3987 // Scan relocations for a section.
3989 template<int size, bool big_endian>
3991 Target_powerpc<size, big_endian>::scan_relocs(
3992 Symbol_table* symtab,
3994 Sized_relobj_file<size, big_endian>* object,
3995 unsigned int data_shndx,
3996 unsigned int sh_type,
3997 const unsigned char* prelocs,
3999 Output_section* output_section,
4000 bool needs_special_offset_handling,
4001 size_t local_symbol_count,
4002 const unsigned char* plocal_symbols)
4004 typedef Target_powerpc<size, big_endian> Powerpc;
4005 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
4007 if (sh_type == elfcpp::SHT_REL)
4009 gold_error(_("%s: unsupported REL reloc section"),
4010 object->name().c_str());
4016 // Define a weak hidden _GLOBAL_OFFSET_TABLE_ to ensure it isn't
4017 // seen as undefined when scanning relocs (and thus requires
4018 // non-relative dynamic relocs). The proper value will be
4020 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
4021 if (gotsym != NULL && gotsym->is_undefined())
4022 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
4023 Symbol_table::PREDEFINED,
4024 this->got_section(symtab, layout), 0, 0,
4027 elfcpp::STV_HIDDEN, 0,
4030 static Output_data_space* sdata;
4032 // Define _SDA_BASE_ at the start of the .sdata section.
4035 // layout->find_output_section(".sdata") == NULL
4036 sdata = new Output_data_space(4, "** sdata");
4038 = layout->add_output_section_data(".sdata", 0,
4040 | elfcpp::SHF_WRITE,
4041 sdata, ORDER_SMALL_DATA, false);
4042 symtab->define_in_output_data("_SDA_BASE_", NULL,
4043 Symbol_table::PREDEFINED,
4044 os, 32768, 0, elfcpp::STT_OBJECT,
4045 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
4050 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
4059 needs_special_offset_handling,
4064 // Functor class for processing the global symbol table.
4065 // Removes symbols defined on discarded opd entries.
4067 template<bool big_endian>
4068 class Global_symbol_visitor_opd
4071 Global_symbol_visitor_opd()
4075 operator()(Sized_symbol<64>* sym)
4077 if (sym->has_symtab_index()
4078 || sym->source() != Symbol::FROM_OBJECT
4079 || !sym->in_real_elf())
4082 Powerpc_relobj<64, big_endian>* symobj
4083 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
4084 if (symobj->is_dynamic()
4085 || symobj->opd_shndx() == 0)
4089 unsigned int shndx = sym->shndx(&is_ordinary);
4090 if (shndx == symobj->opd_shndx()
4091 && symobj->get_opd_discard(sym->value()))
4092 sym->set_symtab_index(-1U);
4096 template<int size, bool big_endian>
4098 Target_powerpc<size, big_endian>::define_save_restore_funcs(
4100 Symbol_table* symtab)
4104 Output_data_save_res<64, big_endian>* savres
4105 = new Output_data_save_res<64, big_endian>(symtab);
4106 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
4107 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
4108 savres, ORDER_TEXT, false);
4112 // Finalize the sections.
4114 template<int size, bool big_endian>
4116 Target_powerpc<size, big_endian>::do_finalize_sections(
4118 const Input_objects*,
4119 Symbol_table* symtab)
4121 if (parameters->doing_static_link())
4123 // At least some versions of glibc elf-init.o have a strong
4124 // reference to __rela_iplt marker syms. A weak ref would be
4126 if (this->iplt_ != NULL)
4128 Reloc_section* rel = this->iplt_->rel_plt();
4129 symtab->define_in_output_data("__rela_iplt_start", NULL,
4130 Symbol_table::PREDEFINED, rel, 0, 0,
4131 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4132 elfcpp::STV_HIDDEN, 0, false, true);
4133 symtab->define_in_output_data("__rela_iplt_end", NULL,
4134 Symbol_table::PREDEFINED, rel, 0, 0,
4135 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4136 elfcpp::STV_HIDDEN, 0, true, true);
4140 symtab->define_as_constant("__rela_iplt_start", NULL,
4141 Symbol_table::PREDEFINED, 0, 0,
4142 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4143 elfcpp::STV_HIDDEN, 0, true, false);
4144 symtab->define_as_constant("__rela_iplt_end", NULL,
4145 Symbol_table::PREDEFINED, 0, 0,
4146 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4147 elfcpp::STV_HIDDEN, 0, true, false);
4153 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
4154 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
4155 this->define_save_restore_funcs(layout, symtab);
4158 // Fill in some more dynamic tags.
4159 Output_data_dynamic* odyn = layout->dynamic_data();
4162 const Reloc_section* rel_plt = (this->plt_ == NULL
4164 : this->plt_->rel_plt());
4165 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
4166 this->rela_dyn_, true, size == 32);
4170 if (this->got_ != NULL)
4172 this->got_->finalize_data_size();
4173 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
4174 this->got_, this->got_->g_o_t());
4179 if (this->glink_ != NULL)
4181 this->glink_->finalize_data_size();
4182 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
4184 (this->glink_->pltresolve()
4185 + this->glink_->pltresolve_size
4191 // Emit any relocs we saved in an attempt to avoid generating COPY
4193 if (this->copy_relocs_.any_saved_relocs())
4194 this->copy_relocs_.emit(this->rela_dyn_section(layout));
4197 // Return the value to use for a branch relocation.
4199 template<int size, bool big_endian>
4200 typename elfcpp::Elf_types<size>::Elf_Addr
4201 Target_powerpc<size, big_endian>::symval_for_branch(
4203 const Sized_symbol<size>* gsym,
4204 Powerpc_relobj<size, big_endian>* object,
4205 unsigned int *dest_shndx)
4211 // If the symbol is defined in an opd section, ie. is a function
4212 // descriptor, use the function descriptor code entry address
4213 Powerpc_relobj<size, big_endian>* symobj = object;
4215 && gsym->source() != Symbol::FROM_OBJECT)
4218 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
4219 unsigned int shndx = symobj->opd_shndx();
4222 Address opd_addr = symobj->get_output_section_offset(shndx);
4223 gold_assert(opd_addr != invalid_address);
4224 opd_addr += symobj->output_section(shndx)->address();
4225 if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
4228 *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
4229 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
4230 gold_assert(sec_addr != invalid_address);
4231 sec_addr += symobj->output_section(*dest_shndx)->address();
4232 value = sec_addr + sec_off;
4237 // Perform a relocation.
4239 template<int size, bool big_endian>
4241 Target_powerpc<size, big_endian>::Relocate::relocate(
4242 const Relocate_info<size, big_endian>* relinfo,
4243 Target_powerpc* target,
4246 const elfcpp::Rela<size, big_endian>& rela,
4247 unsigned int r_type,
4248 const Sized_symbol<size>* gsym,
4249 const Symbol_value<size>* psymval,
4250 unsigned char* view,
4252 section_size_type view_size)
4255 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
4256 || r_type == elfcpp::R_PPC_PLTREL24)
4258 && strcmp(gsym->name(), "__tls_get_addr") == 0);
4259 enum skip_tls last_tls = this->call_tls_get_addr_;
4260 this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
4263 if (last_tls == CALL_NOT_EXPECTED)
4264 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4265 _("__tls_get_addr call lacks marker reloc"));
4266 else if (last_tls == CALL_SKIP)
4269 else if (last_tls != CALL_NOT_EXPECTED)
4270 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4271 _("missing expected __tls_get_addr call"));
4273 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
4274 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
4275 Powerpc_relobj<size, big_endian>* const object
4276 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
4278 bool has_plt_value = false;
4279 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4281 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
4282 : object->local_has_plt_offset(r_sym))
4284 const Output_data_glink<size, big_endian>* glink
4285 = target->glink_section();
4286 unsigned int glink_index;
4288 glink_index = glink->find_entry(object, gsym, rela);
4290 glink_index = glink->find_entry(object, r_sym, rela);
4291 value = glink->address() + glink_index * glink->glink_entry_size();
4292 has_plt_value = true;
4295 if (r_type == elfcpp::R_POWERPC_GOT16
4296 || r_type == elfcpp::R_POWERPC_GOT16_LO
4297 || r_type == elfcpp::R_POWERPC_GOT16_HI
4298 || r_type == elfcpp::R_POWERPC_GOT16_HA
4299 || r_type == elfcpp::R_PPC64_GOT16_DS
4300 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
4304 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4305 value = gsym->got_offset(GOT_TYPE_STANDARD);
4309 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4310 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4311 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
4313 value -= target->got_section()->got_base_offset(object);
4315 else if (r_type == elfcpp::R_PPC64_TOC)
4317 value = (target->got_section()->output_section()->address()
4318 + object->toc_base_offset());
4320 else if (gsym != NULL
4321 && (r_type == elfcpp::R_POWERPC_REL24
4322 || r_type == elfcpp::R_PPC_PLTREL24)
4327 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4328 Valtype* wv = reinterpret_cast<Valtype*>(view);
4329 bool can_plt_call = false;
4330 if (rela.get_r_offset() + 8 <= view_size)
4332 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
4333 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
4336 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
4338 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
4339 can_plt_call = true;
4344 // If we don't have a branch and link followed by a nop,
4345 // we can't go via the plt because there is no place to
4346 // put a toc restoring instruction.
4347 // Unless we know we won't be returning.
4348 if (strcmp(gsym->name(), "__libc_start_main") == 0)
4349 can_plt_call = true;
4353 // This is not an error in one special case: A self
4354 // call. It isn't possible to cheaply verify we have
4355 // such a call so just check for a call to the same
4358 Address code = value;
4359 if (gsym->source() == Symbol::FROM_OBJECT
4360 && gsym->object() == object)
4362 Address addend = rela.get_r_addend();
4363 unsigned int dest_shndx;
4364 Address opdent = psymval->value(object, addend);
4365 code = target->symval_for_branch(opdent, gsym, object,
4368 if (dest_shndx == 0)
4369 dest_shndx = gsym->shndx(&is_ordinary);
4370 ok = dest_shndx == relinfo->data_shndx;
4374 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4375 _("call lacks nop, can't restore toc; "
4376 "recompile with -fPIC"));
4382 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4383 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
4384 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
4385 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
4387 // First instruction of a global dynamic sequence, arg setup insn.
4388 const bool final = gsym == NULL || gsym->final_value_is_known();
4389 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
4390 enum Got_type got_type = GOT_TYPE_STANDARD;
4391 if (tls_type == tls::TLSOPT_NONE)
4392 got_type = GOT_TYPE_TLSGD;
4393 else if (tls_type == tls::TLSOPT_TO_IE)
4394 got_type = GOT_TYPE_TPREL;
4395 if (got_type != GOT_TYPE_STANDARD)
4399 gold_assert(gsym->has_got_offset(got_type));
4400 value = gsym->got_offset(got_type);
4404 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4405 gold_assert(object->local_has_got_offset(r_sym, got_type));
4406 value = object->local_got_offset(r_sym, got_type);
4408 value -= target->got_section()->got_base_offset(object);
4410 if (tls_type == tls::TLSOPT_TO_IE)
4412 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4413 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4415 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4416 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4417 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
4419 insn |= 32 << 26; // lwz
4421 insn |= 58 << 26; // ld
4422 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4424 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
4425 - elfcpp::R_POWERPC_GOT_TLSGD16);
4427 else if (tls_type == tls::TLSOPT_TO_LE)
4429 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4430 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4432 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4433 Insn insn = addis_3_13;
4436 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4437 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4438 value = psymval->value(object, rela.get_r_addend());
4442 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4444 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4445 r_type = elfcpp::R_POWERPC_NONE;
4449 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4450 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
4451 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
4452 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
4454 // First instruction of a local dynamic sequence, arg setup insn.
4455 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4456 if (tls_type == tls::TLSOPT_NONE)
4458 value = target->tlsld_got_offset();
4459 value -= target->got_section()->got_base_offset(object);
4463 gold_assert(tls_type == tls::TLSOPT_TO_LE);
4464 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4465 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
4467 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4468 Insn insn = addis_3_13;
4471 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4472 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4477 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4479 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4480 r_type = elfcpp::R_POWERPC_NONE;
4484 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
4485 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
4486 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
4487 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
4489 // Accesses relative to a local dynamic sequence address,
4490 // no optimisation here.
4493 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
4494 value = gsym->got_offset(GOT_TYPE_DTPREL);
4498 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4499 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
4500 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
4502 value -= target->got_section()->got_base_offset(object);
4504 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4505 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
4506 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
4507 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
4509 // First instruction of initial exec sequence.
4510 const bool final = gsym == NULL || gsym->final_value_is_known();
4511 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
4512 if (tls_type == tls::TLSOPT_NONE)
4516 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
4517 value = gsym->got_offset(GOT_TYPE_TPREL);
4521 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4522 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
4523 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
4525 value -= target->got_section()->got_base_offset(object);
4529 gold_assert(tls_type == tls::TLSOPT_TO_LE);
4530 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4531 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
4533 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4534 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4535 insn &= (1 << 26) - (1 << 21); // extract rt from ld
4540 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4541 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4542 value = psymval->value(object, rela.get_r_addend());
4546 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4548 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4549 r_type = elfcpp::R_POWERPC_NONE;
4553 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4554 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4556 // Second instruction of a global dynamic sequence,
4557 // the __tls_get_addr call
4558 this->call_tls_get_addr_ = CALL_EXPECTED;
4559 const bool final = gsym == NULL || gsym->final_value_is_known();
4560 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
4561 if (tls_type != tls::TLSOPT_NONE)
4563 if (tls_type == tls::TLSOPT_TO_IE)
4565 Insn* iview = reinterpret_cast<Insn*>(view);
4566 Insn insn = add_3_3_13;
4569 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4570 r_type = elfcpp::R_POWERPC_NONE;
4574 Insn* iview = reinterpret_cast<Insn*>(view);
4575 Insn insn = addi_3_3;
4576 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4577 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4578 view += 2 * big_endian;
4579 value = psymval->value(object, rela.get_r_addend());
4581 this->call_tls_get_addr_ = CALL_SKIP;
4584 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4585 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4587 // Second instruction of a local dynamic sequence,
4588 // the __tls_get_addr call
4589 this->call_tls_get_addr_ = CALL_EXPECTED;
4590 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4591 if (tls_type == tls::TLSOPT_TO_LE)
4593 Insn* iview = reinterpret_cast<Insn*>(view);
4594 Insn insn = addi_3_3;
4595 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4596 this->call_tls_get_addr_ = CALL_SKIP;
4597 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4598 view += 2 * big_endian;
4602 else if (r_type == elfcpp::R_POWERPC_TLS)
4604 // Second instruction of an initial exec sequence
4605 const bool final = gsym == NULL || gsym->final_value_is_known();
4606 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
4607 if (tls_type == tls::TLSOPT_TO_LE)
4609 Insn* iview = reinterpret_cast<Insn*>(view);
4610 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4611 unsigned int reg = size == 32 ? 2 : 13;
4612 insn = at_tls_transform(insn, reg);
4613 gold_assert(insn != 0);
4614 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4615 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4616 view += 2 * big_endian;
4617 value = psymval->value(object, rela.get_r_addend());
4620 else if (!has_plt_value)
4623 unsigned int dest_shndx;
4624 if (r_type != elfcpp::R_PPC_PLTREL24)
4625 addend = rela.get_r_addend();
4626 value = psymval->value(object, addend);
4627 if (size == 64 && is_branch_reloc(r_type))
4628 value = target->symval_for_branch(value, gsym, object, &dest_shndx);
4633 case elfcpp::R_PPC64_REL64:
4634 case elfcpp::R_POWERPC_REL32:
4635 case elfcpp::R_POWERPC_REL24:
4636 case elfcpp::R_PPC_PLTREL24:
4637 case elfcpp::R_PPC_LOCAL24PC:
4638 case elfcpp::R_POWERPC_REL16:
4639 case elfcpp::R_POWERPC_REL16_LO:
4640 case elfcpp::R_POWERPC_REL16_HI:
4641 case elfcpp::R_POWERPC_REL16_HA:
4642 case elfcpp::R_POWERPC_REL14:
4643 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4644 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4648 case elfcpp::R_PPC64_TOC16:
4649 case elfcpp::R_PPC64_TOC16_LO:
4650 case elfcpp::R_PPC64_TOC16_HI:
4651 case elfcpp::R_PPC64_TOC16_HA:
4652 case elfcpp::R_PPC64_TOC16_DS:
4653 case elfcpp::R_PPC64_TOC16_LO_DS:
4654 // Subtract the TOC base address.
4655 value -= (target->got_section()->output_section()->address()
4656 + object->toc_base_offset());
4659 case elfcpp::R_POWERPC_SECTOFF:
4660 case elfcpp::R_POWERPC_SECTOFF_LO:
4661 case elfcpp::R_POWERPC_SECTOFF_HI:
4662 case elfcpp::R_POWERPC_SECTOFF_HA:
4663 case elfcpp::R_PPC64_SECTOFF_DS:
4664 case elfcpp::R_PPC64_SECTOFF_LO_DS:
4666 value -= os->address();
4669 case elfcpp::R_PPC64_TPREL16_DS:
4670 case elfcpp::R_PPC64_TPREL16_LO_DS:
4672 // R_PPC_TLSGD and R_PPC_TLSLD
4674 case elfcpp::R_POWERPC_TPREL16:
4675 case elfcpp::R_POWERPC_TPREL16_LO:
4676 case elfcpp::R_POWERPC_TPREL16_HI:
4677 case elfcpp::R_POWERPC_TPREL16_HA:
4678 case elfcpp::R_POWERPC_TPREL:
4679 case elfcpp::R_PPC64_TPREL16_HIGHER:
4680 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4681 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4682 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4683 // tls symbol values are relative to tls_segment()->vaddr()
4687 case elfcpp::R_PPC64_DTPREL16_DS:
4688 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4689 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4690 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4691 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4692 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4694 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
4695 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
4697 case elfcpp::R_POWERPC_DTPREL16:
4698 case elfcpp::R_POWERPC_DTPREL16_LO:
4699 case elfcpp::R_POWERPC_DTPREL16_HI:
4700 case elfcpp::R_POWERPC_DTPREL16_HA:
4701 case elfcpp::R_POWERPC_DTPREL:
4702 // tls symbol values are relative to tls_segment()->vaddr()
4703 value -= dtp_offset;
4710 Insn branch_bit = 0;
4713 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4714 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4715 branch_bit = 1 << 21;
4716 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4717 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4719 Insn* iview = reinterpret_cast<Insn*>(view);
4720 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4723 if (this->is_isa_v2)
4725 // Set 'a' bit. This is 0b00010 in BO field for branch
4726 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
4727 // for branch on CTR insns (BO == 1a00t or 1a01t).
4728 if ((insn & (0x14 << 21)) == (0x04 << 21))
4730 else if ((insn & (0x14 << 21)) == (0x10 << 21))
4737 // Invert 'y' bit if not the default.
4738 if (static_cast<Signed_address>(value) < 0)
4741 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4749 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
4752 case elfcpp::R_POWERPC_ADDR32:
4753 case elfcpp::R_POWERPC_UADDR32:
4755 overflow = Reloc::CHECK_BITFIELD;
4758 case elfcpp::R_POWERPC_REL32:
4760 overflow = Reloc::CHECK_SIGNED;
4763 case elfcpp::R_POWERPC_ADDR24:
4764 case elfcpp::R_POWERPC_ADDR16:
4765 case elfcpp::R_POWERPC_UADDR16:
4766 case elfcpp::R_PPC64_ADDR16_DS:
4767 case elfcpp::R_POWERPC_ADDR14:
4768 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4769 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4770 overflow = Reloc::CHECK_BITFIELD;
4773 case elfcpp::R_POWERPC_REL24:
4774 case elfcpp::R_PPC_PLTREL24:
4775 case elfcpp::R_PPC_LOCAL24PC:
4776 case elfcpp::R_POWERPC_REL16:
4777 case elfcpp::R_PPC64_TOC16:
4778 case elfcpp::R_POWERPC_GOT16:
4779 case elfcpp::R_POWERPC_SECTOFF:
4780 case elfcpp::R_POWERPC_TPREL16:
4781 case elfcpp::R_POWERPC_DTPREL16:
4782 case elfcpp::R_PPC64_TPREL16_DS:
4783 case elfcpp::R_PPC64_DTPREL16_DS:
4784 case elfcpp::R_PPC64_TOC16_DS:
4785 case elfcpp::R_PPC64_GOT16_DS:
4786 case elfcpp::R_PPC64_SECTOFF_DS:
4787 case elfcpp::R_POWERPC_REL14:
4788 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4789 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4790 case elfcpp::R_POWERPC_GOT_TLSGD16:
4791 case elfcpp::R_POWERPC_GOT_TLSLD16:
4792 case elfcpp::R_POWERPC_GOT_TPREL16:
4793 case elfcpp::R_POWERPC_GOT_DTPREL16:
4794 overflow = Reloc::CHECK_SIGNED;
4798 typename Powerpc_relocate_functions<size, big_endian>::Status status
4799 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
4802 case elfcpp::R_POWERPC_NONE:
4803 case elfcpp::R_POWERPC_TLS:
4804 case elfcpp::R_POWERPC_GNU_VTINHERIT:
4805 case elfcpp::R_POWERPC_GNU_VTENTRY:
4806 case elfcpp::R_PPC_EMB_MRKREF:
4809 case elfcpp::R_PPC64_ADDR64:
4810 case elfcpp::R_PPC64_REL64:
4811 case elfcpp::R_PPC64_TOC:
4812 Reloc::addr64(view, value);
4815 case elfcpp::R_POWERPC_TPREL:
4816 case elfcpp::R_POWERPC_DTPREL:
4818 Reloc::addr64(view, value);
4820 status = Reloc::addr32(view, value, overflow);
4823 case elfcpp::R_PPC64_UADDR64:
4824 Reloc::addr64_u(view, value);
4827 case elfcpp::R_POWERPC_ADDR32:
4828 status = Reloc::addr32(view, value, overflow);
4831 case elfcpp::R_POWERPC_REL32:
4832 case elfcpp::R_POWERPC_UADDR32:
4833 status = Reloc::addr32_u(view, value, overflow);
4836 case elfcpp::R_POWERPC_ADDR24:
4837 case elfcpp::R_POWERPC_REL24:
4838 case elfcpp::R_PPC_PLTREL24:
4839 case elfcpp::R_PPC_LOCAL24PC:
4840 status = Reloc::addr24(view, value, overflow);
4843 case elfcpp::R_POWERPC_GOT_DTPREL16:
4844 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
4847 status = Reloc::addr16_ds(view, value, overflow);
4850 case elfcpp::R_POWERPC_ADDR16:
4851 case elfcpp::R_POWERPC_REL16:
4852 case elfcpp::R_PPC64_TOC16:
4853 case elfcpp::R_POWERPC_GOT16:
4854 case elfcpp::R_POWERPC_SECTOFF:
4855 case elfcpp::R_POWERPC_TPREL16:
4856 case elfcpp::R_POWERPC_DTPREL16:
4857 case elfcpp::R_POWERPC_GOT_TLSGD16:
4858 case elfcpp::R_POWERPC_GOT_TLSLD16:
4859 case elfcpp::R_POWERPC_GOT_TPREL16:
4860 case elfcpp::R_POWERPC_ADDR16_LO:
4861 case elfcpp::R_POWERPC_REL16_LO:
4862 case elfcpp::R_PPC64_TOC16_LO:
4863 case elfcpp::R_POWERPC_GOT16_LO:
4864 case elfcpp::R_POWERPC_SECTOFF_LO:
4865 case elfcpp::R_POWERPC_TPREL16_LO:
4866 case elfcpp::R_POWERPC_DTPREL16_LO:
4867 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
4868 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
4869 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
4870 status = Reloc::addr16(view, value, overflow);
4873 case elfcpp::R_POWERPC_UADDR16:
4874 status = Reloc::addr16_u(view, value, overflow);
4877 case elfcpp::R_POWERPC_ADDR16_HI:
4878 case elfcpp::R_POWERPC_REL16_HI:
4879 case elfcpp::R_PPC64_TOC16_HI:
4880 case elfcpp::R_POWERPC_GOT16_HI:
4881 case elfcpp::R_POWERPC_SECTOFF_HI:
4882 case elfcpp::R_POWERPC_TPREL16_HI:
4883 case elfcpp::R_POWERPC_DTPREL16_HI:
4884 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
4885 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
4886 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
4887 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
4888 Reloc::addr16_hi(view, value);
4891 case elfcpp::R_POWERPC_ADDR16_HA:
4892 case elfcpp::R_POWERPC_REL16_HA:
4893 case elfcpp::R_PPC64_TOC16_HA:
4894 case elfcpp::R_POWERPC_GOT16_HA:
4895 case elfcpp::R_POWERPC_SECTOFF_HA:
4896 case elfcpp::R_POWERPC_TPREL16_HA:
4897 case elfcpp::R_POWERPC_DTPREL16_HA:
4898 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
4899 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
4900 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
4901 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
4902 Reloc::addr16_ha(view, value);
4905 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4907 // R_PPC_EMB_NADDR16_LO
4909 case elfcpp::R_PPC64_ADDR16_HIGHER:
4910 case elfcpp::R_PPC64_TPREL16_HIGHER:
4911 Reloc::addr16_hi2(view, value);
4914 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4916 // R_PPC_EMB_NADDR16_HI
4918 case elfcpp::R_PPC64_ADDR16_HIGHERA:
4919 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4920 Reloc::addr16_ha2(view, value);
4923 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4925 // R_PPC_EMB_NADDR16_HA
4927 case elfcpp::R_PPC64_ADDR16_HIGHEST:
4928 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4929 Reloc::addr16_hi3(view, value);
4932 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4936 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4937 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4938 Reloc::addr16_ha3(view, value);
4941 case elfcpp::R_PPC64_DTPREL16_DS:
4942 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4944 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
4946 case elfcpp::R_PPC64_TPREL16_DS:
4947 case elfcpp::R_PPC64_TPREL16_LO_DS:
4949 // R_PPC_TLSGD, R_PPC_TLSLD
4951 case elfcpp::R_PPC64_ADDR16_DS:
4952 case elfcpp::R_PPC64_ADDR16_LO_DS:
4953 case elfcpp::R_PPC64_TOC16_DS:
4954 case elfcpp::R_PPC64_TOC16_LO_DS:
4955 case elfcpp::R_PPC64_GOT16_DS:
4956 case elfcpp::R_PPC64_GOT16_LO_DS:
4957 case elfcpp::R_PPC64_SECTOFF_DS:
4958 case elfcpp::R_PPC64_SECTOFF_LO_DS:
4959 status = Reloc::addr16_ds(view, value, overflow);
4962 case elfcpp::R_POWERPC_ADDR14:
4963 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4964 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4965 case elfcpp::R_POWERPC_REL14:
4966 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4967 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4968 status = Reloc::addr14(view, value, overflow);
4971 case elfcpp::R_POWERPC_COPY:
4972 case elfcpp::R_POWERPC_GLOB_DAT:
4973 case elfcpp::R_POWERPC_JMP_SLOT:
4974 case elfcpp::R_POWERPC_RELATIVE:
4975 case elfcpp::R_POWERPC_DTPMOD:
4976 case elfcpp::R_PPC64_JMP_IREL:
4977 case elfcpp::R_POWERPC_IRELATIVE:
4978 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4979 _("unexpected reloc %u in object file"),
4983 case elfcpp::R_PPC_EMB_SDA21:
4988 // R_PPC64_TOCSAVE. For the time being this can be ignored.
4992 case elfcpp::R_PPC_EMB_SDA2I16:
4993 case elfcpp::R_PPC_EMB_SDA2REL:
4996 // R_PPC64_TLSGD, R_PPC64_TLSLD
4999 case elfcpp::R_POWERPC_PLT32:
5000 case elfcpp::R_POWERPC_PLTREL32:
5001 case elfcpp::R_POWERPC_PLT16_LO:
5002 case elfcpp::R_POWERPC_PLT16_HI:
5003 case elfcpp::R_POWERPC_PLT16_HA:
5004 case elfcpp::R_PPC_SDAREL16:
5005 case elfcpp::R_POWERPC_ADDR30:
5006 case elfcpp::R_PPC64_PLT64:
5007 case elfcpp::R_PPC64_PLTREL64:
5008 case elfcpp::R_PPC64_PLTGOT16:
5009 case elfcpp::R_PPC64_PLTGOT16_LO:
5010 case elfcpp::R_PPC64_PLTGOT16_HI:
5011 case elfcpp::R_PPC64_PLTGOT16_HA:
5012 case elfcpp::R_PPC64_PLT16_LO_DS:
5013 case elfcpp::R_PPC64_PLTGOT16_DS:
5014 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
5015 case elfcpp::R_PPC_EMB_RELSEC16:
5016 case elfcpp::R_PPC_EMB_RELST_LO:
5017 case elfcpp::R_PPC_EMB_RELST_HI:
5018 case elfcpp::R_PPC_EMB_RELST_HA:
5019 case elfcpp::R_PPC_EMB_BIT_FLD:
5020 case elfcpp::R_PPC_EMB_RELSDA:
5021 case elfcpp::R_PPC_TOC16:
5024 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5025 _("unsupported reloc %u"),
5029 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
5030 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5031 _("relocation overflow"));
5036 // Relocate section data.
5038 template<int size, bool big_endian>
5040 Target_powerpc<size, big_endian>::relocate_section(
5041 const Relocate_info<size, big_endian>* relinfo,
5042 unsigned int sh_type,
5043 const unsigned char* prelocs,
5045 Output_section* output_section,
5046 bool needs_special_offset_handling,
5047 unsigned char* view,
5049 section_size_type view_size,
5050 const Reloc_symbol_changes* reloc_symbol_changes)
5052 typedef Target_powerpc<size, big_endian> Powerpc;
5053 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
5055 gold_assert(sh_type == elfcpp::SHT_RELA);
5057 unsigned char *opd_rel = NULL;
5058 Powerpc_relobj<size, big_endian>* const object
5059 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
5061 && relinfo->data_shndx == object->opd_shndx())
5063 // Rewrite opd relocs, omitting those for discarded sections
5064 // to silence gold::relocate_section errors.
5065 const int reloc_size
5066 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
5067 opd_rel = new unsigned char[reloc_count * reloc_size];
5068 const unsigned char* rrel = prelocs;
5069 unsigned char* wrel = opd_rel;
5073 ++i, rrel += reloc_size, wrel += reloc_size)
5075 typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
5077 typename elfcpp::Elf_types<size>::Elf_WXword r_info
5078 = reloc.get_r_info();
5079 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
5080 Address r_off = reloc.get_r_offset();
5081 if (r_type == elfcpp::R_PPC64_TOC)
5083 bool is_discarded = object->get_opd_discard(r_off);
5085 // Reloc number is reported in some errors, so keep all relocs.
5087 memset(wrel, 0, reloc_size);
5089 memcpy(wrel, rrel, reloc_size);
5094 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
5101 needs_special_offset_handling,
5105 reloc_symbol_changes);
5107 if (opd_rel != NULL)
5111 class Powerpc_scan_relocatable_reloc
5114 // Return the strategy to use for a local symbol which is not a
5115 // section symbol, given the relocation type.
5116 inline Relocatable_relocs::Reloc_strategy
5117 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
5119 if (r_type == 0 && r_sym == 0)
5120 return Relocatable_relocs::RELOC_DISCARD;
5121 return Relocatable_relocs::RELOC_COPY;
5124 // Return the strategy to use for a local symbol which is a section
5125 // symbol, given the relocation type.
5126 inline Relocatable_relocs::Reloc_strategy
5127 local_section_strategy(unsigned int, Relobj*)
5129 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
5132 // Return the strategy to use for a global symbol, given the
5133 // relocation type, the object, and the symbol index.
5134 inline Relocatable_relocs::Reloc_strategy
5135 global_strategy(unsigned int r_type, Relobj*, unsigned int)
5137 if (r_type == elfcpp::R_PPC_PLTREL24)
5138 return Relocatable_relocs::RELOC_SPECIAL;
5139 return Relocatable_relocs::RELOC_COPY;
5143 // Scan the relocs during a relocatable link.
5145 template<int size, bool big_endian>
5147 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
5148 Symbol_table* symtab,
5150 Sized_relobj_file<size, big_endian>* object,
5151 unsigned int data_shndx,
5152 unsigned int sh_type,
5153 const unsigned char* prelocs,
5155 Output_section* output_section,
5156 bool needs_special_offset_handling,
5157 size_t local_symbol_count,
5158 const unsigned char* plocal_symbols,
5159 Relocatable_relocs* rr)
5161 gold_assert(sh_type == elfcpp::SHT_RELA);
5163 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
5164 Powerpc_scan_relocatable_reloc>(
5172 needs_special_offset_handling,
5178 // Emit relocations for a section.
5179 // This is a modified version of the function by the same name in
5180 // target-reloc.h. Using relocate_special_relocatable for
5181 // R_PPC_PLTREL24 would require duplication of the entire body of the
5182 // loop, so we may as well duplicate the whole thing.
5184 template<int size, bool big_endian>
5186 Target_powerpc<size, big_endian>::relocate_relocs(
5187 const Relocate_info<size, big_endian>* relinfo,
5188 unsigned int sh_type,
5189 const unsigned char* prelocs,
5191 Output_section* output_section,
5192 off_t offset_in_output_section,
5193 const Relocatable_relocs* rr,
5195 Address view_address,
5197 unsigned char* reloc_view,
5198 section_size_type reloc_view_size)
5200 gold_assert(sh_type == elfcpp::SHT_RELA);
5202 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
5204 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
5206 const int reloc_size
5207 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
5209 Powerpc_relobj<size, big_endian>* const object
5210 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
5211 const unsigned int local_count = object->local_symbol_count();
5212 unsigned int got2_shndx = object->got2_shndx();
5213 Address got2_addend = 0;
5214 if (got2_shndx != 0)
5216 got2_addend = object->get_output_section_offset(got2_shndx);
5217 gold_assert(got2_addend != invalid_address);
5220 unsigned char* pwrite = reloc_view;
5221 bool zap_next = false;
5222 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
5224 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
5225 if (strategy == Relocatable_relocs::RELOC_DISCARD)
5228 Reltype reloc(prelocs);
5229 Reltype_write reloc_write(pwrite);
5231 Address offset = reloc.get_r_offset();
5232 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
5233 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
5234 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
5235 const unsigned int orig_r_sym = r_sym;
5236 typename elfcpp::Elf_types<size>::Elf_Swxword addend
5237 = reloc.get_r_addend();
5238 const Symbol* gsym = NULL;
5242 // We could arrange to discard these and other relocs for
5243 // tls optimised sequences in the strategy methods, but for
5244 // now do as BFD ld does.
5245 r_type = elfcpp::R_POWERPC_NONE;
5249 // Get the new symbol index.
5250 if (r_sym < local_count)
5254 case Relocatable_relocs::RELOC_COPY:
5255 case Relocatable_relocs::RELOC_SPECIAL:
5258 r_sym = object->symtab_index(r_sym);
5259 gold_assert(r_sym != -1U);
5263 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
5265 // We are adjusting a section symbol. We need to find
5266 // the symbol table index of the section symbol for
5267 // the output section corresponding to input section
5268 // in which this symbol is defined.
5269 gold_assert(r_sym < local_count);
5271 unsigned int shndx =
5272 object->local_symbol_input_shndx(r_sym, &is_ordinary);
5273 gold_assert(is_ordinary);
5274 Output_section* os = object->output_section(shndx);
5275 gold_assert(os != NULL);
5276 gold_assert(os->needs_symtab_index());
5277 r_sym = os->symtab_index();
5287 gsym = object->global_symbol(r_sym);
5288 gold_assert(gsym != NULL);
5289 if (gsym->is_forwarder())
5290 gsym = relinfo->symtab->resolve_forwards(gsym);
5292 gold_assert(gsym->has_symtab_index());
5293 r_sym = gsym->symtab_index();
5296 // Get the new offset--the location in the output section where
5297 // this relocation should be applied.
5298 if (static_cast<Address>(offset_in_output_section) != invalid_address)
5299 offset += offset_in_output_section;
5302 section_offset_type sot_offset =
5303 convert_types<section_offset_type, Address>(offset);
5304 section_offset_type new_sot_offset =
5305 output_section->output_offset(object, relinfo->data_shndx,
5307 gold_assert(new_sot_offset != -1);
5308 offset = new_sot_offset;
5311 // In an object file, r_offset is an offset within the section.
5312 // In an executable or dynamic object, generated by
5313 // --emit-relocs, r_offset is an absolute address.
5314 if (!parameters->options().relocatable())
5316 offset += view_address;
5317 if (static_cast<Address>(offset_in_output_section) != invalid_address)
5318 offset -= offset_in_output_section;
5321 // Handle the reloc addend based on the strategy.
5322 if (strategy == Relocatable_relocs::RELOC_COPY)
5324 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
5326 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
5327 addend = psymval->value(object, addend);
5329 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
5331 if (addend >= 32768)
5332 addend += got2_addend;
5337 if (!parameters->options().relocatable())
5339 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5340 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
5341 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
5342 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
5344 // First instruction of a global dynamic sequence,
5346 const bool final = gsym == NULL || gsym->final_value_is_known();
5347 switch (this->optimize_tls_gd(final))
5349 case tls::TLSOPT_TO_IE:
5350 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
5351 - elfcpp::R_POWERPC_GOT_TLSGD16);
5353 case tls::TLSOPT_TO_LE:
5354 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5355 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
5356 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5359 r_type = elfcpp::R_POWERPC_NONE;
5360 offset -= 2 * big_endian;
5367 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5368 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
5369 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
5370 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
5372 // First instruction of a local dynamic sequence,
5374 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
5376 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5377 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
5379 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5380 const Output_section* os = relinfo->layout->tls_segment()
5382 gold_assert(os != NULL);
5383 gold_assert(os->needs_symtab_index());
5384 r_sym = os->symtab_index();
5385 addend = dtp_offset;
5389 r_type = elfcpp::R_POWERPC_NONE;
5390 offset -= 2 * big_endian;
5394 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5395 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
5396 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
5397 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
5399 // First instruction of initial exec sequence.
5400 const bool final = gsym == NULL || gsym->final_value_is_known();
5401 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
5403 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5404 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
5405 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5408 r_type = elfcpp::R_POWERPC_NONE;
5409 offset -= 2 * big_endian;
5413 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5414 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5416 // Second instruction of a global dynamic sequence,
5417 // the __tls_get_addr call
5418 const bool final = gsym == NULL || gsym->final_value_is_known();
5419 switch (this->optimize_tls_gd(final))
5421 case tls::TLSOPT_TO_IE:
5422 r_type = elfcpp::R_POWERPC_NONE;
5425 case tls::TLSOPT_TO_LE:
5426 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5427 offset += 2 * big_endian;
5434 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5435 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5437 // Second instruction of a local dynamic sequence,
5438 // the __tls_get_addr call
5439 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
5441 const Output_section* os = relinfo->layout->tls_segment()
5443 gold_assert(os != NULL);
5444 gold_assert(os->needs_symtab_index());
5445 r_sym = os->symtab_index();
5446 addend = dtp_offset;
5447 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5448 offset += 2 * big_endian;
5452 else if (r_type == elfcpp::R_POWERPC_TLS)
5454 // Second instruction of an initial exec sequence
5455 const bool final = gsym == NULL || gsym->final_value_is_known();
5456 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
5458 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5459 offset += 2 * big_endian;
5464 reloc_write.put_r_offset(offset);
5465 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
5466 reloc_write.put_r_addend(addend);
5468 pwrite += reloc_size;
5471 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
5472 == reloc_view_size);
5475 // Return the value to use for a dynamic which requires special
5476 // treatment. This is how we support equality comparisons of function
5477 // pointers across shared library boundaries, as described in the
5478 // processor specific ABI supplement.
5480 template<int size, bool big_endian>
5482 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
5486 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5487 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5488 unsigned int glink_index = glink->find_entry(gsym);
5489 return glink->address() + glink_index * glink->glink_entry_size();
5495 // Return the PLT address to use for a local symbol.
5496 template<int size, bool big_endian>
5498 Target_powerpc<size, big_endian>::do_plt_address_for_local(
5499 const Relobj* object,
5500 unsigned int symndx) const
5504 const Sized_relobj<size, big_endian>* relobj
5505 = static_cast<const Sized_relobj<size, big_endian>*>(object);
5506 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5507 unsigned int glink_index = glink->find_entry(relobj->sized_relobj(),
5509 return glink->address() + glink_index * glink->glink_entry_size();
5515 // Return the PLT address to use for a global symbol.
5516 template<int size, bool big_endian>
5518 Target_powerpc<size, big_endian>::do_plt_address_for_global(
5519 const Symbol* gsym) const
5523 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5524 unsigned int glink_index = glink->find_entry(gsym);
5525 return glink->address() + glink_index * glink->glink_entry_size();
5531 // Return the offset to use for the GOT_INDX'th got entry which is
5532 // for a local tls symbol specified by OBJECT, SYMNDX.
5533 template<int size, bool big_endian>
5535 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
5536 const Relobj* object,
5537 unsigned int symndx,
5538 unsigned int got_indx) const
5540 const Powerpc_relobj<size, big_endian>* ppc_object
5541 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
5542 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
5544 for (Got_type got_type = GOT_TYPE_TLSGD;
5545 got_type <= GOT_TYPE_TPREL;
5546 got_type = Got_type(got_type + 1))
5547 if (ppc_object->local_has_got_offset(symndx, got_type))
5549 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
5550 if (got_type == GOT_TYPE_TLSGD)
5552 if (off == got_indx * (size / 8))
5554 if (got_type == GOT_TYPE_TPREL)
5564 // Return the offset to use for the GOT_INDX'th got entry which is
5565 // for global tls symbol GSYM.
5566 template<int size, bool big_endian>
5568 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
5570 unsigned int got_indx) const
5572 if (gsym->type() == elfcpp::STT_TLS)
5574 for (Got_type got_type = GOT_TYPE_TLSGD;
5575 got_type <= GOT_TYPE_TPREL;
5576 got_type = Got_type(got_type + 1))
5577 if (gsym->has_got_offset(got_type))
5579 unsigned int off = gsym->got_offset(got_type);
5580 if (got_type == GOT_TYPE_TLSGD)
5582 if (off == got_indx * (size / 8))
5584 if (got_type == GOT_TYPE_TPREL)
5594 // The selector for powerpc object files.
5596 template<int size, bool big_endian>
5597 class Target_selector_powerpc : public Target_selector
5600 Target_selector_powerpc()
5601 : Target_selector(elfcpp::EM_NONE, size, big_endian,
5603 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
5604 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
5606 ? (big_endian ? "elf64ppc" : "elf64lppc")
5607 : (big_endian ? "elf32ppc" : "elf32lppc")))
5611 do_recognize(Input_file*, off_t, int machine, int, int)
5616 if (machine != elfcpp::EM_PPC64)
5621 if (machine != elfcpp::EM_PPC)
5629 return this->instantiate_target();
5633 do_instantiate_target()
5634 { return new Target_powerpc<size, big_endian>(); }
5637 Target_selector_powerpc<32, true> target_selector_ppc32;
5638 Target_selector_powerpc<32, false> target_selector_ppc32le;
5639 Target_selector_powerpc<64, true> target_selector_ppc64;
5640 Target_selector_powerpc<64, false> target_selector_ppc64le;
5642 } // End anonymous namespace.