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
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
>
888 overflowed(Address value
, 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
>
906 rela(unsigned char* view
, Address value
, 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
>
916 rela(unsigned char* view
,
917 unsigned int right_shift
,
918 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
920 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
>
935 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
937 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
, value
);
938 return overflowed
<valsize
>(value
, overflow
);
941 template<int valsize
>
943 rela_ua(unsigned char* view
,
944 unsigned int right_shift
,
945 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
947 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)
972 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
973 { return This::template rela
<32>(view
, value
, overflow
); }
975 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
977 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
978 { return This::template rela_ua
<32>(view
, value
, overflow
); }
980 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
982 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
984 Status stat
= This::template rela
<32>(view
, 0, 0x03fffffc, value
, overflow
);
985 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
986 stat
= STATUS_OVERFLOW
;
990 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
992 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
993 { return This::template rela
<16>(view
, value
, overflow
); }
995 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
997 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
998 { return This::template rela_ua
<16>(view
, value
, overflow
); }
1000 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1001 static inline Status
1002 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
1004 Status stat
= This::template rela
<16>(view
, 0, 0xfffc, value
, overflow
);
1005 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1006 stat
= STATUS_OVERFLOW
;
1010 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1012 addr16_hi(unsigned char* view
, Address value
)
1013 { This::template rela
<16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
1015 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1017 addr16_ha(unsigned char* view
, Address value
)
1018 { This::addr16_hi(view
, value
+ 0x8000); }
1020 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1022 addr16_hi2(unsigned char* view
, Address value
)
1023 { This::template rela
<16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
1025 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1027 addr16_ha2(unsigned char* view
, Address value
)
1028 { This::addr16_hi2(view
, value
+ 0x8000); }
1030 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1032 addr16_hi3(unsigned char* view
, Address value
)
1033 { This::template rela
<16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
1035 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1037 addr16_ha3(unsigned char* view
, Address value
)
1038 { This::addr16_hi3(view
, value
+ 0x8000); }
1040 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1041 static inline Status
1042 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
1044 Status stat
= This::template rela
<32>(view
, 0, 0xfffc, value
, overflow
);
1045 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1046 stat
= STATUS_OVERFLOW
;
1051 // Stash away the index of .got2 or .opd in a relocatable object, if
1052 // such a section exists.
1054 template<int size
, bool big_endian
>
1056 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
1057 Read_symbols_data
* sd
)
1059 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1060 const unsigned char* namesu
= sd
->section_names
->data();
1061 const char* names
= reinterpret_cast<const char*>(namesu
);
1062 section_size_type names_size
= sd
->section_names_size
;
1063 const unsigned char* s
;
1065 s
= this->find_shdr(pshdrs
, size
== 32 ? ".got2" : ".opd",
1066 names
, names_size
, NULL
);
1069 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
1070 this->special_
= ndx
;
1072 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
1075 // Examine .rela.opd to build info about function entry points.
1077 template<int size
, bool big_endian
>
1079 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
1081 const unsigned char* prelocs
,
1082 const unsigned char* plocal_syms
)
1086 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
1088 const int reloc_size
1089 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
1090 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1092 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
1094 Reltype
reloc(prelocs
);
1095 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
1096 = reloc
.get_r_info();
1097 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
1098 if (r_type
== elfcpp::R_PPC64_ADDR64
)
1100 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1101 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
1104 if (r_sym
< this->local_symbol_count())
1106 typename
elfcpp::Sym
<size
, big_endian
>
1107 lsym(plocal_syms
+ r_sym
* sym_size
);
1108 shndx
= lsym
.get_st_shndx();
1109 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1110 value
= lsym
.get_st_value();
1113 shndx
= this->symbol_section_and_value(r_sym
, &value
,
1115 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
1116 value
+ reloc
.get_r_addend());
1122 template<int size
, bool big_endian
>
1124 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
1126 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
1129 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
1130 p
!= rd
->relocs
.end();
1133 if (p
->data_shndx
== this->opd_shndx())
1135 this->init_opd(this->section_size(this->opd_shndx()));
1136 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
1137 rd
->local_symbols
->data());
1144 // Set up PowerPC target specific relobj.
1146 template<int size
, bool big_endian
>
1148 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
1149 const std::string
& name
,
1150 Input_file
* input_file
,
1151 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
1153 int et
= ehdr
.get_e_type();
1154 // ET_EXEC files are valid input for --just-symbols/-R,
1155 // and we treat them as relocatable objects.
1156 if (et
== elfcpp::ET_REL
1157 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
1159 Powerpc_relobj
<size
, big_endian
>* obj
=
1160 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1164 else if (et
== elfcpp::ET_DYN
)
1166 Sized_dynobj
<size
, big_endian
>* obj
=
1167 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1173 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
1178 template<int size
, bool big_endian
>
1179 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
1182 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1183 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1185 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
1186 : Output_data_got
<size
, big_endian
>(),
1187 symtab_(symtab
), layout_(layout
),
1188 header_ent_cnt_(size
== 32 ? 3 : 1),
1189 header_index_(size
== 32 ? 0x2000 : 0)
1194 // Create a new GOT entry and return its offset.
1196 add_got_entry(Got_entry got_entry
)
1198 this->reserve_ent();
1199 return Output_data_got
<size
, big_endian
>::add_got_entry(got_entry
);
1202 // Create a pair of new GOT entries and return the offset of the first.
1204 add_got_entry_pair(Got_entry got_entry_1
, Got_entry got_entry_2
)
1206 this->reserve_ent(2);
1207 return Output_data_got
<size
, big_endian
>::add_got_entry_pair(got_entry_1
,
1212 add_constant_pair(Valtype c1
, Valtype c2
)
1214 this->reserve_ent(2);
1215 unsigned int got_offset
= this->add_constant(c1
);
1216 this->add_constant(c2
);
1220 // Offset of _GLOBAL_OFFSET_TABLE_.
1224 return this->got_offset(this->header_index_
);
1227 // Offset of base used to access the GOT/TOC.
1228 // The got/toc pointer reg will be set to this value.
1229 typename
elfcpp::Elf_types
<size
>::Elf_Off
1230 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
1233 return this->g_o_t();
1235 return (this->output_section()->address()
1236 + object
->toc_base_offset()
1240 // Ensure our GOT has a header.
1242 set_final_data_size()
1244 if (this->header_ent_cnt_
!= 0)
1245 this->make_header();
1246 Output_data_got
<size
, big_endian
>::set_final_data_size();
1249 // First word of GOT header needs some values that are not
1250 // handled by Output_data_got so poke them in here.
1251 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1253 do_write(Output_file
* of
)
1255 this->replace_constant(this->header_index_
,
1257 ? this->layout_
->dynamic_section()->address()
1258 : this->output_section()->address() + 0x8000));
1260 Output_data_got
<size
, big_endian
>::do_write(of
);
1265 reserve_ent(unsigned int cnt
= 1)
1267 if (this->header_ent_cnt_
== 0)
1269 if (this->num_entries() + cnt
> this->header_index_
)
1270 this->make_header();
1276 this->header_ent_cnt_
= 0;
1277 this->header_index_
= this->num_entries();
1280 Output_data_got
<size
, big_endian
>::add_constant(0);
1281 Output_data_got
<size
, big_endian
>::add_constant(0);
1282 Output_data_got
<size
, big_endian
>::add_constant(0);
1284 // Define _GLOBAL_OFFSET_TABLE_ at the header
1285 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1286 Symbol_table::PREDEFINED
,
1287 this, this->g_o_t(), 0,
1294 Output_data_got
<size
, big_endian
>::add_constant(0);
1297 // Stashed pointers.
1298 Symbol_table
* symtab_
;
1302 unsigned int header_ent_cnt_
;
1303 // GOT header index.
1304 unsigned int header_index_
;
1307 // Get the GOT section, creating it if necessary.
1309 template<int size
, bool big_endian
>
1310 Output_data_got_powerpc
<size
, big_endian
>*
1311 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
1314 if (this->got_
== NULL
)
1316 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1319 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
1321 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1322 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1323 this->got_
, ORDER_DATA
, false);
1329 // Get the dynamic reloc section, creating it if necessary.
1331 template<int size
, bool big_endian
>
1332 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
1333 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
1335 if (this->rela_dyn_
== NULL
)
1337 gold_assert(layout
!= NULL
);
1338 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1339 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1340 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1341 ORDER_DYNAMIC_RELOCS
, false);
1343 return this->rela_dyn_
;
1346 // A class to handle the PLT data.
1348 template<int size
, bool big_endian
>
1349 class Output_data_plt_powerpc
: public Output_section_data_build
1352 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
1353 size
, big_endian
> Reloc_section
;
1355 Output_data_plt_powerpc(Layout
*, Target_powerpc
<size
, big_endian
>*);
1357 // Add an entry to the PLT.
1361 // Return the .rela.plt section data.
1362 const Reloc_section
*
1368 // Return the number of PLT entries.
1372 return ((this->current_data_size() - initial_plt_entry_size
)
1376 // Return the offset of the first non-reserved PLT entry.
1378 first_plt_entry_offset()
1379 { return initial_plt_entry_size
; }
1381 // Return the size of a PLT entry.
1383 get_plt_entry_size()
1384 { return plt_entry_size
; }
1388 do_adjust_output_section(Output_section
* os
)
1393 // Write to a map file.
1395 do_print_to_mapfile(Mapfile
* mapfile
) const
1396 { mapfile
->print_output_data(this, _("** PLT")); }
1399 // The size of an entry in the PLT.
1400 static const int plt_entry_size
= size
== 32 ? 4 : 24;
1401 // The size of the first reserved entry.
1402 static const int initial_plt_entry_size
= size
== 32 ? 0 : 24;
1404 // Write out the PLT data.
1406 do_write(Output_file
*);
1408 // The reloc section.
1409 Reloc_section
* rel_
;
1410 // Allows access to .glink for do_write.
1411 Target_powerpc
<size
, big_endian
>* targ_
;
1414 // Create the PLT section.
1416 template<int size
, bool big_endian
>
1417 Output_data_plt_powerpc
<size
, big_endian
>::Output_data_plt_powerpc(
1419 Target_powerpc
<size
, big_endian
>* targ
)
1420 : Output_section_data_build(size
== 32 ? 4 : 8),
1423 this->rel_
= new Reloc_section(false);
1424 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1425 elfcpp::SHF_ALLOC
, this->rel_
,
1426 ORDER_DYNAMIC_PLT_RELOCS
, false);
1429 // Add an entry to the PLT.
1431 template<int size
, bool big_endian
>
1433 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
1435 if (!gsym
->has_plt_offset())
1437 off_t off
= this->current_data_size();
1440 off
+= initial_plt_entry_size
;
1441 gsym
->set_plt_offset(off
);
1442 gsym
->set_needs_dynsym_entry();
1443 this->rel_
->add_global(gsym
, elfcpp::R_POWERPC_JMP_SLOT
, this, off
, 0);
1444 off
+= plt_entry_size
;
1445 this->set_current_data_size(off
);
1449 static const uint32_t add_0_11_11
= 0x7c0b5a14;
1450 static const uint32_t add_3_3_2
= 0x7c631214;
1451 static const uint32_t add_3_3_13
= 0x7c636a14;
1452 static const uint32_t add_11_0_11
= 0x7d605a14;
1453 static const uint32_t add_12_2_11
= 0x7d825a14;
1454 static const uint32_t addi_11_11
= 0x396b0000;
1455 static const uint32_t addi_12_12
= 0x398c0000;
1456 static const uint32_t addi_2_2
= 0x38420000;
1457 static const uint32_t addi_3_2
= 0x38620000;
1458 static const uint32_t addi_3_3
= 0x38630000;
1459 static const uint32_t addis_0_2
= 0x3c020000;
1460 static const uint32_t addis_0_13
= 0x3c0d0000;
1461 static const uint32_t addis_11_11
= 0x3d6b0000;
1462 static const uint32_t addis_11_30
= 0x3d7e0000;
1463 static const uint32_t addis_12_12
= 0x3d8c0000;
1464 static const uint32_t addis_12_2
= 0x3d820000;
1465 static const uint32_t addis_3_2
= 0x3c620000;
1466 static const uint32_t addis_3_13
= 0x3c6d0000;
1467 static const uint32_t b
= 0x48000000;
1468 static const uint32_t bcl_20_31
= 0x429f0005;
1469 static const uint32_t bctr
= 0x4e800420;
1470 static const uint32_t blrl
= 0x4e800021;
1471 static const uint32_t cror_15_15_15
= 0x4def7b82;
1472 static const uint32_t cror_31_31_31
= 0x4ffffb82;
1473 static const uint32_t ld_11_12
= 0xe96c0000;
1474 static const uint32_t ld_11_2
= 0xe9620000;
1475 static const uint32_t ld_2_1
= 0xe8410000;
1476 static const uint32_t ld_2_11
= 0xe84b0000;
1477 static const uint32_t ld_2_12
= 0xe84c0000;
1478 static const uint32_t ld_2_2
= 0xe8420000;
1479 static const uint32_t li_0_0
= 0x38000000;
1480 static const uint32_t lis_0_0
= 0x3c000000;
1481 static const uint32_t lis_11
= 0x3d600000;
1482 static const uint32_t lis_12
= 0x3d800000;
1483 static const uint32_t lwz_0_12
= 0x800c0000;
1484 static const uint32_t lwz_11_11
= 0x816b0000;
1485 static const uint32_t lwz_11_30
= 0x817e0000;
1486 static const uint32_t lwz_12_12
= 0x818c0000;
1487 static const uint32_t lwzu_0_12
= 0x840c0000;
1488 static const uint32_t mflr_0
= 0x7c0802a6;
1489 static const uint32_t mflr_11
= 0x7d6802a6;
1490 static const uint32_t mflr_12
= 0x7d8802a6;
1491 static const uint32_t mtctr_0
= 0x7c0903a6;
1492 static const uint32_t mtctr_11
= 0x7d6903a6;
1493 static const uint32_t mtlr_0
= 0x7c0803a6;
1494 static const uint32_t mtlr_12
= 0x7d8803a6;
1495 static const uint32_t nop
= 0x60000000;
1496 static const uint32_t ori_0_0_0
= 0x60000000;
1497 static const uint32_t std_2_1
= 0xf8410000;
1498 static const uint32_t sub_11_11_12
= 0x7d6c5850;
1500 // Write out the PLT.
1502 template<int size
, bool big_endian
>
1504 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
1508 const off_t offset
= this->offset();
1509 const section_size_type oview_size
1510 = convert_to_section_size_type(this->data_size());
1511 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1512 unsigned char* pov
= oview
;
1513 unsigned char* endpov
= oview
+ oview_size
;
1515 // The address the .glink branch table
1516 const Output_data_glink
<size
, big_endian
>* glink
1517 = this->targ_
->glink_section();
1518 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
1519 = glink
->address() + glink
->pltresolve();
1521 while (pov
< endpov
)
1523 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
1528 of
->write_output_view(offset
, oview_size
, oview
);
1532 // Create the PLT section.
1534 template<int size
, bool big_endian
>
1536 Target_powerpc
<size
, big_endian
>::make_plt_section(Layout
* layout
)
1538 if (this->plt_
== NULL
)
1540 if (this->glink_
== NULL
)
1541 make_glink_section(layout
);
1543 // Ensure that .rela.dyn always appears before .rela.plt This is
1544 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1545 // needs to include .rela.plt in it's range.
1546 this->rela_dyn_section(layout
);
1548 this->plt_
= new Output_data_plt_powerpc
<size
, big_endian
>(layout
, this);
1549 layout
->add_output_section_data(".plt",
1551 ? elfcpp::SHT_PROGBITS
1552 : elfcpp::SHT_NOBITS
),
1553 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1562 // A class to handle .glink.
1564 template<int size
, bool big_endian
>
1565 class Output_data_glink
: public Output_section_data
1568 static const int pltresolve_size
= 16*4;
1570 Output_data_glink(Target_powerpc
<size
, big_endian
>*);
1574 add_entry(const Symbol
*, const elfcpp::Rela
<size
, big_endian
>&,
1575 const Sized_relobj
<size
, big_endian
>*);
1578 find_entry(const Symbol
*, const elfcpp::Rela
<size
, big_endian
>&,
1579 const Sized_relobj
<size
, big_endian
>*) const;
1582 glink_entry_size() const
1587 // FIXME: We should be using multiple glink sections for
1588 // stubs to support > 33M applications.
1595 return this->pltresolve_
;
1599 // Write to a map file.
1601 do_print_to_mapfile(Mapfile
* mapfile
) const
1602 { mapfile
->print_output_data(this, _("** glink")); }
1606 set_final_data_size();
1610 do_write(Output_file
*);
1615 Glink_sym_ent(const Symbol
* sym
,
1616 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1617 const Sized_relobj
<size
, big_endian
>* object
)
1618 : sym_(sym
), addend_(0), object_(0)
1621 this->addend_
= reloc
.get_r_addend();
1622 else if (parameters
->options().output_is_position_independent()
1623 && (elfcpp::elf_r_type
<size
>(reloc
.get_r_info())
1624 == elfcpp::R_PPC_PLTREL24
))
1626 this->addend_
= reloc
.get_r_addend();
1627 if (this->addend_
!= 0)
1628 this->object_
= object
;
1632 bool operator==(const Glink_sym_ent
& that
) const
1634 return (this->sym_
== that
.sym_
1635 && this->object_
== that
.object_
1636 && this->addend_
== that
.addend_
);
1640 unsigned int addend_
;
1641 const Sized_relobj
<size
, big_endian
>* object_
;
1644 class Glink_sym_ent_hash
1647 size_t operator()(const Glink_sym_ent
& ent
) const
1649 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
1650 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
1655 // Map sym/object/addend to index.
1656 typedef Unordered_map
<Glink_sym_ent
, unsigned int,
1657 Glink_sym_ent_hash
> Glink_entries
;
1658 Glink_entries glink_entries_
;
1660 // Offset of pltresolve stub (actually, branch table for 32-bit)
1663 // Allows access to .got and .plt for do_write.
1664 Target_powerpc
<size
, big_endian
>* targ_
;
1667 // Create the glink section.
1669 template<int size
, bool big_endian
>
1670 Output_data_glink
<size
, big_endian
>::Output_data_glink(
1671 Target_powerpc
<size
, big_endian
>* targ
)
1672 : Output_section_data(16),
1673 pltresolve_(0), targ_(targ
)
1677 // Add an entry to glink, if we do not already have one for this
1678 // sym/object/addend combo.
1680 template<int size
, bool big_endian
>
1682 Output_data_glink
<size
, big_endian
>::add_entry(
1684 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1685 const Sized_relobj
<size
, big_endian
>* object
)
1687 Glink_sym_ent
ent(gsym
, reloc
, object
);
1688 unsigned int indx
= this->glink_entries_
.size();
1689 this->glink_entries_
.insert(std::make_pair(ent
, indx
));
1692 template<int size
, bool big_endian
>
1694 Output_data_glink
<size
, big_endian
>::find_entry(
1696 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1697 const Sized_relobj
<size
, big_endian
>* object
) const
1699 Glink_sym_ent
ent(gsym
, reloc
, object
);
1700 typename
Glink_entries::const_iterator p
= this->glink_entries_
.find(ent
);
1701 gold_assert(p
!= this->glink_entries_
.end());
1705 template<int size
, bool big_endian
>
1707 Output_data_glink
<size
, big_endian
>::set_final_data_size()
1709 unsigned int count
= this->glink_entries_
.size();
1710 off_t total
= count
;
1717 this->pltresolve_
= total
;
1719 // space for branch table
1720 total
+= 4 * (count
- 1);
1722 total
+= -total
& 15;
1723 total
+= this->pltresolve_size
;
1728 this->pltresolve_
= total
;
1729 total
+= this->pltresolve_size
;
1731 // space for branch table
1734 total
+= 4 * (count
- 0x8000);
1738 this->set_data_size(total
);
1741 static inline uint32_t
1747 static inline uint32_t
1753 static inline uint32_t
1756 return hi(a
+ 0x8000);
1759 template<bool big_endian
>
1761 write_insn(unsigned char* p
, uint32_t v
)
1763 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
1766 // Write out .glink.
1768 template<int size
, bool big_endian
>
1770 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
1772 const off_t off
= this->offset();
1773 const section_size_type oview_size
=
1774 convert_to_section_size_type(this->data_size());
1775 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1778 // The base address of the .plt section.
1779 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1780 Address plt_base
= this->targ_
->plt_section()->address();
1782 const Output_data_got_powerpc
<size
, big_endian
>* got
1783 = this->targ_
->got_section();
1787 Address got_os_addr
= got
->output_section()->address();
1789 // Write out call stubs.
1790 typename
Glink_entries::const_iterator g
;
1791 for (g
= this->glink_entries_
.begin();
1792 g
!= this->glink_entries_
.end();
1795 Address plt_addr
= plt_base
+ g
->first
.sym_
->plt_offset();
1796 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1797 <const Powerpc_relobj
<size
, big_endian
>*>(g
->first
.object_
);
1798 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
1799 Address pltoff
= plt_addr
- got_addr
;
1801 if (pltoff
+ 0x80008000 > 0xffffffff || (pltoff
& 7) != 0)
1802 gold_error(_("%s: linkage table error against `%s'"),
1803 g
->first
.object_
->name().c_str(),
1804 g
->first
.sym_
->demangled_name().c_str());
1806 p
= oview
+ g
->second
* this->glink_entry_size();
1807 if (ha(pltoff
) != 0)
1809 write_insn
<big_endian
>(p
, addis_12_2
+ ha(pltoff
)), p
+= 4;
1810 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
1811 write_insn
<big_endian
>(p
, ld_11_12
+ l(pltoff
)), p
+= 4;
1812 if (ha(pltoff
+ 16) != ha(pltoff
))
1814 write_insn
<big_endian
>(p
, addi_12_12
+ l(pltoff
)), p
+= 4;
1817 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
1818 write_insn
<big_endian
>(p
, ld_2_12
+ l(pltoff
+ 8)), p
+= 4;
1819 write_insn
<big_endian
>(p
, ld_11_12
+ l(pltoff
+ 16)), p
+= 4;
1820 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
1824 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
1825 write_insn
<big_endian
>(p
, ld_11_2
+ l(pltoff
)), p
+= 4;
1826 if (ha(pltoff
+ 16) != ha(pltoff
))
1828 write_insn
<big_endian
>(p
, addi_2_2
+ l(pltoff
)), p
+= 4;
1831 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
1832 write_insn
<big_endian
>(p
, ld_11_2
+ l(pltoff
+ 16)), p
+= 4;
1833 write_insn
<big_endian
>(p
, ld_2_2
+ l(pltoff
+ 8)), p
+= 4;
1834 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
1838 // Write pltresolve stub.
1839 p
= oview
+ this->pltresolve_
;
1840 Address after_bcl
= this->address() + this->pltresolve_
+ 16;
1841 Address pltoff
= plt_base
- after_bcl
;
1843 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
1845 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
1846 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
1847 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
1848 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
1849 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
1850 write_insn
<big_endian
>(p
, add_12_2_11
), p
+= 4;
1851 write_insn
<big_endian
>(p
, ld_11_12
+ 0), p
+= 4;
1852 write_insn
<big_endian
>(p
, ld_2_12
+ 8), p
+= 4;
1853 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
1854 write_insn
<big_endian
>(p
, ld_11_12
+ 16), p
+= 4;
1855 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
1856 while (p
< oview
+ this->pltresolve_
+ this->pltresolve_size
)
1857 write_insn
<big_endian
>(p
, nop
), p
+= 4;
1859 // Write lazy link call stubs.
1861 while (p
< oview
+ oview_size
)
1865 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
1869 write_insn
<big_endian
>(p
, lis_0_0
+ hi(indx
)), p
+= 4;
1870 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
1872 uint32_t branch_off
= this->pltresolve_
+ 8 - (p
- oview
);
1873 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
1879 // The address of _GLOBAL_OFFSET_TABLE_.
1880 Address g_o_t
= got
->address() + got
->g_o_t();
1882 // Write out call stubs.
1883 typename
Glink_entries::const_iterator g
;
1884 for (g
= this->glink_entries_
.begin();
1885 g
!= this->glink_entries_
.end();
1888 Address plt_addr
= plt_base
+ g
->first
.sym_
->plt_offset();
1890 const Address invalid_address
= static_cast<Address
>(-1);
1892 p
= oview
+ g
->second
* this->glink_entry_size();
1893 if (parameters
->options().output_is_position_independent())
1895 const Powerpc_relobj
<size
, big_endian
>* object
= static_cast
1896 <const Powerpc_relobj
<size
, big_endian
>*>(g
->first
.object_
);
1899 unsigned int got2
= object
->got2_shndx();
1900 got_addr
= g
->first
.object_
->get_output_section_offset(got2
);
1901 gold_assert(got_addr
!= invalid_address
);
1902 got_addr
+= (g
->first
.object_
->output_section(got2
)->address()
1903 + g
->first
.addend_
);
1908 Address pltoff
= plt_addr
- got_addr
;
1909 if (ha(pltoff
) == 0)
1911 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(pltoff
));
1912 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
1913 write_insn
<big_endian
>(p
+ 8, bctr
);
1917 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(pltoff
));
1918 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(pltoff
));
1919 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
1920 write_insn
<big_endian
>(p
+ 12, bctr
);
1925 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
1926 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
1927 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
1928 write_insn
<big_endian
>(p
+ 12, bctr
);
1932 // Write out pltresolve branch table.
1933 p
= oview
+ this->pltresolve_
;
1934 unsigned int the_end
= oview_size
- this->pltresolve_size
;
1935 unsigned char* end_p
= oview
+ the_end
;
1936 while (p
< end_p
- 8 * 4)
1937 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
1939 write_insn
<big_endian
>(p
, nop
), p
+= 4;
1941 // Write out pltresolve call stub.
1942 if (parameters
->options().output_is_position_independent())
1944 Address res0_off
= this->pltresolve_
;
1945 Address after_bcl_off
= the_end
+ 12;
1946 Address bcl_res0
= after_bcl_off
- res0_off
;
1948 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
1949 write_insn
<big_endian
>(p
+ 4, mflr_0
);
1950 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
1951 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
1952 write_insn
<big_endian
>(p
+ 16, mflr_12
);
1953 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
1954 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
1956 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
1958 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
1959 if (ha(got_bcl
) == ha(got_bcl
+ 4))
1961 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
1962 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
1966 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
1967 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
1969 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
1970 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
1971 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
1972 write_insn
<big_endian
>(p
+ 52, bctr
);
1973 write_insn
<big_endian
>(p
+ 56, nop
);
1974 write_insn
<big_endian
>(p
+ 60, nop
);
1978 Address res0
= this->pltresolve_
+ this->address();
1980 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
1981 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
1982 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
1983 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
1985 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
1986 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
1987 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
1988 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
1989 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
1990 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
1992 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
1993 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
1994 write_insn
<big_endian
>(p
+ 32, bctr
);
1995 write_insn
<big_endian
>(p
+ 36, nop
);
1996 write_insn
<big_endian
>(p
+ 40, nop
);
1997 write_insn
<big_endian
>(p
+ 44, nop
);
1998 write_insn
<big_endian
>(p
+ 48, nop
);
1999 write_insn
<big_endian
>(p
+ 52, nop
);
2000 write_insn
<big_endian
>(p
+ 56, nop
);
2001 write_insn
<big_endian
>(p
+ 60, nop
);
2006 of
->write_output_view(off
, oview_size
, oview
);
2009 // Create the glink section.
2011 template<int size
, bool big_endian
>
2013 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
2015 if (this->glink_
== NULL
)
2017 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
2018 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
2019 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
2020 this->glink_
, ORDER_TEXT
, false);
2024 // Create a PLT entry for a global symbol.
2026 template<int size
, bool big_endian
>
2028 Target_powerpc
<size
, big_endian
>::make_plt_entry(
2031 const elfcpp::Rela
<size
, big_endian
>& reloc
,
2032 const Sized_relobj
<size
, big_endian
>* object
)
2034 if (this->plt_
== NULL
)
2035 this->make_plt_section(layout
);
2037 this->plt_
->add_entry(gsym
);
2039 this->glink_
->add_entry(gsym
, reloc
, object
);
2042 // Return the number of entries in the PLT.
2044 template<int size
, bool big_endian
>
2046 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
2048 if (this->plt_
== NULL
)
2050 return this->plt_
->entry_count();
2053 // Return the offset of the first non-reserved PLT entry.
2055 template<int size
, bool big_endian
>
2057 Target_powerpc
<size
, big_endian
>::first_plt_entry_offset() const
2059 return Output_data_plt_powerpc
<size
, big_endian
>::first_plt_entry_offset();
2062 // Return the size of each PLT entry.
2064 template<int size
, bool big_endian
>
2066 Target_powerpc
<size
, big_endian
>::plt_entry_size() const
2068 return Output_data_plt_powerpc
<size
, big_endian
>::get_plt_entry_size();
2071 // Create a GOT entry for local dynamic __tls_get_addr calls.
2073 template<int size
, bool big_endian
>
2075 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
2076 Symbol_table
* symtab
,
2078 Sized_relobj_file
<size
, big_endian
>* object
)
2080 if (this->tlsld_got_offset_
== -1U)
2082 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
2083 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
2084 Output_data_got_powerpc
<size
, big_endian
>* got
2085 = this->got_section(symtab
, layout
);
2086 unsigned int got_offset
= got
->add_constant_pair(0, 0);
2087 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
2089 this->tlsld_got_offset_
= got_offset
;
2091 return this->tlsld_got_offset_
;
2094 // Get the Reference_flags for a particular relocation.
2096 template<int size
, bool big_endian
>
2098 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(unsigned int r_type
)
2102 case elfcpp::R_POWERPC_NONE
:
2103 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
2104 case elfcpp::R_POWERPC_GNU_VTENTRY
:
2105 case elfcpp::R_PPC64_TOC
:
2106 // No symbol reference.
2109 case elfcpp::R_PPC64_ADDR64
:
2110 case elfcpp::R_PPC64_UADDR64
:
2111 case elfcpp::R_POWERPC_ADDR32
:
2112 case elfcpp::R_POWERPC_UADDR32
:
2113 case elfcpp::R_POWERPC_ADDR16
:
2114 case elfcpp::R_POWERPC_UADDR16
:
2115 case elfcpp::R_POWERPC_ADDR16_LO
:
2116 case elfcpp::R_POWERPC_ADDR16_HI
:
2117 case elfcpp::R_POWERPC_ADDR16_HA
:
2118 return Symbol::ABSOLUTE_REF
;
2120 case elfcpp::R_POWERPC_ADDR24
:
2121 case elfcpp::R_POWERPC_ADDR14
:
2122 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
2123 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
2124 return Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
2126 case elfcpp::R_POWERPC_REL32
:
2127 case elfcpp::R_PPC_LOCAL24PC
:
2128 case elfcpp::R_POWERPC_REL16
:
2129 case elfcpp::R_POWERPC_REL16_LO
:
2130 case elfcpp::R_POWERPC_REL16_HI
:
2131 case elfcpp::R_POWERPC_REL16_HA
:
2132 return Symbol::RELATIVE_REF
;
2134 case elfcpp::R_POWERPC_REL24
:
2135 case elfcpp::R_PPC_PLTREL24
:
2136 case elfcpp::R_POWERPC_REL14
:
2137 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
2138 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
2139 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
2141 case elfcpp::R_POWERPC_GOT16
:
2142 case elfcpp::R_POWERPC_GOT16_LO
:
2143 case elfcpp::R_POWERPC_GOT16_HI
:
2144 case elfcpp::R_POWERPC_GOT16_HA
:
2145 case elfcpp::R_PPC64_TOC16
:
2146 case elfcpp::R_PPC64_TOC16_LO
:
2147 case elfcpp::R_PPC64_TOC16_HI
:
2148 case elfcpp::R_PPC64_TOC16_HA
:
2149 case elfcpp::R_PPC64_TOC16_DS
:
2150 case elfcpp::R_PPC64_TOC16_LO_DS
:
2152 return Symbol::ABSOLUTE_REF
;
2154 case elfcpp::R_POWERPC_GOT_TPREL16
:
2155 case elfcpp::R_POWERPC_TLS
:
2156 return Symbol::TLS_REF
;
2158 case elfcpp::R_POWERPC_COPY
:
2159 case elfcpp::R_POWERPC_GLOB_DAT
:
2160 case elfcpp::R_POWERPC_JMP_SLOT
:
2161 case elfcpp::R_POWERPC_RELATIVE
:
2162 case elfcpp::R_POWERPC_DTPMOD
:
2164 // Not expected. We will give an error later.
2169 // Report an unsupported relocation against a local symbol.
2171 template<int size
, bool big_endian
>
2173 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
2174 Sized_relobj_file
<size
, big_endian
>* object
,
2175 unsigned int r_type
)
2177 gold_error(_("%s: unsupported reloc %u against local symbol"),
2178 object
->name().c_str(), r_type
);
2181 // We are about to emit a dynamic relocation of type R_TYPE. If the
2182 // dynamic linker does not support it, issue an error.
2184 template<int size
, bool big_endian
>
2186 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
2187 unsigned int r_type
)
2189 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
2191 // These are the relocation types supported by glibc for both 32-bit
2192 // and 64-bit powerpc.
2195 case elfcpp::R_POWERPC_NONE
:
2196 case elfcpp::R_POWERPC_RELATIVE
:
2197 case elfcpp::R_POWERPC_GLOB_DAT
:
2198 case elfcpp::R_POWERPC_DTPMOD
:
2199 case elfcpp::R_POWERPC_DTPREL
:
2200 case elfcpp::R_POWERPC_TPREL
:
2201 case elfcpp::R_POWERPC_JMP_SLOT
:
2202 case elfcpp::R_POWERPC_COPY
:
2203 case elfcpp::R_POWERPC_IRELATIVE
:
2204 case elfcpp::R_POWERPC_ADDR32
:
2205 case elfcpp::R_POWERPC_UADDR32
:
2206 case elfcpp::R_POWERPC_ADDR24
:
2207 case elfcpp::R_POWERPC_ADDR16
:
2208 case elfcpp::R_POWERPC_UADDR16
:
2209 case elfcpp::R_POWERPC_ADDR16_LO
:
2210 case elfcpp::R_POWERPC_ADDR16_HI
:
2211 case elfcpp::R_POWERPC_ADDR16_HA
:
2212 case elfcpp::R_POWERPC_ADDR14
:
2213 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
2214 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
2215 case elfcpp::R_POWERPC_REL32
:
2216 case elfcpp::R_POWERPC_REL24
:
2217 case elfcpp::R_POWERPC_TPREL16
:
2218 case elfcpp::R_POWERPC_TPREL16_LO
:
2219 case elfcpp::R_POWERPC_TPREL16_HI
:
2220 case elfcpp::R_POWERPC_TPREL16_HA
:
2231 // These are the relocation types supported only on 64-bit.
2232 case elfcpp::R_PPC64_ADDR64
:
2233 case elfcpp::R_PPC64_UADDR64
:
2234 case elfcpp::R_PPC64_JMP_IREL
:
2235 case elfcpp::R_PPC64_ADDR16_DS
:
2236 case elfcpp::R_PPC64_ADDR16_LO_DS
:
2237 case elfcpp::R_PPC64_ADDR16_HIGHER
:
2238 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
2239 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
2240 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
2241 case elfcpp::R_PPC64_REL64
:
2242 case elfcpp::R_POWERPC_ADDR30
:
2243 case elfcpp::R_PPC64_TPREL16_DS
:
2244 case elfcpp::R_PPC64_TPREL16_LO_DS
:
2245 case elfcpp::R_PPC64_TPREL16_HIGHER
:
2246 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
2247 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
2248 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
2259 // These are the relocation types supported only on 32-bit.
2260 // ??? glibc ld.so doesn't need to support these.
2261 case elfcpp::R_POWERPC_DTPREL16
:
2262 case elfcpp::R_POWERPC_DTPREL16_LO
:
2263 case elfcpp::R_POWERPC_DTPREL16_HI
:
2264 case elfcpp::R_POWERPC_DTPREL16_HA
:
2272 // This prevents us from issuing more than one error per reloc
2273 // section. But we can still wind up issuing more than one
2274 // error per object file.
2275 if (this->issued_non_pic_error_
)
2277 gold_assert(parameters
->options().output_is_position_independent());
2278 object
->error(_("requires unsupported dynamic reloc; "
2279 "recompile with -fPIC"));
2280 this->issued_non_pic_error_
= true;
2284 // Scan a relocation for a local symbol.
2286 template<int size
, bool big_endian
>
2288 Target_powerpc
<size
, big_endian
>::Scan::local(
2289 Symbol_table
* symtab
,
2291 Target_powerpc
<size
, big_endian
>* target
,
2292 Sized_relobj_file
<size
, big_endian
>* object
,
2293 unsigned int data_shndx
,
2294 Output_section
* output_section
,
2295 const elfcpp::Rela
<size
, big_endian
>& reloc
,
2296 unsigned int r_type
,
2297 const elfcpp::Sym
<size
, big_endian
>& lsym
)
2299 Powerpc_relobj
<size
, big_endian
>* ppc_object
2300 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
2304 case elfcpp::R_POWERPC_NONE
:
2305 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
2306 case elfcpp::R_POWERPC_GNU_VTENTRY
:
2307 case elfcpp::R_PPC64_TOCSAVE
:
2308 case elfcpp::R_PPC_EMB_MRKREF
:
2309 case elfcpp::R_POWERPC_TLS
:
2312 case elfcpp::R_PPC64_TOC
:
2314 Output_data_got_powerpc
<size
, big_endian
>* got
2315 = target
->got_section(symtab
, layout
);
2316 if (parameters
->options().output_is_position_independent())
2318 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2319 rela_dyn
->add_output_section_relative(got
->output_section(),
2320 elfcpp::R_POWERPC_RELATIVE
,
2323 reloc
.get_r_offset(),
2324 ppc_object
->toc_base_offset());
2329 case elfcpp::R_PPC64_ADDR64
:
2330 case elfcpp::R_PPC64_UADDR64
:
2331 case elfcpp::R_POWERPC_ADDR32
:
2332 case elfcpp::R_POWERPC_UADDR32
:
2333 case elfcpp::R_POWERPC_ADDR24
:
2334 case elfcpp::R_POWERPC_ADDR16
:
2335 case elfcpp::R_POWERPC_ADDR16_LO
:
2336 case elfcpp::R_POWERPC_ADDR16_HI
:
2337 case elfcpp::R_POWERPC_ADDR16_HA
:
2338 case elfcpp::R_POWERPC_UADDR16
:
2339 case elfcpp::R_PPC64_ADDR16_HIGHER
:
2340 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
2341 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
2342 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
2343 case elfcpp::R_PPC64_ADDR16_DS
:
2344 case elfcpp::R_PPC64_ADDR16_LO_DS
:
2345 case elfcpp::R_POWERPC_ADDR14
:
2346 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
2347 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
2348 // If building a shared library (or a position-independent
2349 // executable), we need to create a dynamic relocation for
2351 if (parameters
->options().output_is_position_independent())
2353 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2355 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
2356 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
2358 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2359 rela_dyn
->add_local_relative(object
, r_sym
,
2360 elfcpp::R_POWERPC_RELATIVE
,
2361 output_section
, data_shndx
,
2362 reloc
.get_r_offset(),
2363 reloc
.get_r_addend(), false);
2367 check_non_pic(object
, r_type
);
2368 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2369 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
2370 data_shndx
, reloc
.get_r_offset(),
2371 reloc
.get_r_addend());
2376 case elfcpp::R_PPC64_REL64
:
2377 case elfcpp::R_POWERPC_REL32
:
2378 case elfcpp::R_POWERPC_REL24
:
2379 case elfcpp::R_PPC_LOCAL24PC
:
2380 case elfcpp::R_POWERPC_REL16
:
2381 case elfcpp::R_POWERPC_REL16_LO
:
2382 case elfcpp::R_POWERPC_REL16_HI
:
2383 case elfcpp::R_POWERPC_REL16_HA
:
2384 case elfcpp::R_POWERPC_REL14
:
2385 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
2386 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
2387 case elfcpp::R_POWERPC_SECTOFF
:
2388 case elfcpp::R_POWERPC_TPREL16
:
2389 case elfcpp::R_POWERPC_DTPREL16
:
2390 case elfcpp::R_POWERPC_SECTOFF_LO
:
2391 case elfcpp::R_POWERPC_TPREL16_LO
:
2392 case elfcpp::R_POWERPC_DTPREL16_LO
:
2393 case elfcpp::R_POWERPC_SECTOFF_HI
:
2394 case elfcpp::R_POWERPC_TPREL16_HI
:
2395 case elfcpp::R_POWERPC_DTPREL16_HI
:
2396 case elfcpp::R_POWERPC_SECTOFF_HA
:
2397 case elfcpp::R_POWERPC_TPREL16_HA
:
2398 case elfcpp::R_POWERPC_DTPREL16_HA
:
2399 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
2400 case elfcpp::R_PPC64_TPREL16_HIGHER
:
2401 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
2402 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
2403 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
2404 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
2405 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
2406 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
2407 case elfcpp::R_PPC64_TPREL16_DS
:
2408 case elfcpp::R_PPC64_TPREL16_LO_DS
:
2409 case elfcpp::R_PPC64_DTPREL16_DS
:
2410 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
2411 case elfcpp::R_PPC64_SECTOFF_DS
:
2412 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
2413 case elfcpp::R_PPC64_TLSGD
:
2414 case elfcpp::R_PPC64_TLSLD
:
2417 case elfcpp::R_POWERPC_GOT16
:
2418 case elfcpp::R_POWERPC_GOT16_LO
:
2419 case elfcpp::R_POWERPC_GOT16_HI
:
2420 case elfcpp::R_POWERPC_GOT16_HA
:
2421 case elfcpp::R_PPC64_GOT16_DS
:
2422 case elfcpp::R_PPC64_GOT16_LO_DS
:
2424 // The symbol requires a GOT entry.
2425 Output_data_got_powerpc
<size
, big_endian
>* got
2426 = target
->got_section(symtab
, layout
);
2427 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2429 // If we are generating a shared object, we need to add a
2430 // dynamic relocation for this symbol's GOT entry.
2431 if (parameters
->options().output_is_position_independent())
2433 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
2435 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2438 off
= got
->add_constant(0);
2439 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
2440 rela_dyn
->add_local_relative(object
, r_sym
,
2441 elfcpp::R_POWERPC_RELATIVE
,
2442 got
, off
, 0, false);
2446 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
2450 case elfcpp::R_PPC64_TOC16
:
2451 case elfcpp::R_PPC64_TOC16_LO
:
2452 case elfcpp::R_PPC64_TOC16_HI
:
2453 case elfcpp::R_PPC64_TOC16_HA
:
2454 case elfcpp::R_PPC64_TOC16_DS
:
2455 case elfcpp::R_PPC64_TOC16_LO_DS
:
2456 // We need a GOT section.
2457 target
->got_section(symtab
, layout
);
2460 case elfcpp::R_POWERPC_GOT_TLSGD16
:
2461 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
2462 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
2463 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
2465 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
2466 if (tls_type
== tls::TLSOPT_NONE
)
2468 Output_data_got_powerpc
<size
, big_endian
>* got
2469 = target
->got_section(symtab
, layout
);
2470 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2471 unsigned int shndx
= lsym
.get_st_shndx();
2473 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
2474 gold_assert(is_ordinary
);
2475 got
->add_local_pair_with_rel(object
, r_sym
,
2478 target
->rela_dyn_section(layout
),
2479 elfcpp::R_POWERPC_DTPMOD
,
2480 elfcpp::R_POWERPC_DTPREL
);
2482 else if (tls_type
== tls::TLSOPT_TO_LE
)
2484 // no GOT relocs needed for Local Exec.
2491 case elfcpp::R_POWERPC_GOT_TLSLD16
:
2492 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
2493 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
2494 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
2496 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
2497 if (tls_type
== tls::TLSOPT_NONE
)
2498 target
->tlsld_got_offset(symtab
, layout
, object
);
2499 else if (tls_type
== tls::TLSOPT_TO_LE
)
2501 // no GOT relocs needed for Local Exec.
2502 if (parameters
->options().emit_relocs())
2504 Output_section
* os
= layout
->tls_segment()->first_section();
2505 gold_assert(os
!= NULL
);
2506 os
->set_needs_symtab_index();
2514 case elfcpp::R_POWERPC_GOT_DTPREL16
:
2515 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
2516 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
2517 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
2519 Output_data_got_powerpc
<size
, big_endian
>* got
2520 = target
->got_section(symtab
, layout
);
2521 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2522 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_DTPREL
,
2523 target
->rela_dyn_section(layout
),
2524 elfcpp::R_POWERPC_DTPREL
);
2528 case elfcpp::R_POWERPC_GOT_TPREL16
:
2529 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
2530 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
2531 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
2533 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
2534 if (tls_type
== tls::TLSOPT_NONE
)
2536 Output_data_got_powerpc
<size
, big_endian
>* got
2537 = target
->got_section(symtab
, layout
);
2538 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2539 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TPREL
,
2540 target
->rela_dyn_section(layout
),
2541 elfcpp::R_POWERPC_TPREL
);
2543 else if (tls_type
== tls::TLSOPT_TO_LE
)
2545 // no GOT relocs needed for Local Exec.
2553 unsupported_reloc_local(object
, r_type
);
2558 // Report an unsupported relocation against a global symbol.
2560 template<int size
, bool big_endian
>
2562 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
2563 Sized_relobj_file
<size
, big_endian
>* object
,
2564 unsigned int r_type
,
2567 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2568 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
2571 // Scan a relocation for a global symbol.
2573 template<int size
, bool big_endian
>
2575 Target_powerpc
<size
, big_endian
>::Scan::global(
2576 Symbol_table
* symtab
,
2578 Target_powerpc
<size
, big_endian
>* target
,
2579 Sized_relobj_file
<size
, big_endian
>* object
,
2580 unsigned int data_shndx
,
2581 Output_section
* output_section
,
2582 const elfcpp::Rela
<size
, big_endian
>& reloc
,
2583 unsigned int r_type
,
2586 Powerpc_relobj
<size
, big_endian
>* ppc_object
2587 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
2591 case elfcpp::R_POWERPC_NONE
:
2592 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
2593 case elfcpp::R_POWERPC_GNU_VTENTRY
:
2594 case elfcpp::R_PPC_LOCAL24PC
:
2595 case elfcpp::R_PPC_EMB_MRKREF
:
2596 case elfcpp::R_POWERPC_TLS
:
2599 case elfcpp::R_PPC64_TOC
:
2601 Output_data_got_powerpc
<size
, big_endian
>* got
2602 = target
->got_section(symtab
, layout
);
2603 if (parameters
->options().output_is_position_independent())
2605 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2606 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
2607 if (data_shndx
!= ppc_object
->opd_shndx())
2608 symobj
= static_cast
2609 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
2610 rela_dyn
->add_output_section_relative(got
->output_section(),
2611 elfcpp::R_POWERPC_RELATIVE
,
2614 reloc
.get_r_offset(),
2615 symobj
->toc_base_offset());
2620 case elfcpp::R_PPC64_ADDR64
:
2621 case elfcpp::R_PPC64_UADDR64
:
2622 case elfcpp::R_POWERPC_ADDR32
:
2623 case elfcpp::R_POWERPC_UADDR32
:
2624 case elfcpp::R_POWERPC_ADDR24
:
2625 case elfcpp::R_POWERPC_ADDR16
:
2626 case elfcpp::R_POWERPC_ADDR16_LO
:
2627 case elfcpp::R_POWERPC_ADDR16_HI
:
2628 case elfcpp::R_POWERPC_ADDR16_HA
:
2629 case elfcpp::R_POWERPC_UADDR16
:
2630 case elfcpp::R_PPC64_ADDR16_HIGHER
:
2631 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
2632 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
2633 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
2634 case elfcpp::R_PPC64_ADDR16_DS
:
2635 case elfcpp::R_PPC64_ADDR16_LO_DS
:
2636 case elfcpp::R_POWERPC_ADDR14
:
2637 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
2638 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
2640 // Make a PLT entry if necessary.
2641 if (gsym
->needs_plt_entry())
2643 target
->make_plt_entry(layout
, gsym
, reloc
, 0);
2644 // Since this is not a PC-relative relocation, we may be
2645 // taking the address of a function. In that case we need to
2646 // set the entry in the dynamic symbol table to the address of
2649 && gsym
->is_from_dynobj() && !parameters
->options().shared())
2650 gsym
->set_needs_dynsym_value();
2652 // Make a dynamic relocation if necessary.
2653 if (needs_dynamic_reloc
<size
>(gsym
, Scan::get_reference_flags(r_type
)))
2655 if (gsym
->may_need_copy_reloc())
2657 target
->copy_reloc(symtab
, layout
, object
,
2658 data_shndx
, output_section
, gsym
, reloc
);
2660 else if (((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
2661 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
2662 && (gsym
->can_use_relative_reloc(false)
2663 || data_shndx
== ppc_object
->opd_shndx()))
2665 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2666 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
2667 output_section
, object
,
2668 data_shndx
, reloc
.get_r_offset(),
2669 reloc
.get_r_addend(), false);
2673 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2674 check_non_pic(object
, r_type
);
2675 rela_dyn
->add_global(gsym
, r_type
, output_section
,
2677 reloc
.get_r_offset(),
2678 reloc
.get_r_addend());
2684 case elfcpp::R_PPC_PLTREL24
:
2685 case elfcpp::R_POWERPC_REL24
:
2686 if (gsym
->needs_plt_entry()
2687 || (!gsym
->final_value_is_known()
2688 && (gsym
->is_undefined()
2689 || gsym
->is_from_dynobj()
2690 || gsym
->is_preemptible())))
2691 target
->make_plt_entry(layout
, gsym
, reloc
, object
);
2694 case elfcpp::R_PPC64_REL64
:
2695 case elfcpp::R_POWERPC_REL32
:
2696 // Make a dynamic relocation if necessary.
2697 if (needs_dynamic_reloc
<size
>(gsym
, Scan::get_reference_flags(r_type
)))
2699 if (gsym
->may_need_copy_reloc())
2701 target
->copy_reloc(symtab
, layout
, object
,
2702 data_shndx
, output_section
, gsym
,
2707 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2708 check_non_pic(object
, r_type
);
2709 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2710 data_shndx
, reloc
.get_r_offset(),
2711 reloc
.get_r_addend());
2716 case elfcpp::R_POWERPC_REL16
:
2717 case elfcpp::R_POWERPC_REL16_LO
:
2718 case elfcpp::R_POWERPC_REL16_HI
:
2719 case elfcpp::R_POWERPC_REL16_HA
:
2720 case elfcpp::R_POWERPC_REL14
:
2721 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
2722 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
2723 case elfcpp::R_POWERPC_SECTOFF
:
2724 case elfcpp::R_POWERPC_TPREL16
:
2725 case elfcpp::R_POWERPC_DTPREL16
:
2726 case elfcpp::R_POWERPC_SECTOFF_LO
:
2727 case elfcpp::R_POWERPC_TPREL16_LO
:
2728 case elfcpp::R_POWERPC_DTPREL16_LO
:
2729 case elfcpp::R_POWERPC_SECTOFF_HI
:
2730 case elfcpp::R_POWERPC_TPREL16_HI
:
2731 case elfcpp::R_POWERPC_DTPREL16_HI
:
2732 case elfcpp::R_POWERPC_SECTOFF_HA
:
2733 case elfcpp::R_POWERPC_TPREL16_HA
:
2734 case elfcpp::R_POWERPC_DTPREL16_HA
:
2735 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
2736 case elfcpp::R_PPC64_TPREL16_HIGHER
:
2737 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
2738 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
2739 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
2740 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
2741 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
2742 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
2743 case elfcpp::R_PPC64_TPREL16_DS
:
2744 case elfcpp::R_PPC64_TPREL16_LO_DS
:
2745 case elfcpp::R_PPC64_DTPREL16_DS
:
2746 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
2747 case elfcpp::R_PPC64_SECTOFF_DS
:
2748 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
2749 case elfcpp::R_PPC64_TLSGD
:
2750 case elfcpp::R_PPC64_TLSLD
:
2753 case elfcpp::R_POWERPC_GOT16
:
2754 case elfcpp::R_POWERPC_GOT16_LO
:
2755 case elfcpp::R_POWERPC_GOT16_HI
:
2756 case elfcpp::R_POWERPC_GOT16_HA
:
2757 case elfcpp::R_PPC64_GOT16_DS
:
2758 case elfcpp::R_PPC64_GOT16_LO_DS
:
2760 // The symbol requires a GOT entry.
2761 Output_data_got_powerpc
<size
, big_endian
>* got
;
2763 got
= target
->got_section(symtab
, layout
);
2764 if (gsym
->final_value_is_known())
2765 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2768 // If this symbol is not fully resolved, we need to add a
2769 // dynamic relocation for it.
2770 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2771 if (gsym
->is_from_dynobj()
2772 || gsym
->is_undefined()
2773 || gsym
->is_preemptible())
2774 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
2775 elfcpp::R_POWERPC_GLOB_DAT
);
2776 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
2778 unsigned int off
= got
->add_constant(0);
2780 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
2781 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
2782 got
, off
, 0, false);
2788 case elfcpp::R_PPC64_TOC16
:
2789 case elfcpp::R_PPC64_TOC16_LO
:
2790 case elfcpp::R_PPC64_TOC16_HI
:
2791 case elfcpp::R_PPC64_TOC16_HA
:
2792 case elfcpp::R_PPC64_TOC16_DS
:
2793 case elfcpp::R_PPC64_TOC16_LO_DS
:
2794 // We need a GOT section.
2795 target
->got_section(symtab
, layout
);
2798 case elfcpp::R_POWERPC_GOT_TLSGD16
:
2799 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
2800 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
2801 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
2803 const bool final
= gsym
->final_value_is_known();
2804 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
2805 if (tls_type
== tls::TLSOPT_NONE
)
2807 Output_data_got_powerpc
<size
, big_endian
>* got
2808 = target
->got_section(symtab
, layout
);
2809 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
,
2810 target
->rela_dyn_section(layout
),
2811 elfcpp::R_POWERPC_DTPMOD
,
2812 elfcpp::R_POWERPC_DTPREL
);
2814 else if (tls_type
== tls::TLSOPT_TO_IE
)
2816 Output_data_got_powerpc
<size
, big_endian
>* got
2817 = target
->got_section(symtab
, layout
);
2818 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
,
2819 target
->rela_dyn_section(layout
),
2820 elfcpp::R_POWERPC_TPREL
);
2822 else if (tls_type
== tls::TLSOPT_TO_LE
)
2824 // no GOT relocs needed for Local Exec.
2831 case elfcpp::R_POWERPC_GOT_TLSLD16
:
2832 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
2833 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
2834 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
2836 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
2837 if (tls_type
== tls::TLSOPT_NONE
)
2838 target
->tlsld_got_offset(symtab
, layout
, object
);
2839 else if (tls_type
== tls::TLSOPT_TO_LE
)
2841 // no GOT relocs needed for Local Exec.
2842 if (parameters
->options().emit_relocs())
2844 Output_section
* os
= layout
->tls_segment()->first_section();
2845 gold_assert(os
!= NULL
);
2846 os
->set_needs_symtab_index();
2854 case elfcpp::R_POWERPC_GOT_DTPREL16
:
2855 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
2856 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
2857 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
2859 Output_data_got_powerpc
<size
, big_endian
>* got
2860 = target
->got_section(symtab
, layout
);
2861 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
2862 target
->rela_dyn_section(layout
),
2863 elfcpp::R_POWERPC_DTPREL
);
2867 case elfcpp::R_POWERPC_GOT_TPREL16
:
2868 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
2869 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
2870 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
2872 const bool final
= gsym
->final_value_is_known();
2873 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
2874 if (tls_type
== tls::TLSOPT_NONE
)
2876 Output_data_got_powerpc
<size
, big_endian
>* got
2877 = target
->got_section(symtab
, layout
);
2878 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
,
2879 target
->rela_dyn_section(layout
),
2880 elfcpp::R_POWERPC_TPREL
);
2882 else if (tls_type
== tls::TLSOPT_TO_LE
)
2884 // no GOT relocs needed for Local Exec.
2892 unsupported_reloc_global(object
, r_type
, gsym
);
2897 // Process relocations for gc.
2899 template<int size
, bool big_endian
>
2901 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
2902 Symbol_table
* symtab
,
2904 Sized_relobj_file
<size
, big_endian
>* object
,
2905 unsigned int data_shndx
,
2907 const unsigned char* prelocs
,
2909 Output_section
* output_section
,
2910 bool needs_special_offset_handling
,
2911 size_t local_symbol_count
,
2912 const unsigned char* plocal_symbols
)
2914 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
2915 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
2917 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
2918 typename
Target_powerpc::Relocatable_size_for_reloc
>(
2927 needs_special_offset_handling
,
2932 // Scan relocations for a section.
2934 template<int size
, bool big_endian
>
2936 Target_powerpc
<size
, big_endian
>::scan_relocs(
2937 Symbol_table
* symtab
,
2939 Sized_relobj_file
<size
, big_endian
>* object
,
2940 unsigned int data_shndx
,
2941 unsigned int sh_type
,
2942 const unsigned char* prelocs
,
2944 Output_section
* output_section
,
2945 bool needs_special_offset_handling
,
2946 size_t local_symbol_count
,
2947 const unsigned char* plocal_symbols
)
2949 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
2950 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
2952 if (sh_type
== elfcpp::SHT_REL
)
2954 gold_error(_("%s: unsupported REL reloc section"),
2955 object
->name().c_str());
2961 static Output_data_space
* sdata
;
2963 // Define _SDA_BASE_ at the start of the .sdata section.
2966 // layout->find_output_section(".sdata") == NULL
2967 sdata
= new Output_data_space(4, "** sdata");
2969 = layout
->add_output_section_data(".sdata", 0,
2971 | elfcpp::SHF_WRITE
,
2972 sdata
, ORDER_SMALL_DATA
, false);
2973 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
2974 Symbol_table::PREDEFINED
,
2975 os
, 32768, 0, elfcpp::STT_OBJECT
,
2976 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
2981 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
2990 needs_special_offset_handling
,
2995 // Finalize the sections.
2997 template<int size
, bool big_endian
>
2999 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
3001 const Input_objects
*,
3004 // Fill in some more dynamic tags.
3005 const Reloc_section
* rel_plt
= (this->plt_
== NULL
3007 : this->plt_
->rel_plt());
3008 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
3009 this->rela_dyn_
, true, size
== 32);
3011 Output_data_dynamic
* odyn
= layout
->dynamic_data();
3014 if (this->got_
!= NULL
)
3016 this->got_
->finalize_data_size();
3017 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
3018 this->got_
, this->got_
->g_o_t());
3023 if (this->glink_
!= NULL
)
3025 this->glink_
->finalize_data_size();
3026 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
3028 (this->glink_
->pltresolve()
3029 + this->glink_
->pltresolve_size
- 32));
3033 // Emit any relocs we saved in an attempt to avoid generating COPY
3035 if (this->copy_relocs_
.any_saved_relocs())
3036 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
3039 // Return the value to use for a branch relocation.
3041 template<int size
, bool big_endian
>
3042 typename
elfcpp::Elf_types
<size
>::Elf_Addr
3043 Target_powerpc
<size
, big_endian
>::symval_for_branch(
3045 const Sized_symbol
<size
>* gsym
,
3046 Powerpc_relobj
<size
, big_endian
>* object
,
3047 unsigned int *dest_shndx
)
3053 // If the symbol is defined in an opd section, ie. is a function
3054 // descriptor, use the function descriptor code entry address
3055 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
3057 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
3058 unsigned int shndx
= symobj
->opd_shndx();
3061 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
3062 gold_assert(opd_addr
!= invalid_address
);
3063 opd_addr
+= symobj
->output_section(shndx
)->address();
3064 if (value
>= opd_addr
&& value
< opd_addr
+ symobj
->section_size(shndx
))
3067 symobj
->get_opd_ent(value
- opd_addr
, dest_shndx
, &sec_off
);
3068 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
3069 gold_assert(sec_addr
!= invalid_address
);
3070 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
3071 value
= sec_addr
+ sec_off
;
3076 // Perform a relocation.
3078 template<int size
, bool big_endian
>
3080 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
3081 const Relocate_info
<size
, big_endian
>* relinfo
,
3082 Target_powerpc
* target
,
3085 const elfcpp::Rela
<size
, big_endian
>& rela
,
3086 unsigned int r_type
,
3087 const Sized_symbol
<size
>* gsym
,
3088 const Symbol_value
<size
>* psymval
,
3089 unsigned char* view
,
3091 section_size_type view_size
)
3094 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
3095 || r_type
== elfcpp::R_PPC_PLTREL24
)
3097 && strcmp(gsym
->name(), "__tls_get_addr") == 0);
3098 enum skip_tls last_tls
= this->call_tls_get_addr_
;
3099 this->call_tls_get_addr_
= CALL_NOT_EXPECTED
;
3102 if (last_tls
== CALL_NOT_EXPECTED
)
3103 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3104 _("__tls_get_addr call lacks marker reloc"));
3105 else if (last_tls
== CALL_SKIP
)
3108 else if (last_tls
!= CALL_NOT_EXPECTED
)
3109 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3110 _("missing expected __tls_get_addr call"));
3112 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
3113 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
3114 Powerpc_relobj
<size
, big_endian
>* const object
3115 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
3117 bool has_plt_value
= false;
3119 && use_plt_offset
<size
>(gsym
, Scan::get_reference_flags(r_type
)))
3121 const Output_data_glink
<size
, big_endian
>* glink
3122 = target
->glink_section();
3123 unsigned int glink_index
= glink
->find_entry(gsym
, rela
, object
);
3124 value
= glink
->address() + glink_index
* glink
->glink_entry_size();
3125 has_plt_value
= true;
3128 if (r_type
== elfcpp::R_POWERPC_GOT16
3129 || r_type
== elfcpp::R_POWERPC_GOT16_LO
3130 || r_type
== elfcpp::R_POWERPC_GOT16_HI
3131 || r_type
== elfcpp::R_POWERPC_GOT16_HA
3132 || r_type
== elfcpp::R_PPC64_GOT16_DS
3133 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
3137 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
3138 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
3142 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3143 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3144 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
3146 value
-= target
->got_section()->got_base_offset(object
);
3148 else if (r_type
== elfcpp::R_PPC64_TOC
)
3150 value
= (target
->got_section()->output_section()->address()
3151 + object
->toc_base_offset());
3153 else if (gsym
!= NULL
3154 && (r_type
== elfcpp::R_POWERPC_REL24
3155 || r_type
== elfcpp::R_PPC_PLTREL24
)
3160 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
3161 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3162 bool can_plt_call
= false;
3163 if (rela
.get_r_offset() + 8 <= view_size
)
3165 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
3166 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
3169 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
3171 elfcpp::Swap
<32, big_endian
>::writeval(wv
+ 1, ld_2_1
+ 40);
3172 can_plt_call
= true;
3177 // If we don't have a branch and link followed by a nop,
3178 // we can't go via the plt because there is no place to
3179 // put a toc restoring instruction.
3180 // Unless we know we won't be returning.
3181 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
3182 can_plt_call
= true;
3186 // This is not an error in one special case: A self
3187 // call. It isn't possible to cheaply verify we have
3188 // such a call so just check for a call to the same
3191 if (gsym
->source() == Symbol::FROM_OBJECT
3192 && gsym
->object() == object
)
3194 Address addend
= rela
.get_r_addend();
3195 unsigned int dest_shndx
;
3196 value
= psymval
->value(object
, addend
);
3197 value
= target
->symval_for_branch(value
, gsym
, object
,
3200 if (dest_shndx
== 0)
3201 dest_shndx
= gsym
->shndx(&is_ordinary
);
3202 ok
= dest_shndx
== relinfo
->data_shndx
;
3205 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3206 _("call lacks nop, can't restore toc; "
3207 "recompile with -fPIC"));
3211 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
3212 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
3213 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
3214 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
3216 // First instruction of a global dynamic sequence, arg setup insn.
3217 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
3218 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
3219 enum Got_type got_type
= GOT_TYPE_STANDARD
;
3220 if (tls_type
== tls::TLSOPT_NONE
)
3221 got_type
= GOT_TYPE_TLSGD
;
3222 else if (tls_type
== tls::TLSOPT_TO_IE
)
3223 got_type
= GOT_TYPE_TPREL
;
3224 if (got_type
!= GOT_TYPE_STANDARD
)
3228 gold_assert(gsym
->has_got_offset(got_type
));
3229 value
= gsym
->got_offset(got_type
);
3233 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3234 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3235 value
= object
->local_got_offset(r_sym
, got_type
);
3237 value
-= target
->got_section()->got_base_offset(object
);
3239 if (tls_type
== tls::TLSOPT_TO_IE
)
3241 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
3242 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
3244 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
3245 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
3246 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
3248 insn
|= 32 << 26; // lwz
3250 insn
|= 58 << 26; // ld
3251 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
3253 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
3254 - elfcpp::R_POWERPC_GOT_TLSGD16
);
3256 else if (tls_type
== tls::TLSOPT_TO_LE
)
3258 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
3259 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
3261 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
3262 Insn insn
= addis_3_13
;
3265 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
3266 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
3267 value
= psymval
->value(object
, rela
.get_r_addend());
3271 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
3273 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
3274 r_type
= elfcpp::R_POWERPC_NONE
;
3278 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
3279 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
3280 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
3281 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
3283 // First instruction of a local dynamic sequence, arg setup insn.
3284 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
3285 if (tls_type
== tls::TLSOPT_NONE
)
3287 value
= target
->tlsld_got_offset();
3288 value
-= target
->got_section()->got_base_offset(object
);
3292 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
3293 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
3294 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
3296 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
3297 Insn insn
= addis_3_13
;
3300 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
3301 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
3306 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
3308 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
3309 r_type
= elfcpp::R_POWERPC_NONE
;
3313 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
3314 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
3315 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
3316 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
3318 // Accesses relative to a local dynamic sequence address,
3319 // no optimisation here.
3322 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
3323 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
3327 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3328 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
3329 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
3331 value
-= target
->got_section()->got_base_offset(object
);
3333 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
3334 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
3335 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
3336 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
3338 // First instruction of initial exec sequence.
3339 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
3340 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
3341 if (tls_type
== tls::TLSOPT_NONE
)
3345 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
3346 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
3350 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3351 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
3352 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
3354 value
-= target
->got_section()->got_base_offset(object
);
3358 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
3359 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
3360 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
3362 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
3363 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
3364 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
3369 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
3370 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
3371 value
= psymval
->value(object
, rela
.get_r_addend());
3375 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
3377 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
3378 r_type
= elfcpp::R_POWERPC_NONE
;
3382 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
3383 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
3385 // Second instruction of a global dynamic sequence,
3386 // the __tls_get_addr call
3387 this->call_tls_get_addr_
= CALL_EXPECTED
;
3388 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
3389 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
3390 if (tls_type
!= tls::TLSOPT_NONE
)
3392 if (tls_type
== tls::TLSOPT_TO_IE
)
3394 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
3395 Insn insn
= add_3_3_13
;
3398 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
3399 r_type
= elfcpp::R_POWERPC_NONE
;
3403 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
3404 Insn insn
= addi_3_3
;
3405 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
3406 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
3407 view
+= 2 * big_endian
;
3408 value
= psymval
->value(object
, rela
.get_r_addend());
3410 this->call_tls_get_addr_
= CALL_SKIP
;
3413 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
3414 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
3416 // Second instruction of a local dynamic sequence,
3417 // the __tls_get_addr call
3418 this->call_tls_get_addr_
= CALL_EXPECTED
;
3419 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
3420 if (tls_type
== tls::TLSOPT_TO_LE
)
3422 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
3423 Insn insn
= addi_3_3
;
3424 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
3425 this->call_tls_get_addr_
= CALL_SKIP
;
3426 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
3427 view
+= 2 * big_endian
;
3431 else if (r_type
== elfcpp::R_POWERPC_TLS
)
3433 // Second instruction of an initial exec sequence
3434 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
3435 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
3436 if (tls_type
== tls::TLSOPT_TO_LE
)
3438 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
3439 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
3440 unsigned int reg
= size
== 32 ? 2 : 13;
3441 insn
= at_tls_transform(insn
, reg
);
3442 gold_assert(insn
!= 0);
3443 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
3444 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
3445 view
+= 2 * big_endian
;
3446 value
= psymval
->value(object
, rela
.get_r_addend());
3452 unsigned int dest_shndx
;
3453 if (r_type
!= elfcpp::R_PPC_PLTREL24
)
3454 addend
= rela
.get_r_addend();
3455 if (size
== 64 || !has_plt_value
)
3456 value
= psymval
->value(object
, addend
);
3457 if (size
== 64 && is_branch_reloc(r_type
))
3458 value
= target
->symval_for_branch(value
, gsym
, object
, &dest_shndx
);
3463 case elfcpp::R_PPC64_REL64
:
3464 case elfcpp::R_POWERPC_REL32
:
3465 case elfcpp::R_POWERPC_REL24
:
3466 case elfcpp::R_PPC_PLTREL24
:
3467 case elfcpp::R_PPC_LOCAL24PC
:
3468 case elfcpp::R_POWERPC_REL16
:
3469 case elfcpp::R_POWERPC_REL16_LO
:
3470 case elfcpp::R_POWERPC_REL16_HI
:
3471 case elfcpp::R_POWERPC_REL16_HA
:
3472 case elfcpp::R_POWERPC_REL14
:
3473 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
3474 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
3478 case elfcpp::R_PPC64_TOC16
:
3479 case elfcpp::R_PPC64_TOC16_LO
:
3480 case elfcpp::R_PPC64_TOC16_HI
:
3481 case elfcpp::R_PPC64_TOC16_HA
:
3482 case elfcpp::R_PPC64_TOC16_DS
:
3483 case elfcpp::R_PPC64_TOC16_LO_DS
:
3484 // Subtract the TOC base address.
3485 value
-= (target
->got_section()->output_section()->address()
3486 + object
->toc_base_offset());
3489 case elfcpp::R_POWERPC_SECTOFF
:
3490 case elfcpp::R_POWERPC_SECTOFF_LO
:
3491 case elfcpp::R_POWERPC_SECTOFF_HI
:
3492 case elfcpp::R_POWERPC_SECTOFF_HA
:
3493 case elfcpp::R_PPC64_SECTOFF_DS
:
3494 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
3496 value
-= os
->address();
3499 case elfcpp::R_PPC64_TPREL16_DS
:
3500 case elfcpp::R_PPC64_TPREL16_LO_DS
:
3502 // R_PPC_TLSGD and R_PPC_TLSLD
3504 case elfcpp::R_POWERPC_TPREL16
:
3505 case elfcpp::R_POWERPC_TPREL16_LO
:
3506 case elfcpp::R_POWERPC_TPREL16_HI
:
3507 case elfcpp::R_POWERPC_TPREL16_HA
:
3508 case elfcpp::R_POWERPC_TPREL
:
3509 case elfcpp::R_PPC64_TPREL16_HIGHER
:
3510 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
3511 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
3512 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
3513 // tls symbol values are relative to tls_segment()->vaddr()
3517 case elfcpp::R_PPC64_DTPREL16_DS
:
3518 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
3519 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
3520 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
3521 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
3522 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
3524 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
3525 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
3527 case elfcpp::R_POWERPC_DTPREL16
:
3528 case elfcpp::R_POWERPC_DTPREL16_LO
:
3529 case elfcpp::R_POWERPC_DTPREL16_HI
:
3530 case elfcpp::R_POWERPC_DTPREL16_HA
:
3531 case elfcpp::R_POWERPC_DTPREL
:
3532 // tls symbol values are relative to tls_segment()->vaddr()
3533 value
-= dtp_offset
;
3540 Insn branch_bit
= 0;
3543 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
3544 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
3545 branch_bit
= 1 << 21;
3546 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
3547 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
3549 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
3550 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
3553 if (this->is_isa_v2
)
3555 // Set 'a' bit. This is 0b00010 in BO field for branch
3556 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
3557 // for branch on CTR insns (BO == 1a00t or 1a01t).
3558 if ((insn
& (0x14 << 21)) == (0x04 << 21))
3560 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
3567 // Invert 'y' bit if not the default.
3568 if (static_cast<Signed_address
>(value
) < 0)
3571 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
3579 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
3582 case elfcpp::R_POWERPC_ADDR32
:
3583 case elfcpp::R_POWERPC_UADDR32
:
3585 overflow
= Reloc::CHECK_BITFIELD
;
3588 case elfcpp::R_POWERPC_REL32
:
3590 overflow
= Reloc::CHECK_SIGNED
;
3593 case elfcpp::R_POWERPC_ADDR24
:
3594 case elfcpp::R_POWERPC_ADDR16
:
3595 case elfcpp::R_POWERPC_UADDR16
:
3596 case elfcpp::R_PPC64_ADDR16_DS
:
3597 case elfcpp::R_POWERPC_ADDR14
:
3598 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
3599 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
3600 overflow
= Reloc::CHECK_BITFIELD
;
3603 case elfcpp::R_POWERPC_REL24
:
3604 case elfcpp::R_PPC_PLTREL24
:
3605 case elfcpp::R_PPC_LOCAL24PC
:
3606 case elfcpp::R_POWERPC_REL16
:
3607 case elfcpp::R_PPC64_TOC16
:
3608 case elfcpp::R_POWERPC_GOT16
:
3609 case elfcpp::R_POWERPC_SECTOFF
:
3610 case elfcpp::R_POWERPC_TPREL16
:
3611 case elfcpp::R_POWERPC_DTPREL16
:
3612 case elfcpp::R_PPC64_TPREL16_DS
:
3613 case elfcpp::R_PPC64_DTPREL16_DS
:
3614 case elfcpp::R_PPC64_TOC16_DS
:
3615 case elfcpp::R_PPC64_GOT16_DS
:
3616 case elfcpp::R_PPC64_SECTOFF_DS
:
3617 case elfcpp::R_POWERPC_REL14
:
3618 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
3619 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
3620 case elfcpp::R_POWERPC_GOT_TLSGD16
:
3621 case elfcpp::R_POWERPC_GOT_TLSLD16
:
3622 case elfcpp::R_POWERPC_GOT_TPREL16
:
3623 case elfcpp::R_POWERPC_GOT_DTPREL16
:
3624 overflow
= Reloc::CHECK_SIGNED
;
3628 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
3629 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
3632 case elfcpp::R_POWERPC_NONE
:
3633 case elfcpp::R_POWERPC_TLS
:
3634 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
3635 case elfcpp::R_POWERPC_GNU_VTENTRY
:
3636 case elfcpp::R_PPC_EMB_MRKREF
:
3639 case elfcpp::R_PPC64_ADDR64
:
3640 case elfcpp::R_PPC64_REL64
:
3641 case elfcpp::R_PPC64_TOC
:
3642 Reloc::addr64(view
, value
);
3645 case elfcpp::R_POWERPC_TPREL
:
3646 case elfcpp::R_POWERPC_DTPREL
:
3648 Reloc::addr64(view
, value
);
3650 status
= Reloc::addr32(view
, value
, overflow
);
3653 case elfcpp::R_PPC64_UADDR64
:
3654 Reloc::addr64_u(view
, value
);
3657 case elfcpp::R_POWERPC_ADDR32
:
3658 case elfcpp::R_POWERPC_REL32
:
3659 status
= Reloc::addr32(view
, value
, overflow
);
3662 case elfcpp::R_POWERPC_UADDR32
:
3663 status
= Reloc::addr32_u(view
, value
, overflow
);
3666 case elfcpp::R_POWERPC_ADDR24
:
3667 case elfcpp::R_POWERPC_REL24
:
3668 case elfcpp::R_PPC_PLTREL24
:
3669 case elfcpp::R_PPC_LOCAL24PC
:
3670 status
= Reloc::addr24(view
, value
, overflow
);
3673 case elfcpp::R_POWERPC_GOT_DTPREL16
:
3674 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
3677 status
= Reloc::addr16_ds(view
, value
, overflow
);
3680 case elfcpp::R_POWERPC_ADDR16
:
3681 case elfcpp::R_POWERPC_REL16
:
3682 case elfcpp::R_PPC64_TOC16
:
3683 case elfcpp::R_POWERPC_GOT16
:
3684 case elfcpp::R_POWERPC_SECTOFF
:
3685 case elfcpp::R_POWERPC_TPREL16
:
3686 case elfcpp::R_POWERPC_DTPREL16
:
3687 case elfcpp::R_POWERPC_GOT_TLSGD16
:
3688 case elfcpp::R_POWERPC_GOT_TLSLD16
:
3689 case elfcpp::R_POWERPC_GOT_TPREL16
:
3690 case elfcpp::R_POWERPC_ADDR16_LO
:
3691 case elfcpp::R_POWERPC_REL16_LO
:
3692 case elfcpp::R_PPC64_TOC16_LO
:
3693 case elfcpp::R_POWERPC_GOT16_LO
:
3694 case elfcpp::R_POWERPC_SECTOFF_LO
:
3695 case elfcpp::R_POWERPC_TPREL16_LO
:
3696 case elfcpp::R_POWERPC_DTPREL16_LO
:
3697 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
3698 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
3699 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
3700 status
= Reloc::addr16(view
, value
, overflow
);
3703 case elfcpp::R_POWERPC_UADDR16
:
3704 status
= Reloc::addr16_u(view
, value
, overflow
);
3707 case elfcpp::R_POWERPC_ADDR16_HI
:
3708 case elfcpp::R_POWERPC_REL16_HI
:
3709 case elfcpp::R_PPC64_TOC16_HI
:
3710 case elfcpp::R_POWERPC_GOT16_HI
:
3711 case elfcpp::R_POWERPC_SECTOFF_HI
:
3712 case elfcpp::R_POWERPC_TPREL16_HI
:
3713 case elfcpp::R_POWERPC_DTPREL16_HI
:
3714 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
3715 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
3716 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
3717 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
3718 Reloc::addr16_hi(view
, value
);
3721 case elfcpp::R_POWERPC_ADDR16_HA
:
3722 case elfcpp::R_POWERPC_REL16_HA
:
3723 case elfcpp::R_PPC64_TOC16_HA
:
3724 case elfcpp::R_POWERPC_GOT16_HA
:
3725 case elfcpp::R_POWERPC_SECTOFF_HA
:
3726 case elfcpp::R_POWERPC_TPREL16_HA
:
3727 case elfcpp::R_POWERPC_DTPREL16_HA
:
3728 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
3729 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
3730 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
3731 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
3732 Reloc::addr16_ha(view
, value
);
3735 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
3737 // R_PPC_EMB_NADDR16_LO
3739 case elfcpp::R_PPC64_ADDR16_HIGHER
:
3740 case elfcpp::R_PPC64_TPREL16_HIGHER
:
3741 Reloc::addr16_hi2(view
, value
);
3744 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
3746 // R_PPC_EMB_NADDR16_HI
3748 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
3749 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
3750 Reloc::addr16_ha2(view
, value
);
3753 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
3755 // R_PPC_EMB_NADDR16_HA
3757 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
3758 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
3759 Reloc::addr16_hi3(view
, value
);
3762 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
3766 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
3767 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
3768 Reloc::addr16_ha3(view
, value
);
3771 case elfcpp::R_PPC64_DTPREL16_DS
:
3772 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
3774 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
3776 case elfcpp::R_PPC64_TPREL16_DS
:
3777 case elfcpp::R_PPC64_TPREL16_LO_DS
:
3779 // R_PPC_TLSGD, R_PPC_TLSLD
3781 case elfcpp::R_PPC64_ADDR16_DS
:
3782 case elfcpp::R_PPC64_ADDR16_LO_DS
:
3783 case elfcpp::R_PPC64_TOC16_DS
:
3784 case elfcpp::R_PPC64_TOC16_LO_DS
:
3785 case elfcpp::R_PPC64_GOT16_DS
:
3786 case elfcpp::R_PPC64_GOT16_LO_DS
:
3787 case elfcpp::R_PPC64_SECTOFF_DS
:
3788 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
3789 status
= Reloc::addr16_ds(view
, value
, overflow
);
3792 case elfcpp::R_POWERPC_ADDR14
:
3793 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
3794 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
3795 case elfcpp::R_POWERPC_REL14
:
3796 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
3797 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
3798 status
= Reloc::addr14(view
, value
, overflow
);
3801 case elfcpp::R_POWERPC_COPY
:
3802 case elfcpp::R_POWERPC_GLOB_DAT
:
3803 case elfcpp::R_POWERPC_JMP_SLOT
:
3804 case elfcpp::R_POWERPC_RELATIVE
:
3805 case elfcpp::R_POWERPC_DTPMOD
:
3806 case elfcpp::R_PPC64_JMP_IREL
:
3807 case elfcpp::R_POWERPC_IRELATIVE
:
3808 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3809 _("unexpected reloc %u in object file"),
3813 case elfcpp::R_PPC_EMB_SDA21
:
3818 // R_PPC64_TOCSAVE. For the time being this can be ignored.
3822 case elfcpp::R_PPC_EMB_SDA2I16
:
3823 case elfcpp::R_PPC_EMB_SDA2REL
:
3826 // R_PPC64_TLSGD, R_PPC64_TLSLD
3829 case elfcpp::R_POWERPC_PLT32
:
3830 case elfcpp::R_POWERPC_PLTREL32
:
3831 case elfcpp::R_POWERPC_PLT16_LO
:
3832 case elfcpp::R_POWERPC_PLT16_HI
:
3833 case elfcpp::R_POWERPC_PLT16_HA
:
3834 case elfcpp::R_PPC_SDAREL16
:
3835 case elfcpp::R_POWERPC_ADDR30
:
3836 case elfcpp::R_PPC64_PLT64
:
3837 case elfcpp::R_PPC64_PLTREL64
:
3838 case elfcpp::R_PPC64_PLTGOT16
:
3839 case elfcpp::R_PPC64_PLTGOT16_LO
:
3840 case elfcpp::R_PPC64_PLTGOT16_HI
:
3841 case elfcpp::R_PPC64_PLTGOT16_HA
:
3842 case elfcpp::R_PPC64_PLT16_LO_DS
:
3843 case elfcpp::R_PPC64_PLTGOT16_DS
:
3844 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
3845 case elfcpp::R_PPC_EMB_RELSEC16
:
3846 case elfcpp::R_PPC_EMB_RELST_LO
:
3847 case elfcpp::R_PPC_EMB_RELST_HI
:
3848 case elfcpp::R_PPC_EMB_RELST_HA
:
3849 case elfcpp::R_PPC_EMB_BIT_FLD
:
3850 case elfcpp::R_PPC_EMB_RELSDA
:
3851 case elfcpp::R_PPC_TOC16
:
3854 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3855 _("unsupported reloc %u"),
3859 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
)
3860 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3861 _("relocation overflow"));
3866 // Relocate section data.
3868 template<int size
, bool big_endian
>
3870 Target_powerpc
<size
, big_endian
>::relocate_section(
3871 const Relocate_info
<size
, big_endian
>* relinfo
,
3872 unsigned int sh_type
,
3873 const unsigned char* prelocs
,
3875 Output_section
* output_section
,
3876 bool needs_special_offset_handling
,
3877 unsigned char* view
,
3879 section_size_type view_size
,
3880 const Reloc_symbol_changes
* reloc_symbol_changes
)
3882 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
3883 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
3885 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3887 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
3894 needs_special_offset_handling
,
3898 reloc_symbol_changes
);
3901 class Powerpc_scan_relocatable_reloc
3904 // Return the strategy to use for a local symbol which is not a
3905 // section symbol, given the relocation type.
3906 inline Relocatable_relocs::Reloc_strategy
3907 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
3909 if (r_type
== 0 && r_sym
== 0)
3910 return Relocatable_relocs::RELOC_DISCARD
;
3911 return Relocatable_relocs::RELOC_COPY
;
3914 // Return the strategy to use for a local symbol which is a section
3915 // symbol, given the relocation type.
3916 inline Relocatable_relocs::Reloc_strategy
3917 local_section_strategy(unsigned int, Relobj
*)
3919 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
3922 // Return the strategy to use for a global symbol, given the
3923 // relocation type, the object, and the symbol index.
3924 inline Relocatable_relocs::Reloc_strategy
3925 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
3927 if (r_type
== elfcpp::R_PPC_PLTREL24
)
3928 return Relocatable_relocs::RELOC_SPECIAL
;
3929 return Relocatable_relocs::RELOC_COPY
;
3933 // Scan the relocs during a relocatable link.
3935 template<int size
, bool big_endian
>
3937 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
3938 Symbol_table
* symtab
,
3940 Sized_relobj_file
<size
, big_endian
>* object
,
3941 unsigned int data_shndx
,
3942 unsigned int sh_type
,
3943 const unsigned char* prelocs
,
3945 Output_section
* output_section
,
3946 bool needs_special_offset_handling
,
3947 size_t local_symbol_count
,
3948 const unsigned char* plocal_symbols
,
3949 Relocatable_relocs
* rr
)
3951 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3953 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
3954 Powerpc_scan_relocatable_reloc
>(
3962 needs_special_offset_handling
,
3968 // Emit relocations for a section.
3969 // This is a modified version of the function by the same name in
3970 // target-reloc.h. Using relocate_special_relocatable for
3971 // R_PPC_PLTREL24 would require duplication of the entire body of the
3972 // loop, so we may as well duplicate the whole thing.
3974 template<int size
, bool big_endian
>
3976 Target_powerpc
<size
, big_endian
>::relocate_relocs(
3977 const Relocate_info
<size
, big_endian
>* relinfo
,
3978 unsigned int sh_type
,
3979 const unsigned char* prelocs
,
3981 Output_section
* output_section
,
3982 off_t offset_in_output_section
,
3983 const Relocatable_relocs
* rr
,
3985 Address view_address
,
3987 unsigned char* reloc_view
,
3988 section_size_type reloc_view_size
)
3990 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3992 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
3994 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc_write
3996 const int reloc_size
3997 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
3999 Powerpc_relobj
<size
, big_endian
>* const object
4000 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
4001 const unsigned int local_count
= object
->local_symbol_count();
4002 unsigned int got2_shndx
= object
->got2_shndx();
4003 Address got2_addend
= 0;
4004 if (got2_shndx
!= 0)
4006 got2_addend
= object
->get_output_section_offset(got2_shndx
);
4007 gold_assert(got2_addend
!= invalid_address
);
4010 unsigned char* pwrite
= reloc_view
;
4011 bool zap_next
= false;
4012 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
4014 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
4015 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
4018 Reltype
reloc(prelocs
);
4019 Reltype_write
reloc_write(pwrite
);
4021 Address offset
= reloc
.get_r_offset();
4022 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
4023 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
4024 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
4025 const unsigned int orig_r_sym
= r_sym
;
4026 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
4027 = reloc
.get_r_addend();
4028 const Symbol
* gsym
= NULL
;
4032 // We could arrange to discard these and other relocs for
4033 // tls optimised sequences in the strategy methods, but for
4034 // now do as BFD ld does.
4035 r_type
= elfcpp::R_POWERPC_NONE
;
4039 // Get the new symbol index.
4040 if (r_sym
< local_count
)
4044 case Relocatable_relocs::RELOC_COPY
:
4045 case Relocatable_relocs::RELOC_SPECIAL
:
4048 r_sym
= object
->symtab_index(r_sym
);
4049 gold_assert(r_sym
!= -1U);
4053 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
4055 // We are adjusting a section symbol. We need to find
4056 // the symbol table index of the section symbol for
4057 // the output section corresponding to input section
4058 // in which this symbol is defined.
4059 gold_assert(r_sym
< local_count
);
4061 unsigned int shndx
=
4062 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
4063 gold_assert(is_ordinary
);
4064 Output_section
* os
= object
->output_section(shndx
);
4065 gold_assert(os
!= NULL
);
4066 gold_assert(os
->needs_symtab_index());
4067 r_sym
= os
->symtab_index();
4077 gsym
= object
->global_symbol(r_sym
);
4078 gold_assert(gsym
!= NULL
);
4079 if (gsym
->is_forwarder())
4080 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
4082 gold_assert(gsym
->has_symtab_index());
4083 r_sym
= gsym
->symtab_index();
4086 // Get the new offset--the location in the output section where
4087 // this relocation should be applied.
4088 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
4089 offset
+= offset_in_output_section
;
4092 section_offset_type sot_offset
=
4093 convert_types
<section_offset_type
, Address
>(offset
);
4094 section_offset_type new_sot_offset
=
4095 output_section
->output_offset(object
, relinfo
->data_shndx
,
4097 gold_assert(new_sot_offset
!= -1);
4098 offset
= new_sot_offset
;
4101 // In an object file, r_offset is an offset within the section.
4102 // In an executable or dynamic object, generated by
4103 // --emit-relocs, r_offset is an absolute address.
4104 if (!parameters
->options().relocatable())
4106 offset
+= view_address
;
4107 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
4108 offset
-= offset_in_output_section
;
4111 // Handle the reloc addend based on the strategy.
4112 if (strategy
== Relocatable_relocs::RELOC_COPY
)
4114 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
4116 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
4117 addend
= psymval
->value(object
, addend
);
4119 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
4121 if (addend
>= 32768)
4122 addend
+= got2_addend
;
4127 if (!parameters
->options().relocatable())
4129 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
4130 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
4131 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
4132 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
4134 // First instruction of a global dynamic sequence,
4136 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
4137 switch (this->optimize_tls_gd(final
))
4139 case tls::TLSOPT_TO_IE
:
4140 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
4141 - elfcpp::R_POWERPC_GOT_TLSGD16
);
4143 case tls::TLSOPT_TO_LE
:
4144 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
4145 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
4146 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
4149 r_type
= elfcpp::R_POWERPC_NONE
;
4150 offset
-= 2 * big_endian
;
4157 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
4158 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
4159 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
4160 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
4162 // First instruction of a local dynamic sequence,
4164 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
4166 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
4167 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
4169 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
4170 const Output_section
* os
= relinfo
->layout
->tls_segment()
4172 gold_assert(os
!= NULL
);
4173 gold_assert(os
->needs_symtab_index());
4174 r_sym
= os
->symtab_index();
4175 addend
= dtp_offset
;
4179 r_type
= elfcpp::R_POWERPC_NONE
;
4180 offset
-= 2 * big_endian
;
4184 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
4185 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
4186 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
4187 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
4189 // First instruction of initial exec sequence.
4190 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
4191 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
4193 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
4194 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
4195 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
4198 r_type
= elfcpp::R_POWERPC_NONE
;
4199 offset
-= 2 * big_endian
;
4203 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
4204 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
4206 // Second instruction of a global dynamic sequence,
4207 // the __tls_get_addr call
4208 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
4209 switch (this->optimize_tls_gd(final
))
4211 case tls::TLSOPT_TO_IE
:
4212 r_type
= elfcpp::R_POWERPC_NONE
;
4215 case tls::TLSOPT_TO_LE
:
4216 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
4217 offset
+= 2 * big_endian
;
4224 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
4225 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
4227 // Second instruction of a local dynamic sequence,
4228 // the __tls_get_addr call
4229 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
4231 const Output_section
* os
= relinfo
->layout
->tls_segment()
4233 gold_assert(os
!= NULL
);
4234 gold_assert(os
->needs_symtab_index());
4235 r_sym
= os
->symtab_index();
4236 addend
= dtp_offset
;
4237 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
4238 offset
+= 2 * big_endian
;
4242 else if (r_type
== elfcpp::R_POWERPC_TLS
)
4244 // Second instruction of an initial exec sequence
4245 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
4246 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
4248 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
4249 offset
+= 2 * big_endian
;
4254 reloc_write
.put_r_offset(offset
);
4255 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
4256 reloc_write
.put_r_addend(addend
);
4258 pwrite
+= reloc_size
;
4261 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
4262 == reloc_view_size
);
4265 // Return the value to use for a dynamic which requires special
4266 // treatment. This is how we support equality comparisons of function
4267 // pointers across shared library boundaries, as described in the
4268 // processor specific ABI supplement.
4270 template<int size
, bool big_endian
>
4272 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
4276 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
4277 return this->plt_section()->address() + gsym
->plt_offset();
4283 // The selector for powerpc object files.
4285 template<int size
, bool big_endian
>
4286 class Target_selector_powerpc
: public Target_selector
4289 Target_selector_powerpc()
4290 : Target_selector(elfcpp::EM_NONE
, size
, big_endian
,
4292 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
4293 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
4295 ? (big_endian
? "elf64ppc" : "elf64lppc")
4296 : (big_endian
? "elf32ppc" : "elf32lppc")))
4300 do_recognize(Input_file
*, off_t
, int machine
, int, int)
4305 if (machine
!= elfcpp::EM_PPC64
)
4310 if (machine
!= elfcpp::EM_PPC
)
4318 return this->instantiate_target();
4322 do_instantiate_target()
4323 { return new Target_powerpc
<size
, big_endian
>(); }
4326 Target_selector_powerpc
<32, true> target_selector_ppc32
;
4327 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
4328 Target_selector_powerpc
<64, true> target_selector_ppc64
;
4329 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
4331 } // End anonymous namespace.