1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright (C) 2008-2017 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.
30 #include "parameters.h"
37 #include "copy-relocs.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
50 template<int size
, bool big_endian
>
51 class Output_data_plt_powerpc
;
53 template<int size
, bool big_endian
>
54 class Output_data_brlt_powerpc
;
56 template<int size
, bool big_endian
>
57 class Output_data_got_powerpc
;
59 template<int size
, bool big_endian
>
60 class Output_data_glink
;
62 template<int size
, bool big_endian
>
65 template<int size
, bool big_endian
>
66 class Output_data_save_res
;
68 template<int size
, bool big_endian
>
71 struct Stub_table_owner
74 : output_section(NULL
), owner(NULL
)
77 Output_section
* output_section
;
78 const Output_section::Input_section
* owner
;
82 is_branch_reloc(unsigned int r_type
);
84 // Counter incremented on every Powerpc_relobj constructed.
85 static uint32_t object_id
= 0;
87 template<int size
, bool big_endian
>
88 class Powerpc_relobj
: public Sized_relobj_file
<size
, big_endian
>
91 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
92 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_refs
;
93 typedef Unordered_map
<Address
, Section_refs
> Access_from
;
95 Powerpc_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
96 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
97 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
98 uniq_(object_id
++), special_(0), relatoc_(0), toc_(0),
99 has_small_toc_reloc_(false), opd_valid_(false),
100 e_flags_(ehdr
.get_e_flags()), no_toc_opt_(), opd_ent_(),
101 access_from_map_(), has14_(), stub_table_index_(), st_other_()
103 this->set_abiversion(0);
109 // Read the symbols then set up st_other vector.
111 do_read_symbols(Read_symbols_data
*);
113 // Arrange to always relocate .toc first.
115 do_relocate_sections(
116 const Symbol_table
* symtab
, const Layout
* layout
,
117 const unsigned char* pshdrs
, Output_file
* of
,
118 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
);
120 // The .toc section index.
127 // Mark .toc entry at OFF as not optimizable.
129 set_no_toc_opt(Address off
)
131 if (this->no_toc_opt_
.empty())
132 this->no_toc_opt_
.resize(this->section_size(this->toc_shndx())
135 if (off
< this->no_toc_opt_
.size())
136 this->no_toc_opt_
[off
] = true;
139 // Mark the entire .toc as not optimizable.
143 this->no_toc_opt_
.resize(1);
144 this->no_toc_opt_
[0] = true;
147 // Return true if code using the .toc entry at OFF should not be edited.
149 no_toc_opt(Address off
) const
151 if (this->no_toc_opt_
.empty())
154 if (off
>= this->no_toc_opt_
.size())
156 return this->no_toc_opt_
[off
];
159 // The .got2 section shndx.
164 return this->special_
;
169 // The .opd section shndx.
176 return this->special_
;
179 // Init OPD entry arrays.
181 init_opd(size_t opd_size
)
183 size_t count
= this->opd_ent_ndx(opd_size
);
184 this->opd_ent_
.resize(count
);
187 // Return section and offset of function entry for .opd + R_OFF.
189 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
191 size_t ndx
= this->opd_ent_ndx(r_off
);
192 gold_assert(ndx
< this->opd_ent_
.size());
193 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
195 *value
= this->opd_ent_
[ndx
].off
;
196 return this->opd_ent_
[ndx
].shndx
;
199 // Set section and offset of function entry for .opd + R_OFF.
201 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
203 size_t ndx
= this->opd_ent_ndx(r_off
);
204 gold_assert(ndx
< this->opd_ent_
.size());
205 this->opd_ent_
[ndx
].shndx
= shndx
;
206 this->opd_ent_
[ndx
].off
= value
;
209 // Return discard flag for .opd + R_OFF.
211 get_opd_discard(Address r_off
) const
213 size_t ndx
= this->opd_ent_ndx(r_off
);
214 gold_assert(ndx
< this->opd_ent_
.size());
215 return this->opd_ent_
[ndx
].discard
;
218 // Set discard flag for .opd + R_OFF.
220 set_opd_discard(Address r_off
)
222 size_t ndx
= this->opd_ent_ndx(r_off
);
223 gold_assert(ndx
< this->opd_ent_
.size());
224 this->opd_ent_
[ndx
].discard
= true;
229 { return this->opd_valid_
; }
233 { this->opd_valid_
= true; }
235 // Examine .rela.opd to build info about function entry points.
237 scan_opd_relocs(size_t reloc_count
,
238 const unsigned char* prelocs
,
239 const unsigned char* plocal_syms
);
241 // Returns true if a code sequence loading a TOC entry can be
242 // converted into code calculating a TOC pointer relative offset.
244 make_toc_relative(Target_powerpc
<size
, big_endian
>* target
,
247 // Perform the Sized_relobj_file method, then set up opd info from
250 do_read_relocs(Read_relocs_data
*);
253 do_find_special_sections(Read_symbols_data
* sd
);
255 // Adjust this local symbol value. Return false if the symbol
256 // should be discarded from the output file.
258 do_adjust_local_symbol(Symbol_value
<size
>* lv
) const
260 if (size
== 64 && this->opd_shndx() != 0)
263 if (lv
->input_shndx(&is_ordinary
) != this->opd_shndx())
265 if (this->get_opd_discard(lv
->input_value()))
273 { return &this->access_from_map_
; }
275 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
276 // section at DST_OFF.
278 add_reference(Relobj
* src_obj
,
279 unsigned int src_indx
,
280 typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
282 Section_id
src_id(src_obj
, src_indx
);
283 this->access_from_map_
[dst_off
].insert(src_id
);
286 // Add a reference to the code section specified by the .opd entry
289 add_gc_mark(typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
291 size_t ndx
= this->opd_ent_ndx(dst_off
);
292 if (ndx
>= this->opd_ent_
.size())
293 this->opd_ent_
.resize(ndx
+ 1);
294 this->opd_ent_
[ndx
].gc_mark
= true;
298 process_gc_mark(Symbol_table
* symtab
)
300 for (size_t i
= 0; i
< this->opd_ent_
.size(); i
++)
301 if (this->opd_ent_
[i
].gc_mark
)
303 unsigned int shndx
= this->opd_ent_
[i
].shndx
;
304 symtab
->gc()->worklist().push_back(Section_id(this, shndx
));
308 // Return offset in output GOT section that this object will use
309 // as a TOC pointer. Won't be just a constant with multi-toc support.
311 toc_base_offset() const
315 set_has_small_toc_reloc()
316 { has_small_toc_reloc_
= true; }
319 has_small_toc_reloc() const
320 { return has_small_toc_reloc_
; }
323 set_has_14bit_branch(unsigned int shndx
)
325 if (shndx
>= this->has14_
.size())
326 this->has14_
.resize(shndx
+ 1);
327 this->has14_
[shndx
] = true;
331 has_14bit_branch(unsigned int shndx
) const
332 { return shndx
< this->has14_
.size() && this->has14_
[shndx
]; }
335 set_stub_table(unsigned int shndx
, unsigned int stub_index
)
337 if (shndx
>= this->stub_table_index_
.size())
338 this->stub_table_index_
.resize(shndx
+ 1, -1);
339 this->stub_table_index_
[shndx
] = stub_index
;
342 Stub_table
<size
, big_endian
>*
343 stub_table(unsigned int shndx
)
345 if (shndx
< this->stub_table_index_
.size())
347 Target_powerpc
<size
, big_endian
>* target
348 = static_cast<Target_powerpc
<size
, big_endian
>*>(
349 parameters
->sized_target
<size
, big_endian
>());
350 unsigned int indx
= this->stub_table_index_
[shndx
];
351 if (indx
< target
->stub_tables().size())
352 return target
->stub_tables()[indx
];
360 this->stub_table_index_
.clear();
365 { return this->uniq_
; }
369 { return this->e_flags_
& elfcpp::EF_PPC64_ABI
; }
371 // Set ABI version for input and output
373 set_abiversion(int ver
);
376 ppc64_local_entry_offset(const Symbol
* sym
) const
377 { return elfcpp::ppc64_decode_local_entry(sym
->nonvis() >> 3); }
380 ppc64_local_entry_offset(unsigned int symndx
) const
381 { return elfcpp::ppc64_decode_local_entry(this->st_other_
[symndx
] >> 5); }
392 // Return index into opd_ent_ array for .opd entry at OFF.
393 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
394 // apart when the language doesn't use the last 8-byte word, the
395 // environment pointer. Thus dividing the entry section offset by
396 // 16 will give an index into opd_ent_ that works for either layout
397 // of .opd. (It leaves some elements of the vector unused when .opd
398 // entries are spaced 24 bytes apart, but we don't know the spacing
399 // until relocations are processed, and in any case it is possible
400 // for an object to have some entries spaced 16 bytes apart and
401 // others 24 bytes apart.)
403 opd_ent_ndx(size_t off
) const
406 // Per object unique identifier
409 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
410 unsigned int special_
;
412 // For 64-bit the .rela.toc and .toc section shdnx.
413 unsigned int relatoc_
;
416 // For 64-bit, whether this object uses small model relocs to access
418 bool has_small_toc_reloc_
;
420 // Set at the start of gc_process_relocs, when we know opd_ent_
421 // vector is valid. The flag could be made atomic and set in
422 // do_read_relocs with memory_order_release and then tested with
423 // memory_order_acquire, potentially resulting in fewer entries in
428 elfcpp::Elf_Word e_flags_
;
430 // For 64-bit, an array with one entry per 64-bit word in the .toc
431 // section, set if accesses using that word cannot be optimised.
432 std::vector
<bool> no_toc_opt_
;
434 // The first 8-byte word of an OPD entry gives the address of the
435 // entry point of the function. Relocatable object files have a
436 // relocation on this word. The following vector records the
437 // section and offset specified by these relocations.
438 std::vector
<Opd_ent
> opd_ent_
;
440 // References made to this object's .opd section when running
441 // gc_process_relocs for another object, before the opd_ent_ vector
442 // is valid for this object.
443 Access_from access_from_map_
;
445 // Whether input section has a 14-bit branch reloc.
446 std::vector
<bool> has14_
;
448 // The stub table to use for a given input section.
449 std::vector
<unsigned int> stub_table_index_
;
451 // ELF st_other field for local symbols.
452 std::vector
<unsigned char> st_other_
;
455 template<int size
, bool big_endian
>
456 class Powerpc_dynobj
: public Sized_dynobj
<size
, big_endian
>
459 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
461 Powerpc_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
462 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
463 : Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
464 opd_shndx_(0), e_flags_(ehdr
.get_e_flags()), opd_ent_()
466 this->set_abiversion(0);
472 // Call Sized_dynobj::do_read_symbols to read the symbols then
473 // read .opd from a dynamic object, filling in opd_ent_ vector,
475 do_read_symbols(Read_symbols_data
*);
477 // The .opd section shndx.
481 return this->opd_shndx_
;
484 // The .opd section address.
488 return this->opd_address_
;
491 // Init OPD entry arrays.
493 init_opd(size_t opd_size
)
495 size_t count
= this->opd_ent_ndx(opd_size
);
496 this->opd_ent_
.resize(count
);
499 // Return section and offset of function entry for .opd + R_OFF.
501 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
503 size_t ndx
= this->opd_ent_ndx(r_off
);
504 gold_assert(ndx
< this->opd_ent_
.size());
505 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
507 *value
= this->opd_ent_
[ndx
].off
;
508 return this->opd_ent_
[ndx
].shndx
;
511 // Set section and offset of function entry for .opd + R_OFF.
513 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
515 size_t ndx
= this->opd_ent_ndx(r_off
);
516 gold_assert(ndx
< this->opd_ent_
.size());
517 this->opd_ent_
[ndx
].shndx
= shndx
;
518 this->opd_ent_
[ndx
].off
= value
;
523 { return this->e_flags_
& elfcpp::EF_PPC64_ABI
; }
525 // Set ABI version for input and output.
527 set_abiversion(int ver
);
530 // Used to specify extent of executable sections.
533 Sec_info(Address start_
, Address len_
, unsigned int shndx_
)
534 : start(start_
), len(len_
), shndx(shndx_
)
538 operator<(const Sec_info
& that
) const
539 { return this->start
< that
.start
; }
552 // Return index into opd_ent_ array for .opd entry at OFF.
554 opd_ent_ndx(size_t off
) const
557 // For 64-bit the .opd section shndx and address.
558 unsigned int opd_shndx_
;
559 Address opd_address_
;
562 elfcpp::Elf_Word e_flags_
;
564 // The first 8-byte word of an OPD entry gives the address of the
565 // entry point of the function. Records the section and offset
566 // corresponding to the address. Note that in dynamic objects,
567 // offset is *not* relative to the section.
568 std::vector
<Opd_ent
> opd_ent_
;
571 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
572 // base class will emit.
574 template<int sh_type
, int size
, bool big_endian
>
575 class Powerpc_copy_relocs
: public Copy_relocs
<sh_type
, size
, big_endian
>
578 Powerpc_copy_relocs()
579 : Copy_relocs
<sh_type
, size
, big_endian
>(elfcpp::R_POWERPC_COPY
)
582 // Emit any saved relocations which turn out to be needed. This is
583 // called after all the relocs have been scanned.
585 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
588 template<int size
, bool big_endian
>
589 class Target_powerpc
: public Sized_target
<size
, big_endian
>
593 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
594 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
595 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Signed_address
;
596 static const Address invalid_address
= static_cast<Address
>(0) - 1;
597 // Offset of tp and dtp pointers from start of TLS block.
598 static const Address tp_offset
= 0x7000;
599 static const Address dtp_offset
= 0x8000;
602 : Sized_target
<size
, big_endian
>(&powerpc_info
),
603 got_(NULL
), plt_(NULL
), iplt_(NULL
), brlt_section_(NULL
),
604 glink_(NULL
), rela_dyn_(NULL
), copy_relocs_(),
605 tlsld_got_offset_(-1U),
606 stub_tables_(), branch_lookup_table_(), branch_info_(),
607 plt_thread_safe_(false), relax_failed_(false), relax_fail_count_(0),
608 stub_group_size_(0), savres_section_(0)
612 // Process the relocations to determine unreferenced sections for
613 // garbage collection.
615 gc_process_relocs(Symbol_table
* symtab
,
617 Sized_relobj_file
<size
, big_endian
>* object
,
618 unsigned int data_shndx
,
619 unsigned int sh_type
,
620 const unsigned char* prelocs
,
622 Output_section
* output_section
,
623 bool needs_special_offset_handling
,
624 size_t local_symbol_count
,
625 const unsigned char* plocal_symbols
);
627 // Scan the relocations to look for symbol adjustments.
629 scan_relocs(Symbol_table
* symtab
,
631 Sized_relobj_file
<size
, big_endian
>* object
,
632 unsigned int data_shndx
,
633 unsigned int sh_type
,
634 const unsigned char* prelocs
,
636 Output_section
* output_section
,
637 bool needs_special_offset_handling
,
638 size_t local_symbol_count
,
639 const unsigned char* plocal_symbols
);
641 // Map input .toc section to output .got section.
643 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
645 if (size
== 64 && strcmp(name
, ".toc") == 0)
653 // Provide linker defined save/restore functions.
655 define_save_restore_funcs(Layout
*, Symbol_table
*);
657 // No stubs unless a final link.
660 { return !parameters
->options().relocatable(); }
663 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
666 do_plt_fde_location(const Output_data
*, unsigned char*,
667 uint64_t*, off_t
*) const;
669 // Stash info about branches, for stub generation.
671 push_branch(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
672 unsigned int data_shndx
, Address r_offset
,
673 unsigned int r_type
, unsigned int r_sym
, Address addend
)
675 Branch_info
info(ppc_object
, data_shndx
, r_offset
, r_type
, r_sym
, addend
);
676 this->branch_info_
.push_back(info
);
677 if (r_type
== elfcpp::R_POWERPC_REL14
678 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
679 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
680 ppc_object
->set_has_14bit_branch(data_shndx
);
684 do_define_standard_symbols(Symbol_table
*, Layout
*);
686 // Finalize the sections.
688 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
690 // Return the value to use for a dynamic which requires special
693 do_dynsym_value(const Symbol
*) const;
695 // Return the PLT address to use for a local symbol.
697 do_plt_address_for_local(const Relobj
*, unsigned int) const;
699 // Return the PLT address to use for a global symbol.
701 do_plt_address_for_global(const Symbol
*) const;
703 // Return the offset to use for the GOT_INDX'th got entry which is
704 // for a local tls symbol specified by OBJECT, SYMNDX.
706 do_tls_offset_for_local(const Relobj
* object
,
708 unsigned int got_indx
) const;
710 // Return the offset to use for the GOT_INDX'th got entry which is
711 // for global tls symbol GSYM.
713 do_tls_offset_for_global(Symbol
* gsym
, unsigned int got_indx
) const;
716 do_function_location(Symbol_location
*) const;
719 do_can_check_for_function_pointers() const
722 // Adjust -fsplit-stack code which calls non-split-stack code.
724 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
725 section_offset_type fnoffset
, section_size_type fnsize
,
726 const unsigned char* prelocs
, size_t reloc_count
,
727 unsigned char* view
, section_size_type view_size
,
728 std::string
* from
, std::string
* to
) const;
730 // Relocate a section.
732 relocate_section(const Relocate_info
<size
, big_endian
>*,
733 unsigned int sh_type
,
734 const unsigned char* prelocs
,
736 Output_section
* output_section
,
737 bool needs_special_offset_handling
,
739 Address view_address
,
740 section_size_type view_size
,
741 const Reloc_symbol_changes
*);
743 // Scan the relocs during a relocatable link.
745 scan_relocatable_relocs(Symbol_table
* symtab
,
747 Sized_relobj_file
<size
, big_endian
>* object
,
748 unsigned int data_shndx
,
749 unsigned int sh_type
,
750 const unsigned char* prelocs
,
752 Output_section
* output_section
,
753 bool needs_special_offset_handling
,
754 size_t local_symbol_count
,
755 const unsigned char* plocal_symbols
,
756 Relocatable_relocs
*);
758 // Scan the relocs for --emit-relocs.
760 emit_relocs_scan(Symbol_table
* symtab
,
762 Sized_relobj_file
<size
, big_endian
>* object
,
763 unsigned int data_shndx
,
764 unsigned int sh_type
,
765 const unsigned char* prelocs
,
767 Output_section
* output_section
,
768 bool needs_special_offset_handling
,
769 size_t local_symbol_count
,
770 const unsigned char* plocal_syms
,
771 Relocatable_relocs
* rr
);
773 // Emit relocations for a section.
775 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
776 unsigned int sh_type
,
777 const unsigned char* prelocs
,
779 Output_section
* output_section
,
780 typename
elfcpp::Elf_types
<size
>::Elf_Off
781 offset_in_output_section
,
783 Address view_address
,
785 unsigned char* reloc_view
,
786 section_size_type reloc_view_size
);
788 // Return whether SYM is defined by the ABI.
790 do_is_defined_by_abi(const Symbol
* sym
) const
792 return strcmp(sym
->name(), "__tls_get_addr") == 0;
795 // Return the size of the GOT section.
799 gold_assert(this->got_
!= NULL
);
800 return this->got_
->data_size();
803 // Get the PLT section.
804 const Output_data_plt_powerpc
<size
, big_endian
>*
807 gold_assert(this->plt_
!= NULL
);
811 // Get the IPLT section.
812 const Output_data_plt_powerpc
<size
, big_endian
>*
815 gold_assert(this->iplt_
!= NULL
);
819 // Get the .glink section.
820 const Output_data_glink
<size
, big_endian
>*
821 glink_section() const
823 gold_assert(this->glink_
!= NULL
);
827 Output_data_glink
<size
, big_endian
>*
830 gold_assert(this->glink_
!= NULL
);
834 bool has_glink() const
835 { return this->glink_
!= NULL
; }
837 // Get the GOT section.
838 const Output_data_got_powerpc
<size
, big_endian
>*
841 gold_assert(this->got_
!= NULL
);
845 // Get the GOT section, creating it if necessary.
846 Output_data_got_powerpc
<size
, big_endian
>*
847 got_section(Symbol_table
*, Layout
*);
850 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
851 const elfcpp::Ehdr
<size
, big_endian
>&);
853 // Return the number of entries in the GOT.
855 got_entry_count() const
857 if (this->got_
== NULL
)
859 return this->got_size() / (size
/ 8);
862 // Return the number of entries in the PLT.
864 plt_entry_count() const;
866 // Return the offset of the first non-reserved PLT entry.
868 first_plt_entry_offset() const
872 if (this->abiversion() >= 2)
877 // Return the size of each PLT entry.
879 plt_entry_size() const
883 if (this->abiversion() >= 2)
888 Output_data_save_res
<size
, big_endian
>*
889 savres_section() const
891 return this->savres_section_
;
894 // Add any special sections for this symbol to the gc work list.
895 // For powerpc64, this adds the code section of a function
898 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
900 // Handle target specific gc actions when adding a gc reference from
901 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
902 // and DST_OFF. For powerpc64, this adds a referenc to the code
903 // section of a function descriptor.
905 do_gc_add_reference(Symbol_table
* symtab
,
907 unsigned int src_shndx
,
909 unsigned int dst_shndx
,
910 Address dst_off
) const;
912 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
915 { return this->stub_tables_
; }
917 const Output_data_brlt_powerpc
<size
, big_endian
>*
919 { return this->brlt_section_
; }
922 add_branch_lookup_table(Address to
)
924 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
925 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
929 find_branch_lookup_table(Address to
)
931 typename
Branch_lookup_table::const_iterator p
932 = this->branch_lookup_table_
.find(to
);
933 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
937 write_branch_lookup_table(unsigned char *oview
)
939 for (typename
Branch_lookup_table::const_iterator p
940 = this->branch_lookup_table_
.begin();
941 p
!= this->branch_lookup_table_
.end();
944 elfcpp::Swap
<size
, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
948 // Wrapper used after relax to define a local symbol in output data,
949 // from the end if value < 0.
951 define_local(Symbol_table
* symtab
, const char* name
,
952 Output_data
* od
, Address value
, unsigned int symsize
)
955 = symtab
->define_in_output_data(name
, NULL
, Symbol_table::PREDEFINED
,
956 od
, value
, symsize
, elfcpp::STT_NOTYPE
,
957 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
, 0,
958 static_cast<Signed_address
>(value
) < 0,
960 // We are creating this symbol late, so need to fix up things
961 // done early in Layout::finalize.
962 sym
->set_dynsym_index(-1U);
966 plt_thread_safe() const
967 { return this->plt_thread_safe_
; }
971 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI
; }
974 set_abiversion (int ver
)
976 elfcpp::Elf_Word flags
= this->processor_specific_flags();
977 flags
&= ~elfcpp::EF_PPC64_ABI
;
978 flags
|= ver
& elfcpp::EF_PPC64_ABI
;
979 this->set_processor_specific_flags(flags
);
982 // Offset to to save stack slot
985 { return this->abiversion() < 2 ? 40 : 24; }
1001 : tls_get_addr_(NOT_EXPECTED
),
1002 relinfo_(NULL
), relnum_(0), r_offset_(0)
1007 if (this->tls_get_addr_
!= NOT_EXPECTED
)
1014 if (this->relinfo_
!= NULL
)
1015 gold_error_at_location(this->relinfo_
, this->relnum_
, this->r_offset_
,
1016 _("missing expected __tls_get_addr call"));
1020 expect_tls_get_addr_call(
1021 const Relocate_info
<size
, big_endian
>* relinfo
,
1025 this->tls_get_addr_
= EXPECTED
;
1026 this->relinfo_
= relinfo
;
1027 this->relnum_
= relnum
;
1028 this->r_offset_
= r_offset
;
1032 expect_tls_get_addr_call()
1033 { this->tls_get_addr_
= EXPECTED
; }
1036 skip_next_tls_get_addr_call()
1037 {this->tls_get_addr_
= SKIP
; }
1040 maybe_skip_tls_get_addr_call(unsigned int r_type
, const Symbol
* gsym
)
1042 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
1043 || r_type
== elfcpp::R_PPC_PLTREL24
)
1045 && strcmp(gsym
->name(), "__tls_get_addr") == 0);
1046 Tls_get_addr last_tls
= this->tls_get_addr_
;
1047 this->tls_get_addr_
= NOT_EXPECTED
;
1048 if (is_tls_call
&& last_tls
!= EXPECTED
)
1050 else if (!is_tls_call
&& last_tls
!= NOT_EXPECTED
)
1059 // What we're up to regarding calls to __tls_get_addr.
1060 // On powerpc, the branch and link insn making a call to
1061 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1062 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1063 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
1064 // The marker relocation always comes first, and has the same
1065 // symbol as the reloc on the insn setting up the __tls_get_addr
1066 // argument. This ties the arg setup insn with the call insn,
1067 // allowing ld to safely optimize away the call. We check that
1068 // every call to __tls_get_addr has a marker relocation, and that
1069 // every marker relocation is on a call to __tls_get_addr.
1070 Tls_get_addr tls_get_addr_
;
1071 // Info about the last reloc for error message.
1072 const Relocate_info
<size
, big_endian
>* relinfo_
;
1077 // The class which scans relocations.
1078 class Scan
: protected Track_tls
1081 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1084 : Track_tls(), issued_non_pic_error_(false)
1088 get_reference_flags(unsigned int r_type
, const Target_powerpc
* target
);
1091 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
1092 Sized_relobj_file
<size
, big_endian
>* object
,
1093 unsigned int data_shndx
,
1094 Output_section
* output_section
,
1095 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
1096 const elfcpp::Sym
<size
, big_endian
>& lsym
,
1100 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
1101 Sized_relobj_file
<size
, big_endian
>* object
,
1102 unsigned int data_shndx
,
1103 Output_section
* output_section
,
1104 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
1108 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1110 Sized_relobj_file
<size
, big_endian
>* relobj
,
1113 const elfcpp::Rela
<size
, big_endian
>& ,
1114 unsigned int r_type
,
1115 const elfcpp::Sym
<size
, big_endian
>&)
1117 // PowerPC64 .opd is not folded, so any identical function text
1118 // may be folded and we'll still keep function addresses distinct.
1119 // That means no reloc is of concern here.
1122 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1123 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1124 if (ppcobj
->abiversion() == 1)
1127 // For 32-bit and ELFv2, conservatively assume anything but calls to
1128 // function code might be taking the address of the function.
1129 return !is_branch_reloc(r_type
);
1133 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1135 Sized_relobj_file
<size
, big_endian
>* relobj
,
1138 const elfcpp::Rela
<size
, big_endian
>& ,
1139 unsigned int r_type
,
1145 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1146 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1147 if (ppcobj
->abiversion() == 1)
1150 return !is_branch_reloc(r_type
);
1154 reloc_needs_plt_for_ifunc(Target_powerpc
<size
, big_endian
>* target
,
1155 Sized_relobj_file
<size
, big_endian
>* object
,
1156 unsigned int r_type
, bool report_err
);
1160 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
1161 unsigned int r_type
);
1164 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
1165 unsigned int r_type
, Symbol
*);
1168 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
1169 Target_powerpc
* target
);
1172 check_non_pic(Relobj
*, unsigned int r_type
);
1174 // Whether we have issued an error about a non-PIC compilation.
1175 bool issued_non_pic_error_
;
1179 symval_for_branch(const Symbol_table
* symtab
,
1180 const Sized_symbol
<size
>* gsym
,
1181 Powerpc_relobj
<size
, big_endian
>* object
,
1182 Address
*value
, unsigned int *dest_shndx
);
1184 // The class which implements relocation.
1185 class Relocate
: protected Track_tls
1188 // Use 'at' branch hints when true, 'y' when false.
1189 // FIXME maybe: set this with an option.
1190 static const bool is_isa_v2
= true;
1196 // Do a relocation. Return false if the caller should not issue
1197 // any warnings about this relocation.
1199 relocate(const Relocate_info
<size
, big_endian
>*, unsigned int,
1200 Target_powerpc
*, Output_section
*, size_t, const unsigned char*,
1201 const Sized_symbol
<size
>*, const Symbol_value
<size
>*,
1202 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1206 class Relocate_comdat_behavior
1209 // Decide what the linker should do for relocations that refer to
1210 // discarded comdat sections.
1211 inline Comdat_behavior
1212 get(const char* name
)
1214 gold::Default_comdat_behavior default_behavior
;
1215 Comdat_behavior ret
= default_behavior
.get(name
);
1216 if (ret
== CB_WARNING
)
1219 && (strcmp(name
, ".fixup") == 0
1220 || strcmp(name
, ".got2") == 0))
1223 && (strcmp(name
, ".opd") == 0
1224 || strcmp(name
, ".toc") == 0
1225 || strcmp(name
, ".toc1") == 0))
1232 // Optimize the TLS relocation type based on what we know about the
1233 // symbol. IS_FINAL is true if the final address of this symbol is
1234 // known at link time.
1236 tls::Tls_optimization
1237 optimize_tls_gd(bool is_final
)
1239 // If we are generating a shared library, then we can't do anything
1241 if (parameters
->options().shared())
1242 return tls::TLSOPT_NONE
;
1245 return tls::TLSOPT_TO_IE
;
1246 return tls::TLSOPT_TO_LE
;
1249 tls::Tls_optimization
1252 if (parameters
->options().shared())
1253 return tls::TLSOPT_NONE
;
1255 return tls::TLSOPT_TO_LE
;
1258 tls::Tls_optimization
1259 optimize_tls_ie(bool is_final
)
1261 if (!is_final
|| parameters
->options().shared())
1262 return tls::TLSOPT_NONE
;
1264 return tls::TLSOPT_TO_LE
;
1269 make_glink_section(Layout
*);
1271 // Create the PLT section.
1273 make_plt_section(Symbol_table
*, Layout
*);
1276 make_iplt_section(Symbol_table
*, Layout
*);
1279 make_brlt_section(Layout
*);
1281 // Create a PLT entry for a global symbol.
1283 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1285 // Create a PLT entry for a local IFUNC symbol.
1287 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1288 Sized_relobj_file
<size
, big_endian
>*,
1292 // Create a GOT entry for local dynamic __tls_get_addr.
1294 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
1295 Sized_relobj_file
<size
, big_endian
>* object
);
1298 tlsld_got_offset() const
1300 return this->tlsld_got_offset_
;
1303 // Get the dynamic reloc section, creating it if necessary.
1305 rela_dyn_section(Layout
*);
1307 // Similarly, but for ifunc symbols get the one for ifunc.
1309 rela_dyn_section(Symbol_table
*, Layout
*, bool for_ifunc
);
1311 // Copy a relocation against a global symbol.
1313 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1314 Sized_relobj_file
<size
, big_endian
>* object
,
1315 unsigned int shndx
, Output_section
* output_section
,
1316 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
1318 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1319 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1320 symtab
->get_sized_symbol
<size
>(sym
),
1321 object
, shndx
, output_section
,
1322 r_type
, reloc
.get_r_offset(),
1323 reloc
.get_r_addend(),
1324 this->rela_dyn_section(layout
));
1327 // Look over all the input sections, deciding where to place stubs.
1329 group_sections(Layout
*, const Task
*, bool);
1331 // Sort output sections by address.
1332 struct Sort_sections
1335 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
1336 { return sec1
->address() < sec2
->address(); }
1342 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1343 unsigned int data_shndx
,
1345 unsigned int r_type
,
1348 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
1349 r_type_(r_type
), r_sym_(r_sym
), addend_(addend
)
1355 // If this branch needs a plt call stub, or a long branch stub, make one.
1357 make_stub(Stub_table
<size
, big_endian
>*,
1358 Stub_table
<size
, big_endian
>*,
1359 Symbol_table
*) const;
1362 // The branch location..
1363 Powerpc_relobj
<size
, big_endian
>* object_
;
1364 unsigned int shndx_
;
1366 // ..and the branch type and destination.
1367 unsigned int r_type_
;
1368 unsigned int r_sym_
;
1372 // Information about this specific target which we pass to the
1373 // general Target structure.
1374 static Target::Target_info powerpc_info
;
1376 // The types of GOT entries needed for this platform.
1377 // These values are exposed to the ABI in an incremental link.
1378 // Do not renumber existing values without changing the version
1379 // number of the .gnu_incremental_inputs section.
1383 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
1384 GOT_TYPE_DTPREL
, // entry for @got@dtprel
1385 GOT_TYPE_TPREL
// entry for @got@tprel
1389 Output_data_got_powerpc
<size
, big_endian
>* got_
;
1390 // The PLT section. This is a container for a table of addresses,
1391 // and their relocations. Each address in the PLT has a dynamic
1392 // relocation (R_*_JMP_SLOT) and each address will have a
1393 // corresponding entry in .glink for lazy resolution of the PLT.
1394 // ppc32 initialises the PLT to point at the .glink entry, while
1395 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1396 // linker adds a stub that loads the PLT entry into ctr then
1397 // branches to ctr. There may be more than one stub for each PLT
1398 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1399 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1400 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
1401 // The IPLT section. Like plt_, this is a container for a table of
1402 // addresses and their relocations, specifically for STT_GNU_IFUNC
1403 // functions that resolve locally (STT_GNU_IFUNC functions that
1404 // don't resolve locally go in PLT). Unlike plt_, these have no
1405 // entry in .glink for lazy resolution, and the relocation section
1406 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1407 // the relocation section may contain relocations against
1408 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1409 // relocation section will appear at the end of other dynamic
1410 // relocations, so that ld.so applies these relocations after other
1411 // dynamic relocations. In a static executable, the relocation
1412 // section is emitted and marked with __rela_iplt_start and
1413 // __rela_iplt_end symbols.
1414 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
1415 // Section holding long branch destinations.
1416 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
1417 // The .glink section.
1418 Output_data_glink
<size
, big_endian
>* glink_
;
1419 // The dynamic reloc section.
1420 Reloc_section
* rela_dyn_
;
1421 // Relocs saved to avoid a COPY reloc.
1422 Powerpc_copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
1423 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1424 unsigned int tlsld_got_offset_
;
1426 Stub_tables stub_tables_
;
1427 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
1428 Branch_lookup_table branch_lookup_table_
;
1430 typedef std::vector
<Branch_info
> Branches
;
1431 Branches branch_info_
;
1433 bool plt_thread_safe_
;
1436 int relax_fail_count_
;
1437 int32_t stub_group_size_
;
1439 Output_data_save_res
<size
, big_endian
> *savres_section_
;
1443 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
1446 true, // is_big_endian
1447 elfcpp::EM_PPC
, // machine_code
1448 false, // has_make_symbol
1449 false, // has_resolve
1450 false, // has_code_fill
1451 true, // is_default_stack_executable
1452 false, // can_icf_inline_merge_sections
1454 "/usr/lib/ld.so.1", // dynamic_linker
1455 0x10000000, // default_text_segment_address
1456 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1457 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1458 false, // isolate_execinstr
1460 elfcpp::SHN_UNDEF
, // small_common_shndx
1461 elfcpp::SHN_UNDEF
, // large_common_shndx
1462 0, // small_common_section_flags
1463 0, // large_common_section_flags
1464 NULL
, // attributes_section
1465 NULL
, // attributes_vendor
1466 "_start", // entry_symbol_name
1467 32, // hash_entry_size
1471 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
1474 false, // is_big_endian
1475 elfcpp::EM_PPC
, // machine_code
1476 false, // has_make_symbol
1477 false, // has_resolve
1478 false, // has_code_fill
1479 true, // is_default_stack_executable
1480 false, // can_icf_inline_merge_sections
1482 "/usr/lib/ld.so.1", // dynamic_linker
1483 0x10000000, // default_text_segment_address
1484 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1485 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1486 false, // isolate_execinstr
1488 elfcpp::SHN_UNDEF
, // small_common_shndx
1489 elfcpp::SHN_UNDEF
, // large_common_shndx
1490 0, // small_common_section_flags
1491 0, // large_common_section_flags
1492 NULL
, // attributes_section
1493 NULL
, // attributes_vendor
1494 "_start", // entry_symbol_name
1495 32, // hash_entry_size
1499 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1502 true, // is_big_endian
1503 elfcpp::EM_PPC64
, // machine_code
1504 false, // has_make_symbol
1505 false, // has_resolve
1506 false, // has_code_fill
1507 true, // is_default_stack_executable
1508 false, // can_icf_inline_merge_sections
1510 "/usr/lib/ld.so.1", // dynamic_linker
1511 0x10000000, // default_text_segment_address
1512 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1513 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1514 false, // isolate_execinstr
1516 elfcpp::SHN_UNDEF
, // small_common_shndx
1517 elfcpp::SHN_UNDEF
, // large_common_shndx
1518 0, // small_common_section_flags
1519 0, // large_common_section_flags
1520 NULL
, // attributes_section
1521 NULL
, // attributes_vendor
1522 "_start", // entry_symbol_name
1523 32, // hash_entry_size
1527 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1530 false, // is_big_endian
1531 elfcpp::EM_PPC64
, // machine_code
1532 false, // has_make_symbol
1533 false, // has_resolve
1534 false, // has_code_fill
1535 true, // is_default_stack_executable
1536 false, // can_icf_inline_merge_sections
1538 "/usr/lib/ld.so.1", // dynamic_linker
1539 0x10000000, // default_text_segment_address
1540 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1541 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1542 false, // isolate_execinstr
1544 elfcpp::SHN_UNDEF
, // small_common_shndx
1545 elfcpp::SHN_UNDEF
, // large_common_shndx
1546 0, // small_common_section_flags
1547 0, // large_common_section_flags
1548 NULL
, // attributes_section
1549 NULL
, // attributes_vendor
1550 "_start", // entry_symbol_name
1551 32, // hash_entry_size
1555 is_branch_reloc(unsigned int r_type
)
1557 return (r_type
== elfcpp::R_POWERPC_REL24
1558 || r_type
== elfcpp::R_PPC_PLTREL24
1559 || r_type
== elfcpp::R_PPC_LOCAL24PC
1560 || r_type
== elfcpp::R_POWERPC_REL14
1561 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1562 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1563 || r_type
== elfcpp::R_POWERPC_ADDR24
1564 || r_type
== elfcpp::R_POWERPC_ADDR14
1565 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1566 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1569 // If INSN is an opcode that may be used with an @tls operand, return
1570 // the transformed insn for TLS optimisation, otherwise return 0. If
1571 // REG is non-zero only match an insn with RB or RA equal to REG.
1573 at_tls_transform(uint32_t insn
, unsigned int reg
)
1575 if ((insn
& (0x3f << 26)) != 31 << 26)
1579 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1580 rtra
= insn
& ((1 << 26) - (1 << 16));
1581 else if (((insn
>> 16) & 0x1f) == reg
)
1582 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1586 if ((insn
& (0x3ff << 1)) == 266 << 1)
1589 else if ((insn
& (0x1f << 1)) == 23 << 1
1590 && ((insn
& (0x1f << 6)) < 14 << 6
1591 || ((insn
& (0x1f << 6)) >= 16 << 6
1592 && (insn
& (0x1f << 6)) < 24 << 6)))
1593 // load and store indexed -> dform
1594 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1595 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1596 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1597 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1598 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1600 insn
= (58 << 26) | 2;
1608 template<int size
, bool big_endian
>
1609 class Powerpc_relocate_functions
1629 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1630 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1631 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword SignedAddress
;
1633 template<int valsize
>
1635 has_overflow_signed(Address value
)
1637 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1638 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1639 limit
<<= ((valsize
- 1) >> 1);
1640 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1641 return value
+ limit
> (limit
<< 1) - 1;
1644 template<int valsize
>
1646 has_overflow_unsigned(Address value
)
1648 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1649 limit
<<= ((valsize
- 1) >> 1);
1650 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1651 return value
> (limit
<< 1) - 1;
1654 template<int valsize
>
1656 has_overflow_bitfield(Address value
)
1658 return (has_overflow_unsigned
<valsize
>(value
)
1659 && has_overflow_signed
<valsize
>(value
));
1662 template<int valsize
>
1663 static inline Status
1664 overflowed(Address value
, Overflow_check overflow
)
1666 if (overflow
== CHECK_SIGNED
)
1668 if (has_overflow_signed
<valsize
>(value
))
1669 return STATUS_OVERFLOW
;
1671 else if (overflow
== CHECK_UNSIGNED
)
1673 if (has_overflow_unsigned
<valsize
>(value
))
1674 return STATUS_OVERFLOW
;
1676 else if (overflow
== CHECK_BITFIELD
)
1678 if (has_overflow_bitfield
<valsize
>(value
))
1679 return STATUS_OVERFLOW
;
1684 // Do a simple RELA relocation
1685 template<int fieldsize
, int valsize
>
1686 static inline Status
1687 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1689 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1690 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1691 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, value
);
1692 return overflowed
<valsize
>(value
, overflow
);
1695 template<int fieldsize
, int valsize
>
1696 static inline Status
1697 rela(unsigned char* view
,
1698 unsigned int right_shift
,
1699 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1701 Overflow_check overflow
)
1703 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1704 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1705 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(wv
);
1706 Valtype reloc
= value
>> right_shift
;
1709 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, val
| reloc
);
1710 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1713 // Do a simple RELA relocation, unaligned.
1714 template<int fieldsize
, int valsize
>
1715 static inline Status
1716 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1718 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, value
);
1719 return overflowed
<valsize
>(value
, overflow
);
1722 template<int fieldsize
, int valsize
>
1723 static inline Status
1724 rela_ua(unsigned char* view
,
1725 unsigned int right_shift
,
1726 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1728 Overflow_check overflow
)
1730 typedef typename
elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::Valtype
1732 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(view
);
1733 Valtype reloc
= value
>> right_shift
;
1736 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, val
| reloc
);
1737 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1741 // R_PPC64_ADDR64: (Symbol + Addend)
1743 addr64(unsigned char* view
, Address value
)
1744 { This::template rela
<64,64>(view
, value
, CHECK_NONE
); }
1746 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1748 addr64_u(unsigned char* view
, Address value
)
1749 { This::template rela_ua
<64,64>(view
, value
, CHECK_NONE
); }
1751 // R_POWERPC_ADDR32: (Symbol + Addend)
1752 static inline Status
1753 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
1754 { return This::template rela
<32,32>(view
, value
, overflow
); }
1756 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1757 static inline Status
1758 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1759 { return This::template rela_ua
<32,32>(view
, value
, overflow
); }
1761 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1762 static inline Status
1763 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
1765 Status stat
= This::template rela
<32,26>(view
, 0, 0x03fffffc,
1767 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1768 stat
= STATUS_OVERFLOW
;
1772 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1773 static inline Status
1774 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
1775 { return This::template rela
<16,16>(view
, value
, overflow
); }
1777 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1778 static inline Status
1779 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1780 { return This::template rela_ua
<16,16>(view
, value
, overflow
); }
1782 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1783 static inline Status
1784 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
1786 Status stat
= This::template rela
<16,16>(view
, 0, 0xfffc, value
, overflow
);
1787 if ((value
& 3) != 0)
1788 stat
= STATUS_OVERFLOW
;
1792 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
1793 static inline Status
1794 addr16_dq(unsigned char* view
, Address value
, Overflow_check overflow
)
1796 Status stat
= This::template rela
<16,16>(view
, 0, 0xfff0, value
, overflow
);
1797 if ((value
& 15) != 0)
1798 stat
= STATUS_OVERFLOW
;
1802 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1804 addr16_hi(unsigned char* view
, Address value
)
1805 { This::template rela
<16,16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
1807 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1809 addr16_ha(unsigned char* view
, Address value
)
1810 { This::addr16_hi(view
, value
+ 0x8000); }
1812 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1814 addr16_hi2(unsigned char* view
, Address value
)
1815 { This::template rela
<16,16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
1817 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1819 addr16_ha2(unsigned char* view
, Address value
)
1820 { This::addr16_hi2(view
, value
+ 0x8000); }
1822 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1824 addr16_hi3(unsigned char* view
, Address value
)
1825 { This::template rela
<16,16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
1827 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1829 addr16_ha3(unsigned char* view
, Address value
)
1830 { This::addr16_hi3(view
, value
+ 0x8000); }
1832 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1833 static inline Status
1834 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
1836 Status stat
= This::template rela
<32,16>(view
, 0, 0xfffc, value
, overflow
);
1837 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1838 stat
= STATUS_OVERFLOW
;
1842 // R_POWERPC_REL16DX_HA
1843 static inline Status
1844 addr16dx_ha(unsigned char *view
, Address value
, Overflow_check overflow
)
1846 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1847 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1848 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1850 value
= static_cast<SignedAddress
>(value
) >> 16;
1851 val
|= (value
& 0xffc1) | ((value
& 0x3e) << 15);
1852 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1853 return overflowed
<16>(value
, overflow
);
1857 // Set ABI version for input and output.
1859 template<int size
, bool big_endian
>
1861 Powerpc_relobj
<size
, big_endian
>::set_abiversion(int ver
)
1863 this->e_flags_
|= ver
;
1864 if (this->abiversion() != 0)
1866 Target_powerpc
<size
, big_endian
>* target
=
1867 static_cast<Target_powerpc
<size
, big_endian
>*>(
1868 parameters
->sized_target
<size
, big_endian
>());
1869 if (target
->abiversion() == 0)
1870 target
->set_abiversion(this->abiversion());
1871 else if (target
->abiversion() != this->abiversion())
1872 gold_error(_("%s: ABI version %d is not compatible "
1873 "with ABI version %d output"),
1874 this->name().c_str(),
1875 this->abiversion(), target
->abiversion());
1880 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
1881 // relocatable object, if such sections exists.
1883 template<int size
, bool big_endian
>
1885 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
1886 Read_symbols_data
* sd
)
1888 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1889 const unsigned char* namesu
= sd
->section_names
->data();
1890 const char* names
= reinterpret_cast<const char*>(namesu
);
1891 section_size_type names_size
= sd
->section_names_size
;
1892 const unsigned char* s
;
1894 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
,
1895 size
== 32 ? ".got2" : ".opd",
1896 names
, names_size
, NULL
);
1899 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
1900 this->special_
= ndx
;
1903 if (this->abiversion() == 0)
1904 this->set_abiversion(1);
1905 else if (this->abiversion() > 1)
1906 gold_error(_("%s: .opd invalid in abiv%d"),
1907 this->name().c_str(), this->abiversion());
1912 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".rela.toc",
1913 names
, names_size
, NULL
);
1916 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
1917 this->relatoc_
= ndx
;
1918 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
1919 this->toc_
= this->adjust_shndx(shdr
.get_sh_info());
1922 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
1925 // Examine .rela.opd to build info about function entry points.
1927 template<int size
, bool big_endian
>
1929 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
1931 const unsigned char* prelocs
,
1932 const unsigned char* plocal_syms
)
1936 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
1937 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
1938 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1939 Address expected_off
= 0;
1940 bool regular
= true;
1941 unsigned int opd_ent_size
= 0;
1943 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
1945 Reltype
reloc(prelocs
);
1946 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
1947 = reloc
.get_r_info();
1948 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
1949 if (r_type
== elfcpp::R_PPC64_ADDR64
)
1951 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1952 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
1955 if (r_sym
< this->local_symbol_count())
1957 typename
elfcpp::Sym
<size
, big_endian
>
1958 lsym(plocal_syms
+ r_sym
* sym_size
);
1959 shndx
= lsym
.get_st_shndx();
1960 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1961 value
= lsym
.get_st_value();
1964 shndx
= this->symbol_section_and_value(r_sym
, &value
,
1966 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
1967 value
+ reloc
.get_r_addend());
1970 expected_off
= reloc
.get_r_offset();
1971 opd_ent_size
= expected_off
;
1973 else if (expected_off
!= reloc
.get_r_offset())
1975 expected_off
+= opd_ent_size
;
1977 else if (r_type
== elfcpp::R_PPC64_TOC
)
1979 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
1984 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1985 this->name().c_str(), r_type
);
1989 if (reloc_count
<= 2)
1990 opd_ent_size
= this->section_size(this->opd_shndx());
1991 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
1995 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1996 this->name().c_str());
2002 // Returns true if a code sequence loading the TOC entry at VALUE
2003 // relative to the TOC pointer can be converted into code calculating
2004 // a TOC pointer relative offset.
2005 // If so, the TOC pointer relative offset is stored to VALUE.
2007 template<int size
, bool big_endian
>
2009 Powerpc_relobj
<size
, big_endian
>::make_toc_relative(
2010 Target_powerpc
<size
, big_endian
>* target
,
2016 // With -mcmodel=medium code it is quite possible to have
2017 // toc-relative relocs referring to objects outside the TOC.
2018 // Don't try to look at a non-existent TOC.
2019 if (this->toc_shndx() == 0)
2022 // Convert VALUE back to an address by adding got_base (see below),
2023 // then to an offset in the TOC by subtracting the TOC output
2024 // section address and the TOC output offset. Since this TOC output
2025 // section and the got output section are one and the same, we can
2026 // omit adding and subtracting the output section address.
2027 Address off
= (*value
+ this->toc_base_offset()
2028 - this->output_section_offset(this->toc_shndx()));
2029 // Is this offset in the TOC? -mcmodel=medium code may be using
2030 // TOC relative access to variables outside the TOC. Those of
2031 // course can't be optimized. We also don't try to optimize code
2032 // that is using a different object's TOC.
2033 if (off
>= this->section_size(this->toc_shndx()))
2036 if (this->no_toc_opt(off
))
2039 section_size_type vlen
;
2040 unsigned char* view
= this->get_output_view(this->toc_shndx(), &vlen
);
2041 Address addr
= elfcpp::Swap
<size
, big_endian
>::readval(view
+ off
);
2043 Address got_base
= (target
->got_section()->output_section()->address()
2044 + this->toc_base_offset());
2046 if (addr
+ (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2053 // Perform the Sized_relobj_file method, then set up opd info from
2056 template<int size
, bool big_endian
>
2058 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
2060 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
2063 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
2064 p
!= rd
->relocs
.end();
2067 if (p
->data_shndx
== this->opd_shndx())
2069 uint64_t opd_size
= this->section_size(this->opd_shndx());
2070 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
2073 this->init_opd(opd_size
);
2074 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
2075 rd
->local_symbols
->data());
2083 // Read the symbols then set up st_other vector.
2085 template<int size
, bool big_endian
>
2087 Powerpc_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
2089 this->base_read_symbols(sd
);
2092 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2093 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2094 const unsigned int loccount
= this->do_local_symbol_count();
2097 this->st_other_
.resize(loccount
);
2098 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2099 off_t locsize
= loccount
* sym_size
;
2100 const unsigned int symtab_shndx
= this->symtab_shndx();
2101 const unsigned char *psymtab
= pshdrs
+ symtab_shndx
* shdr_size
;
2102 typename
elfcpp::Shdr
<size
, big_endian
> shdr(psymtab
);
2103 const unsigned char* psyms
= this->get_view(shdr
.get_sh_offset(),
2104 locsize
, true, false);
2106 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
2108 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
2109 unsigned char st_other
= sym
.get_st_other();
2110 this->st_other_
[i
] = st_other
;
2111 if ((st_other
& elfcpp::STO_PPC64_LOCAL_MASK
) != 0)
2113 if (this->abiversion() == 0)
2114 this->set_abiversion(2);
2115 else if (this->abiversion() < 2)
2116 gold_error(_("%s: local symbol %d has invalid st_other"
2117 " for ABI version 1"),
2118 this->name().c_str(), i
);
2125 template<int size
, bool big_endian
>
2127 Powerpc_dynobj
<size
, big_endian
>::set_abiversion(int ver
)
2129 this->e_flags_
|= ver
;
2130 if (this->abiversion() != 0)
2132 Target_powerpc
<size
, big_endian
>* target
=
2133 static_cast<Target_powerpc
<size
, big_endian
>*>(
2134 parameters
->sized_target
<size
, big_endian
>());
2135 if (target
->abiversion() == 0)
2136 target
->set_abiversion(this->abiversion());
2137 else if (target
->abiversion() != this->abiversion())
2138 gold_error(_("%s: ABI version %d is not compatible "
2139 "with ABI version %d output"),
2140 this->name().c_str(),
2141 this->abiversion(), target
->abiversion());
2146 // Call Sized_dynobj::base_read_symbols to read the symbols then
2147 // read .opd from a dynamic object, filling in opd_ent_ vector,
2149 template<int size
, bool big_endian
>
2151 Powerpc_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
2153 this->base_read_symbols(sd
);
2156 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2157 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2158 const unsigned char* namesu
= sd
->section_names
->data();
2159 const char* names
= reinterpret_cast<const char*>(namesu
);
2160 const unsigned char* s
= NULL
;
2161 const unsigned char* opd
;
2162 section_size_type opd_size
;
2164 // Find and read .opd section.
2167 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".opd", names
,
2168 sd
->section_names_size
,
2173 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2174 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2175 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
2177 if (this->abiversion() == 0)
2178 this->set_abiversion(1);
2179 else if (this->abiversion() > 1)
2180 gold_error(_("%s: .opd invalid in abiv%d"),
2181 this->name().c_str(), this->abiversion());
2183 this->opd_shndx_
= (s
- pshdrs
) / shdr_size
;
2184 this->opd_address_
= shdr
.get_sh_addr();
2185 opd_size
= convert_to_section_size_type(shdr
.get_sh_size());
2186 opd
= this->get_view(shdr
.get_sh_offset(), opd_size
,
2192 // Build set of executable sections.
2193 // Using a set is probably overkill. There is likely to be only
2194 // a few executable sections, typically .init, .text and .fini,
2195 // and they are generally grouped together.
2196 typedef std::set
<Sec_info
> Exec_sections
;
2197 Exec_sections exec_sections
;
2199 for (unsigned int i
= 1; i
< this->shnum(); ++i
, s
+= shdr_size
)
2201 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2202 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2203 && ((shdr
.get_sh_flags()
2204 & (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2205 == (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2206 && shdr
.get_sh_size() != 0)
2208 exec_sections
.insert(Sec_info(shdr
.get_sh_addr(),
2209 shdr
.get_sh_size(), i
));
2212 if (exec_sections
.empty())
2215 // Look over the OPD entries. This is complicated by the fact
2216 // that some binaries will use two-word entries while others
2217 // will use the standard three-word entries. In most cases
2218 // the third word (the environment pointer for languages like
2219 // Pascal) is unused and will be zero. If the third word is
2220 // used it should not be pointing into executable sections,
2222 this->init_opd(opd_size
);
2223 for (const unsigned char* p
= opd
; p
< opd
+ opd_size
; p
+= 8)
2225 typedef typename
elfcpp::Swap
<64, big_endian
>::Valtype Valtype
;
2226 const Valtype
* valp
= reinterpret_cast<const Valtype
*>(p
);
2227 Valtype val
= elfcpp::Swap
<64, big_endian
>::readval(valp
);
2229 // Chances are that this is the third word of an OPD entry.
2231 typename
Exec_sections::const_iterator e
2232 = exec_sections
.upper_bound(Sec_info(val
, 0, 0));
2233 if (e
!= exec_sections
.begin())
2236 if (e
->start
<= val
&& val
< e
->start
+ e
->len
)
2238 // We have an address in an executable section.
2239 // VAL ought to be the function entry, set it up.
2240 this->set_opd_ent(p
- opd
, e
->shndx
, val
);
2241 // Skip second word of OPD entry, the TOC pointer.
2245 // If we didn't match any executable sections, we likely
2246 // have a non-zero third word in the OPD entry.
2251 // Relocate sections.
2253 template<int size
, bool big_endian
>
2255 Powerpc_relobj
<size
, big_endian
>::do_relocate_sections(
2256 const Symbol_table
* symtab
, const Layout
* layout
,
2257 const unsigned char* pshdrs
, Output_file
* of
,
2258 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
)
2260 unsigned int start
= 1;
2262 && this->relatoc_
!= 0
2263 && !parameters
->options().relocatable())
2265 // Relocate .toc first.
2266 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2267 this->relatoc_
, this->relatoc_
);
2268 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2269 1, this->relatoc_
- 1);
2270 start
= this->relatoc_
+ 1;
2272 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2273 start
, this->shnum() - 1);
2276 // Set up some symbols.
2278 template<int size
, bool big_endian
>
2280 Target_powerpc
<size
, big_endian
>::do_define_standard_symbols(
2281 Symbol_table
* symtab
,
2286 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2287 // undefined when scanning relocs (and thus requires
2288 // non-relative dynamic relocs). The proper value will be
2290 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2291 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2293 Target_powerpc
<size
, big_endian
>* target
=
2294 static_cast<Target_powerpc
<size
, big_endian
>*>(
2295 parameters
->sized_target
<size
, big_endian
>());
2296 Output_data_got_powerpc
<size
, big_endian
>* got
2297 = target
->got_section(symtab
, layout
);
2298 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2299 Symbol_table::PREDEFINED
,
2303 elfcpp::STV_HIDDEN
, 0,
2307 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2308 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
2309 if (sdasym
!= NULL
&& sdasym
->is_undefined())
2311 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
2313 = layout
->add_output_section_data(".sdata", 0,
2315 | elfcpp::SHF_WRITE
,
2316 sdata
, ORDER_SMALL_DATA
, false);
2317 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
2318 Symbol_table::PREDEFINED
,
2319 os
, 32768, 0, elfcpp::STT_OBJECT
,
2320 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
2326 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2327 Symbol
*gotsym
= symtab
->lookup(".TOC.", NULL
);
2328 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2330 Target_powerpc
<size
, big_endian
>* target
=
2331 static_cast<Target_powerpc
<size
, big_endian
>*>(
2332 parameters
->sized_target
<size
, big_endian
>());
2333 Output_data_got_powerpc
<size
, big_endian
>* got
2334 = target
->got_section(symtab
, layout
);
2335 symtab
->define_in_output_data(".TOC.", NULL
,
2336 Symbol_table::PREDEFINED
,
2340 elfcpp::STV_HIDDEN
, 0,
2346 // Set up PowerPC target specific relobj.
2348 template<int size
, bool big_endian
>
2350 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
2351 const std::string
& name
,
2352 Input_file
* input_file
,
2353 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
2355 int et
= ehdr
.get_e_type();
2356 // ET_EXEC files are valid input for --just-symbols/-R,
2357 // and we treat them as relocatable objects.
2358 if (et
== elfcpp::ET_REL
2359 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
2361 Powerpc_relobj
<size
, big_endian
>* obj
=
2362 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2366 else if (et
== elfcpp::ET_DYN
)
2368 Powerpc_dynobj
<size
, big_endian
>* obj
=
2369 new Powerpc_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2375 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
2380 template<int size
, bool big_endian
>
2381 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
2384 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
2385 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
2387 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
2388 : Output_data_got
<size
, big_endian
>(),
2389 symtab_(symtab
), layout_(layout
),
2390 header_ent_cnt_(size
== 32 ? 3 : 1),
2391 header_index_(size
== 32 ? 0x2000 : 0)
2394 this->set_addralign(256);
2397 // Override all the Output_data_got methods we use so as to first call
2400 add_global(Symbol
* gsym
, unsigned int got_type
)
2402 this->reserve_ent();
2403 return Output_data_got
<size
, big_endian
>::add_global(gsym
, got_type
);
2407 add_global_plt(Symbol
* gsym
, unsigned int got_type
)
2409 this->reserve_ent();
2410 return Output_data_got
<size
, big_endian
>::add_global_plt(gsym
, got_type
);
2414 add_global_tls(Symbol
* gsym
, unsigned int got_type
)
2415 { return this->add_global_plt(gsym
, got_type
); }
2418 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
2419 Output_data_reloc_generic
* rel_dyn
, unsigned int r_type
)
2421 this->reserve_ent();
2422 Output_data_got
<size
, big_endian
>::
2423 add_global_with_rel(gsym
, got_type
, rel_dyn
, r_type
);
2427 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
2428 Output_data_reloc_generic
* rel_dyn
,
2429 unsigned int r_type_1
, unsigned int r_type_2
)
2431 this->reserve_ent(2);
2432 Output_data_got
<size
, big_endian
>::
2433 add_global_pair_with_rel(gsym
, got_type
, rel_dyn
, r_type_1
, r_type_2
);
2437 add_local(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2439 this->reserve_ent();
2440 return Output_data_got
<size
, big_endian
>::add_local(object
, sym_index
,
2445 add_local_plt(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2447 this->reserve_ent();
2448 return Output_data_got
<size
, big_endian
>::add_local_plt(object
, sym_index
,
2453 add_local_tls(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2454 { return this->add_local_plt(object
, sym_index
, got_type
); }
2457 add_local_tls_pair(Relobj
* object
, unsigned int sym_index
,
2458 unsigned int got_type
,
2459 Output_data_reloc_generic
* rel_dyn
,
2460 unsigned int r_type
)
2462 this->reserve_ent(2);
2463 Output_data_got
<size
, big_endian
>::
2464 add_local_tls_pair(object
, sym_index
, got_type
, rel_dyn
, r_type
);
2468 add_constant(Valtype constant
)
2470 this->reserve_ent();
2471 return Output_data_got
<size
, big_endian
>::add_constant(constant
);
2475 add_constant_pair(Valtype c1
, Valtype c2
)
2477 this->reserve_ent(2);
2478 return Output_data_got
<size
, big_endian
>::add_constant_pair(c1
, c2
);
2481 // Offset of _GLOBAL_OFFSET_TABLE_.
2485 return this->got_offset(this->header_index_
);
2488 // Offset of base used to access the GOT/TOC.
2489 // The got/toc pointer reg will be set to this value.
2491 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
2494 return this->g_o_t();
2496 return (this->output_section()->address()
2497 + object
->toc_base_offset()
2501 // Ensure our GOT has a header.
2503 set_final_data_size()
2505 if (this->header_ent_cnt_
!= 0)
2506 this->make_header();
2507 Output_data_got
<size
, big_endian
>::set_final_data_size();
2510 // First word of GOT header needs some values that are not
2511 // handled by Output_data_got so poke them in here.
2512 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2514 do_write(Output_file
* of
)
2517 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
2518 val
= this->layout_
->dynamic_section()->address();
2520 val
= this->output_section()->address() + 0x8000;
2521 this->replace_constant(this->header_index_
, val
);
2522 Output_data_got
<size
, big_endian
>::do_write(of
);
2527 reserve_ent(unsigned int cnt
= 1)
2529 if (this->header_ent_cnt_
== 0)
2531 if (this->num_entries() + cnt
> this->header_index_
)
2532 this->make_header();
2538 this->header_ent_cnt_
= 0;
2539 this->header_index_
= this->num_entries();
2542 Output_data_got
<size
, big_endian
>::add_constant(0);
2543 Output_data_got
<size
, big_endian
>::add_constant(0);
2544 Output_data_got
<size
, big_endian
>::add_constant(0);
2546 // Define _GLOBAL_OFFSET_TABLE_ at the header
2547 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2550 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
2551 sym
->set_value(this->g_o_t());
2554 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2555 Symbol_table::PREDEFINED
,
2556 this, this->g_o_t(), 0,
2559 elfcpp::STV_HIDDEN
, 0,
2563 Output_data_got
<size
, big_endian
>::add_constant(0);
2566 // Stashed pointers.
2567 Symbol_table
* symtab_
;
2571 unsigned int header_ent_cnt_
;
2572 // GOT header index.
2573 unsigned int header_index_
;
2576 // Get the GOT section, creating it if necessary.
2578 template<int size
, bool big_endian
>
2579 Output_data_got_powerpc
<size
, big_endian
>*
2580 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
2583 if (this->got_
== NULL
)
2585 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2588 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
2590 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2591 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2592 this->got_
, ORDER_DATA
, false);
2598 // Get the dynamic reloc section, creating it if necessary.
2600 template<int size
, bool big_endian
>
2601 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2602 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
2604 if (this->rela_dyn_
== NULL
)
2606 gold_assert(layout
!= NULL
);
2607 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
2608 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
2609 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
2610 ORDER_DYNAMIC_RELOCS
, false);
2612 return this->rela_dyn_
;
2615 // Similarly, but for ifunc symbols get the one for ifunc.
2617 template<int size
, bool big_endian
>
2618 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2619 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Symbol_table
* symtab
,
2624 return this->rela_dyn_section(layout
);
2626 if (this->iplt_
== NULL
)
2627 this->make_iplt_section(symtab
, layout
);
2628 return this->iplt_
->rel_plt();
2634 // Determine the stub group size. The group size is the absolute
2635 // value of the parameter --stub-group-size. If --stub-group-size
2636 // is passed a negative value, we restrict stubs to be always after
2637 // the stubbed branches.
2638 Stub_control(int32_t size
, bool no_size_errors
, bool multi_os
)
2639 : stub_group_size_(abs(size
)), stubs_always_after_branch_(size
< 0),
2640 suppress_size_errors_(no_size_errors
), multi_os_(multi_os
),
2641 state_(NO_GROUP
), group_size_(0), group_start_addr_(0),
2642 owner_(NULL
), output_section_(NULL
)
2646 // Return true iff input section can be handled by current stub
2649 can_add_to_stub_group(Output_section
* o
,
2650 const Output_section::Input_section
* i
,
2653 const Output_section::Input_section
*
2659 { return output_section_
; }
2662 set_output_and_owner(Output_section
* o
,
2663 const Output_section::Input_section
* i
)
2665 this->output_section_
= o
;
2674 // Adding group sections before the stubs.
2675 FINDING_STUB_SECTION
,
2676 // Adding group sections after the stubs.
2680 uint32_t stub_group_size_
;
2681 bool stubs_always_after_branch_
;
2682 bool suppress_size_errors_
;
2683 // True if a stub group can serve multiple output sections.
2686 // Current max size of group. Starts at stub_group_size_ but is
2687 // reduced to stub_group_size_/1024 on seeing a section with
2688 // external conditional branches.
2689 uint32_t group_size_
;
2690 uint64_t group_start_addr_
;
2691 // owner_ and output_section_ specify the section to which stubs are
2692 // attached. The stubs are placed at the end of this section.
2693 const Output_section::Input_section
* owner_
;
2694 Output_section
* output_section_
;
2697 // Return true iff input section can be handled by current stub
2698 // group. Sections are presented to this function in order,
2699 // so the first section is the head of the group.
2702 Stub_control::can_add_to_stub_group(Output_section
* o
,
2703 const Output_section::Input_section
* i
,
2706 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
2708 uint64_t start_addr
= o
->address();
2711 // .init and .fini sections are pasted together to form a single
2712 // function. We can't be adding stubs in the middle of the function.
2713 this_size
= o
->data_size();
2716 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
2717 this_size
= i
->data_size();
2720 uint64_t end_addr
= start_addr
+ this_size
;
2721 uint32_t group_size
= this->stub_group_size_
;
2723 this->group_size_
= group_size
= group_size
>> 10;
2725 if (this_size
> group_size
&& !this->suppress_size_errors_
)
2726 gold_warning(_("%s:%s exceeds group size"),
2727 i
->relobj()->name().c_str(),
2728 i
->relobj()->section_name(i
->shndx()).c_str());
2730 gold_debug(DEBUG_TARGET
, "maybe add%s %s:%s size=%#llx total=%#llx",
2731 has14
? " 14bit" : "",
2732 i
->relobj()->name().c_str(),
2733 i
->relobj()->section_name(i
->shndx()).c_str(),
2734 (long long) this_size
,
2735 (this->state_
== NO_GROUP
2737 : (long long) end_addr
- this->group_start_addr_
));
2739 if (this->state_
== NO_GROUP
)
2741 // Only here on very first use of Stub_control
2743 this->output_section_
= o
;
2744 this->state_
= FINDING_STUB_SECTION
;
2745 this->group_size_
= group_size
;
2746 this->group_start_addr_
= start_addr
;
2749 else if (!this->multi_os_
&& this->output_section_
!= o
)
2751 else if (this->state_
== HAS_STUB_SECTION
)
2753 // Can we add this section, which is after the stubs, to the
2755 if (end_addr
- this->group_start_addr_
<= this->group_size_
)
2758 else if (this->state_
== FINDING_STUB_SECTION
)
2760 if ((whole_sec
&& this->output_section_
== o
)
2761 || end_addr
- this->group_start_addr_
<= this->group_size_
)
2763 // Stubs are added at the end of "owner_".
2765 this->output_section_
= o
;
2768 // The group before the stubs has reached maximum size.
2769 // Now see about adding sections after the stubs to the
2770 // group. If the current section has a 14-bit branch and
2771 // the group before the stubs exceeds group_size_ (because
2772 // they didn't have 14-bit branches), don't add sections
2773 // after the stubs: The size of stubs for such a large
2774 // group may exceed the reach of a 14-bit branch.
2775 if (!this->stubs_always_after_branch_
2776 && this_size
<= this->group_size_
2777 && start_addr
- this->group_start_addr_
<= this->group_size_
)
2779 gold_debug(DEBUG_TARGET
, "adding after stubs");
2780 this->state_
= HAS_STUB_SECTION
;
2781 this->group_start_addr_
= start_addr
;
2788 gold_debug(DEBUG_TARGET
,
2789 !this->multi_os_
&& this->output_section_
!= o
2790 ? "nope, new output section\n"
2791 : "nope, didn't fit\n");
2793 // The section fails to fit in the current group. Set up a few
2794 // things for the next group. owner_ and output_section_ will be
2795 // set later after we've retrieved those values for the current
2797 this->state_
= FINDING_STUB_SECTION
;
2798 this->group_size_
= group_size
;
2799 this->group_start_addr_
= start_addr
;
2803 // Look over all the input sections, deciding where to place stubs.
2805 template<int size
, bool big_endian
>
2807 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
2809 bool no_size_errors
)
2811 Stub_control
stub_control(this->stub_group_size_
, no_size_errors
,
2812 parameters
->options().stub_group_multi());
2814 // Group input sections and insert stub table
2815 Stub_table_owner
* table_owner
= NULL
;
2816 std::vector
<Stub_table_owner
*> tables
;
2817 Layout::Section_list section_list
;
2818 layout
->get_executable_sections(§ion_list
);
2819 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
2820 for (Layout::Section_list::iterator o
= section_list
.begin();
2821 o
!= section_list
.end();
2824 typedef Output_section::Input_section_list Input_section_list
;
2825 for (Input_section_list::const_iterator i
2826 = (*o
)->input_sections().begin();
2827 i
!= (*o
)->input_sections().end();
2830 if (i
->is_input_section()
2831 || i
->is_relaxed_input_section())
2833 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2834 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2835 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
2836 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
2838 table_owner
->output_section
= stub_control
.output_section();
2839 table_owner
->owner
= stub_control
.owner();
2840 stub_control
.set_output_and_owner(*o
, &*i
);
2843 if (table_owner
== NULL
)
2845 table_owner
= new Stub_table_owner
;
2846 tables
.push_back(table_owner
);
2848 ppcobj
->set_stub_table(i
->shndx(), tables
.size() - 1);
2852 if (table_owner
!= NULL
)
2854 table_owner
->output_section
= stub_control
.output_section();
2855 table_owner
->owner
= stub_control
.owner();;
2857 for (typename
std::vector
<Stub_table_owner
*>::iterator t
= tables
.begin();
2861 Stub_table
<size
, big_endian
>* stub_table
;
2863 if ((*t
)->owner
->is_input_section())
2864 stub_table
= new Stub_table
<size
, big_endian
>(this,
2865 (*t
)->output_section
,
2867 this->stub_tables_
.size());
2868 else if ((*t
)->owner
->is_relaxed_input_section())
2869 stub_table
= static_cast<Stub_table
<size
, big_endian
>*>(
2870 (*t
)->owner
->relaxed_input_section());
2873 this->stub_tables_
.push_back(stub_table
);
2878 static unsigned long
2879 max_branch_delta (unsigned int r_type
)
2881 if (r_type
== elfcpp::R_POWERPC_REL14
2882 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
2883 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
2885 if (r_type
== elfcpp::R_POWERPC_REL24
2886 || r_type
== elfcpp::R_PPC_PLTREL24
2887 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
2892 // If this branch needs a plt call stub, or a long branch stub, make one.
2894 template<int size
, bool big_endian
>
2896 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
2897 Stub_table
<size
, big_endian
>* stub_table
,
2898 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
2899 Symbol_table
* symtab
) const
2901 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
2902 if (sym
!= NULL
&& sym
->is_forwarder())
2903 sym
= symtab
->resolve_forwards(sym
);
2904 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
2905 Target_powerpc
<size
, big_endian
>* target
=
2906 static_cast<Target_powerpc
<size
, big_endian
>*>(
2907 parameters
->sized_target
<size
, big_endian
>());
2911 ? gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
2912 : this->object_
->local_has_plt_offset(this->r_sym_
))
2916 && target
->abiversion() >= 2
2917 && !parameters
->options().output_is_position_independent()
2918 && !is_branch_reloc(this->r_type_
))
2919 target
->glink_section()->add_global_entry(gsym
);
2922 if (stub_table
== NULL
)
2923 stub_table
= this->object_
->stub_table(this->shndx_
);
2924 if (stub_table
== NULL
)
2926 // This is a ref from a data section to an ifunc symbol.
2927 stub_table
= ifunc_stub_table
;
2929 gold_assert(stub_table
!= NULL
);
2930 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
2931 if (from
!= invalid_address
)
2932 from
+= (this->object_
->output_section(this->shndx_
)->address()
2935 ok
= stub_table
->add_plt_call_entry(from
,
2936 this->object_
, gsym
,
2937 this->r_type_
, this->addend_
);
2939 ok
= stub_table
->add_plt_call_entry(from
,
2940 this->object_
, this->r_sym_
,
2941 this->r_type_
, this->addend_
);
2946 Address max_branch_offset
= max_branch_delta(this->r_type_
);
2947 if (max_branch_offset
== 0)
2949 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
2950 gold_assert(from
!= invalid_address
);
2951 from
+= (this->object_
->output_section(this->shndx_
)->address()
2956 switch (gsym
->source())
2958 case Symbol::FROM_OBJECT
:
2960 Object
* symobj
= gsym
->object();
2961 if (symobj
->is_dynamic()
2962 || symobj
->pluginobj() != NULL
)
2965 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
2966 if (shndx
== elfcpp::SHN_UNDEF
)
2971 case Symbol::IS_UNDEFINED
:
2977 Symbol_table::Compute_final_value_status status
;
2978 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
2979 if (status
!= Symbol_table::CFVS_OK
)
2982 to
+= this->object_
->ppc64_local_entry_offset(gsym
);
2986 const Symbol_value
<size
>* psymval
2987 = this->object_
->local_symbol(this->r_sym_
);
2988 Symbol_value
<size
> symval
;
2989 if (psymval
->is_section_symbol())
2990 symval
.set_is_section_symbol();
2991 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
2992 typename
ObjType::Compute_final_local_value_status status
2993 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
2995 if (status
!= ObjType::CFLV_OK
2996 || !symval
.has_output_value())
2998 to
= symval
.value(this->object_
, 0);
3000 to
+= this->object_
->ppc64_local_entry_offset(this->r_sym_
);
3002 if (!(size
== 32 && this->r_type_
== elfcpp::R_PPC_PLTREL24
))
3003 to
+= this->addend_
;
3004 if (stub_table
== NULL
)
3005 stub_table
= this->object_
->stub_table(this->shndx_
);
3006 if (size
== 64 && target
->abiversion() < 2)
3008 unsigned int dest_shndx
;
3009 if (!target
->symval_for_branch(symtab
, gsym
, this->object_
,
3013 Address delta
= to
- from
;
3014 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
3016 if (stub_table
== NULL
)
3018 gold_warning(_("%s:%s: branch in non-executable section,"
3019 " no long branch stub for you"),
3020 this->object_
->name().c_str(),
3021 this->object_
->section_name(this->shndx_
).c_str());
3024 bool save_res
= (size
== 64
3026 && gsym
->source() == Symbol::IN_OUTPUT_DATA
3027 && gsym
->output_data() == target
->savres_section());
3028 ok
= stub_table
->add_long_branch_entry(this->object_
,
3030 from
, to
, save_res
);
3034 gold_debug(DEBUG_TARGET
,
3035 "branch at %s:%s+%#lx\n"
3036 "can't reach stub attached to %s:%s",
3037 this->object_
->name().c_str(),
3038 this->object_
->section_name(this->shndx_
).c_str(),
3039 (unsigned long) this->offset_
,
3040 stub_table
->relobj()->name().c_str(),
3041 stub_table
->relobj()->section_name(stub_table
->shndx()).c_str());
3046 // Relaxation hook. This is where we do stub generation.
3048 template<int size
, bool big_endian
>
3050 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
3051 const Input_objects
*,
3052 Symbol_table
* symtab
,
3056 unsigned int prev_brlt_size
= 0;
3060 = this->abiversion() < 2 && parameters
->options().plt_thread_safe();
3062 && this->abiversion() < 2
3064 && !parameters
->options().user_set_plt_thread_safe())
3066 static const char* const thread_starter
[] =
3070 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3072 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3073 "mq_notify", "create_timer",
3078 "GOMP_parallel_start",
3079 "GOMP_parallel_loop_static",
3080 "GOMP_parallel_loop_static_start",
3081 "GOMP_parallel_loop_dynamic",
3082 "GOMP_parallel_loop_dynamic_start",
3083 "GOMP_parallel_loop_guided",
3084 "GOMP_parallel_loop_guided_start",
3085 "GOMP_parallel_loop_runtime",
3086 "GOMP_parallel_loop_runtime_start",
3087 "GOMP_parallel_sections",
3088 "GOMP_parallel_sections_start",
3093 if (parameters
->options().shared())
3097 for (unsigned int i
= 0;
3098 i
< sizeof(thread_starter
) / sizeof(thread_starter
[0]);
3101 Symbol
* sym
= symtab
->lookup(thread_starter
[i
], NULL
);
3102 thread_safe
= (sym
!= NULL
3104 && sym
->in_real_elf());
3110 this->plt_thread_safe_
= thread_safe
;
3115 this->stub_group_size_
= parameters
->options().stub_group_size();
3116 bool no_size_errors
= true;
3117 if (this->stub_group_size_
== 1)
3118 this->stub_group_size_
= 0x1c00000;
3119 else if (this->stub_group_size_
== -1)
3120 this->stub_group_size_
= -0x1e00000;
3122 no_size_errors
= false;
3123 this->group_sections(layout
, task
, no_size_errors
);
3125 else if (this->relax_failed_
&& this->relax_fail_count_
< 3)
3127 this->branch_lookup_table_
.clear();
3128 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3129 p
!= this->stub_tables_
.end();
3132 (*p
)->clear_stubs(true);
3134 this->stub_tables_
.clear();
3135 this->stub_group_size_
= this->stub_group_size_
/ 4 * 3;
3136 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3137 program_name
, this->stub_group_size_
);
3138 this->group_sections(layout
, task
, true);
3141 // We need address of stub tables valid for make_stub.
3142 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3143 p
!= this->stub_tables_
.end();
3146 const Powerpc_relobj
<size
, big_endian
>* object
3147 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
3148 Address off
= object
->get_output_section_offset((*p
)->shndx());
3149 gold_assert(off
!= invalid_address
);
3150 Output_section
* os
= (*p
)->output_section();
3151 (*p
)->set_address_and_size(os
, off
);
3156 // Clear plt call stubs, long branch stubs and branch lookup table.
3157 prev_brlt_size
= this->branch_lookup_table_
.size();
3158 this->branch_lookup_table_
.clear();
3159 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3160 p
!= this->stub_tables_
.end();
3163 (*p
)->clear_stubs(false);
3167 // Build all the stubs.
3168 this->relax_failed_
= false;
3169 Stub_table
<size
, big_endian
>* ifunc_stub_table
3170 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
3171 Stub_table
<size
, big_endian
>* one_stub_table
3172 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
3173 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
3174 b
!= this->branch_info_
.end();
3177 if (!b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
)
3178 && !this->relax_failed_
)
3180 this->relax_failed_
= true;
3181 this->relax_fail_count_
++;
3182 if (this->relax_fail_count_
< 3)
3187 // Did anything change size?
3188 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
3189 bool again
= num_huge_branches
!= prev_brlt_size
;
3190 if (size
== 64 && num_huge_branches
!= 0)
3191 this->make_brlt_section(layout
);
3192 if (size
== 64 && again
)
3193 this->brlt_section_
->set_current_size(num_huge_branches
);
3195 typedef Unordered_set
<Output_section
*> Output_sections
;
3196 Output_sections os_need_update
;
3197 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3198 p
!= this->stub_tables_
.end();
3201 if ((*p
)->size_update())
3204 (*p
)->add_eh_frame(layout
);
3205 os_need_update
.insert((*p
)->output_section());
3209 // Set output section offsets for all input sections in an output
3210 // section that just changed size. Anything past the stubs will
3212 for (typename
Output_sections::iterator p
= os_need_update
.begin();
3213 p
!= os_need_update
.end();
3216 Output_section
* os
= *p
;
3218 typedef Output_section::Input_section_list Input_section_list
;
3219 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
3220 i
!= os
->input_sections().end();
3223 off
= align_address(off
, i
->addralign());
3224 if (i
->is_input_section() || i
->is_relaxed_input_section())
3225 i
->relobj()->set_section_offset(i
->shndx(), off
);
3226 if (i
->is_relaxed_input_section())
3228 Stub_table
<size
, big_endian
>* stub_table
3229 = static_cast<Stub_table
<size
, big_endian
>*>(
3230 i
->relaxed_input_section());
3231 Address stub_table_size
= stub_table
->set_address_and_size(os
, off
);
3232 off
+= stub_table_size
;
3233 // After a few iterations, set current stub table size
3234 // as min size threshold, so later stub tables can only
3237 stub_table
->set_min_size_threshold(stub_table_size
);
3240 off
+= i
->data_size();
3242 // If .branch_lt is part of this output section, then we have
3243 // just done the offset adjustment.
3244 os
->clear_section_offsets_need_adjustment();
3249 && num_huge_branches
!= 0
3250 && parameters
->options().output_is_position_independent())
3252 // Fill in the BRLT relocs.
3253 this->brlt_section_
->reset_brlt_sizes();
3254 for (typename
Branch_lookup_table::const_iterator p
3255 = this->branch_lookup_table_
.begin();
3256 p
!= this->branch_lookup_table_
.end();
3259 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
3261 this->brlt_section_
->finalize_brlt_sizes();
3265 && (parameters
->options().user_set_emit_stub_syms()
3266 ? parameters
->options().emit_stub_syms()
3268 || parameters
->options().output_is_position_independent()
3269 || parameters
->options().emit_relocs())))
3271 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3272 p
!= this->stub_tables_
.end();
3274 (*p
)->define_stub_syms(symtab
);
3276 if (this->glink_
!= NULL
)
3278 int stub_size
= this->glink_
->pltresolve_size
;
3279 Address value
= -stub_size
;
3285 this->define_local(symtab
, "__glink_PLTresolve",
3286 this->glink_
, value
, stub_size
);
3289 this->define_local(symtab
, "__glink", this->glink_
, 0, 0);
3296 template<int size
, bool big_endian
>
3298 Target_powerpc
<size
, big_endian
>::do_plt_fde_location(const Output_data
* plt
,
3299 unsigned char* oview
,
3303 uint64_t address
= plt
->address();
3304 off_t len
= plt
->data_size();
3306 if (plt
== this->glink_
)
3308 // See Output_data_glink::do_write() for glink contents.
3311 gold_assert(parameters
->doing_static_link());
3312 // Static linking may need stubs, to support ifunc and long
3313 // branches. We need to create an output section for
3314 // .eh_frame early in the link process, to have a place to
3315 // attach stub .eh_frame info. We also need to have
3316 // registered a CIE that matches the stub CIE. Both of
3317 // these requirements are satisfied by creating an FDE and
3318 // CIE for .glink, even though static linking will leave
3319 // .glink zero length.
3320 // ??? Hopefully generating an FDE with a zero address range
3321 // won't confuse anything that consumes .eh_frame info.
3323 else if (size
== 64)
3325 // There is one word before __glink_PLTresolve
3329 else if (parameters
->options().output_is_position_independent())
3331 // There are two FDEs for a position independent glink.
3332 // The first covers the branch table, the second
3333 // __glink_PLTresolve at the end of glink.
3334 off_t resolve_size
= this->glink_
->pltresolve_size
;
3335 if (oview
[9] == elfcpp::DW_CFA_nop
)
3336 len
-= resolve_size
;
3339 address
+= len
- resolve_size
;
3346 // Must be a stub table.
3347 const Stub_table
<size
, big_endian
>* stub_table
3348 = static_cast<const Stub_table
<size
, big_endian
>*>(plt
);
3349 uint64_t stub_address
= stub_table
->stub_address();
3350 len
-= stub_address
- address
;
3351 address
= stub_address
;
3354 *paddress
= address
;
3358 // A class to handle the PLT data.
3360 template<int size
, bool big_endian
>
3361 class Output_data_plt_powerpc
: public Output_section_data_build
3364 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3365 size
, big_endian
> Reloc_section
;
3367 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3368 Reloc_section
* plt_rel
,
3370 : Output_section_data_build(size
== 32 ? 4 : 8),
3376 // Add an entry to the PLT.
3381 add_ifunc_entry(Symbol
*);
3384 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
3386 // Return the .rela.plt section data.
3393 // Return the number of PLT entries.
3397 if (this->current_data_size() == 0)
3399 return ((this->current_data_size() - this->first_plt_entry_offset())
3400 / this->plt_entry_size());
3405 do_adjust_output_section(Output_section
* os
)
3410 // Write to a map file.
3412 do_print_to_mapfile(Mapfile
* mapfile
) const
3413 { mapfile
->print_output_data(this, this->name_
); }
3416 // Return the offset of the first non-reserved PLT entry.
3418 first_plt_entry_offset() const
3420 // IPLT has no reserved entry.
3421 if (this->name_
[3] == 'I')
3423 return this->targ_
->first_plt_entry_offset();
3426 // Return the size of each PLT entry.
3428 plt_entry_size() const
3430 return this->targ_
->plt_entry_size();
3433 // Write out the PLT data.
3435 do_write(Output_file
*);
3437 // The reloc section.
3438 Reloc_section
* rel_
;
3439 // Allows access to .glink for do_write.
3440 Target_powerpc
<size
, big_endian
>* targ_
;
3441 // What to report in map file.
3445 // Add an entry to the PLT.
3447 template<int size
, bool big_endian
>
3449 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
3451 if (!gsym
->has_plt_offset())
3453 section_size_type off
= this->current_data_size();
3455 off
+= this->first_plt_entry_offset();
3456 gsym
->set_plt_offset(off
);
3457 gsym
->set_needs_dynsym_entry();
3458 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
3459 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
3460 off
+= this->plt_entry_size();
3461 this->set_current_data_size(off
);
3465 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3467 template<int size
, bool big_endian
>
3469 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
3471 if (!gsym
->has_plt_offset())
3473 section_size_type off
= this->current_data_size();
3474 gsym
->set_plt_offset(off
);
3475 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3476 if (size
== 64 && this->targ_
->abiversion() < 2)
3477 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3478 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
3479 off
+= this->plt_entry_size();
3480 this->set_current_data_size(off
);
3484 // Add an entry for a local ifunc symbol to the IPLT.
3486 template<int size
, bool big_endian
>
3488 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
3489 Sized_relobj_file
<size
, big_endian
>* relobj
,
3490 unsigned int local_sym_index
)
3492 if (!relobj
->local_has_plt_offset(local_sym_index
))
3494 section_size_type off
= this->current_data_size();
3495 relobj
->set_local_plt_offset(local_sym_index
, off
);
3496 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3497 if (size
== 64 && this->targ_
->abiversion() < 2)
3498 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3499 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
3501 off
+= this->plt_entry_size();
3502 this->set_current_data_size(off
);
3506 static const uint32_t add_0_11_11
= 0x7c0b5a14;
3507 static const uint32_t add_2_2_11
= 0x7c425a14;
3508 static const uint32_t add_2_2_12
= 0x7c426214;
3509 static const uint32_t add_3_3_2
= 0x7c631214;
3510 static const uint32_t add_3_3_13
= 0x7c636a14;
3511 static const uint32_t add_11_0_11
= 0x7d605a14;
3512 static const uint32_t add_11_2_11
= 0x7d625a14;
3513 static const uint32_t add_11_11_2
= 0x7d6b1214;
3514 static const uint32_t addi_0_12
= 0x380c0000;
3515 static const uint32_t addi_2_2
= 0x38420000;
3516 static const uint32_t addi_3_3
= 0x38630000;
3517 static const uint32_t addi_11_11
= 0x396b0000;
3518 static const uint32_t addi_12_1
= 0x39810000;
3519 static const uint32_t addi_12_12
= 0x398c0000;
3520 static const uint32_t addis_0_2
= 0x3c020000;
3521 static const uint32_t addis_0_13
= 0x3c0d0000;
3522 static const uint32_t addis_2_12
= 0x3c4c0000;
3523 static const uint32_t addis_11_2
= 0x3d620000;
3524 static const uint32_t addis_11_11
= 0x3d6b0000;
3525 static const uint32_t addis_11_30
= 0x3d7e0000;
3526 static const uint32_t addis_12_1
= 0x3d810000;
3527 static const uint32_t addis_12_2
= 0x3d820000;
3528 static const uint32_t addis_12_12
= 0x3d8c0000;
3529 static const uint32_t b
= 0x48000000;
3530 static const uint32_t bcl_20_31
= 0x429f0005;
3531 static const uint32_t bctr
= 0x4e800420;
3532 static const uint32_t blr
= 0x4e800020;
3533 static const uint32_t bnectr_p4
= 0x4ce20420;
3534 static const uint32_t cmpld_7_12_0
= 0x7fac0040;
3535 static const uint32_t cmpldi_2_0
= 0x28220000;
3536 static const uint32_t cror_15_15_15
= 0x4def7b82;
3537 static const uint32_t cror_31_31_31
= 0x4ffffb82;
3538 static const uint32_t ld_0_1
= 0xe8010000;
3539 static const uint32_t ld_0_12
= 0xe80c0000;
3540 static const uint32_t ld_2_1
= 0xe8410000;
3541 static const uint32_t ld_2_2
= 0xe8420000;
3542 static const uint32_t ld_2_11
= 0xe84b0000;
3543 static const uint32_t ld_2_12
= 0xe84c0000;
3544 static const uint32_t ld_11_2
= 0xe9620000;
3545 static const uint32_t ld_11_11
= 0xe96b0000;
3546 static const uint32_t ld_12_2
= 0xe9820000;
3547 static const uint32_t ld_12_11
= 0xe98b0000;
3548 static const uint32_t ld_12_12
= 0xe98c0000;
3549 static const uint32_t lfd_0_1
= 0xc8010000;
3550 static const uint32_t li_0_0
= 0x38000000;
3551 static const uint32_t li_12_0
= 0x39800000;
3552 static const uint32_t lis_0
= 0x3c000000;
3553 static const uint32_t lis_2
= 0x3c400000;
3554 static const uint32_t lis_11
= 0x3d600000;
3555 static const uint32_t lis_12
= 0x3d800000;
3556 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
3557 static const uint32_t lwz_0_12
= 0x800c0000;
3558 static const uint32_t lwz_11_11
= 0x816b0000;
3559 static const uint32_t lwz_11_30
= 0x817e0000;
3560 static const uint32_t lwz_12_12
= 0x818c0000;
3561 static const uint32_t lwzu_0_12
= 0x840c0000;
3562 static const uint32_t mflr_0
= 0x7c0802a6;
3563 static const uint32_t mflr_11
= 0x7d6802a6;
3564 static const uint32_t mflr_12
= 0x7d8802a6;
3565 static const uint32_t mtctr_0
= 0x7c0903a6;
3566 static const uint32_t mtctr_11
= 0x7d6903a6;
3567 static const uint32_t mtctr_12
= 0x7d8903a6;
3568 static const uint32_t mtlr_0
= 0x7c0803a6;
3569 static const uint32_t mtlr_12
= 0x7d8803a6;
3570 static const uint32_t nop
= 0x60000000;
3571 static const uint32_t ori_0_0_0
= 0x60000000;
3572 static const uint32_t srdi_0_0_2
= 0x7800f082;
3573 static const uint32_t std_0_1
= 0xf8010000;
3574 static const uint32_t std_0_12
= 0xf80c0000;
3575 static const uint32_t std_2_1
= 0xf8410000;
3576 static const uint32_t stfd_0_1
= 0xd8010000;
3577 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
3578 static const uint32_t sub_11_11_12
= 0x7d6c5850;
3579 static const uint32_t sub_12_12_11
= 0x7d8b6050;
3580 static const uint32_t xor_2_12_12
= 0x7d826278;
3581 static const uint32_t xor_11_12_12
= 0x7d8b6278;
3583 // Write out the PLT.
3585 template<int size
, bool big_endian
>
3587 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3589 if (size
== 32 && this->name_
[3] != 'I')
3591 const section_size_type offset
= this->offset();
3592 const section_size_type oview_size
3593 = convert_to_section_size_type(this->data_size());
3594 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3595 unsigned char* pov
= oview
;
3596 unsigned char* endpov
= oview
+ oview_size
;
3598 // The address of the .glink branch table
3599 const Output_data_glink
<size
, big_endian
>* glink
3600 = this->targ_
->glink_section();
3601 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
3603 while (pov
< endpov
)
3605 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
3610 of
->write_output_view(offset
, oview_size
, oview
);
3614 // Create the PLT section.
3616 template<int size
, bool big_endian
>
3618 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
3621 if (this->plt_
== NULL
)
3623 if (this->got_
== NULL
)
3624 this->got_section(symtab
, layout
);
3626 if (this->glink_
== NULL
)
3627 make_glink_section(layout
);
3629 // Ensure that .rela.dyn always appears before .rela.plt This is
3630 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3631 // needs to include .rela.plt in its range.
3632 this->rela_dyn_section(layout
);
3634 Reloc_section
* plt_rel
= new Reloc_section(false);
3635 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
3636 elfcpp::SHF_ALLOC
, plt_rel
,
3637 ORDER_DYNAMIC_PLT_RELOCS
, false);
3639 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
3641 layout
->add_output_section_data(".plt",
3643 ? elfcpp::SHT_PROGBITS
3644 : elfcpp::SHT_NOBITS
),
3645 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3652 Output_section
* rela_plt_os
= plt_rel
->output_section();
3653 rela_plt_os
->set_info_section(this->plt_
->output_section());
3657 // Create the IPLT section.
3659 template<int size
, bool big_endian
>
3661 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
3664 if (this->iplt_
== NULL
)
3666 this->make_plt_section(symtab
, layout
);
3668 Reloc_section
* iplt_rel
= new Reloc_section(false);
3669 if (this->rela_dyn_
->output_section())
3670 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
3672 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
3674 if (this->plt_
->output_section())
3675 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
3679 // A section for huge long branch addresses, similar to plt section.
3681 template<int size
, bool big_endian
>
3682 class Output_data_brlt_powerpc
: public Output_section_data_build
3685 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3686 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3687 size
, big_endian
> Reloc_section
;
3689 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3690 Reloc_section
* brlt_rel
)
3691 : Output_section_data_build(size
== 32 ? 4 : 8),
3699 this->reset_data_size();
3700 this->rel_
->reset_data_size();
3704 finalize_brlt_sizes()
3706 this->finalize_data_size();
3707 this->rel_
->finalize_data_size();
3710 // Add a reloc for an entry in the BRLT.
3712 add_reloc(Address to
, unsigned int off
)
3713 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
3715 // Update section and reloc section size.
3717 set_current_size(unsigned int num_branches
)
3719 this->reset_address_and_file_offset();
3720 this->set_current_data_size(num_branches
* 16);
3721 this->finalize_data_size();
3722 Output_section
* os
= this->output_section();
3723 os
->set_section_offsets_need_adjustment();
3724 if (this->rel_
!= NULL
)
3726 const unsigned int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
3727 this->rel_
->reset_address_and_file_offset();
3728 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
3729 this->rel_
->finalize_data_size();
3730 Output_section
* os
= this->rel_
->output_section();
3731 os
->set_section_offsets_need_adjustment();
3737 do_adjust_output_section(Output_section
* os
)
3742 // Write to a map file.
3744 do_print_to_mapfile(Mapfile
* mapfile
) const
3745 { mapfile
->print_output_data(this, "** BRLT"); }
3748 // Write out the BRLT data.
3750 do_write(Output_file
*);
3752 // The reloc section.
3753 Reloc_section
* rel_
;
3754 Target_powerpc
<size
, big_endian
>* targ_
;
3757 // Make the branch lookup table section.
3759 template<int size
, bool big_endian
>
3761 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
3763 if (size
== 64 && this->brlt_section_
== NULL
)
3765 Reloc_section
* brlt_rel
= NULL
;
3766 bool is_pic
= parameters
->options().output_is_position_independent();
3769 // When PIC we can't fill in .branch_lt (like .plt it can be
3770 // a bss style section) but must initialise at runtime via
3771 // dynamic relocations.
3772 this->rela_dyn_section(layout
);
3773 brlt_rel
= new Reloc_section(false);
3774 if (this->rela_dyn_
->output_section())
3775 this->rela_dyn_
->output_section()
3776 ->add_output_section_data(brlt_rel
);
3779 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
3780 if (this->plt_
&& is_pic
&& this->plt_
->output_section())
3781 this->plt_
->output_section()
3782 ->add_output_section_data(this->brlt_section_
);
3784 layout
->add_output_section_data(".branch_lt",
3785 (is_pic
? elfcpp::SHT_NOBITS
3786 : elfcpp::SHT_PROGBITS
),
3787 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3788 this->brlt_section_
,
3789 (is_pic
? ORDER_SMALL_BSS
3790 : ORDER_SMALL_DATA
),
3795 // Write out .branch_lt when non-PIC.
3797 template<int size
, bool big_endian
>
3799 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3801 if (size
== 64 && !parameters
->options().output_is_position_independent())
3803 const section_size_type offset
= this->offset();
3804 const section_size_type oview_size
3805 = convert_to_section_size_type(this->data_size());
3806 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3808 this->targ_
->write_branch_lookup_table(oview
);
3809 of
->write_output_view(offset
, oview_size
, oview
);
3813 static inline uint32_t
3819 static inline uint32_t
3825 static inline uint32_t
3828 return hi(a
+ 0x8000);
3834 static const unsigned char eh_frame_cie
[12];
3838 const unsigned char Eh_cie
<size
>::eh_frame_cie
[] =
3841 'z', 'R', 0, // Augmentation string.
3842 4, // Code alignment.
3843 0x80 - size
/ 8 , // Data alignment.
3845 1, // Augmentation size.
3846 (elfcpp::DW_EH_PE_pcrel
3847 | elfcpp::DW_EH_PE_sdata4
), // FDE encoding.
3848 elfcpp::DW_CFA_def_cfa
, 1, 0 // def_cfa: r1 offset 0.
3851 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3852 static const unsigned char glink_eh_frame_fde_64v1
[] =
3854 0, 0, 0, 0, // Replaced with offset to .glink.
3855 0, 0, 0, 0, // Replaced with size of .glink.
3856 0, // Augmentation size.
3857 elfcpp::DW_CFA_advance_loc
+ 1,
3858 elfcpp::DW_CFA_register
, 65, 12,
3859 elfcpp::DW_CFA_advance_loc
+ 4,
3860 elfcpp::DW_CFA_restore_extended
, 65
3863 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
3864 static const unsigned char glink_eh_frame_fde_64v2
[] =
3866 0, 0, 0, 0, // Replaced with offset to .glink.
3867 0, 0, 0, 0, // Replaced with size of .glink.
3868 0, // Augmentation size.
3869 elfcpp::DW_CFA_advance_loc
+ 1,
3870 elfcpp::DW_CFA_register
, 65, 0,
3871 elfcpp::DW_CFA_advance_loc
+ 4,
3872 elfcpp::DW_CFA_restore_extended
, 65
3875 // Describe __glink_PLTresolve use of LR, 32-bit version.
3876 static const unsigned char glink_eh_frame_fde_32
[] =
3878 0, 0, 0, 0, // Replaced with offset to .glink.
3879 0, 0, 0, 0, // Replaced with size of .glink.
3880 0, // Augmentation size.
3881 elfcpp::DW_CFA_advance_loc
+ 2,
3882 elfcpp::DW_CFA_register
, 65, 0,
3883 elfcpp::DW_CFA_advance_loc
+ 4,
3884 elfcpp::DW_CFA_restore_extended
, 65
3887 static const unsigned char default_fde
[] =
3889 0, 0, 0, 0, // Replaced with offset to stubs.
3890 0, 0, 0, 0, // Replaced with size of stubs.
3891 0, // Augmentation size.
3892 elfcpp::DW_CFA_nop
, // Pad.
3897 template<bool big_endian
>
3899 write_insn(unsigned char* p
, uint32_t v
)
3901 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
3904 // Stub_table holds information about plt and long branch stubs.
3905 // Stubs are built in an area following some input section determined
3906 // by group_sections(). This input section is converted to a relaxed
3907 // input section allowing it to be resized to accommodate the stubs
3909 template<int size
, bool big_endian
>
3910 class Stub_table
: public Output_relaxed_input_section
3913 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3914 static const Address invalid_address
= static_cast<Address
>(0) - 1;
3916 Stub_table(Target_powerpc
<size
, big_endian
>* targ
,
3917 Output_section
* output_section
,
3918 const Output_section::Input_section
* owner
,
3920 : Output_relaxed_input_section(owner
->relobj(), owner
->shndx(),
3922 ->section_addralign(owner
->shndx())),
3923 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
3924 orig_data_size_(owner
->current_data_size()),
3925 plt_size_(0), last_plt_size_(0),
3926 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
3927 eh_frame_added_(false), need_save_res_(false), uniq_(id
)
3929 this->set_output_section(output_section
);
3931 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
3932 new_relaxed
.push_back(this);
3933 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
3936 // Add a plt call stub.
3938 add_plt_call_entry(Address
,
3939 const Sized_relobj_file
<size
, big_endian
>*,
3945 add_plt_call_entry(Address
,
3946 const Sized_relobj_file
<size
, big_endian
>*,
3951 // Find a given plt call stub.
3953 find_plt_call_entry(const Symbol
*) const;
3956 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3957 unsigned int) const;
3960 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3966 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3971 // Add a long branch stub.
3973 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
3974 unsigned int, Address
, Address
, bool);
3977 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
3981 can_reach_stub(Address from
, unsigned int off
, unsigned int r_type
)
3983 Address max_branch_offset
= max_branch_delta(r_type
);
3984 if (max_branch_offset
== 0)
3986 gold_assert(from
!= invalid_address
);
3987 Address loc
= off
+ this->stub_address();
3988 return loc
- from
+ max_branch_offset
< 2 * max_branch_offset
;
3992 clear_stubs(bool all
)
3994 this->plt_call_stubs_
.clear();
3995 this->plt_size_
= 0;
3996 this->long_branch_stubs_
.clear();
3997 this->branch_size_
= 0;
3998 this->need_save_res_
= false;
4001 this->last_plt_size_
= 0;
4002 this->last_branch_size_
= 0;
4007 set_address_and_size(const Output_section
* os
, Address off
)
4009 Address start_off
= off
;
4010 off
+= this->orig_data_size_
;
4011 Address my_size
= this->plt_size_
+ this->branch_size_
;
4012 if (this->need_save_res_
)
4013 my_size
+= this->targ_
->savres_section()->data_size();
4015 off
= align_address(off
, this->stub_align());
4016 // Include original section size and alignment padding in size
4017 my_size
+= off
- start_off
;
4018 // Ensure new size is always larger than min size
4019 // threshold. Alignment requirement is included in "my_size", so
4020 // increase "my_size" does not invalidate alignment.
4021 if (my_size
< this->min_size_threshold_
)
4022 my_size
= this->min_size_threshold_
;
4023 this->reset_address_and_file_offset();
4024 this->set_current_data_size(my_size
);
4025 this->set_address_and_file_offset(os
->address() + start_off
,
4026 os
->offset() + start_off
);
4031 stub_address() const
4033 return align_address(this->address() + this->orig_data_size_
,
4034 this->stub_align());
4040 return align_address(this->offset() + this->orig_data_size_
,
4041 this->stub_align());
4046 { return this->plt_size_
; }
4049 set_min_size_threshold(Address min_size
)
4050 { this->min_size_threshold_
= min_size
; }
4053 define_stub_syms(Symbol_table
*);
4058 Output_section
* os
= this->output_section();
4059 if (os
->addralign() < this->stub_align())
4061 os
->set_addralign(this->stub_align());
4062 // FIXME: get rid of the insane checkpointing.
4063 // We can't increase alignment of the input section to which
4064 // stubs are attached; The input section may be .init which
4065 // is pasted together with other .init sections to form a
4066 // function. Aligning might insert zero padding resulting in
4067 // sigill. However we do need to increase alignment of the
4068 // output section so that the align_address() on offset in
4069 // set_address_and_size() adds the same padding as the
4070 // align_address() on address in stub_address().
4071 // What's more, we need this alignment for the layout done in
4072 // relaxation_loop_body() so that the output section starts at
4073 // a suitably aligned address.
4074 os
->checkpoint_set_addralign(this->stub_align());
4076 if (this->last_plt_size_
!= this->plt_size_
4077 || this->last_branch_size_
!= this->branch_size_
)
4079 this->last_plt_size_
= this->plt_size_
;
4080 this->last_branch_size_
= this->branch_size_
;
4086 // Add .eh_frame info for this stub section. Unlike other linker
4087 // generated .eh_frame this is added late in the link, because we
4088 // only want the .eh_frame info if this particular stub section is
4091 add_eh_frame(Layout
* layout
)
4093 if (!this->eh_frame_added_
)
4095 if (!parameters
->options().ld_generated_unwind_info())
4098 // Since we add stub .eh_frame info late, it must be placed
4099 // after all other linker generated .eh_frame info so that
4100 // merge mapping need not be updated for input sections.
4101 // There is no provision to use a different CIE to that used
4103 if (!this->targ_
->has_glink())
4106 layout
->add_eh_frame_for_plt(this,
4107 Eh_cie
<size
>::eh_frame_cie
,
4108 sizeof (Eh_cie
<size
>::eh_frame_cie
),
4110 sizeof (default_fde
));
4111 this->eh_frame_added_
= true;
4115 Target_powerpc
<size
, big_endian
>*
4121 class Plt_stub_ent_hash
;
4122 typedef Unordered_map
<Plt_stub_ent
, unsigned int,
4123 Plt_stub_ent_hash
> Plt_stub_entries
;
4124 class Branch_stub_ent
;
4125 class Branch_stub_ent_hash
;
4126 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
4127 Branch_stub_ent_hash
> Branch_stub_entries
;
4129 // Alignment of stub section.
4135 unsigned int min_align
= 32;
4136 unsigned int user_align
= 1 << parameters
->options().plt_align();
4137 return std::max(user_align
, min_align
);
4140 // Return the plt offset for the given call stub.
4142 plt_off(typename
Plt_stub_entries::const_iterator p
, bool* is_iplt
) const
4144 const Symbol
* gsym
= p
->first
.sym_
;
4147 *is_iplt
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
4148 && gsym
->can_use_relative_reloc(false));
4149 return gsym
->plt_offset();
4154 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
4155 unsigned int local_sym_index
= p
->first
.locsym_
;
4156 return relobj
->local_plt_offset(local_sym_index
);
4160 // Size of a given plt call stub.
4162 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
4168 Address plt_addr
= this->plt_off(p
, &is_iplt
);
4170 plt_addr
+= this->targ_
->iplt_section()->address();
4172 plt_addr
+= this->targ_
->plt_section()->address();
4173 Address got_addr
= this->targ_
->got_section()->output_section()->address();
4174 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4175 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
4176 got_addr
+= ppcobj
->toc_base_offset();
4177 Address off
= plt_addr
- got_addr
;
4178 unsigned int bytes
= 4 * 4 + 4 * (ha(off
) != 0);
4179 if (this->targ_
->abiversion() < 2)
4181 bool static_chain
= parameters
->options().plt_static_chain();
4182 bool thread_safe
= this->targ_
->plt_thread_safe();
4186 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
4188 unsigned int align
= 1 << parameters
->options().plt_align();
4190 bytes
= (bytes
+ align
- 1) & -align
;
4194 // Return long branch stub size.
4196 branch_stub_size(typename
Branch_stub_entries::const_iterator p
)
4198 Address loc
= this->stub_address() + this->last_plt_size_
+ p
->second
;
4199 if (p
->first
.dest_
- loc
+ (1 << 25) < 2 << 25)
4201 if (size
== 64 || !parameters
->options().output_is_position_independent())
4208 do_write(Output_file
*);
4210 // Plt call stub keys.
4214 Plt_stub_ent(const Symbol
* sym
)
4215 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4218 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
4219 unsigned int locsym_index
)
4220 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4223 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
4225 unsigned int r_type
,
4227 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4230 this->addend_
= addend
;
4231 else if (parameters
->options().output_is_position_independent()
4232 && r_type
== elfcpp::R_PPC_PLTREL24
)
4234 this->addend_
= addend
;
4235 if (this->addend_
>= 32768)
4236 this->object_
= object
;
4240 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
4241 unsigned int locsym_index
,
4242 unsigned int r_type
,
4244 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4247 this->addend_
= addend
;
4248 else if (parameters
->options().output_is_position_independent()
4249 && r_type
== elfcpp::R_PPC_PLTREL24
)
4250 this->addend_
= addend
;
4253 bool operator==(const Plt_stub_ent
& that
) const
4255 return (this->sym_
== that
.sym_
4256 && this->object_
== that
.object_
4257 && this->addend_
== that
.addend_
4258 && this->locsym_
== that
.locsym_
);
4262 const Sized_relobj_file
<size
, big_endian
>* object_
;
4263 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
4264 unsigned int locsym_
;
4268 class Plt_stub_ent_hash
4271 size_t operator()(const Plt_stub_ent
& ent
) const
4273 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
4274 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
4280 // Long branch stub keys.
4281 class Branch_stub_ent
4284 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
,
4285 Address to
, bool save_res
)
4286 : dest_(to
), toc_base_off_(0), save_res_(save_res
)
4289 toc_base_off_
= obj
->toc_base_offset();
4292 bool operator==(const Branch_stub_ent
& that
) const
4294 return (this->dest_
== that
.dest_
4296 || this->toc_base_off_
== that
.toc_base_off_
));
4300 unsigned int toc_base_off_
;
4304 class Branch_stub_ent_hash
4307 size_t operator()(const Branch_stub_ent
& ent
) const
4308 { return ent
.dest_
^ ent
.toc_base_off_
; }
4311 // In a sane world this would be a global.
4312 Target_powerpc
<size
, big_endian
>* targ_
;
4313 // Map sym/object/addend to stub offset.
4314 Plt_stub_entries plt_call_stubs_
;
4315 // Map destination address to stub offset.
4316 Branch_stub_entries long_branch_stubs_
;
4317 // size of input section
4318 section_size_type orig_data_size_
;
4320 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
4321 // Some rare cases cause (PR/20529) fluctuation in stub table
4322 // size, which leads to an endless relax loop. This is to be fixed
4323 // by, after the first few iterations, allowing only increase of
4324 // stub table size. This variable sets the minimal possible size of
4325 // a stub table, it is zero for the first few iterations, then
4326 // increases monotonically.
4327 Address min_size_threshold_
;
4328 // Whether .eh_frame info has been created for this stub section.
4329 bool eh_frame_added_
;
4330 // Set if this stub group needs a copy of out-of-line register
4331 // save/restore functions.
4332 bool need_save_res_
;
4333 // Per stub table unique identifier.
4337 // Add a plt call stub, if we do not already have one for this
4338 // sym/object/addend combo.
4340 template<int size
, bool big_endian
>
4342 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4344 const Sized_relobj_file
<size
, big_endian
>* object
,
4346 unsigned int r_type
,
4349 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
4350 unsigned int off
= this->plt_size_
;
4351 ent
.indx_
= this->plt_call_stubs_
.size();
4352 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4353 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
4355 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
4356 return this->can_reach_stub(from
, off
, r_type
);
4359 template<int size
, bool big_endian
>
4361 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4363 const Sized_relobj_file
<size
, big_endian
>* object
,
4364 unsigned int locsym_index
,
4365 unsigned int r_type
,
4368 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
4369 unsigned int off
= this->plt_size_
;
4370 ent
.indx_
= this->plt_call_stubs_
.size();
4371 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4372 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
4374 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
4375 return this->can_reach_stub(from
, off
, r_type
);
4378 // Find a plt call stub.
4380 template<int size
, bool big_endian
>
4381 typename Stub_table
<size
, big_endian
>::Address
4382 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4383 const Sized_relobj_file
<size
, big_endian
>* object
,
4385 unsigned int r_type
,
4386 Address addend
) const
4388 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
4389 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
4390 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
4393 template<int size
, bool big_endian
>
4394 typename Stub_table
<size
, big_endian
>::Address
4395 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
4397 Plt_stub_ent
ent(gsym
);
4398 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
4399 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
4402 template<int size
, bool big_endian
>
4403 typename Stub_table
<size
, big_endian
>::Address
4404 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4405 const Sized_relobj_file
<size
, big_endian
>* object
,
4406 unsigned int locsym_index
,
4407 unsigned int r_type
,
4408 Address addend
) const
4410 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
4411 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
4412 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
4415 template<int size
, bool big_endian
>
4416 typename Stub_table
<size
, big_endian
>::Address
4417 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4418 const Sized_relobj_file
<size
, big_endian
>* object
,
4419 unsigned int locsym_index
) const
4421 Plt_stub_ent
ent(object
, locsym_index
);
4422 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
4423 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
4426 // Add a long branch stub if we don't already have one to given
4429 template<int size
, bool big_endian
>
4431 Stub_table
<size
, big_endian
>::add_long_branch_entry(
4432 const Powerpc_relobj
<size
, big_endian
>* object
,
4433 unsigned int r_type
,
4438 Branch_stub_ent
ent(object
, to
, save_res
);
4439 Address off
= this->branch_size_
;
4440 std::pair
<typename
Branch_stub_entries::iterator
, bool> p
4441 = this->long_branch_stubs_
.insert(std::make_pair(ent
, off
));
4445 this->need_save_res_
= true;
4448 unsigned int stub_size
= this->branch_stub_size(p
.first
);
4449 this->branch_size_
= off
+ stub_size
;
4450 if (size
== 64 && stub_size
!= 4)
4451 this->targ_
->add_branch_lookup_table(to
);
4454 return this->can_reach_stub(from
, off
, r_type
);
4457 // Find long branch stub offset.
4459 template<int size
, bool big_endian
>
4460 typename Stub_table
<size
, big_endian
>::Address
4461 Stub_table
<size
, big_endian
>::find_long_branch_entry(
4462 const Powerpc_relobj
<size
, big_endian
>* object
,
4465 Branch_stub_ent
ent(object
, to
, false);
4466 typename
Branch_stub_entries::const_iterator p
4467 = this->long_branch_stubs_
.find(ent
);
4468 if (p
== this->long_branch_stubs_
.end())
4469 return invalid_address
;
4470 if (p
->first
.save_res_
)
4471 return to
- this->targ_
->savres_section()->address() + this->branch_size_
;
4475 // A class to handle .glink.
4477 template<int size
, bool big_endian
>
4478 class Output_data_glink
: public Output_section_data
4481 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4482 static const Address invalid_address
= static_cast<Address
>(0) - 1;
4483 static const int pltresolve_size
= 16*4;
4485 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
4486 : Output_section_data(16), targ_(targ
), global_entry_stubs_(),
4487 end_branch_table_(), ge_size_(0)
4491 add_eh_frame(Layout
* layout
);
4494 add_global_entry(const Symbol
*);
4497 find_global_entry(const Symbol
*) const;
4500 global_entry_address() const
4502 gold_assert(this->is_data_size_valid());
4503 unsigned int global_entry_off
= (this->end_branch_table_
+ 15) & -16;
4504 return this->address() + global_entry_off
;
4508 // Write to a map file.
4510 do_print_to_mapfile(Mapfile
* mapfile
) const
4511 { mapfile
->print_output_data(this, _("** glink")); }
4515 set_final_data_size();
4519 do_write(Output_file
*);
4521 // Allows access to .got and .plt for do_write.
4522 Target_powerpc
<size
, big_endian
>* targ_
;
4524 // Map sym to stub offset.
4525 typedef Unordered_map
<const Symbol
*, unsigned int> Global_entry_stub_entries
;
4526 Global_entry_stub_entries global_entry_stubs_
;
4528 unsigned int end_branch_table_
, ge_size_
;
4531 template<int size
, bool big_endian
>
4533 Output_data_glink
<size
, big_endian
>::add_eh_frame(Layout
* layout
)
4535 if (!parameters
->options().ld_generated_unwind_info())
4540 if (this->targ_
->abiversion() < 2)
4541 layout
->add_eh_frame_for_plt(this,
4542 Eh_cie
<64>::eh_frame_cie
,
4543 sizeof (Eh_cie
<64>::eh_frame_cie
),
4544 glink_eh_frame_fde_64v1
,
4545 sizeof (glink_eh_frame_fde_64v1
));
4547 layout
->add_eh_frame_for_plt(this,
4548 Eh_cie
<64>::eh_frame_cie
,
4549 sizeof (Eh_cie
<64>::eh_frame_cie
),
4550 glink_eh_frame_fde_64v2
,
4551 sizeof (glink_eh_frame_fde_64v2
));
4555 // 32-bit .glink can use the default since the CIE return
4556 // address reg, LR, is valid.
4557 layout
->add_eh_frame_for_plt(this,
4558 Eh_cie
<32>::eh_frame_cie
,
4559 sizeof (Eh_cie
<32>::eh_frame_cie
),
4561 sizeof (default_fde
));
4562 // Except where LR is used in a PIC __glink_PLTresolve.
4563 if (parameters
->options().output_is_position_independent())
4564 layout
->add_eh_frame_for_plt(this,
4565 Eh_cie
<32>::eh_frame_cie
,
4566 sizeof (Eh_cie
<32>::eh_frame_cie
),
4567 glink_eh_frame_fde_32
,
4568 sizeof (glink_eh_frame_fde_32
));
4572 template<int size
, bool big_endian
>
4574 Output_data_glink
<size
, big_endian
>::add_global_entry(const Symbol
* gsym
)
4576 std::pair
<typename
Global_entry_stub_entries::iterator
, bool> p
4577 = this->global_entry_stubs_
.insert(std::make_pair(gsym
, this->ge_size_
));
4579 this->ge_size_
+= 16;
4582 template<int size
, bool big_endian
>
4583 typename Output_data_glink
<size
, big_endian
>::Address
4584 Output_data_glink
<size
, big_endian
>::find_global_entry(const Symbol
* gsym
) const
4586 typename
Global_entry_stub_entries::const_iterator p
4587 = this->global_entry_stubs_
.find(gsym
);
4588 return p
== this->global_entry_stubs_
.end() ? invalid_address
: p
->second
;
4591 template<int size
, bool big_endian
>
4593 Output_data_glink
<size
, big_endian
>::set_final_data_size()
4595 unsigned int count
= this->targ_
->plt_entry_count();
4596 section_size_type total
= 0;
4602 // space for branch table
4603 total
+= 4 * (count
- 1);
4605 total
+= -total
& 15;
4606 total
+= this->pltresolve_size
;
4610 total
+= this->pltresolve_size
;
4612 // space for branch table
4614 if (this->targ_
->abiversion() < 2)
4618 total
+= 4 * (count
- 0x8000);
4622 this->end_branch_table_
= total
;
4623 total
= (total
+ 15) & -16;
4624 total
+= this->ge_size_
;
4626 this->set_data_size(total
);
4629 // Define symbols on stubs, identifying the stub.
4631 template<int size
, bool big_endian
>
4633 Stub_table
<size
, big_endian
>::define_stub_syms(Symbol_table
* symtab
)
4635 if (!this->plt_call_stubs_
.empty())
4637 // The key for the plt call stub hash table includes addresses,
4638 // therefore traversal order depends on those addresses, which
4639 // can change between runs if gold is a PIE. Unfortunately the
4640 // output .symtab ordering depends on the order in which symbols
4641 // are added to the linker symtab. We want reproducible output
4642 // so must sort the call stub symbols.
4643 typedef typename
Plt_stub_entries::const_iterator plt_iter
;
4644 std::vector
<plt_iter
> sorted
;
4645 sorted
.resize(this->plt_call_stubs_
.size());
4647 for (plt_iter cs
= this->plt_call_stubs_
.begin();
4648 cs
!= this->plt_call_stubs_
.end();
4650 sorted
[cs
->first
.indx_
] = cs
;
4652 for (unsigned int i
= 0; i
< this->plt_call_stubs_
.size(); ++i
)
4654 plt_iter cs
= sorted
[i
];
4657 if (cs
->first
.addend_
!= 0)
4658 sprintf(add
, "+%x", static_cast<uint32_t>(cs
->first
.addend_
));
4660 const char *symname
;
4661 if (cs
->first
.sym_
== NULL
)
4663 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4664 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
4665 sprintf(localname
, "%x:%x", ppcobj
->uniq(), cs
->first
.locsym_
);
4666 symname
= localname
;
4669 symname
= cs
->first
.sym_
->name();
4670 char* name
= new char[8 + 10 + strlen(symname
) + strlen(add
) + 1];
4671 sprintf(name
, "%08x.plt_call.%s%s", this->uniq_
, symname
, add
);
4672 Address value
= this->stub_address() - this->address() + cs
->second
;
4673 unsigned int stub_size
= this->plt_call_size(cs
);
4674 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
4678 typedef typename
Branch_stub_entries::const_iterator branch_iter
;
4679 for (branch_iter bs
= this->long_branch_stubs_
.begin();
4680 bs
!= this->long_branch_stubs_
.end();
4683 if (bs
->first
.save_res_
)
4686 char* name
= new char[8 + 13 + 16 + 1];
4687 sprintf(name
, "%08x.long_branch.%llx", this->uniq_
,
4688 static_cast<unsigned long long>(bs
->first
.dest_
));
4689 Address value
= (this->stub_address() - this->address()
4690 + this->plt_size_
+ bs
->second
);
4691 unsigned int stub_size
= this->branch_stub_size(bs
);
4692 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
4696 // Write out plt and long branch stub code.
4698 template<int size
, bool big_endian
>
4700 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
4702 if (this->plt_call_stubs_
.empty()
4703 && this->long_branch_stubs_
.empty())
4706 const section_size_type start_off
= this->offset();
4707 const section_size_type off
= this->stub_offset();
4708 const section_size_type oview_size
=
4709 convert_to_section_size_type(this->data_size() - (off
- start_off
));
4710 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4715 const Output_data_got_powerpc
<size
, big_endian
>* got
4716 = this->targ_
->got_section();
4717 Address got_os_addr
= got
->output_section()->address();
4719 if (!this->plt_call_stubs_
.empty())
4721 // The base address of the .plt section.
4722 Address plt_base
= this->targ_
->plt_section()->address();
4723 Address iplt_base
= invalid_address
;
4725 // Write out plt call stubs.
4726 typename
Plt_stub_entries::const_iterator cs
;
4727 for (cs
= this->plt_call_stubs_
.begin();
4728 cs
!= this->plt_call_stubs_
.end();
4732 Address pltoff
= this->plt_off(cs
, &is_iplt
);
4733 Address plt_addr
= pltoff
;
4736 if (iplt_base
== invalid_address
)
4737 iplt_base
= this->targ_
->iplt_section()->address();
4738 plt_addr
+= iplt_base
;
4741 plt_addr
+= plt_base
;
4742 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4743 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
4744 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
4745 Address off
= plt_addr
- got_addr
;
4747 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
4748 gold_error(_("%s: linkage table error against `%s'"),
4749 cs
->first
.object_
->name().c_str(),
4750 cs
->first
.sym_
->demangled_name().c_str());
4752 bool plt_load_toc
= this->targ_
->abiversion() < 2;
4754 = plt_load_toc
&& parameters
->options().plt_static_chain();
4756 = plt_load_toc
&& this->targ_
->plt_thread_safe();
4757 bool use_fake_dep
= false;
4758 Address cmp_branch_off
= 0;
4761 unsigned int pltindex
4762 = ((pltoff
- this->targ_
->first_plt_entry_offset())
4763 / this->targ_
->plt_entry_size());
4765 = (this->targ_
->glink_section()->pltresolve_size
4767 if (pltindex
> 32768)
4768 glinkoff
+= (pltindex
- 32768) * 4;
4770 = this->targ_
->glink_section()->address() + glinkoff
;
4772 = (this->stub_address() + cs
->second
+ 24
4773 + 4 * (ha(off
) != 0)
4774 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4775 + 4 * static_chain
);
4776 cmp_branch_off
= to
- from
;
4777 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
4780 p
= oview
+ cs
->second
;
4783 write_insn
<big_endian
>(p
, std_2_1
+ this->targ_
->stk_toc());
4787 write_insn
<big_endian
>(p
, addis_11_2
+ ha(off
));
4789 write_insn
<big_endian
>(p
, ld_12_11
+ l(off
));
4794 write_insn
<big_endian
>(p
, addis_12_2
+ ha(off
));
4796 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
));
4800 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4802 write_insn
<big_endian
>(p
, addi_11_11
+ l(off
));
4806 write_insn
<big_endian
>(p
, mtctr_12
);
4812 write_insn
<big_endian
>(p
, xor_2_12_12
);
4814 write_insn
<big_endian
>(p
, add_11_11_2
);
4817 write_insn
<big_endian
>(p
, ld_2_11
+ l(off
+ 8));
4821 write_insn
<big_endian
>(p
, ld_11_11
+ l(off
+ 16));
4828 write_insn
<big_endian
>(p
, std_2_1
+ this->targ_
->stk_toc());
4830 write_insn
<big_endian
>(p
, ld_12_2
+ l(off
));
4833 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4835 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
));
4839 write_insn
<big_endian
>(p
, mtctr_12
);
4845 write_insn
<big_endian
>(p
, xor_11_12_12
);
4847 write_insn
<big_endian
>(p
, add_2_2_11
);
4852 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16));
4855 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8));
4859 if (thread_safe
&& !use_fake_dep
)
4861 write_insn
<big_endian
>(p
, cmpldi_2_0
);
4863 write_insn
<big_endian
>(p
, bnectr_p4
);
4865 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
4868 write_insn
<big_endian
>(p
, bctr
);
4872 // Write out long branch stubs.
4873 typename
Branch_stub_entries::const_iterator bs
;
4874 for (bs
= this->long_branch_stubs_
.begin();
4875 bs
!= this->long_branch_stubs_
.end();
4878 if (bs
->first
.save_res_
)
4880 p
= oview
+ this->plt_size_
+ bs
->second
;
4881 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
4882 Address delta
= bs
->first
.dest_
- loc
;
4883 if (delta
+ (1 << 25) < 2 << 25)
4884 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
4888 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
4889 gold_assert(brlt_addr
!= invalid_address
);
4890 brlt_addr
+= this->targ_
->brlt_section()->address();
4891 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
4892 Address brltoff
= brlt_addr
- got_addr
;
4893 if (ha(brltoff
) == 0)
4895 write_insn
<big_endian
>(p
, ld_12_2
+ l(brltoff
)), p
+= 4;
4899 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
4900 write_insn
<big_endian
>(p
, ld_12_12
+ l(brltoff
)), p
+= 4;
4902 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
4903 write_insn
<big_endian
>(p
, bctr
);
4909 if (!this->plt_call_stubs_
.empty())
4911 // The base address of the .plt section.
4912 Address plt_base
= this->targ_
->plt_section()->address();
4913 Address iplt_base
= invalid_address
;
4914 // The address of _GLOBAL_OFFSET_TABLE_.
4915 Address g_o_t
= invalid_address
;
4917 // Write out plt call stubs.
4918 typename
Plt_stub_entries::const_iterator cs
;
4919 for (cs
= this->plt_call_stubs_
.begin();
4920 cs
!= this->plt_call_stubs_
.end();
4924 Address plt_addr
= this->plt_off(cs
, &is_iplt
);
4927 if (iplt_base
== invalid_address
)
4928 iplt_base
= this->targ_
->iplt_section()->address();
4929 plt_addr
+= iplt_base
;
4932 plt_addr
+= plt_base
;
4934 p
= oview
+ cs
->second
;
4935 if (parameters
->options().output_is_position_independent())
4938 const Powerpc_relobj
<size
, big_endian
>* ppcobj
4939 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
4940 (cs
->first
.object_
));
4941 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
4943 unsigned int got2
= ppcobj
->got2_shndx();
4944 got_addr
= ppcobj
->get_output_section_offset(got2
);
4945 gold_assert(got_addr
!= invalid_address
);
4946 got_addr
+= (ppcobj
->output_section(got2
)->address()
4947 + cs
->first
.addend_
);
4951 if (g_o_t
== invalid_address
)
4953 const Output_data_got_powerpc
<size
, big_endian
>* got
4954 = this->targ_
->got_section();
4955 g_o_t
= got
->address() + got
->g_o_t();
4960 Address off
= plt_addr
- got_addr
;
4963 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(off
));
4964 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
4965 write_insn
<big_endian
>(p
+ 8, bctr
);
4969 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(off
));
4970 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(off
));
4971 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
4972 write_insn
<big_endian
>(p
+ 12, bctr
);
4977 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
4978 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
4979 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
4980 write_insn
<big_endian
>(p
+ 12, bctr
);
4985 // Write out long branch stubs.
4986 typename
Branch_stub_entries::const_iterator bs
;
4987 for (bs
= this->long_branch_stubs_
.begin();
4988 bs
!= this->long_branch_stubs_
.end();
4991 if (bs
->first
.save_res_
)
4993 p
= oview
+ this->plt_size_
+ bs
->second
;
4994 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
4995 Address delta
= bs
->first
.dest_
- loc
;
4996 if (delta
+ (1 << 25) < 2 << 25)
4997 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
4998 else if (!parameters
->options().output_is_position_independent())
5000 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(bs
->first
.dest_
));
5001 write_insn
<big_endian
>(p
+ 4, addi_12_12
+ l(bs
->first
.dest_
));
5002 write_insn
<big_endian
>(p
+ 8, mtctr_12
);
5003 write_insn
<big_endian
>(p
+ 12, bctr
);
5008 write_insn
<big_endian
>(p
+ 0, mflr_0
);
5009 write_insn
<big_endian
>(p
+ 4, bcl_20_31
);
5010 write_insn
<big_endian
>(p
+ 8, mflr_12
);
5011 write_insn
<big_endian
>(p
+ 12, addis_12_12
+ ha(delta
));
5012 write_insn
<big_endian
>(p
+ 16, addi_12_12
+ l(delta
));
5013 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
5014 write_insn
<big_endian
>(p
+ 24, mtctr_12
);
5015 write_insn
<big_endian
>(p
+ 28, bctr
);
5019 if (this->need_save_res_
)
5021 p
= oview
+ this->plt_size_
+ this->branch_size_
;
5022 memcpy (p
, this->targ_
->savres_section()->contents(),
5023 this->targ_
->savres_section()->data_size());
5027 // Write out .glink.
5029 template<int size
, bool big_endian
>
5031 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
5033 const section_size_type off
= this->offset();
5034 const section_size_type oview_size
=
5035 convert_to_section_size_type(this->data_size());
5036 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5039 // The base address of the .plt section.
5040 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
5041 Address plt_base
= this->targ_
->plt_section()->address();
5045 if (this->end_branch_table_
!= 0)
5047 // Write pltresolve stub.
5049 Address after_bcl
= this->address() + 16;
5050 Address pltoff
= plt_base
- after_bcl
;
5052 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
5054 if (this->targ_
->abiversion() < 2)
5056 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
5057 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5058 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5059 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5060 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
5061 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5062 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5063 write_insn
<big_endian
>(p
, ld_2_11
+ 8), p
+= 4;
5064 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5065 write_insn
<big_endian
>(p
, ld_11_11
+ 16), p
+= 4;
5069 write_insn
<big_endian
>(p
, mflr_0
), p
+= 4;
5070 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5071 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5072 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5073 write_insn
<big_endian
>(p
, mtlr_0
), p
+= 4;
5074 write_insn
<big_endian
>(p
, sub_12_12_11
), p
+= 4;
5075 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5076 write_insn
<big_endian
>(p
, addi_0_12
+ l(-48)), p
+= 4;
5077 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5078 write_insn
<big_endian
>(p
, srdi_0_0_2
), p
+= 4;
5079 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5080 write_insn
<big_endian
>(p
, ld_11_11
+ 8), p
+= 4;
5082 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
5083 while (p
< oview
+ this->pltresolve_size
)
5084 write_insn
<big_endian
>(p
, nop
), p
+= 4;
5086 // Write lazy link call stubs.
5088 while (p
< oview
+ this->end_branch_table_
)
5090 if (this->targ_
->abiversion() < 2)
5094 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
5098 write_insn
<big_endian
>(p
, lis_0
+ hi(indx
)), p
+= 4;
5099 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
5102 uint32_t branch_off
= 8 - (p
- oview
);
5103 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
5108 Address plt_base
= this->targ_
->plt_section()->address();
5109 Address iplt_base
= invalid_address
;
5110 unsigned int global_entry_off
= (this->end_branch_table_
+ 15) & -16;
5111 Address global_entry_base
= this->address() + global_entry_off
;
5112 typename
Global_entry_stub_entries::const_iterator ge
;
5113 for (ge
= this->global_entry_stubs_
.begin();
5114 ge
!= this->global_entry_stubs_
.end();
5117 p
= oview
+ global_entry_off
+ ge
->second
;
5118 Address plt_addr
= ge
->first
->plt_offset();
5119 if (ge
->first
->type() == elfcpp::STT_GNU_IFUNC
5120 && ge
->first
->can_use_relative_reloc(false))
5122 if (iplt_base
== invalid_address
)
5123 iplt_base
= this->targ_
->iplt_section()->address();
5124 plt_addr
+= iplt_base
;
5127 plt_addr
+= plt_base
;
5128 Address my_addr
= global_entry_base
+ ge
->second
;
5129 Address off
= plt_addr
- my_addr
;
5131 if (off
+ 0x80008000 > 0xffffffff || (off
& 3) != 0)
5132 gold_error(_("%s: linkage table error against `%s'"),
5133 ge
->first
->object()->name().c_str(),
5134 ge
->first
->demangled_name().c_str());
5136 write_insn
<big_endian
>(p
, addis_12_12
+ ha(off
)), p
+= 4;
5137 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
)), p
+= 4;
5138 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5139 write_insn
<big_endian
>(p
, bctr
);
5144 const Output_data_got_powerpc
<size
, big_endian
>* got
5145 = this->targ_
->got_section();
5146 // The address of _GLOBAL_OFFSET_TABLE_.
5147 Address g_o_t
= got
->address() + got
->g_o_t();
5149 // Write out pltresolve branch table.
5151 unsigned int the_end
= oview_size
- this->pltresolve_size
;
5152 unsigned char* end_p
= oview
+ the_end
;
5153 while (p
< end_p
- 8 * 4)
5154 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
5156 write_insn
<big_endian
>(p
, nop
), p
+= 4;
5158 // Write out pltresolve call stub.
5159 if (parameters
->options().output_is_position_independent())
5161 Address res0_off
= 0;
5162 Address after_bcl_off
= the_end
+ 12;
5163 Address bcl_res0
= after_bcl_off
- res0_off
;
5165 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
5166 write_insn
<big_endian
>(p
+ 4, mflr_0
);
5167 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
5168 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
5169 write_insn
<big_endian
>(p
+ 16, mflr_12
);
5170 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
5171 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
5173 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
5175 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
5176 if (ha(got_bcl
) == ha(got_bcl
+ 4))
5178 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
5179 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
5183 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
5184 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
5186 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
5187 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
5188 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
5189 write_insn
<big_endian
>(p
+ 52, bctr
);
5190 write_insn
<big_endian
>(p
+ 56, nop
);
5191 write_insn
<big_endian
>(p
+ 60, nop
);
5195 Address res0
= this->address();
5197 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
5198 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
5199 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5200 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
5202 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
5203 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
5204 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
5205 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
5206 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5207 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
5209 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
5210 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
5211 write_insn
<big_endian
>(p
+ 32, bctr
);
5212 write_insn
<big_endian
>(p
+ 36, nop
);
5213 write_insn
<big_endian
>(p
+ 40, nop
);
5214 write_insn
<big_endian
>(p
+ 44, nop
);
5215 write_insn
<big_endian
>(p
+ 48, nop
);
5216 write_insn
<big_endian
>(p
+ 52, nop
);
5217 write_insn
<big_endian
>(p
+ 56, nop
);
5218 write_insn
<big_endian
>(p
+ 60, nop
);
5223 of
->write_output_view(off
, oview_size
, oview
);
5227 // A class to handle linker generated save/restore functions.
5229 template<int size
, bool big_endian
>
5230 class Output_data_save_res
: public Output_section_data_build
5233 Output_data_save_res(Symbol_table
* symtab
);
5235 const unsigned char*
5242 // Write to a map file.
5244 do_print_to_mapfile(Mapfile
* mapfile
) const
5245 { mapfile
->print_output_data(this, _("** save/restore")); }
5248 do_write(Output_file
*);
5251 // The maximum size of save/restore contents.
5252 static const unsigned int savres_max
= 218*4;
5255 savres_define(Symbol_table
* symtab
,
5257 unsigned int lo
, unsigned int hi
,
5258 unsigned char* write_ent(unsigned char*, int),
5259 unsigned char* write_tail(unsigned char*, int));
5261 unsigned char *contents_
;
5264 template<bool big_endian
>
5265 static unsigned char*
5266 savegpr0(unsigned char* p
, int r
)
5268 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5269 write_insn
<big_endian
>(p
, insn
);
5273 template<bool big_endian
>
5274 static unsigned char*
5275 savegpr0_tail(unsigned char* p
, int r
)
5277 p
= savegpr0
<big_endian
>(p
, r
);
5278 uint32_t insn
= std_0_1
+ 16;
5279 write_insn
<big_endian
>(p
, insn
);
5281 write_insn
<big_endian
>(p
, blr
);
5285 template<bool big_endian
>
5286 static unsigned char*
5287 restgpr0(unsigned char* p
, int r
)
5289 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5290 write_insn
<big_endian
>(p
, insn
);
5294 template<bool big_endian
>
5295 static unsigned char*
5296 restgpr0_tail(unsigned char* p
, int r
)
5298 uint32_t insn
= ld_0_1
+ 16;
5299 write_insn
<big_endian
>(p
, insn
);
5301 p
= restgpr0
<big_endian
>(p
, r
);
5302 write_insn
<big_endian
>(p
, mtlr_0
);
5306 p
= restgpr0
<big_endian
>(p
, 30);
5307 p
= restgpr0
<big_endian
>(p
, 31);
5309 write_insn
<big_endian
>(p
, blr
);
5313 template<bool big_endian
>
5314 static unsigned char*
5315 savegpr1(unsigned char* p
, int r
)
5317 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5318 write_insn
<big_endian
>(p
, insn
);
5322 template<bool big_endian
>
5323 static unsigned char*
5324 savegpr1_tail(unsigned char* p
, int r
)
5326 p
= savegpr1
<big_endian
>(p
, r
);
5327 write_insn
<big_endian
>(p
, blr
);
5331 template<bool big_endian
>
5332 static unsigned char*
5333 restgpr1(unsigned char* p
, int r
)
5335 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5336 write_insn
<big_endian
>(p
, insn
);
5340 template<bool big_endian
>
5341 static unsigned char*
5342 restgpr1_tail(unsigned char* p
, int r
)
5344 p
= restgpr1
<big_endian
>(p
, r
);
5345 write_insn
<big_endian
>(p
, blr
);
5349 template<bool big_endian
>
5350 static unsigned char*
5351 savefpr(unsigned char* p
, int r
)
5353 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5354 write_insn
<big_endian
>(p
, insn
);
5358 template<bool big_endian
>
5359 static unsigned char*
5360 savefpr0_tail(unsigned char* p
, int r
)
5362 p
= savefpr
<big_endian
>(p
, r
);
5363 write_insn
<big_endian
>(p
, std_0_1
+ 16);
5365 write_insn
<big_endian
>(p
, blr
);
5369 template<bool big_endian
>
5370 static unsigned char*
5371 restfpr(unsigned char* p
, int r
)
5373 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5374 write_insn
<big_endian
>(p
, insn
);
5378 template<bool big_endian
>
5379 static unsigned char*
5380 restfpr0_tail(unsigned char* p
, int r
)
5382 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
5384 p
= restfpr
<big_endian
>(p
, r
);
5385 write_insn
<big_endian
>(p
, mtlr_0
);
5389 p
= restfpr
<big_endian
>(p
, 30);
5390 p
= restfpr
<big_endian
>(p
, 31);
5392 write_insn
<big_endian
>(p
, blr
);
5396 template<bool big_endian
>
5397 static unsigned char*
5398 savefpr1_tail(unsigned char* p
, int r
)
5400 p
= savefpr
<big_endian
>(p
, r
);
5401 write_insn
<big_endian
>(p
, blr
);
5405 template<bool big_endian
>
5406 static unsigned char*
5407 restfpr1_tail(unsigned char* p
, int r
)
5409 p
= restfpr
<big_endian
>(p
, r
);
5410 write_insn
<big_endian
>(p
, blr
);
5414 template<bool big_endian
>
5415 static unsigned char*
5416 savevr(unsigned char* p
, int r
)
5418 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5419 write_insn
<big_endian
>(p
, insn
);
5421 insn
= stvx_0_12_0
+ (r
<< 21);
5422 write_insn
<big_endian
>(p
, insn
);
5426 template<bool big_endian
>
5427 static unsigned char*
5428 savevr_tail(unsigned char* p
, int r
)
5430 p
= savevr
<big_endian
>(p
, r
);
5431 write_insn
<big_endian
>(p
, blr
);
5435 template<bool big_endian
>
5436 static unsigned char*
5437 restvr(unsigned char* p
, int r
)
5439 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5440 write_insn
<big_endian
>(p
, insn
);
5442 insn
= lvx_0_12_0
+ (r
<< 21);
5443 write_insn
<big_endian
>(p
, insn
);
5447 template<bool big_endian
>
5448 static unsigned char*
5449 restvr_tail(unsigned char* p
, int r
)
5451 p
= restvr
<big_endian
>(p
, r
);
5452 write_insn
<big_endian
>(p
, blr
);
5457 template<int size
, bool big_endian
>
5458 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
5459 Symbol_table
* symtab
)
5460 : Output_section_data_build(4),
5463 this->savres_define(symtab
,
5464 "_savegpr0_", 14, 31,
5465 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
5466 this->savres_define(symtab
,
5467 "_restgpr0_", 14, 29,
5468 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
5469 this->savres_define(symtab
,
5470 "_restgpr0_", 30, 31,
5471 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
5472 this->savres_define(symtab
,
5473 "_savegpr1_", 14, 31,
5474 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
5475 this->savres_define(symtab
,
5476 "_restgpr1_", 14, 31,
5477 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
5478 this->savres_define(symtab
,
5479 "_savefpr_", 14, 31,
5480 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
5481 this->savres_define(symtab
,
5482 "_restfpr_", 14, 29,
5483 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
5484 this->savres_define(symtab
,
5485 "_restfpr_", 30, 31,
5486 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
5487 this->savres_define(symtab
,
5489 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
5490 this->savres_define(symtab
,
5492 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
5493 this->savres_define(symtab
,
5495 savevr
<big_endian
>, savevr_tail
<big_endian
>);
5496 this->savres_define(symtab
,
5498 restvr
<big_endian
>, restvr_tail
<big_endian
>);
5501 template<int size
, bool big_endian
>
5503 Output_data_save_res
<size
, big_endian
>::savres_define(
5504 Symbol_table
* symtab
,
5506 unsigned int lo
, unsigned int hi
,
5507 unsigned char* write_ent(unsigned char*, int),
5508 unsigned char* write_tail(unsigned char*, int))
5510 size_t len
= strlen(name
);
5511 bool writing
= false;
5514 memcpy(sym
, name
, len
);
5517 for (unsigned int i
= lo
; i
<= hi
; i
++)
5519 sym
[len
+ 0] = i
/ 10 + '0';
5520 sym
[len
+ 1] = i
% 10 + '0';
5521 Symbol
* gsym
= symtab
->lookup(sym
);
5522 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
5523 writing
= writing
|| refd
;
5526 if (this->contents_
== NULL
)
5527 this->contents_
= new unsigned char[this->savres_max
];
5529 section_size_type value
= this->current_data_size();
5530 unsigned char* p
= this->contents_
+ value
;
5532 p
= write_ent(p
, i
);
5534 p
= write_tail(p
, i
);
5535 section_size_type cur_size
= p
- this->contents_
;
5536 this->set_current_data_size(cur_size
);
5538 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
5539 this, value
, cur_size
- value
,
5540 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
5541 elfcpp::STV_HIDDEN
, 0, false, false);
5546 // Write out save/restore.
5548 template<int size
, bool big_endian
>
5550 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
5552 const section_size_type off
= this->offset();
5553 const section_size_type oview_size
=
5554 convert_to_section_size_type(this->data_size());
5555 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5556 memcpy(oview
, this->contents_
, oview_size
);
5557 of
->write_output_view(off
, oview_size
, oview
);
5561 // Create the glink section.
5563 template<int size
, bool big_endian
>
5565 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
5567 if (this->glink_
== NULL
)
5569 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
5570 this->glink_
->add_eh_frame(layout
);
5571 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
5572 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
5573 this->glink_
, ORDER_TEXT
, false);
5577 // Create a PLT entry for a global symbol.
5579 template<int size
, bool big_endian
>
5581 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
5585 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
5586 && gsym
->can_use_relative_reloc(false))
5588 if (this->iplt_
== NULL
)
5589 this->make_iplt_section(symtab
, layout
);
5590 this->iplt_
->add_ifunc_entry(gsym
);
5594 if (this->plt_
== NULL
)
5595 this->make_plt_section(symtab
, layout
);
5596 this->plt_
->add_entry(gsym
);
5600 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5602 template<int size
, bool big_endian
>
5604 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
5605 Symbol_table
* symtab
,
5607 Sized_relobj_file
<size
, big_endian
>* relobj
,
5610 if (this->iplt_
== NULL
)
5611 this->make_iplt_section(symtab
, layout
);
5612 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
5615 // Return the number of entries in the PLT.
5617 template<int size
, bool big_endian
>
5619 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
5621 if (this->plt_
== NULL
)
5623 return this->plt_
->entry_count();
5626 // Create a GOT entry for local dynamic __tls_get_addr calls.
5628 template<int size
, bool big_endian
>
5630 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
5631 Symbol_table
* symtab
,
5633 Sized_relobj_file
<size
, big_endian
>* object
)
5635 if (this->tlsld_got_offset_
== -1U)
5637 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
5638 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
5639 Output_data_got_powerpc
<size
, big_endian
>* got
5640 = this->got_section(symtab
, layout
);
5641 unsigned int got_offset
= got
->add_constant_pair(0, 0);
5642 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
5644 this->tlsld_got_offset_
= got_offset
;
5646 return this->tlsld_got_offset_
;
5649 // Get the Reference_flags for a particular relocation.
5651 template<int size
, bool big_endian
>
5653 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(
5654 unsigned int r_type
,
5655 const Target_powerpc
* target
)
5661 case elfcpp::R_POWERPC_NONE
:
5662 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5663 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5664 case elfcpp::R_PPC64_TOC
:
5665 // No symbol reference.
5668 case elfcpp::R_PPC64_ADDR64
:
5669 case elfcpp::R_PPC64_UADDR64
:
5670 case elfcpp::R_POWERPC_ADDR32
:
5671 case elfcpp::R_POWERPC_UADDR32
:
5672 case elfcpp::R_POWERPC_ADDR16
:
5673 case elfcpp::R_POWERPC_UADDR16
:
5674 case elfcpp::R_POWERPC_ADDR16_LO
:
5675 case elfcpp::R_POWERPC_ADDR16_HI
:
5676 case elfcpp::R_POWERPC_ADDR16_HA
:
5677 ref
= Symbol::ABSOLUTE_REF
;
5680 case elfcpp::R_POWERPC_ADDR24
:
5681 case elfcpp::R_POWERPC_ADDR14
:
5682 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5683 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5684 ref
= Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
5687 case elfcpp::R_PPC64_REL64
:
5688 case elfcpp::R_POWERPC_REL32
:
5689 case elfcpp::R_PPC_LOCAL24PC
:
5690 case elfcpp::R_POWERPC_REL16
:
5691 case elfcpp::R_POWERPC_REL16_LO
:
5692 case elfcpp::R_POWERPC_REL16_HI
:
5693 case elfcpp::R_POWERPC_REL16_HA
:
5694 ref
= Symbol::RELATIVE_REF
;
5697 case elfcpp::R_POWERPC_REL24
:
5698 case elfcpp::R_PPC_PLTREL24
:
5699 case elfcpp::R_POWERPC_REL14
:
5700 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5701 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5702 ref
= Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
5705 case elfcpp::R_POWERPC_GOT16
:
5706 case elfcpp::R_POWERPC_GOT16_LO
:
5707 case elfcpp::R_POWERPC_GOT16_HI
:
5708 case elfcpp::R_POWERPC_GOT16_HA
:
5709 case elfcpp::R_PPC64_GOT16_DS
:
5710 case elfcpp::R_PPC64_GOT16_LO_DS
:
5711 case elfcpp::R_PPC64_TOC16
:
5712 case elfcpp::R_PPC64_TOC16_LO
:
5713 case elfcpp::R_PPC64_TOC16_HI
:
5714 case elfcpp::R_PPC64_TOC16_HA
:
5715 case elfcpp::R_PPC64_TOC16_DS
:
5716 case elfcpp::R_PPC64_TOC16_LO_DS
:
5717 ref
= Symbol::RELATIVE_REF
;
5720 case elfcpp::R_POWERPC_GOT_TPREL16
:
5721 case elfcpp::R_POWERPC_TLS
:
5722 ref
= Symbol::TLS_REF
;
5725 case elfcpp::R_POWERPC_COPY
:
5726 case elfcpp::R_POWERPC_GLOB_DAT
:
5727 case elfcpp::R_POWERPC_JMP_SLOT
:
5728 case elfcpp::R_POWERPC_RELATIVE
:
5729 case elfcpp::R_POWERPC_DTPMOD
:
5731 // Not expected. We will give an error later.
5735 if (size
== 64 && target
->abiversion() < 2)
5736 ref
|= Symbol::FUNC_DESC_ABI
;
5740 // Report an unsupported relocation against a local symbol.
5742 template<int size
, bool big_endian
>
5744 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
5745 Sized_relobj_file
<size
, big_endian
>* object
,
5746 unsigned int r_type
)
5748 gold_error(_("%s: unsupported reloc %u against local symbol"),
5749 object
->name().c_str(), r_type
);
5752 // We are about to emit a dynamic relocation of type R_TYPE. If the
5753 // dynamic linker does not support it, issue an error.
5755 template<int size
, bool big_endian
>
5757 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
5758 unsigned int r_type
)
5760 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
5762 // These are the relocation types supported by glibc for both 32-bit
5763 // and 64-bit powerpc.
5766 case elfcpp::R_POWERPC_NONE
:
5767 case elfcpp::R_POWERPC_RELATIVE
:
5768 case elfcpp::R_POWERPC_GLOB_DAT
:
5769 case elfcpp::R_POWERPC_DTPMOD
:
5770 case elfcpp::R_POWERPC_DTPREL
:
5771 case elfcpp::R_POWERPC_TPREL
:
5772 case elfcpp::R_POWERPC_JMP_SLOT
:
5773 case elfcpp::R_POWERPC_COPY
:
5774 case elfcpp::R_POWERPC_IRELATIVE
:
5775 case elfcpp::R_POWERPC_ADDR32
:
5776 case elfcpp::R_POWERPC_UADDR32
:
5777 case elfcpp::R_POWERPC_ADDR24
:
5778 case elfcpp::R_POWERPC_ADDR16
:
5779 case elfcpp::R_POWERPC_UADDR16
:
5780 case elfcpp::R_POWERPC_ADDR16_LO
:
5781 case elfcpp::R_POWERPC_ADDR16_HI
:
5782 case elfcpp::R_POWERPC_ADDR16_HA
:
5783 case elfcpp::R_POWERPC_ADDR14
:
5784 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5785 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5786 case elfcpp::R_POWERPC_REL32
:
5787 case elfcpp::R_POWERPC_REL24
:
5788 case elfcpp::R_POWERPC_TPREL16
:
5789 case elfcpp::R_POWERPC_TPREL16_LO
:
5790 case elfcpp::R_POWERPC_TPREL16_HI
:
5791 case elfcpp::R_POWERPC_TPREL16_HA
:
5802 // These are the relocation types supported only on 64-bit.
5803 case elfcpp::R_PPC64_ADDR64
:
5804 case elfcpp::R_PPC64_UADDR64
:
5805 case elfcpp::R_PPC64_JMP_IREL
:
5806 case elfcpp::R_PPC64_ADDR16_DS
:
5807 case elfcpp::R_PPC64_ADDR16_LO_DS
:
5808 case elfcpp::R_PPC64_ADDR16_HIGH
:
5809 case elfcpp::R_PPC64_ADDR16_HIGHA
:
5810 case elfcpp::R_PPC64_ADDR16_HIGHER
:
5811 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
5812 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
5813 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
5814 case elfcpp::R_PPC64_REL64
:
5815 case elfcpp::R_POWERPC_ADDR30
:
5816 case elfcpp::R_PPC64_TPREL16_DS
:
5817 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5818 case elfcpp::R_PPC64_TPREL16_HIGH
:
5819 case elfcpp::R_PPC64_TPREL16_HIGHA
:
5820 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5821 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5822 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5823 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5834 // These are the relocation types supported only on 32-bit.
5835 // ??? glibc ld.so doesn't need to support these.
5836 case elfcpp::R_POWERPC_DTPREL16
:
5837 case elfcpp::R_POWERPC_DTPREL16_LO
:
5838 case elfcpp::R_POWERPC_DTPREL16_HI
:
5839 case elfcpp::R_POWERPC_DTPREL16_HA
:
5847 // This prevents us from issuing more than one error per reloc
5848 // section. But we can still wind up issuing more than one
5849 // error per object file.
5850 if (this->issued_non_pic_error_
)
5852 gold_assert(parameters
->options().output_is_position_independent());
5853 object
->error(_("requires unsupported dynamic reloc; "
5854 "recompile with -fPIC"));
5855 this->issued_non_pic_error_
= true;
5859 // Return whether we need to make a PLT entry for a relocation of the
5860 // given type against a STT_GNU_IFUNC symbol.
5862 template<int size
, bool big_endian
>
5864 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
5865 Target_powerpc
<size
, big_endian
>* target
,
5866 Sized_relobj_file
<size
, big_endian
>* object
,
5867 unsigned int r_type
,
5870 // In non-pic code any reference will resolve to the plt call stub
5871 // for the ifunc symbol.
5872 if ((size
== 32 || target
->abiversion() >= 2)
5873 && !parameters
->options().output_is_position_independent())
5878 // Word size refs from data sections are OK, but don't need a PLT entry.
5879 case elfcpp::R_POWERPC_ADDR32
:
5880 case elfcpp::R_POWERPC_UADDR32
:
5885 case elfcpp::R_PPC64_ADDR64
:
5886 case elfcpp::R_PPC64_UADDR64
:
5891 // GOT refs are good, but also don't need a PLT entry.
5892 case elfcpp::R_POWERPC_GOT16
:
5893 case elfcpp::R_POWERPC_GOT16_LO
:
5894 case elfcpp::R_POWERPC_GOT16_HI
:
5895 case elfcpp::R_POWERPC_GOT16_HA
:
5896 case elfcpp::R_PPC64_GOT16_DS
:
5897 case elfcpp::R_PPC64_GOT16_LO_DS
:
5900 // Function calls are good, and these do need a PLT entry.
5901 case elfcpp::R_POWERPC_ADDR24
:
5902 case elfcpp::R_POWERPC_ADDR14
:
5903 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5904 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5905 case elfcpp::R_POWERPC_REL24
:
5906 case elfcpp::R_PPC_PLTREL24
:
5907 case elfcpp::R_POWERPC_REL14
:
5908 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5909 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5916 // Anything else is a problem.
5917 // If we are building a static executable, the libc startup function
5918 // responsible for applying indirect function relocations is going
5919 // to complain about the reloc type.
5920 // If we are building a dynamic executable, we will have a text
5921 // relocation. The dynamic loader will set the text segment
5922 // writable and non-executable to apply text relocations. So we'll
5923 // segfault when trying to run the indirection function to resolve
5926 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
5927 object
->name().c_str(), r_type
);
5931 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
5935 ok_lo_toc_insn(uint32_t insn
, unsigned int r_type
)
5937 return ((insn
& (0x3f << 26)) == 12u << 26 /* addic */
5938 || (insn
& (0x3f << 26)) == 14u << 26 /* addi */
5939 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
5940 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
5941 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
5942 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
5943 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
5944 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
5945 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
5946 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
5947 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
5948 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
5949 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
5950 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
5951 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
5952 || (insn
& (0x3f << 26)) == 56u << 26 /* lq,lfq */
5953 || ((insn
& (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
5954 /* Exclude lfqu by testing reloc. If relocs are ever
5955 defined for the reduced D field in psq_lu then those
5956 will need testing too. */
5957 && r_type
!= elfcpp::R_PPC64_TOC16_LO
5958 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
5959 || ((insn
& (0x3f << 26)) == 58u << 26 /* ld,lwa */
5961 || (insn
& (0x3f << 26)) == 60u << 26 /* stfq */
5962 || ((insn
& (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
5963 /* Exclude stfqu. psq_stu as above for psq_lu. */
5964 && r_type
!= elfcpp::R_PPC64_TOC16_LO
5965 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
5966 || ((insn
& (0x3f << 26)) == 62u << 26 /* std,stq */
5967 && (insn
& 1) == 0));
5970 // Scan a relocation for a local symbol.
5972 template<int size
, bool big_endian
>
5974 Target_powerpc
<size
, big_endian
>::Scan::local(
5975 Symbol_table
* symtab
,
5977 Target_powerpc
<size
, big_endian
>* target
,
5978 Sized_relobj_file
<size
, big_endian
>* object
,
5979 unsigned int data_shndx
,
5980 Output_section
* output_section
,
5981 const elfcpp::Rela
<size
, big_endian
>& reloc
,
5982 unsigned int r_type
,
5983 const elfcpp::Sym
<size
, big_endian
>& lsym
,
5986 this->maybe_skip_tls_get_addr_call(r_type
, NULL
);
5988 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
5989 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
5991 this->expect_tls_get_addr_call();
5992 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
5993 if (tls_type
!= tls::TLSOPT_NONE
)
5994 this->skip_next_tls_get_addr_call();
5996 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
5997 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
5999 this->expect_tls_get_addr_call();
6000 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6001 if (tls_type
!= tls::TLSOPT_NONE
)
6002 this->skip_next_tls_get_addr_call();
6005 Powerpc_relobj
<size
, big_endian
>* ppc_object
6006 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6011 && data_shndx
== ppc_object
->opd_shndx()
6012 && r_type
== elfcpp::R_PPC64_ADDR64
)
6013 ppc_object
->set_opd_discard(reloc
.get_r_offset());
6017 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6018 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
6019 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
6021 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6022 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6023 r_type
, r_sym
, reloc
.get_r_addend());
6024 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
6029 case elfcpp::R_POWERPC_NONE
:
6030 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6031 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6032 case elfcpp::R_PPC64_TOCSAVE
:
6033 case elfcpp::R_POWERPC_TLS
:
6034 case elfcpp::R_PPC64_ENTRY
:
6037 case elfcpp::R_PPC64_TOC
:
6039 Output_data_got_powerpc
<size
, big_endian
>* got
6040 = target
->got_section(symtab
, layout
);
6041 if (parameters
->options().output_is_position_independent())
6043 Address off
= reloc
.get_r_offset();
6045 && target
->abiversion() < 2
6046 && data_shndx
== ppc_object
->opd_shndx()
6047 && ppc_object
->get_opd_discard(off
- 8))
6050 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6051 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
6052 rela_dyn
->add_output_section_relative(got
->output_section(),
6053 elfcpp::R_POWERPC_RELATIVE
,
6055 object
, data_shndx
, off
,
6056 symobj
->toc_base_offset());
6061 case elfcpp::R_PPC64_ADDR64
:
6062 case elfcpp::R_PPC64_UADDR64
:
6063 case elfcpp::R_POWERPC_ADDR32
:
6064 case elfcpp::R_POWERPC_UADDR32
:
6065 case elfcpp::R_POWERPC_ADDR24
:
6066 case elfcpp::R_POWERPC_ADDR16
:
6067 case elfcpp::R_POWERPC_ADDR16_LO
:
6068 case elfcpp::R_POWERPC_ADDR16_HI
:
6069 case elfcpp::R_POWERPC_ADDR16_HA
:
6070 case elfcpp::R_POWERPC_UADDR16
:
6071 case elfcpp::R_PPC64_ADDR16_HIGH
:
6072 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6073 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6074 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6075 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6076 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6077 case elfcpp::R_PPC64_ADDR16_DS
:
6078 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6079 case elfcpp::R_POWERPC_ADDR14
:
6080 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6081 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6082 // If building a shared library (or a position-independent
6083 // executable), we need to create a dynamic relocation for
6085 if (parameters
->options().output_is_position_independent()
6086 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
6088 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
6090 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6091 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
6092 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
6094 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6095 : elfcpp::R_POWERPC_RELATIVE
);
6096 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
6097 output_section
, data_shndx
,
6098 reloc
.get_r_offset(),
6099 reloc
.get_r_addend(), false);
6101 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
6103 check_non_pic(object
, r_type
);
6104 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
6105 data_shndx
, reloc
.get_r_offset(),
6106 reloc
.get_r_addend());
6110 gold_assert(lsym
.get_st_value() == 0);
6111 unsigned int shndx
= lsym
.get_st_shndx();
6113 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
6116 object
->error(_("section symbol %u has bad shndx %u"),
6119 rela_dyn
->add_local_section(object
, shndx
, r_type
,
6120 output_section
, data_shndx
,
6121 reloc
.get_r_offset());
6126 case elfcpp::R_POWERPC_REL24
:
6127 case elfcpp::R_PPC_PLTREL24
:
6128 case elfcpp::R_PPC_LOCAL24PC
:
6129 case elfcpp::R_POWERPC_REL14
:
6130 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6131 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6134 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6135 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6136 r_type
, r_sym
, reloc
.get_r_addend());
6140 case elfcpp::R_PPC64_REL64
:
6141 case elfcpp::R_POWERPC_REL32
:
6142 case elfcpp::R_POWERPC_REL16
:
6143 case elfcpp::R_POWERPC_REL16_LO
:
6144 case elfcpp::R_POWERPC_REL16_HI
:
6145 case elfcpp::R_POWERPC_REL16_HA
:
6146 case elfcpp::R_POWERPC_REL16DX_HA
:
6147 case elfcpp::R_POWERPC_SECTOFF
:
6148 case elfcpp::R_POWERPC_SECTOFF_LO
:
6149 case elfcpp::R_POWERPC_SECTOFF_HI
:
6150 case elfcpp::R_POWERPC_SECTOFF_HA
:
6151 case elfcpp::R_PPC64_SECTOFF_DS
:
6152 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6153 case elfcpp::R_POWERPC_TPREL16
:
6154 case elfcpp::R_POWERPC_TPREL16_LO
:
6155 case elfcpp::R_POWERPC_TPREL16_HI
:
6156 case elfcpp::R_POWERPC_TPREL16_HA
:
6157 case elfcpp::R_PPC64_TPREL16_DS
:
6158 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6159 case elfcpp::R_PPC64_TPREL16_HIGH
:
6160 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6161 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6162 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6163 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6164 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6165 case elfcpp::R_POWERPC_DTPREL16
:
6166 case elfcpp::R_POWERPC_DTPREL16_LO
:
6167 case elfcpp::R_POWERPC_DTPREL16_HI
:
6168 case elfcpp::R_POWERPC_DTPREL16_HA
:
6169 case elfcpp::R_PPC64_DTPREL16_DS
:
6170 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6171 case elfcpp::R_PPC64_DTPREL16_HIGH
:
6172 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
6173 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6174 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6175 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6176 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6177 case elfcpp::R_PPC64_TLSGD
:
6178 case elfcpp::R_PPC64_TLSLD
:
6179 case elfcpp::R_PPC64_ADDR64_LOCAL
:
6182 case elfcpp::R_POWERPC_GOT16
:
6183 case elfcpp::R_POWERPC_GOT16_LO
:
6184 case elfcpp::R_POWERPC_GOT16_HI
:
6185 case elfcpp::R_POWERPC_GOT16_HA
:
6186 case elfcpp::R_PPC64_GOT16_DS
:
6187 case elfcpp::R_PPC64_GOT16_LO_DS
:
6189 // The symbol requires a GOT entry.
6190 Output_data_got_powerpc
<size
, big_endian
>* got
6191 = target
->got_section(symtab
, layout
);
6192 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6194 if (!parameters
->options().output_is_position_independent())
6197 && (size
== 32 || target
->abiversion() >= 2))
6198 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
6200 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
6202 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
6204 // If we are generating a shared object or a pie, this
6205 // symbol's GOT entry will be set by a dynamic relocation.
6207 off
= got
->add_constant(0);
6208 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
6210 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
6212 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6213 : elfcpp::R_POWERPC_RELATIVE
);
6214 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
6215 got
, off
, 0, false);
6220 case elfcpp::R_PPC64_TOC16
:
6221 case elfcpp::R_PPC64_TOC16_LO
:
6222 case elfcpp::R_PPC64_TOC16_HI
:
6223 case elfcpp::R_PPC64_TOC16_HA
:
6224 case elfcpp::R_PPC64_TOC16_DS
:
6225 case elfcpp::R_PPC64_TOC16_LO_DS
:
6226 // We need a GOT section.
6227 target
->got_section(symtab
, layout
);
6230 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6231 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6232 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6233 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6235 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
6236 if (tls_type
== tls::TLSOPT_NONE
)
6238 Output_data_got_powerpc
<size
, big_endian
>* got
6239 = target
->got_section(symtab
, layout
);
6240 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6241 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6242 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
6243 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
6245 else if (tls_type
== tls::TLSOPT_TO_LE
)
6247 // no GOT relocs needed for Local Exec.
6254 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6255 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6256 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6257 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6259 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6260 if (tls_type
== tls::TLSOPT_NONE
)
6261 target
->tlsld_got_offset(symtab
, layout
, object
);
6262 else if (tls_type
== tls::TLSOPT_TO_LE
)
6264 // no GOT relocs needed for Local Exec.
6265 if (parameters
->options().emit_relocs())
6267 Output_section
* os
= layout
->tls_segment()->first_section();
6268 gold_assert(os
!= NULL
);
6269 os
->set_needs_symtab_index();
6277 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6278 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6279 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6280 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6282 Output_data_got_powerpc
<size
, big_endian
>* got
6283 = target
->got_section(symtab
, layout
);
6284 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6285 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
6289 case elfcpp::R_POWERPC_GOT_TPREL16
:
6290 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6291 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6292 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6294 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
6295 if (tls_type
== tls::TLSOPT_NONE
)
6297 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6298 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
6300 Output_data_got_powerpc
<size
, big_endian
>* got
6301 = target
->got_section(symtab
, layout
);
6302 unsigned int off
= got
->add_constant(0);
6303 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
6305 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6306 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
6307 elfcpp::R_POWERPC_TPREL
,
6311 else if (tls_type
== tls::TLSOPT_TO_LE
)
6313 // no GOT relocs needed for Local Exec.
6321 unsupported_reloc_local(object
, r_type
);
6326 && parameters
->options().toc_optimize())
6328 if (data_shndx
== ppc_object
->toc_shndx())
6331 if (r_type
!= elfcpp::R_PPC64_ADDR64
6332 || (is_ifunc
&& target
->abiversion() < 2))
6334 else if (parameters
->options().output_is_position_independent())
6340 unsigned int shndx
= lsym
.get_st_shndx();
6341 if (shndx
>= elfcpp::SHN_LORESERVE
6342 && shndx
!= elfcpp::SHN_XINDEX
)
6347 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
6350 enum {no_check
, check_lo
, check_ha
} insn_check
;
6354 insn_check
= no_check
;
6357 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6358 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6359 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6360 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6361 case elfcpp::R_POWERPC_GOT16_HA
:
6362 case elfcpp::R_PPC64_TOC16_HA
:
6363 insn_check
= check_ha
;
6366 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6367 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6368 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6369 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6370 case elfcpp::R_POWERPC_GOT16_LO
:
6371 case elfcpp::R_PPC64_GOT16_LO_DS
:
6372 case elfcpp::R_PPC64_TOC16_LO
:
6373 case elfcpp::R_PPC64_TOC16_LO_DS
:
6374 insn_check
= check_lo
;
6378 section_size_type slen
;
6379 const unsigned char* view
= NULL
;
6380 if (insn_check
!= no_check
)
6382 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
6383 section_size_type off
=
6384 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
6387 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
6388 if (insn_check
== check_lo
6389 ? !ok_lo_toc_insn(insn
, r_type
)
6390 : ((insn
& ((0x3f << 26) | 0x1f << 16))
6391 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
6393 ppc_object
->set_no_toc_opt();
6394 gold_warning(_("%s: toc optimization is not supported "
6395 "for %#08x instruction"),
6396 ppc_object
->name().c_str(), insn
);
6405 case elfcpp::R_PPC64_TOC16
:
6406 case elfcpp::R_PPC64_TOC16_LO
:
6407 case elfcpp::R_PPC64_TOC16_HI
:
6408 case elfcpp::R_PPC64_TOC16_HA
:
6409 case elfcpp::R_PPC64_TOC16_DS
:
6410 case elfcpp::R_PPC64_TOC16_LO_DS
:
6411 unsigned int shndx
= lsym
.get_st_shndx();
6412 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6414 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6415 if (is_ordinary
&& shndx
== ppc_object
->toc_shndx())
6417 Address dst_off
= lsym
.get_st_value() + reloc
.get_r_offset();
6418 if (dst_off
< ppc_object
->section_size(shndx
))
6421 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
6423 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
6425 // Need to check that the insn is a ld
6427 view
= ppc_object
->section_contents(data_shndx
,
6430 section_size_type off
=
6431 (convert_to_section_size_type(reloc
.get_r_offset())
6432 + (big_endian
? -2 : 3));
6434 && (view
[off
] & (0x3f << 2)) == 58u << 2)
6438 ppc_object
->set_no_toc_opt(dst_off
);
6449 case elfcpp::R_POWERPC_REL32
:
6450 if (ppc_object
->got2_shndx() != 0
6451 && parameters
->options().output_is_position_independent())
6453 unsigned int shndx
= lsym
.get_st_shndx();
6454 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6456 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6457 if (is_ordinary
&& shndx
== ppc_object
->got2_shndx()
6458 && (ppc_object
->section_flags(data_shndx
)
6459 & elfcpp::SHF_EXECINSTR
) != 0)
6460 gold_error(_("%s: unsupported -mbss-plt code"),
6461 ppc_object
->name().c_str());
6471 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6472 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6473 case elfcpp::R_POWERPC_GOT_TPREL16
:
6474 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6475 case elfcpp::R_POWERPC_GOT16
:
6476 case elfcpp::R_PPC64_GOT16_DS
:
6477 case elfcpp::R_PPC64_TOC16
:
6478 case elfcpp::R_PPC64_TOC16_DS
:
6479 ppc_object
->set_has_small_toc_reloc();
6485 // Report an unsupported relocation against a global symbol.
6487 template<int size
, bool big_endian
>
6489 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
6490 Sized_relobj_file
<size
, big_endian
>* object
,
6491 unsigned int r_type
,
6494 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6495 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
6498 // Scan a relocation for a global symbol.
6500 template<int size
, bool big_endian
>
6502 Target_powerpc
<size
, big_endian
>::Scan::global(
6503 Symbol_table
* symtab
,
6505 Target_powerpc
<size
, big_endian
>* target
,
6506 Sized_relobj_file
<size
, big_endian
>* object
,
6507 unsigned int data_shndx
,
6508 Output_section
* output_section
,
6509 const elfcpp::Rela
<size
, big_endian
>& reloc
,
6510 unsigned int r_type
,
6513 if (this->maybe_skip_tls_get_addr_call(r_type
, gsym
) == Track_tls::SKIP
)
6516 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6517 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6519 this->expect_tls_get_addr_call();
6520 const bool final
= gsym
->final_value_is_known();
6521 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6522 if (tls_type
!= tls::TLSOPT_NONE
)
6523 this->skip_next_tls_get_addr_call();
6525 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6526 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6528 this->expect_tls_get_addr_call();
6529 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6530 if (tls_type
!= tls::TLSOPT_NONE
)
6531 this->skip_next_tls_get_addr_call();
6534 Powerpc_relobj
<size
, big_endian
>* ppc_object
6535 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6537 // A STT_GNU_IFUNC symbol may require a PLT entry.
6538 bool is_ifunc
= gsym
->type() == elfcpp::STT_GNU_IFUNC
;
6539 bool pushed_ifunc
= false;
6540 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
6542 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6543 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6544 r_type
, r_sym
, reloc
.get_r_addend());
6545 target
->make_plt_entry(symtab
, layout
, gsym
);
6546 pushed_ifunc
= true;
6551 case elfcpp::R_POWERPC_NONE
:
6552 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6553 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6554 case elfcpp::R_PPC_LOCAL24PC
:
6555 case elfcpp::R_POWERPC_TLS
:
6556 case elfcpp::R_PPC64_ENTRY
:
6559 case elfcpp::R_PPC64_TOC
:
6561 Output_data_got_powerpc
<size
, big_endian
>* got
6562 = target
->got_section(symtab
, layout
);
6563 if (parameters
->options().output_is_position_independent())
6565 Address off
= reloc
.get_r_offset();
6567 && data_shndx
== ppc_object
->opd_shndx()
6568 && ppc_object
->get_opd_discard(off
- 8))
6571 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6572 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
6573 if (data_shndx
!= ppc_object
->opd_shndx())
6574 symobj
= static_cast
6575 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
6576 rela_dyn
->add_output_section_relative(got
->output_section(),
6577 elfcpp::R_POWERPC_RELATIVE
,
6579 object
, data_shndx
, off
,
6580 symobj
->toc_base_offset());
6585 case elfcpp::R_PPC64_ADDR64
:
6587 && target
->abiversion() < 2
6588 && data_shndx
== ppc_object
->opd_shndx()
6589 && (gsym
->is_defined_in_discarded_section()
6590 || gsym
->object() != object
))
6592 ppc_object
->set_opd_discard(reloc
.get_r_offset());
6596 case elfcpp::R_PPC64_UADDR64
:
6597 case elfcpp::R_POWERPC_ADDR32
:
6598 case elfcpp::R_POWERPC_UADDR32
:
6599 case elfcpp::R_POWERPC_ADDR24
:
6600 case elfcpp::R_POWERPC_ADDR16
:
6601 case elfcpp::R_POWERPC_ADDR16_LO
:
6602 case elfcpp::R_POWERPC_ADDR16_HI
:
6603 case elfcpp::R_POWERPC_ADDR16_HA
:
6604 case elfcpp::R_POWERPC_UADDR16
:
6605 case elfcpp::R_PPC64_ADDR16_HIGH
:
6606 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6607 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6608 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6609 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6610 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6611 case elfcpp::R_PPC64_ADDR16_DS
:
6612 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6613 case elfcpp::R_POWERPC_ADDR14
:
6614 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6615 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6617 // Make a PLT entry if necessary.
6618 if (gsym
->needs_plt_entry())
6620 // Since this is not a PC-relative relocation, we may be
6621 // taking the address of a function. In that case we need to
6622 // set the entry in the dynamic symbol table to the address of
6623 // the PLT call stub.
6624 bool need_ifunc_plt
= false;
6625 if ((size
== 32 || target
->abiversion() >= 2)
6626 && gsym
->is_from_dynobj()
6627 && !parameters
->options().output_is_position_independent())
6629 gsym
->set_needs_dynsym_value();
6630 need_ifunc_plt
= true;
6632 if (!is_ifunc
|| (!pushed_ifunc
&& need_ifunc_plt
))
6634 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6635 target
->push_branch(ppc_object
, data_shndx
,
6636 reloc
.get_r_offset(), r_type
, r_sym
,
6637 reloc
.get_r_addend());
6638 target
->make_plt_entry(symtab
, layout
, gsym
);
6641 // Make a dynamic relocation if necessary.
6642 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
))
6643 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
6645 if (!parameters
->options().output_is_position_independent()
6646 && gsym
->may_need_copy_reloc())
6648 target
->copy_reloc(symtab
, layout
, object
,
6649 data_shndx
, output_section
, gsym
, reloc
);
6651 else if ((((size
== 32
6652 && r_type
== elfcpp::R_POWERPC_ADDR32
)
6654 && r_type
== elfcpp::R_PPC64_ADDR64
6655 && target
->abiversion() >= 2))
6656 && gsym
->can_use_relative_reloc(false)
6657 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
6658 && parameters
->options().shared()))
6660 && r_type
== elfcpp::R_PPC64_ADDR64
6661 && target
->abiversion() < 2
6662 && (gsym
->can_use_relative_reloc(false)
6663 || data_shndx
== ppc_object
->opd_shndx())))
6665 Reloc_section
* rela_dyn
6666 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6667 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6668 : elfcpp::R_POWERPC_RELATIVE
);
6669 rela_dyn
->add_symbolless_global_addend(
6670 gsym
, dynrel
, output_section
, object
, data_shndx
,
6671 reloc
.get_r_offset(), reloc
.get_r_addend());
6675 Reloc_section
* rela_dyn
6676 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6677 check_non_pic(object
, r_type
);
6678 rela_dyn
->add_global(gsym
, r_type
, output_section
,
6680 reloc
.get_r_offset(),
6681 reloc
.get_r_addend());
6684 && parameters
->options().toc_optimize()
6685 && data_shndx
== ppc_object
->toc_shndx())
6686 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
6692 case elfcpp::R_PPC_PLTREL24
:
6693 case elfcpp::R_POWERPC_REL24
:
6696 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6697 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6698 r_type
, r_sym
, reloc
.get_r_addend());
6699 if (gsym
->needs_plt_entry()
6700 || (!gsym
->final_value_is_known()
6701 && (gsym
->is_undefined()
6702 || gsym
->is_from_dynobj()
6703 || gsym
->is_preemptible())))
6704 target
->make_plt_entry(symtab
, layout
, gsym
);
6708 case elfcpp::R_PPC64_REL64
:
6709 case elfcpp::R_POWERPC_REL32
:
6710 // Make a dynamic relocation if necessary.
6711 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
)))
6713 if (!parameters
->options().output_is_position_independent()
6714 && gsym
->may_need_copy_reloc())
6716 target
->copy_reloc(symtab
, layout
, object
,
6717 data_shndx
, output_section
, gsym
,
6722 Reloc_section
* rela_dyn
6723 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6724 check_non_pic(object
, r_type
);
6725 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
6726 data_shndx
, reloc
.get_r_offset(),
6727 reloc
.get_r_addend());
6732 case elfcpp::R_POWERPC_REL14
:
6733 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6734 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6737 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6738 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6739 r_type
, r_sym
, reloc
.get_r_addend());
6743 case elfcpp::R_POWERPC_REL16
:
6744 case elfcpp::R_POWERPC_REL16_LO
:
6745 case elfcpp::R_POWERPC_REL16_HI
:
6746 case elfcpp::R_POWERPC_REL16_HA
:
6747 case elfcpp::R_POWERPC_REL16DX_HA
:
6748 case elfcpp::R_POWERPC_SECTOFF
:
6749 case elfcpp::R_POWERPC_SECTOFF_LO
:
6750 case elfcpp::R_POWERPC_SECTOFF_HI
:
6751 case elfcpp::R_POWERPC_SECTOFF_HA
:
6752 case elfcpp::R_PPC64_SECTOFF_DS
:
6753 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6754 case elfcpp::R_POWERPC_TPREL16
:
6755 case elfcpp::R_POWERPC_TPREL16_LO
:
6756 case elfcpp::R_POWERPC_TPREL16_HI
:
6757 case elfcpp::R_POWERPC_TPREL16_HA
:
6758 case elfcpp::R_PPC64_TPREL16_DS
:
6759 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6760 case elfcpp::R_PPC64_TPREL16_HIGH
:
6761 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6762 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6763 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6764 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6765 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6766 case elfcpp::R_POWERPC_DTPREL16
:
6767 case elfcpp::R_POWERPC_DTPREL16_LO
:
6768 case elfcpp::R_POWERPC_DTPREL16_HI
:
6769 case elfcpp::R_POWERPC_DTPREL16_HA
:
6770 case elfcpp::R_PPC64_DTPREL16_DS
:
6771 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6772 case elfcpp::R_PPC64_DTPREL16_HIGH
:
6773 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
6774 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6775 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6776 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6777 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6778 case elfcpp::R_PPC64_TLSGD
:
6779 case elfcpp::R_PPC64_TLSLD
:
6780 case elfcpp::R_PPC64_ADDR64_LOCAL
:
6783 case elfcpp::R_POWERPC_GOT16
:
6784 case elfcpp::R_POWERPC_GOT16_LO
:
6785 case elfcpp::R_POWERPC_GOT16_HI
:
6786 case elfcpp::R_POWERPC_GOT16_HA
:
6787 case elfcpp::R_PPC64_GOT16_DS
:
6788 case elfcpp::R_PPC64_GOT16_LO_DS
:
6790 // The symbol requires a GOT entry.
6791 Output_data_got_powerpc
<size
, big_endian
>* got
;
6793 got
= target
->got_section(symtab
, layout
);
6794 if (gsym
->final_value_is_known())
6797 && (size
== 32 || target
->abiversion() >= 2))
6798 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
6800 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
6802 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
6804 // If we are generating a shared object or a pie, this
6805 // symbol's GOT entry will be set by a dynamic relocation.
6806 unsigned int off
= got
->add_constant(0);
6807 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
6809 Reloc_section
* rela_dyn
6810 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6812 if (gsym
->can_use_relative_reloc(false)
6814 || target
->abiversion() >= 2)
6815 && gsym
->visibility() == elfcpp::STV_PROTECTED
6816 && parameters
->options().shared()))
6818 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6819 : elfcpp::R_POWERPC_RELATIVE
);
6820 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
6824 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
6825 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
6831 case elfcpp::R_PPC64_TOC16
:
6832 case elfcpp::R_PPC64_TOC16_LO
:
6833 case elfcpp::R_PPC64_TOC16_HI
:
6834 case elfcpp::R_PPC64_TOC16_HA
:
6835 case elfcpp::R_PPC64_TOC16_DS
:
6836 case elfcpp::R_PPC64_TOC16_LO_DS
:
6837 // We need a GOT section.
6838 target
->got_section(symtab
, layout
);
6841 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6842 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6843 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6844 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6846 const bool final
= gsym
->final_value_is_known();
6847 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6848 if (tls_type
== tls::TLSOPT_NONE
)
6850 Output_data_got_powerpc
<size
, big_endian
>* got
6851 = target
->got_section(symtab
, layout
);
6852 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6853 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
, rela_dyn
,
6854 elfcpp::R_POWERPC_DTPMOD
,
6855 elfcpp::R_POWERPC_DTPREL
);
6857 else if (tls_type
== tls::TLSOPT_TO_IE
)
6859 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
6861 Output_data_got_powerpc
<size
, big_endian
>* got
6862 = target
->got_section(symtab
, layout
);
6863 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6864 if (gsym
->is_undefined()
6865 || gsym
->is_from_dynobj())
6867 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
6868 elfcpp::R_POWERPC_TPREL
);
6872 unsigned int off
= got
->add_constant(0);
6873 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
6874 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
6875 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
6880 else if (tls_type
== tls::TLSOPT_TO_LE
)
6882 // no GOT relocs needed for Local Exec.
6889 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6890 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6891 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6892 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6894 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6895 if (tls_type
== tls::TLSOPT_NONE
)
6896 target
->tlsld_got_offset(symtab
, layout
, object
);
6897 else if (tls_type
== tls::TLSOPT_TO_LE
)
6899 // no GOT relocs needed for Local Exec.
6900 if (parameters
->options().emit_relocs())
6902 Output_section
* os
= layout
->tls_segment()->first_section();
6903 gold_assert(os
!= NULL
);
6904 os
->set_needs_symtab_index();
6912 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6913 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6914 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6915 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6917 Output_data_got_powerpc
<size
, big_endian
>* got
6918 = target
->got_section(symtab
, layout
);
6919 if (!gsym
->final_value_is_known()
6920 && (gsym
->is_from_dynobj()
6921 || gsym
->is_undefined()
6922 || gsym
->is_preemptible()))
6923 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
6924 target
->rela_dyn_section(layout
),
6925 elfcpp::R_POWERPC_DTPREL
);
6927 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
6931 case elfcpp::R_POWERPC_GOT_TPREL16
:
6932 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6933 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6934 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6936 const bool final
= gsym
->final_value_is_known();
6937 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
6938 if (tls_type
== tls::TLSOPT_NONE
)
6940 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
6942 Output_data_got_powerpc
<size
, big_endian
>* got
6943 = target
->got_section(symtab
, layout
);
6944 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6945 if (gsym
->is_undefined()
6946 || gsym
->is_from_dynobj())
6948 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
6949 elfcpp::R_POWERPC_TPREL
);
6953 unsigned int off
= got
->add_constant(0);
6954 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
6955 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
6956 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
6961 else if (tls_type
== tls::TLSOPT_TO_LE
)
6963 // no GOT relocs needed for Local Exec.
6971 unsupported_reloc_global(object
, r_type
, gsym
);
6976 && parameters
->options().toc_optimize())
6978 if (data_shndx
== ppc_object
->toc_shndx())
6981 if (r_type
!= elfcpp::R_PPC64_ADDR64
6982 || (is_ifunc
&& target
->abiversion() < 2))
6984 else if (parameters
->options().output_is_position_independent()
6985 && (is_ifunc
|| gsym
->is_absolute() || gsym
->is_undefined()))
6988 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
6991 enum {no_check
, check_lo
, check_ha
} insn_check
;
6995 insn_check
= no_check
;
6998 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6999 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7000 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7001 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7002 case elfcpp::R_POWERPC_GOT16_HA
:
7003 case elfcpp::R_PPC64_TOC16_HA
:
7004 insn_check
= check_ha
;
7007 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7008 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7009 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7010 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7011 case elfcpp::R_POWERPC_GOT16_LO
:
7012 case elfcpp::R_PPC64_GOT16_LO_DS
:
7013 case elfcpp::R_PPC64_TOC16_LO
:
7014 case elfcpp::R_PPC64_TOC16_LO_DS
:
7015 insn_check
= check_lo
;
7019 section_size_type slen
;
7020 const unsigned char* view
= NULL
;
7021 if (insn_check
!= no_check
)
7023 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
7024 section_size_type off
=
7025 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
7028 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
7029 if (insn_check
== check_lo
7030 ? !ok_lo_toc_insn(insn
, r_type
)
7031 : ((insn
& ((0x3f << 26) | 0x1f << 16))
7032 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7034 ppc_object
->set_no_toc_opt();
7035 gold_warning(_("%s: toc optimization is not supported "
7036 "for %#08x instruction"),
7037 ppc_object
->name().c_str(), insn
);
7046 case elfcpp::R_PPC64_TOC16
:
7047 case elfcpp::R_PPC64_TOC16_LO
:
7048 case elfcpp::R_PPC64_TOC16_HI
:
7049 case elfcpp::R_PPC64_TOC16_HA
:
7050 case elfcpp::R_PPC64_TOC16_DS
:
7051 case elfcpp::R_PPC64_TOC16_LO_DS
:
7052 if (gsym
->source() == Symbol::FROM_OBJECT
7053 && !gsym
->object()->is_dynamic())
7055 Powerpc_relobj
<size
, big_endian
>* sym_object
7056 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7058 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
7059 if (shndx
== sym_object
->toc_shndx())
7061 Sized_symbol
<size
>* sym
= symtab
->get_sized_symbol
<size
>(gsym
);
7062 Address dst_off
= sym
->value() + reloc
.get_r_offset();
7063 if (dst_off
< sym_object
->section_size(shndx
))
7066 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
7068 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
7070 // Need to check that the insn is a ld
7072 view
= ppc_object
->section_contents(data_shndx
,
7075 section_size_type off
=
7076 (convert_to_section_size_type(reloc
.get_r_offset())
7077 + (big_endian
? -2 : 3));
7079 && (view
[off
] & (0x3f << 2)) == (58u << 2))
7083 sym_object
->set_no_toc_opt(dst_off
);
7095 case elfcpp::R_PPC_LOCAL24PC
:
7096 if (strcmp(gsym
->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7097 gold_error(_("%s: unsupported -mbss-plt code"),
7098 ppc_object
->name().c_str());
7107 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7108 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7109 case elfcpp::R_POWERPC_GOT_TPREL16
:
7110 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7111 case elfcpp::R_POWERPC_GOT16
:
7112 case elfcpp::R_PPC64_GOT16_DS
:
7113 case elfcpp::R_PPC64_TOC16
:
7114 case elfcpp::R_PPC64_TOC16_DS
:
7115 ppc_object
->set_has_small_toc_reloc();
7121 // Process relocations for gc.
7123 template<int size
, bool big_endian
>
7125 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
7126 Symbol_table
* symtab
,
7128 Sized_relobj_file
<size
, big_endian
>* object
,
7129 unsigned int data_shndx
,
7131 const unsigned char* prelocs
,
7133 Output_section
* output_section
,
7134 bool needs_special_offset_handling
,
7135 size_t local_symbol_count
,
7136 const unsigned char* plocal_symbols
)
7138 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
7139 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
7142 Powerpc_relobj
<size
, big_endian
>* ppc_object
7143 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
7145 ppc_object
->set_opd_valid();
7146 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
7148 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
7149 for (p
= ppc_object
->access_from_map()->begin();
7150 p
!= ppc_object
->access_from_map()->end();
7153 Address dst_off
= p
->first
;
7154 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
7155 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
7156 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
7158 Relobj
* src_obj
= s
->first
;
7159 unsigned int src_indx
= s
->second
;
7160 symtab
->gc()->add_reference(src_obj
, src_indx
,
7161 ppc_object
, dst_indx
);
7165 ppc_object
->access_from_map()->clear();
7166 ppc_object
->process_gc_mark(symtab
);
7167 // Don't look at .opd relocs as .opd will reference everything.
7171 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
7180 needs_special_offset_handling
,
7185 // Handle target specific gc actions when adding a gc reference from
7186 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7187 // and DST_OFF. For powerpc64, this adds a referenc to the code
7188 // section of a function descriptor.
7190 template<int size
, bool big_endian
>
7192 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
7193 Symbol_table
* symtab
,
7195 unsigned int src_shndx
,
7197 unsigned int dst_shndx
,
7198 Address dst_off
) const
7200 if (size
!= 64 || dst_obj
->is_dynamic())
7203 Powerpc_relobj
<size
, big_endian
>* ppc_object
7204 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
7205 if (dst_shndx
!= 0 && dst_shndx
== ppc_object
->opd_shndx())
7207 if (ppc_object
->opd_valid())
7209 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
7210 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
7214 // If we haven't run scan_opd_relocs, we must delay
7215 // processing this function descriptor reference.
7216 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
7221 // Add any special sections for this symbol to the gc work list.
7222 // For powerpc64, this adds the code section of a function
7225 template<int size
, bool big_endian
>
7227 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
7228 Symbol_table
* symtab
,
7233 Powerpc_relobj
<size
, big_endian
>* ppc_object
7234 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
7236 unsigned int shndx
= sym
->shndx(&is_ordinary
);
7237 if (is_ordinary
&& shndx
!= 0 && shndx
== ppc_object
->opd_shndx())
7239 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
7240 Address dst_off
= gsym
->value();
7241 if (ppc_object
->opd_valid())
7243 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
7244 symtab
->gc()->worklist().push_back(Section_id(ppc_object
,
7248 ppc_object
->add_gc_mark(dst_off
);
7253 // For a symbol location in .opd, set LOC to the location of the
7256 template<int size
, bool big_endian
>
7258 Target_powerpc
<size
, big_endian
>::do_function_location(
7259 Symbol_location
* loc
) const
7261 if (size
== 64 && loc
->shndx
!= 0)
7263 if (loc
->object
->is_dynamic())
7265 Powerpc_dynobj
<size
, big_endian
>* ppc_object
7266 = static_cast<Powerpc_dynobj
<size
, big_endian
>*>(loc
->object
);
7267 if (loc
->shndx
== ppc_object
->opd_shndx())
7270 Address off
= loc
->offset
- ppc_object
->opd_address();
7271 loc
->shndx
= ppc_object
->get_opd_ent(off
, &dest_off
);
7272 loc
->offset
= dest_off
;
7277 const Powerpc_relobj
<size
, big_endian
>* ppc_object
7278 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(loc
->object
);
7279 if (loc
->shndx
== ppc_object
->opd_shndx())
7282 loc
->shndx
= ppc_object
->get_opd_ent(loc
->offset
, &dest_off
);
7283 loc
->offset
= dest_off
;
7289 // FNOFFSET in section SHNDX in OBJECT is the start of a function
7290 // compiled with -fsplit-stack. The function calls non-split-stack
7291 // code. Change the function to ensure it has enough stack space to
7292 // call some random function.
7294 template<int size
, bool big_endian
>
7296 Target_powerpc
<size
, big_endian
>::do_calls_non_split(
7299 section_offset_type fnoffset
,
7300 section_size_type fnsize
,
7301 const unsigned char* prelocs
,
7303 unsigned char* view
,
7304 section_size_type view_size
,
7306 std::string
* to
) const
7308 // 32-bit not supported.
7312 Target::do_calls_non_split(object
, shndx
, fnoffset
, fnsize
,
7313 prelocs
, reloc_count
, view
, view_size
,
7318 // The function always starts with
7319 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
7320 // addis %r12,%r1,-allocate@ha
7321 // addi %r12,%r12,-allocate@l
7323 // but note that the addis or addi may be replaced with a nop
7325 unsigned char *entry
= view
+ fnoffset
;
7326 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
7328 if ((insn
& 0xffff0000) == addis_2_12
)
7330 /* Skip ELFv2 global entry code. */
7332 insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
7335 unsigned char *pinsn
= entry
;
7337 const uint32_t ld_private_ss
= 0xe80d8fc0;
7338 if (insn
== ld_private_ss
)
7340 int32_t allocate
= 0;
7344 insn
= elfcpp::Swap
<32, big_endian
>::readval(pinsn
);
7345 if ((insn
& 0xffff0000) == addis_12_1
)
7346 allocate
+= (insn
& 0xffff) << 16;
7347 else if ((insn
& 0xffff0000) == addi_12_1
7348 || (insn
& 0xffff0000) == addi_12_12
)
7349 allocate
+= ((insn
& 0xffff) ^ 0x8000) - 0x8000;
7350 else if (insn
!= nop
)
7353 if (insn
== cmpld_7_12_0
&& pinsn
== entry
+ 12)
7355 int extra
= parameters
->options().split_stack_adjust_size();
7357 if (allocate
>= 0 || extra
< 0)
7359 object
->error(_("split-stack stack size overflow at "
7360 "section %u offset %0zx"),
7361 shndx
, static_cast<size_t>(fnoffset
));
7365 insn
= addis_12_1
| (((allocate
+ 0x8000) >> 16) & 0xffff);
7366 if (insn
!= addis_12_1
)
7368 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7370 insn
= addi_12_12
| (allocate
& 0xffff);
7371 if (insn
!= addi_12_12
)
7373 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7379 insn
= addi_12_1
| (allocate
& 0xffff);
7380 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7383 if (pinsn
!= entry
+ 12)
7384 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, nop
);
7392 if (!object
->has_no_split_stack())
7393 object
->error(_("failed to match split-stack sequence at "
7394 "section %u offset %0zx"),
7395 shndx
, static_cast<size_t>(fnoffset
));
7399 // Scan relocations for a section.
7401 template<int size
, bool big_endian
>
7403 Target_powerpc
<size
, big_endian
>::scan_relocs(
7404 Symbol_table
* symtab
,
7406 Sized_relobj_file
<size
, big_endian
>* object
,
7407 unsigned int data_shndx
,
7408 unsigned int sh_type
,
7409 const unsigned char* prelocs
,
7411 Output_section
* output_section
,
7412 bool needs_special_offset_handling
,
7413 size_t local_symbol_count
,
7414 const unsigned char* plocal_symbols
)
7416 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
7417 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
7420 if (sh_type
== elfcpp::SHT_REL
)
7422 gold_error(_("%s: unsupported REL reloc section"),
7423 object
->name().c_str());
7427 gold::scan_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
7436 needs_special_offset_handling
,
7441 // Functor class for processing the global symbol table.
7442 // Removes symbols defined on discarded opd entries.
7444 template<bool big_endian
>
7445 class Global_symbol_visitor_opd
7448 Global_symbol_visitor_opd()
7452 operator()(Sized_symbol
<64>* sym
)
7454 if (sym
->has_symtab_index()
7455 || sym
->source() != Symbol::FROM_OBJECT
7456 || !sym
->in_real_elf())
7459 if (sym
->object()->is_dynamic())
7462 Powerpc_relobj
<64, big_endian
>* symobj
7463 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
7464 if (symobj
->opd_shndx() == 0)
7468 unsigned int shndx
= sym
->shndx(&is_ordinary
);
7469 if (shndx
== symobj
->opd_shndx()
7470 && symobj
->get_opd_discard(sym
->value()))
7472 sym
->set_undefined();
7473 sym
->set_visibility(elfcpp::STV_DEFAULT
);
7474 sym
->set_is_defined_in_discarded_section();
7475 sym
->set_symtab_index(-1U);
7480 template<int size
, bool big_endian
>
7482 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
7484 Symbol_table
* symtab
)
7488 Output_data_save_res
<size
, big_endian
>* savres
7489 = new Output_data_save_res
<size
, big_endian
>(symtab
);
7490 this->savres_section_
= savres
;
7491 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
7492 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
7493 savres
, ORDER_TEXT
, false);
7497 // Sort linker created .got section first (for the header), then input
7498 // sections belonging to files using small model code.
7500 template<bool big_endian
>
7501 class Sort_toc_sections
7505 operator()(const Output_section::Input_section
& is1
,
7506 const Output_section::Input_section
& is2
) const
7508 if (!is1
.is_input_section() && is2
.is_input_section())
7511 = (is1
.is_input_section()
7512 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
7513 ->has_small_toc_reloc()));
7515 = (is2
.is_input_section()
7516 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
7517 ->has_small_toc_reloc()));
7518 return small1
&& !small2
;
7522 // Finalize the sections.
7524 template<int size
, bool big_endian
>
7526 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
7528 const Input_objects
*,
7529 Symbol_table
* symtab
)
7531 if (parameters
->doing_static_link())
7533 // At least some versions of glibc elf-init.o have a strong
7534 // reference to __rela_iplt marker syms. A weak ref would be
7536 if (this->iplt_
!= NULL
)
7538 Reloc_section
* rel
= this->iplt_
->rel_plt();
7539 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
7540 Symbol_table::PREDEFINED
, rel
, 0, 0,
7541 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7542 elfcpp::STV_HIDDEN
, 0, false, true);
7543 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
7544 Symbol_table::PREDEFINED
, rel
, 0, 0,
7545 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7546 elfcpp::STV_HIDDEN
, 0, true, true);
7550 symtab
->define_as_constant("__rela_iplt_start", NULL
,
7551 Symbol_table::PREDEFINED
, 0, 0,
7552 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7553 elfcpp::STV_HIDDEN
, 0, true, false);
7554 symtab
->define_as_constant("__rela_iplt_end", NULL
,
7555 Symbol_table::PREDEFINED
, 0, 0,
7556 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7557 elfcpp::STV_HIDDEN
, 0, true, false);
7563 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
7564 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
7566 if (!parameters
->options().relocatable())
7568 this->define_save_restore_funcs(layout
, symtab
);
7570 // Annoyingly, we need to make these sections now whether or
7571 // not we need them. If we delay until do_relax then we
7572 // need to mess with the relaxation machinery checkpointing.
7573 this->got_section(symtab
, layout
);
7574 this->make_brlt_section(layout
);
7576 if (parameters
->options().toc_sort())
7578 Output_section
* os
= this->got_
->output_section();
7579 if (os
!= NULL
&& os
->input_sections().size() > 1)
7580 std::stable_sort(os
->input_sections().begin(),
7581 os
->input_sections().end(),
7582 Sort_toc_sections
<big_endian
>());
7587 // Fill in some more dynamic tags.
7588 Output_data_dynamic
* odyn
= layout
->dynamic_data();
7591 const Reloc_section
* rel_plt
= (this->plt_
== NULL
7593 : this->plt_
->rel_plt());
7594 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
7595 this->rela_dyn_
, true, size
== 32);
7599 if (this->got_
!= NULL
)
7601 this->got_
->finalize_data_size();
7602 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
7603 this->got_
, this->got_
->g_o_t());
7608 if (this->glink_
!= NULL
)
7610 this->glink_
->finalize_data_size();
7611 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
7613 (this->glink_
->pltresolve_size
7619 // Emit any relocs we saved in an attempt to avoid generating COPY
7621 if (this->copy_relocs_
.any_saved_relocs())
7622 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
7625 // Emit any saved relocs, and mark toc entries using any of these
7626 // relocs as not optimizable.
7628 template<int sh_type
, int size
, bool big_endian
>
7630 Powerpc_copy_relocs
<sh_type
, size
, big_endian
>::emit(
7631 Output_data_reloc
<sh_type
, true, size
, big_endian
>* reloc_section
)
7634 && parameters
->options().toc_optimize())
7636 for (typename Copy_relocs
<sh_type
, size
, big_endian
>::
7637 Copy_reloc_entries::iterator p
= this->entries_
.begin();
7638 p
!= this->entries_
.end();
7641 typename Copy_relocs
<sh_type
, size
, big_endian
>::Copy_reloc_entry
&
7644 // If the symbol is no longer defined in a dynamic object,
7645 // then we emitted a COPY relocation. If it is still
7646 // dynamic then we'll need dynamic relocations and thus
7647 // can't optimize toc entries.
7648 if (entry
.sym_
->is_from_dynobj())
7650 Powerpc_relobj
<size
, big_endian
>* ppc_object
7651 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(entry
.relobj_
);
7652 if (entry
.shndx_
== ppc_object
->toc_shndx())
7653 ppc_object
->set_no_toc_opt(entry
.address_
);
7658 Copy_relocs
<sh_type
, size
, big_endian
>::emit(reloc_section
);
7661 // Return the value to use for a branch relocation.
7663 template<int size
, bool big_endian
>
7665 Target_powerpc
<size
, big_endian
>::symval_for_branch(
7666 const Symbol_table
* symtab
,
7667 const Sized_symbol
<size
>* gsym
,
7668 Powerpc_relobj
<size
, big_endian
>* object
,
7670 unsigned int *dest_shndx
)
7672 if (size
== 32 || this->abiversion() >= 2)
7676 // If the symbol is defined in an opd section, ie. is a function
7677 // descriptor, use the function descriptor code entry address
7678 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
7680 && (gsym
->source() != Symbol::FROM_OBJECT
7681 || gsym
->object()->is_dynamic()))
7684 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7685 unsigned int shndx
= symobj
->opd_shndx();
7688 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
7689 if (opd_addr
== invalid_address
)
7691 opd_addr
+= symobj
->output_section_address(shndx
);
7692 if (*value
>= opd_addr
&& *value
< opd_addr
+ symobj
->section_size(shndx
))
7695 *dest_shndx
= symobj
->get_opd_ent(*value
- opd_addr
, &sec_off
);
7696 if (symtab
->is_section_folded(symobj
, *dest_shndx
))
7699 = symtab
->icf()->get_folded_section(symobj
, *dest_shndx
);
7700 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(folded
.first
);
7701 *dest_shndx
= folded
.second
;
7703 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
7704 if (sec_addr
== invalid_address
)
7707 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
7708 *value
= sec_addr
+ sec_off
;
7713 // Perform a relocation.
7715 template<int size
, bool big_endian
>
7717 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
7718 const Relocate_info
<size
, big_endian
>* relinfo
,
7720 Target_powerpc
* target
,
7723 const unsigned char* preloc
,
7724 const Sized_symbol
<size
>* gsym
,
7725 const Symbol_value
<size
>* psymval
,
7726 unsigned char* view
,
7728 section_size_type view_size
)
7733 const elfcpp::Rela
<size
, big_endian
> rela(preloc
);
7734 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
7735 switch (this->maybe_skip_tls_get_addr_call(r_type
, gsym
))
7737 case Track_tls::NOT_EXPECTED
:
7738 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7739 _("__tls_get_addr call lacks marker reloc"));
7741 case Track_tls::EXPECTED
:
7742 // We have already complained.
7744 case Track_tls::SKIP
:
7746 case Track_tls::NORMAL
:
7750 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
7751 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
7752 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
7753 // Offset from start of insn to d-field reloc.
7754 const int d_offset
= big_endian
? 2 : 0;
7756 Powerpc_relobj
<size
, big_endian
>* const object
7757 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
7759 bool has_stub_value
= false;
7760 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7762 ? gsym
->use_plt_offset(Scan::get_reference_flags(r_type
, target
))
7763 : object
->local_has_plt_offset(r_sym
))
7764 && (!psymval
->is_ifunc_symbol()
7765 || Scan::reloc_needs_plt_for_ifunc(target
, object
, r_type
, false)))
7769 && target
->abiversion() >= 2
7770 && !parameters
->options().output_is_position_independent()
7771 && !is_branch_reloc(r_type
))
7773 Address off
= target
->glink_section()->find_global_entry(gsym
);
7774 if (off
!= invalid_address
)
7776 value
= target
->glink_section()->global_entry_address() + off
;
7777 has_stub_value
= true;
7782 Stub_table
<size
, big_endian
>* stub_table
7783 = object
->stub_table(relinfo
->data_shndx
);
7784 if (stub_table
== NULL
)
7786 // This is a ref from a data section to an ifunc symbol.
7787 if (target
->stub_tables().size() != 0)
7788 stub_table
= target
->stub_tables()[0];
7790 if (stub_table
!= NULL
)
7794 off
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
7795 rela
.get_r_addend());
7797 off
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
7798 rela
.get_r_addend());
7799 if (off
!= invalid_address
)
7801 value
= stub_table
->stub_address() + off
;
7802 has_stub_value
= true;
7806 // We don't care too much about bogus debug references to
7807 // non-local functions, but otherwise there had better be a plt
7808 // call stub or global entry stub as appropriate.
7809 gold_assert(has_stub_value
|| !(os
->flags() & elfcpp::SHF_ALLOC
));
7812 if (r_type
== elfcpp::R_POWERPC_GOT16
7813 || r_type
== elfcpp::R_POWERPC_GOT16_LO
7814 || r_type
== elfcpp::R_POWERPC_GOT16_HI
7815 || r_type
== elfcpp::R_POWERPC_GOT16_HA
7816 || r_type
== elfcpp::R_PPC64_GOT16_DS
7817 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
7821 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
7822 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
7826 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
7827 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
7829 value
-= target
->got_section()->got_base_offset(object
);
7831 else if (r_type
== elfcpp::R_PPC64_TOC
)
7833 value
= (target
->got_section()->output_section()->address()
7834 + object
->toc_base_offset());
7836 else if (gsym
!= NULL
7837 && (r_type
== elfcpp::R_POWERPC_REL24
7838 || r_type
== elfcpp::R_PPC_PLTREL24
)
7843 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
7844 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
7845 bool can_plt_call
= false;
7846 if (rela
.get_r_offset() + 8 <= view_size
)
7848 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
7849 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
7852 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
7854 elfcpp::Swap
<32, big_endian
>::
7855 writeval(wv
+ 1, ld_2_1
+ target
->stk_toc());
7856 can_plt_call
= true;
7861 // If we don't have a branch and link followed by a nop,
7862 // we can't go via the plt because there is no place to
7863 // put a toc restoring instruction.
7864 // Unless we know we won't be returning.
7865 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
7866 can_plt_call
= true;
7870 // g++ as of 20130507 emits self-calls without a
7871 // following nop. This is arguably wrong since we have
7872 // conflicting information. On the one hand a global
7873 // symbol and on the other a local call sequence, but
7874 // don't error for this special case.
7875 // It isn't possible to cheaply verify we have exactly
7876 // such a call. Allow all calls to the same section.
7878 Address code
= value
;
7879 if (gsym
->source() == Symbol::FROM_OBJECT
7880 && gsym
->object() == object
)
7882 unsigned int dest_shndx
= 0;
7883 if (target
->abiversion() < 2)
7885 Address addend
= rela
.get_r_addend();
7886 code
= psymval
->value(object
, addend
);
7887 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
7888 &code
, &dest_shndx
);
7891 if (dest_shndx
== 0)
7892 dest_shndx
= gsym
->shndx(&is_ordinary
);
7893 ok
= dest_shndx
== relinfo
->data_shndx
;
7897 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7898 _("call lacks nop, can't restore toc; "
7899 "recompile with -fPIC"));
7905 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7906 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
7907 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
7908 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
7910 // First instruction of a global dynamic sequence, arg setup insn.
7911 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7912 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7913 enum Got_type got_type
= GOT_TYPE_STANDARD
;
7914 if (tls_type
== tls::TLSOPT_NONE
)
7915 got_type
= GOT_TYPE_TLSGD
;
7916 else if (tls_type
== tls::TLSOPT_TO_IE
)
7917 got_type
= GOT_TYPE_TPREL
;
7918 if (got_type
!= GOT_TYPE_STANDARD
)
7922 gold_assert(gsym
->has_got_offset(got_type
));
7923 value
= gsym
->got_offset(got_type
);
7927 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
7928 value
= object
->local_got_offset(r_sym
, got_type
);
7930 value
-= target
->got_section()->got_base_offset(object
);
7932 if (tls_type
== tls::TLSOPT_TO_IE
)
7934 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7935 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
7937 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
7938 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7939 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
7941 insn
|= 32 << 26; // lwz
7943 insn
|= 58 << 26; // ld
7944 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7946 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
7947 - elfcpp::R_POWERPC_GOT_TLSGD16
);
7949 else if (tls_type
== tls::TLSOPT_TO_LE
)
7951 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7952 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
7954 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
7955 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7956 insn
&= (1 << 26) - (1 << 21); // extract rt
7961 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7962 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7963 value
= psymval
->value(object
, rela
.get_r_addend());
7967 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
7969 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7970 r_type
= elfcpp::R_POWERPC_NONE
;
7974 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7975 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
7976 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
7977 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
7979 // First instruction of a local dynamic sequence, arg setup insn.
7980 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7981 if (tls_type
== tls::TLSOPT_NONE
)
7983 value
= target
->tlsld_got_offset();
7984 value
-= target
->got_section()->got_base_offset(object
);
7988 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
7989 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7990 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
7992 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
7993 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7994 insn
&= (1 << 26) - (1 << 21); // extract rt
7999 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8000 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8005 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8007 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8008 r_type
= elfcpp::R_POWERPC_NONE
;
8012 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
8013 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
8014 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
8015 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
8017 // Accesses relative to a local dynamic sequence address,
8018 // no optimisation here.
8021 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
8022 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
8026 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
8027 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
8029 value
-= target
->got_section()->got_base_offset(object
);
8031 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8032 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
8033 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
8034 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
8036 // First instruction of initial exec sequence.
8037 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8038 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
8039 if (tls_type
== tls::TLSOPT_NONE
)
8043 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
8044 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
8048 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
8049 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
8051 value
-= target
->got_section()->got_base_offset(object
);
8055 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
8056 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8057 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
8059 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8060 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8061 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
8066 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8067 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8068 value
= psymval
->value(object
, rela
.get_r_addend());
8072 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8074 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8075 r_type
= elfcpp::R_POWERPC_NONE
;
8079 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
8080 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
8082 // Second instruction of a global dynamic sequence,
8083 // the __tls_get_addr call
8084 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
8085 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8086 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
8087 if (tls_type
!= tls::TLSOPT_NONE
)
8089 if (tls_type
== tls::TLSOPT_TO_IE
)
8091 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8092 Insn insn
= add_3_3_13
;
8095 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8096 r_type
= elfcpp::R_POWERPC_NONE
;
8100 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8101 Insn insn
= addi_3_3
;
8102 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8103 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8105 value
= psymval
->value(object
, rela
.get_r_addend());
8107 this->skip_next_tls_get_addr_call();
8110 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
8111 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
8113 // Second instruction of a local dynamic sequence,
8114 // the __tls_get_addr call
8115 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
8116 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
8117 if (tls_type
== tls::TLSOPT_TO_LE
)
8119 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8120 Insn insn
= addi_3_3
;
8121 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8122 this->skip_next_tls_get_addr_call();
8123 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8128 else if (r_type
== elfcpp::R_POWERPC_TLS
)
8130 // Second instruction of an initial exec sequence
8131 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8132 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
8133 if (tls_type
== tls::TLSOPT_TO_LE
)
8135 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8136 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8137 unsigned int reg
= size
== 32 ? 2 : 13;
8138 insn
= at_tls_transform(insn
, reg
);
8139 gold_assert(insn
!= 0);
8140 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8141 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8143 value
= psymval
->value(object
, rela
.get_r_addend());
8146 else if (!has_stub_value
)
8149 if (!(size
== 32 && r_type
== elfcpp::R_PPC_PLTREL24
))
8150 addend
= rela
.get_r_addend();
8151 value
= psymval
->value(object
, addend
);
8152 if (size
== 64 && is_branch_reloc(r_type
))
8154 if (target
->abiversion() >= 2)
8157 value
+= object
->ppc64_local_entry_offset(gsym
);
8159 value
+= object
->ppc64_local_entry_offset(r_sym
);
8163 unsigned int dest_shndx
;
8164 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
8165 &value
, &dest_shndx
);
8168 Address max_branch_offset
= max_branch_delta(r_type
);
8169 if (max_branch_offset
!= 0
8170 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
8172 Stub_table
<size
, big_endian
>* stub_table
8173 = object
->stub_table(relinfo
->data_shndx
);
8174 if (stub_table
!= NULL
)
8176 Address off
= stub_table
->find_long_branch_entry(object
, value
);
8177 if (off
!= invalid_address
)
8179 value
= (stub_table
->stub_address() + stub_table
->plt_size()
8181 has_stub_value
= true;
8189 case elfcpp::R_PPC64_REL64
:
8190 case elfcpp::R_POWERPC_REL32
:
8191 case elfcpp::R_POWERPC_REL24
:
8192 case elfcpp::R_PPC_PLTREL24
:
8193 case elfcpp::R_PPC_LOCAL24PC
:
8194 case elfcpp::R_POWERPC_REL16
:
8195 case elfcpp::R_POWERPC_REL16_LO
:
8196 case elfcpp::R_POWERPC_REL16_HI
:
8197 case elfcpp::R_POWERPC_REL16_HA
:
8198 case elfcpp::R_POWERPC_REL16DX_HA
:
8199 case elfcpp::R_POWERPC_REL14
:
8200 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8201 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8205 case elfcpp::R_PPC64_TOC16
:
8206 case elfcpp::R_PPC64_TOC16_LO
:
8207 case elfcpp::R_PPC64_TOC16_HI
:
8208 case elfcpp::R_PPC64_TOC16_HA
:
8209 case elfcpp::R_PPC64_TOC16_DS
:
8210 case elfcpp::R_PPC64_TOC16_LO_DS
:
8211 // Subtract the TOC base address.
8212 value
-= (target
->got_section()->output_section()->address()
8213 + object
->toc_base_offset());
8216 case elfcpp::R_POWERPC_SECTOFF
:
8217 case elfcpp::R_POWERPC_SECTOFF_LO
:
8218 case elfcpp::R_POWERPC_SECTOFF_HI
:
8219 case elfcpp::R_POWERPC_SECTOFF_HA
:
8220 case elfcpp::R_PPC64_SECTOFF_DS
:
8221 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
8223 value
-= os
->address();
8226 case elfcpp::R_PPC64_TPREL16_DS
:
8227 case elfcpp::R_PPC64_TPREL16_LO_DS
:
8228 case elfcpp::R_PPC64_TPREL16_HIGH
:
8229 case elfcpp::R_PPC64_TPREL16_HIGHA
:
8231 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
8234 case elfcpp::R_POWERPC_TPREL16
:
8235 case elfcpp::R_POWERPC_TPREL16_LO
:
8236 case elfcpp::R_POWERPC_TPREL16_HI
:
8237 case elfcpp::R_POWERPC_TPREL16_HA
:
8238 case elfcpp::R_POWERPC_TPREL
:
8239 case elfcpp::R_PPC64_TPREL16_HIGHER
:
8240 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
8241 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
8242 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
8243 // tls symbol values are relative to tls_segment()->vaddr()
8247 case elfcpp::R_PPC64_DTPREL16_DS
:
8248 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
8249 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
8250 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
8251 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
8252 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
8254 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
8255 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
8258 case elfcpp::R_POWERPC_DTPREL16
:
8259 case elfcpp::R_POWERPC_DTPREL16_LO
:
8260 case elfcpp::R_POWERPC_DTPREL16_HI
:
8261 case elfcpp::R_POWERPC_DTPREL16_HA
:
8262 case elfcpp::R_POWERPC_DTPREL
:
8263 case elfcpp::R_PPC64_DTPREL16_HIGH
:
8264 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
8265 // tls symbol values are relative to tls_segment()->vaddr()
8266 value
-= dtp_offset
;
8269 case elfcpp::R_PPC64_ADDR64_LOCAL
:
8271 value
+= object
->ppc64_local_entry_offset(gsym
);
8273 value
+= object
->ppc64_local_entry_offset(r_sym
);
8280 Insn branch_bit
= 0;
8283 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
8284 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8285 branch_bit
= 1 << 21;
8287 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
8288 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8290 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8291 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8294 if (this->is_isa_v2
)
8296 // Set 'a' bit. This is 0b00010 in BO field for branch
8297 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
8298 // for branch on CTR insns (BO == 1a00t or 1a01t).
8299 if ((insn
& (0x14 << 21)) == (0x04 << 21))
8301 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
8308 // Invert 'y' bit if not the default.
8309 if (static_cast<Signed_address
>(value
) < 0)
8312 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8327 // Multi-instruction sequences that access the GOT/TOC can
8328 // be optimized, eg.
8329 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
8330 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
8332 // addis ra,r2,0; addi rb,ra,x@toc@l;
8333 // to nop; addi rb,r2,x@toc;
8334 // FIXME: the @got sequence shown above is not yet
8335 // optimized. Note that gcc as of 2017-01-07 doesn't use
8336 // the ELF @got relocs except for TLS, instead using the
8337 // PowerOpen variant of a compiler managed GOT (called TOC).
8338 // The PowerOpen TOC sequence equivalent to the first
8339 // example is optimized.
8340 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
8341 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
8342 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
8343 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
8344 case elfcpp::R_POWERPC_GOT16_HA
:
8345 case elfcpp::R_PPC64_TOC16_HA
:
8346 if (parameters
->options().toc_optimize())
8348 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8349 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8350 if (r_type
== elfcpp::R_PPC64_TOC16_HA
8351 && object
->make_toc_relative(target
, &value
))
8353 gold_assert((insn
& ((0x3f << 26) | 0x1f << 16))
8354 == ((15u << 26) | (2 << 16)));
8356 if (((insn
& ((0x3f << 26) | 0x1f << 16))
8357 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8358 && value
+ 0x8000 < 0x10000)
8360 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
8366 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
8367 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
8368 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
8369 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
8370 case elfcpp::R_POWERPC_GOT16_LO
:
8371 case elfcpp::R_PPC64_GOT16_LO_DS
:
8372 case elfcpp::R_PPC64_TOC16_LO
:
8373 case elfcpp::R_PPC64_TOC16_LO_DS
:
8374 if (parameters
->options().toc_optimize())
8376 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8377 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8378 bool changed
= false;
8379 if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
8380 && object
->make_toc_relative(target
, &value
))
8382 gold_assert ((insn
& (0x3f << 26)) == 58u << 26 /* ld */);
8383 insn
^= (14u << 26) ^ (58u << 26);
8384 r_type
= elfcpp::R_PPC64_TOC16_LO
;
8387 if (ok_lo_toc_insn(insn
, r_type
)
8388 && value
+ 0x8000 < 0x10000)
8390 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
8392 // Transform addic to addi when we change reg.
8393 insn
&= ~((0x3f << 26) | (0x1f << 16));
8394 insn
|= (14u << 26) | (2 << 16);
8398 insn
&= ~(0x1f << 16);
8404 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8408 case elfcpp::R_PPC64_ENTRY
:
8409 value
= (target
->got_section()->output_section()->address()
8410 + object
->toc_base_offset());
8411 if (value
+ 0x80008000 <= 0xffffffff
8412 && !parameters
->options().output_is_position_independent())
8414 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8415 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8416 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
8418 if ((insn1
& ~0xfffc) == ld_2_12
8419 && insn2
== add_2_2_12
)
8421 insn1
= lis_2
+ ha(value
);
8422 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
8423 insn2
= addi_2_2
+ l(value
);
8424 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
8431 if (value
+ 0x80008000 <= 0xffffffff)
8433 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8434 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8435 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
8437 if ((insn1
& ~0xfffc) == ld_2_12
8438 && insn2
== add_2_2_12
)
8440 insn1
= addis_2_12
+ ha(value
);
8441 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
8442 insn2
= addi_2_2
+ l(value
);
8443 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
8450 case elfcpp::R_POWERPC_REL16_LO
:
8451 // If we are generating a non-PIC executable, edit
8452 // 0: addis 2,12,.TOC.-0b@ha
8453 // addi 2,2,.TOC.-0b@l
8454 // used by ELFv2 global entry points to set up r2, to
8457 // if .TOC. is in range. */
8458 if (value
+ address
- 4 + 0x80008000 <= 0xffffffff
8461 && target
->abiversion() >= 2
8462 && !parameters
->options().output_is_position_independent()
8463 && rela
.get_r_addend() == d_offset
+ 4
8465 && strcmp(gsym
->name(), ".TOC.") == 0)
8467 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
8468 Reltype
prev_rela(preloc
- reloc_size
);
8469 if ((prev_rela
.get_r_info()
8470 == elfcpp::elf_r_info
<size
>(r_sym
,
8471 elfcpp::R_POWERPC_REL16_HA
))
8472 && prev_rela
.get_r_offset() + 4 == rela
.get_r_offset()
8473 && prev_rela
.get_r_addend() + 4 == rela
.get_r_addend())
8475 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8476 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
- 1);
8477 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8479 if ((insn1
& 0xffff0000) == addis_2_12
8480 && (insn2
& 0xffff0000) == addi_2_2
)
8482 insn1
= lis_2
+ ha(value
+ address
- 4);
8483 elfcpp::Swap
<32, big_endian
>::writeval(iview
- 1, insn1
);
8484 insn2
= addi_2_2
+ l(value
+ address
- 4);
8485 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn2
);
8488 relinfo
->rr
->set_strategy(relnum
- 1,
8489 Relocatable_relocs::RELOC_SPECIAL
);
8490 relinfo
->rr
->set_strategy(relnum
,
8491 Relocatable_relocs::RELOC_SPECIAL
);
8501 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
8502 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->data_shdr
);
8505 case elfcpp::R_POWERPC_ADDR32
:
8506 case elfcpp::R_POWERPC_UADDR32
:
8508 overflow
= Reloc::CHECK_BITFIELD
;
8511 case elfcpp::R_POWERPC_REL32
:
8512 case elfcpp::R_POWERPC_REL16DX_HA
:
8514 overflow
= Reloc::CHECK_SIGNED
;
8517 case elfcpp::R_POWERPC_UADDR16
:
8518 overflow
= Reloc::CHECK_BITFIELD
;
8521 case elfcpp::R_POWERPC_ADDR16
:
8522 // We really should have three separate relocations,
8523 // one for 16-bit data, one for insns with 16-bit signed fields,
8524 // and one for insns with 16-bit unsigned fields.
8525 overflow
= Reloc::CHECK_BITFIELD
;
8526 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
8527 overflow
= Reloc::CHECK_LOW_INSN
;
8530 case elfcpp::R_POWERPC_ADDR16_HI
:
8531 case elfcpp::R_POWERPC_ADDR16_HA
:
8532 case elfcpp::R_POWERPC_GOT16_HI
:
8533 case elfcpp::R_POWERPC_GOT16_HA
:
8534 case elfcpp::R_POWERPC_PLT16_HI
:
8535 case elfcpp::R_POWERPC_PLT16_HA
:
8536 case elfcpp::R_POWERPC_SECTOFF_HI
:
8537 case elfcpp::R_POWERPC_SECTOFF_HA
:
8538 case elfcpp::R_PPC64_TOC16_HI
:
8539 case elfcpp::R_PPC64_TOC16_HA
:
8540 case elfcpp::R_PPC64_PLTGOT16_HI
:
8541 case elfcpp::R_PPC64_PLTGOT16_HA
:
8542 case elfcpp::R_POWERPC_TPREL16_HI
:
8543 case elfcpp::R_POWERPC_TPREL16_HA
:
8544 case elfcpp::R_POWERPC_DTPREL16_HI
:
8545 case elfcpp::R_POWERPC_DTPREL16_HA
:
8546 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
8547 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
8548 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
8549 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
8550 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
8551 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
8552 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
8553 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
8554 case elfcpp::R_POWERPC_REL16_HI
:
8555 case elfcpp::R_POWERPC_REL16_HA
:
8557 overflow
= Reloc::CHECK_HIGH_INSN
;
8560 case elfcpp::R_POWERPC_REL16
:
8561 case elfcpp::R_PPC64_TOC16
:
8562 case elfcpp::R_POWERPC_GOT16
:
8563 case elfcpp::R_POWERPC_SECTOFF
:
8564 case elfcpp::R_POWERPC_TPREL16
:
8565 case elfcpp::R_POWERPC_DTPREL16
:
8566 case elfcpp::R_POWERPC_GOT_TLSGD16
:
8567 case elfcpp::R_POWERPC_GOT_TLSLD16
:
8568 case elfcpp::R_POWERPC_GOT_TPREL16
:
8569 case elfcpp::R_POWERPC_GOT_DTPREL16
:
8570 overflow
= Reloc::CHECK_LOW_INSN
;
8573 case elfcpp::R_POWERPC_ADDR24
:
8574 case elfcpp::R_POWERPC_ADDR14
:
8575 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
8576 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
8577 case elfcpp::R_PPC64_ADDR16_DS
:
8578 case elfcpp::R_POWERPC_REL24
:
8579 case elfcpp::R_PPC_PLTREL24
:
8580 case elfcpp::R_PPC_LOCAL24PC
:
8581 case elfcpp::R_PPC64_TPREL16_DS
:
8582 case elfcpp::R_PPC64_DTPREL16_DS
:
8583 case elfcpp::R_PPC64_TOC16_DS
:
8584 case elfcpp::R_PPC64_GOT16_DS
:
8585 case elfcpp::R_PPC64_SECTOFF_DS
:
8586 case elfcpp::R_POWERPC_REL14
:
8587 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8588 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8589 overflow
= Reloc::CHECK_SIGNED
;
8593 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8596 if (overflow
== Reloc::CHECK_LOW_INSN
8597 || overflow
== Reloc::CHECK_HIGH_INSN
)
8599 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8601 if ((insn
& (0x3f << 26)) == 10u << 26 /* cmpli */)
8602 overflow
= Reloc::CHECK_BITFIELD
;
8603 else if (overflow
== Reloc::CHECK_LOW_INSN
8604 ? ((insn
& (0x3f << 26)) == 28u << 26 /* andi */
8605 || (insn
& (0x3f << 26)) == 24u << 26 /* ori */
8606 || (insn
& (0x3f << 26)) == 26u << 26 /* xori */)
8607 : ((insn
& (0x3f << 26)) == 29u << 26 /* andis */
8608 || (insn
& (0x3f << 26)) == 25u << 26 /* oris */
8609 || (insn
& (0x3f << 26)) == 27u << 26 /* xoris */))
8610 overflow
= Reloc::CHECK_UNSIGNED
;
8612 overflow
= Reloc::CHECK_SIGNED
;
8615 bool maybe_dq_reloc
= false;
8616 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
8617 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
8620 case elfcpp::R_POWERPC_NONE
:
8621 case elfcpp::R_POWERPC_TLS
:
8622 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
8623 case elfcpp::R_POWERPC_GNU_VTENTRY
:
8626 case elfcpp::R_PPC64_ADDR64
:
8627 case elfcpp::R_PPC64_REL64
:
8628 case elfcpp::R_PPC64_TOC
:
8629 case elfcpp::R_PPC64_ADDR64_LOCAL
:
8630 Reloc::addr64(view
, value
);
8633 case elfcpp::R_POWERPC_TPREL
:
8634 case elfcpp::R_POWERPC_DTPREL
:
8636 Reloc::addr64(view
, value
);
8638 status
= Reloc::addr32(view
, value
, overflow
);
8641 case elfcpp::R_PPC64_UADDR64
:
8642 Reloc::addr64_u(view
, value
);
8645 case elfcpp::R_POWERPC_ADDR32
:
8646 status
= Reloc::addr32(view
, value
, overflow
);
8649 case elfcpp::R_POWERPC_REL32
:
8650 case elfcpp::R_POWERPC_UADDR32
:
8651 status
= Reloc::addr32_u(view
, value
, overflow
);
8654 case elfcpp::R_POWERPC_ADDR24
:
8655 case elfcpp::R_POWERPC_REL24
:
8656 case elfcpp::R_PPC_PLTREL24
:
8657 case elfcpp::R_PPC_LOCAL24PC
:
8658 status
= Reloc::addr24(view
, value
, overflow
);
8661 case elfcpp::R_POWERPC_GOT_DTPREL16
:
8662 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
8663 case elfcpp::R_POWERPC_GOT_TPREL16
:
8664 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
8667 // On ppc64 these are all ds form
8668 maybe_dq_reloc
= true;
8672 case elfcpp::R_POWERPC_ADDR16
:
8673 case elfcpp::R_POWERPC_REL16
:
8674 case elfcpp::R_PPC64_TOC16
:
8675 case elfcpp::R_POWERPC_GOT16
:
8676 case elfcpp::R_POWERPC_SECTOFF
:
8677 case elfcpp::R_POWERPC_TPREL16
:
8678 case elfcpp::R_POWERPC_DTPREL16
:
8679 case elfcpp::R_POWERPC_GOT_TLSGD16
:
8680 case elfcpp::R_POWERPC_GOT_TLSLD16
:
8681 case elfcpp::R_POWERPC_ADDR16_LO
:
8682 case elfcpp::R_POWERPC_REL16_LO
:
8683 case elfcpp::R_PPC64_TOC16_LO
:
8684 case elfcpp::R_POWERPC_GOT16_LO
:
8685 case elfcpp::R_POWERPC_SECTOFF_LO
:
8686 case elfcpp::R_POWERPC_TPREL16_LO
:
8687 case elfcpp::R_POWERPC_DTPREL16_LO
:
8688 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
8689 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
8691 status
= Reloc::addr16(view
, value
, overflow
);
8693 maybe_dq_reloc
= true;
8696 case elfcpp::R_POWERPC_UADDR16
:
8697 status
= Reloc::addr16_u(view
, value
, overflow
);
8700 case elfcpp::R_PPC64_ADDR16_HIGH
:
8701 case elfcpp::R_PPC64_TPREL16_HIGH
:
8702 case elfcpp::R_PPC64_DTPREL16_HIGH
:
8704 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
8707 case elfcpp::R_POWERPC_ADDR16_HI
:
8708 case elfcpp::R_POWERPC_REL16_HI
:
8709 case elfcpp::R_PPC64_TOC16_HI
:
8710 case elfcpp::R_POWERPC_GOT16_HI
:
8711 case elfcpp::R_POWERPC_SECTOFF_HI
:
8712 case elfcpp::R_POWERPC_TPREL16_HI
:
8713 case elfcpp::R_POWERPC_DTPREL16_HI
:
8714 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
8715 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
8716 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
8717 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
8718 Reloc::addr16_hi(view
, value
);
8721 case elfcpp::R_PPC64_ADDR16_HIGHA
:
8722 case elfcpp::R_PPC64_TPREL16_HIGHA
:
8723 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
8725 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
8728 case elfcpp::R_POWERPC_ADDR16_HA
:
8729 case elfcpp::R_POWERPC_REL16_HA
:
8730 case elfcpp::R_PPC64_TOC16_HA
:
8731 case elfcpp::R_POWERPC_GOT16_HA
:
8732 case elfcpp::R_POWERPC_SECTOFF_HA
:
8733 case elfcpp::R_POWERPC_TPREL16_HA
:
8734 case elfcpp::R_POWERPC_DTPREL16_HA
:
8735 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
8736 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
8737 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
8738 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
8739 Reloc::addr16_ha(view
, value
);
8742 case elfcpp::R_POWERPC_REL16DX_HA
:
8743 status
= Reloc::addr16dx_ha(view
, value
, overflow
);
8746 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
8748 // R_PPC_EMB_NADDR16_LO
8751 case elfcpp::R_PPC64_ADDR16_HIGHER
:
8752 case elfcpp::R_PPC64_TPREL16_HIGHER
:
8753 Reloc::addr16_hi2(view
, value
);
8756 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
8758 // R_PPC_EMB_NADDR16_HI
8761 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
8762 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
8763 Reloc::addr16_ha2(view
, value
);
8766 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
8768 // R_PPC_EMB_NADDR16_HA
8771 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
8772 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
8773 Reloc::addr16_hi3(view
, value
);
8776 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
8781 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
8782 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
8783 Reloc::addr16_ha3(view
, value
);
8786 case elfcpp::R_PPC64_DTPREL16_DS
:
8787 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
8789 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
8792 case elfcpp::R_PPC64_TPREL16_DS
:
8793 case elfcpp::R_PPC64_TPREL16_LO_DS
:
8795 // R_PPC_TLSGD, R_PPC_TLSLD
8798 case elfcpp::R_PPC64_ADDR16_DS
:
8799 case elfcpp::R_PPC64_ADDR16_LO_DS
:
8800 case elfcpp::R_PPC64_TOC16_DS
:
8801 case elfcpp::R_PPC64_TOC16_LO_DS
:
8802 case elfcpp::R_PPC64_GOT16_DS
:
8803 case elfcpp::R_PPC64_GOT16_LO_DS
:
8804 case elfcpp::R_PPC64_SECTOFF_DS
:
8805 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
8806 maybe_dq_reloc
= true;
8809 case elfcpp::R_POWERPC_ADDR14
:
8810 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
8811 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
8812 case elfcpp::R_POWERPC_REL14
:
8813 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8814 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8815 status
= Reloc::addr14(view
, value
, overflow
);
8818 case elfcpp::R_POWERPC_COPY
:
8819 case elfcpp::R_POWERPC_GLOB_DAT
:
8820 case elfcpp::R_POWERPC_JMP_SLOT
:
8821 case elfcpp::R_POWERPC_RELATIVE
:
8822 case elfcpp::R_POWERPC_DTPMOD
:
8823 case elfcpp::R_PPC64_JMP_IREL
:
8824 case elfcpp::R_POWERPC_IRELATIVE
:
8825 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8826 _("unexpected reloc %u in object file"),
8830 case elfcpp::R_PPC_EMB_SDA21
:
8835 // R_PPC64_TOCSAVE. For the time being this can be ignored.
8839 case elfcpp::R_PPC_EMB_SDA2I16
:
8840 case elfcpp::R_PPC_EMB_SDA2REL
:
8843 // R_PPC64_TLSGD, R_PPC64_TLSLD
8846 case elfcpp::R_POWERPC_PLT32
:
8847 case elfcpp::R_POWERPC_PLTREL32
:
8848 case elfcpp::R_POWERPC_PLT16_LO
:
8849 case elfcpp::R_POWERPC_PLT16_HI
:
8850 case elfcpp::R_POWERPC_PLT16_HA
:
8851 case elfcpp::R_PPC_SDAREL16
:
8852 case elfcpp::R_POWERPC_ADDR30
:
8853 case elfcpp::R_PPC64_PLT64
:
8854 case elfcpp::R_PPC64_PLTREL64
:
8855 case elfcpp::R_PPC64_PLTGOT16
:
8856 case elfcpp::R_PPC64_PLTGOT16_LO
:
8857 case elfcpp::R_PPC64_PLTGOT16_HI
:
8858 case elfcpp::R_PPC64_PLTGOT16_HA
:
8859 case elfcpp::R_PPC64_PLT16_LO_DS
:
8860 case elfcpp::R_PPC64_PLTGOT16_DS
:
8861 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
8862 case elfcpp::R_PPC_EMB_RELSDA
:
8863 case elfcpp::R_PPC_TOC16
:
8866 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8867 _("unsupported reloc %u"),
8875 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8877 if ((insn
& (0x3f << 26)) == 56u << 26 /* lq */
8878 || ((insn
& (0x3f << 26)) == (61u << 26) /* lxv, stxv */
8879 && (insn
& 3) == 1))
8880 status
= Reloc::addr16_dq(view
, value
, overflow
);
8882 || (insn
& (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
8883 || (insn
& (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
8884 || (insn
& (0x3f << 26)) == 57u << 26 /* lfdp */
8885 || (insn
& (0x3f << 26)) == 61u << 26 /* stfdp */)
8886 status
= Reloc::addr16_ds(view
, value
, overflow
);
8888 status
= Reloc::addr16(view
, value
, overflow
);
8891 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
8894 && gsym
->is_undefined()
8895 && is_branch_reloc(r_type
))))
8897 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8898 _("relocation overflow"));
8900 gold_info(_("try relinking with a smaller --stub-group-size"));
8906 // Relocate section data.
8908 template<int size
, bool big_endian
>
8910 Target_powerpc
<size
, big_endian
>::relocate_section(
8911 const Relocate_info
<size
, big_endian
>* relinfo
,
8912 unsigned int sh_type
,
8913 const unsigned char* prelocs
,
8915 Output_section
* output_section
,
8916 bool needs_special_offset_handling
,
8917 unsigned char* view
,
8919 section_size_type view_size
,
8920 const Reloc_symbol_changes
* reloc_symbol_changes
)
8922 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
8923 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
8924 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
8925 Powerpc_comdat_behavior
;
8926 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
8929 gold_assert(sh_type
== elfcpp::SHT_RELA
);
8931 gold::relocate_section
<size
, big_endian
, Powerpc
, Powerpc_relocate
,
8932 Powerpc_comdat_behavior
, Classify_reloc
>(
8938 needs_special_offset_handling
,
8942 reloc_symbol_changes
);
8945 template<int size
, bool big_endian
>
8946 class Powerpc_scan_relocatable_reloc
8949 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
8950 static const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
8951 static const int sh_type
= elfcpp::SHT_RELA
;
8953 // Return the symbol referred to by the relocation.
8954 static inline unsigned int
8955 get_r_sym(const Reltype
* reloc
)
8956 { return elfcpp::elf_r_sym
<size
>(reloc
->get_r_info()); }
8958 // Return the type of the relocation.
8959 static inline unsigned int
8960 get_r_type(const Reltype
* reloc
)
8961 { return elfcpp::elf_r_type
<size
>(reloc
->get_r_info()); }
8963 // Return the strategy to use for a local symbol which is not a
8964 // section symbol, given the relocation type.
8965 inline Relocatable_relocs::Reloc_strategy
8966 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
8968 if (r_type
== 0 && r_sym
== 0)
8969 return Relocatable_relocs::RELOC_DISCARD
;
8970 return Relocatable_relocs::RELOC_COPY
;
8973 // Return the strategy to use for a local symbol which is a section
8974 // symbol, given the relocation type.
8975 inline Relocatable_relocs::Reloc_strategy
8976 local_section_strategy(unsigned int, Relobj
*)
8978 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
8981 // Return the strategy to use for a global symbol, given the
8982 // relocation type, the object, and the symbol index.
8983 inline Relocatable_relocs::Reloc_strategy
8984 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
8986 if (r_type
== elfcpp::R_PPC_PLTREL24
)
8987 return Relocatable_relocs::RELOC_SPECIAL
;
8988 return Relocatable_relocs::RELOC_COPY
;
8992 // Scan the relocs during a relocatable link.
8994 template<int size
, bool big_endian
>
8996 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
8997 Symbol_table
* symtab
,
8999 Sized_relobj_file
<size
, big_endian
>* object
,
9000 unsigned int data_shndx
,
9001 unsigned int sh_type
,
9002 const unsigned char* prelocs
,
9004 Output_section
* output_section
,
9005 bool needs_special_offset_handling
,
9006 size_t local_symbol_count
,
9007 const unsigned char* plocal_symbols
,
9008 Relocatable_relocs
* rr
)
9010 typedef Powerpc_scan_relocatable_reloc
<size
, big_endian
> Scan_strategy
;
9012 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9014 gold::scan_relocatable_relocs
<size
, big_endian
, Scan_strategy
>(
9022 needs_special_offset_handling
,
9028 // Scan the relocs for --emit-relocs.
9030 template<int size
, bool big_endian
>
9032 Target_powerpc
<size
, big_endian
>::emit_relocs_scan(
9033 Symbol_table
* symtab
,
9035 Sized_relobj_file
<size
, big_endian
>* object
,
9036 unsigned int data_shndx
,
9037 unsigned int sh_type
,
9038 const unsigned char* prelocs
,
9040 Output_section
* output_section
,
9041 bool needs_special_offset_handling
,
9042 size_t local_symbol_count
,
9043 const unsigned char* plocal_syms
,
9044 Relocatable_relocs
* rr
)
9046 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
9048 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
9049 Emit_relocs_strategy
;
9051 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9053 gold::scan_relocatable_relocs
<size
, big_endian
, Emit_relocs_strategy
>(
9061 needs_special_offset_handling
,
9067 // Emit relocations for a section.
9068 // This is a modified version of the function by the same name in
9069 // target-reloc.h. Using relocate_special_relocatable for
9070 // R_PPC_PLTREL24 would require duplication of the entire body of the
9071 // loop, so we may as well duplicate the whole thing.
9073 template<int size
, bool big_endian
>
9075 Target_powerpc
<size
, big_endian
>::relocate_relocs(
9076 const Relocate_info
<size
, big_endian
>* relinfo
,
9077 unsigned int sh_type
,
9078 const unsigned char* prelocs
,
9080 Output_section
* output_section
,
9081 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
9083 Address view_address
,
9085 unsigned char* reloc_view
,
9086 section_size_type reloc_view_size
)
9088 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9090 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
9091 typedef typename
elfcpp::Rela_write
<size
, big_endian
> Reltype_write
;
9092 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
9093 // Offset from start of insn to d-field reloc.
9094 const int d_offset
= big_endian
? 2 : 0;
9096 Powerpc_relobj
<size
, big_endian
>* const object
9097 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
9098 const unsigned int local_count
= object
->local_symbol_count();
9099 unsigned int got2_shndx
= object
->got2_shndx();
9100 Address got2_addend
= 0;
9101 if (got2_shndx
!= 0)
9103 got2_addend
= object
->get_output_section_offset(got2_shndx
);
9104 gold_assert(got2_addend
!= invalid_address
);
9107 unsigned char* pwrite
= reloc_view
;
9108 bool zap_next
= false;
9109 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
9111 Relocatable_relocs::Reloc_strategy strategy
= relinfo
->rr
->strategy(i
);
9112 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
9115 Reltype
reloc(prelocs
);
9116 Reltype_write
reloc_write(pwrite
);
9118 Address offset
= reloc
.get_r_offset();
9119 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
9120 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
9121 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
9122 const unsigned int orig_r_sym
= r_sym
;
9123 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
9124 = reloc
.get_r_addend();
9125 const Symbol
* gsym
= NULL
;
9129 // We could arrange to discard these and other relocs for
9130 // tls optimised sequences in the strategy methods, but for
9131 // now do as BFD ld does.
9132 r_type
= elfcpp::R_POWERPC_NONE
;
9136 // Get the new symbol index.
9137 Output_section
* os
= NULL
;
9138 if (r_sym
< local_count
)
9142 case Relocatable_relocs::RELOC_COPY
:
9143 case Relocatable_relocs::RELOC_SPECIAL
:
9146 r_sym
= object
->symtab_index(r_sym
);
9147 gold_assert(r_sym
!= -1U);
9151 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
9153 // We are adjusting a section symbol. We need to find
9154 // the symbol table index of the section symbol for
9155 // the output section corresponding to input section
9156 // in which this symbol is defined.
9157 gold_assert(r_sym
< local_count
);
9159 unsigned int shndx
=
9160 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
9161 gold_assert(is_ordinary
);
9162 os
= object
->output_section(shndx
);
9163 gold_assert(os
!= NULL
);
9164 gold_assert(os
->needs_symtab_index());
9165 r_sym
= os
->symtab_index();
9175 gsym
= object
->global_symbol(r_sym
);
9176 gold_assert(gsym
!= NULL
);
9177 if (gsym
->is_forwarder())
9178 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
9180 gold_assert(gsym
->has_symtab_index());
9181 r_sym
= gsym
->symtab_index();
9184 // Get the new offset--the location in the output section where
9185 // this relocation should be applied.
9186 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
9187 offset
+= offset_in_output_section
;
9190 section_offset_type sot_offset
=
9191 convert_types
<section_offset_type
, Address
>(offset
);
9192 section_offset_type new_sot_offset
=
9193 output_section
->output_offset(object
, relinfo
->data_shndx
,
9195 gold_assert(new_sot_offset
!= -1);
9196 offset
= new_sot_offset
;
9199 // In an object file, r_offset is an offset within the section.
9200 // In an executable or dynamic object, generated by
9201 // --emit-relocs, r_offset is an absolute address.
9202 if (!parameters
->options().relocatable())
9204 offset
+= view_address
;
9205 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
9206 offset
-= offset_in_output_section
;
9209 // Handle the reloc addend based on the strategy.
9210 if (strategy
== Relocatable_relocs::RELOC_COPY
)
9212 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
9214 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
9215 gold_assert(os
!= NULL
);
9216 addend
= psymval
->value(object
, addend
) - os
->address();
9218 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
9222 if (addend
>= 32768)
9223 addend
+= got2_addend
;
9225 else if (r_type
== elfcpp::R_POWERPC_REL16_HA
)
9227 r_type
= elfcpp::R_POWERPC_ADDR16_HA
;
9230 else if (r_type
== elfcpp::R_POWERPC_REL16_LO
)
9232 r_type
= elfcpp::R_POWERPC_ADDR16_LO
;
9233 addend
-= d_offset
+ 4;
9239 if (!parameters
->options().relocatable())
9241 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
9242 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
9243 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
9244 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
9246 // First instruction of a global dynamic sequence,
9248 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9249 switch (this->optimize_tls_gd(final
))
9251 case tls::TLSOPT_TO_IE
:
9252 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
9253 - elfcpp::R_POWERPC_GOT_TLSGD16
);
9255 case tls::TLSOPT_TO_LE
:
9256 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
9257 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
9258 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9261 r_type
= elfcpp::R_POWERPC_NONE
;
9269 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
9270 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
9271 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
9272 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
9274 // First instruction of a local dynamic sequence,
9276 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
9278 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
9279 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
9281 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9282 const Output_section
* os
= relinfo
->layout
->tls_segment()
9284 gold_assert(os
!= NULL
);
9285 gold_assert(os
->needs_symtab_index());
9286 r_sym
= os
->symtab_index();
9287 addend
= dtp_offset
;
9291 r_type
= elfcpp::R_POWERPC_NONE
;
9296 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
9297 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
9298 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
9299 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
9301 // First instruction of initial exec sequence.
9302 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9303 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
9305 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
9306 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
9307 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9310 r_type
= elfcpp::R_POWERPC_NONE
;
9315 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
9316 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
9318 // Second instruction of a global dynamic sequence,
9319 // the __tls_get_addr call
9320 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9321 switch (this->optimize_tls_gd(final
))
9323 case tls::TLSOPT_TO_IE
:
9324 r_type
= elfcpp::R_POWERPC_NONE
;
9327 case tls::TLSOPT_TO_LE
:
9328 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9336 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
9337 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
9339 // Second instruction of a local dynamic sequence,
9340 // the __tls_get_addr call
9341 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
9343 const Output_section
* os
= relinfo
->layout
->tls_segment()
9345 gold_assert(os
!= NULL
);
9346 gold_assert(os
->needs_symtab_index());
9347 r_sym
= os
->symtab_index();
9348 addend
= dtp_offset
;
9349 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9354 else if (r_type
== elfcpp::R_POWERPC_TLS
)
9356 // Second instruction of an initial exec sequence
9357 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9358 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
9360 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9366 reloc_write
.put_r_offset(offset
);
9367 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
9368 reloc_write
.put_r_addend(addend
);
9370 pwrite
+= reloc_size
;
9373 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
9374 == reloc_view_size
);
9377 // Return the value to use for a dynamic symbol which requires special
9378 // treatment. This is how we support equality comparisons of function
9379 // pointers across shared library boundaries, as described in the
9380 // processor specific ABI supplement.
9382 template<int size
, bool big_endian
>
9384 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
9388 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
9389 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
9390 p
!= this->stub_tables_
.end();
9393 Address off
= (*p
)->find_plt_call_entry(gsym
);
9394 if (off
!= invalid_address
)
9395 return (*p
)->stub_address() + off
;
9398 else if (this->abiversion() >= 2)
9400 Address off
= this->glink_section()->find_global_entry(gsym
);
9401 if (off
!= invalid_address
)
9402 return this->glink_section()->global_entry_address() + off
;
9407 // Return the PLT address to use for a local symbol.
9408 template<int size
, bool big_endian
>
9410 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
9411 const Relobj
* object
,
9412 unsigned int symndx
) const
9416 const Sized_relobj
<size
, big_endian
>* relobj
9417 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
9418 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
9419 p
!= this->stub_tables_
.end();
9422 Address off
= (*p
)->find_plt_call_entry(relobj
->sized_relobj(),
9424 if (off
!= invalid_address
)
9425 return (*p
)->stub_address() + off
;
9431 // Return the PLT address to use for a global symbol.
9432 template<int size
, bool big_endian
>
9434 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
9435 const Symbol
* gsym
) const
9439 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
9440 p
!= this->stub_tables_
.end();
9443 Address off
= (*p
)->find_plt_call_entry(gsym
);
9444 if (off
!= invalid_address
)
9445 return (*p
)->stub_address() + off
;
9448 else if (this->abiversion() >= 2)
9450 Address off
= this->glink_section()->find_global_entry(gsym
);
9451 if (off
!= invalid_address
)
9452 return this->glink_section()->global_entry_address() + off
;
9457 // Return the offset to use for the GOT_INDX'th got entry which is
9458 // for a local tls symbol specified by OBJECT, SYMNDX.
9459 template<int size
, bool big_endian
>
9461 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
9462 const Relobj
* object
,
9463 unsigned int symndx
,
9464 unsigned int got_indx
) const
9466 const Powerpc_relobj
<size
, big_endian
>* ppc_object
9467 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
9468 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
9470 for (Got_type got_type
= GOT_TYPE_TLSGD
;
9471 got_type
<= GOT_TYPE_TPREL
;
9472 got_type
= Got_type(got_type
+ 1))
9473 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
9475 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
9476 if (got_type
== GOT_TYPE_TLSGD
)
9478 if (off
== got_indx
* (size
/ 8))
9480 if (got_type
== GOT_TYPE_TPREL
)
9490 // Return the offset to use for the GOT_INDX'th got entry which is
9491 // for global tls symbol GSYM.
9492 template<int size
, bool big_endian
>
9494 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
9496 unsigned int got_indx
) const
9498 if (gsym
->type() == elfcpp::STT_TLS
)
9500 for (Got_type got_type
= GOT_TYPE_TLSGD
;
9501 got_type
<= GOT_TYPE_TPREL
;
9502 got_type
= Got_type(got_type
+ 1))
9503 if (gsym
->has_got_offset(got_type
))
9505 unsigned int off
= gsym
->got_offset(got_type
);
9506 if (got_type
== GOT_TYPE_TLSGD
)
9508 if (off
== got_indx
* (size
/ 8))
9510 if (got_type
== GOT_TYPE_TPREL
)
9520 // The selector for powerpc object files.
9522 template<int size
, bool big_endian
>
9523 class Target_selector_powerpc
: public Target_selector
9526 Target_selector_powerpc()
9527 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
9530 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
9531 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
9533 ? (big_endian
? "elf64ppc" : "elf64lppc")
9534 : (big_endian
? "elf32ppc" : "elf32lppc")))
9538 do_instantiate_target()
9539 { return new Target_powerpc
<size
, big_endian
>(); }
9542 Target_selector_powerpc
<32, true> target_selector_ppc32
;
9543 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
9544 Target_selector_powerpc
<64, true> target_selector_ppc64
;
9545 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
9547 // Instantiate these constants for -O0
9548 template<int size
, bool big_endian
>
9549 const int Output_data_glink
<size
, big_endian
>::pltresolve_size
;
9550 template<int size
, bool big_endian
>
9551 const typename Output_data_glink
<size
, big_endian
>::Address
9552 Output_data_glink
<size
, big_endian
>::invalid_address
;
9553 template<int size
, bool big_endian
>
9554 const typename Stub_table
<size
, big_endian
>::Address
9555 Stub_table
<size
, big_endian
>::invalid_address
;
9556 template<int size
, bool big_endian
>
9557 const typename Target_powerpc
<size
, big_endian
>::Address
9558 Target_powerpc
<size
, big_endian
>::invalid_address
;
9560 } // End anonymous namespace.