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.
29 #include "parameters.h"
36 #include "copy-relocs.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
49 template<int size
, bool big_endian
>
50 class Output_data_plt_powerpc
;
52 template<int size
, bool big_endian
>
53 class Output_data_brlt_powerpc
;
55 template<int size
, bool big_endian
>
56 class Output_data_got_powerpc
;
58 template<int size
, bool big_endian
>
59 class Output_data_glink
;
61 template<int size
, bool big_endian
>
64 template<int size
, bool big_endian
>
65 class Powerpc_relobj
: public Sized_relobj_file
<size
, big_endian
>
68 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
69 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_refs
;
70 typedef Unordered_map
<Address
, Section_refs
> Access_from
;
72 Powerpc_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
73 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
74 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
75 special_(0), has_small_toc_reloc_(false), opd_valid_(false),
76 opd_ent_(), access_from_map_(), has14_(), stub_table_()
82 // The .got2 section shndx.
87 return this->special_
;
92 // The .opd section shndx.
99 return this->special_
;
102 // Init OPD entry arrays.
104 init_opd(size_t opd_size
)
106 size_t count
= this->opd_ent_ndx(opd_size
);
107 this->opd_ent_
.resize(count
);
110 // Return section and offset of function entry for .opd + R_OFF.
112 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
114 size_t ndx
= this->opd_ent_ndx(r_off
);
115 gold_assert(ndx
< this->opd_ent_
.size());
116 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
118 *value
= this->opd_ent_
[ndx
].off
;
119 return this->opd_ent_
[ndx
].shndx
;
122 // Set section and offset of function entry for .opd + R_OFF.
124 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
126 size_t ndx
= this->opd_ent_ndx(r_off
);
127 gold_assert(ndx
< this->opd_ent_
.size());
128 this->opd_ent_
[ndx
].shndx
= shndx
;
129 this->opd_ent_
[ndx
].off
= value
;
132 // Return discard flag for .opd + R_OFF.
134 get_opd_discard(Address r_off
) const
136 size_t ndx
= this->opd_ent_ndx(r_off
);
137 gold_assert(ndx
< this->opd_ent_
.size());
138 return this->opd_ent_
[ndx
].discard
;
141 // Set discard flag for .opd + R_OFF.
143 set_opd_discard(Address r_off
)
145 size_t ndx
= this->opd_ent_ndx(r_off
);
146 gold_assert(ndx
< this->opd_ent_
.size());
147 this->opd_ent_
[ndx
].discard
= true;
152 { return this->opd_valid_
; }
156 { this->opd_valid_
= true; }
158 // Examine .rela.opd to build info about function entry points.
160 scan_opd_relocs(size_t reloc_count
,
161 const unsigned char* prelocs
,
162 const unsigned char* plocal_syms
);
164 // Perform the Sized_relobj_file method, then set up opd info from
167 do_read_relocs(Read_relocs_data
*);
170 do_find_special_sections(Read_symbols_data
* sd
);
172 // Adjust this local symbol value. Return false if the symbol
173 // should be discarded from the output file.
175 do_adjust_local_symbol(Symbol_value
<size
>* lv
) const
177 if (size
== 64 && this->opd_shndx() != 0)
180 if (lv
->input_shndx(&is_ordinary
) != this->opd_shndx())
182 if (this->get_opd_discard(lv
->input_value()))
190 { return &this->access_from_map_
; }
192 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
193 // section at DST_OFF.
195 add_reference(Object
* src_obj
,
196 unsigned int src_indx
,
197 typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
199 Section_id
src_id(src_obj
, src_indx
);
200 this->access_from_map_
[dst_off
].insert(src_id
);
203 // Add a reference to the code section specified by the .opd entry
206 add_gc_mark(typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
208 size_t ndx
= this->opd_ent_ndx(dst_off
);
209 if (ndx
>= this->opd_ent_
.size())
210 this->opd_ent_
.resize(ndx
+ 1);
211 this->opd_ent_
[ndx
].gc_mark
= true;
215 process_gc_mark(Symbol_table
* symtab
)
217 for (size_t i
= 0; i
< this->opd_ent_
.size(); i
++)
218 if (this->opd_ent_
[i
].gc_mark
)
220 unsigned int shndx
= this->opd_ent_
[i
].shndx
;
221 symtab
->gc()->worklist().push(Section_id(this, shndx
));
225 // Return offset in output GOT section that this object will use
226 // as a TOC pointer. Won't be just a constant with multi-toc support.
228 toc_base_offset() const
232 set_has_small_toc_reloc()
233 { has_small_toc_reloc_
= true; }
236 has_small_toc_reloc() const
237 { return has_small_toc_reloc_
; }
240 set_has_14bit_branch(unsigned int shndx
)
242 if (shndx
>= this->has14_
.size())
243 this->has14_
.resize(shndx
+ 1);
244 this->has14_
[shndx
] = true;
248 has_14bit_branch(unsigned int shndx
) const
249 { return shndx
< this->has14_
.size() && this->has14_
[shndx
]; }
252 set_stub_table(unsigned int shndx
, Stub_table
<size
, big_endian
>* stub_table
)
254 if (shndx
>= this->stub_table_
.size())
255 this->stub_table_
.resize(shndx
+ 1);
256 this->stub_table_
[shndx
] = stub_table
;
259 Stub_table
<size
, big_endian
>*
260 stub_table(unsigned int shndx
)
262 if (shndx
< this->stub_table_
.size())
263 return this->stub_table_
[shndx
];
276 // Return index into opd_ent_ array for .opd entry at OFF.
277 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
278 // apart when the language doesn't use the last 8-byte word, the
279 // environment pointer. Thus dividing the entry section offset by
280 // 16 will give an index into opd_ent_ that works for either layout
281 // of .opd. (It leaves some elements of the vector unused when .opd
282 // entries are spaced 24 bytes apart, but we don't know the spacing
283 // until relocations are processed, and in any case it is possible
284 // for an object to have some entries spaced 16 bytes apart and
285 // others 24 bytes apart.)
287 opd_ent_ndx(size_t off
) const
290 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
291 unsigned int special_
;
293 // For 64-bit, whether this object uses small model relocs to access
295 bool has_small_toc_reloc_
;
297 // Set at the start of gc_process_relocs, when we know opd_ent_
298 // vector is valid. The flag could be made atomic and set in
299 // do_read_relocs with memory_order_release and then tested with
300 // memory_order_acquire, potentially resulting in fewer entries in
304 // The first 8-byte word of an OPD entry gives the address of the
305 // entry point of the function. Relocatable object files have a
306 // relocation on this word. The following vector records the
307 // section and offset specified by these relocations.
308 std::vector
<Opd_ent
> opd_ent_
;
310 // References made to this object's .opd section when running
311 // gc_process_relocs for another object, before the opd_ent_ vector
312 // is valid for this object.
313 Access_from access_from_map_
;
315 // Whether input section has a 14-bit branch reloc.
316 std::vector
<bool> has14_
;
318 // The stub table to use for a given input section.
319 std::vector
<Stub_table
<size
, big_endian
>*> stub_table_
;
322 template<int size
, bool big_endian
>
323 class Target_powerpc
: public Sized_target
<size
, big_endian
>
327 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
328 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
329 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Signed_address
;
330 static const Address invalid_address
= static_cast<Address
>(0) - 1;
331 // Offset of tp and dtp pointers from start of TLS block.
332 static const Address tp_offset
= 0x7000;
333 static const Address dtp_offset
= 0x8000;
336 : Sized_target
<size
, big_endian
>(&powerpc_info
),
337 got_(NULL
), plt_(NULL
), iplt_(NULL
), brlt_section_(NULL
),
338 glink_(NULL
), rela_dyn_(NULL
), copy_relocs_(elfcpp::R_POWERPC_COPY
),
339 dynbss_(NULL
), tlsld_got_offset_(-1U),
340 stub_tables_(), branch_lookup_table_(), branch_info_(),
341 plt_thread_safe_(false)
345 // Process the relocations to determine unreferenced sections for
346 // garbage collection.
348 gc_process_relocs(Symbol_table
* symtab
,
350 Sized_relobj_file
<size
, big_endian
>* object
,
351 unsigned int data_shndx
,
352 unsigned int sh_type
,
353 const unsigned char* prelocs
,
355 Output_section
* output_section
,
356 bool needs_special_offset_handling
,
357 size_t local_symbol_count
,
358 const unsigned char* plocal_symbols
);
360 // Scan the relocations to look for symbol adjustments.
362 scan_relocs(Symbol_table
* symtab
,
364 Sized_relobj_file
<size
, big_endian
>* object
,
365 unsigned int data_shndx
,
366 unsigned int sh_type
,
367 const unsigned char* prelocs
,
369 Output_section
* output_section
,
370 bool needs_special_offset_handling
,
371 size_t local_symbol_count
,
372 const unsigned char* plocal_symbols
);
374 // Map input .toc section to output .got section.
376 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
378 if (size
== 64 && strcmp(name
, ".toc") == 0)
386 // Provide linker defined save/restore functions.
388 define_save_restore_funcs(Layout
*, Symbol_table
*);
390 // No stubs unless a final link.
393 { return !parameters
->options().relocatable(); }
396 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
399 do_plt_fde_location(const Output_data
*, unsigned char*,
400 uint64_t*, off_t
*) const;
402 // Stash info about branches, for stub generation.
404 push_branch(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
405 unsigned int data_shndx
, Address r_offset
,
406 unsigned int r_type
, unsigned int r_sym
, Address addend
)
408 Branch_info
info(ppc_object
, data_shndx
, r_offset
, r_type
, r_sym
, addend
);
409 this->branch_info_
.push_back(info
);
410 if (r_type
== elfcpp::R_POWERPC_REL14
411 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
412 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
413 ppc_object
->set_has_14bit_branch(data_shndx
);
416 Stub_table
<size
, big_endian
>*
420 do_define_standard_symbols(Symbol_table
*, Layout
*);
422 // Finalize the sections.
424 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
426 // Return the value to use for a dynamic which requires special
429 do_dynsym_value(const Symbol
*) const;
431 // Return the PLT address to use for a local symbol.
433 do_plt_address_for_local(const Relobj
*, unsigned int) const;
435 // Return the PLT address to use for a global symbol.
437 do_plt_address_for_global(const Symbol
*) const;
439 // Return the offset to use for the GOT_INDX'th got entry which is
440 // for a local tls symbol specified by OBJECT, SYMNDX.
442 do_tls_offset_for_local(const Relobj
* object
,
444 unsigned int got_indx
) const;
446 // Return the offset to use for the GOT_INDX'th got entry which is
447 // for global tls symbol GSYM.
449 do_tls_offset_for_global(Symbol
* gsym
, unsigned int got_indx
) const;
451 // Relocate a section.
453 relocate_section(const Relocate_info
<size
, big_endian
>*,
454 unsigned int sh_type
,
455 const unsigned char* prelocs
,
457 Output_section
* output_section
,
458 bool needs_special_offset_handling
,
460 Address view_address
,
461 section_size_type view_size
,
462 const Reloc_symbol_changes
*);
464 // Scan the relocs during a relocatable link.
466 scan_relocatable_relocs(Symbol_table
* symtab
,
468 Sized_relobj_file
<size
, big_endian
>* object
,
469 unsigned int data_shndx
,
470 unsigned int sh_type
,
471 const unsigned char* prelocs
,
473 Output_section
* output_section
,
474 bool needs_special_offset_handling
,
475 size_t local_symbol_count
,
476 const unsigned char* plocal_symbols
,
477 Relocatable_relocs
*);
479 // Emit relocations for a section.
481 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
482 unsigned int sh_type
,
483 const unsigned char* prelocs
,
485 Output_section
* output_section
,
486 typename
elfcpp::Elf_types
<size
>::Elf_Off
487 offset_in_output_section
,
488 const Relocatable_relocs
*,
490 Address view_address
,
492 unsigned char* reloc_view
,
493 section_size_type reloc_view_size
);
495 // Return whether SYM is defined by the ABI.
497 do_is_defined_by_abi(const Symbol
* sym
) const
499 return strcmp(sym
->name(), "__tls_get_addr") == 0;
502 // Return the size of the GOT section.
506 gold_assert(this->got_
!= NULL
);
507 return this->got_
->data_size();
510 // Get the PLT section.
511 const Output_data_plt_powerpc
<size
, big_endian
>*
514 gold_assert(this->plt_
!= NULL
);
518 // Get the IPLT section.
519 const Output_data_plt_powerpc
<size
, big_endian
>*
522 gold_assert(this->iplt_
!= NULL
);
526 // Get the .glink section.
527 const Output_data_glink
<size
, big_endian
>*
528 glink_section() const
530 gold_assert(this->glink_
!= NULL
);
534 bool has_glink() const
535 { return this->glink_
!= NULL
; }
537 // Get the GOT section.
538 const Output_data_got_powerpc
<size
, big_endian
>*
541 gold_assert(this->got_
!= NULL
);
545 // Get the GOT section, creating it if necessary.
546 Output_data_got_powerpc
<size
, big_endian
>*
547 got_section(Symbol_table
*, Layout
*);
550 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
551 const elfcpp::Ehdr
<size
, big_endian
>&);
553 // Return the number of entries in the GOT.
555 got_entry_count() const
557 if (this->got_
== NULL
)
559 return this->got_size() / (size
/ 8);
562 // Return the number of entries in the PLT.
564 plt_entry_count() const;
566 // Return the offset of the first non-reserved PLT entry.
568 first_plt_entry_offset() const;
570 // Return the size of each PLT entry.
572 plt_entry_size() const;
574 // Add any special sections for this symbol to the gc work list.
575 // For powerpc64, this adds the code section of a function
578 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
580 // Handle target specific gc actions when adding a gc reference from
581 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
582 // and DST_OFF. For powerpc64, this adds a referenc to the code
583 // section of a function descriptor.
585 do_gc_add_reference(Symbol_table
* symtab
,
587 unsigned int src_shndx
,
589 unsigned int dst_shndx
,
590 Address dst_off
) const;
592 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
595 { return this->stub_tables_
; }
597 const Output_data_brlt_powerpc
<size
, big_endian
>*
599 { return this->brlt_section_
; }
602 add_branch_lookup_table(Address to
)
604 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
605 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
609 find_branch_lookup_table(Address to
)
611 typename
Branch_lookup_table::const_iterator p
612 = this->branch_lookup_table_
.find(to
);
613 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
617 write_branch_lookup_table(unsigned char *oview
)
619 for (typename
Branch_lookup_table::const_iterator p
620 = this->branch_lookup_table_
.begin();
621 p
!= this->branch_lookup_table_
.end();
624 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
629 plt_thread_safe() const
630 { return this->plt_thread_safe_
; }
646 : tls_get_addr_(NOT_EXPECTED
),
647 relinfo_(NULL
), relnum_(0), r_offset_(0)
652 if (this->tls_get_addr_
!= NOT_EXPECTED
)
659 if (this->relinfo_
!= NULL
)
660 gold_error_at_location(this->relinfo_
, this->relnum_
, this->r_offset_
,
661 _("missing expected __tls_get_addr call"));
665 expect_tls_get_addr_call(
666 const Relocate_info
<size
, big_endian
>* relinfo
,
670 this->tls_get_addr_
= EXPECTED
;
671 this->relinfo_
= relinfo
;
672 this->relnum_
= relnum
;
673 this->r_offset_
= r_offset
;
677 expect_tls_get_addr_call()
678 { this->tls_get_addr_
= EXPECTED
; }
681 skip_next_tls_get_addr_call()
682 {this->tls_get_addr_
= SKIP
; }
685 maybe_skip_tls_get_addr_call(unsigned int r_type
, const Symbol
* gsym
)
687 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
688 || r_type
== elfcpp::R_PPC_PLTREL24
)
690 && strcmp(gsym
->name(), "__tls_get_addr") == 0);
691 Tls_get_addr last_tls
= this->tls_get_addr_
;
692 this->tls_get_addr_
= NOT_EXPECTED
;
693 if (is_tls_call
&& last_tls
!= EXPECTED
)
695 else if (!is_tls_call
&& last_tls
!= NOT_EXPECTED
)
704 // What we're up to regarding calls to __tls_get_addr.
705 // On powerpc, the branch and link insn making a call to
706 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
707 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
708 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
709 // The marker relocation always comes first, and has the same
710 // symbol as the reloc on the insn setting up the __tls_get_addr
711 // argument. This ties the arg setup insn with the call insn,
712 // allowing ld to safely optimize away the call. We check that
713 // every call to __tls_get_addr has a marker relocation, and that
714 // every marker relocation is on a call to __tls_get_addr.
715 Tls_get_addr tls_get_addr_
;
716 // Info about the last reloc for error message.
717 const Relocate_info
<size
, big_endian
>* relinfo_
;
722 // The class which scans relocations.
723 class Scan
: protected Track_tls
726 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
729 : Track_tls(), issued_non_pic_error_(false)
733 get_reference_flags(unsigned int r_type
);
736 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
737 Sized_relobj_file
<size
, big_endian
>* object
,
738 unsigned int data_shndx
,
739 Output_section
* output_section
,
740 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
741 const elfcpp::Sym
<size
, big_endian
>& lsym
,
745 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
746 Sized_relobj_file
<size
, big_endian
>* object
,
747 unsigned int data_shndx
,
748 Output_section
* output_section
,
749 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
753 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
755 Sized_relobj_file
<size
, big_endian
>* ,
758 const elfcpp::Rela
<size
, big_endian
>& ,
760 const elfcpp::Sym
<size
, big_endian
>&)
764 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
766 Sized_relobj_file
<size
, big_endian
>* ,
769 const elfcpp::Rela
<size
,
771 unsigned int , Symbol
*)
776 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
777 unsigned int r_type
);
780 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
781 unsigned int r_type
, Symbol
*);
784 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
785 Target_powerpc
* target
);
788 check_non_pic(Relobj
*, unsigned int r_type
);
791 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, big_endian
>* object
,
792 unsigned int r_type
);
794 // Whether we have issued an error about a non-PIC compilation.
795 bool issued_non_pic_error_
;
799 symval_for_branch(const Symbol_table
* symtab
, Address value
,
800 const Sized_symbol
<size
>* gsym
,
801 Powerpc_relobj
<size
, big_endian
>* object
,
802 unsigned int *dest_shndx
);
804 // The class which implements relocation.
805 class Relocate
: protected Track_tls
808 // Use 'at' branch hints when true, 'y' when false.
809 // FIXME maybe: set this with an option.
810 static const bool is_isa_v2
= true;
816 // Do a relocation. Return false if the caller should not issue
817 // any warnings about this relocation.
819 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
820 Output_section
*, size_t relnum
,
821 const elfcpp::Rela
<size
, big_endian
>&,
822 unsigned int r_type
, const Sized_symbol
<size
>*,
823 const Symbol_value
<size
>*,
825 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
829 class Relocate_comdat_behavior
832 // Decide what the linker should do for relocations that refer to
833 // discarded comdat sections.
834 inline Comdat_behavior
835 get(const char* name
)
837 gold::Default_comdat_behavior default_behavior
;
838 Comdat_behavior ret
= default_behavior
.get(name
);
839 if (ret
== CB_WARNING
)
842 && (strcmp(name
, ".fixup") == 0
843 || strcmp(name
, ".got2") == 0))
846 && (strcmp(name
, ".opd") == 0
847 || strcmp(name
, ".toc") == 0
848 || strcmp(name
, ".toc1") == 0))
855 // A class which returns the size required for a relocation type,
856 // used while scanning relocs during a relocatable link.
857 class Relocatable_size_for_reloc
861 get_size_for_reloc(unsigned int, Relobj
*)
868 // Optimize the TLS relocation type based on what we know about the
869 // symbol. IS_FINAL is true if the final address of this symbol is
870 // known at link time.
872 tls::Tls_optimization
873 optimize_tls_gd(bool is_final
)
875 // If we are generating a shared library, then we can't do anything
877 if (parameters
->options().shared())
878 return tls::TLSOPT_NONE
;
881 return tls::TLSOPT_TO_IE
;
882 return tls::TLSOPT_TO_LE
;
885 tls::Tls_optimization
888 if (parameters
->options().shared())
889 return tls::TLSOPT_NONE
;
891 return tls::TLSOPT_TO_LE
;
894 tls::Tls_optimization
895 optimize_tls_ie(bool is_final
)
897 if (!is_final
|| parameters
->options().shared())
898 return tls::TLSOPT_NONE
;
900 return tls::TLSOPT_TO_LE
;
905 make_glink_section(Layout
*);
907 // Create the PLT section.
909 make_plt_section(Symbol_table
*, Layout
*);
912 make_iplt_section(Symbol_table
*, Layout
*);
915 make_brlt_section(Layout
*);
917 // Create a PLT entry for a global symbol.
919 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
921 // Create a PLT entry for a local IFUNC symbol.
923 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
924 Sized_relobj_file
<size
, big_endian
>*,
928 // Create a GOT entry for local dynamic __tls_get_addr.
930 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
931 Sized_relobj_file
<size
, big_endian
>* object
);
934 tlsld_got_offset() const
936 return this->tlsld_got_offset_
;
939 // Get the dynamic reloc section, creating it if necessary.
941 rela_dyn_section(Layout
*);
943 // Copy a relocation against a global symbol.
945 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
946 Sized_relobj_file
<size
, big_endian
>* object
,
947 unsigned int shndx
, Output_section
* output_section
,
948 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
950 this->copy_relocs_
.copy_reloc(symtab
, layout
,
951 symtab
->get_sized_symbol
<size
>(sym
),
952 object
, shndx
, output_section
,
953 reloc
, this->rela_dyn_section(layout
));
956 // Look over all the input sections, deciding where to place stub.
958 group_sections(Layout
*, const Task
*);
960 // Sort output sections by address.
964 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
965 { return sec1
->address() < sec2
->address(); }
971 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
972 unsigned int data_shndx
,
977 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
978 r_type_(r_type
), r_sym_(r_sym
), addend_(addend
)
984 // If this branch needs a plt call stub, or a long branch stub, make one.
986 make_stub(Stub_table
<size
, big_endian
>*,
987 Stub_table
<size
, big_endian
>*,
988 Symbol_table
*) const;
991 // The branch location..
992 Powerpc_relobj
<size
, big_endian
>* object_
;
995 // ..and the branch type and destination.
996 unsigned int r_type_
;
1001 // Information about this specific target which we pass to the
1002 // general Target structure.
1003 static Target::Target_info powerpc_info
;
1005 // The types of GOT entries needed for this platform.
1006 // These values are exposed to the ABI in an incremental link.
1007 // Do not renumber existing values without changing the version
1008 // number of the .gnu_incremental_inputs section.
1012 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
1013 GOT_TYPE_DTPREL
, // entry for @got@dtprel
1014 GOT_TYPE_TPREL
// entry for @got@tprel
1018 Output_data_got_powerpc
<size
, big_endian
>* got_
;
1020 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
1021 // The IPLT section.
1022 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
1023 // Section holding long branch destinations.
1024 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
1025 // The .glink section.
1026 Output_data_glink
<size
, big_endian
>* glink_
;
1027 // The dynamic reloc section.
1028 Reloc_section
* rela_dyn_
;
1029 // Relocs saved to avoid a COPY reloc.
1030 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
1031 // Space for variables copied with a COPY reloc.
1032 Output_data_space
* dynbss_
;
1033 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1034 unsigned int tlsld_got_offset_
;
1036 Stub_tables stub_tables_
;
1037 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
1038 Branch_lookup_table branch_lookup_table_
;
1040 typedef std::vector
<Branch_info
> Branches
;
1041 Branches branch_info_
;
1043 bool plt_thread_safe_
;
1047 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
1050 true, // is_big_endian
1051 elfcpp::EM_PPC
, // machine_code
1052 false, // has_make_symbol
1053 false, // has_resolve
1054 false, // has_code_fill
1055 true, // is_default_stack_executable
1056 false, // can_icf_inline_merge_sections
1058 "/usr/lib/ld.so.1", // dynamic_linker
1059 0x10000000, // default_text_segment_address
1060 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1061 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1062 false, // isolate_execinstr
1064 elfcpp::SHN_UNDEF
, // small_common_shndx
1065 elfcpp::SHN_UNDEF
, // large_common_shndx
1066 0, // small_common_section_flags
1067 0, // large_common_section_flags
1068 NULL
, // attributes_section
1069 NULL
// attributes_vendor
1073 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
1076 false, // is_big_endian
1077 elfcpp::EM_PPC
, // machine_code
1078 false, // has_make_symbol
1079 false, // has_resolve
1080 false, // has_code_fill
1081 true, // is_default_stack_executable
1082 false, // can_icf_inline_merge_sections
1084 "/usr/lib/ld.so.1", // dynamic_linker
1085 0x10000000, // default_text_segment_address
1086 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1087 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1088 false, // isolate_execinstr
1090 elfcpp::SHN_UNDEF
, // small_common_shndx
1091 elfcpp::SHN_UNDEF
, // large_common_shndx
1092 0, // small_common_section_flags
1093 0, // large_common_section_flags
1094 NULL
, // attributes_section
1095 NULL
// attributes_vendor
1099 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1102 true, // is_big_endian
1103 elfcpp::EM_PPC64
, // machine_code
1104 false, // has_make_symbol
1105 false, // has_resolve
1106 false, // has_code_fill
1107 true, // is_default_stack_executable
1108 false, // can_icf_inline_merge_sections
1110 "/usr/lib/ld.so.1", // dynamic_linker
1111 0x10000000, // default_text_segment_address
1112 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1113 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1114 false, // isolate_execinstr
1116 elfcpp::SHN_UNDEF
, // small_common_shndx
1117 elfcpp::SHN_UNDEF
, // large_common_shndx
1118 0, // small_common_section_flags
1119 0, // large_common_section_flags
1120 NULL
, // attributes_section
1121 NULL
// attributes_vendor
1125 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1128 false, // is_big_endian
1129 elfcpp::EM_PPC64
, // machine_code
1130 false, // has_make_symbol
1131 false, // has_resolve
1132 false, // has_code_fill
1133 true, // is_default_stack_executable
1134 false, // can_icf_inline_merge_sections
1136 "/usr/lib/ld.so.1", // dynamic_linker
1137 0x10000000, // default_text_segment_address
1138 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1139 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1140 false, // isolate_execinstr
1142 elfcpp::SHN_UNDEF
, // small_common_shndx
1143 elfcpp::SHN_UNDEF
, // large_common_shndx
1144 0, // small_common_section_flags
1145 0, // large_common_section_flags
1146 NULL
, // attributes_section
1147 NULL
// attributes_vendor
1151 is_branch_reloc(unsigned int r_type
)
1153 return (r_type
== elfcpp::R_POWERPC_REL24
1154 || r_type
== elfcpp::R_PPC_PLTREL24
1155 || r_type
== elfcpp::R_PPC_LOCAL24PC
1156 || r_type
== elfcpp::R_POWERPC_REL14
1157 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1158 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1159 || r_type
== elfcpp::R_POWERPC_ADDR24
1160 || r_type
== elfcpp::R_POWERPC_ADDR14
1161 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1162 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1165 // If INSN is an opcode that may be used with an @tls operand, return
1166 // the transformed insn for TLS optimisation, otherwise return 0. If
1167 // REG is non-zero only match an insn with RB or RA equal to REG.
1169 at_tls_transform(uint32_t insn
, unsigned int reg
)
1171 if ((insn
& (0x3f << 26)) != 31 << 26)
1175 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1176 rtra
= insn
& ((1 << 26) - (1 << 16));
1177 else if (((insn
>> 16) & 0x1f) == reg
)
1178 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1182 if ((insn
& (0x3ff << 1)) == 266 << 1)
1185 else if ((insn
& (0x1f << 1)) == 23 << 1
1186 && ((insn
& (0x1f << 6)) < 14 << 6
1187 || ((insn
& (0x1f << 6)) >= 16 << 6
1188 && (insn
& (0x1f << 6)) < 24 << 6)))
1189 // load and store indexed -> dform
1190 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1191 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1192 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1193 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1194 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1196 insn
= (58 << 26) | 2;
1203 // Modified version of symtab.h class Symbol member
1204 // Given a direct absolute or pc-relative static relocation against
1205 // the global symbol, this function returns whether a dynamic relocation
1210 needs_dynamic_reloc(const Symbol
* gsym
, int flags
)
1212 // No dynamic relocations in a static link!
1213 if (parameters
->doing_static_link())
1216 // A reference to an undefined symbol from an executable should be
1217 // statically resolved to 0, and does not need a dynamic relocation.
1218 // This matches gnu ld behavior.
1219 if (gsym
->is_undefined() && !parameters
->options().shared())
1222 // A reference to an absolute symbol does not need a dynamic relocation.
1223 if (gsym
->is_absolute())
1226 // An absolute reference within a position-independent output file
1227 // will need a dynamic relocation.
1228 if ((flags
& Symbol::ABSOLUTE_REF
)
1229 && parameters
->options().output_is_position_independent())
1232 // A function call that can branch to a local PLT entry does not need
1233 // a dynamic relocation.
1234 if ((flags
& Symbol::FUNCTION_CALL
) && gsym
->has_plt_offset())
1237 // A reference to any PLT entry in a non-position-independent executable
1238 // does not need a dynamic relocation.
1239 // Except due to having function descriptors on powerpc64 we don't define
1240 // functions to their plt code in an executable, so this doesn't apply.
1242 && !parameters
->options().output_is_position_independent()
1243 && gsym
->has_plt_offset())
1246 // A reference to a symbol defined in a dynamic object or to a
1247 // symbol that is preemptible will need a dynamic relocation.
1248 if (gsym
->is_from_dynobj()
1249 || gsym
->is_undefined()
1250 || gsym
->is_preemptible())
1253 // For all other cases, return FALSE.
1257 // Modified version of symtab.h class Symbol member
1258 // Whether we should use the PLT offset associated with a symbol for
1259 // a relocation. FLAGS is a set of Reference_flags.
1263 use_plt_offset(const Symbol
* gsym
, int flags
)
1265 // If the symbol doesn't have a PLT offset, then naturally we
1266 // don't want to use it.
1267 if (!gsym
->has_plt_offset())
1270 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
1271 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1274 // If we are going to generate a dynamic relocation, then we will
1275 // wind up using that, so no need to use the PLT entry.
1276 if (needs_dynamic_reloc
<size
>(gsym
, flags
))
1279 // If the symbol is from a dynamic object, we need to use the PLT
1281 if (gsym
->is_from_dynobj())
1284 // If we are generating a shared object, and this symbol is
1285 // undefined or preemptible, we need to use the PLT entry.
1286 if (parameters
->options().shared()
1287 && (gsym
->is_undefined() || gsym
->is_preemptible()))
1290 // If this is a call to a weak undefined symbol, we need to use
1291 // the PLT entry; the symbol may be defined by a library loaded
1293 if ((flags
& Symbol::FUNCTION_CALL
) && gsym
->is_weak_undefined())
1296 // Otherwise we can use the regular definition.
1300 template<int size
, bool big_endian
>
1301 class Powerpc_relocate_functions
1318 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1319 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1321 template<int valsize
>
1323 has_overflow_signed(Address value
)
1325 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1326 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1327 limit
<<= ((valsize
- 1) >> 1);
1328 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1329 return value
+ limit
> (limit
<< 1) - 1;
1332 template<int valsize
>
1334 has_overflow_bitfield(Address value
)
1336 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1337 limit
<<= ((valsize
- 1) >> 1);
1338 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1339 return value
> (limit
<< 1) - 1 && value
+ limit
> (limit
<< 1) - 1;
1342 template<int valsize
>
1343 static inline Status
1344 overflowed(Address value
, Overflow_check overflow
)
1346 if (overflow
== CHECK_SIGNED
)
1348 if (has_overflow_signed
<valsize
>(value
))
1349 return STATUS_OVERFLOW
;
1351 else if (overflow
== CHECK_BITFIELD
)
1353 if (has_overflow_bitfield
<valsize
>(value
))
1354 return STATUS_OVERFLOW
;
1359 // Do a simple RELA relocation
1360 template<int valsize
>
1361 static inline Status
1362 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1364 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1365 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1366 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
);
1367 return overflowed
<valsize
>(value
, overflow
);
1370 template<int valsize
>
1371 static inline Status
1372 rela(unsigned char* view
,
1373 unsigned int right_shift
,
1374 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
1376 Overflow_check overflow
)
1378 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1379 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1380 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
1381 Valtype reloc
= value
>> right_shift
;
1384 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
1385 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1388 // Do a simple RELA relocation, unaligned.
1389 template<int valsize
>
1390 static inline Status
1391 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1393 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
, value
);
1394 return overflowed
<valsize
>(value
, overflow
);
1397 template<int valsize
>
1398 static inline Status
1399 rela_ua(unsigned char* view
,
1400 unsigned int right_shift
,
1401 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
1403 Overflow_check overflow
)
1405 typedef typename
elfcpp::Swap_unaligned
<valsize
, big_endian
>::Valtype
1407 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(view
);
1408 Valtype reloc
= value
>> right_shift
;
1411 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
, val
| reloc
);
1412 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1416 // R_PPC64_ADDR64: (Symbol + Addend)
1418 addr64(unsigned char* view
, Address value
)
1419 { This::template rela
<64>(view
, value
, CHECK_NONE
); }
1421 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1423 addr64_u(unsigned char* view
, Address value
)
1424 { This::template rela_ua
<64>(view
, value
, CHECK_NONE
); }
1426 // R_POWERPC_ADDR32: (Symbol + Addend)
1427 static inline Status
1428 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
1429 { return This::template rela
<32>(view
, value
, overflow
); }
1431 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1432 static inline Status
1433 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1434 { return This::template rela_ua
<32>(view
, value
, overflow
); }
1436 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1437 static inline Status
1438 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
1440 Status stat
= This::template rela
<32>(view
, 0, 0x03fffffc, value
, overflow
);
1441 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1442 stat
= STATUS_OVERFLOW
;
1446 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1447 static inline Status
1448 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
1449 { return This::template rela
<16>(view
, value
, overflow
); }
1451 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1452 static inline Status
1453 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1454 { return This::template rela_ua
<16>(view
, value
, overflow
); }
1456 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1457 static inline Status
1458 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
1460 Status stat
= This::template rela
<16>(view
, 0, 0xfffc, value
, overflow
);
1461 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1462 stat
= STATUS_OVERFLOW
;
1466 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1468 addr16_hi(unsigned char* view
, Address value
)
1469 { This::template rela
<16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
1471 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1473 addr16_ha(unsigned char* view
, Address value
)
1474 { This::addr16_hi(view
, value
+ 0x8000); }
1476 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1478 addr16_hi2(unsigned char* view
, Address value
)
1479 { This::template rela
<16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
1481 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1483 addr16_ha2(unsigned char* view
, Address value
)
1484 { This::addr16_hi2(view
, value
+ 0x8000); }
1486 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1488 addr16_hi3(unsigned char* view
, Address value
)
1489 { This::template rela
<16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
1491 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1493 addr16_ha3(unsigned char* view
, Address value
)
1494 { This::addr16_hi3(view
, value
+ 0x8000); }
1496 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1497 static inline Status
1498 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
1500 Status stat
= This::template rela
<32>(view
, 0, 0xfffc, value
, overflow
);
1501 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1502 stat
= STATUS_OVERFLOW
;
1507 // Stash away the index of .got2 or .opd in a relocatable object, if
1508 // such a section exists.
1510 template<int size
, bool big_endian
>
1512 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
1513 Read_symbols_data
* sd
)
1515 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1516 const unsigned char* namesu
= sd
->section_names
->data();
1517 const char* names
= reinterpret_cast<const char*>(namesu
);
1518 section_size_type names_size
= sd
->section_names_size
;
1519 const unsigned char* s
;
1521 s
= this->find_shdr(pshdrs
, size
== 32 ? ".got2" : ".opd",
1522 names
, names_size
, NULL
);
1525 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
1526 this->special_
= ndx
;
1528 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
1531 // Examine .rela.opd to build info about function entry points.
1533 template<int size
, bool big_endian
>
1535 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
1537 const unsigned char* prelocs
,
1538 const unsigned char* plocal_syms
)
1542 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
1544 const int reloc_size
1545 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
1546 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1547 Address expected_off
= 0;
1548 bool regular
= true;
1549 unsigned int opd_ent_size
= 0;
1551 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
1553 Reltype
reloc(prelocs
);
1554 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
1555 = reloc
.get_r_info();
1556 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
1557 if (r_type
== elfcpp::R_PPC64_ADDR64
)
1559 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1560 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
1563 if (r_sym
< this->local_symbol_count())
1565 typename
elfcpp::Sym
<size
, big_endian
>
1566 lsym(plocal_syms
+ r_sym
* sym_size
);
1567 shndx
= lsym
.get_st_shndx();
1568 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1569 value
= lsym
.get_st_value();
1572 shndx
= this->symbol_section_and_value(r_sym
, &value
,
1574 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
1575 value
+ reloc
.get_r_addend());
1578 expected_off
= reloc
.get_r_offset();
1579 opd_ent_size
= expected_off
;
1581 else if (expected_off
!= reloc
.get_r_offset())
1583 expected_off
+= opd_ent_size
;
1585 else if (r_type
== elfcpp::R_PPC64_TOC
)
1587 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
1592 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1593 this->name().c_str(), r_type
);
1597 if (reloc_count
<= 2)
1598 opd_ent_size
= this->section_size(this->opd_shndx());
1599 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
1603 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1604 this->name().c_str());
1610 template<int size
, bool big_endian
>
1612 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
1614 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
1617 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
1618 p
!= rd
->relocs
.end();
1621 if (p
->data_shndx
== this->opd_shndx())
1623 uint64_t opd_size
= this->section_size(this->opd_shndx());
1624 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
1627 this->init_opd(opd_size
);
1628 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
1629 rd
->local_symbols
->data());
1637 // Set up some symbols.
1639 template<int size
, bool big_endian
>
1641 Target_powerpc
<size
, big_endian
>::do_define_standard_symbols(
1642 Symbol_table
* symtab
,
1647 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
1648 // undefined when scanning relocs (and thus requires
1649 // non-relative dynamic relocs). The proper value will be
1651 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
1652 if (gotsym
!= NULL
&& gotsym
->is_undefined())
1654 Target_powerpc
<size
, big_endian
>* target
=
1655 static_cast<Target_powerpc
<size
, big_endian
>*>(
1656 parameters
->sized_target
<size
, big_endian
>());
1657 Output_data_got_powerpc
<size
, big_endian
>* got
1658 = target
->got_section(symtab
, layout
);
1659 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1660 Symbol_table::PREDEFINED
,
1664 elfcpp::STV_HIDDEN
, 0,
1668 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
1669 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
1670 if (sdasym
!= NULL
&& sdasym
->is_undefined())
1672 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
1674 = layout
->add_output_section_data(".sdata", 0,
1676 | elfcpp::SHF_WRITE
,
1677 sdata
, ORDER_SMALL_DATA
, false);
1678 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
1679 Symbol_table::PREDEFINED
,
1680 os
, 32768, 0, elfcpp::STT_OBJECT
,
1681 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
1687 // Set up PowerPC target specific relobj.
1689 template<int size
, bool big_endian
>
1691 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
1692 const std::string
& name
,
1693 Input_file
* input_file
,
1694 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
1696 int et
= ehdr
.get_e_type();
1697 // ET_EXEC files are valid input for --just-symbols/-R,
1698 // and we treat them as relocatable objects.
1699 if (et
== elfcpp::ET_REL
1700 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
1702 Powerpc_relobj
<size
, big_endian
>* obj
=
1703 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1707 else if (et
== elfcpp::ET_DYN
)
1709 Sized_dynobj
<size
, big_endian
>* obj
=
1710 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1716 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
1721 template<int size
, bool big_endian
>
1722 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
1725 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1726 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1728 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
1729 : Output_data_got
<size
, big_endian
>(),
1730 symtab_(symtab
), layout_(layout
),
1731 header_ent_cnt_(size
== 32 ? 3 : 1),
1732 header_index_(size
== 32 ? 0x2000 : 0)
1737 // Create a new GOT entry and return its offset.
1739 add_got_entry(Got_entry got_entry
)
1741 this->reserve_ent();
1742 return Output_data_got
<size
, big_endian
>::add_got_entry(got_entry
);
1745 // Create a pair of new GOT entries and return the offset of the first.
1747 add_got_entry_pair(Got_entry got_entry_1
, Got_entry got_entry_2
)
1749 this->reserve_ent(2);
1750 return Output_data_got
<size
, big_endian
>::add_got_entry_pair(got_entry_1
,
1755 add_constant_pair(Valtype c1
, Valtype c2
)
1757 this->reserve_ent(2);
1758 unsigned int got_offset
= this->add_constant(c1
);
1759 this->add_constant(c2
);
1763 // Offset of _GLOBAL_OFFSET_TABLE_.
1767 return this->got_offset(this->header_index_
);
1770 // Offset of base used to access the GOT/TOC.
1771 // The got/toc pointer reg will be set to this value.
1773 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
1776 return this->g_o_t();
1778 return (this->output_section()->address()
1779 + object
->toc_base_offset()
1783 // Ensure our GOT has a header.
1785 set_final_data_size()
1787 if (this->header_ent_cnt_
!= 0)
1788 this->make_header();
1789 Output_data_got
<size
, big_endian
>::set_final_data_size();
1792 // First word of GOT header needs some values that are not
1793 // handled by Output_data_got so poke them in here.
1794 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1796 do_write(Output_file
* of
)
1799 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
1800 val
= this->layout_
->dynamic_section()->address();
1802 val
= this->output_section()->address() + 0x8000;
1803 this->replace_constant(this->header_index_
, val
);
1804 Output_data_got
<size
, big_endian
>::do_write(of
);
1809 reserve_ent(unsigned int cnt
= 1)
1811 if (this->header_ent_cnt_
== 0)
1813 if (this->num_entries() + cnt
> this->header_index_
)
1814 this->make_header();
1820 this->header_ent_cnt_
= 0;
1821 this->header_index_
= this->num_entries();
1824 Output_data_got
<size
, big_endian
>::add_constant(0);
1825 Output_data_got
<size
, big_endian
>::add_constant(0);
1826 Output_data_got
<size
, big_endian
>::add_constant(0);
1828 // Define _GLOBAL_OFFSET_TABLE_ at the header
1829 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
1832 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
1833 sym
->set_value(this->g_o_t());
1836 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1837 Symbol_table::PREDEFINED
,
1838 this, this->g_o_t(), 0,
1841 elfcpp::STV_HIDDEN
, 0,
1845 Output_data_got
<size
, big_endian
>::add_constant(0);
1848 // Stashed pointers.
1849 Symbol_table
* symtab_
;
1853 unsigned int header_ent_cnt_
;
1854 // GOT header index.
1855 unsigned int header_index_
;
1858 // Get the GOT section, creating it if necessary.
1860 template<int size
, bool big_endian
>
1861 Output_data_got_powerpc
<size
, big_endian
>*
1862 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
1865 if (this->got_
== NULL
)
1867 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1870 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
1872 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1873 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1874 this->got_
, ORDER_DATA
, false);
1880 // Get the dynamic reloc section, creating it if necessary.
1882 template<int size
, bool big_endian
>
1883 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
1884 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
1886 if (this->rela_dyn_
== NULL
)
1888 gold_assert(layout
!= NULL
);
1889 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1890 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1891 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1892 ORDER_DYNAMIC_RELOCS
, false);
1894 return this->rela_dyn_
;
1900 // Determine the stub group size. The group size is the absolute
1901 // value of the parameter --stub-group-size. If --stub-group-size
1902 // is passed a negative value, we restrict stubs to be always before
1903 // the stubbed branches.
1904 Stub_control(int32_t size
)
1905 : state_(NO_GROUP
), stub_group_size_(abs(size
)),
1906 stub14_group_size_(abs(size
)),
1907 stubs_always_before_branch_(size
< 0), suppress_size_errors_(false),
1908 group_end_addr_(0), owner_(NULL
), output_section_(NULL
)
1910 if (stub_group_size_
== 1)
1913 if (stubs_always_before_branch_
)
1915 stub_group_size_
= 0x1e00000;
1916 stub14_group_size_
= 0x7800;
1920 stub_group_size_
= 0x1c00000;
1921 stub14_group_size_
= 0x7000;
1923 suppress_size_errors_
= true;
1927 // Return true iff input section can be handled by current stub
1930 can_add_to_stub_group(Output_section
* o
,
1931 const Output_section::Input_section
* i
,
1934 const Output_section::Input_section
*
1940 { return output_section_
; }
1946 FINDING_STUB_SECTION
,
1951 uint32_t stub_group_size_
;
1952 uint32_t stub14_group_size_
;
1953 bool stubs_always_before_branch_
;
1954 bool suppress_size_errors_
;
1955 uint64_t group_end_addr_
;
1956 const Output_section::Input_section
* owner_
;
1957 Output_section
* output_section_
;
1960 // Return true iff input section can be handled by current stub/
1964 Stub_control::can_add_to_stub_group(Output_section
* o
,
1965 const Output_section::Input_section
* i
,
1969 = has14
? this->stub14_group_size_
: this->stub_group_size_
;
1970 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
1972 uint64_t start_addr
= o
->address();
1975 // .init and .fini sections are pasted together to form a single
1976 // function. We can't be adding stubs in the middle of the function.
1977 this_size
= o
->data_size();
1980 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
1981 this_size
= i
->data_size();
1983 uint64_t end_addr
= start_addr
+ this_size
;
1984 bool toobig
= this_size
> group_size
;
1986 if (toobig
&& !this->suppress_size_errors_
)
1987 gold_warning(_("%s:%s exceeds group size"),
1988 i
->relobj()->name().c_str(),
1989 i
->relobj()->section_name(i
->shndx()).c_str());
1991 if (this->state_
!= HAS_STUB_SECTION
1992 && (!whole_sec
|| this->output_section_
!= o
))
1995 this->output_section_
= o
;
1998 if (this->state_
== NO_GROUP
)
2000 this->state_
= FINDING_STUB_SECTION
;
2001 this->group_end_addr_
= end_addr
;
2003 else if (this->group_end_addr_
- start_addr
< group_size
)
2005 // Adding this section would make the group larger than GROUP_SIZE.
2006 else if (this->state_
== FINDING_STUB_SECTION
2007 && !this->stubs_always_before_branch_
2010 // But wait, there's more! Input sections up to GROUP_SIZE
2011 // bytes before the stub table can be handled by it too.
2012 this->state_
= HAS_STUB_SECTION
;
2013 this->group_end_addr_
= end_addr
;
2017 this->state_
= NO_GROUP
;
2023 // Look over all the input sections, deciding where to place stubs.
2025 template<int size
, bool big_endian
>
2027 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
2030 Stub_control
stub_control(parameters
->options().stub_group_size());
2032 // Group input sections and insert stub table
2033 Stub_table
<size
, big_endian
>* stub_table
= NULL
;
2034 Layout::Section_list section_list
;
2035 layout
->get_executable_sections(§ion_list
);
2036 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
2037 for (Layout::Section_list::reverse_iterator o
= section_list
.rbegin();
2038 o
!= section_list
.rend();
2041 typedef Output_section::Input_section_list Input_section_list
;
2042 for (Input_section_list::const_reverse_iterator i
2043 = (*o
)->input_sections().rbegin();
2044 i
!= (*o
)->input_sections().rend();
2047 if (i
->is_input_section())
2049 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2050 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2051 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
2052 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
2054 stub_table
->init(stub_control
.owner(),
2055 stub_control
.output_section());
2058 if (stub_table
== NULL
)
2059 stub_table
= this->new_stub_table();
2060 ppcobj
->set_stub_table(i
->shndx(), stub_table
);
2064 if (stub_table
!= NULL
)
2065 stub_table
->init(stub_control
.owner(), stub_control
.output_section());
2068 // If this branch needs a plt call stub, or a long branch stub, make one.
2070 template<int size
, bool big_endian
>
2072 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
2073 Stub_table
<size
, big_endian
>* stub_table
,
2074 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
2075 Symbol_table
* symtab
) const
2077 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
2078 if (sym
!= NULL
&& sym
->is_forwarder())
2079 sym
= symtab
->resolve_forwards(sym
);
2080 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
2082 ? use_plt_offset
<size
>(gsym
, Scan::get_reference_flags(this->r_type_
))
2083 : this->object_
->local_has_plt_offset(this->r_sym_
))
2085 if (stub_table
== NULL
)
2086 stub_table
= this->object_
->stub_table(this->shndx_
);
2087 if (stub_table
== NULL
)
2089 // This is a ref from a data section to an ifunc symbol.
2090 stub_table
= ifunc_stub_table
;
2092 gold_assert(stub_table
!= NULL
);
2094 stub_table
->add_plt_call_entry(this->object_
, gsym
,
2095 this->r_type_
, this->addend_
);
2097 stub_table
->add_plt_call_entry(this->object_
, this->r_sym_
,
2098 this->r_type_
, this->addend_
);
2102 unsigned int max_branch_offset
;
2103 if (this->r_type_
== elfcpp::R_POWERPC_REL14
2104 || this->r_type_
== elfcpp::R_POWERPC_REL14_BRTAKEN
2105 || this->r_type_
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
2106 max_branch_offset
= 1 << 15;
2107 else if (this->r_type_
== elfcpp::R_POWERPC_REL24
2108 || this->r_type_
== elfcpp::R_PPC_PLTREL24
2109 || this->r_type_
== elfcpp::R_PPC_LOCAL24PC
)
2110 max_branch_offset
= 1 << 25;
2113 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
2114 gold_assert(from
!= invalid_address
);
2115 from
+= (this->object_
->output_section(this->shndx_
)->address()
2120 switch (gsym
->source())
2122 case Symbol::FROM_OBJECT
:
2124 Object
* symobj
= gsym
->object();
2125 if (symobj
->is_dynamic()
2126 || symobj
->pluginobj() != NULL
)
2129 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
2130 if (shndx
== elfcpp::SHN_UNDEF
)
2135 case Symbol::IS_UNDEFINED
:
2141 Symbol_table::Compute_final_value_status status
;
2142 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
2143 if (status
!= Symbol_table::CFVS_OK
)
2148 const Symbol_value
<size
>* psymval
2149 = this->object_
->local_symbol(this->r_sym_
);
2150 Symbol_value
<size
> symval
;
2151 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
2152 typename
ObjType::Compute_final_local_value_status status
2153 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
2155 if (status
!= ObjType::CFLV_OK
2156 || !symval
.has_output_value())
2158 to
= symval
.value(this->object_
, 0);
2160 to
+= this->addend_
;
2161 if (stub_table
== NULL
)
2162 stub_table
= this->object_
->stub_table(this->shndx_
);
2163 gold_assert(stub_table
!= NULL
);
2164 if (size
== 64 && is_branch_reloc(this->r_type_
))
2166 unsigned int dest_shndx
;
2167 to
= stub_table
->targ()->symval_for_branch(symtab
, to
, gsym
,
2171 Address delta
= to
- from
;
2172 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
2174 stub_table
->add_long_branch_entry(this->object_
, to
);
2179 // Relaxation hook. This is where we do stub generation.
2181 template<int size
, bool big_endian
>
2183 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
2184 const Input_objects
*,
2185 Symbol_table
* symtab
,
2189 unsigned int prev_brlt_size
= 0;
2192 bool thread_safe
= parameters
->options().plt_thread_safe();
2193 if (size
== 64 && !parameters
->options().user_set_plt_thread_safe())
2195 static const char* const thread_starter
[] =
2199 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2201 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2202 "mq_notify", "create_timer",
2206 "GOMP_parallel_start",
2207 "GOMP_parallel_loop_static_start",
2208 "GOMP_parallel_loop_dynamic_start",
2209 "GOMP_parallel_loop_guided_start",
2210 "GOMP_parallel_loop_runtime_start",
2211 "GOMP_parallel_sections_start",
2214 if (parameters
->options().shared())
2218 for (unsigned int i
= 0;
2219 i
< sizeof(thread_starter
) / sizeof(thread_starter
[0]);
2222 Symbol
* sym
= symtab
->lookup(thread_starter
[i
], NULL
);
2223 thread_safe
= (sym
!= NULL
2225 && sym
->in_real_elf());
2231 this->plt_thread_safe_
= thread_safe
;
2232 this->group_sections(layout
, task
);
2235 // We need address of stub tables valid for make_stub.
2236 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2237 p
!= this->stub_tables_
.end();
2240 const Powerpc_relobj
<size
, big_endian
>* object
2241 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
2242 Address off
= object
->get_output_section_offset((*p
)->shndx());
2243 gold_assert(off
!= invalid_address
);
2244 Output_section
* os
= (*p
)->output_section();
2245 (*p
)->set_address_and_size(os
, off
);
2250 // Clear plt call stubs, long branch stubs and branch lookup table.
2251 prev_brlt_size
= this->branch_lookup_table_
.size();
2252 this->branch_lookup_table_
.clear();
2253 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2254 p
!= this->stub_tables_
.end();
2257 (*p
)->clear_stubs();
2261 // Build all the stubs.
2262 Stub_table
<size
, big_endian
>* ifunc_stub_table
2263 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
2264 Stub_table
<size
, big_endian
>* one_stub_table
2265 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
2266 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
2267 b
!= this->branch_info_
.end();
2270 b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
);
2273 // Did anything change size?
2274 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
2275 bool again
= num_huge_branches
!= prev_brlt_size
;
2276 if (size
== 64 && num_huge_branches
!= 0)
2277 this->make_brlt_section(layout
);
2278 if (size
== 64 && again
)
2279 this->brlt_section_
->set_current_size(num_huge_branches
);
2281 typedef Unordered_set
<Output_section
*> Output_sections
;
2282 Output_sections os_need_update
;
2283 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2284 p
!= this->stub_tables_
.end();
2287 if ((*p
)->size_update())
2290 (*p
)->add_eh_frame(layout
);
2291 os_need_update
.insert((*p
)->output_section());
2295 // Set output section offsets for all input sections in an output
2296 // section that just changed size. Anything past the stubs will
2298 for (typename
Output_sections::iterator p
= os_need_update
.begin();
2299 p
!= os_need_update
.end();
2302 Output_section
* os
= *p
;
2304 typedef Output_section::Input_section_list Input_section_list
;
2305 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
2306 i
!= os
->input_sections().end();
2309 off
= align_address(off
, i
->addralign());
2310 if (i
->is_input_section() || i
->is_relaxed_input_section())
2311 i
->relobj()->set_section_offset(i
->shndx(), off
);
2312 if (i
->is_relaxed_input_section())
2314 Stub_table
<size
, big_endian
>* stub_table
2315 = static_cast<Stub_table
<size
, big_endian
>*>(
2316 i
->relaxed_input_section());
2317 off
+= stub_table
->set_address_and_size(os
, off
);
2320 off
+= i
->data_size();
2322 // If .brlt is part of this output section, then we have just
2323 // done the offset adjustment.
2324 os
->clear_section_offsets_need_adjustment();
2329 && num_huge_branches
!= 0
2330 && parameters
->options().output_is_position_independent())
2332 // Fill in the BRLT relocs.
2333 this->brlt_section_
->reset_data_size();
2334 for (typename
Branch_lookup_table::const_iterator p
2335 = this->branch_lookup_table_
.begin();
2336 p
!= this->branch_lookup_table_
.end();
2339 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
2341 this->brlt_section_
->finalize_data_size();
2346 template<int size
, bool big_endian
>
2348 Target_powerpc
<size
, big_endian
>::do_plt_fde_location(const Output_data
* plt
,
2349 unsigned char* oview
,
2353 uint64_t address
= plt
->address();
2354 off_t len
= plt
->data_size();
2356 if (plt
== this->glink_
)
2358 // See Output_data_glink::do_write() for glink contents.
2361 // There is one word before __glink_PLTresolve
2365 else if (parameters
->options().output_is_position_independent())
2367 // There are two FDEs for a position independent glink.
2368 // The first covers the branch table, the second
2369 // __glink_PLTresolve at the end of glink.
2370 off_t resolve_size
= this->glink_
->pltresolve_size
;
2372 len
-= resolve_size
;
2375 address
+= len
- resolve_size
;
2382 // Must be a stub table.
2383 const Stub_table
<size
, big_endian
>* stub_table
2384 = static_cast<const Stub_table
<size
, big_endian
>*>(plt
);
2385 uint64_t stub_address
= stub_table
->stub_address();
2386 len
-= stub_address
- address
;
2387 address
= stub_address
;
2390 *paddress
= address
;
2394 // A class to handle the PLT data.
2396 template<int size
, bool big_endian
>
2397 class Output_data_plt_powerpc
: public Output_section_data_build
2400 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
2401 size
, big_endian
> Reloc_section
;
2403 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
2404 Reloc_section
* plt_rel
,
2405 unsigned int reserved_size
,
2407 : Output_section_data_build(size
== 32 ? 4 : 8),
2410 initial_plt_entry_size_(reserved_size
),
2414 // Add an entry to the PLT.
2419 add_ifunc_entry(Symbol
*);
2422 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
2424 // Return the .rela.plt section data.
2431 // Return the number of PLT entries.
2435 return ((this->current_data_size() - this->initial_plt_entry_size_
)
2439 // Return the offset of the first non-reserved PLT entry.
2441 first_plt_entry_offset()
2442 { return this->initial_plt_entry_size_
; }
2444 // Return the size of a PLT entry.
2446 get_plt_entry_size()
2447 { return plt_entry_size
; }
2451 do_adjust_output_section(Output_section
* os
)
2456 // Write to a map file.
2458 do_print_to_mapfile(Mapfile
* mapfile
) const
2459 { mapfile
->print_output_data(this, this->name_
); }
2462 // The size of an entry in the PLT.
2463 static const int plt_entry_size
= size
== 32 ? 4 : 24;
2465 // Write out the PLT data.
2467 do_write(Output_file
*);
2469 // The reloc section.
2470 Reloc_section
* rel_
;
2471 // Allows access to .glink for do_write.
2472 Target_powerpc
<size
, big_endian
>* targ_
;
2473 // The size of the first reserved entry.
2474 int initial_plt_entry_size_
;
2475 // What to report in map file.
2479 // Add an entry to the PLT.
2481 template<int size
, bool big_endian
>
2483 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
2485 if (!gsym
->has_plt_offset())
2487 section_size_type off
= this->current_data_size();
2489 off
+= this->first_plt_entry_offset();
2490 gsym
->set_plt_offset(off
);
2491 gsym
->set_needs_dynsym_entry();
2492 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
2493 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
2494 off
+= plt_entry_size
;
2495 this->set_current_data_size(off
);
2499 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
2501 template<int size
, bool big_endian
>
2503 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
2505 if (!gsym
->has_plt_offset())
2507 section_size_type off
= this->current_data_size();
2508 gsym
->set_plt_offset(off
);
2509 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
2511 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
2512 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
2513 off
+= plt_entry_size
;
2514 this->set_current_data_size(off
);
2518 // Add an entry for a local ifunc symbol to the IPLT.
2520 template<int size
, bool big_endian
>
2522 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
2523 Sized_relobj_file
<size
, big_endian
>* relobj
,
2524 unsigned int local_sym_index
)
2526 if (!relobj
->local_has_plt_offset(local_sym_index
))
2528 section_size_type off
= this->current_data_size();
2529 relobj
->set_local_plt_offset(local_sym_index
, off
);
2530 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
2532 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
2533 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
2535 off
+= plt_entry_size
;
2536 this->set_current_data_size(off
);
2540 static const uint32_t add_0_11_11
= 0x7c0b5a14;
2541 static const uint32_t add_2_2_11
= 0x7c425a14;
2542 static const uint32_t add_3_3_2
= 0x7c631214;
2543 static const uint32_t add_3_3_13
= 0x7c636a14;
2544 static const uint32_t add_11_0_11
= 0x7d605a14;
2545 static const uint32_t add_12_2_11
= 0x7d825a14;
2546 static const uint32_t add_12_12_11
= 0x7d8c5a14;
2547 static const uint32_t addi_11_11
= 0x396b0000;
2548 static const uint32_t addi_12_12
= 0x398c0000;
2549 static const uint32_t addi_2_2
= 0x38420000;
2550 static const uint32_t addi_3_2
= 0x38620000;
2551 static const uint32_t addi_3_3
= 0x38630000;
2552 static const uint32_t addis_0_2
= 0x3c020000;
2553 static const uint32_t addis_0_13
= 0x3c0d0000;
2554 static const uint32_t addis_11_11
= 0x3d6b0000;
2555 static const uint32_t addis_11_30
= 0x3d7e0000;
2556 static const uint32_t addis_12_12
= 0x3d8c0000;
2557 static const uint32_t addis_12_2
= 0x3d820000;
2558 static const uint32_t addis_3_2
= 0x3c620000;
2559 static const uint32_t addis_3_13
= 0x3c6d0000;
2560 static const uint32_t b
= 0x48000000;
2561 static const uint32_t bcl_20_31
= 0x429f0005;
2562 static const uint32_t bctr
= 0x4e800420;
2563 static const uint32_t blr
= 0x4e800020;
2564 static const uint32_t blrl
= 0x4e800021;
2565 static const uint32_t bnectr_p4
= 0x4ce20420;
2566 static const uint32_t cmpldi_2_0
= 0x28220000;
2567 static const uint32_t cror_15_15_15
= 0x4def7b82;
2568 static const uint32_t cror_31_31_31
= 0x4ffffb82;
2569 static const uint32_t ld_0_1
= 0xe8010000;
2570 static const uint32_t ld_0_12
= 0xe80c0000;
2571 static const uint32_t ld_11_12
= 0xe96c0000;
2572 static const uint32_t ld_11_2
= 0xe9620000;
2573 static const uint32_t ld_2_1
= 0xe8410000;
2574 static const uint32_t ld_2_11
= 0xe84b0000;
2575 static const uint32_t ld_2_12
= 0xe84c0000;
2576 static const uint32_t ld_2_2
= 0xe8420000;
2577 static const uint32_t lfd_0_1
= 0xc8010000;
2578 static const uint32_t li_0_0
= 0x38000000;
2579 static const uint32_t li_12_0
= 0x39800000;
2580 static const uint32_t lis_0_0
= 0x3c000000;
2581 static const uint32_t lis_11
= 0x3d600000;
2582 static const uint32_t lis_12
= 0x3d800000;
2583 static const uint32_t lwz_0_12
= 0x800c0000;
2584 static const uint32_t lwz_11_11
= 0x816b0000;
2585 static const uint32_t lwz_11_30
= 0x817e0000;
2586 static const uint32_t lwz_12_12
= 0x818c0000;
2587 static const uint32_t lwzu_0_12
= 0x840c0000;
2588 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
2589 static const uint32_t mflr_0
= 0x7c0802a6;
2590 static const uint32_t mflr_11
= 0x7d6802a6;
2591 static const uint32_t mflr_12
= 0x7d8802a6;
2592 static const uint32_t mtctr_0
= 0x7c0903a6;
2593 static const uint32_t mtctr_11
= 0x7d6903a6;
2594 static const uint32_t mtctr_12
= 0x7d8903a6;
2595 static const uint32_t mtlr_0
= 0x7c0803a6;
2596 static const uint32_t mtlr_12
= 0x7d8803a6;
2597 static const uint32_t nop
= 0x60000000;
2598 static const uint32_t ori_0_0_0
= 0x60000000;
2599 static const uint32_t std_0_1
= 0xf8010000;
2600 static const uint32_t std_0_12
= 0xf80c0000;
2601 static const uint32_t std_2_1
= 0xf8410000;
2602 static const uint32_t stfd_0_1
= 0xd8010000;
2603 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
2604 static const uint32_t sub_11_11_12
= 0x7d6c5850;
2605 static const uint32_t xor_11_11_11
= 0x7d6b5a78;
2607 // Write out the PLT.
2609 template<int size
, bool big_endian
>
2611 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
2615 const section_size_type offset
= this->offset();
2616 const section_size_type oview_size
2617 = convert_to_section_size_type(this->data_size());
2618 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2619 unsigned char* pov
= oview
;
2620 unsigned char* endpov
= oview
+ oview_size
;
2622 // The address of the .glink branch table
2623 const Output_data_glink
<size
, big_endian
>* glink
2624 = this->targ_
->glink_section();
2625 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
2627 while (pov
< endpov
)
2629 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
2634 of
->write_output_view(offset
, oview_size
, oview
);
2638 // Create the PLT section.
2640 template<int size
, bool big_endian
>
2642 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
2645 if (this->plt_
== NULL
)
2647 if (this->got_
== NULL
)
2648 this->got_section(symtab
, layout
);
2650 if (this->glink_
== NULL
)
2651 make_glink_section(layout
);
2653 // Ensure that .rela.dyn always appears before .rela.plt This is
2654 // necessary due to how, on PowerPC and some other targets, .rela.dyn
2655 // needs to include .rela.plt in it's range.
2656 this->rela_dyn_section(layout
);
2658 Reloc_section
* plt_rel
= new Reloc_section(false);
2659 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
2660 elfcpp::SHF_ALLOC
, plt_rel
,
2661 ORDER_DYNAMIC_PLT_RELOCS
, false);
2663 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
2664 size
== 32 ? 0 : 24,
2666 layout
->add_output_section_data(".plt",
2668 ? elfcpp::SHT_PROGBITS
2669 : elfcpp::SHT_NOBITS
),
2670 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2679 // Create the IPLT section.
2681 template<int size
, bool big_endian
>
2683 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
2686 if (this->iplt_
== NULL
)
2688 this->make_plt_section(symtab
, layout
);
2690 Reloc_section
* iplt_rel
= new Reloc_section(false);
2691 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
2693 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
2695 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
2699 // A section for huge long branch addresses, similar to plt section.
2701 template<int size
, bool big_endian
>
2702 class Output_data_brlt_powerpc
: public Output_section_data_build
2705 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2706 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
2707 size
, big_endian
> Reloc_section
;
2709 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
2710 Reloc_section
* brlt_rel
)
2711 : Output_section_data_build(size
== 32 ? 4 : 8),
2716 // Add a reloc for an entry in the BRLT.
2718 add_reloc(Address to
, unsigned int off
)
2719 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
2721 // Update section and reloc section size.
2723 set_current_size(unsigned int num_branches
)
2725 this->reset_address_and_file_offset();
2726 this->set_current_data_size(num_branches
* 16);
2727 this->finalize_data_size();
2728 Output_section
* os
= this->output_section();
2729 os
->set_section_offsets_need_adjustment();
2730 if (this->rel_
!= NULL
)
2732 unsigned int reloc_size
2733 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
2734 this->rel_
->reset_address_and_file_offset();
2735 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
2736 this->rel_
->finalize_data_size();
2737 Output_section
* os
= this->rel_
->output_section();
2738 os
->set_section_offsets_need_adjustment();
2744 do_adjust_output_section(Output_section
* os
)
2749 // Write to a map file.
2751 do_print_to_mapfile(Mapfile
* mapfile
) const
2752 { mapfile
->print_output_data(this, "** BRLT"); }
2755 // Write out the BRLT data.
2757 do_write(Output_file
*);
2759 // The reloc section.
2760 Reloc_section
* rel_
;
2761 Target_powerpc
<size
, big_endian
>* targ_
;
2764 // Make the branch lookup table section.
2766 template<int size
, bool big_endian
>
2768 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
2770 if (size
== 64 && this->brlt_section_
== NULL
)
2772 Reloc_section
* brlt_rel
= NULL
;
2773 bool is_pic
= parameters
->options().output_is_position_independent();
2776 // When PIC we can't fill in .brlt (like .plt it can be a
2777 // bss style section) but must initialise at runtime via
2778 // dynamic relocats.
2779 this->rela_dyn_section(layout
);
2780 brlt_rel
= new Reloc_section(false);
2781 this->rela_dyn_
->output_section()->add_output_section_data(brlt_rel
);
2784 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
2785 if (this->plt_
&& is_pic
)
2786 this->plt_
->output_section()
2787 ->add_output_section_data(this->brlt_section_
);
2789 layout
->add_output_section_data(".brlt",
2790 (is_pic
? elfcpp::SHT_NOBITS
2791 : elfcpp::SHT_PROGBITS
),
2792 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2793 this->brlt_section_
,
2794 (is_pic
? ORDER_SMALL_BSS
2795 : ORDER_SMALL_DATA
),
2800 // Write out .brlt when non-PIC.
2802 template<int size
, bool big_endian
>
2804 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
2806 if (size
== 64 && !parameters
->options().output_is_position_independent())
2808 const section_size_type offset
= this->offset();
2809 const section_size_type oview_size
2810 = convert_to_section_size_type(this->data_size());
2811 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2813 this->targ_
->write_branch_lookup_table(oview
);
2814 of
->write_output_view(offset
, oview_size
, oview
);
2818 static inline uint32_t
2824 static inline uint32_t
2830 static inline uint32_t
2833 return hi(a
+ 0x8000);
2839 static const unsigned char eh_frame_cie
[12];
2843 const unsigned char Eh_cie
<size
>::eh_frame_cie
[] =
2846 'z', 'R', 0, // Augmentation string.
2847 4, // Code alignment.
2848 0x80 - size
/ 8 , // Data alignment.
2850 1, // Augmentation size.
2851 (elfcpp::DW_EH_PE_pcrel
2852 | elfcpp::DW_EH_PE_sdata4
), // FDE encoding.
2853 elfcpp::DW_CFA_def_cfa
, 1, 0 // def_cfa: r1 offset 0.
2856 // Describe __glink_PLTresolve use of LR, 64-bit version.
2857 static const unsigned char glink_eh_frame_fde_64
[] =
2859 0, 0, 0, 0, // Replaced with offset to .glink.
2860 0, 0, 0, 0, // Replaced with size of .glink.
2861 0, // Augmentation size.
2862 elfcpp::DW_CFA_advance_loc
+ 1,
2863 elfcpp::DW_CFA_register
, 65, 12,
2864 elfcpp::DW_CFA_advance_loc
+ 4,
2865 elfcpp::DW_CFA_restore_extended
, 65
2868 // Describe __glink_PLTresolve use of LR, 32-bit version.
2869 static const unsigned char glink_eh_frame_fde_32
[] =
2871 0, 0, 0, 0, // Replaced with offset to .glink.
2872 0, 0, 0, 0, // Replaced with size of .glink.
2873 0, // Augmentation size.
2874 elfcpp::DW_CFA_advance_loc
+ 2,
2875 elfcpp::DW_CFA_register
, 65, 0,
2876 elfcpp::DW_CFA_advance_loc
+ 4,
2877 elfcpp::DW_CFA_restore_extended
, 65
2880 static const unsigned char default_fde
[] =
2882 0, 0, 0, 0, // Replaced with offset to stubs.
2883 0, 0, 0, 0, // Replaced with size of stubs.
2884 0, // Augmentation size.
2885 elfcpp::DW_CFA_nop
, // Pad.
2890 template<bool big_endian
>
2892 write_insn(unsigned char* p
, uint32_t v
)
2894 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
2897 // Stub_table holds information about plt and long branch stubs.
2898 // Stubs are built in an area following some input section determined
2899 // by group_sections(). This input section is converted to a relaxed
2900 // input section allowing it to be resized to accommodate the stubs
2902 template<int size
, bool big_endian
>
2903 class Stub_table
: public Output_relaxed_input_section
2906 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2907 static const Address invalid_address
= static_cast<Address
>(0) - 1;
2909 Stub_table(Target_powerpc
<size
, big_endian
>* targ
)
2910 : Output_relaxed_input_section(NULL
, 0, 0),
2911 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
2912 orig_data_size_(0), plt_size_(0), last_plt_size_(0),
2913 branch_size_(0), last_branch_size_(0), eh_frame_added_(false)
2916 // Delayed Output_relaxed_input_section init.
2918 init(const Output_section::Input_section
*, Output_section
*);
2920 // Add a plt call stub.
2922 add_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2928 add_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2933 // Find a given plt call stub.
2935 find_plt_call_entry(const Symbol
*) const;
2938 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2939 unsigned int) const;
2942 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2948 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2953 // Add a long branch stub.
2955 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*, Address
);
2958 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
2964 this->plt_call_stubs_
.clear();
2965 this->plt_size_
= 0;
2966 this->long_branch_stubs_
.clear();
2967 this->branch_size_
= 0;
2971 set_address_and_size(const Output_section
* os
, Address off
)
2973 Address start_off
= off
;
2974 off
+= this->orig_data_size_
;
2975 Address my_size
= this->plt_size_
+ this->branch_size_
;
2977 off
= align_address(off
, this->stub_align());
2978 // Include original section size and alignment padding in size
2979 my_size
+= off
- start_off
;
2980 this->reset_address_and_file_offset();
2981 this->set_current_data_size(my_size
);
2982 this->set_address_and_file_offset(os
->address() + start_off
,
2983 os
->offset() + start_off
);
2988 stub_address() const
2990 return align_address(this->address() + this->orig_data_size_
,
2991 this->stub_align());
2997 return align_address(this->offset() + this->orig_data_size_
,
2998 this->stub_align());
3003 { return this->plt_size_
; }
3008 Output_section
* os
= this->output_section();
3009 if (os
->addralign() < this->stub_align())
3011 os
->set_addralign(this->stub_align());
3012 // FIXME: get rid of the insane checkpointing.
3013 // We can't increase alignment of the input section to which
3014 // stubs are attached; The input section may be .init which
3015 // is pasted together with other .init sections to form a
3016 // function. Aligning might insert zero padding resulting in
3017 // sigill. However we do need to increase alignment of the
3018 // output section so that the align_address() on offset in
3019 // set_address_and_size() adds the same padding as the
3020 // align_address() on address in stub_address().
3021 // What's more, we need this alignment for the layout done in
3022 // relaxation_loop_body() so that the output section starts at
3023 // a suitably aligned address.
3024 os
->checkpoint_set_addralign(this->stub_align());
3026 if (this->last_plt_size_
!= this->plt_size_
3027 || this->last_branch_size_
!= this->branch_size_
)
3029 this->last_plt_size_
= this->plt_size_
;
3030 this->last_branch_size_
= this->branch_size_
;
3036 // Add .eh_frame info for this stub section. Unlike other linker
3037 // generated .eh_frame this is added late in the link, because we
3038 // only want the .eh_frame info if this particular stub section is
3041 add_eh_frame(Layout
* layout
)
3043 if (!this->eh_frame_added_
)
3045 if (!parameters
->options().ld_generated_unwind_info())
3048 // Since we add stub .eh_frame info late, it must be placed
3049 // after all other linker generated .eh_frame info so that
3050 // merge mapping need not be updated for input sections.
3051 // There is no provision to use a different CIE to that used
3053 if (!this->targ_
->has_glink())
3056 layout
->add_eh_frame_for_plt(this,
3057 Eh_cie
<size
>::eh_frame_cie
,
3058 sizeof (Eh_cie
<size
>::eh_frame_cie
),
3060 sizeof (default_fde
));
3061 this->eh_frame_added_
= true;
3065 Target_powerpc
<size
, big_endian
>*
3071 class Plt_stub_ent_hash
;
3072 typedef Unordered_map
<Plt_stub_ent
, unsigned int,
3073 Plt_stub_ent_hash
> Plt_stub_entries
;
3075 // Alignment of stub section.
3081 unsigned int min_align
= 32;
3082 unsigned int user_align
= 1 << parameters
->options().plt_align();
3083 return std::max(user_align
, min_align
);
3086 // Return the plt offset for the given call stub.
3088 plt_off(typename
Plt_stub_entries::const_iterator p
, bool* is_iplt
) const
3090 const Symbol
* gsym
= p
->first
.sym_
;
3093 *is_iplt
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
3094 && gsym
->can_use_relative_reloc(false));
3095 return gsym
->plt_offset();
3100 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
3101 unsigned int local_sym_index
= p
->first
.locsym_
;
3102 return relobj
->local_plt_offset(local_sym_index
);
3106 // Size of a given plt call stub.
3108 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
3114 Address plt_addr
= this->plt_off(p
, &is_iplt
);
3116 plt_addr
+= this->targ_
->iplt_section()->address();
3118 plt_addr
+= this->targ_
->plt_section()->address();
3119 Address got_addr
= this->targ_
->got_section()->output_section()->address();
3120 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3121 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
3122 got_addr
+= ppcobj
->toc_base_offset();
3123 Address off
= plt_addr
- got_addr
;
3124 bool static_chain
= parameters
->options().plt_static_chain();
3125 bool thread_safe
= this->targ_
->plt_thread_safe();
3126 unsigned int bytes
= (4 * 5
3129 + 4 * (ha(off
) != 0)
3130 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
3131 unsigned int align
= 1 << parameters
->options().plt_align();
3133 bytes
= (bytes
+ align
- 1) & -align
;
3137 // Return long branch stub size.
3139 branch_stub_size(Address to
)
3142 = this->stub_address() + this->last_plt_size_
+ this->branch_size_
;
3143 if (to
- loc
+ (1 << 25) < 2 << 25)
3145 if (size
== 64 || !parameters
->options().output_is_position_independent())
3152 do_write(Output_file
*);
3154 // Plt call stub keys.
3158 Plt_stub_ent(const Symbol
* sym
)
3159 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3162 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3163 unsigned int locsym_index
)
3164 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3167 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3169 unsigned int r_type
,
3171 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3174 this->addend_
= addend
;
3175 else if (parameters
->options().output_is_position_independent()
3176 && r_type
== elfcpp::R_PPC_PLTREL24
)
3178 this->addend_
= addend
;
3179 if (this->addend_
>= 32768)
3180 this->object_
= object
;
3184 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3185 unsigned int locsym_index
,
3186 unsigned int r_type
,
3188 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3191 this->addend_
= addend
;
3192 else if (parameters
->options().output_is_position_independent()
3193 && r_type
== elfcpp::R_PPC_PLTREL24
)
3194 this->addend_
= addend
;
3197 bool operator==(const Plt_stub_ent
& that
) const
3199 return (this->sym_
== that
.sym_
3200 && this->object_
== that
.object_
3201 && this->addend_
== that
.addend_
3202 && this->locsym_
== that
.locsym_
);
3206 const Sized_relobj_file
<size
, big_endian
>* object_
;
3207 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
3208 unsigned int locsym_
;
3211 class Plt_stub_ent_hash
3214 size_t operator()(const Plt_stub_ent
& ent
) const
3216 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
3217 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
3223 // Long branch stub keys.
3224 class Branch_stub_ent
3227 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
, Address to
)
3228 : dest_(to
), toc_base_off_(0)
3231 toc_base_off_
= obj
->toc_base_offset();
3234 bool operator==(const Branch_stub_ent
& that
) const
3236 return (this->dest_
== that
.dest_
3238 || this->toc_base_off_
== that
.toc_base_off_
));
3242 unsigned int toc_base_off_
;
3245 class Branch_stub_ent_hash
3248 size_t operator()(const Branch_stub_ent
& ent
) const
3249 { return ent
.dest_
^ ent
.toc_base_off_
; }
3252 // In a sane world this would be a global.
3253 Target_powerpc
<size
, big_endian
>* targ_
;
3254 // Map sym/object/addend to stub offset.
3255 Plt_stub_entries plt_call_stubs_
;
3256 // Map destination address to stub offset.
3257 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
3258 Branch_stub_ent_hash
> Branch_stub_entries
;
3259 Branch_stub_entries long_branch_stubs_
;
3260 // size of input section
3261 section_size_type orig_data_size_
;
3263 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
3264 // Whether .eh_frame info has been created for this stub section.
3265 bool eh_frame_added_
;
3268 // Make a new stub table, and record.
3270 template<int size
, bool big_endian
>
3271 Stub_table
<size
, big_endian
>*
3272 Target_powerpc
<size
, big_endian
>::new_stub_table()
3274 Stub_table
<size
, big_endian
>* stub_table
3275 = new Stub_table
<size
, big_endian
>(this);
3276 this->stub_tables_
.push_back(stub_table
);
3280 // Delayed stub table initialisation, because we create the stub table
3281 // before we know to which section it will be attached.
3283 template<int size
, bool big_endian
>
3285 Stub_table
<size
, big_endian
>::init(
3286 const Output_section::Input_section
* owner
,
3287 Output_section
* output_section
)
3289 this->set_relobj(owner
->relobj());
3290 this->set_shndx(owner
->shndx());
3291 this->set_addralign(this->relobj()->section_addralign(this->shndx()));
3292 this->set_output_section(output_section
);
3293 this->orig_data_size_
= owner
->current_data_size();
3295 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
3296 new_relaxed
.push_back(this);
3297 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
3300 // Add a plt call stub, if we do not already have one for this
3301 // sym/object/addend combo.
3303 template<int size
, bool big_endian
>
3305 Stub_table
<size
, big_endian
>::add_plt_call_entry(
3306 const Sized_relobj_file
<size
, big_endian
>* object
,
3308 unsigned int r_type
,
3311 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
3312 Address off
= this->plt_size_
;
3313 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
3314 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
3316 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
3319 template<int size
, bool big_endian
>
3321 Stub_table
<size
, big_endian
>::add_plt_call_entry(
3322 const Sized_relobj_file
<size
, big_endian
>* object
,
3323 unsigned int locsym_index
,
3324 unsigned int r_type
,
3327 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
3328 Address off
= this->plt_size_
;
3329 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
3330 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
3332 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
3335 // Find a plt call stub.
3337 template<int size
, bool big_endian
>
3338 typename Stub_table
<size
, big_endian
>::Address
3339 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3340 const Sized_relobj_file
<size
, big_endian
>* object
,
3342 unsigned int r_type
,
3343 Address addend
) const
3345 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
3346 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3347 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3350 template<int size
, bool big_endian
>
3351 typename Stub_table
<size
, big_endian
>::Address
3352 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
3354 Plt_stub_ent
ent(gsym
);
3355 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3356 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3359 template<int size
, bool big_endian
>
3360 typename Stub_table
<size
, big_endian
>::Address
3361 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3362 const Sized_relobj_file
<size
, big_endian
>* object
,
3363 unsigned int locsym_index
,
3364 unsigned int r_type
,
3365 Address addend
) const
3367 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
3368 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3369 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3372 template<int size
, bool big_endian
>
3373 typename Stub_table
<size
, big_endian
>::Address
3374 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3375 const Sized_relobj_file
<size
, big_endian
>* object
,
3376 unsigned int locsym_index
) const
3378 Plt_stub_ent
ent(object
, locsym_index
);
3379 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3380 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3383 // Add a long branch stub if we don't already have one to given
3386 template<int size
, bool big_endian
>
3388 Stub_table
<size
, big_endian
>::add_long_branch_entry(
3389 const Powerpc_relobj
<size
, big_endian
>* object
,
3392 Branch_stub_ent
ent(object
, to
);
3393 Address off
= this->branch_size_
;
3394 if (this->long_branch_stubs_
.insert(std::make_pair(ent
, off
)).second
)
3396 unsigned int stub_size
= this->branch_stub_size(to
);
3397 this->branch_size_
= off
+ stub_size
;
3398 if (size
== 64 && stub_size
!= 4)
3399 this->targ_
->add_branch_lookup_table(to
);
3403 // Find long branch stub.
3405 template<int size
, bool big_endian
>
3406 typename Stub_table
<size
, big_endian
>::Address
3407 Stub_table
<size
, big_endian
>::find_long_branch_entry(
3408 const Powerpc_relobj
<size
, big_endian
>* object
,
3411 Branch_stub_ent
ent(object
, to
);
3412 typename
Branch_stub_entries::const_iterator p
3413 = this->long_branch_stubs_
.find(ent
);
3414 return p
== this->long_branch_stubs_
.end() ? invalid_address
: p
->second
;
3417 // A class to handle .glink.
3419 template<int size
, bool big_endian
>
3420 class Output_data_glink
: public Output_section_data
3423 static const int pltresolve_size
= 16*4;
3425 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
3426 : Output_section_data(16), targ_(targ
)
3430 add_eh_frame(Layout
* layout
)
3432 if (!parameters
->options().ld_generated_unwind_info())
3436 layout
->add_eh_frame_for_plt(this,
3437 Eh_cie
<64>::eh_frame_cie
,
3438 sizeof (Eh_cie
<64>::eh_frame_cie
),
3439 glink_eh_frame_fde_64
,
3440 sizeof (glink_eh_frame_fde_64
));
3443 // 32-bit .glink can use the default since the CIE return
3444 // address reg, LR, is valid.
3445 layout
->add_eh_frame_for_plt(this,
3446 Eh_cie
<32>::eh_frame_cie
,
3447 sizeof (Eh_cie
<32>::eh_frame_cie
),
3449 sizeof (default_fde
));
3450 // Except where LR is used in a PIC __glink_PLTresolve.
3451 if (parameters
->options().output_is_position_independent())
3452 layout
->add_eh_frame_for_plt(this,
3453 Eh_cie
<32>::eh_frame_cie
,
3454 sizeof (Eh_cie
<32>::eh_frame_cie
),
3455 glink_eh_frame_fde_32
,
3456 sizeof (glink_eh_frame_fde_32
));
3461 // Write to a map file.
3463 do_print_to_mapfile(Mapfile
* mapfile
) const
3464 { mapfile
->print_output_data(this, _("** glink")); }
3468 set_final_data_size();
3472 do_write(Output_file
*);
3474 // Allows access to .got and .plt for do_write.
3475 Target_powerpc
<size
, big_endian
>* targ_
;
3478 template<int size
, bool big_endian
>
3480 Output_data_glink
<size
, big_endian
>::set_final_data_size()
3482 unsigned int count
= this->targ_
->plt_entry_count();
3483 section_size_type total
= 0;
3489 // space for branch table
3490 total
+= 4 * (count
- 1);
3492 total
+= -total
& 15;
3493 total
+= this->pltresolve_size
;
3497 total
+= this->pltresolve_size
;
3499 // space for branch table
3502 total
+= 4 * (count
- 0x8000);
3506 this->set_data_size(total
);
3509 // Write out plt and long branch stub code.
3511 template<int size
, bool big_endian
>
3513 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
3515 if (this->plt_call_stubs_
.empty()
3516 && this->long_branch_stubs_
.empty())
3519 const section_size_type start_off
= this->offset();
3520 const section_size_type off
= this->stub_offset();
3521 const section_size_type oview_size
=
3522 convert_to_section_size_type(this->data_size() - (off
- start_off
));
3523 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
3528 const Output_data_got_powerpc
<size
, big_endian
>* got
3529 = this->targ_
->got_section();
3530 Address got_os_addr
= got
->output_section()->address();
3532 if (!this->plt_call_stubs_
.empty())
3534 // The base address of the .plt section.
3535 Address plt_base
= this->targ_
->plt_section()->address();
3536 Address iplt_base
= invalid_address
;
3538 // Write out plt call stubs.
3539 typename
Plt_stub_entries::const_iterator cs
;
3540 for (cs
= this->plt_call_stubs_
.begin();
3541 cs
!= this->plt_call_stubs_
.end();
3545 Address pltoff
= this->plt_off(cs
, &is_iplt
);
3546 Address plt_addr
= pltoff
;
3549 if (iplt_base
== invalid_address
)
3550 iplt_base
= this->targ_
->iplt_section()->address();
3551 plt_addr
+= iplt_base
;
3554 plt_addr
+= plt_base
;
3555 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3556 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
3557 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
3558 Address off
= plt_addr
- got_addr
;
3560 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
3561 gold_error(_("%s: linkage table error against `%s'"),
3562 cs
->first
.object_
->name().c_str(),
3563 cs
->first
.sym_
->demangled_name().c_str());
3565 bool static_chain
= parameters
->options().plt_static_chain();
3566 bool thread_safe
= this->targ_
->plt_thread_safe();
3567 bool use_fake_dep
= false;
3568 Address cmp_branch_off
= 0;
3571 unsigned int pltindex
3572 = ((pltoff
- this->targ_
->first_plt_entry_offset())
3573 / this->targ_
->plt_entry_size());
3575 = (this->targ_
->glink_section()->pltresolve_size
3577 if (pltindex
> 32768)
3578 glinkoff
+= (pltindex
- 32768) * 4;
3580 = this->targ_
->glink_section()->address() + glinkoff
;
3582 = (this->stub_address() + cs
->second
+ 24
3583 + 4 * (ha(off
) != 0)
3584 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
3585 + 4 * static_chain
);
3586 cmp_branch_off
= to
- from
;
3587 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
3590 p
= oview
+ cs
->second
;
3593 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
3594 write_insn
<big_endian
>(p
, addis_12_2
+ ha(off
)), p
+= 4;
3595 write_insn
<big_endian
>(p
, ld_11_12
+ l(off
)), p
+= 4;
3596 if (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
3598 write_insn
<big_endian
>(p
, addi_12_12
+ l(off
)), p
+= 4;
3601 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3604 write_insn
<big_endian
>(p
, xor_11_11_11
), p
+= 4;
3605 write_insn
<big_endian
>(p
, add_12_12_11
), p
+= 4;
3607 write_insn
<big_endian
>(p
, ld_2_12
+ l(off
+ 8)), p
+= 4;
3609 write_insn
<big_endian
>(p
, ld_11_12
+ l(off
+ 16)), p
+= 4;
3613 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
3614 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
)), p
+= 4;
3615 if (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
3617 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
)), p
+= 4;
3620 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3623 write_insn
<big_endian
>(p
, xor_11_11_11
), p
+= 4;
3624 write_insn
<big_endian
>(p
, add_2_2_11
), p
+= 4;
3627 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16)), p
+= 4;
3628 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8)), p
+= 4;
3630 if (thread_safe
&& !use_fake_dep
)
3632 write_insn
<big_endian
>(p
, cmpldi_2_0
), p
+= 4;
3633 write_insn
<big_endian
>(p
, bnectr_p4
), p
+= 4;
3634 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
3637 write_insn
<big_endian
>(p
, bctr
);
3641 // Write out long branch stubs.
3642 typename
Branch_stub_entries::const_iterator bs
;
3643 for (bs
= this->long_branch_stubs_
.begin();
3644 bs
!= this->long_branch_stubs_
.end();
3647 p
= oview
+ this->plt_size_
+ bs
->second
;
3648 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
3649 Address delta
= bs
->first
.dest_
- loc
;
3650 if (delta
+ (1 << 25) < 2 << 25)
3651 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
3655 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
3656 gold_assert(brlt_addr
!= invalid_address
);
3657 brlt_addr
+= this->targ_
->brlt_section()->address();
3658 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
3659 Address brltoff
= brlt_addr
- got_addr
;
3660 if (ha(brltoff
) == 0)
3662 write_insn
<big_endian
>(p
, ld_11_2
+ l(brltoff
)), p
+= 4;
3666 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
3667 write_insn
<big_endian
>(p
, ld_11_12
+ l(brltoff
)), p
+= 4;
3669 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3670 write_insn
<big_endian
>(p
, bctr
);
3676 if (!this->plt_call_stubs_
.empty())
3678 // The base address of the .plt section.
3679 Address plt_base
= this->targ_
->plt_section()->address();
3680 Address iplt_base
= invalid_address
;
3681 // The address of _GLOBAL_OFFSET_TABLE_.
3682 Address g_o_t
= invalid_address
;
3684 // Write out plt call stubs.
3685 typename
Plt_stub_entries::const_iterator cs
;
3686 for (cs
= this->plt_call_stubs_
.begin();
3687 cs
!= this->plt_call_stubs_
.end();
3691 Address plt_addr
= this->plt_off(cs
, &is_iplt
);
3694 if (iplt_base
== invalid_address
)
3695 iplt_base
= this->targ_
->iplt_section()->address();
3696 plt_addr
+= iplt_base
;
3699 plt_addr
+= plt_base
;
3701 p
= oview
+ cs
->second
;
3702 if (parameters
->options().output_is_position_independent())
3705 const Powerpc_relobj
<size
, big_endian
>* ppcobj
3706 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
3707 (cs
->first
.object_
));
3708 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
3710 unsigned int got2
= ppcobj
->got2_shndx();
3711 got_addr
= ppcobj
->get_output_section_offset(got2
);
3712 gold_assert(got_addr
!= invalid_address
);
3713 got_addr
+= (ppcobj
->output_section(got2
)->address()
3714 + cs
->first
.addend_
);
3718 if (g_o_t
== invalid_address
)
3720 const Output_data_got_powerpc
<size
, big_endian
>* got
3721 = this->targ_
->got_section();
3722 g_o_t
= got
->address() + got
->g_o_t();
3727 Address off
= plt_addr
- got_addr
;
3730 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(off
));
3731 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
3732 write_insn
<big_endian
>(p
+ 8, bctr
);
3736 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(off
));
3737 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(off
));
3738 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
3739 write_insn
<big_endian
>(p
+ 12, bctr
);
3744 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
3745 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
3746 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
3747 write_insn
<big_endian
>(p
+ 12, bctr
);
3752 // Write out long branch stubs.
3753 typename
Branch_stub_entries::const_iterator bs
;
3754 for (bs
= this->long_branch_stubs_
.begin();
3755 bs
!= this->long_branch_stubs_
.end();
3758 p
= oview
+ this->plt_size_
+ bs
->second
;
3759 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
3760 Address delta
= bs
->first
.dest_
- loc
;
3761 if (delta
+ (1 << 25) < 2 << 25)
3762 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
3763 else if (!parameters
->options().output_is_position_independent())
3765 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(bs
->first
.dest_
));
3766 write_insn
<big_endian
>(p
+ 4, addi_12_12
+ l(bs
->first
.dest_
));
3767 write_insn
<big_endian
>(p
+ 8, mtctr_12
);
3768 write_insn
<big_endian
>(p
+ 12, bctr
);
3773 write_insn
<big_endian
>(p
+ 0, mflr_0
);
3774 write_insn
<big_endian
>(p
+ 4, bcl_20_31
);
3775 write_insn
<big_endian
>(p
+ 8, mflr_12
);
3776 write_insn
<big_endian
>(p
+ 12, addis_12_12
+ ha(delta
));
3777 write_insn
<big_endian
>(p
+ 16, addi_12_12
+ l(delta
));
3778 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
3779 write_insn
<big_endian
>(p
+ 24, mtctr_12
);
3780 write_insn
<big_endian
>(p
+ 28, bctr
);
3786 // Write out .glink.
3788 template<int size
, bool big_endian
>
3790 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
3792 const section_size_type off
= this->offset();
3793 const section_size_type oview_size
=
3794 convert_to_section_size_type(this->data_size());
3795 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
3798 // The base address of the .plt section.
3799 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3800 Address plt_base
= this->targ_
->plt_section()->address();
3804 // Write pltresolve stub.
3806 Address after_bcl
= this->address() + 16;
3807 Address pltoff
= plt_base
- after_bcl
;
3809 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
3811 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
3812 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
3813 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
3814 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
3815 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
3816 write_insn
<big_endian
>(p
, add_12_2_11
), p
+= 4;
3817 write_insn
<big_endian
>(p
, ld_11_12
+ 0), p
+= 4;
3818 write_insn
<big_endian
>(p
, ld_2_12
+ 8), p
+= 4;
3819 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3820 write_insn
<big_endian
>(p
, ld_11_12
+ 16), p
+= 4;
3821 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
3822 while (p
< oview
+ this->pltresolve_size
)
3823 write_insn
<big_endian
>(p
, nop
), p
+= 4;
3825 // Write lazy link call stubs.
3827 while (p
< oview
+ oview_size
)
3831 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
3835 write_insn
<big_endian
>(p
, lis_0_0
+ hi(indx
)), p
+= 4;
3836 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
3838 uint32_t branch_off
= 8 - (p
- oview
);
3839 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
3845 const Output_data_got_powerpc
<size
, big_endian
>* got
3846 = this->targ_
->got_section();
3847 // The address of _GLOBAL_OFFSET_TABLE_.
3848 Address g_o_t
= got
->address() + got
->g_o_t();
3850 // Write out pltresolve branch table.
3852 unsigned int the_end
= oview_size
- this->pltresolve_size
;
3853 unsigned char* end_p
= oview
+ the_end
;
3854 while (p
< end_p
- 8 * 4)
3855 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
3857 write_insn
<big_endian
>(p
, nop
), p
+= 4;
3859 // Write out pltresolve call stub.
3860 if (parameters
->options().output_is_position_independent())
3862 Address res0_off
= 0;
3863 Address after_bcl_off
= the_end
+ 12;
3864 Address bcl_res0
= after_bcl_off
- res0_off
;
3866 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
3867 write_insn
<big_endian
>(p
+ 4, mflr_0
);
3868 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
3869 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
3870 write_insn
<big_endian
>(p
+ 16, mflr_12
);
3871 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
3872 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
3874 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
3876 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
3877 if (ha(got_bcl
) == ha(got_bcl
+ 4))
3879 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
3880 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
3884 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
3885 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
3887 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
3888 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
3889 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
3890 write_insn
<big_endian
>(p
+ 52, bctr
);
3891 write_insn
<big_endian
>(p
+ 56, nop
);
3892 write_insn
<big_endian
>(p
+ 60, nop
);
3896 Address res0
= this->address();
3898 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
3899 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
3900 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
3901 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
3903 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
3904 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
3905 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
3906 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
3907 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
3908 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
3910 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
3911 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
3912 write_insn
<big_endian
>(p
+ 32, bctr
);
3913 write_insn
<big_endian
>(p
+ 36, nop
);
3914 write_insn
<big_endian
>(p
+ 40, nop
);
3915 write_insn
<big_endian
>(p
+ 44, nop
);
3916 write_insn
<big_endian
>(p
+ 48, nop
);
3917 write_insn
<big_endian
>(p
+ 52, nop
);
3918 write_insn
<big_endian
>(p
+ 56, nop
);
3919 write_insn
<big_endian
>(p
+ 60, nop
);
3924 of
->write_output_view(off
, oview_size
, oview
);
3928 // A class to handle linker generated save/restore functions.
3930 template<int size
, bool big_endian
>
3931 class Output_data_save_res
: public Output_section_data_build
3934 Output_data_save_res(Symbol_table
* symtab
);
3937 // Write to a map file.
3939 do_print_to_mapfile(Mapfile
* mapfile
) const
3940 { mapfile
->print_output_data(this, _("** save/restore")); }
3943 do_write(Output_file
*);
3946 // The maximum size of save/restore contents.
3947 static const unsigned int savres_max
= 218*4;
3950 savres_define(Symbol_table
* symtab
,
3952 unsigned int lo
, unsigned int hi
,
3953 unsigned char* write_ent(unsigned char*, int),
3954 unsigned char* write_tail(unsigned char*, int));
3956 unsigned char *contents_
;
3959 template<bool big_endian
>
3960 static unsigned char*
3961 savegpr0(unsigned char* p
, int r
)
3963 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3964 write_insn
<big_endian
>(p
, insn
);
3968 template<bool big_endian
>
3969 static unsigned char*
3970 savegpr0_tail(unsigned char* p
, int r
)
3972 p
= savegpr0
<big_endian
>(p
, r
);
3973 uint32_t insn
= std_0_1
+ 16;
3974 write_insn
<big_endian
>(p
, insn
);
3976 write_insn
<big_endian
>(p
, blr
);
3980 template<bool big_endian
>
3981 static unsigned char*
3982 restgpr0(unsigned char* p
, int r
)
3984 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3985 write_insn
<big_endian
>(p
, insn
);
3989 template<bool big_endian
>
3990 static unsigned char*
3991 restgpr0_tail(unsigned char* p
, int r
)
3993 uint32_t insn
= ld_0_1
+ 16;
3994 write_insn
<big_endian
>(p
, insn
);
3996 p
= restgpr0
<big_endian
>(p
, r
);
3997 write_insn
<big_endian
>(p
, mtlr_0
);
4001 p
= restgpr0
<big_endian
>(p
, 30);
4002 p
= restgpr0
<big_endian
>(p
, 31);
4004 write_insn
<big_endian
>(p
, blr
);
4008 template<bool big_endian
>
4009 static unsigned char*
4010 savegpr1(unsigned char* p
, int r
)
4012 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4013 write_insn
<big_endian
>(p
, insn
);
4017 template<bool big_endian
>
4018 static unsigned char*
4019 savegpr1_tail(unsigned char* p
, int r
)
4021 p
= savegpr1
<big_endian
>(p
, r
);
4022 write_insn
<big_endian
>(p
, blr
);
4026 template<bool big_endian
>
4027 static unsigned char*
4028 restgpr1(unsigned char* p
, int r
)
4030 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4031 write_insn
<big_endian
>(p
, insn
);
4035 template<bool big_endian
>
4036 static unsigned char*
4037 restgpr1_tail(unsigned char* p
, int r
)
4039 p
= restgpr1
<big_endian
>(p
, r
);
4040 write_insn
<big_endian
>(p
, blr
);
4044 template<bool big_endian
>
4045 static unsigned char*
4046 savefpr(unsigned char* p
, int r
)
4048 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4049 write_insn
<big_endian
>(p
, insn
);
4053 template<bool big_endian
>
4054 static unsigned char*
4055 savefpr0_tail(unsigned char* p
, int r
)
4057 p
= savefpr
<big_endian
>(p
, r
);
4058 write_insn
<big_endian
>(p
, std_0_1
+ 16);
4060 write_insn
<big_endian
>(p
, blr
);
4064 template<bool big_endian
>
4065 static unsigned char*
4066 restfpr(unsigned char* p
, int r
)
4068 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4069 write_insn
<big_endian
>(p
, insn
);
4073 template<bool big_endian
>
4074 static unsigned char*
4075 restfpr0_tail(unsigned char* p
, int r
)
4077 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
4079 p
= restfpr
<big_endian
>(p
, r
);
4080 write_insn
<big_endian
>(p
, mtlr_0
);
4084 p
= restfpr
<big_endian
>(p
, 30);
4085 p
= restfpr
<big_endian
>(p
, 31);
4087 write_insn
<big_endian
>(p
, blr
);
4091 template<bool big_endian
>
4092 static unsigned char*
4093 savefpr1_tail(unsigned char* p
, int r
)
4095 p
= savefpr
<big_endian
>(p
, r
);
4096 write_insn
<big_endian
>(p
, blr
);
4100 template<bool big_endian
>
4101 static unsigned char*
4102 restfpr1_tail(unsigned char* p
, int r
)
4104 p
= restfpr
<big_endian
>(p
, r
);
4105 write_insn
<big_endian
>(p
, blr
);
4109 template<bool big_endian
>
4110 static unsigned char*
4111 savevr(unsigned char* p
, int r
)
4113 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
4114 write_insn
<big_endian
>(p
, insn
);
4116 insn
= stvx_0_12_0
+ (r
<< 21);
4117 write_insn
<big_endian
>(p
, insn
);
4121 template<bool big_endian
>
4122 static unsigned char*
4123 savevr_tail(unsigned char* p
, int r
)
4125 p
= savevr
<big_endian
>(p
, r
);
4126 write_insn
<big_endian
>(p
, blr
);
4130 template<bool big_endian
>
4131 static unsigned char*
4132 restvr(unsigned char* p
, int r
)
4134 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
4135 write_insn
<big_endian
>(p
, insn
);
4137 insn
= lvx_0_12_0
+ (r
<< 21);
4138 write_insn
<big_endian
>(p
, insn
);
4142 template<bool big_endian
>
4143 static unsigned char*
4144 restvr_tail(unsigned char* p
, int r
)
4146 p
= restvr
<big_endian
>(p
, r
);
4147 write_insn
<big_endian
>(p
, blr
);
4152 template<int size
, bool big_endian
>
4153 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
4154 Symbol_table
* symtab
)
4155 : Output_section_data_build(4),
4158 this->savres_define(symtab
,
4159 "_savegpr0_", 14, 31,
4160 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
4161 this->savres_define(symtab
,
4162 "_restgpr0_", 14, 29,
4163 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
4164 this->savres_define(symtab
,
4165 "_restgpr0_", 30, 31,
4166 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
4167 this->savres_define(symtab
,
4168 "_savegpr1_", 14, 31,
4169 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
4170 this->savres_define(symtab
,
4171 "_restgpr1_", 14, 31,
4172 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
4173 this->savres_define(symtab
,
4174 "_savefpr_", 14, 31,
4175 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
4176 this->savres_define(symtab
,
4177 "_restfpr_", 14, 29,
4178 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
4179 this->savres_define(symtab
,
4180 "_restfpr_", 30, 31,
4181 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
4182 this->savres_define(symtab
,
4184 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
4185 this->savres_define(symtab
,
4187 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
4188 this->savres_define(symtab
,
4190 savevr
<big_endian
>, savevr_tail
<big_endian
>);
4191 this->savres_define(symtab
,
4193 restvr
<big_endian
>, restvr_tail
<big_endian
>);
4196 template<int size
, bool big_endian
>
4198 Output_data_save_res
<size
, big_endian
>::savres_define(
4199 Symbol_table
* symtab
,
4201 unsigned int lo
, unsigned int hi
,
4202 unsigned char* write_ent(unsigned char*, int),
4203 unsigned char* write_tail(unsigned char*, int))
4205 size_t len
= strlen(name
);
4206 bool writing
= false;
4209 memcpy(sym
, name
, len
);
4212 for (unsigned int i
= lo
; i
<= hi
; i
++)
4214 sym
[len
+ 0] = i
/ 10 + '0';
4215 sym
[len
+ 1] = i
% 10 + '0';
4216 Symbol
* gsym
= symtab
->lookup(sym
);
4217 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
4218 writing
= writing
|| refd
;
4221 if (this->contents_
== NULL
)
4222 this->contents_
= new unsigned char[this->savres_max
];
4224 section_size_type value
= this->current_data_size();
4225 unsigned char* p
= this->contents_
+ value
;
4227 p
= write_ent(p
, i
);
4229 p
= write_tail(p
, i
);
4230 section_size_type cur_size
= p
- this->contents_
;
4231 this->set_current_data_size(cur_size
);
4233 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
4234 this, value
, cur_size
- value
,
4235 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
4236 elfcpp::STV_HIDDEN
, 0, false, false);
4241 // Write out save/restore.
4243 template<int size
, bool big_endian
>
4245 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
4247 const section_size_type off
= this->offset();
4248 const section_size_type oview_size
=
4249 convert_to_section_size_type(this->data_size());
4250 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4251 memcpy(oview
, this->contents_
, oview_size
);
4252 of
->write_output_view(off
, oview_size
, oview
);
4256 // Create the glink section.
4258 template<int size
, bool big_endian
>
4260 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
4262 if (this->glink_
== NULL
)
4264 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
4265 this->glink_
->add_eh_frame(layout
);
4266 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
4267 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
4268 this->glink_
, ORDER_TEXT
, false);
4272 // Create a PLT entry for a global symbol.
4274 template<int size
, bool big_endian
>
4276 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
4280 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4281 && gsym
->can_use_relative_reloc(false))
4283 if (this->iplt_
== NULL
)
4284 this->make_iplt_section(symtab
, layout
);
4285 this->iplt_
->add_ifunc_entry(gsym
);
4289 if (this->plt_
== NULL
)
4290 this->make_plt_section(symtab
, layout
);
4291 this->plt_
->add_entry(gsym
);
4295 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
4297 template<int size
, bool big_endian
>
4299 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
4300 Symbol_table
* symtab
,
4302 Sized_relobj_file
<size
, big_endian
>* relobj
,
4305 if (this->iplt_
== NULL
)
4306 this->make_iplt_section(symtab
, layout
);
4307 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
4310 // Return the number of entries in the PLT.
4312 template<int size
, bool big_endian
>
4314 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
4316 if (this->plt_
== NULL
)
4318 unsigned int count
= this->plt_
->entry_count();
4319 if (this->iplt_
!= NULL
)
4320 count
+= this->iplt_
->entry_count();
4324 // Return the offset of the first non-reserved PLT entry.
4326 template<int size
, bool big_endian
>
4328 Target_powerpc
<size
, big_endian
>::first_plt_entry_offset() const
4330 return this->plt_
->first_plt_entry_offset();
4333 // Return the size of each PLT entry.
4335 template<int size
, bool big_endian
>
4337 Target_powerpc
<size
, big_endian
>::plt_entry_size() const
4339 return Output_data_plt_powerpc
<size
, big_endian
>::get_plt_entry_size();
4342 // Create a GOT entry for local dynamic __tls_get_addr calls.
4344 template<int size
, bool big_endian
>
4346 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
4347 Symbol_table
* symtab
,
4349 Sized_relobj_file
<size
, big_endian
>* object
)
4351 if (this->tlsld_got_offset_
== -1U)
4353 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
4354 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
4355 Output_data_got_powerpc
<size
, big_endian
>* got
4356 = this->got_section(symtab
, layout
);
4357 unsigned int got_offset
= got
->add_constant_pair(0, 0);
4358 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
4360 this->tlsld_got_offset_
= got_offset
;
4362 return this->tlsld_got_offset_
;
4365 // Get the Reference_flags for a particular relocation.
4367 template<int size
, bool big_endian
>
4369 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(unsigned int r_type
)
4373 case elfcpp::R_POWERPC_NONE
:
4374 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
4375 case elfcpp::R_POWERPC_GNU_VTENTRY
:
4376 case elfcpp::R_PPC64_TOC
:
4377 // No symbol reference.
4380 case elfcpp::R_PPC64_ADDR64
:
4381 case elfcpp::R_PPC64_UADDR64
:
4382 case elfcpp::R_POWERPC_ADDR32
:
4383 case elfcpp::R_POWERPC_UADDR32
:
4384 case elfcpp::R_POWERPC_ADDR16
:
4385 case elfcpp::R_POWERPC_UADDR16
:
4386 case elfcpp::R_POWERPC_ADDR16_LO
:
4387 case elfcpp::R_POWERPC_ADDR16_HI
:
4388 case elfcpp::R_POWERPC_ADDR16_HA
:
4389 return Symbol::ABSOLUTE_REF
;
4391 case elfcpp::R_POWERPC_ADDR24
:
4392 case elfcpp::R_POWERPC_ADDR14
:
4393 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4394 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4395 return Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
4397 case elfcpp::R_PPC64_REL64
:
4398 case elfcpp::R_POWERPC_REL32
:
4399 case elfcpp::R_PPC_LOCAL24PC
:
4400 case elfcpp::R_POWERPC_REL16
:
4401 case elfcpp::R_POWERPC_REL16_LO
:
4402 case elfcpp::R_POWERPC_REL16_HI
:
4403 case elfcpp::R_POWERPC_REL16_HA
:
4404 return Symbol::RELATIVE_REF
;
4406 case elfcpp::R_POWERPC_REL24
:
4407 case elfcpp::R_PPC_PLTREL24
:
4408 case elfcpp::R_POWERPC_REL14
:
4409 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4410 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4411 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
4413 case elfcpp::R_POWERPC_GOT16
:
4414 case elfcpp::R_POWERPC_GOT16_LO
:
4415 case elfcpp::R_POWERPC_GOT16_HI
:
4416 case elfcpp::R_POWERPC_GOT16_HA
:
4417 case elfcpp::R_PPC64_GOT16_DS
:
4418 case elfcpp::R_PPC64_GOT16_LO_DS
:
4419 case elfcpp::R_PPC64_TOC16
:
4420 case elfcpp::R_PPC64_TOC16_LO
:
4421 case elfcpp::R_PPC64_TOC16_HI
:
4422 case elfcpp::R_PPC64_TOC16_HA
:
4423 case elfcpp::R_PPC64_TOC16_DS
:
4424 case elfcpp::R_PPC64_TOC16_LO_DS
:
4426 return Symbol::ABSOLUTE_REF
;
4428 case elfcpp::R_POWERPC_GOT_TPREL16
:
4429 case elfcpp::R_POWERPC_TLS
:
4430 return Symbol::TLS_REF
;
4432 case elfcpp::R_POWERPC_COPY
:
4433 case elfcpp::R_POWERPC_GLOB_DAT
:
4434 case elfcpp::R_POWERPC_JMP_SLOT
:
4435 case elfcpp::R_POWERPC_RELATIVE
:
4436 case elfcpp::R_POWERPC_DTPMOD
:
4438 // Not expected. We will give an error later.
4443 // Report an unsupported relocation against a local symbol.
4445 template<int size
, bool big_endian
>
4447 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
4448 Sized_relobj_file
<size
, big_endian
>* object
,
4449 unsigned int r_type
)
4451 gold_error(_("%s: unsupported reloc %u against local symbol"),
4452 object
->name().c_str(), r_type
);
4455 // We are about to emit a dynamic relocation of type R_TYPE. If the
4456 // dynamic linker does not support it, issue an error.
4458 template<int size
, bool big_endian
>
4460 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
4461 unsigned int r_type
)
4463 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
4465 // These are the relocation types supported by glibc for both 32-bit
4466 // and 64-bit powerpc.
4469 case elfcpp::R_POWERPC_NONE
:
4470 case elfcpp::R_POWERPC_RELATIVE
:
4471 case elfcpp::R_POWERPC_GLOB_DAT
:
4472 case elfcpp::R_POWERPC_DTPMOD
:
4473 case elfcpp::R_POWERPC_DTPREL
:
4474 case elfcpp::R_POWERPC_TPREL
:
4475 case elfcpp::R_POWERPC_JMP_SLOT
:
4476 case elfcpp::R_POWERPC_COPY
:
4477 case elfcpp::R_POWERPC_IRELATIVE
:
4478 case elfcpp::R_POWERPC_ADDR32
:
4479 case elfcpp::R_POWERPC_UADDR32
:
4480 case elfcpp::R_POWERPC_ADDR24
:
4481 case elfcpp::R_POWERPC_ADDR16
:
4482 case elfcpp::R_POWERPC_UADDR16
:
4483 case elfcpp::R_POWERPC_ADDR16_LO
:
4484 case elfcpp::R_POWERPC_ADDR16_HI
:
4485 case elfcpp::R_POWERPC_ADDR16_HA
:
4486 case elfcpp::R_POWERPC_ADDR14
:
4487 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4488 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4489 case elfcpp::R_POWERPC_REL32
:
4490 case elfcpp::R_POWERPC_REL24
:
4491 case elfcpp::R_POWERPC_TPREL16
:
4492 case elfcpp::R_POWERPC_TPREL16_LO
:
4493 case elfcpp::R_POWERPC_TPREL16_HI
:
4494 case elfcpp::R_POWERPC_TPREL16_HA
:
4505 // These are the relocation types supported only on 64-bit.
4506 case elfcpp::R_PPC64_ADDR64
:
4507 case elfcpp::R_PPC64_UADDR64
:
4508 case elfcpp::R_PPC64_JMP_IREL
:
4509 case elfcpp::R_PPC64_ADDR16_DS
:
4510 case elfcpp::R_PPC64_ADDR16_LO_DS
:
4511 case elfcpp::R_PPC64_ADDR16_HIGHER
:
4512 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
4513 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
4514 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
4515 case elfcpp::R_PPC64_REL64
:
4516 case elfcpp::R_POWERPC_ADDR30
:
4517 case elfcpp::R_PPC64_TPREL16_DS
:
4518 case elfcpp::R_PPC64_TPREL16_LO_DS
:
4519 case elfcpp::R_PPC64_TPREL16_HIGHER
:
4520 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
4521 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
4522 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
4533 // These are the relocation types supported only on 32-bit.
4534 // ??? glibc ld.so doesn't need to support these.
4535 case elfcpp::R_POWERPC_DTPREL16
:
4536 case elfcpp::R_POWERPC_DTPREL16_LO
:
4537 case elfcpp::R_POWERPC_DTPREL16_HI
:
4538 case elfcpp::R_POWERPC_DTPREL16_HA
:
4546 // This prevents us from issuing more than one error per reloc
4547 // section. But we can still wind up issuing more than one
4548 // error per object file.
4549 if (this->issued_non_pic_error_
)
4551 gold_assert(parameters
->options().output_is_position_independent());
4552 object
->error(_("requires unsupported dynamic reloc; "
4553 "recompile with -fPIC"));
4554 this->issued_non_pic_error_
= true;
4558 // Return whether we need to make a PLT entry for a relocation of the
4559 // given type against a STT_GNU_IFUNC symbol.
4561 template<int size
, bool big_endian
>
4563 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
4564 Sized_relobj_file
<size
, big_endian
>* object
,
4565 unsigned int r_type
)
4567 // In non-pic code any reference will resolve to the plt call stub
4568 // for the ifunc symbol.
4569 if (size
== 32 && !parameters
->options().output_is_position_independent())
4574 // Word size refs from data sections are OK.
4575 case elfcpp::R_POWERPC_ADDR32
:
4576 case elfcpp::R_POWERPC_UADDR32
:
4581 case elfcpp::R_PPC64_ADDR64
:
4582 case elfcpp::R_PPC64_UADDR64
:
4587 // GOT refs are good.
4588 case elfcpp::R_POWERPC_GOT16
:
4589 case elfcpp::R_POWERPC_GOT16_LO
:
4590 case elfcpp::R_POWERPC_GOT16_HI
:
4591 case elfcpp::R_POWERPC_GOT16_HA
:
4592 case elfcpp::R_PPC64_GOT16_DS
:
4593 case elfcpp::R_PPC64_GOT16_LO_DS
:
4596 // So are function calls.
4597 case elfcpp::R_POWERPC_ADDR24
:
4598 case elfcpp::R_POWERPC_ADDR14
:
4599 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4600 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4601 case elfcpp::R_POWERPC_REL24
:
4602 case elfcpp::R_PPC_PLTREL24
:
4603 case elfcpp::R_POWERPC_REL14
:
4604 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4605 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4612 // Anything else is a problem.
4613 // If we are building a static executable, the libc startup function
4614 // responsible for applying indirect function relocations is going
4615 // to complain about the reloc type.
4616 // If we are building a dynamic executable, we will have a text
4617 // relocation. The dynamic loader will set the text segment
4618 // writable and non-executable to apply text relocations. So we'll
4619 // segfault when trying to run the indirection function to resolve
4621 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
4622 object
->name().c_str(), r_type
);
4626 // Scan a relocation for a local symbol.
4628 template<int size
, bool big_endian
>
4630 Target_powerpc
<size
, big_endian
>::Scan::local(
4631 Symbol_table
* symtab
,
4633 Target_powerpc
<size
, big_endian
>* target
,
4634 Sized_relobj_file
<size
, big_endian
>* object
,
4635 unsigned int data_shndx
,
4636 Output_section
* output_section
,
4637 const elfcpp::Rela
<size
, big_endian
>& reloc
,
4638 unsigned int r_type
,
4639 const elfcpp::Sym
<size
, big_endian
>& lsym
,
4642 this->maybe_skip_tls_get_addr_call(r_type
, NULL
);
4644 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
4645 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
4647 this->expect_tls_get_addr_call();
4648 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
4649 if (tls_type
!= tls::TLSOPT_NONE
)
4650 this->skip_next_tls_get_addr_call();
4652 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
4653 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
4655 this->expect_tls_get_addr_call();
4656 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
4657 if (tls_type
!= tls::TLSOPT_NONE
)
4658 this->skip_next_tls_get_addr_call();
4661 Powerpc_relobj
<size
, big_endian
>* ppc_object
4662 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
4667 && data_shndx
== ppc_object
->opd_shndx()
4668 && r_type
== elfcpp::R_PPC64_ADDR64
)
4669 ppc_object
->set_opd_discard(reloc
.get_r_offset());
4673 // A local STT_GNU_IFUNC symbol may require a PLT entry.
4674 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
4675 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
4677 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4678 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4679 r_type
, r_sym
, reloc
.get_r_addend());
4680 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
4685 case elfcpp::R_POWERPC_NONE
:
4686 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
4687 case elfcpp::R_POWERPC_GNU_VTENTRY
:
4688 case elfcpp::R_PPC64_TOCSAVE
:
4689 case elfcpp::R_PPC_EMB_MRKREF
:
4690 case elfcpp::R_POWERPC_TLS
:
4693 case elfcpp::R_PPC64_TOC
:
4695 Output_data_got_powerpc
<size
, big_endian
>* got
4696 = target
->got_section(symtab
, layout
);
4697 if (parameters
->options().output_is_position_independent())
4699 Address off
= reloc
.get_r_offset();
4701 && data_shndx
== ppc_object
->opd_shndx()
4702 && ppc_object
->get_opd_discard(off
- 8))
4705 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4706 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
4707 rela_dyn
->add_output_section_relative(got
->output_section(),
4708 elfcpp::R_POWERPC_RELATIVE
,
4710 object
, data_shndx
, off
,
4711 symobj
->toc_base_offset());
4716 case elfcpp::R_PPC64_ADDR64
:
4717 case elfcpp::R_PPC64_UADDR64
:
4718 case elfcpp::R_POWERPC_ADDR32
:
4719 case elfcpp::R_POWERPC_UADDR32
:
4720 case elfcpp::R_POWERPC_ADDR24
:
4721 case elfcpp::R_POWERPC_ADDR16
:
4722 case elfcpp::R_POWERPC_ADDR16_LO
:
4723 case elfcpp::R_POWERPC_ADDR16_HI
:
4724 case elfcpp::R_POWERPC_ADDR16_HA
:
4725 case elfcpp::R_POWERPC_UADDR16
:
4726 case elfcpp::R_PPC64_ADDR16_HIGHER
:
4727 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
4728 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
4729 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
4730 case elfcpp::R_PPC64_ADDR16_DS
:
4731 case elfcpp::R_PPC64_ADDR16_LO_DS
:
4732 case elfcpp::R_POWERPC_ADDR14
:
4733 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4734 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4735 // If building a shared library (or a position-independent
4736 // executable), we need to create a dynamic relocation for
4738 if (parameters
->options().output_is_position_independent()
4739 || (size
== 64 && is_ifunc
))
4741 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4743 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
4744 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
4746 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4747 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
4750 rela_dyn
= target
->iplt_section()->rel_plt();
4751 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
4753 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
4754 output_section
, data_shndx
,
4755 reloc
.get_r_offset(),
4756 reloc
.get_r_addend(), false);
4760 check_non_pic(object
, r_type
);
4761 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4762 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
4763 data_shndx
, reloc
.get_r_offset(),
4764 reloc
.get_r_addend());
4769 case elfcpp::R_POWERPC_REL24
:
4770 case elfcpp::R_PPC_PLTREL24
:
4771 case elfcpp::R_PPC_LOCAL24PC
:
4772 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4773 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4774 reloc
.get_r_addend());
4777 case elfcpp::R_POWERPC_REL14
:
4778 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4779 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4780 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4781 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4782 reloc
.get_r_addend());
4785 case elfcpp::R_PPC64_REL64
:
4786 case elfcpp::R_POWERPC_REL32
:
4787 case elfcpp::R_POWERPC_REL16
:
4788 case elfcpp::R_POWERPC_REL16_LO
:
4789 case elfcpp::R_POWERPC_REL16_HI
:
4790 case elfcpp::R_POWERPC_REL16_HA
:
4791 case elfcpp::R_POWERPC_SECTOFF
:
4792 case elfcpp::R_POWERPC_TPREL16
:
4793 case elfcpp::R_POWERPC_DTPREL16
:
4794 case elfcpp::R_POWERPC_SECTOFF_LO
:
4795 case elfcpp::R_POWERPC_TPREL16_LO
:
4796 case elfcpp::R_POWERPC_DTPREL16_LO
:
4797 case elfcpp::R_POWERPC_SECTOFF_HI
:
4798 case elfcpp::R_POWERPC_TPREL16_HI
:
4799 case elfcpp::R_POWERPC_DTPREL16_HI
:
4800 case elfcpp::R_POWERPC_SECTOFF_HA
:
4801 case elfcpp::R_POWERPC_TPREL16_HA
:
4802 case elfcpp::R_POWERPC_DTPREL16_HA
:
4803 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
4804 case elfcpp::R_PPC64_TPREL16_HIGHER
:
4805 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
4806 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
4807 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
4808 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
4809 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
4810 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
4811 case elfcpp::R_PPC64_TPREL16_DS
:
4812 case elfcpp::R_PPC64_TPREL16_LO_DS
:
4813 case elfcpp::R_PPC64_DTPREL16_DS
:
4814 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
4815 case elfcpp::R_PPC64_SECTOFF_DS
:
4816 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
4817 case elfcpp::R_PPC64_TLSGD
:
4818 case elfcpp::R_PPC64_TLSLD
:
4821 case elfcpp::R_POWERPC_GOT16
:
4822 case elfcpp::R_POWERPC_GOT16_LO
:
4823 case elfcpp::R_POWERPC_GOT16_HI
:
4824 case elfcpp::R_POWERPC_GOT16_HA
:
4825 case elfcpp::R_PPC64_GOT16_DS
:
4826 case elfcpp::R_PPC64_GOT16_LO_DS
:
4828 // The symbol requires a GOT entry.
4829 Output_data_got_powerpc
<size
, big_endian
>* got
4830 = target
->got_section(symtab
, layout
);
4831 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4833 if (!parameters
->options().output_is_position_independent())
4835 if (size
== 32 && is_ifunc
)
4836 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
4838 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
4840 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
4842 // If we are generating a shared object or a pie, this
4843 // symbol's GOT entry will be set by a dynamic relocation.
4845 off
= got
->add_constant(0);
4846 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
4848 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4849 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
4852 rela_dyn
= target
->iplt_section()->rel_plt();
4853 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
4855 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
4856 got
, off
, 0, false);
4861 case elfcpp::R_PPC64_TOC16
:
4862 case elfcpp::R_PPC64_TOC16_LO
:
4863 case elfcpp::R_PPC64_TOC16_HI
:
4864 case elfcpp::R_PPC64_TOC16_HA
:
4865 case elfcpp::R_PPC64_TOC16_DS
:
4866 case elfcpp::R_PPC64_TOC16_LO_DS
:
4867 // We need a GOT section.
4868 target
->got_section(symtab
, layout
);
4871 case elfcpp::R_POWERPC_GOT_TLSGD16
:
4872 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
4873 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
4874 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
4876 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
4877 if (tls_type
== tls::TLSOPT_NONE
)
4879 Output_data_got_powerpc
<size
, big_endian
>* got
4880 = target
->got_section(symtab
, layout
);
4881 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4882 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4883 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
4884 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
4886 else if (tls_type
== tls::TLSOPT_TO_LE
)
4888 // no GOT relocs needed for Local Exec.
4895 case elfcpp::R_POWERPC_GOT_TLSLD16
:
4896 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
4897 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
4898 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
4900 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
4901 if (tls_type
== tls::TLSOPT_NONE
)
4902 target
->tlsld_got_offset(symtab
, layout
, object
);
4903 else if (tls_type
== tls::TLSOPT_TO_LE
)
4905 // no GOT relocs needed for Local Exec.
4906 if (parameters
->options().emit_relocs())
4908 Output_section
* os
= layout
->tls_segment()->first_section();
4909 gold_assert(os
!= NULL
);
4910 os
->set_needs_symtab_index();
4918 case elfcpp::R_POWERPC_GOT_DTPREL16
:
4919 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
4920 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
4921 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
4923 Output_data_got_powerpc
<size
, big_endian
>* got
4924 = target
->got_section(symtab
, layout
);
4925 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4926 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
4930 case elfcpp::R_POWERPC_GOT_TPREL16
:
4931 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
4932 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
4933 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
4935 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
4936 if (tls_type
== tls::TLSOPT_NONE
)
4938 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4939 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
4941 Output_data_got_powerpc
<size
, big_endian
>* got
4942 = target
->got_section(symtab
, layout
);
4943 unsigned int off
= got
->add_constant(0);
4944 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
4946 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4947 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
4948 elfcpp::R_POWERPC_TPREL
,
4952 else if (tls_type
== tls::TLSOPT_TO_LE
)
4954 // no GOT relocs needed for Local Exec.
4962 unsupported_reloc_local(object
, r_type
);
4968 case elfcpp::R_POWERPC_GOT_TLSLD16
:
4969 case elfcpp::R_POWERPC_GOT_TLSGD16
:
4970 case elfcpp::R_POWERPC_GOT_TPREL16
:
4971 case elfcpp::R_POWERPC_GOT_DTPREL16
:
4972 case elfcpp::R_POWERPC_GOT16
:
4973 case elfcpp::R_PPC64_GOT16_DS
:
4974 case elfcpp::R_PPC64_TOC16
:
4975 case elfcpp::R_PPC64_TOC16_DS
:
4976 ppc_object
->set_has_small_toc_reloc();
4982 // Report an unsupported relocation against a global symbol.
4984 template<int size
, bool big_endian
>
4986 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
4987 Sized_relobj_file
<size
, big_endian
>* object
,
4988 unsigned int r_type
,
4991 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4992 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
4995 // Scan a relocation for a global symbol.
4997 template<int size
, bool big_endian
>
4999 Target_powerpc
<size
, big_endian
>::Scan::global(
5000 Symbol_table
* symtab
,
5002 Target_powerpc
<size
, big_endian
>* target
,
5003 Sized_relobj_file
<size
, big_endian
>* object
,
5004 unsigned int data_shndx
,
5005 Output_section
* output_section
,
5006 const elfcpp::Rela
<size
, big_endian
>& reloc
,
5007 unsigned int r_type
,
5010 if (this->maybe_skip_tls_get_addr_call(r_type
, gsym
) == Track_tls::SKIP
)
5013 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
5014 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
5016 this->expect_tls_get_addr_call();
5017 const bool final
= gsym
->final_value_is_known();
5018 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
5019 if (tls_type
!= tls::TLSOPT_NONE
)
5020 this->skip_next_tls_get_addr_call();
5022 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
5023 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
5025 this->expect_tls_get_addr_call();
5026 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5027 if (tls_type
!= tls::TLSOPT_NONE
)
5028 this->skip_next_tls_get_addr_call();
5031 Powerpc_relobj
<size
, big_endian
>* ppc_object
5032 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5034 // A STT_GNU_IFUNC symbol may require a PLT entry.
5035 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
5036 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
5038 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5039 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5040 reloc
.get_r_addend());
5041 target
->make_plt_entry(symtab
, layout
, gsym
);
5046 case elfcpp::R_POWERPC_NONE
:
5047 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5048 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5049 case elfcpp::R_PPC_LOCAL24PC
:
5050 case elfcpp::R_PPC_EMB_MRKREF
:
5051 case elfcpp::R_POWERPC_TLS
:
5054 case elfcpp::R_PPC64_TOC
:
5056 Output_data_got_powerpc
<size
, big_endian
>* got
5057 = target
->got_section(symtab
, layout
);
5058 if (parameters
->options().output_is_position_independent())
5060 Address off
= reloc
.get_r_offset();
5062 && data_shndx
== ppc_object
->opd_shndx()
5063 && ppc_object
->get_opd_discard(off
- 8))
5066 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5067 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
5068 if (data_shndx
!= ppc_object
->opd_shndx())
5069 symobj
= static_cast
5070 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
5071 rela_dyn
->add_output_section_relative(got
->output_section(),
5072 elfcpp::R_POWERPC_RELATIVE
,
5074 object
, data_shndx
, off
,
5075 symobj
->toc_base_offset());
5080 case elfcpp::R_PPC64_ADDR64
:
5082 && data_shndx
== ppc_object
->opd_shndx()
5083 && (gsym
->is_defined_in_discarded_section()
5084 || gsym
->object() != object
))
5086 ppc_object
->set_opd_discard(reloc
.get_r_offset());
5090 case elfcpp::R_PPC64_UADDR64
:
5091 case elfcpp::R_POWERPC_ADDR32
:
5092 case elfcpp::R_POWERPC_UADDR32
:
5093 case elfcpp::R_POWERPC_ADDR24
:
5094 case elfcpp::R_POWERPC_ADDR16
:
5095 case elfcpp::R_POWERPC_ADDR16_LO
:
5096 case elfcpp::R_POWERPC_ADDR16_HI
:
5097 case elfcpp::R_POWERPC_ADDR16_HA
:
5098 case elfcpp::R_POWERPC_UADDR16
:
5099 case elfcpp::R_PPC64_ADDR16_HIGHER
:
5100 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
5101 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
5102 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
5103 case elfcpp::R_PPC64_ADDR16_DS
:
5104 case elfcpp::R_PPC64_ADDR16_LO_DS
:
5105 case elfcpp::R_POWERPC_ADDR14
:
5106 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5107 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5109 // Make a PLT entry if necessary.
5110 if (gsym
->needs_plt_entry())
5112 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5114 elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5115 reloc
.get_r_addend());
5116 target
->make_plt_entry(symtab
, layout
, gsym
);
5117 // Since this is not a PC-relative relocation, we may be
5118 // taking the address of a function. In that case we need to
5119 // set the entry in the dynamic symbol table to the address of
5120 // the PLT call stub.
5122 && gsym
->is_from_dynobj()
5123 && !parameters
->options().output_is_position_independent())
5124 gsym
->set_needs_dynsym_value();
5126 // Make a dynamic relocation if necessary.
5127 if (needs_dynamic_reloc
<size
>(gsym
, Scan::get_reference_flags(r_type
))
5128 || (size
== 64 && gsym
->type() == elfcpp::STT_GNU_IFUNC
))
5130 if (gsym
->may_need_copy_reloc())
5132 target
->copy_reloc(symtab
, layout
, object
,
5133 data_shndx
, output_section
, gsym
, reloc
);
5135 else if ((size
== 32
5136 && r_type
== elfcpp::R_POWERPC_ADDR32
5137 && gsym
->can_use_relative_reloc(false)
5138 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
5139 && parameters
->options().shared()))
5141 && r_type
== elfcpp::R_PPC64_ADDR64
5142 && (gsym
->can_use_relative_reloc(false)
5143 || data_shndx
== ppc_object
->opd_shndx())))
5145 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5146 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
5147 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
5149 rela_dyn
= target
->iplt_section()->rel_plt();
5150 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
5152 rela_dyn
->add_symbolless_global_addend(
5153 gsym
, dynrel
, output_section
, object
, data_shndx
,
5154 reloc
.get_r_offset(), reloc
.get_r_addend());
5158 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5159 check_non_pic(object
, r_type
);
5160 rela_dyn
->add_global(gsym
, r_type
, output_section
,
5162 reloc
.get_r_offset(),
5163 reloc
.get_r_addend());
5169 case elfcpp::R_PPC_PLTREL24
:
5170 case elfcpp::R_POWERPC_REL24
:
5171 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5172 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5173 reloc
.get_r_addend());
5174 if (gsym
->needs_plt_entry()
5175 || (!gsym
->final_value_is_known()
5176 && (gsym
->is_undefined()
5177 || gsym
->is_from_dynobj()
5178 || gsym
->is_preemptible())))
5179 target
->make_plt_entry(symtab
, layout
, gsym
);
5182 case elfcpp::R_PPC64_REL64
:
5183 case elfcpp::R_POWERPC_REL32
:
5184 // Make a dynamic relocation if necessary.
5185 if (needs_dynamic_reloc
<size
>(gsym
, Scan::get_reference_flags(r_type
)))
5187 if (gsym
->may_need_copy_reloc())
5189 target
->copy_reloc(symtab
, layout
, object
,
5190 data_shndx
, output_section
, gsym
,
5195 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5196 check_non_pic(object
, r_type
);
5197 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
5198 data_shndx
, reloc
.get_r_offset(),
5199 reloc
.get_r_addend());
5204 case elfcpp::R_POWERPC_REL14
:
5205 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5206 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5207 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5208 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5209 reloc
.get_r_addend());
5212 case elfcpp::R_POWERPC_REL16
:
5213 case elfcpp::R_POWERPC_REL16_LO
:
5214 case elfcpp::R_POWERPC_REL16_HI
:
5215 case elfcpp::R_POWERPC_REL16_HA
:
5216 case elfcpp::R_POWERPC_SECTOFF
:
5217 case elfcpp::R_POWERPC_TPREL16
:
5218 case elfcpp::R_POWERPC_DTPREL16
:
5219 case elfcpp::R_POWERPC_SECTOFF_LO
:
5220 case elfcpp::R_POWERPC_TPREL16_LO
:
5221 case elfcpp::R_POWERPC_DTPREL16_LO
:
5222 case elfcpp::R_POWERPC_SECTOFF_HI
:
5223 case elfcpp::R_POWERPC_TPREL16_HI
:
5224 case elfcpp::R_POWERPC_DTPREL16_HI
:
5225 case elfcpp::R_POWERPC_SECTOFF_HA
:
5226 case elfcpp::R_POWERPC_TPREL16_HA
:
5227 case elfcpp::R_POWERPC_DTPREL16_HA
:
5228 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
5229 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5230 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
5231 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5232 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
5233 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5234 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
5235 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5236 case elfcpp::R_PPC64_TPREL16_DS
:
5237 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5238 case elfcpp::R_PPC64_DTPREL16_DS
:
5239 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
5240 case elfcpp::R_PPC64_SECTOFF_DS
:
5241 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
5242 case elfcpp::R_PPC64_TLSGD
:
5243 case elfcpp::R_PPC64_TLSLD
:
5246 case elfcpp::R_POWERPC_GOT16
:
5247 case elfcpp::R_POWERPC_GOT16_LO
:
5248 case elfcpp::R_POWERPC_GOT16_HI
:
5249 case elfcpp::R_POWERPC_GOT16_HA
:
5250 case elfcpp::R_PPC64_GOT16_DS
:
5251 case elfcpp::R_PPC64_GOT16_LO_DS
:
5253 // The symbol requires a GOT entry.
5254 Output_data_got_powerpc
<size
, big_endian
>* got
;
5256 got
= target
->got_section(symtab
, layout
);
5257 if (gsym
->final_value_is_known())
5259 if (size
== 32 && gsym
->type() == elfcpp::STT_GNU_IFUNC
)
5260 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
5262 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
5264 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
5266 // If we are generating a shared object or a pie, this
5267 // symbol's GOT entry will be set by a dynamic relocation.
5268 unsigned int off
= got
->add_constant(0);
5269 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
5271 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5272 if (gsym
->can_use_relative_reloc(false)
5274 && gsym
->visibility() == elfcpp::STV_PROTECTED
5275 && parameters
->options().shared()))
5277 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
5278 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
5280 rela_dyn
= target
->iplt_section()->rel_plt();
5281 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
5283 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
5287 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
5288 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
5294 case elfcpp::R_PPC64_TOC16
:
5295 case elfcpp::R_PPC64_TOC16_LO
:
5296 case elfcpp::R_PPC64_TOC16_HI
:
5297 case elfcpp::R_PPC64_TOC16_HA
:
5298 case elfcpp::R_PPC64_TOC16_DS
:
5299 case elfcpp::R_PPC64_TOC16_LO_DS
:
5300 // We need a GOT section.
5301 target
->got_section(symtab
, layout
);
5304 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5305 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
5306 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
5307 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
5309 const bool final
= gsym
->final_value_is_known();
5310 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
5311 if (tls_type
== tls::TLSOPT_NONE
)
5313 Output_data_got_powerpc
<size
, big_endian
>* got
5314 = target
->got_section(symtab
, layout
);
5315 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
,
5316 target
->rela_dyn_section(layout
),
5317 elfcpp::R_POWERPC_DTPMOD
,
5318 elfcpp::R_POWERPC_DTPREL
);
5320 else if (tls_type
== tls::TLSOPT_TO_IE
)
5322 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
5324 Output_data_got_powerpc
<size
, big_endian
>* got
5325 = target
->got_section(symtab
, layout
);
5326 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5327 if (gsym
->is_undefined()
5328 || gsym
->is_from_dynobj())
5330 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
5331 elfcpp::R_POWERPC_TPREL
);
5335 unsigned int off
= got
->add_constant(0);
5336 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
5337 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
5338 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
5343 else if (tls_type
== tls::TLSOPT_TO_LE
)
5345 // no GOT relocs needed for Local Exec.
5352 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5353 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
5354 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
5355 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
5357 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5358 if (tls_type
== tls::TLSOPT_NONE
)
5359 target
->tlsld_got_offset(symtab
, layout
, object
);
5360 else if (tls_type
== tls::TLSOPT_TO_LE
)
5362 // no GOT relocs needed for Local Exec.
5363 if (parameters
->options().emit_relocs())
5365 Output_section
* os
= layout
->tls_segment()->first_section();
5366 gold_assert(os
!= NULL
);
5367 os
->set_needs_symtab_index();
5375 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5376 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
5377 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
5378 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
5380 Output_data_got_powerpc
<size
, big_endian
>* got
5381 = target
->got_section(symtab
, layout
);
5382 if (!gsym
->final_value_is_known()
5383 && (gsym
->is_from_dynobj()
5384 || gsym
->is_undefined()
5385 || gsym
->is_preemptible()))
5386 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
5387 target
->rela_dyn_section(layout
),
5388 elfcpp::R_POWERPC_DTPREL
);
5390 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
5394 case elfcpp::R_POWERPC_GOT_TPREL16
:
5395 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
5396 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
5397 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
5399 const bool final
= gsym
->final_value_is_known();
5400 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
5401 if (tls_type
== tls::TLSOPT_NONE
)
5403 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
5405 Output_data_got_powerpc
<size
, big_endian
>* got
5406 = target
->got_section(symtab
, layout
);
5407 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5408 if (gsym
->is_undefined()
5409 || gsym
->is_from_dynobj())
5411 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
5412 elfcpp::R_POWERPC_TPREL
);
5416 unsigned int off
= got
->add_constant(0);
5417 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
5418 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
5419 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
5424 else if (tls_type
== tls::TLSOPT_TO_LE
)
5426 // no GOT relocs needed for Local Exec.
5434 unsupported_reloc_global(object
, r_type
, gsym
);
5440 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5441 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5442 case elfcpp::R_POWERPC_GOT_TPREL16
:
5443 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5444 case elfcpp::R_POWERPC_GOT16
:
5445 case elfcpp::R_PPC64_GOT16_DS
:
5446 case elfcpp::R_PPC64_TOC16
:
5447 case elfcpp::R_PPC64_TOC16_DS
:
5448 ppc_object
->set_has_small_toc_reloc();
5454 // Process relocations for gc.
5456 template<int size
, bool big_endian
>
5458 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
5459 Symbol_table
* symtab
,
5461 Sized_relobj_file
<size
, big_endian
>* object
,
5462 unsigned int data_shndx
,
5464 const unsigned char* prelocs
,
5466 Output_section
* output_section
,
5467 bool needs_special_offset_handling
,
5468 size_t local_symbol_count
,
5469 const unsigned char* plocal_symbols
)
5471 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
5472 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
5473 Powerpc_relobj
<size
, big_endian
>* ppc_object
5474 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5476 ppc_object
->set_opd_valid();
5477 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
5479 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
5480 for (p
= ppc_object
->access_from_map()->begin();
5481 p
!= ppc_object
->access_from_map()->end();
5484 Address dst_off
= p
->first
;
5485 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
5486 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
5487 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
5489 Object
* src_obj
= s
->first
;
5490 unsigned int src_indx
= s
->second
;
5491 symtab
->gc()->add_reference(src_obj
, src_indx
,
5492 ppc_object
, dst_indx
);
5496 ppc_object
->access_from_map()->clear();
5497 ppc_object
->process_gc_mark(symtab
);
5498 // Don't look at .opd relocs as .opd will reference everything.
5502 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
5503 typename
Target_powerpc::Relocatable_size_for_reloc
>(
5512 needs_special_offset_handling
,
5517 // Handle target specific gc actions when adding a gc reference from
5518 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
5519 // and DST_OFF. For powerpc64, this adds a referenc to the code
5520 // section of a function descriptor.
5522 template<int size
, bool big_endian
>
5524 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
5525 Symbol_table
* symtab
,
5527 unsigned int src_shndx
,
5529 unsigned int dst_shndx
,
5530 Address dst_off
) const
5532 if (size
!= 64 || dst_obj
->is_dynamic())
5535 Powerpc_relobj
<size
, big_endian
>* ppc_object
5536 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
5537 if (dst_shndx
== ppc_object
->opd_shndx())
5539 if (ppc_object
->opd_valid())
5541 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
5542 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
5546 // If we haven't run scan_opd_relocs, we must delay
5547 // processing this function descriptor reference.
5548 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
5553 // Add any special sections for this symbol to the gc work list.
5554 // For powerpc64, this adds the code section of a function
5557 template<int size
, bool big_endian
>
5559 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
5560 Symbol_table
* symtab
,
5565 Powerpc_relobj
<size
, big_endian
>* ppc_object
5566 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
5568 unsigned int shndx
= sym
->shndx(&is_ordinary
);
5569 if (is_ordinary
&& shndx
== ppc_object
->opd_shndx())
5571 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
5572 Address dst_off
= gsym
->value();
5573 if (ppc_object
->opd_valid())
5575 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
5576 symtab
->gc()->worklist().push(Section_id(ppc_object
, dst_indx
));
5579 ppc_object
->add_gc_mark(dst_off
);
5584 // Scan relocations for a section.
5586 template<int size
, bool big_endian
>
5588 Target_powerpc
<size
, big_endian
>::scan_relocs(
5589 Symbol_table
* symtab
,
5591 Sized_relobj_file
<size
, big_endian
>* object
,
5592 unsigned int data_shndx
,
5593 unsigned int sh_type
,
5594 const unsigned char* prelocs
,
5596 Output_section
* output_section
,
5597 bool needs_special_offset_handling
,
5598 size_t local_symbol_count
,
5599 const unsigned char* plocal_symbols
)
5601 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
5602 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
5604 if (sh_type
== elfcpp::SHT_REL
)
5606 gold_error(_("%s: unsupported REL reloc section"),
5607 object
->name().c_str());
5611 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
5620 needs_special_offset_handling
,
5625 // Functor class for processing the global symbol table.
5626 // Removes symbols defined on discarded opd entries.
5628 template<bool big_endian
>
5629 class Global_symbol_visitor_opd
5632 Global_symbol_visitor_opd()
5636 operator()(Sized_symbol
<64>* sym
)
5638 if (sym
->has_symtab_index()
5639 || sym
->source() != Symbol::FROM_OBJECT
5640 || !sym
->in_real_elf())
5643 if (sym
->object()->is_dynamic())
5646 Powerpc_relobj
<64, big_endian
>* symobj
5647 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
5648 if (symobj
->opd_shndx() == 0)
5652 unsigned int shndx
= sym
->shndx(&is_ordinary
);
5653 if (shndx
== symobj
->opd_shndx()
5654 && symobj
->get_opd_discard(sym
->value()))
5655 sym
->set_symtab_index(-1U);
5659 template<int size
, bool big_endian
>
5661 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
5663 Symbol_table
* symtab
)
5667 Output_data_save_res
<64, big_endian
>* savres
5668 = new Output_data_save_res
<64, big_endian
>(symtab
);
5669 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
5670 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
5671 savres
, ORDER_TEXT
, false);
5675 // Sort linker created .got section first (for the header), then input
5676 // sections belonging to files using small model code.
5678 template<bool big_endian
>
5679 class Sort_toc_sections
5683 operator()(const Output_section::Input_section
& is1
,
5684 const Output_section::Input_section
& is2
) const
5686 if (!is1
.is_input_section() && is2
.is_input_section())
5689 = (is1
.is_input_section()
5690 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
5691 ->has_small_toc_reloc()));
5693 = (is2
.is_input_section()
5694 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
5695 ->has_small_toc_reloc()));
5696 return small1
&& !small2
;
5700 // Finalize the sections.
5702 template<int size
, bool big_endian
>
5704 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
5706 const Input_objects
*,
5707 Symbol_table
* symtab
)
5709 if (parameters
->doing_static_link())
5711 // At least some versions of glibc elf-init.o have a strong
5712 // reference to __rela_iplt marker syms. A weak ref would be
5714 if (this->iplt_
!= NULL
)
5716 Reloc_section
* rel
= this->iplt_
->rel_plt();
5717 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
5718 Symbol_table::PREDEFINED
, rel
, 0, 0,
5719 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5720 elfcpp::STV_HIDDEN
, 0, false, true);
5721 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
5722 Symbol_table::PREDEFINED
, rel
, 0, 0,
5723 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5724 elfcpp::STV_HIDDEN
, 0, true, true);
5728 symtab
->define_as_constant("__rela_iplt_start", NULL
,
5729 Symbol_table::PREDEFINED
, 0, 0,
5730 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5731 elfcpp::STV_HIDDEN
, 0, true, false);
5732 symtab
->define_as_constant("__rela_iplt_end", NULL
,
5733 Symbol_table::PREDEFINED
, 0, 0,
5734 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5735 elfcpp::STV_HIDDEN
, 0, true, false);
5741 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
5742 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
5744 if (!parameters
->options().relocatable())
5746 this->define_save_restore_funcs(layout
, symtab
);
5748 // Annoyingly, we need to make these sections now whether or
5749 // not we need them. If we delay until do_relax then we
5750 // need to mess with the relaxation machinery checkpointing.
5751 this->got_section(symtab
, layout
);
5752 this->make_brlt_section(layout
);
5754 if (parameters
->options().toc_sort())
5756 Output_section
* os
= this->got_
->output_section();
5757 if (os
!= NULL
&& os
->input_sections().size() > 1)
5758 std::stable_sort(os
->input_sections().begin(),
5759 os
->input_sections().end(),
5760 Sort_toc_sections
<big_endian
>());
5765 // Fill in some more dynamic tags.
5766 Output_data_dynamic
* odyn
= layout
->dynamic_data();
5769 const Reloc_section
* rel_plt
= (this->plt_
== NULL
5771 : this->plt_
->rel_plt());
5772 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
5773 this->rela_dyn_
, true, size
== 32);
5777 if (this->got_
!= NULL
)
5779 this->got_
->finalize_data_size();
5780 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
5781 this->got_
, this->got_
->g_o_t());
5786 if (this->glink_
!= NULL
)
5788 this->glink_
->finalize_data_size();
5789 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
5791 (this->glink_
->pltresolve_size
5797 // Emit any relocs we saved in an attempt to avoid generating COPY
5799 if (this->copy_relocs_
.any_saved_relocs())
5800 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
5803 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
5807 ok_lo_toc_insn(uint32_t insn
)
5809 return ((insn
& (0x3f << 26)) == 14u << 26 /* addi */
5810 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
5811 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
5812 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
5813 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
5814 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
5815 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
5816 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
5817 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
5818 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
5819 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
5820 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
5821 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
5822 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
5823 || ((insn
& (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
5825 || ((insn
& (0x3f << 26)) == 62u << 26 /* std, stmd */
5826 && ((insn
& 3) == 0 || (insn
& 3) == 3))
5827 || (insn
& (0x3f << 26)) == 12u << 26 /* addic */);
5830 // Return the value to use for a branch relocation.
5832 template<int size
, bool big_endian
>
5833 typename Target_powerpc
<size
, big_endian
>::Address
5834 Target_powerpc
<size
, big_endian
>::symval_for_branch(
5835 const Symbol_table
* symtab
,
5837 const Sized_symbol
<size
>* gsym
,
5838 Powerpc_relobj
<size
, big_endian
>* object
,
5839 unsigned int *dest_shndx
)
5845 // If the symbol is defined in an opd section, ie. is a function
5846 // descriptor, use the function descriptor code entry address
5847 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
5849 && gsym
->source() != Symbol::FROM_OBJECT
)
5852 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
5853 unsigned int shndx
= symobj
->opd_shndx();
5856 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
5857 gold_assert(opd_addr
!= invalid_address
);
5858 opd_addr
+= symobj
->output_section(shndx
)->address();
5859 if (value
>= opd_addr
&& value
< opd_addr
+ symobj
->section_size(shndx
))
5862 *dest_shndx
= symobj
->get_opd_ent(value
- opd_addr
, &sec_off
);
5863 if (symtab
->is_section_folded(symobj
, *dest_shndx
))
5866 = symtab
->icf()->get_folded_section(symobj
, *dest_shndx
);
5867 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(folded
.first
);
5868 *dest_shndx
= folded
.second
;
5870 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
5871 gold_assert(sec_addr
!= invalid_address
);
5872 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
5873 value
= sec_addr
+ sec_off
;
5878 // Perform a relocation.
5880 template<int size
, bool big_endian
>
5882 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
5883 const Relocate_info
<size
, big_endian
>* relinfo
,
5884 Target_powerpc
* target
,
5887 const elfcpp::Rela
<size
, big_endian
>& rela
,
5888 unsigned int r_type
,
5889 const Sized_symbol
<size
>* gsym
,
5890 const Symbol_value
<size
>* psymval
,
5891 unsigned char* view
,
5893 section_size_type view_size
)
5895 switch (this->maybe_skip_tls_get_addr_call(r_type
, gsym
))
5897 case Track_tls::NOT_EXPECTED
:
5898 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5899 _("__tls_get_addr call lacks marker reloc"));
5901 case Track_tls::EXPECTED
:
5902 // We have already complained.
5904 case Track_tls::SKIP
:
5906 case Track_tls::NORMAL
:
5910 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
5911 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
5912 Powerpc_relobj
<size
, big_endian
>* const object
5913 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
5915 bool has_plt_value
= false;
5916 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5918 ? use_plt_offset
<size
>(gsym
, Scan::get_reference_flags(r_type
))
5919 : object
->local_has_plt_offset(r_sym
))
5921 Stub_table
<size
, big_endian
>* stub_table
5922 = object
->stub_table(relinfo
->data_shndx
);
5923 if (stub_table
== NULL
)
5925 // This is a ref from a data section to an ifunc symbol.
5926 if (target
->stub_tables().size() != 0)
5927 stub_table
= target
->stub_tables()[0];
5929 gold_assert(stub_table
!= NULL
);
5932 off
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
5933 rela
.get_r_addend());
5935 off
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
5936 rela
.get_r_addend());
5937 gold_assert(off
!= invalid_address
);
5938 value
= stub_table
->stub_address() + off
;
5939 has_plt_value
= true;
5942 if (r_type
== elfcpp::R_POWERPC_GOT16
5943 || r_type
== elfcpp::R_POWERPC_GOT16_LO
5944 || r_type
== elfcpp::R_POWERPC_GOT16_HI
5945 || r_type
== elfcpp::R_POWERPC_GOT16_HA
5946 || r_type
== elfcpp::R_PPC64_GOT16_DS
5947 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
5951 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
5952 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
5956 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5957 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
5958 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
5960 value
-= target
->got_section()->got_base_offset(object
);
5962 else if (r_type
== elfcpp::R_PPC64_TOC
)
5964 value
= (target
->got_section()->output_section()->address()
5965 + object
->toc_base_offset());
5967 else if (gsym
!= NULL
5968 && (r_type
== elfcpp::R_POWERPC_REL24
5969 || r_type
== elfcpp::R_PPC_PLTREL24
)
5974 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
5975 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
5976 bool can_plt_call
= false;
5977 if (rela
.get_r_offset() + 8 <= view_size
)
5979 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5980 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
5983 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
5985 elfcpp::Swap
<32, big_endian
>::writeval(wv
+ 1, ld_2_1
+ 40);
5986 can_plt_call
= true;
5991 // If we don't have a branch and link followed by a nop,
5992 // we can't go via the plt because there is no place to
5993 // put a toc restoring instruction.
5994 // Unless we know we won't be returning.
5995 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
5996 can_plt_call
= true;
6000 // This is not an error in one special case: A self
6001 // call. It isn't possible to cheaply verify we have
6002 // such a call so just check for a call to the same
6005 Address code
= value
;
6006 if (gsym
->source() == Symbol::FROM_OBJECT
6007 && gsym
->object() == object
)
6009 Address addend
= rela
.get_r_addend();
6010 unsigned int dest_shndx
;
6011 Address opdent
= psymval
->value(object
, addend
);
6012 code
= target
->symval_for_branch(relinfo
->symtab
, opdent
,
6013 gsym
, object
, &dest_shndx
);
6015 if (dest_shndx
== 0)
6016 dest_shndx
= gsym
->shndx(&is_ordinary
);
6017 ok
= dest_shndx
== relinfo
->data_shndx
;
6021 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6022 _("call lacks nop, can't restore toc; "
6023 "recompile with -fPIC"));
6029 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6030 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
6031 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
6032 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
6034 // First instruction of a global dynamic sequence, arg setup insn.
6035 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6036 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6037 enum Got_type got_type
= GOT_TYPE_STANDARD
;
6038 if (tls_type
== tls::TLSOPT_NONE
)
6039 got_type
= GOT_TYPE_TLSGD
;
6040 else if (tls_type
== tls::TLSOPT_TO_IE
)
6041 got_type
= GOT_TYPE_TPREL
;
6042 if (got_type
!= GOT_TYPE_STANDARD
)
6046 gold_assert(gsym
->has_got_offset(got_type
));
6047 value
= gsym
->got_offset(got_type
);
6051 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6052 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
6053 value
= object
->local_got_offset(r_sym
, got_type
);
6055 value
-= target
->got_section()->got_base_offset(object
);
6057 if (tls_type
== tls::TLSOPT_TO_IE
)
6059 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6060 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
6062 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6063 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6064 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
6066 insn
|= 32 << 26; // lwz
6068 insn
|= 58 << 26; // ld
6069 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6071 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
6072 - elfcpp::R_POWERPC_GOT_TLSGD16
);
6074 else if (tls_type
== tls::TLSOPT_TO_LE
)
6076 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6077 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
6079 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6080 Insn insn
= addis_3_13
;
6083 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6084 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6085 value
= psymval
->value(object
, rela
.get_r_addend());
6089 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6091 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6092 r_type
= elfcpp::R_POWERPC_NONE
;
6096 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
6097 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
6098 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
6099 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
6101 // First instruction of a local dynamic sequence, arg setup insn.
6102 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6103 if (tls_type
== tls::TLSOPT_NONE
)
6105 value
= target
->tlsld_got_offset();
6106 value
-= target
->got_section()->got_base_offset(object
);
6110 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
6111 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
6112 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
6114 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6115 Insn insn
= addis_3_13
;
6118 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6119 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6124 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6126 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6127 r_type
= elfcpp::R_POWERPC_NONE
;
6131 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
6132 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
6133 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
6134 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
6136 // Accesses relative to a local dynamic sequence address,
6137 // no optimisation here.
6140 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
6141 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
6145 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6146 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
6147 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
6149 value
-= target
->got_section()->got_base_offset(object
);
6151 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
6152 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
6153 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
6154 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
6156 // First instruction of initial exec sequence.
6157 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6158 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
6159 if (tls_type
== tls::TLSOPT_NONE
)
6163 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
6164 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
6168 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6169 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
6170 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
6172 value
-= target
->got_section()->got_base_offset(object
);
6176 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
6177 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
6178 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
6180 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6181 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6182 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
6187 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6188 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6189 value
= psymval
->value(object
, rela
.get_r_addend());
6193 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6195 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6196 r_type
= elfcpp::R_POWERPC_NONE
;
6200 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6201 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6203 // Second instruction of a global dynamic sequence,
6204 // the __tls_get_addr call
6205 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
6206 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6207 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6208 if (tls_type
!= tls::TLSOPT_NONE
)
6210 if (tls_type
== tls::TLSOPT_TO_IE
)
6212 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6213 Insn insn
= add_3_3_13
;
6216 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6217 r_type
= elfcpp::R_POWERPC_NONE
;
6221 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6222 Insn insn
= addi_3_3
;
6223 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6224 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6225 view
+= 2 * big_endian
;
6226 value
= psymval
->value(object
, rela
.get_r_addend());
6228 this->skip_next_tls_get_addr_call();
6231 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6232 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6234 // Second instruction of a local dynamic sequence,
6235 // the __tls_get_addr call
6236 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
6237 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6238 if (tls_type
== tls::TLSOPT_TO_LE
)
6240 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6241 Insn insn
= addi_3_3
;
6242 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6243 this->skip_next_tls_get_addr_call();
6244 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6245 view
+= 2 * big_endian
;
6249 else if (r_type
== elfcpp::R_POWERPC_TLS
)
6251 // Second instruction of an initial exec sequence
6252 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6253 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
6254 if (tls_type
== tls::TLSOPT_TO_LE
)
6256 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6257 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6258 unsigned int reg
= size
== 32 ? 2 : 13;
6259 insn
= at_tls_transform(insn
, reg
);
6260 gold_assert(insn
!= 0);
6261 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6262 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6263 view
+= 2 * big_endian
;
6264 value
= psymval
->value(object
, rela
.get_r_addend());
6267 else if (!has_plt_value
)
6270 unsigned int dest_shndx
;
6271 if (r_type
!= elfcpp::R_PPC_PLTREL24
)
6272 addend
= rela
.get_r_addend();
6273 value
= psymval
->value(object
, addend
);
6274 if (size
== 64 && is_branch_reloc(r_type
))
6275 value
= target
->symval_for_branch(relinfo
->symtab
, value
,
6276 gsym
, object
, &dest_shndx
);
6277 unsigned int max_branch_offset
= 0;
6278 if (r_type
== elfcpp::R_POWERPC_REL24
6279 || r_type
== elfcpp::R_PPC_PLTREL24
6280 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
6281 max_branch_offset
= 1 << 25;
6282 else if (r_type
== elfcpp::R_POWERPC_REL14
6283 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
6284 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
6285 max_branch_offset
= 1 << 15;
6286 if (max_branch_offset
!= 0
6287 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
6289 Stub_table
<size
, big_endian
>* stub_table
6290 = object
->stub_table(relinfo
->data_shndx
);
6291 gold_assert(stub_table
!= NULL
);
6292 Address off
= stub_table
->find_long_branch_entry(object
, value
);
6293 if (off
!= invalid_address
)
6294 value
= stub_table
->stub_address() + stub_table
->plt_size() + off
;
6300 case elfcpp::R_PPC64_REL64
:
6301 case elfcpp::R_POWERPC_REL32
:
6302 case elfcpp::R_POWERPC_REL24
:
6303 case elfcpp::R_PPC_PLTREL24
:
6304 case elfcpp::R_PPC_LOCAL24PC
:
6305 case elfcpp::R_POWERPC_REL16
:
6306 case elfcpp::R_POWERPC_REL16_LO
:
6307 case elfcpp::R_POWERPC_REL16_HI
:
6308 case elfcpp::R_POWERPC_REL16_HA
:
6309 case elfcpp::R_POWERPC_REL14
:
6310 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6311 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6315 case elfcpp::R_PPC64_TOC16
:
6316 case elfcpp::R_PPC64_TOC16_LO
:
6317 case elfcpp::R_PPC64_TOC16_HI
:
6318 case elfcpp::R_PPC64_TOC16_HA
:
6319 case elfcpp::R_PPC64_TOC16_DS
:
6320 case elfcpp::R_PPC64_TOC16_LO_DS
:
6321 // Subtract the TOC base address.
6322 value
-= (target
->got_section()->output_section()->address()
6323 + object
->toc_base_offset());
6326 case elfcpp::R_POWERPC_SECTOFF
:
6327 case elfcpp::R_POWERPC_SECTOFF_LO
:
6328 case elfcpp::R_POWERPC_SECTOFF_HI
:
6329 case elfcpp::R_POWERPC_SECTOFF_HA
:
6330 case elfcpp::R_PPC64_SECTOFF_DS
:
6331 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6333 value
-= os
->address();
6336 case elfcpp::R_PPC64_TPREL16_DS
:
6337 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6339 // R_PPC_TLSGD and R_PPC_TLSLD
6341 case elfcpp::R_POWERPC_TPREL16
:
6342 case elfcpp::R_POWERPC_TPREL16_LO
:
6343 case elfcpp::R_POWERPC_TPREL16_HI
:
6344 case elfcpp::R_POWERPC_TPREL16_HA
:
6345 case elfcpp::R_POWERPC_TPREL
:
6346 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6347 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6348 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6349 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6350 // tls symbol values are relative to tls_segment()->vaddr()
6354 case elfcpp::R_PPC64_DTPREL16_DS
:
6355 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6356 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6357 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6358 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6359 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6361 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
6362 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
6364 case elfcpp::R_POWERPC_DTPREL16
:
6365 case elfcpp::R_POWERPC_DTPREL16_LO
:
6366 case elfcpp::R_POWERPC_DTPREL16_HI
:
6367 case elfcpp::R_POWERPC_DTPREL16_HA
:
6368 case elfcpp::R_POWERPC_DTPREL
:
6369 // tls symbol values are relative to tls_segment()->vaddr()
6370 value
-= dtp_offset
;
6377 Insn branch_bit
= 0;
6380 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6381 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6382 branch_bit
= 1 << 21;
6383 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6384 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6386 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6387 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6390 if (this->is_isa_v2
)
6392 // Set 'a' bit. This is 0b00010 in BO field for branch
6393 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
6394 // for branch on CTR insns (BO == 1a00t or 1a01t).
6395 if ((insn
& (0x14 << 21)) == (0x04 << 21))
6397 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
6404 // Invert 'y' bit if not the default.
6405 if (static_cast<Signed_address
>(value
) < 0)
6408 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6418 // Multi-instruction sequences that access the TOC can be
6419 // optimized, eg. addis ra,r2,0; addi rb,ra,x;
6420 // to nop; addi rb,r2,x;
6426 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6427 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6428 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6429 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6430 case elfcpp::R_POWERPC_GOT16_HA
:
6431 case elfcpp::R_PPC64_TOC16_HA
:
6432 if (parameters
->options().toc_optimize())
6434 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6435 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6436 if ((insn
& ((0x3f << 26) | 0x1f << 16))
6437 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
6438 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6439 _("toc optimization is not supported "
6440 "for %#08x instruction"), insn
);
6441 else if (value
+ 0x8000 < 0x10000)
6443 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
6449 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6450 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6451 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6452 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6453 case elfcpp::R_POWERPC_GOT16_LO
:
6454 case elfcpp::R_PPC64_GOT16_LO_DS
:
6455 case elfcpp::R_PPC64_TOC16_LO
:
6456 case elfcpp::R_PPC64_TOC16_LO_DS
:
6457 if (parameters
->options().toc_optimize())
6459 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6460 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6461 if (!ok_lo_toc_insn(insn
))
6462 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6463 _("toc optimization is not supported "
6464 "for %#08x instruction"), insn
);
6465 else if (value
+ 0x8000 < 0x10000)
6467 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
6469 // Transform addic to addi when we change reg.
6470 insn
&= ~((0x3f << 26) | (0x1f << 16));
6471 insn
|= (14u << 26) | (2 << 16);
6475 insn
&= ~(0x1f << 16);
6478 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6485 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
6488 case elfcpp::R_POWERPC_ADDR32
:
6489 case elfcpp::R_POWERPC_UADDR32
:
6491 overflow
= Reloc::CHECK_BITFIELD
;
6494 case elfcpp::R_POWERPC_REL32
:
6496 overflow
= Reloc::CHECK_SIGNED
;
6499 case elfcpp::R_POWERPC_ADDR24
:
6500 case elfcpp::R_POWERPC_ADDR16
:
6501 case elfcpp::R_POWERPC_UADDR16
:
6502 case elfcpp::R_PPC64_ADDR16_DS
:
6503 case elfcpp::R_POWERPC_ADDR14
:
6504 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6505 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6506 overflow
= Reloc::CHECK_BITFIELD
;
6509 case elfcpp::R_POWERPC_REL24
:
6510 case elfcpp::R_PPC_PLTREL24
:
6511 case elfcpp::R_PPC_LOCAL24PC
:
6512 case elfcpp::R_POWERPC_REL16
:
6513 case elfcpp::R_PPC64_TOC16
:
6514 case elfcpp::R_POWERPC_GOT16
:
6515 case elfcpp::R_POWERPC_SECTOFF
:
6516 case elfcpp::R_POWERPC_TPREL16
:
6517 case elfcpp::R_POWERPC_DTPREL16
:
6518 case elfcpp::R_PPC64_TPREL16_DS
:
6519 case elfcpp::R_PPC64_DTPREL16_DS
:
6520 case elfcpp::R_PPC64_TOC16_DS
:
6521 case elfcpp::R_PPC64_GOT16_DS
:
6522 case elfcpp::R_PPC64_SECTOFF_DS
:
6523 case elfcpp::R_POWERPC_REL14
:
6524 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6525 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6526 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6527 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6528 case elfcpp::R_POWERPC_GOT_TPREL16
:
6529 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6530 overflow
= Reloc::CHECK_SIGNED
;
6534 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
6535 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
6538 case elfcpp::R_POWERPC_NONE
:
6539 case elfcpp::R_POWERPC_TLS
:
6540 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6541 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6542 case elfcpp::R_PPC_EMB_MRKREF
:
6545 case elfcpp::R_PPC64_ADDR64
:
6546 case elfcpp::R_PPC64_REL64
:
6547 case elfcpp::R_PPC64_TOC
:
6548 Reloc::addr64(view
, value
);
6551 case elfcpp::R_POWERPC_TPREL
:
6552 case elfcpp::R_POWERPC_DTPREL
:
6554 Reloc::addr64(view
, value
);
6556 status
= Reloc::addr32(view
, value
, overflow
);
6559 case elfcpp::R_PPC64_UADDR64
:
6560 Reloc::addr64_u(view
, value
);
6563 case elfcpp::R_POWERPC_ADDR32
:
6564 status
= Reloc::addr32(view
, value
, overflow
);
6567 case elfcpp::R_POWERPC_REL32
:
6568 case elfcpp::R_POWERPC_UADDR32
:
6569 status
= Reloc::addr32_u(view
, value
, overflow
);
6572 case elfcpp::R_POWERPC_ADDR24
:
6573 case elfcpp::R_POWERPC_REL24
:
6574 case elfcpp::R_PPC_PLTREL24
:
6575 case elfcpp::R_PPC_LOCAL24PC
:
6576 status
= Reloc::addr24(view
, value
, overflow
);
6579 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6580 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6583 status
= Reloc::addr16_ds(view
, value
, overflow
);
6586 case elfcpp::R_POWERPC_ADDR16
:
6587 case elfcpp::R_POWERPC_REL16
:
6588 case elfcpp::R_PPC64_TOC16
:
6589 case elfcpp::R_POWERPC_GOT16
:
6590 case elfcpp::R_POWERPC_SECTOFF
:
6591 case elfcpp::R_POWERPC_TPREL16
:
6592 case elfcpp::R_POWERPC_DTPREL16
:
6593 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6594 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6595 case elfcpp::R_POWERPC_GOT_TPREL16
:
6596 case elfcpp::R_POWERPC_ADDR16_LO
:
6597 case elfcpp::R_POWERPC_REL16_LO
:
6598 case elfcpp::R_PPC64_TOC16_LO
:
6599 case elfcpp::R_POWERPC_GOT16_LO
:
6600 case elfcpp::R_POWERPC_SECTOFF_LO
:
6601 case elfcpp::R_POWERPC_TPREL16_LO
:
6602 case elfcpp::R_POWERPC_DTPREL16_LO
:
6603 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6604 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6605 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6606 status
= Reloc::addr16(view
, value
, overflow
);
6609 case elfcpp::R_POWERPC_UADDR16
:
6610 status
= Reloc::addr16_u(view
, value
, overflow
);
6613 case elfcpp::R_POWERPC_ADDR16_HI
:
6614 case elfcpp::R_POWERPC_REL16_HI
:
6615 case elfcpp::R_PPC64_TOC16_HI
:
6616 case elfcpp::R_POWERPC_GOT16_HI
:
6617 case elfcpp::R_POWERPC_SECTOFF_HI
:
6618 case elfcpp::R_POWERPC_TPREL16_HI
:
6619 case elfcpp::R_POWERPC_DTPREL16_HI
:
6620 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6621 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6622 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6623 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6624 Reloc::addr16_hi(view
, value
);
6627 case elfcpp::R_POWERPC_ADDR16_HA
:
6628 case elfcpp::R_POWERPC_REL16_HA
:
6629 case elfcpp::R_PPC64_TOC16_HA
:
6630 case elfcpp::R_POWERPC_GOT16_HA
:
6631 case elfcpp::R_POWERPC_SECTOFF_HA
:
6632 case elfcpp::R_POWERPC_TPREL16_HA
:
6633 case elfcpp::R_POWERPC_DTPREL16_HA
:
6634 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6635 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6636 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6637 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6638 Reloc::addr16_ha(view
, value
);
6641 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6643 // R_PPC_EMB_NADDR16_LO
6645 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6646 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6647 Reloc::addr16_hi2(view
, value
);
6650 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6652 // R_PPC_EMB_NADDR16_HI
6654 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6655 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6656 Reloc::addr16_ha2(view
, value
);
6659 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6661 // R_PPC_EMB_NADDR16_HA
6663 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6664 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6665 Reloc::addr16_hi3(view
, value
);
6668 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6672 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6673 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6674 Reloc::addr16_ha3(view
, value
);
6677 case elfcpp::R_PPC64_DTPREL16_DS
:
6678 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6680 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
6682 case elfcpp::R_PPC64_TPREL16_DS
:
6683 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6685 // R_PPC_TLSGD, R_PPC_TLSLD
6687 case elfcpp::R_PPC64_ADDR16_DS
:
6688 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6689 case elfcpp::R_PPC64_TOC16_DS
:
6690 case elfcpp::R_PPC64_TOC16_LO_DS
:
6691 case elfcpp::R_PPC64_GOT16_DS
:
6692 case elfcpp::R_PPC64_GOT16_LO_DS
:
6693 case elfcpp::R_PPC64_SECTOFF_DS
:
6694 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6695 status
= Reloc::addr16_ds(view
, value
, overflow
);
6698 case elfcpp::R_POWERPC_ADDR14
:
6699 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6700 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6701 case elfcpp::R_POWERPC_REL14
:
6702 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6703 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6704 status
= Reloc::addr14(view
, value
, overflow
);
6707 case elfcpp::R_POWERPC_COPY
:
6708 case elfcpp::R_POWERPC_GLOB_DAT
:
6709 case elfcpp::R_POWERPC_JMP_SLOT
:
6710 case elfcpp::R_POWERPC_RELATIVE
:
6711 case elfcpp::R_POWERPC_DTPMOD
:
6712 case elfcpp::R_PPC64_JMP_IREL
:
6713 case elfcpp::R_POWERPC_IRELATIVE
:
6714 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6715 _("unexpected reloc %u in object file"),
6719 case elfcpp::R_PPC_EMB_SDA21
:
6724 // R_PPC64_TOCSAVE. For the time being this can be ignored.
6728 case elfcpp::R_PPC_EMB_SDA2I16
:
6729 case elfcpp::R_PPC_EMB_SDA2REL
:
6732 // R_PPC64_TLSGD, R_PPC64_TLSLD
6735 case elfcpp::R_POWERPC_PLT32
:
6736 case elfcpp::R_POWERPC_PLTREL32
:
6737 case elfcpp::R_POWERPC_PLT16_LO
:
6738 case elfcpp::R_POWERPC_PLT16_HI
:
6739 case elfcpp::R_POWERPC_PLT16_HA
:
6740 case elfcpp::R_PPC_SDAREL16
:
6741 case elfcpp::R_POWERPC_ADDR30
:
6742 case elfcpp::R_PPC64_PLT64
:
6743 case elfcpp::R_PPC64_PLTREL64
:
6744 case elfcpp::R_PPC64_PLTGOT16
:
6745 case elfcpp::R_PPC64_PLTGOT16_LO
:
6746 case elfcpp::R_PPC64_PLTGOT16_HI
:
6747 case elfcpp::R_PPC64_PLTGOT16_HA
:
6748 case elfcpp::R_PPC64_PLT16_LO_DS
:
6749 case elfcpp::R_PPC64_PLTGOT16_DS
:
6750 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
6751 case elfcpp::R_PPC_EMB_RELSEC16
:
6752 case elfcpp::R_PPC_EMB_RELST_LO
:
6753 case elfcpp::R_PPC_EMB_RELST_HI
:
6754 case elfcpp::R_PPC_EMB_RELST_HA
:
6755 case elfcpp::R_PPC_EMB_BIT_FLD
:
6756 case elfcpp::R_PPC_EMB_RELSDA
:
6757 case elfcpp::R_PPC_TOC16
:
6760 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6761 _("unsupported reloc %u"),
6765 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
)
6766 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6767 _("relocation overflow"));
6772 // Relocate section data.
6774 template<int size
, bool big_endian
>
6776 Target_powerpc
<size
, big_endian
>::relocate_section(
6777 const Relocate_info
<size
, big_endian
>* relinfo
,
6778 unsigned int sh_type
,
6779 const unsigned char* prelocs
,
6781 Output_section
* output_section
,
6782 bool needs_special_offset_handling
,
6783 unsigned char* view
,
6785 section_size_type view_size
,
6786 const Reloc_symbol_changes
* reloc_symbol_changes
)
6788 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
6789 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
6790 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
6791 Powerpc_comdat_behavior
;
6793 gold_assert(sh_type
== elfcpp::SHT_RELA
);
6795 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
6796 Powerpc_relocate
, Powerpc_comdat_behavior
>(
6802 needs_special_offset_handling
,
6806 reloc_symbol_changes
);
6809 class Powerpc_scan_relocatable_reloc
6812 // Return the strategy to use for a local symbol which is not a
6813 // section symbol, given the relocation type.
6814 inline Relocatable_relocs::Reloc_strategy
6815 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
6817 if (r_type
== 0 && r_sym
== 0)
6818 return Relocatable_relocs::RELOC_DISCARD
;
6819 return Relocatable_relocs::RELOC_COPY
;
6822 // Return the strategy to use for a local symbol which is a section
6823 // symbol, given the relocation type.
6824 inline Relocatable_relocs::Reloc_strategy
6825 local_section_strategy(unsigned int, Relobj
*)
6827 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
6830 // Return the strategy to use for a global symbol, given the
6831 // relocation type, the object, and the symbol index.
6832 inline Relocatable_relocs::Reloc_strategy
6833 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
6835 if (r_type
== elfcpp::R_PPC_PLTREL24
)
6836 return Relocatable_relocs::RELOC_SPECIAL
;
6837 return Relocatable_relocs::RELOC_COPY
;
6841 // Scan the relocs during a relocatable link.
6843 template<int size
, bool big_endian
>
6845 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
6846 Symbol_table
* symtab
,
6848 Sized_relobj_file
<size
, big_endian
>* object
,
6849 unsigned int data_shndx
,
6850 unsigned int sh_type
,
6851 const unsigned char* prelocs
,
6853 Output_section
* output_section
,
6854 bool needs_special_offset_handling
,
6855 size_t local_symbol_count
,
6856 const unsigned char* plocal_symbols
,
6857 Relocatable_relocs
* rr
)
6859 gold_assert(sh_type
== elfcpp::SHT_RELA
);
6861 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
6862 Powerpc_scan_relocatable_reloc
>(
6870 needs_special_offset_handling
,
6876 // Emit relocations for a section.
6877 // This is a modified version of the function by the same name in
6878 // target-reloc.h. Using relocate_special_relocatable for
6879 // R_PPC_PLTREL24 would require duplication of the entire body of the
6880 // loop, so we may as well duplicate the whole thing.
6882 template<int size
, bool big_endian
>
6884 Target_powerpc
<size
, big_endian
>::relocate_relocs(
6885 const Relocate_info
<size
, big_endian
>* relinfo
,
6886 unsigned int sh_type
,
6887 const unsigned char* prelocs
,
6889 Output_section
* output_section
,
6890 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
6891 const Relocatable_relocs
* rr
,
6893 Address view_address
,
6895 unsigned char* reloc_view
,
6896 section_size_type reloc_view_size
)
6898 gold_assert(sh_type
== elfcpp::SHT_RELA
);
6900 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
6902 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc_write
6904 const int reloc_size
6905 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
6907 Powerpc_relobj
<size
, big_endian
>* const object
6908 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
6909 const unsigned int local_count
= object
->local_symbol_count();
6910 unsigned int got2_shndx
= object
->got2_shndx();
6911 Address got2_addend
= 0;
6912 if (got2_shndx
!= 0)
6914 got2_addend
= object
->get_output_section_offset(got2_shndx
);
6915 gold_assert(got2_addend
!= invalid_address
);
6918 unsigned char* pwrite
= reloc_view
;
6919 bool zap_next
= false;
6920 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
6922 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
6923 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
6926 Reltype
reloc(prelocs
);
6927 Reltype_write
reloc_write(pwrite
);
6929 Address offset
= reloc
.get_r_offset();
6930 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
6931 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
6932 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
6933 const unsigned int orig_r_sym
= r_sym
;
6934 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
6935 = reloc
.get_r_addend();
6936 const Symbol
* gsym
= NULL
;
6940 // We could arrange to discard these and other relocs for
6941 // tls optimised sequences in the strategy methods, but for
6942 // now do as BFD ld does.
6943 r_type
= elfcpp::R_POWERPC_NONE
;
6947 // Get the new symbol index.
6948 if (r_sym
< local_count
)
6952 case Relocatable_relocs::RELOC_COPY
:
6953 case Relocatable_relocs::RELOC_SPECIAL
:
6956 r_sym
= object
->symtab_index(r_sym
);
6957 gold_assert(r_sym
!= -1U);
6961 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
6963 // We are adjusting a section symbol. We need to find
6964 // the symbol table index of the section symbol for
6965 // the output section corresponding to input section
6966 // in which this symbol is defined.
6967 gold_assert(r_sym
< local_count
);
6969 unsigned int shndx
=
6970 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
6971 gold_assert(is_ordinary
);
6972 Output_section
* os
= object
->output_section(shndx
);
6973 gold_assert(os
!= NULL
);
6974 gold_assert(os
->needs_symtab_index());
6975 r_sym
= os
->symtab_index();
6985 gsym
= object
->global_symbol(r_sym
);
6986 gold_assert(gsym
!= NULL
);
6987 if (gsym
->is_forwarder())
6988 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
6990 gold_assert(gsym
->has_symtab_index());
6991 r_sym
= gsym
->symtab_index();
6994 // Get the new offset--the location in the output section where
6995 // this relocation should be applied.
6996 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
6997 offset
+= offset_in_output_section
;
7000 section_offset_type sot_offset
=
7001 convert_types
<section_offset_type
, Address
>(offset
);
7002 section_offset_type new_sot_offset
=
7003 output_section
->output_offset(object
, relinfo
->data_shndx
,
7005 gold_assert(new_sot_offset
!= -1);
7006 offset
= new_sot_offset
;
7009 // In an object file, r_offset is an offset within the section.
7010 // In an executable or dynamic object, generated by
7011 // --emit-relocs, r_offset is an absolute address.
7012 if (!parameters
->options().relocatable())
7014 offset
+= view_address
;
7015 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
7016 offset
-= offset_in_output_section
;
7019 // Handle the reloc addend based on the strategy.
7020 if (strategy
== Relocatable_relocs::RELOC_COPY
)
7022 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
7024 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
7025 addend
= psymval
->value(object
, addend
);
7027 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
7029 if (addend
>= 32768)
7030 addend
+= got2_addend
;
7035 if (!parameters
->options().relocatable())
7037 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7038 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
7039 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
7040 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
7042 // First instruction of a global dynamic sequence,
7044 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7045 switch (this->optimize_tls_gd(final
))
7047 case tls::TLSOPT_TO_IE
:
7048 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
7049 - elfcpp::R_POWERPC_GOT_TLSGD16
);
7051 case tls::TLSOPT_TO_LE
:
7052 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7053 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
7054 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7057 r_type
= elfcpp::R_POWERPC_NONE
;
7058 offset
-= 2 * big_endian
;
7065 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7066 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
7067 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
7068 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
7070 // First instruction of a local dynamic sequence,
7072 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
7074 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7075 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
7077 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7078 const Output_section
* os
= relinfo
->layout
->tls_segment()
7080 gold_assert(os
!= NULL
);
7081 gold_assert(os
->needs_symtab_index());
7082 r_sym
= os
->symtab_index();
7083 addend
= dtp_offset
;
7087 r_type
= elfcpp::R_POWERPC_NONE
;
7088 offset
-= 2 * big_endian
;
7092 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
7093 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
7094 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
7095 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
7097 // First instruction of initial exec sequence.
7098 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7099 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
7101 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
7102 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
7103 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7106 r_type
= elfcpp::R_POWERPC_NONE
;
7107 offset
-= 2 * big_endian
;
7111 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
7112 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
7114 // Second instruction of a global dynamic sequence,
7115 // the __tls_get_addr call
7116 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7117 switch (this->optimize_tls_gd(final
))
7119 case tls::TLSOPT_TO_IE
:
7120 r_type
= elfcpp::R_POWERPC_NONE
;
7123 case tls::TLSOPT_TO_LE
:
7124 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7125 offset
+= 2 * big_endian
;
7132 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
7133 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
7135 // Second instruction of a local dynamic sequence,
7136 // the __tls_get_addr call
7137 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
7139 const Output_section
* os
= relinfo
->layout
->tls_segment()
7141 gold_assert(os
!= NULL
);
7142 gold_assert(os
->needs_symtab_index());
7143 r_sym
= os
->symtab_index();
7144 addend
= dtp_offset
;
7145 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7146 offset
+= 2 * big_endian
;
7150 else if (r_type
== elfcpp::R_POWERPC_TLS
)
7152 // Second instruction of an initial exec sequence
7153 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7154 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
7156 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7157 offset
+= 2 * big_endian
;
7162 reloc_write
.put_r_offset(offset
);
7163 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
7164 reloc_write
.put_r_addend(addend
);
7166 pwrite
+= reloc_size
;
7169 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
7170 == reloc_view_size
);
7173 // Return the value to use for a dynamic symbol which requires special
7174 // treatment. This is how we support equality comparisons of function
7175 // pointers across shared library boundaries, as described in the
7176 // processor specific ABI supplement.
7178 template<int size
, bool big_endian
>
7180 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
7184 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
7185 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7186 p
!= this->stub_tables_
.end();
7189 Address off
= (*p
)->find_plt_call_entry(gsym
);
7190 if (off
!= invalid_address
)
7191 return (*p
)->stub_address() + off
;
7197 // Return the PLT address to use for a local symbol.
7198 template<int size
, bool big_endian
>
7200 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
7201 const Relobj
* object
,
7202 unsigned int symndx
) const
7206 const Sized_relobj
<size
, big_endian
>* relobj
7207 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
7208 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7209 p
!= this->stub_tables_
.end();
7212 Address off
= (*p
)->find_plt_call_entry(relobj
->sized_relobj(),
7214 if (off
!= invalid_address
)
7215 return (*p
)->stub_address() + off
;
7221 // Return the PLT address to use for a global symbol.
7222 template<int size
, bool big_endian
>
7224 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
7225 const Symbol
* gsym
) const
7229 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7230 p
!= this->stub_tables_
.end();
7233 Address off
= (*p
)->find_plt_call_entry(gsym
);
7234 if (off
!= invalid_address
)
7235 return (*p
)->stub_address() + off
;
7241 // Return the offset to use for the GOT_INDX'th got entry which is
7242 // for a local tls symbol specified by OBJECT, SYMNDX.
7243 template<int size
, bool big_endian
>
7245 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
7246 const Relobj
* object
,
7247 unsigned int symndx
,
7248 unsigned int got_indx
) const
7250 const Powerpc_relobj
<size
, big_endian
>* ppc_object
7251 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
7252 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
7254 for (Got_type got_type
= GOT_TYPE_TLSGD
;
7255 got_type
<= GOT_TYPE_TPREL
;
7256 got_type
= Got_type(got_type
+ 1))
7257 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
7259 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
7260 if (got_type
== GOT_TYPE_TLSGD
)
7262 if (off
== got_indx
* (size
/ 8))
7264 if (got_type
== GOT_TYPE_TPREL
)
7274 // Return the offset to use for the GOT_INDX'th got entry which is
7275 // for global tls symbol GSYM.
7276 template<int size
, bool big_endian
>
7278 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
7280 unsigned int got_indx
) const
7282 if (gsym
->type() == elfcpp::STT_TLS
)
7284 for (Got_type got_type
= GOT_TYPE_TLSGD
;
7285 got_type
<= GOT_TYPE_TPREL
;
7286 got_type
= Got_type(got_type
+ 1))
7287 if (gsym
->has_got_offset(got_type
))
7289 unsigned int off
= gsym
->got_offset(got_type
);
7290 if (got_type
== GOT_TYPE_TLSGD
)
7292 if (off
== got_indx
* (size
/ 8))
7294 if (got_type
== GOT_TYPE_TPREL
)
7304 // The selector for powerpc object files.
7306 template<int size
, bool big_endian
>
7307 class Target_selector_powerpc
: public Target_selector
7310 Target_selector_powerpc()
7311 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
7314 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
7315 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
7317 ? (big_endian
? "elf64ppc" : "elf64lppc")
7318 : (big_endian
? "elf32ppc" : "elf32lppc")))
7322 do_instantiate_target()
7323 { return new Target_powerpc
<size
, big_endian
>(); }
7326 Target_selector_powerpc
<32, true> target_selector_ppc32
;
7327 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
7328 Target_selector_powerpc
<64, true> target_selector_ppc64
;
7329 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
7331 // Instantiate these constants for -O0
7332 template<int size
, bool big_endian
>
7333 const int Output_data_glink
<size
, big_endian
>::pltresolve_size
;
7334 template<int size
, bool big_endian
>
7335 const typename Stub_table
<size
, big_endian
>::Address
7336 Stub_table
<size
, big_endian
>::invalid_address
;
7337 template<int size
, bool big_endian
>
7338 const typename Target_powerpc
<size
, big_endian
>::Address
7339 Target_powerpc
<size
, big_endian
>::invalid_address
;
7341 } // End anonymous namespace.