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;
63 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
64 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
65 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
66 special_(0), opd_ent_shndx_(), opd_ent_off_()
72 // The .got2 section shndx.
77 return this->special_;
82 // The .opd section shndx.
89 return this->special_;
92 // Init OPD entry arrays.
94 init_opd(size_t opd_size)
96 size_t count = this->opd_ent_ndx(opd_size);
97 this->opd_ent_shndx_.resize(count);
98 this->opd_ent_off_.reserve(count);
101 // Return section and offset of function entry for .opd + R_OFF.
103 get_opd_ent(Address r_off, unsigned int* shndx, Address* value)
105 size_t ndx = this->opd_ent_ndx(r_off);
106 gold_assert(ndx < this->opd_ent_shndx_.size());
107 gold_assert(this->opd_ent_shndx_[ndx] != 0);
108 *shndx = this->opd_ent_shndx_[ndx];
109 *value = this->opd_ent_off_[ndx];
112 // Set section and offset of function entry for .opd + R_OFF.
114 set_opd_ent(Address r_off, unsigned int shndx, Address value)
116 size_t ndx = this->opd_ent_ndx(r_off);
117 gold_assert(ndx < this->opd_ent_shndx_.size());
118 this->opd_ent_shndx_[ndx] = shndx;
119 this->opd_ent_off_[ndx] = value;
122 // Examine .rela.opd to build info about function entry points.
124 scan_opd_relocs(size_t reloc_count,
125 const unsigned char* prelocs,
126 const unsigned char* plocal_syms);
129 do_read_relocs(Read_relocs_data*);
132 do_find_special_sections(Read_symbols_data* sd);
134 // Return offset in output GOT section that this object will use
135 // as a TOC pointer. Won't be just a constant with multi-toc support.
137 toc_base_offset() const
141 // Return index into opd_ent_shndx or opd_ent_off array for .opd entry
142 // at OFF. .opd entries are 24 bytes long, but they can be spaced
143 // 16 bytes apart when the language doesn't use the last 8-byte
144 // word, the environment pointer. Thus dividing the entry section
145 // offset by 16 will give an index into opd_ent_shndx_ and
146 // opd_ent_off_ that works for either layout of .opd. (It leaves
147 // some elements of the vectors unused when .opd entries are spaced
148 // 24 bytes apart, but we don't know the spacing until relocations
149 // are processed, and in any case it is possible for an object to
150 // have some entries spaced 16 bytes apart and others 24 bytes apart.)
152 opd_ent_ndx(size_t off) const
155 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
156 unsigned int special_;
157 // The first 8-byte word of an OPD entry gives the address of the
158 // entry point of the function. Relocatable object files have a
159 // relocation on this word. The following two vectors record the
160 // section and offset specified by these relocations.
161 std::vector<unsigned int> opd_ent_shndx_;
162 std::vector<Offset> opd_ent_off_;
165 template<int size, bool big_endian>
166 class Target_powerpc : public Sized_target<size, big_endian>
170 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
171 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
172 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
173 static const Address invalid_address = static_cast<Address>(0) - 1;
174 // Offset of tp and dtp pointers from start of TLS block.
175 static const Address tp_offset = 0x7000;
176 static const Address dtp_offset = 0x8000;
179 : Sized_target<size, big_endian>(&powerpc_info),
180 got_(NULL), plt_(NULL), glink_(NULL), rela_dyn_(NULL),
181 copy_relocs_(elfcpp::R_POWERPC_COPY),
182 dynbss_(NULL), tlsld_got_offset_(-1U)
186 // Process the relocations to determine unreferenced sections for
187 // garbage collection.
189 gc_process_relocs(Symbol_table* symtab,
191 Sized_relobj_file<size, big_endian>* object,
192 unsigned int data_shndx,
193 unsigned int sh_type,
194 const unsigned char* prelocs,
196 Output_section* output_section,
197 bool needs_special_offset_handling,
198 size_t local_symbol_count,
199 const unsigned char* plocal_symbols);
201 // Scan the relocations to look for symbol adjustments.
203 scan_relocs(Symbol_table* symtab,
205 Sized_relobj_file<size, big_endian>* object,
206 unsigned int data_shndx,
207 unsigned int sh_type,
208 const unsigned char* prelocs,
210 Output_section* output_section,
211 bool needs_special_offset_handling,
212 size_t local_symbol_count,
213 const unsigned char* plocal_symbols);
215 // Map input .toc section to output .got section.
217 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
219 if (size == 64 && strcmp(name, ".toc") == 0)
227 // Finalize the sections.
229 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
231 // Return the value to use for a dynamic which requires special
234 do_dynsym_value(const Symbol*) const;
236 // Relocate a section.
238 relocate_section(const Relocate_info<size, big_endian>*,
239 unsigned int sh_type,
240 const unsigned char* prelocs,
242 Output_section* output_section,
243 bool needs_special_offset_handling,
245 Address view_address,
246 section_size_type view_size,
247 const Reloc_symbol_changes*);
249 // Scan the relocs during a relocatable link.
251 scan_relocatable_relocs(Symbol_table* symtab,
253 Sized_relobj_file<size, big_endian>* object,
254 unsigned int data_shndx,
255 unsigned int sh_type,
256 const unsigned char* prelocs,
258 Output_section* output_section,
259 bool needs_special_offset_handling,
260 size_t local_symbol_count,
261 const unsigned char* plocal_symbols,
262 Relocatable_relocs*);
264 // Relocate a section during a relocatable link.
266 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
267 unsigned int sh_type,
268 const unsigned char* prelocs,
270 Output_section* output_section,
271 off_t offset_in_output_section,
272 const Relocatable_relocs*,
274 Address view_address,
276 unsigned char* reloc_view,
277 section_size_type reloc_view_size);
279 // Return whether SYM is defined by the ABI.
281 do_is_defined_by_abi(const Symbol* sym) const
283 return strcmp(sym->name(), "__tls_get_addr") == 0;
286 // Return the size of the GOT section.
290 gold_assert(this->got_ != NULL);
291 return this->got_->data_size();
294 // Get the PLT section.
295 const Output_data_plt_powerpc<size, big_endian>*
298 gold_assert(this->plt_ != NULL);
302 // Get the .glink section.
303 const Output_data_glink<size, big_endian>*
304 glink_section() const
306 gold_assert(this->glink_ != NULL);
310 // Get the GOT section.
311 const Output_data_got_powerpc<size, big_endian>*
314 gold_assert(this->got_ != NULL);
319 do_make_elf_object(const std::string&, Input_file*, off_t,
320 const elfcpp::Ehdr<size, big_endian>&);
322 // Return the number of entries in the GOT.
324 got_entry_count() const
326 if (this->got_ == NULL)
328 return this->got_size() / (size / 8);
331 // Return the number of entries in the PLT.
333 plt_entry_count() const;
335 // Return the offset of the first non-reserved PLT entry.
337 first_plt_entry_offset() const;
339 // Return the size of each PLT entry.
341 plt_entry_size() const;
345 // The class which scans relocations.
350 : issued_non_pic_error_(false)
354 get_reference_flags(unsigned int r_type);
357 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
358 Sized_relobj_file<size, big_endian>* object,
359 unsigned int data_shndx,
360 Output_section* output_section,
361 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
362 const elfcpp::Sym<size, big_endian>& lsym);
365 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
366 Sized_relobj_file<size, big_endian>* object,
367 unsigned int data_shndx,
368 Output_section* output_section,
369 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
373 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
375 Sized_relobj_file<size, big_endian>* ,
378 const elfcpp::Rela<size, big_endian>& ,
380 const elfcpp::Sym<size, big_endian>&)
384 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
386 Sized_relobj_file<size, big_endian>* ,
389 const elfcpp::Rela<size,
391 unsigned int , Symbol*)
396 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
397 unsigned int r_type);
400 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
401 unsigned int r_type, Symbol*);
404 generate_tls_call(Symbol_table* symtab, Layout* layout,
405 Target_powerpc* target);
408 check_non_pic(Relobj*, unsigned int r_type);
410 // Whether we have issued an error about a non-PIC compilation.
411 bool issued_non_pic_error_;
414 // The class which implements relocation.
418 // Use 'at' branch hints when true, 'y' when false.
419 // FIXME maybe: set this with an option.
420 static const bool is_isa_v2 = true;
424 CALL_NOT_EXPECTED = 0,
430 : call_tls_get_addr_(CALL_NOT_EXPECTED)
435 if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
437 // FIXME: This needs to specify the location somehow.
438 gold_error(_("missing expected __tls_get_addr call"));
442 // Do a relocation. Return false if the caller should not issue
443 // any warnings about this relocation.
445 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
446 Output_section*, size_t relnum,
447 const elfcpp::Rela<size, big_endian>&,
448 unsigned int r_type, const Sized_symbol<size>*,
449 const Symbol_value<size>*,
451 typename elfcpp::Elf_types<size>::Elf_Addr,
454 // This is set if we should skip the next reloc, which should be a
455 // call to __tls_get_addr.
456 enum skip_tls call_tls_get_addr_;
459 // A class which returns the size required for a relocation type,
460 // used while scanning relocs during a relocatable link.
461 class Relocatable_size_for_reloc
465 get_size_for_reloc(unsigned int, Relobj*)
472 // Optimize the TLS relocation type based on what we know about the
473 // symbol. IS_FINAL is true if the final address of this symbol is
474 // known at link time.
476 tls::Tls_optimization
477 optimize_tls_gd(bool is_final)
479 // If we are generating a shared library, then we can't do anything
481 if (parameters->options().shared())
482 return tls::TLSOPT_NONE;
485 return tls::TLSOPT_TO_IE;
486 return tls::TLSOPT_TO_LE;
489 tls::Tls_optimization
492 if (parameters->options().shared())
493 return tls::TLSOPT_NONE;
495 return tls::TLSOPT_TO_LE;
498 tls::Tls_optimization
499 optimize_tls_ie(bool is_final)
501 if (!is_final || parameters->options().shared())
502 return tls::TLSOPT_NONE;
504 return tls::TLSOPT_TO_LE;
507 // Get the GOT section, creating it if necessary.
508 Output_data_got_powerpc<size, big_endian>*
509 got_section(Symbol_table*, Layout*);
513 make_glink_section(Layout*);
515 // Create the PLT section.
517 make_plt_section(Layout*);
519 // Create a PLT entry for a global symbol.
521 make_plt_entry(Layout*, Symbol*,
522 const elfcpp::Rela<size, big_endian>&,
523 const Sized_relobj<size, big_endian>* object);
525 // Create a GOT entry for local dynamic __tls_get_addr.
527 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
528 Sized_relobj_file<size, big_endian>* object);
531 tlsld_got_offset() const
533 return this->tlsld_got_offset_;
536 // Get the dynamic reloc section, creating it if necessary.
538 rela_dyn_section(Layout*);
540 // Copy a relocation against a global symbol.
542 copy_reloc(Symbol_table* symtab, Layout* layout,
543 Sized_relobj_file<size, big_endian>* object,
544 unsigned int shndx, Output_section* output_section,
545 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
547 this->copy_relocs_.copy_reloc(symtab, layout,
548 symtab->get_sized_symbol<size>(sym),
549 object, shndx, output_section,
550 reloc, this->rela_dyn_section(layout));
553 // Information about this specific target which we pass to the
554 // general Target structure.
555 static Target::Target_info powerpc_info;
557 // The types of GOT entries needed for this platform.
558 // These values are exposed to the ABI in an incremental link.
559 // Do not renumber existing values without changing the version
560 // number of the .gnu_incremental_inputs section.
564 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
565 GOT_TYPE_DTPREL, // entry for @got@dtprel
566 GOT_TYPE_TPREL // entry for @got@tprel
569 // The GOT output section.
570 Output_data_got_powerpc<size, big_endian>* got_;
571 // The PLT output section.
572 Output_data_plt_powerpc<size, big_endian>* plt_;
573 // The .glink output section.
574 Output_data_glink<size, big_endian>* glink_;
575 // The dynamic reloc output section.
576 Reloc_section* rela_dyn_;
577 // Relocs saved to avoid a COPY reloc.
578 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
579 // Space for variables copied with a COPY reloc.
580 Output_data_space* dynbss_;
581 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
582 unsigned int tlsld_got_offset_;
586 Target::Target_info Target_powerpc<32, true>::powerpc_info =
589 true, // is_big_endian
590 elfcpp::EM_PPC, // machine_code
591 false, // has_make_symbol
592 false, // has_resolve
593 false, // has_code_fill
594 true, // is_default_stack_executable
595 false, // can_icf_inline_merge_sections
597 "/usr/lib/ld.so.1", // dynamic_linker
598 0x10000000, // default_text_segment_address
599 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
600 4 * 1024, // common_pagesize (overridable by -z common-page-size)
601 false, // isolate_execinstr
603 elfcpp::SHN_UNDEF, // small_common_shndx
604 elfcpp::SHN_UNDEF, // large_common_shndx
605 0, // small_common_section_flags
606 0, // large_common_section_flags
607 NULL, // attributes_section
608 NULL // attributes_vendor
612 Target::Target_info Target_powerpc<32, false>::powerpc_info =
615 false, // is_big_endian
616 elfcpp::EM_PPC, // machine_code
617 false, // has_make_symbol
618 false, // has_resolve
619 false, // has_code_fill
620 true, // is_default_stack_executable
621 false, // can_icf_inline_merge_sections
623 "/usr/lib/ld.so.1", // dynamic_linker
624 0x10000000, // default_text_segment_address
625 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
626 4 * 1024, // common_pagesize (overridable by -z common-page-size)
627 false, // isolate_execinstr
629 elfcpp::SHN_UNDEF, // small_common_shndx
630 elfcpp::SHN_UNDEF, // large_common_shndx
631 0, // small_common_section_flags
632 0, // large_common_section_flags
633 NULL, // attributes_section
634 NULL // attributes_vendor
638 Target::Target_info Target_powerpc<64, true>::powerpc_info =
641 true, // is_big_endian
642 elfcpp::EM_PPC64, // machine_code
643 false, // has_make_symbol
644 false, // has_resolve
645 false, // has_code_fill
646 true, // is_default_stack_executable
647 false, // can_icf_inline_merge_sections
649 "/usr/lib/ld.so.1", // dynamic_linker
650 0x10000000, // default_text_segment_address
651 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
652 4 * 1024, // common_pagesize (overridable by -z common-page-size)
653 false, // isolate_execinstr
655 elfcpp::SHN_UNDEF, // small_common_shndx
656 elfcpp::SHN_UNDEF, // large_common_shndx
657 0, // small_common_section_flags
658 0, // large_common_section_flags
659 NULL, // attributes_section
660 NULL // attributes_vendor
664 Target::Target_info Target_powerpc<64, false>::powerpc_info =
667 false, // is_big_endian
668 elfcpp::EM_PPC64, // machine_code
669 false, // has_make_symbol
670 false, // has_resolve
671 false, // has_code_fill
672 true, // is_default_stack_executable
673 false, // can_icf_inline_merge_sections
675 "/usr/lib/ld.so.1", // dynamic_linker
676 0x10000000, // default_text_segment_address
677 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
678 4 * 1024, // common_pagesize (overridable by -z common-page-size)
679 false, // isolate_execinstr
681 elfcpp::SHN_UNDEF, // small_common_shndx
682 elfcpp::SHN_UNDEF, // large_common_shndx
683 0, // small_common_section_flags
684 0, // large_common_section_flags
685 NULL, // attributes_section
686 NULL // attributes_vendor
690 is_branch_reloc(unsigned int r_type)
692 return (r_type == elfcpp::R_POWERPC_REL24
693 || r_type == elfcpp::R_PPC_PLTREL24
694 || r_type == elfcpp::R_PPC_LOCAL24PC
695 || r_type == elfcpp::R_POWERPC_REL14
696 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
697 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
698 || r_type == elfcpp::R_POWERPC_ADDR24
699 || r_type == elfcpp::R_POWERPC_ADDR14
700 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
701 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
704 // If INSN is an opcode that may be used with an @tls operand, return
705 // the transformed insn for TLS optimisation, otherwise return 0. If
706 // REG is non-zero only match an insn with RB or RA equal to REG.
708 at_tls_transform(uint32_t insn, unsigned int reg)
710 if ((insn & (0x3f << 26)) != 31 << 26)
714 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
715 rtra = insn & ((1 << 26) - (1 << 16));
716 else if (((insn >> 16) & 0x1f) == reg)
717 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
721 if ((insn & (0x3ff << 1)) == 266 << 1)
724 else if ((insn & (0x1f << 1)) == 23 << 1
725 && ((insn & (0x1f << 6)) < 14 << 6
726 || ((insn & (0x1f << 6)) >= 16 << 6
727 && (insn & (0x1f << 6)) < 24 << 6)))
728 // load and store indexed -> dform
729 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
730 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
731 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
732 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
733 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
735 insn = (58 << 26) | 2;
742 // Modified version of symtab.h class Symbol member
743 // Given a direct absolute or pc-relative static relocation against
744 // the global symbol, this function returns whether a dynamic relocation
749 needs_dynamic_reloc(const Symbol* gsym, int flags)
751 // No dynamic relocations in a static link!
752 if (parameters->doing_static_link())
755 // A reference to an undefined symbol from an executable should be
756 // statically resolved to 0, and does not need a dynamic relocation.
757 // This matches gnu ld behavior.
758 if (gsym->is_undefined() && !parameters->options().shared())
761 // A reference to an absolute symbol does not need a dynamic relocation.
762 if (gsym->is_absolute())
765 // An absolute reference within a position-independent output file
766 // will need a dynamic relocation.
767 if ((flags & Symbol::ABSOLUTE_REF)
768 && parameters->options().output_is_position_independent())
771 // A function call that can branch to a local PLT entry does not need
772 // a dynamic relocation.
773 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
776 // A reference to any PLT entry in a non-position-independent executable
777 // does not need a dynamic relocation.
778 // Except due to having function descriptors on powerpc64 we don't define
779 // functions to their plt code in an executable, so this doesn't apply.
781 && !parameters->options().output_is_position_independent()
782 && gsym->has_plt_offset())
785 // A reference to a symbol defined in a dynamic object or to a
786 // symbol that is preemptible will need a dynamic relocation.
787 if (gsym->is_from_dynobj()
788 || gsym->is_undefined()
789 || gsym->is_preemptible())
792 // For all other cases, return FALSE.
796 // Modified version of symtab.h class Symbol member
797 // Whether we should use the PLT offset associated with a symbol for
798 // a relocation. FLAGS is a set of Reference_flags.
802 use_plt_offset(const Symbol* gsym, int flags)
804 // If the symbol doesn't have a PLT offset, then naturally we
805 // don't want to use it.
806 if (!gsym->has_plt_offset())
809 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
810 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
813 // If we are going to generate a dynamic relocation, then we will
814 // wind up using that, so no need to use the PLT entry.
815 if (needs_dynamic_reloc<size>(gsym, flags))
818 // If the symbol is from a dynamic object, we need to use the PLT
820 if (gsym->is_from_dynobj())
823 // If we are generating a shared object, and gsym symbol is
824 // undefined or preemptible, we need to use the PLT entry.
825 if (parameters->options().shared()
826 && (gsym->is_undefined() || gsym->is_preemptible()))
829 // If gsym is a call to a weak undefined symbol, we need to use
830 // the PLT entry; the symbol may be defined by a library loaded
832 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
835 // Otherwise we can use the regular definition.
839 template<int size, bool big_endian>
840 class Powerpc_relocate_functions
857 typedef Powerpc_relocate_functions<size, big_endian> This;
858 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
860 template<int valsize>
862 has_overflow_signed(Address value)
864 // limit = 1 << (valsize - 1) without shift count exceeding size of type
865 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
866 limit <<= ((valsize - 1) >> 1);
867 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
868 return value + limit > (limit << 1) - 1;
871 template<int valsize>
873 has_overflow_bitfield(Address value)
875 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
876 limit <<= ((valsize - 1) >> 1);
877 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
878 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
881 template<int valsize>
882 static inline enum overflow_status
883 overflowed(Address value, enum overflow_check overflow)
885 if (overflow == check_signed)
887 if (has_overflow_signed<valsize>(value))
888 return status_overflow;
890 else if (overflow == check_bitfield)
892 if (has_overflow_bitfield<valsize>(value))
893 return status_overflow;
898 // Do a simple RELA relocation
899 template<int valsize>
900 static inline enum overflow_status
901 rela(unsigned char* view, Address value, enum overflow_check overflow)
903 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
904 Valtype* wv = reinterpret_cast<Valtype*>(view);
905 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
906 return overflowed<valsize>(value, overflow);
909 template<int valsize>
910 static inline enum overflow_status
911 rela(unsigned char* view,
912 unsigned int right_shift,
913 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
915 enum overflow_check overflow)
917 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
918 Valtype* wv = reinterpret_cast<Valtype*>(view);
919 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
920 Valtype reloc = value >> right_shift;
923 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
924 return overflowed<valsize>(value >> right_shift, overflow);
927 // Do a simple RELA relocation, unaligned.
928 template<int valsize>
929 static inline enum overflow_status
930 rela_ua(unsigned char* view, Address value, enum overflow_check overflow)
932 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
933 return overflowed<valsize>(value, overflow);
936 template<int valsize>
937 static inline enum overflow_status
938 rela_ua(unsigned char* view,
939 unsigned int right_shift,
940 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
942 enum overflow_check overflow)
944 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
946 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
947 Valtype reloc = value >> right_shift;
950 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
951 return overflowed<valsize>(value >> right_shift, overflow);
955 // R_PPC64_ADDR64: (Symbol + Addend)
957 addr64(unsigned char* view, Address value)
958 { This::template rela<64>(view, value, check_none); }
960 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
962 addr64_u(unsigned char* view, Address value)
963 { This::template rela_ua<64>(view, value, check_none); }
965 // R_POWERPC_ADDR32: (Symbol + Addend)
966 static inline enum overflow_status
967 addr32(unsigned char* view, Address value, enum overflow_check overflow)
968 { return This::template rela<32>(view, value, overflow); }
970 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
971 static inline enum overflow_status
972 addr32_u(unsigned char* view, Address value, enum overflow_check overflow)
973 { return This::template rela_ua<32>(view, value, overflow); }
975 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
976 static inline enum overflow_status
977 addr24(unsigned char* view, Address value, enum overflow_check overflow)
979 enum overflow_status stat
980 = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
981 if (overflow != check_none && (value & 3) != 0)
982 stat = status_overflow;
986 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
987 static inline enum overflow_status
988 addr16(unsigned char* view, Address value, enum overflow_check overflow)
989 { return This::template rela<16>(view, value, overflow); }
991 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
992 static inline enum overflow_status
993 addr16_u(unsigned char* view, Address value, enum overflow_check overflow)
994 { return This::template rela_ua<16>(view, value, overflow); }
996 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
997 static inline enum overflow_status
998 addr16_ds(unsigned char* view, Address value, enum overflow_check overflow)
1000 enum overflow_status stat
1001 = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1002 if (overflow != check_none && (value & 3) != 0)
1003 stat = status_overflow;
1007 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1009 addr16_hi(unsigned char* view, Address value)
1010 { This::template rela<16>(view, 16, 0xffff, value, check_none); }
1012 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1014 addr16_ha(unsigned char* view, Address value)
1015 { This::addr16_hi(view, value + 0x8000); }
1017 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1019 addr16_hi2(unsigned char* view, Address value)
1020 { This::template rela<16>(view, 32, 0xffff, value, check_none); }
1022 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1024 addr16_ha2(unsigned char* view, Address value)
1025 { This::addr16_hi2(view, value + 0x8000); }
1027 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1029 addr16_hi3(unsigned char* view, Address value)
1030 { This::template rela<16>(view, 48, 0xffff, value, check_none); }
1032 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1034 addr16_ha3(unsigned char* view, Address value)
1035 { This::addr16_hi3(view, value + 0x8000); }
1037 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1038 static inline enum overflow_status
1039 addr14(unsigned char* view, Address value, enum overflow_check overflow)
1041 enum overflow_status stat
1042 = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1043 if (overflow != check_none && (value & 3) != 0)
1044 stat = status_overflow;
1049 // Stash away the index of .got2 or .opd in a relocatable object, if
1050 // such a section exists.
1052 template<int size, bool big_endian>
1054 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1055 Read_symbols_data* sd)
1057 const unsigned char* const pshdrs = sd->section_headers->data();
1058 const unsigned char* namesu = sd->section_names->data();
1059 const char* names = reinterpret_cast<const char*>(namesu);
1060 section_size_type names_size = sd->section_names_size;
1061 const unsigned char* s;
1063 s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1064 names, names_size, NULL);
1067 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1068 this->special_ = ndx;
1070 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1073 // Examine .rela.opd to build info about function entry points.
1075 template<int size, bool big_endian>
1077 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1079 const unsigned char* prelocs,
1080 const unsigned char* plocal_syms)
1084 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1086 const int reloc_size
1087 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1088 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1090 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1092 Reltype reloc(prelocs);
1093 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1094 = reloc.get_r_info();
1095 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1096 if (r_type == elfcpp::R_PPC64_ADDR64)
1098 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1099 typename elfcpp::Elf_types<size>::Elf_Addr value;
1102 if (r_sym < this->local_symbol_count())
1104 typename elfcpp::Sym<size, big_endian>
1105 lsym(plocal_syms + r_sym * sym_size);
1106 shndx = lsym.get_st_shndx();
1107 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1108 value = lsym.get_st_value();
1111 shndx = this->symbol_section_and_value(r_sym, &value,
1113 this->set_opd_ent(reloc.get_r_offset(), shndx,
1114 value + reloc.get_r_addend());
1120 template<int size, bool big_endian>
1122 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1124 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1127 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1128 p != rd->relocs.end();
1131 if (p->data_shndx == this->opd_shndx())
1133 this->init_opd(this->section_size(this->opd_shndx()));
1134 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1135 rd->local_symbols->data());
1142 // Set up PowerPC target specific relobj.
1144 template<int size, bool big_endian>
1146 Target_powerpc<size, big_endian>::do_make_elf_object(
1147 const std::string& name,
1148 Input_file* input_file,
1149 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1151 int et = ehdr.get_e_type();
1152 // ET_EXEC files are valid input for --just-symbols/-R,
1153 // and we treat them as relocatable objects.
1154 if (et == elfcpp::ET_REL
1155 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1157 Powerpc_relobj<size, big_endian>* obj =
1158 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1162 else if (et == elfcpp::ET_DYN)
1164 Sized_dynobj<size, big_endian>* obj =
1165 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1171 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1176 template<int size, bool big_endian>
1177 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1180 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1181 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1183 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1184 : Output_data_got<size, big_endian>(),
1185 symtab_(symtab), layout_(layout),
1186 header_ent_cnt_(size == 32 ? 3 : 1),
1187 header_index_(size == 32 ? 0x2000 : 0)
1192 // Create a new GOT entry and return its offset.
1194 add_got_entry(Got_entry got_entry)
1196 this->reserve_ent();
1197 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1200 // Create a pair of new GOT entries and return the offset of the first.
1202 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1204 this->reserve_ent(2);
1205 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1210 add_constant_pair(Valtype c1, Valtype c2)
1212 this->reserve_ent(2);
1213 unsigned int got_offset = this->add_constant(c1);
1214 this->add_constant(c2);
1218 // Offset of _GLOBAL_OFFSET_TABLE_.
1222 return this->got_offset(this->header_index_);
1225 // Offset of base used to access the GOT/TOC.
1226 // The got/toc pointer reg will be set to this value.
1227 typename elfcpp::Elf_types<size>::Elf_Off
1228 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1231 return this->g_o_t();
1233 return (this->output_section()->address()
1234 + object->toc_base_offset()
1238 // Ensure our GOT has a header.
1240 set_final_data_size()
1242 if (this->header_ent_cnt_ != 0)
1243 this->make_header();
1244 Output_data_got<size, big_endian>::set_final_data_size();
1247 // First word of GOT header needs some values that are not
1248 // handled by Output_data_got so poke them in here.
1249 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1251 do_write(Output_file* of)
1253 this->replace_constant(this->header_index_,
1255 ? this->layout_->dynamic_section()->address()
1256 : this->output_section()->address() + 0x8000));
1258 Output_data_got<size, big_endian>::do_write(of);
1263 reserve_ent(unsigned int cnt = 1)
1265 if (this->header_ent_cnt_ == 0)
1267 if (this->num_entries() + cnt > this->header_index_)
1268 this->make_header();
1274 this->header_ent_cnt_ = 0;
1275 this->header_index_ = this->num_entries();
1278 Output_data_got<size, big_endian>::add_constant(0);
1279 Output_data_got<size, big_endian>::add_constant(0);
1280 Output_data_got<size, big_endian>::add_constant(0);
1282 // Define _GLOBAL_OFFSET_TABLE_ at the header
1283 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1284 Symbol_table::PREDEFINED,
1285 this, this->g_o_t(), 0,
1292 Output_data_got<size, big_endian>::add_constant(0);
1295 // Stashed pointers.
1296 Symbol_table* symtab_;
1300 unsigned int header_ent_cnt_;
1301 // GOT header index.
1302 unsigned int header_index_;
1305 // Get the GOT section, creating it if necessary.
1307 template<int size, bool big_endian>
1308 Output_data_got_powerpc<size, big_endian>*
1309 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1312 if (this->got_ == NULL)
1314 gold_assert(symtab != NULL && layout != NULL);
1317 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
1319 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1320 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1321 this->got_, ORDER_DATA, false);
1327 // Get the dynamic reloc section, creating it if necessary.
1329 template<int size, bool big_endian>
1330 typename Target_powerpc<size, big_endian>::Reloc_section*
1331 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1333 if (this->rela_dyn_ == NULL)
1335 gold_assert(layout != NULL);
1336 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1337 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1338 elfcpp::SHF_ALLOC, this->rela_dyn_,
1339 ORDER_DYNAMIC_RELOCS, false);
1341 return this->rela_dyn_;
1344 // A class to handle the PLT data.
1346 template<int size, bool big_endian>
1347 class Output_data_plt_powerpc : public Output_section_data_build
1350 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1351 size, big_endian> Reloc_section;
1353 Output_data_plt_powerpc(Layout*, Target_powerpc<size, big_endian>*);
1355 // Add an entry to the PLT.
1359 // Return the .rela.plt section data.
1360 const Reloc_section*
1366 // Return the number of PLT entries.
1370 return ((this->current_data_size() - initial_plt_entry_size)
1374 // Return the offset of the first non-reserved PLT entry.
1376 first_plt_entry_offset()
1377 { return initial_plt_entry_size; }
1379 // Return the size of a PLT entry.
1381 get_plt_entry_size()
1382 { return plt_entry_size; }
1386 do_adjust_output_section(Output_section* os)
1391 // Write to a map file.
1393 do_print_to_mapfile(Mapfile* mapfile) const
1394 { mapfile->print_output_data(this, _("** PLT")); }
1397 // The size of an entry in the PLT.
1398 static const int plt_entry_size = size == 32 ? 4 : 24;
1399 // The size of the first reserved entry.
1400 static const int initial_plt_entry_size = size == 32 ? 0 : 24;
1402 // Write out the PLT data.
1404 do_write(Output_file*);
1406 // The reloc section.
1407 Reloc_section* rel_;
1408 // Allows access to .glink for do_write.
1409 Target_powerpc<size, big_endian>* targ_;
1412 // Create the PLT section.
1414 template<int size, bool big_endian>
1415 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(
1417 Target_powerpc<size, big_endian>* targ)
1418 : Output_section_data_build(size == 32 ? 4 : 8),
1421 this->rel_ = new Reloc_section(false);
1422 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1423 elfcpp::SHF_ALLOC, this->rel_,
1424 ORDER_DYNAMIC_PLT_RELOCS, false);
1427 // Add an entry to the PLT.
1429 template<int size, bool big_endian>
1431 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1433 if (!gsym->has_plt_offset())
1435 off_t off = this->current_data_size();
1438 off += initial_plt_entry_size;
1439 gsym->set_plt_offset(off);
1440 gsym->set_needs_dynsym_entry();
1441 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, off, 0);
1442 off += plt_entry_size;
1443 this->set_current_data_size(off);
1447 static const uint32_t add_0_11_11 = 0x7c0b5a14;
1448 static const uint32_t add_3_3_2 = 0x7c631214;
1449 static const uint32_t add_3_3_13 = 0x7c636a14;
1450 static const uint32_t add_11_0_11 = 0x7d605a14;
1451 static const uint32_t add_12_2_11 = 0x7d825a14;
1452 static const uint32_t addi_11_11 = 0x396b0000;
1453 static const uint32_t addi_12_12 = 0x398c0000;
1454 static const uint32_t addi_2_2 = 0x38420000;
1455 static const uint32_t addi_3_2 = 0x38620000;
1456 static const uint32_t addi_3_3 = 0x38630000;
1457 static const uint32_t addis_0_2 = 0x3c020000;
1458 static const uint32_t addis_0_13 = 0x3c0d0000;
1459 static const uint32_t addis_11_11 = 0x3d6b0000;
1460 static const uint32_t addis_11_30 = 0x3d7e0000;
1461 static const uint32_t addis_12_12 = 0x3d8c0000;
1462 static const uint32_t addis_12_2 = 0x3d820000;
1463 static const uint32_t addis_3_2 = 0x3c620000;
1464 static const uint32_t addis_3_13 = 0x3c6d0000;
1465 static const uint32_t b = 0x48000000;
1466 static const uint32_t bcl_20_31 = 0x429f0005;
1467 static const uint32_t bctr = 0x4e800420;
1468 static const uint32_t blrl = 0x4e800021;
1469 static const uint32_t cror_15_15_15 = 0x4def7b82;
1470 static const uint32_t cror_31_31_31 = 0x4ffffb82;
1471 static const uint32_t ld_11_12 = 0xe96c0000;
1472 static const uint32_t ld_11_2 = 0xe9620000;
1473 static const uint32_t ld_2_1 = 0xe8410000;
1474 static const uint32_t ld_2_11 = 0xe84b0000;
1475 static const uint32_t ld_2_12 = 0xe84c0000;
1476 static const uint32_t ld_2_2 = 0xe8420000;
1477 static const uint32_t li_0_0 = 0x38000000;
1478 static const uint32_t lis_0_0 = 0x3c000000;
1479 static const uint32_t lis_11 = 0x3d600000;
1480 static const uint32_t lis_12 = 0x3d800000;
1481 static const uint32_t lwz_0_12 = 0x800c0000;
1482 static const uint32_t lwz_11_11 = 0x816b0000;
1483 static const uint32_t lwz_11_30 = 0x817e0000;
1484 static const uint32_t lwz_12_12 = 0x818c0000;
1485 static const uint32_t lwzu_0_12 = 0x840c0000;
1486 static const uint32_t mflr_0 = 0x7c0802a6;
1487 static const uint32_t mflr_11 = 0x7d6802a6;
1488 static const uint32_t mflr_12 = 0x7d8802a6;
1489 static const uint32_t mtctr_0 = 0x7c0903a6;
1490 static const uint32_t mtctr_11 = 0x7d6903a6;
1491 static const uint32_t mtlr_0 = 0x7c0803a6;
1492 static const uint32_t mtlr_12 = 0x7d8803a6;
1493 static const uint32_t nop = 0x60000000;
1494 static const uint32_t ori_0_0_0 = 0x60000000;
1495 static const uint32_t std_2_1 = 0xf8410000;
1496 static const uint32_t sub_11_11_12 = 0x7d6c5850;
1498 // Write out the PLT.
1500 template<int size, bool big_endian>
1502 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1506 const off_t offset = this->offset();
1507 const section_size_type oview_size
1508 = convert_to_section_size_type(this->data_size());
1509 unsigned char* const oview = of->get_output_view(offset, oview_size);
1510 unsigned char* pov = oview;
1511 unsigned char* endpov = oview + oview_size;
1513 // The address the .glink branch table
1514 const Output_data_glink<size, big_endian>* glink
1515 = this->targ_->glink_section();
1516 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1517 = glink->address() + glink->pltresolve();
1519 while (pov < endpov)
1521 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1526 of->write_output_view(offset, oview_size, oview);
1530 // Create the PLT section.
1532 template<int size, bool big_endian>
1534 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1536 if (this->plt_ == NULL)
1538 if (this->glink_ == NULL)
1539 make_glink_section(layout);
1541 // Ensure that .rela.dyn always appears before .rela.plt This is
1542 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1543 // needs to include .rela.plt in it's range.
1544 this->rela_dyn_section(layout);
1546 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout, this);
1547 layout->add_output_section_data(".plt",
1549 ? elfcpp::SHT_PROGBITS
1550 : elfcpp::SHT_NOBITS),
1551 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1560 // A class to handle .glink.
1562 template<int size, bool big_endian>
1563 class Output_data_glink : public Output_section_data
1566 static const int pltresolve_size = 16*4;
1568 Output_data_glink(Target_powerpc<size, big_endian>*);
1572 add_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1573 const Sized_relobj<size, big_endian>*);
1576 find_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1577 const Sized_relobj<size, big_endian>*) const;
1580 glink_entry_size() const
1585 // FIXME: We should be using multiple glink sections for
1586 // stubs to support > 33M applications.
1593 return this->pltresolve_;
1597 // Write to a map file.
1599 do_print_to_mapfile(Mapfile* mapfile) const
1600 { mapfile->print_output_data(this, _("** glink")); }
1604 set_final_data_size();
1608 do_write(Output_file*);
1613 Glink_sym_ent(const Symbol* sym,
1614 const elfcpp::Rela<size, big_endian>& reloc,
1615 const Sized_relobj<size, big_endian>* object)
1616 : sym_(sym), addend_(0), object_(0)
1619 this->addend_ = reloc.get_r_addend();
1620 else if (parameters->options().output_is_position_independent()
1621 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1622 == elfcpp::R_PPC_PLTREL24))
1624 this->addend_ = reloc.get_r_addend();
1625 if (this->addend_ != 0)
1626 this->object_ = object;
1630 bool operator==(const Glink_sym_ent& that) const
1632 return (this->sym_ == that.sym_
1633 && this->object_ == that.object_
1634 && this->addend_ == that.addend_);
1638 unsigned int addend_;
1639 const Sized_relobj<size, big_endian>* object_;
1642 class Glink_sym_ent_hash
1645 size_t operator()(const Glink_sym_ent& ent) const
1647 return (reinterpret_cast<uintptr_t>(ent.sym_)
1648 ^ reinterpret_cast<uintptr_t>(ent.object_)
1653 // Map sym/object/addend to index.
1654 typedef Unordered_map<Glink_sym_ent, unsigned int,
1655 Glink_sym_ent_hash> Glink_entries;
1656 Glink_entries glink_entries_;
1658 // Offset of pltresolve stub (actually, branch table for 32-bit)
1661 // Allows access to .got and .plt for do_write.
1662 Target_powerpc<size, big_endian>* targ_;
1665 // Create the glink section.
1667 template<int size, bool big_endian>
1668 Output_data_glink<size, big_endian>::Output_data_glink(
1669 Target_powerpc<size, big_endian>* targ)
1670 : Output_section_data(16),
1671 pltresolve_(0), targ_(targ)
1675 // Add an entry to glink, if we do not already have one for this
1676 // sym/object/addend combo.
1678 template<int size, bool big_endian>
1680 Output_data_glink<size, big_endian>::add_entry(
1682 const elfcpp::Rela<size, big_endian>& reloc,
1683 const Sized_relobj<size, big_endian>* object)
1685 Glink_sym_ent ent(gsym, reloc, object);
1686 unsigned int indx = this->glink_entries_.size();
1687 this->glink_entries_.insert(std::make_pair(ent, indx));
1690 template<int size, bool big_endian>
1692 Output_data_glink<size, big_endian>::find_entry(
1694 const elfcpp::Rela<size, big_endian>& reloc,
1695 const Sized_relobj<size, big_endian>* object) const
1697 Glink_sym_ent ent(gsym, reloc, object);
1698 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
1699 gold_assert(p != this->glink_entries_.end());
1703 template<int size, bool big_endian>
1705 Output_data_glink<size, big_endian>::set_final_data_size()
1707 unsigned int count = this->glink_entries_.size();
1708 off_t total = count;
1715 this->pltresolve_ = total;
1717 // space for branch table
1718 total += 4 * (count - 1);
1720 total += -total & 15;
1721 total += this->pltresolve_size;
1726 this->pltresolve_ = total;
1727 total += this->pltresolve_size;
1729 // space for branch table
1732 total += 4 * (count - 0x8000);
1736 this->set_data_size(total);
1739 static inline uint32_t
1745 static inline uint32_t
1751 static inline uint32_t
1754 return hi(a + 0x8000);
1757 template<bool big_endian>
1759 write_insn(unsigned char* p, uint32_t v)
1761 elfcpp::Swap<32, big_endian>::writeval(p, v);
1764 // Write out .glink.
1766 template<int size, bool big_endian>
1768 Output_data_glink<size, big_endian>::do_write(Output_file* of)
1770 const off_t off = this->offset();
1771 const section_size_type oview_size =
1772 convert_to_section_size_type(this->data_size());
1773 unsigned char* const oview = of->get_output_view(off, oview_size);
1776 // The base address of the .plt section.
1777 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1778 Address plt_base = this->targ_->plt_section()->address();
1780 const Output_data_got_powerpc<size, big_endian>* got
1781 = this->targ_->got_section();
1785 Address got_os_addr = got->output_section()->address();
1787 // Write out call stubs.
1788 typename Glink_entries::const_iterator g;
1789 for (g = this->glink_entries_.begin();
1790 g != this->glink_entries_.end();
1793 Address plt_addr = plt_base + g->first.sym_->plt_offset();
1794 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1795 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1796 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
1797 Address pltoff = plt_addr - got_addr;
1799 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
1800 gold_error(_("%s: linkage table error against `%s'"),
1801 g->first.object_->name().c_str(),
1802 g->first.sym_->demangled_name().c_str());
1804 p = oview + g->second * this->glink_entry_size();
1805 if (ha(pltoff) != 0)
1807 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
1808 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1809 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
1810 if (ha(pltoff + 16) != ha(pltoff))
1812 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
1815 write_insn<big_endian>(p, mtctr_11), p += 4;
1816 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
1817 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
1818 write_insn<big_endian>(p, bctr), p += 4;
1822 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1823 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
1824 if (ha(pltoff + 16) != ha(pltoff))
1826 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
1829 write_insn<big_endian>(p, mtctr_11), p += 4;
1830 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
1831 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
1832 write_insn<big_endian>(p, bctr), p += 4;
1836 // Write pltresolve stub.
1837 p = oview + this->pltresolve_;
1838 Address after_bcl = this->address() + this->pltresolve_ + 16;
1839 Address pltoff = plt_base - after_bcl;
1841 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
1843 write_insn<big_endian>(p, mflr_12), p += 4;
1844 write_insn<big_endian>(p, bcl_20_31), p += 4;
1845 write_insn<big_endian>(p, mflr_11), p += 4;
1846 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
1847 write_insn<big_endian>(p, mtlr_12), p += 4;
1848 write_insn<big_endian>(p, add_12_2_11), p += 4;
1849 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
1850 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
1851 write_insn<big_endian>(p, mtctr_11), p += 4;
1852 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
1853 write_insn<big_endian>(p, bctr), p += 4;
1854 while (p < oview + this->pltresolve_ + this->pltresolve_size)
1855 write_insn<big_endian>(p, nop), p += 4;
1857 // Write lazy link call stubs.
1859 while (p < oview + oview_size)
1863 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
1867 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
1868 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
1870 uint32_t branch_off = this->pltresolve_ + 8 - (p - oview);
1871 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
1877 // The address of _GLOBAL_OFFSET_TABLE_.
1878 Address g_o_t = got->address() + got->g_o_t();
1880 // Write out call stubs.
1881 typename Glink_entries::const_iterator g;
1882 for (g = this->glink_entries_.begin();
1883 g != this->glink_entries_.end();
1886 Address plt_addr = plt_base + g->first.sym_->plt_offset();
1888 const Address invalid_address = static_cast<Address>(-1);
1890 p = oview + g->second * this->glink_entry_size();
1891 if (parameters->options().output_is_position_independent())
1893 const Powerpc_relobj<size, big_endian>* object = static_cast
1894 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1897 unsigned int got2 = object->got2_shndx();
1898 got_addr = g->first.object_->get_output_section_offset(got2);
1899 gold_assert(got_addr != invalid_address);
1900 got_addr += (g->first.object_->output_section(got2)->address()
1901 + g->first.addend_);
1906 Address pltoff = plt_addr - got_addr;
1907 if (ha(pltoff) == 0)
1909 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
1910 write_insn<big_endian>(p + 4, mtctr_11);
1911 write_insn<big_endian>(p + 8, bctr);
1915 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
1916 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
1917 write_insn<big_endian>(p + 8, mtctr_11);
1918 write_insn<big_endian>(p + 12, bctr);
1923 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
1924 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
1925 write_insn<big_endian>(p + 8, mtctr_11);
1926 write_insn<big_endian>(p + 12, bctr);
1930 // Write out pltresolve branch table.
1931 p = oview + this->pltresolve_;
1932 unsigned int the_end = oview_size - this->pltresolve_size;
1933 unsigned char* end_p = oview + the_end;
1934 while (p < end_p - 8 * 4)
1935 write_insn<big_endian>(p, b + end_p - p), p += 4;
1937 write_insn<big_endian>(p, nop), p += 4;
1939 // Write out pltresolve call stub.
1940 if (parameters->options().output_is_position_independent())
1942 Address res0_off = this->pltresolve_;
1943 Address after_bcl_off = the_end + 12;
1944 Address bcl_res0 = after_bcl_off - res0_off;
1946 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
1947 write_insn<big_endian>(p + 4, mflr_0);
1948 write_insn<big_endian>(p + 8, bcl_20_31);
1949 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
1950 write_insn<big_endian>(p + 16, mflr_12);
1951 write_insn<big_endian>(p + 20, mtlr_0);
1952 write_insn<big_endian>(p + 24, sub_11_11_12);
1954 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
1956 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
1957 if (ha(got_bcl) == ha(got_bcl + 4))
1959 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
1960 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
1964 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
1965 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
1967 write_insn<big_endian>(p + 40, mtctr_0);
1968 write_insn<big_endian>(p + 44, add_0_11_11);
1969 write_insn<big_endian>(p + 48, add_11_0_11);
1970 write_insn<big_endian>(p + 52, bctr);
1971 write_insn<big_endian>(p + 56, nop);
1972 write_insn<big_endian>(p + 60, nop);
1976 Address res0 = this->pltresolve_ + this->address();
1978 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
1979 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
1980 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1981 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
1983 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
1984 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
1985 write_insn<big_endian>(p + 16, mtctr_0);
1986 write_insn<big_endian>(p + 20, add_0_11_11);
1987 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1988 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
1990 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
1991 write_insn<big_endian>(p + 28, add_11_0_11);
1992 write_insn<big_endian>(p + 32, bctr);
1993 write_insn<big_endian>(p + 36, nop);
1994 write_insn<big_endian>(p + 40, nop);
1995 write_insn<big_endian>(p + 44, nop);
1996 write_insn<big_endian>(p + 48, nop);
1997 write_insn<big_endian>(p + 52, nop);
1998 write_insn<big_endian>(p + 56, nop);
1999 write_insn<big_endian>(p + 60, nop);
2004 of->write_output_view(off, oview_size, oview);
2007 // Create the glink section.
2009 template<int size, bool big_endian>
2011 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
2013 if (this->glink_ == NULL)
2015 this->glink_ = new Output_data_glink<size, big_endian>(this);
2016 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2017 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2018 this->glink_, ORDER_TEXT, false);
2022 // Create a PLT entry for a global symbol.
2024 template<int size, bool big_endian>
2026 Target_powerpc<size, big_endian>::make_plt_entry(
2029 const elfcpp::Rela<size, big_endian>& reloc,
2030 const Sized_relobj<size, big_endian>* object)
2032 if (this->plt_ == NULL)
2033 this->make_plt_section(layout);
2035 this->plt_->add_entry(gsym);
2037 this->glink_->add_entry(gsym, reloc, object);
2040 // Return the number of entries in the PLT.
2042 template<int size, bool big_endian>
2044 Target_powerpc<size, big_endian>::plt_entry_count() const
2046 if (this->plt_ == NULL)
2048 return this->plt_->entry_count();
2051 // Return the offset of the first non-reserved PLT entry.
2053 template<int size, bool big_endian>
2055 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
2057 return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
2060 // Return the size of each PLT entry.
2062 template<int size, bool big_endian>
2064 Target_powerpc<size, big_endian>::plt_entry_size() const
2066 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
2069 // Create a GOT entry for local dynamic __tls_get_addr calls.
2071 template<int size, bool big_endian>
2073 Target_powerpc<size, big_endian>::tlsld_got_offset(
2074 Symbol_table* symtab,
2076 Sized_relobj_file<size, big_endian>* object)
2078 if (this->tlsld_got_offset_ == -1U)
2080 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2081 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2082 Output_data_got_powerpc<size, big_endian>* got
2083 = this->got_section(symtab, layout);
2084 unsigned int got_offset = got->add_constant_pair(0, 0);
2085 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
2087 this->tlsld_got_offset_ = got_offset;
2089 return this->tlsld_got_offset_;
2092 // Get the Reference_flags for a particular relocation.
2094 template<int size, bool big_endian>
2096 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
2100 case elfcpp::R_POWERPC_NONE:
2101 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2102 case elfcpp::R_POWERPC_GNU_VTENTRY:
2103 case elfcpp::R_PPC64_TOC:
2104 // No symbol reference.
2107 case elfcpp::R_PPC64_ADDR64:
2108 case elfcpp::R_PPC64_UADDR64:
2109 case elfcpp::R_POWERPC_ADDR32:
2110 case elfcpp::R_POWERPC_UADDR32:
2111 case elfcpp::R_POWERPC_ADDR16:
2112 case elfcpp::R_POWERPC_UADDR16:
2113 case elfcpp::R_POWERPC_ADDR16_LO:
2114 case elfcpp::R_POWERPC_ADDR16_HI:
2115 case elfcpp::R_POWERPC_ADDR16_HA:
2116 return Symbol::ABSOLUTE_REF;
2118 case elfcpp::R_POWERPC_ADDR24:
2119 case elfcpp::R_POWERPC_ADDR14:
2120 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2121 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2122 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2124 case elfcpp::R_POWERPC_REL32:
2125 case elfcpp::R_PPC_LOCAL24PC:
2126 case elfcpp::R_POWERPC_REL16:
2127 case elfcpp::R_POWERPC_REL16_LO:
2128 case elfcpp::R_POWERPC_REL16_HI:
2129 case elfcpp::R_POWERPC_REL16_HA:
2130 return Symbol::RELATIVE_REF;
2132 case elfcpp::R_POWERPC_REL24:
2133 case elfcpp::R_PPC_PLTREL24:
2134 case elfcpp::R_POWERPC_REL14:
2135 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2136 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2137 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2139 case elfcpp::R_POWERPC_GOT16:
2140 case elfcpp::R_POWERPC_GOT16_LO:
2141 case elfcpp::R_POWERPC_GOT16_HI:
2142 case elfcpp::R_POWERPC_GOT16_HA:
2143 case elfcpp::R_PPC64_TOC16:
2144 case elfcpp::R_PPC64_TOC16_LO:
2145 case elfcpp::R_PPC64_TOC16_HI:
2146 case elfcpp::R_PPC64_TOC16_HA:
2147 case elfcpp::R_PPC64_TOC16_DS:
2148 case elfcpp::R_PPC64_TOC16_LO_DS:
2150 return Symbol::ABSOLUTE_REF;
2152 case elfcpp::R_POWERPC_GOT_TPREL16:
2153 case elfcpp::R_POWERPC_TLS:
2154 return Symbol::TLS_REF;
2156 case elfcpp::R_POWERPC_COPY:
2157 case elfcpp::R_POWERPC_GLOB_DAT:
2158 case elfcpp::R_POWERPC_JMP_SLOT:
2159 case elfcpp::R_POWERPC_RELATIVE:
2160 case elfcpp::R_POWERPC_DTPMOD:
2162 // Not expected. We will give an error later.
2167 // Report an unsupported relocation against a local symbol.
2169 template<int size, bool big_endian>
2171 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
2172 Sized_relobj_file<size, big_endian>* object,
2173 unsigned int r_type)
2175 gold_error(_("%s: unsupported reloc %u against local symbol"),
2176 object->name().c_str(), r_type);
2179 // We are about to emit a dynamic relocation of type R_TYPE. If the
2180 // dynamic linker does not support it, issue an error.
2182 template<int size, bool big_endian>
2184 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
2185 unsigned int r_type)
2187 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
2189 // These are the relocation types supported by glibc for both 32-bit
2190 // and 64-bit powerpc.
2193 case elfcpp::R_POWERPC_RELATIVE:
2194 case elfcpp::R_POWERPC_GLOB_DAT:
2195 case elfcpp::R_POWERPC_DTPMOD:
2196 case elfcpp::R_POWERPC_DTPREL:
2197 case elfcpp::R_POWERPC_TPREL:
2198 case elfcpp::R_POWERPC_JMP_SLOT:
2199 case elfcpp::R_POWERPC_COPY:
2200 case elfcpp::R_POWERPC_ADDR32:
2201 case elfcpp::R_POWERPC_ADDR24:
2202 case elfcpp::R_POWERPC_REL24:
2213 // These are the relocation types supported only on 64-bit.
2214 case elfcpp::R_PPC64_ADDR64:
2215 case elfcpp::R_PPC64_TPREL16_LO_DS:
2216 case elfcpp::R_PPC64_TPREL16_DS:
2217 case elfcpp::R_POWERPC_TPREL16:
2218 case elfcpp::R_POWERPC_TPREL16_LO:
2219 case elfcpp::R_POWERPC_TPREL16_HI:
2220 case elfcpp::R_POWERPC_TPREL16_HA:
2221 case elfcpp::R_PPC64_TPREL16_HIGHER:
2222 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2223 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2224 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2225 case elfcpp::R_PPC64_ADDR16_LO_DS:
2226 case elfcpp::R_POWERPC_ADDR16_LO:
2227 case elfcpp::R_POWERPC_ADDR16_HI:
2228 case elfcpp::R_POWERPC_ADDR16_HA:
2229 case elfcpp::R_POWERPC_ADDR30:
2230 case elfcpp::R_PPC64_UADDR64:
2231 case elfcpp::R_POWERPC_UADDR32:
2232 case elfcpp::R_POWERPC_ADDR16:
2233 case elfcpp::R_POWERPC_UADDR16:
2234 case elfcpp::R_PPC64_ADDR16_DS:
2235 case elfcpp::R_PPC64_ADDR16_HIGHER:
2236 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2237 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2238 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2239 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2240 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2241 case elfcpp::R_POWERPC_REL32:
2242 case elfcpp::R_PPC64_REL64:
2253 // These are the relocation types supported only on 32-bit.
2260 // This prevents us from issuing more than one error per reloc
2261 // section. But we can still wind up issuing more than one
2262 // error per object file.
2263 if (this->issued_non_pic_error_)
2265 gold_assert(parameters->options().output_is_position_independent());
2266 object->error(_("requires unsupported dynamic reloc; "
2267 "recompile with -fPIC"));
2268 this->issued_non_pic_error_ = true;
2272 // Scan a relocation for a local symbol.
2274 template<int size, bool big_endian>
2276 Target_powerpc<size, big_endian>::Scan::local(
2277 Symbol_table* symtab,
2279 Target_powerpc<size, big_endian>* target,
2280 Sized_relobj_file<size, big_endian>* object,
2281 unsigned int data_shndx,
2282 Output_section* output_section,
2283 const elfcpp::Rela<size, big_endian>& reloc,
2284 unsigned int r_type,
2285 const elfcpp::Sym<size, big_endian>& lsym)
2287 Powerpc_relobj<size, big_endian>* ppc_object
2288 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2292 case elfcpp::R_POWERPC_NONE:
2293 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2294 case elfcpp::R_POWERPC_GNU_VTENTRY:
2295 case elfcpp::R_PPC64_TOCSAVE:
2296 case elfcpp::R_PPC_EMB_MRKREF:
2299 case elfcpp::R_PPC64_TOC:
2301 Output_data_got_powerpc<size, big_endian>* got
2302 = target->got_section(symtab, layout);
2303 if (parameters->options().output_is_position_independent())
2305 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2306 rela_dyn->add_output_section_relative(got->output_section(),
2307 elfcpp::R_POWERPC_RELATIVE,
2310 reloc.get_r_offset(),
2311 ppc_object->toc_base_offset());
2316 case elfcpp::R_PPC64_ADDR64:
2317 case elfcpp::R_PPC64_UADDR64:
2318 case elfcpp::R_POWERPC_ADDR32:
2319 case elfcpp::R_POWERPC_UADDR32:
2320 case elfcpp::R_POWERPC_ADDR24:
2321 case elfcpp::R_POWERPC_ADDR16:
2322 case elfcpp::R_POWERPC_ADDR16_LO:
2323 case elfcpp::R_POWERPC_ADDR16_HI:
2324 case elfcpp::R_POWERPC_ADDR16_HA:
2325 case elfcpp::R_POWERPC_UADDR16:
2326 case elfcpp::R_PPC64_ADDR16_HIGHER:
2327 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2328 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2329 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2330 case elfcpp::R_PPC64_ADDR16_DS:
2331 case elfcpp::R_PPC64_ADDR16_LO_DS:
2332 case elfcpp::R_POWERPC_ADDR14:
2333 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2334 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2335 // If building a shared library (or a position-independent
2336 // executable), we need to create a dynamic relocation for
2338 if (parameters->options().output_is_position_independent())
2340 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2342 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2343 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2345 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2346 rela_dyn->add_local_relative(object, r_sym,
2347 elfcpp::R_POWERPC_RELATIVE,
2348 output_section, data_shndx,
2349 reloc.get_r_offset(),
2350 reloc.get_r_addend(), false);
2354 check_non_pic(object, r_type);
2355 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2356 rela_dyn->add_local(object, r_sym, r_type, output_section,
2357 data_shndx, reloc.get_r_offset(),
2358 reloc.get_r_addend());
2363 case elfcpp::R_POWERPC_REL32:
2364 case elfcpp::R_POWERPC_REL24:
2365 case elfcpp::R_PPC_LOCAL24PC:
2366 case elfcpp::R_POWERPC_REL16:
2367 case elfcpp::R_POWERPC_REL16_LO:
2368 case elfcpp::R_POWERPC_REL16_HI:
2369 case elfcpp::R_POWERPC_REL16_HA:
2370 case elfcpp::R_POWERPC_REL14:
2371 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2372 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2373 case elfcpp::R_POWERPC_SECTOFF:
2374 case elfcpp::R_POWERPC_TPREL16:
2375 case elfcpp::R_POWERPC_DTPREL16:
2376 case elfcpp::R_POWERPC_SECTOFF_LO:
2377 case elfcpp::R_POWERPC_TPREL16_LO:
2378 case elfcpp::R_POWERPC_DTPREL16_LO:
2379 case elfcpp::R_POWERPC_SECTOFF_HI:
2380 case elfcpp::R_POWERPC_TPREL16_HI:
2381 case elfcpp::R_POWERPC_DTPREL16_HI:
2382 case elfcpp::R_POWERPC_SECTOFF_HA:
2383 case elfcpp::R_POWERPC_TPREL16_HA:
2384 case elfcpp::R_POWERPC_DTPREL16_HA:
2385 case elfcpp::R_PPC64_DTPREL16_HIGHER:
2386 case elfcpp::R_PPC64_TPREL16_HIGHER:
2387 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2388 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2389 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2390 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2391 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2392 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2393 case elfcpp::R_PPC64_TPREL16_DS:
2394 case elfcpp::R_PPC64_TPREL16_LO_DS:
2395 case elfcpp::R_PPC64_DTPREL16_DS:
2396 case elfcpp::R_PPC64_DTPREL16_LO_DS:
2397 case elfcpp::R_PPC64_SECTOFF_DS:
2398 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2399 case elfcpp::R_PPC64_TLSGD:
2400 case elfcpp::R_PPC64_TLSLD:
2403 case elfcpp::R_POWERPC_GOT16:
2404 case elfcpp::R_POWERPC_GOT16_LO:
2405 case elfcpp::R_POWERPC_GOT16_HI:
2406 case elfcpp::R_POWERPC_GOT16_HA:
2407 case elfcpp::R_PPC64_GOT16_DS:
2408 case elfcpp::R_PPC64_GOT16_LO_DS:
2410 // The symbol requires a GOT entry.
2411 Output_data_got_powerpc<size, big_endian>* got
2412 = target->got_section(symtab, layout);
2413 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2415 // If we are generating a shared object, we need to add a
2416 // dynamic relocation for this symbol's GOT entry.
2417 if (parameters->options().output_is_position_independent())
2419 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
2421 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2424 off = got->add_constant(0);
2425 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
2426 rela_dyn->add_local_relative(object, r_sym,
2427 elfcpp::R_POWERPC_RELATIVE,
2428 got, off, 0, false);
2432 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2436 case elfcpp::R_PPC64_TOC16:
2437 case elfcpp::R_PPC64_TOC16_LO:
2438 case elfcpp::R_PPC64_TOC16_HI:
2439 case elfcpp::R_PPC64_TOC16_HA:
2440 case elfcpp::R_PPC64_TOC16_DS:
2441 case elfcpp::R_PPC64_TOC16_LO_DS:
2442 // We need a GOT section.
2443 target->got_section(symtab, layout);
2446 case elfcpp::R_POWERPC_GOT_TLSGD16:
2447 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2448 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2449 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2451 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
2452 if (tls_type == tls::TLSOPT_NONE)
2454 Output_data_got_powerpc<size, big_endian>* got
2455 = target->got_section(symtab, layout);
2456 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2457 unsigned int shndx = lsym.get_st_shndx();
2459 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2460 gold_assert(is_ordinary);
2461 got->add_local_pair_with_rel(object, r_sym,
2464 target->rela_dyn_section(layout),
2465 elfcpp::R_POWERPC_DTPMOD,
2466 elfcpp::R_POWERPC_DTPREL);
2468 else if (tls_type == tls::TLSOPT_TO_LE)
2470 // no GOT relocs needed for Local Exec.
2477 case elfcpp::R_POWERPC_GOT_TLSLD16:
2478 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2479 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2480 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2482 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2483 if (tls_type == tls::TLSOPT_NONE)
2484 target->tlsld_got_offset(symtab, layout, object);
2485 else if (tls_type == tls::TLSOPT_TO_LE)
2487 // no GOT relocs needed for Local Exec.
2494 case elfcpp::R_POWERPC_GOT_DTPREL16:
2495 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2496 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2497 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2499 Output_data_got_powerpc<size, big_endian>* got
2500 = target->got_section(symtab, layout);
2501 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2502 got->add_local_with_rel(object, r_sym, GOT_TYPE_DTPREL,
2503 target->rela_dyn_section(layout),
2504 elfcpp::R_POWERPC_DTPREL);
2508 case elfcpp::R_POWERPC_GOT_TPREL16:
2509 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2510 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2511 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2513 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
2514 if (tls_type == tls::TLSOPT_NONE)
2516 Output_data_got_powerpc<size, big_endian>* got
2517 = target->got_section(symtab, layout);
2518 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2519 got->add_local_with_rel(object, r_sym, GOT_TYPE_TPREL,
2520 target->rela_dyn_section(layout),
2521 elfcpp::R_POWERPC_TPREL);
2523 else if (tls_type == tls::TLSOPT_TO_LE)
2525 // no GOT relocs needed for Local Exec.
2533 unsupported_reloc_local(object, r_type);
2538 // Report an unsupported relocation against a global symbol.
2540 template<int size, bool big_endian>
2542 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
2543 Sized_relobj_file<size, big_endian>* object,
2544 unsigned int r_type,
2547 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2548 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2551 // Scan a relocation for a global symbol.
2553 template<int size, bool big_endian>
2555 Target_powerpc<size, big_endian>::Scan::global(
2556 Symbol_table* symtab,
2558 Target_powerpc<size, big_endian>* target,
2559 Sized_relobj_file<size, big_endian>* object,
2560 unsigned int data_shndx,
2561 Output_section* output_section,
2562 const elfcpp::Rela<size, big_endian>& reloc,
2563 unsigned int r_type,
2566 Powerpc_relobj<size, big_endian>* ppc_object
2567 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2571 case elfcpp::R_POWERPC_NONE:
2572 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2573 case elfcpp::R_POWERPC_GNU_VTENTRY:
2574 case elfcpp::R_PPC_LOCAL24PC:
2575 case elfcpp::R_PPC_EMB_MRKREF:
2578 case elfcpp::R_PPC64_TOC:
2580 Output_data_got_powerpc<size, big_endian>* got
2581 = target->got_section(symtab, layout);
2582 if (parameters->options().output_is_position_independent())
2584 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2585 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
2586 if (data_shndx != ppc_object->opd_shndx())
2587 symobj = static_cast
2588 <Powerpc_relobj<size, big_endian>*>(gsym->object());
2589 rela_dyn->add_output_section_relative(got->output_section(),
2590 elfcpp::R_POWERPC_RELATIVE,
2593 reloc.get_r_offset(),
2594 symobj->toc_base_offset());
2599 case elfcpp::R_PPC64_ADDR64:
2600 case elfcpp::R_PPC64_UADDR64:
2601 case elfcpp::R_POWERPC_ADDR32:
2602 case elfcpp::R_POWERPC_UADDR32:
2603 case elfcpp::R_POWERPC_ADDR24:
2604 case elfcpp::R_POWERPC_ADDR16:
2605 case elfcpp::R_POWERPC_ADDR16_LO:
2606 case elfcpp::R_POWERPC_ADDR16_HI:
2607 case elfcpp::R_POWERPC_ADDR16_HA:
2608 case elfcpp::R_POWERPC_UADDR16:
2609 case elfcpp::R_PPC64_ADDR16_HIGHER:
2610 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2611 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2612 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2613 case elfcpp::R_PPC64_ADDR16_DS:
2614 case elfcpp::R_PPC64_ADDR16_LO_DS:
2615 case elfcpp::R_POWERPC_ADDR14:
2616 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2617 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2619 // Make a PLT entry if necessary.
2620 if (gsym->needs_plt_entry())
2622 target->make_plt_entry(layout, gsym, reloc, 0);
2623 // Since this is not a PC-relative relocation, we may be
2624 // taking the address of a function. In that case we need to
2625 // set the entry in the dynamic symbol table to the address of
2628 && gsym->is_from_dynobj() && !parameters->options().shared())
2629 gsym->set_needs_dynsym_value();
2631 // Make a dynamic relocation if necessary.
2632 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
2634 if (gsym->may_need_copy_reloc())
2636 target->copy_reloc(symtab, layout, object,
2637 data_shndx, output_section, gsym, reloc);
2639 else if (((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2640 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2641 && (gsym->can_use_relative_reloc(false)
2642 || data_shndx == ppc_object->opd_shndx()))
2644 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2645 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2646 output_section, object,
2647 data_shndx, reloc.get_r_offset(),
2648 reloc.get_r_addend(), false);
2652 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2653 check_non_pic(object, r_type);
2654 rela_dyn->add_global(gsym, r_type, output_section,
2656 reloc.get_r_offset(),
2657 reloc.get_r_addend());
2663 case elfcpp::R_PPC_PLTREL24:
2664 case elfcpp::R_POWERPC_REL24:
2666 if (gsym->needs_plt_entry()
2667 || (!gsym->final_value_is_known()
2668 && !(gsym->is_defined()
2669 && !gsym->is_from_dynobj()
2670 && !gsym->is_preemptible())))
2671 target->make_plt_entry(layout, gsym, reloc, object);
2672 // Make a dynamic relocation if necessary.
2673 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
2675 if (gsym->may_need_copy_reloc())
2677 target->copy_reloc(symtab, layout, object,
2678 data_shndx, output_section, gsym,
2683 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2684 check_non_pic(object, r_type);
2685 rela_dyn->add_global(gsym, r_type, output_section, object,
2686 data_shndx, reloc.get_r_offset(),
2687 reloc.get_r_addend());
2693 case elfcpp::R_POWERPC_REL32:
2694 case elfcpp::R_POWERPC_REL16:
2695 case elfcpp::R_POWERPC_REL16_LO:
2696 case elfcpp::R_POWERPC_REL16_HI:
2697 case elfcpp::R_POWERPC_REL16_HA:
2698 case elfcpp::R_POWERPC_REL14:
2699 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2700 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2701 case elfcpp::R_POWERPC_SECTOFF:
2702 case elfcpp::R_POWERPC_TPREL16:
2703 case elfcpp::R_POWERPC_DTPREL16:
2704 case elfcpp::R_POWERPC_SECTOFF_LO:
2705 case elfcpp::R_POWERPC_TPREL16_LO:
2706 case elfcpp::R_POWERPC_DTPREL16_LO:
2707 case elfcpp::R_POWERPC_SECTOFF_HI:
2708 case elfcpp::R_POWERPC_TPREL16_HI:
2709 case elfcpp::R_POWERPC_DTPREL16_HI:
2710 case elfcpp::R_POWERPC_SECTOFF_HA:
2711 case elfcpp::R_POWERPC_TPREL16_HA:
2712 case elfcpp::R_POWERPC_DTPREL16_HA:
2713 case elfcpp::R_PPC64_DTPREL16_HIGHER:
2714 case elfcpp::R_PPC64_TPREL16_HIGHER:
2715 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2716 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2717 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2718 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2719 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2720 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2721 case elfcpp::R_PPC64_TPREL16_DS:
2722 case elfcpp::R_PPC64_TPREL16_LO_DS:
2723 case elfcpp::R_PPC64_DTPREL16_DS:
2724 case elfcpp::R_PPC64_DTPREL16_LO_DS:
2725 case elfcpp::R_PPC64_SECTOFF_DS:
2726 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2727 case elfcpp::R_PPC64_TLSGD:
2728 case elfcpp::R_PPC64_TLSLD:
2731 case elfcpp::R_POWERPC_GOT16:
2732 case elfcpp::R_POWERPC_GOT16_LO:
2733 case elfcpp::R_POWERPC_GOT16_HI:
2734 case elfcpp::R_POWERPC_GOT16_HA:
2735 case elfcpp::R_PPC64_GOT16_DS:
2736 case elfcpp::R_PPC64_GOT16_LO_DS:
2738 // The symbol requires a GOT entry.
2739 Output_data_got_powerpc<size, big_endian>* got;
2741 got = target->got_section(symtab, layout);
2742 if (gsym->final_value_is_known())
2743 got->add_global(gsym, GOT_TYPE_STANDARD);
2746 // If this symbol is not fully resolved, we need to add a
2747 // dynamic relocation for it.
2748 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2749 if (gsym->is_from_dynobj()
2750 || gsym->is_undefined()
2751 || gsym->is_preemptible())
2752 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2753 elfcpp::R_POWERPC_GLOB_DAT);
2754 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2756 unsigned int off = got->add_constant(0);
2758 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2759 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2760 got, off, 0, false);
2766 case elfcpp::R_PPC64_TOC16:
2767 case elfcpp::R_PPC64_TOC16_LO:
2768 case elfcpp::R_PPC64_TOC16_HI:
2769 case elfcpp::R_PPC64_TOC16_HA:
2770 case elfcpp::R_PPC64_TOC16_DS:
2771 case elfcpp::R_PPC64_TOC16_LO_DS:
2772 // We need a GOT section.
2773 target->got_section(symtab, layout);
2776 case elfcpp::R_POWERPC_GOT_TLSGD16:
2777 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2778 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2779 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2781 const bool final = gsym->final_value_is_known();
2782 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
2783 if (tls_type == tls::TLSOPT_NONE)
2785 Output_data_got_powerpc<size, big_endian>* got
2786 = target->got_section(symtab, layout);
2787 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
2788 target->rela_dyn_section(layout),
2789 elfcpp::R_POWERPC_DTPMOD,
2790 elfcpp::R_POWERPC_DTPREL);
2792 else if (tls_type == tls::TLSOPT_TO_IE)
2794 Output_data_got_powerpc<size, big_endian>* got
2795 = target->got_section(symtab, layout);
2796 got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
2797 target->rela_dyn_section(layout),
2798 elfcpp::R_POWERPC_TPREL);
2800 else if (tls_type == tls::TLSOPT_TO_LE)
2802 // no GOT relocs needed for Local Exec.
2809 case elfcpp::R_POWERPC_GOT_TLSLD16:
2810 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2811 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2812 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2814 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2815 if (tls_type == tls::TLSOPT_NONE)
2816 target->tlsld_got_offset(symtab, layout, object);
2817 else if (tls_type == tls::TLSOPT_TO_LE)
2819 // no GOT relocs needed for Local Exec.
2826 case elfcpp::R_POWERPC_GOT_DTPREL16:
2827 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2828 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2829 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2831 Output_data_got_powerpc<size, big_endian>* got
2832 = target->got_section(symtab, layout);
2833 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
2834 target->rela_dyn_section(layout),
2835 elfcpp::R_POWERPC_DTPREL);
2839 case elfcpp::R_POWERPC_GOT_TPREL16:
2840 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2841 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2842 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2844 const bool final = gsym->final_value_is_known();
2845 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
2846 if (tls_type == tls::TLSOPT_NONE)
2848 Output_data_got_powerpc<size, big_endian>* got
2849 = target->got_section(symtab, layout);
2850 got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
2851 target->rela_dyn_section(layout),
2852 elfcpp::R_POWERPC_TPREL);
2854 else if (tls_type == tls::TLSOPT_TO_LE)
2856 // no GOT relocs needed for Local Exec.
2864 unsupported_reloc_global(object, r_type, gsym);
2869 // Process relocations for gc.
2871 template<int size, bool big_endian>
2873 Target_powerpc<size, big_endian>::gc_process_relocs(
2874 Symbol_table* symtab,
2876 Sized_relobj_file<size, big_endian>* object,
2877 unsigned int data_shndx,
2879 const unsigned char* prelocs,
2881 Output_section* output_section,
2882 bool needs_special_offset_handling,
2883 size_t local_symbol_count,
2884 const unsigned char* plocal_symbols)
2886 typedef Target_powerpc<size, big_endian> Powerpc;
2887 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2889 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
2890 typename Target_powerpc::Relocatable_size_for_reloc>(
2899 needs_special_offset_handling,
2904 // Scan relocations for a section.
2906 template<int size, bool big_endian>
2908 Target_powerpc<size, big_endian>::scan_relocs(
2909 Symbol_table* symtab,
2911 Sized_relobj_file<size, big_endian>* object,
2912 unsigned int data_shndx,
2913 unsigned int sh_type,
2914 const unsigned char* prelocs,
2916 Output_section* output_section,
2917 bool needs_special_offset_handling,
2918 size_t local_symbol_count,
2919 const unsigned char* plocal_symbols)
2921 typedef Target_powerpc<size, big_endian> Powerpc;
2922 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2924 if (sh_type == elfcpp::SHT_REL)
2926 gold_error(_("%s: unsupported REL reloc section"),
2927 object->name().c_str());
2933 static Output_data_space* sdata;
2935 // Define _SDA_BASE_ at the start of the .sdata section.
2938 // layout->find_output_section(".sdata") == NULL
2939 sdata = new Output_data_space(4, "** sdata");
2941 = layout->add_output_section_data(".sdata", 0,
2943 | elfcpp::SHF_WRITE,
2944 sdata, ORDER_SMALL_DATA, false);
2945 symtab->define_in_output_data("_SDA_BASE_", NULL,
2946 Symbol_table::PREDEFINED,
2947 os, 32768, 0, elfcpp::STT_OBJECT,
2948 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2953 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
2962 needs_special_offset_handling,
2967 // Finalize the sections.
2969 template<int size, bool big_endian>
2971 Target_powerpc<size, big_endian>::do_finalize_sections(
2973 const Input_objects*,
2976 // Fill in some more dynamic tags.
2977 const Reloc_section* rel_plt = (this->plt_ == NULL
2979 : this->plt_->rel_plt());
2980 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
2981 this->rela_dyn_, true, size == 32);
2983 Output_data_dynamic* odyn = layout->dynamic_data();
2986 if (this->got_ != NULL)
2988 this->got_->finalize_data_size();
2989 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
2990 this->got_, this->got_->g_o_t());
2995 if (this->glink_ != NULL)
2997 this->glink_->finalize_data_size();
2998 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
3000 (this->glink_->pltresolve()
3001 + this->glink_->pltresolve_size - 32));
3005 // Emit any relocs we saved in an attempt to avoid generating COPY
3007 if (this->copy_relocs_.any_saved_relocs())
3008 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3011 // Perform a relocation.
3013 template<int size, bool big_endian>
3015 Target_powerpc<size, big_endian>::Relocate::relocate(
3016 const Relocate_info<size, big_endian>* relinfo,
3017 Target_powerpc* target,
3020 const elfcpp::Rela<size, big_endian>& rela,
3021 unsigned int r_type,
3022 const Sized_symbol<size>* gsym,
3023 const Symbol_value<size>* psymval,
3024 unsigned char* view,
3026 section_size_type view_size)
3029 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
3030 || r_type == elfcpp::R_PPC_PLTREL24)
3032 && strcmp(gsym->name(), "__tls_get_addr") == 0);
3033 enum skip_tls last_tls = this->call_tls_get_addr_;
3034 this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
3037 if (last_tls == CALL_NOT_EXPECTED)
3038 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3039 _("__tls_get_addr call lacks marker reloc"));
3040 else if (last_tls == CALL_SKIP)
3043 else if (last_tls != CALL_NOT_EXPECTED)
3044 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3045 _("missing expected __tls_get_addr call"));
3047 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
3048 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
3049 const Powerpc_relobj<size, big_endian>* const object
3050 = static_cast<const Powerpc_relobj<size, big_endian>*>(relinfo->object);
3052 bool has_plt_value = false;
3054 && use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type)))
3056 const Output_data_glink<size, big_endian>* glink
3057 = target->glink_section();
3058 unsigned int glink_index = glink->find_entry(gsym, rela, object);
3059 value = glink->address() + glink_index * glink->glink_entry_size();
3060 has_plt_value = true;
3063 if (r_type == elfcpp::R_POWERPC_GOT16
3064 || r_type == elfcpp::R_POWERPC_GOT16_LO
3065 || r_type == elfcpp::R_POWERPC_GOT16_HI
3066 || r_type == elfcpp::R_POWERPC_GOT16_HA
3067 || r_type == elfcpp::R_PPC64_GOT16_DS
3068 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
3072 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3073 value = gsym->got_offset(GOT_TYPE_STANDARD);
3077 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3078 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3079 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3081 value -= target->got_section()->got_base_offset(object);
3083 else if (r_type == elfcpp::R_PPC64_TOC)
3085 value = (target->got_section()->output_section()->address()
3086 + object->toc_base_offset());
3088 else if (gsym != NULL
3089 && (r_type == elfcpp::R_POWERPC_REL24
3090 || r_type == elfcpp::R_PPC_PLTREL24)
3095 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3096 Valtype* wv = reinterpret_cast<Valtype*>(view);
3097 bool can_plt_call = false;
3098 if (rela.get_r_offset() + 8 <= view_size)
3100 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3102 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31)
3104 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
3105 can_plt_call = true;
3109 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3110 _("call lacks nop, can't restore toc"));
3113 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3114 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
3115 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
3116 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
3118 // First instruction of a global dynamic sequence, arg setup insn.
3119 const bool final = gsym == NULL || gsym->final_value_is_known();
3120 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3121 enum Got_type got_type = GOT_TYPE_STANDARD;
3122 if (tls_type == tls::TLSOPT_NONE)
3123 got_type = GOT_TYPE_TLSGD;
3124 else if (tls_type == tls::TLSOPT_TO_IE)
3125 got_type = GOT_TYPE_TPREL;
3126 if (got_type != GOT_TYPE_STANDARD)
3130 gold_assert(gsym->has_got_offset(got_type));
3131 value = gsym->got_offset(got_type);
3135 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3136 gold_assert(object->local_has_got_offset(r_sym, got_type));
3137 value = object->local_got_offset(r_sym, got_type);
3139 value -= target->got_section()->got_base_offset(object);
3141 if (tls_type == tls::TLSOPT_TO_IE)
3143 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3144 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3146 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3147 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3148 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
3150 insn |= 32 << 26; // lwz
3152 insn |= 58 << 26; // ld
3153 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3155 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
3156 - elfcpp::R_POWERPC_GOT_TLSGD16);
3158 else if (tls_type == tls::TLSOPT_TO_LE)
3160 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3161 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3163 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3164 Insn insn = addis_3_13;
3167 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3168 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3169 value = psymval->value(object, rela.get_r_addend());
3173 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3175 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3176 r_type = elfcpp::R_POWERPC_NONE;
3180 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3181 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
3182 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
3183 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
3185 // First instruction of a local dynamic sequence, arg setup insn.
3186 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3187 if (tls_type == tls::TLSOPT_NONE)
3189 value = target->tlsld_got_offset();
3190 value -= target->got_section()->got_base_offset(object);
3194 gold_assert(tls_type == tls::TLSOPT_TO_LE);
3195 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3196 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
3198 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3199 Insn insn = addis_3_13;
3202 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3203 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3204 value = relinfo->layout->tls_segment()->vaddr() + dtp_offset;
3208 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3210 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3211 r_type = elfcpp::R_POWERPC_NONE;
3215 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
3216 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
3217 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
3218 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
3220 // Accesses relative to a local dynamic sequence address,
3221 // no optimisation here.
3224 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
3225 value = gsym->got_offset(GOT_TYPE_DTPREL);
3229 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3230 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
3231 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
3233 value -= target->got_section()->got_base_offset(object);
3235 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3236 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
3237 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
3238 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
3240 // First instruction of initial exec sequence.
3241 const bool final = gsym == NULL || gsym->final_value_is_known();
3242 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3243 if (tls_type == tls::TLSOPT_NONE)
3247 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
3248 value = gsym->got_offset(GOT_TYPE_TPREL);
3252 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3253 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
3254 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
3256 value -= target->got_section()->got_base_offset(object);
3260 gold_assert(tls_type == tls::TLSOPT_TO_LE);
3261 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3262 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
3264 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3265 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3266 insn &= (1 << 26) - (1 << 21); // extract rt from ld
3271 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3272 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3273 value = psymval->value(object, rela.get_r_addend());
3277 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3279 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3280 r_type = elfcpp::R_POWERPC_NONE;
3284 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
3285 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
3287 // Second instruction of a global dynamic sequence,
3288 // the __tls_get_addr call
3289 this->call_tls_get_addr_ = CALL_EXPECTED;
3290 const bool final = gsym == NULL || gsym->final_value_is_known();
3291 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3292 if (tls_type != tls::TLSOPT_NONE)
3294 if (tls_type == tls::TLSOPT_TO_IE)
3296 Insn* iview = reinterpret_cast<Insn*>(view);
3297 Insn insn = add_3_3_13;
3300 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3301 r_type = elfcpp::R_POWERPC_NONE;
3305 Insn* iview = reinterpret_cast<Insn*>(view);
3306 Insn insn = addi_3_3;
3307 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3308 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3309 view += 2 * big_endian;
3310 value = psymval->value(object, rela.get_r_addend());
3312 this->call_tls_get_addr_ = CALL_SKIP;
3315 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
3316 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
3318 // Second instruction of a local dynamic sequence,
3319 // the __tls_get_addr call
3320 this->call_tls_get_addr_ = CALL_EXPECTED;
3321 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3322 if (tls_type == tls::TLSOPT_TO_LE)
3324 Insn* iview = reinterpret_cast<Insn*>(view);
3325 Insn insn = addi_3_3;
3326 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3327 this->call_tls_get_addr_ = CALL_SKIP;
3328 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3329 view += 2 * big_endian;
3330 value = relinfo->layout->tls_segment()->vaddr() + dtp_offset;
3333 else if (r_type == elfcpp::R_POWERPC_TLS)
3335 // Second instruction of an initial exec sequence
3336 const bool final = gsym == NULL || gsym->final_value_is_known();
3337 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3338 if (tls_type == tls::TLSOPT_TO_LE)
3340 Insn* iview = reinterpret_cast<Insn*>(view);
3341 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3342 unsigned int reg = size == 32 ? 2 : 13;
3343 insn = at_tls_transform(insn, reg);
3344 gold_assert(insn != 0);
3345 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3346 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3347 view += 2 * big_endian;
3348 value = psymval->value(object, rela.get_r_addend());
3354 if (r_type != elfcpp::R_PPC_PLTREL24)
3355 addend = rela.get_r_addend();
3356 if (size == 64 || !has_plt_value)
3357 value = psymval->value(object, addend);
3358 if (size == 64 && is_branch_reloc(r_type))
3360 // If the symbol is defined in an opd section, ie. is a function
3361 // descriptor, use the function descriptor code entry address
3362 Powerpc_relobj<size, big_endian>* symobj = const_cast
3363 <Powerpc_relobj<size, big_endian>*>(object);
3365 symobj = static_cast
3366 <Powerpc_relobj<size, big_endian>*>(gsym->object());
3367 unsigned int shndx = symobj->opd_shndx();
3368 Address opd_addr = symobj->get_output_section_offset(shndx);
3369 gold_assert(opd_addr != invalid_address);
3370 opd_addr += symobj->output_section(shndx)->address();
3371 if (value >= opd_addr
3372 && value < opd_addr + symobj->section_size(shndx))
3375 symobj->get_opd_ent(value - opd_addr, &shndx, &sec_off);
3376 Address sec_addr = symobj->get_output_section_offset(shndx);
3377 gold_assert(sec_addr != invalid_address);
3378 sec_addr += symobj->output_section(shndx)->address();
3379 value = sec_addr + sec_off;
3386 case elfcpp::R_PPC64_REL64:
3387 case elfcpp::R_POWERPC_REL32:
3388 case elfcpp::R_POWERPC_REL24:
3389 case elfcpp::R_PPC_PLTREL24:
3390 case elfcpp::R_PPC_LOCAL24PC:
3391 case elfcpp::R_POWERPC_REL16:
3392 case elfcpp::R_POWERPC_REL16_LO:
3393 case elfcpp::R_POWERPC_REL16_HI:
3394 case elfcpp::R_POWERPC_REL16_HA:
3395 case elfcpp::R_POWERPC_REL14:
3396 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3397 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3401 case elfcpp::R_PPC64_TOC16:
3402 case elfcpp::R_PPC64_TOC16_LO:
3403 case elfcpp::R_PPC64_TOC16_HI:
3404 case elfcpp::R_PPC64_TOC16_HA:
3405 case elfcpp::R_PPC64_TOC16_DS:
3406 case elfcpp::R_PPC64_TOC16_LO_DS:
3407 // Subtract the TOC base address.
3408 value -= (target->got_section()->output_section()->address()
3409 + object->toc_base_offset());
3412 case elfcpp::R_POWERPC_SECTOFF:
3413 case elfcpp::R_POWERPC_SECTOFF_LO:
3414 case elfcpp::R_POWERPC_SECTOFF_HI:
3415 case elfcpp::R_POWERPC_SECTOFF_HA:
3416 case elfcpp::R_PPC64_SECTOFF_DS:
3417 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3419 value -= os->address();
3422 case elfcpp::R_PPC64_TPREL16_DS:
3423 case elfcpp::R_PPC64_TPREL16_LO_DS:
3425 // R_PPC_TLSGD and R_PPC_TLSLD
3427 case elfcpp::R_POWERPC_TPREL16:
3428 case elfcpp::R_POWERPC_TPREL16_LO:
3429 case elfcpp::R_POWERPC_TPREL16_HI:
3430 case elfcpp::R_POWERPC_TPREL16_HA:
3431 case elfcpp::R_POWERPC_TPREL:
3432 case elfcpp::R_PPC64_TPREL16_HIGHER:
3433 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3434 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3435 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3436 // tls symbol values are relative to tls_segment()->vaddr()
3440 case elfcpp::R_PPC64_DTPREL16_DS:
3441 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3442 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3443 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3444 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3445 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3447 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
3448 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
3450 case elfcpp::R_POWERPC_DTPREL16:
3451 case elfcpp::R_POWERPC_DTPREL16_LO:
3452 case elfcpp::R_POWERPC_DTPREL16_HI:
3453 case elfcpp::R_POWERPC_DTPREL16_HA:
3454 case elfcpp::R_POWERPC_DTPREL:
3455 // tls symbol values are relative to tls_segment()->vaddr()
3456 value -= dtp_offset;
3463 Insn branch_bit = 0;
3466 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3467 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3468 branch_bit = 1 << 21;
3469 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3470 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3472 Insn* iview = reinterpret_cast<Insn*>(view);
3473 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3476 if (this->is_isa_v2)
3478 // Set 'a' bit. This is 0b00010 in BO field for branch
3479 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
3480 // for branch on CTR insns (BO == 1a00t or 1a01t).
3481 if ((insn & (0x14 << 21)) == (0x04 << 21))
3483 else if ((insn & (0x14 << 21)) == (0x10 << 21))
3490 // Invert 'y' bit if not the default.
3491 if (static_cast<Signed_address>(value) < 0)
3494 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3502 enum Reloc::overflow_check overflow = Reloc::check_none;
3505 case elfcpp::R_POWERPC_ADDR32:
3506 case elfcpp::R_POWERPC_UADDR32:
3508 overflow = Reloc::check_bitfield;
3511 case elfcpp::R_POWERPC_REL32:
3513 overflow = Reloc::check_signed;
3516 case elfcpp::R_POWERPC_ADDR24:
3517 case elfcpp::R_POWERPC_ADDR16:
3518 case elfcpp::R_POWERPC_UADDR16:
3519 case elfcpp::R_PPC64_ADDR16_DS:
3520 case elfcpp::R_POWERPC_ADDR14:
3521 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3522 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3523 overflow = Reloc::check_bitfield;
3526 case elfcpp::R_POWERPC_REL24:
3527 case elfcpp::R_PPC_PLTREL24:
3528 case elfcpp::R_PPC_LOCAL24PC:
3529 case elfcpp::R_POWERPC_REL16:
3530 case elfcpp::R_PPC64_TOC16:
3531 case elfcpp::R_POWERPC_GOT16:
3532 case elfcpp::R_POWERPC_SECTOFF:
3533 case elfcpp::R_POWERPC_TPREL16:
3534 case elfcpp::R_POWERPC_DTPREL16:
3535 case elfcpp::R_PPC64_TPREL16_DS:
3536 case elfcpp::R_PPC64_DTPREL16_DS:
3537 case elfcpp::R_PPC64_TOC16_DS:
3538 case elfcpp::R_PPC64_GOT16_DS:
3539 case elfcpp::R_PPC64_SECTOFF_DS:
3540 case elfcpp::R_POWERPC_REL14:
3541 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3542 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3543 case elfcpp::R_POWERPC_GOT_TLSGD16:
3544 case elfcpp::R_POWERPC_GOT_TLSLD16:
3545 case elfcpp::R_POWERPC_GOT_TPREL16:
3546 case elfcpp::R_POWERPC_GOT_DTPREL16:
3547 overflow = Reloc::check_signed;
3553 case elfcpp::R_POWERPC_NONE:
3554 case elfcpp::R_POWERPC_TLS:
3555 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3556 case elfcpp::R_POWERPC_GNU_VTENTRY:
3557 case elfcpp::R_PPC_EMB_MRKREF:
3560 case elfcpp::R_PPC64_ADDR64:
3561 case elfcpp::R_PPC64_REL64:
3562 case elfcpp::R_PPC64_TOC:
3563 Reloc::addr64(view, value);
3566 case elfcpp::R_POWERPC_TPREL:
3567 case elfcpp::R_POWERPC_DTPREL:
3569 Reloc::addr64(view, value);
3571 Reloc::addr32(view, value, overflow);
3574 case elfcpp::R_PPC64_UADDR64:
3575 Reloc::addr64_u(view, value);
3578 case elfcpp::R_POWERPC_ADDR32:
3579 case elfcpp::R_POWERPC_REL32:
3580 Reloc::addr32(view, value, overflow);
3583 case elfcpp::R_POWERPC_UADDR32:
3584 Reloc::addr32_u(view, value, overflow);
3587 case elfcpp::R_POWERPC_ADDR24:
3588 case elfcpp::R_POWERPC_REL24:
3589 case elfcpp::R_PPC_PLTREL24:
3590 case elfcpp::R_PPC_LOCAL24PC:
3591 Reloc::addr24(view, value, overflow);
3594 case elfcpp::R_POWERPC_GOT_DTPREL16:
3595 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3598 Reloc::addr16_ds(view, value, overflow);
3601 case elfcpp::R_POWERPC_ADDR16:
3602 case elfcpp::R_POWERPC_REL16:
3603 case elfcpp::R_PPC64_TOC16:
3604 case elfcpp::R_POWERPC_GOT16:
3605 case elfcpp::R_POWERPC_SECTOFF:
3606 case elfcpp::R_POWERPC_TPREL16:
3607 case elfcpp::R_POWERPC_DTPREL16:
3608 case elfcpp::R_POWERPC_GOT_TLSGD16:
3609 case elfcpp::R_POWERPC_GOT_TLSLD16:
3610 case elfcpp::R_POWERPC_GOT_TPREL16:
3611 case elfcpp::R_POWERPC_ADDR16_LO:
3612 case elfcpp::R_POWERPC_REL16_LO:
3613 case elfcpp::R_PPC64_TOC16_LO:
3614 case elfcpp::R_POWERPC_GOT16_LO:
3615 case elfcpp::R_POWERPC_SECTOFF_LO:
3616 case elfcpp::R_POWERPC_TPREL16_LO:
3617 case elfcpp::R_POWERPC_DTPREL16_LO:
3618 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3619 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3620 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3621 Reloc::addr16(view, value, overflow);
3624 case elfcpp::R_POWERPC_UADDR16:
3625 Reloc::addr16_u(view, value, overflow);
3628 case elfcpp::R_POWERPC_ADDR16_HI:
3629 case elfcpp::R_POWERPC_REL16_HI:
3630 case elfcpp::R_PPC64_TOC16_HI:
3631 case elfcpp::R_POWERPC_GOT16_HI:
3632 case elfcpp::R_POWERPC_SECTOFF_HI:
3633 case elfcpp::R_POWERPC_TPREL16_HI:
3634 case elfcpp::R_POWERPC_DTPREL16_HI:
3635 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3636 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3637 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3638 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3639 Reloc::addr16_hi(view, value);
3642 case elfcpp::R_POWERPC_ADDR16_HA:
3643 case elfcpp::R_POWERPC_REL16_HA:
3644 case elfcpp::R_PPC64_TOC16_HA:
3645 case elfcpp::R_POWERPC_GOT16_HA:
3646 case elfcpp::R_POWERPC_SECTOFF_HA:
3647 case elfcpp::R_POWERPC_TPREL16_HA:
3648 case elfcpp::R_POWERPC_DTPREL16_HA:
3649 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3650 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3651 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3652 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3653 Reloc::addr16_ha(view, value);
3656 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3658 // R_PPC_EMB_NADDR16_LO
3660 case elfcpp::R_PPC64_ADDR16_HIGHER:
3661 case elfcpp::R_PPC64_TPREL16_HIGHER:
3662 Reloc::addr16_hi2(view, value);
3665 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3667 // R_PPC_EMB_NADDR16_HI
3669 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3670 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3671 Reloc::addr16_ha2(view, value);
3674 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3676 // R_PPC_EMB_NADDR16_HA
3678 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3679 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3680 Reloc::addr16_hi3(view, value);
3683 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3687 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3688 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3689 Reloc::addr16_ha3(view, value);
3692 case elfcpp::R_PPC64_DTPREL16_DS:
3693 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3695 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
3697 case elfcpp::R_PPC64_TPREL16_DS:
3698 case elfcpp::R_PPC64_TPREL16_LO_DS:
3700 // R_PPC_TLSGD, R_PPC_TLSLD
3702 case elfcpp::R_PPC64_ADDR16_DS:
3703 case elfcpp::R_PPC64_ADDR16_LO_DS:
3704 case elfcpp::R_PPC64_TOC16_DS:
3705 case elfcpp::R_PPC64_TOC16_LO_DS:
3706 case elfcpp::R_PPC64_GOT16_DS:
3707 case elfcpp::R_PPC64_GOT16_LO_DS:
3708 case elfcpp::R_PPC64_SECTOFF_DS:
3709 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3710 Reloc::addr16_ds(view, value, overflow);
3713 case elfcpp::R_POWERPC_ADDR14:
3714 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3715 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3716 case elfcpp::R_POWERPC_REL14:
3717 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3718 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3719 Reloc::addr14(view, value, overflow);
3722 case elfcpp::R_POWERPC_COPY:
3723 case elfcpp::R_POWERPC_GLOB_DAT:
3724 case elfcpp::R_POWERPC_JMP_SLOT:
3725 case elfcpp::R_POWERPC_RELATIVE:
3726 case elfcpp::R_POWERPC_DTPMOD:
3727 case elfcpp::R_PPC64_JMP_IREL:
3728 case elfcpp::R_POWERPC_IRELATIVE:
3729 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3730 _("unexpected reloc %u in object file"),
3734 case elfcpp::R_PPC_EMB_SDA21:
3739 // R_PPC64_TOCSAVE. For the time being this can be ignored.
3743 case elfcpp::R_PPC_EMB_SDA2I16:
3744 case elfcpp::R_PPC_EMB_SDA2REL:
3747 // R_PPC64_TLSGD, R_PPC64_TLSLD
3750 case elfcpp::R_POWERPC_PLT32:
3751 case elfcpp::R_POWERPC_PLTREL32:
3752 case elfcpp::R_POWERPC_PLT16_LO:
3753 case elfcpp::R_POWERPC_PLT16_HI:
3754 case elfcpp::R_POWERPC_PLT16_HA:
3755 case elfcpp::R_PPC_SDAREL16:
3756 case elfcpp::R_POWERPC_ADDR30:
3757 case elfcpp::R_PPC64_PLT64:
3758 case elfcpp::R_PPC64_PLTREL64:
3759 case elfcpp::R_PPC64_PLTGOT16:
3760 case elfcpp::R_PPC64_PLTGOT16_LO:
3761 case elfcpp::R_PPC64_PLTGOT16_HI:
3762 case elfcpp::R_PPC64_PLTGOT16_HA:
3763 case elfcpp::R_PPC64_PLT16_LO_DS:
3764 case elfcpp::R_PPC64_PLTGOT16_DS:
3765 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
3766 case elfcpp::R_PPC_EMB_RELSEC16:
3767 case elfcpp::R_PPC_EMB_RELST_LO:
3768 case elfcpp::R_PPC_EMB_RELST_HI:
3769 case elfcpp::R_PPC_EMB_RELST_HA:
3770 case elfcpp::R_PPC_EMB_BIT_FLD:
3771 case elfcpp::R_PPC_EMB_RELSDA:
3772 case elfcpp::R_PPC_TOC16:
3775 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3776 _("unsupported reloc %u"),
3784 // Relocate section data.
3786 template<int size, bool big_endian>
3788 Target_powerpc<size, big_endian>::relocate_section(
3789 const Relocate_info<size, big_endian>* relinfo,
3790 unsigned int sh_type,
3791 const unsigned char* prelocs,
3793 Output_section* output_section,
3794 bool needs_special_offset_handling,
3795 unsigned char* view,
3797 section_size_type view_size,
3798 const Reloc_symbol_changes* reloc_symbol_changes)
3800 typedef Target_powerpc<size, big_endian> Powerpc;
3801 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
3803 gold_assert(sh_type == elfcpp::SHT_RELA);
3805 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
3812 needs_special_offset_handling,
3816 reloc_symbol_changes);
3819 class Powerpc_scan_relocatable_reloc
3822 // Return the strategy to use for a local symbol which is not a
3823 // section symbol, given the relocation type.
3824 inline Relocatable_relocs::Reloc_strategy
3825 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
3827 if (r_type == 0 && r_sym == 0)
3828 return Relocatable_relocs::RELOC_DISCARD;
3829 return Relocatable_relocs::RELOC_COPY;
3832 // Return the strategy to use for a local symbol which is a section
3833 // symbol, given the relocation type.
3834 inline Relocatable_relocs::Reloc_strategy
3835 local_section_strategy(unsigned int, Relobj*)
3837 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
3840 // Return the strategy to use for a global symbol, given the
3841 // relocation type, the object, and the symbol index.
3842 inline Relocatable_relocs::Reloc_strategy
3843 global_strategy(unsigned int r_type, Relobj*, unsigned int)
3845 if (r_type == elfcpp::R_PPC_PLTREL24)
3846 return Relocatable_relocs::RELOC_SPECIAL;
3847 return Relocatable_relocs::RELOC_COPY;
3851 // Scan the relocs during a relocatable link.
3853 template<int size, bool big_endian>
3855 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
3856 Symbol_table* symtab,
3858 Sized_relobj_file<size, big_endian>* object,
3859 unsigned int data_shndx,
3860 unsigned int sh_type,
3861 const unsigned char* prelocs,
3863 Output_section* output_section,
3864 bool needs_special_offset_handling,
3865 size_t local_symbol_count,
3866 const unsigned char* plocal_symbols,
3867 Relocatable_relocs* rr)
3869 gold_assert(sh_type == elfcpp::SHT_RELA);
3871 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
3872 Powerpc_scan_relocatable_reloc>(
3880 needs_special_offset_handling,
3886 // Relocate a section during a relocatable link.
3887 // This is a modified version of the function by the same name in
3888 // target-reloc.h. Using relocate_special_relocatable for
3889 // R_PPC_PLTREL24 would require duplication of the entire body of the
3890 // loop, so we may as well duplicate the whole thing.
3892 template<int size, bool big_endian>
3894 Target_powerpc<size, big_endian>::relocate_for_relocatable(
3895 const Relocate_info<size, big_endian>* relinfo,
3896 unsigned int sh_type,
3897 const unsigned char* prelocs,
3899 Output_section* output_section,
3900 off_t offset_in_output_section,
3901 const Relocatable_relocs* rr,
3903 Address view_address,
3905 unsigned char* reloc_view,
3906 section_size_type reloc_view_size)
3908 gold_assert(sh_type == elfcpp::SHT_RELA);
3910 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3912 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
3914 const int reloc_size
3915 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
3917 Powerpc_relobj<size, big_endian>* const object
3918 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
3919 const unsigned int local_count = object->local_symbol_count();
3920 unsigned int got2_shndx = object->got2_shndx();
3921 Address got2_addend = 0;
3922 if (got2_shndx != 0)
3924 got2_addend = object->get_output_section_offset(got2_shndx);
3925 gold_assert(got2_addend != invalid_address);
3928 unsigned char* pwrite = reloc_view;
3930 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
3932 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
3933 if (strategy == Relocatable_relocs::RELOC_DISCARD)
3936 Reltype reloc(prelocs);
3937 Reltype_write reloc_write(pwrite);
3939 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
3940 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3941 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
3943 // Get the new symbol index.
3945 unsigned int new_symndx;
3946 if (r_sym < local_count)
3950 case Relocatable_relocs::RELOC_COPY:
3951 case Relocatable_relocs::RELOC_SPECIAL:
3956 new_symndx = object->symtab_index(r_sym);
3957 gold_assert(new_symndx != -1U);
3961 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
3963 // We are adjusting a section symbol. We need to find
3964 // the symbol table index of the section symbol for
3965 // the output section corresponding to input section
3966 // in which this symbol is defined.
3967 gold_assert(r_sym < local_count);
3969 unsigned int shndx =
3970 object->local_symbol_input_shndx(r_sym, &is_ordinary);
3971 gold_assert(is_ordinary);
3972 Output_section* os = object->output_section(shndx);
3973 gold_assert(os != NULL);
3974 gold_assert(os->needs_symtab_index());
3975 new_symndx = os->symtab_index();
3985 const Symbol* gsym = object->global_symbol(r_sym);
3986 gold_assert(gsym != NULL);
3987 if (gsym->is_forwarder())
3988 gsym = relinfo->symtab->resolve_forwards(gsym);
3990 gold_assert(gsym->has_symtab_index());
3991 new_symndx = gsym->symtab_index();
3994 // Get the new offset--the location in the output section where
3995 // this relocation should be applied.
3997 Address offset = reloc.get_r_offset();
3999 if (static_cast<Address>(offset_in_output_section) != invalid_address)
4000 new_offset = offset + offset_in_output_section;
4003 section_offset_type sot_offset =
4004 convert_types<section_offset_type, Address>(offset);
4005 section_offset_type new_sot_offset =
4006 output_section->output_offset(object, relinfo->data_shndx,
4008 gold_assert(new_sot_offset != -1);
4009 new_offset = new_sot_offset;
4012 // In an object file, r_offset is an offset within the section.
4013 // In an executable or dynamic object, generated by
4014 // --emit-relocs, r_offset is an absolute address.
4015 // FIXME: Arrange to call this function for --emit-relocs too,
4016 // so that we can make emitted relocs match edited TLS code.
4017 if (0 && !parameters->options().relocatable())
4019 new_offset += view_address;
4020 if (static_cast<Address>(offset_in_output_section) != invalid_address)
4021 new_offset -= offset_in_output_section;
4024 reloc_write.put_r_offset(new_offset);
4025 reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
4027 // Handle the reloc addend based on the strategy.
4028 typename elfcpp::Elf_types<size>::Elf_Swxword addend;
4029 addend = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
4030 get_reloc_addend(&reloc);
4032 if (strategy == Relocatable_relocs::RELOC_COPY)
4034 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
4036 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
4037 addend = psymval->value(object, addend);
4039 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
4041 if (addend >= 32768)
4042 addend += got2_addend;
4047 Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
4048 set_reloc_addend(&reloc_write, addend);
4050 pwrite += reloc_size;
4053 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
4054 == reloc_view_size);
4057 // Return the value to use for a dynamic which requires special
4058 // treatment. This is how we support equality comparisons of function
4059 // pointers across shared library boundaries, as described in the
4060 // processor specific ABI supplement.
4062 template<int size, bool big_endian>
4064 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
4068 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4069 return this->plt_section()->address() + gsym->plt_offset();
4075 // The selector for powerpc object files.
4077 template<int size, bool big_endian>
4078 class Target_selector_powerpc : public Target_selector
4081 Target_selector_powerpc()
4082 : Target_selector(elfcpp::EM_NONE, size, big_endian,
4084 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
4085 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
4087 ? (big_endian ? "elf64ppc" : "elf64lppc")
4088 : (big_endian ? "elf32ppc" : "elf32lppc")))
4092 do_recognize(Input_file*, off_t, int machine, int, int)
4097 if (machine != elfcpp::EM_PPC64)
4102 if (machine != elfcpp::EM_PPC)
4110 return this->instantiate_target();
4114 do_instantiate_target()
4115 { return new Target_powerpc<size, big_endian>(); }
4118 Target_selector_powerpc<32, true> target_selector_ppc32;
4119 Target_selector_powerpc<32, false> target_selector_ppc32le;
4120 Target_selector_powerpc<64, true> target_selector_ppc64;
4121 Target_selector_powerpc<64, false> target_selector_ppc64le;
4123 } // End anonymous namespace.