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.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
48 template<int size
, bool big_endian
>
49 class Output_data_plt_powerpc
;
51 template<int size
, bool big_endian
>
52 class Output_data_brlt_powerpc
;
54 template<int size
, bool big_endian
>
55 class Output_data_got_powerpc
;
57 template<int size
, bool big_endian
>
58 class Output_data_glink
;
60 template<int size
, bool big_endian
>
63 template<int size
, bool big_endian
>
64 class Powerpc_relobj
: public Sized_relobj_file
<size
, big_endian
>
67 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
68 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_refs
;
69 typedef Unordered_map
<Address
, Section_refs
> Access_from
;
71 Powerpc_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
72 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
73 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
74 special_(0), opd_valid_(false), opd_ent_(), access_from_map_()
80 // The .got2 section shndx.
85 return this->special_
;
90 // The .opd section shndx.
97 return this->special_
;
100 // Init OPD entry arrays.
102 init_opd(size_t opd_size
)
104 size_t count
= this->opd_ent_ndx(opd_size
);
105 this->opd_ent_
.resize(count
);
108 // Return section and offset of function entry for .opd + R_OFF.
110 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
112 size_t ndx
= this->opd_ent_ndx(r_off
);
113 gold_assert(ndx
< this->opd_ent_
.size());
114 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
116 *value
= this->opd_ent_
[ndx
].off
;
117 return this->opd_ent_
[ndx
].shndx
;
120 // Set section and offset of function entry for .opd + R_OFF.
122 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
124 size_t ndx
= this->opd_ent_ndx(r_off
);
125 gold_assert(ndx
< this->opd_ent_
.size());
126 this->opd_ent_
[ndx
].shndx
= shndx
;
127 this->opd_ent_
[ndx
].off
= value
;
130 // Return discard flag for .opd + R_OFF.
132 get_opd_discard(Address r_off
) const
134 size_t ndx
= this->opd_ent_ndx(r_off
);
135 gold_assert(ndx
< this->opd_ent_
.size());
136 return this->opd_ent_
[ndx
].discard
;
139 // Set discard flag for .opd + R_OFF.
141 set_opd_discard(Address r_off
)
143 size_t ndx
= this->opd_ent_ndx(r_off
);
144 gold_assert(ndx
< this->opd_ent_
.size());
145 this->opd_ent_
[ndx
].discard
= true;
150 { return &this->access_from_map_
; }
152 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
153 // section at DST_OFF.
155 add_reference(Object
* src_obj
,
156 unsigned int src_indx
,
157 typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
159 Section_id
src_id(src_obj
, src_indx
);
160 this->access_from_map_
[dst_off
].insert(src_id
);
163 // Add a reference to the code section specified by the .opd entry
166 add_gc_mark(typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
168 size_t ndx
= this->opd_ent_ndx(dst_off
);
169 if (ndx
>= this->opd_ent_
.size())
170 this->opd_ent_
.resize(ndx
+ 1);
171 this->opd_ent_
[ndx
].gc_mark
= true;
175 process_gc_mark(Symbol_table
* symtab
)
177 for (size_t i
= 0; i
< this->opd_ent_
.size(); i
++)
178 if (this->opd_ent_
[i
].gc_mark
)
180 unsigned int shndx
= this->opd_ent_
[i
].shndx
;
181 symtab
->gc()->worklist().push(Section_id(this, shndx
));
187 { return this->opd_valid_
; }
191 { this->opd_valid_
= true; }
193 // Examine .rela.opd to build info about function entry points.
195 scan_opd_relocs(size_t reloc_count
,
196 const unsigned char* prelocs
,
197 const unsigned char* plocal_syms
);
199 // Perform the Sized_relobj_file method, then set up opd info from
202 do_read_relocs(Read_relocs_data
*);
204 // Set up some symbols, then perform Sized_relobj_file method.
205 // Occurs after garbage collection, which is why opd info can't be
208 do_scan_relocs(Symbol_table
*, Layout
*, Read_relocs_data
*);
211 do_find_special_sections(Read_symbols_data
* sd
);
213 // Adjust this local symbol value. Return false if the symbol
214 // should be discarded from the output file.
216 do_adjust_local_symbol(Symbol_value
<size
>* lv
) const
218 if (size
== 64 && this->opd_shndx() != 0)
221 if (lv
->input_shndx(&is_ordinary
) != this->opd_shndx())
223 if (this->get_opd_discard(lv
->input_value()))
229 // Return offset in output GOT section that this object will use
230 // as a TOC pointer. Won't be just a constant with multi-toc support.
232 toc_base_offset() const
236 set_has_14bit_branch(unsigned int shndx
)
238 if (shndx
>= this->has14_
.size())
239 this->has14_
.resize(shndx
+ 1);
240 this->has14_
[shndx
] = true;
244 has_14bit_branch(unsigned int shndx
) const
245 { return shndx
< this->has14_
.size() && this->has14_
[shndx
]; }
248 set_stub_table(unsigned int shndx
, Stub_table
<size
, big_endian
>* stub_table
)
250 if (shndx
>= this->stub_table_
.size())
251 this->stub_table_
.resize(shndx
+ 1);
252 this->stub_table_
[shndx
] = stub_table
;
255 Stub_table
<size
, big_endian
>*
256 stub_table(unsigned int shndx
)
258 if (shndx
< this->stub_table_
.size())
259 return this->stub_table_
[shndx
];
272 // Return index into opd_ent_ array for .opd entry at OFF.
273 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
274 // apart when the language doesn't use the last 8-byte word, the
275 // environment pointer. Thus dividing the entry section offset by
276 // 16 will give an index into opd_ent_ that works for either layout
277 // of .opd. (It leaves some elements of the vector unused when .opd
278 // entries are spaced 24 bytes apart, but we don't know the spacing
279 // until relocations are processed, and in any case it is possible
280 // for an object to have some entries spaced 16 bytes apart and
281 // others 24 bytes apart.)
283 opd_ent_ndx(size_t off
) const
286 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
287 unsigned int special_
;
289 // Set at the start of gc_process_relocs, when we know opd_ent_
290 // vector is valid. The flag could be made atomic and set in
291 // do_read_relocs with memory_order_release and then tested with
292 // memory_order_acquire, potentially resulting in fewer entries in
296 // The first 8-byte word of an OPD entry gives the address of the
297 // entry point of the function. Relocatable object files have a
298 // relocation on this word. The following vector records the
299 // section and offset specified by these relocations.
300 std::vector
<Opd_ent
> opd_ent_
;
302 // References made to this object's .opd section when running
303 // gc_process_relocs for another object, before the opd_ent_ vector
304 // is valid for this object.
305 Access_from access_from_map_
;
307 // Whether input section has a 14-bit branch reloc.
308 std::vector
<bool> has14_
;
310 // The stub table to use for a given input section.
311 std::vector
<Stub_table
<size
, big_endian
>*> stub_table_
;
314 template<int size
, bool big_endian
>
315 class Target_powerpc
: public Sized_target
<size
, big_endian
>
319 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
320 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
321 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Signed_address
;
322 static const Address invalid_address
= static_cast<Address
>(0) - 1;
323 // Offset of tp and dtp pointers from start of TLS block.
324 static const Address tp_offset
= 0x7000;
325 static const Address dtp_offset
= 0x8000;
328 : Sized_target
<size
, big_endian
>(&powerpc_info
),
329 got_(NULL
), plt_(NULL
), iplt_(NULL
), brlt_section_(NULL
),
330 glink_(NULL
), rela_dyn_(NULL
), copy_relocs_(elfcpp::R_POWERPC_COPY
),
331 dynbss_(NULL
), tlsld_got_offset_(-1U),
332 stub_tables_(), branch_lookup_table_(), branch_info_()
336 // Process the relocations to determine unreferenced sections for
337 // garbage collection.
339 gc_process_relocs(Symbol_table
* symtab
,
341 Sized_relobj_file
<size
, big_endian
>* object
,
342 unsigned int data_shndx
,
343 unsigned int sh_type
,
344 const unsigned char* prelocs
,
346 Output_section
* output_section
,
347 bool needs_special_offset_handling
,
348 size_t local_symbol_count
,
349 const unsigned char* plocal_symbols
);
351 // Scan the relocations to look for symbol adjustments.
353 scan_relocs(Symbol_table
* symtab
,
355 Sized_relobj_file
<size
, big_endian
>* object
,
356 unsigned int data_shndx
,
357 unsigned int sh_type
,
358 const unsigned char* prelocs
,
360 Output_section
* output_section
,
361 bool needs_special_offset_handling
,
362 size_t local_symbol_count
,
363 const unsigned char* plocal_symbols
);
365 // Map input .toc section to output .got section.
367 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
369 if (size
== 64 && strcmp(name
, ".toc") == 0)
377 // Provide linker defined save/restore functions.
379 define_save_restore_funcs(Layout
*, Symbol_table
*);
381 // No stubs unless a final link.
384 { return !parameters
->options().relocatable(); }
387 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
389 // Stash info about branches, for stub generation.
391 push_branch(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
392 unsigned int data_shndx
, Address r_offset
,
393 unsigned int r_type
, unsigned int r_sym
, Address addend
)
395 Branch_info
info(ppc_object
, data_shndx
, r_offset
, r_type
, r_sym
, addend
);
396 this->branch_info_
.push_back(info
);
397 if (r_type
== elfcpp::R_POWERPC_REL14
398 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
399 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
400 ppc_object
->set_has_14bit_branch(data_shndx
);
403 Stub_table
<size
, big_endian
>*
406 // Finalize the sections.
408 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
410 // Return the value to use for a dynamic which requires special
413 do_dynsym_value(const Symbol
*) const;
415 // Return the PLT address to use for a local symbol.
417 do_plt_address_for_local(const Relobj
*, unsigned int) const;
419 // Return the PLT address to use for a global symbol.
421 do_plt_address_for_global(const Symbol
*) const;
423 // Return the offset to use for the GOT_INDX'th got entry which is
424 // for a local tls symbol specified by OBJECT, SYMNDX.
426 do_tls_offset_for_local(const Relobj
* object
,
428 unsigned int got_indx
) const;
430 // Return the offset to use for the GOT_INDX'th got entry which is
431 // for global tls symbol GSYM.
433 do_tls_offset_for_global(Symbol
* gsym
, unsigned int got_indx
) const;
435 // Relocate a section.
437 relocate_section(const Relocate_info
<size
, big_endian
>*,
438 unsigned int sh_type
,
439 const unsigned char* prelocs
,
441 Output_section
* output_section
,
442 bool needs_special_offset_handling
,
444 Address view_address
,
445 section_size_type view_size
,
446 const Reloc_symbol_changes
*);
448 // Scan the relocs during a relocatable link.
450 scan_relocatable_relocs(Symbol_table
* symtab
,
452 Sized_relobj_file
<size
, big_endian
>* object
,
453 unsigned int data_shndx
,
454 unsigned int sh_type
,
455 const unsigned char* prelocs
,
457 Output_section
* output_section
,
458 bool needs_special_offset_handling
,
459 size_t local_symbol_count
,
460 const unsigned char* plocal_symbols
,
461 Relocatable_relocs
*);
463 // Emit relocations for a section.
465 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
466 unsigned int sh_type
,
467 const unsigned char* prelocs
,
469 Output_section
* output_section
,
470 typename
elfcpp::Elf_types
<size
>::Elf_Off
471 offset_in_output_section
,
472 const Relocatable_relocs
*,
474 Address view_address
,
476 unsigned char* reloc_view
,
477 section_size_type reloc_view_size
);
479 // Return whether SYM is defined by the ABI.
481 do_is_defined_by_abi(const Symbol
* sym
) const
483 return strcmp(sym
->name(), "__tls_get_addr") == 0;
486 // Return the size of the GOT section.
490 gold_assert(this->got_
!= NULL
);
491 return this->got_
->data_size();
494 // Get the PLT section.
495 const Output_data_plt_powerpc
<size
, big_endian
>*
498 gold_assert(this->plt_
!= NULL
);
502 // Get the IPLT section.
503 const Output_data_plt_powerpc
<size
, big_endian
>*
506 gold_assert(this->iplt_
!= NULL
);
510 // Get the .glink section.
511 const Output_data_glink
<size
, big_endian
>*
512 glink_section() const
514 gold_assert(this->glink_
!= NULL
);
518 // Get the GOT section.
519 const Output_data_got_powerpc
<size
, big_endian
>*
522 gold_assert(this->got_
!= NULL
);
526 // Get the GOT section, creating it if necessary.
527 Output_data_got_powerpc
<size
, big_endian
>*
528 got_section(Symbol_table
*, Layout
*);
531 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
532 const elfcpp::Ehdr
<size
, big_endian
>&);
534 // Return the number of entries in the GOT.
536 got_entry_count() const
538 if (this->got_
== NULL
)
540 return this->got_size() / (size
/ 8);
543 // Return the number of entries in the PLT.
545 plt_entry_count() const;
547 // Return the offset of the first non-reserved PLT entry.
549 first_plt_entry_offset() const;
551 // Return the size of each PLT entry.
553 plt_entry_size() const;
555 // Add any special sections for this symbol to the gc work list.
556 // For powerpc64, this adds the code section of a function
559 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
561 // Handle target specific gc actions when adding a gc reference from
562 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
563 // and DST_OFF. For powerpc64, this adds a referenc to the code
564 // section of a function descriptor.
566 do_gc_add_reference(Symbol_table
* symtab
,
568 unsigned int src_shndx
,
570 unsigned int dst_shndx
,
571 Address dst_off
) const;
573 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
576 { return this->stub_tables_
; }
578 const Output_data_brlt_powerpc
<size
, big_endian
>*
580 { return this->brlt_section_
; }
583 add_branch_lookup_table(Address to
)
585 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
586 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
590 find_branch_lookup_table(Address to
)
592 typename
Branch_lookup_table::const_iterator p
593 = this->branch_lookup_table_
.find(to
);
594 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
598 write_branch_lookup_table(unsigned char *oview
)
600 for (typename
Branch_lookup_table::const_iterator p
601 = this->branch_lookup_table_
.begin();
602 p
!= this->branch_lookup_table_
.end();
605 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
611 // The class which scans relocations.
615 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
618 : issued_non_pic_error_(false)
622 get_reference_flags(unsigned int r_type
);
625 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
626 Sized_relobj_file
<size
, big_endian
>* object
,
627 unsigned int data_shndx
,
628 Output_section
* output_section
,
629 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
630 const elfcpp::Sym
<size
, big_endian
>& lsym
,
634 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
635 Sized_relobj_file
<size
, big_endian
>* object
,
636 unsigned int data_shndx
,
637 Output_section
* output_section
,
638 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
642 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
644 Sized_relobj_file
<size
, big_endian
>* ,
647 const elfcpp::Rela
<size
, big_endian
>& ,
649 const elfcpp::Sym
<size
, big_endian
>&)
653 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
655 Sized_relobj_file
<size
, big_endian
>* ,
658 const elfcpp::Rela
<size
,
660 unsigned int , Symbol
*)
665 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
666 unsigned int r_type
);
669 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
670 unsigned int r_type
, Symbol
*);
673 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
674 Target_powerpc
* target
);
677 check_non_pic(Relobj
*, unsigned int r_type
);
680 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, big_endian
>* object
,
681 unsigned int r_type
);
683 // Whether we have issued an error about a non-PIC compilation.
684 bool issued_non_pic_error_
;
688 symval_for_branch(Address value
, const Sized_symbol
<size
>* gsym
,
689 Powerpc_relobj
<size
, big_endian
>* object
,
690 unsigned int *dest_shndx
);
692 // The class which implements relocation.
696 // Use 'at' branch hints when true, 'y' when false.
697 // FIXME maybe: set this with an option.
698 static const bool is_isa_v2
= true;
702 CALL_NOT_EXPECTED
= 0,
708 : call_tls_get_addr_(CALL_NOT_EXPECTED
)
713 if (this->call_tls_get_addr_
!= CALL_NOT_EXPECTED
)
715 // FIXME: This needs to specify the location somehow.
716 gold_error(_("missing expected __tls_get_addr call"));
720 // Do a relocation. Return false if the caller should not issue
721 // any warnings about this relocation.
723 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
724 Output_section
*, size_t relnum
,
725 const elfcpp::Rela
<size
, big_endian
>&,
726 unsigned int r_type
, const Sized_symbol
<size
>*,
727 const Symbol_value
<size
>*,
729 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
732 // This is set if we should skip the next reloc, which should be a
733 // call to __tls_get_addr.
734 enum skip_tls call_tls_get_addr_
;
737 class Relocate_comdat_behavior
740 // Decide what the linker should do for relocations that refer to
741 // discarded comdat sections.
742 inline Comdat_behavior
743 get(const char* name
)
745 gold::Default_comdat_behavior default_behavior
;
746 Comdat_behavior ret
= default_behavior
.get(name
);
747 if (ret
== CB_WARNING
)
750 && (strcmp(name
, ".fixup") == 0
751 || strcmp(name
, ".got2") == 0))
754 && (strcmp(name
, ".opd") == 0
755 || strcmp(name
, ".toc") == 0
756 || strcmp(name
, ".toc1") == 0))
763 // A class which returns the size required for a relocation type,
764 // used while scanning relocs during a relocatable link.
765 class Relocatable_size_for_reloc
769 get_size_for_reloc(unsigned int, Relobj
*)
776 // Optimize the TLS relocation type based on what we know about the
777 // symbol. IS_FINAL is true if the final address of this symbol is
778 // known at link time.
780 tls::Tls_optimization
781 optimize_tls_gd(bool is_final
)
783 // If we are generating a shared library, then we can't do anything
785 if (parameters
->options().shared())
786 return tls::TLSOPT_NONE
;
789 return tls::TLSOPT_TO_IE
;
790 return tls::TLSOPT_TO_LE
;
793 tls::Tls_optimization
796 if (parameters
->options().shared())
797 return tls::TLSOPT_NONE
;
799 return tls::TLSOPT_TO_LE
;
802 tls::Tls_optimization
803 optimize_tls_ie(bool is_final
)
805 if (!is_final
|| parameters
->options().shared())
806 return tls::TLSOPT_NONE
;
808 return tls::TLSOPT_TO_LE
;
813 make_glink_section(Layout
*);
815 // Create the PLT section.
817 make_plt_section(Symbol_table
*, Layout
*);
820 make_iplt_section(Symbol_table
*, Layout
*);
823 make_brlt_section(Layout
*);
825 // Create a PLT entry for a global symbol.
827 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
829 // Create a PLT entry for a local IFUNC symbol.
831 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
832 Sized_relobj_file
<size
, big_endian
>*,
836 // Create a GOT entry for local dynamic __tls_get_addr.
838 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
839 Sized_relobj_file
<size
, big_endian
>* object
);
842 tlsld_got_offset() const
844 return this->tlsld_got_offset_
;
847 // Get the dynamic reloc section, creating it if necessary.
849 rela_dyn_section(Layout
*);
851 // Copy a relocation against a global symbol.
853 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
854 Sized_relobj_file
<size
, big_endian
>* object
,
855 unsigned int shndx
, Output_section
* output_section
,
856 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
858 this->copy_relocs_
.copy_reloc(symtab
, layout
,
859 symtab
->get_sized_symbol
<size
>(sym
),
860 object
, shndx
, output_section
,
861 reloc
, this->rela_dyn_section(layout
));
864 // Look over all the input sections, deciding where to place stub.
866 group_sections(Layout
*, const Task
*);
868 // Sort output sections by address.
872 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
873 { return sec1
->address() < sec2
->address(); }
879 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
880 unsigned int data_shndx
,
885 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
886 r_type_(r_type
), r_sym_(r_sym
), addend_(addend
)
892 // If this branch needs a plt call stub, or a long branch stub, make one.
894 make_stub(Stub_table
<size
, big_endian
>*,
895 Stub_table
<size
, big_endian
>*,
896 Symbol_table
*) const;
899 // The branch location..
900 Powerpc_relobj
<size
, big_endian
>* object_
;
903 // ..and the branch type and destination.
904 unsigned int r_type_
;
909 // Information about this specific target which we pass to the
910 // general Target structure.
911 static Target::Target_info powerpc_info
;
913 // The types of GOT entries needed for this platform.
914 // These values are exposed to the ABI in an incremental link.
915 // Do not renumber existing values without changing the version
916 // number of the .gnu_incremental_inputs section.
920 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
921 GOT_TYPE_DTPREL
, // entry for @got@dtprel
922 GOT_TYPE_TPREL
// entry for @got@tprel
926 Output_data_got_powerpc
<size
, big_endian
>* got_
;
928 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
930 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
931 // Section holding long branch destinations.
932 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
933 // The .glink section.
934 Output_data_glink
<size
, big_endian
>* glink_
;
935 // The dynamic reloc section.
936 Reloc_section
* rela_dyn_
;
937 // Relocs saved to avoid a COPY reloc.
938 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
939 // Space for variables copied with a COPY reloc.
940 Output_data_space
* dynbss_
;
941 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
942 unsigned int tlsld_got_offset_
;
944 Stub_tables stub_tables_
;
945 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
946 Branch_lookup_table branch_lookup_table_
;
948 typedef std::vector
<Branch_info
> Branches
;
949 Branches branch_info_
;
953 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
956 true, // is_big_endian
957 elfcpp::EM_PPC
, // machine_code
958 false, // has_make_symbol
959 false, // has_resolve
960 false, // has_code_fill
961 true, // is_default_stack_executable
962 false, // can_icf_inline_merge_sections
964 "/usr/lib/ld.so.1", // dynamic_linker
965 0x10000000, // default_text_segment_address
966 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
967 4 * 1024, // common_pagesize (overridable by -z common-page-size)
968 false, // isolate_execinstr
970 elfcpp::SHN_UNDEF
, // small_common_shndx
971 elfcpp::SHN_UNDEF
, // large_common_shndx
972 0, // small_common_section_flags
973 0, // large_common_section_flags
974 NULL
, // attributes_section
975 NULL
// attributes_vendor
979 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
982 false, // is_big_endian
983 elfcpp::EM_PPC
, // machine_code
984 false, // has_make_symbol
985 false, // has_resolve
986 false, // has_code_fill
987 true, // is_default_stack_executable
988 false, // can_icf_inline_merge_sections
990 "/usr/lib/ld.so.1", // dynamic_linker
991 0x10000000, // default_text_segment_address
992 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
993 4 * 1024, // common_pagesize (overridable by -z common-page-size)
994 false, // isolate_execinstr
996 elfcpp::SHN_UNDEF
, // small_common_shndx
997 elfcpp::SHN_UNDEF
, // large_common_shndx
998 0, // small_common_section_flags
999 0, // large_common_section_flags
1000 NULL
, // attributes_section
1001 NULL
// attributes_vendor
1005 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1008 true, // is_big_endian
1009 elfcpp::EM_PPC64
, // machine_code
1010 false, // has_make_symbol
1011 false, // has_resolve
1012 false, // has_code_fill
1013 true, // is_default_stack_executable
1014 false, // can_icf_inline_merge_sections
1016 "/usr/lib/ld.so.1", // dynamic_linker
1017 0x10000000, // default_text_segment_address
1018 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1019 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1020 false, // isolate_execinstr
1022 elfcpp::SHN_UNDEF
, // small_common_shndx
1023 elfcpp::SHN_UNDEF
, // large_common_shndx
1024 0, // small_common_section_flags
1025 0, // large_common_section_flags
1026 NULL
, // attributes_section
1027 NULL
// attributes_vendor
1031 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1034 false, // is_big_endian
1035 elfcpp::EM_PPC64
, // machine_code
1036 false, // has_make_symbol
1037 false, // has_resolve
1038 false, // has_code_fill
1039 true, // is_default_stack_executable
1040 false, // can_icf_inline_merge_sections
1042 "/usr/lib/ld.so.1", // dynamic_linker
1043 0x10000000, // default_text_segment_address
1044 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1045 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1046 false, // isolate_execinstr
1048 elfcpp::SHN_UNDEF
, // small_common_shndx
1049 elfcpp::SHN_UNDEF
, // large_common_shndx
1050 0, // small_common_section_flags
1051 0, // large_common_section_flags
1052 NULL
, // attributes_section
1053 NULL
// attributes_vendor
1057 is_branch_reloc(unsigned int r_type
)
1059 return (r_type
== elfcpp::R_POWERPC_REL24
1060 || r_type
== elfcpp::R_PPC_PLTREL24
1061 || r_type
== elfcpp::R_PPC_LOCAL24PC
1062 || r_type
== elfcpp::R_POWERPC_REL14
1063 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1064 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1065 || r_type
== elfcpp::R_POWERPC_ADDR24
1066 || r_type
== elfcpp::R_POWERPC_ADDR14
1067 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1068 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1071 // If INSN is an opcode that may be used with an @tls operand, return
1072 // the transformed insn for TLS optimisation, otherwise return 0. If
1073 // REG is non-zero only match an insn with RB or RA equal to REG.
1075 at_tls_transform(uint32_t insn
, unsigned int reg
)
1077 if ((insn
& (0x3f << 26)) != 31 << 26)
1081 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1082 rtra
= insn
& ((1 << 26) - (1 << 16));
1083 else if (((insn
>> 16) & 0x1f) == reg
)
1084 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1088 if ((insn
& (0x3ff << 1)) == 266 << 1)
1091 else if ((insn
& (0x1f << 1)) == 23 << 1
1092 && ((insn
& (0x1f << 6)) < 14 << 6
1093 || ((insn
& (0x1f << 6)) >= 16 << 6
1094 && (insn
& (0x1f << 6)) < 24 << 6)))
1095 // load and store indexed -> dform
1096 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1097 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1098 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1099 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1100 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1102 insn
= (58 << 26) | 2;
1109 // Modified version of symtab.h class Symbol member
1110 // Given a direct absolute or pc-relative static relocation against
1111 // the global symbol, this function returns whether a dynamic relocation
1116 needs_dynamic_reloc(const Symbol
* gsym
, int flags
)
1118 // No dynamic relocations in a static link!
1119 if (parameters
->doing_static_link())
1122 // A reference to an undefined symbol from an executable should be
1123 // statically resolved to 0, and does not need a dynamic relocation.
1124 // This matches gnu ld behavior.
1125 if (gsym
->is_undefined() && !parameters
->options().shared())
1128 // A reference to an absolute symbol does not need a dynamic relocation.
1129 if (gsym
->is_absolute())
1132 // An absolute reference within a position-independent output file
1133 // will need a dynamic relocation.
1134 if ((flags
& Symbol::ABSOLUTE_REF
)
1135 && parameters
->options().output_is_position_independent())
1138 // A function call that can branch to a local PLT entry does not need
1139 // a dynamic relocation.
1140 if ((flags
& Symbol::FUNCTION_CALL
) && gsym
->has_plt_offset())
1143 // A reference to any PLT entry in a non-position-independent executable
1144 // does not need a dynamic relocation.
1145 // Except due to having function descriptors on powerpc64 we don't define
1146 // functions to their plt code in an executable, so this doesn't apply.
1148 && !parameters
->options().output_is_position_independent()
1149 && gsym
->has_plt_offset())
1152 // A reference to a symbol defined in a dynamic object or to a
1153 // symbol that is preemptible will need a dynamic relocation.
1154 if (gsym
->is_from_dynobj()
1155 || gsym
->is_undefined()
1156 || gsym
->is_preemptible())
1159 // For all other cases, return FALSE.
1163 // Modified version of symtab.h class Symbol member
1164 // Whether we should use the PLT offset associated with a symbol for
1165 // a relocation. FLAGS is a set of Reference_flags.
1169 use_plt_offset(const Symbol
* gsym
, int flags
)
1171 // If the symbol doesn't have a PLT offset, then naturally we
1172 // don't want to use it.
1173 if (!gsym
->has_plt_offset())
1176 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
1177 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1180 // If we are going to generate a dynamic relocation, then we will
1181 // wind up using that, so no need to use the PLT entry.
1182 if (needs_dynamic_reloc
<size
>(gsym
, flags
))
1185 // If the symbol is from a dynamic object, we need to use the PLT
1187 if (gsym
->is_from_dynobj())
1190 // If we are generating a shared object, and gsym symbol is
1191 // undefined or preemptible, we need to use the PLT entry.
1192 if (parameters
->options().shared()
1193 && (gsym
->is_undefined() || gsym
->is_preemptible()))
1196 // If gsym is a call to a weak undefined symbol, we need to use
1197 // the PLT entry; the symbol may be defined by a library loaded
1199 if ((flags
& Symbol::FUNCTION_CALL
) && gsym
->is_weak_undefined())
1202 // Otherwise we can use the regular definition.
1206 template<int size
, bool big_endian
>
1207 class Powerpc_relocate_functions
1224 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1225 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1227 template<int valsize
>
1229 has_overflow_signed(Address value
)
1231 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1232 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1233 limit
<<= ((valsize
- 1) >> 1);
1234 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1235 return value
+ limit
> (limit
<< 1) - 1;
1238 template<int valsize
>
1240 has_overflow_bitfield(Address value
)
1242 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1243 limit
<<= ((valsize
- 1) >> 1);
1244 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1245 return value
> (limit
<< 1) - 1 && value
+ limit
> (limit
<< 1) - 1;
1248 template<int valsize
>
1249 static inline Status
1250 overflowed(Address value
, Overflow_check overflow
)
1252 if (overflow
== CHECK_SIGNED
)
1254 if (has_overflow_signed
<valsize
>(value
))
1255 return STATUS_OVERFLOW
;
1257 else if (overflow
== CHECK_BITFIELD
)
1259 if (has_overflow_bitfield
<valsize
>(value
))
1260 return STATUS_OVERFLOW
;
1265 // Do a simple RELA relocation
1266 template<int valsize
>
1267 static inline Status
1268 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1270 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1271 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1272 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
);
1273 return overflowed
<valsize
>(value
, overflow
);
1276 template<int valsize
>
1277 static inline Status
1278 rela(unsigned char* view
,
1279 unsigned int right_shift
,
1280 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
1282 Overflow_check overflow
)
1284 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1285 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1286 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
1287 Valtype reloc
= value
>> right_shift
;
1290 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
1291 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1294 // Do a simple RELA relocation, unaligned.
1295 template<int valsize
>
1296 static inline Status
1297 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1299 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
, value
);
1300 return overflowed
<valsize
>(value
, overflow
);
1303 template<int valsize
>
1304 static inline Status
1305 rela_ua(unsigned char* view
,
1306 unsigned int right_shift
,
1307 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
1309 Overflow_check overflow
)
1311 typedef typename
elfcpp::Swap_unaligned
<valsize
, big_endian
>::Valtype
1313 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(view
);
1314 Valtype reloc
= value
>> right_shift
;
1317 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
, val
| reloc
);
1318 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1322 // R_PPC64_ADDR64: (Symbol + Addend)
1324 addr64(unsigned char* view
, Address value
)
1325 { This::template rela
<64>(view
, value
, CHECK_NONE
); }
1327 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1329 addr64_u(unsigned char* view
, Address value
)
1330 { This::template rela_ua
<64>(view
, value
, CHECK_NONE
); }
1332 // R_POWERPC_ADDR32: (Symbol + Addend)
1333 static inline Status
1334 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
1335 { return This::template rela
<32>(view
, value
, overflow
); }
1337 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1338 static inline Status
1339 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1340 { return This::template rela_ua
<32>(view
, value
, overflow
); }
1342 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1343 static inline Status
1344 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
1346 Status stat
= This::template rela
<32>(view
, 0, 0x03fffffc, value
, overflow
);
1347 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1348 stat
= STATUS_OVERFLOW
;
1352 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1353 static inline Status
1354 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
1355 { return This::template rela
<16>(view
, value
, overflow
); }
1357 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1358 static inline Status
1359 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1360 { return This::template rela_ua
<16>(view
, value
, overflow
); }
1362 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1363 static inline Status
1364 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
1366 Status stat
= This::template rela
<16>(view
, 0, 0xfffc, value
, overflow
);
1367 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1368 stat
= STATUS_OVERFLOW
;
1372 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1374 addr16_hi(unsigned char* view
, Address value
)
1375 { This::template rela
<16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
1377 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1379 addr16_ha(unsigned char* view
, Address value
)
1380 { This::addr16_hi(view
, value
+ 0x8000); }
1382 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1384 addr16_hi2(unsigned char* view
, Address value
)
1385 { This::template rela
<16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
1387 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1389 addr16_ha2(unsigned char* view
, Address value
)
1390 { This::addr16_hi2(view
, value
+ 0x8000); }
1392 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1394 addr16_hi3(unsigned char* view
, Address value
)
1395 { This::template rela
<16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
1397 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1399 addr16_ha3(unsigned char* view
, Address value
)
1400 { This::addr16_hi3(view
, value
+ 0x8000); }
1402 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1403 static inline Status
1404 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
1406 Status stat
= This::template rela
<32>(view
, 0, 0xfffc, value
, overflow
);
1407 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1408 stat
= STATUS_OVERFLOW
;
1413 // Stash away the index of .got2 or .opd in a relocatable object, if
1414 // such a section exists.
1416 template<int size
, bool big_endian
>
1418 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
1419 Read_symbols_data
* sd
)
1421 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1422 const unsigned char* namesu
= sd
->section_names
->data();
1423 const char* names
= reinterpret_cast<const char*>(namesu
);
1424 section_size_type names_size
= sd
->section_names_size
;
1425 const unsigned char* s
;
1427 s
= this->find_shdr(pshdrs
, size
== 32 ? ".got2" : ".opd",
1428 names
, names_size
, NULL
);
1431 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
1432 this->special_
= ndx
;
1434 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
1437 // Examine .rela.opd to build info about function entry points.
1439 template<int size
, bool big_endian
>
1441 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
1443 const unsigned char* prelocs
,
1444 const unsigned char* plocal_syms
)
1448 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
1450 const int reloc_size
1451 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
1452 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1453 Address expected_off
= 0;
1454 bool regular
= true;
1455 unsigned int opd_ent_size
= 0;
1457 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
1459 Reltype
reloc(prelocs
);
1460 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
1461 = reloc
.get_r_info();
1462 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
1463 if (r_type
== elfcpp::R_PPC64_ADDR64
)
1465 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1466 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
1469 if (r_sym
< this->local_symbol_count())
1471 typename
elfcpp::Sym
<size
, big_endian
>
1472 lsym(plocal_syms
+ r_sym
* sym_size
);
1473 shndx
= lsym
.get_st_shndx();
1474 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1475 value
= lsym
.get_st_value();
1478 shndx
= this->symbol_section_and_value(r_sym
, &value
,
1480 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
1481 value
+ reloc
.get_r_addend());
1484 expected_off
= reloc
.get_r_offset();
1485 opd_ent_size
= expected_off
;
1487 else if (expected_off
!= reloc
.get_r_offset())
1489 expected_off
+= opd_ent_size
;
1491 else if (r_type
== elfcpp::R_PPC64_TOC
)
1493 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
1498 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1499 this->name().c_str(), r_type
);
1503 if (reloc_count
<= 2)
1504 opd_ent_size
= this->section_size(this->opd_shndx());
1505 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
1509 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1510 this->name().c_str());
1516 template<int size
, bool big_endian
>
1518 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
1520 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
1523 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
1524 p
!= rd
->relocs
.end();
1527 if (p
->data_shndx
== this->opd_shndx())
1529 uint64_t opd_size
= this->section_size(this->opd_shndx());
1530 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
1533 this->init_opd(opd_size
);
1534 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
1535 rd
->local_symbols
->data());
1543 // Set up some symbols, then perform Sized_relobj_file method.
1545 template<int size
, bool big_endian
>
1547 Powerpc_relobj
<size
, big_endian
>::do_scan_relocs(Symbol_table
* symtab
,
1549 Read_relocs_data
* rd
)
1553 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
1554 // undefined when scanning relocs (and thus requires
1555 // non-relative dynamic relocs). The proper value will be
1557 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
1558 if (gotsym
!= NULL
&& gotsym
->is_undefined())
1560 Target_powerpc
<size
, big_endian
>* target
=
1561 static_cast<Target_powerpc
<size
, big_endian
>*>(
1562 parameters
->sized_target
<size
, big_endian
>());
1563 Output_data_got_powerpc
<size
, big_endian
>* got
1564 = target
->got_section(symtab
, layout
);
1565 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1566 Symbol_table::PREDEFINED
,
1570 elfcpp::STV_HIDDEN
, 0,
1574 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
1575 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
1576 if (sdasym
!= NULL
&& sdasym
->is_undefined())
1578 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
1580 = layout
->add_output_section_data(".sdata", 0,
1582 | elfcpp::SHF_WRITE
,
1583 sdata
, ORDER_SMALL_DATA
, false);
1584 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
1585 Symbol_table::PREDEFINED
,
1586 os
, 32768, 0, elfcpp::STT_OBJECT
,
1587 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
1591 Sized_relobj_file
<size
, big_endian
>::do_scan_relocs(symtab
, layout
, rd
);
1594 // Set up PowerPC target specific relobj.
1596 template<int size
, bool big_endian
>
1598 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
1599 const std::string
& name
,
1600 Input_file
* input_file
,
1601 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
1603 int et
= ehdr
.get_e_type();
1604 // ET_EXEC files are valid input for --just-symbols/-R,
1605 // and we treat them as relocatable objects.
1606 if (et
== elfcpp::ET_REL
1607 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
1609 Powerpc_relobj
<size
, big_endian
>* obj
=
1610 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1614 else if (et
== elfcpp::ET_DYN
)
1616 Sized_dynobj
<size
, big_endian
>* obj
=
1617 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1623 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
1628 template<int size
, bool big_endian
>
1629 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
1632 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1633 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1635 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
1636 : Output_data_got
<size
, big_endian
>(),
1637 symtab_(symtab
), layout_(layout
),
1638 header_ent_cnt_(size
== 32 ? 3 : 1),
1639 header_index_(size
== 32 ? 0x2000 : 0)
1644 // Create a new GOT entry and return its offset.
1646 add_got_entry(Got_entry got_entry
)
1648 this->reserve_ent();
1649 return Output_data_got
<size
, big_endian
>::add_got_entry(got_entry
);
1652 // Create a pair of new GOT entries and return the offset of the first.
1654 add_got_entry_pair(Got_entry got_entry_1
, Got_entry got_entry_2
)
1656 this->reserve_ent(2);
1657 return Output_data_got
<size
, big_endian
>::add_got_entry_pair(got_entry_1
,
1662 add_constant_pair(Valtype c1
, Valtype c2
)
1664 this->reserve_ent(2);
1665 unsigned int got_offset
= this->add_constant(c1
);
1666 this->add_constant(c2
);
1670 // Offset of _GLOBAL_OFFSET_TABLE_.
1674 return this->got_offset(this->header_index_
);
1677 // Offset of base used to access the GOT/TOC.
1678 // The got/toc pointer reg will be set to this value.
1680 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
1683 return this->g_o_t();
1685 return (this->output_section()->address()
1686 + object
->toc_base_offset()
1690 // Ensure our GOT has a header.
1692 set_final_data_size()
1694 if (this->header_ent_cnt_
!= 0)
1695 this->make_header();
1696 Output_data_got
<size
, big_endian
>::set_final_data_size();
1699 // First word of GOT header needs some values that are not
1700 // handled by Output_data_got so poke them in here.
1701 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1703 do_write(Output_file
* of
)
1706 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
1707 val
= this->layout_
->dynamic_section()->address();
1709 val
= this->output_section()->address() + 0x8000;
1710 this->replace_constant(this->header_index_
, val
);
1711 Output_data_got
<size
, big_endian
>::do_write(of
);
1716 reserve_ent(unsigned int cnt
= 1)
1718 if (this->header_ent_cnt_
== 0)
1720 if (this->num_entries() + cnt
> this->header_index_
)
1721 this->make_header();
1727 this->header_ent_cnt_
= 0;
1728 this->header_index_
= this->num_entries();
1731 Output_data_got
<size
, big_endian
>::add_constant(0);
1732 Output_data_got
<size
, big_endian
>::add_constant(0);
1733 Output_data_got
<size
, big_endian
>::add_constant(0);
1735 // Define _GLOBAL_OFFSET_TABLE_ at the header
1736 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
1739 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
1740 sym
->set_value(this->g_o_t());
1743 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1744 Symbol_table::PREDEFINED
,
1745 this, this->g_o_t(), 0,
1748 elfcpp::STV_HIDDEN
, 0,
1752 Output_data_got
<size
, big_endian
>::add_constant(0);
1755 // Stashed pointers.
1756 Symbol_table
* symtab_
;
1760 unsigned int header_ent_cnt_
;
1761 // GOT header index.
1762 unsigned int header_index_
;
1765 // Get the GOT section, creating it if necessary.
1767 template<int size
, bool big_endian
>
1768 Output_data_got_powerpc
<size
, big_endian
>*
1769 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
1772 if (this->got_
== NULL
)
1774 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1777 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
1779 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1780 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1781 this->got_
, ORDER_DATA
, false);
1787 // Get the dynamic reloc section, creating it if necessary.
1789 template<int size
, bool big_endian
>
1790 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
1791 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
1793 if (this->rela_dyn_
== NULL
)
1795 gold_assert(layout
!= NULL
);
1796 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1797 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1798 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1799 ORDER_DYNAMIC_RELOCS
, false);
1801 return this->rela_dyn_
;
1807 // Determine the stub group size. The group size is the absolute
1808 // value of the parameter --stub-group-size. If --stub-group-size
1809 // is passed a negative value, we restrict stubs to be always before
1810 // the stubbed branches.
1811 Stub_control(int32_t size
)
1812 : state_(NO_GROUP
), stub_group_size_(abs(size
)),
1813 stub14_group_size_(abs(size
)),
1814 stubs_always_before_branch_(size
< 0), suppress_size_errors_(false),
1815 group_end_addr_(0), owner_(NULL
), output_section_(NULL
)
1817 if (stub_group_size_
== 1)
1820 if (stubs_always_before_branch_
)
1822 stub_group_size_
= 0x1e00000;
1823 stub14_group_size_
= 0x7800;
1827 stub_group_size_
= 0x1c00000;
1828 stub14_group_size_
= 0x7000;
1830 suppress_size_errors_
= true;
1834 // Return true iff input section can be handled by current stub
1837 can_add_to_stub_group(Output_section
* o
,
1838 const Output_section::Input_section
* i
,
1841 const Output_section::Input_section
*
1847 { return output_section_
; }
1853 FINDING_STUB_SECTION
,
1858 uint32_t stub_group_size_
;
1859 uint32_t stub14_group_size_
;
1860 bool stubs_always_before_branch_
;
1861 bool suppress_size_errors_
;
1862 uint64_t group_end_addr_
;
1863 const Output_section::Input_section
* owner_
;
1864 Output_section
* output_section_
;
1867 // Return true iff input section can be handled by current stub/
1871 Stub_control::can_add_to_stub_group(Output_section
* o
,
1872 const Output_section::Input_section
* i
,
1876 = has14
? this->stub14_group_size_
: this->stub_group_size_
;
1877 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
1879 uint64_t start_addr
= o
->address();
1882 // .init and .fini sections are pasted together to form a single
1883 // function. We can't be adding stubs in the middle of the function.
1884 this_size
= o
->data_size();
1887 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
1888 this_size
= i
->data_size();
1890 uint64_t end_addr
= start_addr
+ this_size
;
1891 bool toobig
= this_size
> group_size
;
1893 if (toobig
&& !this->suppress_size_errors_
)
1894 gold_warning(_("%s:%s exceeds group size"),
1895 i
->relobj()->name().c_str(),
1896 i
->relobj()->section_name(i
->shndx()).c_str());
1898 if (this->state_
!= HAS_STUB_SECTION
1899 && (!whole_sec
|| this->output_section_
!= o
))
1902 this->output_section_
= o
;
1905 if (this->state_
== NO_GROUP
)
1907 this->state_
= FINDING_STUB_SECTION
;
1908 this->group_end_addr_
= end_addr
;
1910 else if (this->group_end_addr_
- start_addr
< group_size
)
1912 // Adding this section would make the group larger than GROUP_SIZE.
1913 else if (this->state_
== FINDING_STUB_SECTION
1914 && !this->stubs_always_before_branch_
1917 // But wait, there's more! Input sections up to GROUP_SIZE
1918 // bytes before the stub table can be handled by it too.
1919 this->state_
= HAS_STUB_SECTION
;
1920 this->group_end_addr_
= end_addr
;
1924 this->state_
= NO_GROUP
;
1930 // Look over all the input sections, deciding where to place stubs.
1932 template<int size
, bool big_endian
>
1934 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
1937 Stub_control
stub_control(parameters
->options().stub_group_size());
1939 // Group input sections and insert stub table
1940 Stub_table
<size
, big_endian
>* stub_table
= NULL
;
1941 Layout::Section_list section_list
;
1942 layout
->get_executable_sections(§ion_list
);
1943 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
1944 for (Layout::Section_list::reverse_iterator o
= section_list
.rbegin();
1945 o
!= section_list
.rend();
1948 typedef Output_section::Input_section_list Input_section_list
;
1949 for (Input_section_list::const_reverse_iterator i
1950 = (*o
)->input_sections().rbegin();
1951 i
!= (*o
)->input_sections().rend();
1954 if (i
->is_input_section())
1956 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1957 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
1958 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
1959 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
1961 stub_table
->init(stub_control
.owner(),
1962 stub_control
.output_section());
1965 if (stub_table
== NULL
)
1966 stub_table
= this->new_stub_table();
1967 ppcobj
->set_stub_table(i
->shndx(), stub_table
);
1971 if (stub_table
!= NULL
)
1972 stub_table
->init(stub_control
.owner(), stub_control
.output_section());
1975 // If this branch needs a plt call stub, or a long branch stub, make one.
1977 template<int size
, bool big_endian
>
1979 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
1980 Stub_table
<size
, big_endian
>* stub_table
,
1981 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
1982 Symbol_table
* symtab
) const
1984 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
1985 if (sym
!= NULL
&& sym
->is_forwarder())
1986 sym
= symtab
->resolve_forwards(sym
);
1987 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
1989 ? use_plt_offset
<size
>(gsym
, Scan::get_reference_flags(this->r_type_
))
1990 : this->object_
->local_has_plt_offset(this->r_sym_
))
1992 if (stub_table
== NULL
)
1993 stub_table
= this->object_
->stub_table(this->shndx_
);
1994 if (stub_table
== NULL
)
1996 // This is a ref from a data section to an ifunc symbol.
1997 stub_table
= ifunc_stub_table
;
1999 gold_assert(stub_table
!= NULL
);
2001 stub_table
->add_plt_call_entry(this->object_
, gsym
,
2002 this->r_type_
, this->addend_
);
2004 stub_table
->add_plt_call_entry(this->object_
, this->r_sym_
,
2005 this->r_type_
, this->addend_
);
2009 unsigned int max_branch_offset
;
2010 if (this->r_type_
== elfcpp::R_POWERPC_REL14
2011 || this->r_type_
== elfcpp::R_POWERPC_REL14_BRTAKEN
2012 || this->r_type_
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
2013 max_branch_offset
= 1 << 15;
2014 else if (this->r_type_
== elfcpp::R_POWERPC_REL24
2015 || this->r_type_
== elfcpp::R_PPC_PLTREL24
2016 || this->r_type_
== elfcpp::R_PPC_LOCAL24PC
)
2017 max_branch_offset
= 1 << 25;
2020 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
2021 gold_assert(from
!= invalid_address
);
2022 from
+= (this->object_
->output_section(this->shndx_
)->address()
2027 switch (gsym
->source())
2029 case Symbol::FROM_OBJECT
:
2031 Object
* symobj
= gsym
->object();
2032 if (symobj
->is_dynamic()
2033 || symobj
->pluginobj() != NULL
)
2036 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
2037 if (shndx
== elfcpp::SHN_UNDEF
)
2042 case Symbol::IS_UNDEFINED
:
2048 Symbol_table::Compute_final_value_status status
;
2049 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
2050 if (status
!= Symbol_table::CFVS_OK
)
2055 const Symbol_value
<size
>* psymval
2056 = this->object_
->local_symbol(this->r_sym_
);
2057 Symbol_value
<size
> symval
;
2058 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
2059 typename
ObjType::Compute_final_local_value_status status
2060 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
2062 if (status
!= ObjType::CFLV_OK
2063 || !symval
.has_output_value())
2065 to
= symval
.value(this->object_
, 0);
2067 if (stub_table
== NULL
)
2068 stub_table
= this->object_
->stub_table(this->shndx_
);
2069 gold_assert(stub_table
!= NULL
);
2070 if (size
== 64 && is_branch_reloc(this->r_type_
))
2072 unsigned int dest_shndx
;
2073 to
= stub_table
->targ()->symval_for_branch(to
, gsym
, this->object_
,
2076 Address delta
= to
- from
;
2077 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
2079 stub_table
->add_long_branch_entry(this->object_
, to
);
2084 // Relaxation hook. This is where we do stub generation.
2086 template<int size
, bool big_endian
>
2088 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
2089 const Input_objects
*,
2090 Symbol_table
* symtab
,
2094 unsigned int prev_brlt_size
= 0;
2096 this->group_sections(layout
, task
);
2099 prev_brlt_size
= this->branch_lookup_table_
.size();
2100 this->branch_lookup_table_
.clear();
2101 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2102 p
!= this->stub_tables_
.end();
2105 (*p
)->clear_long_branch_stubs();
2109 // We need address of stub tables valid for make_stub.
2110 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2111 p
!= this->stub_tables_
.end();
2114 const Powerpc_relobj
<size
, big_endian
>* object
2115 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
2116 Address off
= object
->get_output_section_offset((*p
)->shndx());
2117 gold_assert(off
!= invalid_address
);
2118 Output_section
* os
= (*p
)->output_section();
2119 (*p
)->set_address_and_size(os
, off
);
2122 Stub_table
<size
, big_endian
>* ifunc_stub_table
2123 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
2124 Stub_table
<size
, big_endian
>* one_stub_table
2125 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
2126 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
2127 b
!= this->branch_info_
.end();
2130 b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
);
2133 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
2134 bool again
= num_huge_branches
!= prev_brlt_size
;
2135 if (size
== 64 && num_huge_branches
!= 0)
2136 this->make_brlt_section(layout
);
2137 if (size
== 64 && again
)
2138 this->brlt_section_
->set_current_size(num_huge_branches
);
2140 typedef Unordered_set
<Output_section
*> Output_sections
;
2141 Output_sections os_need_update
;
2142 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2143 p
!= this->stub_tables_
.end();
2146 if ((*p
)->size_update())
2149 os_need_update
.insert((*p
)->output_section());
2153 for (typename
Output_sections::iterator p
= os_need_update
.begin();
2154 p
!= os_need_update
.end();
2157 Output_section
* os
= *p
;
2159 typedef Output_section::Input_section_list Input_section_list
;
2160 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
2161 i
!= os
->input_sections().end();
2164 off
= align_address(off
, i
->addralign());
2165 if (i
->is_input_section() || i
->is_relaxed_input_section())
2166 i
->relobj()->set_section_offset(i
->shndx(), off
);
2167 if (i
->is_relaxed_input_section())
2169 Stub_table
<size
, big_endian
>* stub_table
2170 = static_cast<Stub_table
<size
, big_endian
>*>(
2171 i
->relaxed_input_section());
2172 off
+= stub_table
->set_address_and_size(os
, off
);
2175 off
+= i
->data_size();
2177 // If .brlt is part of this output section, then we have just
2178 // done the offset adjustment.
2179 os
->clear_section_offsets_need_adjustment();
2184 && num_huge_branches
!= 0
2185 && parameters
->options().output_is_position_independent())
2187 // Fill in the BRLT relocs.
2188 this->brlt_section_
->reset_data_size();
2189 for (typename
Branch_lookup_table::const_iterator p
2190 = this->branch_lookup_table_
.begin();
2191 p
!= this->branch_lookup_table_
.end();
2194 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
2196 this->brlt_section_
->finalize_data_size();
2201 // A class to handle the PLT data.
2203 template<int size
, bool big_endian
>
2204 class Output_data_plt_powerpc
: public Output_section_data_build
2207 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
2208 size
, big_endian
> Reloc_section
;
2210 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
2211 Reloc_section
* plt_rel
,
2212 unsigned int reserved_size
,
2214 : Output_section_data_build(size
== 32 ? 4 : 8),
2217 initial_plt_entry_size_(reserved_size
),
2221 // Add an entry to the PLT.
2226 add_ifunc_entry(Symbol
*);
2229 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
2231 // Return the .rela.plt section data.
2238 // Return the number of PLT entries.
2242 return ((this->current_data_size() - this->initial_plt_entry_size_
)
2246 // Return the offset of the first non-reserved PLT entry.
2248 first_plt_entry_offset()
2249 { return this->initial_plt_entry_size_
; }
2251 // Return the size of a PLT entry.
2253 get_plt_entry_size()
2254 { return plt_entry_size
; }
2258 do_adjust_output_section(Output_section
* os
)
2263 // Write to a map file.
2265 do_print_to_mapfile(Mapfile
* mapfile
) const
2266 { mapfile
->print_output_data(this, this->name_
); }
2269 // The size of an entry in the PLT.
2270 static const int plt_entry_size
= size
== 32 ? 4 : 24;
2272 // Write out the PLT data.
2274 do_write(Output_file
*);
2276 // The reloc section.
2277 Reloc_section
* rel_
;
2278 // Allows access to .glink for do_write.
2279 Target_powerpc
<size
, big_endian
>* targ_
;
2280 // The size of the first reserved entry.
2281 int initial_plt_entry_size_
;
2282 // What to report in map file.
2286 // Add an entry to the PLT.
2288 template<int size
, bool big_endian
>
2290 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
2292 if (!gsym
->has_plt_offset())
2294 section_size_type off
= this->current_data_size();
2296 off
+= this->first_plt_entry_offset();
2297 gsym
->set_plt_offset(off
);
2298 gsym
->set_needs_dynsym_entry();
2299 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
2300 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
2301 off
+= plt_entry_size
;
2302 this->set_current_data_size(off
);
2306 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
2308 template<int size
, bool big_endian
>
2310 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
2312 if (!gsym
->has_plt_offset())
2314 section_size_type off
= this->current_data_size();
2315 gsym
->set_plt_offset(off
);
2316 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
2318 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
2319 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
2320 off
+= plt_entry_size
;
2321 this->set_current_data_size(off
);
2325 // Add an entry for a local ifunc symbol to the IPLT.
2327 template<int size
, bool big_endian
>
2329 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
2330 Sized_relobj_file
<size
, big_endian
>* relobj
,
2331 unsigned int local_sym_index
)
2333 if (!relobj
->local_has_plt_offset(local_sym_index
))
2335 section_size_type off
= this->current_data_size();
2336 relobj
->set_local_plt_offset(local_sym_index
, off
);
2337 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
2339 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
2340 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
2342 off
+= plt_entry_size
;
2343 this->set_current_data_size(off
);
2347 static const uint32_t add_0_11_11
= 0x7c0b5a14;
2348 static const uint32_t add_3_3_2
= 0x7c631214;
2349 static const uint32_t add_3_3_13
= 0x7c636a14;
2350 static const uint32_t add_11_0_11
= 0x7d605a14;
2351 static const uint32_t add_12_2_11
= 0x7d825a14;
2352 static const uint32_t addi_11_11
= 0x396b0000;
2353 static const uint32_t addi_12_12
= 0x398c0000;
2354 static const uint32_t addi_2_2
= 0x38420000;
2355 static const uint32_t addi_3_2
= 0x38620000;
2356 static const uint32_t addi_3_3
= 0x38630000;
2357 static const uint32_t addis_0_2
= 0x3c020000;
2358 static const uint32_t addis_0_13
= 0x3c0d0000;
2359 static const uint32_t addis_11_11
= 0x3d6b0000;
2360 static const uint32_t addis_11_30
= 0x3d7e0000;
2361 static const uint32_t addis_12_12
= 0x3d8c0000;
2362 static const uint32_t addis_12_2
= 0x3d820000;
2363 static const uint32_t addis_3_2
= 0x3c620000;
2364 static const uint32_t addis_3_13
= 0x3c6d0000;
2365 static const uint32_t b
= 0x48000000;
2366 static const uint32_t bcl_20_31
= 0x429f0005;
2367 static const uint32_t bctr
= 0x4e800420;
2368 static const uint32_t blr
= 0x4e800020;
2369 static const uint32_t blrl
= 0x4e800021;
2370 static const uint32_t cror_15_15_15
= 0x4def7b82;
2371 static const uint32_t cror_31_31_31
= 0x4ffffb82;
2372 static const uint32_t ld_0_1
= 0xe8010000;
2373 static const uint32_t ld_0_12
= 0xe80c0000;
2374 static const uint32_t ld_11_12
= 0xe96c0000;
2375 static const uint32_t ld_11_2
= 0xe9620000;
2376 static const uint32_t ld_2_1
= 0xe8410000;
2377 static const uint32_t ld_2_11
= 0xe84b0000;
2378 static const uint32_t ld_2_12
= 0xe84c0000;
2379 static const uint32_t ld_2_2
= 0xe8420000;
2380 static const uint32_t lfd_0_1
= 0xc8010000;
2381 static const uint32_t li_0_0
= 0x38000000;
2382 static const uint32_t li_12_0
= 0x39800000;
2383 static const uint32_t lis_0_0
= 0x3c000000;
2384 static const uint32_t lis_11
= 0x3d600000;
2385 static const uint32_t lis_12
= 0x3d800000;
2386 static const uint32_t lwz_0_12
= 0x800c0000;
2387 static const uint32_t lwz_11_11
= 0x816b0000;
2388 static const uint32_t lwz_11_30
= 0x817e0000;
2389 static const uint32_t lwz_12_12
= 0x818c0000;
2390 static const uint32_t lwzu_0_12
= 0x840c0000;
2391 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
2392 static const uint32_t mflr_0
= 0x7c0802a6;
2393 static const uint32_t mflr_11
= 0x7d6802a6;
2394 static const uint32_t mflr_12
= 0x7d8802a6;
2395 static const uint32_t mtctr_0
= 0x7c0903a6;
2396 static const uint32_t mtctr_11
= 0x7d6903a6;
2397 static const uint32_t mtctr_12
= 0x7d8903a6;
2398 static const uint32_t mtlr_0
= 0x7c0803a6;
2399 static const uint32_t mtlr_12
= 0x7d8803a6;
2400 static const uint32_t nop
= 0x60000000;
2401 static const uint32_t ori_0_0_0
= 0x60000000;
2402 static const uint32_t std_0_1
= 0xf8010000;
2403 static const uint32_t std_0_12
= 0xf80c0000;
2404 static const uint32_t std_2_1
= 0xf8410000;
2405 static const uint32_t stfd_0_1
= 0xd8010000;
2406 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
2407 static const uint32_t sub_11_11_12
= 0x7d6c5850;
2409 // Write out the PLT.
2411 template<int size
, bool big_endian
>
2413 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
2417 const section_size_type offset
= this->offset();
2418 const section_size_type oview_size
2419 = convert_to_section_size_type(this->data_size());
2420 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2421 unsigned char* pov
= oview
;
2422 unsigned char* endpov
= oview
+ oview_size
;
2424 // The address of the .glink branch table
2425 const Output_data_glink
<size
, big_endian
>* glink
2426 = this->targ_
->glink_section();
2427 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
2429 while (pov
< endpov
)
2431 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
2436 of
->write_output_view(offset
, oview_size
, oview
);
2440 // Create the PLT section.
2442 template<int size
, bool big_endian
>
2444 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
2447 if (this->plt_
== NULL
)
2449 if (this->got_
== NULL
)
2450 this->got_section(symtab
, layout
);
2452 if (this->glink_
== NULL
)
2453 make_glink_section(layout
);
2455 // Ensure that .rela.dyn always appears before .rela.plt This is
2456 // necessary due to how, on PowerPC and some other targets, .rela.dyn
2457 // needs to include .rela.plt in it's range.
2458 this->rela_dyn_section(layout
);
2460 Reloc_section
* plt_rel
= new Reloc_section(false);
2461 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
2462 elfcpp::SHF_ALLOC
, plt_rel
,
2463 ORDER_DYNAMIC_PLT_RELOCS
, false);
2465 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
2466 size
== 32 ? 0 : 24,
2468 layout
->add_output_section_data(".plt",
2470 ? elfcpp::SHT_PROGBITS
2471 : elfcpp::SHT_NOBITS
),
2472 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2481 // Create the IPLT section.
2483 template<int size
, bool big_endian
>
2485 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
2488 if (this->iplt_
== NULL
)
2490 this->make_plt_section(symtab
, layout
);
2492 Reloc_section
* iplt_rel
= new Reloc_section(false);
2493 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
2495 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
2497 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
2501 // A section for huge long branch addresses, similar to plt section.
2503 template<int size
, bool big_endian
>
2504 class Output_data_brlt_powerpc
: public Output_section_data_build
2507 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2508 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
2509 size
, big_endian
> Reloc_section
;
2511 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
2512 Reloc_section
* brlt_rel
)
2513 : Output_section_data_build(size
== 32 ? 4 : 8),
2518 // Add a reloc for an entry in the BRLT.
2520 add_reloc(Address to
, unsigned int off
)
2521 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
2523 // Update section and reloc section size.
2525 set_current_size(unsigned int num_branches
)
2527 this->reset_address_and_file_offset();
2528 this->set_current_data_size(num_branches
* 16);
2529 this->finalize_data_size();
2530 Output_section
* os
= this->output_section();
2531 os
->set_section_offsets_need_adjustment();
2532 if (this->rel_
!= NULL
)
2534 unsigned int reloc_size
2535 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
2536 this->rel_
->reset_address_and_file_offset();
2537 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
2538 this->rel_
->finalize_data_size();
2539 Output_section
* os
= this->rel_
->output_section();
2540 os
->set_section_offsets_need_adjustment();
2546 do_adjust_output_section(Output_section
* os
)
2551 // Write to a map file.
2553 do_print_to_mapfile(Mapfile
* mapfile
) const
2554 { mapfile
->print_output_data(this, "** BRLT"); }
2557 // Write out the BRLT data.
2559 do_write(Output_file
*);
2561 // The reloc section.
2562 Reloc_section
* rel_
;
2563 Target_powerpc
<size
, big_endian
>* targ_
;
2566 // Make the branch lookup table section.
2568 template<int size
, bool big_endian
>
2570 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
2572 if (size
== 64 && this->brlt_section_
== NULL
)
2574 Reloc_section
* brlt_rel
= NULL
;
2575 bool is_pic
= parameters
->options().output_is_position_independent();
2578 // When PIC we can't fill in .brlt (like .plt it can be a
2579 // bss style section) but must initialise at runtime via
2580 // dynamic relocats.
2581 this->rela_dyn_section(layout
);
2582 brlt_rel
= new Reloc_section(false);
2583 this->rela_dyn_
->output_section()->add_output_section_data(brlt_rel
);
2586 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
2587 if (this->plt_
&& is_pic
)
2588 this->plt_
->output_section()
2589 ->add_output_section_data(this->brlt_section_
);
2591 layout
->add_output_section_data(".brlt",
2592 (is_pic
? elfcpp::SHT_NOBITS
2593 : elfcpp::SHT_PROGBITS
),
2594 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2595 this->brlt_section_
,
2596 (is_pic
? ORDER_SMALL_BSS
2597 : ORDER_SMALL_DATA
),
2602 // Write out .brlt when non-PIC.
2604 template<int size
, bool big_endian
>
2606 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
2608 if (size
== 64 && !parameters
->options().output_is_position_independent())
2610 const section_size_type offset
= this->offset();
2611 const section_size_type oview_size
2612 = convert_to_section_size_type(this->data_size());
2613 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2615 this->targ_
->write_branch_lookup_table(oview
);
2616 of
->write_output_view(offset
, oview_size
, oview
);
2620 // Stub_table holds information about plt and long branch stubs.
2621 // Stubs are built in an area following some input section determined
2622 // by group_sections(). This input section is converted to a relaxed
2623 // input section allowing it to be resized to accommodate the stubs
2625 template<int size
, bool big_endian
>
2626 class Stub_table
: public Output_relaxed_input_section
2629 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2630 static const Address invalid_address
= static_cast<Address
>(0) - 1;
2632 Stub_table(Target_powerpc
<size
, big_endian
>* targ
)
2633 : Output_relaxed_input_section(NULL
, 0, 0),
2634 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
2635 orig_data_size_(0), plt_size_(0), branch_size_(0), prev_size_(0)
2638 // Delayed Output_relaxed_input_section init.
2640 init(const Output_section::Input_section
*, Output_section
*);
2642 // Add a plt call stub.
2644 add_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2650 add_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2655 // Find a given plt call stub.
2657 find_plt_call_entry(const Symbol
*) const;
2660 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2661 unsigned int) const;
2664 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2670 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2675 // Add a long branch stub.
2677 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*, Address
);
2680 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*, Address
);
2683 clear_long_branch_stubs()
2685 this->long_branch_stubs_
.clear();
2686 this->branch_size_
= 0;
2690 set_address_and_size(const Output_section
* os
, Address off
)
2692 Address start_off
= off
;
2693 off
+= this->orig_data_size_
;
2694 Address my_size
= this->plt_size_
+ this->branch_size_
;
2696 off
= align_address(off
, this->stub_align());
2697 // Include original section size and alignment padding in size
2698 my_size
+= off
- start_off
;
2699 this->reset_address_and_file_offset();
2700 this->set_current_data_size(my_size
);
2701 this->set_address_and_file_offset(os
->address() + start_off
,
2702 os
->offset() + start_off
);
2709 return align_address(this->address() + this->orig_data_size_
,
2710 this->stub_align());
2716 return align_address(this->offset() + this->orig_data_size_
,
2717 this->stub_align());
2722 { return this->plt_size_
; }
2727 Output_section
* os
= this->output_section();
2728 if (os
->addralign() < this->stub_align())
2730 os
->set_addralign(this->stub_align());
2731 // FIXME: get rid of the insane checkpointing.
2732 // We can't increase alignment of the input section to which
2733 // stubs are attached; The input section may be .init which
2734 // is pasted together with other .init sections to form a
2735 // function. Aligning might insert zero padding resulting in
2736 // sigill. However we do need to increase alignment of the
2737 // output section so that the align_address() on offset in
2738 // set_address_and_size() adds the same padding as the
2739 // align_address() on address in stub_address().
2740 // What's more, we need this alignment for the layout done in
2741 // relaxation_loop_body() so that the output section starts at
2742 // a suitably aligned address.
2743 os
->checkpoint_set_addralign(this->stub_align());
2745 if (this->prev_size_
!= this->plt_size_
+ this->branch_size_
)
2747 this->prev_size_
= this->plt_size_
+ this->branch_size_
;
2755 { return this->prev_size_
; }
2758 set_prev_size(section_size_type val
)
2759 { this->prev_size_
= val
; }
2761 Target_powerpc
<size
, big_endian
>*
2768 { return size
== 32 ? 16 : 32; }
2770 // We keep plt stubs aligned, so no fancy sizing.
2772 plt_call_size() const
2773 { return size
== 32 ? 16 : 32; }
2775 // Return long branch stub size.
2777 branch_stub_size(Address to
)
2779 Address loc
= this->stub_address() + this->plt_size_
+ this->branch_size_
;
2780 if (loc
- to
+ (1 << 25) < 2 << 25)
2782 if (size
== 64 || !parameters
->options().output_is_position_independent())
2789 do_write(Output_file
*);
2791 // Plt call stub keys.
2795 Plt_stub_ent(const Symbol
* sym
)
2796 : sym_(sym
), object_(0), addend_(0), locsym_(0)
2799 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
2800 unsigned int locsym_index
)
2801 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
2804 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
2806 unsigned int r_type
,
2808 : sym_(sym
), object_(0), addend_(0), locsym_(0)
2811 this->addend_
= addend
;
2812 else if (parameters
->options().output_is_position_independent()
2813 && r_type
== elfcpp::R_PPC_PLTREL24
)
2815 this->addend_
= addend
;
2816 if (this->addend_
>= 32768)
2817 this->object_
= object
;
2821 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
2822 unsigned int locsym_index
,
2823 unsigned int r_type
,
2825 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
2828 this->addend_
= addend
;
2829 else if (parameters
->options().output_is_position_independent()
2830 && r_type
== elfcpp::R_PPC_PLTREL24
)
2831 this->addend_
= addend
;
2834 bool operator==(const Plt_stub_ent
& that
) const
2836 return (this->sym_
== that
.sym_
2837 && this->object_
== that
.object_
2838 && this->addend_
== that
.addend_
2839 && this->locsym_
== that
.locsym_
);
2843 const Sized_relobj_file
<size
, big_endian
>* object_
;
2844 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
2845 unsigned int locsym_
;
2848 class Plt_stub_ent_hash
2851 size_t operator()(const Plt_stub_ent
& ent
) const
2853 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
2854 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
2860 // Long branch stub keys.
2861 class Branch_stub_ent
2864 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
, Address to
)
2865 : dest_(to
), toc_base_off_(0)
2868 toc_base_off_
= obj
->toc_base_offset();
2871 bool operator==(const Branch_stub_ent
& that
) const
2873 return (this->dest_
== that
.dest_
2875 || this->toc_base_off_
== that
.toc_base_off_
));
2879 unsigned int toc_base_off_
;
2882 class Branch_stub_ent_hash
2885 size_t operator()(const Branch_stub_ent
& ent
) const
2886 { return ent
.dest_
^ ent
.toc_base_off_
; }
2889 // In a sane world this would be a global.
2890 Target_powerpc
<size
, big_endian
>* targ_
;
2891 // Map sym/object/addend to stub offset.
2892 typedef Unordered_map
<Plt_stub_ent
, unsigned int,
2893 Plt_stub_ent_hash
> Plt_stub_entries
;
2894 Plt_stub_entries plt_call_stubs_
;
2895 // Map destination address to stub offset.
2896 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
2897 Branch_stub_ent_hash
> Branch_stub_entries
;
2898 Branch_stub_entries long_branch_stubs_
;
2899 // size of input section
2900 section_size_type orig_data_size_
;
2902 section_size_type plt_size_
, branch_size_
, prev_size_
;
2905 // Make a new stub table, and record.
2907 template<int size
, bool big_endian
>
2908 Stub_table
<size
, big_endian
>*
2909 Target_powerpc
<size
, big_endian
>::new_stub_table()
2911 Stub_table
<size
, big_endian
>* stub_table
2912 = new Stub_table
<size
, big_endian
>(this);
2913 this->stub_tables_
.push_back(stub_table
);
2917 // Delayed stub table initialisation, because we create the stub table
2918 // before we know to which section it will be attached.
2920 template<int size
, bool big_endian
>
2922 Stub_table
<size
, big_endian
>::init(
2923 const Output_section::Input_section
* owner
,
2924 Output_section
* output_section
)
2926 this->set_relobj(owner
->relobj());
2927 this->set_shndx(owner
->shndx());
2928 this->set_addralign(this->relobj()->section_addralign(this->shndx()));
2929 this->set_output_section(output_section
);
2930 this->orig_data_size_
= owner
->current_data_size();
2932 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
2933 new_relaxed
.push_back(this);
2934 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
2937 // Add a plt call stub, if we do not already have one for this
2938 // sym/object/addend combo.
2940 template<int size
, bool big_endian
>
2942 Stub_table
<size
, big_endian
>::add_plt_call_entry(
2943 const Sized_relobj_file
<size
, big_endian
>* object
,
2945 unsigned int r_type
,
2948 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
2949 Address off
= this->plt_size_
;
2950 if (this->plt_call_stubs_
.insert(std::make_pair(ent
, off
)).second
)
2951 this->plt_size_
= off
+ this->plt_call_size();
2954 template<int size
, bool big_endian
>
2956 Stub_table
<size
, big_endian
>::add_plt_call_entry(
2957 const Sized_relobj_file
<size
, big_endian
>* object
,
2958 unsigned int locsym_index
,
2959 unsigned int r_type
,
2962 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
2963 Address off
= this->plt_size_
;
2964 if (this->plt_call_stubs_
.insert(std::make_pair(ent
, off
)).second
)
2965 this->plt_size_
= off
+ this->plt_call_size();
2968 // Find a plt call stub.
2970 template<int size
, bool big_endian
>
2971 typename
elfcpp::Elf_types
<size
>::Elf_Addr
2972 Stub_table
<size
, big_endian
>::find_plt_call_entry(
2973 const Sized_relobj_file
<size
, big_endian
>* object
,
2975 unsigned int r_type
,
2976 Address addend
) const
2978 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
2979 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
2980 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
2983 template<int size
, bool big_endian
>
2984 typename
elfcpp::Elf_types
<size
>::Elf_Addr
2985 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
2987 Plt_stub_ent
ent(gsym
);
2988 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
2989 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
2992 template<int size
, bool big_endian
>
2993 typename
elfcpp::Elf_types
<size
>::Elf_Addr
2994 Stub_table
<size
, big_endian
>::find_plt_call_entry(
2995 const Sized_relobj_file
<size
, big_endian
>* object
,
2996 unsigned int locsym_index
,
2997 unsigned int r_type
,
2998 Address addend
) const
3000 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
3001 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3002 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3005 template<int size
, bool big_endian
>
3006 typename
elfcpp::Elf_types
<size
>::Elf_Addr
3007 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3008 const Sized_relobj_file
<size
, big_endian
>* object
,
3009 unsigned int locsym_index
) const
3011 Plt_stub_ent
ent(object
, locsym_index
);
3012 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3013 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3016 // Add a long branch stub if we don't already have one to given
3019 template<int size
, bool big_endian
>
3021 Stub_table
<size
, big_endian
>::add_long_branch_entry(
3022 const Powerpc_relobj
<size
, big_endian
>* object
,
3025 Branch_stub_ent
ent(object
, to
);
3026 Address off
= this->branch_size_
;
3027 if (this->long_branch_stubs_
.insert(std::make_pair(ent
, off
)).second
)
3029 unsigned int stub_size
= this->branch_stub_size(to
);
3030 this->branch_size_
= off
+ stub_size
;
3031 if (size
== 64 && stub_size
!= 4)
3032 this->targ_
->add_branch_lookup_table(to
);
3036 // Find long branch stub.
3038 template<int size
, bool big_endian
>
3039 typename
elfcpp::Elf_types
<size
>::Elf_Addr
3040 Stub_table
<size
, big_endian
>::find_long_branch_entry(
3041 const Powerpc_relobj
<size
, big_endian
>* object
,
3044 Branch_stub_ent
ent(object
, to
);
3045 typename
Branch_stub_entries::const_iterator p
3046 = this->long_branch_stubs_
.find(ent
);
3047 return p
== this->long_branch_stubs_
.end() ? invalid_address
: p
->second
;
3050 // A class to handle .glink.
3052 template<int size
, bool big_endian
>
3053 class Output_data_glink
: public Output_section_data
3056 static const int pltresolve_size
= 16*4;
3058 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
3059 : Output_section_data(16), targ_(targ
)
3063 // Write to a map file.
3065 do_print_to_mapfile(Mapfile
* mapfile
) const
3066 { mapfile
->print_output_data(this, _("** glink")); }
3070 set_final_data_size();
3074 do_write(Output_file
*);
3076 // Allows access to .got and .plt for do_write.
3077 Target_powerpc
<size
, big_endian
>* targ_
;
3080 template<int size
, bool big_endian
>
3082 Output_data_glink
<size
, big_endian
>::set_final_data_size()
3084 unsigned int count
= this->targ_
->plt_entry_count();
3085 section_size_type total
= 0;
3091 // space for branch table
3092 total
+= 4 * (count
- 1);
3094 total
+= -total
& 15;
3095 total
+= this->pltresolve_size
;
3099 total
+= this->pltresolve_size
;
3101 // space for branch table
3104 total
+= 4 * (count
- 0x8000);
3108 this->set_data_size(total
);
3111 static inline uint32_t
3117 static inline uint32_t
3123 static inline uint32_t
3126 return hi(a
+ 0x8000);
3129 template<bool big_endian
>
3131 write_insn(unsigned char* p
, uint32_t v
)
3133 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
3136 // Write out plt and long branch stub code.
3138 template<int size
, bool big_endian
>
3140 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
3142 if (this->plt_call_stubs_
.empty()
3143 && this->long_branch_stubs_
.empty())
3146 const section_size_type start_off
= this->offset();
3147 const section_size_type off
= this->stub_offset();
3148 const section_size_type oview_size
=
3149 convert_to_section_size_type(this->data_size() - (off
- start_off
));
3150 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
3153 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3154 static const Address invalid_address
= static_cast<Address
>(0) - 1;
3158 const Output_data_got_powerpc
<size
, big_endian
>* got
3159 = this->targ_
->got_section();
3160 Address got_os_addr
= got
->output_section()->address();
3162 if (!this->plt_call_stubs_
.empty())
3164 // The base address of the .plt section.
3165 Address plt_base
= this->targ_
->plt_section()->address();
3166 Address iplt_base
= invalid_address
;
3168 // Write out plt call stubs.
3169 typename
Plt_stub_entries::const_iterator cs
;
3170 for (cs
= this->plt_call_stubs_
.begin();
3171 cs
!= this->plt_call_stubs_
.end();
3176 const Symbol
* gsym
= cs
->first
.sym_
;
3179 is_ifunc
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
3180 && gsym
->can_use_relative_reloc(false));
3181 plt_addr
= gsym
->plt_offset();
3186 const Sized_relobj_file
<size
, big_endian
>* relobj
3187 = cs
->first
.object_
;
3188 unsigned int local_sym_index
= cs
->first
.locsym_
;
3189 plt_addr
= relobj
->local_plt_offset(local_sym_index
);
3193 if (iplt_base
== invalid_address
)
3194 iplt_base
= this->targ_
->iplt_section()->address();
3195 plt_addr
+= iplt_base
;
3198 plt_addr
+= plt_base
;
3199 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3200 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
3201 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
3202 Address pltoff
= plt_addr
- got_addr
;
3204 if (pltoff
+ 0x80008000 > 0xffffffff || (pltoff
& 7) != 0)
3205 gold_error(_("%s: linkage table error against `%s'"),
3206 cs
->first
.object_
->name().c_str(),
3207 cs
->first
.sym_
->demangled_name().c_str());
3209 p
= oview
+ cs
->second
;
3210 if (ha(pltoff
) != 0)
3212 write_insn
<big_endian
>(p
, addis_12_2
+ ha(pltoff
)), p
+= 4;
3213 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
3214 write_insn
<big_endian
>(p
, ld_11_12
+ l(pltoff
)), p
+= 4;
3215 if (ha(pltoff
+ 16) != ha(pltoff
))
3217 write_insn
<big_endian
>(p
, addi_12_12
+ l(pltoff
)),p
+= 4;
3220 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3221 write_insn
<big_endian
>(p
, ld_2_12
+ l(pltoff
+ 8)), p
+= 4;
3222 write_insn
<big_endian
>(p
, ld_11_12
+ l(pltoff
+ 16)), p
+= 4;
3223 write_insn
<big_endian
>(p
, bctr
);
3227 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
3228 write_insn
<big_endian
>(p
, ld_11_2
+ l(pltoff
)), p
+= 4;
3229 if (ha(pltoff
+ 16) != ha(pltoff
))
3231 write_insn
<big_endian
>(p
, addi_2_2
+ l(pltoff
)), p
+= 4;
3234 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3235 write_insn
<big_endian
>(p
, ld_11_2
+ l(pltoff
+ 16)), p
+= 4;
3236 write_insn
<big_endian
>(p
, ld_2_2
+ l(pltoff
+ 8)), p
+= 4;
3237 write_insn
<big_endian
>(p
, bctr
);
3242 // Write out long branch stubs.
3243 typename
Branch_stub_entries::const_iterator bs
;
3244 for (bs
= this->long_branch_stubs_
.begin();
3245 bs
!= this->long_branch_stubs_
.end();
3248 p
= oview
+ this->plt_size_
+ bs
->second
;
3249 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
3250 Address delta
= bs
->first
.dest_
- loc
;
3251 if (delta
+ (1 << 25) < 2 << 25)
3252 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
3256 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
3257 gold_assert(brlt_addr
!= invalid_address
);
3258 brlt_addr
+= this->targ_
->brlt_section()->address();
3259 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
3260 Address brltoff
= brlt_addr
- got_addr
;
3261 if (ha(brltoff
) == 0)
3263 write_insn
<big_endian
>(p
, ld_11_2
+ l(brltoff
)), p
+= 4;
3267 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
3268 write_insn
<big_endian
>(p
, ld_11_12
+ l(brltoff
)), p
+= 4;
3270 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3271 write_insn
<big_endian
>(p
, bctr
);
3277 if (!this->plt_call_stubs_
.empty())
3279 // The base address of the .plt section.
3280 Address plt_base
= this->targ_
->plt_section()->address();
3281 Address iplt_base
= invalid_address
;
3282 // The address of _GLOBAL_OFFSET_TABLE_.
3283 Address g_o_t
= invalid_address
;
3285 // Write out plt call stubs.
3286 typename
Plt_stub_entries::const_iterator cs
;
3287 for (cs
= this->plt_call_stubs_
.begin();
3288 cs
!= this->plt_call_stubs_
.end();
3293 const Symbol
* gsym
= cs
->first
.sym_
;
3296 is_ifunc
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
3297 && gsym
->can_use_relative_reloc(false));
3298 plt_addr
= gsym
->plt_offset();
3303 const Sized_relobj_file
<size
, big_endian
>* relobj
3304 = cs
->first
.object_
;
3305 unsigned int local_sym_index
= cs
->first
.locsym_
;
3306 plt_addr
= relobj
->local_plt_offset(local_sym_index
);
3310 if (iplt_base
== invalid_address
)
3311 iplt_base
= this->targ_
->iplt_section()->address();
3312 plt_addr
+= iplt_base
;
3315 plt_addr
+= plt_base
;
3317 p
= oview
+ cs
->second
;
3318 if (parameters
->options().output_is_position_independent())
3321 const Powerpc_relobj
<size
, big_endian
>* ppcobj
3322 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
3323 (cs
->first
.object_
));
3324 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
3326 unsigned int got2
= ppcobj
->got2_shndx();
3327 got_addr
= ppcobj
->get_output_section_offset(got2
);
3328 gold_assert(got_addr
!= invalid_address
);
3329 got_addr
+= (ppcobj
->output_section(got2
)->address()
3330 + cs
->first
.addend_
);
3334 if (g_o_t
== invalid_address
)
3336 const Output_data_got_powerpc
<size
, big_endian
>* got
3337 = this->targ_
->got_section();
3338 g_o_t
= got
->address() + got
->g_o_t();
3343 Address pltoff
= plt_addr
- got_addr
;
3344 if (ha(pltoff
) == 0)
3346 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(pltoff
));
3347 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
3348 write_insn
<big_endian
>(p
+ 8, bctr
);
3352 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(pltoff
));
3353 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(pltoff
));
3354 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
3355 write_insn
<big_endian
>(p
+ 12, bctr
);
3360 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
3361 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
3362 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
3363 write_insn
<big_endian
>(p
+ 12, bctr
);
3368 // Write out long branch stubs.
3369 typename
Branch_stub_entries::const_iterator bs
;
3370 for (bs
= this->long_branch_stubs_
.begin();
3371 bs
!= this->long_branch_stubs_
.end();
3374 p
= oview
+ this->plt_size_
+ bs
->second
;
3375 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
3376 Address delta
= bs
->first
.dest_
- loc
;
3377 if (delta
+ (1 << 25) < 2 << 25)
3378 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
3379 else if (!parameters
->options().output_is_position_independent())
3381 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(bs
->first
.dest_
));
3382 write_insn
<big_endian
>(p
+ 4, addi_12_12
+ l(bs
->first
.dest_
));
3383 write_insn
<big_endian
>(p
+ 8, mtctr_12
);
3384 write_insn
<big_endian
>(p
+ 12, bctr
);
3389 write_insn
<big_endian
>(p
+ 0, mflr_0
);
3390 write_insn
<big_endian
>(p
+ 4, bcl_20_31
);
3391 write_insn
<big_endian
>(p
+ 8, mflr_12
);
3392 write_insn
<big_endian
>(p
+ 12, addis_12_12
+ ha(delta
));
3393 write_insn
<big_endian
>(p
+ 16, addi_12_12
+ l(delta
));
3394 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
3395 write_insn
<big_endian
>(p
+ 24, mtctr_12
);
3396 write_insn
<big_endian
>(p
+ 28, bctr
);
3402 // Write out .glink.
3404 template<int size
, bool big_endian
>
3406 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
3408 const section_size_type off
= this->offset();
3409 const section_size_type oview_size
=
3410 convert_to_section_size_type(this->data_size());
3411 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
3414 // The base address of the .plt section.
3415 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3416 Address plt_base
= this->targ_
->plt_section()->address();
3420 // Write pltresolve stub.
3422 Address after_bcl
= this->address() + 16;
3423 Address pltoff
= plt_base
- after_bcl
;
3425 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
3427 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
3428 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
3429 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
3430 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
3431 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
3432 write_insn
<big_endian
>(p
, add_12_2_11
), p
+= 4;
3433 write_insn
<big_endian
>(p
, ld_11_12
+ 0), p
+= 4;
3434 write_insn
<big_endian
>(p
, ld_2_12
+ 8), p
+= 4;
3435 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3436 write_insn
<big_endian
>(p
, ld_11_12
+ 16), p
+= 4;
3437 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
3438 while (p
< oview
+ this->pltresolve_size
)
3439 write_insn
<big_endian
>(p
, nop
), p
+= 4;
3441 // Write lazy link call stubs.
3443 while (p
< oview
+ oview_size
)
3447 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
3451 write_insn
<big_endian
>(p
, lis_0_0
+ hi(indx
)), p
+= 4;
3452 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
3454 uint32_t branch_off
= 8 - (p
- oview
);
3455 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
3461 const Output_data_got_powerpc
<size
, big_endian
>* got
3462 = this->targ_
->got_section();
3463 // The address of _GLOBAL_OFFSET_TABLE_.
3464 Address g_o_t
= got
->address() + got
->g_o_t();
3466 // Write out pltresolve branch table.
3468 unsigned int the_end
= oview_size
- this->pltresolve_size
;
3469 unsigned char* end_p
= oview
+ the_end
;
3470 while (p
< end_p
- 8 * 4)
3471 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
3473 write_insn
<big_endian
>(p
, nop
), p
+= 4;
3475 // Write out pltresolve call stub.
3476 if (parameters
->options().output_is_position_independent())
3478 Address res0_off
= 0;
3479 Address after_bcl_off
= the_end
+ 12;
3480 Address bcl_res0
= after_bcl_off
- res0_off
;
3482 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
3483 write_insn
<big_endian
>(p
+ 4, mflr_0
);
3484 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
3485 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
3486 write_insn
<big_endian
>(p
+ 16, mflr_12
);
3487 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
3488 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
3490 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
3492 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
3493 if (ha(got_bcl
) == ha(got_bcl
+ 4))
3495 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
3496 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
3500 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
3501 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
3503 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
3504 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
3505 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
3506 write_insn
<big_endian
>(p
+ 52, bctr
);
3507 write_insn
<big_endian
>(p
+ 56, nop
);
3508 write_insn
<big_endian
>(p
+ 60, nop
);
3512 Address res0
= this->address();
3514 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
3515 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
3516 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
3517 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
3519 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
3520 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
3521 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
3522 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
3523 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
3524 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
3526 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
3527 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
3528 write_insn
<big_endian
>(p
+ 32, bctr
);
3529 write_insn
<big_endian
>(p
+ 36, nop
);
3530 write_insn
<big_endian
>(p
+ 40, nop
);
3531 write_insn
<big_endian
>(p
+ 44, nop
);
3532 write_insn
<big_endian
>(p
+ 48, nop
);
3533 write_insn
<big_endian
>(p
+ 52, nop
);
3534 write_insn
<big_endian
>(p
+ 56, nop
);
3535 write_insn
<big_endian
>(p
+ 60, nop
);
3540 of
->write_output_view(off
, oview_size
, oview
);
3544 // A class to handle linker generated save/restore functions.
3546 template<int size
, bool big_endian
>
3547 class Output_data_save_res
: public Output_section_data_build
3550 Output_data_save_res(Symbol_table
* symtab
);
3553 // Write to a map file.
3555 do_print_to_mapfile(Mapfile
* mapfile
) const
3556 { mapfile
->print_output_data(this, _("** save/restore")); }
3559 do_write(Output_file
*);
3562 // The maximum size of save/restore contents.
3563 static const unsigned int savres_max
= 218*4;
3566 savres_define(Symbol_table
* symtab
,
3568 unsigned int lo
, unsigned int hi
,
3569 unsigned char* write_ent(unsigned char*, int),
3570 unsigned char* write_tail(unsigned char*, int));
3572 unsigned char *contents_
;
3575 template<bool big_endian
>
3576 static unsigned char*
3577 savegpr0(unsigned char* p
, int r
)
3579 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3580 write_insn
<big_endian
>(p
, insn
);
3584 template<bool big_endian
>
3585 static unsigned char*
3586 savegpr0_tail(unsigned char* p
, int r
)
3588 p
= savegpr0
<big_endian
>(p
, r
);
3589 uint32_t insn
= std_0_1
+ 16;
3590 write_insn
<big_endian
>(p
, insn
);
3592 write_insn
<big_endian
>(p
, blr
);
3596 template<bool big_endian
>
3597 static unsigned char*
3598 restgpr0(unsigned char* p
, int r
)
3600 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3601 write_insn
<big_endian
>(p
, insn
);
3605 template<bool big_endian
>
3606 static unsigned char*
3607 restgpr0_tail(unsigned char* p
, int r
)
3609 uint32_t insn
= ld_0_1
+ 16;
3610 write_insn
<big_endian
>(p
, insn
);
3612 p
= restgpr0
<big_endian
>(p
, r
);
3613 write_insn
<big_endian
>(p
, mtlr_0
);
3617 p
= restgpr0
<big_endian
>(p
, 30);
3618 p
= restgpr0
<big_endian
>(p
, 31);
3620 write_insn
<big_endian
>(p
, blr
);
3624 template<bool big_endian
>
3625 static unsigned char*
3626 savegpr1(unsigned char* p
, int r
)
3628 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3629 write_insn
<big_endian
>(p
, insn
);
3633 template<bool big_endian
>
3634 static unsigned char*
3635 savegpr1_tail(unsigned char* p
, int r
)
3637 p
= savegpr1
<big_endian
>(p
, r
);
3638 write_insn
<big_endian
>(p
, blr
);
3642 template<bool big_endian
>
3643 static unsigned char*
3644 restgpr1(unsigned char* p
, int r
)
3646 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3647 write_insn
<big_endian
>(p
, insn
);
3651 template<bool big_endian
>
3652 static unsigned char*
3653 restgpr1_tail(unsigned char* p
, int r
)
3655 p
= restgpr1
<big_endian
>(p
, r
);
3656 write_insn
<big_endian
>(p
, blr
);
3660 template<bool big_endian
>
3661 static unsigned char*
3662 savefpr(unsigned char* p
, int r
)
3664 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3665 write_insn
<big_endian
>(p
, insn
);
3669 template<bool big_endian
>
3670 static unsigned char*
3671 savefpr0_tail(unsigned char* p
, int r
)
3673 p
= savefpr
<big_endian
>(p
, r
);
3674 write_insn
<big_endian
>(p
, std_0_1
+ 16);
3676 write_insn
<big_endian
>(p
, blr
);
3680 template<bool big_endian
>
3681 static unsigned char*
3682 restfpr(unsigned char* p
, int r
)
3684 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3685 write_insn
<big_endian
>(p
, insn
);
3689 template<bool big_endian
>
3690 static unsigned char*
3691 restfpr0_tail(unsigned char* p
, int r
)
3693 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
3695 p
= restfpr
<big_endian
>(p
, r
);
3696 write_insn
<big_endian
>(p
, mtlr_0
);
3700 p
= restfpr
<big_endian
>(p
, 30);
3701 p
= restfpr
<big_endian
>(p
, 31);
3703 write_insn
<big_endian
>(p
, blr
);
3707 template<bool big_endian
>
3708 static unsigned char*
3709 savefpr1_tail(unsigned char* p
, int r
)
3711 p
= savefpr
<big_endian
>(p
, r
);
3712 write_insn
<big_endian
>(p
, blr
);
3716 template<bool big_endian
>
3717 static unsigned char*
3718 restfpr1_tail(unsigned char* p
, int r
)
3720 p
= restfpr
<big_endian
>(p
, r
);
3721 write_insn
<big_endian
>(p
, blr
);
3725 template<bool big_endian
>
3726 static unsigned char*
3727 savevr(unsigned char* p
, int r
)
3729 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
3730 write_insn
<big_endian
>(p
, insn
);
3732 insn
= stvx_0_12_0
+ (r
<< 21);
3733 write_insn
<big_endian
>(p
, insn
);
3737 template<bool big_endian
>
3738 static unsigned char*
3739 savevr_tail(unsigned char* p
, int r
)
3741 p
= savevr
<big_endian
>(p
, r
);
3742 write_insn
<big_endian
>(p
, blr
);
3746 template<bool big_endian
>
3747 static unsigned char*
3748 restvr(unsigned char* p
, int r
)
3750 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
3751 write_insn
<big_endian
>(p
, insn
);
3753 insn
= lvx_0_12_0
+ (r
<< 21);
3754 write_insn
<big_endian
>(p
, insn
);
3758 template<bool big_endian
>
3759 static unsigned char*
3760 restvr_tail(unsigned char* p
, int r
)
3762 p
= restvr
<big_endian
>(p
, r
);
3763 write_insn
<big_endian
>(p
, blr
);
3768 template<int size
, bool big_endian
>
3769 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
3770 Symbol_table
* symtab
)
3771 : Output_section_data_build(4),
3774 this->savres_define(symtab
,
3775 "_savegpr0_", 14, 31,
3776 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
3777 this->savres_define(symtab
,
3778 "_restgpr0_", 14, 29,
3779 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
3780 this->savres_define(symtab
,
3781 "_restgpr0_", 30, 31,
3782 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
3783 this->savres_define(symtab
,
3784 "_savegpr1_", 14, 31,
3785 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
3786 this->savres_define(symtab
,
3787 "_restgpr1_", 14, 31,
3788 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
3789 this->savres_define(symtab
,
3790 "_savefpr_", 14, 31,
3791 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
3792 this->savres_define(symtab
,
3793 "_restfpr_", 14, 29,
3794 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
3795 this->savres_define(symtab
,
3796 "_restfpr_", 30, 31,
3797 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
3798 this->savres_define(symtab
,
3800 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
3801 this->savres_define(symtab
,
3803 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
3804 this->savres_define(symtab
,
3806 savevr
<big_endian
>, savevr_tail
<big_endian
>);
3807 this->savres_define(symtab
,
3809 restvr
<big_endian
>, restvr_tail
<big_endian
>);
3812 template<int size
, bool big_endian
>
3814 Output_data_save_res
<size
, big_endian
>::savres_define(
3815 Symbol_table
* symtab
,
3817 unsigned int lo
, unsigned int hi
,
3818 unsigned char* write_ent(unsigned char*, int),
3819 unsigned char* write_tail(unsigned char*, int))
3821 size_t len
= strlen(name
);
3822 bool writing
= false;
3825 memcpy(sym
, name
, len
);
3828 for (unsigned int i
= lo
; i
<= hi
; i
++)
3830 sym
[len
+ 0] = i
/ 10 + '0';
3831 sym
[len
+ 1] = i
% 10 + '0';
3832 Symbol
* gsym
= symtab
->lookup(sym
);
3833 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
3834 writing
= writing
|| refd
;
3837 if (this->contents_
== NULL
)
3838 this->contents_
= new unsigned char[this->savres_max
];
3840 section_size_type value
= this->current_data_size();
3841 unsigned char* p
= this->contents_
+ value
;
3843 p
= write_ent(p
, i
);
3845 p
= write_tail(p
, i
);
3846 section_size_type cur_size
= p
- this->contents_
;
3847 this->set_current_data_size(cur_size
);
3849 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
3850 this, value
, cur_size
- value
,
3851 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
3852 elfcpp::STV_HIDDEN
, 0, false, false);
3857 // Write out save/restore.
3859 template<int size
, bool big_endian
>
3861 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
3863 const section_size_type off
= this->offset();
3864 const section_size_type oview_size
=
3865 convert_to_section_size_type(this->data_size());
3866 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
3867 memcpy(oview
, this->contents_
, oview_size
);
3868 of
->write_output_view(off
, oview_size
, oview
);
3872 // Create the glink section.
3874 template<int size
, bool big_endian
>
3876 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
3878 if (this->glink_
== NULL
)
3880 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
3881 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
3882 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
3883 this->glink_
, ORDER_TEXT
, false);
3887 // Create a PLT entry for a global symbol.
3889 template<int size
, bool big_endian
>
3891 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
3895 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
3896 && gsym
->can_use_relative_reloc(false))
3898 if (this->iplt_
== NULL
)
3899 this->make_iplt_section(symtab
, layout
);
3900 this->iplt_
->add_ifunc_entry(gsym
);
3904 if (this->plt_
== NULL
)
3905 this->make_plt_section(symtab
, layout
);
3906 this->plt_
->add_entry(gsym
);
3910 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
3912 template<int size
, bool big_endian
>
3914 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
3915 Symbol_table
* symtab
,
3917 Sized_relobj_file
<size
, big_endian
>* relobj
,
3920 if (this->iplt_
== NULL
)
3921 this->make_iplt_section(symtab
, layout
);
3922 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
3925 // Return the number of entries in the PLT.
3927 template<int size
, bool big_endian
>
3929 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
3931 if (this->plt_
== NULL
)
3933 unsigned int count
= this->plt_
->entry_count();
3934 if (this->iplt_
!= NULL
)
3935 count
+= this->iplt_
->entry_count();
3939 // Return the offset of the first non-reserved PLT entry.
3941 template<int size
, bool big_endian
>
3943 Target_powerpc
<size
, big_endian
>::first_plt_entry_offset() const
3945 return this->plt_
->first_plt_entry_offset();
3948 // Return the size of each PLT entry.
3950 template<int size
, bool big_endian
>
3952 Target_powerpc
<size
, big_endian
>::plt_entry_size() const
3954 return Output_data_plt_powerpc
<size
, big_endian
>::get_plt_entry_size();
3957 // Create a GOT entry for local dynamic __tls_get_addr calls.
3959 template<int size
, bool big_endian
>
3961 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
3962 Symbol_table
* symtab
,
3964 Sized_relobj_file
<size
, big_endian
>* object
)
3966 if (this->tlsld_got_offset_
== -1U)
3968 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
3969 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
3970 Output_data_got_powerpc
<size
, big_endian
>* got
3971 = this->got_section(symtab
, layout
);
3972 unsigned int got_offset
= got
->add_constant_pair(0, 0);
3973 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
3975 this->tlsld_got_offset_
= got_offset
;
3977 return this->tlsld_got_offset_
;
3980 // Get the Reference_flags for a particular relocation.
3982 template<int size
, bool big_endian
>
3984 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(unsigned int r_type
)
3988 case elfcpp::R_POWERPC_NONE
:
3989 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
3990 case elfcpp::R_POWERPC_GNU_VTENTRY
:
3991 case elfcpp::R_PPC64_TOC
:
3992 // No symbol reference.
3995 case elfcpp::R_PPC64_ADDR64
:
3996 case elfcpp::R_PPC64_UADDR64
:
3997 case elfcpp::R_POWERPC_ADDR32
:
3998 case elfcpp::R_POWERPC_UADDR32
:
3999 case elfcpp::R_POWERPC_ADDR16
:
4000 case elfcpp::R_POWERPC_UADDR16
:
4001 case elfcpp::R_POWERPC_ADDR16_LO
:
4002 case elfcpp::R_POWERPC_ADDR16_HI
:
4003 case elfcpp::R_POWERPC_ADDR16_HA
:
4004 return Symbol::ABSOLUTE_REF
;
4006 case elfcpp::R_POWERPC_ADDR24
:
4007 case elfcpp::R_POWERPC_ADDR14
:
4008 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4009 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4010 return Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
4012 case elfcpp::R_PPC64_REL64
:
4013 case elfcpp::R_POWERPC_REL32
:
4014 case elfcpp::R_PPC_LOCAL24PC
:
4015 case elfcpp::R_POWERPC_REL16
:
4016 case elfcpp::R_POWERPC_REL16_LO
:
4017 case elfcpp::R_POWERPC_REL16_HI
:
4018 case elfcpp::R_POWERPC_REL16_HA
:
4019 return Symbol::RELATIVE_REF
;
4021 case elfcpp::R_POWERPC_REL24
:
4022 case elfcpp::R_PPC_PLTREL24
:
4023 case elfcpp::R_POWERPC_REL14
:
4024 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4025 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4026 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
4028 case elfcpp::R_POWERPC_GOT16
:
4029 case elfcpp::R_POWERPC_GOT16_LO
:
4030 case elfcpp::R_POWERPC_GOT16_HI
:
4031 case elfcpp::R_POWERPC_GOT16_HA
:
4032 case elfcpp::R_PPC64_GOT16_DS
:
4033 case elfcpp::R_PPC64_GOT16_LO_DS
:
4034 case elfcpp::R_PPC64_TOC16
:
4035 case elfcpp::R_PPC64_TOC16_LO
:
4036 case elfcpp::R_PPC64_TOC16_HI
:
4037 case elfcpp::R_PPC64_TOC16_HA
:
4038 case elfcpp::R_PPC64_TOC16_DS
:
4039 case elfcpp::R_PPC64_TOC16_LO_DS
:
4041 return Symbol::ABSOLUTE_REF
;
4043 case elfcpp::R_POWERPC_GOT_TPREL16
:
4044 case elfcpp::R_POWERPC_TLS
:
4045 return Symbol::TLS_REF
;
4047 case elfcpp::R_POWERPC_COPY
:
4048 case elfcpp::R_POWERPC_GLOB_DAT
:
4049 case elfcpp::R_POWERPC_JMP_SLOT
:
4050 case elfcpp::R_POWERPC_RELATIVE
:
4051 case elfcpp::R_POWERPC_DTPMOD
:
4053 // Not expected. We will give an error later.
4058 // Report an unsupported relocation against a local symbol.
4060 template<int size
, bool big_endian
>
4062 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
4063 Sized_relobj_file
<size
, big_endian
>* object
,
4064 unsigned int r_type
)
4066 gold_error(_("%s: unsupported reloc %u against local symbol"),
4067 object
->name().c_str(), r_type
);
4070 // We are about to emit a dynamic relocation of type R_TYPE. If the
4071 // dynamic linker does not support it, issue an error.
4073 template<int size
, bool big_endian
>
4075 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
4076 unsigned int r_type
)
4078 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
4080 // These are the relocation types supported by glibc for both 32-bit
4081 // and 64-bit powerpc.
4084 case elfcpp::R_POWERPC_NONE
:
4085 case elfcpp::R_POWERPC_RELATIVE
:
4086 case elfcpp::R_POWERPC_GLOB_DAT
:
4087 case elfcpp::R_POWERPC_DTPMOD
:
4088 case elfcpp::R_POWERPC_DTPREL
:
4089 case elfcpp::R_POWERPC_TPREL
:
4090 case elfcpp::R_POWERPC_JMP_SLOT
:
4091 case elfcpp::R_POWERPC_COPY
:
4092 case elfcpp::R_POWERPC_IRELATIVE
:
4093 case elfcpp::R_POWERPC_ADDR32
:
4094 case elfcpp::R_POWERPC_UADDR32
:
4095 case elfcpp::R_POWERPC_ADDR24
:
4096 case elfcpp::R_POWERPC_ADDR16
:
4097 case elfcpp::R_POWERPC_UADDR16
:
4098 case elfcpp::R_POWERPC_ADDR16_LO
:
4099 case elfcpp::R_POWERPC_ADDR16_HI
:
4100 case elfcpp::R_POWERPC_ADDR16_HA
:
4101 case elfcpp::R_POWERPC_ADDR14
:
4102 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4103 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4104 case elfcpp::R_POWERPC_REL32
:
4105 case elfcpp::R_POWERPC_REL24
:
4106 case elfcpp::R_POWERPC_TPREL16
:
4107 case elfcpp::R_POWERPC_TPREL16_LO
:
4108 case elfcpp::R_POWERPC_TPREL16_HI
:
4109 case elfcpp::R_POWERPC_TPREL16_HA
:
4120 // These are the relocation types supported only on 64-bit.
4121 case elfcpp::R_PPC64_ADDR64
:
4122 case elfcpp::R_PPC64_UADDR64
:
4123 case elfcpp::R_PPC64_JMP_IREL
:
4124 case elfcpp::R_PPC64_ADDR16_DS
:
4125 case elfcpp::R_PPC64_ADDR16_LO_DS
:
4126 case elfcpp::R_PPC64_ADDR16_HIGHER
:
4127 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
4128 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
4129 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
4130 case elfcpp::R_PPC64_REL64
:
4131 case elfcpp::R_POWERPC_ADDR30
:
4132 case elfcpp::R_PPC64_TPREL16_DS
:
4133 case elfcpp::R_PPC64_TPREL16_LO_DS
:
4134 case elfcpp::R_PPC64_TPREL16_HIGHER
:
4135 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
4136 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
4137 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
4148 // These are the relocation types supported only on 32-bit.
4149 // ??? glibc ld.so doesn't need to support these.
4150 case elfcpp::R_POWERPC_DTPREL16
:
4151 case elfcpp::R_POWERPC_DTPREL16_LO
:
4152 case elfcpp::R_POWERPC_DTPREL16_HI
:
4153 case elfcpp::R_POWERPC_DTPREL16_HA
:
4161 // This prevents us from issuing more than one error per reloc
4162 // section. But we can still wind up issuing more than one
4163 // error per object file.
4164 if (this->issued_non_pic_error_
)
4166 gold_assert(parameters
->options().output_is_position_independent());
4167 object
->error(_("requires unsupported dynamic reloc; "
4168 "recompile with -fPIC"));
4169 this->issued_non_pic_error_
= true;
4173 // Return whether we need to make a PLT entry for a relocation of the
4174 // given type against a STT_GNU_IFUNC symbol.
4176 template<int size
, bool big_endian
>
4178 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
4179 Sized_relobj_file
<size
, big_endian
>* object
,
4180 unsigned int r_type
)
4182 // In non-pic code any reference will resolve to the plt call stub
4183 // for the ifunc symbol.
4184 if (size
== 32 && !parameters
->options().output_is_position_independent())
4189 // Word size refs from data sections are OK.
4190 case elfcpp::R_POWERPC_ADDR32
:
4191 case elfcpp::R_POWERPC_UADDR32
:
4196 case elfcpp::R_PPC64_ADDR64
:
4197 case elfcpp::R_PPC64_UADDR64
:
4202 // GOT refs are good.
4203 case elfcpp::R_POWERPC_GOT16
:
4204 case elfcpp::R_POWERPC_GOT16_LO
:
4205 case elfcpp::R_POWERPC_GOT16_HI
:
4206 case elfcpp::R_POWERPC_GOT16_HA
:
4207 case elfcpp::R_PPC64_GOT16_DS
:
4208 case elfcpp::R_PPC64_GOT16_LO_DS
:
4211 // So are function calls.
4212 case elfcpp::R_POWERPC_ADDR24
:
4213 case elfcpp::R_POWERPC_ADDR14
:
4214 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4215 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4216 case elfcpp::R_POWERPC_REL24
:
4217 case elfcpp::R_PPC_PLTREL24
:
4218 case elfcpp::R_POWERPC_REL14
:
4219 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4220 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4227 // Anything else is a problem.
4228 // If we are building a static executable, the libc startup function
4229 // responsible for applying indirect function relocations is going
4230 // to complain about the reloc type.
4231 // If we are building a dynamic executable, we will have a text
4232 // relocation. The dynamic loader will set the text segment
4233 // writable and non-executable to apply text relocations. So we'll
4234 // segfault when trying to run the indirection function to resolve
4236 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
4237 object
->name().c_str(), r_type
);
4241 // Scan a relocation for a local symbol.
4243 template<int size
, bool big_endian
>
4245 Target_powerpc
<size
, big_endian
>::Scan::local(
4246 Symbol_table
* symtab
,
4248 Target_powerpc
<size
, big_endian
>* target
,
4249 Sized_relobj_file
<size
, big_endian
>* object
,
4250 unsigned int data_shndx
,
4251 Output_section
* output_section
,
4252 const elfcpp::Rela
<size
, big_endian
>& reloc
,
4253 unsigned int r_type
,
4254 const elfcpp::Sym
<size
, big_endian
>& lsym
,
4257 Powerpc_relobj
<size
, big_endian
>* ppc_object
4258 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
4263 && data_shndx
== ppc_object
->opd_shndx()
4264 && r_type
== elfcpp::R_PPC64_ADDR64
)
4265 ppc_object
->set_opd_discard(reloc
.get_r_offset());
4269 // A local STT_GNU_IFUNC symbol may require a PLT entry.
4270 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
4271 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
4273 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4274 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4275 r_type
, r_sym
, reloc
.get_r_addend());
4276 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
4281 case elfcpp::R_POWERPC_NONE
:
4282 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
4283 case elfcpp::R_POWERPC_GNU_VTENTRY
:
4284 case elfcpp::R_PPC64_TOCSAVE
:
4285 case elfcpp::R_PPC_EMB_MRKREF
:
4286 case elfcpp::R_POWERPC_TLS
:
4289 case elfcpp::R_PPC64_TOC
:
4291 Output_data_got_powerpc
<size
, big_endian
>* got
4292 = target
->got_section(symtab
, layout
);
4293 if (parameters
->options().output_is_position_independent())
4295 Address off
= reloc
.get_r_offset();
4297 && data_shndx
== ppc_object
->opd_shndx()
4298 && ppc_object
->get_opd_discard(off
- 8))
4301 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4302 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
4303 rela_dyn
->add_output_section_relative(got
->output_section(),
4304 elfcpp::R_POWERPC_RELATIVE
,
4306 object
, data_shndx
, off
,
4307 symobj
->toc_base_offset());
4312 case elfcpp::R_PPC64_ADDR64
:
4313 case elfcpp::R_PPC64_UADDR64
:
4314 case elfcpp::R_POWERPC_ADDR32
:
4315 case elfcpp::R_POWERPC_UADDR32
:
4316 case elfcpp::R_POWERPC_ADDR24
:
4317 case elfcpp::R_POWERPC_ADDR16
:
4318 case elfcpp::R_POWERPC_ADDR16_LO
:
4319 case elfcpp::R_POWERPC_ADDR16_HI
:
4320 case elfcpp::R_POWERPC_ADDR16_HA
:
4321 case elfcpp::R_POWERPC_UADDR16
:
4322 case elfcpp::R_PPC64_ADDR16_HIGHER
:
4323 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
4324 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
4325 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
4326 case elfcpp::R_PPC64_ADDR16_DS
:
4327 case elfcpp::R_PPC64_ADDR16_LO_DS
:
4328 case elfcpp::R_POWERPC_ADDR14
:
4329 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4330 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4331 // If building a shared library (or a position-independent
4332 // executable), we need to create a dynamic relocation for
4334 if (parameters
->options().output_is_position_independent()
4335 || (size
== 64 && is_ifunc
))
4337 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4339 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
4340 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
4342 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4343 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
4346 rela_dyn
= target
->iplt_section()->rel_plt();
4347 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
4349 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
4350 output_section
, data_shndx
,
4351 reloc
.get_r_offset(),
4352 reloc
.get_r_addend(), false);
4356 check_non_pic(object
, r_type
);
4357 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4358 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
4359 data_shndx
, reloc
.get_r_offset(),
4360 reloc
.get_r_addend());
4365 case elfcpp::R_POWERPC_REL24
:
4366 case elfcpp::R_PPC_PLTREL24
:
4367 case elfcpp::R_PPC_LOCAL24PC
:
4368 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4369 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4370 reloc
.get_r_addend());
4373 case elfcpp::R_POWERPC_REL14
:
4374 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4375 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4376 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4377 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4378 reloc
.get_r_addend());
4381 case elfcpp::R_PPC64_REL64
:
4382 case elfcpp::R_POWERPC_REL32
:
4383 case elfcpp::R_POWERPC_REL16
:
4384 case elfcpp::R_POWERPC_REL16_LO
:
4385 case elfcpp::R_POWERPC_REL16_HI
:
4386 case elfcpp::R_POWERPC_REL16_HA
:
4387 case elfcpp::R_POWERPC_SECTOFF
:
4388 case elfcpp::R_POWERPC_TPREL16
:
4389 case elfcpp::R_POWERPC_DTPREL16
:
4390 case elfcpp::R_POWERPC_SECTOFF_LO
:
4391 case elfcpp::R_POWERPC_TPREL16_LO
:
4392 case elfcpp::R_POWERPC_DTPREL16_LO
:
4393 case elfcpp::R_POWERPC_SECTOFF_HI
:
4394 case elfcpp::R_POWERPC_TPREL16_HI
:
4395 case elfcpp::R_POWERPC_DTPREL16_HI
:
4396 case elfcpp::R_POWERPC_SECTOFF_HA
:
4397 case elfcpp::R_POWERPC_TPREL16_HA
:
4398 case elfcpp::R_POWERPC_DTPREL16_HA
:
4399 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
4400 case elfcpp::R_PPC64_TPREL16_HIGHER
:
4401 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
4402 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
4403 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
4404 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
4405 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
4406 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
4407 case elfcpp::R_PPC64_TPREL16_DS
:
4408 case elfcpp::R_PPC64_TPREL16_LO_DS
:
4409 case elfcpp::R_PPC64_DTPREL16_DS
:
4410 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
4411 case elfcpp::R_PPC64_SECTOFF_DS
:
4412 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
4413 case elfcpp::R_PPC64_TLSGD
:
4414 case elfcpp::R_PPC64_TLSLD
:
4417 case elfcpp::R_POWERPC_GOT16
:
4418 case elfcpp::R_POWERPC_GOT16_LO
:
4419 case elfcpp::R_POWERPC_GOT16_HI
:
4420 case elfcpp::R_POWERPC_GOT16_HA
:
4421 case elfcpp::R_PPC64_GOT16_DS
:
4422 case elfcpp::R_PPC64_GOT16_LO_DS
:
4424 // The symbol requires a GOT entry.
4425 Output_data_got_powerpc
<size
, big_endian
>* got
4426 = target
->got_section(symtab
, layout
);
4427 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4429 if (!parameters
->options().output_is_position_independent())
4431 if (size
== 32 && is_ifunc
)
4432 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
4434 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
4436 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
4438 // If we are generating a shared object or a pie, this
4439 // symbol's GOT entry will be set by a dynamic relocation.
4441 off
= got
->add_constant(0);
4442 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
4444 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4445 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
4448 rela_dyn
= target
->iplt_section()->rel_plt();
4449 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
4451 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
4452 got
, off
, 0, false);
4457 case elfcpp::R_PPC64_TOC16
:
4458 case elfcpp::R_PPC64_TOC16_LO
:
4459 case elfcpp::R_PPC64_TOC16_HI
:
4460 case elfcpp::R_PPC64_TOC16_HA
:
4461 case elfcpp::R_PPC64_TOC16_DS
:
4462 case elfcpp::R_PPC64_TOC16_LO_DS
:
4463 // We need a GOT section.
4464 target
->got_section(symtab
, layout
);
4467 case elfcpp::R_POWERPC_GOT_TLSGD16
:
4468 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
4469 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
4470 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
4472 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
4473 if (tls_type
== tls::TLSOPT_NONE
)
4475 Output_data_got_powerpc
<size
, big_endian
>* got
4476 = target
->got_section(symtab
, layout
);
4477 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4478 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4479 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
4480 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
4482 else if (tls_type
== tls::TLSOPT_TO_LE
)
4484 // no GOT relocs needed for Local Exec.
4491 case elfcpp::R_POWERPC_GOT_TLSLD16
:
4492 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
4493 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
4494 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
4496 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
4497 if (tls_type
== tls::TLSOPT_NONE
)
4498 target
->tlsld_got_offset(symtab
, layout
, object
);
4499 else if (tls_type
== tls::TLSOPT_TO_LE
)
4501 // no GOT relocs needed for Local Exec.
4502 if (parameters
->options().emit_relocs())
4504 Output_section
* os
= layout
->tls_segment()->first_section();
4505 gold_assert(os
!= NULL
);
4506 os
->set_needs_symtab_index();
4514 case elfcpp::R_POWERPC_GOT_DTPREL16
:
4515 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
4516 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
4517 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
4519 Output_data_got_powerpc
<size
, big_endian
>* got
4520 = target
->got_section(symtab
, layout
);
4521 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4522 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
4526 case elfcpp::R_POWERPC_GOT_TPREL16
:
4527 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
4528 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
4529 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
4531 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
4532 if (tls_type
== tls::TLSOPT_NONE
)
4534 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4535 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
4537 Output_data_got_powerpc
<size
, big_endian
>* got
4538 = target
->got_section(symtab
, layout
);
4539 unsigned int off
= got
->add_constant(0);
4540 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
4542 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4543 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
4544 elfcpp::R_POWERPC_TPREL
,
4548 else if (tls_type
== tls::TLSOPT_TO_LE
)
4550 // no GOT relocs needed for Local Exec.
4558 unsupported_reloc_local(object
, r_type
);
4563 // Report an unsupported relocation against a global symbol.
4565 template<int size
, bool big_endian
>
4567 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
4568 Sized_relobj_file
<size
, big_endian
>* object
,
4569 unsigned int r_type
,
4572 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4573 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
4576 // Scan a relocation for a global symbol.
4578 template<int size
, bool big_endian
>
4580 Target_powerpc
<size
, big_endian
>::Scan::global(
4581 Symbol_table
* symtab
,
4583 Target_powerpc
<size
, big_endian
>* target
,
4584 Sized_relobj_file
<size
, big_endian
>* object
,
4585 unsigned int data_shndx
,
4586 Output_section
* output_section
,
4587 const elfcpp::Rela
<size
, big_endian
>& reloc
,
4588 unsigned int r_type
,
4591 Powerpc_relobj
<size
, big_endian
>* ppc_object
4592 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
4594 // A STT_GNU_IFUNC symbol may require a PLT entry.
4595 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4596 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
4598 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4599 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4600 reloc
.get_r_addend());
4601 target
->make_plt_entry(symtab
, layout
, gsym
);
4606 case elfcpp::R_POWERPC_NONE
:
4607 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
4608 case elfcpp::R_POWERPC_GNU_VTENTRY
:
4609 case elfcpp::R_PPC_LOCAL24PC
:
4610 case elfcpp::R_PPC_EMB_MRKREF
:
4611 case elfcpp::R_POWERPC_TLS
:
4614 case elfcpp::R_PPC64_TOC
:
4616 Output_data_got_powerpc
<size
, big_endian
>* got
4617 = target
->got_section(symtab
, layout
);
4618 if (parameters
->options().output_is_position_independent())
4620 Address off
= reloc
.get_r_offset();
4622 && data_shndx
== ppc_object
->opd_shndx()
4623 && ppc_object
->get_opd_discard(off
- 8))
4626 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4627 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
4628 if (data_shndx
!= ppc_object
->opd_shndx())
4629 symobj
= static_cast
4630 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
4631 rela_dyn
->add_output_section_relative(got
->output_section(),
4632 elfcpp::R_POWERPC_RELATIVE
,
4634 object
, data_shndx
, off
,
4635 symobj
->toc_base_offset());
4640 case elfcpp::R_PPC64_ADDR64
:
4642 && data_shndx
== ppc_object
->opd_shndx()
4643 && (gsym
->is_defined_in_discarded_section()
4644 || gsym
->object() != object
))
4646 ppc_object
->set_opd_discard(reloc
.get_r_offset());
4650 case elfcpp::R_PPC64_UADDR64
:
4651 case elfcpp::R_POWERPC_ADDR32
:
4652 case elfcpp::R_POWERPC_UADDR32
:
4653 case elfcpp::R_POWERPC_ADDR24
:
4654 case elfcpp::R_POWERPC_ADDR16
:
4655 case elfcpp::R_POWERPC_ADDR16_LO
:
4656 case elfcpp::R_POWERPC_ADDR16_HI
:
4657 case elfcpp::R_POWERPC_ADDR16_HA
:
4658 case elfcpp::R_POWERPC_UADDR16
:
4659 case elfcpp::R_PPC64_ADDR16_HIGHER
:
4660 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
4661 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
4662 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
4663 case elfcpp::R_PPC64_ADDR16_DS
:
4664 case elfcpp::R_PPC64_ADDR16_LO_DS
:
4665 case elfcpp::R_POWERPC_ADDR14
:
4666 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4667 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4669 // Make a PLT entry if necessary.
4670 if (gsym
->needs_plt_entry())
4672 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4674 elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4675 reloc
.get_r_addend());
4676 target
->make_plt_entry(symtab
, layout
, gsym
);
4677 // Since this is not a PC-relative relocation, we may be
4678 // taking the address of a function. In that case we need to
4679 // set the entry in the dynamic symbol table to the address of
4680 // the PLT call stub.
4682 && gsym
->is_from_dynobj()
4683 && !parameters
->options().output_is_position_independent())
4684 gsym
->set_needs_dynsym_value();
4686 // Make a dynamic relocation if necessary.
4687 if (needs_dynamic_reloc
<size
>(gsym
, Scan::get_reference_flags(r_type
))
4688 || (size
== 64 && gsym
->type() == elfcpp::STT_GNU_IFUNC
))
4690 if (gsym
->may_need_copy_reloc())
4692 target
->copy_reloc(symtab
, layout
, object
,
4693 data_shndx
, output_section
, gsym
, reloc
);
4695 else if ((size
== 32
4696 && r_type
== elfcpp::R_POWERPC_ADDR32
4697 && gsym
->can_use_relative_reloc(false)
4698 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
4699 && parameters
->options().shared()))
4701 && r_type
== elfcpp::R_PPC64_ADDR64
4702 && (gsym
->can_use_relative_reloc(false)
4703 || data_shndx
== ppc_object
->opd_shndx())))
4705 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4706 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
4707 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
4709 rela_dyn
= target
->iplt_section()->rel_plt();
4710 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
4712 rela_dyn
->add_symbolless_global_addend(
4713 gsym
, dynrel
, output_section
, object
, data_shndx
,
4714 reloc
.get_r_offset(), reloc
.get_r_addend());
4718 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4719 check_non_pic(object
, r_type
);
4720 rela_dyn
->add_global(gsym
, r_type
, output_section
,
4722 reloc
.get_r_offset(),
4723 reloc
.get_r_addend());
4729 case elfcpp::R_PPC_PLTREL24
:
4730 case elfcpp::R_POWERPC_REL24
:
4731 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4732 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4733 reloc
.get_r_addend());
4734 if (gsym
->needs_plt_entry()
4735 || (!gsym
->final_value_is_known()
4736 && (gsym
->is_undefined()
4737 || gsym
->is_from_dynobj()
4738 || gsym
->is_preemptible())))
4739 target
->make_plt_entry(symtab
, layout
, gsym
);
4742 case elfcpp::R_PPC64_REL64
:
4743 case elfcpp::R_POWERPC_REL32
:
4744 // Make a dynamic relocation if necessary.
4745 if (needs_dynamic_reloc
<size
>(gsym
, Scan::get_reference_flags(r_type
)))
4747 if (gsym
->may_need_copy_reloc())
4749 target
->copy_reloc(symtab
, layout
, object
,
4750 data_shndx
, output_section
, gsym
,
4755 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4756 check_non_pic(object
, r_type
);
4757 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4758 data_shndx
, reloc
.get_r_offset(),
4759 reloc
.get_r_addend());
4764 case elfcpp::R_POWERPC_REL14
:
4765 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4766 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4767 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4768 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4769 reloc
.get_r_addend());
4772 case elfcpp::R_POWERPC_REL16
:
4773 case elfcpp::R_POWERPC_REL16_LO
:
4774 case elfcpp::R_POWERPC_REL16_HI
:
4775 case elfcpp::R_POWERPC_REL16_HA
:
4776 case elfcpp::R_POWERPC_SECTOFF
:
4777 case elfcpp::R_POWERPC_TPREL16
:
4778 case elfcpp::R_POWERPC_DTPREL16
:
4779 case elfcpp::R_POWERPC_SECTOFF_LO
:
4780 case elfcpp::R_POWERPC_TPREL16_LO
:
4781 case elfcpp::R_POWERPC_DTPREL16_LO
:
4782 case elfcpp::R_POWERPC_SECTOFF_HI
:
4783 case elfcpp::R_POWERPC_TPREL16_HI
:
4784 case elfcpp::R_POWERPC_DTPREL16_HI
:
4785 case elfcpp::R_POWERPC_SECTOFF_HA
:
4786 case elfcpp::R_POWERPC_TPREL16_HA
:
4787 case elfcpp::R_POWERPC_DTPREL16_HA
:
4788 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
4789 case elfcpp::R_PPC64_TPREL16_HIGHER
:
4790 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
4791 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
4792 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
4793 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
4794 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
4795 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
4796 case elfcpp::R_PPC64_TPREL16_DS
:
4797 case elfcpp::R_PPC64_TPREL16_LO_DS
:
4798 case elfcpp::R_PPC64_DTPREL16_DS
:
4799 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
4800 case elfcpp::R_PPC64_SECTOFF_DS
:
4801 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
4802 case elfcpp::R_PPC64_TLSGD
:
4803 case elfcpp::R_PPC64_TLSLD
:
4806 case elfcpp::R_POWERPC_GOT16
:
4807 case elfcpp::R_POWERPC_GOT16_LO
:
4808 case elfcpp::R_POWERPC_GOT16_HI
:
4809 case elfcpp::R_POWERPC_GOT16_HA
:
4810 case elfcpp::R_PPC64_GOT16_DS
:
4811 case elfcpp::R_PPC64_GOT16_LO_DS
:
4813 // The symbol requires a GOT entry.
4814 Output_data_got_powerpc
<size
, big_endian
>* got
;
4816 got
= target
->got_section(symtab
, layout
);
4817 if (gsym
->final_value_is_known())
4819 if (size
== 32 && gsym
->type() == elfcpp::STT_GNU_IFUNC
)
4820 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
4822 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
4824 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
4826 // If we are generating a shared object or a pie, this
4827 // symbol's GOT entry will be set by a dynamic relocation.
4828 unsigned int off
= got
->add_constant(0);
4829 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
4831 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4832 if (gsym
->can_use_relative_reloc(false)
4834 && gsym
->visibility() == elfcpp::STV_PROTECTED
4835 && parameters
->options().shared()))
4837 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
4838 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
4840 rela_dyn
= target
->iplt_section()->rel_plt();
4841 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
4843 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
4847 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
4848 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
4854 case elfcpp::R_PPC64_TOC16
:
4855 case elfcpp::R_PPC64_TOC16_LO
:
4856 case elfcpp::R_PPC64_TOC16_HI
:
4857 case elfcpp::R_PPC64_TOC16_HA
:
4858 case elfcpp::R_PPC64_TOC16_DS
:
4859 case elfcpp::R_PPC64_TOC16_LO_DS
:
4860 // We need a GOT section.
4861 target
->got_section(symtab
, layout
);
4864 case elfcpp::R_POWERPC_GOT_TLSGD16
:
4865 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
4866 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
4867 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
4869 const bool final
= gsym
->final_value_is_known();
4870 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
4871 if (tls_type
== tls::TLSOPT_NONE
)
4873 Output_data_got_powerpc
<size
, big_endian
>* got
4874 = target
->got_section(symtab
, layout
);
4875 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
,
4876 target
->rela_dyn_section(layout
),
4877 elfcpp::R_POWERPC_DTPMOD
,
4878 elfcpp::R_POWERPC_DTPREL
);
4880 else if (tls_type
== tls::TLSOPT_TO_IE
)
4882 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
4884 Output_data_got_powerpc
<size
, big_endian
>* got
4885 = target
->got_section(symtab
, layout
);
4886 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4887 if (gsym
->is_undefined()
4888 || gsym
->is_from_dynobj())
4890 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
4891 elfcpp::R_POWERPC_TPREL
);
4895 unsigned int off
= got
->add_constant(0);
4896 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
4897 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
4898 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
4903 else if (tls_type
== tls::TLSOPT_TO_LE
)
4905 // no GOT relocs needed for Local Exec.
4912 case elfcpp::R_POWERPC_GOT_TLSLD16
:
4913 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
4914 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
4915 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
4917 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
4918 if (tls_type
== tls::TLSOPT_NONE
)
4919 target
->tlsld_got_offset(symtab
, layout
, object
);
4920 else if (tls_type
== tls::TLSOPT_TO_LE
)
4922 // no GOT relocs needed for Local Exec.
4923 if (parameters
->options().emit_relocs())
4925 Output_section
* os
= layout
->tls_segment()->first_section();
4926 gold_assert(os
!= NULL
);
4927 os
->set_needs_symtab_index();
4935 case elfcpp::R_POWERPC_GOT_DTPREL16
:
4936 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
4937 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
4938 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
4940 Output_data_got_powerpc
<size
, big_endian
>* got
4941 = target
->got_section(symtab
, layout
);
4942 if (!gsym
->final_value_is_known()
4943 && (gsym
->is_from_dynobj()
4944 || gsym
->is_undefined()
4945 || gsym
->is_preemptible()))
4946 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
4947 target
->rela_dyn_section(layout
),
4948 elfcpp::R_POWERPC_DTPREL
);
4950 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
4954 case elfcpp::R_POWERPC_GOT_TPREL16
:
4955 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
4956 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
4957 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
4959 const bool final
= gsym
->final_value_is_known();
4960 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
4961 if (tls_type
== tls::TLSOPT_NONE
)
4963 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
4965 Output_data_got_powerpc
<size
, big_endian
>* got
4966 = target
->got_section(symtab
, layout
);
4967 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4968 if (gsym
->is_undefined()
4969 || gsym
->is_from_dynobj())
4971 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
4972 elfcpp::R_POWERPC_TPREL
);
4976 unsigned int off
= got
->add_constant(0);
4977 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
4978 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
4979 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
4984 else if (tls_type
== tls::TLSOPT_TO_LE
)
4986 // no GOT relocs needed for Local Exec.
4994 unsupported_reloc_global(object
, r_type
, gsym
);
4999 // Process relocations for gc.
5001 template<int size
, bool big_endian
>
5003 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
5004 Symbol_table
* symtab
,
5006 Sized_relobj_file
<size
, big_endian
>* object
,
5007 unsigned int data_shndx
,
5009 const unsigned char* prelocs
,
5011 Output_section
* output_section
,
5012 bool needs_special_offset_handling
,
5013 size_t local_symbol_count
,
5014 const unsigned char* plocal_symbols
)
5016 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
5017 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
5018 Powerpc_relobj
<size
, big_endian
>* ppc_object
5019 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5021 ppc_object
->set_opd_valid();
5022 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
5024 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
5025 for (p
= ppc_object
->access_from_map()->begin();
5026 p
!= ppc_object
->access_from_map()->end();
5029 Address dst_off
= p
->first
;
5030 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
5031 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
5032 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
5034 Object
* src_obj
= s
->first
;
5035 unsigned int src_indx
= s
->second
;
5036 symtab
->gc()->add_reference(src_obj
, src_indx
,
5037 ppc_object
, dst_indx
);
5041 ppc_object
->access_from_map()->clear();
5042 ppc_object
->process_gc_mark(symtab
);
5043 // Don't look at .opd relocs as .opd will reference everything.
5047 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
5048 typename
Target_powerpc::Relocatable_size_for_reloc
>(
5057 needs_special_offset_handling
,
5062 // Handle target specific gc actions when adding a gc reference from
5063 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
5064 // and DST_OFF. For powerpc64, this adds a referenc to the code
5065 // section of a function descriptor.
5067 template<int size
, bool big_endian
>
5069 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
5070 Symbol_table
* symtab
,
5072 unsigned int src_shndx
,
5074 unsigned int dst_shndx
,
5075 Address dst_off
) const
5077 Powerpc_relobj
<size
, big_endian
>* ppc_object
5078 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
5080 && !ppc_object
->is_dynamic()
5081 && dst_shndx
== ppc_object
->opd_shndx())
5083 if (ppc_object
->opd_valid())
5085 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
5086 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
5090 // If we haven't run scan_opd_relocs, we must delay
5091 // processing this function descriptor reference.
5092 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
5097 // Add any special sections for this symbol to the gc work list.
5098 // For powerpc64, this adds the code section of a function
5101 template<int size
, bool big_endian
>
5103 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
5104 Symbol_table
* symtab
,
5109 Powerpc_relobj
<size
, big_endian
>* ppc_object
5110 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
5112 unsigned int shndx
= sym
->shndx(&is_ordinary
);
5113 if (is_ordinary
&& shndx
== ppc_object
->opd_shndx())
5115 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
5116 Address dst_off
= gsym
->value();
5117 if (ppc_object
->opd_valid())
5119 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
5120 symtab
->gc()->worklist().push(Section_id(ppc_object
, dst_indx
));
5123 ppc_object
->add_gc_mark(dst_off
);
5128 // Scan relocations for a section.
5130 template<int size
, bool big_endian
>
5132 Target_powerpc
<size
, big_endian
>::scan_relocs(
5133 Symbol_table
* symtab
,
5135 Sized_relobj_file
<size
, big_endian
>* object
,
5136 unsigned int data_shndx
,
5137 unsigned int sh_type
,
5138 const unsigned char* prelocs
,
5140 Output_section
* output_section
,
5141 bool needs_special_offset_handling
,
5142 size_t local_symbol_count
,
5143 const unsigned char* plocal_symbols
)
5145 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
5146 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
5148 if (sh_type
== elfcpp::SHT_REL
)
5150 gold_error(_("%s: unsupported REL reloc section"),
5151 object
->name().c_str());
5155 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
5164 needs_special_offset_handling
,
5169 // Functor class for processing the global symbol table.
5170 // Removes symbols defined on discarded opd entries.
5172 template<bool big_endian
>
5173 class Global_symbol_visitor_opd
5176 Global_symbol_visitor_opd()
5180 operator()(Sized_symbol
<64>* sym
)
5182 if (sym
->has_symtab_index()
5183 || sym
->source() != Symbol::FROM_OBJECT
5184 || !sym
->in_real_elf())
5187 Powerpc_relobj
<64, big_endian
>* symobj
5188 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
5189 if (symobj
->is_dynamic()
5190 || symobj
->opd_shndx() == 0)
5194 unsigned int shndx
= sym
->shndx(&is_ordinary
);
5195 if (shndx
== symobj
->opd_shndx()
5196 && symobj
->get_opd_discard(sym
->value()))
5197 sym
->set_symtab_index(-1U);
5201 template<int size
, bool big_endian
>
5203 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
5205 Symbol_table
* symtab
)
5209 Output_data_save_res
<64, big_endian
>* savres
5210 = new Output_data_save_res
<64, big_endian
>(symtab
);
5211 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
5212 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
5213 savres
, ORDER_TEXT
, false);
5217 // Finalize the sections.
5219 template<int size
, bool big_endian
>
5221 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
5223 const Input_objects
*,
5224 Symbol_table
* symtab
)
5226 if (parameters
->doing_static_link())
5228 // At least some versions of glibc elf-init.o have a strong
5229 // reference to __rela_iplt marker syms. A weak ref would be
5231 if (this->iplt_
!= NULL
)
5233 Reloc_section
* rel
= this->iplt_
->rel_plt();
5234 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
5235 Symbol_table::PREDEFINED
, rel
, 0, 0,
5236 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5237 elfcpp::STV_HIDDEN
, 0, false, true);
5238 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
5239 Symbol_table::PREDEFINED
, rel
, 0, 0,
5240 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5241 elfcpp::STV_HIDDEN
, 0, true, true);
5245 symtab
->define_as_constant("__rela_iplt_start", NULL
,
5246 Symbol_table::PREDEFINED
, 0, 0,
5247 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5248 elfcpp::STV_HIDDEN
, 0, true, false);
5249 symtab
->define_as_constant("__rela_iplt_end", NULL
,
5250 Symbol_table::PREDEFINED
, 0, 0,
5251 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5252 elfcpp::STV_HIDDEN
, 0, true, false);
5258 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
5259 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
5261 if (!parameters
->options().relocatable())
5263 this->define_save_restore_funcs(layout
, symtab
);
5265 // Annoyingly, we need to make these sections now whether or
5266 // not we need them. If we delay until do_relax then we
5267 // need to mess with the relaxation machinery checkpointing.
5268 this->got_section(symtab
, layout
);
5269 this->make_brlt_section(layout
);
5273 // Fill in some more dynamic tags.
5274 Output_data_dynamic
* odyn
= layout
->dynamic_data();
5277 const Reloc_section
* rel_plt
= (this->plt_
== NULL
5279 : this->plt_
->rel_plt());
5280 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
5281 this->rela_dyn_
, true, size
== 32);
5285 if (this->got_
!= NULL
)
5287 this->got_
->finalize_data_size();
5288 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
5289 this->got_
, this->got_
->g_o_t());
5294 if (this->glink_
!= NULL
)
5296 this->glink_
->finalize_data_size();
5297 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
5299 (this->glink_
->pltresolve_size
5305 // Emit any relocs we saved in an attempt to avoid generating COPY
5307 if (this->copy_relocs_
.any_saved_relocs())
5308 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
5311 // Return the value to use for a branch relocation.
5313 template<int size
, bool big_endian
>
5314 typename
elfcpp::Elf_types
<size
>::Elf_Addr
5315 Target_powerpc
<size
, big_endian
>::symval_for_branch(
5317 const Sized_symbol
<size
>* gsym
,
5318 Powerpc_relobj
<size
, big_endian
>* object
,
5319 unsigned int *dest_shndx
)
5325 // If the symbol is defined in an opd section, ie. is a function
5326 // descriptor, use the function descriptor code entry address
5327 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
5329 && gsym
->source() != Symbol::FROM_OBJECT
)
5332 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
5333 unsigned int shndx
= symobj
->opd_shndx();
5336 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
5337 gold_assert(opd_addr
!= invalid_address
);
5338 opd_addr
+= symobj
->output_section(shndx
)->address();
5339 if (value
>= opd_addr
&& value
< opd_addr
+ symobj
->section_size(shndx
))
5342 *dest_shndx
= symobj
->get_opd_ent(value
- opd_addr
, &sec_off
);
5343 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
5344 gold_assert(sec_addr
!= invalid_address
);
5345 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
5346 value
= sec_addr
+ sec_off
;
5351 // Perform a relocation.
5353 template<int size
, bool big_endian
>
5355 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
5356 const Relocate_info
<size
, big_endian
>* relinfo
,
5357 Target_powerpc
* target
,
5360 const elfcpp::Rela
<size
, big_endian
>& rela
,
5361 unsigned int r_type
,
5362 const Sized_symbol
<size
>* gsym
,
5363 const Symbol_value
<size
>* psymval
,
5364 unsigned char* view
,
5366 section_size_type view_size
)
5368 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
5369 || r_type
== elfcpp::R_PPC_PLTREL24
)
5371 && strcmp(gsym
->name(), "__tls_get_addr") == 0);
5372 enum skip_tls last_tls
= this->call_tls_get_addr_
;
5373 this->call_tls_get_addr_
= CALL_NOT_EXPECTED
;
5376 if (last_tls
== CALL_NOT_EXPECTED
)
5377 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5378 _("__tls_get_addr call lacks marker reloc"));
5379 else if (last_tls
== CALL_SKIP
)
5382 else if (last_tls
!= CALL_NOT_EXPECTED
)
5383 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5384 _("missing expected __tls_get_addr call"));
5386 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
5387 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
5388 Powerpc_relobj
<size
, big_endian
>* const object
5389 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
5391 bool has_plt_value
= false;
5392 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5394 ? use_plt_offset
<size
>(gsym
, Scan::get_reference_flags(r_type
))
5395 : object
->local_has_plt_offset(r_sym
))
5397 Stub_table
<size
, big_endian
>* stub_table
5398 = object
->stub_table(relinfo
->data_shndx
);
5399 if (stub_table
== NULL
)
5401 // This is a ref from a data section to an ifunc symbol.
5402 if (target
->stub_tables().size() != 0)
5403 stub_table
= target
->stub_tables()[0];
5405 gold_assert(stub_table
!= NULL
);
5408 off
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
5409 rela
.get_r_addend());
5411 off
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
5412 rela
.get_r_addend());
5413 gold_assert(off
!= invalid_address
);
5414 value
= stub_table
->stub_address() + off
;
5415 has_plt_value
= true;
5418 if (r_type
== elfcpp::R_POWERPC_GOT16
5419 || r_type
== elfcpp::R_POWERPC_GOT16_LO
5420 || r_type
== elfcpp::R_POWERPC_GOT16_HI
5421 || r_type
== elfcpp::R_POWERPC_GOT16_HA
5422 || r_type
== elfcpp::R_PPC64_GOT16_DS
5423 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
5427 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
5428 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
5432 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5433 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
5434 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
5436 value
-= target
->got_section()->got_base_offset(object
);
5438 else if (r_type
== elfcpp::R_PPC64_TOC
)
5440 value
= (target
->got_section()->output_section()->address()
5441 + object
->toc_base_offset());
5443 else if (gsym
!= NULL
5444 && (r_type
== elfcpp::R_POWERPC_REL24
5445 || r_type
== elfcpp::R_PPC_PLTREL24
)
5450 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
5451 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
5452 bool can_plt_call
= false;
5453 if (rela
.get_r_offset() + 8 <= view_size
)
5455 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5456 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
5459 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
5461 elfcpp::Swap
<32, big_endian
>::writeval(wv
+ 1, ld_2_1
+ 40);
5462 can_plt_call
= true;
5467 // If we don't have a branch and link followed by a nop,
5468 // we can't go via the plt because there is no place to
5469 // put a toc restoring instruction.
5470 // Unless we know we won't be returning.
5471 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
5472 can_plt_call
= true;
5476 // This is not an error in one special case: A self
5477 // call. It isn't possible to cheaply verify we have
5478 // such a call so just check for a call to the same
5481 Address code
= value
;
5482 if (gsym
->source() == Symbol::FROM_OBJECT
5483 && gsym
->object() == object
)
5485 Address addend
= rela
.get_r_addend();
5486 unsigned int dest_shndx
;
5487 Address opdent
= psymval
->value(object
, addend
);
5488 code
= target
->symval_for_branch(opdent
, gsym
, object
,
5491 if (dest_shndx
== 0)
5492 dest_shndx
= gsym
->shndx(&is_ordinary
);
5493 ok
= dest_shndx
== relinfo
->data_shndx
;
5497 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5498 _("call lacks nop, can't restore toc; "
5499 "recompile with -fPIC"));
5505 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
5506 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
5507 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
5508 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
5510 // First instruction of a global dynamic sequence, arg setup insn.
5511 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
5512 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
5513 enum Got_type got_type
= GOT_TYPE_STANDARD
;
5514 if (tls_type
== tls::TLSOPT_NONE
)
5515 got_type
= GOT_TYPE_TLSGD
;
5516 else if (tls_type
== tls::TLSOPT_TO_IE
)
5517 got_type
= GOT_TYPE_TPREL
;
5518 if (got_type
!= GOT_TYPE_STANDARD
)
5522 gold_assert(gsym
->has_got_offset(got_type
));
5523 value
= gsym
->got_offset(got_type
);
5527 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5528 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
5529 value
= object
->local_got_offset(r_sym
, got_type
);
5531 value
-= target
->got_section()->got_base_offset(object
);
5533 if (tls_type
== tls::TLSOPT_TO_IE
)
5535 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
5536 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
5538 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
5539 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
5540 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
5542 insn
|= 32 << 26; // lwz
5544 insn
|= 58 << 26; // ld
5545 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5547 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
5548 - elfcpp::R_POWERPC_GOT_TLSGD16
);
5550 else if (tls_type
== tls::TLSOPT_TO_LE
)
5552 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
5553 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
5555 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
5556 Insn insn
= addis_3_13
;
5559 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5560 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
5561 value
= psymval
->value(object
, rela
.get_r_addend());
5565 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
5567 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5568 r_type
= elfcpp::R_POWERPC_NONE
;
5572 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
5573 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
5574 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
5575 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
5577 // First instruction of a local dynamic sequence, arg setup insn.
5578 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5579 if (tls_type
== tls::TLSOPT_NONE
)
5581 value
= target
->tlsld_got_offset();
5582 value
-= target
->got_section()->got_base_offset(object
);
5586 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
5587 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
5588 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
5590 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
5591 Insn insn
= addis_3_13
;
5594 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5595 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
5600 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
5602 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5603 r_type
= elfcpp::R_POWERPC_NONE
;
5607 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
5608 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
5609 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
5610 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
5612 // Accesses relative to a local dynamic sequence address,
5613 // no optimisation here.
5616 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
5617 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
5621 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5622 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
5623 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
5625 value
-= target
->got_section()->got_base_offset(object
);
5627 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
5628 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
5629 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
5630 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
5632 // First instruction of initial exec sequence.
5633 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
5634 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
5635 if (tls_type
== tls::TLSOPT_NONE
)
5639 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
5640 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
5644 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5645 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
5646 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
5648 value
-= target
->got_section()->got_base_offset(object
);
5652 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
5653 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
5654 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
5656 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
5657 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
5658 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
5663 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5664 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
5665 value
= psymval
->value(object
, rela
.get_r_addend());
5669 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
5671 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5672 r_type
= elfcpp::R_POWERPC_NONE
;
5676 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
5677 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
5679 // Second instruction of a global dynamic sequence,
5680 // the __tls_get_addr call
5681 this->call_tls_get_addr_
= CALL_EXPECTED
;
5682 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
5683 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
5684 if (tls_type
!= tls::TLSOPT_NONE
)
5686 if (tls_type
== tls::TLSOPT_TO_IE
)
5688 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
5689 Insn insn
= add_3_3_13
;
5692 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5693 r_type
= elfcpp::R_POWERPC_NONE
;
5697 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
5698 Insn insn
= addi_3_3
;
5699 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5700 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
5701 view
+= 2 * big_endian
;
5702 value
= psymval
->value(object
, rela
.get_r_addend());
5704 this->call_tls_get_addr_
= CALL_SKIP
;
5707 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
5708 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
5710 // Second instruction of a local dynamic sequence,
5711 // the __tls_get_addr call
5712 this->call_tls_get_addr_
= CALL_EXPECTED
;
5713 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5714 if (tls_type
== tls::TLSOPT_TO_LE
)
5716 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
5717 Insn insn
= addi_3_3
;
5718 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5719 this->call_tls_get_addr_
= CALL_SKIP
;
5720 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
5721 view
+= 2 * big_endian
;
5725 else if (r_type
== elfcpp::R_POWERPC_TLS
)
5727 // Second instruction of an initial exec sequence
5728 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
5729 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
5730 if (tls_type
== tls::TLSOPT_TO_LE
)
5732 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
5733 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
5734 unsigned int reg
= size
== 32 ? 2 : 13;
5735 insn
= at_tls_transform(insn
, reg
);
5736 gold_assert(insn
!= 0);
5737 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5738 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
5739 view
+= 2 * big_endian
;
5740 value
= psymval
->value(object
, rela
.get_r_addend());
5743 else if (!has_plt_value
)
5746 unsigned int dest_shndx
;
5747 if (r_type
!= elfcpp::R_PPC_PLTREL24
)
5748 addend
= rela
.get_r_addend();
5749 value
= psymval
->value(object
, addend
);
5750 if (size
== 64 && is_branch_reloc(r_type
))
5751 value
= target
->symval_for_branch(value
, gsym
, object
, &dest_shndx
);
5752 unsigned int max_branch_offset
= 0;
5753 if (r_type
== elfcpp::R_POWERPC_REL24
5754 || r_type
== elfcpp::R_PPC_PLTREL24
5755 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
5756 max_branch_offset
= 1 << 25;
5757 else if (r_type
== elfcpp::R_POWERPC_REL14
5758 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
5759 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
5760 max_branch_offset
= 1 << 15;
5761 if (max_branch_offset
!= 0
5762 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
5764 Stub_table
<size
, big_endian
>* stub_table
5765 = object
->stub_table(relinfo
->data_shndx
);
5766 gold_assert(stub_table
!= NULL
);
5767 Address off
= stub_table
->find_long_branch_entry(object
, value
);
5768 if (off
!= invalid_address
)
5769 value
= stub_table
->stub_address() + stub_table
->plt_size() + off
;
5775 case elfcpp::R_PPC64_REL64
:
5776 case elfcpp::R_POWERPC_REL32
:
5777 case elfcpp::R_POWERPC_REL24
:
5778 case elfcpp::R_PPC_PLTREL24
:
5779 case elfcpp::R_PPC_LOCAL24PC
:
5780 case elfcpp::R_POWERPC_REL16
:
5781 case elfcpp::R_POWERPC_REL16_LO
:
5782 case elfcpp::R_POWERPC_REL16_HI
:
5783 case elfcpp::R_POWERPC_REL16_HA
:
5784 case elfcpp::R_POWERPC_REL14
:
5785 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5786 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5790 case elfcpp::R_PPC64_TOC16
:
5791 case elfcpp::R_PPC64_TOC16_LO
:
5792 case elfcpp::R_PPC64_TOC16_HI
:
5793 case elfcpp::R_PPC64_TOC16_HA
:
5794 case elfcpp::R_PPC64_TOC16_DS
:
5795 case elfcpp::R_PPC64_TOC16_LO_DS
:
5796 // Subtract the TOC base address.
5797 value
-= (target
->got_section()->output_section()->address()
5798 + object
->toc_base_offset());
5801 case elfcpp::R_POWERPC_SECTOFF
:
5802 case elfcpp::R_POWERPC_SECTOFF_LO
:
5803 case elfcpp::R_POWERPC_SECTOFF_HI
:
5804 case elfcpp::R_POWERPC_SECTOFF_HA
:
5805 case elfcpp::R_PPC64_SECTOFF_DS
:
5806 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
5808 value
-= os
->address();
5811 case elfcpp::R_PPC64_TPREL16_DS
:
5812 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5814 // R_PPC_TLSGD and R_PPC_TLSLD
5816 case elfcpp::R_POWERPC_TPREL16
:
5817 case elfcpp::R_POWERPC_TPREL16_LO
:
5818 case elfcpp::R_POWERPC_TPREL16_HI
:
5819 case elfcpp::R_POWERPC_TPREL16_HA
:
5820 case elfcpp::R_POWERPC_TPREL
:
5821 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5822 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5823 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5824 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5825 // tls symbol values are relative to tls_segment()->vaddr()
5829 case elfcpp::R_PPC64_DTPREL16_DS
:
5830 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
5831 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
5832 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
5833 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
5834 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
5836 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
5837 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
5839 case elfcpp::R_POWERPC_DTPREL16
:
5840 case elfcpp::R_POWERPC_DTPREL16_LO
:
5841 case elfcpp::R_POWERPC_DTPREL16_HI
:
5842 case elfcpp::R_POWERPC_DTPREL16_HA
:
5843 case elfcpp::R_POWERPC_DTPREL
:
5844 // tls symbol values are relative to tls_segment()->vaddr()
5845 value
-= dtp_offset
;
5852 Insn branch_bit
= 0;
5855 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5856 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5857 branch_bit
= 1 << 21;
5858 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5859 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5861 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
5862 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
5865 if (this->is_isa_v2
)
5867 // Set 'a' bit. This is 0b00010 in BO field for branch
5868 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
5869 // for branch on CTR insns (BO == 1a00t or 1a01t).
5870 if ((insn
& (0x14 << 21)) == (0x04 << 21))
5872 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
5879 // Invert 'y' bit if not the default.
5880 if (static_cast<Signed_address
>(value
) < 0)
5883 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5891 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
5894 case elfcpp::R_POWERPC_ADDR32
:
5895 case elfcpp::R_POWERPC_UADDR32
:
5897 overflow
= Reloc::CHECK_BITFIELD
;
5900 case elfcpp::R_POWERPC_REL32
:
5902 overflow
= Reloc::CHECK_SIGNED
;
5905 case elfcpp::R_POWERPC_ADDR24
:
5906 case elfcpp::R_POWERPC_ADDR16
:
5907 case elfcpp::R_POWERPC_UADDR16
:
5908 case elfcpp::R_PPC64_ADDR16_DS
:
5909 case elfcpp::R_POWERPC_ADDR14
:
5910 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5911 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5912 overflow
= Reloc::CHECK_BITFIELD
;
5915 case elfcpp::R_POWERPC_REL24
:
5916 case elfcpp::R_PPC_PLTREL24
:
5917 case elfcpp::R_PPC_LOCAL24PC
:
5918 case elfcpp::R_POWERPC_REL16
:
5919 case elfcpp::R_PPC64_TOC16
:
5920 case elfcpp::R_POWERPC_GOT16
:
5921 case elfcpp::R_POWERPC_SECTOFF
:
5922 case elfcpp::R_POWERPC_TPREL16
:
5923 case elfcpp::R_POWERPC_DTPREL16
:
5924 case elfcpp::R_PPC64_TPREL16_DS
:
5925 case elfcpp::R_PPC64_DTPREL16_DS
:
5926 case elfcpp::R_PPC64_TOC16_DS
:
5927 case elfcpp::R_PPC64_GOT16_DS
:
5928 case elfcpp::R_PPC64_SECTOFF_DS
:
5929 case elfcpp::R_POWERPC_REL14
:
5930 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5931 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5932 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5933 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5934 case elfcpp::R_POWERPC_GOT_TPREL16
:
5935 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5936 overflow
= Reloc::CHECK_SIGNED
;
5940 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
5941 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
5944 case elfcpp::R_POWERPC_NONE
:
5945 case elfcpp::R_POWERPC_TLS
:
5946 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5947 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5948 case elfcpp::R_PPC_EMB_MRKREF
:
5951 case elfcpp::R_PPC64_ADDR64
:
5952 case elfcpp::R_PPC64_REL64
:
5953 case elfcpp::R_PPC64_TOC
:
5954 Reloc::addr64(view
, value
);
5957 case elfcpp::R_POWERPC_TPREL
:
5958 case elfcpp::R_POWERPC_DTPREL
:
5960 Reloc::addr64(view
, value
);
5962 status
= Reloc::addr32(view
, value
, overflow
);
5965 case elfcpp::R_PPC64_UADDR64
:
5966 Reloc::addr64_u(view
, value
);
5969 case elfcpp::R_POWERPC_ADDR32
:
5970 status
= Reloc::addr32(view
, value
, overflow
);
5973 case elfcpp::R_POWERPC_REL32
:
5974 case elfcpp::R_POWERPC_UADDR32
:
5975 status
= Reloc::addr32_u(view
, value
, overflow
);
5978 case elfcpp::R_POWERPC_ADDR24
:
5979 case elfcpp::R_POWERPC_REL24
:
5980 case elfcpp::R_PPC_PLTREL24
:
5981 case elfcpp::R_PPC_LOCAL24PC
:
5982 status
= Reloc::addr24(view
, value
, overflow
);
5985 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5986 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
5989 status
= Reloc::addr16_ds(view
, value
, overflow
);
5992 case elfcpp::R_POWERPC_ADDR16
:
5993 case elfcpp::R_POWERPC_REL16
:
5994 case elfcpp::R_PPC64_TOC16
:
5995 case elfcpp::R_POWERPC_GOT16
:
5996 case elfcpp::R_POWERPC_SECTOFF
:
5997 case elfcpp::R_POWERPC_TPREL16
:
5998 case elfcpp::R_POWERPC_DTPREL16
:
5999 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6000 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6001 case elfcpp::R_POWERPC_GOT_TPREL16
:
6002 case elfcpp::R_POWERPC_ADDR16_LO
:
6003 case elfcpp::R_POWERPC_REL16_LO
:
6004 case elfcpp::R_PPC64_TOC16_LO
:
6005 case elfcpp::R_POWERPC_GOT16_LO
:
6006 case elfcpp::R_POWERPC_SECTOFF_LO
:
6007 case elfcpp::R_POWERPC_TPREL16_LO
:
6008 case elfcpp::R_POWERPC_DTPREL16_LO
:
6009 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6010 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6011 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6012 status
= Reloc::addr16(view
, value
, overflow
);
6015 case elfcpp::R_POWERPC_UADDR16
:
6016 status
= Reloc::addr16_u(view
, value
, overflow
);
6019 case elfcpp::R_POWERPC_ADDR16_HI
:
6020 case elfcpp::R_POWERPC_REL16_HI
:
6021 case elfcpp::R_PPC64_TOC16_HI
:
6022 case elfcpp::R_POWERPC_GOT16_HI
:
6023 case elfcpp::R_POWERPC_SECTOFF_HI
:
6024 case elfcpp::R_POWERPC_TPREL16_HI
:
6025 case elfcpp::R_POWERPC_DTPREL16_HI
:
6026 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6027 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6028 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6029 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6030 Reloc::addr16_hi(view
, value
);
6033 case elfcpp::R_POWERPC_ADDR16_HA
:
6034 case elfcpp::R_POWERPC_REL16_HA
:
6035 case elfcpp::R_PPC64_TOC16_HA
:
6036 case elfcpp::R_POWERPC_GOT16_HA
:
6037 case elfcpp::R_POWERPC_SECTOFF_HA
:
6038 case elfcpp::R_POWERPC_TPREL16_HA
:
6039 case elfcpp::R_POWERPC_DTPREL16_HA
:
6040 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6041 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6042 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6043 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6044 Reloc::addr16_ha(view
, value
);
6047 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6049 // R_PPC_EMB_NADDR16_LO
6051 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6052 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6053 Reloc::addr16_hi2(view
, value
);
6056 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6058 // R_PPC_EMB_NADDR16_HI
6060 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6061 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6062 Reloc::addr16_ha2(view
, value
);
6065 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6067 // R_PPC_EMB_NADDR16_HA
6069 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6070 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6071 Reloc::addr16_hi3(view
, value
);
6074 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6078 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6079 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6080 Reloc::addr16_ha3(view
, value
);
6083 case elfcpp::R_PPC64_DTPREL16_DS
:
6084 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6086 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
6088 case elfcpp::R_PPC64_TPREL16_DS
:
6089 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6091 // R_PPC_TLSGD, R_PPC_TLSLD
6093 case elfcpp::R_PPC64_ADDR16_DS
:
6094 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6095 case elfcpp::R_PPC64_TOC16_DS
:
6096 case elfcpp::R_PPC64_TOC16_LO_DS
:
6097 case elfcpp::R_PPC64_GOT16_DS
:
6098 case elfcpp::R_PPC64_GOT16_LO_DS
:
6099 case elfcpp::R_PPC64_SECTOFF_DS
:
6100 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6101 status
= Reloc::addr16_ds(view
, value
, overflow
);
6104 case elfcpp::R_POWERPC_ADDR14
:
6105 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6106 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6107 case elfcpp::R_POWERPC_REL14
:
6108 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6109 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6110 status
= Reloc::addr14(view
, value
, overflow
);
6113 case elfcpp::R_POWERPC_COPY
:
6114 case elfcpp::R_POWERPC_GLOB_DAT
:
6115 case elfcpp::R_POWERPC_JMP_SLOT
:
6116 case elfcpp::R_POWERPC_RELATIVE
:
6117 case elfcpp::R_POWERPC_DTPMOD
:
6118 case elfcpp::R_PPC64_JMP_IREL
:
6119 case elfcpp::R_POWERPC_IRELATIVE
:
6120 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6121 _("unexpected reloc %u in object file"),
6125 case elfcpp::R_PPC_EMB_SDA21
:
6130 // R_PPC64_TOCSAVE. For the time being this can be ignored.
6134 case elfcpp::R_PPC_EMB_SDA2I16
:
6135 case elfcpp::R_PPC_EMB_SDA2REL
:
6138 // R_PPC64_TLSGD, R_PPC64_TLSLD
6141 case elfcpp::R_POWERPC_PLT32
:
6142 case elfcpp::R_POWERPC_PLTREL32
:
6143 case elfcpp::R_POWERPC_PLT16_LO
:
6144 case elfcpp::R_POWERPC_PLT16_HI
:
6145 case elfcpp::R_POWERPC_PLT16_HA
:
6146 case elfcpp::R_PPC_SDAREL16
:
6147 case elfcpp::R_POWERPC_ADDR30
:
6148 case elfcpp::R_PPC64_PLT64
:
6149 case elfcpp::R_PPC64_PLTREL64
:
6150 case elfcpp::R_PPC64_PLTGOT16
:
6151 case elfcpp::R_PPC64_PLTGOT16_LO
:
6152 case elfcpp::R_PPC64_PLTGOT16_HI
:
6153 case elfcpp::R_PPC64_PLTGOT16_HA
:
6154 case elfcpp::R_PPC64_PLT16_LO_DS
:
6155 case elfcpp::R_PPC64_PLTGOT16_DS
:
6156 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
6157 case elfcpp::R_PPC_EMB_RELSEC16
:
6158 case elfcpp::R_PPC_EMB_RELST_LO
:
6159 case elfcpp::R_PPC_EMB_RELST_HI
:
6160 case elfcpp::R_PPC_EMB_RELST_HA
:
6161 case elfcpp::R_PPC_EMB_BIT_FLD
:
6162 case elfcpp::R_PPC_EMB_RELSDA
:
6163 case elfcpp::R_PPC_TOC16
:
6166 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6167 _("unsupported reloc %u"),
6171 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
)
6172 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6173 _("relocation overflow"));
6178 // Relocate section data.
6180 template<int size
, bool big_endian
>
6182 Target_powerpc
<size
, big_endian
>::relocate_section(
6183 const Relocate_info
<size
, big_endian
>* relinfo
,
6184 unsigned int sh_type
,
6185 const unsigned char* prelocs
,
6187 Output_section
* output_section
,
6188 bool needs_special_offset_handling
,
6189 unsigned char* view
,
6191 section_size_type view_size
,
6192 const Reloc_symbol_changes
* reloc_symbol_changes
)
6194 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
6195 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
6196 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
6197 Powerpc_comdat_behavior
;
6199 gold_assert(sh_type
== elfcpp::SHT_RELA
);
6201 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
6202 Powerpc_relocate
, Powerpc_comdat_behavior
>(
6208 needs_special_offset_handling
,
6212 reloc_symbol_changes
);
6215 class Powerpc_scan_relocatable_reloc
6218 // Return the strategy to use for a local symbol which is not a
6219 // section symbol, given the relocation type.
6220 inline Relocatable_relocs::Reloc_strategy
6221 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
6223 if (r_type
== 0 && r_sym
== 0)
6224 return Relocatable_relocs::RELOC_DISCARD
;
6225 return Relocatable_relocs::RELOC_COPY
;
6228 // Return the strategy to use for a local symbol which is a section
6229 // symbol, given the relocation type.
6230 inline Relocatable_relocs::Reloc_strategy
6231 local_section_strategy(unsigned int, Relobj
*)
6233 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
6236 // Return the strategy to use for a global symbol, given the
6237 // relocation type, the object, and the symbol index.
6238 inline Relocatable_relocs::Reloc_strategy
6239 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
6241 if (r_type
== elfcpp::R_PPC_PLTREL24
)
6242 return Relocatable_relocs::RELOC_SPECIAL
;
6243 return Relocatable_relocs::RELOC_COPY
;
6247 // Scan the relocs during a relocatable link.
6249 template<int size
, bool big_endian
>
6251 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
6252 Symbol_table
* symtab
,
6254 Sized_relobj_file
<size
, big_endian
>* object
,
6255 unsigned int data_shndx
,
6256 unsigned int sh_type
,
6257 const unsigned char* prelocs
,
6259 Output_section
* output_section
,
6260 bool needs_special_offset_handling
,
6261 size_t local_symbol_count
,
6262 const unsigned char* plocal_symbols
,
6263 Relocatable_relocs
* rr
)
6265 gold_assert(sh_type
== elfcpp::SHT_RELA
);
6267 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
6268 Powerpc_scan_relocatable_reloc
>(
6276 needs_special_offset_handling
,
6282 // Emit relocations for a section.
6283 // This is a modified version of the function by the same name in
6284 // target-reloc.h. Using relocate_special_relocatable for
6285 // R_PPC_PLTREL24 would require duplication of the entire body of the
6286 // loop, so we may as well duplicate the whole thing.
6288 template<int size
, bool big_endian
>
6290 Target_powerpc
<size
, big_endian
>::relocate_relocs(
6291 const Relocate_info
<size
, big_endian
>* relinfo
,
6292 unsigned int sh_type
,
6293 const unsigned char* prelocs
,
6295 Output_section
* output_section
,
6296 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
6297 const Relocatable_relocs
* rr
,
6299 Address view_address
,
6301 unsigned char* reloc_view
,
6302 section_size_type reloc_view_size
)
6304 gold_assert(sh_type
== elfcpp::SHT_RELA
);
6306 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
6308 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc_write
6310 const int reloc_size
6311 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
6313 Powerpc_relobj
<size
, big_endian
>* const object
6314 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
6315 const unsigned int local_count
= object
->local_symbol_count();
6316 unsigned int got2_shndx
= object
->got2_shndx();
6317 Address got2_addend
= 0;
6318 if (got2_shndx
!= 0)
6320 got2_addend
= object
->get_output_section_offset(got2_shndx
);
6321 gold_assert(got2_addend
!= invalid_address
);
6324 unsigned char* pwrite
= reloc_view
;
6325 bool zap_next
= false;
6326 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
6328 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
6329 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
6332 Reltype
reloc(prelocs
);
6333 Reltype_write
reloc_write(pwrite
);
6335 Address offset
= reloc
.get_r_offset();
6336 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
6337 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
6338 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
6339 const unsigned int orig_r_sym
= r_sym
;
6340 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
6341 = reloc
.get_r_addend();
6342 const Symbol
* gsym
= NULL
;
6346 // We could arrange to discard these and other relocs for
6347 // tls optimised sequences in the strategy methods, but for
6348 // now do as BFD ld does.
6349 r_type
= elfcpp::R_POWERPC_NONE
;
6353 // Get the new symbol index.
6354 if (r_sym
< local_count
)
6358 case Relocatable_relocs::RELOC_COPY
:
6359 case Relocatable_relocs::RELOC_SPECIAL
:
6362 r_sym
= object
->symtab_index(r_sym
);
6363 gold_assert(r_sym
!= -1U);
6367 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
6369 // We are adjusting a section symbol. We need to find
6370 // the symbol table index of the section symbol for
6371 // the output section corresponding to input section
6372 // in which this symbol is defined.
6373 gold_assert(r_sym
< local_count
);
6375 unsigned int shndx
=
6376 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
6377 gold_assert(is_ordinary
);
6378 Output_section
* os
= object
->output_section(shndx
);
6379 gold_assert(os
!= NULL
);
6380 gold_assert(os
->needs_symtab_index());
6381 r_sym
= os
->symtab_index();
6391 gsym
= object
->global_symbol(r_sym
);
6392 gold_assert(gsym
!= NULL
);
6393 if (gsym
->is_forwarder())
6394 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
6396 gold_assert(gsym
->has_symtab_index());
6397 r_sym
= gsym
->symtab_index();
6400 // Get the new offset--the location in the output section where
6401 // this relocation should be applied.
6402 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
6403 offset
+= offset_in_output_section
;
6406 section_offset_type sot_offset
=
6407 convert_types
<section_offset_type
, Address
>(offset
);
6408 section_offset_type new_sot_offset
=
6409 output_section
->output_offset(object
, relinfo
->data_shndx
,
6411 gold_assert(new_sot_offset
!= -1);
6412 offset
= new_sot_offset
;
6415 // In an object file, r_offset is an offset within the section.
6416 // In an executable or dynamic object, generated by
6417 // --emit-relocs, r_offset is an absolute address.
6418 if (!parameters
->options().relocatable())
6420 offset
+= view_address
;
6421 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
6422 offset
-= offset_in_output_section
;
6425 // Handle the reloc addend based on the strategy.
6426 if (strategy
== Relocatable_relocs::RELOC_COPY
)
6428 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
6430 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
6431 addend
= psymval
->value(object
, addend
);
6433 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
6435 if (addend
>= 32768)
6436 addend
+= got2_addend
;
6441 if (!parameters
->options().relocatable())
6443 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6444 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
6445 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
6446 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
6448 // First instruction of a global dynamic sequence,
6450 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6451 switch (this->optimize_tls_gd(final
))
6453 case tls::TLSOPT_TO_IE
:
6454 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
6455 - elfcpp::R_POWERPC_GOT_TLSGD16
);
6457 case tls::TLSOPT_TO_LE
:
6458 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6459 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
6460 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6463 r_type
= elfcpp::R_POWERPC_NONE
;
6464 offset
-= 2 * big_endian
;
6471 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
6472 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
6473 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
6474 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
6476 // First instruction of a local dynamic sequence,
6478 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
6480 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
6481 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
6483 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6484 const Output_section
* os
= relinfo
->layout
->tls_segment()
6486 gold_assert(os
!= NULL
);
6487 gold_assert(os
->needs_symtab_index());
6488 r_sym
= os
->symtab_index();
6489 addend
= dtp_offset
;
6493 r_type
= elfcpp::R_POWERPC_NONE
;
6494 offset
-= 2 * big_endian
;
6498 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
6499 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
6500 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
6501 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
6503 // First instruction of initial exec sequence.
6504 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6505 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
6507 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
6508 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
6509 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6512 r_type
= elfcpp::R_POWERPC_NONE
;
6513 offset
-= 2 * big_endian
;
6517 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6518 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6520 // Second instruction of a global dynamic sequence,
6521 // the __tls_get_addr call
6522 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6523 switch (this->optimize_tls_gd(final
))
6525 case tls::TLSOPT_TO_IE
:
6526 r_type
= elfcpp::R_POWERPC_NONE
;
6529 case tls::TLSOPT_TO_LE
:
6530 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6531 offset
+= 2 * big_endian
;
6538 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6539 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6541 // Second instruction of a local dynamic sequence,
6542 // the __tls_get_addr call
6543 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
6545 const Output_section
* os
= relinfo
->layout
->tls_segment()
6547 gold_assert(os
!= NULL
);
6548 gold_assert(os
->needs_symtab_index());
6549 r_sym
= os
->symtab_index();
6550 addend
= dtp_offset
;
6551 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6552 offset
+= 2 * big_endian
;
6556 else if (r_type
== elfcpp::R_POWERPC_TLS
)
6558 // Second instruction of an initial exec sequence
6559 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6560 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
6562 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6563 offset
+= 2 * big_endian
;
6568 reloc_write
.put_r_offset(offset
);
6569 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
6570 reloc_write
.put_r_addend(addend
);
6572 pwrite
+= reloc_size
;
6575 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
6576 == reloc_view_size
);
6579 // Return the value to use for a dynamic symbol which requires special
6580 // treatment. This is how we support equality comparisons of function
6581 // pointers across shared library boundaries, as described in the
6582 // processor specific ABI supplement.
6584 template<int size
, bool big_endian
>
6586 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
6590 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
6591 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
6592 p
!= this->stub_tables_
.end();
6595 Address off
= (*p
)->find_plt_call_entry(gsym
);
6596 if (off
!= invalid_address
)
6597 return (*p
)->stub_address() + off
;
6603 // Return the PLT address to use for a local symbol.
6604 template<int size
, bool big_endian
>
6606 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
6607 const Relobj
* object
,
6608 unsigned int symndx
) const
6612 const Sized_relobj
<size
, big_endian
>* relobj
6613 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
6614 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
6615 p
!= this->stub_tables_
.end();
6618 Address off
= (*p
)->find_plt_call_entry(relobj
->sized_relobj(),
6620 if (off
!= invalid_address
)
6621 return (*p
)->stub_address() + off
;
6627 // Return the PLT address to use for a global symbol.
6628 template<int size
, bool big_endian
>
6630 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
6631 const Symbol
* gsym
) const
6635 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
6636 p
!= this->stub_tables_
.end();
6639 Address off
= (*p
)->find_plt_call_entry(gsym
);
6640 if (off
!= invalid_address
)
6641 return (*p
)->stub_address() + off
;
6647 // Return the offset to use for the GOT_INDX'th got entry which is
6648 // for a local tls symbol specified by OBJECT, SYMNDX.
6649 template<int size
, bool big_endian
>
6651 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
6652 const Relobj
* object
,
6653 unsigned int symndx
,
6654 unsigned int got_indx
) const
6656 const Powerpc_relobj
<size
, big_endian
>* ppc_object
6657 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
6658 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
6660 for (Got_type got_type
= GOT_TYPE_TLSGD
;
6661 got_type
<= GOT_TYPE_TPREL
;
6662 got_type
= Got_type(got_type
+ 1))
6663 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
6665 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
6666 if (got_type
== GOT_TYPE_TLSGD
)
6668 if (off
== got_indx
* (size
/ 8))
6670 if (got_type
== GOT_TYPE_TPREL
)
6680 // Return the offset to use for the GOT_INDX'th got entry which is
6681 // for global tls symbol GSYM.
6682 template<int size
, bool big_endian
>
6684 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
6686 unsigned int got_indx
) const
6688 if (gsym
->type() == elfcpp::STT_TLS
)
6690 for (Got_type got_type
= GOT_TYPE_TLSGD
;
6691 got_type
<= GOT_TYPE_TPREL
;
6692 got_type
= Got_type(got_type
+ 1))
6693 if (gsym
->has_got_offset(got_type
))
6695 unsigned int off
= gsym
->got_offset(got_type
);
6696 if (got_type
== GOT_TYPE_TLSGD
)
6698 if (off
== got_indx
* (size
/ 8))
6700 if (got_type
== GOT_TYPE_TPREL
)
6710 // The selector for powerpc object files.
6712 template<int size
, bool big_endian
>
6713 class Target_selector_powerpc
: public Target_selector
6716 Target_selector_powerpc()
6717 : Target_selector(elfcpp::EM_NONE
, size
, big_endian
,
6719 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
6720 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
6722 ? (big_endian
? "elf64ppc" : "elf64lppc")
6723 : (big_endian
? "elf32ppc" : "elf32lppc")))
6727 do_recognize(Input_file
*, off_t
, int machine
, int, int)
6732 if (machine
!= elfcpp::EM_PPC64
)
6737 if (machine
!= elfcpp::EM_PPC
)
6745 return this->instantiate_target();
6749 do_instantiate_target()
6750 { return new Target_powerpc
<size
, big_endian
>(); }
6753 Target_selector_powerpc
<32, true> target_selector_ppc32
;
6754 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
6755 Target_selector_powerpc
<64, true> target_selector_ppc64
;
6756 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
6758 } // End anonymous namespace.