1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
48 template<int size
, bool big_endian
>
49 class Output_data_plt_powerpc
;
51 template<int size
, bool big_endian
>
52 class Output_data_brlt_powerpc
;
54 template<int size
, bool big_endian
>
55 class Output_data_got_powerpc
;
57 template<int size
, bool big_endian
>
58 class Output_data_glink
;
60 template<int size
, bool big_endian
>
63 template<int size
, bool big_endian
>
64 class Powerpc_relobj
: public Sized_relobj_file
<size
, big_endian
>
67 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
68 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_refs
;
69 typedef Unordered_map
<Address
, Section_refs
> Access_from
;
71 Powerpc_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
72 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
73 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
74 special_(0), has_small_toc_reloc_(false), opd_valid_(false),
75 opd_ent_(), access_from_map_(), has14_(), stub_table_()
81 // The .got2 section shndx.
86 return this->special_
;
91 // The .opd section shndx.
98 return this->special_
;
101 // Init OPD entry arrays.
103 init_opd(size_t opd_size
)
105 size_t count
= this->opd_ent_ndx(opd_size
);
106 this->opd_ent_
.resize(count
);
109 // Return section and offset of function entry for .opd + R_OFF.
111 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
113 size_t ndx
= this->opd_ent_ndx(r_off
);
114 gold_assert(ndx
< this->opd_ent_
.size());
115 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
117 *value
= this->opd_ent_
[ndx
].off
;
118 return this->opd_ent_
[ndx
].shndx
;
121 // Set section and offset of function entry for .opd + R_OFF.
123 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
125 size_t ndx
= this->opd_ent_ndx(r_off
);
126 gold_assert(ndx
< this->opd_ent_
.size());
127 this->opd_ent_
[ndx
].shndx
= shndx
;
128 this->opd_ent_
[ndx
].off
= value
;
131 // Return discard flag for .opd + R_OFF.
133 get_opd_discard(Address r_off
) const
135 size_t ndx
= this->opd_ent_ndx(r_off
);
136 gold_assert(ndx
< this->opd_ent_
.size());
137 return this->opd_ent_
[ndx
].discard
;
140 // Set discard flag for .opd + R_OFF.
142 set_opd_discard(Address r_off
)
144 size_t ndx
= this->opd_ent_ndx(r_off
);
145 gold_assert(ndx
< this->opd_ent_
.size());
146 this->opd_ent_
[ndx
].discard
= true;
151 { return &this->access_from_map_
; }
153 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
154 // section at DST_OFF.
156 add_reference(Object
* src_obj
,
157 unsigned int src_indx
,
158 typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
160 Section_id
src_id(src_obj
, src_indx
);
161 this->access_from_map_
[dst_off
].insert(src_id
);
164 // Add a reference to the code section specified by the .opd entry
167 add_gc_mark(typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
169 size_t ndx
= this->opd_ent_ndx(dst_off
);
170 if (ndx
>= this->opd_ent_
.size())
171 this->opd_ent_
.resize(ndx
+ 1);
172 this->opd_ent_
[ndx
].gc_mark
= true;
176 process_gc_mark(Symbol_table
* symtab
)
178 for (size_t i
= 0; i
< this->opd_ent_
.size(); i
++)
179 if (this->opd_ent_
[i
].gc_mark
)
181 unsigned int shndx
= this->opd_ent_
[i
].shndx
;
182 symtab
->gc()->worklist().push(Section_id(this, shndx
));
188 { return this->opd_valid_
; }
192 { this->opd_valid_
= true; }
194 // Examine .rela.opd to build info about function entry points.
196 scan_opd_relocs(size_t reloc_count
,
197 const unsigned char* prelocs
,
198 const unsigned char* plocal_syms
);
200 // Perform the Sized_relobj_file method, then set up opd info from
203 do_read_relocs(Read_relocs_data
*);
206 do_find_special_sections(Read_symbols_data
* sd
);
208 // Adjust this local symbol value. Return false if the symbol
209 // should be discarded from the output file.
211 do_adjust_local_symbol(Symbol_value
<size
>* lv
) const
213 if (size
== 64 && this->opd_shndx() != 0)
216 if (lv
->input_shndx(&is_ordinary
) != this->opd_shndx())
218 if (this->get_opd_discard(lv
->input_value()))
224 // Return offset in output GOT section that this object will use
225 // as a TOC pointer. Won't be just a constant with multi-toc support.
227 toc_base_offset() const
231 set_has_small_toc_reloc()
232 { has_small_toc_reloc_
= true; }
235 has_small_toc_reloc() const
236 { return has_small_toc_reloc_
; }
239 set_has_14bit_branch(unsigned int shndx
)
241 if (shndx
>= this->has14_
.size())
242 this->has14_
.resize(shndx
+ 1);
243 this->has14_
[shndx
] = true;
247 has_14bit_branch(unsigned int shndx
) const
248 { return shndx
< this->has14_
.size() && this->has14_
[shndx
]; }
251 set_stub_table(unsigned int shndx
, Stub_table
<size
, big_endian
>* stub_table
)
253 if (shndx
>= this->stub_table_
.size())
254 this->stub_table_
.resize(shndx
+ 1);
255 this->stub_table_
[shndx
] = stub_table
;
258 Stub_table
<size
, big_endian
>*
259 stub_table(unsigned int shndx
)
261 if (shndx
< this->stub_table_
.size())
262 return this->stub_table_
[shndx
];
275 // Return index into opd_ent_ array for .opd entry at OFF.
276 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
277 // apart when the language doesn't use the last 8-byte word, the
278 // environment pointer. Thus dividing the entry section offset by
279 // 16 will give an index into opd_ent_ that works for either layout
280 // of .opd. (It leaves some elements of the vector unused when .opd
281 // entries are spaced 24 bytes apart, but we don't know the spacing
282 // until relocations are processed, and in any case it is possible
283 // for an object to have some entries spaced 16 bytes apart and
284 // others 24 bytes apart.)
286 opd_ent_ndx(size_t off
) const
289 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
290 unsigned int special_
;
292 // For 64-bit, whether this object uses small model relocs to access
294 bool has_small_toc_reloc_
;
296 // Set at the start of gc_process_relocs, when we know opd_ent_
297 // vector is valid. The flag could be made atomic and set in
298 // do_read_relocs with memory_order_release and then tested with
299 // memory_order_acquire, potentially resulting in fewer entries in
303 // The first 8-byte word of an OPD entry gives the address of the
304 // entry point of the function. Relocatable object files have a
305 // relocation on this word. The following vector records the
306 // section and offset specified by these relocations.
307 std::vector
<Opd_ent
> opd_ent_
;
309 // References made to this object's .opd section when running
310 // gc_process_relocs for another object, before the opd_ent_ vector
311 // is valid for this object.
312 Access_from access_from_map_
;
314 // Whether input section has a 14-bit branch reloc.
315 std::vector
<bool> has14_
;
317 // The stub table to use for a given input section.
318 std::vector
<Stub_table
<size
, big_endian
>*> stub_table_
;
321 template<int size
, bool big_endian
>
322 class Target_powerpc
: public Sized_target
<size
, big_endian
>
326 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
327 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
328 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Signed_address
;
329 static const Address invalid_address
= static_cast<Address
>(0) - 1;
330 // Offset of tp and dtp pointers from start of TLS block.
331 static const Address tp_offset
= 0x7000;
332 static const Address dtp_offset
= 0x8000;
335 : Sized_target
<size
, big_endian
>(&powerpc_info
),
336 got_(NULL
), plt_(NULL
), iplt_(NULL
), brlt_section_(NULL
),
337 glink_(NULL
), rela_dyn_(NULL
), copy_relocs_(elfcpp::R_POWERPC_COPY
),
338 dynbss_(NULL
), tlsld_got_offset_(-1U),
339 stub_tables_(), branch_lookup_table_(), branch_info_(),
340 plt_thread_safe_(false)
344 // Process the relocations to determine unreferenced sections for
345 // garbage collection.
347 gc_process_relocs(Symbol_table
* symtab
,
349 Sized_relobj_file
<size
, big_endian
>* object
,
350 unsigned int data_shndx
,
351 unsigned int sh_type
,
352 const unsigned char* prelocs
,
354 Output_section
* output_section
,
355 bool needs_special_offset_handling
,
356 size_t local_symbol_count
,
357 const unsigned char* plocal_symbols
);
359 // Scan the relocations to look for symbol adjustments.
361 scan_relocs(Symbol_table
* symtab
,
363 Sized_relobj_file
<size
, big_endian
>* object
,
364 unsigned int data_shndx
,
365 unsigned int sh_type
,
366 const unsigned char* prelocs
,
368 Output_section
* output_section
,
369 bool needs_special_offset_handling
,
370 size_t local_symbol_count
,
371 const unsigned char* plocal_symbols
);
373 // Map input .toc section to output .got section.
375 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
377 if (size
== 64 && strcmp(name
, ".toc") == 0)
385 // Provide linker defined save/restore functions.
387 define_save_restore_funcs(Layout
*, Symbol_table
*);
389 // No stubs unless a final link.
392 { return !parameters
->options().relocatable(); }
395 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
397 // Stash info about branches, for stub generation.
399 push_branch(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
400 unsigned int data_shndx
, Address r_offset
,
401 unsigned int r_type
, unsigned int r_sym
, Address addend
)
403 Branch_info
info(ppc_object
, data_shndx
, r_offset
, r_type
, r_sym
, addend
);
404 this->branch_info_
.push_back(info
);
405 if (r_type
== elfcpp::R_POWERPC_REL14
406 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
407 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
408 ppc_object
->set_has_14bit_branch(data_shndx
);
411 Stub_table
<size
, big_endian
>*
415 do_define_standard_symbols(Symbol_table
*, Layout
*);
417 // Finalize the sections.
419 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
421 // Return the value to use for a dynamic which requires special
424 do_dynsym_value(const Symbol
*) const;
426 // Return the PLT address to use for a local symbol.
428 do_plt_address_for_local(const Relobj
*, unsigned int) const;
430 // Return the PLT address to use for a global symbol.
432 do_plt_address_for_global(const Symbol
*) const;
434 // Return the offset to use for the GOT_INDX'th got entry which is
435 // for a local tls symbol specified by OBJECT, SYMNDX.
437 do_tls_offset_for_local(const Relobj
* object
,
439 unsigned int got_indx
) const;
441 // Return the offset to use for the GOT_INDX'th got entry which is
442 // for global tls symbol GSYM.
444 do_tls_offset_for_global(Symbol
* gsym
, unsigned int got_indx
) const;
446 // Relocate a section.
448 relocate_section(const Relocate_info
<size
, big_endian
>*,
449 unsigned int sh_type
,
450 const unsigned char* prelocs
,
452 Output_section
* output_section
,
453 bool needs_special_offset_handling
,
455 Address view_address
,
456 section_size_type view_size
,
457 const Reloc_symbol_changes
*);
459 // Scan the relocs during a relocatable link.
461 scan_relocatable_relocs(Symbol_table
* symtab
,
463 Sized_relobj_file
<size
, big_endian
>* object
,
464 unsigned int data_shndx
,
465 unsigned int sh_type
,
466 const unsigned char* prelocs
,
468 Output_section
* output_section
,
469 bool needs_special_offset_handling
,
470 size_t local_symbol_count
,
471 const unsigned char* plocal_symbols
,
472 Relocatable_relocs
*);
474 // Emit relocations for a section.
476 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
477 unsigned int sh_type
,
478 const unsigned char* prelocs
,
480 Output_section
* output_section
,
481 typename
elfcpp::Elf_types
<size
>::Elf_Off
482 offset_in_output_section
,
483 const Relocatable_relocs
*,
485 Address view_address
,
487 unsigned char* reloc_view
,
488 section_size_type reloc_view_size
);
490 // Return whether SYM is defined by the ABI.
492 do_is_defined_by_abi(const Symbol
* sym
) const
494 return strcmp(sym
->name(), "__tls_get_addr") == 0;
497 // Return the size of the GOT section.
501 gold_assert(this->got_
!= NULL
);
502 return this->got_
->data_size();
505 // Get the PLT section.
506 const Output_data_plt_powerpc
<size
, big_endian
>*
509 gold_assert(this->plt_
!= NULL
);
513 // Get the IPLT section.
514 const Output_data_plt_powerpc
<size
, big_endian
>*
517 gold_assert(this->iplt_
!= NULL
);
521 // Get the .glink section.
522 const Output_data_glink
<size
, big_endian
>*
523 glink_section() const
525 gold_assert(this->glink_
!= NULL
);
529 // Get the GOT section.
530 const Output_data_got_powerpc
<size
, big_endian
>*
533 gold_assert(this->got_
!= NULL
);
537 // Get the GOT section, creating it if necessary.
538 Output_data_got_powerpc
<size
, big_endian
>*
539 got_section(Symbol_table
*, Layout
*);
542 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
543 const elfcpp::Ehdr
<size
, big_endian
>&);
545 // Return the number of entries in the GOT.
547 got_entry_count() const
549 if (this->got_
== NULL
)
551 return this->got_size() / (size
/ 8);
554 // Return the number of entries in the PLT.
556 plt_entry_count() const;
558 // Return the offset of the first non-reserved PLT entry.
560 first_plt_entry_offset() const;
562 // Return the size of each PLT entry.
564 plt_entry_size() const;
566 // Add any special sections for this symbol to the gc work list.
567 // For powerpc64, this adds the code section of a function
570 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
572 // Handle target specific gc actions when adding a gc reference from
573 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
574 // and DST_OFF. For powerpc64, this adds a referenc to the code
575 // section of a function descriptor.
577 do_gc_add_reference(Symbol_table
* symtab
,
579 unsigned int src_shndx
,
581 unsigned int dst_shndx
,
582 Address dst_off
) const;
584 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
587 { return this->stub_tables_
; }
589 const Output_data_brlt_powerpc
<size
, big_endian
>*
591 { return this->brlt_section_
; }
594 add_branch_lookup_table(Address to
)
596 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
597 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
601 find_branch_lookup_table(Address to
)
603 typename
Branch_lookup_table::const_iterator p
604 = this->branch_lookup_table_
.find(to
);
605 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
609 write_branch_lookup_table(unsigned char *oview
)
611 for (typename
Branch_lookup_table::const_iterator p
612 = this->branch_lookup_table_
.begin();
613 p
!= this->branch_lookup_table_
.end();
616 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
621 plt_thread_safe() const
622 { return this->plt_thread_safe_
; }
638 : tls_get_addr_(NOT_EXPECTED
),
639 relinfo_(NULL
), relnum_(0), r_offset_(0)
644 if (this->tls_get_addr_
!= NOT_EXPECTED
)
651 if (this->relinfo_
!= NULL
)
652 gold_error_at_location(this->relinfo_
, this->relnum_
, this->r_offset_
,
653 _("missing expected __tls_get_addr call"));
657 expect_tls_get_addr_call(
658 const Relocate_info
<size
, big_endian
>* relinfo
,
662 this->tls_get_addr_
= EXPECTED
;
663 this->relinfo_
= relinfo
;
664 this->relnum_
= relnum
;
665 this->r_offset_
= r_offset
;
669 expect_tls_get_addr_call()
670 { this->tls_get_addr_
= EXPECTED
; }
673 skip_next_tls_get_addr_call()
674 {this->tls_get_addr_
= SKIP
; }
677 maybe_skip_tls_get_addr_call(unsigned int r_type
, const Symbol
* gsym
)
679 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
680 || r_type
== elfcpp::R_PPC_PLTREL24
)
682 && strcmp(gsym
->name(), "__tls_get_addr") == 0);
683 Tls_get_addr last_tls
= this->tls_get_addr_
;
684 this->tls_get_addr_
= NOT_EXPECTED
;
685 if (is_tls_call
&& last_tls
!= EXPECTED
)
687 else if (!is_tls_call
&& last_tls
!= NOT_EXPECTED
)
696 // What we're up to regarding calls to __tls_get_addr.
697 // On powerpc, the branch and link insn making a call to
698 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
699 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
700 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
701 // The marker relocation always comes first, and has the same
702 // symbol as the reloc on the insn setting up the __tls_get_addr
703 // argument. This ties the arg setup insn with the call insn,
704 // allowing ld to safely optimize away the call. We check that
705 // every call to __tls_get_addr has a marker relocation, and that
706 // every marker relocation is on a call to __tls_get_addr.
707 Tls_get_addr tls_get_addr_
;
708 // Info about the last reloc for error message.
709 const Relocate_info
<size
, big_endian
>* relinfo_
;
714 // The class which scans relocations.
715 class Scan
: protected Track_tls
718 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
721 : Track_tls(), issued_non_pic_error_(false)
725 get_reference_flags(unsigned int r_type
);
728 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
729 Sized_relobj_file
<size
, big_endian
>* object
,
730 unsigned int data_shndx
,
731 Output_section
* output_section
,
732 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
733 const elfcpp::Sym
<size
, big_endian
>& lsym
,
737 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
738 Sized_relobj_file
<size
, big_endian
>* object
,
739 unsigned int data_shndx
,
740 Output_section
* output_section
,
741 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
745 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
747 Sized_relobj_file
<size
, big_endian
>* ,
750 const elfcpp::Rela
<size
, big_endian
>& ,
752 const elfcpp::Sym
<size
, big_endian
>&)
756 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
758 Sized_relobj_file
<size
, big_endian
>* ,
761 const elfcpp::Rela
<size
,
763 unsigned int , Symbol
*)
768 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
769 unsigned int r_type
);
772 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
773 unsigned int r_type
, Symbol
*);
776 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
777 Target_powerpc
* target
);
780 check_non_pic(Relobj
*, unsigned int r_type
);
783 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, big_endian
>* object
,
784 unsigned int r_type
);
786 // Whether we have issued an error about a non-PIC compilation.
787 bool issued_non_pic_error_
;
791 symval_for_branch(Address value
, const Sized_symbol
<size
>* gsym
,
792 Powerpc_relobj
<size
, big_endian
>* object
,
793 unsigned int *dest_shndx
);
795 // The class which implements relocation.
796 class Relocate
: protected Track_tls
799 // Use 'at' branch hints when true, 'y' when false.
800 // FIXME maybe: set this with an option.
801 static const bool is_isa_v2
= true;
807 // Do a relocation. Return false if the caller should not issue
808 // any warnings about this relocation.
810 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
811 Output_section
*, size_t relnum
,
812 const elfcpp::Rela
<size
, big_endian
>&,
813 unsigned int r_type
, const Sized_symbol
<size
>*,
814 const Symbol_value
<size
>*,
816 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
820 class Relocate_comdat_behavior
823 // Decide what the linker should do for relocations that refer to
824 // discarded comdat sections.
825 inline Comdat_behavior
826 get(const char* name
)
828 gold::Default_comdat_behavior default_behavior
;
829 Comdat_behavior ret
= default_behavior
.get(name
);
830 if (ret
== CB_WARNING
)
833 && (strcmp(name
, ".fixup") == 0
834 || strcmp(name
, ".got2") == 0))
837 && (strcmp(name
, ".opd") == 0
838 || strcmp(name
, ".toc") == 0
839 || strcmp(name
, ".toc1") == 0))
846 // A class which returns the size required for a relocation type,
847 // used while scanning relocs during a relocatable link.
848 class Relocatable_size_for_reloc
852 get_size_for_reloc(unsigned int, Relobj
*)
859 // Optimize the TLS relocation type based on what we know about the
860 // symbol. IS_FINAL is true if the final address of this symbol is
861 // known at link time.
863 tls::Tls_optimization
864 optimize_tls_gd(bool is_final
)
866 // If we are generating a shared library, then we can't do anything
868 if (parameters
->options().shared())
869 return tls::TLSOPT_NONE
;
872 return tls::TLSOPT_TO_IE
;
873 return tls::TLSOPT_TO_LE
;
876 tls::Tls_optimization
879 if (parameters
->options().shared())
880 return tls::TLSOPT_NONE
;
882 return tls::TLSOPT_TO_LE
;
885 tls::Tls_optimization
886 optimize_tls_ie(bool is_final
)
888 if (!is_final
|| parameters
->options().shared())
889 return tls::TLSOPT_NONE
;
891 return tls::TLSOPT_TO_LE
;
896 make_glink_section(Layout
*);
898 // Create the PLT section.
900 make_plt_section(Symbol_table
*, Layout
*);
903 make_iplt_section(Symbol_table
*, Layout
*);
906 make_brlt_section(Layout
*);
908 // Create a PLT entry for a global symbol.
910 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
912 // Create a PLT entry for a local IFUNC symbol.
914 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
915 Sized_relobj_file
<size
, big_endian
>*,
919 // Create a GOT entry for local dynamic __tls_get_addr.
921 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
922 Sized_relobj_file
<size
, big_endian
>* object
);
925 tlsld_got_offset() const
927 return this->tlsld_got_offset_
;
930 // Get the dynamic reloc section, creating it if necessary.
932 rela_dyn_section(Layout
*);
934 // Copy a relocation against a global symbol.
936 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
937 Sized_relobj_file
<size
, big_endian
>* object
,
938 unsigned int shndx
, Output_section
* output_section
,
939 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
941 this->copy_relocs_
.copy_reloc(symtab
, layout
,
942 symtab
->get_sized_symbol
<size
>(sym
),
943 object
, shndx
, output_section
,
944 reloc
, this->rela_dyn_section(layout
));
947 // Look over all the input sections, deciding where to place stub.
949 group_sections(Layout
*, const Task
*);
951 // Sort output sections by address.
955 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
956 { return sec1
->address() < sec2
->address(); }
962 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
963 unsigned int data_shndx
,
968 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
969 r_type_(r_type
), r_sym_(r_sym
), addend_(addend
)
975 // If this branch needs a plt call stub, or a long branch stub, make one.
977 make_stub(Stub_table
<size
, big_endian
>*,
978 Stub_table
<size
, big_endian
>*,
979 Symbol_table
*) const;
982 // The branch location..
983 Powerpc_relobj
<size
, big_endian
>* object_
;
986 // ..and the branch type and destination.
987 unsigned int r_type_
;
992 // Information about this specific target which we pass to the
993 // general Target structure.
994 static Target::Target_info powerpc_info
;
996 // The types of GOT entries needed for this platform.
997 // These values are exposed to the ABI in an incremental link.
998 // Do not renumber existing values without changing the version
999 // number of the .gnu_incremental_inputs section.
1003 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
1004 GOT_TYPE_DTPREL
, // entry for @got@dtprel
1005 GOT_TYPE_TPREL
// entry for @got@tprel
1009 Output_data_got_powerpc
<size
, big_endian
>* got_
;
1011 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
1012 // The IPLT section.
1013 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
1014 // Section holding long branch destinations.
1015 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
1016 // The .glink section.
1017 Output_data_glink
<size
, big_endian
>* glink_
;
1018 // The dynamic reloc section.
1019 Reloc_section
* rela_dyn_
;
1020 // Relocs saved to avoid a COPY reloc.
1021 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
1022 // Space for variables copied with a COPY reloc.
1023 Output_data_space
* dynbss_
;
1024 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1025 unsigned int tlsld_got_offset_
;
1027 Stub_tables stub_tables_
;
1028 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
1029 Branch_lookup_table branch_lookup_table_
;
1031 typedef std::vector
<Branch_info
> Branches
;
1032 Branches branch_info_
;
1034 bool plt_thread_safe_
;
1038 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
1041 true, // is_big_endian
1042 elfcpp::EM_PPC
, // machine_code
1043 false, // has_make_symbol
1044 false, // has_resolve
1045 false, // has_code_fill
1046 true, // is_default_stack_executable
1047 false, // can_icf_inline_merge_sections
1049 "/usr/lib/ld.so.1", // dynamic_linker
1050 0x10000000, // default_text_segment_address
1051 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1052 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1053 false, // isolate_execinstr
1055 elfcpp::SHN_UNDEF
, // small_common_shndx
1056 elfcpp::SHN_UNDEF
, // large_common_shndx
1057 0, // small_common_section_flags
1058 0, // large_common_section_flags
1059 NULL
, // attributes_section
1060 NULL
// attributes_vendor
1064 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
1067 false, // is_big_endian
1068 elfcpp::EM_PPC
, // machine_code
1069 false, // has_make_symbol
1070 false, // has_resolve
1071 false, // has_code_fill
1072 true, // is_default_stack_executable
1073 false, // can_icf_inline_merge_sections
1075 "/usr/lib/ld.so.1", // dynamic_linker
1076 0x10000000, // default_text_segment_address
1077 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1078 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1079 false, // isolate_execinstr
1081 elfcpp::SHN_UNDEF
, // small_common_shndx
1082 elfcpp::SHN_UNDEF
, // large_common_shndx
1083 0, // small_common_section_flags
1084 0, // large_common_section_flags
1085 NULL
, // attributes_section
1086 NULL
// attributes_vendor
1090 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1093 true, // is_big_endian
1094 elfcpp::EM_PPC64
, // machine_code
1095 false, // has_make_symbol
1096 false, // has_resolve
1097 false, // has_code_fill
1098 true, // is_default_stack_executable
1099 false, // can_icf_inline_merge_sections
1101 "/usr/lib/ld.so.1", // dynamic_linker
1102 0x10000000, // default_text_segment_address
1103 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1104 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1105 false, // isolate_execinstr
1107 elfcpp::SHN_UNDEF
, // small_common_shndx
1108 elfcpp::SHN_UNDEF
, // large_common_shndx
1109 0, // small_common_section_flags
1110 0, // large_common_section_flags
1111 NULL
, // attributes_section
1112 NULL
// attributes_vendor
1116 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1119 false, // is_big_endian
1120 elfcpp::EM_PPC64
, // machine_code
1121 false, // has_make_symbol
1122 false, // has_resolve
1123 false, // has_code_fill
1124 true, // is_default_stack_executable
1125 false, // can_icf_inline_merge_sections
1127 "/usr/lib/ld.so.1", // dynamic_linker
1128 0x10000000, // default_text_segment_address
1129 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1130 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1131 false, // isolate_execinstr
1133 elfcpp::SHN_UNDEF
, // small_common_shndx
1134 elfcpp::SHN_UNDEF
, // large_common_shndx
1135 0, // small_common_section_flags
1136 0, // large_common_section_flags
1137 NULL
, // attributes_section
1138 NULL
// attributes_vendor
1142 is_branch_reloc(unsigned int r_type
)
1144 return (r_type
== elfcpp::R_POWERPC_REL24
1145 || r_type
== elfcpp::R_PPC_PLTREL24
1146 || r_type
== elfcpp::R_PPC_LOCAL24PC
1147 || r_type
== elfcpp::R_POWERPC_REL14
1148 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1149 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1150 || r_type
== elfcpp::R_POWERPC_ADDR24
1151 || r_type
== elfcpp::R_POWERPC_ADDR14
1152 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1153 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1156 // If INSN is an opcode that may be used with an @tls operand, return
1157 // the transformed insn for TLS optimisation, otherwise return 0. If
1158 // REG is non-zero only match an insn with RB or RA equal to REG.
1160 at_tls_transform(uint32_t insn
, unsigned int reg
)
1162 if ((insn
& (0x3f << 26)) != 31 << 26)
1166 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1167 rtra
= insn
& ((1 << 26) - (1 << 16));
1168 else if (((insn
>> 16) & 0x1f) == reg
)
1169 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1173 if ((insn
& (0x3ff << 1)) == 266 << 1)
1176 else if ((insn
& (0x1f << 1)) == 23 << 1
1177 && ((insn
& (0x1f << 6)) < 14 << 6
1178 || ((insn
& (0x1f << 6)) >= 16 << 6
1179 && (insn
& (0x1f << 6)) < 24 << 6)))
1180 // load and store indexed -> dform
1181 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1182 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1183 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1184 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1185 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1187 insn
= (58 << 26) | 2;
1194 // Modified version of symtab.h class Symbol member
1195 // Given a direct absolute or pc-relative static relocation against
1196 // the global symbol, this function returns whether a dynamic relocation
1201 needs_dynamic_reloc(const Symbol
* gsym
, int flags
)
1203 // No dynamic relocations in a static link!
1204 if (parameters
->doing_static_link())
1207 // A reference to an undefined symbol from an executable should be
1208 // statically resolved to 0, and does not need a dynamic relocation.
1209 // This matches gnu ld behavior.
1210 if (gsym
->is_undefined() && !parameters
->options().shared())
1213 // A reference to an absolute symbol does not need a dynamic relocation.
1214 if (gsym
->is_absolute())
1217 // An absolute reference within a position-independent output file
1218 // will need a dynamic relocation.
1219 if ((flags
& Symbol::ABSOLUTE_REF
)
1220 && parameters
->options().output_is_position_independent())
1223 // A function call that can branch to a local PLT entry does not need
1224 // a dynamic relocation.
1225 if ((flags
& Symbol::FUNCTION_CALL
) && gsym
->has_plt_offset())
1228 // A reference to any PLT entry in a non-position-independent executable
1229 // does not need a dynamic relocation.
1230 // Except due to having function descriptors on powerpc64 we don't define
1231 // functions to their plt code in an executable, so this doesn't apply.
1233 && !parameters
->options().output_is_position_independent()
1234 && gsym
->has_plt_offset())
1237 // A reference to a symbol defined in a dynamic object or to a
1238 // symbol that is preemptible will need a dynamic relocation.
1239 if (gsym
->is_from_dynobj()
1240 || gsym
->is_undefined()
1241 || gsym
->is_preemptible())
1244 // For all other cases, return FALSE.
1248 // Modified version of symtab.h class Symbol member
1249 // Whether we should use the PLT offset associated with a symbol for
1250 // a relocation. FLAGS is a set of Reference_flags.
1254 use_plt_offset(const Symbol
* gsym
, int flags
)
1256 // If the symbol doesn't have a PLT offset, then naturally we
1257 // don't want to use it.
1258 if (!gsym
->has_plt_offset())
1261 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
1262 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1265 // If we are going to generate a dynamic relocation, then we will
1266 // wind up using that, so no need to use the PLT entry.
1267 if (needs_dynamic_reloc
<size
>(gsym
, flags
))
1270 // If the symbol is from a dynamic object, we need to use the PLT
1272 if (gsym
->is_from_dynobj())
1275 // If we are generating a shared object, and this symbol is
1276 // undefined or preemptible, we need to use the PLT entry.
1277 if (parameters
->options().shared()
1278 && (gsym
->is_undefined() || gsym
->is_preemptible()))
1281 // If this is a call to a weak undefined symbol, we need to use
1282 // the PLT entry; the symbol may be defined by a library loaded
1284 if ((flags
& Symbol::FUNCTION_CALL
) && gsym
->is_weak_undefined())
1287 // Otherwise we can use the regular definition.
1291 template<int size
, bool big_endian
>
1292 class Powerpc_relocate_functions
1309 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1310 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1312 template<int valsize
>
1314 has_overflow_signed(Address value
)
1316 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1317 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1318 limit
<<= ((valsize
- 1) >> 1);
1319 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1320 return value
+ limit
> (limit
<< 1) - 1;
1323 template<int valsize
>
1325 has_overflow_bitfield(Address value
)
1327 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1328 limit
<<= ((valsize
- 1) >> 1);
1329 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1330 return value
> (limit
<< 1) - 1 && value
+ limit
> (limit
<< 1) - 1;
1333 template<int valsize
>
1334 static inline Status
1335 overflowed(Address value
, Overflow_check overflow
)
1337 if (overflow
== CHECK_SIGNED
)
1339 if (has_overflow_signed
<valsize
>(value
))
1340 return STATUS_OVERFLOW
;
1342 else if (overflow
== CHECK_BITFIELD
)
1344 if (has_overflow_bitfield
<valsize
>(value
))
1345 return STATUS_OVERFLOW
;
1350 // Do a simple RELA relocation
1351 template<int valsize
>
1352 static inline Status
1353 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1355 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1356 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1357 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
);
1358 return overflowed
<valsize
>(value
, overflow
);
1361 template<int valsize
>
1362 static inline Status
1363 rela(unsigned char* view
,
1364 unsigned int right_shift
,
1365 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
1367 Overflow_check overflow
)
1369 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1370 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1371 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
1372 Valtype reloc
= value
>> right_shift
;
1375 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
1376 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1379 // Do a simple RELA relocation, unaligned.
1380 template<int valsize
>
1381 static inline Status
1382 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1384 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
, value
);
1385 return overflowed
<valsize
>(value
, overflow
);
1388 template<int valsize
>
1389 static inline Status
1390 rela_ua(unsigned char* view
,
1391 unsigned int right_shift
,
1392 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
1394 Overflow_check overflow
)
1396 typedef typename
elfcpp::Swap_unaligned
<valsize
, big_endian
>::Valtype
1398 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(view
);
1399 Valtype reloc
= value
>> right_shift
;
1402 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
, val
| reloc
);
1403 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1407 // R_PPC64_ADDR64: (Symbol + Addend)
1409 addr64(unsigned char* view
, Address value
)
1410 { This::template rela
<64>(view
, value
, CHECK_NONE
); }
1412 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1414 addr64_u(unsigned char* view
, Address value
)
1415 { This::template rela_ua
<64>(view
, value
, CHECK_NONE
); }
1417 // R_POWERPC_ADDR32: (Symbol + Addend)
1418 static inline Status
1419 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
1420 { return This::template rela
<32>(view
, value
, overflow
); }
1422 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1423 static inline Status
1424 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1425 { return This::template rela_ua
<32>(view
, value
, overflow
); }
1427 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1428 static inline Status
1429 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
1431 Status stat
= This::template rela
<32>(view
, 0, 0x03fffffc, value
, overflow
);
1432 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1433 stat
= STATUS_OVERFLOW
;
1437 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1438 static inline Status
1439 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
1440 { return This::template rela
<16>(view
, value
, overflow
); }
1442 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1443 static inline Status
1444 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1445 { return This::template rela_ua
<16>(view
, value
, overflow
); }
1447 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1448 static inline Status
1449 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
1451 Status stat
= This::template rela
<16>(view
, 0, 0xfffc, value
, overflow
);
1452 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1453 stat
= STATUS_OVERFLOW
;
1457 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1459 addr16_hi(unsigned char* view
, Address value
)
1460 { This::template rela
<16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
1462 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1464 addr16_ha(unsigned char* view
, Address value
)
1465 { This::addr16_hi(view
, value
+ 0x8000); }
1467 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1469 addr16_hi2(unsigned char* view
, Address value
)
1470 { This::template rela
<16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
1472 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1474 addr16_ha2(unsigned char* view
, Address value
)
1475 { This::addr16_hi2(view
, value
+ 0x8000); }
1477 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1479 addr16_hi3(unsigned char* view
, Address value
)
1480 { This::template rela
<16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
1482 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1484 addr16_ha3(unsigned char* view
, Address value
)
1485 { This::addr16_hi3(view
, value
+ 0x8000); }
1487 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1488 static inline Status
1489 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
1491 Status stat
= This::template rela
<32>(view
, 0, 0xfffc, value
, overflow
);
1492 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1493 stat
= STATUS_OVERFLOW
;
1498 // Stash away the index of .got2 or .opd in a relocatable object, if
1499 // such a section exists.
1501 template<int size
, bool big_endian
>
1503 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
1504 Read_symbols_data
* sd
)
1506 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1507 const unsigned char* namesu
= sd
->section_names
->data();
1508 const char* names
= reinterpret_cast<const char*>(namesu
);
1509 section_size_type names_size
= sd
->section_names_size
;
1510 const unsigned char* s
;
1512 s
= this->find_shdr(pshdrs
, size
== 32 ? ".got2" : ".opd",
1513 names
, names_size
, NULL
);
1516 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
1517 this->special_
= ndx
;
1519 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
1522 // Examine .rela.opd to build info about function entry points.
1524 template<int size
, bool big_endian
>
1526 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
1528 const unsigned char* prelocs
,
1529 const unsigned char* plocal_syms
)
1533 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
1535 const int reloc_size
1536 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
1537 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1538 Address expected_off
= 0;
1539 bool regular
= true;
1540 unsigned int opd_ent_size
= 0;
1542 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
1544 Reltype
reloc(prelocs
);
1545 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
1546 = reloc
.get_r_info();
1547 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
1548 if (r_type
== elfcpp::R_PPC64_ADDR64
)
1550 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1551 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
1554 if (r_sym
< this->local_symbol_count())
1556 typename
elfcpp::Sym
<size
, big_endian
>
1557 lsym(plocal_syms
+ r_sym
* sym_size
);
1558 shndx
= lsym
.get_st_shndx();
1559 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1560 value
= lsym
.get_st_value();
1563 shndx
= this->symbol_section_and_value(r_sym
, &value
,
1565 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
1566 value
+ reloc
.get_r_addend());
1569 expected_off
= reloc
.get_r_offset();
1570 opd_ent_size
= expected_off
;
1572 else if (expected_off
!= reloc
.get_r_offset())
1574 expected_off
+= opd_ent_size
;
1576 else if (r_type
== elfcpp::R_PPC64_TOC
)
1578 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
1583 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1584 this->name().c_str(), r_type
);
1588 if (reloc_count
<= 2)
1589 opd_ent_size
= this->section_size(this->opd_shndx());
1590 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
1594 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1595 this->name().c_str());
1601 template<int size
, bool big_endian
>
1603 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
1605 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
1608 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
1609 p
!= rd
->relocs
.end();
1612 if (p
->data_shndx
== this->opd_shndx())
1614 uint64_t opd_size
= this->section_size(this->opd_shndx());
1615 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
1618 this->init_opd(opd_size
);
1619 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
1620 rd
->local_symbols
->data());
1628 // Set up some symbols.
1630 template<int size
, bool big_endian
>
1632 Target_powerpc
<size
, big_endian
>::do_define_standard_symbols(
1633 Symbol_table
* symtab
,
1638 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
1639 // undefined when scanning relocs (and thus requires
1640 // non-relative dynamic relocs). The proper value will be
1642 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
1643 if (gotsym
!= NULL
&& gotsym
->is_undefined())
1645 Target_powerpc
<size
, big_endian
>* target
=
1646 static_cast<Target_powerpc
<size
, big_endian
>*>(
1647 parameters
->sized_target
<size
, big_endian
>());
1648 Output_data_got_powerpc
<size
, big_endian
>* got
1649 = target
->got_section(symtab
, layout
);
1650 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1651 Symbol_table::PREDEFINED
,
1655 elfcpp::STV_HIDDEN
, 0,
1659 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
1660 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
1661 if (sdasym
!= NULL
&& sdasym
->is_undefined())
1663 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
1665 = layout
->add_output_section_data(".sdata", 0,
1667 | elfcpp::SHF_WRITE
,
1668 sdata
, ORDER_SMALL_DATA
, false);
1669 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
1670 Symbol_table::PREDEFINED
,
1671 os
, 32768, 0, elfcpp::STT_OBJECT
,
1672 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
1678 // Set up PowerPC target specific relobj.
1680 template<int size
, bool big_endian
>
1682 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
1683 const std::string
& name
,
1684 Input_file
* input_file
,
1685 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
1687 int et
= ehdr
.get_e_type();
1688 // ET_EXEC files are valid input for --just-symbols/-R,
1689 // and we treat them as relocatable objects.
1690 if (et
== elfcpp::ET_REL
1691 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
1693 Powerpc_relobj
<size
, big_endian
>* obj
=
1694 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1698 else if (et
== elfcpp::ET_DYN
)
1700 Sized_dynobj
<size
, big_endian
>* obj
=
1701 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1707 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
1712 template<int size
, bool big_endian
>
1713 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
1716 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1717 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1719 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
1720 : Output_data_got
<size
, big_endian
>(),
1721 symtab_(symtab
), layout_(layout
),
1722 header_ent_cnt_(size
== 32 ? 3 : 1),
1723 header_index_(size
== 32 ? 0x2000 : 0)
1728 // Create a new GOT entry and return its offset.
1730 add_got_entry(Got_entry got_entry
)
1732 this->reserve_ent();
1733 return Output_data_got
<size
, big_endian
>::add_got_entry(got_entry
);
1736 // Create a pair of new GOT entries and return the offset of the first.
1738 add_got_entry_pair(Got_entry got_entry_1
, Got_entry got_entry_2
)
1740 this->reserve_ent(2);
1741 return Output_data_got
<size
, big_endian
>::add_got_entry_pair(got_entry_1
,
1746 add_constant_pair(Valtype c1
, Valtype c2
)
1748 this->reserve_ent(2);
1749 unsigned int got_offset
= this->add_constant(c1
);
1750 this->add_constant(c2
);
1754 // Offset of _GLOBAL_OFFSET_TABLE_.
1758 return this->got_offset(this->header_index_
);
1761 // Offset of base used to access the GOT/TOC.
1762 // The got/toc pointer reg will be set to this value.
1764 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
1767 return this->g_o_t();
1769 return (this->output_section()->address()
1770 + object
->toc_base_offset()
1774 // Ensure our GOT has a header.
1776 set_final_data_size()
1778 if (this->header_ent_cnt_
!= 0)
1779 this->make_header();
1780 Output_data_got
<size
, big_endian
>::set_final_data_size();
1783 // First word of GOT header needs some values that are not
1784 // handled by Output_data_got so poke them in here.
1785 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1787 do_write(Output_file
* of
)
1790 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
1791 val
= this->layout_
->dynamic_section()->address();
1793 val
= this->output_section()->address() + 0x8000;
1794 this->replace_constant(this->header_index_
, val
);
1795 Output_data_got
<size
, big_endian
>::do_write(of
);
1800 reserve_ent(unsigned int cnt
= 1)
1802 if (this->header_ent_cnt_
== 0)
1804 if (this->num_entries() + cnt
> this->header_index_
)
1805 this->make_header();
1811 this->header_ent_cnt_
= 0;
1812 this->header_index_
= this->num_entries();
1815 Output_data_got
<size
, big_endian
>::add_constant(0);
1816 Output_data_got
<size
, big_endian
>::add_constant(0);
1817 Output_data_got
<size
, big_endian
>::add_constant(0);
1819 // Define _GLOBAL_OFFSET_TABLE_ at the header
1820 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
1823 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
1824 sym
->set_value(this->g_o_t());
1827 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1828 Symbol_table::PREDEFINED
,
1829 this, this->g_o_t(), 0,
1832 elfcpp::STV_HIDDEN
, 0,
1836 Output_data_got
<size
, big_endian
>::add_constant(0);
1839 // Stashed pointers.
1840 Symbol_table
* symtab_
;
1844 unsigned int header_ent_cnt_
;
1845 // GOT header index.
1846 unsigned int header_index_
;
1849 // Get the GOT section, creating it if necessary.
1851 template<int size
, bool big_endian
>
1852 Output_data_got_powerpc
<size
, big_endian
>*
1853 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
1856 if (this->got_
== NULL
)
1858 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1861 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
1863 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1864 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1865 this->got_
, ORDER_DATA
, false);
1871 // Get the dynamic reloc section, creating it if necessary.
1873 template<int size
, bool big_endian
>
1874 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
1875 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
1877 if (this->rela_dyn_
== NULL
)
1879 gold_assert(layout
!= NULL
);
1880 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1881 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1882 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1883 ORDER_DYNAMIC_RELOCS
, false);
1885 return this->rela_dyn_
;
1891 // Determine the stub group size. The group size is the absolute
1892 // value of the parameter --stub-group-size. If --stub-group-size
1893 // is passed a negative value, we restrict stubs to be always before
1894 // the stubbed branches.
1895 Stub_control(int32_t size
)
1896 : state_(NO_GROUP
), stub_group_size_(abs(size
)),
1897 stub14_group_size_(abs(size
)),
1898 stubs_always_before_branch_(size
< 0), suppress_size_errors_(false),
1899 group_end_addr_(0), owner_(NULL
), output_section_(NULL
)
1901 if (stub_group_size_
== 1)
1904 if (stubs_always_before_branch_
)
1906 stub_group_size_
= 0x1e00000;
1907 stub14_group_size_
= 0x7800;
1911 stub_group_size_
= 0x1c00000;
1912 stub14_group_size_
= 0x7000;
1914 suppress_size_errors_
= true;
1918 // Return true iff input section can be handled by current stub
1921 can_add_to_stub_group(Output_section
* o
,
1922 const Output_section::Input_section
* i
,
1925 const Output_section::Input_section
*
1931 { return output_section_
; }
1937 FINDING_STUB_SECTION
,
1942 uint32_t stub_group_size_
;
1943 uint32_t stub14_group_size_
;
1944 bool stubs_always_before_branch_
;
1945 bool suppress_size_errors_
;
1946 uint64_t group_end_addr_
;
1947 const Output_section::Input_section
* owner_
;
1948 Output_section
* output_section_
;
1951 // Return true iff input section can be handled by current stub/
1955 Stub_control::can_add_to_stub_group(Output_section
* o
,
1956 const Output_section::Input_section
* i
,
1960 = has14
? this->stub14_group_size_
: this->stub_group_size_
;
1961 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
1963 uint64_t start_addr
= o
->address();
1966 // .init and .fini sections are pasted together to form a single
1967 // function. We can't be adding stubs in the middle of the function.
1968 this_size
= o
->data_size();
1971 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
1972 this_size
= i
->data_size();
1974 uint64_t end_addr
= start_addr
+ this_size
;
1975 bool toobig
= this_size
> group_size
;
1977 if (toobig
&& !this->suppress_size_errors_
)
1978 gold_warning(_("%s:%s exceeds group size"),
1979 i
->relobj()->name().c_str(),
1980 i
->relobj()->section_name(i
->shndx()).c_str());
1982 if (this->state_
!= HAS_STUB_SECTION
1983 && (!whole_sec
|| this->output_section_
!= o
))
1986 this->output_section_
= o
;
1989 if (this->state_
== NO_GROUP
)
1991 this->state_
= FINDING_STUB_SECTION
;
1992 this->group_end_addr_
= end_addr
;
1994 else if (this->group_end_addr_
- start_addr
< group_size
)
1996 // Adding this section would make the group larger than GROUP_SIZE.
1997 else if (this->state_
== FINDING_STUB_SECTION
1998 && !this->stubs_always_before_branch_
2001 // But wait, there's more! Input sections up to GROUP_SIZE
2002 // bytes before the stub table can be handled by it too.
2003 this->state_
= HAS_STUB_SECTION
;
2004 this->group_end_addr_
= end_addr
;
2008 this->state_
= NO_GROUP
;
2014 // Look over all the input sections, deciding where to place stubs.
2016 template<int size
, bool big_endian
>
2018 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
2021 Stub_control
stub_control(parameters
->options().stub_group_size());
2023 // Group input sections and insert stub table
2024 Stub_table
<size
, big_endian
>* stub_table
= NULL
;
2025 Layout::Section_list section_list
;
2026 layout
->get_executable_sections(§ion_list
);
2027 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
2028 for (Layout::Section_list::reverse_iterator o
= section_list
.rbegin();
2029 o
!= section_list
.rend();
2032 typedef Output_section::Input_section_list Input_section_list
;
2033 for (Input_section_list::const_reverse_iterator i
2034 = (*o
)->input_sections().rbegin();
2035 i
!= (*o
)->input_sections().rend();
2038 if (i
->is_input_section())
2040 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2041 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2042 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
2043 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
2045 stub_table
->init(stub_control
.owner(),
2046 stub_control
.output_section());
2049 if (stub_table
== NULL
)
2050 stub_table
= this->new_stub_table();
2051 ppcobj
->set_stub_table(i
->shndx(), stub_table
);
2055 if (stub_table
!= NULL
)
2056 stub_table
->init(stub_control
.owner(), stub_control
.output_section());
2059 // If this branch needs a plt call stub, or a long branch stub, make one.
2061 template<int size
, bool big_endian
>
2063 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
2064 Stub_table
<size
, big_endian
>* stub_table
,
2065 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
2066 Symbol_table
* symtab
) const
2068 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
2069 if (sym
!= NULL
&& sym
->is_forwarder())
2070 sym
= symtab
->resolve_forwards(sym
);
2071 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
2073 ? use_plt_offset
<size
>(gsym
, Scan::get_reference_flags(this->r_type_
))
2074 : this->object_
->local_has_plt_offset(this->r_sym_
))
2076 if (stub_table
== NULL
)
2077 stub_table
= this->object_
->stub_table(this->shndx_
);
2078 if (stub_table
== NULL
)
2080 // This is a ref from a data section to an ifunc symbol.
2081 stub_table
= ifunc_stub_table
;
2083 gold_assert(stub_table
!= NULL
);
2085 stub_table
->add_plt_call_entry(this->object_
, gsym
,
2086 this->r_type_
, this->addend_
);
2088 stub_table
->add_plt_call_entry(this->object_
, this->r_sym_
,
2089 this->r_type_
, this->addend_
);
2093 unsigned int max_branch_offset
;
2094 if (this->r_type_
== elfcpp::R_POWERPC_REL14
2095 || this->r_type_
== elfcpp::R_POWERPC_REL14_BRTAKEN
2096 || this->r_type_
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
2097 max_branch_offset
= 1 << 15;
2098 else if (this->r_type_
== elfcpp::R_POWERPC_REL24
2099 || this->r_type_
== elfcpp::R_PPC_PLTREL24
2100 || this->r_type_
== elfcpp::R_PPC_LOCAL24PC
)
2101 max_branch_offset
= 1 << 25;
2104 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
2105 gold_assert(from
!= invalid_address
);
2106 from
+= (this->object_
->output_section(this->shndx_
)->address()
2111 switch (gsym
->source())
2113 case Symbol::FROM_OBJECT
:
2115 Object
* symobj
= gsym
->object();
2116 if (symobj
->is_dynamic()
2117 || symobj
->pluginobj() != NULL
)
2120 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
2121 if (shndx
== elfcpp::SHN_UNDEF
)
2126 case Symbol::IS_UNDEFINED
:
2132 Symbol_table::Compute_final_value_status status
;
2133 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
2134 if (status
!= Symbol_table::CFVS_OK
)
2139 const Symbol_value
<size
>* psymval
2140 = this->object_
->local_symbol(this->r_sym_
);
2141 Symbol_value
<size
> symval
;
2142 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
2143 typename
ObjType::Compute_final_local_value_status status
2144 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
2146 if (status
!= ObjType::CFLV_OK
2147 || !symval
.has_output_value())
2149 to
= symval
.value(this->object_
, 0);
2151 to
+= this->addend_
;
2152 if (stub_table
== NULL
)
2153 stub_table
= this->object_
->stub_table(this->shndx_
);
2154 gold_assert(stub_table
!= NULL
);
2155 if (size
== 64 && is_branch_reloc(this->r_type_
))
2157 unsigned int dest_shndx
;
2158 to
= stub_table
->targ()->symval_for_branch(to
, gsym
, this->object_
,
2161 Address delta
= to
- from
;
2162 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
2164 stub_table
->add_long_branch_entry(this->object_
, to
);
2169 // Relaxation hook. This is where we do stub generation.
2171 template<int size
, bool big_endian
>
2173 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
2174 const Input_objects
*,
2175 Symbol_table
* symtab
,
2179 unsigned int prev_brlt_size
= 0;
2182 bool thread_safe
= parameters
->options().plt_thread_safe();
2183 if (size
== 64 && !parameters
->options().user_set_plt_thread_safe())
2185 static const char* const thread_starter
[] =
2189 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2191 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2192 "mq_notify", "create_timer",
2196 "GOMP_parallel_start",
2197 "GOMP_parallel_loop_static_start",
2198 "GOMP_parallel_loop_dynamic_start",
2199 "GOMP_parallel_loop_guided_start",
2200 "GOMP_parallel_loop_runtime_start",
2201 "GOMP_parallel_sections_start",
2204 if (parameters
->options().shared())
2208 for (unsigned int i
= 0;
2209 i
< sizeof(thread_starter
) / sizeof(thread_starter
[0]);
2212 Symbol
* sym
= symtab
->lookup(thread_starter
[i
], NULL
);
2213 thread_safe
= (sym
!= NULL
2215 && sym
->in_real_elf());
2221 this->plt_thread_safe_
= thread_safe
;
2222 this->group_sections(layout
, task
);
2225 // We need address of stub tables valid for make_stub.
2226 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2227 p
!= this->stub_tables_
.end();
2230 const Powerpc_relobj
<size
, big_endian
>* object
2231 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
2232 Address off
= object
->get_output_section_offset((*p
)->shndx());
2233 gold_assert(off
!= invalid_address
);
2234 Output_section
* os
= (*p
)->output_section();
2235 (*p
)->set_address_and_size(os
, off
);
2240 // Clear plt call stubs, long branch stubs and branch lookup table.
2241 prev_brlt_size
= this->branch_lookup_table_
.size();
2242 this->branch_lookup_table_
.clear();
2243 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2244 p
!= this->stub_tables_
.end();
2247 (*p
)->clear_stubs();
2251 // Build all the stubs.
2252 Stub_table
<size
, big_endian
>* ifunc_stub_table
2253 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
2254 Stub_table
<size
, big_endian
>* one_stub_table
2255 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
2256 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
2257 b
!= this->branch_info_
.end();
2260 b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
);
2263 // Did anything change size?
2264 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
2265 bool again
= num_huge_branches
!= prev_brlt_size
;
2266 if (size
== 64 && num_huge_branches
!= 0)
2267 this->make_brlt_section(layout
);
2268 if (size
== 64 && again
)
2269 this->brlt_section_
->set_current_size(num_huge_branches
);
2271 typedef Unordered_set
<Output_section
*> Output_sections
;
2272 Output_sections os_need_update
;
2273 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2274 p
!= this->stub_tables_
.end();
2277 if ((*p
)->size_update())
2280 os_need_update
.insert((*p
)->output_section());
2284 // Set output section offsets for all input sections in an output
2285 // section that just changed size. Anything past the stubs will
2287 for (typename
Output_sections::iterator p
= os_need_update
.begin();
2288 p
!= os_need_update
.end();
2291 Output_section
* os
= *p
;
2293 typedef Output_section::Input_section_list Input_section_list
;
2294 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
2295 i
!= os
->input_sections().end();
2298 off
= align_address(off
, i
->addralign());
2299 if (i
->is_input_section() || i
->is_relaxed_input_section())
2300 i
->relobj()->set_section_offset(i
->shndx(), off
);
2301 if (i
->is_relaxed_input_section())
2303 Stub_table
<size
, big_endian
>* stub_table
2304 = static_cast<Stub_table
<size
, big_endian
>*>(
2305 i
->relaxed_input_section());
2306 off
+= stub_table
->set_address_and_size(os
, off
);
2309 off
+= i
->data_size();
2311 // If .brlt is part of this output section, then we have just
2312 // done the offset adjustment.
2313 os
->clear_section_offsets_need_adjustment();
2318 && num_huge_branches
!= 0
2319 && parameters
->options().output_is_position_independent())
2321 // Fill in the BRLT relocs.
2322 this->brlt_section_
->reset_data_size();
2323 for (typename
Branch_lookup_table::const_iterator p
2324 = this->branch_lookup_table_
.begin();
2325 p
!= this->branch_lookup_table_
.end();
2328 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
2330 this->brlt_section_
->finalize_data_size();
2335 // A class to handle the PLT data.
2337 template<int size
, bool big_endian
>
2338 class Output_data_plt_powerpc
: public Output_section_data_build
2341 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
2342 size
, big_endian
> Reloc_section
;
2344 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
2345 Reloc_section
* plt_rel
,
2346 unsigned int reserved_size
,
2348 : Output_section_data_build(size
== 32 ? 4 : 8),
2351 initial_plt_entry_size_(reserved_size
),
2355 // Add an entry to the PLT.
2360 add_ifunc_entry(Symbol
*);
2363 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
2365 // Return the .rela.plt section data.
2372 // Return the number of PLT entries.
2376 return ((this->current_data_size() - this->initial_plt_entry_size_
)
2380 // Return the offset of the first non-reserved PLT entry.
2382 first_plt_entry_offset()
2383 { return this->initial_plt_entry_size_
; }
2385 // Return the size of a PLT entry.
2387 get_plt_entry_size()
2388 { return plt_entry_size
; }
2392 do_adjust_output_section(Output_section
* os
)
2397 // Write to a map file.
2399 do_print_to_mapfile(Mapfile
* mapfile
) const
2400 { mapfile
->print_output_data(this, this->name_
); }
2403 // The size of an entry in the PLT.
2404 static const int plt_entry_size
= size
== 32 ? 4 : 24;
2406 // Write out the PLT data.
2408 do_write(Output_file
*);
2410 // The reloc section.
2411 Reloc_section
* rel_
;
2412 // Allows access to .glink for do_write.
2413 Target_powerpc
<size
, big_endian
>* targ_
;
2414 // The size of the first reserved entry.
2415 int initial_plt_entry_size_
;
2416 // What to report in map file.
2420 // Add an entry to the PLT.
2422 template<int size
, bool big_endian
>
2424 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
2426 if (!gsym
->has_plt_offset())
2428 section_size_type off
= this->current_data_size();
2430 off
+= this->first_plt_entry_offset();
2431 gsym
->set_plt_offset(off
);
2432 gsym
->set_needs_dynsym_entry();
2433 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
2434 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
2435 off
+= plt_entry_size
;
2436 this->set_current_data_size(off
);
2440 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
2442 template<int size
, bool big_endian
>
2444 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
2446 if (!gsym
->has_plt_offset())
2448 section_size_type off
= this->current_data_size();
2449 gsym
->set_plt_offset(off
);
2450 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
2452 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
2453 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
2454 off
+= plt_entry_size
;
2455 this->set_current_data_size(off
);
2459 // Add an entry for a local ifunc symbol to the IPLT.
2461 template<int size
, bool big_endian
>
2463 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
2464 Sized_relobj_file
<size
, big_endian
>* relobj
,
2465 unsigned int local_sym_index
)
2467 if (!relobj
->local_has_plt_offset(local_sym_index
))
2469 section_size_type off
= this->current_data_size();
2470 relobj
->set_local_plt_offset(local_sym_index
, off
);
2471 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
2473 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
2474 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
2476 off
+= plt_entry_size
;
2477 this->set_current_data_size(off
);
2481 static const uint32_t add_0_11_11
= 0x7c0b5a14;
2482 static const uint32_t add_2_2_11
= 0x7c425a14;
2483 static const uint32_t add_3_3_2
= 0x7c631214;
2484 static const uint32_t add_3_3_13
= 0x7c636a14;
2485 static const uint32_t add_11_0_11
= 0x7d605a14;
2486 static const uint32_t add_12_2_11
= 0x7d825a14;
2487 static const uint32_t add_12_12_11
= 0x7d8c5a14;
2488 static const uint32_t addi_11_11
= 0x396b0000;
2489 static const uint32_t addi_12_12
= 0x398c0000;
2490 static const uint32_t addi_2_2
= 0x38420000;
2491 static const uint32_t addi_3_2
= 0x38620000;
2492 static const uint32_t addi_3_3
= 0x38630000;
2493 static const uint32_t addis_0_2
= 0x3c020000;
2494 static const uint32_t addis_0_13
= 0x3c0d0000;
2495 static const uint32_t addis_11_11
= 0x3d6b0000;
2496 static const uint32_t addis_11_30
= 0x3d7e0000;
2497 static const uint32_t addis_12_12
= 0x3d8c0000;
2498 static const uint32_t addis_12_2
= 0x3d820000;
2499 static const uint32_t addis_3_2
= 0x3c620000;
2500 static const uint32_t addis_3_13
= 0x3c6d0000;
2501 static const uint32_t b
= 0x48000000;
2502 static const uint32_t bcl_20_31
= 0x429f0005;
2503 static const uint32_t bctr
= 0x4e800420;
2504 static const uint32_t blr
= 0x4e800020;
2505 static const uint32_t blrl
= 0x4e800021;
2506 static const uint32_t bnectr_p4
= 0x4ce20420;
2507 static const uint32_t cmpldi_2_0
= 0x28220000;
2508 static const uint32_t cror_15_15_15
= 0x4def7b82;
2509 static const uint32_t cror_31_31_31
= 0x4ffffb82;
2510 static const uint32_t ld_0_1
= 0xe8010000;
2511 static const uint32_t ld_0_12
= 0xe80c0000;
2512 static const uint32_t ld_11_12
= 0xe96c0000;
2513 static const uint32_t ld_11_2
= 0xe9620000;
2514 static const uint32_t ld_2_1
= 0xe8410000;
2515 static const uint32_t ld_2_11
= 0xe84b0000;
2516 static const uint32_t ld_2_12
= 0xe84c0000;
2517 static const uint32_t ld_2_2
= 0xe8420000;
2518 static const uint32_t lfd_0_1
= 0xc8010000;
2519 static const uint32_t li_0_0
= 0x38000000;
2520 static const uint32_t li_12_0
= 0x39800000;
2521 static const uint32_t lis_0_0
= 0x3c000000;
2522 static const uint32_t lis_11
= 0x3d600000;
2523 static const uint32_t lis_12
= 0x3d800000;
2524 static const uint32_t lwz_0_12
= 0x800c0000;
2525 static const uint32_t lwz_11_11
= 0x816b0000;
2526 static const uint32_t lwz_11_30
= 0x817e0000;
2527 static const uint32_t lwz_12_12
= 0x818c0000;
2528 static const uint32_t lwzu_0_12
= 0x840c0000;
2529 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
2530 static const uint32_t mflr_0
= 0x7c0802a6;
2531 static const uint32_t mflr_11
= 0x7d6802a6;
2532 static const uint32_t mflr_12
= 0x7d8802a6;
2533 static const uint32_t mtctr_0
= 0x7c0903a6;
2534 static const uint32_t mtctr_11
= 0x7d6903a6;
2535 static const uint32_t mtctr_12
= 0x7d8903a6;
2536 static const uint32_t mtlr_0
= 0x7c0803a6;
2537 static const uint32_t mtlr_12
= 0x7d8803a6;
2538 static const uint32_t nop
= 0x60000000;
2539 static const uint32_t ori_0_0_0
= 0x60000000;
2540 static const uint32_t std_0_1
= 0xf8010000;
2541 static const uint32_t std_0_12
= 0xf80c0000;
2542 static const uint32_t std_2_1
= 0xf8410000;
2543 static const uint32_t stfd_0_1
= 0xd8010000;
2544 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
2545 static const uint32_t sub_11_11_12
= 0x7d6c5850;
2546 static const uint32_t xor_11_11_11
= 0x7d6b5a78;
2548 // Write out the PLT.
2550 template<int size
, bool big_endian
>
2552 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
2556 const section_size_type offset
= this->offset();
2557 const section_size_type oview_size
2558 = convert_to_section_size_type(this->data_size());
2559 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2560 unsigned char* pov
= oview
;
2561 unsigned char* endpov
= oview
+ oview_size
;
2563 // The address of the .glink branch table
2564 const Output_data_glink
<size
, big_endian
>* glink
2565 = this->targ_
->glink_section();
2566 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
2568 while (pov
< endpov
)
2570 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
2575 of
->write_output_view(offset
, oview_size
, oview
);
2579 // Create the PLT section.
2581 template<int size
, bool big_endian
>
2583 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
2586 if (this->plt_
== NULL
)
2588 if (this->got_
== NULL
)
2589 this->got_section(symtab
, layout
);
2591 if (this->glink_
== NULL
)
2592 make_glink_section(layout
);
2594 // Ensure that .rela.dyn always appears before .rela.plt This is
2595 // necessary due to how, on PowerPC and some other targets, .rela.dyn
2596 // needs to include .rela.plt in it's range.
2597 this->rela_dyn_section(layout
);
2599 Reloc_section
* plt_rel
= new Reloc_section(false);
2600 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
2601 elfcpp::SHF_ALLOC
, plt_rel
,
2602 ORDER_DYNAMIC_PLT_RELOCS
, false);
2604 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
2605 size
== 32 ? 0 : 24,
2607 layout
->add_output_section_data(".plt",
2609 ? elfcpp::SHT_PROGBITS
2610 : elfcpp::SHT_NOBITS
),
2611 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2620 // Create the IPLT section.
2622 template<int size
, bool big_endian
>
2624 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
2627 if (this->iplt_
== NULL
)
2629 this->make_plt_section(symtab
, layout
);
2631 Reloc_section
* iplt_rel
= new Reloc_section(false);
2632 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
2634 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
2636 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
2640 // A section for huge long branch addresses, similar to plt section.
2642 template<int size
, bool big_endian
>
2643 class Output_data_brlt_powerpc
: public Output_section_data_build
2646 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2647 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
2648 size
, big_endian
> Reloc_section
;
2650 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
2651 Reloc_section
* brlt_rel
)
2652 : Output_section_data_build(size
== 32 ? 4 : 8),
2657 // Add a reloc for an entry in the BRLT.
2659 add_reloc(Address to
, unsigned int off
)
2660 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
2662 // Update section and reloc section size.
2664 set_current_size(unsigned int num_branches
)
2666 this->reset_address_and_file_offset();
2667 this->set_current_data_size(num_branches
* 16);
2668 this->finalize_data_size();
2669 Output_section
* os
= this->output_section();
2670 os
->set_section_offsets_need_adjustment();
2671 if (this->rel_
!= NULL
)
2673 unsigned int reloc_size
2674 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
2675 this->rel_
->reset_address_and_file_offset();
2676 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
2677 this->rel_
->finalize_data_size();
2678 Output_section
* os
= this->rel_
->output_section();
2679 os
->set_section_offsets_need_adjustment();
2685 do_adjust_output_section(Output_section
* os
)
2690 // Write to a map file.
2692 do_print_to_mapfile(Mapfile
* mapfile
) const
2693 { mapfile
->print_output_data(this, "** BRLT"); }
2696 // Write out the BRLT data.
2698 do_write(Output_file
*);
2700 // The reloc section.
2701 Reloc_section
* rel_
;
2702 Target_powerpc
<size
, big_endian
>* targ_
;
2705 // Make the branch lookup table section.
2707 template<int size
, bool big_endian
>
2709 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
2711 if (size
== 64 && this->brlt_section_
== NULL
)
2713 Reloc_section
* brlt_rel
= NULL
;
2714 bool is_pic
= parameters
->options().output_is_position_independent();
2717 // When PIC we can't fill in .brlt (like .plt it can be a
2718 // bss style section) but must initialise at runtime via
2719 // dynamic relocats.
2720 this->rela_dyn_section(layout
);
2721 brlt_rel
= new Reloc_section(false);
2722 this->rela_dyn_
->output_section()->add_output_section_data(brlt_rel
);
2725 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
2726 if (this->plt_
&& is_pic
)
2727 this->plt_
->output_section()
2728 ->add_output_section_data(this->brlt_section_
);
2730 layout
->add_output_section_data(".brlt",
2731 (is_pic
? elfcpp::SHT_NOBITS
2732 : elfcpp::SHT_PROGBITS
),
2733 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2734 this->brlt_section_
,
2735 (is_pic
? ORDER_SMALL_BSS
2736 : ORDER_SMALL_DATA
),
2741 // Write out .brlt when non-PIC.
2743 template<int size
, bool big_endian
>
2745 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
2747 if (size
== 64 && !parameters
->options().output_is_position_independent())
2749 const section_size_type offset
= this->offset();
2750 const section_size_type oview_size
2751 = convert_to_section_size_type(this->data_size());
2752 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2754 this->targ_
->write_branch_lookup_table(oview
);
2755 of
->write_output_view(offset
, oview_size
, oview
);
2759 static inline uint32_t
2765 static inline uint32_t
2771 static inline uint32_t
2774 return hi(a
+ 0x8000);
2777 template<bool big_endian
>
2779 write_insn(unsigned char* p
, uint32_t v
)
2781 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
2784 // Stub_table holds information about plt and long branch stubs.
2785 // Stubs are built in an area following some input section determined
2786 // by group_sections(). This input section is converted to a relaxed
2787 // input section allowing it to be resized to accommodate the stubs
2789 template<int size
, bool big_endian
>
2790 class Stub_table
: public Output_relaxed_input_section
2793 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2794 static const Address invalid_address
= static_cast<Address
>(0) - 1;
2796 Stub_table(Target_powerpc
<size
, big_endian
>* targ
)
2797 : Output_relaxed_input_section(NULL
, 0, 0),
2798 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
2799 orig_data_size_(0), plt_size_(0), last_plt_size_(0),
2800 branch_size_(0), last_branch_size_(0)
2803 // Delayed Output_relaxed_input_section init.
2805 init(const Output_section::Input_section
*, Output_section
*);
2807 // Add a plt call stub.
2809 add_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2815 add_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2820 // Find a given plt call stub.
2822 find_plt_call_entry(const Symbol
*) const;
2825 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2826 unsigned int) const;
2829 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2835 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2840 // Add a long branch stub.
2842 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*, Address
);
2845 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*, Address
);
2850 this->plt_call_stubs_
.clear();
2851 this->plt_size_
= 0;
2852 this->long_branch_stubs_
.clear();
2853 this->branch_size_
= 0;
2857 set_address_and_size(const Output_section
* os
, Address off
)
2859 Address start_off
= off
;
2860 off
+= this->orig_data_size_
;
2861 Address my_size
= this->plt_size_
+ this->branch_size_
;
2863 off
= align_address(off
, this->stub_align());
2864 // Include original section size and alignment padding in size
2865 my_size
+= off
- start_off
;
2866 this->reset_address_and_file_offset();
2867 this->set_current_data_size(my_size
);
2868 this->set_address_and_file_offset(os
->address() + start_off
,
2869 os
->offset() + start_off
);
2876 return align_address(this->address() + this->orig_data_size_
,
2877 this->stub_align());
2883 return align_address(this->offset() + this->orig_data_size_
,
2884 this->stub_align());
2889 { return this->plt_size_
; }
2894 Output_section
* os
= this->output_section();
2895 if (os
->addralign() < this->stub_align())
2897 os
->set_addralign(this->stub_align());
2898 // FIXME: get rid of the insane checkpointing.
2899 // We can't increase alignment of the input section to which
2900 // stubs are attached; The input section may be .init which
2901 // is pasted together with other .init sections to form a
2902 // function. Aligning might insert zero padding resulting in
2903 // sigill. However we do need to increase alignment of the
2904 // output section so that the align_address() on offset in
2905 // set_address_and_size() adds the same padding as the
2906 // align_address() on address in stub_address().
2907 // What's more, we need this alignment for the layout done in
2908 // relaxation_loop_body() so that the output section starts at
2909 // a suitably aligned address.
2910 os
->checkpoint_set_addralign(this->stub_align());
2912 if (this->last_plt_size_
!= this->plt_size_
2913 || this->last_branch_size_
!= this->branch_size_
)
2915 this->last_plt_size_
= this->plt_size_
;
2916 this->last_branch_size_
= this->branch_size_
;
2922 Target_powerpc
<size
, big_endian
>*
2928 class Plt_stub_ent_hash
;
2929 typedef Unordered_map
<Plt_stub_ent
, unsigned int,
2930 Plt_stub_ent_hash
> Plt_stub_entries
;
2932 // Alignment of stub section.
2938 unsigned int min_align
= 32;
2939 unsigned int user_align
= 1 << parameters
->options().plt_align();
2940 return std::max(user_align
, min_align
);
2943 // Return the plt offset for the given call stub.
2945 plt_off(typename
Plt_stub_entries::const_iterator p
, bool* is_iplt
) const
2947 const Symbol
* gsym
= p
->first
.sym_
;
2950 *is_iplt
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
2951 && gsym
->can_use_relative_reloc(false));
2952 return gsym
->plt_offset();
2957 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
2958 unsigned int local_sym_index
= p
->first
.locsym_
;
2959 return relobj
->local_plt_offset(local_sym_index
);
2963 // Size of a given plt call stub.
2965 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
2971 Address plt_addr
= this->plt_off(p
, &is_iplt
);
2973 plt_addr
+= this->targ_
->iplt_section()->address();
2975 plt_addr
+= this->targ_
->plt_section()->address();
2976 Address got_addr
= this->targ_
->got_section()->output_section()->address();
2977 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2978 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
2979 got_addr
+= ppcobj
->toc_base_offset();
2980 Address off
= plt_addr
- got_addr
;
2981 bool static_chain
= parameters
->options().plt_static_chain();
2982 bool thread_safe
= this->targ_
->plt_thread_safe();
2983 unsigned int bytes
= (4 * 5
2986 + 4 * (ha(off
) != 0)
2987 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
2988 unsigned int align
= 1 << parameters
->options().plt_align();
2990 bytes
= (bytes
+ align
- 1) & -align
;
2994 // Return long branch stub size.
2996 branch_stub_size(Address to
)
2999 = this->stub_address() + this->last_plt_size_
+ this->branch_size_
;
3000 if (to
- loc
+ (1 << 25) < 2 << 25)
3002 if (size
== 64 || !parameters
->options().output_is_position_independent())
3009 do_write(Output_file
*);
3011 // Plt call stub keys.
3015 Plt_stub_ent(const Symbol
* sym
)
3016 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3019 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3020 unsigned int locsym_index
)
3021 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3024 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3026 unsigned int r_type
,
3028 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3031 this->addend_
= addend
;
3032 else if (parameters
->options().output_is_position_independent()
3033 && r_type
== elfcpp::R_PPC_PLTREL24
)
3035 this->addend_
= addend
;
3036 if (this->addend_
>= 32768)
3037 this->object_
= object
;
3041 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3042 unsigned int locsym_index
,
3043 unsigned int r_type
,
3045 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3048 this->addend_
= addend
;
3049 else if (parameters
->options().output_is_position_independent()
3050 && r_type
== elfcpp::R_PPC_PLTREL24
)
3051 this->addend_
= addend
;
3054 bool operator==(const Plt_stub_ent
& that
) const
3056 return (this->sym_
== that
.sym_
3057 && this->object_
== that
.object_
3058 && this->addend_
== that
.addend_
3059 && this->locsym_
== that
.locsym_
);
3063 const Sized_relobj_file
<size
, big_endian
>* object_
;
3064 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
3065 unsigned int locsym_
;
3068 class Plt_stub_ent_hash
3071 size_t operator()(const Plt_stub_ent
& ent
) const
3073 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
3074 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
3080 // Long branch stub keys.
3081 class Branch_stub_ent
3084 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
, Address to
)
3085 : dest_(to
), toc_base_off_(0)
3088 toc_base_off_
= obj
->toc_base_offset();
3091 bool operator==(const Branch_stub_ent
& that
) const
3093 return (this->dest_
== that
.dest_
3095 || this->toc_base_off_
== that
.toc_base_off_
));
3099 unsigned int toc_base_off_
;
3102 class Branch_stub_ent_hash
3105 size_t operator()(const Branch_stub_ent
& ent
) const
3106 { return ent
.dest_
^ ent
.toc_base_off_
; }
3109 // In a sane world this would be a global.
3110 Target_powerpc
<size
, big_endian
>* targ_
;
3111 // Map sym/object/addend to stub offset.
3112 Plt_stub_entries plt_call_stubs_
;
3113 // Map destination address to stub offset.
3114 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
3115 Branch_stub_ent_hash
> Branch_stub_entries
;
3116 Branch_stub_entries long_branch_stubs_
;
3117 // size of input section
3118 section_size_type orig_data_size_
;
3120 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
3123 // Make a new stub table, and record.
3125 template<int size
, bool big_endian
>
3126 Stub_table
<size
, big_endian
>*
3127 Target_powerpc
<size
, big_endian
>::new_stub_table()
3129 Stub_table
<size
, big_endian
>* stub_table
3130 = new Stub_table
<size
, big_endian
>(this);
3131 this->stub_tables_
.push_back(stub_table
);
3135 // Delayed stub table initialisation, because we create the stub table
3136 // before we know to which section it will be attached.
3138 template<int size
, bool big_endian
>
3140 Stub_table
<size
, big_endian
>::init(
3141 const Output_section::Input_section
* owner
,
3142 Output_section
* output_section
)
3144 this->set_relobj(owner
->relobj());
3145 this->set_shndx(owner
->shndx());
3146 this->set_addralign(this->relobj()->section_addralign(this->shndx()));
3147 this->set_output_section(output_section
);
3148 this->orig_data_size_
= owner
->current_data_size();
3150 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
3151 new_relaxed
.push_back(this);
3152 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
3155 // Add a plt call stub, if we do not already have one for this
3156 // sym/object/addend combo.
3158 template<int size
, bool big_endian
>
3160 Stub_table
<size
, big_endian
>::add_plt_call_entry(
3161 const Sized_relobj_file
<size
, big_endian
>* object
,
3163 unsigned int r_type
,
3166 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
3167 Address off
= this->plt_size_
;
3168 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
3169 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
3171 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
3174 template<int size
, bool big_endian
>
3176 Stub_table
<size
, big_endian
>::add_plt_call_entry(
3177 const Sized_relobj_file
<size
, big_endian
>* object
,
3178 unsigned int locsym_index
,
3179 unsigned int r_type
,
3182 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
3183 Address off
= this->plt_size_
;
3184 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
3185 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
3187 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
3190 // Find a plt call stub.
3192 template<int size
, bool big_endian
>
3193 typename Stub_table
<size
, big_endian
>::Address
3194 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3195 const Sized_relobj_file
<size
, big_endian
>* object
,
3197 unsigned int r_type
,
3198 Address addend
) const
3200 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
3201 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3202 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3205 template<int size
, bool big_endian
>
3206 typename Stub_table
<size
, big_endian
>::Address
3207 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
3209 Plt_stub_ent
ent(gsym
);
3210 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3211 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3214 template<int size
, bool big_endian
>
3215 typename Stub_table
<size
, big_endian
>::Address
3216 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3217 const Sized_relobj_file
<size
, big_endian
>* object
,
3218 unsigned int locsym_index
,
3219 unsigned int r_type
,
3220 Address addend
) const
3222 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
3223 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3224 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3227 template<int size
, bool big_endian
>
3228 typename Stub_table
<size
, big_endian
>::Address
3229 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3230 const Sized_relobj_file
<size
, big_endian
>* object
,
3231 unsigned int locsym_index
) const
3233 Plt_stub_ent
ent(object
, locsym_index
);
3234 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3235 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3238 // Add a long branch stub if we don't already have one to given
3241 template<int size
, bool big_endian
>
3243 Stub_table
<size
, big_endian
>::add_long_branch_entry(
3244 const Powerpc_relobj
<size
, big_endian
>* object
,
3247 Branch_stub_ent
ent(object
, to
);
3248 Address off
= this->branch_size_
;
3249 if (this->long_branch_stubs_
.insert(std::make_pair(ent
, off
)).second
)
3251 unsigned int stub_size
= this->branch_stub_size(to
);
3252 this->branch_size_
= off
+ stub_size
;
3253 if (size
== 64 && stub_size
!= 4)
3254 this->targ_
->add_branch_lookup_table(to
);
3258 // Find long branch stub.
3260 template<int size
, bool big_endian
>
3261 typename Stub_table
<size
, big_endian
>::Address
3262 Stub_table
<size
, big_endian
>::find_long_branch_entry(
3263 const Powerpc_relobj
<size
, big_endian
>* object
,
3266 Branch_stub_ent
ent(object
, to
);
3267 typename
Branch_stub_entries::const_iterator p
3268 = this->long_branch_stubs_
.find(ent
);
3269 return p
== this->long_branch_stubs_
.end() ? invalid_address
: p
->second
;
3272 // A class to handle .glink.
3274 template<int size
, bool big_endian
>
3275 class Output_data_glink
: public Output_section_data
3278 static const int pltresolve_size
= 16*4;
3280 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
3281 : Output_section_data(16), targ_(targ
)
3285 // Write to a map file.
3287 do_print_to_mapfile(Mapfile
* mapfile
) const
3288 { mapfile
->print_output_data(this, _("** glink")); }
3292 set_final_data_size();
3296 do_write(Output_file
*);
3298 // Allows access to .got and .plt for do_write.
3299 Target_powerpc
<size
, big_endian
>* targ_
;
3302 template<int size
, bool big_endian
>
3304 Output_data_glink
<size
, big_endian
>::set_final_data_size()
3306 unsigned int count
= this->targ_
->plt_entry_count();
3307 section_size_type total
= 0;
3313 // space for branch table
3314 total
+= 4 * (count
- 1);
3316 total
+= -total
& 15;
3317 total
+= this->pltresolve_size
;
3321 total
+= this->pltresolve_size
;
3323 // space for branch table
3326 total
+= 4 * (count
- 0x8000);
3330 this->set_data_size(total
);
3333 // Write out plt and long branch stub code.
3335 template<int size
, bool big_endian
>
3337 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
3339 if (this->plt_call_stubs_
.empty()
3340 && this->long_branch_stubs_
.empty())
3343 const section_size_type start_off
= this->offset();
3344 const section_size_type off
= this->stub_offset();
3345 const section_size_type oview_size
=
3346 convert_to_section_size_type(this->data_size() - (off
- start_off
));
3347 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
3352 const Output_data_got_powerpc
<size
, big_endian
>* got
3353 = this->targ_
->got_section();
3354 Address got_os_addr
= got
->output_section()->address();
3356 if (!this->plt_call_stubs_
.empty())
3358 // The base address of the .plt section.
3359 Address plt_base
= this->targ_
->plt_section()->address();
3360 Address iplt_base
= invalid_address
;
3362 // Write out plt call stubs.
3363 typename
Plt_stub_entries::const_iterator cs
;
3364 for (cs
= this->plt_call_stubs_
.begin();
3365 cs
!= this->plt_call_stubs_
.end();
3369 Address pltoff
= this->plt_off(cs
, &is_iplt
);
3370 Address plt_addr
= pltoff
;
3373 if (iplt_base
== invalid_address
)
3374 iplt_base
= this->targ_
->iplt_section()->address();
3375 plt_addr
+= iplt_base
;
3378 plt_addr
+= plt_base
;
3379 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3380 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
3381 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
3382 Address off
= plt_addr
- got_addr
;
3384 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
3385 gold_error(_("%s: linkage table error against `%s'"),
3386 cs
->first
.object_
->name().c_str(),
3387 cs
->first
.sym_
->demangled_name().c_str());
3389 bool static_chain
= parameters
->options().plt_static_chain();
3390 bool thread_safe
= this->targ_
->plt_thread_safe();
3391 bool use_fake_dep
= false;
3392 Address cmp_branch_off
= 0;
3395 unsigned int pltindex
3396 = ((pltoff
- this->targ_
->first_plt_entry_offset())
3397 / this->targ_
->plt_entry_size());
3399 = (this->targ_
->glink_section()->pltresolve_size
3401 if (pltindex
> 32768)
3402 glinkoff
+= (pltindex
- 32768) * 4;
3404 = this->targ_
->glink_section()->address() + glinkoff
;
3406 = (this->stub_address() + cs
->second
+ 24
3407 + 4 * (ha(off
) != 0)
3408 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
3409 + 4 * static_chain
);
3410 cmp_branch_off
= to
- from
;
3411 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
3414 p
= oview
+ cs
->second
;
3417 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
3418 write_insn
<big_endian
>(p
, addis_12_2
+ ha(off
)), p
+= 4;
3419 write_insn
<big_endian
>(p
, ld_11_12
+ l(off
)), p
+= 4;
3420 if (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
3422 write_insn
<big_endian
>(p
, addi_12_12
+ l(off
)), p
+= 4;
3425 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3428 write_insn
<big_endian
>(p
, xor_11_11_11
), p
+= 4;
3429 write_insn
<big_endian
>(p
, add_12_12_11
), p
+= 4;
3431 write_insn
<big_endian
>(p
, ld_2_12
+ l(off
+ 8)), p
+= 4;
3433 write_insn
<big_endian
>(p
, ld_11_12
+ l(off
+ 16)), p
+= 4;
3437 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
3438 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
)), p
+= 4;
3439 if (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
3441 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
)), p
+= 4;
3444 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3447 write_insn
<big_endian
>(p
, xor_11_11_11
), p
+= 4;
3448 write_insn
<big_endian
>(p
, add_2_2_11
), p
+= 4;
3451 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16)), p
+= 4;
3452 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8)), p
+= 4;
3454 if (thread_safe
&& !use_fake_dep
)
3456 write_insn
<big_endian
>(p
, cmpldi_2_0
), p
+= 4;
3457 write_insn
<big_endian
>(p
, bnectr_p4
), p
+= 4;
3458 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
3461 write_insn
<big_endian
>(p
, bctr
);
3465 // Write out long branch stubs.
3466 typename
Branch_stub_entries::const_iterator bs
;
3467 for (bs
= this->long_branch_stubs_
.begin();
3468 bs
!= this->long_branch_stubs_
.end();
3471 p
= oview
+ this->plt_size_
+ bs
->second
;
3472 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
3473 Address delta
= bs
->first
.dest_
- loc
;
3474 if (delta
+ (1 << 25) < 2 << 25)
3475 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
3479 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
3480 gold_assert(brlt_addr
!= invalid_address
);
3481 brlt_addr
+= this->targ_
->brlt_section()->address();
3482 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
3483 Address brltoff
= brlt_addr
- got_addr
;
3484 if (ha(brltoff
) == 0)
3486 write_insn
<big_endian
>(p
, ld_11_2
+ l(brltoff
)), p
+= 4;
3490 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
3491 write_insn
<big_endian
>(p
, ld_11_12
+ l(brltoff
)), p
+= 4;
3493 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3494 write_insn
<big_endian
>(p
, bctr
);
3500 if (!this->plt_call_stubs_
.empty())
3502 // The base address of the .plt section.
3503 Address plt_base
= this->targ_
->plt_section()->address();
3504 Address iplt_base
= invalid_address
;
3505 // The address of _GLOBAL_OFFSET_TABLE_.
3506 Address g_o_t
= invalid_address
;
3508 // Write out plt call stubs.
3509 typename
Plt_stub_entries::const_iterator cs
;
3510 for (cs
= this->plt_call_stubs_
.begin();
3511 cs
!= this->plt_call_stubs_
.end();
3515 Address plt_addr
= this->plt_off(cs
, &is_iplt
);
3518 if (iplt_base
== invalid_address
)
3519 iplt_base
= this->targ_
->iplt_section()->address();
3520 plt_addr
+= iplt_base
;
3523 plt_addr
+= plt_base
;
3525 p
= oview
+ cs
->second
;
3526 if (parameters
->options().output_is_position_independent())
3529 const Powerpc_relobj
<size
, big_endian
>* ppcobj
3530 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
3531 (cs
->first
.object_
));
3532 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
3534 unsigned int got2
= ppcobj
->got2_shndx();
3535 got_addr
= ppcobj
->get_output_section_offset(got2
);
3536 gold_assert(got_addr
!= invalid_address
);
3537 got_addr
+= (ppcobj
->output_section(got2
)->address()
3538 + cs
->first
.addend_
);
3542 if (g_o_t
== invalid_address
)
3544 const Output_data_got_powerpc
<size
, big_endian
>* got
3545 = this->targ_
->got_section();
3546 g_o_t
= got
->address() + got
->g_o_t();
3551 Address off
= plt_addr
- got_addr
;
3554 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(off
));
3555 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
3556 write_insn
<big_endian
>(p
+ 8, bctr
);
3560 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(off
));
3561 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(off
));
3562 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
3563 write_insn
<big_endian
>(p
+ 12, bctr
);
3568 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
3569 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
3570 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
3571 write_insn
<big_endian
>(p
+ 12, bctr
);
3576 // Write out long branch stubs.
3577 typename
Branch_stub_entries::const_iterator bs
;
3578 for (bs
= this->long_branch_stubs_
.begin();
3579 bs
!= this->long_branch_stubs_
.end();
3582 p
= oview
+ this->plt_size_
+ bs
->second
;
3583 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
3584 Address delta
= bs
->first
.dest_
- loc
;
3585 if (delta
+ (1 << 25) < 2 << 25)
3586 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
3587 else if (!parameters
->options().output_is_position_independent())
3589 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(bs
->first
.dest_
));
3590 write_insn
<big_endian
>(p
+ 4, addi_12_12
+ l(bs
->first
.dest_
));
3591 write_insn
<big_endian
>(p
+ 8, mtctr_12
);
3592 write_insn
<big_endian
>(p
+ 12, bctr
);
3597 write_insn
<big_endian
>(p
+ 0, mflr_0
);
3598 write_insn
<big_endian
>(p
+ 4, bcl_20_31
);
3599 write_insn
<big_endian
>(p
+ 8, mflr_12
);
3600 write_insn
<big_endian
>(p
+ 12, addis_12_12
+ ha(delta
));
3601 write_insn
<big_endian
>(p
+ 16, addi_12_12
+ l(delta
));
3602 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
3603 write_insn
<big_endian
>(p
+ 24, mtctr_12
);
3604 write_insn
<big_endian
>(p
+ 28, bctr
);
3610 // Write out .glink.
3612 template<int size
, bool big_endian
>
3614 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
3616 const section_size_type off
= this->offset();
3617 const section_size_type oview_size
=
3618 convert_to_section_size_type(this->data_size());
3619 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
3622 // The base address of the .plt section.
3623 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3624 Address plt_base
= this->targ_
->plt_section()->address();
3628 // Write pltresolve stub.
3630 Address after_bcl
= this->address() + 16;
3631 Address pltoff
= plt_base
- after_bcl
;
3633 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
3635 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
3636 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
3637 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
3638 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
3639 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
3640 write_insn
<big_endian
>(p
, add_12_2_11
), p
+= 4;
3641 write_insn
<big_endian
>(p
, ld_11_12
+ 0), p
+= 4;
3642 write_insn
<big_endian
>(p
, ld_2_12
+ 8), p
+= 4;
3643 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3644 write_insn
<big_endian
>(p
, ld_11_12
+ 16), p
+= 4;
3645 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
3646 while (p
< oview
+ this->pltresolve_size
)
3647 write_insn
<big_endian
>(p
, nop
), p
+= 4;
3649 // Write lazy link call stubs.
3651 while (p
< oview
+ oview_size
)
3655 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
3659 write_insn
<big_endian
>(p
, lis_0_0
+ hi(indx
)), p
+= 4;
3660 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
3662 uint32_t branch_off
= 8 - (p
- oview
);
3663 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
3669 const Output_data_got_powerpc
<size
, big_endian
>* got
3670 = this->targ_
->got_section();
3671 // The address of _GLOBAL_OFFSET_TABLE_.
3672 Address g_o_t
= got
->address() + got
->g_o_t();
3674 // Write out pltresolve branch table.
3676 unsigned int the_end
= oview_size
- this->pltresolve_size
;
3677 unsigned char* end_p
= oview
+ the_end
;
3678 while (p
< end_p
- 8 * 4)
3679 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
3681 write_insn
<big_endian
>(p
, nop
), p
+= 4;
3683 // Write out pltresolve call stub.
3684 if (parameters
->options().output_is_position_independent())
3686 Address res0_off
= 0;
3687 Address after_bcl_off
= the_end
+ 12;
3688 Address bcl_res0
= after_bcl_off
- res0_off
;
3690 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
3691 write_insn
<big_endian
>(p
+ 4, mflr_0
);
3692 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
3693 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
3694 write_insn
<big_endian
>(p
+ 16, mflr_12
);
3695 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
3696 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
3698 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
3700 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
3701 if (ha(got_bcl
) == ha(got_bcl
+ 4))
3703 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
3704 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
3708 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
3709 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
3711 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
3712 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
3713 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
3714 write_insn
<big_endian
>(p
+ 52, bctr
);
3715 write_insn
<big_endian
>(p
+ 56, nop
);
3716 write_insn
<big_endian
>(p
+ 60, nop
);
3720 Address res0
= this->address();
3722 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
3723 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
3724 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
3725 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
3727 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
3728 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
3729 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
3730 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
3731 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
3732 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
3734 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
3735 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
3736 write_insn
<big_endian
>(p
+ 32, bctr
);
3737 write_insn
<big_endian
>(p
+ 36, nop
);
3738 write_insn
<big_endian
>(p
+ 40, nop
);
3739 write_insn
<big_endian
>(p
+ 44, nop
);
3740 write_insn
<big_endian
>(p
+ 48, nop
);
3741 write_insn
<big_endian
>(p
+ 52, nop
);
3742 write_insn
<big_endian
>(p
+ 56, nop
);
3743 write_insn
<big_endian
>(p
+ 60, nop
);
3748 of
->write_output_view(off
, oview_size
, oview
);
3752 // A class to handle linker generated save/restore functions.
3754 template<int size
, bool big_endian
>
3755 class Output_data_save_res
: public Output_section_data_build
3758 Output_data_save_res(Symbol_table
* symtab
);
3761 // Write to a map file.
3763 do_print_to_mapfile(Mapfile
* mapfile
) const
3764 { mapfile
->print_output_data(this, _("** save/restore")); }
3767 do_write(Output_file
*);
3770 // The maximum size of save/restore contents.
3771 static const unsigned int savres_max
= 218*4;
3774 savres_define(Symbol_table
* symtab
,
3776 unsigned int lo
, unsigned int hi
,
3777 unsigned char* write_ent(unsigned char*, int),
3778 unsigned char* write_tail(unsigned char*, int));
3780 unsigned char *contents_
;
3783 template<bool big_endian
>
3784 static unsigned char*
3785 savegpr0(unsigned char* p
, int r
)
3787 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3788 write_insn
<big_endian
>(p
, insn
);
3792 template<bool big_endian
>
3793 static unsigned char*
3794 savegpr0_tail(unsigned char* p
, int r
)
3796 p
= savegpr0
<big_endian
>(p
, r
);
3797 uint32_t insn
= std_0_1
+ 16;
3798 write_insn
<big_endian
>(p
, insn
);
3800 write_insn
<big_endian
>(p
, blr
);
3804 template<bool big_endian
>
3805 static unsigned char*
3806 restgpr0(unsigned char* p
, int r
)
3808 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3809 write_insn
<big_endian
>(p
, insn
);
3813 template<bool big_endian
>
3814 static unsigned char*
3815 restgpr0_tail(unsigned char* p
, int r
)
3817 uint32_t insn
= ld_0_1
+ 16;
3818 write_insn
<big_endian
>(p
, insn
);
3820 p
= restgpr0
<big_endian
>(p
, r
);
3821 write_insn
<big_endian
>(p
, mtlr_0
);
3825 p
= restgpr0
<big_endian
>(p
, 30);
3826 p
= restgpr0
<big_endian
>(p
, 31);
3828 write_insn
<big_endian
>(p
, blr
);
3832 template<bool big_endian
>
3833 static unsigned char*
3834 savegpr1(unsigned char* p
, int r
)
3836 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3837 write_insn
<big_endian
>(p
, insn
);
3841 template<bool big_endian
>
3842 static unsigned char*
3843 savegpr1_tail(unsigned char* p
, int r
)
3845 p
= savegpr1
<big_endian
>(p
, r
);
3846 write_insn
<big_endian
>(p
, blr
);
3850 template<bool big_endian
>
3851 static unsigned char*
3852 restgpr1(unsigned char* p
, int r
)
3854 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3855 write_insn
<big_endian
>(p
, insn
);
3859 template<bool big_endian
>
3860 static unsigned char*
3861 restgpr1_tail(unsigned char* p
, int r
)
3863 p
= restgpr1
<big_endian
>(p
, r
);
3864 write_insn
<big_endian
>(p
, blr
);
3868 template<bool big_endian
>
3869 static unsigned char*
3870 savefpr(unsigned char* p
, int r
)
3872 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3873 write_insn
<big_endian
>(p
, insn
);
3877 template<bool big_endian
>
3878 static unsigned char*
3879 savefpr0_tail(unsigned char* p
, int r
)
3881 p
= savefpr
<big_endian
>(p
, r
);
3882 write_insn
<big_endian
>(p
, std_0_1
+ 16);
3884 write_insn
<big_endian
>(p
, blr
);
3888 template<bool big_endian
>
3889 static unsigned char*
3890 restfpr(unsigned char* p
, int r
)
3892 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3893 write_insn
<big_endian
>(p
, insn
);
3897 template<bool big_endian
>
3898 static unsigned char*
3899 restfpr0_tail(unsigned char* p
, int r
)
3901 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
3903 p
= restfpr
<big_endian
>(p
, r
);
3904 write_insn
<big_endian
>(p
, mtlr_0
);
3908 p
= restfpr
<big_endian
>(p
, 30);
3909 p
= restfpr
<big_endian
>(p
, 31);
3911 write_insn
<big_endian
>(p
, blr
);
3915 template<bool big_endian
>
3916 static unsigned char*
3917 savefpr1_tail(unsigned char* p
, int r
)
3919 p
= savefpr
<big_endian
>(p
, r
);
3920 write_insn
<big_endian
>(p
, blr
);
3924 template<bool big_endian
>
3925 static unsigned char*
3926 restfpr1_tail(unsigned char* p
, int r
)
3928 p
= restfpr
<big_endian
>(p
, r
);
3929 write_insn
<big_endian
>(p
, blr
);
3933 template<bool big_endian
>
3934 static unsigned char*
3935 savevr(unsigned char* p
, int r
)
3937 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
3938 write_insn
<big_endian
>(p
, insn
);
3940 insn
= stvx_0_12_0
+ (r
<< 21);
3941 write_insn
<big_endian
>(p
, insn
);
3945 template<bool big_endian
>
3946 static unsigned char*
3947 savevr_tail(unsigned char* p
, int r
)
3949 p
= savevr
<big_endian
>(p
, r
);
3950 write_insn
<big_endian
>(p
, blr
);
3954 template<bool big_endian
>
3955 static unsigned char*
3956 restvr(unsigned char* p
, int r
)
3958 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
3959 write_insn
<big_endian
>(p
, insn
);
3961 insn
= lvx_0_12_0
+ (r
<< 21);
3962 write_insn
<big_endian
>(p
, insn
);
3966 template<bool big_endian
>
3967 static unsigned char*
3968 restvr_tail(unsigned char* p
, int r
)
3970 p
= restvr
<big_endian
>(p
, r
);
3971 write_insn
<big_endian
>(p
, blr
);
3976 template<int size
, bool big_endian
>
3977 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
3978 Symbol_table
* symtab
)
3979 : Output_section_data_build(4),
3982 this->savres_define(symtab
,
3983 "_savegpr0_", 14, 31,
3984 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
3985 this->savres_define(symtab
,
3986 "_restgpr0_", 14, 29,
3987 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
3988 this->savres_define(symtab
,
3989 "_restgpr0_", 30, 31,
3990 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
3991 this->savres_define(symtab
,
3992 "_savegpr1_", 14, 31,
3993 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
3994 this->savres_define(symtab
,
3995 "_restgpr1_", 14, 31,
3996 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
3997 this->savres_define(symtab
,
3998 "_savefpr_", 14, 31,
3999 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
4000 this->savres_define(symtab
,
4001 "_restfpr_", 14, 29,
4002 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
4003 this->savres_define(symtab
,
4004 "_restfpr_", 30, 31,
4005 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
4006 this->savres_define(symtab
,
4008 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
4009 this->savres_define(symtab
,
4011 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
4012 this->savres_define(symtab
,
4014 savevr
<big_endian
>, savevr_tail
<big_endian
>);
4015 this->savres_define(symtab
,
4017 restvr
<big_endian
>, restvr_tail
<big_endian
>);
4020 template<int size
, bool big_endian
>
4022 Output_data_save_res
<size
, big_endian
>::savres_define(
4023 Symbol_table
* symtab
,
4025 unsigned int lo
, unsigned int hi
,
4026 unsigned char* write_ent(unsigned char*, int),
4027 unsigned char* write_tail(unsigned char*, int))
4029 size_t len
= strlen(name
);
4030 bool writing
= false;
4033 memcpy(sym
, name
, len
);
4036 for (unsigned int i
= lo
; i
<= hi
; i
++)
4038 sym
[len
+ 0] = i
/ 10 + '0';
4039 sym
[len
+ 1] = i
% 10 + '0';
4040 Symbol
* gsym
= symtab
->lookup(sym
);
4041 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
4042 writing
= writing
|| refd
;
4045 if (this->contents_
== NULL
)
4046 this->contents_
= new unsigned char[this->savres_max
];
4048 section_size_type value
= this->current_data_size();
4049 unsigned char* p
= this->contents_
+ value
;
4051 p
= write_ent(p
, i
);
4053 p
= write_tail(p
, i
);
4054 section_size_type cur_size
= p
- this->contents_
;
4055 this->set_current_data_size(cur_size
);
4057 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
4058 this, value
, cur_size
- value
,
4059 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
4060 elfcpp::STV_HIDDEN
, 0, false, false);
4065 // Write out save/restore.
4067 template<int size
, bool big_endian
>
4069 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
4071 const section_size_type off
= this->offset();
4072 const section_size_type oview_size
=
4073 convert_to_section_size_type(this->data_size());
4074 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4075 memcpy(oview
, this->contents_
, oview_size
);
4076 of
->write_output_view(off
, oview_size
, oview
);
4080 // Create the glink section.
4082 template<int size
, bool big_endian
>
4084 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
4086 if (this->glink_
== NULL
)
4088 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
4089 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
4090 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
4091 this->glink_
, ORDER_TEXT
, false);
4095 // Create a PLT entry for a global symbol.
4097 template<int size
, bool big_endian
>
4099 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
4103 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4104 && gsym
->can_use_relative_reloc(false))
4106 if (this->iplt_
== NULL
)
4107 this->make_iplt_section(symtab
, layout
);
4108 this->iplt_
->add_ifunc_entry(gsym
);
4112 if (this->plt_
== NULL
)
4113 this->make_plt_section(symtab
, layout
);
4114 this->plt_
->add_entry(gsym
);
4118 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
4120 template<int size
, bool big_endian
>
4122 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
4123 Symbol_table
* symtab
,
4125 Sized_relobj_file
<size
, big_endian
>* relobj
,
4128 if (this->iplt_
== NULL
)
4129 this->make_iplt_section(symtab
, layout
);
4130 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
4133 // Return the number of entries in the PLT.
4135 template<int size
, bool big_endian
>
4137 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
4139 if (this->plt_
== NULL
)
4141 unsigned int count
= this->plt_
->entry_count();
4142 if (this->iplt_
!= NULL
)
4143 count
+= this->iplt_
->entry_count();
4147 // Return the offset of the first non-reserved PLT entry.
4149 template<int size
, bool big_endian
>
4151 Target_powerpc
<size
, big_endian
>::first_plt_entry_offset() const
4153 return this->plt_
->first_plt_entry_offset();
4156 // Return the size of each PLT entry.
4158 template<int size
, bool big_endian
>
4160 Target_powerpc
<size
, big_endian
>::plt_entry_size() const
4162 return Output_data_plt_powerpc
<size
, big_endian
>::get_plt_entry_size();
4165 // Create a GOT entry for local dynamic __tls_get_addr calls.
4167 template<int size
, bool big_endian
>
4169 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
4170 Symbol_table
* symtab
,
4172 Sized_relobj_file
<size
, big_endian
>* object
)
4174 if (this->tlsld_got_offset_
== -1U)
4176 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
4177 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
4178 Output_data_got_powerpc
<size
, big_endian
>* got
4179 = this->got_section(symtab
, layout
);
4180 unsigned int got_offset
= got
->add_constant_pair(0, 0);
4181 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
4183 this->tlsld_got_offset_
= got_offset
;
4185 return this->tlsld_got_offset_
;
4188 // Get the Reference_flags for a particular relocation.
4190 template<int size
, bool big_endian
>
4192 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(unsigned int r_type
)
4196 case elfcpp::R_POWERPC_NONE
:
4197 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
4198 case elfcpp::R_POWERPC_GNU_VTENTRY
:
4199 case elfcpp::R_PPC64_TOC
:
4200 // No symbol reference.
4203 case elfcpp::R_PPC64_ADDR64
:
4204 case elfcpp::R_PPC64_UADDR64
:
4205 case elfcpp::R_POWERPC_ADDR32
:
4206 case elfcpp::R_POWERPC_UADDR32
:
4207 case elfcpp::R_POWERPC_ADDR16
:
4208 case elfcpp::R_POWERPC_UADDR16
:
4209 case elfcpp::R_POWERPC_ADDR16_LO
:
4210 case elfcpp::R_POWERPC_ADDR16_HI
:
4211 case elfcpp::R_POWERPC_ADDR16_HA
:
4212 return Symbol::ABSOLUTE_REF
;
4214 case elfcpp::R_POWERPC_ADDR24
:
4215 case elfcpp::R_POWERPC_ADDR14
:
4216 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4217 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4218 return Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
4220 case elfcpp::R_PPC64_REL64
:
4221 case elfcpp::R_POWERPC_REL32
:
4222 case elfcpp::R_PPC_LOCAL24PC
:
4223 case elfcpp::R_POWERPC_REL16
:
4224 case elfcpp::R_POWERPC_REL16_LO
:
4225 case elfcpp::R_POWERPC_REL16_HI
:
4226 case elfcpp::R_POWERPC_REL16_HA
:
4227 return Symbol::RELATIVE_REF
;
4229 case elfcpp::R_POWERPC_REL24
:
4230 case elfcpp::R_PPC_PLTREL24
:
4231 case elfcpp::R_POWERPC_REL14
:
4232 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4233 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4234 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
4236 case elfcpp::R_POWERPC_GOT16
:
4237 case elfcpp::R_POWERPC_GOT16_LO
:
4238 case elfcpp::R_POWERPC_GOT16_HI
:
4239 case elfcpp::R_POWERPC_GOT16_HA
:
4240 case elfcpp::R_PPC64_GOT16_DS
:
4241 case elfcpp::R_PPC64_GOT16_LO_DS
:
4242 case elfcpp::R_PPC64_TOC16
:
4243 case elfcpp::R_PPC64_TOC16_LO
:
4244 case elfcpp::R_PPC64_TOC16_HI
:
4245 case elfcpp::R_PPC64_TOC16_HA
:
4246 case elfcpp::R_PPC64_TOC16_DS
:
4247 case elfcpp::R_PPC64_TOC16_LO_DS
:
4249 return Symbol::ABSOLUTE_REF
;
4251 case elfcpp::R_POWERPC_GOT_TPREL16
:
4252 case elfcpp::R_POWERPC_TLS
:
4253 return Symbol::TLS_REF
;
4255 case elfcpp::R_POWERPC_COPY
:
4256 case elfcpp::R_POWERPC_GLOB_DAT
:
4257 case elfcpp::R_POWERPC_JMP_SLOT
:
4258 case elfcpp::R_POWERPC_RELATIVE
:
4259 case elfcpp::R_POWERPC_DTPMOD
:
4261 // Not expected. We will give an error later.
4266 // Report an unsupported relocation against a local symbol.
4268 template<int size
, bool big_endian
>
4270 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
4271 Sized_relobj_file
<size
, big_endian
>* object
,
4272 unsigned int r_type
)
4274 gold_error(_("%s: unsupported reloc %u against local symbol"),
4275 object
->name().c_str(), r_type
);
4278 // We are about to emit a dynamic relocation of type R_TYPE. If the
4279 // dynamic linker does not support it, issue an error.
4281 template<int size
, bool big_endian
>
4283 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
4284 unsigned int r_type
)
4286 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
4288 // These are the relocation types supported by glibc for both 32-bit
4289 // and 64-bit powerpc.
4292 case elfcpp::R_POWERPC_NONE
:
4293 case elfcpp::R_POWERPC_RELATIVE
:
4294 case elfcpp::R_POWERPC_GLOB_DAT
:
4295 case elfcpp::R_POWERPC_DTPMOD
:
4296 case elfcpp::R_POWERPC_DTPREL
:
4297 case elfcpp::R_POWERPC_TPREL
:
4298 case elfcpp::R_POWERPC_JMP_SLOT
:
4299 case elfcpp::R_POWERPC_COPY
:
4300 case elfcpp::R_POWERPC_IRELATIVE
:
4301 case elfcpp::R_POWERPC_ADDR32
:
4302 case elfcpp::R_POWERPC_UADDR32
:
4303 case elfcpp::R_POWERPC_ADDR24
:
4304 case elfcpp::R_POWERPC_ADDR16
:
4305 case elfcpp::R_POWERPC_UADDR16
:
4306 case elfcpp::R_POWERPC_ADDR16_LO
:
4307 case elfcpp::R_POWERPC_ADDR16_HI
:
4308 case elfcpp::R_POWERPC_ADDR16_HA
:
4309 case elfcpp::R_POWERPC_ADDR14
:
4310 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4311 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4312 case elfcpp::R_POWERPC_REL32
:
4313 case elfcpp::R_POWERPC_REL24
:
4314 case elfcpp::R_POWERPC_TPREL16
:
4315 case elfcpp::R_POWERPC_TPREL16_LO
:
4316 case elfcpp::R_POWERPC_TPREL16_HI
:
4317 case elfcpp::R_POWERPC_TPREL16_HA
:
4328 // These are the relocation types supported only on 64-bit.
4329 case elfcpp::R_PPC64_ADDR64
:
4330 case elfcpp::R_PPC64_UADDR64
:
4331 case elfcpp::R_PPC64_JMP_IREL
:
4332 case elfcpp::R_PPC64_ADDR16_DS
:
4333 case elfcpp::R_PPC64_ADDR16_LO_DS
:
4334 case elfcpp::R_PPC64_ADDR16_HIGHER
:
4335 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
4336 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
4337 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
4338 case elfcpp::R_PPC64_REL64
:
4339 case elfcpp::R_POWERPC_ADDR30
:
4340 case elfcpp::R_PPC64_TPREL16_DS
:
4341 case elfcpp::R_PPC64_TPREL16_LO_DS
:
4342 case elfcpp::R_PPC64_TPREL16_HIGHER
:
4343 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
4344 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
4345 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
4356 // These are the relocation types supported only on 32-bit.
4357 // ??? glibc ld.so doesn't need to support these.
4358 case elfcpp::R_POWERPC_DTPREL16
:
4359 case elfcpp::R_POWERPC_DTPREL16_LO
:
4360 case elfcpp::R_POWERPC_DTPREL16_HI
:
4361 case elfcpp::R_POWERPC_DTPREL16_HA
:
4369 // This prevents us from issuing more than one error per reloc
4370 // section. But we can still wind up issuing more than one
4371 // error per object file.
4372 if (this->issued_non_pic_error_
)
4374 gold_assert(parameters
->options().output_is_position_independent());
4375 object
->error(_("requires unsupported dynamic reloc; "
4376 "recompile with -fPIC"));
4377 this->issued_non_pic_error_
= true;
4381 // Return whether we need to make a PLT entry for a relocation of the
4382 // given type against a STT_GNU_IFUNC symbol.
4384 template<int size
, bool big_endian
>
4386 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
4387 Sized_relobj_file
<size
, big_endian
>* object
,
4388 unsigned int r_type
)
4390 // In non-pic code any reference will resolve to the plt call stub
4391 // for the ifunc symbol.
4392 if (size
== 32 && !parameters
->options().output_is_position_independent())
4397 // Word size refs from data sections are OK.
4398 case elfcpp::R_POWERPC_ADDR32
:
4399 case elfcpp::R_POWERPC_UADDR32
:
4404 case elfcpp::R_PPC64_ADDR64
:
4405 case elfcpp::R_PPC64_UADDR64
:
4410 // GOT refs are good.
4411 case elfcpp::R_POWERPC_GOT16
:
4412 case elfcpp::R_POWERPC_GOT16_LO
:
4413 case elfcpp::R_POWERPC_GOT16_HI
:
4414 case elfcpp::R_POWERPC_GOT16_HA
:
4415 case elfcpp::R_PPC64_GOT16_DS
:
4416 case elfcpp::R_PPC64_GOT16_LO_DS
:
4419 // So are function calls.
4420 case elfcpp::R_POWERPC_ADDR24
:
4421 case elfcpp::R_POWERPC_ADDR14
:
4422 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4423 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4424 case elfcpp::R_POWERPC_REL24
:
4425 case elfcpp::R_PPC_PLTREL24
:
4426 case elfcpp::R_POWERPC_REL14
:
4427 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4428 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4435 // Anything else is a problem.
4436 // If we are building a static executable, the libc startup function
4437 // responsible for applying indirect function relocations is going
4438 // to complain about the reloc type.
4439 // If we are building a dynamic executable, we will have a text
4440 // relocation. The dynamic loader will set the text segment
4441 // writable and non-executable to apply text relocations. So we'll
4442 // segfault when trying to run the indirection function to resolve
4444 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
4445 object
->name().c_str(), r_type
);
4449 // Scan a relocation for a local symbol.
4451 template<int size
, bool big_endian
>
4453 Target_powerpc
<size
, big_endian
>::Scan::local(
4454 Symbol_table
* symtab
,
4456 Target_powerpc
<size
, big_endian
>* target
,
4457 Sized_relobj_file
<size
, big_endian
>* object
,
4458 unsigned int data_shndx
,
4459 Output_section
* output_section
,
4460 const elfcpp::Rela
<size
, big_endian
>& reloc
,
4461 unsigned int r_type
,
4462 const elfcpp::Sym
<size
, big_endian
>& lsym
,
4465 this->maybe_skip_tls_get_addr_call(r_type
, NULL
);
4467 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
4468 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
4470 this->expect_tls_get_addr_call();
4471 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
4472 if (tls_type
!= tls::TLSOPT_NONE
)
4473 this->skip_next_tls_get_addr_call();
4475 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
4476 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
4478 this->expect_tls_get_addr_call();
4479 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
4480 if (tls_type
!= tls::TLSOPT_NONE
)
4481 this->skip_next_tls_get_addr_call();
4484 Powerpc_relobj
<size
, big_endian
>* ppc_object
4485 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
4490 && data_shndx
== ppc_object
->opd_shndx()
4491 && r_type
== elfcpp::R_PPC64_ADDR64
)
4492 ppc_object
->set_opd_discard(reloc
.get_r_offset());
4496 // A local STT_GNU_IFUNC symbol may require a PLT entry.
4497 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
4498 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
4500 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4501 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4502 r_type
, r_sym
, reloc
.get_r_addend());
4503 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
4508 case elfcpp::R_POWERPC_NONE
:
4509 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
4510 case elfcpp::R_POWERPC_GNU_VTENTRY
:
4511 case elfcpp::R_PPC64_TOCSAVE
:
4512 case elfcpp::R_PPC_EMB_MRKREF
:
4513 case elfcpp::R_POWERPC_TLS
:
4516 case elfcpp::R_PPC64_TOC
:
4518 Output_data_got_powerpc
<size
, big_endian
>* got
4519 = target
->got_section(symtab
, layout
);
4520 if (parameters
->options().output_is_position_independent())
4522 Address off
= reloc
.get_r_offset();
4524 && data_shndx
== ppc_object
->opd_shndx()
4525 && ppc_object
->get_opd_discard(off
- 8))
4528 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4529 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
4530 rela_dyn
->add_output_section_relative(got
->output_section(),
4531 elfcpp::R_POWERPC_RELATIVE
,
4533 object
, data_shndx
, off
,
4534 symobj
->toc_base_offset());
4539 case elfcpp::R_PPC64_ADDR64
:
4540 case elfcpp::R_PPC64_UADDR64
:
4541 case elfcpp::R_POWERPC_ADDR32
:
4542 case elfcpp::R_POWERPC_UADDR32
:
4543 case elfcpp::R_POWERPC_ADDR24
:
4544 case elfcpp::R_POWERPC_ADDR16
:
4545 case elfcpp::R_POWERPC_ADDR16_LO
:
4546 case elfcpp::R_POWERPC_ADDR16_HI
:
4547 case elfcpp::R_POWERPC_ADDR16_HA
:
4548 case elfcpp::R_POWERPC_UADDR16
:
4549 case elfcpp::R_PPC64_ADDR16_HIGHER
:
4550 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
4551 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
4552 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
4553 case elfcpp::R_PPC64_ADDR16_DS
:
4554 case elfcpp::R_PPC64_ADDR16_LO_DS
:
4555 case elfcpp::R_POWERPC_ADDR14
:
4556 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4557 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4558 // If building a shared library (or a position-independent
4559 // executable), we need to create a dynamic relocation for
4561 if (parameters
->options().output_is_position_independent()
4562 || (size
== 64 && is_ifunc
))
4564 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4566 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
4567 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
4569 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4570 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
4573 rela_dyn
= target
->iplt_section()->rel_plt();
4574 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
4576 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
4577 output_section
, data_shndx
,
4578 reloc
.get_r_offset(),
4579 reloc
.get_r_addend(), false);
4583 check_non_pic(object
, r_type
);
4584 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4585 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
4586 data_shndx
, reloc
.get_r_offset(),
4587 reloc
.get_r_addend());
4592 case elfcpp::R_POWERPC_REL24
:
4593 case elfcpp::R_PPC_PLTREL24
:
4594 case elfcpp::R_PPC_LOCAL24PC
:
4595 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4596 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4597 reloc
.get_r_addend());
4600 case elfcpp::R_POWERPC_REL14
:
4601 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4602 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4603 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4604 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4605 reloc
.get_r_addend());
4608 case elfcpp::R_PPC64_REL64
:
4609 case elfcpp::R_POWERPC_REL32
:
4610 case elfcpp::R_POWERPC_REL16
:
4611 case elfcpp::R_POWERPC_REL16_LO
:
4612 case elfcpp::R_POWERPC_REL16_HI
:
4613 case elfcpp::R_POWERPC_REL16_HA
:
4614 case elfcpp::R_POWERPC_SECTOFF
:
4615 case elfcpp::R_POWERPC_TPREL16
:
4616 case elfcpp::R_POWERPC_DTPREL16
:
4617 case elfcpp::R_POWERPC_SECTOFF_LO
:
4618 case elfcpp::R_POWERPC_TPREL16_LO
:
4619 case elfcpp::R_POWERPC_DTPREL16_LO
:
4620 case elfcpp::R_POWERPC_SECTOFF_HI
:
4621 case elfcpp::R_POWERPC_TPREL16_HI
:
4622 case elfcpp::R_POWERPC_DTPREL16_HI
:
4623 case elfcpp::R_POWERPC_SECTOFF_HA
:
4624 case elfcpp::R_POWERPC_TPREL16_HA
:
4625 case elfcpp::R_POWERPC_DTPREL16_HA
:
4626 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
4627 case elfcpp::R_PPC64_TPREL16_HIGHER
:
4628 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
4629 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
4630 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
4631 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
4632 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
4633 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
4634 case elfcpp::R_PPC64_TPREL16_DS
:
4635 case elfcpp::R_PPC64_TPREL16_LO_DS
:
4636 case elfcpp::R_PPC64_DTPREL16_DS
:
4637 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
4638 case elfcpp::R_PPC64_SECTOFF_DS
:
4639 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
4640 case elfcpp::R_PPC64_TLSGD
:
4641 case elfcpp::R_PPC64_TLSLD
:
4644 case elfcpp::R_POWERPC_GOT16
:
4645 case elfcpp::R_POWERPC_GOT16_LO
:
4646 case elfcpp::R_POWERPC_GOT16_HI
:
4647 case elfcpp::R_POWERPC_GOT16_HA
:
4648 case elfcpp::R_PPC64_GOT16_DS
:
4649 case elfcpp::R_PPC64_GOT16_LO_DS
:
4651 // The symbol requires a GOT entry.
4652 Output_data_got_powerpc
<size
, big_endian
>* got
4653 = target
->got_section(symtab
, layout
);
4654 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4656 if (!parameters
->options().output_is_position_independent())
4658 if (size
== 32 && is_ifunc
)
4659 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
4661 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
4663 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
4665 // If we are generating a shared object or a pie, this
4666 // symbol's GOT entry will be set by a dynamic relocation.
4668 off
= got
->add_constant(0);
4669 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
4671 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4672 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
4675 rela_dyn
= target
->iplt_section()->rel_plt();
4676 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
4678 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
4679 got
, off
, 0, false);
4684 case elfcpp::R_PPC64_TOC16
:
4685 case elfcpp::R_PPC64_TOC16_LO
:
4686 case elfcpp::R_PPC64_TOC16_HI
:
4687 case elfcpp::R_PPC64_TOC16_HA
:
4688 case elfcpp::R_PPC64_TOC16_DS
:
4689 case elfcpp::R_PPC64_TOC16_LO_DS
:
4690 // We need a GOT section.
4691 target
->got_section(symtab
, layout
);
4694 case elfcpp::R_POWERPC_GOT_TLSGD16
:
4695 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
4696 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
4697 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
4699 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
4700 if (tls_type
== tls::TLSOPT_NONE
)
4702 Output_data_got_powerpc
<size
, big_endian
>* got
4703 = target
->got_section(symtab
, layout
);
4704 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4705 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4706 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
4707 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
4709 else if (tls_type
== tls::TLSOPT_TO_LE
)
4711 // no GOT relocs needed for Local Exec.
4718 case elfcpp::R_POWERPC_GOT_TLSLD16
:
4719 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
4720 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
4721 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
4723 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
4724 if (tls_type
== tls::TLSOPT_NONE
)
4725 target
->tlsld_got_offset(symtab
, layout
, object
);
4726 else if (tls_type
== tls::TLSOPT_TO_LE
)
4728 // no GOT relocs needed for Local Exec.
4729 if (parameters
->options().emit_relocs())
4731 Output_section
* os
= layout
->tls_segment()->first_section();
4732 gold_assert(os
!= NULL
);
4733 os
->set_needs_symtab_index();
4741 case elfcpp::R_POWERPC_GOT_DTPREL16
:
4742 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
4743 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
4744 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
4746 Output_data_got_powerpc
<size
, big_endian
>* got
4747 = target
->got_section(symtab
, layout
);
4748 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4749 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
4753 case elfcpp::R_POWERPC_GOT_TPREL16
:
4754 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
4755 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
4756 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
4758 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
4759 if (tls_type
== tls::TLSOPT_NONE
)
4761 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4762 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
4764 Output_data_got_powerpc
<size
, big_endian
>* got
4765 = target
->got_section(symtab
, layout
);
4766 unsigned int off
= got
->add_constant(0);
4767 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
4769 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4770 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
4771 elfcpp::R_POWERPC_TPREL
,
4775 else if (tls_type
== tls::TLSOPT_TO_LE
)
4777 // no GOT relocs needed for Local Exec.
4785 unsupported_reloc_local(object
, r_type
);
4791 case elfcpp::R_POWERPC_GOT_TLSLD16
:
4792 case elfcpp::R_POWERPC_GOT_TLSGD16
:
4793 case elfcpp::R_POWERPC_GOT_TPREL16
:
4794 case elfcpp::R_POWERPC_GOT_DTPREL16
:
4795 case elfcpp::R_POWERPC_GOT16
:
4796 case elfcpp::R_PPC64_GOT16_DS
:
4797 case elfcpp::R_PPC64_TOC16
:
4798 case elfcpp::R_PPC64_TOC16_DS
:
4799 ppc_object
->set_has_small_toc_reloc();
4805 // Report an unsupported relocation against a global symbol.
4807 template<int size
, bool big_endian
>
4809 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
4810 Sized_relobj_file
<size
, big_endian
>* object
,
4811 unsigned int r_type
,
4814 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4815 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
4818 // Scan a relocation for a global symbol.
4820 template<int size
, bool big_endian
>
4822 Target_powerpc
<size
, big_endian
>::Scan::global(
4823 Symbol_table
* symtab
,
4825 Target_powerpc
<size
, big_endian
>* target
,
4826 Sized_relobj_file
<size
, big_endian
>* object
,
4827 unsigned int data_shndx
,
4828 Output_section
* output_section
,
4829 const elfcpp::Rela
<size
, big_endian
>& reloc
,
4830 unsigned int r_type
,
4833 if (this->maybe_skip_tls_get_addr_call(r_type
, gsym
) == Track_tls::SKIP
)
4836 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
4837 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
4839 this->expect_tls_get_addr_call();
4840 const bool final
= gsym
->final_value_is_known();
4841 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
4842 if (tls_type
!= tls::TLSOPT_NONE
)
4843 this->skip_next_tls_get_addr_call();
4845 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
4846 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
4848 this->expect_tls_get_addr_call();
4849 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
4850 if (tls_type
!= tls::TLSOPT_NONE
)
4851 this->skip_next_tls_get_addr_call();
4854 Powerpc_relobj
<size
, big_endian
>* ppc_object
4855 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
4857 // A STT_GNU_IFUNC symbol may require a PLT entry.
4858 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4859 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
4861 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4862 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4863 reloc
.get_r_addend());
4864 target
->make_plt_entry(symtab
, layout
, gsym
);
4869 case elfcpp::R_POWERPC_NONE
:
4870 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
4871 case elfcpp::R_POWERPC_GNU_VTENTRY
:
4872 case elfcpp::R_PPC_LOCAL24PC
:
4873 case elfcpp::R_PPC_EMB_MRKREF
:
4874 case elfcpp::R_POWERPC_TLS
:
4877 case elfcpp::R_PPC64_TOC
:
4879 Output_data_got_powerpc
<size
, big_endian
>* got
4880 = target
->got_section(symtab
, layout
);
4881 if (parameters
->options().output_is_position_independent())
4883 Address off
= reloc
.get_r_offset();
4885 && data_shndx
== ppc_object
->opd_shndx()
4886 && ppc_object
->get_opd_discard(off
- 8))
4889 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4890 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
4891 if (data_shndx
!= ppc_object
->opd_shndx())
4892 symobj
= static_cast
4893 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
4894 rela_dyn
->add_output_section_relative(got
->output_section(),
4895 elfcpp::R_POWERPC_RELATIVE
,
4897 object
, data_shndx
, off
,
4898 symobj
->toc_base_offset());
4903 case elfcpp::R_PPC64_ADDR64
:
4905 && data_shndx
== ppc_object
->opd_shndx()
4906 && (gsym
->is_defined_in_discarded_section()
4907 || gsym
->object() != object
))
4909 ppc_object
->set_opd_discard(reloc
.get_r_offset());
4913 case elfcpp::R_PPC64_UADDR64
:
4914 case elfcpp::R_POWERPC_ADDR32
:
4915 case elfcpp::R_POWERPC_UADDR32
:
4916 case elfcpp::R_POWERPC_ADDR24
:
4917 case elfcpp::R_POWERPC_ADDR16
:
4918 case elfcpp::R_POWERPC_ADDR16_LO
:
4919 case elfcpp::R_POWERPC_ADDR16_HI
:
4920 case elfcpp::R_POWERPC_ADDR16_HA
:
4921 case elfcpp::R_POWERPC_UADDR16
:
4922 case elfcpp::R_PPC64_ADDR16_HIGHER
:
4923 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
4924 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
4925 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
4926 case elfcpp::R_PPC64_ADDR16_DS
:
4927 case elfcpp::R_PPC64_ADDR16_LO_DS
:
4928 case elfcpp::R_POWERPC_ADDR14
:
4929 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4930 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4932 // Make a PLT entry if necessary.
4933 if (gsym
->needs_plt_entry())
4935 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4937 elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4938 reloc
.get_r_addend());
4939 target
->make_plt_entry(symtab
, layout
, gsym
);
4940 // Since this is not a PC-relative relocation, we may be
4941 // taking the address of a function. In that case we need to
4942 // set the entry in the dynamic symbol table to the address of
4943 // the PLT call stub.
4945 && gsym
->is_from_dynobj()
4946 && !parameters
->options().output_is_position_independent())
4947 gsym
->set_needs_dynsym_value();
4949 // Make a dynamic relocation if necessary.
4950 if (needs_dynamic_reloc
<size
>(gsym
, Scan::get_reference_flags(r_type
))
4951 || (size
== 64 && gsym
->type() == elfcpp::STT_GNU_IFUNC
))
4953 if (gsym
->may_need_copy_reloc())
4955 target
->copy_reloc(symtab
, layout
, object
,
4956 data_shndx
, output_section
, gsym
, reloc
);
4958 else if ((size
== 32
4959 && r_type
== elfcpp::R_POWERPC_ADDR32
4960 && gsym
->can_use_relative_reloc(false)
4961 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
4962 && parameters
->options().shared()))
4964 && r_type
== elfcpp::R_PPC64_ADDR64
4965 && (gsym
->can_use_relative_reloc(false)
4966 || data_shndx
== ppc_object
->opd_shndx())))
4968 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4969 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
4970 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
4972 rela_dyn
= target
->iplt_section()->rel_plt();
4973 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
4975 rela_dyn
->add_symbolless_global_addend(
4976 gsym
, dynrel
, output_section
, object
, data_shndx
,
4977 reloc
.get_r_offset(), reloc
.get_r_addend());
4981 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4982 check_non_pic(object
, r_type
);
4983 rela_dyn
->add_global(gsym
, r_type
, output_section
,
4985 reloc
.get_r_offset(),
4986 reloc
.get_r_addend());
4992 case elfcpp::R_PPC_PLTREL24
:
4993 case elfcpp::R_POWERPC_REL24
:
4994 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4995 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4996 reloc
.get_r_addend());
4997 if (gsym
->needs_plt_entry()
4998 || (!gsym
->final_value_is_known()
4999 && (gsym
->is_undefined()
5000 || gsym
->is_from_dynobj()
5001 || gsym
->is_preemptible())))
5002 target
->make_plt_entry(symtab
, layout
, gsym
);
5005 case elfcpp::R_PPC64_REL64
:
5006 case elfcpp::R_POWERPC_REL32
:
5007 // Make a dynamic relocation if necessary.
5008 if (needs_dynamic_reloc
<size
>(gsym
, Scan::get_reference_flags(r_type
)))
5010 if (gsym
->may_need_copy_reloc())
5012 target
->copy_reloc(symtab
, layout
, object
,
5013 data_shndx
, output_section
, gsym
,
5018 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5019 check_non_pic(object
, r_type
);
5020 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
5021 data_shndx
, reloc
.get_r_offset(),
5022 reloc
.get_r_addend());
5027 case elfcpp::R_POWERPC_REL14
:
5028 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5029 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5030 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5031 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5032 reloc
.get_r_addend());
5035 case elfcpp::R_POWERPC_REL16
:
5036 case elfcpp::R_POWERPC_REL16_LO
:
5037 case elfcpp::R_POWERPC_REL16_HI
:
5038 case elfcpp::R_POWERPC_REL16_HA
:
5039 case elfcpp::R_POWERPC_SECTOFF
:
5040 case elfcpp::R_POWERPC_TPREL16
:
5041 case elfcpp::R_POWERPC_DTPREL16
:
5042 case elfcpp::R_POWERPC_SECTOFF_LO
:
5043 case elfcpp::R_POWERPC_TPREL16_LO
:
5044 case elfcpp::R_POWERPC_DTPREL16_LO
:
5045 case elfcpp::R_POWERPC_SECTOFF_HI
:
5046 case elfcpp::R_POWERPC_TPREL16_HI
:
5047 case elfcpp::R_POWERPC_DTPREL16_HI
:
5048 case elfcpp::R_POWERPC_SECTOFF_HA
:
5049 case elfcpp::R_POWERPC_TPREL16_HA
:
5050 case elfcpp::R_POWERPC_DTPREL16_HA
:
5051 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
5052 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5053 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
5054 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5055 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
5056 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5057 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
5058 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5059 case elfcpp::R_PPC64_TPREL16_DS
:
5060 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5061 case elfcpp::R_PPC64_DTPREL16_DS
:
5062 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
5063 case elfcpp::R_PPC64_SECTOFF_DS
:
5064 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
5065 case elfcpp::R_PPC64_TLSGD
:
5066 case elfcpp::R_PPC64_TLSLD
:
5069 case elfcpp::R_POWERPC_GOT16
:
5070 case elfcpp::R_POWERPC_GOT16_LO
:
5071 case elfcpp::R_POWERPC_GOT16_HI
:
5072 case elfcpp::R_POWERPC_GOT16_HA
:
5073 case elfcpp::R_PPC64_GOT16_DS
:
5074 case elfcpp::R_PPC64_GOT16_LO_DS
:
5076 // The symbol requires a GOT entry.
5077 Output_data_got_powerpc
<size
, big_endian
>* got
;
5079 got
= target
->got_section(symtab
, layout
);
5080 if (gsym
->final_value_is_known())
5082 if (size
== 32 && gsym
->type() == elfcpp::STT_GNU_IFUNC
)
5083 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
5085 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
5087 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
5089 // If we are generating a shared object or a pie, this
5090 // symbol's GOT entry will be set by a dynamic relocation.
5091 unsigned int off
= got
->add_constant(0);
5092 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
5094 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5095 if (gsym
->can_use_relative_reloc(false)
5097 && gsym
->visibility() == elfcpp::STV_PROTECTED
5098 && parameters
->options().shared()))
5100 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
5101 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
5103 rela_dyn
= target
->iplt_section()->rel_plt();
5104 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
5106 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
5110 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
5111 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
5117 case elfcpp::R_PPC64_TOC16
:
5118 case elfcpp::R_PPC64_TOC16_LO
:
5119 case elfcpp::R_PPC64_TOC16_HI
:
5120 case elfcpp::R_PPC64_TOC16_HA
:
5121 case elfcpp::R_PPC64_TOC16_DS
:
5122 case elfcpp::R_PPC64_TOC16_LO_DS
:
5123 // We need a GOT section.
5124 target
->got_section(symtab
, layout
);
5127 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5128 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
5129 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
5130 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
5132 const bool final
= gsym
->final_value_is_known();
5133 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
5134 if (tls_type
== tls::TLSOPT_NONE
)
5136 Output_data_got_powerpc
<size
, big_endian
>* got
5137 = target
->got_section(symtab
, layout
);
5138 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
,
5139 target
->rela_dyn_section(layout
),
5140 elfcpp::R_POWERPC_DTPMOD
,
5141 elfcpp::R_POWERPC_DTPREL
);
5143 else if (tls_type
== tls::TLSOPT_TO_IE
)
5145 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
5147 Output_data_got_powerpc
<size
, big_endian
>* got
5148 = target
->got_section(symtab
, layout
);
5149 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5150 if (gsym
->is_undefined()
5151 || gsym
->is_from_dynobj())
5153 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
5154 elfcpp::R_POWERPC_TPREL
);
5158 unsigned int off
= got
->add_constant(0);
5159 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
5160 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
5161 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
5166 else if (tls_type
== tls::TLSOPT_TO_LE
)
5168 // no GOT relocs needed for Local Exec.
5175 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5176 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
5177 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
5178 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
5180 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5181 if (tls_type
== tls::TLSOPT_NONE
)
5182 target
->tlsld_got_offset(symtab
, layout
, object
);
5183 else if (tls_type
== tls::TLSOPT_TO_LE
)
5185 // no GOT relocs needed for Local Exec.
5186 if (parameters
->options().emit_relocs())
5188 Output_section
* os
= layout
->tls_segment()->first_section();
5189 gold_assert(os
!= NULL
);
5190 os
->set_needs_symtab_index();
5198 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5199 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
5200 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
5201 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
5203 Output_data_got_powerpc
<size
, big_endian
>* got
5204 = target
->got_section(symtab
, layout
);
5205 if (!gsym
->final_value_is_known()
5206 && (gsym
->is_from_dynobj()
5207 || gsym
->is_undefined()
5208 || gsym
->is_preemptible()))
5209 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
5210 target
->rela_dyn_section(layout
),
5211 elfcpp::R_POWERPC_DTPREL
);
5213 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
5217 case elfcpp::R_POWERPC_GOT_TPREL16
:
5218 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
5219 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
5220 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
5222 const bool final
= gsym
->final_value_is_known();
5223 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
5224 if (tls_type
== tls::TLSOPT_NONE
)
5226 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
5228 Output_data_got_powerpc
<size
, big_endian
>* got
5229 = target
->got_section(symtab
, layout
);
5230 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5231 if (gsym
->is_undefined()
5232 || gsym
->is_from_dynobj())
5234 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
5235 elfcpp::R_POWERPC_TPREL
);
5239 unsigned int off
= got
->add_constant(0);
5240 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
5241 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
5242 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
5247 else if (tls_type
== tls::TLSOPT_TO_LE
)
5249 // no GOT relocs needed for Local Exec.
5257 unsupported_reloc_global(object
, r_type
, gsym
);
5263 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5264 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5265 case elfcpp::R_POWERPC_GOT_TPREL16
:
5266 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5267 case elfcpp::R_POWERPC_GOT16
:
5268 case elfcpp::R_PPC64_GOT16_DS
:
5269 case elfcpp::R_PPC64_TOC16
:
5270 case elfcpp::R_PPC64_TOC16_DS
:
5271 ppc_object
->set_has_small_toc_reloc();
5277 // Process relocations for gc.
5279 template<int size
, bool big_endian
>
5281 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
5282 Symbol_table
* symtab
,
5284 Sized_relobj_file
<size
, big_endian
>* object
,
5285 unsigned int data_shndx
,
5287 const unsigned char* prelocs
,
5289 Output_section
* output_section
,
5290 bool needs_special_offset_handling
,
5291 size_t local_symbol_count
,
5292 const unsigned char* plocal_symbols
)
5294 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
5295 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
5296 Powerpc_relobj
<size
, big_endian
>* ppc_object
5297 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5299 ppc_object
->set_opd_valid();
5300 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
5302 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
5303 for (p
= ppc_object
->access_from_map()->begin();
5304 p
!= ppc_object
->access_from_map()->end();
5307 Address dst_off
= p
->first
;
5308 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
5309 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
5310 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
5312 Object
* src_obj
= s
->first
;
5313 unsigned int src_indx
= s
->second
;
5314 symtab
->gc()->add_reference(src_obj
, src_indx
,
5315 ppc_object
, dst_indx
);
5319 ppc_object
->access_from_map()->clear();
5320 ppc_object
->process_gc_mark(symtab
);
5321 // Don't look at .opd relocs as .opd will reference everything.
5325 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
5326 typename
Target_powerpc::Relocatable_size_for_reloc
>(
5335 needs_special_offset_handling
,
5340 // Handle target specific gc actions when adding a gc reference from
5341 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
5342 // and DST_OFF. For powerpc64, this adds a referenc to the code
5343 // section of a function descriptor.
5345 template<int size
, bool big_endian
>
5347 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
5348 Symbol_table
* symtab
,
5350 unsigned int src_shndx
,
5352 unsigned int dst_shndx
,
5353 Address dst_off
) const
5355 Powerpc_relobj
<size
, big_endian
>* ppc_object
5356 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
5358 && !ppc_object
->is_dynamic()
5359 && dst_shndx
== ppc_object
->opd_shndx())
5361 if (ppc_object
->opd_valid())
5363 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
5364 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
5368 // If we haven't run scan_opd_relocs, we must delay
5369 // processing this function descriptor reference.
5370 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
5375 // Add any special sections for this symbol to the gc work list.
5376 // For powerpc64, this adds the code section of a function
5379 template<int size
, bool big_endian
>
5381 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
5382 Symbol_table
* symtab
,
5387 Powerpc_relobj
<size
, big_endian
>* ppc_object
5388 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
5390 unsigned int shndx
= sym
->shndx(&is_ordinary
);
5391 if (is_ordinary
&& shndx
== ppc_object
->opd_shndx())
5393 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
5394 Address dst_off
= gsym
->value();
5395 if (ppc_object
->opd_valid())
5397 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
5398 symtab
->gc()->worklist().push(Section_id(ppc_object
, dst_indx
));
5401 ppc_object
->add_gc_mark(dst_off
);
5406 // Scan relocations for a section.
5408 template<int size
, bool big_endian
>
5410 Target_powerpc
<size
, big_endian
>::scan_relocs(
5411 Symbol_table
* symtab
,
5413 Sized_relobj_file
<size
, big_endian
>* object
,
5414 unsigned int data_shndx
,
5415 unsigned int sh_type
,
5416 const unsigned char* prelocs
,
5418 Output_section
* output_section
,
5419 bool needs_special_offset_handling
,
5420 size_t local_symbol_count
,
5421 const unsigned char* plocal_symbols
)
5423 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
5424 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
5426 if (sh_type
== elfcpp::SHT_REL
)
5428 gold_error(_("%s: unsupported REL reloc section"),
5429 object
->name().c_str());
5433 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
5442 needs_special_offset_handling
,
5447 // Functor class for processing the global symbol table.
5448 // Removes symbols defined on discarded opd entries.
5450 template<bool big_endian
>
5451 class Global_symbol_visitor_opd
5454 Global_symbol_visitor_opd()
5458 operator()(Sized_symbol
<64>* sym
)
5460 if (sym
->has_symtab_index()
5461 || sym
->source() != Symbol::FROM_OBJECT
5462 || !sym
->in_real_elf())
5465 Powerpc_relobj
<64, big_endian
>* symobj
5466 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
5467 if (symobj
->is_dynamic()
5468 || symobj
->opd_shndx() == 0)
5472 unsigned int shndx
= sym
->shndx(&is_ordinary
);
5473 if (shndx
== symobj
->opd_shndx()
5474 && symobj
->get_opd_discard(sym
->value()))
5475 sym
->set_symtab_index(-1U);
5479 template<int size
, bool big_endian
>
5481 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
5483 Symbol_table
* symtab
)
5487 Output_data_save_res
<64, big_endian
>* savres
5488 = new Output_data_save_res
<64, big_endian
>(symtab
);
5489 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
5490 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
5491 savres
, ORDER_TEXT
, false);
5495 // Sort linker created .got section first (for the header), then input
5496 // sections belonging to files using small model code.
5498 template<bool big_endian
>
5499 class Sort_toc_sections
5503 operator()(const Output_section::Input_section
& is1
,
5504 const Output_section::Input_section
& is2
) const
5506 if (!is1
.is_input_section() && is2
.is_input_section())
5509 = (is1
.is_input_section()
5510 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
5511 ->has_small_toc_reloc()));
5513 = (is2
.is_input_section()
5514 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
5515 ->has_small_toc_reloc()));
5516 return small1
&& !small2
;
5520 // Finalize the sections.
5522 template<int size
, bool big_endian
>
5524 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
5526 const Input_objects
*,
5527 Symbol_table
* symtab
)
5529 if (parameters
->doing_static_link())
5531 // At least some versions of glibc elf-init.o have a strong
5532 // reference to __rela_iplt marker syms. A weak ref would be
5534 if (this->iplt_
!= NULL
)
5536 Reloc_section
* rel
= this->iplt_
->rel_plt();
5537 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
5538 Symbol_table::PREDEFINED
, rel
, 0, 0,
5539 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5540 elfcpp::STV_HIDDEN
, 0, false, true);
5541 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
5542 Symbol_table::PREDEFINED
, rel
, 0, 0,
5543 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5544 elfcpp::STV_HIDDEN
, 0, true, true);
5548 symtab
->define_as_constant("__rela_iplt_start", NULL
,
5549 Symbol_table::PREDEFINED
, 0, 0,
5550 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5551 elfcpp::STV_HIDDEN
, 0, true, false);
5552 symtab
->define_as_constant("__rela_iplt_end", NULL
,
5553 Symbol_table::PREDEFINED
, 0, 0,
5554 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5555 elfcpp::STV_HIDDEN
, 0, true, false);
5561 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
5562 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
5564 if (!parameters
->options().relocatable())
5566 this->define_save_restore_funcs(layout
, symtab
);
5568 // Annoyingly, we need to make these sections now whether or
5569 // not we need them. If we delay until do_relax then we
5570 // need to mess with the relaxation machinery checkpointing.
5571 this->got_section(symtab
, layout
);
5572 this->make_brlt_section(layout
);
5574 if (parameters
->options().toc_sort())
5576 Output_section
* os
= this->got_
->output_section();
5577 if (os
!= NULL
&& os
->input_sections().size() > 1)
5578 std::stable_sort(os
->input_sections().begin(),
5579 os
->input_sections().end(),
5580 Sort_toc_sections
<big_endian
>());
5585 // Fill in some more dynamic tags.
5586 Output_data_dynamic
* odyn
= layout
->dynamic_data();
5589 const Reloc_section
* rel_plt
= (this->plt_
== NULL
5591 : this->plt_
->rel_plt());
5592 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
5593 this->rela_dyn_
, true, size
== 32);
5597 if (this->got_
!= NULL
)
5599 this->got_
->finalize_data_size();
5600 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
5601 this->got_
, this->got_
->g_o_t());
5606 if (this->glink_
!= NULL
)
5608 this->glink_
->finalize_data_size();
5609 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
5611 (this->glink_
->pltresolve_size
5617 // Emit any relocs we saved in an attempt to avoid generating COPY
5619 if (this->copy_relocs_
.any_saved_relocs())
5620 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
5623 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
5627 ok_lo_toc_insn(uint32_t insn
)
5629 return ((insn
& (0x3f << 26)) == 14u << 26 /* addi */
5630 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
5631 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
5632 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
5633 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
5634 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
5635 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
5636 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
5637 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
5638 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
5639 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
5640 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
5641 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
5642 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
5643 || ((insn
& (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
5645 || ((insn
& (0x3f << 26)) == 62u << 26 /* std, stmd */
5646 && ((insn
& 3) == 0 || (insn
& 3) == 3))
5647 || (insn
& (0x3f << 26)) == 12u << 26 /* addic */);
5650 // Return the value to use for a branch relocation.
5652 template<int size
, bool big_endian
>
5653 typename Target_powerpc
<size
, big_endian
>::Address
5654 Target_powerpc
<size
, big_endian
>::symval_for_branch(
5656 const Sized_symbol
<size
>* gsym
,
5657 Powerpc_relobj
<size
, big_endian
>* object
,
5658 unsigned int *dest_shndx
)
5664 // If the symbol is defined in an opd section, ie. is a function
5665 // descriptor, use the function descriptor code entry address
5666 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
5668 && gsym
->source() != Symbol::FROM_OBJECT
)
5671 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
5672 unsigned int shndx
= symobj
->opd_shndx();
5675 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
5676 gold_assert(opd_addr
!= invalid_address
);
5677 opd_addr
+= symobj
->output_section(shndx
)->address();
5678 if (value
>= opd_addr
&& value
< opd_addr
+ symobj
->section_size(shndx
))
5681 *dest_shndx
= symobj
->get_opd_ent(value
- opd_addr
, &sec_off
);
5682 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
5683 gold_assert(sec_addr
!= invalid_address
);
5684 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
5685 value
= sec_addr
+ sec_off
;
5690 // Perform a relocation.
5692 template<int size
, bool big_endian
>
5694 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
5695 const Relocate_info
<size
, big_endian
>* relinfo
,
5696 Target_powerpc
* target
,
5699 const elfcpp::Rela
<size
, big_endian
>& rela
,
5700 unsigned int r_type
,
5701 const Sized_symbol
<size
>* gsym
,
5702 const Symbol_value
<size
>* psymval
,
5703 unsigned char* view
,
5705 section_size_type view_size
)
5707 switch (this->maybe_skip_tls_get_addr_call(r_type
, gsym
))
5709 case Track_tls::NOT_EXPECTED
:
5710 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5711 _("__tls_get_addr call lacks marker reloc"));
5713 case Track_tls::EXPECTED
:
5714 // We have already complained.
5716 case Track_tls::SKIP
:
5718 case Track_tls::NORMAL
:
5722 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
5723 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
5724 Powerpc_relobj
<size
, big_endian
>* const object
5725 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
5727 bool has_plt_value
= false;
5728 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5730 ? use_plt_offset
<size
>(gsym
, Scan::get_reference_flags(r_type
))
5731 : object
->local_has_plt_offset(r_sym
))
5733 Stub_table
<size
, big_endian
>* stub_table
5734 = object
->stub_table(relinfo
->data_shndx
);
5735 if (stub_table
== NULL
)
5737 // This is a ref from a data section to an ifunc symbol.
5738 if (target
->stub_tables().size() != 0)
5739 stub_table
= target
->stub_tables()[0];
5741 gold_assert(stub_table
!= NULL
);
5744 off
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
5745 rela
.get_r_addend());
5747 off
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
5748 rela
.get_r_addend());
5749 gold_assert(off
!= invalid_address
);
5750 value
= stub_table
->stub_address() + off
;
5751 has_plt_value
= true;
5754 if (r_type
== elfcpp::R_POWERPC_GOT16
5755 || r_type
== elfcpp::R_POWERPC_GOT16_LO
5756 || r_type
== elfcpp::R_POWERPC_GOT16_HI
5757 || r_type
== elfcpp::R_POWERPC_GOT16_HA
5758 || r_type
== elfcpp::R_PPC64_GOT16_DS
5759 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
5763 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
5764 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
5768 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5769 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
5770 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
5772 value
-= target
->got_section()->got_base_offset(object
);
5774 else if (r_type
== elfcpp::R_PPC64_TOC
)
5776 value
= (target
->got_section()->output_section()->address()
5777 + object
->toc_base_offset());
5779 else if (gsym
!= NULL
5780 && (r_type
== elfcpp::R_POWERPC_REL24
5781 || r_type
== elfcpp::R_PPC_PLTREL24
)
5786 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
5787 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
5788 bool can_plt_call
= false;
5789 if (rela
.get_r_offset() + 8 <= view_size
)
5791 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5792 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
5795 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
5797 elfcpp::Swap
<32, big_endian
>::writeval(wv
+ 1, ld_2_1
+ 40);
5798 can_plt_call
= true;
5803 // If we don't have a branch and link followed by a nop,
5804 // we can't go via the plt because there is no place to
5805 // put a toc restoring instruction.
5806 // Unless we know we won't be returning.
5807 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
5808 can_plt_call
= true;
5812 // This is not an error in one special case: A self
5813 // call. It isn't possible to cheaply verify we have
5814 // such a call so just check for a call to the same
5817 Address code
= value
;
5818 if (gsym
->source() == Symbol::FROM_OBJECT
5819 && gsym
->object() == object
)
5821 Address addend
= rela
.get_r_addend();
5822 unsigned int dest_shndx
;
5823 Address opdent
= psymval
->value(object
, addend
);
5824 code
= target
->symval_for_branch(opdent
, gsym
, object
,
5827 if (dest_shndx
== 0)
5828 dest_shndx
= gsym
->shndx(&is_ordinary
);
5829 ok
= dest_shndx
== relinfo
->data_shndx
;
5833 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5834 _("call lacks nop, can't restore toc; "
5835 "recompile with -fPIC"));
5841 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
5842 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
5843 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
5844 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
5846 // First instruction of a global dynamic sequence, arg setup insn.
5847 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
5848 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
5849 enum Got_type got_type
= GOT_TYPE_STANDARD
;
5850 if (tls_type
== tls::TLSOPT_NONE
)
5851 got_type
= GOT_TYPE_TLSGD
;
5852 else if (tls_type
== tls::TLSOPT_TO_IE
)
5853 got_type
= GOT_TYPE_TPREL
;
5854 if (got_type
!= GOT_TYPE_STANDARD
)
5858 gold_assert(gsym
->has_got_offset(got_type
));
5859 value
= gsym
->got_offset(got_type
);
5863 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5864 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
5865 value
= object
->local_got_offset(r_sym
, got_type
);
5867 value
-= target
->got_section()->got_base_offset(object
);
5869 if (tls_type
== tls::TLSOPT_TO_IE
)
5871 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
5872 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
5874 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
5875 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
5876 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
5878 insn
|= 32 << 26; // lwz
5880 insn
|= 58 << 26; // ld
5881 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5883 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
5884 - elfcpp::R_POWERPC_GOT_TLSGD16
);
5886 else if (tls_type
== tls::TLSOPT_TO_LE
)
5888 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
5889 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
5891 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
5892 Insn insn
= addis_3_13
;
5895 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5896 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
5897 value
= psymval
->value(object
, rela
.get_r_addend());
5901 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
5903 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5904 r_type
= elfcpp::R_POWERPC_NONE
;
5908 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
5909 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
5910 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
5911 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
5913 // First instruction of a local dynamic sequence, arg setup insn.
5914 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5915 if (tls_type
== tls::TLSOPT_NONE
)
5917 value
= target
->tlsld_got_offset();
5918 value
-= target
->got_section()->got_base_offset(object
);
5922 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
5923 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
5924 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
5926 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
5927 Insn insn
= addis_3_13
;
5930 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5931 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
5936 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
5938 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
5939 r_type
= elfcpp::R_POWERPC_NONE
;
5943 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
5944 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
5945 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
5946 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
5948 // Accesses relative to a local dynamic sequence address,
5949 // no optimisation here.
5952 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
5953 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
5957 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5958 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
5959 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
5961 value
-= target
->got_section()->got_base_offset(object
);
5963 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
5964 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
5965 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
5966 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
5968 // First instruction of initial exec sequence.
5969 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
5970 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
5971 if (tls_type
== tls::TLSOPT_NONE
)
5975 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
5976 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
5980 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5981 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
5982 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
5984 value
-= target
->got_section()->got_base_offset(object
);
5988 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
5989 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
5990 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
5992 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
5993 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
5994 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
5999 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6000 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6001 value
= psymval
->value(object
, rela
.get_r_addend());
6005 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6007 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6008 r_type
= elfcpp::R_POWERPC_NONE
;
6012 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6013 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6015 // Second instruction of a global dynamic sequence,
6016 // the __tls_get_addr call
6017 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
6018 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6019 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6020 if (tls_type
!= tls::TLSOPT_NONE
)
6022 if (tls_type
== tls::TLSOPT_TO_IE
)
6024 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6025 Insn insn
= add_3_3_13
;
6028 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6029 r_type
= elfcpp::R_POWERPC_NONE
;
6033 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6034 Insn insn
= addi_3_3
;
6035 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6036 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6037 view
+= 2 * big_endian
;
6038 value
= psymval
->value(object
, rela
.get_r_addend());
6040 this->skip_next_tls_get_addr_call();
6043 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6044 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6046 // Second instruction of a local dynamic sequence,
6047 // the __tls_get_addr call
6048 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
6049 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6050 if (tls_type
== tls::TLSOPT_TO_LE
)
6052 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6053 Insn insn
= addi_3_3
;
6054 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6055 this->skip_next_tls_get_addr_call();
6056 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6057 view
+= 2 * big_endian
;
6061 else if (r_type
== elfcpp::R_POWERPC_TLS
)
6063 // Second instruction of an initial exec sequence
6064 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6065 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
6066 if (tls_type
== tls::TLSOPT_TO_LE
)
6068 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6069 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6070 unsigned int reg
= size
== 32 ? 2 : 13;
6071 insn
= at_tls_transform(insn
, reg
);
6072 gold_assert(insn
!= 0);
6073 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6074 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6075 view
+= 2 * big_endian
;
6076 value
= psymval
->value(object
, rela
.get_r_addend());
6079 else if (!has_plt_value
)
6082 unsigned int dest_shndx
;
6083 if (r_type
!= elfcpp::R_PPC_PLTREL24
)
6084 addend
= rela
.get_r_addend();
6085 value
= psymval
->value(object
, addend
);
6086 if (size
== 64 && is_branch_reloc(r_type
))
6087 value
= target
->symval_for_branch(value
, gsym
, object
, &dest_shndx
);
6088 unsigned int max_branch_offset
= 0;
6089 if (r_type
== elfcpp::R_POWERPC_REL24
6090 || r_type
== elfcpp::R_PPC_PLTREL24
6091 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
6092 max_branch_offset
= 1 << 25;
6093 else if (r_type
== elfcpp::R_POWERPC_REL14
6094 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
6095 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
6096 max_branch_offset
= 1 << 15;
6097 if (max_branch_offset
!= 0
6098 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
6100 Stub_table
<size
, big_endian
>* stub_table
6101 = object
->stub_table(relinfo
->data_shndx
);
6102 gold_assert(stub_table
!= NULL
);
6103 Address off
= stub_table
->find_long_branch_entry(object
, value
);
6104 if (off
!= invalid_address
)
6105 value
= stub_table
->stub_address() + stub_table
->plt_size() + off
;
6111 case elfcpp::R_PPC64_REL64
:
6112 case elfcpp::R_POWERPC_REL32
:
6113 case elfcpp::R_POWERPC_REL24
:
6114 case elfcpp::R_PPC_PLTREL24
:
6115 case elfcpp::R_PPC_LOCAL24PC
:
6116 case elfcpp::R_POWERPC_REL16
:
6117 case elfcpp::R_POWERPC_REL16_LO
:
6118 case elfcpp::R_POWERPC_REL16_HI
:
6119 case elfcpp::R_POWERPC_REL16_HA
:
6120 case elfcpp::R_POWERPC_REL14
:
6121 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6122 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6126 case elfcpp::R_PPC64_TOC16
:
6127 case elfcpp::R_PPC64_TOC16_LO
:
6128 case elfcpp::R_PPC64_TOC16_HI
:
6129 case elfcpp::R_PPC64_TOC16_HA
:
6130 case elfcpp::R_PPC64_TOC16_DS
:
6131 case elfcpp::R_PPC64_TOC16_LO_DS
:
6132 // Subtract the TOC base address.
6133 value
-= (target
->got_section()->output_section()->address()
6134 + object
->toc_base_offset());
6137 case elfcpp::R_POWERPC_SECTOFF
:
6138 case elfcpp::R_POWERPC_SECTOFF_LO
:
6139 case elfcpp::R_POWERPC_SECTOFF_HI
:
6140 case elfcpp::R_POWERPC_SECTOFF_HA
:
6141 case elfcpp::R_PPC64_SECTOFF_DS
:
6142 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6144 value
-= os
->address();
6147 case elfcpp::R_PPC64_TPREL16_DS
:
6148 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6150 // R_PPC_TLSGD and R_PPC_TLSLD
6152 case elfcpp::R_POWERPC_TPREL16
:
6153 case elfcpp::R_POWERPC_TPREL16_LO
:
6154 case elfcpp::R_POWERPC_TPREL16_HI
:
6155 case elfcpp::R_POWERPC_TPREL16_HA
:
6156 case elfcpp::R_POWERPC_TPREL
:
6157 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6158 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6159 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6160 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6161 // tls symbol values are relative to tls_segment()->vaddr()
6165 case elfcpp::R_PPC64_DTPREL16_DS
:
6166 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6167 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6168 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6169 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6170 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6172 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
6173 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
6175 case elfcpp::R_POWERPC_DTPREL16
:
6176 case elfcpp::R_POWERPC_DTPREL16_LO
:
6177 case elfcpp::R_POWERPC_DTPREL16_HI
:
6178 case elfcpp::R_POWERPC_DTPREL16_HA
:
6179 case elfcpp::R_POWERPC_DTPREL
:
6180 // tls symbol values are relative to tls_segment()->vaddr()
6181 value
-= dtp_offset
;
6188 Insn branch_bit
= 0;
6191 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6192 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6193 branch_bit
= 1 << 21;
6194 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6195 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6197 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6198 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6201 if (this->is_isa_v2
)
6203 // Set 'a' bit. This is 0b00010 in BO field for branch
6204 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
6205 // for branch on CTR insns (BO == 1a00t or 1a01t).
6206 if ((insn
& (0x14 << 21)) == (0x04 << 21))
6208 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
6215 // Invert 'y' bit if not the default.
6216 if (static_cast<Signed_address
>(value
) < 0)
6219 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6229 // Multi-instruction sequences that access the TOC can be
6230 // optimized, eg. addis ra,r2,0; addi rb,ra,x;
6231 // to nop; addi rb,r2,x;
6237 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6238 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6239 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6240 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6241 case elfcpp::R_POWERPC_GOT16_HA
:
6242 case elfcpp::R_PPC64_TOC16_HA
:
6243 if (parameters
->options().toc_optimize())
6245 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6246 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6247 if ((insn
& ((0x3f << 26) | 0x1f << 16))
6248 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
6249 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6250 _("toc optimization is not supported "
6251 "for %#08x instruction"), insn
);
6252 else if (value
+ 0x8000 < 0x10000)
6254 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
6260 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6261 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6262 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6263 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6264 case elfcpp::R_POWERPC_GOT16_LO
:
6265 case elfcpp::R_PPC64_GOT16_LO_DS
:
6266 case elfcpp::R_PPC64_TOC16_LO
:
6267 case elfcpp::R_PPC64_TOC16_LO_DS
:
6268 if (parameters
->options().toc_optimize())
6270 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6271 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6272 if (!ok_lo_toc_insn(insn
))
6273 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6274 _("toc optimization is not supported "
6275 "for %#08x instruction"), insn
);
6276 else if (value
+ 0x8000 < 0x10000)
6278 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
6280 // Transform addic to addi when we change reg.
6281 insn
&= ~((0x3f << 26) | (0x1f << 16));
6282 insn
|= (14u << 26) | (2 << 16);
6286 insn
&= ~(0x1f << 16);
6289 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6296 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
6299 case elfcpp::R_POWERPC_ADDR32
:
6300 case elfcpp::R_POWERPC_UADDR32
:
6302 overflow
= Reloc::CHECK_BITFIELD
;
6305 case elfcpp::R_POWERPC_REL32
:
6307 overflow
= Reloc::CHECK_SIGNED
;
6310 case elfcpp::R_POWERPC_ADDR24
:
6311 case elfcpp::R_POWERPC_ADDR16
:
6312 case elfcpp::R_POWERPC_UADDR16
:
6313 case elfcpp::R_PPC64_ADDR16_DS
:
6314 case elfcpp::R_POWERPC_ADDR14
:
6315 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6316 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6317 overflow
= Reloc::CHECK_BITFIELD
;
6320 case elfcpp::R_POWERPC_REL24
:
6321 case elfcpp::R_PPC_PLTREL24
:
6322 case elfcpp::R_PPC_LOCAL24PC
:
6323 case elfcpp::R_POWERPC_REL16
:
6324 case elfcpp::R_PPC64_TOC16
:
6325 case elfcpp::R_POWERPC_GOT16
:
6326 case elfcpp::R_POWERPC_SECTOFF
:
6327 case elfcpp::R_POWERPC_TPREL16
:
6328 case elfcpp::R_POWERPC_DTPREL16
:
6329 case elfcpp::R_PPC64_TPREL16_DS
:
6330 case elfcpp::R_PPC64_DTPREL16_DS
:
6331 case elfcpp::R_PPC64_TOC16_DS
:
6332 case elfcpp::R_PPC64_GOT16_DS
:
6333 case elfcpp::R_PPC64_SECTOFF_DS
:
6334 case elfcpp::R_POWERPC_REL14
:
6335 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6336 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6337 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6338 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6339 case elfcpp::R_POWERPC_GOT_TPREL16
:
6340 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6341 overflow
= Reloc::CHECK_SIGNED
;
6345 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
6346 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
6349 case elfcpp::R_POWERPC_NONE
:
6350 case elfcpp::R_POWERPC_TLS
:
6351 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6352 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6353 case elfcpp::R_PPC_EMB_MRKREF
:
6356 case elfcpp::R_PPC64_ADDR64
:
6357 case elfcpp::R_PPC64_REL64
:
6358 case elfcpp::R_PPC64_TOC
:
6359 Reloc::addr64(view
, value
);
6362 case elfcpp::R_POWERPC_TPREL
:
6363 case elfcpp::R_POWERPC_DTPREL
:
6365 Reloc::addr64(view
, value
);
6367 status
= Reloc::addr32(view
, value
, overflow
);
6370 case elfcpp::R_PPC64_UADDR64
:
6371 Reloc::addr64_u(view
, value
);
6374 case elfcpp::R_POWERPC_ADDR32
:
6375 status
= Reloc::addr32(view
, value
, overflow
);
6378 case elfcpp::R_POWERPC_REL32
:
6379 case elfcpp::R_POWERPC_UADDR32
:
6380 status
= Reloc::addr32_u(view
, value
, overflow
);
6383 case elfcpp::R_POWERPC_ADDR24
:
6384 case elfcpp::R_POWERPC_REL24
:
6385 case elfcpp::R_PPC_PLTREL24
:
6386 case elfcpp::R_PPC_LOCAL24PC
:
6387 status
= Reloc::addr24(view
, value
, overflow
);
6390 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6391 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6394 status
= Reloc::addr16_ds(view
, value
, overflow
);
6397 case elfcpp::R_POWERPC_ADDR16
:
6398 case elfcpp::R_POWERPC_REL16
:
6399 case elfcpp::R_PPC64_TOC16
:
6400 case elfcpp::R_POWERPC_GOT16
:
6401 case elfcpp::R_POWERPC_SECTOFF
:
6402 case elfcpp::R_POWERPC_TPREL16
:
6403 case elfcpp::R_POWERPC_DTPREL16
:
6404 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6405 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6406 case elfcpp::R_POWERPC_GOT_TPREL16
:
6407 case elfcpp::R_POWERPC_ADDR16_LO
:
6408 case elfcpp::R_POWERPC_REL16_LO
:
6409 case elfcpp::R_PPC64_TOC16_LO
:
6410 case elfcpp::R_POWERPC_GOT16_LO
:
6411 case elfcpp::R_POWERPC_SECTOFF_LO
:
6412 case elfcpp::R_POWERPC_TPREL16_LO
:
6413 case elfcpp::R_POWERPC_DTPREL16_LO
:
6414 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6415 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6416 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6417 status
= Reloc::addr16(view
, value
, overflow
);
6420 case elfcpp::R_POWERPC_UADDR16
:
6421 status
= Reloc::addr16_u(view
, value
, overflow
);
6424 case elfcpp::R_POWERPC_ADDR16_HI
:
6425 case elfcpp::R_POWERPC_REL16_HI
:
6426 case elfcpp::R_PPC64_TOC16_HI
:
6427 case elfcpp::R_POWERPC_GOT16_HI
:
6428 case elfcpp::R_POWERPC_SECTOFF_HI
:
6429 case elfcpp::R_POWERPC_TPREL16_HI
:
6430 case elfcpp::R_POWERPC_DTPREL16_HI
:
6431 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6432 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6433 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6434 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6435 Reloc::addr16_hi(view
, value
);
6438 case elfcpp::R_POWERPC_ADDR16_HA
:
6439 case elfcpp::R_POWERPC_REL16_HA
:
6440 case elfcpp::R_PPC64_TOC16_HA
:
6441 case elfcpp::R_POWERPC_GOT16_HA
:
6442 case elfcpp::R_POWERPC_SECTOFF_HA
:
6443 case elfcpp::R_POWERPC_TPREL16_HA
:
6444 case elfcpp::R_POWERPC_DTPREL16_HA
:
6445 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6446 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6447 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6448 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6449 Reloc::addr16_ha(view
, value
);
6452 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6454 // R_PPC_EMB_NADDR16_LO
6456 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6457 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6458 Reloc::addr16_hi2(view
, value
);
6461 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6463 // R_PPC_EMB_NADDR16_HI
6465 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6466 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6467 Reloc::addr16_ha2(view
, value
);
6470 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6472 // R_PPC_EMB_NADDR16_HA
6474 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6475 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6476 Reloc::addr16_hi3(view
, value
);
6479 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6483 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6484 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6485 Reloc::addr16_ha3(view
, value
);
6488 case elfcpp::R_PPC64_DTPREL16_DS
:
6489 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6491 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
6493 case elfcpp::R_PPC64_TPREL16_DS
:
6494 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6496 // R_PPC_TLSGD, R_PPC_TLSLD
6498 case elfcpp::R_PPC64_ADDR16_DS
:
6499 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6500 case elfcpp::R_PPC64_TOC16_DS
:
6501 case elfcpp::R_PPC64_TOC16_LO_DS
:
6502 case elfcpp::R_PPC64_GOT16_DS
:
6503 case elfcpp::R_PPC64_GOT16_LO_DS
:
6504 case elfcpp::R_PPC64_SECTOFF_DS
:
6505 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6506 status
= Reloc::addr16_ds(view
, value
, overflow
);
6509 case elfcpp::R_POWERPC_ADDR14
:
6510 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6511 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6512 case elfcpp::R_POWERPC_REL14
:
6513 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6514 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6515 status
= Reloc::addr14(view
, value
, overflow
);
6518 case elfcpp::R_POWERPC_COPY
:
6519 case elfcpp::R_POWERPC_GLOB_DAT
:
6520 case elfcpp::R_POWERPC_JMP_SLOT
:
6521 case elfcpp::R_POWERPC_RELATIVE
:
6522 case elfcpp::R_POWERPC_DTPMOD
:
6523 case elfcpp::R_PPC64_JMP_IREL
:
6524 case elfcpp::R_POWERPC_IRELATIVE
:
6525 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6526 _("unexpected reloc %u in object file"),
6530 case elfcpp::R_PPC_EMB_SDA21
:
6535 // R_PPC64_TOCSAVE. For the time being this can be ignored.
6539 case elfcpp::R_PPC_EMB_SDA2I16
:
6540 case elfcpp::R_PPC_EMB_SDA2REL
:
6543 // R_PPC64_TLSGD, R_PPC64_TLSLD
6546 case elfcpp::R_POWERPC_PLT32
:
6547 case elfcpp::R_POWERPC_PLTREL32
:
6548 case elfcpp::R_POWERPC_PLT16_LO
:
6549 case elfcpp::R_POWERPC_PLT16_HI
:
6550 case elfcpp::R_POWERPC_PLT16_HA
:
6551 case elfcpp::R_PPC_SDAREL16
:
6552 case elfcpp::R_POWERPC_ADDR30
:
6553 case elfcpp::R_PPC64_PLT64
:
6554 case elfcpp::R_PPC64_PLTREL64
:
6555 case elfcpp::R_PPC64_PLTGOT16
:
6556 case elfcpp::R_PPC64_PLTGOT16_LO
:
6557 case elfcpp::R_PPC64_PLTGOT16_HI
:
6558 case elfcpp::R_PPC64_PLTGOT16_HA
:
6559 case elfcpp::R_PPC64_PLT16_LO_DS
:
6560 case elfcpp::R_PPC64_PLTGOT16_DS
:
6561 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
6562 case elfcpp::R_PPC_EMB_RELSEC16
:
6563 case elfcpp::R_PPC_EMB_RELST_LO
:
6564 case elfcpp::R_PPC_EMB_RELST_HI
:
6565 case elfcpp::R_PPC_EMB_RELST_HA
:
6566 case elfcpp::R_PPC_EMB_BIT_FLD
:
6567 case elfcpp::R_PPC_EMB_RELSDA
:
6568 case elfcpp::R_PPC_TOC16
:
6571 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6572 _("unsupported reloc %u"),
6576 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
)
6577 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6578 _("relocation overflow"));
6583 // Relocate section data.
6585 template<int size
, bool big_endian
>
6587 Target_powerpc
<size
, big_endian
>::relocate_section(
6588 const Relocate_info
<size
, big_endian
>* relinfo
,
6589 unsigned int sh_type
,
6590 const unsigned char* prelocs
,
6592 Output_section
* output_section
,
6593 bool needs_special_offset_handling
,
6594 unsigned char* view
,
6596 section_size_type view_size
,
6597 const Reloc_symbol_changes
* reloc_symbol_changes
)
6599 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
6600 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
6601 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
6602 Powerpc_comdat_behavior
;
6604 gold_assert(sh_type
== elfcpp::SHT_RELA
);
6606 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
6607 Powerpc_relocate
, Powerpc_comdat_behavior
>(
6613 needs_special_offset_handling
,
6617 reloc_symbol_changes
);
6620 class Powerpc_scan_relocatable_reloc
6623 // Return the strategy to use for a local symbol which is not a
6624 // section symbol, given the relocation type.
6625 inline Relocatable_relocs::Reloc_strategy
6626 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
6628 if (r_type
== 0 && r_sym
== 0)
6629 return Relocatable_relocs::RELOC_DISCARD
;
6630 return Relocatable_relocs::RELOC_COPY
;
6633 // Return the strategy to use for a local symbol which is a section
6634 // symbol, given the relocation type.
6635 inline Relocatable_relocs::Reloc_strategy
6636 local_section_strategy(unsigned int, Relobj
*)
6638 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
6641 // Return the strategy to use for a global symbol, given the
6642 // relocation type, the object, and the symbol index.
6643 inline Relocatable_relocs::Reloc_strategy
6644 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
6646 if (r_type
== elfcpp::R_PPC_PLTREL24
)
6647 return Relocatable_relocs::RELOC_SPECIAL
;
6648 return Relocatable_relocs::RELOC_COPY
;
6652 // Scan the relocs during a relocatable link.
6654 template<int size
, bool big_endian
>
6656 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
6657 Symbol_table
* symtab
,
6659 Sized_relobj_file
<size
, big_endian
>* object
,
6660 unsigned int data_shndx
,
6661 unsigned int sh_type
,
6662 const unsigned char* prelocs
,
6664 Output_section
* output_section
,
6665 bool needs_special_offset_handling
,
6666 size_t local_symbol_count
,
6667 const unsigned char* plocal_symbols
,
6668 Relocatable_relocs
* rr
)
6670 gold_assert(sh_type
== elfcpp::SHT_RELA
);
6672 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
6673 Powerpc_scan_relocatable_reloc
>(
6681 needs_special_offset_handling
,
6687 // Emit relocations for a section.
6688 // This is a modified version of the function by the same name in
6689 // target-reloc.h. Using relocate_special_relocatable for
6690 // R_PPC_PLTREL24 would require duplication of the entire body of the
6691 // loop, so we may as well duplicate the whole thing.
6693 template<int size
, bool big_endian
>
6695 Target_powerpc
<size
, big_endian
>::relocate_relocs(
6696 const Relocate_info
<size
, big_endian
>* relinfo
,
6697 unsigned int sh_type
,
6698 const unsigned char* prelocs
,
6700 Output_section
* output_section
,
6701 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
6702 const Relocatable_relocs
* rr
,
6704 Address view_address
,
6706 unsigned char* reloc_view
,
6707 section_size_type reloc_view_size
)
6709 gold_assert(sh_type
== elfcpp::SHT_RELA
);
6711 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
6713 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc_write
6715 const int reloc_size
6716 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
6718 Powerpc_relobj
<size
, big_endian
>* const object
6719 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
6720 const unsigned int local_count
= object
->local_symbol_count();
6721 unsigned int got2_shndx
= object
->got2_shndx();
6722 Address got2_addend
= 0;
6723 if (got2_shndx
!= 0)
6725 got2_addend
= object
->get_output_section_offset(got2_shndx
);
6726 gold_assert(got2_addend
!= invalid_address
);
6729 unsigned char* pwrite
= reloc_view
;
6730 bool zap_next
= false;
6731 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
6733 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
6734 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
6737 Reltype
reloc(prelocs
);
6738 Reltype_write
reloc_write(pwrite
);
6740 Address offset
= reloc
.get_r_offset();
6741 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
6742 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
6743 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
6744 const unsigned int orig_r_sym
= r_sym
;
6745 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
6746 = reloc
.get_r_addend();
6747 const Symbol
* gsym
= NULL
;
6751 // We could arrange to discard these and other relocs for
6752 // tls optimised sequences in the strategy methods, but for
6753 // now do as BFD ld does.
6754 r_type
= elfcpp::R_POWERPC_NONE
;
6758 // Get the new symbol index.
6759 if (r_sym
< local_count
)
6763 case Relocatable_relocs::RELOC_COPY
:
6764 case Relocatable_relocs::RELOC_SPECIAL
:
6767 r_sym
= object
->symtab_index(r_sym
);
6768 gold_assert(r_sym
!= -1U);
6772 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
6774 // We are adjusting a section symbol. We need to find
6775 // the symbol table index of the section symbol for
6776 // the output section corresponding to input section
6777 // in which this symbol is defined.
6778 gold_assert(r_sym
< local_count
);
6780 unsigned int shndx
=
6781 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
6782 gold_assert(is_ordinary
);
6783 Output_section
* os
= object
->output_section(shndx
);
6784 gold_assert(os
!= NULL
);
6785 gold_assert(os
->needs_symtab_index());
6786 r_sym
= os
->symtab_index();
6796 gsym
= object
->global_symbol(r_sym
);
6797 gold_assert(gsym
!= NULL
);
6798 if (gsym
->is_forwarder())
6799 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
6801 gold_assert(gsym
->has_symtab_index());
6802 r_sym
= gsym
->symtab_index();
6805 // Get the new offset--the location in the output section where
6806 // this relocation should be applied.
6807 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
6808 offset
+= offset_in_output_section
;
6811 section_offset_type sot_offset
=
6812 convert_types
<section_offset_type
, Address
>(offset
);
6813 section_offset_type new_sot_offset
=
6814 output_section
->output_offset(object
, relinfo
->data_shndx
,
6816 gold_assert(new_sot_offset
!= -1);
6817 offset
= new_sot_offset
;
6820 // In an object file, r_offset is an offset within the section.
6821 // In an executable or dynamic object, generated by
6822 // --emit-relocs, r_offset is an absolute address.
6823 if (!parameters
->options().relocatable())
6825 offset
+= view_address
;
6826 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
6827 offset
-= offset_in_output_section
;
6830 // Handle the reloc addend based on the strategy.
6831 if (strategy
== Relocatable_relocs::RELOC_COPY
)
6833 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
6835 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
6836 addend
= psymval
->value(object
, addend
);
6838 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
6840 if (addend
>= 32768)
6841 addend
+= got2_addend
;
6846 if (!parameters
->options().relocatable())
6848 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6849 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
6850 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
6851 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
6853 // First instruction of a global dynamic sequence,
6855 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6856 switch (this->optimize_tls_gd(final
))
6858 case tls::TLSOPT_TO_IE
:
6859 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
6860 - elfcpp::R_POWERPC_GOT_TLSGD16
);
6862 case tls::TLSOPT_TO_LE
:
6863 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6864 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
6865 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6868 r_type
= elfcpp::R_POWERPC_NONE
;
6869 offset
-= 2 * big_endian
;
6876 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
6877 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
6878 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
6879 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
6881 // First instruction of a local dynamic sequence,
6883 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
6885 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
6886 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
6888 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6889 const Output_section
* os
= relinfo
->layout
->tls_segment()
6891 gold_assert(os
!= NULL
);
6892 gold_assert(os
->needs_symtab_index());
6893 r_sym
= os
->symtab_index();
6894 addend
= dtp_offset
;
6898 r_type
= elfcpp::R_POWERPC_NONE
;
6899 offset
-= 2 * big_endian
;
6903 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
6904 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
6905 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
6906 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
6908 // First instruction of initial exec sequence.
6909 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6910 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
6912 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
6913 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
6914 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6917 r_type
= elfcpp::R_POWERPC_NONE
;
6918 offset
-= 2 * big_endian
;
6922 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6923 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6925 // Second instruction of a global dynamic sequence,
6926 // the __tls_get_addr call
6927 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6928 switch (this->optimize_tls_gd(final
))
6930 case tls::TLSOPT_TO_IE
:
6931 r_type
= elfcpp::R_POWERPC_NONE
;
6934 case tls::TLSOPT_TO_LE
:
6935 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6936 offset
+= 2 * big_endian
;
6943 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6944 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6946 // Second instruction of a local dynamic sequence,
6947 // the __tls_get_addr call
6948 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
6950 const Output_section
* os
= relinfo
->layout
->tls_segment()
6952 gold_assert(os
!= NULL
);
6953 gold_assert(os
->needs_symtab_index());
6954 r_sym
= os
->symtab_index();
6955 addend
= dtp_offset
;
6956 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6957 offset
+= 2 * big_endian
;
6961 else if (r_type
== elfcpp::R_POWERPC_TLS
)
6963 // Second instruction of an initial exec sequence
6964 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6965 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
6967 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6968 offset
+= 2 * big_endian
;
6973 reloc_write
.put_r_offset(offset
);
6974 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
6975 reloc_write
.put_r_addend(addend
);
6977 pwrite
+= reloc_size
;
6980 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
6981 == reloc_view_size
);
6984 // Return the value to use for a dynamic symbol which requires special
6985 // treatment. This is how we support equality comparisons of function
6986 // pointers across shared library boundaries, as described in the
6987 // processor specific ABI supplement.
6989 template<int size
, bool big_endian
>
6991 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
6995 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
6996 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
6997 p
!= this->stub_tables_
.end();
7000 Address off
= (*p
)->find_plt_call_entry(gsym
);
7001 if (off
!= invalid_address
)
7002 return (*p
)->stub_address() + off
;
7008 // Return the PLT address to use for a local symbol.
7009 template<int size
, bool big_endian
>
7011 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
7012 const Relobj
* object
,
7013 unsigned int symndx
) const
7017 const Sized_relobj
<size
, big_endian
>* relobj
7018 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
7019 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7020 p
!= this->stub_tables_
.end();
7023 Address off
= (*p
)->find_plt_call_entry(relobj
->sized_relobj(),
7025 if (off
!= invalid_address
)
7026 return (*p
)->stub_address() + off
;
7032 // Return the PLT address to use for a global symbol.
7033 template<int size
, bool big_endian
>
7035 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
7036 const Symbol
* gsym
) const
7040 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7041 p
!= this->stub_tables_
.end();
7044 Address off
= (*p
)->find_plt_call_entry(gsym
);
7045 if (off
!= invalid_address
)
7046 return (*p
)->stub_address() + off
;
7052 // Return the offset to use for the GOT_INDX'th got entry which is
7053 // for a local tls symbol specified by OBJECT, SYMNDX.
7054 template<int size
, bool big_endian
>
7056 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
7057 const Relobj
* object
,
7058 unsigned int symndx
,
7059 unsigned int got_indx
) const
7061 const Powerpc_relobj
<size
, big_endian
>* ppc_object
7062 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
7063 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
7065 for (Got_type got_type
= GOT_TYPE_TLSGD
;
7066 got_type
<= GOT_TYPE_TPREL
;
7067 got_type
= Got_type(got_type
+ 1))
7068 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
7070 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
7071 if (got_type
== GOT_TYPE_TLSGD
)
7073 if (off
== got_indx
* (size
/ 8))
7075 if (got_type
== GOT_TYPE_TPREL
)
7085 // Return the offset to use for the GOT_INDX'th got entry which is
7086 // for global tls symbol GSYM.
7087 template<int size
, bool big_endian
>
7089 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
7091 unsigned int got_indx
) const
7093 if (gsym
->type() == elfcpp::STT_TLS
)
7095 for (Got_type got_type
= GOT_TYPE_TLSGD
;
7096 got_type
<= GOT_TYPE_TPREL
;
7097 got_type
= Got_type(got_type
+ 1))
7098 if (gsym
->has_got_offset(got_type
))
7100 unsigned int off
= gsym
->got_offset(got_type
);
7101 if (got_type
== GOT_TYPE_TLSGD
)
7103 if (off
== got_indx
* (size
/ 8))
7105 if (got_type
== GOT_TYPE_TPREL
)
7115 // The selector for powerpc object files.
7117 template<int size
, bool big_endian
>
7118 class Target_selector_powerpc
: public Target_selector
7121 Target_selector_powerpc()
7122 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
7125 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
7126 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
7128 ? (big_endian
? "elf64ppc" : "elf64lppc")
7129 : (big_endian
? "elf32ppc" : "elf32lppc")))
7133 do_instantiate_target()
7134 { return new Target_powerpc
<size
, big_endian
>(); }
7137 Target_selector_powerpc
<32, true> target_selector_ppc32
;
7138 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
7139 Target_selector_powerpc
<64, true> target_selector_ppc64
;
7140 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
7142 // Instantiate these constants for -O0
7143 template<int size
, bool big_endian
>
7144 const int Output_data_glink
<size
, big_endian
>::pltresolve_size
;
7145 template<int size
, bool big_endian
>
7146 const typename Stub_table
<size
, big_endian
>::Address
7147 Stub_table
<size
, big_endian
>::invalid_address
;
7148 template<int size
, bool big_endian
>
7149 const typename Target_powerpc
<size
, big_endian
>::Address
7150 Target_powerpc
<size
, big_endian
>::invalid_address
;
7152 } // End anonymous namespace.