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) const
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 // Emit relocations for a section.
266 relocate_relocs(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_;
415 symval_for_branch(Address value, const Sized_symbol<size>* gsym,
416 Powerpc_relobj<size, big_endian>* object,
417 unsigned int *dest_shndx);
419 // The class which implements relocation.
423 // Use 'at' branch hints when true, 'y' when false.
424 // FIXME maybe: set this with an option.
425 static const bool is_isa_v2 = true;
429 CALL_NOT_EXPECTED = 0,
435 : call_tls_get_addr_(CALL_NOT_EXPECTED)
440 if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
442 // FIXME: This needs to specify the location somehow.
443 gold_error(_("missing expected __tls_get_addr call"));
447 // Do a relocation. Return false if the caller should not issue
448 // any warnings about this relocation.
450 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
451 Output_section*, size_t relnum,
452 const elfcpp::Rela<size, big_endian>&,
453 unsigned int r_type, const Sized_symbol<size>*,
454 const Symbol_value<size>*,
456 typename elfcpp::Elf_types<size>::Elf_Addr,
459 // This is set if we should skip the next reloc, which should be a
460 // call to __tls_get_addr.
461 enum skip_tls call_tls_get_addr_;
464 // A class which returns the size required for a relocation type,
465 // used while scanning relocs during a relocatable link.
466 class Relocatable_size_for_reloc
470 get_size_for_reloc(unsigned int, Relobj*)
477 // Optimize the TLS relocation type based on what we know about the
478 // symbol. IS_FINAL is true if the final address of this symbol is
479 // known at link time.
481 tls::Tls_optimization
482 optimize_tls_gd(bool is_final)
484 // If we are generating a shared library, then we can't do anything
486 if (parameters->options().shared())
487 return tls::TLSOPT_NONE;
490 return tls::TLSOPT_TO_IE;
491 return tls::TLSOPT_TO_LE;
494 tls::Tls_optimization
497 if (parameters->options().shared())
498 return tls::TLSOPT_NONE;
500 return tls::TLSOPT_TO_LE;
503 tls::Tls_optimization
504 optimize_tls_ie(bool is_final)
506 if (!is_final || parameters->options().shared())
507 return tls::TLSOPT_NONE;
509 return tls::TLSOPT_TO_LE;
512 // Get the GOT section, creating it if necessary.
513 Output_data_got_powerpc<size, big_endian>*
514 got_section(Symbol_table*, Layout*);
518 make_glink_section(Layout*);
520 // Create the PLT section.
522 make_plt_section(Layout*);
524 // Create a PLT entry for a global symbol.
526 make_plt_entry(Layout*, Symbol*,
527 const elfcpp::Rela<size, big_endian>&,
528 const Sized_relobj<size, big_endian>* object);
530 // Create a GOT entry for local dynamic __tls_get_addr.
532 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
533 Sized_relobj_file<size, big_endian>* object);
536 tlsld_got_offset() const
538 return this->tlsld_got_offset_;
541 // Get the dynamic reloc section, creating it if necessary.
543 rela_dyn_section(Layout*);
545 // Copy a relocation against a global symbol.
547 copy_reloc(Symbol_table* symtab, Layout* layout,
548 Sized_relobj_file<size, big_endian>* object,
549 unsigned int shndx, Output_section* output_section,
550 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
552 this->copy_relocs_.copy_reloc(symtab, layout,
553 symtab->get_sized_symbol<size>(sym),
554 object, shndx, output_section,
555 reloc, this->rela_dyn_section(layout));
558 // Information about this specific target which we pass to the
559 // general Target structure.
560 static Target::Target_info powerpc_info;
562 // The types of GOT entries needed for this platform.
563 // These values are exposed to the ABI in an incremental link.
564 // Do not renumber existing values without changing the version
565 // number of the .gnu_incremental_inputs section.
569 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
570 GOT_TYPE_DTPREL, // entry for @got@dtprel
571 GOT_TYPE_TPREL // entry for @got@tprel
574 // The GOT output section.
575 Output_data_got_powerpc<size, big_endian>* got_;
576 // The PLT output section.
577 Output_data_plt_powerpc<size, big_endian>* plt_;
578 // The .glink output section.
579 Output_data_glink<size, big_endian>* glink_;
580 // The dynamic reloc output section.
581 Reloc_section* rela_dyn_;
582 // Relocs saved to avoid a COPY reloc.
583 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
584 // Space for variables copied with a COPY reloc.
585 Output_data_space* dynbss_;
586 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
587 unsigned int tlsld_got_offset_;
591 Target::Target_info Target_powerpc<32, true>::powerpc_info =
594 true, // is_big_endian
595 elfcpp::EM_PPC, // machine_code
596 false, // has_make_symbol
597 false, // has_resolve
598 false, // has_code_fill
599 true, // is_default_stack_executable
600 false, // can_icf_inline_merge_sections
602 "/usr/lib/ld.so.1", // dynamic_linker
603 0x10000000, // default_text_segment_address
604 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
605 4 * 1024, // common_pagesize (overridable by -z common-page-size)
606 false, // isolate_execinstr
608 elfcpp::SHN_UNDEF, // small_common_shndx
609 elfcpp::SHN_UNDEF, // large_common_shndx
610 0, // small_common_section_flags
611 0, // large_common_section_flags
612 NULL, // attributes_section
613 NULL // attributes_vendor
617 Target::Target_info Target_powerpc<32, false>::powerpc_info =
620 false, // is_big_endian
621 elfcpp::EM_PPC, // machine_code
622 false, // has_make_symbol
623 false, // has_resolve
624 false, // has_code_fill
625 true, // is_default_stack_executable
626 false, // can_icf_inline_merge_sections
628 "/usr/lib/ld.so.1", // dynamic_linker
629 0x10000000, // default_text_segment_address
630 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
631 4 * 1024, // common_pagesize (overridable by -z common-page-size)
632 false, // isolate_execinstr
634 elfcpp::SHN_UNDEF, // small_common_shndx
635 elfcpp::SHN_UNDEF, // large_common_shndx
636 0, // small_common_section_flags
637 0, // large_common_section_flags
638 NULL, // attributes_section
639 NULL // attributes_vendor
643 Target::Target_info Target_powerpc<64, true>::powerpc_info =
646 true, // is_big_endian
647 elfcpp::EM_PPC64, // machine_code
648 false, // has_make_symbol
649 false, // has_resolve
650 false, // has_code_fill
651 true, // is_default_stack_executable
652 false, // can_icf_inline_merge_sections
654 "/usr/lib/ld.so.1", // dynamic_linker
655 0x10000000, // default_text_segment_address
656 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
657 4 * 1024, // common_pagesize (overridable by -z common-page-size)
658 false, // isolate_execinstr
660 elfcpp::SHN_UNDEF, // small_common_shndx
661 elfcpp::SHN_UNDEF, // large_common_shndx
662 0, // small_common_section_flags
663 0, // large_common_section_flags
664 NULL, // attributes_section
665 NULL // attributes_vendor
669 Target::Target_info Target_powerpc<64, false>::powerpc_info =
672 false, // is_big_endian
673 elfcpp::EM_PPC64, // machine_code
674 false, // has_make_symbol
675 false, // has_resolve
676 false, // has_code_fill
677 true, // is_default_stack_executable
678 false, // can_icf_inline_merge_sections
680 "/usr/lib/ld.so.1", // dynamic_linker
681 0x10000000, // default_text_segment_address
682 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
683 4 * 1024, // common_pagesize (overridable by -z common-page-size)
684 false, // isolate_execinstr
686 elfcpp::SHN_UNDEF, // small_common_shndx
687 elfcpp::SHN_UNDEF, // large_common_shndx
688 0, // small_common_section_flags
689 0, // large_common_section_flags
690 NULL, // attributes_section
691 NULL // attributes_vendor
695 is_branch_reloc(unsigned int r_type)
697 return (r_type == elfcpp::R_POWERPC_REL24
698 || r_type == elfcpp::R_PPC_PLTREL24
699 || r_type == elfcpp::R_PPC_LOCAL24PC
700 || r_type == elfcpp::R_POWERPC_REL14
701 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
702 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
703 || r_type == elfcpp::R_POWERPC_ADDR24
704 || r_type == elfcpp::R_POWERPC_ADDR14
705 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
706 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
709 // If INSN is an opcode that may be used with an @tls operand, return
710 // the transformed insn for TLS optimisation, otherwise return 0. If
711 // REG is non-zero only match an insn with RB or RA equal to REG.
713 at_tls_transform(uint32_t insn, unsigned int reg)
715 if ((insn & (0x3f << 26)) != 31 << 26)
719 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
720 rtra = insn & ((1 << 26) - (1 << 16));
721 else if (((insn >> 16) & 0x1f) == reg)
722 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
726 if ((insn & (0x3ff << 1)) == 266 << 1)
729 else if ((insn & (0x1f << 1)) == 23 << 1
730 && ((insn & (0x1f << 6)) < 14 << 6
731 || ((insn & (0x1f << 6)) >= 16 << 6
732 && (insn & (0x1f << 6)) < 24 << 6)))
733 // load and store indexed -> dform
734 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
735 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
736 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
737 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
738 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
740 insn = (58 << 26) | 2;
747 // Modified version of symtab.h class Symbol member
748 // Given a direct absolute or pc-relative static relocation against
749 // the global symbol, this function returns whether a dynamic relocation
754 needs_dynamic_reloc(const Symbol* gsym, int flags)
756 // No dynamic relocations in a static link!
757 if (parameters->doing_static_link())
760 // A reference to an undefined symbol from an executable should be
761 // statically resolved to 0, and does not need a dynamic relocation.
762 // This matches gnu ld behavior.
763 if (gsym->is_undefined() && !parameters->options().shared())
766 // A reference to an absolute symbol does not need a dynamic relocation.
767 if (gsym->is_absolute())
770 // An absolute reference within a position-independent output file
771 // will need a dynamic relocation.
772 if ((flags & Symbol::ABSOLUTE_REF)
773 && parameters->options().output_is_position_independent())
776 // A function call that can branch to a local PLT entry does not need
777 // a dynamic relocation.
778 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
781 // A reference to any PLT entry in a non-position-independent executable
782 // does not need a dynamic relocation.
783 // Except due to having function descriptors on powerpc64 we don't define
784 // functions to their plt code in an executable, so this doesn't apply.
786 && !parameters->options().output_is_position_independent()
787 && gsym->has_plt_offset())
790 // A reference to a symbol defined in a dynamic object or to a
791 // symbol that is preemptible will need a dynamic relocation.
792 if (gsym->is_from_dynobj()
793 || gsym->is_undefined()
794 || gsym->is_preemptible())
797 // For all other cases, return FALSE.
801 // Modified version of symtab.h class Symbol member
802 // Whether we should use the PLT offset associated with a symbol for
803 // a relocation. FLAGS is a set of Reference_flags.
807 use_plt_offset(const Symbol* gsym, int flags)
809 // If the symbol doesn't have a PLT offset, then naturally we
810 // don't want to use it.
811 if (!gsym->has_plt_offset())
814 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
815 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
818 // If we are going to generate a dynamic relocation, then we will
819 // wind up using that, so no need to use the PLT entry.
820 if (needs_dynamic_reloc<size>(gsym, flags))
823 // If the symbol is from a dynamic object, we need to use the PLT
825 if (gsym->is_from_dynobj())
828 // If we are generating a shared object, and gsym symbol is
829 // undefined or preemptible, we need to use the PLT entry.
830 if (parameters->options().shared()
831 && (gsym->is_undefined() || gsym->is_preemptible()))
834 // If gsym is a call to a weak undefined symbol, we need to use
835 // the PLT entry; the symbol may be defined by a library loaded
837 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
840 // Otherwise we can use the regular definition.
844 template<int size, bool big_endian>
845 class Powerpc_relocate_functions
855 typedef enum overflow_status
862 typedef Powerpc_relocate_functions<size, big_endian> This;
863 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
865 template<int valsize>
867 has_overflow_signed(Address value)
869 // limit = 1 << (valsize - 1) without shift count exceeding size of type
870 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
871 limit <<= ((valsize - 1) >> 1);
872 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
873 return value + limit > (limit << 1) - 1;
876 template<int valsize>
878 has_overflow_bitfield(Address value)
880 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
881 limit <<= ((valsize - 1) >> 1);
882 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
883 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
886 template<int valsize>
887 static inline enum overflow_status
888 overflowed(Address value, enum overflow_check overflow)
890 if (overflow == check_signed)
892 if (has_overflow_signed<valsize>(value))
893 return status_overflow;
895 else if (overflow == check_bitfield)
897 if (has_overflow_bitfield<valsize>(value))
898 return status_overflow;
903 // Do a simple RELA relocation
904 template<int valsize>
905 static inline enum overflow_status
906 rela(unsigned char* view, Address value, enum overflow_check overflow)
908 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
909 Valtype* wv = reinterpret_cast<Valtype*>(view);
910 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
911 return overflowed<valsize>(value, overflow);
914 template<int valsize>
915 static inline enum overflow_status
916 rela(unsigned char* view,
917 unsigned int right_shift,
918 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
920 enum overflow_check overflow)
922 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
923 Valtype* wv = reinterpret_cast<Valtype*>(view);
924 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
925 Valtype reloc = value >> right_shift;
928 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
929 return overflowed<valsize>(value >> right_shift, overflow);
932 // Do a simple RELA relocation, unaligned.
933 template<int valsize>
934 static inline enum overflow_status
935 rela_ua(unsigned char* view, Address value, enum overflow_check overflow)
937 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
938 return overflowed<valsize>(value, overflow);
941 template<int valsize>
942 static inline enum overflow_status
943 rela_ua(unsigned char* view,
944 unsigned int right_shift,
945 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
947 enum overflow_check overflow)
949 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
951 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
952 Valtype reloc = value >> right_shift;
955 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
956 return overflowed<valsize>(value >> right_shift, overflow);
960 // R_PPC64_ADDR64: (Symbol + Addend)
962 addr64(unsigned char* view, Address value)
963 { This::template rela<64>(view, value, check_none); }
965 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
967 addr64_u(unsigned char* view, Address value)
968 { This::template rela_ua<64>(view, value, check_none); }
970 // R_POWERPC_ADDR32: (Symbol + Addend)
971 static inline enum overflow_status
972 addr32(unsigned char* view, Address value, enum overflow_check overflow)
973 { return This::template rela<32>(view, value, overflow); }
975 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
976 static inline enum overflow_status
977 addr32_u(unsigned char* view, Address value, enum overflow_check overflow)
978 { return This::template rela_ua<32>(view, value, overflow); }
980 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
981 static inline enum overflow_status
982 addr24(unsigned char* view, Address value, enum overflow_check overflow)
984 enum overflow_status stat
985 = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
986 if (overflow != check_none && (value & 3) != 0)
987 stat = status_overflow;
991 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
992 static inline enum overflow_status
993 addr16(unsigned char* view, Address value, enum overflow_check overflow)
994 { return This::template rela<16>(view, value, overflow); }
996 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
997 static inline enum overflow_status
998 addr16_u(unsigned char* view, Address value, enum overflow_check overflow)
999 { return This::template rela_ua<16>(view, value, overflow); }
1001 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1002 static inline enum overflow_status
1003 addr16_ds(unsigned char* view, Address value, enum overflow_check overflow)
1005 enum overflow_status stat
1006 = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1007 if (overflow != check_none && (value & 3) != 0)
1008 stat = status_overflow;
1012 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1014 addr16_hi(unsigned char* view, Address value)
1015 { This::template rela<16>(view, 16, 0xffff, value, check_none); }
1017 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1019 addr16_ha(unsigned char* view, Address value)
1020 { This::addr16_hi(view, value + 0x8000); }
1022 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1024 addr16_hi2(unsigned char* view, Address value)
1025 { This::template rela<16>(view, 32, 0xffff, value, check_none); }
1027 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1029 addr16_ha2(unsigned char* view, Address value)
1030 { This::addr16_hi2(view, value + 0x8000); }
1032 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1034 addr16_hi3(unsigned char* view, Address value)
1035 { This::template rela<16>(view, 48, 0xffff, value, check_none); }
1037 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1039 addr16_ha3(unsigned char* view, Address value)
1040 { This::addr16_hi3(view, value + 0x8000); }
1042 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1043 static inline enum overflow_status
1044 addr14(unsigned char* view, Address value, enum overflow_check overflow)
1046 enum overflow_status stat
1047 = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1048 if (overflow != check_none && (value & 3) != 0)
1049 stat = status_overflow;
1054 // Stash away the index of .got2 or .opd in a relocatable object, if
1055 // such a section exists.
1057 template<int size, bool big_endian>
1059 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1060 Read_symbols_data* sd)
1062 const unsigned char* const pshdrs = sd->section_headers->data();
1063 const unsigned char* namesu = sd->section_names->data();
1064 const char* names = reinterpret_cast<const char*>(namesu);
1065 section_size_type names_size = sd->section_names_size;
1066 const unsigned char* s;
1068 s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1069 names, names_size, NULL);
1072 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1073 this->special_ = ndx;
1075 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1078 // Examine .rela.opd to build info about function entry points.
1080 template<int size, bool big_endian>
1082 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1084 const unsigned char* prelocs,
1085 const unsigned char* plocal_syms)
1089 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1091 const int reloc_size
1092 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1093 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1095 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1097 Reltype reloc(prelocs);
1098 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1099 = reloc.get_r_info();
1100 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1101 if (r_type == elfcpp::R_PPC64_ADDR64)
1103 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1104 typename elfcpp::Elf_types<size>::Elf_Addr value;
1107 if (r_sym < this->local_symbol_count())
1109 typename elfcpp::Sym<size, big_endian>
1110 lsym(plocal_syms + r_sym * sym_size);
1111 shndx = lsym.get_st_shndx();
1112 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1113 value = lsym.get_st_value();
1116 shndx = this->symbol_section_and_value(r_sym, &value,
1118 this->set_opd_ent(reloc.get_r_offset(), shndx,
1119 value + reloc.get_r_addend());
1125 template<int size, bool big_endian>
1127 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1129 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1132 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1133 p != rd->relocs.end();
1136 if (p->data_shndx == this->opd_shndx())
1138 this->init_opd(this->section_size(this->opd_shndx()));
1139 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1140 rd->local_symbols->data());
1147 // Set up PowerPC target specific relobj.
1149 template<int size, bool big_endian>
1151 Target_powerpc<size, big_endian>::do_make_elf_object(
1152 const std::string& name,
1153 Input_file* input_file,
1154 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1156 int et = ehdr.get_e_type();
1157 // ET_EXEC files are valid input for --just-symbols/-R,
1158 // and we treat them as relocatable objects.
1159 if (et == elfcpp::ET_REL
1160 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1162 Powerpc_relobj<size, big_endian>* obj =
1163 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1167 else if (et == elfcpp::ET_DYN)
1169 Sized_dynobj<size, big_endian>* obj =
1170 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1176 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1181 template<int size, bool big_endian>
1182 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1185 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1186 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1188 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1189 : Output_data_got<size, big_endian>(),
1190 symtab_(symtab), layout_(layout),
1191 header_ent_cnt_(size == 32 ? 3 : 1),
1192 header_index_(size == 32 ? 0x2000 : 0)
1197 // Create a new GOT entry and return its offset.
1199 add_got_entry(Got_entry got_entry)
1201 this->reserve_ent();
1202 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1205 // Create a pair of new GOT entries and return the offset of the first.
1207 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1209 this->reserve_ent(2);
1210 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1215 add_constant_pair(Valtype c1, Valtype c2)
1217 this->reserve_ent(2);
1218 unsigned int got_offset = this->add_constant(c1);
1219 this->add_constant(c2);
1223 // Offset of _GLOBAL_OFFSET_TABLE_.
1227 return this->got_offset(this->header_index_);
1230 // Offset of base used to access the GOT/TOC.
1231 // The got/toc pointer reg will be set to this value.
1232 typename elfcpp::Elf_types<size>::Elf_Off
1233 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1236 return this->g_o_t();
1238 return (this->output_section()->address()
1239 + object->toc_base_offset()
1243 // Ensure our GOT has a header.
1245 set_final_data_size()
1247 if (this->header_ent_cnt_ != 0)
1248 this->make_header();
1249 Output_data_got<size, big_endian>::set_final_data_size();
1252 // First word of GOT header needs some values that are not
1253 // handled by Output_data_got so poke them in here.
1254 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1256 do_write(Output_file* of)
1258 this->replace_constant(this->header_index_,
1260 ? this->layout_->dynamic_section()->address()
1261 : this->output_section()->address() + 0x8000));
1263 Output_data_got<size, big_endian>::do_write(of);
1268 reserve_ent(unsigned int cnt = 1)
1270 if (this->header_ent_cnt_ == 0)
1272 if (this->num_entries() + cnt > this->header_index_)
1273 this->make_header();
1279 this->header_ent_cnt_ = 0;
1280 this->header_index_ = this->num_entries();
1283 Output_data_got<size, big_endian>::add_constant(0);
1284 Output_data_got<size, big_endian>::add_constant(0);
1285 Output_data_got<size, big_endian>::add_constant(0);
1287 // Define _GLOBAL_OFFSET_TABLE_ at the header
1288 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1289 Symbol_table::PREDEFINED,
1290 this, this->g_o_t(), 0,
1297 Output_data_got<size, big_endian>::add_constant(0);
1300 // Stashed pointers.
1301 Symbol_table* symtab_;
1305 unsigned int header_ent_cnt_;
1306 // GOT header index.
1307 unsigned int header_index_;
1310 // Get the GOT section, creating it if necessary.
1312 template<int size, bool big_endian>
1313 Output_data_got_powerpc<size, big_endian>*
1314 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1317 if (this->got_ == NULL)
1319 gold_assert(symtab != NULL && layout != NULL);
1322 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
1324 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1325 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1326 this->got_, ORDER_DATA, false);
1332 // Get the dynamic reloc section, creating it if necessary.
1334 template<int size, bool big_endian>
1335 typename Target_powerpc<size, big_endian>::Reloc_section*
1336 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1338 if (this->rela_dyn_ == NULL)
1340 gold_assert(layout != NULL);
1341 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1342 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1343 elfcpp::SHF_ALLOC, this->rela_dyn_,
1344 ORDER_DYNAMIC_RELOCS, false);
1346 return this->rela_dyn_;
1349 // A class to handle the PLT data.
1351 template<int size, bool big_endian>
1352 class Output_data_plt_powerpc : public Output_section_data_build
1355 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1356 size, big_endian> Reloc_section;
1358 Output_data_plt_powerpc(Layout*, Target_powerpc<size, big_endian>*);
1360 // Add an entry to the PLT.
1364 // Return the .rela.plt section data.
1365 const Reloc_section*
1371 // Return the number of PLT entries.
1375 return ((this->current_data_size() - initial_plt_entry_size)
1379 // Return the offset of the first non-reserved PLT entry.
1381 first_plt_entry_offset()
1382 { return initial_plt_entry_size; }
1384 // Return the size of a PLT entry.
1386 get_plt_entry_size()
1387 { return plt_entry_size; }
1391 do_adjust_output_section(Output_section* os)
1396 // Write to a map file.
1398 do_print_to_mapfile(Mapfile* mapfile) const
1399 { mapfile->print_output_data(this, _("** PLT")); }
1402 // The size of an entry in the PLT.
1403 static const int plt_entry_size = size == 32 ? 4 : 24;
1404 // The size of the first reserved entry.
1405 static const int initial_plt_entry_size = size == 32 ? 0 : 24;
1407 // Write out the PLT data.
1409 do_write(Output_file*);
1411 // The reloc section.
1412 Reloc_section* rel_;
1413 // Allows access to .glink for do_write.
1414 Target_powerpc<size, big_endian>* targ_;
1417 // Create the PLT section.
1419 template<int size, bool big_endian>
1420 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(
1422 Target_powerpc<size, big_endian>* targ)
1423 : Output_section_data_build(size == 32 ? 4 : 8),
1426 this->rel_ = new Reloc_section(false);
1427 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1428 elfcpp::SHF_ALLOC, this->rel_,
1429 ORDER_DYNAMIC_PLT_RELOCS, false);
1432 // Add an entry to the PLT.
1434 template<int size, bool big_endian>
1436 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1438 if (!gsym->has_plt_offset())
1440 off_t off = this->current_data_size();
1443 off += initial_plt_entry_size;
1444 gsym->set_plt_offset(off);
1445 gsym->set_needs_dynsym_entry();
1446 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, off, 0);
1447 off += plt_entry_size;
1448 this->set_current_data_size(off);
1452 static const uint32_t add_0_11_11 = 0x7c0b5a14;
1453 static const uint32_t add_3_3_2 = 0x7c631214;
1454 static const uint32_t add_3_3_13 = 0x7c636a14;
1455 static const uint32_t add_11_0_11 = 0x7d605a14;
1456 static const uint32_t add_12_2_11 = 0x7d825a14;
1457 static const uint32_t addi_11_11 = 0x396b0000;
1458 static const uint32_t addi_12_12 = 0x398c0000;
1459 static const uint32_t addi_2_2 = 0x38420000;
1460 static const uint32_t addi_3_2 = 0x38620000;
1461 static const uint32_t addi_3_3 = 0x38630000;
1462 static const uint32_t addis_0_2 = 0x3c020000;
1463 static const uint32_t addis_0_13 = 0x3c0d0000;
1464 static const uint32_t addis_11_11 = 0x3d6b0000;
1465 static const uint32_t addis_11_30 = 0x3d7e0000;
1466 static const uint32_t addis_12_12 = 0x3d8c0000;
1467 static const uint32_t addis_12_2 = 0x3d820000;
1468 static const uint32_t addis_3_2 = 0x3c620000;
1469 static const uint32_t addis_3_13 = 0x3c6d0000;
1470 static const uint32_t b = 0x48000000;
1471 static const uint32_t bcl_20_31 = 0x429f0005;
1472 static const uint32_t bctr = 0x4e800420;
1473 static const uint32_t blrl = 0x4e800021;
1474 static const uint32_t cror_15_15_15 = 0x4def7b82;
1475 static const uint32_t cror_31_31_31 = 0x4ffffb82;
1476 static const uint32_t ld_11_12 = 0xe96c0000;
1477 static const uint32_t ld_11_2 = 0xe9620000;
1478 static const uint32_t ld_2_1 = 0xe8410000;
1479 static const uint32_t ld_2_11 = 0xe84b0000;
1480 static const uint32_t ld_2_12 = 0xe84c0000;
1481 static const uint32_t ld_2_2 = 0xe8420000;
1482 static const uint32_t li_0_0 = 0x38000000;
1483 static const uint32_t lis_0_0 = 0x3c000000;
1484 static const uint32_t lis_11 = 0x3d600000;
1485 static const uint32_t lis_12 = 0x3d800000;
1486 static const uint32_t lwz_0_12 = 0x800c0000;
1487 static const uint32_t lwz_11_11 = 0x816b0000;
1488 static const uint32_t lwz_11_30 = 0x817e0000;
1489 static const uint32_t lwz_12_12 = 0x818c0000;
1490 static const uint32_t lwzu_0_12 = 0x840c0000;
1491 static const uint32_t mflr_0 = 0x7c0802a6;
1492 static const uint32_t mflr_11 = 0x7d6802a6;
1493 static const uint32_t mflr_12 = 0x7d8802a6;
1494 static const uint32_t mtctr_0 = 0x7c0903a6;
1495 static const uint32_t mtctr_11 = 0x7d6903a6;
1496 static const uint32_t mtlr_0 = 0x7c0803a6;
1497 static const uint32_t mtlr_12 = 0x7d8803a6;
1498 static const uint32_t nop = 0x60000000;
1499 static const uint32_t ori_0_0_0 = 0x60000000;
1500 static const uint32_t std_2_1 = 0xf8410000;
1501 static const uint32_t sub_11_11_12 = 0x7d6c5850;
1503 // Write out the PLT.
1505 template<int size, bool big_endian>
1507 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1511 const off_t offset = this->offset();
1512 const section_size_type oview_size
1513 = convert_to_section_size_type(this->data_size());
1514 unsigned char* const oview = of->get_output_view(offset, oview_size);
1515 unsigned char* pov = oview;
1516 unsigned char* endpov = oview + oview_size;
1518 // The address the .glink branch table
1519 const Output_data_glink<size, big_endian>* glink
1520 = this->targ_->glink_section();
1521 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1522 = glink->address() + glink->pltresolve();
1524 while (pov < endpov)
1526 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1531 of->write_output_view(offset, oview_size, oview);
1535 // Create the PLT section.
1537 template<int size, bool big_endian>
1539 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1541 if (this->plt_ == NULL)
1543 if (this->glink_ == NULL)
1544 make_glink_section(layout);
1546 // Ensure that .rela.dyn always appears before .rela.plt This is
1547 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1548 // needs to include .rela.plt in it's range.
1549 this->rela_dyn_section(layout);
1551 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout, this);
1552 layout->add_output_section_data(".plt",
1554 ? elfcpp::SHT_PROGBITS
1555 : elfcpp::SHT_NOBITS),
1556 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1565 // A class to handle .glink.
1567 template<int size, bool big_endian>
1568 class Output_data_glink : public Output_section_data
1571 static const int pltresolve_size = 16*4;
1573 Output_data_glink(Target_powerpc<size, big_endian>*);
1577 add_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1578 const Sized_relobj<size, big_endian>*);
1581 find_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1582 const Sized_relobj<size, big_endian>*) const;
1585 glink_entry_size() const
1590 // FIXME: We should be using multiple glink sections for
1591 // stubs to support > 33M applications.
1598 return this->pltresolve_;
1602 // Write to a map file.
1604 do_print_to_mapfile(Mapfile* mapfile) const
1605 { mapfile->print_output_data(this, _("** glink")); }
1609 set_final_data_size();
1613 do_write(Output_file*);
1618 Glink_sym_ent(const Symbol* sym,
1619 const elfcpp::Rela<size, big_endian>& reloc,
1620 const Sized_relobj<size, big_endian>* object)
1621 : sym_(sym), addend_(0), object_(0)
1624 this->addend_ = reloc.get_r_addend();
1625 else if (parameters->options().output_is_position_independent()
1626 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1627 == elfcpp::R_PPC_PLTREL24))
1629 this->addend_ = reloc.get_r_addend();
1630 if (this->addend_ != 0)
1631 this->object_ = object;
1635 bool operator==(const Glink_sym_ent& that) const
1637 return (this->sym_ == that.sym_
1638 && this->object_ == that.object_
1639 && this->addend_ == that.addend_);
1643 unsigned int addend_;
1644 const Sized_relobj<size, big_endian>* object_;
1647 class Glink_sym_ent_hash
1650 size_t operator()(const Glink_sym_ent& ent) const
1652 return (reinterpret_cast<uintptr_t>(ent.sym_)
1653 ^ reinterpret_cast<uintptr_t>(ent.object_)
1658 // Map sym/object/addend to index.
1659 typedef Unordered_map<Glink_sym_ent, unsigned int,
1660 Glink_sym_ent_hash> Glink_entries;
1661 Glink_entries glink_entries_;
1663 // Offset of pltresolve stub (actually, branch table for 32-bit)
1666 // Allows access to .got and .plt for do_write.
1667 Target_powerpc<size, big_endian>* targ_;
1670 // Create the glink section.
1672 template<int size, bool big_endian>
1673 Output_data_glink<size, big_endian>::Output_data_glink(
1674 Target_powerpc<size, big_endian>* targ)
1675 : Output_section_data(16),
1676 pltresolve_(0), targ_(targ)
1680 // Add an entry to glink, if we do not already have one for this
1681 // sym/object/addend combo.
1683 template<int size, bool big_endian>
1685 Output_data_glink<size, big_endian>::add_entry(
1687 const elfcpp::Rela<size, big_endian>& reloc,
1688 const Sized_relobj<size, big_endian>* object)
1690 Glink_sym_ent ent(gsym, reloc, object);
1691 unsigned int indx = this->glink_entries_.size();
1692 this->glink_entries_.insert(std::make_pair(ent, indx));
1695 template<int size, bool big_endian>
1697 Output_data_glink<size, big_endian>::find_entry(
1699 const elfcpp::Rela<size, big_endian>& reloc,
1700 const Sized_relobj<size, big_endian>* object) const
1702 Glink_sym_ent ent(gsym, reloc, object);
1703 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
1704 gold_assert(p != this->glink_entries_.end());
1708 template<int size, bool big_endian>
1710 Output_data_glink<size, big_endian>::set_final_data_size()
1712 unsigned int count = this->glink_entries_.size();
1713 off_t total = count;
1720 this->pltresolve_ = total;
1722 // space for branch table
1723 total += 4 * (count - 1);
1725 total += -total & 15;
1726 total += this->pltresolve_size;
1731 this->pltresolve_ = total;
1732 total += this->pltresolve_size;
1734 // space for branch table
1737 total += 4 * (count - 0x8000);
1741 this->set_data_size(total);
1744 static inline uint32_t
1750 static inline uint32_t
1756 static inline uint32_t
1759 return hi(a + 0x8000);
1762 template<bool big_endian>
1764 write_insn(unsigned char* p, uint32_t v)
1766 elfcpp::Swap<32, big_endian>::writeval(p, v);
1769 // Write out .glink.
1771 template<int size, bool big_endian>
1773 Output_data_glink<size, big_endian>::do_write(Output_file* of)
1775 const off_t off = this->offset();
1776 const section_size_type oview_size =
1777 convert_to_section_size_type(this->data_size());
1778 unsigned char* const oview = of->get_output_view(off, oview_size);
1781 // The base address of the .plt section.
1782 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1783 Address plt_base = this->targ_->plt_section()->address();
1785 const Output_data_got_powerpc<size, big_endian>* got
1786 = this->targ_->got_section();
1790 Address got_os_addr = got->output_section()->address();
1792 // Write out call stubs.
1793 typename Glink_entries::const_iterator g;
1794 for (g = this->glink_entries_.begin();
1795 g != this->glink_entries_.end();
1798 Address plt_addr = plt_base + g->first.sym_->plt_offset();
1799 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1800 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1801 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
1802 Address pltoff = plt_addr - got_addr;
1804 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
1805 gold_error(_("%s: linkage table error against `%s'"),
1806 g->first.object_->name().c_str(),
1807 g->first.sym_->demangled_name().c_str());
1809 p = oview + g->second * this->glink_entry_size();
1810 if (ha(pltoff) != 0)
1812 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
1813 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1814 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
1815 if (ha(pltoff + 16) != ha(pltoff))
1817 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
1820 write_insn<big_endian>(p, mtctr_11), p += 4;
1821 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
1822 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
1823 write_insn<big_endian>(p, bctr), p += 4;
1827 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1828 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
1829 if (ha(pltoff + 16) != ha(pltoff))
1831 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
1834 write_insn<big_endian>(p, mtctr_11), p += 4;
1835 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
1836 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
1837 write_insn<big_endian>(p, bctr), p += 4;
1841 // Write pltresolve stub.
1842 p = oview + this->pltresolve_;
1843 Address after_bcl = this->address() + this->pltresolve_ + 16;
1844 Address pltoff = plt_base - after_bcl;
1846 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
1848 write_insn<big_endian>(p, mflr_12), p += 4;
1849 write_insn<big_endian>(p, bcl_20_31), p += 4;
1850 write_insn<big_endian>(p, mflr_11), p += 4;
1851 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
1852 write_insn<big_endian>(p, mtlr_12), p += 4;
1853 write_insn<big_endian>(p, add_12_2_11), p += 4;
1854 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
1855 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
1856 write_insn<big_endian>(p, mtctr_11), p += 4;
1857 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
1858 write_insn<big_endian>(p, bctr), p += 4;
1859 while (p < oview + this->pltresolve_ + this->pltresolve_size)
1860 write_insn<big_endian>(p, nop), p += 4;
1862 // Write lazy link call stubs.
1864 while (p < oview + oview_size)
1868 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
1872 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
1873 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
1875 uint32_t branch_off = this->pltresolve_ + 8 - (p - oview);
1876 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
1882 // The address of _GLOBAL_OFFSET_TABLE_.
1883 Address g_o_t = got->address() + got->g_o_t();
1885 // Write out call stubs.
1886 typename Glink_entries::const_iterator g;
1887 for (g = this->glink_entries_.begin();
1888 g != this->glink_entries_.end();
1891 Address plt_addr = plt_base + g->first.sym_->plt_offset();
1893 const Address invalid_address = static_cast<Address>(-1);
1895 p = oview + g->second * this->glink_entry_size();
1896 if (parameters->options().output_is_position_independent())
1898 const Powerpc_relobj<size, big_endian>* object = static_cast
1899 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1902 unsigned int got2 = object->got2_shndx();
1903 got_addr = g->first.object_->get_output_section_offset(got2);
1904 gold_assert(got_addr != invalid_address);
1905 got_addr += (g->first.object_->output_section(got2)->address()
1906 + g->first.addend_);
1911 Address pltoff = plt_addr - got_addr;
1912 if (ha(pltoff) == 0)
1914 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
1915 write_insn<big_endian>(p + 4, mtctr_11);
1916 write_insn<big_endian>(p + 8, bctr);
1920 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
1921 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
1922 write_insn<big_endian>(p + 8, mtctr_11);
1923 write_insn<big_endian>(p + 12, bctr);
1928 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
1929 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
1930 write_insn<big_endian>(p + 8, mtctr_11);
1931 write_insn<big_endian>(p + 12, bctr);
1935 // Write out pltresolve branch table.
1936 p = oview + this->pltresolve_;
1937 unsigned int the_end = oview_size - this->pltresolve_size;
1938 unsigned char* end_p = oview + the_end;
1939 while (p < end_p - 8 * 4)
1940 write_insn<big_endian>(p, b + end_p - p), p += 4;
1942 write_insn<big_endian>(p, nop), p += 4;
1944 // Write out pltresolve call stub.
1945 if (parameters->options().output_is_position_independent())
1947 Address res0_off = this->pltresolve_;
1948 Address after_bcl_off = the_end + 12;
1949 Address bcl_res0 = after_bcl_off - res0_off;
1951 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
1952 write_insn<big_endian>(p + 4, mflr_0);
1953 write_insn<big_endian>(p + 8, bcl_20_31);
1954 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
1955 write_insn<big_endian>(p + 16, mflr_12);
1956 write_insn<big_endian>(p + 20, mtlr_0);
1957 write_insn<big_endian>(p + 24, sub_11_11_12);
1959 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
1961 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
1962 if (ha(got_bcl) == ha(got_bcl + 4))
1964 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
1965 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
1969 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
1970 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
1972 write_insn<big_endian>(p + 40, mtctr_0);
1973 write_insn<big_endian>(p + 44, add_0_11_11);
1974 write_insn<big_endian>(p + 48, add_11_0_11);
1975 write_insn<big_endian>(p + 52, bctr);
1976 write_insn<big_endian>(p + 56, nop);
1977 write_insn<big_endian>(p + 60, nop);
1981 Address res0 = this->pltresolve_ + this->address();
1983 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
1984 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
1985 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1986 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
1988 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
1989 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
1990 write_insn<big_endian>(p + 16, mtctr_0);
1991 write_insn<big_endian>(p + 20, add_0_11_11);
1992 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1993 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
1995 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
1996 write_insn<big_endian>(p + 28, add_11_0_11);
1997 write_insn<big_endian>(p + 32, bctr);
1998 write_insn<big_endian>(p + 36, nop);
1999 write_insn<big_endian>(p + 40, nop);
2000 write_insn<big_endian>(p + 44, nop);
2001 write_insn<big_endian>(p + 48, nop);
2002 write_insn<big_endian>(p + 52, nop);
2003 write_insn<big_endian>(p + 56, nop);
2004 write_insn<big_endian>(p + 60, nop);
2009 of->write_output_view(off, oview_size, oview);
2012 // Create the glink section.
2014 template<int size, bool big_endian>
2016 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
2018 if (this->glink_ == NULL)
2020 this->glink_ = new Output_data_glink<size, big_endian>(this);
2021 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2022 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2023 this->glink_, ORDER_TEXT, false);
2027 // Create a PLT entry for a global symbol.
2029 template<int size, bool big_endian>
2031 Target_powerpc<size, big_endian>::make_plt_entry(
2034 const elfcpp::Rela<size, big_endian>& reloc,
2035 const Sized_relobj<size, big_endian>* object)
2037 if (this->plt_ == NULL)
2038 this->make_plt_section(layout);
2040 this->plt_->add_entry(gsym);
2042 this->glink_->add_entry(gsym, reloc, object);
2045 // Return the number of entries in the PLT.
2047 template<int size, bool big_endian>
2049 Target_powerpc<size, big_endian>::plt_entry_count() const
2051 if (this->plt_ == NULL)
2053 return this->plt_->entry_count();
2056 // Return the offset of the first non-reserved PLT entry.
2058 template<int size, bool big_endian>
2060 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
2062 return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
2065 // Return the size of each PLT entry.
2067 template<int size, bool big_endian>
2069 Target_powerpc<size, big_endian>::plt_entry_size() const
2071 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
2074 // Create a GOT entry for local dynamic __tls_get_addr calls.
2076 template<int size, bool big_endian>
2078 Target_powerpc<size, big_endian>::tlsld_got_offset(
2079 Symbol_table* symtab,
2081 Sized_relobj_file<size, big_endian>* object)
2083 if (this->tlsld_got_offset_ == -1U)
2085 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2086 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2087 Output_data_got_powerpc<size, big_endian>* got
2088 = this->got_section(symtab, layout);
2089 unsigned int got_offset = got->add_constant_pair(0, 0);
2090 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
2092 this->tlsld_got_offset_ = got_offset;
2094 return this->tlsld_got_offset_;
2097 // Get the Reference_flags for a particular relocation.
2099 template<int size, bool big_endian>
2101 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
2105 case elfcpp::R_POWERPC_NONE:
2106 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2107 case elfcpp::R_POWERPC_GNU_VTENTRY:
2108 case elfcpp::R_PPC64_TOC:
2109 // No symbol reference.
2112 case elfcpp::R_PPC64_ADDR64:
2113 case elfcpp::R_PPC64_UADDR64:
2114 case elfcpp::R_POWERPC_ADDR32:
2115 case elfcpp::R_POWERPC_UADDR32:
2116 case elfcpp::R_POWERPC_ADDR16:
2117 case elfcpp::R_POWERPC_UADDR16:
2118 case elfcpp::R_POWERPC_ADDR16_LO:
2119 case elfcpp::R_POWERPC_ADDR16_HI:
2120 case elfcpp::R_POWERPC_ADDR16_HA:
2121 return Symbol::ABSOLUTE_REF;
2123 case elfcpp::R_POWERPC_ADDR24:
2124 case elfcpp::R_POWERPC_ADDR14:
2125 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2126 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2127 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2129 case elfcpp::R_POWERPC_REL32:
2130 case elfcpp::R_PPC_LOCAL24PC:
2131 case elfcpp::R_POWERPC_REL16:
2132 case elfcpp::R_POWERPC_REL16_LO:
2133 case elfcpp::R_POWERPC_REL16_HI:
2134 case elfcpp::R_POWERPC_REL16_HA:
2135 return Symbol::RELATIVE_REF;
2137 case elfcpp::R_POWERPC_REL24:
2138 case elfcpp::R_PPC_PLTREL24:
2139 case elfcpp::R_POWERPC_REL14:
2140 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2141 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2142 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2144 case elfcpp::R_POWERPC_GOT16:
2145 case elfcpp::R_POWERPC_GOT16_LO:
2146 case elfcpp::R_POWERPC_GOT16_HI:
2147 case elfcpp::R_POWERPC_GOT16_HA:
2148 case elfcpp::R_PPC64_TOC16:
2149 case elfcpp::R_PPC64_TOC16_LO:
2150 case elfcpp::R_PPC64_TOC16_HI:
2151 case elfcpp::R_PPC64_TOC16_HA:
2152 case elfcpp::R_PPC64_TOC16_DS:
2153 case elfcpp::R_PPC64_TOC16_LO_DS:
2155 return Symbol::ABSOLUTE_REF;
2157 case elfcpp::R_POWERPC_GOT_TPREL16:
2158 case elfcpp::R_POWERPC_TLS:
2159 return Symbol::TLS_REF;
2161 case elfcpp::R_POWERPC_COPY:
2162 case elfcpp::R_POWERPC_GLOB_DAT:
2163 case elfcpp::R_POWERPC_JMP_SLOT:
2164 case elfcpp::R_POWERPC_RELATIVE:
2165 case elfcpp::R_POWERPC_DTPMOD:
2167 // Not expected. We will give an error later.
2172 // Report an unsupported relocation against a local symbol.
2174 template<int size, bool big_endian>
2176 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
2177 Sized_relobj_file<size, big_endian>* object,
2178 unsigned int r_type)
2180 gold_error(_("%s: unsupported reloc %u against local symbol"),
2181 object->name().c_str(), r_type);
2184 // We are about to emit a dynamic relocation of type R_TYPE. If the
2185 // dynamic linker does not support it, issue an error.
2187 template<int size, bool big_endian>
2189 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
2190 unsigned int r_type)
2192 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
2194 // These are the relocation types supported by glibc for both 32-bit
2195 // and 64-bit powerpc.
2198 case elfcpp::R_POWERPC_NONE:
2199 case elfcpp::R_POWERPC_RELATIVE:
2200 case elfcpp::R_POWERPC_GLOB_DAT:
2201 case elfcpp::R_POWERPC_DTPMOD:
2202 case elfcpp::R_POWERPC_DTPREL:
2203 case elfcpp::R_POWERPC_TPREL:
2204 case elfcpp::R_POWERPC_JMP_SLOT:
2205 case elfcpp::R_POWERPC_COPY:
2206 case elfcpp::R_POWERPC_IRELATIVE:
2207 case elfcpp::R_POWERPC_ADDR32:
2208 case elfcpp::R_POWERPC_UADDR32:
2209 case elfcpp::R_POWERPC_ADDR24:
2210 case elfcpp::R_POWERPC_ADDR16:
2211 case elfcpp::R_POWERPC_UADDR16:
2212 case elfcpp::R_POWERPC_ADDR16_LO:
2213 case elfcpp::R_POWERPC_ADDR16_HI:
2214 case elfcpp::R_POWERPC_ADDR16_HA:
2215 case elfcpp::R_POWERPC_ADDR14:
2216 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2217 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2218 case elfcpp::R_POWERPC_REL32:
2219 case elfcpp::R_POWERPC_REL24:
2220 case elfcpp::R_POWERPC_TPREL16:
2221 case elfcpp::R_POWERPC_TPREL16_LO:
2222 case elfcpp::R_POWERPC_TPREL16_HI:
2223 case elfcpp::R_POWERPC_TPREL16_HA:
2234 // These are the relocation types supported only on 64-bit.
2235 case elfcpp::R_PPC64_ADDR64:
2236 case elfcpp::R_PPC64_UADDR64:
2237 case elfcpp::R_PPC64_JMP_IREL:
2238 case elfcpp::R_PPC64_ADDR16_DS:
2239 case elfcpp::R_PPC64_ADDR16_LO_DS:
2240 case elfcpp::R_PPC64_ADDR16_HIGHER:
2241 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2242 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2243 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2244 case elfcpp::R_PPC64_REL64:
2245 case elfcpp::R_POWERPC_ADDR30:
2246 case elfcpp::R_PPC64_TPREL16_DS:
2247 case elfcpp::R_PPC64_TPREL16_LO_DS:
2248 case elfcpp::R_PPC64_TPREL16_HIGHER:
2249 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2250 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2251 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2262 // These are the relocation types supported only on 32-bit.
2263 // ??? glibc ld.so doesn't need to support these.
2264 case elfcpp::R_POWERPC_DTPREL16:
2265 case elfcpp::R_POWERPC_DTPREL16_LO:
2266 case elfcpp::R_POWERPC_DTPREL16_HI:
2267 case elfcpp::R_POWERPC_DTPREL16_HA:
2275 // This prevents us from issuing more than one error per reloc
2276 // section. But we can still wind up issuing more than one
2277 // error per object file.
2278 if (this->issued_non_pic_error_)
2280 gold_assert(parameters->options().output_is_position_independent());
2281 object->error(_("requires unsupported dynamic reloc; "
2282 "recompile with -fPIC"));
2283 this->issued_non_pic_error_ = true;
2287 // Scan a relocation for a local symbol.
2289 template<int size, bool big_endian>
2291 Target_powerpc<size, big_endian>::Scan::local(
2292 Symbol_table* symtab,
2294 Target_powerpc<size, big_endian>* target,
2295 Sized_relobj_file<size, big_endian>* object,
2296 unsigned int data_shndx,
2297 Output_section* output_section,
2298 const elfcpp::Rela<size, big_endian>& reloc,
2299 unsigned int r_type,
2300 const elfcpp::Sym<size, big_endian>& lsym)
2302 Powerpc_relobj<size, big_endian>* ppc_object
2303 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2307 case elfcpp::R_POWERPC_NONE:
2308 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2309 case elfcpp::R_POWERPC_GNU_VTENTRY:
2310 case elfcpp::R_PPC64_TOCSAVE:
2311 case elfcpp::R_PPC_EMB_MRKREF:
2312 case elfcpp::R_POWERPC_TLS:
2315 case elfcpp::R_PPC64_TOC:
2317 Output_data_got_powerpc<size, big_endian>* got
2318 = target->got_section(symtab, layout);
2319 if (parameters->options().output_is_position_independent())
2321 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2322 rela_dyn->add_output_section_relative(got->output_section(),
2323 elfcpp::R_POWERPC_RELATIVE,
2326 reloc.get_r_offset(),
2327 ppc_object->toc_base_offset());
2332 case elfcpp::R_PPC64_ADDR64:
2333 case elfcpp::R_PPC64_UADDR64:
2334 case elfcpp::R_POWERPC_ADDR32:
2335 case elfcpp::R_POWERPC_UADDR32:
2336 case elfcpp::R_POWERPC_ADDR24:
2337 case elfcpp::R_POWERPC_ADDR16:
2338 case elfcpp::R_POWERPC_ADDR16_LO:
2339 case elfcpp::R_POWERPC_ADDR16_HI:
2340 case elfcpp::R_POWERPC_ADDR16_HA:
2341 case elfcpp::R_POWERPC_UADDR16:
2342 case elfcpp::R_PPC64_ADDR16_HIGHER:
2343 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2344 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2345 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2346 case elfcpp::R_PPC64_ADDR16_DS:
2347 case elfcpp::R_PPC64_ADDR16_LO_DS:
2348 case elfcpp::R_POWERPC_ADDR14:
2349 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2350 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2351 // If building a shared library (or a position-independent
2352 // executable), we need to create a dynamic relocation for
2354 if (parameters->options().output_is_position_independent())
2356 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2358 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2359 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2361 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2362 rela_dyn->add_local_relative(object, r_sym,
2363 elfcpp::R_POWERPC_RELATIVE,
2364 output_section, data_shndx,
2365 reloc.get_r_offset(),
2366 reloc.get_r_addend(), false);
2370 check_non_pic(object, r_type);
2371 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2372 rela_dyn->add_local(object, r_sym, r_type, output_section,
2373 data_shndx, reloc.get_r_offset(),
2374 reloc.get_r_addend());
2379 case elfcpp::R_PPC64_REL64:
2380 case elfcpp::R_POWERPC_REL32:
2381 case elfcpp::R_POWERPC_REL24:
2382 case elfcpp::R_PPC_LOCAL24PC:
2383 case elfcpp::R_POWERPC_REL16:
2384 case elfcpp::R_POWERPC_REL16_LO:
2385 case elfcpp::R_POWERPC_REL16_HI:
2386 case elfcpp::R_POWERPC_REL16_HA:
2387 case elfcpp::R_POWERPC_REL14:
2388 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2389 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2390 case elfcpp::R_POWERPC_SECTOFF:
2391 case elfcpp::R_POWERPC_TPREL16:
2392 case elfcpp::R_POWERPC_DTPREL16:
2393 case elfcpp::R_POWERPC_SECTOFF_LO:
2394 case elfcpp::R_POWERPC_TPREL16_LO:
2395 case elfcpp::R_POWERPC_DTPREL16_LO:
2396 case elfcpp::R_POWERPC_SECTOFF_HI:
2397 case elfcpp::R_POWERPC_TPREL16_HI:
2398 case elfcpp::R_POWERPC_DTPREL16_HI:
2399 case elfcpp::R_POWERPC_SECTOFF_HA:
2400 case elfcpp::R_POWERPC_TPREL16_HA:
2401 case elfcpp::R_POWERPC_DTPREL16_HA:
2402 case elfcpp::R_PPC64_DTPREL16_HIGHER:
2403 case elfcpp::R_PPC64_TPREL16_HIGHER:
2404 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2405 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2406 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2407 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2408 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2409 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2410 case elfcpp::R_PPC64_TPREL16_DS:
2411 case elfcpp::R_PPC64_TPREL16_LO_DS:
2412 case elfcpp::R_PPC64_DTPREL16_DS:
2413 case elfcpp::R_PPC64_DTPREL16_LO_DS:
2414 case elfcpp::R_PPC64_SECTOFF_DS:
2415 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2416 case elfcpp::R_PPC64_TLSGD:
2417 case elfcpp::R_PPC64_TLSLD:
2420 case elfcpp::R_POWERPC_GOT16:
2421 case elfcpp::R_POWERPC_GOT16_LO:
2422 case elfcpp::R_POWERPC_GOT16_HI:
2423 case elfcpp::R_POWERPC_GOT16_HA:
2424 case elfcpp::R_PPC64_GOT16_DS:
2425 case elfcpp::R_PPC64_GOT16_LO_DS:
2427 // The symbol requires a GOT entry.
2428 Output_data_got_powerpc<size, big_endian>* got
2429 = target->got_section(symtab, layout);
2430 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2432 // If we are generating a shared object, we need to add a
2433 // dynamic relocation for this symbol's GOT entry.
2434 if (parameters->options().output_is_position_independent())
2436 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
2438 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2441 off = got->add_constant(0);
2442 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
2443 rela_dyn->add_local_relative(object, r_sym,
2444 elfcpp::R_POWERPC_RELATIVE,
2445 got, off, 0, false);
2449 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2453 case elfcpp::R_PPC64_TOC16:
2454 case elfcpp::R_PPC64_TOC16_LO:
2455 case elfcpp::R_PPC64_TOC16_HI:
2456 case elfcpp::R_PPC64_TOC16_HA:
2457 case elfcpp::R_PPC64_TOC16_DS:
2458 case elfcpp::R_PPC64_TOC16_LO_DS:
2459 // We need a GOT section.
2460 target->got_section(symtab, layout);
2463 case elfcpp::R_POWERPC_GOT_TLSGD16:
2464 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2465 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2466 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2468 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
2469 if (tls_type == tls::TLSOPT_NONE)
2471 Output_data_got_powerpc<size, big_endian>* got
2472 = target->got_section(symtab, layout);
2473 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2474 unsigned int shndx = lsym.get_st_shndx();
2476 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2477 gold_assert(is_ordinary);
2478 got->add_local_pair_with_rel(object, r_sym,
2481 target->rela_dyn_section(layout),
2482 elfcpp::R_POWERPC_DTPMOD,
2483 elfcpp::R_POWERPC_DTPREL);
2485 else if (tls_type == tls::TLSOPT_TO_LE)
2487 // no GOT relocs needed for Local Exec.
2494 case elfcpp::R_POWERPC_GOT_TLSLD16:
2495 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2496 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2497 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2499 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2500 if (tls_type == tls::TLSOPT_NONE)
2501 target->tlsld_got_offset(symtab, layout, object);
2502 else if (tls_type == tls::TLSOPT_TO_LE)
2504 // no GOT relocs needed for Local Exec.
2505 if (parameters->options().emit_relocs())
2507 Output_section* os = layout->tls_segment()->first_section();
2508 gold_assert(os != NULL);
2509 os->set_needs_symtab_index();
2517 case elfcpp::R_POWERPC_GOT_DTPREL16:
2518 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2519 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2520 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2522 Output_data_got_powerpc<size, big_endian>* got
2523 = target->got_section(symtab, layout);
2524 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2525 got->add_local_with_rel(object, r_sym, GOT_TYPE_DTPREL,
2526 target->rela_dyn_section(layout),
2527 elfcpp::R_POWERPC_DTPREL);
2531 case elfcpp::R_POWERPC_GOT_TPREL16:
2532 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2533 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2534 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2536 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
2537 if (tls_type == tls::TLSOPT_NONE)
2539 Output_data_got_powerpc<size, big_endian>* got
2540 = target->got_section(symtab, layout);
2541 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2542 got->add_local_with_rel(object, r_sym, GOT_TYPE_TPREL,
2543 target->rela_dyn_section(layout),
2544 elfcpp::R_POWERPC_TPREL);
2546 else if (tls_type == tls::TLSOPT_TO_LE)
2548 // no GOT relocs needed for Local Exec.
2556 unsupported_reloc_local(object, r_type);
2561 // Report an unsupported relocation against a global symbol.
2563 template<int size, bool big_endian>
2565 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
2566 Sized_relobj_file<size, big_endian>* object,
2567 unsigned int r_type,
2570 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2571 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2574 // Scan a relocation for a global symbol.
2576 template<int size, bool big_endian>
2578 Target_powerpc<size, big_endian>::Scan::global(
2579 Symbol_table* symtab,
2581 Target_powerpc<size, big_endian>* target,
2582 Sized_relobj_file<size, big_endian>* object,
2583 unsigned int data_shndx,
2584 Output_section* output_section,
2585 const elfcpp::Rela<size, big_endian>& reloc,
2586 unsigned int r_type,
2589 Powerpc_relobj<size, big_endian>* ppc_object
2590 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2594 case elfcpp::R_POWERPC_NONE:
2595 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2596 case elfcpp::R_POWERPC_GNU_VTENTRY:
2597 case elfcpp::R_PPC_LOCAL24PC:
2598 case elfcpp::R_PPC_EMB_MRKREF:
2599 case elfcpp::R_POWERPC_TLS:
2602 case elfcpp::R_PPC64_TOC:
2604 Output_data_got_powerpc<size, big_endian>* got
2605 = target->got_section(symtab, layout);
2606 if (parameters->options().output_is_position_independent())
2608 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2609 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
2610 if (data_shndx != ppc_object->opd_shndx())
2611 symobj = static_cast
2612 <Powerpc_relobj<size, big_endian>*>(gsym->object());
2613 rela_dyn->add_output_section_relative(got->output_section(),
2614 elfcpp::R_POWERPC_RELATIVE,
2617 reloc.get_r_offset(),
2618 symobj->toc_base_offset());
2623 case elfcpp::R_PPC64_ADDR64:
2624 case elfcpp::R_PPC64_UADDR64:
2625 case elfcpp::R_POWERPC_ADDR32:
2626 case elfcpp::R_POWERPC_UADDR32:
2627 case elfcpp::R_POWERPC_ADDR24:
2628 case elfcpp::R_POWERPC_ADDR16:
2629 case elfcpp::R_POWERPC_ADDR16_LO:
2630 case elfcpp::R_POWERPC_ADDR16_HI:
2631 case elfcpp::R_POWERPC_ADDR16_HA:
2632 case elfcpp::R_POWERPC_UADDR16:
2633 case elfcpp::R_PPC64_ADDR16_HIGHER:
2634 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2635 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2636 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2637 case elfcpp::R_PPC64_ADDR16_DS:
2638 case elfcpp::R_PPC64_ADDR16_LO_DS:
2639 case elfcpp::R_POWERPC_ADDR14:
2640 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2641 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2643 // Make a PLT entry if necessary.
2644 if (gsym->needs_plt_entry())
2646 target->make_plt_entry(layout, gsym, reloc, 0);
2647 // Since this is not a PC-relative relocation, we may be
2648 // taking the address of a function. In that case we need to
2649 // set the entry in the dynamic symbol table to the address of
2652 && gsym->is_from_dynobj() && !parameters->options().shared())
2653 gsym->set_needs_dynsym_value();
2655 // Make a dynamic relocation if necessary.
2656 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
2658 if (gsym->may_need_copy_reloc())
2660 target->copy_reloc(symtab, layout, object,
2661 data_shndx, output_section, gsym, reloc);
2663 else if (((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2664 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2665 && (gsym->can_use_relative_reloc(false)
2666 || data_shndx == ppc_object->opd_shndx()))
2668 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2669 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2670 output_section, object,
2671 data_shndx, reloc.get_r_offset(),
2672 reloc.get_r_addend(), false);
2676 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2677 check_non_pic(object, r_type);
2678 rela_dyn->add_global(gsym, r_type, output_section,
2680 reloc.get_r_offset(),
2681 reloc.get_r_addend());
2687 case elfcpp::R_PPC_PLTREL24:
2688 case elfcpp::R_POWERPC_REL24:
2689 if (gsym->needs_plt_entry()
2690 || (!gsym->final_value_is_known()
2691 && (gsym->is_undefined()
2692 || gsym->is_from_dynobj()
2693 || gsym->is_preemptible())))
2694 target->make_plt_entry(layout, gsym, reloc, object);
2697 case elfcpp::R_PPC64_REL64:
2698 case elfcpp::R_POWERPC_REL32:
2699 // Make a dynamic relocation if necessary.
2700 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
2702 if (gsym->may_need_copy_reloc())
2704 target->copy_reloc(symtab, layout, object,
2705 data_shndx, output_section, gsym,
2710 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2711 check_non_pic(object, r_type);
2712 rela_dyn->add_global(gsym, r_type, output_section, object,
2713 data_shndx, reloc.get_r_offset(),
2714 reloc.get_r_addend());
2719 case elfcpp::R_POWERPC_REL16:
2720 case elfcpp::R_POWERPC_REL16_LO:
2721 case elfcpp::R_POWERPC_REL16_HI:
2722 case elfcpp::R_POWERPC_REL16_HA:
2723 case elfcpp::R_POWERPC_REL14:
2724 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2725 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2726 case elfcpp::R_POWERPC_SECTOFF:
2727 case elfcpp::R_POWERPC_TPREL16:
2728 case elfcpp::R_POWERPC_DTPREL16:
2729 case elfcpp::R_POWERPC_SECTOFF_LO:
2730 case elfcpp::R_POWERPC_TPREL16_LO:
2731 case elfcpp::R_POWERPC_DTPREL16_LO:
2732 case elfcpp::R_POWERPC_SECTOFF_HI:
2733 case elfcpp::R_POWERPC_TPREL16_HI:
2734 case elfcpp::R_POWERPC_DTPREL16_HI:
2735 case elfcpp::R_POWERPC_SECTOFF_HA:
2736 case elfcpp::R_POWERPC_TPREL16_HA:
2737 case elfcpp::R_POWERPC_DTPREL16_HA:
2738 case elfcpp::R_PPC64_DTPREL16_HIGHER:
2739 case elfcpp::R_PPC64_TPREL16_HIGHER:
2740 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2741 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2742 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2743 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2744 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2745 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2746 case elfcpp::R_PPC64_TPREL16_DS:
2747 case elfcpp::R_PPC64_TPREL16_LO_DS:
2748 case elfcpp::R_PPC64_DTPREL16_DS:
2749 case elfcpp::R_PPC64_DTPREL16_LO_DS:
2750 case elfcpp::R_PPC64_SECTOFF_DS:
2751 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2752 case elfcpp::R_PPC64_TLSGD:
2753 case elfcpp::R_PPC64_TLSLD:
2756 case elfcpp::R_POWERPC_GOT16:
2757 case elfcpp::R_POWERPC_GOT16_LO:
2758 case elfcpp::R_POWERPC_GOT16_HI:
2759 case elfcpp::R_POWERPC_GOT16_HA:
2760 case elfcpp::R_PPC64_GOT16_DS:
2761 case elfcpp::R_PPC64_GOT16_LO_DS:
2763 // The symbol requires a GOT entry.
2764 Output_data_got_powerpc<size, big_endian>* got;
2766 got = target->got_section(symtab, layout);
2767 if (gsym->final_value_is_known())
2768 got->add_global(gsym, GOT_TYPE_STANDARD);
2771 // If this symbol is not fully resolved, we need to add a
2772 // dynamic relocation for it.
2773 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2774 if (gsym->is_from_dynobj()
2775 || gsym->is_undefined()
2776 || gsym->is_preemptible())
2777 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2778 elfcpp::R_POWERPC_GLOB_DAT);
2779 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2781 unsigned int off = got->add_constant(0);
2783 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2784 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2785 got, off, 0, false);
2791 case elfcpp::R_PPC64_TOC16:
2792 case elfcpp::R_PPC64_TOC16_LO:
2793 case elfcpp::R_PPC64_TOC16_HI:
2794 case elfcpp::R_PPC64_TOC16_HA:
2795 case elfcpp::R_PPC64_TOC16_DS:
2796 case elfcpp::R_PPC64_TOC16_LO_DS:
2797 // We need a GOT section.
2798 target->got_section(symtab, layout);
2801 case elfcpp::R_POWERPC_GOT_TLSGD16:
2802 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2803 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2804 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2806 const bool final = gsym->final_value_is_known();
2807 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
2808 if (tls_type == tls::TLSOPT_NONE)
2810 Output_data_got_powerpc<size, big_endian>* got
2811 = target->got_section(symtab, layout);
2812 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
2813 target->rela_dyn_section(layout),
2814 elfcpp::R_POWERPC_DTPMOD,
2815 elfcpp::R_POWERPC_DTPREL);
2817 else if (tls_type == tls::TLSOPT_TO_IE)
2819 Output_data_got_powerpc<size, big_endian>* got
2820 = target->got_section(symtab, layout);
2821 got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
2822 target->rela_dyn_section(layout),
2823 elfcpp::R_POWERPC_TPREL);
2825 else if (tls_type == tls::TLSOPT_TO_LE)
2827 // no GOT relocs needed for Local Exec.
2834 case elfcpp::R_POWERPC_GOT_TLSLD16:
2835 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2836 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2837 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2839 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2840 if (tls_type == tls::TLSOPT_NONE)
2841 target->tlsld_got_offset(symtab, layout, object);
2842 else if (tls_type == tls::TLSOPT_TO_LE)
2844 // no GOT relocs needed for Local Exec.
2845 if (parameters->options().emit_relocs())
2847 Output_section* os = layout->tls_segment()->first_section();
2848 gold_assert(os != NULL);
2849 os->set_needs_symtab_index();
2857 case elfcpp::R_POWERPC_GOT_DTPREL16:
2858 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2859 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2860 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2862 Output_data_got_powerpc<size, big_endian>* got
2863 = target->got_section(symtab, layout);
2864 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
2865 target->rela_dyn_section(layout),
2866 elfcpp::R_POWERPC_DTPREL);
2870 case elfcpp::R_POWERPC_GOT_TPREL16:
2871 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2872 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2873 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2875 const bool final = gsym->final_value_is_known();
2876 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
2877 if (tls_type == tls::TLSOPT_NONE)
2879 Output_data_got_powerpc<size, big_endian>* got
2880 = target->got_section(symtab, layout);
2881 got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
2882 target->rela_dyn_section(layout),
2883 elfcpp::R_POWERPC_TPREL);
2885 else if (tls_type == tls::TLSOPT_TO_LE)
2887 // no GOT relocs needed for Local Exec.
2895 unsupported_reloc_global(object, r_type, gsym);
2900 // Process relocations for gc.
2902 template<int size, bool big_endian>
2904 Target_powerpc<size, big_endian>::gc_process_relocs(
2905 Symbol_table* symtab,
2907 Sized_relobj_file<size, big_endian>* object,
2908 unsigned int data_shndx,
2910 const unsigned char* prelocs,
2912 Output_section* output_section,
2913 bool needs_special_offset_handling,
2914 size_t local_symbol_count,
2915 const unsigned char* plocal_symbols)
2917 typedef Target_powerpc<size, big_endian> Powerpc;
2918 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2920 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
2921 typename Target_powerpc::Relocatable_size_for_reloc>(
2930 needs_special_offset_handling,
2935 // Scan relocations for a section.
2937 template<int size, bool big_endian>
2939 Target_powerpc<size, big_endian>::scan_relocs(
2940 Symbol_table* symtab,
2942 Sized_relobj_file<size, big_endian>* object,
2943 unsigned int data_shndx,
2944 unsigned int sh_type,
2945 const unsigned char* prelocs,
2947 Output_section* output_section,
2948 bool needs_special_offset_handling,
2949 size_t local_symbol_count,
2950 const unsigned char* plocal_symbols)
2952 typedef Target_powerpc<size, big_endian> Powerpc;
2953 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2955 if (sh_type == elfcpp::SHT_REL)
2957 gold_error(_("%s: unsupported REL reloc section"),
2958 object->name().c_str());
2964 static Output_data_space* sdata;
2966 // Define _SDA_BASE_ at the start of the .sdata section.
2969 // layout->find_output_section(".sdata") == NULL
2970 sdata = new Output_data_space(4, "** sdata");
2972 = layout->add_output_section_data(".sdata", 0,
2974 | elfcpp::SHF_WRITE,
2975 sdata, ORDER_SMALL_DATA, false);
2976 symtab->define_in_output_data("_SDA_BASE_", NULL,
2977 Symbol_table::PREDEFINED,
2978 os, 32768, 0, elfcpp::STT_OBJECT,
2979 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2984 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
2993 needs_special_offset_handling,
2998 // Finalize the sections.
3000 template<int size, bool big_endian>
3002 Target_powerpc<size, big_endian>::do_finalize_sections(
3004 const Input_objects*,
3007 // Fill in some more dynamic tags.
3008 const Reloc_section* rel_plt = (this->plt_ == NULL
3010 : this->plt_->rel_plt());
3011 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
3012 this->rela_dyn_, true, size == 32);
3014 Output_data_dynamic* odyn = layout->dynamic_data();
3017 if (this->got_ != NULL)
3019 this->got_->finalize_data_size();
3020 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
3021 this->got_, this->got_->g_o_t());
3026 if (this->glink_ != NULL)
3028 this->glink_->finalize_data_size();
3029 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
3031 (this->glink_->pltresolve()
3032 + this->glink_->pltresolve_size - 32));
3036 // Emit any relocs we saved in an attempt to avoid generating COPY
3038 if (this->copy_relocs_.any_saved_relocs())
3039 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3042 // Return the value to use for a branch relocation.
3044 template<int size, bool big_endian>
3045 typename elfcpp::Elf_types<size>::Elf_Addr
3046 Target_powerpc<size, big_endian>::symval_for_branch(
3048 const Sized_symbol<size>* gsym,
3049 Powerpc_relobj<size, big_endian>* object,
3050 unsigned int *dest_shndx)
3056 // If the symbol is defined in an opd section, ie. is a function
3057 // descriptor, use the function descriptor code entry address
3058 Powerpc_relobj<size, big_endian>* symobj = object;
3060 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
3061 unsigned int shndx = symobj->opd_shndx();
3064 Address opd_addr = symobj->get_output_section_offset(shndx);
3065 gold_assert(opd_addr != invalid_address);
3066 opd_addr += symobj->output_section(shndx)->address();
3067 if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
3070 symobj->get_opd_ent(value - opd_addr, dest_shndx, &sec_off);
3071 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
3072 gold_assert(sec_addr != invalid_address);
3073 sec_addr += symobj->output_section(*dest_shndx)->address();
3074 value = sec_addr + sec_off;
3079 // Perform a relocation.
3081 template<int size, bool big_endian>
3083 Target_powerpc<size, big_endian>::Relocate::relocate(
3084 const Relocate_info<size, big_endian>* relinfo,
3085 Target_powerpc* target,
3088 const elfcpp::Rela<size, big_endian>& rela,
3089 unsigned int r_type,
3090 const Sized_symbol<size>* gsym,
3091 const Symbol_value<size>* psymval,
3092 unsigned char* view,
3094 section_size_type view_size)
3097 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
3098 || r_type == elfcpp::R_PPC_PLTREL24)
3100 && strcmp(gsym->name(), "__tls_get_addr") == 0);
3101 enum skip_tls last_tls = this->call_tls_get_addr_;
3102 this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
3105 if (last_tls == CALL_NOT_EXPECTED)
3106 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3107 _("__tls_get_addr call lacks marker reloc"));
3108 else if (last_tls == CALL_SKIP)
3111 else if (last_tls != CALL_NOT_EXPECTED)
3112 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3113 _("missing expected __tls_get_addr call"));
3115 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
3116 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
3117 Powerpc_relobj<size, big_endian>* const object
3118 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
3120 bool has_plt_value = false;
3122 && use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type)))
3124 const Output_data_glink<size, big_endian>* glink
3125 = target->glink_section();
3126 unsigned int glink_index = glink->find_entry(gsym, rela, object);
3127 value = glink->address() + glink_index * glink->glink_entry_size();
3128 has_plt_value = true;
3131 if (r_type == elfcpp::R_POWERPC_GOT16
3132 || r_type == elfcpp::R_POWERPC_GOT16_LO
3133 || r_type == elfcpp::R_POWERPC_GOT16_HI
3134 || r_type == elfcpp::R_POWERPC_GOT16_HA
3135 || r_type == elfcpp::R_PPC64_GOT16_DS
3136 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
3140 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3141 value = gsym->got_offset(GOT_TYPE_STANDARD);
3145 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3146 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3147 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3149 value -= target->got_section()->got_base_offset(object);
3151 else if (r_type == elfcpp::R_PPC64_TOC)
3153 value = (target->got_section()->output_section()->address()
3154 + object->toc_base_offset());
3156 else if (gsym != NULL
3157 && (r_type == elfcpp::R_POWERPC_REL24
3158 || r_type == elfcpp::R_PPC_PLTREL24)
3163 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3164 Valtype* wv = reinterpret_cast<Valtype*>(view);
3165 bool can_plt_call = false;
3166 if (rela.get_r_offset() + 8 <= view_size)
3168 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3169 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3172 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
3174 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
3175 can_plt_call = true;
3180 // If we don't have a branch and link followed by a nop,
3181 // we can't go via the plt because there is no place to
3182 // put a toc restoring instruction.
3183 // Unless we know we won't be returning.
3184 if (strcmp(gsym->name(), "__libc_start_main") == 0)
3185 can_plt_call = true;
3189 // This is not an error in one special case: A self
3190 // call. It isn't possible to cheaply verify we have
3191 // such a call so just check for a call to the same
3194 if (gsym->source() == Symbol::FROM_OBJECT
3195 && gsym->object() == object)
3197 Address addend = rela.get_r_addend();
3198 unsigned int dest_shndx;
3199 value = psymval->value(object, addend);
3200 value = target->symval_for_branch(value, gsym, object,
3203 if (dest_shndx == 0)
3204 dest_shndx = gsym->shndx(&is_ordinary);
3205 ok = dest_shndx == relinfo->data_shndx;
3208 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3209 _("call lacks nop, can't restore toc; "
3210 "recompile with -fPIC"));
3214 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3215 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
3216 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
3217 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
3219 // First instruction of a global dynamic sequence, arg setup insn.
3220 const bool final = gsym == NULL || gsym->final_value_is_known();
3221 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3222 enum Got_type got_type = GOT_TYPE_STANDARD;
3223 if (tls_type == tls::TLSOPT_NONE)
3224 got_type = GOT_TYPE_TLSGD;
3225 else if (tls_type == tls::TLSOPT_TO_IE)
3226 got_type = GOT_TYPE_TPREL;
3227 if (got_type != GOT_TYPE_STANDARD)
3231 gold_assert(gsym->has_got_offset(got_type));
3232 value = gsym->got_offset(got_type);
3236 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3237 gold_assert(object->local_has_got_offset(r_sym, got_type));
3238 value = object->local_got_offset(r_sym, got_type);
3240 value -= target->got_section()->got_base_offset(object);
3242 if (tls_type == tls::TLSOPT_TO_IE)
3244 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3245 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3247 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3248 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3249 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
3251 insn |= 32 << 26; // lwz
3253 insn |= 58 << 26; // ld
3254 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3256 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
3257 - elfcpp::R_POWERPC_GOT_TLSGD16);
3259 else if (tls_type == tls::TLSOPT_TO_LE)
3261 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3262 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3264 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3265 Insn insn = addis_3_13;
3268 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3269 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3270 value = psymval->value(object, rela.get_r_addend());
3274 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3276 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3277 r_type = elfcpp::R_POWERPC_NONE;
3281 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3282 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
3283 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
3284 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
3286 // First instruction of a local dynamic sequence, arg setup insn.
3287 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3288 if (tls_type == tls::TLSOPT_NONE)
3290 value = target->tlsld_got_offset();
3291 value -= target->got_section()->got_base_offset(object);
3295 gold_assert(tls_type == tls::TLSOPT_TO_LE);
3296 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3297 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
3299 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3300 Insn insn = addis_3_13;
3303 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3304 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3309 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3311 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3312 r_type = elfcpp::R_POWERPC_NONE;
3316 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
3317 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
3318 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
3319 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
3321 // Accesses relative to a local dynamic sequence address,
3322 // no optimisation here.
3325 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
3326 value = gsym->got_offset(GOT_TYPE_DTPREL);
3330 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3331 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
3332 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
3334 value -= target->got_section()->got_base_offset(object);
3336 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3337 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
3338 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
3339 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
3341 // First instruction of initial exec sequence.
3342 const bool final = gsym == NULL || gsym->final_value_is_known();
3343 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3344 if (tls_type == tls::TLSOPT_NONE)
3348 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
3349 value = gsym->got_offset(GOT_TYPE_TPREL);
3353 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3354 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
3355 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
3357 value -= target->got_section()->got_base_offset(object);
3361 gold_assert(tls_type == tls::TLSOPT_TO_LE);
3362 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3363 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
3365 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3366 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3367 insn &= (1 << 26) - (1 << 21); // extract rt from ld
3372 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3373 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3374 value = psymval->value(object, rela.get_r_addend());
3378 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3380 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3381 r_type = elfcpp::R_POWERPC_NONE;
3385 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
3386 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
3388 // Second instruction of a global dynamic sequence,
3389 // the __tls_get_addr call
3390 this->call_tls_get_addr_ = CALL_EXPECTED;
3391 const bool final = gsym == NULL || gsym->final_value_is_known();
3392 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3393 if (tls_type != tls::TLSOPT_NONE)
3395 if (tls_type == tls::TLSOPT_TO_IE)
3397 Insn* iview = reinterpret_cast<Insn*>(view);
3398 Insn insn = add_3_3_13;
3401 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3402 r_type = elfcpp::R_POWERPC_NONE;
3406 Insn* iview = reinterpret_cast<Insn*>(view);
3407 Insn insn = addi_3_3;
3408 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3409 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3410 view += 2 * big_endian;
3411 value = psymval->value(object, rela.get_r_addend());
3413 this->call_tls_get_addr_ = CALL_SKIP;
3416 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
3417 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
3419 // Second instruction of a local dynamic sequence,
3420 // the __tls_get_addr call
3421 this->call_tls_get_addr_ = CALL_EXPECTED;
3422 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3423 if (tls_type == tls::TLSOPT_TO_LE)
3425 Insn* iview = reinterpret_cast<Insn*>(view);
3426 Insn insn = addi_3_3;
3427 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3428 this->call_tls_get_addr_ = CALL_SKIP;
3429 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3430 view += 2 * big_endian;
3434 else if (r_type == elfcpp::R_POWERPC_TLS)
3436 // Second instruction of an initial exec sequence
3437 const bool final = gsym == NULL || gsym->final_value_is_known();
3438 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3439 if (tls_type == tls::TLSOPT_TO_LE)
3441 Insn* iview = reinterpret_cast<Insn*>(view);
3442 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3443 unsigned int reg = size == 32 ? 2 : 13;
3444 insn = at_tls_transform(insn, reg);
3445 gold_assert(insn != 0);
3446 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3447 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3448 view += 2 * big_endian;
3449 value = psymval->value(object, rela.get_r_addend());
3455 unsigned int dest_shndx;
3456 if (r_type != elfcpp::R_PPC_PLTREL24)
3457 addend = rela.get_r_addend();
3458 if (size == 64 || !has_plt_value)
3459 value = psymval->value(object, addend);
3460 if (size == 64 && is_branch_reloc(r_type))
3461 value = target->symval_for_branch(value, gsym, object, &dest_shndx);
3466 case elfcpp::R_PPC64_REL64:
3467 case elfcpp::R_POWERPC_REL32:
3468 case elfcpp::R_POWERPC_REL24:
3469 case elfcpp::R_PPC_PLTREL24:
3470 case elfcpp::R_PPC_LOCAL24PC:
3471 case elfcpp::R_POWERPC_REL16:
3472 case elfcpp::R_POWERPC_REL16_LO:
3473 case elfcpp::R_POWERPC_REL16_HI:
3474 case elfcpp::R_POWERPC_REL16_HA:
3475 case elfcpp::R_POWERPC_REL14:
3476 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3477 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3481 case elfcpp::R_PPC64_TOC16:
3482 case elfcpp::R_PPC64_TOC16_LO:
3483 case elfcpp::R_PPC64_TOC16_HI:
3484 case elfcpp::R_PPC64_TOC16_HA:
3485 case elfcpp::R_PPC64_TOC16_DS:
3486 case elfcpp::R_PPC64_TOC16_LO_DS:
3487 // Subtract the TOC base address.
3488 value -= (target->got_section()->output_section()->address()
3489 + object->toc_base_offset());
3492 case elfcpp::R_POWERPC_SECTOFF:
3493 case elfcpp::R_POWERPC_SECTOFF_LO:
3494 case elfcpp::R_POWERPC_SECTOFF_HI:
3495 case elfcpp::R_POWERPC_SECTOFF_HA:
3496 case elfcpp::R_PPC64_SECTOFF_DS:
3497 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3499 value -= os->address();
3502 case elfcpp::R_PPC64_TPREL16_DS:
3503 case elfcpp::R_PPC64_TPREL16_LO_DS:
3505 // R_PPC_TLSGD and R_PPC_TLSLD
3507 case elfcpp::R_POWERPC_TPREL16:
3508 case elfcpp::R_POWERPC_TPREL16_LO:
3509 case elfcpp::R_POWERPC_TPREL16_HI:
3510 case elfcpp::R_POWERPC_TPREL16_HA:
3511 case elfcpp::R_POWERPC_TPREL:
3512 case elfcpp::R_PPC64_TPREL16_HIGHER:
3513 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3514 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3515 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3516 // tls symbol values are relative to tls_segment()->vaddr()
3520 case elfcpp::R_PPC64_DTPREL16_DS:
3521 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3522 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3523 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3524 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3525 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3527 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
3528 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
3530 case elfcpp::R_POWERPC_DTPREL16:
3531 case elfcpp::R_POWERPC_DTPREL16_LO:
3532 case elfcpp::R_POWERPC_DTPREL16_HI:
3533 case elfcpp::R_POWERPC_DTPREL16_HA:
3534 case elfcpp::R_POWERPC_DTPREL:
3535 // tls symbol values are relative to tls_segment()->vaddr()
3536 value -= dtp_offset;
3543 Insn branch_bit = 0;
3546 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3547 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3548 branch_bit = 1 << 21;
3549 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3550 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3552 Insn* iview = reinterpret_cast<Insn*>(view);
3553 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3556 if (this->is_isa_v2)
3558 // Set 'a' bit. This is 0b00010 in BO field for branch
3559 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
3560 // for branch on CTR insns (BO == 1a00t or 1a01t).
3561 if ((insn & (0x14 << 21)) == (0x04 << 21))
3563 else if ((insn & (0x14 << 21)) == (0x10 << 21))
3570 // Invert 'y' bit if not the default.
3571 if (static_cast<Signed_address>(value) < 0)
3574 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3582 enum Reloc::overflow_check overflow = Reloc::check_none;
3585 case elfcpp::R_POWERPC_ADDR32:
3586 case elfcpp::R_POWERPC_UADDR32:
3588 overflow = Reloc::check_bitfield;
3591 case elfcpp::R_POWERPC_REL32:
3593 overflow = Reloc::check_signed;
3596 case elfcpp::R_POWERPC_ADDR24:
3597 case elfcpp::R_POWERPC_ADDR16:
3598 case elfcpp::R_POWERPC_UADDR16:
3599 case elfcpp::R_PPC64_ADDR16_DS:
3600 case elfcpp::R_POWERPC_ADDR14:
3601 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3602 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3603 overflow = Reloc::check_bitfield;
3606 case elfcpp::R_POWERPC_REL24:
3607 case elfcpp::R_PPC_PLTREL24:
3608 case elfcpp::R_PPC_LOCAL24PC:
3609 case elfcpp::R_POWERPC_REL16:
3610 case elfcpp::R_PPC64_TOC16:
3611 case elfcpp::R_POWERPC_GOT16:
3612 case elfcpp::R_POWERPC_SECTOFF:
3613 case elfcpp::R_POWERPC_TPREL16:
3614 case elfcpp::R_POWERPC_DTPREL16:
3615 case elfcpp::R_PPC64_TPREL16_DS:
3616 case elfcpp::R_PPC64_DTPREL16_DS:
3617 case elfcpp::R_PPC64_TOC16_DS:
3618 case elfcpp::R_PPC64_GOT16_DS:
3619 case elfcpp::R_PPC64_SECTOFF_DS:
3620 case elfcpp::R_POWERPC_REL14:
3621 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3622 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3623 case elfcpp::R_POWERPC_GOT_TLSGD16:
3624 case elfcpp::R_POWERPC_GOT_TLSLD16:
3625 case elfcpp::R_POWERPC_GOT_TPREL16:
3626 case elfcpp::R_POWERPC_GOT_DTPREL16:
3627 overflow = Reloc::check_signed;
3631 typename Powerpc_relocate_functions<size, big_endian>::Status status
3632 = Powerpc_relocate_functions<size, big_endian>::status_ok;
3635 case elfcpp::R_POWERPC_NONE:
3636 case elfcpp::R_POWERPC_TLS:
3637 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3638 case elfcpp::R_POWERPC_GNU_VTENTRY:
3639 case elfcpp::R_PPC_EMB_MRKREF:
3642 case elfcpp::R_PPC64_ADDR64:
3643 case elfcpp::R_PPC64_REL64:
3644 case elfcpp::R_PPC64_TOC:
3645 Reloc::addr64(view, value);
3648 case elfcpp::R_POWERPC_TPREL:
3649 case elfcpp::R_POWERPC_DTPREL:
3651 Reloc::addr64(view, value);
3653 status = Reloc::addr32(view, value, overflow);
3656 case elfcpp::R_PPC64_UADDR64:
3657 Reloc::addr64_u(view, value);
3660 case elfcpp::R_POWERPC_ADDR32:
3661 case elfcpp::R_POWERPC_REL32:
3662 status = Reloc::addr32(view, value, overflow);
3665 case elfcpp::R_POWERPC_UADDR32:
3666 status = Reloc::addr32_u(view, value, overflow);
3669 case elfcpp::R_POWERPC_ADDR24:
3670 case elfcpp::R_POWERPC_REL24:
3671 case elfcpp::R_PPC_PLTREL24:
3672 case elfcpp::R_PPC_LOCAL24PC:
3673 status = Reloc::addr24(view, value, overflow);
3676 case elfcpp::R_POWERPC_GOT_DTPREL16:
3677 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3680 status = Reloc::addr16_ds(view, value, overflow);
3683 case elfcpp::R_POWERPC_ADDR16:
3684 case elfcpp::R_POWERPC_REL16:
3685 case elfcpp::R_PPC64_TOC16:
3686 case elfcpp::R_POWERPC_GOT16:
3687 case elfcpp::R_POWERPC_SECTOFF:
3688 case elfcpp::R_POWERPC_TPREL16:
3689 case elfcpp::R_POWERPC_DTPREL16:
3690 case elfcpp::R_POWERPC_GOT_TLSGD16:
3691 case elfcpp::R_POWERPC_GOT_TLSLD16:
3692 case elfcpp::R_POWERPC_GOT_TPREL16:
3693 case elfcpp::R_POWERPC_ADDR16_LO:
3694 case elfcpp::R_POWERPC_REL16_LO:
3695 case elfcpp::R_PPC64_TOC16_LO:
3696 case elfcpp::R_POWERPC_GOT16_LO:
3697 case elfcpp::R_POWERPC_SECTOFF_LO:
3698 case elfcpp::R_POWERPC_TPREL16_LO:
3699 case elfcpp::R_POWERPC_DTPREL16_LO:
3700 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3701 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3702 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3703 status = Reloc::addr16(view, value, overflow);
3706 case elfcpp::R_POWERPC_UADDR16:
3707 status = Reloc::addr16_u(view, value, overflow);
3710 case elfcpp::R_POWERPC_ADDR16_HI:
3711 case elfcpp::R_POWERPC_REL16_HI:
3712 case elfcpp::R_PPC64_TOC16_HI:
3713 case elfcpp::R_POWERPC_GOT16_HI:
3714 case elfcpp::R_POWERPC_SECTOFF_HI:
3715 case elfcpp::R_POWERPC_TPREL16_HI:
3716 case elfcpp::R_POWERPC_DTPREL16_HI:
3717 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3718 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3719 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3720 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3721 Reloc::addr16_hi(view, value);
3724 case elfcpp::R_POWERPC_ADDR16_HA:
3725 case elfcpp::R_POWERPC_REL16_HA:
3726 case elfcpp::R_PPC64_TOC16_HA:
3727 case elfcpp::R_POWERPC_GOT16_HA:
3728 case elfcpp::R_POWERPC_SECTOFF_HA:
3729 case elfcpp::R_POWERPC_TPREL16_HA:
3730 case elfcpp::R_POWERPC_DTPREL16_HA:
3731 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3732 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3733 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3734 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3735 Reloc::addr16_ha(view, value);
3738 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3740 // R_PPC_EMB_NADDR16_LO
3742 case elfcpp::R_PPC64_ADDR16_HIGHER:
3743 case elfcpp::R_PPC64_TPREL16_HIGHER:
3744 Reloc::addr16_hi2(view, value);
3747 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3749 // R_PPC_EMB_NADDR16_HI
3751 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3752 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3753 Reloc::addr16_ha2(view, value);
3756 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3758 // R_PPC_EMB_NADDR16_HA
3760 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3761 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3762 Reloc::addr16_hi3(view, value);
3765 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3769 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3770 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3771 Reloc::addr16_ha3(view, value);
3774 case elfcpp::R_PPC64_DTPREL16_DS:
3775 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3777 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
3779 case elfcpp::R_PPC64_TPREL16_DS:
3780 case elfcpp::R_PPC64_TPREL16_LO_DS:
3782 // R_PPC_TLSGD, R_PPC_TLSLD
3784 case elfcpp::R_PPC64_ADDR16_DS:
3785 case elfcpp::R_PPC64_ADDR16_LO_DS:
3786 case elfcpp::R_PPC64_TOC16_DS:
3787 case elfcpp::R_PPC64_TOC16_LO_DS:
3788 case elfcpp::R_PPC64_GOT16_DS:
3789 case elfcpp::R_PPC64_GOT16_LO_DS:
3790 case elfcpp::R_PPC64_SECTOFF_DS:
3791 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3792 status = Reloc::addr16_ds(view, value, overflow);
3795 case elfcpp::R_POWERPC_ADDR14:
3796 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3797 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3798 case elfcpp::R_POWERPC_REL14:
3799 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3800 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3801 status = Reloc::addr14(view, value, overflow);
3804 case elfcpp::R_POWERPC_COPY:
3805 case elfcpp::R_POWERPC_GLOB_DAT:
3806 case elfcpp::R_POWERPC_JMP_SLOT:
3807 case elfcpp::R_POWERPC_RELATIVE:
3808 case elfcpp::R_POWERPC_DTPMOD:
3809 case elfcpp::R_PPC64_JMP_IREL:
3810 case elfcpp::R_POWERPC_IRELATIVE:
3811 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3812 _("unexpected reloc %u in object file"),
3816 case elfcpp::R_PPC_EMB_SDA21:
3821 // R_PPC64_TOCSAVE. For the time being this can be ignored.
3825 case elfcpp::R_PPC_EMB_SDA2I16:
3826 case elfcpp::R_PPC_EMB_SDA2REL:
3829 // R_PPC64_TLSGD, R_PPC64_TLSLD
3832 case elfcpp::R_POWERPC_PLT32:
3833 case elfcpp::R_POWERPC_PLTREL32:
3834 case elfcpp::R_POWERPC_PLT16_LO:
3835 case elfcpp::R_POWERPC_PLT16_HI:
3836 case elfcpp::R_POWERPC_PLT16_HA:
3837 case elfcpp::R_PPC_SDAREL16:
3838 case elfcpp::R_POWERPC_ADDR30:
3839 case elfcpp::R_PPC64_PLT64:
3840 case elfcpp::R_PPC64_PLTREL64:
3841 case elfcpp::R_PPC64_PLTGOT16:
3842 case elfcpp::R_PPC64_PLTGOT16_LO:
3843 case elfcpp::R_PPC64_PLTGOT16_HI:
3844 case elfcpp::R_PPC64_PLTGOT16_HA:
3845 case elfcpp::R_PPC64_PLT16_LO_DS:
3846 case elfcpp::R_PPC64_PLTGOT16_DS:
3847 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
3848 case elfcpp::R_PPC_EMB_RELSEC16:
3849 case elfcpp::R_PPC_EMB_RELST_LO:
3850 case elfcpp::R_PPC_EMB_RELST_HI:
3851 case elfcpp::R_PPC_EMB_RELST_HA:
3852 case elfcpp::R_PPC_EMB_BIT_FLD:
3853 case elfcpp::R_PPC_EMB_RELSDA:
3854 case elfcpp::R_PPC_TOC16:
3857 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3858 _("unsupported reloc %u"),
3862 if (status != Powerpc_relocate_functions<size, big_endian>::status_ok)
3863 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3864 _("relocation overflow"));
3869 // Relocate section data.
3871 template<int size, bool big_endian>
3873 Target_powerpc<size, big_endian>::relocate_section(
3874 const Relocate_info<size, big_endian>* relinfo,
3875 unsigned int sh_type,
3876 const unsigned char* prelocs,
3878 Output_section* output_section,
3879 bool needs_special_offset_handling,
3880 unsigned char* view,
3882 section_size_type view_size,
3883 const Reloc_symbol_changes* reloc_symbol_changes)
3885 typedef Target_powerpc<size, big_endian> Powerpc;
3886 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
3888 gold_assert(sh_type == elfcpp::SHT_RELA);
3890 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
3897 needs_special_offset_handling,
3901 reloc_symbol_changes);
3904 class Powerpc_scan_relocatable_reloc
3907 // Return the strategy to use for a local symbol which is not a
3908 // section symbol, given the relocation type.
3909 inline Relocatable_relocs::Reloc_strategy
3910 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
3912 if (r_type == 0 && r_sym == 0)
3913 return Relocatable_relocs::RELOC_DISCARD;
3914 return Relocatable_relocs::RELOC_COPY;
3917 // Return the strategy to use for a local symbol which is a section
3918 // symbol, given the relocation type.
3919 inline Relocatable_relocs::Reloc_strategy
3920 local_section_strategy(unsigned int, Relobj*)
3922 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
3925 // Return the strategy to use for a global symbol, given the
3926 // relocation type, the object, and the symbol index.
3927 inline Relocatable_relocs::Reloc_strategy
3928 global_strategy(unsigned int r_type, Relobj*, unsigned int)
3930 if (r_type == elfcpp::R_PPC_PLTREL24)
3931 return Relocatable_relocs::RELOC_SPECIAL;
3932 return Relocatable_relocs::RELOC_COPY;
3936 // Scan the relocs during a relocatable link.
3938 template<int size, bool big_endian>
3940 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
3941 Symbol_table* symtab,
3943 Sized_relobj_file<size, big_endian>* object,
3944 unsigned int data_shndx,
3945 unsigned int sh_type,
3946 const unsigned char* prelocs,
3948 Output_section* output_section,
3949 bool needs_special_offset_handling,
3950 size_t local_symbol_count,
3951 const unsigned char* plocal_symbols,
3952 Relocatable_relocs* rr)
3954 gold_assert(sh_type == elfcpp::SHT_RELA);
3956 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
3957 Powerpc_scan_relocatable_reloc>(
3965 needs_special_offset_handling,
3971 // Emit relocations for a section.
3972 // This is a modified version of the function by the same name in
3973 // target-reloc.h. Using relocate_special_relocatable for
3974 // R_PPC_PLTREL24 would require duplication of the entire body of the
3975 // loop, so we may as well duplicate the whole thing.
3977 template<int size, bool big_endian>
3979 Target_powerpc<size, big_endian>::relocate_relocs(
3980 const Relocate_info<size, big_endian>* relinfo,
3981 unsigned int sh_type,
3982 const unsigned char* prelocs,
3984 Output_section* output_section,
3985 off_t offset_in_output_section,
3986 const Relocatable_relocs* rr,
3988 Address view_address,
3990 unsigned char* reloc_view,
3991 section_size_type reloc_view_size)
3993 gold_assert(sh_type == elfcpp::SHT_RELA);
3995 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3997 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
3999 const int reloc_size
4000 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
4002 Powerpc_relobj<size, big_endian>* const object
4003 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
4004 const unsigned int local_count = object->local_symbol_count();
4005 unsigned int got2_shndx = object->got2_shndx();
4006 Address got2_addend = 0;
4007 if (got2_shndx != 0)
4009 got2_addend = object->get_output_section_offset(got2_shndx);
4010 gold_assert(got2_addend != invalid_address);
4013 unsigned char* pwrite = reloc_view;
4014 bool zap_next = false;
4015 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
4017 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
4018 if (strategy == Relocatable_relocs::RELOC_DISCARD)
4021 Reltype reloc(prelocs);
4022 Reltype_write reloc_write(pwrite);
4024 Address offset = reloc.get_r_offset();
4025 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
4026 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
4027 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
4028 const unsigned int orig_r_sym = r_sym;
4029 typename elfcpp::Elf_types<size>::Elf_Swxword addend
4030 = reloc.get_r_addend();
4031 const Symbol* gsym = NULL;
4035 // We could arrange to discard these and other relocs for
4036 // tls optimised sequences in the strategy methods, but for
4037 // now do as BFD ld does.
4038 r_type = elfcpp::R_POWERPC_NONE;
4042 // Get the new symbol index.
4043 if (r_sym < local_count)
4047 case Relocatable_relocs::RELOC_COPY:
4048 case Relocatable_relocs::RELOC_SPECIAL:
4051 r_sym = object->symtab_index(r_sym);
4052 gold_assert(r_sym != -1U);
4056 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
4058 // We are adjusting a section symbol. We need to find
4059 // the symbol table index of the section symbol for
4060 // the output section corresponding to input section
4061 // in which this symbol is defined.
4062 gold_assert(r_sym < local_count);
4064 unsigned int shndx =
4065 object->local_symbol_input_shndx(r_sym, &is_ordinary);
4066 gold_assert(is_ordinary);
4067 Output_section* os = object->output_section(shndx);
4068 gold_assert(os != NULL);
4069 gold_assert(os->needs_symtab_index());
4070 r_sym = os->symtab_index();
4080 gsym = object->global_symbol(r_sym);
4081 gold_assert(gsym != NULL);
4082 if (gsym->is_forwarder())
4083 gsym = relinfo->symtab->resolve_forwards(gsym);
4085 gold_assert(gsym->has_symtab_index());
4086 r_sym = gsym->symtab_index();
4089 // Get the new offset--the location in the output section where
4090 // this relocation should be applied.
4091 if (static_cast<Address>(offset_in_output_section) != invalid_address)
4092 offset += offset_in_output_section;
4095 section_offset_type sot_offset =
4096 convert_types<section_offset_type, Address>(offset);
4097 section_offset_type new_sot_offset =
4098 output_section->output_offset(object, relinfo->data_shndx,
4100 gold_assert(new_sot_offset != -1);
4101 offset = new_sot_offset;
4104 // In an object file, r_offset is an offset within the section.
4105 // In an executable or dynamic object, generated by
4106 // --emit-relocs, r_offset is an absolute address.
4107 if (!parameters->options().relocatable())
4109 offset += view_address;
4110 if (static_cast<Address>(offset_in_output_section) != invalid_address)
4111 offset -= offset_in_output_section;
4114 // Handle the reloc addend based on the strategy.
4115 if (strategy == Relocatable_relocs::RELOC_COPY)
4117 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
4119 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
4120 addend = psymval->value(object, addend);
4122 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
4124 if (addend >= 32768)
4125 addend += got2_addend;
4130 if (!parameters->options().relocatable())
4132 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4133 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
4134 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
4135 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
4137 // First instruction of a global dynamic sequence,
4139 const bool final = gsym == NULL || gsym->final_value_is_known();
4140 switch (this->optimize_tls_gd(final))
4142 case tls::TLSOPT_TO_IE:
4143 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
4144 - elfcpp::R_POWERPC_GOT_TLSGD16);
4146 case tls::TLSOPT_TO_LE:
4147 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4148 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4149 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4152 r_type = elfcpp::R_POWERPC_NONE;
4153 offset -= 2 * big_endian;
4160 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4161 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
4162 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
4163 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
4165 // First instruction of a local dynamic sequence,
4167 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
4169 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4170 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
4172 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4173 const Output_section* os = relinfo->layout->tls_segment()
4175 gold_assert(os != NULL);
4176 gold_assert(os->needs_symtab_index());
4177 r_sym = os->symtab_index();
4178 addend = dtp_offset;
4182 r_type = elfcpp::R_POWERPC_NONE;
4183 offset -= 2 * big_endian;
4187 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4188 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
4189 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
4190 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
4192 // First instruction of initial exec sequence.
4193 const bool final = gsym == NULL || gsym->final_value_is_known();
4194 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
4196 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4197 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
4198 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4201 r_type = elfcpp::R_POWERPC_NONE;
4202 offset -= 2 * big_endian;
4206 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4207 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4209 // Second instruction of a global dynamic sequence,
4210 // the __tls_get_addr call
4211 const bool final = gsym == NULL || gsym->final_value_is_known();
4212 switch (this->optimize_tls_gd(final))
4214 case tls::TLSOPT_TO_IE:
4215 r_type = elfcpp::R_POWERPC_NONE;
4218 case tls::TLSOPT_TO_LE:
4219 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4220 offset += 2 * big_endian;
4227 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4228 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4230 // Second instruction of a local dynamic sequence,
4231 // the __tls_get_addr call
4232 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
4234 const Output_section* os = relinfo->layout->tls_segment()
4236 gold_assert(os != NULL);
4237 gold_assert(os->needs_symtab_index());
4238 r_sym = os->symtab_index();
4239 addend = dtp_offset;
4240 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4241 offset += 2 * big_endian;
4245 else if (r_type == elfcpp::R_POWERPC_TLS)
4247 // Second instruction of an initial exec sequence
4248 const bool final = gsym == NULL || gsym->final_value_is_known();
4249 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
4251 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4252 offset += 2 * big_endian;
4257 reloc_write.put_r_offset(offset);
4258 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
4259 reloc_write.put_r_addend(addend);
4261 pwrite += reloc_size;
4264 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
4265 == reloc_view_size);
4268 // Return the value to use for a dynamic which requires special
4269 // treatment. This is how we support equality comparisons of function
4270 // pointers across shared library boundaries, as described in the
4271 // processor specific ABI supplement.
4273 template<int size, bool big_endian>
4275 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
4279 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4280 return this->plt_section()->address() + gsym->plt_offset();
4286 // The selector for powerpc object files.
4288 template<int size, bool big_endian>
4289 class Target_selector_powerpc : public Target_selector
4292 Target_selector_powerpc()
4293 : Target_selector(elfcpp::EM_NONE, size, big_endian,
4295 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
4296 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
4298 ? (big_endian ? "elf64ppc" : "elf64lppc")
4299 : (big_endian ? "elf32ppc" : "elf32lppc")))
4303 do_recognize(Input_file*, off_t, int machine, int, int)
4308 if (machine != elfcpp::EM_PPC64)
4313 if (machine != elfcpp::EM_PPC)
4321 return this->instantiate_target();
4325 do_instantiate_target()
4326 { return new Target_powerpc<size, big_endian>(); }
4329 Target_selector_powerpc<32, true> target_selector_ppc32;
4330 Target_selector_powerpc<32, false> target_selector_ppc32le;
4331 Target_selector_powerpc<64, true> target_selector_ppc64;
4332 Target_selector_powerpc<64, false> target_selector_ppc64le;
4334 } // End anonymous namespace.