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);
159 { return this->opd_valid_; }
163 { this->opd_valid_ = true; }
165 // Examine .rela.opd to build info about function entry points.
167 scan_opd_relocs(size_t reloc_count,
168 const unsigned char* prelocs,
169 const unsigned char* plocal_syms);
172 do_read_relocs(Read_relocs_data*);
175 do_find_special_sections(Read_symbols_data* sd);
177 // Adjust this local symbol value. Return false if the symbol
178 // should be discarded from the output file.
180 do_adjust_local_symbol(Symbol_value<size>* lv) const
182 if (size == 64 && this->opd_shndx() != 0)
185 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
187 if (this->get_opd_discard(lv->input_value()))
193 // Return offset in output GOT section that this object will use
194 // as a TOC pointer. Won't be just a constant with multi-toc support.
196 toc_base_offset() const
207 // Return index into opd_ent_ array for .opd entry at OFF.
208 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
209 // apart when the language doesn't use the last 8-byte word, the
210 // environment pointer. Thus dividing the entry section offset by
211 // 16 will give an index into opd_ent_ that works for either layout
212 // of .opd. (It leaves some elements of the vector unused when .opd
213 // entries are spaced 24 bytes apart, but we don't know the spacing
214 // until relocations are processed, and in any case it is possible
215 // for an object to have some entries spaced 16 bytes apart and
216 // others 24 bytes apart.)
218 opd_ent_ndx(size_t off) const
221 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
222 unsigned int special_;
224 // Set at the start of gc_process_relocs, when we know opd_ent_
225 // vector is valid. The flag could be made atomic and set in
226 // do_read_relocs with memory_order_release and then tested with
227 // memory_order_acquire, potentially resulting in fewer entries in
231 // The first 8-byte word of an OPD entry gives the address of the
232 // entry point of the function. Relocatable object files have a
233 // relocation on this word. The following vector records the
234 // section and offset specified by these relocations.
235 std::vector<Opd_ent> opd_ent_;
237 // References made to this object's .opd section when running
238 // gc_process_relocs for another object, before the opd_ent_ vector
239 // is valid for this object.
240 Access_from access_from_map_;
243 template<int size, bool big_endian>
244 class Target_powerpc : public Sized_target<size, big_endian>
248 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
249 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
250 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
251 static const Address invalid_address = static_cast<Address>(0) - 1;
252 // Offset of tp and dtp pointers from start of TLS block.
253 static const Address tp_offset = 0x7000;
254 static const Address dtp_offset = 0x8000;
257 : Sized_target<size, big_endian>(&powerpc_info),
258 got_(NULL), plt_(NULL), iplt_(NULL), glink_(NULL), rela_dyn_(NULL),
259 copy_relocs_(elfcpp::R_POWERPC_COPY),
260 dynbss_(NULL), tlsld_got_offset_(-1U)
264 // Process the relocations to determine unreferenced sections for
265 // garbage collection.
267 gc_process_relocs(Symbol_table* symtab,
269 Sized_relobj_file<size, big_endian>* object,
270 unsigned int data_shndx,
271 unsigned int sh_type,
272 const unsigned char* prelocs,
274 Output_section* output_section,
275 bool needs_special_offset_handling,
276 size_t local_symbol_count,
277 const unsigned char* plocal_symbols);
279 // Scan the relocations to look for symbol adjustments.
281 scan_relocs(Symbol_table* symtab,
283 Sized_relobj_file<size, big_endian>* object,
284 unsigned int data_shndx,
285 unsigned int sh_type,
286 const unsigned char* prelocs,
288 Output_section* output_section,
289 bool needs_special_offset_handling,
290 size_t local_symbol_count,
291 const unsigned char* plocal_symbols);
293 // Map input .toc section to output .got section.
295 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
297 if (size == 64 && strcmp(name, ".toc") == 0)
305 // Finalize the sections.
307 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
309 // Return the value to use for a dynamic which requires special
312 do_dynsym_value(const Symbol*) const;
314 // Return the PLT address to use for a local symbol.
316 do_plt_address_for_local(const Relobj*, unsigned int) const;
318 // Return the PLT address to use for a global symbol.
320 do_plt_address_for_global(const Symbol*) const;
322 // Return the offset to use for the GOT_INDX'th got entry which is
323 // for a local tls symbol specified by OBJECT, SYMNDX.
325 do_tls_offset_for_local(const Relobj* object,
327 unsigned int got_indx) const;
329 // Return the offset to use for the GOT_INDX'th got entry which is
330 // for global tls symbol GSYM.
332 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
334 // Relocate a section.
336 relocate_section(const Relocate_info<size, big_endian>*,
337 unsigned int sh_type,
338 const unsigned char* prelocs,
340 Output_section* output_section,
341 bool needs_special_offset_handling,
343 Address view_address,
344 section_size_type view_size,
345 const Reloc_symbol_changes*);
347 // Scan the relocs during a relocatable link.
349 scan_relocatable_relocs(Symbol_table* symtab,
351 Sized_relobj_file<size, big_endian>* object,
352 unsigned int data_shndx,
353 unsigned int sh_type,
354 const unsigned char* prelocs,
356 Output_section* output_section,
357 bool needs_special_offset_handling,
358 size_t local_symbol_count,
359 const unsigned char* plocal_symbols,
360 Relocatable_relocs*);
362 // Emit relocations for a section.
364 relocate_relocs(const Relocate_info<size, big_endian>*,
365 unsigned int sh_type,
366 const unsigned char* prelocs,
368 Output_section* output_section,
369 off_t offset_in_output_section,
370 const Relocatable_relocs*,
372 Address view_address,
374 unsigned char* reloc_view,
375 section_size_type reloc_view_size);
377 // Return whether SYM is defined by the ABI.
379 do_is_defined_by_abi(const Symbol* sym) const
381 return strcmp(sym->name(), "__tls_get_addr") == 0;
384 // Return the size of the GOT section.
388 gold_assert(this->got_ != NULL);
389 return this->got_->data_size();
392 // Get the PLT section.
393 const Output_data_plt_powerpc<size, big_endian>*
396 gold_assert(this->plt_ != NULL);
400 // Get the IPLT section.
401 const Output_data_plt_powerpc<size, big_endian>*
404 gold_assert(this->iplt_ != NULL);
408 // Get the .glink section.
409 const Output_data_glink<size, big_endian>*
410 glink_section() const
412 gold_assert(this->glink_ != NULL);
416 // Get the GOT section.
417 const Output_data_got_powerpc<size, big_endian>*
420 gold_assert(this->got_ != NULL);
425 do_make_elf_object(const std::string&, Input_file*, off_t,
426 const elfcpp::Ehdr<size, big_endian>&);
428 // Return the number of entries in the GOT.
430 got_entry_count() const
432 if (this->got_ == NULL)
434 return this->got_size() / (size / 8);
437 // Return the number of entries in the PLT.
439 plt_entry_count() const;
441 // Return the offset of the first non-reserved PLT entry.
443 first_plt_entry_offset() const;
445 // Return the size of each PLT entry.
447 plt_entry_size() const;
449 // Add any special sections for this symbol to the gc work list.
450 // For powerpc64, this adds the code section of a function
453 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
455 // Handle target specific gc actions when adding a gc reference from
456 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
457 // and DST_OFF. For powerpc64, this adds a referenc to the code
458 // section of a function descriptor.
460 do_gc_add_reference(Symbol_table* symtab,
462 unsigned int src_shndx,
464 unsigned int dst_shndx,
465 Address dst_off) const;
469 // The class which scans relocations.
473 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
476 : issued_non_pic_error_(false)
480 get_reference_flags(unsigned int r_type);
483 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
484 Sized_relobj_file<size, big_endian>* object,
485 unsigned int data_shndx,
486 Output_section* output_section,
487 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
488 const elfcpp::Sym<size, big_endian>& lsym,
492 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
493 Sized_relobj_file<size, big_endian>* object,
494 unsigned int data_shndx,
495 Output_section* output_section,
496 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
500 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
502 Sized_relobj_file<size, big_endian>* ,
505 const elfcpp::Rela<size, big_endian>& ,
507 const elfcpp::Sym<size, big_endian>&)
511 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
513 Sized_relobj_file<size, big_endian>* ,
516 const elfcpp::Rela<size,
518 unsigned int , Symbol*)
523 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
524 unsigned int r_type);
527 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
528 unsigned int r_type, Symbol*);
531 generate_tls_call(Symbol_table* symtab, Layout* layout,
532 Target_powerpc* target);
535 check_non_pic(Relobj*, unsigned int r_type);
538 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>* object,
539 unsigned int r_type);
541 // Whether we have issued an error about a non-PIC compilation.
542 bool issued_non_pic_error_;
546 symval_for_branch(Address value, const Sized_symbol<size>* gsym,
547 Powerpc_relobj<size, big_endian>* object,
548 unsigned int *dest_shndx);
550 // The class which implements relocation.
554 // Use 'at' branch hints when true, 'y' when false.
555 // FIXME maybe: set this with an option.
556 static const bool is_isa_v2 = true;
560 CALL_NOT_EXPECTED = 0,
566 : call_tls_get_addr_(CALL_NOT_EXPECTED)
571 if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
573 // FIXME: This needs to specify the location somehow.
574 gold_error(_("missing expected __tls_get_addr call"));
578 // Do a relocation. Return false if the caller should not issue
579 // any warnings about this relocation.
581 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
582 Output_section*, size_t relnum,
583 const elfcpp::Rela<size, big_endian>&,
584 unsigned int r_type, const Sized_symbol<size>*,
585 const Symbol_value<size>*,
587 typename elfcpp::Elf_types<size>::Elf_Addr,
590 // This is set if we should skip the next reloc, which should be a
591 // call to __tls_get_addr.
592 enum skip_tls call_tls_get_addr_;
595 // A class which returns the size required for a relocation type,
596 // used while scanning relocs during a relocatable link.
597 class Relocatable_size_for_reloc
601 get_size_for_reloc(unsigned int, Relobj*)
608 // Optimize the TLS relocation type based on what we know about the
609 // symbol. IS_FINAL is true if the final address of this symbol is
610 // known at link time.
612 tls::Tls_optimization
613 optimize_tls_gd(bool is_final)
615 // If we are generating a shared library, then we can't do anything
617 if (parameters->options().shared())
618 return tls::TLSOPT_NONE;
621 return tls::TLSOPT_TO_IE;
622 return tls::TLSOPT_TO_LE;
625 tls::Tls_optimization
628 if (parameters->options().shared())
629 return tls::TLSOPT_NONE;
631 return tls::TLSOPT_TO_LE;
634 tls::Tls_optimization
635 optimize_tls_ie(bool is_final)
637 if (!is_final || parameters->options().shared())
638 return tls::TLSOPT_NONE;
640 return tls::TLSOPT_TO_LE;
643 // Get the GOT section, creating it if necessary.
644 Output_data_got_powerpc<size, big_endian>*
645 got_section(Symbol_table*, Layout*);
649 make_glink_section(Layout*);
651 // Create the PLT section.
653 make_plt_section(Layout*);
656 make_iplt_section(Layout*);
658 // Create a PLT entry for a global symbol.
660 make_plt_entry(Layout*, Symbol*,
661 const elfcpp::Rela<size, big_endian>&,
662 const Sized_relobj_file<size, big_endian>* object);
664 // Create a PLT entry for a local IFUNC symbol.
666 make_local_ifunc_plt_entry(Layout*,
667 const elfcpp::Rela<size, big_endian>&,
668 Sized_relobj_file<size, big_endian>*);
670 // Create a GOT entry for local dynamic __tls_get_addr.
672 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
673 Sized_relobj_file<size, big_endian>* object);
676 tlsld_got_offset() const
678 return this->tlsld_got_offset_;
681 // Get the dynamic reloc section, creating it if necessary.
683 rela_dyn_section(Layout*);
685 // Copy a relocation against a global symbol.
687 copy_reloc(Symbol_table* symtab, Layout* layout,
688 Sized_relobj_file<size, big_endian>* object,
689 unsigned int shndx, Output_section* output_section,
690 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
692 this->copy_relocs_.copy_reloc(symtab, layout,
693 symtab->get_sized_symbol<size>(sym),
694 object, shndx, output_section,
695 reloc, this->rela_dyn_section(layout));
698 // Information about this specific target which we pass to the
699 // general Target structure.
700 static Target::Target_info powerpc_info;
702 // The types of GOT entries needed for this platform.
703 // These values are exposed to the ABI in an incremental link.
704 // Do not renumber existing values without changing the version
705 // number of the .gnu_incremental_inputs section.
709 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
710 GOT_TYPE_DTPREL, // entry for @got@dtprel
711 GOT_TYPE_TPREL // entry for @got@tprel
714 // The GOT output section.
715 Output_data_got_powerpc<size, big_endian>* got_;
716 // The PLT output section.
717 Output_data_plt_powerpc<size, big_endian>* plt_;
718 // The IPLT output section.
719 Output_data_plt_powerpc<size, big_endian>* iplt_;
720 // The .glink output section.
721 Output_data_glink<size, big_endian>* glink_;
722 // The dynamic reloc output section.
723 Reloc_section* rela_dyn_;
724 // Relocs saved to avoid a COPY reloc.
725 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
726 // Space for variables copied with a COPY reloc.
727 Output_data_space* dynbss_;
728 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
729 unsigned int tlsld_got_offset_;
733 Target::Target_info Target_powerpc<32, true>::powerpc_info =
736 true, // is_big_endian
737 elfcpp::EM_PPC, // machine_code
738 false, // has_make_symbol
739 false, // has_resolve
740 false, // has_code_fill
741 true, // is_default_stack_executable
742 false, // can_icf_inline_merge_sections
744 "/usr/lib/ld.so.1", // dynamic_linker
745 0x10000000, // default_text_segment_address
746 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
747 4 * 1024, // common_pagesize (overridable by -z common-page-size)
748 false, // isolate_execinstr
750 elfcpp::SHN_UNDEF, // small_common_shndx
751 elfcpp::SHN_UNDEF, // large_common_shndx
752 0, // small_common_section_flags
753 0, // large_common_section_flags
754 NULL, // attributes_section
755 NULL // attributes_vendor
759 Target::Target_info Target_powerpc<32, false>::powerpc_info =
762 false, // is_big_endian
763 elfcpp::EM_PPC, // machine_code
764 false, // has_make_symbol
765 false, // has_resolve
766 false, // has_code_fill
767 true, // is_default_stack_executable
768 false, // can_icf_inline_merge_sections
770 "/usr/lib/ld.so.1", // dynamic_linker
771 0x10000000, // default_text_segment_address
772 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
773 4 * 1024, // common_pagesize (overridable by -z common-page-size)
774 false, // isolate_execinstr
776 elfcpp::SHN_UNDEF, // small_common_shndx
777 elfcpp::SHN_UNDEF, // large_common_shndx
778 0, // small_common_section_flags
779 0, // large_common_section_flags
780 NULL, // attributes_section
781 NULL // attributes_vendor
785 Target::Target_info Target_powerpc<64, true>::powerpc_info =
788 true, // is_big_endian
789 elfcpp::EM_PPC64, // machine_code
790 false, // has_make_symbol
791 false, // has_resolve
792 false, // has_code_fill
793 true, // is_default_stack_executable
794 false, // can_icf_inline_merge_sections
796 "/usr/lib/ld.so.1", // dynamic_linker
797 0x10000000, // default_text_segment_address
798 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
799 4 * 1024, // common_pagesize (overridable by -z common-page-size)
800 false, // isolate_execinstr
802 elfcpp::SHN_UNDEF, // small_common_shndx
803 elfcpp::SHN_UNDEF, // large_common_shndx
804 0, // small_common_section_flags
805 0, // large_common_section_flags
806 NULL, // attributes_section
807 NULL // attributes_vendor
811 Target::Target_info Target_powerpc<64, false>::powerpc_info =
814 false, // is_big_endian
815 elfcpp::EM_PPC64, // machine_code
816 false, // has_make_symbol
817 false, // has_resolve
818 false, // has_code_fill
819 true, // is_default_stack_executable
820 false, // can_icf_inline_merge_sections
822 "/usr/lib/ld.so.1", // dynamic_linker
823 0x10000000, // default_text_segment_address
824 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
825 4 * 1024, // common_pagesize (overridable by -z common-page-size)
826 false, // isolate_execinstr
828 elfcpp::SHN_UNDEF, // small_common_shndx
829 elfcpp::SHN_UNDEF, // large_common_shndx
830 0, // small_common_section_flags
831 0, // large_common_section_flags
832 NULL, // attributes_section
833 NULL // attributes_vendor
837 is_branch_reloc(unsigned int r_type)
839 return (r_type == elfcpp::R_POWERPC_REL24
840 || r_type == elfcpp::R_PPC_PLTREL24
841 || r_type == elfcpp::R_PPC_LOCAL24PC
842 || r_type == elfcpp::R_POWERPC_REL14
843 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
844 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
845 || r_type == elfcpp::R_POWERPC_ADDR24
846 || r_type == elfcpp::R_POWERPC_ADDR14
847 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
848 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
851 // If INSN is an opcode that may be used with an @tls operand, return
852 // the transformed insn for TLS optimisation, otherwise return 0. If
853 // REG is non-zero only match an insn with RB or RA equal to REG.
855 at_tls_transform(uint32_t insn, unsigned int reg)
857 if ((insn & (0x3f << 26)) != 31 << 26)
861 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
862 rtra = insn & ((1 << 26) - (1 << 16));
863 else if (((insn >> 16) & 0x1f) == reg)
864 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
868 if ((insn & (0x3ff << 1)) == 266 << 1)
871 else if ((insn & (0x1f << 1)) == 23 << 1
872 && ((insn & (0x1f << 6)) < 14 << 6
873 || ((insn & (0x1f << 6)) >= 16 << 6
874 && (insn & (0x1f << 6)) < 24 << 6)))
875 // load and store indexed -> dform
876 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
877 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
878 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
879 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
880 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
882 insn = (58 << 26) | 2;
889 // Modified version of symtab.h class Symbol member
890 // Given a direct absolute or pc-relative static relocation against
891 // the global symbol, this function returns whether a dynamic relocation
896 needs_dynamic_reloc(const Symbol* gsym, int flags)
898 // No dynamic relocations in a static link!
899 if (parameters->doing_static_link())
902 // A reference to an undefined symbol from an executable should be
903 // statically resolved to 0, and does not need a dynamic relocation.
904 // This matches gnu ld behavior.
905 if (gsym->is_undefined() && !parameters->options().shared())
908 // A reference to an absolute symbol does not need a dynamic relocation.
909 if (gsym->is_absolute())
912 // An absolute reference within a position-independent output file
913 // will need a dynamic relocation.
914 if ((flags & Symbol::ABSOLUTE_REF)
915 && parameters->options().output_is_position_independent())
918 // A function call that can branch to a local PLT entry does not need
919 // a dynamic relocation.
920 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
923 // A reference to any PLT entry in a non-position-independent executable
924 // does not need a dynamic relocation.
925 // Except due to having function descriptors on powerpc64 we don't define
926 // functions to their plt code in an executable, so this doesn't apply.
928 && !parameters->options().output_is_position_independent()
929 && gsym->has_plt_offset())
932 // A reference to a symbol defined in a dynamic object or to a
933 // symbol that is preemptible will need a dynamic relocation.
934 if (gsym->is_from_dynobj()
935 || gsym->is_undefined()
936 || gsym->is_preemptible())
939 // For all other cases, return FALSE.
943 // Modified version of symtab.h class Symbol member
944 // Whether we should use the PLT offset associated with a symbol for
945 // a relocation. FLAGS is a set of Reference_flags.
949 use_plt_offset(const Symbol* gsym, int flags)
951 // If the symbol doesn't have a PLT offset, then naturally we
952 // don't want to use it.
953 if (!gsym->has_plt_offset())
956 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
957 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
960 // If we are going to generate a dynamic relocation, then we will
961 // wind up using that, so no need to use the PLT entry.
962 if (needs_dynamic_reloc<size>(gsym, flags))
965 // If the symbol is from a dynamic object, we need to use the PLT
967 if (gsym->is_from_dynobj())
970 // If we are generating a shared object, and gsym symbol is
971 // undefined or preemptible, we need to use the PLT entry.
972 if (parameters->options().shared()
973 && (gsym->is_undefined() || gsym->is_preemptible()))
976 // If gsym is a call to a weak undefined symbol, we need to use
977 // the PLT entry; the symbol may be defined by a library loaded
979 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
982 // Otherwise we can use the regular definition.
986 template<int size, bool big_endian>
987 class Powerpc_relocate_functions
1004 typedef Powerpc_relocate_functions<size, big_endian> This;
1005 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1007 template<int valsize>
1009 has_overflow_signed(Address value)
1011 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1012 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1013 limit <<= ((valsize - 1) >> 1);
1014 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1015 return value + limit > (limit << 1) - 1;
1018 template<int valsize>
1020 has_overflow_bitfield(Address value)
1022 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1023 limit <<= ((valsize - 1) >> 1);
1024 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1025 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
1028 template<int valsize>
1029 static inline Status
1030 overflowed(Address value, Overflow_check overflow)
1032 if (overflow == CHECK_SIGNED)
1034 if (has_overflow_signed<valsize>(value))
1035 return STATUS_OVERFLOW;
1037 else if (overflow == CHECK_BITFIELD)
1039 if (has_overflow_bitfield<valsize>(value))
1040 return STATUS_OVERFLOW;
1045 // Do a simple RELA relocation
1046 template<int valsize>
1047 static inline Status
1048 rela(unsigned char* view, Address value, Overflow_check overflow)
1050 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1051 Valtype* wv = reinterpret_cast<Valtype*>(view);
1052 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
1053 return overflowed<valsize>(value, overflow);
1056 template<int valsize>
1057 static inline Status
1058 rela(unsigned char* view,
1059 unsigned int right_shift,
1060 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1062 Overflow_check overflow)
1064 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1065 Valtype* wv = reinterpret_cast<Valtype*>(view);
1066 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1067 Valtype reloc = value >> right_shift;
1070 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
1071 return overflowed<valsize>(value >> right_shift, overflow);
1074 // Do a simple RELA relocation, unaligned.
1075 template<int valsize>
1076 static inline Status
1077 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1079 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
1080 return overflowed<valsize>(value, overflow);
1083 template<int valsize>
1084 static inline Status
1085 rela_ua(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_unaligned<valsize, big_endian>::Valtype
1093 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
1094 Valtype reloc = value >> right_shift;
1097 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
1098 return overflowed<valsize>(value >> right_shift, overflow);
1102 // R_PPC64_ADDR64: (Symbol + Addend)
1104 addr64(unsigned char* view, Address value)
1105 { This::template rela<64>(view, value, CHECK_NONE); }
1107 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1109 addr64_u(unsigned char* view, Address value)
1110 { This::template rela_ua<64>(view, value, CHECK_NONE); }
1112 // R_POWERPC_ADDR32: (Symbol + Addend)
1113 static inline Status
1114 addr32(unsigned char* view, Address value, Overflow_check overflow)
1115 { return This::template rela<32>(view, value, overflow); }
1117 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1118 static inline Status
1119 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1120 { return This::template rela_ua<32>(view, value, overflow); }
1122 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1123 static inline Status
1124 addr24(unsigned char* view, Address value, Overflow_check overflow)
1126 Status stat = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
1127 if (overflow != CHECK_NONE && (value & 3) != 0)
1128 stat = STATUS_OVERFLOW;
1132 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1133 static inline Status
1134 addr16(unsigned char* view, Address value, Overflow_check overflow)
1135 { return This::template rela<16>(view, value, overflow); }
1137 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1138 static inline Status
1139 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1140 { return This::template rela_ua<16>(view, value, overflow); }
1142 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1143 static inline Status
1144 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1146 Status stat = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1147 if (overflow != CHECK_NONE && (value & 3) != 0)
1148 stat = STATUS_OVERFLOW;
1152 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1154 addr16_hi(unsigned char* view, Address value)
1155 { This::template rela<16>(view, 16, 0xffff, value, CHECK_NONE); }
1157 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1159 addr16_ha(unsigned char* view, Address value)
1160 { This::addr16_hi(view, value + 0x8000); }
1162 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1164 addr16_hi2(unsigned char* view, Address value)
1165 { This::template rela<16>(view, 32, 0xffff, value, CHECK_NONE); }
1167 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1169 addr16_ha2(unsigned char* view, Address value)
1170 { This::addr16_hi2(view, value + 0x8000); }
1172 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1174 addr16_hi3(unsigned char* view, Address value)
1175 { This::template rela<16>(view, 48, 0xffff, value, CHECK_NONE); }
1177 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1179 addr16_ha3(unsigned char* view, Address value)
1180 { This::addr16_hi3(view, value + 0x8000); }
1182 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1183 static inline Status
1184 addr14(unsigned char* view, Address value, Overflow_check overflow)
1186 Status stat = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1187 if (overflow != CHECK_NONE && (value & 3) != 0)
1188 stat = STATUS_OVERFLOW;
1193 // Stash away the index of .got2 or .opd in a relocatable object, if
1194 // such a section exists.
1196 template<int size, bool big_endian>
1198 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1199 Read_symbols_data* sd)
1201 const unsigned char* const pshdrs = sd->section_headers->data();
1202 const unsigned char* namesu = sd->section_names->data();
1203 const char* names = reinterpret_cast<const char*>(namesu);
1204 section_size_type names_size = sd->section_names_size;
1205 const unsigned char* s;
1207 s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1208 names, names_size, NULL);
1211 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1212 this->special_ = ndx;
1214 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1217 // Examine .rela.opd to build info about function entry points.
1219 template<int size, bool big_endian>
1221 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1223 const unsigned char* prelocs,
1224 const unsigned char* plocal_syms)
1228 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1230 const int reloc_size
1231 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1232 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1233 Address expected_off = 0;
1234 bool regular = true;
1235 unsigned int opd_ent_size = 0;
1237 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1239 Reltype reloc(prelocs);
1240 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1241 = reloc.get_r_info();
1242 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1243 if (r_type == elfcpp::R_PPC64_ADDR64)
1245 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1246 typename elfcpp::Elf_types<size>::Elf_Addr value;
1249 if (r_sym < this->local_symbol_count())
1251 typename elfcpp::Sym<size, big_endian>
1252 lsym(plocal_syms + r_sym * sym_size);
1253 shndx = lsym.get_st_shndx();
1254 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1255 value = lsym.get_st_value();
1258 shndx = this->symbol_section_and_value(r_sym, &value,
1260 this->set_opd_ent(reloc.get_r_offset(), shndx,
1261 value + reloc.get_r_addend());
1264 expected_off = reloc.get_r_offset();
1265 opd_ent_size = expected_off;
1267 else if (expected_off != reloc.get_r_offset())
1269 expected_off += opd_ent_size;
1271 else if (r_type == elfcpp::R_PPC64_TOC)
1273 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
1278 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1279 this->name().c_str(), r_type);
1283 if (reloc_count <= 2)
1284 opd_ent_size = this->section_size(this->opd_shndx());
1285 if (opd_ent_size != 24 && opd_ent_size != 16)
1289 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1290 this->name().c_str());
1296 template<int size, bool big_endian>
1298 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1300 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1303 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1304 p != rd->relocs.end();
1307 if (p->data_shndx == this->opd_shndx())
1309 uint64_t opd_size = this->section_size(this->opd_shndx());
1310 gold_assert(opd_size == static_cast<size_t>(opd_size));
1313 this->init_opd(opd_size);
1314 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1315 rd->local_symbols->data());
1323 // Set up PowerPC target specific relobj.
1325 template<int size, bool big_endian>
1327 Target_powerpc<size, big_endian>::do_make_elf_object(
1328 const std::string& name,
1329 Input_file* input_file,
1330 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1332 int et = ehdr.get_e_type();
1333 // ET_EXEC files are valid input for --just-symbols/-R,
1334 // and we treat them as relocatable objects.
1335 if (et == elfcpp::ET_REL
1336 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1338 Powerpc_relobj<size, big_endian>* obj =
1339 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1343 else if (et == elfcpp::ET_DYN)
1345 Sized_dynobj<size, big_endian>* obj =
1346 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1352 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1357 template<int size, bool big_endian>
1358 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1361 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1362 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1364 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1365 : Output_data_got<size, big_endian>(),
1366 symtab_(symtab), layout_(layout),
1367 header_ent_cnt_(size == 32 ? 3 : 1),
1368 header_index_(size == 32 ? 0x2000 : 0)
1373 // Create a new GOT entry and return its offset.
1375 add_got_entry(Got_entry got_entry)
1377 this->reserve_ent();
1378 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1381 // Create a pair of new GOT entries and return the offset of the first.
1383 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1385 this->reserve_ent(2);
1386 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1391 add_constant_pair(Valtype c1, Valtype c2)
1393 this->reserve_ent(2);
1394 unsigned int got_offset = this->add_constant(c1);
1395 this->add_constant(c2);
1399 // Offset of _GLOBAL_OFFSET_TABLE_.
1403 return this->got_offset(this->header_index_);
1406 // Offset of base used to access the GOT/TOC.
1407 // The got/toc pointer reg will be set to this value.
1408 typename elfcpp::Elf_types<size>::Elf_Off
1409 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1412 return this->g_o_t();
1414 return (this->output_section()->address()
1415 + object->toc_base_offset()
1419 // Ensure our GOT has a header.
1421 set_final_data_size()
1423 if (this->header_ent_cnt_ != 0)
1424 this->make_header();
1425 Output_data_got<size, big_endian>::set_final_data_size();
1428 // First word of GOT header needs some values that are not
1429 // handled by Output_data_got so poke them in here.
1430 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1432 do_write(Output_file* of)
1435 if (size == 32 && this->layout_->dynamic_data() != NULL)
1436 val = this->layout_->dynamic_section()->address();
1438 val = this->output_section()->address() + 0x8000;
1439 this->replace_constant(this->header_index_, val);
1440 Output_data_got<size, big_endian>::do_write(of);
1445 reserve_ent(unsigned int cnt = 1)
1447 if (this->header_ent_cnt_ == 0)
1449 if (this->num_entries() + cnt > this->header_index_)
1450 this->make_header();
1456 this->header_ent_cnt_ = 0;
1457 this->header_index_ = this->num_entries();
1460 Output_data_got<size, big_endian>::add_constant(0);
1461 Output_data_got<size, big_endian>::add_constant(0);
1462 Output_data_got<size, big_endian>::add_constant(0);
1464 // Define _GLOBAL_OFFSET_TABLE_ at the header
1465 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1466 Symbol_table::PREDEFINED,
1467 this, this->g_o_t(), 0,
1474 Output_data_got<size, big_endian>::add_constant(0);
1477 // Stashed pointers.
1478 Symbol_table* symtab_;
1482 unsigned int header_ent_cnt_;
1483 // GOT header index.
1484 unsigned int header_index_;
1487 // Get the GOT section, creating it if necessary.
1489 template<int size, bool big_endian>
1490 Output_data_got_powerpc<size, big_endian>*
1491 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1494 if (this->got_ == NULL)
1496 gold_assert(symtab != NULL && layout != NULL);
1499 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
1501 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1502 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1503 this->got_, ORDER_DATA, false);
1509 // Get the dynamic reloc section, creating it if necessary.
1511 template<int size, bool big_endian>
1512 typename Target_powerpc<size, big_endian>::Reloc_section*
1513 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1515 if (this->rela_dyn_ == NULL)
1517 gold_assert(layout != NULL);
1518 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1519 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1520 elfcpp::SHF_ALLOC, this->rela_dyn_,
1521 ORDER_DYNAMIC_RELOCS, false);
1523 return this->rela_dyn_;
1526 // A class to handle the PLT data.
1528 template<int size, bool big_endian>
1529 class Output_data_plt_powerpc : public Output_section_data_build
1532 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1533 size, big_endian> Reloc_section;
1535 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
1536 Reloc_section* plt_rel,
1537 unsigned int reserved_size,
1539 : Output_section_data_build(size == 32 ? 4 : 8),
1542 initial_plt_entry_size_(reserved_size),
1546 // Add an entry to the PLT.
1551 add_ifunc_entry(Symbol*);
1554 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
1556 // Return the .rela.plt section data.
1563 // Return the number of PLT entries.
1567 return ((this->current_data_size() - this->initial_plt_entry_size_)
1571 // Return the offset of the first non-reserved PLT entry.
1573 first_plt_entry_offset()
1574 { return this->initial_plt_entry_size_; }
1576 // Return the size of a PLT entry.
1578 get_plt_entry_size()
1579 { return plt_entry_size; }
1583 do_adjust_output_section(Output_section* os)
1588 // Write to a map file.
1590 do_print_to_mapfile(Mapfile* mapfile) const
1591 { mapfile->print_output_data(this, this->name_); }
1594 // The size of an entry in the PLT.
1595 static const int plt_entry_size = size == 32 ? 4 : 24;
1597 // Write out the PLT data.
1599 do_write(Output_file*);
1601 // The reloc section.
1602 Reloc_section* rel_;
1603 // Allows access to .glink for do_write.
1604 Target_powerpc<size, big_endian>* targ_;
1605 // The size of the first reserved entry.
1606 int initial_plt_entry_size_;
1607 // What to report in map file.
1611 // Add an entry to the PLT.
1613 template<int size, bool big_endian>
1615 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1617 if (!gsym->has_plt_offset())
1619 off_t off = this->current_data_size();
1621 off += this->first_plt_entry_offset();
1622 gsym->set_plt_offset(off);
1623 gsym->set_needs_dynsym_entry();
1624 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
1625 this->rel_->add_global(gsym, dynrel, this, off, 0);
1626 off += plt_entry_size;
1627 this->set_current_data_size(off);
1631 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
1633 template<int size, bool big_endian>
1635 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
1637 if (!gsym->has_plt_offset())
1639 off_t off = this->current_data_size();
1640 gsym->set_plt_offset(off);
1641 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
1643 dynrel = elfcpp::R_PPC64_JMP_IREL;
1644 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
1645 off += plt_entry_size;
1646 this->set_current_data_size(off);
1650 // Add an entry for a local ifunc symbol to the IPLT.
1652 template<int size, bool big_endian>
1654 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
1655 Sized_relobj_file<size, big_endian>* relobj,
1656 unsigned int local_sym_index)
1658 if (!relobj->local_has_plt_offset(local_sym_index))
1660 off_t off = this->current_data_size();
1661 relobj->set_local_plt_offset(local_sym_index, off);
1662 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
1664 dynrel = elfcpp::R_PPC64_JMP_IREL;
1665 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
1667 off += plt_entry_size;
1668 this->set_current_data_size(off);
1672 static const uint32_t add_0_11_11 = 0x7c0b5a14;
1673 static const uint32_t add_3_3_2 = 0x7c631214;
1674 static const uint32_t add_3_3_13 = 0x7c636a14;
1675 static const uint32_t add_11_0_11 = 0x7d605a14;
1676 static const uint32_t add_12_2_11 = 0x7d825a14;
1677 static const uint32_t addi_11_11 = 0x396b0000;
1678 static const uint32_t addi_12_12 = 0x398c0000;
1679 static const uint32_t addi_2_2 = 0x38420000;
1680 static const uint32_t addi_3_2 = 0x38620000;
1681 static const uint32_t addi_3_3 = 0x38630000;
1682 static const uint32_t addis_0_2 = 0x3c020000;
1683 static const uint32_t addis_0_13 = 0x3c0d0000;
1684 static const uint32_t addis_11_11 = 0x3d6b0000;
1685 static const uint32_t addis_11_30 = 0x3d7e0000;
1686 static const uint32_t addis_12_12 = 0x3d8c0000;
1687 static const uint32_t addis_12_2 = 0x3d820000;
1688 static const uint32_t addis_3_2 = 0x3c620000;
1689 static const uint32_t addis_3_13 = 0x3c6d0000;
1690 static const uint32_t b = 0x48000000;
1691 static const uint32_t bcl_20_31 = 0x429f0005;
1692 static const uint32_t bctr = 0x4e800420;
1693 static const uint32_t blrl = 0x4e800021;
1694 static const uint32_t cror_15_15_15 = 0x4def7b82;
1695 static const uint32_t cror_31_31_31 = 0x4ffffb82;
1696 static const uint32_t ld_11_12 = 0xe96c0000;
1697 static const uint32_t ld_11_2 = 0xe9620000;
1698 static const uint32_t ld_2_1 = 0xe8410000;
1699 static const uint32_t ld_2_11 = 0xe84b0000;
1700 static const uint32_t ld_2_12 = 0xe84c0000;
1701 static const uint32_t ld_2_2 = 0xe8420000;
1702 static const uint32_t li_0_0 = 0x38000000;
1703 static const uint32_t lis_0_0 = 0x3c000000;
1704 static const uint32_t lis_11 = 0x3d600000;
1705 static const uint32_t lis_12 = 0x3d800000;
1706 static const uint32_t lwz_0_12 = 0x800c0000;
1707 static const uint32_t lwz_11_11 = 0x816b0000;
1708 static const uint32_t lwz_11_30 = 0x817e0000;
1709 static const uint32_t lwz_12_12 = 0x818c0000;
1710 static const uint32_t lwzu_0_12 = 0x840c0000;
1711 static const uint32_t mflr_0 = 0x7c0802a6;
1712 static const uint32_t mflr_11 = 0x7d6802a6;
1713 static const uint32_t mflr_12 = 0x7d8802a6;
1714 static const uint32_t mtctr_0 = 0x7c0903a6;
1715 static const uint32_t mtctr_11 = 0x7d6903a6;
1716 static const uint32_t mtlr_0 = 0x7c0803a6;
1717 static const uint32_t mtlr_12 = 0x7d8803a6;
1718 static const uint32_t nop = 0x60000000;
1719 static const uint32_t ori_0_0_0 = 0x60000000;
1720 static const uint32_t std_2_1 = 0xf8410000;
1721 static const uint32_t sub_11_11_12 = 0x7d6c5850;
1723 // Write out the PLT.
1725 template<int size, bool big_endian>
1727 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1731 const off_t offset = this->offset();
1732 const section_size_type oview_size
1733 = convert_to_section_size_type(this->data_size());
1734 unsigned char* const oview = of->get_output_view(offset, oview_size);
1735 unsigned char* pov = oview;
1736 unsigned char* endpov = oview + oview_size;
1738 // The address of the .glink branch table
1739 const Output_data_glink<size, big_endian>* glink
1740 = this->targ_->glink_section();
1741 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1742 = glink->address() + glink->pltresolve();
1744 while (pov < endpov)
1746 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1751 of->write_output_view(offset, oview_size, oview);
1755 // Create the PLT section.
1757 template<int size, bool big_endian>
1759 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1761 if (this->plt_ == NULL)
1763 if (this->glink_ == NULL)
1764 make_glink_section(layout);
1766 // Ensure that .rela.dyn always appears before .rela.plt This is
1767 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1768 // needs to include .rela.plt in it's range.
1769 this->rela_dyn_section(layout);
1771 Reloc_section* plt_rel = new Reloc_section(false);
1772 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1773 elfcpp::SHF_ALLOC, plt_rel,
1774 ORDER_DYNAMIC_PLT_RELOCS, false);
1776 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
1777 size == 32 ? 0 : 24,
1779 layout->add_output_section_data(".plt",
1781 ? elfcpp::SHT_PROGBITS
1782 : elfcpp::SHT_NOBITS),
1783 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1792 // Create the IPLT section.
1794 template<int size, bool big_endian>
1796 Target_powerpc<size, big_endian>::make_iplt_section(Layout* layout)
1798 if (this->iplt_ == NULL)
1800 this->make_plt_section(layout);
1802 Reloc_section* iplt_rel = new Reloc_section(false);
1803 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
1805 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
1807 this->plt_->output_section()->add_output_section_data(this->iplt_);
1811 // A class to handle .glink.
1813 template<int size, bool big_endian>
1814 class Output_data_glink : public Output_section_data
1817 static const int pltresolve_size = 16*4;
1819 Output_data_glink(Target_powerpc<size, big_endian>*);
1823 add_entry(const Sized_relobj_file<size, big_endian>*,
1825 const elfcpp::Rela<size, big_endian>&);
1828 add_entry(const Sized_relobj_file<size, big_endian>*,
1830 const elfcpp::Rela<size, big_endian>&);
1833 find_entry(const Symbol*) const;
1836 find_entry(const Sized_relobj_file<size, big_endian>*, unsigned int) const;
1839 find_entry(const Sized_relobj_file<size, big_endian>*,
1841 const elfcpp::Rela<size, big_endian>&) const;
1844 find_entry(const Sized_relobj_file<size, big_endian>*,
1846 const elfcpp::Rela<size, big_endian>&) const;
1849 glink_entry_size() const
1854 // FIXME: We should be using multiple glink sections for
1855 // stubs to support > 33M applications.
1862 return this->pltresolve_;
1866 // Write to a map file.
1868 do_print_to_mapfile(Mapfile* mapfile) const
1869 { mapfile->print_output_data(this, _("** glink")); }
1873 set_final_data_size();
1877 do_write(Output_file*);
1882 Glink_sym_ent(const Symbol* sym)
1883 : sym_(sym), object_(0), addend_(0), locsym_(0)
1886 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
1887 unsigned int locsym_index)
1888 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
1891 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
1893 const elfcpp::Rela<size, big_endian>& reloc)
1894 : sym_(sym), object_(0), addend_(0), locsym_(0)
1897 this->addend_ = reloc.get_r_addend();
1898 else if (parameters->options().output_is_position_independent()
1899 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1900 == elfcpp::R_PPC_PLTREL24))
1902 this->addend_ = reloc.get_r_addend();
1903 if (this->addend_ >= 32768)
1904 this->object_ = object;
1908 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
1909 unsigned int locsym_index,
1910 const elfcpp::Rela<size, big_endian>& reloc)
1911 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
1914 this->addend_ = reloc.get_r_addend();
1915 else if (parameters->options().output_is_position_independent()
1916 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1917 == elfcpp::R_PPC_PLTREL24))
1918 this->addend_ = reloc.get_r_addend();
1921 bool operator==(const Glink_sym_ent& that) const
1923 return (this->sym_ == that.sym_
1924 && this->object_ == that.object_
1925 && this->addend_ == that.addend_
1926 && this->locsym_ == that.locsym_);
1930 const Sized_relobj_file<size, big_endian>* object_;
1931 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
1932 unsigned int locsym_;
1935 class Glink_sym_ent_hash
1938 size_t operator()(const Glink_sym_ent& ent) const
1940 return (reinterpret_cast<uintptr_t>(ent.sym_)
1941 ^ reinterpret_cast<uintptr_t>(ent.object_)
1947 // Map sym/object/addend to index.
1948 typedef Unordered_map<Glink_sym_ent, unsigned int,
1949 Glink_sym_ent_hash> Glink_entries;
1950 Glink_entries glink_entries_;
1952 // Offset of pltresolve stub (actually, branch table for 32-bit)
1955 // Allows access to .got and .plt for do_write.
1956 Target_powerpc<size, big_endian>* targ_;
1959 // Create the glink section.
1961 template<int size, bool big_endian>
1962 Output_data_glink<size, big_endian>::Output_data_glink(
1963 Target_powerpc<size, big_endian>* targ)
1964 : Output_section_data(16),
1965 pltresolve_(0), targ_(targ)
1969 // Add an entry to glink, if we do not already have one for this
1970 // sym/object/addend combo.
1972 template<int size, bool big_endian>
1974 Output_data_glink<size, big_endian>::add_entry(
1975 const Sized_relobj_file<size, big_endian>* object,
1977 const elfcpp::Rela<size, big_endian>& reloc)
1979 Glink_sym_ent ent(object, gsym, reloc);
1980 unsigned int indx = this->glink_entries_.size();
1981 this->glink_entries_.insert(std::make_pair(ent, indx));
1984 template<int size, bool big_endian>
1986 Output_data_glink<size, big_endian>::add_entry(
1987 const Sized_relobj_file<size, big_endian>* object,
1988 unsigned int locsym_index,
1989 const elfcpp::Rela<size, big_endian>& reloc)
1991 Glink_sym_ent ent(object, locsym_index, reloc);
1992 unsigned int indx = this->glink_entries_.size();
1993 this->glink_entries_.insert(std::make_pair(ent, indx));
1996 template<int size, bool big_endian>
1998 Output_data_glink<size, big_endian>::find_entry(
1999 const Sized_relobj_file<size, big_endian>* object,
2001 const elfcpp::Rela<size, big_endian>& reloc) const
2003 Glink_sym_ent ent(object, gsym, reloc);
2004 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2005 gold_assert(p != this->glink_entries_.end());
2009 template<int size, bool big_endian>
2011 Output_data_glink<size, big_endian>::find_entry(const Symbol* gsym) const
2013 Glink_sym_ent ent(gsym);
2014 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2015 gold_assert(p != this->glink_entries_.end());
2019 template<int size, bool big_endian>
2021 Output_data_glink<size, big_endian>::find_entry(
2022 const Sized_relobj_file<size, big_endian>* object,
2023 unsigned int locsym_index,
2024 const elfcpp::Rela<size, big_endian>& reloc) const
2026 Glink_sym_ent ent(object, locsym_index, reloc);
2027 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2028 gold_assert(p != this->glink_entries_.end());
2032 template<int size, bool big_endian>
2034 Output_data_glink<size, big_endian>::find_entry(
2035 const Sized_relobj_file<size, big_endian>* object,
2036 unsigned int locsym_index) const
2038 Glink_sym_ent ent(object, locsym_index);
2039 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2040 gold_assert(p != this->glink_entries_.end());
2044 template<int size, bool big_endian>
2046 Output_data_glink<size, big_endian>::set_final_data_size()
2048 unsigned int count = this->glink_entries_.size();
2049 off_t total = count;
2056 this->pltresolve_ = total;
2058 // space for branch table
2059 total += 4 * (count - 1);
2061 total += -total & 15;
2062 total += this->pltresolve_size;
2067 this->pltresolve_ = total;
2068 total += this->pltresolve_size;
2070 // space for branch table
2073 total += 4 * (count - 0x8000);
2077 this->set_data_size(total);
2080 static inline uint32_t
2086 static inline uint32_t
2092 static inline uint32_t
2095 return hi(a + 0x8000);
2098 template<bool big_endian>
2100 write_insn(unsigned char* p, uint32_t v)
2102 elfcpp::Swap<32, big_endian>::writeval(p, v);
2105 // Write out .glink.
2107 template<int size, bool big_endian>
2109 Output_data_glink<size, big_endian>::do_write(Output_file* of)
2111 const off_t off = this->offset();
2112 const section_size_type oview_size =
2113 convert_to_section_size_type(this->data_size());
2114 unsigned char* const oview = of->get_output_view(off, oview_size);
2117 // The base address of the .plt section.
2118 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2119 static const Address invalid_address = static_cast<Address>(0) - 1;
2120 Address plt_base = this->targ_->plt_section()->address();
2121 Address iplt_base = invalid_address;
2123 const Output_data_got_powerpc<size, big_endian>* got
2124 = this->targ_->got_section();
2128 Address got_os_addr = got->output_section()->address();
2130 // Write out call stubs.
2131 typename Glink_entries::const_iterator g;
2132 for (g = this->glink_entries_.begin();
2133 g != this->glink_entries_.end();
2138 const Symbol* gsym = g->first.sym_;
2141 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
2142 && gsym->can_use_relative_reloc(false));
2143 plt_addr = gsym->plt_offset();
2148 const Sized_relobj_file<size, big_endian>* relobj
2150 unsigned int local_sym_index = g->first.locsym_;
2151 plt_addr = relobj->local_plt_offset(local_sym_index);
2155 if (iplt_base == invalid_address)
2156 iplt_base = this->targ_->iplt_section()->address();
2157 plt_addr += iplt_base;
2160 plt_addr += plt_base;
2161 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2162 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
2163 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
2164 Address pltoff = plt_addr - got_addr;
2166 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
2167 gold_error(_("%s: linkage table error against `%s'"),
2168 g->first.object_->name().c_str(),
2169 g->first.sym_->demangled_name().c_str());
2171 p = oview + g->second * this->glink_entry_size();
2172 if (ha(pltoff) != 0)
2174 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
2175 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
2176 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
2177 if (ha(pltoff + 16) != ha(pltoff))
2179 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
2182 write_insn<big_endian>(p, mtctr_11), p += 4;
2183 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
2184 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
2185 write_insn<big_endian>(p, bctr), p += 4;
2189 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
2190 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
2191 if (ha(pltoff + 16) != ha(pltoff))
2193 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
2196 write_insn<big_endian>(p, mtctr_11), p += 4;
2197 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
2198 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
2199 write_insn<big_endian>(p, bctr), p += 4;
2203 // Write pltresolve stub.
2204 p = oview + this->pltresolve_;
2205 Address after_bcl = this->address() + this->pltresolve_ + 16;
2206 Address pltoff = plt_base - after_bcl;
2208 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
2210 write_insn<big_endian>(p, mflr_12), p += 4;
2211 write_insn<big_endian>(p, bcl_20_31), p += 4;
2212 write_insn<big_endian>(p, mflr_11), p += 4;
2213 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
2214 write_insn<big_endian>(p, mtlr_12), p += 4;
2215 write_insn<big_endian>(p, add_12_2_11), p += 4;
2216 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
2217 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
2218 write_insn<big_endian>(p, mtctr_11), p += 4;
2219 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
2220 write_insn<big_endian>(p, bctr), p += 4;
2221 while (p < oview + this->pltresolve_ + this->pltresolve_size)
2222 write_insn<big_endian>(p, nop), p += 4;
2224 // Write lazy link call stubs.
2226 while (p < oview + oview_size)
2230 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
2234 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
2235 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
2237 uint32_t branch_off = this->pltresolve_ + 8 - (p - oview);
2238 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
2244 // The address of _GLOBAL_OFFSET_TABLE_.
2245 Address g_o_t = got->address() + got->g_o_t();
2247 // Write out call stubs.
2248 typename Glink_entries::const_iterator g;
2249 for (g = this->glink_entries_.begin();
2250 g != this->glink_entries_.end();
2255 const Symbol* gsym = g->first.sym_;
2258 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
2259 && gsym->can_use_relative_reloc(false));
2260 plt_addr = gsym->plt_offset();
2265 const Sized_relobj_file<size, big_endian>* relobj
2267 unsigned int local_sym_index = g->first.locsym_;
2268 plt_addr = relobj->local_plt_offset(local_sym_index);
2272 if (iplt_base == invalid_address)
2273 iplt_base = this->targ_->iplt_section()->address();
2274 plt_addr += iplt_base;
2277 plt_addr += plt_base;
2279 p = oview + g->second * this->glink_entry_size();
2280 if (parameters->options().output_is_position_independent())
2283 const Powerpc_relobj<size, big_endian>* object = static_cast
2284 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
2285 if (object != NULL && g->first.addend_ >= 32768)
2287 unsigned int got2 = object->got2_shndx();
2288 got_addr = g->first.object_->get_output_section_offset(got2);
2289 gold_assert(got_addr != invalid_address);
2290 got_addr += (g->first.object_->output_section(got2)->address()
2291 + g->first.addend_);
2296 Address pltoff = plt_addr - got_addr;
2297 if (ha(pltoff) == 0)
2299 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
2300 write_insn<big_endian>(p + 4, mtctr_11);
2301 write_insn<big_endian>(p + 8, bctr);
2305 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
2306 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
2307 write_insn<big_endian>(p + 8, mtctr_11);
2308 write_insn<big_endian>(p + 12, bctr);
2313 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
2314 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
2315 write_insn<big_endian>(p + 8, mtctr_11);
2316 write_insn<big_endian>(p + 12, bctr);
2320 // Write out pltresolve branch table.
2321 p = oview + this->pltresolve_;
2322 unsigned int the_end = oview_size - this->pltresolve_size;
2323 unsigned char* end_p = oview + the_end;
2324 while (p < end_p - 8 * 4)
2325 write_insn<big_endian>(p, b + end_p - p), p += 4;
2327 write_insn<big_endian>(p, nop), p += 4;
2329 // Write out pltresolve call stub.
2330 if (parameters->options().output_is_position_independent())
2332 Address res0_off = this->pltresolve_;
2333 Address after_bcl_off = the_end + 12;
2334 Address bcl_res0 = after_bcl_off - res0_off;
2336 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
2337 write_insn<big_endian>(p + 4, mflr_0);
2338 write_insn<big_endian>(p + 8, bcl_20_31);
2339 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
2340 write_insn<big_endian>(p + 16, mflr_12);
2341 write_insn<big_endian>(p + 20, mtlr_0);
2342 write_insn<big_endian>(p + 24, sub_11_11_12);
2344 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
2346 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
2347 if (ha(got_bcl) == ha(got_bcl + 4))
2349 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
2350 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
2354 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
2355 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
2357 write_insn<big_endian>(p + 40, mtctr_0);
2358 write_insn<big_endian>(p + 44, add_0_11_11);
2359 write_insn<big_endian>(p + 48, add_11_0_11);
2360 write_insn<big_endian>(p + 52, bctr);
2361 write_insn<big_endian>(p + 56, nop);
2362 write_insn<big_endian>(p + 60, nop);
2366 Address res0 = this->pltresolve_ + this->address();
2368 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
2369 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
2370 if (ha(g_o_t + 4) == ha(g_o_t + 8))
2371 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
2373 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
2374 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
2375 write_insn<big_endian>(p + 16, mtctr_0);
2376 write_insn<big_endian>(p + 20, add_0_11_11);
2377 if (ha(g_o_t + 4) == ha(g_o_t + 8))
2378 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
2380 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
2381 write_insn<big_endian>(p + 28, add_11_0_11);
2382 write_insn<big_endian>(p + 32, bctr);
2383 write_insn<big_endian>(p + 36, nop);
2384 write_insn<big_endian>(p + 40, nop);
2385 write_insn<big_endian>(p + 44, nop);
2386 write_insn<big_endian>(p + 48, nop);
2387 write_insn<big_endian>(p + 52, nop);
2388 write_insn<big_endian>(p + 56, nop);
2389 write_insn<big_endian>(p + 60, nop);
2394 of->write_output_view(off, oview_size, oview);
2397 // Create the glink section.
2399 template<int size, bool big_endian>
2401 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
2403 if (this->glink_ == NULL)
2405 this->glink_ = new Output_data_glink<size, big_endian>(this);
2406 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2407 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2408 this->glink_, ORDER_TEXT, false);
2412 // Create a PLT entry for a global symbol.
2414 template<int size, bool big_endian>
2416 Target_powerpc<size, big_endian>::make_plt_entry(
2419 const elfcpp::Rela<size, big_endian>& reloc,
2420 const Sized_relobj_file<size, big_endian>* object)
2422 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2423 && gsym->can_use_relative_reloc(false))
2425 if (this->iplt_ == NULL)
2426 this->make_iplt_section(layout);
2427 this->iplt_->add_ifunc_entry(gsym);
2431 if (this->plt_ == NULL)
2432 this->make_plt_section(layout);
2433 this->plt_->add_entry(gsym);
2435 this->glink_->add_entry(object, gsym, reloc);
2438 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2440 template<int size, bool big_endian>
2442 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
2444 const elfcpp::Rela<size, big_endian>& reloc,
2445 Sized_relobj_file<size, big_endian>* relobj)
2447 if (this->iplt_ == NULL)
2448 this->make_iplt_section(layout);
2449 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2450 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
2451 this->glink_->add_entry(relobj, r_sym, reloc);
2454 // Return the number of entries in the PLT.
2456 template<int size, bool big_endian>
2458 Target_powerpc<size, big_endian>::plt_entry_count() const
2460 if (this->plt_ == NULL)
2462 unsigned int count = this->plt_->entry_count();
2463 if (this->iplt_ != NULL)
2464 count += this->iplt_->entry_count();
2468 // Return the offset of the first non-reserved PLT entry.
2470 template<int size, bool big_endian>
2472 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
2474 return this->plt_->first_plt_entry_offset();
2477 // Return the size of each PLT entry.
2479 template<int size, bool big_endian>
2481 Target_powerpc<size, big_endian>::plt_entry_size() const
2483 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
2486 // Create a GOT entry for local dynamic __tls_get_addr calls.
2488 template<int size, bool big_endian>
2490 Target_powerpc<size, big_endian>::tlsld_got_offset(
2491 Symbol_table* symtab,
2493 Sized_relobj_file<size, big_endian>* object)
2495 if (this->tlsld_got_offset_ == -1U)
2497 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2498 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2499 Output_data_got_powerpc<size, big_endian>* got
2500 = this->got_section(symtab, layout);
2501 unsigned int got_offset = got->add_constant_pair(0, 0);
2502 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
2504 this->tlsld_got_offset_ = got_offset;
2506 return this->tlsld_got_offset_;
2509 // Get the Reference_flags for a particular relocation.
2511 template<int size, bool big_endian>
2513 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
2517 case elfcpp::R_POWERPC_NONE:
2518 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2519 case elfcpp::R_POWERPC_GNU_VTENTRY:
2520 case elfcpp::R_PPC64_TOC:
2521 // No symbol reference.
2524 case elfcpp::R_PPC64_ADDR64:
2525 case elfcpp::R_PPC64_UADDR64:
2526 case elfcpp::R_POWERPC_ADDR32:
2527 case elfcpp::R_POWERPC_UADDR32:
2528 case elfcpp::R_POWERPC_ADDR16:
2529 case elfcpp::R_POWERPC_UADDR16:
2530 case elfcpp::R_POWERPC_ADDR16_LO:
2531 case elfcpp::R_POWERPC_ADDR16_HI:
2532 case elfcpp::R_POWERPC_ADDR16_HA:
2533 return Symbol::ABSOLUTE_REF;
2535 case elfcpp::R_POWERPC_ADDR24:
2536 case elfcpp::R_POWERPC_ADDR14:
2537 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2538 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2539 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2541 case elfcpp::R_PPC64_REL64:
2542 case elfcpp::R_POWERPC_REL32:
2543 case elfcpp::R_PPC_LOCAL24PC:
2544 case elfcpp::R_POWERPC_REL16:
2545 case elfcpp::R_POWERPC_REL16_LO:
2546 case elfcpp::R_POWERPC_REL16_HI:
2547 case elfcpp::R_POWERPC_REL16_HA:
2548 return Symbol::RELATIVE_REF;
2550 case elfcpp::R_POWERPC_REL24:
2551 case elfcpp::R_PPC_PLTREL24:
2552 case elfcpp::R_POWERPC_REL14:
2553 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2554 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2555 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2557 case elfcpp::R_POWERPC_GOT16:
2558 case elfcpp::R_POWERPC_GOT16_LO:
2559 case elfcpp::R_POWERPC_GOT16_HI:
2560 case elfcpp::R_POWERPC_GOT16_HA:
2561 case elfcpp::R_PPC64_GOT16_DS:
2562 case elfcpp::R_PPC64_GOT16_LO_DS:
2563 case elfcpp::R_PPC64_TOC16:
2564 case elfcpp::R_PPC64_TOC16_LO:
2565 case elfcpp::R_PPC64_TOC16_HI:
2566 case elfcpp::R_PPC64_TOC16_HA:
2567 case elfcpp::R_PPC64_TOC16_DS:
2568 case elfcpp::R_PPC64_TOC16_LO_DS:
2570 return Symbol::ABSOLUTE_REF;
2572 case elfcpp::R_POWERPC_GOT_TPREL16:
2573 case elfcpp::R_POWERPC_TLS:
2574 return Symbol::TLS_REF;
2576 case elfcpp::R_POWERPC_COPY:
2577 case elfcpp::R_POWERPC_GLOB_DAT:
2578 case elfcpp::R_POWERPC_JMP_SLOT:
2579 case elfcpp::R_POWERPC_RELATIVE:
2580 case elfcpp::R_POWERPC_DTPMOD:
2582 // Not expected. We will give an error later.
2587 // Report an unsupported relocation against a local symbol.
2589 template<int size, bool big_endian>
2591 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
2592 Sized_relobj_file<size, big_endian>* object,
2593 unsigned int r_type)
2595 gold_error(_("%s: unsupported reloc %u against local symbol"),
2596 object->name().c_str(), r_type);
2599 // We are about to emit a dynamic relocation of type R_TYPE. If the
2600 // dynamic linker does not support it, issue an error.
2602 template<int size, bool big_endian>
2604 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
2605 unsigned int r_type)
2607 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
2609 // These are the relocation types supported by glibc for both 32-bit
2610 // and 64-bit powerpc.
2613 case elfcpp::R_POWERPC_NONE:
2614 case elfcpp::R_POWERPC_RELATIVE:
2615 case elfcpp::R_POWERPC_GLOB_DAT:
2616 case elfcpp::R_POWERPC_DTPMOD:
2617 case elfcpp::R_POWERPC_DTPREL:
2618 case elfcpp::R_POWERPC_TPREL:
2619 case elfcpp::R_POWERPC_JMP_SLOT:
2620 case elfcpp::R_POWERPC_COPY:
2621 case elfcpp::R_POWERPC_IRELATIVE:
2622 case elfcpp::R_POWERPC_ADDR32:
2623 case elfcpp::R_POWERPC_UADDR32:
2624 case elfcpp::R_POWERPC_ADDR24:
2625 case elfcpp::R_POWERPC_ADDR16:
2626 case elfcpp::R_POWERPC_UADDR16:
2627 case elfcpp::R_POWERPC_ADDR16_LO:
2628 case elfcpp::R_POWERPC_ADDR16_HI:
2629 case elfcpp::R_POWERPC_ADDR16_HA:
2630 case elfcpp::R_POWERPC_ADDR14:
2631 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2632 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2633 case elfcpp::R_POWERPC_REL32:
2634 case elfcpp::R_POWERPC_REL24:
2635 case elfcpp::R_POWERPC_TPREL16:
2636 case elfcpp::R_POWERPC_TPREL16_LO:
2637 case elfcpp::R_POWERPC_TPREL16_HI:
2638 case elfcpp::R_POWERPC_TPREL16_HA:
2649 // These are the relocation types supported only on 64-bit.
2650 case elfcpp::R_PPC64_ADDR64:
2651 case elfcpp::R_PPC64_UADDR64:
2652 case elfcpp::R_PPC64_JMP_IREL:
2653 case elfcpp::R_PPC64_ADDR16_DS:
2654 case elfcpp::R_PPC64_ADDR16_LO_DS:
2655 case elfcpp::R_PPC64_ADDR16_HIGHER:
2656 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2657 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2658 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2659 case elfcpp::R_PPC64_REL64:
2660 case elfcpp::R_POWERPC_ADDR30:
2661 case elfcpp::R_PPC64_TPREL16_DS:
2662 case elfcpp::R_PPC64_TPREL16_LO_DS:
2663 case elfcpp::R_PPC64_TPREL16_HIGHER:
2664 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2665 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2666 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2677 // These are the relocation types supported only on 32-bit.
2678 // ??? glibc ld.so doesn't need to support these.
2679 case elfcpp::R_POWERPC_DTPREL16:
2680 case elfcpp::R_POWERPC_DTPREL16_LO:
2681 case elfcpp::R_POWERPC_DTPREL16_HI:
2682 case elfcpp::R_POWERPC_DTPREL16_HA:
2690 // This prevents us from issuing more than one error per reloc
2691 // section. But we can still wind up issuing more than one
2692 // error per object file.
2693 if (this->issued_non_pic_error_)
2695 gold_assert(parameters->options().output_is_position_independent());
2696 object->error(_("requires unsupported dynamic reloc; "
2697 "recompile with -fPIC"));
2698 this->issued_non_pic_error_ = true;
2702 // Return whether we need to make a PLT entry for a relocation of the
2703 // given type against a STT_GNU_IFUNC symbol.
2705 template<int size, bool big_endian>
2707 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
2708 Sized_relobj_file<size, big_endian>* object,
2709 unsigned int r_type)
2711 // In non-pic code any reference will resolve to the plt call stub
2712 // for the ifunc symbol.
2713 if (size == 32 && !parameters->options().output_is_position_independent())
2718 // Word size refs from data sections are OK.
2719 case elfcpp::R_POWERPC_ADDR32:
2720 case elfcpp::R_POWERPC_UADDR32:
2725 case elfcpp::R_PPC64_ADDR64:
2726 case elfcpp::R_PPC64_UADDR64:
2731 // GOT refs are good.
2732 case elfcpp::R_POWERPC_GOT16:
2733 case elfcpp::R_POWERPC_GOT16_LO:
2734 case elfcpp::R_POWERPC_GOT16_HI:
2735 case elfcpp::R_POWERPC_GOT16_HA:
2736 case elfcpp::R_PPC64_GOT16_DS:
2737 case elfcpp::R_PPC64_GOT16_LO_DS:
2740 // So are function calls.
2741 case elfcpp::R_POWERPC_ADDR24:
2742 case elfcpp::R_POWERPC_ADDR14:
2743 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2744 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2745 case elfcpp::R_POWERPC_REL24:
2746 case elfcpp::R_PPC_PLTREL24:
2747 case elfcpp::R_POWERPC_REL14:
2748 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2749 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2756 // Anything else is a problem.
2757 // If we are building a static executable, the libc startup function
2758 // responsible for applying indirect function relocations is going
2759 // to complain about the reloc type.
2760 // If we are building a dynamic executable, we will have a text
2761 // relocation. The dynamic loader will set the text segment
2762 // writable and non-executable to apply text relocations. So we'll
2763 // segfault when trying to run the indirection function to resolve
2765 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
2766 object->name().c_str(), r_type);
2770 // Scan a relocation for a local symbol.
2772 template<int size, bool big_endian>
2774 Target_powerpc<size, big_endian>::Scan::local(
2775 Symbol_table* symtab,
2777 Target_powerpc<size, big_endian>* target,
2778 Sized_relobj_file<size, big_endian>* object,
2779 unsigned int data_shndx,
2780 Output_section* output_section,
2781 const elfcpp::Rela<size, big_endian>& reloc,
2782 unsigned int r_type,
2783 const elfcpp::Sym<size, big_endian>& lsym,
2786 Powerpc_relobj<size, big_endian>* ppc_object
2787 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2792 && data_shndx == ppc_object->opd_shndx()
2793 && r_type == elfcpp::R_PPC64_ADDR64)
2794 ppc_object->set_opd_discard(reloc.get_r_offset());
2798 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2799 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2800 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
2801 target->make_local_ifunc_plt_entry(layout, reloc, object);
2805 case elfcpp::R_POWERPC_NONE:
2806 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2807 case elfcpp::R_POWERPC_GNU_VTENTRY:
2808 case elfcpp::R_PPC64_TOCSAVE:
2809 case elfcpp::R_PPC_EMB_MRKREF:
2810 case elfcpp::R_POWERPC_TLS:
2813 case elfcpp::R_PPC64_TOC:
2815 Output_data_got_powerpc<size, big_endian>* got
2816 = target->got_section(symtab, layout);
2817 if (parameters->options().output_is_position_independent())
2819 Address off = reloc.get_r_offset();
2821 && data_shndx == ppc_object->opd_shndx()
2822 && ppc_object->get_opd_discard(off - 8))
2825 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2826 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
2827 rela_dyn->add_output_section_relative(got->output_section(),
2828 elfcpp::R_POWERPC_RELATIVE,
2830 object, data_shndx, off,
2831 symobj->toc_base_offset());
2836 case elfcpp::R_PPC64_ADDR64:
2837 case elfcpp::R_PPC64_UADDR64:
2838 case elfcpp::R_POWERPC_ADDR32:
2839 case elfcpp::R_POWERPC_UADDR32:
2840 case elfcpp::R_POWERPC_ADDR24:
2841 case elfcpp::R_POWERPC_ADDR16:
2842 case elfcpp::R_POWERPC_ADDR16_LO:
2843 case elfcpp::R_POWERPC_ADDR16_HI:
2844 case elfcpp::R_POWERPC_ADDR16_HA:
2845 case elfcpp::R_POWERPC_UADDR16:
2846 case elfcpp::R_PPC64_ADDR16_HIGHER:
2847 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2848 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2849 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2850 case elfcpp::R_PPC64_ADDR16_DS:
2851 case elfcpp::R_PPC64_ADDR16_LO_DS:
2852 case elfcpp::R_POWERPC_ADDR14:
2853 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2854 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2855 // If building a shared library (or a position-independent
2856 // executable), we need to create a dynamic relocation for
2858 if (parameters->options().output_is_position_independent()
2859 || (size == 64 && is_ifunc))
2861 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2863 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2864 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2866 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2867 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
2870 rela_dyn = target->iplt_section()->rel_plt();
2871 dynrel = elfcpp::R_POWERPC_IRELATIVE;
2873 rela_dyn->add_local_relative(object, r_sym, dynrel,
2874 output_section, data_shndx,
2875 reloc.get_r_offset(),
2876 reloc.get_r_addend(), false);
2880 check_non_pic(object, r_type);
2881 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2882 rela_dyn->add_local(object, r_sym, r_type, output_section,
2883 data_shndx, reloc.get_r_offset(),
2884 reloc.get_r_addend());
2889 case elfcpp::R_PPC64_REL64:
2890 case elfcpp::R_POWERPC_REL32:
2891 case elfcpp::R_POWERPC_REL24:
2892 case elfcpp::R_PPC_PLTREL24:
2893 case elfcpp::R_PPC_LOCAL24PC:
2894 case elfcpp::R_POWERPC_REL16:
2895 case elfcpp::R_POWERPC_REL16_LO:
2896 case elfcpp::R_POWERPC_REL16_HI:
2897 case elfcpp::R_POWERPC_REL16_HA:
2898 case elfcpp::R_POWERPC_REL14:
2899 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2900 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2901 case elfcpp::R_POWERPC_SECTOFF:
2902 case elfcpp::R_POWERPC_TPREL16:
2903 case elfcpp::R_POWERPC_DTPREL16:
2904 case elfcpp::R_POWERPC_SECTOFF_LO:
2905 case elfcpp::R_POWERPC_TPREL16_LO:
2906 case elfcpp::R_POWERPC_DTPREL16_LO:
2907 case elfcpp::R_POWERPC_SECTOFF_HI:
2908 case elfcpp::R_POWERPC_TPREL16_HI:
2909 case elfcpp::R_POWERPC_DTPREL16_HI:
2910 case elfcpp::R_POWERPC_SECTOFF_HA:
2911 case elfcpp::R_POWERPC_TPREL16_HA:
2912 case elfcpp::R_POWERPC_DTPREL16_HA:
2913 case elfcpp::R_PPC64_DTPREL16_HIGHER:
2914 case elfcpp::R_PPC64_TPREL16_HIGHER:
2915 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2916 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2917 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2918 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2919 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2920 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2921 case elfcpp::R_PPC64_TPREL16_DS:
2922 case elfcpp::R_PPC64_TPREL16_LO_DS:
2923 case elfcpp::R_PPC64_DTPREL16_DS:
2924 case elfcpp::R_PPC64_DTPREL16_LO_DS:
2925 case elfcpp::R_PPC64_SECTOFF_DS:
2926 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2927 case elfcpp::R_PPC64_TLSGD:
2928 case elfcpp::R_PPC64_TLSLD:
2931 case elfcpp::R_POWERPC_GOT16:
2932 case elfcpp::R_POWERPC_GOT16_LO:
2933 case elfcpp::R_POWERPC_GOT16_HI:
2934 case elfcpp::R_POWERPC_GOT16_HA:
2935 case elfcpp::R_PPC64_GOT16_DS:
2936 case elfcpp::R_PPC64_GOT16_LO_DS:
2938 // The symbol requires a GOT entry.
2939 Output_data_got_powerpc<size, big_endian>* got
2940 = target->got_section(symtab, layout);
2941 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2943 if (!parameters->options().output_is_position_independent())
2945 if (size == 32 && is_ifunc)
2946 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
2948 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2950 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
2952 // If we are generating a shared object or a pie, this
2953 // symbol's GOT entry will be set by a dynamic relocation.
2955 off = got->add_constant(0);
2956 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
2958 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2959 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
2962 rela_dyn = target->iplt_section()->rel_plt();
2963 dynrel = elfcpp::R_POWERPC_IRELATIVE;
2965 rela_dyn->add_local_relative(object, r_sym, dynrel,
2966 got, off, 0, false);
2971 case elfcpp::R_PPC64_TOC16:
2972 case elfcpp::R_PPC64_TOC16_LO:
2973 case elfcpp::R_PPC64_TOC16_HI:
2974 case elfcpp::R_PPC64_TOC16_HA:
2975 case elfcpp::R_PPC64_TOC16_DS:
2976 case elfcpp::R_PPC64_TOC16_LO_DS:
2977 // We need a GOT section.
2978 target->got_section(symtab, layout);
2981 case elfcpp::R_POWERPC_GOT_TLSGD16:
2982 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2983 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2984 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2986 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
2987 if (tls_type == tls::TLSOPT_NONE)
2989 Output_data_got_powerpc<size, big_endian>* got
2990 = target->got_section(symtab, layout);
2991 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2992 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2993 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
2994 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
2996 else if (tls_type == tls::TLSOPT_TO_LE)
2998 // no GOT relocs needed for Local Exec.
3005 case elfcpp::R_POWERPC_GOT_TLSLD16:
3006 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3007 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3008 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3010 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3011 if (tls_type == tls::TLSOPT_NONE)
3012 target->tlsld_got_offset(symtab, layout, object);
3013 else if (tls_type == tls::TLSOPT_TO_LE)
3015 // no GOT relocs needed for Local Exec.
3016 if (parameters->options().emit_relocs())
3018 Output_section* os = layout->tls_segment()->first_section();
3019 gold_assert(os != NULL);
3020 os->set_needs_symtab_index();
3028 case elfcpp::R_POWERPC_GOT_DTPREL16:
3029 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3030 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3031 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3033 Output_data_got_powerpc<size, big_endian>* got
3034 = target->got_section(symtab, layout);
3035 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3036 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
3040 case elfcpp::R_POWERPC_GOT_TPREL16:
3041 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3042 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3043 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3045 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
3046 if (tls_type == tls::TLSOPT_NONE)
3048 Output_data_got_powerpc<size, big_endian>* got
3049 = target->got_section(symtab, layout);
3050 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3051 got->add_local_tls(object, r_sym, GOT_TYPE_TPREL);
3053 else if (tls_type == tls::TLSOPT_TO_LE)
3055 // no GOT relocs needed for Local Exec.
3063 unsupported_reloc_local(object, r_type);
3068 // Report an unsupported relocation against a global symbol.
3070 template<int size, bool big_endian>
3072 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
3073 Sized_relobj_file<size, big_endian>* object,
3074 unsigned int r_type,
3077 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3078 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3081 // Scan a relocation for a global symbol.
3083 template<int size, bool big_endian>
3085 Target_powerpc<size, big_endian>::Scan::global(
3086 Symbol_table* symtab,
3088 Target_powerpc<size, big_endian>* target,
3089 Sized_relobj_file<size, big_endian>* object,
3090 unsigned int data_shndx,
3091 Output_section* output_section,
3092 const elfcpp::Rela<size, big_endian>& reloc,
3093 unsigned int r_type,
3096 Powerpc_relobj<size, big_endian>* ppc_object
3097 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3099 // A STT_GNU_IFUNC symbol may require a PLT entry.
3100 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3101 && this->reloc_needs_plt_for_ifunc(object, r_type))
3102 target->make_plt_entry(layout, gsym, reloc, object);
3106 case elfcpp::R_POWERPC_NONE:
3107 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3108 case elfcpp::R_POWERPC_GNU_VTENTRY:
3109 case elfcpp::R_PPC_LOCAL24PC:
3110 case elfcpp::R_PPC_EMB_MRKREF:
3111 case elfcpp::R_POWERPC_TLS:
3114 case elfcpp::R_PPC64_TOC:
3116 Output_data_got_powerpc<size, big_endian>* got
3117 = target->got_section(symtab, layout);
3118 if (parameters->options().output_is_position_independent())
3120 Address off = reloc.get_r_offset();
3122 && data_shndx == ppc_object->opd_shndx()
3123 && ppc_object->get_opd_discard(off - 8))
3126 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3127 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
3128 if (data_shndx != ppc_object->opd_shndx())
3129 symobj = static_cast
3130 <Powerpc_relobj<size, big_endian>*>(gsym->object());
3131 rela_dyn->add_output_section_relative(got->output_section(),
3132 elfcpp::R_POWERPC_RELATIVE,
3134 object, data_shndx, off,
3135 symobj->toc_base_offset());
3140 case elfcpp::R_PPC64_ADDR64:
3142 && data_shndx == ppc_object->opd_shndx()
3143 && (gsym->is_defined_in_discarded_section()
3144 || gsym->object() != object))
3146 ppc_object->set_opd_discard(reloc.get_r_offset());
3150 case elfcpp::R_PPC64_UADDR64:
3151 case elfcpp::R_POWERPC_ADDR32:
3152 case elfcpp::R_POWERPC_UADDR32:
3153 case elfcpp::R_POWERPC_ADDR24:
3154 case elfcpp::R_POWERPC_ADDR16:
3155 case elfcpp::R_POWERPC_ADDR16_LO:
3156 case elfcpp::R_POWERPC_ADDR16_HI:
3157 case elfcpp::R_POWERPC_ADDR16_HA:
3158 case elfcpp::R_POWERPC_UADDR16:
3159 case elfcpp::R_PPC64_ADDR16_HIGHER:
3160 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3161 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3162 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3163 case elfcpp::R_PPC64_ADDR16_DS:
3164 case elfcpp::R_PPC64_ADDR16_LO_DS:
3165 case elfcpp::R_POWERPC_ADDR14:
3166 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3167 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3169 // Make a PLT entry if necessary.
3170 if (gsym->needs_plt_entry())
3172 target->make_plt_entry(layout, gsym, reloc, 0);
3173 // Since this is not a PC-relative relocation, we may be
3174 // taking the address of a function. In that case we need to
3175 // set the entry in the dynamic symbol table to the address of
3176 // the PLT call stub.
3178 && gsym->is_from_dynobj()
3179 && !parameters->options().output_is_position_independent())
3180 gsym->set_needs_dynsym_value();
3182 // Make a dynamic relocation if necessary.
3183 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
3184 || (size == 64 && gsym->type() == elfcpp::STT_GNU_IFUNC))
3186 if (gsym->may_need_copy_reloc())
3188 target->copy_reloc(symtab, layout, object,
3189 data_shndx, output_section, gsym, reloc);
3191 else if (((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
3192 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
3193 && (gsym->can_use_relative_reloc(false)
3195 && data_shndx == ppc_object->opd_shndx())))
3197 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3198 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3199 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3201 rela_dyn = target->iplt_section()->rel_plt();
3202 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3204 rela_dyn->add_symbolless_global_addend(
3205 gsym, dynrel, output_section, object, data_shndx,
3206 reloc.get_r_offset(), reloc.get_r_addend());
3210 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3211 check_non_pic(object, r_type);
3212 rela_dyn->add_global(gsym, r_type, output_section,
3214 reloc.get_r_offset(),
3215 reloc.get_r_addend());
3221 case elfcpp::R_PPC_PLTREL24:
3222 case elfcpp::R_POWERPC_REL24:
3223 if (gsym->needs_plt_entry()
3224 || (!gsym->final_value_is_known()
3225 && (gsym->is_undefined()
3226 || gsym->is_from_dynobj()
3227 || gsym->is_preemptible())))
3228 target->make_plt_entry(layout, gsym, reloc, object);
3231 case elfcpp::R_PPC64_REL64:
3232 case elfcpp::R_POWERPC_REL32:
3233 // Make a dynamic relocation if necessary.
3234 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
3236 if (gsym->may_need_copy_reloc())
3238 target->copy_reloc(symtab, layout, object,
3239 data_shndx, output_section, gsym,
3244 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3245 check_non_pic(object, r_type);
3246 rela_dyn->add_global(gsym, r_type, output_section, object,
3247 data_shndx, reloc.get_r_offset(),
3248 reloc.get_r_addend());
3253 case elfcpp::R_POWERPC_REL16:
3254 case elfcpp::R_POWERPC_REL16_LO:
3255 case elfcpp::R_POWERPC_REL16_HI:
3256 case elfcpp::R_POWERPC_REL16_HA:
3257 case elfcpp::R_POWERPC_REL14:
3258 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3259 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3260 case elfcpp::R_POWERPC_SECTOFF:
3261 case elfcpp::R_POWERPC_TPREL16:
3262 case elfcpp::R_POWERPC_DTPREL16:
3263 case elfcpp::R_POWERPC_SECTOFF_LO:
3264 case elfcpp::R_POWERPC_TPREL16_LO:
3265 case elfcpp::R_POWERPC_DTPREL16_LO:
3266 case elfcpp::R_POWERPC_SECTOFF_HI:
3267 case elfcpp::R_POWERPC_TPREL16_HI:
3268 case elfcpp::R_POWERPC_DTPREL16_HI:
3269 case elfcpp::R_POWERPC_SECTOFF_HA:
3270 case elfcpp::R_POWERPC_TPREL16_HA:
3271 case elfcpp::R_POWERPC_DTPREL16_HA:
3272 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3273 case elfcpp::R_PPC64_TPREL16_HIGHER:
3274 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3275 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3276 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3277 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3278 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3279 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3280 case elfcpp::R_PPC64_TPREL16_DS:
3281 case elfcpp::R_PPC64_TPREL16_LO_DS:
3282 case elfcpp::R_PPC64_DTPREL16_DS:
3283 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3284 case elfcpp::R_PPC64_SECTOFF_DS:
3285 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3286 case elfcpp::R_PPC64_TLSGD:
3287 case elfcpp::R_PPC64_TLSLD:
3290 case elfcpp::R_POWERPC_GOT16:
3291 case elfcpp::R_POWERPC_GOT16_LO:
3292 case elfcpp::R_POWERPC_GOT16_HI:
3293 case elfcpp::R_POWERPC_GOT16_HA:
3294 case elfcpp::R_PPC64_GOT16_DS:
3295 case elfcpp::R_PPC64_GOT16_LO_DS:
3297 // The symbol requires a GOT entry.
3298 Output_data_got_powerpc<size, big_endian>* got;
3300 got = target->got_section(symtab, layout);
3301 if (gsym->final_value_is_known())
3303 if (size == 32 && gsym->type() == elfcpp::STT_GNU_IFUNC)
3304 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3306 got->add_global(gsym, GOT_TYPE_STANDARD);
3308 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
3310 // If we are generating a shared object or a pie, this
3311 // symbol's GOT entry will be set by a dynamic relocation.
3312 unsigned int off = got->add_constant(0);
3313 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
3315 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3316 if (gsym->can_use_relative_reloc(false)
3318 && gsym->visibility() == elfcpp::STV_PROTECTED
3319 && parameters->options().shared()))
3321 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3322 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3324 rela_dyn = target->iplt_section()->rel_plt();
3325 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3327 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
3331 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
3332 rela_dyn->add_global(gsym, dynrel, got, off, 0);
3338 case elfcpp::R_PPC64_TOC16:
3339 case elfcpp::R_PPC64_TOC16_LO:
3340 case elfcpp::R_PPC64_TOC16_HI:
3341 case elfcpp::R_PPC64_TOC16_HA:
3342 case elfcpp::R_PPC64_TOC16_DS:
3343 case elfcpp::R_PPC64_TOC16_LO_DS:
3344 // We need a GOT section.
3345 target->got_section(symtab, layout);
3348 case elfcpp::R_POWERPC_GOT_TLSGD16:
3349 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3350 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3351 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3353 const bool final = gsym->final_value_is_known();
3354 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3355 if (tls_type == tls::TLSOPT_NONE)
3357 Output_data_got_powerpc<size, big_endian>* got
3358 = target->got_section(symtab, layout);
3359 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
3360 target->rela_dyn_section(layout),
3361 elfcpp::R_POWERPC_DTPMOD,
3362 elfcpp::R_POWERPC_DTPREL);
3364 else if (tls_type == tls::TLSOPT_TO_IE)
3366 Output_data_got_powerpc<size, big_endian>* got
3367 = target->got_section(symtab, layout);
3368 got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
3369 target->rela_dyn_section(layout),
3370 elfcpp::R_POWERPC_TPREL);
3372 else if (tls_type == tls::TLSOPT_TO_LE)
3374 // no GOT relocs needed for Local Exec.
3381 case elfcpp::R_POWERPC_GOT_TLSLD16:
3382 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3383 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3384 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3386 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3387 if (tls_type == tls::TLSOPT_NONE)
3388 target->tlsld_got_offset(symtab, layout, object);
3389 else if (tls_type == tls::TLSOPT_TO_LE)
3391 // no GOT relocs needed for Local Exec.
3392 if (parameters->options().emit_relocs())
3394 Output_section* os = layout->tls_segment()->first_section();
3395 gold_assert(os != NULL);
3396 os->set_needs_symtab_index();
3404 case elfcpp::R_POWERPC_GOT_DTPREL16:
3405 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3406 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3407 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3409 Output_data_got_powerpc<size, big_endian>* got
3410 = target->got_section(symtab, layout);
3411 if (!gsym->final_value_is_known()
3412 && (gsym->is_from_dynobj()
3413 || gsym->is_undefined()
3414 || gsym->is_preemptible()))
3415 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
3416 target->rela_dyn_section(layout),
3417 elfcpp::R_POWERPC_DTPREL);
3419 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
3423 case elfcpp::R_POWERPC_GOT_TPREL16:
3424 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3425 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3426 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3428 const bool final = gsym->final_value_is_known();
3429 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3430 if (tls_type == tls::TLSOPT_NONE)
3432 Output_data_got_powerpc<size, big_endian>* got
3433 = target->got_section(symtab, layout);
3434 if (!gsym->final_value_is_known()
3435 && (gsym->is_from_dynobj()
3436 || gsym->is_undefined()
3437 || gsym->is_preemptible()))
3438 got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
3439 target->rela_dyn_section(layout),
3440 elfcpp::R_POWERPC_TPREL);
3442 got->add_global_tls(gsym, GOT_TYPE_TPREL);
3444 else if (tls_type == tls::TLSOPT_TO_LE)
3446 // no GOT relocs needed for Local Exec.
3454 unsupported_reloc_global(object, r_type, gsym);
3459 // Process relocations for gc.
3461 template<int size, bool big_endian>
3463 Target_powerpc<size, big_endian>::gc_process_relocs(
3464 Symbol_table* symtab,
3466 Sized_relobj_file<size, big_endian>* object,
3467 unsigned int data_shndx,
3469 const unsigned char* prelocs,
3471 Output_section* output_section,
3472 bool needs_special_offset_handling,
3473 size_t local_symbol_count,
3474 const unsigned char* plocal_symbols)
3476 typedef Target_powerpc<size, big_endian> Powerpc;
3477 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
3478 Powerpc_relobj<size, big_endian>* ppc_object
3479 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3481 ppc_object->set_opd_valid();
3482 if (size == 64 && data_shndx == ppc_object->opd_shndx())
3484 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
3485 for (p = ppc_object->access_from_map()->begin();
3486 p != ppc_object->access_from_map()->end();
3489 Address dst_off = p->first;
3490 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
3491 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
3492 for (s = p->second.begin(); s != p->second.end(); ++s)
3494 Object* src_obj = s->first;
3495 unsigned int src_indx = s->second;
3496 symtab->gc()->add_reference(src_obj, src_indx,
3497 ppc_object, dst_indx);
3501 ppc_object->access_from_map()->clear();
3502 // Don't look at .opd relocs as .opd will reference everything.
3506 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
3507 typename Target_powerpc::Relocatable_size_for_reloc>(
3516 needs_special_offset_handling,
3521 // Handle target specific gc actions when adding a gc reference from
3522 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
3523 // and DST_OFF. For powerpc64, this adds a referenc to the code
3524 // section of a function descriptor.
3526 template<int size, bool big_endian>
3528 Target_powerpc<size, big_endian>::do_gc_add_reference(
3529 Symbol_table* symtab,
3531 unsigned int src_shndx,
3533 unsigned int dst_shndx,
3534 Address dst_off) const
3536 Powerpc_relobj<size, big_endian>* ppc_object
3537 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
3538 if (size == 64 && dst_shndx == ppc_object->opd_shndx())
3540 if (ppc_object->opd_valid())
3542 dst_shndx = ppc_object->get_opd_ent(dst_off);
3543 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
3547 // If we haven't run scan_opd_relocs, we must delay
3548 // processing this function descriptor reference.
3549 ppc_object->add_reference(src_obj, src_shndx, dst_off);
3554 // Add any special sections for this symbol to the gc work list.
3555 // For powerpc64, this adds the code section of a function
3558 template<int size, bool big_endian>
3560 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
3561 Symbol_table* symtab,
3566 Powerpc_relobj<size, big_endian>* ppc_object
3567 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
3569 unsigned int shndx = sym->shndx(&is_ordinary);
3570 if (is_ordinary && shndx == ppc_object->opd_shndx())
3572 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
3573 Address dst_off = gsym->value();
3574 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
3575 symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
3580 // Scan relocations for a section.
3582 template<int size, bool big_endian>
3584 Target_powerpc<size, big_endian>::scan_relocs(
3585 Symbol_table* symtab,
3587 Sized_relobj_file<size, big_endian>* object,
3588 unsigned int data_shndx,
3589 unsigned int sh_type,
3590 const unsigned char* prelocs,
3592 Output_section* output_section,
3593 bool needs_special_offset_handling,
3594 size_t local_symbol_count,
3595 const unsigned char* plocal_symbols)
3597 typedef Target_powerpc<size, big_endian> Powerpc;
3598 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
3600 if (sh_type == elfcpp::SHT_REL)
3602 gold_error(_("%s: unsupported REL reloc section"),
3603 object->name().c_str());
3609 static Output_data_space* sdata;
3611 // Define _SDA_BASE_ at the start of the .sdata section.
3614 // layout->find_output_section(".sdata") == NULL
3615 sdata = new Output_data_space(4, "** sdata");
3617 = layout->add_output_section_data(".sdata", 0,
3619 | elfcpp::SHF_WRITE,
3620 sdata, ORDER_SMALL_DATA, false);
3621 symtab->define_in_output_data("_SDA_BASE_", NULL,
3622 Symbol_table::PREDEFINED,
3623 os, 32768, 0, elfcpp::STT_OBJECT,
3624 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
3629 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
3638 needs_special_offset_handling,
3643 // Functor class for processing the global symbol table.
3644 // Removes symbols defined on discarded opd entries.
3646 template<bool big_endian>
3647 class Global_symbol_visitor_opd
3650 Global_symbol_visitor_opd()
3654 operator()(Sized_symbol<64>* sym)
3656 if (sym->has_symtab_index()
3657 || sym->source() != Symbol::FROM_OBJECT
3658 || !sym->in_real_elf())
3661 Powerpc_relobj<64, big_endian>* symobj
3662 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
3663 if (symobj->is_dynamic()
3664 || symobj->opd_shndx() == 0)
3668 unsigned int shndx = sym->shndx(&is_ordinary);
3669 if (shndx == symobj->opd_shndx()
3670 && symobj->get_opd_discard(sym->value()))
3671 sym->set_symtab_index(-1U);
3675 // Finalize the sections.
3677 template<int size, bool big_endian>
3679 Target_powerpc<size, big_endian>::do_finalize_sections(
3681 const Input_objects*,
3682 Symbol_table* symtab)
3684 if (parameters->doing_static_link())
3686 // At least some versions of glibc elf-init.o have a strong
3687 // reference to __rela_iplt marker syms. A weak ref would be
3689 if (this->iplt_ != NULL)
3691 Reloc_section* rel = this->iplt_->rel_plt();
3692 symtab->define_in_output_data("__rela_iplt_start", NULL,
3693 Symbol_table::PREDEFINED, rel, 0, 0,
3694 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
3695 elfcpp::STV_HIDDEN, 0, false, true);
3696 symtab->define_in_output_data("__rela_iplt_end", NULL,
3697 Symbol_table::PREDEFINED, rel, 0, 0,
3698 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
3699 elfcpp::STV_HIDDEN, 0, true, true);
3703 symtab->define_as_constant("__rela_iplt_start", NULL,
3704 Symbol_table::PREDEFINED, 0, 0,
3705 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
3706 elfcpp::STV_HIDDEN, 0, true, false);
3707 symtab->define_as_constant("__rela_iplt_end", NULL,
3708 Symbol_table::PREDEFINED, 0, 0,
3709 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
3710 elfcpp::STV_HIDDEN, 0, true, false);
3716 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
3717 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
3720 // Fill in some more dynamic tags.
3721 Output_data_dynamic* odyn = layout->dynamic_data();
3724 const Reloc_section* rel_plt = (this->plt_ == NULL
3726 : this->plt_->rel_plt());
3727 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
3728 this->rela_dyn_, true, size == 32);
3732 if (this->got_ != NULL)
3734 this->got_->finalize_data_size();
3735 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
3736 this->got_, this->got_->g_o_t());
3741 if (this->glink_ != NULL)
3743 this->glink_->finalize_data_size();
3744 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
3746 (this->glink_->pltresolve()
3747 + this->glink_->pltresolve_size
3753 // Emit any relocs we saved in an attempt to avoid generating COPY
3755 if (this->copy_relocs_.any_saved_relocs())
3756 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3759 // Return the value to use for a branch relocation.
3761 template<int size, bool big_endian>
3762 typename elfcpp::Elf_types<size>::Elf_Addr
3763 Target_powerpc<size, big_endian>::symval_for_branch(
3765 const Sized_symbol<size>* gsym,
3766 Powerpc_relobj<size, big_endian>* object,
3767 unsigned int *dest_shndx)
3773 // If the symbol is defined in an opd section, ie. is a function
3774 // descriptor, use the function descriptor code entry address
3775 Powerpc_relobj<size, big_endian>* symobj = object;
3777 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
3778 unsigned int shndx = symobj->opd_shndx();
3781 Address opd_addr = symobj->get_output_section_offset(shndx);
3782 gold_assert(opd_addr != invalid_address);
3783 opd_addr += symobj->output_section(shndx)->address();
3784 if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
3787 *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
3788 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
3789 gold_assert(sec_addr != invalid_address);
3790 sec_addr += symobj->output_section(*dest_shndx)->address();
3791 value = sec_addr + sec_off;
3796 // Perform a relocation.
3798 template<int size, bool big_endian>
3800 Target_powerpc<size, big_endian>::Relocate::relocate(
3801 const Relocate_info<size, big_endian>* relinfo,
3802 Target_powerpc* target,
3805 const elfcpp::Rela<size, big_endian>& rela,
3806 unsigned int r_type,
3807 const Sized_symbol<size>* gsym,
3808 const Symbol_value<size>* psymval,
3809 unsigned char* view,
3811 section_size_type view_size)
3814 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
3815 || r_type == elfcpp::R_PPC_PLTREL24)
3817 && strcmp(gsym->name(), "__tls_get_addr") == 0);
3818 enum skip_tls last_tls = this->call_tls_get_addr_;
3819 this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
3822 if (last_tls == CALL_NOT_EXPECTED)
3823 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3824 _("__tls_get_addr call lacks marker reloc"));
3825 else if (last_tls == CALL_SKIP)
3828 else if (last_tls != CALL_NOT_EXPECTED)
3829 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3830 _("missing expected __tls_get_addr call"));
3832 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
3833 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
3834 Powerpc_relobj<size, big_endian>* const object
3835 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
3837 bool has_plt_value = false;
3838 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3840 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
3841 : object->local_has_plt_offset(r_sym))
3843 const Output_data_glink<size, big_endian>* glink
3844 = target->glink_section();
3845 unsigned int glink_index;
3847 glink_index = glink->find_entry(object, gsym, rela);
3849 glink_index = glink->find_entry(object, r_sym, rela);
3850 value = glink->address() + glink_index * glink->glink_entry_size();
3851 has_plt_value = true;
3854 if (r_type == elfcpp::R_POWERPC_GOT16
3855 || r_type == elfcpp::R_POWERPC_GOT16_LO
3856 || r_type == elfcpp::R_POWERPC_GOT16_HI
3857 || r_type == elfcpp::R_POWERPC_GOT16_HA
3858 || r_type == elfcpp::R_PPC64_GOT16_DS
3859 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
3863 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3864 value = gsym->got_offset(GOT_TYPE_STANDARD);
3868 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3869 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3870 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3872 value -= target->got_section()->got_base_offset(object);
3874 else if (r_type == elfcpp::R_PPC64_TOC)
3876 value = (target->got_section()->output_section()->address()
3877 + object->toc_base_offset());
3879 else if (gsym != NULL
3880 && (r_type == elfcpp::R_POWERPC_REL24
3881 || r_type == elfcpp::R_PPC_PLTREL24)
3886 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3887 Valtype* wv = reinterpret_cast<Valtype*>(view);
3888 bool can_plt_call = false;
3889 if (rela.get_r_offset() + 8 <= view_size)
3891 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3892 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3895 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
3897 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
3898 can_plt_call = true;
3903 // If we don't have a branch and link followed by a nop,
3904 // we can't go via the plt because there is no place to
3905 // put a toc restoring instruction.
3906 // Unless we know we won't be returning.
3907 if (strcmp(gsym->name(), "__libc_start_main") == 0)
3908 can_plt_call = true;
3912 // This is not an error in one special case: A self
3913 // call. It isn't possible to cheaply verify we have
3914 // such a call so just check for a call to the same
3917 Address code = value;
3918 if (gsym->source() == Symbol::FROM_OBJECT
3919 && gsym->object() == object)
3921 Address addend = rela.get_r_addend();
3922 unsigned int dest_shndx;
3923 Address opdent = psymval->value(object, addend);
3924 code = target->symval_for_branch(opdent, gsym, object,
3927 if (dest_shndx == 0)
3928 dest_shndx = gsym->shndx(&is_ordinary);
3929 ok = dest_shndx == relinfo->data_shndx;
3933 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3934 _("call lacks nop, can't restore toc; "
3935 "recompile with -fPIC"));
3941 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3942 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
3943 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
3944 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
3946 // First instruction of a global dynamic sequence, arg setup insn.
3947 const bool final = gsym == NULL || gsym->final_value_is_known();
3948 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3949 enum Got_type got_type = GOT_TYPE_STANDARD;
3950 if (tls_type == tls::TLSOPT_NONE)
3951 got_type = GOT_TYPE_TLSGD;
3952 else if (tls_type == tls::TLSOPT_TO_IE)
3953 got_type = GOT_TYPE_TPREL;
3954 if (got_type != GOT_TYPE_STANDARD)
3958 gold_assert(gsym->has_got_offset(got_type));
3959 value = gsym->got_offset(got_type);
3963 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3964 gold_assert(object->local_has_got_offset(r_sym, got_type));
3965 value = object->local_got_offset(r_sym, got_type);
3967 value -= target->got_section()->got_base_offset(object);
3969 if (tls_type == tls::TLSOPT_TO_IE)
3971 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3972 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3974 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3975 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3976 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
3978 insn |= 32 << 26; // lwz
3980 insn |= 58 << 26; // ld
3981 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3983 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
3984 - elfcpp::R_POWERPC_GOT_TLSGD16);
3986 else if (tls_type == tls::TLSOPT_TO_LE)
3988 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3989 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3991 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3992 Insn insn = addis_3_13;
3995 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3996 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3997 value = psymval->value(object, rela.get_r_addend());
4001 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4003 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4004 r_type = elfcpp::R_POWERPC_NONE;
4008 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4009 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
4010 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
4011 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
4013 // First instruction of a local dynamic sequence, arg setup insn.
4014 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4015 if (tls_type == tls::TLSOPT_NONE)
4017 value = target->tlsld_got_offset();
4018 value -= target->got_section()->got_base_offset(object);
4022 gold_assert(tls_type == tls::TLSOPT_TO_LE);
4023 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4024 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
4026 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4027 Insn insn = addis_3_13;
4030 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4031 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4036 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4038 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4039 r_type = elfcpp::R_POWERPC_NONE;
4043 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
4044 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
4045 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
4046 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
4048 // Accesses relative to a local dynamic sequence address,
4049 // no optimisation here.
4052 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
4053 value = gsym->got_offset(GOT_TYPE_DTPREL);
4057 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4058 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
4059 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
4061 value -= target->got_section()->got_base_offset(object);
4063 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4064 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
4065 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
4066 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
4068 // First instruction of initial exec sequence.
4069 const bool final = gsym == NULL || gsym->final_value_is_known();
4070 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
4071 if (tls_type == tls::TLSOPT_NONE)
4075 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
4076 value = gsym->got_offset(GOT_TYPE_TPREL);
4080 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4081 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
4082 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
4084 value -= target->got_section()->got_base_offset(object);
4088 gold_assert(tls_type == tls::TLSOPT_TO_LE);
4089 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4090 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
4092 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4093 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4094 insn &= (1 << 26) - (1 << 21); // extract rt from ld
4099 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4100 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4101 value = psymval->value(object, rela.get_r_addend());
4105 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4107 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4108 r_type = elfcpp::R_POWERPC_NONE;
4112 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4113 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4115 // Second instruction of a global dynamic sequence,
4116 // the __tls_get_addr call
4117 this->call_tls_get_addr_ = CALL_EXPECTED;
4118 const bool final = gsym == NULL || gsym->final_value_is_known();
4119 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
4120 if (tls_type != tls::TLSOPT_NONE)
4122 if (tls_type == tls::TLSOPT_TO_IE)
4124 Insn* iview = reinterpret_cast<Insn*>(view);
4125 Insn insn = add_3_3_13;
4128 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4129 r_type = elfcpp::R_POWERPC_NONE;
4133 Insn* iview = reinterpret_cast<Insn*>(view);
4134 Insn insn = addi_3_3;
4135 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4136 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4137 view += 2 * big_endian;
4138 value = psymval->value(object, rela.get_r_addend());
4140 this->call_tls_get_addr_ = CALL_SKIP;
4143 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4144 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4146 // Second instruction of a local dynamic sequence,
4147 // the __tls_get_addr call
4148 this->call_tls_get_addr_ = CALL_EXPECTED;
4149 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4150 if (tls_type == tls::TLSOPT_TO_LE)
4152 Insn* iview = reinterpret_cast<Insn*>(view);
4153 Insn insn = addi_3_3;
4154 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4155 this->call_tls_get_addr_ = CALL_SKIP;
4156 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4157 view += 2 * big_endian;
4161 else if (r_type == elfcpp::R_POWERPC_TLS)
4163 // Second instruction of an initial exec sequence
4164 const bool final = gsym == NULL || gsym->final_value_is_known();
4165 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
4166 if (tls_type == tls::TLSOPT_TO_LE)
4168 Insn* iview = reinterpret_cast<Insn*>(view);
4169 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4170 unsigned int reg = size == 32 ? 2 : 13;
4171 insn = at_tls_transform(insn, reg);
4172 gold_assert(insn != 0);
4173 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4174 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4175 view += 2 * big_endian;
4176 value = psymval->value(object, rela.get_r_addend());
4179 else if (!has_plt_value)
4182 unsigned int dest_shndx;
4183 if (r_type != elfcpp::R_PPC_PLTREL24)
4184 addend = rela.get_r_addend();
4185 value = psymval->value(object, addend);
4186 if (size == 64 && is_branch_reloc(r_type))
4187 value = target->symval_for_branch(value, gsym, object, &dest_shndx);
4192 case elfcpp::R_PPC64_REL64:
4193 case elfcpp::R_POWERPC_REL32:
4194 case elfcpp::R_POWERPC_REL24:
4195 case elfcpp::R_PPC_PLTREL24:
4196 case elfcpp::R_PPC_LOCAL24PC:
4197 case elfcpp::R_POWERPC_REL16:
4198 case elfcpp::R_POWERPC_REL16_LO:
4199 case elfcpp::R_POWERPC_REL16_HI:
4200 case elfcpp::R_POWERPC_REL16_HA:
4201 case elfcpp::R_POWERPC_REL14:
4202 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4203 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4207 case elfcpp::R_PPC64_TOC16:
4208 case elfcpp::R_PPC64_TOC16_LO:
4209 case elfcpp::R_PPC64_TOC16_HI:
4210 case elfcpp::R_PPC64_TOC16_HA:
4211 case elfcpp::R_PPC64_TOC16_DS:
4212 case elfcpp::R_PPC64_TOC16_LO_DS:
4213 // Subtract the TOC base address.
4214 value -= (target->got_section()->output_section()->address()
4215 + object->toc_base_offset());
4218 case elfcpp::R_POWERPC_SECTOFF:
4219 case elfcpp::R_POWERPC_SECTOFF_LO:
4220 case elfcpp::R_POWERPC_SECTOFF_HI:
4221 case elfcpp::R_POWERPC_SECTOFF_HA:
4222 case elfcpp::R_PPC64_SECTOFF_DS:
4223 case elfcpp::R_PPC64_SECTOFF_LO_DS:
4225 value -= os->address();
4228 case elfcpp::R_PPC64_TPREL16_DS:
4229 case elfcpp::R_PPC64_TPREL16_LO_DS:
4231 // R_PPC_TLSGD and R_PPC_TLSLD
4233 case elfcpp::R_POWERPC_TPREL16:
4234 case elfcpp::R_POWERPC_TPREL16_LO:
4235 case elfcpp::R_POWERPC_TPREL16_HI:
4236 case elfcpp::R_POWERPC_TPREL16_HA:
4237 case elfcpp::R_POWERPC_TPREL:
4238 case elfcpp::R_PPC64_TPREL16_HIGHER:
4239 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4240 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4241 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4242 // tls symbol values are relative to tls_segment()->vaddr()
4246 case elfcpp::R_PPC64_DTPREL16_DS:
4247 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4248 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4249 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4250 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4251 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4253 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
4254 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
4256 case elfcpp::R_POWERPC_DTPREL16:
4257 case elfcpp::R_POWERPC_DTPREL16_LO:
4258 case elfcpp::R_POWERPC_DTPREL16_HI:
4259 case elfcpp::R_POWERPC_DTPREL16_HA:
4260 case elfcpp::R_POWERPC_DTPREL:
4261 // tls symbol values are relative to tls_segment()->vaddr()
4262 value -= dtp_offset;
4269 Insn branch_bit = 0;
4272 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4273 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4274 branch_bit = 1 << 21;
4275 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4276 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4278 Insn* iview = reinterpret_cast<Insn*>(view);
4279 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4282 if (this->is_isa_v2)
4284 // Set 'a' bit. This is 0b00010 in BO field for branch
4285 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
4286 // for branch on CTR insns (BO == 1a00t or 1a01t).
4287 if ((insn & (0x14 << 21)) == (0x04 << 21))
4289 else if ((insn & (0x14 << 21)) == (0x10 << 21))
4296 // Invert 'y' bit if not the default.
4297 if (static_cast<Signed_address>(value) < 0)
4300 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4308 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
4311 case elfcpp::R_POWERPC_ADDR32:
4312 case elfcpp::R_POWERPC_UADDR32:
4314 overflow = Reloc::CHECK_BITFIELD;
4317 case elfcpp::R_POWERPC_REL32:
4319 overflow = Reloc::CHECK_SIGNED;
4322 case elfcpp::R_POWERPC_ADDR24:
4323 case elfcpp::R_POWERPC_ADDR16:
4324 case elfcpp::R_POWERPC_UADDR16:
4325 case elfcpp::R_PPC64_ADDR16_DS:
4326 case elfcpp::R_POWERPC_ADDR14:
4327 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4328 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4329 overflow = Reloc::CHECK_BITFIELD;
4332 case elfcpp::R_POWERPC_REL24:
4333 case elfcpp::R_PPC_PLTREL24:
4334 case elfcpp::R_PPC_LOCAL24PC:
4335 case elfcpp::R_POWERPC_REL16:
4336 case elfcpp::R_PPC64_TOC16:
4337 case elfcpp::R_POWERPC_GOT16:
4338 case elfcpp::R_POWERPC_SECTOFF:
4339 case elfcpp::R_POWERPC_TPREL16:
4340 case elfcpp::R_POWERPC_DTPREL16:
4341 case elfcpp::R_PPC64_TPREL16_DS:
4342 case elfcpp::R_PPC64_DTPREL16_DS:
4343 case elfcpp::R_PPC64_TOC16_DS:
4344 case elfcpp::R_PPC64_GOT16_DS:
4345 case elfcpp::R_PPC64_SECTOFF_DS:
4346 case elfcpp::R_POWERPC_REL14:
4347 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4348 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4349 case elfcpp::R_POWERPC_GOT_TLSGD16:
4350 case elfcpp::R_POWERPC_GOT_TLSLD16:
4351 case elfcpp::R_POWERPC_GOT_TPREL16:
4352 case elfcpp::R_POWERPC_GOT_DTPREL16:
4353 overflow = Reloc::CHECK_SIGNED;
4357 typename Powerpc_relocate_functions<size, big_endian>::Status status
4358 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
4361 case elfcpp::R_POWERPC_NONE:
4362 case elfcpp::R_POWERPC_TLS:
4363 case elfcpp::R_POWERPC_GNU_VTINHERIT:
4364 case elfcpp::R_POWERPC_GNU_VTENTRY:
4365 case elfcpp::R_PPC_EMB_MRKREF:
4368 case elfcpp::R_PPC64_ADDR64:
4369 case elfcpp::R_PPC64_REL64:
4370 case elfcpp::R_PPC64_TOC:
4371 Reloc::addr64(view, value);
4374 case elfcpp::R_POWERPC_TPREL:
4375 case elfcpp::R_POWERPC_DTPREL:
4377 Reloc::addr64(view, value);
4379 status = Reloc::addr32(view, value, overflow);
4382 case elfcpp::R_PPC64_UADDR64:
4383 Reloc::addr64_u(view, value);
4386 case elfcpp::R_POWERPC_ADDR32:
4387 case elfcpp::R_POWERPC_REL32:
4388 status = Reloc::addr32(view, value, overflow);
4391 case elfcpp::R_POWERPC_UADDR32:
4392 status = Reloc::addr32_u(view, value, overflow);
4395 case elfcpp::R_POWERPC_ADDR24:
4396 case elfcpp::R_POWERPC_REL24:
4397 case elfcpp::R_PPC_PLTREL24:
4398 case elfcpp::R_PPC_LOCAL24PC:
4399 status = Reloc::addr24(view, value, overflow);
4402 case elfcpp::R_POWERPC_GOT_DTPREL16:
4403 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
4406 status = Reloc::addr16_ds(view, value, overflow);
4409 case elfcpp::R_POWERPC_ADDR16:
4410 case elfcpp::R_POWERPC_REL16:
4411 case elfcpp::R_PPC64_TOC16:
4412 case elfcpp::R_POWERPC_GOT16:
4413 case elfcpp::R_POWERPC_SECTOFF:
4414 case elfcpp::R_POWERPC_TPREL16:
4415 case elfcpp::R_POWERPC_DTPREL16:
4416 case elfcpp::R_POWERPC_GOT_TLSGD16:
4417 case elfcpp::R_POWERPC_GOT_TLSLD16:
4418 case elfcpp::R_POWERPC_GOT_TPREL16:
4419 case elfcpp::R_POWERPC_ADDR16_LO:
4420 case elfcpp::R_POWERPC_REL16_LO:
4421 case elfcpp::R_PPC64_TOC16_LO:
4422 case elfcpp::R_POWERPC_GOT16_LO:
4423 case elfcpp::R_POWERPC_SECTOFF_LO:
4424 case elfcpp::R_POWERPC_TPREL16_LO:
4425 case elfcpp::R_POWERPC_DTPREL16_LO:
4426 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
4427 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
4428 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
4429 status = Reloc::addr16(view, value, overflow);
4432 case elfcpp::R_POWERPC_UADDR16:
4433 status = Reloc::addr16_u(view, value, overflow);
4436 case elfcpp::R_POWERPC_ADDR16_HI:
4437 case elfcpp::R_POWERPC_REL16_HI:
4438 case elfcpp::R_PPC64_TOC16_HI:
4439 case elfcpp::R_POWERPC_GOT16_HI:
4440 case elfcpp::R_POWERPC_SECTOFF_HI:
4441 case elfcpp::R_POWERPC_TPREL16_HI:
4442 case elfcpp::R_POWERPC_DTPREL16_HI:
4443 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
4444 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
4445 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
4446 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
4447 Reloc::addr16_hi(view, value);
4450 case elfcpp::R_POWERPC_ADDR16_HA:
4451 case elfcpp::R_POWERPC_REL16_HA:
4452 case elfcpp::R_PPC64_TOC16_HA:
4453 case elfcpp::R_POWERPC_GOT16_HA:
4454 case elfcpp::R_POWERPC_SECTOFF_HA:
4455 case elfcpp::R_POWERPC_TPREL16_HA:
4456 case elfcpp::R_POWERPC_DTPREL16_HA:
4457 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
4458 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
4459 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
4460 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
4461 Reloc::addr16_ha(view, value);
4464 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4466 // R_PPC_EMB_NADDR16_LO
4468 case elfcpp::R_PPC64_ADDR16_HIGHER:
4469 case elfcpp::R_PPC64_TPREL16_HIGHER:
4470 Reloc::addr16_hi2(view, value);
4473 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4475 // R_PPC_EMB_NADDR16_HI
4477 case elfcpp::R_PPC64_ADDR16_HIGHERA:
4478 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4479 Reloc::addr16_ha2(view, value);
4482 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4484 // R_PPC_EMB_NADDR16_HA
4486 case elfcpp::R_PPC64_ADDR16_HIGHEST:
4487 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4488 Reloc::addr16_hi3(view, value);
4491 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4495 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4496 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4497 Reloc::addr16_ha3(view, value);
4500 case elfcpp::R_PPC64_DTPREL16_DS:
4501 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4503 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
4505 case elfcpp::R_PPC64_TPREL16_DS:
4506 case elfcpp::R_PPC64_TPREL16_LO_DS:
4508 // R_PPC_TLSGD, R_PPC_TLSLD
4510 case elfcpp::R_PPC64_ADDR16_DS:
4511 case elfcpp::R_PPC64_ADDR16_LO_DS:
4512 case elfcpp::R_PPC64_TOC16_DS:
4513 case elfcpp::R_PPC64_TOC16_LO_DS:
4514 case elfcpp::R_PPC64_GOT16_DS:
4515 case elfcpp::R_PPC64_GOT16_LO_DS:
4516 case elfcpp::R_PPC64_SECTOFF_DS:
4517 case elfcpp::R_PPC64_SECTOFF_LO_DS:
4518 status = Reloc::addr16_ds(view, value, overflow);
4521 case elfcpp::R_POWERPC_ADDR14:
4522 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4523 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4524 case elfcpp::R_POWERPC_REL14:
4525 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4526 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4527 status = Reloc::addr14(view, value, overflow);
4530 case elfcpp::R_POWERPC_COPY:
4531 case elfcpp::R_POWERPC_GLOB_DAT:
4532 case elfcpp::R_POWERPC_JMP_SLOT:
4533 case elfcpp::R_POWERPC_RELATIVE:
4534 case elfcpp::R_POWERPC_DTPMOD:
4535 case elfcpp::R_PPC64_JMP_IREL:
4536 case elfcpp::R_POWERPC_IRELATIVE:
4537 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4538 _("unexpected reloc %u in object file"),
4542 case elfcpp::R_PPC_EMB_SDA21:
4547 // R_PPC64_TOCSAVE. For the time being this can be ignored.
4551 case elfcpp::R_PPC_EMB_SDA2I16:
4552 case elfcpp::R_PPC_EMB_SDA2REL:
4555 // R_PPC64_TLSGD, R_PPC64_TLSLD
4558 case elfcpp::R_POWERPC_PLT32:
4559 case elfcpp::R_POWERPC_PLTREL32:
4560 case elfcpp::R_POWERPC_PLT16_LO:
4561 case elfcpp::R_POWERPC_PLT16_HI:
4562 case elfcpp::R_POWERPC_PLT16_HA:
4563 case elfcpp::R_PPC_SDAREL16:
4564 case elfcpp::R_POWERPC_ADDR30:
4565 case elfcpp::R_PPC64_PLT64:
4566 case elfcpp::R_PPC64_PLTREL64:
4567 case elfcpp::R_PPC64_PLTGOT16:
4568 case elfcpp::R_PPC64_PLTGOT16_LO:
4569 case elfcpp::R_PPC64_PLTGOT16_HI:
4570 case elfcpp::R_PPC64_PLTGOT16_HA:
4571 case elfcpp::R_PPC64_PLT16_LO_DS:
4572 case elfcpp::R_PPC64_PLTGOT16_DS:
4573 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
4574 case elfcpp::R_PPC_EMB_RELSEC16:
4575 case elfcpp::R_PPC_EMB_RELST_LO:
4576 case elfcpp::R_PPC_EMB_RELST_HI:
4577 case elfcpp::R_PPC_EMB_RELST_HA:
4578 case elfcpp::R_PPC_EMB_BIT_FLD:
4579 case elfcpp::R_PPC_EMB_RELSDA:
4580 case elfcpp::R_PPC_TOC16:
4583 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4584 _("unsupported reloc %u"),
4588 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
4589 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4590 _("relocation overflow"));
4595 // Relocate section data.
4597 template<int size, bool big_endian>
4599 Target_powerpc<size, big_endian>::relocate_section(
4600 const Relocate_info<size, big_endian>* relinfo,
4601 unsigned int sh_type,
4602 const unsigned char* prelocs,
4604 Output_section* output_section,
4605 bool needs_special_offset_handling,
4606 unsigned char* view,
4608 section_size_type view_size,
4609 const Reloc_symbol_changes* reloc_symbol_changes)
4611 typedef Target_powerpc<size, big_endian> Powerpc;
4612 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
4614 gold_assert(sh_type == elfcpp::SHT_RELA);
4616 unsigned char *opd_rel = NULL;
4617 Powerpc_relobj<size, big_endian>* const object
4618 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
4620 && relinfo->data_shndx == object->opd_shndx())
4622 // Rewrite opd relocs, omitting those for discarded sections
4623 // to silence gold::relocate_section errors.
4624 const int reloc_size
4625 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
4626 opd_rel = new unsigned char[reloc_count * reloc_size];
4627 const unsigned char* rrel = prelocs;
4628 unsigned char* wrel = opd_rel;
4632 ++i, rrel += reloc_size, wrel += reloc_size)
4634 typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
4636 typename elfcpp::Elf_types<size>::Elf_WXword r_info
4637 = reloc.get_r_info();
4638 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
4639 Address r_off = reloc.get_r_offset();
4640 if (r_type == elfcpp::R_PPC64_TOC)
4642 bool is_discarded = object->get_opd_discard(r_off);
4644 // Reloc number is reported in some errors, so keep all relocs.
4646 memset(wrel, 0, reloc_size);
4648 memcpy(wrel, rrel, reloc_size);
4653 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
4660 needs_special_offset_handling,
4664 reloc_symbol_changes);
4666 if (opd_rel != NULL)
4670 class Powerpc_scan_relocatable_reloc
4673 // Return the strategy to use for a local symbol which is not a
4674 // section symbol, given the relocation type.
4675 inline Relocatable_relocs::Reloc_strategy
4676 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
4678 if (r_type == 0 && r_sym == 0)
4679 return Relocatable_relocs::RELOC_DISCARD;
4680 return Relocatable_relocs::RELOC_COPY;
4683 // Return the strategy to use for a local symbol which is a section
4684 // symbol, given the relocation type.
4685 inline Relocatable_relocs::Reloc_strategy
4686 local_section_strategy(unsigned int, Relobj*)
4688 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
4691 // Return the strategy to use for a global symbol, given the
4692 // relocation type, the object, and the symbol index.
4693 inline Relocatable_relocs::Reloc_strategy
4694 global_strategy(unsigned int r_type, Relobj*, unsigned int)
4696 if (r_type == elfcpp::R_PPC_PLTREL24)
4697 return Relocatable_relocs::RELOC_SPECIAL;
4698 return Relocatable_relocs::RELOC_COPY;
4702 // Scan the relocs during a relocatable link.
4704 template<int size, bool big_endian>
4706 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
4707 Symbol_table* symtab,
4709 Sized_relobj_file<size, big_endian>* object,
4710 unsigned int data_shndx,
4711 unsigned int sh_type,
4712 const unsigned char* prelocs,
4714 Output_section* output_section,
4715 bool needs_special_offset_handling,
4716 size_t local_symbol_count,
4717 const unsigned char* plocal_symbols,
4718 Relocatable_relocs* rr)
4720 gold_assert(sh_type == elfcpp::SHT_RELA);
4722 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
4723 Powerpc_scan_relocatable_reloc>(
4731 needs_special_offset_handling,
4737 // Emit relocations for a section.
4738 // This is a modified version of the function by the same name in
4739 // target-reloc.h. Using relocate_special_relocatable for
4740 // R_PPC_PLTREL24 would require duplication of the entire body of the
4741 // loop, so we may as well duplicate the whole thing.
4743 template<int size, bool big_endian>
4745 Target_powerpc<size, big_endian>::relocate_relocs(
4746 const Relocate_info<size, big_endian>* relinfo,
4747 unsigned int sh_type,
4748 const unsigned char* prelocs,
4750 Output_section* output_section,
4751 off_t offset_in_output_section,
4752 const Relocatable_relocs* rr,
4754 Address view_address,
4756 unsigned char* reloc_view,
4757 section_size_type reloc_view_size)
4759 gold_assert(sh_type == elfcpp::SHT_RELA);
4761 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
4763 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
4765 const int reloc_size
4766 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
4768 Powerpc_relobj<size, big_endian>* const object
4769 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
4770 const unsigned int local_count = object->local_symbol_count();
4771 unsigned int got2_shndx = object->got2_shndx();
4772 Address got2_addend = 0;
4773 if (got2_shndx != 0)
4775 got2_addend = object->get_output_section_offset(got2_shndx);
4776 gold_assert(got2_addend != invalid_address);
4779 unsigned char* pwrite = reloc_view;
4780 bool zap_next = false;
4781 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
4783 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
4784 if (strategy == Relocatable_relocs::RELOC_DISCARD)
4787 Reltype reloc(prelocs);
4788 Reltype_write reloc_write(pwrite);
4790 Address offset = reloc.get_r_offset();
4791 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
4792 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
4793 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
4794 const unsigned int orig_r_sym = r_sym;
4795 typename elfcpp::Elf_types<size>::Elf_Swxword addend
4796 = reloc.get_r_addend();
4797 const Symbol* gsym = NULL;
4801 // We could arrange to discard these and other relocs for
4802 // tls optimised sequences in the strategy methods, but for
4803 // now do as BFD ld does.
4804 r_type = elfcpp::R_POWERPC_NONE;
4808 // Get the new symbol index.
4809 if (r_sym < local_count)
4813 case Relocatable_relocs::RELOC_COPY:
4814 case Relocatable_relocs::RELOC_SPECIAL:
4817 r_sym = object->symtab_index(r_sym);
4818 gold_assert(r_sym != -1U);
4822 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
4824 // We are adjusting a section symbol. We need to find
4825 // the symbol table index of the section symbol for
4826 // the output section corresponding to input section
4827 // in which this symbol is defined.
4828 gold_assert(r_sym < local_count);
4830 unsigned int shndx =
4831 object->local_symbol_input_shndx(r_sym, &is_ordinary);
4832 gold_assert(is_ordinary);
4833 Output_section* os = object->output_section(shndx);
4834 gold_assert(os != NULL);
4835 gold_assert(os->needs_symtab_index());
4836 r_sym = os->symtab_index();
4846 gsym = object->global_symbol(r_sym);
4847 gold_assert(gsym != NULL);
4848 if (gsym->is_forwarder())
4849 gsym = relinfo->symtab->resolve_forwards(gsym);
4851 gold_assert(gsym->has_symtab_index());
4852 r_sym = gsym->symtab_index();
4855 // Get the new offset--the location in the output section where
4856 // this relocation should be applied.
4857 if (static_cast<Address>(offset_in_output_section) != invalid_address)
4858 offset += offset_in_output_section;
4861 section_offset_type sot_offset =
4862 convert_types<section_offset_type, Address>(offset);
4863 section_offset_type new_sot_offset =
4864 output_section->output_offset(object, relinfo->data_shndx,
4866 gold_assert(new_sot_offset != -1);
4867 offset = new_sot_offset;
4870 // In an object file, r_offset is an offset within the section.
4871 // In an executable or dynamic object, generated by
4872 // --emit-relocs, r_offset is an absolute address.
4873 if (!parameters->options().relocatable())
4875 offset += view_address;
4876 if (static_cast<Address>(offset_in_output_section) != invalid_address)
4877 offset -= offset_in_output_section;
4880 // Handle the reloc addend based on the strategy.
4881 if (strategy == Relocatable_relocs::RELOC_COPY)
4883 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
4885 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
4886 addend = psymval->value(object, addend);
4888 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
4890 if (addend >= 32768)
4891 addend += got2_addend;
4896 if (!parameters->options().relocatable())
4898 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4899 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
4900 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
4901 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
4903 // First instruction of a global dynamic sequence,
4905 const bool final = gsym == NULL || gsym->final_value_is_known();
4906 switch (this->optimize_tls_gd(final))
4908 case tls::TLSOPT_TO_IE:
4909 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
4910 - elfcpp::R_POWERPC_GOT_TLSGD16);
4912 case tls::TLSOPT_TO_LE:
4913 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4914 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4915 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4918 r_type = elfcpp::R_POWERPC_NONE;
4919 offset -= 2 * big_endian;
4926 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4927 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
4928 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
4929 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
4931 // First instruction of a local dynamic sequence,
4933 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
4935 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4936 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
4938 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4939 const Output_section* os = relinfo->layout->tls_segment()
4941 gold_assert(os != NULL);
4942 gold_assert(os->needs_symtab_index());
4943 r_sym = os->symtab_index();
4944 addend = dtp_offset;
4948 r_type = elfcpp::R_POWERPC_NONE;
4949 offset -= 2 * big_endian;
4953 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4954 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
4955 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
4956 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
4958 // First instruction of initial exec sequence.
4959 const bool final = gsym == NULL || gsym->final_value_is_known();
4960 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
4962 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4963 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
4964 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4967 r_type = elfcpp::R_POWERPC_NONE;
4968 offset -= 2 * big_endian;
4972 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4973 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4975 // Second instruction of a global dynamic sequence,
4976 // the __tls_get_addr call
4977 const bool final = gsym == NULL || gsym->final_value_is_known();
4978 switch (this->optimize_tls_gd(final))
4980 case tls::TLSOPT_TO_IE:
4981 r_type = elfcpp::R_POWERPC_NONE;
4984 case tls::TLSOPT_TO_LE:
4985 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4986 offset += 2 * big_endian;
4993 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4994 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4996 // Second instruction of a local dynamic sequence,
4997 // the __tls_get_addr call
4998 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
5000 const Output_section* os = relinfo->layout->tls_segment()
5002 gold_assert(os != NULL);
5003 gold_assert(os->needs_symtab_index());
5004 r_sym = os->symtab_index();
5005 addend = dtp_offset;
5006 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5007 offset += 2 * big_endian;
5011 else if (r_type == elfcpp::R_POWERPC_TLS)
5013 // Second instruction of an initial exec sequence
5014 const bool final = gsym == NULL || gsym->final_value_is_known();
5015 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
5017 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5018 offset += 2 * big_endian;
5023 reloc_write.put_r_offset(offset);
5024 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
5025 reloc_write.put_r_addend(addend);
5027 pwrite += reloc_size;
5030 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
5031 == reloc_view_size);
5034 // Return the value to use for a dynamic which requires special
5035 // treatment. This is how we support equality comparisons of function
5036 // pointers across shared library boundaries, as described in the
5037 // processor specific ABI supplement.
5039 template<int size, bool big_endian>
5041 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
5045 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5046 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5047 unsigned int glink_index = glink->find_entry(gsym);
5048 return glink->address() + glink_index * glink->glink_entry_size();
5054 // Return the PLT address to use for a local symbol.
5055 template<int size, bool big_endian>
5057 Target_powerpc<size, big_endian>::do_plt_address_for_local(
5058 const Relobj* object,
5059 unsigned int symndx) const
5063 const Sized_relobj<size, big_endian>* relobj
5064 = static_cast<const Sized_relobj<size, big_endian>*>(object);
5065 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5066 unsigned int glink_index = glink->find_entry(relobj->sized_relobj(),
5068 return glink->address() + glink_index * glink->glink_entry_size();
5074 // Return the PLT address to use for a global symbol.
5075 template<int size, bool big_endian>
5077 Target_powerpc<size, big_endian>::do_plt_address_for_global(
5078 const Symbol* gsym) const
5082 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5083 unsigned int glink_index = glink->find_entry(gsym);
5084 return glink->address() + glink_index * glink->glink_entry_size();
5090 // Return the offset to use for the GOT_INDX'th got entry which is
5091 // for a local tls symbol specified by OBJECT, SYMNDX.
5092 template<int size, bool big_endian>
5094 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
5095 const Relobj* object,
5096 unsigned int symndx,
5097 unsigned int got_indx) const
5099 const Powerpc_relobj<size, big_endian>* ppc_object
5100 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
5101 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
5103 for (Got_type got_type = GOT_TYPE_TLSGD;
5104 got_type <= GOT_TYPE_TPREL;
5105 got_type = Got_type(got_type + 1))
5106 if (ppc_object->local_has_got_offset(symndx, got_type))
5108 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
5109 if (got_type == GOT_TYPE_TLSGD)
5111 if (off == got_indx * (size / 8))
5113 if (got_type == GOT_TYPE_TPREL)
5123 // Return the offset to use for the GOT_INDX'th got entry which is
5124 // for global tls symbol GSYM.
5125 template<int size, bool big_endian>
5127 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
5129 unsigned int got_indx) const
5131 if (gsym->type() == elfcpp::STT_TLS)
5133 for (Got_type got_type = GOT_TYPE_TLSGD;
5134 got_type <= GOT_TYPE_TPREL;
5135 got_type = Got_type(got_type + 1))
5136 if (gsym->has_got_offset(got_type))
5138 unsigned int off = gsym->got_offset(got_type);
5139 if (got_type == GOT_TYPE_TLSGD)
5141 if (off == got_indx * (size / 8))
5143 if (got_type == GOT_TYPE_TPREL)
5153 // The selector for powerpc object files.
5155 template<int size, bool big_endian>
5156 class Target_selector_powerpc : public Target_selector
5159 Target_selector_powerpc()
5160 : Target_selector(elfcpp::EM_NONE, size, big_endian,
5162 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
5163 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
5165 ? (big_endian ? "elf64ppc" : "elf64lppc")
5166 : (big_endian ? "elf32ppc" : "elf32lppc")))
5170 do_recognize(Input_file*, off_t, int machine, int, int)
5175 if (machine != elfcpp::EM_PPC64)
5180 if (machine != elfcpp::EM_PPC)
5188 return this->instantiate_target();
5192 do_instantiate_target()
5193 { return new Target_powerpc<size, big_endian>(); }
5196 Target_selector_powerpc<32, true> target_selector_ppc32;
5197 Target_selector_powerpc<32, false> target_selector_ppc32le;
5198 Target_selector_powerpc<64, true> target_selector_ppc64;
5199 Target_selector_powerpc<64, false> target_selector_ppc64le;
5201 } // End anonymous namespace.