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_key_hash
;
4124 Plt_stub_ent(unsigned int off
, unsigned int indx
)
4125 : off_(off
), indx_(indx
)
4131 typedef Unordered_map
<Plt_stub_key
, Plt_stub_ent
,
4132 Plt_stub_key_hash
> Plt_stub_entries
;
4133 class Branch_stub_ent
;
4134 class Branch_stub_ent_hash
;
4135 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
4136 Branch_stub_ent_hash
> Branch_stub_entries
;
4138 // Alignment of stub section.
4144 unsigned int min_align
= 32;
4145 unsigned int user_align
= 1 << parameters
->options().plt_align();
4146 return std::max(user_align
, min_align
);
4149 // Return the plt offset for the given call stub.
4151 plt_off(typename
Plt_stub_entries::const_iterator p
, bool* is_iplt
) const
4153 const Symbol
* gsym
= p
->first
.sym_
;
4156 *is_iplt
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
4157 && gsym
->can_use_relative_reloc(false));
4158 return gsym
->plt_offset();
4163 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
4164 unsigned int local_sym_index
= p
->first
.locsym_
;
4165 return relobj
->local_plt_offset(local_sym_index
);
4169 // Size of a given plt call stub.
4171 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
4177 Address plt_addr
= this->plt_off(p
, &is_iplt
);
4179 plt_addr
+= this->targ_
->iplt_section()->address();
4181 plt_addr
+= this->targ_
->plt_section()->address();
4182 Address got_addr
= this->targ_
->got_section()->output_section()->address();
4183 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4184 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
4185 got_addr
+= ppcobj
->toc_base_offset();
4186 Address off
= plt_addr
- got_addr
;
4187 unsigned int bytes
= 4 * 4 + 4 * (ha(off
) != 0);
4188 if (this->targ_
->abiversion() < 2)
4190 bool static_chain
= parameters
->options().plt_static_chain();
4191 bool thread_safe
= this->targ_
->plt_thread_safe();
4195 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
4197 unsigned int align
= 1 << parameters
->options().plt_align();
4199 bytes
= (bytes
+ align
- 1) & -align
;
4203 // Return long branch stub size.
4205 branch_stub_size(typename
Branch_stub_entries::const_iterator p
)
4207 Address loc
= this->stub_address() + this->last_plt_size_
+ p
->second
;
4208 if (p
->first
.dest_
- loc
+ (1 << 25) < 2 << 25)
4210 if (size
== 64 || !parameters
->options().output_is_position_independent())
4217 do_write(Output_file
*);
4219 // Plt call stub keys.
4223 Plt_stub_key(const Symbol
* sym
)
4224 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4227 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4228 unsigned int locsym_index
)
4229 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4232 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4234 unsigned int r_type
,
4236 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4239 this->addend_
= addend
;
4240 else if (parameters
->options().output_is_position_independent()
4241 && r_type
== elfcpp::R_PPC_PLTREL24
)
4243 this->addend_
= addend
;
4244 if (this->addend_
>= 32768)
4245 this->object_
= object
;
4249 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4250 unsigned int locsym_index
,
4251 unsigned int r_type
,
4253 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4256 this->addend_
= addend
;
4257 else if (parameters
->options().output_is_position_independent()
4258 && r_type
== elfcpp::R_PPC_PLTREL24
)
4259 this->addend_
= addend
;
4262 bool operator==(const Plt_stub_key
& that
) const
4264 return (this->sym_
== that
.sym_
4265 && this->object_
== that
.object_
4266 && this->addend_
== that
.addend_
4267 && this->locsym_
== that
.locsym_
);
4271 const Sized_relobj_file
<size
, big_endian
>* object_
;
4272 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
4273 unsigned int locsym_
;
4276 class Plt_stub_key_hash
4279 size_t operator()(const Plt_stub_key
& ent
) const
4281 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
4282 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
4288 // Long branch stub keys.
4289 class Branch_stub_ent
4292 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
,
4293 Address to
, bool save_res
)
4294 : dest_(to
), toc_base_off_(0), save_res_(save_res
)
4297 toc_base_off_
= obj
->toc_base_offset();
4300 bool operator==(const Branch_stub_ent
& that
) const
4302 return (this->dest_
== that
.dest_
4304 || this->toc_base_off_
== that
.toc_base_off_
));
4308 unsigned int toc_base_off_
;
4312 class Branch_stub_ent_hash
4315 size_t operator()(const Branch_stub_ent
& ent
) const
4316 { return ent
.dest_
^ ent
.toc_base_off_
; }
4319 // In a sane world this would be a global.
4320 Target_powerpc
<size
, big_endian
>* targ_
;
4321 // Map sym/object/addend to stub offset.
4322 Plt_stub_entries plt_call_stubs_
;
4323 // Map destination address to stub offset.
4324 Branch_stub_entries long_branch_stubs_
;
4325 // size of input section
4326 section_size_type orig_data_size_
;
4328 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
4329 // Some rare cases cause (PR/20529) fluctuation in stub table
4330 // size, which leads to an endless relax loop. This is to be fixed
4331 // by, after the first few iterations, allowing only increase of
4332 // stub table size. This variable sets the minimal possible size of
4333 // a stub table, it is zero for the first few iterations, then
4334 // increases monotonically.
4335 Address min_size_threshold_
;
4336 // Whether .eh_frame info has been created for this stub section.
4337 bool eh_frame_added_
;
4338 // Set if this stub group needs a copy of out-of-line register
4339 // save/restore functions.
4340 bool need_save_res_
;
4341 // Per stub table unique identifier.
4345 // Add a plt call stub, if we do not already have one for this
4346 // sym/object/addend combo.
4348 template<int size
, bool big_endian
>
4350 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4352 const Sized_relobj_file
<size
, big_endian
>* object
,
4354 unsigned int r_type
,
4357 Plt_stub_key
key(object
, gsym
, r_type
, addend
);
4358 Plt_stub_ent
ent(this->plt_size_
, this->plt_call_stubs_
.size());
4359 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4360 = this->plt_call_stubs_
.insert(std::make_pair(key
, ent
));
4362 this->plt_size_
= ent
.off_
+ this->plt_call_size(p
.first
);
4363 return this->can_reach_stub(from
, ent
.off_
, r_type
);
4366 template<int size
, bool big_endian
>
4368 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4370 const Sized_relobj_file
<size
, big_endian
>* object
,
4371 unsigned int locsym_index
,
4372 unsigned int r_type
,
4375 Plt_stub_key
key(object
, locsym_index
, r_type
, addend
);
4376 Plt_stub_ent
ent(this->plt_size_
, this->plt_call_stubs_
.size());
4377 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4378 = this->plt_call_stubs_
.insert(std::make_pair(key
, ent
));
4380 this->plt_size_
= ent
.off_
+ this->plt_call_size(p
.first
);
4381 return this->can_reach_stub(from
, ent
.off_
, r_type
);
4384 // Find a plt call stub.
4386 template<int size
, bool big_endian
>
4387 typename Stub_table
<size
, big_endian
>::Address
4388 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4389 const Sized_relobj_file
<size
, big_endian
>* object
,
4391 unsigned int r_type
,
4392 Address addend
) const
4394 Plt_stub_key
key(object
, gsym
, r_type
, addend
);
4395 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4396 if (p
== this->plt_call_stubs_
.end())
4397 return invalid_address
;
4398 return p
->second
.off_
;
4401 template<int size
, bool big_endian
>
4402 typename Stub_table
<size
, big_endian
>::Address
4403 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
4405 Plt_stub_key
key(gsym
);
4406 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4407 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
.off_
;
4410 template<int size
, bool big_endian
>
4411 typename Stub_table
<size
, big_endian
>::Address
4412 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4413 const Sized_relobj_file
<size
, big_endian
>* object
,
4414 unsigned int locsym_index
,
4415 unsigned int r_type
,
4416 Address addend
) const
4418 Plt_stub_key
key(object
, locsym_index
, r_type
, addend
);
4419 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4420 if (p
== this->plt_call_stubs_
.end())
4421 return invalid_address
;
4422 return p
->second
.off_
;
4425 template<int size
, bool big_endian
>
4426 typename Stub_table
<size
, big_endian
>::Address
4427 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4428 const Sized_relobj_file
<size
, big_endian
>* object
,
4429 unsigned int locsym_index
) const
4431 Plt_stub_key
key(object
, locsym_index
);
4432 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4433 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
.off_
;
4436 // Add a long branch stub if we don't already have one to given
4439 template<int size
, bool big_endian
>
4441 Stub_table
<size
, big_endian
>::add_long_branch_entry(
4442 const Powerpc_relobj
<size
, big_endian
>* object
,
4443 unsigned int r_type
,
4448 Branch_stub_ent
ent(object
, to
, save_res
);
4449 Address off
= this->branch_size_
;
4450 std::pair
<typename
Branch_stub_entries::iterator
, bool> p
4451 = this->long_branch_stubs_
.insert(std::make_pair(ent
, off
));
4455 this->need_save_res_
= true;
4458 unsigned int stub_size
= this->branch_stub_size(p
.first
);
4459 this->branch_size_
= off
+ stub_size
;
4460 if (size
== 64 && stub_size
!= 4)
4461 this->targ_
->add_branch_lookup_table(to
);
4464 return this->can_reach_stub(from
, off
, r_type
);
4467 // Find long branch stub offset.
4469 template<int size
, bool big_endian
>
4470 typename Stub_table
<size
, big_endian
>::Address
4471 Stub_table
<size
, big_endian
>::find_long_branch_entry(
4472 const Powerpc_relobj
<size
, big_endian
>* object
,
4475 Branch_stub_ent
ent(object
, to
, false);
4476 typename
Branch_stub_entries::const_iterator p
4477 = this->long_branch_stubs_
.find(ent
);
4478 if (p
== this->long_branch_stubs_
.end())
4479 return invalid_address
;
4480 if (p
->first
.save_res_
)
4481 return to
- this->targ_
->savres_section()->address() + this->branch_size_
;
4485 // A class to handle .glink.
4487 template<int size
, bool big_endian
>
4488 class Output_data_glink
: public Output_section_data
4491 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4492 static const Address invalid_address
= static_cast<Address
>(0) - 1;
4493 static const int pltresolve_size
= 16*4;
4495 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
4496 : Output_section_data(16), targ_(targ
), global_entry_stubs_(),
4497 end_branch_table_(), ge_size_(0)
4501 add_eh_frame(Layout
* layout
);
4504 add_global_entry(const Symbol
*);
4507 find_global_entry(const Symbol
*) const;
4510 global_entry_address() const
4512 gold_assert(this->is_data_size_valid());
4513 unsigned int global_entry_off
= (this->end_branch_table_
+ 15) & -16;
4514 return this->address() + global_entry_off
;
4518 // Write to a map file.
4520 do_print_to_mapfile(Mapfile
* mapfile
) const
4521 { mapfile
->print_output_data(this, _("** glink")); }
4525 set_final_data_size();
4529 do_write(Output_file
*);
4531 // Allows access to .got and .plt for do_write.
4532 Target_powerpc
<size
, big_endian
>* targ_
;
4534 // Map sym to stub offset.
4535 typedef Unordered_map
<const Symbol
*, unsigned int> Global_entry_stub_entries
;
4536 Global_entry_stub_entries global_entry_stubs_
;
4538 unsigned int end_branch_table_
, ge_size_
;
4541 template<int size
, bool big_endian
>
4543 Output_data_glink
<size
, big_endian
>::add_eh_frame(Layout
* layout
)
4545 if (!parameters
->options().ld_generated_unwind_info())
4550 if (this->targ_
->abiversion() < 2)
4551 layout
->add_eh_frame_for_plt(this,
4552 Eh_cie
<64>::eh_frame_cie
,
4553 sizeof (Eh_cie
<64>::eh_frame_cie
),
4554 glink_eh_frame_fde_64v1
,
4555 sizeof (glink_eh_frame_fde_64v1
));
4557 layout
->add_eh_frame_for_plt(this,
4558 Eh_cie
<64>::eh_frame_cie
,
4559 sizeof (Eh_cie
<64>::eh_frame_cie
),
4560 glink_eh_frame_fde_64v2
,
4561 sizeof (glink_eh_frame_fde_64v2
));
4565 // 32-bit .glink can use the default since the CIE return
4566 // address reg, LR, is valid.
4567 layout
->add_eh_frame_for_plt(this,
4568 Eh_cie
<32>::eh_frame_cie
,
4569 sizeof (Eh_cie
<32>::eh_frame_cie
),
4571 sizeof (default_fde
));
4572 // Except where LR is used in a PIC __glink_PLTresolve.
4573 if (parameters
->options().output_is_position_independent())
4574 layout
->add_eh_frame_for_plt(this,
4575 Eh_cie
<32>::eh_frame_cie
,
4576 sizeof (Eh_cie
<32>::eh_frame_cie
),
4577 glink_eh_frame_fde_32
,
4578 sizeof (glink_eh_frame_fde_32
));
4582 template<int size
, bool big_endian
>
4584 Output_data_glink
<size
, big_endian
>::add_global_entry(const Symbol
* gsym
)
4586 std::pair
<typename
Global_entry_stub_entries::iterator
, bool> p
4587 = this->global_entry_stubs_
.insert(std::make_pair(gsym
, this->ge_size_
));
4589 this->ge_size_
+= 16;
4592 template<int size
, bool big_endian
>
4593 typename Output_data_glink
<size
, big_endian
>::Address
4594 Output_data_glink
<size
, big_endian
>::find_global_entry(const Symbol
* gsym
) const
4596 typename
Global_entry_stub_entries::const_iterator p
4597 = this->global_entry_stubs_
.find(gsym
);
4598 return p
== this->global_entry_stubs_
.end() ? invalid_address
: p
->second
;
4601 template<int size
, bool big_endian
>
4603 Output_data_glink
<size
, big_endian
>::set_final_data_size()
4605 unsigned int count
= this->targ_
->plt_entry_count();
4606 section_size_type total
= 0;
4612 // space for branch table
4613 total
+= 4 * (count
- 1);
4615 total
+= -total
& 15;
4616 total
+= this->pltresolve_size
;
4620 total
+= this->pltresolve_size
;
4622 // space for branch table
4624 if (this->targ_
->abiversion() < 2)
4628 total
+= 4 * (count
- 0x8000);
4632 this->end_branch_table_
= total
;
4633 total
= (total
+ 15) & -16;
4634 total
+= this->ge_size_
;
4636 this->set_data_size(total
);
4639 // Define symbols on stubs, identifying the stub.
4641 template<int size
, bool big_endian
>
4643 Stub_table
<size
, big_endian
>::define_stub_syms(Symbol_table
* symtab
)
4645 if (!this->plt_call_stubs_
.empty())
4647 // The key for the plt call stub hash table includes addresses,
4648 // therefore traversal order depends on those addresses, which
4649 // can change between runs if gold is a PIE. Unfortunately the
4650 // output .symtab ordering depends on the order in which symbols
4651 // are added to the linker symtab. We want reproducible output
4652 // so must sort the call stub symbols.
4653 typedef typename
Plt_stub_entries::const_iterator plt_iter
;
4654 std::vector
<plt_iter
> sorted
;
4655 sorted
.resize(this->plt_call_stubs_
.size());
4657 for (plt_iter cs
= this->plt_call_stubs_
.begin();
4658 cs
!= this->plt_call_stubs_
.end();
4660 sorted
[cs
->second
.indx_
] = cs
;
4662 for (unsigned int i
= 0; i
< this->plt_call_stubs_
.size(); ++i
)
4664 plt_iter cs
= sorted
[i
];
4667 if (cs
->first
.addend_
!= 0)
4668 sprintf(add
, "+%x", static_cast<uint32_t>(cs
->first
.addend_
));
4671 if (cs
->first
.object_
)
4673 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4674 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
4675 sprintf(obj
, "%x:", ppcobj
->uniq());
4678 const char *symname
;
4679 if (cs
->first
.sym_
== NULL
)
4681 sprintf(localname
, "%x", cs
->first
.locsym_
);
4682 symname
= localname
;
4685 symname
= cs
->first
.sym_
->name();
4686 char* name
= new char[8 + 10 + strlen(obj
) + strlen(symname
) + strlen(add
) + 1];
4687 sprintf(name
, "%08x.plt_call.%s%s%s", this->uniq_
, obj
, symname
, add
);
4689 = this->stub_address() - this->address() + cs
->second
.off_
;
4690 unsigned int stub_size
= this->plt_call_size(cs
);
4691 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
4695 typedef typename
Branch_stub_entries::const_iterator branch_iter
;
4696 for (branch_iter bs
= this->long_branch_stubs_
.begin();
4697 bs
!= this->long_branch_stubs_
.end();
4700 if (bs
->first
.save_res_
)
4703 char* name
= new char[8 + 13 + 16 + 1];
4704 sprintf(name
, "%08x.long_branch.%llx", this->uniq_
,
4705 static_cast<unsigned long long>(bs
->first
.dest_
));
4706 Address value
= (this->stub_address() - this->address()
4707 + this->plt_size_
+ bs
->second
);
4708 unsigned int stub_size
= this->branch_stub_size(bs
);
4709 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
4713 // Write out plt and long branch stub code.
4715 template<int size
, bool big_endian
>
4717 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
4719 if (this->plt_call_stubs_
.empty()
4720 && this->long_branch_stubs_
.empty())
4723 const section_size_type start_off
= this->offset();
4724 const section_size_type off
= this->stub_offset();
4725 const section_size_type oview_size
=
4726 convert_to_section_size_type(this->data_size() - (off
- start_off
));
4727 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4732 const Output_data_got_powerpc
<size
, big_endian
>* got
4733 = this->targ_
->got_section();
4734 Address got_os_addr
= got
->output_section()->address();
4736 if (!this->plt_call_stubs_
.empty())
4738 // The base address of the .plt section.
4739 Address plt_base
= this->targ_
->plt_section()->address();
4740 Address iplt_base
= invalid_address
;
4742 // Write out plt call stubs.
4743 typename
Plt_stub_entries::const_iterator cs
;
4744 for (cs
= this->plt_call_stubs_
.begin();
4745 cs
!= this->plt_call_stubs_
.end();
4749 Address pltoff
= this->plt_off(cs
, &is_iplt
);
4750 Address plt_addr
= pltoff
;
4753 if (iplt_base
== invalid_address
)
4754 iplt_base
= this->targ_
->iplt_section()->address();
4755 plt_addr
+= iplt_base
;
4758 plt_addr
+= plt_base
;
4759 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4760 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
4761 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
4762 Address off
= plt_addr
- got_addr
;
4764 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
4765 gold_error(_("%s: linkage table error against `%s'"),
4766 cs
->first
.object_
->name().c_str(),
4767 cs
->first
.sym_
->demangled_name().c_str());
4769 bool plt_load_toc
= this->targ_
->abiversion() < 2;
4771 = plt_load_toc
&& parameters
->options().plt_static_chain();
4773 = plt_load_toc
&& this->targ_
->plt_thread_safe();
4774 bool use_fake_dep
= false;
4775 Address cmp_branch_off
= 0;
4778 unsigned int pltindex
4779 = ((pltoff
- this->targ_
->first_plt_entry_offset())
4780 / this->targ_
->plt_entry_size());
4782 = (this->targ_
->glink_section()->pltresolve_size
4784 if (pltindex
> 32768)
4785 glinkoff
+= (pltindex
- 32768) * 4;
4787 = this->targ_
->glink_section()->address() + glinkoff
;
4789 = (this->stub_address() + cs
->second
.off_
+ 24
4790 + 4 * (ha(off
) != 0)
4791 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4792 + 4 * static_chain
);
4793 cmp_branch_off
= to
- from
;
4794 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
4797 p
= oview
+ cs
->second
.off_
;
4800 write_insn
<big_endian
>(p
, std_2_1
+ this->targ_
->stk_toc());
4804 write_insn
<big_endian
>(p
, addis_11_2
+ ha(off
));
4806 write_insn
<big_endian
>(p
, ld_12_11
+ l(off
));
4811 write_insn
<big_endian
>(p
, addis_12_2
+ ha(off
));
4813 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
));
4817 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4819 write_insn
<big_endian
>(p
, addi_11_11
+ l(off
));
4823 write_insn
<big_endian
>(p
, mtctr_12
);
4829 write_insn
<big_endian
>(p
, xor_2_12_12
);
4831 write_insn
<big_endian
>(p
, add_11_11_2
);
4834 write_insn
<big_endian
>(p
, ld_2_11
+ l(off
+ 8));
4838 write_insn
<big_endian
>(p
, ld_11_11
+ l(off
+ 16));
4845 write_insn
<big_endian
>(p
, std_2_1
+ this->targ_
->stk_toc());
4847 write_insn
<big_endian
>(p
, ld_12_2
+ l(off
));
4850 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4852 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
));
4856 write_insn
<big_endian
>(p
, mtctr_12
);
4862 write_insn
<big_endian
>(p
, xor_11_12_12
);
4864 write_insn
<big_endian
>(p
, add_2_2_11
);
4869 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16));
4872 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8));
4876 if (thread_safe
&& !use_fake_dep
)
4878 write_insn
<big_endian
>(p
, cmpldi_2_0
);
4880 write_insn
<big_endian
>(p
, bnectr_p4
);
4882 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
4885 write_insn
<big_endian
>(p
, bctr
);
4889 // Write out long branch stubs.
4890 typename
Branch_stub_entries::const_iterator bs
;
4891 for (bs
= this->long_branch_stubs_
.begin();
4892 bs
!= this->long_branch_stubs_
.end();
4895 if (bs
->first
.save_res_
)
4897 p
= oview
+ this->plt_size_
+ bs
->second
;
4898 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
4899 Address delta
= bs
->first
.dest_
- loc
;
4900 if (delta
+ (1 << 25) < 2 << 25)
4901 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
4905 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
4906 gold_assert(brlt_addr
!= invalid_address
);
4907 brlt_addr
+= this->targ_
->brlt_section()->address();
4908 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
4909 Address brltoff
= brlt_addr
- got_addr
;
4910 if (ha(brltoff
) == 0)
4912 write_insn
<big_endian
>(p
, ld_12_2
+ l(brltoff
)), p
+= 4;
4916 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
4917 write_insn
<big_endian
>(p
, ld_12_12
+ l(brltoff
)), p
+= 4;
4919 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
4920 write_insn
<big_endian
>(p
, bctr
);
4926 if (!this->plt_call_stubs_
.empty())
4928 // The base address of the .plt section.
4929 Address plt_base
= this->targ_
->plt_section()->address();
4930 Address iplt_base
= invalid_address
;
4931 // The address of _GLOBAL_OFFSET_TABLE_.
4932 Address g_o_t
= invalid_address
;
4934 // Write out plt call stubs.
4935 typename
Plt_stub_entries::const_iterator cs
;
4936 for (cs
= this->plt_call_stubs_
.begin();
4937 cs
!= this->plt_call_stubs_
.end();
4941 Address plt_addr
= this->plt_off(cs
, &is_iplt
);
4944 if (iplt_base
== invalid_address
)
4945 iplt_base
= this->targ_
->iplt_section()->address();
4946 plt_addr
+= iplt_base
;
4949 plt_addr
+= plt_base
;
4951 p
= oview
+ cs
->second
.off_
;
4952 if (parameters
->options().output_is_position_independent())
4955 const Powerpc_relobj
<size
, big_endian
>* ppcobj
4956 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
4957 (cs
->first
.object_
));
4958 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
4960 unsigned int got2
= ppcobj
->got2_shndx();
4961 got_addr
= ppcobj
->get_output_section_offset(got2
);
4962 gold_assert(got_addr
!= invalid_address
);
4963 got_addr
+= (ppcobj
->output_section(got2
)->address()
4964 + cs
->first
.addend_
);
4968 if (g_o_t
== invalid_address
)
4970 const Output_data_got_powerpc
<size
, big_endian
>* got
4971 = this->targ_
->got_section();
4972 g_o_t
= got
->address() + got
->g_o_t();
4977 Address off
= plt_addr
- got_addr
;
4980 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(off
));
4981 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
4982 write_insn
<big_endian
>(p
+ 8, bctr
);
4986 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(off
));
4987 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(off
));
4988 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
4989 write_insn
<big_endian
>(p
+ 12, bctr
);
4994 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
4995 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
4996 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
4997 write_insn
<big_endian
>(p
+ 12, bctr
);
5002 // Write out long branch stubs.
5003 typename
Branch_stub_entries::const_iterator bs
;
5004 for (bs
= this->long_branch_stubs_
.begin();
5005 bs
!= this->long_branch_stubs_
.end();
5008 if (bs
->first
.save_res_
)
5010 p
= oview
+ this->plt_size_
+ bs
->second
;
5011 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
5012 Address delta
= bs
->first
.dest_
- loc
;
5013 if (delta
+ (1 << 25) < 2 << 25)
5014 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
5015 else if (!parameters
->options().output_is_position_independent())
5017 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(bs
->first
.dest_
));
5018 write_insn
<big_endian
>(p
+ 4, addi_12_12
+ l(bs
->first
.dest_
));
5019 write_insn
<big_endian
>(p
+ 8, mtctr_12
);
5020 write_insn
<big_endian
>(p
+ 12, bctr
);
5025 write_insn
<big_endian
>(p
+ 0, mflr_0
);
5026 write_insn
<big_endian
>(p
+ 4, bcl_20_31
);
5027 write_insn
<big_endian
>(p
+ 8, mflr_12
);
5028 write_insn
<big_endian
>(p
+ 12, addis_12_12
+ ha(delta
));
5029 write_insn
<big_endian
>(p
+ 16, addi_12_12
+ l(delta
));
5030 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
5031 write_insn
<big_endian
>(p
+ 24, mtctr_12
);
5032 write_insn
<big_endian
>(p
+ 28, bctr
);
5036 if (this->need_save_res_
)
5038 p
= oview
+ this->plt_size_
+ this->branch_size_
;
5039 memcpy (p
, this->targ_
->savres_section()->contents(),
5040 this->targ_
->savres_section()->data_size());
5044 // Write out .glink.
5046 template<int size
, bool big_endian
>
5048 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
5050 const section_size_type off
= this->offset();
5051 const section_size_type oview_size
=
5052 convert_to_section_size_type(this->data_size());
5053 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5056 // The base address of the .plt section.
5057 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
5058 Address plt_base
= this->targ_
->plt_section()->address();
5062 if (this->end_branch_table_
!= 0)
5064 // Write pltresolve stub.
5066 Address after_bcl
= this->address() + 16;
5067 Address pltoff
= plt_base
- after_bcl
;
5069 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
5071 if (this->targ_
->abiversion() < 2)
5073 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
5074 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5075 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5076 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5077 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
5078 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5079 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5080 write_insn
<big_endian
>(p
, ld_2_11
+ 8), p
+= 4;
5081 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5082 write_insn
<big_endian
>(p
, ld_11_11
+ 16), p
+= 4;
5086 write_insn
<big_endian
>(p
, mflr_0
), p
+= 4;
5087 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5088 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5089 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5090 write_insn
<big_endian
>(p
, mtlr_0
), p
+= 4;
5091 write_insn
<big_endian
>(p
, sub_12_12_11
), p
+= 4;
5092 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5093 write_insn
<big_endian
>(p
, addi_0_12
+ l(-48)), p
+= 4;
5094 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5095 write_insn
<big_endian
>(p
, srdi_0_0_2
), p
+= 4;
5096 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5097 write_insn
<big_endian
>(p
, ld_11_11
+ 8), p
+= 4;
5099 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
5100 while (p
< oview
+ this->pltresolve_size
)
5101 write_insn
<big_endian
>(p
, nop
), p
+= 4;
5103 // Write lazy link call stubs.
5105 while (p
< oview
+ this->end_branch_table_
)
5107 if (this->targ_
->abiversion() < 2)
5111 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
5115 write_insn
<big_endian
>(p
, lis_0
+ hi(indx
)), p
+= 4;
5116 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
5119 uint32_t branch_off
= 8 - (p
- oview
);
5120 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
5125 Address plt_base
= this->targ_
->plt_section()->address();
5126 Address iplt_base
= invalid_address
;
5127 unsigned int global_entry_off
= (this->end_branch_table_
+ 15) & -16;
5128 Address global_entry_base
= this->address() + global_entry_off
;
5129 typename
Global_entry_stub_entries::const_iterator ge
;
5130 for (ge
= this->global_entry_stubs_
.begin();
5131 ge
!= this->global_entry_stubs_
.end();
5134 p
= oview
+ global_entry_off
+ ge
->second
;
5135 Address plt_addr
= ge
->first
->plt_offset();
5136 if (ge
->first
->type() == elfcpp::STT_GNU_IFUNC
5137 && ge
->first
->can_use_relative_reloc(false))
5139 if (iplt_base
== invalid_address
)
5140 iplt_base
= this->targ_
->iplt_section()->address();
5141 plt_addr
+= iplt_base
;
5144 plt_addr
+= plt_base
;
5145 Address my_addr
= global_entry_base
+ ge
->second
;
5146 Address off
= plt_addr
- my_addr
;
5148 if (off
+ 0x80008000 > 0xffffffff || (off
& 3) != 0)
5149 gold_error(_("%s: linkage table error against `%s'"),
5150 ge
->first
->object()->name().c_str(),
5151 ge
->first
->demangled_name().c_str());
5153 write_insn
<big_endian
>(p
, addis_12_12
+ ha(off
)), p
+= 4;
5154 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
)), p
+= 4;
5155 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5156 write_insn
<big_endian
>(p
, bctr
);
5161 const Output_data_got_powerpc
<size
, big_endian
>* got
5162 = this->targ_
->got_section();
5163 // The address of _GLOBAL_OFFSET_TABLE_.
5164 Address g_o_t
= got
->address() + got
->g_o_t();
5166 // Write out pltresolve branch table.
5168 unsigned int the_end
= oview_size
- this->pltresolve_size
;
5169 unsigned char* end_p
= oview
+ the_end
;
5170 while (p
< end_p
- 8 * 4)
5171 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
5173 write_insn
<big_endian
>(p
, nop
), p
+= 4;
5175 // Write out pltresolve call stub.
5176 if (parameters
->options().output_is_position_independent())
5178 Address res0_off
= 0;
5179 Address after_bcl_off
= the_end
+ 12;
5180 Address bcl_res0
= after_bcl_off
- res0_off
;
5182 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
5183 write_insn
<big_endian
>(p
+ 4, mflr_0
);
5184 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
5185 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
5186 write_insn
<big_endian
>(p
+ 16, mflr_12
);
5187 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
5188 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
5190 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
5192 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
5193 if (ha(got_bcl
) == ha(got_bcl
+ 4))
5195 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
5196 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
5200 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
5201 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
5203 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
5204 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
5205 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
5206 write_insn
<big_endian
>(p
+ 52, bctr
);
5207 write_insn
<big_endian
>(p
+ 56, nop
);
5208 write_insn
<big_endian
>(p
+ 60, nop
);
5212 Address res0
= this->address();
5214 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
5215 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
5216 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5217 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
5219 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
5220 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
5221 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
5222 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
5223 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5224 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
5226 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
5227 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
5228 write_insn
<big_endian
>(p
+ 32, bctr
);
5229 write_insn
<big_endian
>(p
+ 36, nop
);
5230 write_insn
<big_endian
>(p
+ 40, nop
);
5231 write_insn
<big_endian
>(p
+ 44, nop
);
5232 write_insn
<big_endian
>(p
+ 48, nop
);
5233 write_insn
<big_endian
>(p
+ 52, nop
);
5234 write_insn
<big_endian
>(p
+ 56, nop
);
5235 write_insn
<big_endian
>(p
+ 60, nop
);
5240 of
->write_output_view(off
, oview_size
, oview
);
5244 // A class to handle linker generated save/restore functions.
5246 template<int size
, bool big_endian
>
5247 class Output_data_save_res
: public Output_section_data_build
5250 Output_data_save_res(Symbol_table
* symtab
);
5252 const unsigned char*
5259 // Write to a map file.
5261 do_print_to_mapfile(Mapfile
* mapfile
) const
5262 { mapfile
->print_output_data(this, _("** save/restore")); }
5265 do_write(Output_file
*);
5268 // The maximum size of save/restore contents.
5269 static const unsigned int savres_max
= 218*4;
5272 savres_define(Symbol_table
* symtab
,
5274 unsigned int lo
, unsigned int hi
,
5275 unsigned char* write_ent(unsigned char*, int),
5276 unsigned char* write_tail(unsigned char*, int));
5278 unsigned char *contents_
;
5281 template<bool big_endian
>
5282 static unsigned char*
5283 savegpr0(unsigned char* p
, int r
)
5285 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5286 write_insn
<big_endian
>(p
, insn
);
5290 template<bool big_endian
>
5291 static unsigned char*
5292 savegpr0_tail(unsigned char* p
, int r
)
5294 p
= savegpr0
<big_endian
>(p
, r
);
5295 uint32_t insn
= std_0_1
+ 16;
5296 write_insn
<big_endian
>(p
, insn
);
5298 write_insn
<big_endian
>(p
, blr
);
5302 template<bool big_endian
>
5303 static unsigned char*
5304 restgpr0(unsigned char* p
, int r
)
5306 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5307 write_insn
<big_endian
>(p
, insn
);
5311 template<bool big_endian
>
5312 static unsigned char*
5313 restgpr0_tail(unsigned char* p
, int r
)
5315 uint32_t insn
= ld_0_1
+ 16;
5316 write_insn
<big_endian
>(p
, insn
);
5318 p
= restgpr0
<big_endian
>(p
, r
);
5319 write_insn
<big_endian
>(p
, mtlr_0
);
5323 p
= restgpr0
<big_endian
>(p
, 30);
5324 p
= restgpr0
<big_endian
>(p
, 31);
5326 write_insn
<big_endian
>(p
, blr
);
5330 template<bool big_endian
>
5331 static unsigned char*
5332 savegpr1(unsigned char* p
, int r
)
5334 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5335 write_insn
<big_endian
>(p
, insn
);
5339 template<bool big_endian
>
5340 static unsigned char*
5341 savegpr1_tail(unsigned char* p
, int r
)
5343 p
= savegpr1
<big_endian
>(p
, r
);
5344 write_insn
<big_endian
>(p
, blr
);
5348 template<bool big_endian
>
5349 static unsigned char*
5350 restgpr1(unsigned char* p
, int r
)
5352 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5353 write_insn
<big_endian
>(p
, insn
);
5357 template<bool big_endian
>
5358 static unsigned char*
5359 restgpr1_tail(unsigned char* p
, int r
)
5361 p
= restgpr1
<big_endian
>(p
, r
);
5362 write_insn
<big_endian
>(p
, blr
);
5366 template<bool big_endian
>
5367 static unsigned char*
5368 savefpr(unsigned char* p
, int r
)
5370 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5371 write_insn
<big_endian
>(p
, insn
);
5375 template<bool big_endian
>
5376 static unsigned char*
5377 savefpr0_tail(unsigned char* p
, int r
)
5379 p
= savefpr
<big_endian
>(p
, r
);
5380 write_insn
<big_endian
>(p
, std_0_1
+ 16);
5382 write_insn
<big_endian
>(p
, blr
);
5386 template<bool big_endian
>
5387 static unsigned char*
5388 restfpr(unsigned char* p
, int r
)
5390 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5391 write_insn
<big_endian
>(p
, insn
);
5395 template<bool big_endian
>
5396 static unsigned char*
5397 restfpr0_tail(unsigned char* p
, int r
)
5399 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
5401 p
= restfpr
<big_endian
>(p
, r
);
5402 write_insn
<big_endian
>(p
, mtlr_0
);
5406 p
= restfpr
<big_endian
>(p
, 30);
5407 p
= restfpr
<big_endian
>(p
, 31);
5409 write_insn
<big_endian
>(p
, blr
);
5413 template<bool big_endian
>
5414 static unsigned char*
5415 savefpr1_tail(unsigned char* p
, int r
)
5417 p
= savefpr
<big_endian
>(p
, r
);
5418 write_insn
<big_endian
>(p
, blr
);
5422 template<bool big_endian
>
5423 static unsigned char*
5424 restfpr1_tail(unsigned char* p
, int r
)
5426 p
= restfpr
<big_endian
>(p
, r
);
5427 write_insn
<big_endian
>(p
, blr
);
5431 template<bool big_endian
>
5432 static unsigned char*
5433 savevr(unsigned char* p
, int r
)
5435 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5436 write_insn
<big_endian
>(p
, insn
);
5438 insn
= stvx_0_12_0
+ (r
<< 21);
5439 write_insn
<big_endian
>(p
, insn
);
5443 template<bool big_endian
>
5444 static unsigned char*
5445 savevr_tail(unsigned char* p
, int r
)
5447 p
= savevr
<big_endian
>(p
, r
);
5448 write_insn
<big_endian
>(p
, blr
);
5452 template<bool big_endian
>
5453 static unsigned char*
5454 restvr(unsigned char* p
, int r
)
5456 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5457 write_insn
<big_endian
>(p
, insn
);
5459 insn
= lvx_0_12_0
+ (r
<< 21);
5460 write_insn
<big_endian
>(p
, insn
);
5464 template<bool big_endian
>
5465 static unsigned char*
5466 restvr_tail(unsigned char* p
, int r
)
5468 p
= restvr
<big_endian
>(p
, r
);
5469 write_insn
<big_endian
>(p
, blr
);
5474 template<int size
, bool big_endian
>
5475 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
5476 Symbol_table
* symtab
)
5477 : Output_section_data_build(4),
5480 this->savres_define(symtab
,
5481 "_savegpr0_", 14, 31,
5482 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
5483 this->savres_define(symtab
,
5484 "_restgpr0_", 14, 29,
5485 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
5486 this->savres_define(symtab
,
5487 "_restgpr0_", 30, 31,
5488 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
5489 this->savres_define(symtab
,
5490 "_savegpr1_", 14, 31,
5491 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
5492 this->savres_define(symtab
,
5493 "_restgpr1_", 14, 31,
5494 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
5495 this->savres_define(symtab
,
5496 "_savefpr_", 14, 31,
5497 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
5498 this->savres_define(symtab
,
5499 "_restfpr_", 14, 29,
5500 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
5501 this->savres_define(symtab
,
5502 "_restfpr_", 30, 31,
5503 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
5504 this->savres_define(symtab
,
5506 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
5507 this->savres_define(symtab
,
5509 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
5510 this->savres_define(symtab
,
5512 savevr
<big_endian
>, savevr_tail
<big_endian
>);
5513 this->savres_define(symtab
,
5515 restvr
<big_endian
>, restvr_tail
<big_endian
>);
5518 template<int size
, bool big_endian
>
5520 Output_data_save_res
<size
, big_endian
>::savres_define(
5521 Symbol_table
* symtab
,
5523 unsigned int lo
, unsigned int hi
,
5524 unsigned char* write_ent(unsigned char*, int),
5525 unsigned char* write_tail(unsigned char*, int))
5527 size_t len
= strlen(name
);
5528 bool writing
= false;
5531 memcpy(sym
, name
, len
);
5534 for (unsigned int i
= lo
; i
<= hi
; i
++)
5536 sym
[len
+ 0] = i
/ 10 + '0';
5537 sym
[len
+ 1] = i
% 10 + '0';
5538 Symbol
* gsym
= symtab
->lookup(sym
);
5539 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
5540 writing
= writing
|| refd
;
5543 if (this->contents_
== NULL
)
5544 this->contents_
= new unsigned char[this->savres_max
];
5546 section_size_type value
= this->current_data_size();
5547 unsigned char* p
= this->contents_
+ value
;
5549 p
= write_ent(p
, i
);
5551 p
= write_tail(p
, i
);
5552 section_size_type cur_size
= p
- this->contents_
;
5553 this->set_current_data_size(cur_size
);
5555 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
5556 this, value
, cur_size
- value
,
5557 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
5558 elfcpp::STV_HIDDEN
, 0, false, false);
5563 // Write out save/restore.
5565 template<int size
, bool big_endian
>
5567 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
5569 const section_size_type off
= this->offset();
5570 const section_size_type oview_size
=
5571 convert_to_section_size_type(this->data_size());
5572 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5573 memcpy(oview
, this->contents_
, oview_size
);
5574 of
->write_output_view(off
, oview_size
, oview
);
5578 // Create the glink section.
5580 template<int size
, bool big_endian
>
5582 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
5584 if (this->glink_
== NULL
)
5586 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
5587 this->glink_
->add_eh_frame(layout
);
5588 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
5589 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
5590 this->glink_
, ORDER_TEXT
, false);
5594 // Create a PLT entry for a global symbol.
5596 template<int size
, bool big_endian
>
5598 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
5602 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
5603 && gsym
->can_use_relative_reloc(false))
5605 if (this->iplt_
== NULL
)
5606 this->make_iplt_section(symtab
, layout
);
5607 this->iplt_
->add_ifunc_entry(gsym
);
5611 if (this->plt_
== NULL
)
5612 this->make_plt_section(symtab
, layout
);
5613 this->plt_
->add_entry(gsym
);
5617 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5619 template<int size
, bool big_endian
>
5621 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
5622 Symbol_table
* symtab
,
5624 Sized_relobj_file
<size
, big_endian
>* relobj
,
5627 if (this->iplt_
== NULL
)
5628 this->make_iplt_section(symtab
, layout
);
5629 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
5632 // Return the number of entries in the PLT.
5634 template<int size
, bool big_endian
>
5636 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
5638 if (this->plt_
== NULL
)
5640 return this->plt_
->entry_count();
5643 // Create a GOT entry for local dynamic __tls_get_addr calls.
5645 template<int size
, bool big_endian
>
5647 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
5648 Symbol_table
* symtab
,
5650 Sized_relobj_file
<size
, big_endian
>* object
)
5652 if (this->tlsld_got_offset_
== -1U)
5654 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
5655 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
5656 Output_data_got_powerpc
<size
, big_endian
>* got
5657 = this->got_section(symtab
, layout
);
5658 unsigned int got_offset
= got
->add_constant_pair(0, 0);
5659 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
5661 this->tlsld_got_offset_
= got_offset
;
5663 return this->tlsld_got_offset_
;
5666 // Get the Reference_flags for a particular relocation.
5668 template<int size
, bool big_endian
>
5670 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(
5671 unsigned int r_type
,
5672 const Target_powerpc
* target
)
5678 case elfcpp::R_POWERPC_NONE
:
5679 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5680 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5681 case elfcpp::R_PPC64_TOC
:
5682 // No symbol reference.
5685 case elfcpp::R_PPC64_ADDR64
:
5686 case elfcpp::R_PPC64_UADDR64
:
5687 case elfcpp::R_POWERPC_ADDR32
:
5688 case elfcpp::R_POWERPC_UADDR32
:
5689 case elfcpp::R_POWERPC_ADDR16
:
5690 case elfcpp::R_POWERPC_UADDR16
:
5691 case elfcpp::R_POWERPC_ADDR16_LO
:
5692 case elfcpp::R_POWERPC_ADDR16_HI
:
5693 case elfcpp::R_POWERPC_ADDR16_HA
:
5694 ref
= Symbol::ABSOLUTE_REF
;
5697 case elfcpp::R_POWERPC_ADDR24
:
5698 case elfcpp::R_POWERPC_ADDR14
:
5699 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5700 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5701 ref
= Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
5704 case elfcpp::R_PPC64_REL64
:
5705 case elfcpp::R_POWERPC_REL32
:
5706 case elfcpp::R_PPC_LOCAL24PC
:
5707 case elfcpp::R_POWERPC_REL16
:
5708 case elfcpp::R_POWERPC_REL16_LO
:
5709 case elfcpp::R_POWERPC_REL16_HI
:
5710 case elfcpp::R_POWERPC_REL16_HA
:
5711 ref
= Symbol::RELATIVE_REF
;
5714 case elfcpp::R_POWERPC_REL24
:
5715 case elfcpp::R_PPC_PLTREL24
:
5716 case elfcpp::R_POWERPC_REL14
:
5717 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5718 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5719 ref
= Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
5722 case elfcpp::R_POWERPC_GOT16
:
5723 case elfcpp::R_POWERPC_GOT16_LO
:
5724 case elfcpp::R_POWERPC_GOT16_HI
:
5725 case elfcpp::R_POWERPC_GOT16_HA
:
5726 case elfcpp::R_PPC64_GOT16_DS
:
5727 case elfcpp::R_PPC64_GOT16_LO_DS
:
5728 case elfcpp::R_PPC64_TOC16
:
5729 case elfcpp::R_PPC64_TOC16_LO
:
5730 case elfcpp::R_PPC64_TOC16_HI
:
5731 case elfcpp::R_PPC64_TOC16_HA
:
5732 case elfcpp::R_PPC64_TOC16_DS
:
5733 case elfcpp::R_PPC64_TOC16_LO_DS
:
5734 ref
= Symbol::RELATIVE_REF
;
5737 case elfcpp::R_POWERPC_GOT_TPREL16
:
5738 case elfcpp::R_POWERPC_TLS
:
5739 ref
= Symbol::TLS_REF
;
5742 case elfcpp::R_POWERPC_COPY
:
5743 case elfcpp::R_POWERPC_GLOB_DAT
:
5744 case elfcpp::R_POWERPC_JMP_SLOT
:
5745 case elfcpp::R_POWERPC_RELATIVE
:
5746 case elfcpp::R_POWERPC_DTPMOD
:
5748 // Not expected. We will give an error later.
5752 if (size
== 64 && target
->abiversion() < 2)
5753 ref
|= Symbol::FUNC_DESC_ABI
;
5757 // Report an unsupported relocation against a local symbol.
5759 template<int size
, bool big_endian
>
5761 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
5762 Sized_relobj_file
<size
, big_endian
>* object
,
5763 unsigned int r_type
)
5765 gold_error(_("%s: unsupported reloc %u against local symbol"),
5766 object
->name().c_str(), r_type
);
5769 // We are about to emit a dynamic relocation of type R_TYPE. If the
5770 // dynamic linker does not support it, issue an error.
5772 template<int size
, bool big_endian
>
5774 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
5775 unsigned int r_type
)
5777 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
5779 // These are the relocation types supported by glibc for both 32-bit
5780 // and 64-bit powerpc.
5783 case elfcpp::R_POWERPC_NONE
:
5784 case elfcpp::R_POWERPC_RELATIVE
:
5785 case elfcpp::R_POWERPC_GLOB_DAT
:
5786 case elfcpp::R_POWERPC_DTPMOD
:
5787 case elfcpp::R_POWERPC_DTPREL
:
5788 case elfcpp::R_POWERPC_TPREL
:
5789 case elfcpp::R_POWERPC_JMP_SLOT
:
5790 case elfcpp::R_POWERPC_COPY
:
5791 case elfcpp::R_POWERPC_IRELATIVE
:
5792 case elfcpp::R_POWERPC_ADDR32
:
5793 case elfcpp::R_POWERPC_UADDR32
:
5794 case elfcpp::R_POWERPC_ADDR24
:
5795 case elfcpp::R_POWERPC_ADDR16
:
5796 case elfcpp::R_POWERPC_UADDR16
:
5797 case elfcpp::R_POWERPC_ADDR16_LO
:
5798 case elfcpp::R_POWERPC_ADDR16_HI
:
5799 case elfcpp::R_POWERPC_ADDR16_HA
:
5800 case elfcpp::R_POWERPC_ADDR14
:
5801 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5802 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5803 case elfcpp::R_POWERPC_REL32
:
5804 case elfcpp::R_POWERPC_REL24
:
5805 case elfcpp::R_POWERPC_TPREL16
:
5806 case elfcpp::R_POWERPC_TPREL16_LO
:
5807 case elfcpp::R_POWERPC_TPREL16_HI
:
5808 case elfcpp::R_POWERPC_TPREL16_HA
:
5819 // These are the relocation types supported only on 64-bit.
5820 case elfcpp::R_PPC64_ADDR64
:
5821 case elfcpp::R_PPC64_UADDR64
:
5822 case elfcpp::R_PPC64_JMP_IREL
:
5823 case elfcpp::R_PPC64_ADDR16_DS
:
5824 case elfcpp::R_PPC64_ADDR16_LO_DS
:
5825 case elfcpp::R_PPC64_ADDR16_HIGH
:
5826 case elfcpp::R_PPC64_ADDR16_HIGHA
:
5827 case elfcpp::R_PPC64_ADDR16_HIGHER
:
5828 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
5829 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
5830 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
5831 case elfcpp::R_PPC64_REL64
:
5832 case elfcpp::R_POWERPC_ADDR30
:
5833 case elfcpp::R_PPC64_TPREL16_DS
:
5834 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5835 case elfcpp::R_PPC64_TPREL16_HIGH
:
5836 case elfcpp::R_PPC64_TPREL16_HIGHA
:
5837 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5838 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5839 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5840 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5851 // These are the relocation types supported only on 32-bit.
5852 // ??? glibc ld.so doesn't need to support these.
5853 case elfcpp::R_POWERPC_DTPREL16
:
5854 case elfcpp::R_POWERPC_DTPREL16_LO
:
5855 case elfcpp::R_POWERPC_DTPREL16_HI
:
5856 case elfcpp::R_POWERPC_DTPREL16_HA
:
5864 // This prevents us from issuing more than one error per reloc
5865 // section. But we can still wind up issuing more than one
5866 // error per object file.
5867 if (this->issued_non_pic_error_
)
5869 gold_assert(parameters
->options().output_is_position_independent());
5870 object
->error(_("requires unsupported dynamic reloc; "
5871 "recompile with -fPIC"));
5872 this->issued_non_pic_error_
= true;
5876 // Return whether we need to make a PLT entry for a relocation of the
5877 // given type against a STT_GNU_IFUNC symbol.
5879 template<int size
, bool big_endian
>
5881 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
5882 Target_powerpc
<size
, big_endian
>* target
,
5883 Sized_relobj_file
<size
, big_endian
>* object
,
5884 unsigned int r_type
,
5887 // In non-pic code any reference will resolve to the plt call stub
5888 // for the ifunc symbol.
5889 if ((size
== 32 || target
->abiversion() >= 2)
5890 && !parameters
->options().output_is_position_independent())
5895 // Word size refs from data sections are OK, but don't need a PLT entry.
5896 case elfcpp::R_POWERPC_ADDR32
:
5897 case elfcpp::R_POWERPC_UADDR32
:
5902 case elfcpp::R_PPC64_ADDR64
:
5903 case elfcpp::R_PPC64_UADDR64
:
5908 // GOT refs are good, but also don't need a PLT entry.
5909 case elfcpp::R_POWERPC_GOT16
:
5910 case elfcpp::R_POWERPC_GOT16_LO
:
5911 case elfcpp::R_POWERPC_GOT16_HI
:
5912 case elfcpp::R_POWERPC_GOT16_HA
:
5913 case elfcpp::R_PPC64_GOT16_DS
:
5914 case elfcpp::R_PPC64_GOT16_LO_DS
:
5917 // Function calls are good, and these do need a PLT entry.
5918 case elfcpp::R_POWERPC_ADDR24
:
5919 case elfcpp::R_POWERPC_ADDR14
:
5920 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5921 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5922 case elfcpp::R_POWERPC_REL24
:
5923 case elfcpp::R_PPC_PLTREL24
:
5924 case elfcpp::R_POWERPC_REL14
:
5925 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5926 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5933 // Anything else is a problem.
5934 // If we are building a static executable, the libc startup function
5935 // responsible for applying indirect function relocations is going
5936 // to complain about the reloc type.
5937 // If we are building a dynamic executable, we will have a text
5938 // relocation. The dynamic loader will set the text segment
5939 // writable and non-executable to apply text relocations. So we'll
5940 // segfault when trying to run the indirection function to resolve
5943 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
5944 object
->name().c_str(), r_type
);
5948 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
5952 ok_lo_toc_insn(uint32_t insn
, unsigned int r_type
)
5954 return ((insn
& (0x3f << 26)) == 12u << 26 /* addic */
5955 || (insn
& (0x3f << 26)) == 14u << 26 /* addi */
5956 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
5957 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
5958 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
5959 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
5960 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
5961 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
5962 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
5963 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
5964 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
5965 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
5966 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
5967 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
5968 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
5969 || (insn
& (0x3f << 26)) == 56u << 26 /* lq,lfq */
5970 || ((insn
& (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
5971 /* Exclude lfqu by testing reloc. If relocs are ever
5972 defined for the reduced D field in psq_lu then those
5973 will need testing too. */
5974 && r_type
!= elfcpp::R_PPC64_TOC16_LO
5975 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
5976 || ((insn
& (0x3f << 26)) == 58u << 26 /* ld,lwa */
5978 || (insn
& (0x3f << 26)) == 60u << 26 /* stfq */
5979 || ((insn
& (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
5980 /* Exclude stfqu. psq_stu as above for psq_lu. */
5981 && r_type
!= elfcpp::R_PPC64_TOC16_LO
5982 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
5983 || ((insn
& (0x3f << 26)) == 62u << 26 /* std,stq */
5984 && (insn
& 1) == 0));
5987 // Scan a relocation for a local symbol.
5989 template<int size
, bool big_endian
>
5991 Target_powerpc
<size
, big_endian
>::Scan::local(
5992 Symbol_table
* symtab
,
5994 Target_powerpc
<size
, big_endian
>* target
,
5995 Sized_relobj_file
<size
, big_endian
>* object
,
5996 unsigned int data_shndx
,
5997 Output_section
* output_section
,
5998 const elfcpp::Rela
<size
, big_endian
>& reloc
,
5999 unsigned int r_type
,
6000 const elfcpp::Sym
<size
, big_endian
>& lsym
,
6003 this->maybe_skip_tls_get_addr_call(r_type
, NULL
);
6005 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6006 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6008 this->expect_tls_get_addr_call();
6009 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
6010 if (tls_type
!= tls::TLSOPT_NONE
)
6011 this->skip_next_tls_get_addr_call();
6013 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6014 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6016 this->expect_tls_get_addr_call();
6017 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6018 if (tls_type
!= tls::TLSOPT_NONE
)
6019 this->skip_next_tls_get_addr_call();
6022 Powerpc_relobj
<size
, big_endian
>* ppc_object
6023 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6028 && data_shndx
== ppc_object
->opd_shndx()
6029 && r_type
== elfcpp::R_PPC64_ADDR64
)
6030 ppc_object
->set_opd_discard(reloc
.get_r_offset());
6034 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6035 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
6036 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
6038 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6039 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6040 r_type
, r_sym
, reloc
.get_r_addend());
6041 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
6046 case elfcpp::R_POWERPC_NONE
:
6047 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6048 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6049 case elfcpp::R_PPC64_TOCSAVE
:
6050 case elfcpp::R_POWERPC_TLS
:
6051 case elfcpp::R_PPC64_ENTRY
:
6054 case elfcpp::R_PPC64_TOC
:
6056 Output_data_got_powerpc
<size
, big_endian
>* got
6057 = target
->got_section(symtab
, layout
);
6058 if (parameters
->options().output_is_position_independent())
6060 Address off
= reloc
.get_r_offset();
6062 && target
->abiversion() < 2
6063 && data_shndx
== ppc_object
->opd_shndx()
6064 && ppc_object
->get_opd_discard(off
- 8))
6067 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6068 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
6069 rela_dyn
->add_output_section_relative(got
->output_section(),
6070 elfcpp::R_POWERPC_RELATIVE
,
6072 object
, data_shndx
, off
,
6073 symobj
->toc_base_offset());
6078 case elfcpp::R_PPC64_ADDR64
:
6079 case elfcpp::R_PPC64_UADDR64
:
6080 case elfcpp::R_POWERPC_ADDR32
:
6081 case elfcpp::R_POWERPC_UADDR32
:
6082 case elfcpp::R_POWERPC_ADDR24
:
6083 case elfcpp::R_POWERPC_ADDR16
:
6084 case elfcpp::R_POWERPC_ADDR16_LO
:
6085 case elfcpp::R_POWERPC_ADDR16_HI
:
6086 case elfcpp::R_POWERPC_ADDR16_HA
:
6087 case elfcpp::R_POWERPC_UADDR16
:
6088 case elfcpp::R_PPC64_ADDR16_HIGH
:
6089 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6090 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6091 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6092 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6093 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6094 case elfcpp::R_PPC64_ADDR16_DS
:
6095 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6096 case elfcpp::R_POWERPC_ADDR14
:
6097 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6098 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6099 // If building a shared library (or a position-independent
6100 // executable), we need to create a dynamic relocation for
6102 if (parameters
->options().output_is_position_independent()
6103 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
6105 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
6107 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6108 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
6109 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
6111 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6112 : elfcpp::R_POWERPC_RELATIVE
);
6113 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
6114 output_section
, data_shndx
,
6115 reloc
.get_r_offset(),
6116 reloc
.get_r_addend(), false);
6118 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
6120 check_non_pic(object
, r_type
);
6121 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
6122 data_shndx
, reloc
.get_r_offset(),
6123 reloc
.get_r_addend());
6127 gold_assert(lsym
.get_st_value() == 0);
6128 unsigned int shndx
= lsym
.get_st_shndx();
6130 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
6133 object
->error(_("section symbol %u has bad shndx %u"),
6136 rela_dyn
->add_local_section(object
, shndx
, r_type
,
6137 output_section
, data_shndx
,
6138 reloc
.get_r_offset());
6143 case elfcpp::R_POWERPC_REL24
:
6144 case elfcpp::R_PPC_PLTREL24
:
6145 case elfcpp::R_PPC_LOCAL24PC
:
6146 case elfcpp::R_POWERPC_REL14
:
6147 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6148 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6151 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6152 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6153 r_type
, r_sym
, reloc
.get_r_addend());
6157 case elfcpp::R_PPC64_REL64
:
6158 case elfcpp::R_POWERPC_REL32
:
6159 case elfcpp::R_POWERPC_REL16
:
6160 case elfcpp::R_POWERPC_REL16_LO
:
6161 case elfcpp::R_POWERPC_REL16_HI
:
6162 case elfcpp::R_POWERPC_REL16_HA
:
6163 case elfcpp::R_POWERPC_REL16DX_HA
:
6164 case elfcpp::R_POWERPC_SECTOFF
:
6165 case elfcpp::R_POWERPC_SECTOFF_LO
:
6166 case elfcpp::R_POWERPC_SECTOFF_HI
:
6167 case elfcpp::R_POWERPC_SECTOFF_HA
:
6168 case elfcpp::R_PPC64_SECTOFF_DS
:
6169 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6170 case elfcpp::R_POWERPC_TPREL16
:
6171 case elfcpp::R_POWERPC_TPREL16_LO
:
6172 case elfcpp::R_POWERPC_TPREL16_HI
:
6173 case elfcpp::R_POWERPC_TPREL16_HA
:
6174 case elfcpp::R_PPC64_TPREL16_DS
:
6175 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6176 case elfcpp::R_PPC64_TPREL16_HIGH
:
6177 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6178 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6179 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6180 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6181 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6182 case elfcpp::R_POWERPC_DTPREL16
:
6183 case elfcpp::R_POWERPC_DTPREL16_LO
:
6184 case elfcpp::R_POWERPC_DTPREL16_HI
:
6185 case elfcpp::R_POWERPC_DTPREL16_HA
:
6186 case elfcpp::R_PPC64_DTPREL16_DS
:
6187 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6188 case elfcpp::R_PPC64_DTPREL16_HIGH
:
6189 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
6190 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6191 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6192 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6193 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6194 case elfcpp::R_PPC64_TLSGD
:
6195 case elfcpp::R_PPC64_TLSLD
:
6196 case elfcpp::R_PPC64_ADDR64_LOCAL
:
6199 case elfcpp::R_POWERPC_GOT16
:
6200 case elfcpp::R_POWERPC_GOT16_LO
:
6201 case elfcpp::R_POWERPC_GOT16_HI
:
6202 case elfcpp::R_POWERPC_GOT16_HA
:
6203 case elfcpp::R_PPC64_GOT16_DS
:
6204 case elfcpp::R_PPC64_GOT16_LO_DS
:
6206 // The symbol requires a GOT entry.
6207 Output_data_got_powerpc
<size
, big_endian
>* got
6208 = target
->got_section(symtab
, layout
);
6209 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6211 if (!parameters
->options().output_is_position_independent())
6214 && (size
== 32 || target
->abiversion() >= 2))
6215 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
6217 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
6219 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
6221 // If we are generating a shared object or a pie, this
6222 // symbol's GOT entry will be set by a dynamic relocation.
6224 off
= got
->add_constant(0);
6225 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
6227 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
6229 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6230 : elfcpp::R_POWERPC_RELATIVE
);
6231 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
6232 got
, off
, 0, false);
6237 case elfcpp::R_PPC64_TOC16
:
6238 case elfcpp::R_PPC64_TOC16_LO
:
6239 case elfcpp::R_PPC64_TOC16_HI
:
6240 case elfcpp::R_PPC64_TOC16_HA
:
6241 case elfcpp::R_PPC64_TOC16_DS
:
6242 case elfcpp::R_PPC64_TOC16_LO_DS
:
6243 // We need a GOT section.
6244 target
->got_section(symtab
, layout
);
6247 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6248 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6249 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6250 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6252 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
6253 if (tls_type
== tls::TLSOPT_NONE
)
6255 Output_data_got_powerpc
<size
, big_endian
>* got
6256 = target
->got_section(symtab
, layout
);
6257 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6258 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6259 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
6260 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
6262 else if (tls_type
== tls::TLSOPT_TO_LE
)
6264 // no GOT relocs needed for Local Exec.
6271 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6272 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6273 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6274 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6276 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6277 if (tls_type
== tls::TLSOPT_NONE
)
6278 target
->tlsld_got_offset(symtab
, layout
, object
);
6279 else if (tls_type
== tls::TLSOPT_TO_LE
)
6281 // no GOT relocs needed for Local Exec.
6282 if (parameters
->options().emit_relocs())
6284 Output_section
* os
= layout
->tls_segment()->first_section();
6285 gold_assert(os
!= NULL
);
6286 os
->set_needs_symtab_index();
6294 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6295 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6296 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6297 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6299 Output_data_got_powerpc
<size
, big_endian
>* got
6300 = target
->got_section(symtab
, layout
);
6301 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6302 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
6306 case elfcpp::R_POWERPC_GOT_TPREL16
:
6307 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6308 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6309 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6311 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
6312 if (tls_type
== tls::TLSOPT_NONE
)
6314 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6315 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
6317 Output_data_got_powerpc
<size
, big_endian
>* got
6318 = target
->got_section(symtab
, layout
);
6319 unsigned int off
= got
->add_constant(0);
6320 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
6322 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6323 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
6324 elfcpp::R_POWERPC_TPREL
,
6328 else if (tls_type
== tls::TLSOPT_TO_LE
)
6330 // no GOT relocs needed for Local Exec.
6338 unsupported_reloc_local(object
, r_type
);
6343 && parameters
->options().toc_optimize())
6345 if (data_shndx
== ppc_object
->toc_shndx())
6348 if (r_type
!= elfcpp::R_PPC64_ADDR64
6349 || (is_ifunc
&& target
->abiversion() < 2))
6351 else if (parameters
->options().output_is_position_independent())
6357 unsigned int shndx
= lsym
.get_st_shndx();
6358 if (shndx
>= elfcpp::SHN_LORESERVE
6359 && shndx
!= elfcpp::SHN_XINDEX
)
6364 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
6367 enum {no_check
, check_lo
, check_ha
} insn_check
;
6371 insn_check
= no_check
;
6374 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6375 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6376 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6377 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6378 case elfcpp::R_POWERPC_GOT16_HA
:
6379 case elfcpp::R_PPC64_TOC16_HA
:
6380 insn_check
= check_ha
;
6383 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6384 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6385 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6386 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6387 case elfcpp::R_POWERPC_GOT16_LO
:
6388 case elfcpp::R_PPC64_GOT16_LO_DS
:
6389 case elfcpp::R_PPC64_TOC16_LO
:
6390 case elfcpp::R_PPC64_TOC16_LO_DS
:
6391 insn_check
= check_lo
;
6395 section_size_type slen
;
6396 const unsigned char* view
= NULL
;
6397 if (insn_check
!= no_check
)
6399 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
6400 section_size_type off
=
6401 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
6404 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
6405 if (insn_check
== check_lo
6406 ? !ok_lo_toc_insn(insn
, r_type
)
6407 : ((insn
& ((0x3f << 26) | 0x1f << 16))
6408 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
6410 ppc_object
->set_no_toc_opt();
6411 gold_warning(_("%s: toc optimization is not supported "
6412 "for %#08x instruction"),
6413 ppc_object
->name().c_str(), insn
);
6422 case elfcpp::R_PPC64_TOC16
:
6423 case elfcpp::R_PPC64_TOC16_LO
:
6424 case elfcpp::R_PPC64_TOC16_HI
:
6425 case elfcpp::R_PPC64_TOC16_HA
:
6426 case elfcpp::R_PPC64_TOC16_DS
:
6427 case elfcpp::R_PPC64_TOC16_LO_DS
:
6428 unsigned int shndx
= lsym
.get_st_shndx();
6429 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6431 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6432 if (is_ordinary
&& shndx
== ppc_object
->toc_shndx())
6434 Address dst_off
= lsym
.get_st_value() + reloc
.get_r_offset();
6435 if (dst_off
< ppc_object
->section_size(shndx
))
6438 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
6440 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
6442 // Need to check that the insn is a ld
6444 view
= ppc_object
->section_contents(data_shndx
,
6447 section_size_type off
=
6448 (convert_to_section_size_type(reloc
.get_r_offset())
6449 + (big_endian
? -2 : 3));
6451 && (view
[off
] & (0x3f << 2)) == 58u << 2)
6455 ppc_object
->set_no_toc_opt(dst_off
);
6466 case elfcpp::R_POWERPC_REL32
:
6467 if (ppc_object
->got2_shndx() != 0
6468 && parameters
->options().output_is_position_independent())
6470 unsigned int shndx
= lsym
.get_st_shndx();
6471 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6473 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6474 if (is_ordinary
&& shndx
== ppc_object
->got2_shndx()
6475 && (ppc_object
->section_flags(data_shndx
)
6476 & elfcpp::SHF_EXECINSTR
) != 0)
6477 gold_error(_("%s: unsupported -mbss-plt code"),
6478 ppc_object
->name().c_str());
6488 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6489 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6490 case elfcpp::R_POWERPC_GOT_TPREL16
:
6491 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6492 case elfcpp::R_POWERPC_GOT16
:
6493 case elfcpp::R_PPC64_GOT16_DS
:
6494 case elfcpp::R_PPC64_TOC16
:
6495 case elfcpp::R_PPC64_TOC16_DS
:
6496 ppc_object
->set_has_small_toc_reloc();
6502 // Report an unsupported relocation against a global symbol.
6504 template<int size
, bool big_endian
>
6506 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
6507 Sized_relobj_file
<size
, big_endian
>* object
,
6508 unsigned int r_type
,
6511 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6512 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
6515 // Scan a relocation for a global symbol.
6517 template<int size
, bool big_endian
>
6519 Target_powerpc
<size
, big_endian
>::Scan::global(
6520 Symbol_table
* symtab
,
6522 Target_powerpc
<size
, big_endian
>* target
,
6523 Sized_relobj_file
<size
, big_endian
>* object
,
6524 unsigned int data_shndx
,
6525 Output_section
* output_section
,
6526 const elfcpp::Rela
<size
, big_endian
>& reloc
,
6527 unsigned int r_type
,
6530 if (this->maybe_skip_tls_get_addr_call(r_type
, gsym
) == Track_tls::SKIP
)
6533 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6534 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6536 this->expect_tls_get_addr_call();
6537 const bool final
= gsym
->final_value_is_known();
6538 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6539 if (tls_type
!= tls::TLSOPT_NONE
)
6540 this->skip_next_tls_get_addr_call();
6542 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6543 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6545 this->expect_tls_get_addr_call();
6546 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6547 if (tls_type
!= tls::TLSOPT_NONE
)
6548 this->skip_next_tls_get_addr_call();
6551 Powerpc_relobj
<size
, big_endian
>* ppc_object
6552 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6554 // A STT_GNU_IFUNC symbol may require a PLT entry.
6555 bool is_ifunc
= gsym
->type() == elfcpp::STT_GNU_IFUNC
;
6556 bool pushed_ifunc
= false;
6557 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
6559 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6560 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6561 r_type
, r_sym
, reloc
.get_r_addend());
6562 target
->make_plt_entry(symtab
, layout
, gsym
);
6563 pushed_ifunc
= true;
6568 case elfcpp::R_POWERPC_NONE
:
6569 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6570 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6571 case elfcpp::R_PPC_LOCAL24PC
:
6572 case elfcpp::R_POWERPC_TLS
:
6573 case elfcpp::R_PPC64_ENTRY
:
6576 case elfcpp::R_PPC64_TOC
:
6578 Output_data_got_powerpc
<size
, big_endian
>* got
6579 = target
->got_section(symtab
, layout
);
6580 if (parameters
->options().output_is_position_independent())
6582 Address off
= reloc
.get_r_offset();
6584 && data_shndx
== ppc_object
->opd_shndx()
6585 && ppc_object
->get_opd_discard(off
- 8))
6588 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6589 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
6590 if (data_shndx
!= ppc_object
->opd_shndx())
6591 symobj
= static_cast
6592 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
6593 rela_dyn
->add_output_section_relative(got
->output_section(),
6594 elfcpp::R_POWERPC_RELATIVE
,
6596 object
, data_shndx
, off
,
6597 symobj
->toc_base_offset());
6602 case elfcpp::R_PPC64_ADDR64
:
6604 && target
->abiversion() < 2
6605 && data_shndx
== ppc_object
->opd_shndx()
6606 && (gsym
->is_defined_in_discarded_section()
6607 || gsym
->object() != object
))
6609 ppc_object
->set_opd_discard(reloc
.get_r_offset());
6613 case elfcpp::R_PPC64_UADDR64
:
6614 case elfcpp::R_POWERPC_ADDR32
:
6615 case elfcpp::R_POWERPC_UADDR32
:
6616 case elfcpp::R_POWERPC_ADDR24
:
6617 case elfcpp::R_POWERPC_ADDR16
:
6618 case elfcpp::R_POWERPC_ADDR16_LO
:
6619 case elfcpp::R_POWERPC_ADDR16_HI
:
6620 case elfcpp::R_POWERPC_ADDR16_HA
:
6621 case elfcpp::R_POWERPC_UADDR16
:
6622 case elfcpp::R_PPC64_ADDR16_HIGH
:
6623 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6624 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6625 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6626 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6627 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6628 case elfcpp::R_PPC64_ADDR16_DS
:
6629 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6630 case elfcpp::R_POWERPC_ADDR14
:
6631 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6632 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6634 // Make a PLT entry if necessary.
6635 if (gsym
->needs_plt_entry())
6637 // Since this is not a PC-relative relocation, we may be
6638 // taking the address of a function. In that case we need to
6639 // set the entry in the dynamic symbol table to the address of
6640 // the PLT call stub.
6641 bool need_ifunc_plt
= false;
6642 if ((size
== 32 || target
->abiversion() >= 2)
6643 && gsym
->is_from_dynobj()
6644 && !parameters
->options().output_is_position_independent())
6646 gsym
->set_needs_dynsym_value();
6647 need_ifunc_plt
= true;
6649 if (!is_ifunc
|| (!pushed_ifunc
&& need_ifunc_plt
))
6651 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6652 target
->push_branch(ppc_object
, data_shndx
,
6653 reloc
.get_r_offset(), r_type
, r_sym
,
6654 reloc
.get_r_addend());
6655 target
->make_plt_entry(symtab
, layout
, gsym
);
6658 // Make a dynamic relocation if necessary.
6659 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
))
6660 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
6662 if (!parameters
->options().output_is_position_independent()
6663 && gsym
->may_need_copy_reloc())
6665 target
->copy_reloc(symtab
, layout
, object
,
6666 data_shndx
, output_section
, gsym
, reloc
);
6668 else if ((((size
== 32
6669 && r_type
== elfcpp::R_POWERPC_ADDR32
)
6671 && r_type
== elfcpp::R_PPC64_ADDR64
6672 && target
->abiversion() >= 2))
6673 && gsym
->can_use_relative_reloc(false)
6674 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
6675 && parameters
->options().shared()))
6677 && r_type
== elfcpp::R_PPC64_ADDR64
6678 && target
->abiversion() < 2
6679 && (gsym
->can_use_relative_reloc(false)
6680 || data_shndx
== ppc_object
->opd_shndx())))
6682 Reloc_section
* rela_dyn
6683 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6684 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6685 : elfcpp::R_POWERPC_RELATIVE
);
6686 rela_dyn
->add_symbolless_global_addend(
6687 gsym
, dynrel
, output_section
, object
, data_shndx
,
6688 reloc
.get_r_offset(), reloc
.get_r_addend());
6692 Reloc_section
* rela_dyn
6693 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6694 check_non_pic(object
, r_type
);
6695 rela_dyn
->add_global(gsym
, r_type
, output_section
,
6697 reloc
.get_r_offset(),
6698 reloc
.get_r_addend());
6701 && parameters
->options().toc_optimize()
6702 && data_shndx
== ppc_object
->toc_shndx())
6703 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
6709 case elfcpp::R_PPC_PLTREL24
:
6710 case elfcpp::R_POWERPC_REL24
:
6713 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6714 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6715 r_type
, r_sym
, reloc
.get_r_addend());
6716 if (gsym
->needs_plt_entry()
6717 || (!gsym
->final_value_is_known()
6718 && (gsym
->is_undefined()
6719 || gsym
->is_from_dynobj()
6720 || gsym
->is_preemptible())))
6721 target
->make_plt_entry(symtab
, layout
, gsym
);
6725 case elfcpp::R_PPC64_REL64
:
6726 case elfcpp::R_POWERPC_REL32
:
6727 // Make a dynamic relocation if necessary.
6728 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
)))
6730 if (!parameters
->options().output_is_position_independent()
6731 && gsym
->may_need_copy_reloc())
6733 target
->copy_reloc(symtab
, layout
, object
,
6734 data_shndx
, output_section
, gsym
,
6739 Reloc_section
* rela_dyn
6740 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6741 check_non_pic(object
, r_type
);
6742 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
6743 data_shndx
, reloc
.get_r_offset(),
6744 reloc
.get_r_addend());
6749 case elfcpp::R_POWERPC_REL14
:
6750 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6751 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6754 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6755 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6756 r_type
, r_sym
, reloc
.get_r_addend());
6760 case elfcpp::R_POWERPC_REL16
:
6761 case elfcpp::R_POWERPC_REL16_LO
:
6762 case elfcpp::R_POWERPC_REL16_HI
:
6763 case elfcpp::R_POWERPC_REL16_HA
:
6764 case elfcpp::R_POWERPC_REL16DX_HA
:
6765 case elfcpp::R_POWERPC_SECTOFF
:
6766 case elfcpp::R_POWERPC_SECTOFF_LO
:
6767 case elfcpp::R_POWERPC_SECTOFF_HI
:
6768 case elfcpp::R_POWERPC_SECTOFF_HA
:
6769 case elfcpp::R_PPC64_SECTOFF_DS
:
6770 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6771 case elfcpp::R_POWERPC_TPREL16
:
6772 case elfcpp::R_POWERPC_TPREL16_LO
:
6773 case elfcpp::R_POWERPC_TPREL16_HI
:
6774 case elfcpp::R_POWERPC_TPREL16_HA
:
6775 case elfcpp::R_PPC64_TPREL16_DS
:
6776 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6777 case elfcpp::R_PPC64_TPREL16_HIGH
:
6778 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6779 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6780 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6781 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6782 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6783 case elfcpp::R_POWERPC_DTPREL16
:
6784 case elfcpp::R_POWERPC_DTPREL16_LO
:
6785 case elfcpp::R_POWERPC_DTPREL16_HI
:
6786 case elfcpp::R_POWERPC_DTPREL16_HA
:
6787 case elfcpp::R_PPC64_DTPREL16_DS
:
6788 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6789 case elfcpp::R_PPC64_DTPREL16_HIGH
:
6790 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
6791 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6792 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6793 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6794 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6795 case elfcpp::R_PPC64_TLSGD
:
6796 case elfcpp::R_PPC64_TLSLD
:
6797 case elfcpp::R_PPC64_ADDR64_LOCAL
:
6800 case elfcpp::R_POWERPC_GOT16
:
6801 case elfcpp::R_POWERPC_GOT16_LO
:
6802 case elfcpp::R_POWERPC_GOT16_HI
:
6803 case elfcpp::R_POWERPC_GOT16_HA
:
6804 case elfcpp::R_PPC64_GOT16_DS
:
6805 case elfcpp::R_PPC64_GOT16_LO_DS
:
6807 // The symbol requires a GOT entry.
6808 Output_data_got_powerpc
<size
, big_endian
>* got
;
6810 got
= target
->got_section(symtab
, layout
);
6811 if (gsym
->final_value_is_known())
6814 && (size
== 32 || target
->abiversion() >= 2))
6815 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
6817 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
6819 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
6821 // If we are generating a shared object or a pie, this
6822 // symbol's GOT entry will be set by a dynamic relocation.
6823 unsigned int off
= got
->add_constant(0);
6824 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
6826 Reloc_section
* rela_dyn
6827 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6829 if (gsym
->can_use_relative_reloc(false)
6831 || target
->abiversion() >= 2)
6832 && gsym
->visibility() == elfcpp::STV_PROTECTED
6833 && parameters
->options().shared()))
6835 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6836 : elfcpp::R_POWERPC_RELATIVE
);
6837 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
6841 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
6842 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
6848 case elfcpp::R_PPC64_TOC16
:
6849 case elfcpp::R_PPC64_TOC16_LO
:
6850 case elfcpp::R_PPC64_TOC16_HI
:
6851 case elfcpp::R_PPC64_TOC16_HA
:
6852 case elfcpp::R_PPC64_TOC16_DS
:
6853 case elfcpp::R_PPC64_TOC16_LO_DS
:
6854 // We need a GOT section.
6855 target
->got_section(symtab
, layout
);
6858 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6859 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6860 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6861 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6863 const bool final
= gsym
->final_value_is_known();
6864 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6865 if (tls_type
== tls::TLSOPT_NONE
)
6867 Output_data_got_powerpc
<size
, big_endian
>* got
6868 = target
->got_section(symtab
, layout
);
6869 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6870 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
, rela_dyn
,
6871 elfcpp::R_POWERPC_DTPMOD
,
6872 elfcpp::R_POWERPC_DTPREL
);
6874 else if (tls_type
== tls::TLSOPT_TO_IE
)
6876 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
6878 Output_data_got_powerpc
<size
, big_endian
>* got
6879 = target
->got_section(symtab
, layout
);
6880 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6881 if (gsym
->is_undefined()
6882 || gsym
->is_from_dynobj())
6884 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
6885 elfcpp::R_POWERPC_TPREL
);
6889 unsigned int off
= got
->add_constant(0);
6890 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
6891 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
6892 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
6897 else if (tls_type
== tls::TLSOPT_TO_LE
)
6899 // no GOT relocs needed for Local Exec.
6906 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6907 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6908 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6909 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6911 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6912 if (tls_type
== tls::TLSOPT_NONE
)
6913 target
->tlsld_got_offset(symtab
, layout
, object
);
6914 else if (tls_type
== tls::TLSOPT_TO_LE
)
6916 // no GOT relocs needed for Local Exec.
6917 if (parameters
->options().emit_relocs())
6919 Output_section
* os
= layout
->tls_segment()->first_section();
6920 gold_assert(os
!= NULL
);
6921 os
->set_needs_symtab_index();
6929 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6930 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6931 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6932 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6934 Output_data_got_powerpc
<size
, big_endian
>* got
6935 = target
->got_section(symtab
, layout
);
6936 if (!gsym
->final_value_is_known()
6937 && (gsym
->is_from_dynobj()
6938 || gsym
->is_undefined()
6939 || gsym
->is_preemptible()))
6940 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
6941 target
->rela_dyn_section(layout
),
6942 elfcpp::R_POWERPC_DTPREL
);
6944 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
6948 case elfcpp::R_POWERPC_GOT_TPREL16
:
6949 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6950 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6951 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6953 const bool final
= gsym
->final_value_is_known();
6954 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
6955 if (tls_type
== tls::TLSOPT_NONE
)
6957 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
6959 Output_data_got_powerpc
<size
, big_endian
>* got
6960 = target
->got_section(symtab
, layout
);
6961 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6962 if (gsym
->is_undefined()
6963 || gsym
->is_from_dynobj())
6965 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
6966 elfcpp::R_POWERPC_TPREL
);
6970 unsigned int off
= got
->add_constant(0);
6971 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
6972 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
6973 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
6978 else if (tls_type
== tls::TLSOPT_TO_LE
)
6980 // no GOT relocs needed for Local Exec.
6988 unsupported_reloc_global(object
, r_type
, gsym
);
6993 && parameters
->options().toc_optimize())
6995 if (data_shndx
== ppc_object
->toc_shndx())
6998 if (r_type
!= elfcpp::R_PPC64_ADDR64
6999 || (is_ifunc
&& target
->abiversion() < 2))
7001 else if (parameters
->options().output_is_position_independent()
7002 && (is_ifunc
|| gsym
->is_absolute() || gsym
->is_undefined()))
7005 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
7008 enum {no_check
, check_lo
, check_ha
} insn_check
;
7012 insn_check
= no_check
;
7015 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7016 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7017 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7018 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7019 case elfcpp::R_POWERPC_GOT16_HA
:
7020 case elfcpp::R_PPC64_TOC16_HA
:
7021 insn_check
= check_ha
;
7024 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7025 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7026 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7027 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7028 case elfcpp::R_POWERPC_GOT16_LO
:
7029 case elfcpp::R_PPC64_GOT16_LO_DS
:
7030 case elfcpp::R_PPC64_TOC16_LO
:
7031 case elfcpp::R_PPC64_TOC16_LO_DS
:
7032 insn_check
= check_lo
;
7036 section_size_type slen
;
7037 const unsigned char* view
= NULL
;
7038 if (insn_check
!= no_check
)
7040 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
7041 section_size_type off
=
7042 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
7045 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
7046 if (insn_check
== check_lo
7047 ? !ok_lo_toc_insn(insn
, r_type
)
7048 : ((insn
& ((0x3f << 26) | 0x1f << 16))
7049 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7051 ppc_object
->set_no_toc_opt();
7052 gold_warning(_("%s: toc optimization is not supported "
7053 "for %#08x instruction"),
7054 ppc_object
->name().c_str(), insn
);
7063 case elfcpp::R_PPC64_TOC16
:
7064 case elfcpp::R_PPC64_TOC16_LO
:
7065 case elfcpp::R_PPC64_TOC16_HI
:
7066 case elfcpp::R_PPC64_TOC16_HA
:
7067 case elfcpp::R_PPC64_TOC16_DS
:
7068 case elfcpp::R_PPC64_TOC16_LO_DS
:
7069 if (gsym
->source() == Symbol::FROM_OBJECT
7070 && !gsym
->object()->is_dynamic())
7072 Powerpc_relobj
<size
, big_endian
>* sym_object
7073 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7075 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
7076 if (shndx
== sym_object
->toc_shndx())
7078 Sized_symbol
<size
>* sym
= symtab
->get_sized_symbol
<size
>(gsym
);
7079 Address dst_off
= sym
->value() + reloc
.get_r_offset();
7080 if (dst_off
< sym_object
->section_size(shndx
))
7083 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
7085 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
7087 // Need to check that the insn is a ld
7089 view
= ppc_object
->section_contents(data_shndx
,
7092 section_size_type off
=
7093 (convert_to_section_size_type(reloc
.get_r_offset())
7094 + (big_endian
? -2 : 3));
7096 && (view
[off
] & (0x3f << 2)) == (58u << 2))
7100 sym_object
->set_no_toc_opt(dst_off
);
7112 case elfcpp::R_PPC_LOCAL24PC
:
7113 if (strcmp(gsym
->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7114 gold_error(_("%s: unsupported -mbss-plt code"),
7115 ppc_object
->name().c_str());
7124 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7125 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7126 case elfcpp::R_POWERPC_GOT_TPREL16
:
7127 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7128 case elfcpp::R_POWERPC_GOT16
:
7129 case elfcpp::R_PPC64_GOT16_DS
:
7130 case elfcpp::R_PPC64_TOC16
:
7131 case elfcpp::R_PPC64_TOC16_DS
:
7132 ppc_object
->set_has_small_toc_reloc();
7138 // Process relocations for gc.
7140 template<int size
, bool big_endian
>
7142 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
7143 Symbol_table
* symtab
,
7145 Sized_relobj_file
<size
, big_endian
>* object
,
7146 unsigned int data_shndx
,
7148 const unsigned char* prelocs
,
7150 Output_section
* output_section
,
7151 bool needs_special_offset_handling
,
7152 size_t local_symbol_count
,
7153 const unsigned char* plocal_symbols
)
7155 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
7156 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
7159 Powerpc_relobj
<size
, big_endian
>* ppc_object
7160 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
7162 ppc_object
->set_opd_valid();
7163 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
7165 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
7166 for (p
= ppc_object
->access_from_map()->begin();
7167 p
!= ppc_object
->access_from_map()->end();
7170 Address dst_off
= p
->first
;
7171 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
7172 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
7173 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
7175 Relobj
* src_obj
= s
->first
;
7176 unsigned int src_indx
= s
->second
;
7177 symtab
->gc()->add_reference(src_obj
, src_indx
,
7178 ppc_object
, dst_indx
);
7182 ppc_object
->access_from_map()->clear();
7183 ppc_object
->process_gc_mark(symtab
);
7184 // Don't look at .opd relocs as .opd will reference everything.
7188 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
7197 needs_special_offset_handling
,
7202 // Handle target specific gc actions when adding a gc reference from
7203 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7204 // and DST_OFF. For powerpc64, this adds a referenc to the code
7205 // section of a function descriptor.
7207 template<int size
, bool big_endian
>
7209 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
7210 Symbol_table
* symtab
,
7212 unsigned int src_shndx
,
7214 unsigned int dst_shndx
,
7215 Address dst_off
) const
7217 if (size
!= 64 || dst_obj
->is_dynamic())
7220 Powerpc_relobj
<size
, big_endian
>* ppc_object
7221 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
7222 if (dst_shndx
!= 0 && dst_shndx
== ppc_object
->opd_shndx())
7224 if (ppc_object
->opd_valid())
7226 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
7227 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
7231 // If we haven't run scan_opd_relocs, we must delay
7232 // processing this function descriptor reference.
7233 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
7238 // Add any special sections for this symbol to the gc work list.
7239 // For powerpc64, this adds the code section of a function
7242 template<int size
, bool big_endian
>
7244 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
7245 Symbol_table
* symtab
,
7250 Powerpc_relobj
<size
, big_endian
>* ppc_object
7251 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
7253 unsigned int shndx
= sym
->shndx(&is_ordinary
);
7254 if (is_ordinary
&& shndx
!= 0 && shndx
== ppc_object
->opd_shndx())
7256 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
7257 Address dst_off
= gsym
->value();
7258 if (ppc_object
->opd_valid())
7260 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
7261 symtab
->gc()->worklist().push_back(Section_id(ppc_object
,
7265 ppc_object
->add_gc_mark(dst_off
);
7270 // For a symbol location in .opd, set LOC to the location of the
7273 template<int size
, bool big_endian
>
7275 Target_powerpc
<size
, big_endian
>::do_function_location(
7276 Symbol_location
* loc
) const
7278 if (size
== 64 && loc
->shndx
!= 0)
7280 if (loc
->object
->is_dynamic())
7282 Powerpc_dynobj
<size
, big_endian
>* ppc_object
7283 = static_cast<Powerpc_dynobj
<size
, big_endian
>*>(loc
->object
);
7284 if (loc
->shndx
== ppc_object
->opd_shndx())
7287 Address off
= loc
->offset
- ppc_object
->opd_address();
7288 loc
->shndx
= ppc_object
->get_opd_ent(off
, &dest_off
);
7289 loc
->offset
= dest_off
;
7294 const Powerpc_relobj
<size
, big_endian
>* ppc_object
7295 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(loc
->object
);
7296 if (loc
->shndx
== ppc_object
->opd_shndx())
7299 loc
->shndx
= ppc_object
->get_opd_ent(loc
->offset
, &dest_off
);
7300 loc
->offset
= dest_off
;
7306 // FNOFFSET in section SHNDX in OBJECT is the start of a function
7307 // compiled with -fsplit-stack. The function calls non-split-stack
7308 // code. Change the function to ensure it has enough stack space to
7309 // call some random function.
7311 template<int size
, bool big_endian
>
7313 Target_powerpc
<size
, big_endian
>::do_calls_non_split(
7316 section_offset_type fnoffset
,
7317 section_size_type fnsize
,
7318 const unsigned char* prelocs
,
7320 unsigned char* view
,
7321 section_size_type view_size
,
7323 std::string
* to
) const
7325 // 32-bit not supported.
7329 Target::do_calls_non_split(object
, shndx
, fnoffset
, fnsize
,
7330 prelocs
, reloc_count
, view
, view_size
,
7335 // The function always starts with
7336 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
7337 // addis %r12,%r1,-allocate@ha
7338 // addi %r12,%r12,-allocate@l
7340 // but note that the addis or addi may be replaced with a nop
7342 unsigned char *entry
= view
+ fnoffset
;
7343 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
7345 if ((insn
& 0xffff0000) == addis_2_12
)
7347 /* Skip ELFv2 global entry code. */
7349 insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
7352 unsigned char *pinsn
= entry
;
7354 const uint32_t ld_private_ss
= 0xe80d8fc0;
7355 if (insn
== ld_private_ss
)
7357 int32_t allocate
= 0;
7361 insn
= elfcpp::Swap
<32, big_endian
>::readval(pinsn
);
7362 if ((insn
& 0xffff0000) == addis_12_1
)
7363 allocate
+= (insn
& 0xffff) << 16;
7364 else if ((insn
& 0xffff0000) == addi_12_1
7365 || (insn
& 0xffff0000) == addi_12_12
)
7366 allocate
+= ((insn
& 0xffff) ^ 0x8000) - 0x8000;
7367 else if (insn
!= nop
)
7370 if (insn
== cmpld_7_12_0
&& pinsn
== entry
+ 12)
7372 int extra
= parameters
->options().split_stack_adjust_size();
7374 if (allocate
>= 0 || extra
< 0)
7376 object
->error(_("split-stack stack size overflow at "
7377 "section %u offset %0zx"),
7378 shndx
, static_cast<size_t>(fnoffset
));
7382 insn
= addis_12_1
| (((allocate
+ 0x8000) >> 16) & 0xffff);
7383 if (insn
!= addis_12_1
)
7385 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7387 insn
= addi_12_12
| (allocate
& 0xffff);
7388 if (insn
!= addi_12_12
)
7390 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7396 insn
= addi_12_1
| (allocate
& 0xffff);
7397 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7400 if (pinsn
!= entry
+ 12)
7401 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, nop
);
7409 if (!object
->has_no_split_stack())
7410 object
->error(_("failed to match split-stack sequence at "
7411 "section %u offset %0zx"),
7412 shndx
, static_cast<size_t>(fnoffset
));
7416 // Scan relocations for a section.
7418 template<int size
, bool big_endian
>
7420 Target_powerpc
<size
, big_endian
>::scan_relocs(
7421 Symbol_table
* symtab
,
7423 Sized_relobj_file
<size
, big_endian
>* object
,
7424 unsigned int data_shndx
,
7425 unsigned int sh_type
,
7426 const unsigned char* prelocs
,
7428 Output_section
* output_section
,
7429 bool needs_special_offset_handling
,
7430 size_t local_symbol_count
,
7431 const unsigned char* plocal_symbols
)
7433 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
7434 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
7437 if (sh_type
== elfcpp::SHT_REL
)
7439 gold_error(_("%s: unsupported REL reloc section"),
7440 object
->name().c_str());
7444 gold::scan_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
7453 needs_special_offset_handling
,
7458 // Functor class for processing the global symbol table.
7459 // Removes symbols defined on discarded opd entries.
7461 template<bool big_endian
>
7462 class Global_symbol_visitor_opd
7465 Global_symbol_visitor_opd()
7469 operator()(Sized_symbol
<64>* sym
)
7471 if (sym
->has_symtab_index()
7472 || sym
->source() != Symbol::FROM_OBJECT
7473 || !sym
->in_real_elf())
7476 if (sym
->object()->is_dynamic())
7479 Powerpc_relobj
<64, big_endian
>* symobj
7480 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
7481 if (symobj
->opd_shndx() == 0)
7485 unsigned int shndx
= sym
->shndx(&is_ordinary
);
7486 if (shndx
== symobj
->opd_shndx()
7487 && symobj
->get_opd_discard(sym
->value()))
7489 sym
->set_undefined();
7490 sym
->set_visibility(elfcpp::STV_DEFAULT
);
7491 sym
->set_is_defined_in_discarded_section();
7492 sym
->set_symtab_index(-1U);
7497 template<int size
, bool big_endian
>
7499 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
7501 Symbol_table
* symtab
)
7505 Output_data_save_res
<size
, big_endian
>* savres
7506 = new Output_data_save_res
<size
, big_endian
>(symtab
);
7507 this->savres_section_
= savres
;
7508 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
7509 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
7510 savres
, ORDER_TEXT
, false);
7514 // Sort linker created .got section first (for the header), then input
7515 // sections belonging to files using small model code.
7517 template<bool big_endian
>
7518 class Sort_toc_sections
7522 operator()(const Output_section::Input_section
& is1
,
7523 const Output_section::Input_section
& is2
) const
7525 if (!is1
.is_input_section() && is2
.is_input_section())
7528 = (is1
.is_input_section()
7529 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
7530 ->has_small_toc_reloc()));
7532 = (is2
.is_input_section()
7533 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
7534 ->has_small_toc_reloc()));
7535 return small1
&& !small2
;
7539 // Finalize the sections.
7541 template<int size
, bool big_endian
>
7543 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
7545 const Input_objects
*,
7546 Symbol_table
* symtab
)
7548 if (parameters
->doing_static_link())
7550 // At least some versions of glibc elf-init.o have a strong
7551 // reference to __rela_iplt marker syms. A weak ref would be
7553 if (this->iplt_
!= NULL
)
7555 Reloc_section
* rel
= this->iplt_
->rel_plt();
7556 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
7557 Symbol_table::PREDEFINED
, rel
, 0, 0,
7558 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7559 elfcpp::STV_HIDDEN
, 0, false, true);
7560 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
7561 Symbol_table::PREDEFINED
, rel
, 0, 0,
7562 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7563 elfcpp::STV_HIDDEN
, 0, true, true);
7567 symtab
->define_as_constant("__rela_iplt_start", NULL
,
7568 Symbol_table::PREDEFINED
, 0, 0,
7569 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7570 elfcpp::STV_HIDDEN
, 0, true, false);
7571 symtab
->define_as_constant("__rela_iplt_end", NULL
,
7572 Symbol_table::PREDEFINED
, 0, 0,
7573 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7574 elfcpp::STV_HIDDEN
, 0, true, false);
7580 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
7581 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
7583 if (!parameters
->options().relocatable())
7585 this->define_save_restore_funcs(layout
, symtab
);
7587 // Annoyingly, we need to make these sections now whether or
7588 // not we need them. If we delay until do_relax then we
7589 // need to mess with the relaxation machinery checkpointing.
7590 this->got_section(symtab
, layout
);
7591 this->make_brlt_section(layout
);
7593 if (parameters
->options().toc_sort())
7595 Output_section
* os
= this->got_
->output_section();
7596 if (os
!= NULL
&& os
->input_sections().size() > 1)
7597 std::stable_sort(os
->input_sections().begin(),
7598 os
->input_sections().end(),
7599 Sort_toc_sections
<big_endian
>());
7604 // Fill in some more dynamic tags.
7605 Output_data_dynamic
* odyn
= layout
->dynamic_data();
7608 const Reloc_section
* rel_plt
= (this->plt_
== NULL
7610 : this->plt_
->rel_plt());
7611 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
7612 this->rela_dyn_
, true, size
== 32);
7616 if (this->got_
!= NULL
)
7618 this->got_
->finalize_data_size();
7619 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
7620 this->got_
, this->got_
->g_o_t());
7625 if (this->glink_
!= NULL
)
7627 this->glink_
->finalize_data_size();
7628 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
7630 (this->glink_
->pltresolve_size
7636 // Emit any relocs we saved in an attempt to avoid generating COPY
7638 if (this->copy_relocs_
.any_saved_relocs())
7639 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
7642 // Emit any saved relocs, and mark toc entries using any of these
7643 // relocs as not optimizable.
7645 template<int sh_type
, int size
, bool big_endian
>
7647 Powerpc_copy_relocs
<sh_type
, size
, big_endian
>::emit(
7648 Output_data_reloc
<sh_type
, true, size
, big_endian
>* reloc_section
)
7651 && parameters
->options().toc_optimize())
7653 for (typename Copy_relocs
<sh_type
, size
, big_endian
>::
7654 Copy_reloc_entries::iterator p
= this->entries_
.begin();
7655 p
!= this->entries_
.end();
7658 typename Copy_relocs
<sh_type
, size
, big_endian
>::Copy_reloc_entry
&
7661 // If the symbol is no longer defined in a dynamic object,
7662 // then we emitted a COPY relocation. If it is still
7663 // dynamic then we'll need dynamic relocations and thus
7664 // can't optimize toc entries.
7665 if (entry
.sym_
->is_from_dynobj())
7667 Powerpc_relobj
<size
, big_endian
>* ppc_object
7668 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(entry
.relobj_
);
7669 if (entry
.shndx_
== ppc_object
->toc_shndx())
7670 ppc_object
->set_no_toc_opt(entry
.address_
);
7675 Copy_relocs
<sh_type
, size
, big_endian
>::emit(reloc_section
);
7678 // Return the value to use for a branch relocation.
7680 template<int size
, bool big_endian
>
7682 Target_powerpc
<size
, big_endian
>::symval_for_branch(
7683 const Symbol_table
* symtab
,
7684 const Sized_symbol
<size
>* gsym
,
7685 Powerpc_relobj
<size
, big_endian
>* object
,
7687 unsigned int *dest_shndx
)
7689 if (size
== 32 || this->abiversion() >= 2)
7693 // If the symbol is defined in an opd section, ie. is a function
7694 // descriptor, use the function descriptor code entry address
7695 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
7697 && (gsym
->source() != Symbol::FROM_OBJECT
7698 || gsym
->object()->is_dynamic()))
7701 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7702 unsigned int shndx
= symobj
->opd_shndx();
7705 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
7706 if (opd_addr
== invalid_address
)
7708 opd_addr
+= symobj
->output_section_address(shndx
);
7709 if (*value
>= opd_addr
&& *value
< opd_addr
+ symobj
->section_size(shndx
))
7712 *dest_shndx
= symobj
->get_opd_ent(*value
- opd_addr
, &sec_off
);
7713 if (symtab
->is_section_folded(symobj
, *dest_shndx
))
7716 = symtab
->icf()->get_folded_section(symobj
, *dest_shndx
);
7717 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(folded
.first
);
7718 *dest_shndx
= folded
.second
;
7720 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
7721 if (sec_addr
== invalid_address
)
7724 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
7725 *value
= sec_addr
+ sec_off
;
7730 // Perform a relocation.
7732 template<int size
, bool big_endian
>
7734 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
7735 const Relocate_info
<size
, big_endian
>* relinfo
,
7737 Target_powerpc
* target
,
7740 const unsigned char* preloc
,
7741 const Sized_symbol
<size
>* gsym
,
7742 const Symbol_value
<size
>* psymval
,
7743 unsigned char* view
,
7745 section_size_type view_size
)
7750 const elfcpp::Rela
<size
, big_endian
> rela(preloc
);
7751 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
7752 switch (this->maybe_skip_tls_get_addr_call(r_type
, gsym
))
7754 case Track_tls::NOT_EXPECTED
:
7755 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7756 _("__tls_get_addr call lacks marker reloc"));
7758 case Track_tls::EXPECTED
:
7759 // We have already complained.
7761 case Track_tls::SKIP
:
7763 case Track_tls::NORMAL
:
7767 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
7768 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
7769 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
7770 // Offset from start of insn to d-field reloc.
7771 const int d_offset
= big_endian
? 2 : 0;
7773 Powerpc_relobj
<size
, big_endian
>* const object
7774 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
7776 bool has_stub_value
= false;
7777 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7779 ? gsym
->use_plt_offset(Scan::get_reference_flags(r_type
, target
))
7780 : object
->local_has_plt_offset(r_sym
))
7781 && (!psymval
->is_ifunc_symbol()
7782 || Scan::reloc_needs_plt_for_ifunc(target
, object
, r_type
, false)))
7786 && target
->abiversion() >= 2
7787 && !parameters
->options().output_is_position_independent()
7788 && !is_branch_reloc(r_type
))
7790 Address off
= target
->glink_section()->find_global_entry(gsym
);
7791 if (off
!= invalid_address
)
7793 value
= target
->glink_section()->global_entry_address() + off
;
7794 has_stub_value
= true;
7799 Stub_table
<size
, big_endian
>* stub_table
7800 = object
->stub_table(relinfo
->data_shndx
);
7801 if (stub_table
== NULL
)
7803 // This is a ref from a data section to an ifunc symbol.
7804 if (target
->stub_tables().size() != 0)
7805 stub_table
= target
->stub_tables()[0];
7807 if (stub_table
!= NULL
)
7811 off
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
7812 rela
.get_r_addend());
7814 off
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
7815 rela
.get_r_addend());
7816 if (off
!= invalid_address
)
7818 value
= stub_table
->stub_address() + off
;
7819 has_stub_value
= true;
7823 // We don't care too much about bogus debug references to
7824 // non-local functions, but otherwise there had better be a plt
7825 // call stub or global entry stub as appropriate.
7826 gold_assert(has_stub_value
|| !(os
->flags() & elfcpp::SHF_ALLOC
));
7829 if (r_type
== elfcpp::R_POWERPC_GOT16
7830 || r_type
== elfcpp::R_POWERPC_GOT16_LO
7831 || r_type
== elfcpp::R_POWERPC_GOT16_HI
7832 || r_type
== elfcpp::R_POWERPC_GOT16_HA
7833 || r_type
== elfcpp::R_PPC64_GOT16_DS
7834 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
7838 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
7839 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
7843 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
7844 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
7846 value
-= target
->got_section()->got_base_offset(object
);
7848 else if (r_type
== elfcpp::R_PPC64_TOC
)
7850 value
= (target
->got_section()->output_section()->address()
7851 + object
->toc_base_offset());
7853 else if (gsym
!= NULL
7854 && (r_type
== elfcpp::R_POWERPC_REL24
7855 || r_type
== elfcpp::R_PPC_PLTREL24
)
7860 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
7861 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
7862 bool can_plt_call
= false;
7863 if (rela
.get_r_offset() + 8 <= view_size
)
7865 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
7866 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
7869 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
7871 elfcpp::Swap
<32, big_endian
>::
7872 writeval(wv
+ 1, ld_2_1
+ target
->stk_toc());
7873 can_plt_call
= true;
7878 // If we don't have a branch and link followed by a nop,
7879 // we can't go via the plt because there is no place to
7880 // put a toc restoring instruction.
7881 // Unless we know we won't be returning.
7882 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
7883 can_plt_call
= true;
7887 // g++ as of 20130507 emits self-calls without a
7888 // following nop. This is arguably wrong since we have
7889 // conflicting information. On the one hand a global
7890 // symbol and on the other a local call sequence, but
7891 // don't error for this special case.
7892 // It isn't possible to cheaply verify we have exactly
7893 // such a call. Allow all calls to the same section.
7895 Address code
= value
;
7896 if (gsym
->source() == Symbol::FROM_OBJECT
7897 && gsym
->object() == object
)
7899 unsigned int dest_shndx
= 0;
7900 if (target
->abiversion() < 2)
7902 Address addend
= rela
.get_r_addend();
7903 code
= psymval
->value(object
, addend
);
7904 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
7905 &code
, &dest_shndx
);
7908 if (dest_shndx
== 0)
7909 dest_shndx
= gsym
->shndx(&is_ordinary
);
7910 ok
= dest_shndx
== relinfo
->data_shndx
;
7914 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7915 _("call lacks nop, can't restore toc; "
7916 "recompile with -fPIC"));
7922 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7923 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
7924 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
7925 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
7927 // First instruction of a global dynamic sequence, arg setup insn.
7928 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7929 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7930 enum Got_type got_type
= GOT_TYPE_STANDARD
;
7931 if (tls_type
== tls::TLSOPT_NONE
)
7932 got_type
= GOT_TYPE_TLSGD
;
7933 else if (tls_type
== tls::TLSOPT_TO_IE
)
7934 got_type
= GOT_TYPE_TPREL
;
7935 if (got_type
!= GOT_TYPE_STANDARD
)
7939 gold_assert(gsym
->has_got_offset(got_type
));
7940 value
= gsym
->got_offset(got_type
);
7944 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
7945 value
= object
->local_got_offset(r_sym
, got_type
);
7947 value
-= target
->got_section()->got_base_offset(object
);
7949 if (tls_type
== tls::TLSOPT_TO_IE
)
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 << 16); // extract rt,ra from addi
7958 insn
|= 32 << 26; // lwz
7960 insn
|= 58 << 26; // ld
7961 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7963 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
7964 - elfcpp::R_POWERPC_GOT_TLSGD16
);
7966 else if (tls_type
== tls::TLSOPT_TO_LE
)
7968 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7969 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
7971 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
7972 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7973 insn
&= (1 << 26) - (1 << 21); // extract rt
7978 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7979 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7980 value
= psymval
->value(object
, rela
.get_r_addend());
7984 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
7986 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7987 r_type
= elfcpp::R_POWERPC_NONE
;
7991 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7992 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
7993 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
7994 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
7996 // First instruction of a local dynamic sequence, arg setup insn.
7997 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7998 if (tls_type
== tls::TLSOPT_NONE
)
8000 value
= target
->tlsld_got_offset();
8001 value
-= target
->got_section()->got_base_offset(object
);
8005 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
8006 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
8007 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
8009 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8010 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8011 insn
&= (1 << 26) - (1 << 21); // extract rt
8016 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8017 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8022 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8024 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8025 r_type
= elfcpp::R_POWERPC_NONE
;
8029 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
8030 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
8031 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
8032 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
8034 // Accesses relative to a local dynamic sequence address,
8035 // no optimisation here.
8038 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
8039 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
8043 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
8044 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
8046 value
-= target
->got_section()->got_base_offset(object
);
8048 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8049 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
8050 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
8051 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
8053 // First instruction of initial exec sequence.
8054 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8055 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
8056 if (tls_type
== tls::TLSOPT_NONE
)
8060 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
8061 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
8065 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
8066 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
8068 value
-= target
->got_section()->got_base_offset(object
);
8072 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
8073 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8074 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
8076 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8077 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8078 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
8083 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8084 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8085 value
= psymval
->value(object
, rela
.get_r_addend());
8089 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8091 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8092 r_type
= elfcpp::R_POWERPC_NONE
;
8096 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
8097 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
8099 // Second instruction of a global dynamic sequence,
8100 // the __tls_get_addr call
8101 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
8102 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8103 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
8104 if (tls_type
!= tls::TLSOPT_NONE
)
8106 if (tls_type
== tls::TLSOPT_TO_IE
)
8108 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8109 Insn insn
= add_3_3_13
;
8112 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8113 r_type
= elfcpp::R_POWERPC_NONE
;
8117 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8118 Insn insn
= addi_3_3
;
8119 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8120 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8122 value
= psymval
->value(object
, rela
.get_r_addend());
8124 this->skip_next_tls_get_addr_call();
8127 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
8128 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
8130 // Second instruction of a local dynamic sequence,
8131 // the __tls_get_addr call
8132 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
8133 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
8134 if (tls_type
== tls::TLSOPT_TO_LE
)
8136 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8137 Insn insn
= addi_3_3
;
8138 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8139 this->skip_next_tls_get_addr_call();
8140 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8145 else if (r_type
== elfcpp::R_POWERPC_TLS
)
8147 // Second instruction of an initial exec sequence
8148 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8149 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
8150 if (tls_type
== tls::TLSOPT_TO_LE
)
8152 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8153 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8154 unsigned int reg
= size
== 32 ? 2 : 13;
8155 insn
= at_tls_transform(insn
, reg
);
8156 gold_assert(insn
!= 0);
8157 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8158 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8160 value
= psymval
->value(object
, rela
.get_r_addend());
8163 else if (!has_stub_value
)
8166 if (!(size
== 32 && r_type
== elfcpp::R_PPC_PLTREL24
))
8167 addend
= rela
.get_r_addend();
8168 value
= psymval
->value(object
, addend
);
8169 if (size
== 64 && is_branch_reloc(r_type
))
8171 if (target
->abiversion() >= 2)
8174 value
+= object
->ppc64_local_entry_offset(gsym
);
8176 value
+= object
->ppc64_local_entry_offset(r_sym
);
8180 unsigned int dest_shndx
;
8181 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
8182 &value
, &dest_shndx
);
8185 Address max_branch_offset
= max_branch_delta(r_type
);
8186 if (max_branch_offset
!= 0
8187 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
8189 Stub_table
<size
, big_endian
>* stub_table
8190 = object
->stub_table(relinfo
->data_shndx
);
8191 if (stub_table
!= NULL
)
8193 Address off
= stub_table
->find_long_branch_entry(object
, value
);
8194 if (off
!= invalid_address
)
8196 value
= (stub_table
->stub_address() + stub_table
->plt_size()
8198 has_stub_value
= true;
8206 case elfcpp::R_PPC64_REL64
:
8207 case elfcpp::R_POWERPC_REL32
:
8208 case elfcpp::R_POWERPC_REL24
:
8209 case elfcpp::R_PPC_PLTREL24
:
8210 case elfcpp::R_PPC_LOCAL24PC
:
8211 case elfcpp::R_POWERPC_REL16
:
8212 case elfcpp::R_POWERPC_REL16_LO
:
8213 case elfcpp::R_POWERPC_REL16_HI
:
8214 case elfcpp::R_POWERPC_REL16_HA
:
8215 case elfcpp::R_POWERPC_REL16DX_HA
:
8216 case elfcpp::R_POWERPC_REL14
:
8217 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8218 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8222 case elfcpp::R_PPC64_TOC16
:
8223 case elfcpp::R_PPC64_TOC16_LO
:
8224 case elfcpp::R_PPC64_TOC16_HI
:
8225 case elfcpp::R_PPC64_TOC16_HA
:
8226 case elfcpp::R_PPC64_TOC16_DS
:
8227 case elfcpp::R_PPC64_TOC16_LO_DS
:
8228 // Subtract the TOC base address.
8229 value
-= (target
->got_section()->output_section()->address()
8230 + object
->toc_base_offset());
8233 case elfcpp::R_POWERPC_SECTOFF
:
8234 case elfcpp::R_POWERPC_SECTOFF_LO
:
8235 case elfcpp::R_POWERPC_SECTOFF_HI
:
8236 case elfcpp::R_POWERPC_SECTOFF_HA
:
8237 case elfcpp::R_PPC64_SECTOFF_DS
:
8238 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
8240 value
-= os
->address();
8243 case elfcpp::R_PPC64_TPREL16_DS
:
8244 case elfcpp::R_PPC64_TPREL16_LO_DS
:
8245 case elfcpp::R_PPC64_TPREL16_HIGH
:
8246 case elfcpp::R_PPC64_TPREL16_HIGHA
:
8248 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
8251 case elfcpp::R_POWERPC_TPREL16
:
8252 case elfcpp::R_POWERPC_TPREL16_LO
:
8253 case elfcpp::R_POWERPC_TPREL16_HI
:
8254 case elfcpp::R_POWERPC_TPREL16_HA
:
8255 case elfcpp::R_POWERPC_TPREL
:
8256 case elfcpp::R_PPC64_TPREL16_HIGHER
:
8257 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
8258 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
8259 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
8260 // tls symbol values are relative to tls_segment()->vaddr()
8264 case elfcpp::R_PPC64_DTPREL16_DS
:
8265 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
8266 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
8267 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
8268 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
8269 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
8271 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
8272 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
8275 case elfcpp::R_POWERPC_DTPREL16
:
8276 case elfcpp::R_POWERPC_DTPREL16_LO
:
8277 case elfcpp::R_POWERPC_DTPREL16_HI
:
8278 case elfcpp::R_POWERPC_DTPREL16_HA
:
8279 case elfcpp::R_POWERPC_DTPREL
:
8280 case elfcpp::R_PPC64_DTPREL16_HIGH
:
8281 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
8282 // tls symbol values are relative to tls_segment()->vaddr()
8283 value
-= dtp_offset
;
8286 case elfcpp::R_PPC64_ADDR64_LOCAL
:
8288 value
+= object
->ppc64_local_entry_offset(gsym
);
8290 value
+= object
->ppc64_local_entry_offset(r_sym
);
8297 Insn branch_bit
= 0;
8300 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
8301 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8302 branch_bit
= 1 << 21;
8304 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
8305 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8307 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8308 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8311 if (this->is_isa_v2
)
8313 // Set 'a' bit. This is 0b00010 in BO field for branch
8314 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
8315 // for branch on CTR insns (BO == 1a00t or 1a01t).
8316 if ((insn
& (0x14 << 21)) == (0x04 << 21))
8318 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
8325 // Invert 'y' bit if not the default.
8326 if (static_cast<Signed_address
>(value
) < 0)
8329 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8344 // Multi-instruction sequences that access the GOT/TOC can
8345 // be optimized, eg.
8346 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
8347 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
8349 // addis ra,r2,0; addi rb,ra,x@toc@l;
8350 // to nop; addi rb,r2,x@toc;
8351 // FIXME: the @got sequence shown above is not yet
8352 // optimized. Note that gcc as of 2017-01-07 doesn't use
8353 // the ELF @got relocs except for TLS, instead using the
8354 // PowerOpen variant of a compiler managed GOT (called TOC).
8355 // The PowerOpen TOC sequence equivalent to the first
8356 // example is optimized.
8357 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
8358 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
8359 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
8360 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
8361 case elfcpp::R_POWERPC_GOT16_HA
:
8362 case elfcpp::R_PPC64_TOC16_HA
:
8363 if (parameters
->options().toc_optimize())
8365 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8366 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8367 if (r_type
== elfcpp::R_PPC64_TOC16_HA
8368 && object
->make_toc_relative(target
, &value
))
8370 gold_assert((insn
& ((0x3f << 26) | 0x1f << 16))
8371 == ((15u << 26) | (2 << 16)));
8373 if (((insn
& ((0x3f << 26) | 0x1f << 16))
8374 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8375 && value
+ 0x8000 < 0x10000)
8377 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
8383 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
8384 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
8385 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
8386 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
8387 case elfcpp::R_POWERPC_GOT16_LO
:
8388 case elfcpp::R_PPC64_GOT16_LO_DS
:
8389 case elfcpp::R_PPC64_TOC16_LO
:
8390 case elfcpp::R_PPC64_TOC16_LO_DS
:
8391 if (parameters
->options().toc_optimize())
8393 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8394 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8395 bool changed
= false;
8396 if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
8397 && object
->make_toc_relative(target
, &value
))
8399 gold_assert ((insn
& (0x3f << 26)) == 58u << 26 /* ld */);
8400 insn
^= (14u << 26) ^ (58u << 26);
8401 r_type
= elfcpp::R_PPC64_TOC16_LO
;
8404 if (ok_lo_toc_insn(insn
, r_type
)
8405 && value
+ 0x8000 < 0x10000)
8407 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
8409 // Transform addic to addi when we change reg.
8410 insn
&= ~((0x3f << 26) | (0x1f << 16));
8411 insn
|= (14u << 26) | (2 << 16);
8415 insn
&= ~(0x1f << 16);
8421 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8425 case elfcpp::R_PPC64_ENTRY
:
8426 value
= (target
->got_section()->output_section()->address()
8427 + object
->toc_base_offset());
8428 if (value
+ 0x80008000 <= 0xffffffff
8429 && !parameters
->options().output_is_position_independent())
8431 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8432 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8433 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
8435 if ((insn1
& ~0xfffc) == ld_2_12
8436 && insn2
== add_2_2_12
)
8438 insn1
= lis_2
+ ha(value
);
8439 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
8440 insn2
= addi_2_2
+ l(value
);
8441 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
8448 if (value
+ 0x80008000 <= 0xffffffff)
8450 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8451 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8452 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
8454 if ((insn1
& ~0xfffc) == ld_2_12
8455 && insn2
== add_2_2_12
)
8457 insn1
= addis_2_12
+ ha(value
);
8458 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
8459 insn2
= addi_2_2
+ l(value
);
8460 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
8467 case elfcpp::R_POWERPC_REL16_LO
:
8468 // If we are generating a non-PIC executable, edit
8469 // 0: addis 2,12,.TOC.-0b@ha
8470 // addi 2,2,.TOC.-0b@l
8471 // used by ELFv2 global entry points to set up r2, to
8474 // if .TOC. is in range. */
8475 if (value
+ address
- 4 + 0x80008000 <= 0xffffffff
8478 && target
->abiversion() >= 2
8479 && !parameters
->options().output_is_position_independent()
8480 && rela
.get_r_addend() == d_offset
+ 4
8482 && strcmp(gsym
->name(), ".TOC.") == 0)
8484 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
8485 Reltype
prev_rela(preloc
- reloc_size
);
8486 if ((prev_rela
.get_r_info()
8487 == elfcpp::elf_r_info
<size
>(r_sym
,
8488 elfcpp::R_POWERPC_REL16_HA
))
8489 && prev_rela
.get_r_offset() + 4 == rela
.get_r_offset()
8490 && prev_rela
.get_r_addend() + 4 == rela
.get_r_addend())
8492 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8493 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
- 1);
8494 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8496 if ((insn1
& 0xffff0000) == addis_2_12
8497 && (insn2
& 0xffff0000) == addi_2_2
)
8499 insn1
= lis_2
+ ha(value
+ address
- 4);
8500 elfcpp::Swap
<32, big_endian
>::writeval(iview
- 1, insn1
);
8501 insn2
= addi_2_2
+ l(value
+ address
- 4);
8502 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn2
);
8505 relinfo
->rr
->set_strategy(relnum
- 1,
8506 Relocatable_relocs::RELOC_SPECIAL
);
8507 relinfo
->rr
->set_strategy(relnum
,
8508 Relocatable_relocs::RELOC_SPECIAL
);
8518 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
8519 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->data_shdr
);
8522 case elfcpp::R_POWERPC_ADDR32
:
8523 case elfcpp::R_POWERPC_UADDR32
:
8525 overflow
= Reloc::CHECK_BITFIELD
;
8528 case elfcpp::R_POWERPC_REL32
:
8529 case elfcpp::R_POWERPC_REL16DX_HA
:
8531 overflow
= Reloc::CHECK_SIGNED
;
8534 case elfcpp::R_POWERPC_UADDR16
:
8535 overflow
= Reloc::CHECK_BITFIELD
;
8538 case elfcpp::R_POWERPC_ADDR16
:
8539 // We really should have three separate relocations,
8540 // one for 16-bit data, one for insns with 16-bit signed fields,
8541 // and one for insns with 16-bit unsigned fields.
8542 overflow
= Reloc::CHECK_BITFIELD
;
8543 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
8544 overflow
= Reloc::CHECK_LOW_INSN
;
8547 case elfcpp::R_POWERPC_ADDR16_HI
:
8548 case elfcpp::R_POWERPC_ADDR16_HA
:
8549 case elfcpp::R_POWERPC_GOT16_HI
:
8550 case elfcpp::R_POWERPC_GOT16_HA
:
8551 case elfcpp::R_POWERPC_PLT16_HI
:
8552 case elfcpp::R_POWERPC_PLT16_HA
:
8553 case elfcpp::R_POWERPC_SECTOFF_HI
:
8554 case elfcpp::R_POWERPC_SECTOFF_HA
:
8555 case elfcpp::R_PPC64_TOC16_HI
:
8556 case elfcpp::R_PPC64_TOC16_HA
:
8557 case elfcpp::R_PPC64_PLTGOT16_HI
:
8558 case elfcpp::R_PPC64_PLTGOT16_HA
:
8559 case elfcpp::R_POWERPC_TPREL16_HI
:
8560 case elfcpp::R_POWERPC_TPREL16_HA
:
8561 case elfcpp::R_POWERPC_DTPREL16_HI
:
8562 case elfcpp::R_POWERPC_DTPREL16_HA
:
8563 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
8564 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
8565 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
8566 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
8567 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
8568 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
8569 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
8570 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
8571 case elfcpp::R_POWERPC_REL16_HI
:
8572 case elfcpp::R_POWERPC_REL16_HA
:
8574 overflow
= Reloc::CHECK_HIGH_INSN
;
8577 case elfcpp::R_POWERPC_REL16
:
8578 case elfcpp::R_PPC64_TOC16
:
8579 case elfcpp::R_POWERPC_GOT16
:
8580 case elfcpp::R_POWERPC_SECTOFF
:
8581 case elfcpp::R_POWERPC_TPREL16
:
8582 case elfcpp::R_POWERPC_DTPREL16
:
8583 case elfcpp::R_POWERPC_GOT_TLSGD16
:
8584 case elfcpp::R_POWERPC_GOT_TLSLD16
:
8585 case elfcpp::R_POWERPC_GOT_TPREL16
:
8586 case elfcpp::R_POWERPC_GOT_DTPREL16
:
8587 overflow
= Reloc::CHECK_LOW_INSN
;
8590 case elfcpp::R_POWERPC_ADDR24
:
8591 case elfcpp::R_POWERPC_ADDR14
:
8592 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
8593 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
8594 case elfcpp::R_PPC64_ADDR16_DS
:
8595 case elfcpp::R_POWERPC_REL24
:
8596 case elfcpp::R_PPC_PLTREL24
:
8597 case elfcpp::R_PPC_LOCAL24PC
:
8598 case elfcpp::R_PPC64_TPREL16_DS
:
8599 case elfcpp::R_PPC64_DTPREL16_DS
:
8600 case elfcpp::R_PPC64_TOC16_DS
:
8601 case elfcpp::R_PPC64_GOT16_DS
:
8602 case elfcpp::R_PPC64_SECTOFF_DS
:
8603 case elfcpp::R_POWERPC_REL14
:
8604 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8605 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8606 overflow
= Reloc::CHECK_SIGNED
;
8610 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8613 if (overflow
== Reloc::CHECK_LOW_INSN
8614 || overflow
== Reloc::CHECK_HIGH_INSN
)
8616 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8618 if ((insn
& (0x3f << 26)) == 10u << 26 /* cmpli */)
8619 overflow
= Reloc::CHECK_BITFIELD
;
8620 else if (overflow
== Reloc::CHECK_LOW_INSN
8621 ? ((insn
& (0x3f << 26)) == 28u << 26 /* andi */
8622 || (insn
& (0x3f << 26)) == 24u << 26 /* ori */
8623 || (insn
& (0x3f << 26)) == 26u << 26 /* xori */)
8624 : ((insn
& (0x3f << 26)) == 29u << 26 /* andis */
8625 || (insn
& (0x3f << 26)) == 25u << 26 /* oris */
8626 || (insn
& (0x3f << 26)) == 27u << 26 /* xoris */))
8627 overflow
= Reloc::CHECK_UNSIGNED
;
8629 overflow
= Reloc::CHECK_SIGNED
;
8632 bool maybe_dq_reloc
= false;
8633 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
8634 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
8637 case elfcpp::R_POWERPC_NONE
:
8638 case elfcpp::R_POWERPC_TLS
:
8639 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
8640 case elfcpp::R_POWERPC_GNU_VTENTRY
:
8643 case elfcpp::R_PPC64_ADDR64
:
8644 case elfcpp::R_PPC64_REL64
:
8645 case elfcpp::R_PPC64_TOC
:
8646 case elfcpp::R_PPC64_ADDR64_LOCAL
:
8647 Reloc::addr64(view
, value
);
8650 case elfcpp::R_POWERPC_TPREL
:
8651 case elfcpp::R_POWERPC_DTPREL
:
8653 Reloc::addr64(view
, value
);
8655 status
= Reloc::addr32(view
, value
, overflow
);
8658 case elfcpp::R_PPC64_UADDR64
:
8659 Reloc::addr64_u(view
, value
);
8662 case elfcpp::R_POWERPC_ADDR32
:
8663 status
= Reloc::addr32(view
, value
, overflow
);
8666 case elfcpp::R_POWERPC_REL32
:
8667 case elfcpp::R_POWERPC_UADDR32
:
8668 status
= Reloc::addr32_u(view
, value
, overflow
);
8671 case elfcpp::R_POWERPC_ADDR24
:
8672 case elfcpp::R_POWERPC_REL24
:
8673 case elfcpp::R_PPC_PLTREL24
:
8674 case elfcpp::R_PPC_LOCAL24PC
:
8675 status
= Reloc::addr24(view
, value
, overflow
);
8678 case elfcpp::R_POWERPC_GOT_DTPREL16
:
8679 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
8680 case elfcpp::R_POWERPC_GOT_TPREL16
:
8681 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
8684 // On ppc64 these are all ds form
8685 maybe_dq_reloc
= true;
8689 case elfcpp::R_POWERPC_ADDR16
:
8690 case elfcpp::R_POWERPC_REL16
:
8691 case elfcpp::R_PPC64_TOC16
:
8692 case elfcpp::R_POWERPC_GOT16
:
8693 case elfcpp::R_POWERPC_SECTOFF
:
8694 case elfcpp::R_POWERPC_TPREL16
:
8695 case elfcpp::R_POWERPC_DTPREL16
:
8696 case elfcpp::R_POWERPC_GOT_TLSGD16
:
8697 case elfcpp::R_POWERPC_GOT_TLSLD16
:
8698 case elfcpp::R_POWERPC_ADDR16_LO
:
8699 case elfcpp::R_POWERPC_REL16_LO
:
8700 case elfcpp::R_PPC64_TOC16_LO
:
8701 case elfcpp::R_POWERPC_GOT16_LO
:
8702 case elfcpp::R_POWERPC_SECTOFF_LO
:
8703 case elfcpp::R_POWERPC_TPREL16_LO
:
8704 case elfcpp::R_POWERPC_DTPREL16_LO
:
8705 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
8706 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
8708 status
= Reloc::addr16(view
, value
, overflow
);
8710 maybe_dq_reloc
= true;
8713 case elfcpp::R_POWERPC_UADDR16
:
8714 status
= Reloc::addr16_u(view
, value
, overflow
);
8717 case elfcpp::R_PPC64_ADDR16_HIGH
:
8718 case elfcpp::R_PPC64_TPREL16_HIGH
:
8719 case elfcpp::R_PPC64_DTPREL16_HIGH
:
8721 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
8724 case elfcpp::R_POWERPC_ADDR16_HI
:
8725 case elfcpp::R_POWERPC_REL16_HI
:
8726 case elfcpp::R_PPC64_TOC16_HI
:
8727 case elfcpp::R_POWERPC_GOT16_HI
:
8728 case elfcpp::R_POWERPC_SECTOFF_HI
:
8729 case elfcpp::R_POWERPC_TPREL16_HI
:
8730 case elfcpp::R_POWERPC_DTPREL16_HI
:
8731 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
8732 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
8733 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
8734 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
8735 Reloc::addr16_hi(view
, value
);
8738 case elfcpp::R_PPC64_ADDR16_HIGHA
:
8739 case elfcpp::R_PPC64_TPREL16_HIGHA
:
8740 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
8742 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
8745 case elfcpp::R_POWERPC_ADDR16_HA
:
8746 case elfcpp::R_POWERPC_REL16_HA
:
8747 case elfcpp::R_PPC64_TOC16_HA
:
8748 case elfcpp::R_POWERPC_GOT16_HA
:
8749 case elfcpp::R_POWERPC_SECTOFF_HA
:
8750 case elfcpp::R_POWERPC_TPREL16_HA
:
8751 case elfcpp::R_POWERPC_DTPREL16_HA
:
8752 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
8753 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
8754 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
8755 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
8756 Reloc::addr16_ha(view
, value
);
8759 case elfcpp::R_POWERPC_REL16DX_HA
:
8760 status
= Reloc::addr16dx_ha(view
, value
, overflow
);
8763 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
8765 // R_PPC_EMB_NADDR16_LO
8768 case elfcpp::R_PPC64_ADDR16_HIGHER
:
8769 case elfcpp::R_PPC64_TPREL16_HIGHER
:
8770 Reloc::addr16_hi2(view
, value
);
8773 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
8775 // R_PPC_EMB_NADDR16_HI
8778 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
8779 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
8780 Reloc::addr16_ha2(view
, value
);
8783 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
8785 // R_PPC_EMB_NADDR16_HA
8788 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
8789 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
8790 Reloc::addr16_hi3(view
, value
);
8793 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
8798 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
8799 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
8800 Reloc::addr16_ha3(view
, value
);
8803 case elfcpp::R_PPC64_DTPREL16_DS
:
8804 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
8806 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
8809 case elfcpp::R_PPC64_TPREL16_DS
:
8810 case elfcpp::R_PPC64_TPREL16_LO_DS
:
8812 // R_PPC_TLSGD, R_PPC_TLSLD
8815 case elfcpp::R_PPC64_ADDR16_DS
:
8816 case elfcpp::R_PPC64_ADDR16_LO_DS
:
8817 case elfcpp::R_PPC64_TOC16_DS
:
8818 case elfcpp::R_PPC64_TOC16_LO_DS
:
8819 case elfcpp::R_PPC64_GOT16_DS
:
8820 case elfcpp::R_PPC64_GOT16_LO_DS
:
8821 case elfcpp::R_PPC64_SECTOFF_DS
:
8822 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
8823 maybe_dq_reloc
= true;
8826 case elfcpp::R_POWERPC_ADDR14
:
8827 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
8828 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
8829 case elfcpp::R_POWERPC_REL14
:
8830 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8831 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8832 status
= Reloc::addr14(view
, value
, overflow
);
8835 case elfcpp::R_POWERPC_COPY
:
8836 case elfcpp::R_POWERPC_GLOB_DAT
:
8837 case elfcpp::R_POWERPC_JMP_SLOT
:
8838 case elfcpp::R_POWERPC_RELATIVE
:
8839 case elfcpp::R_POWERPC_DTPMOD
:
8840 case elfcpp::R_PPC64_JMP_IREL
:
8841 case elfcpp::R_POWERPC_IRELATIVE
:
8842 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8843 _("unexpected reloc %u in object file"),
8847 case elfcpp::R_PPC_EMB_SDA21
:
8852 // R_PPC64_TOCSAVE. For the time being this can be ignored.
8856 case elfcpp::R_PPC_EMB_SDA2I16
:
8857 case elfcpp::R_PPC_EMB_SDA2REL
:
8860 // R_PPC64_TLSGD, R_PPC64_TLSLD
8863 case elfcpp::R_POWERPC_PLT32
:
8864 case elfcpp::R_POWERPC_PLTREL32
:
8865 case elfcpp::R_POWERPC_PLT16_LO
:
8866 case elfcpp::R_POWERPC_PLT16_HI
:
8867 case elfcpp::R_POWERPC_PLT16_HA
:
8868 case elfcpp::R_PPC_SDAREL16
:
8869 case elfcpp::R_POWERPC_ADDR30
:
8870 case elfcpp::R_PPC64_PLT64
:
8871 case elfcpp::R_PPC64_PLTREL64
:
8872 case elfcpp::R_PPC64_PLTGOT16
:
8873 case elfcpp::R_PPC64_PLTGOT16_LO
:
8874 case elfcpp::R_PPC64_PLTGOT16_HI
:
8875 case elfcpp::R_PPC64_PLTGOT16_HA
:
8876 case elfcpp::R_PPC64_PLT16_LO_DS
:
8877 case elfcpp::R_PPC64_PLTGOT16_DS
:
8878 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
8879 case elfcpp::R_PPC_EMB_RELSDA
:
8880 case elfcpp::R_PPC_TOC16
:
8883 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8884 _("unsupported reloc %u"),
8892 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8894 if ((insn
& (0x3f << 26)) == 56u << 26 /* lq */
8895 || ((insn
& (0x3f << 26)) == (61u << 26) /* lxv, stxv */
8896 && (insn
& 3) == 1))
8897 status
= Reloc::addr16_dq(view
, value
, overflow
);
8899 || (insn
& (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
8900 || (insn
& (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
8901 || (insn
& (0x3f << 26)) == 57u << 26 /* lfdp */
8902 || (insn
& (0x3f << 26)) == 61u << 26 /* stfdp */)
8903 status
= Reloc::addr16_ds(view
, value
, overflow
);
8905 status
= Reloc::addr16(view
, value
, overflow
);
8908 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
8911 && gsym
->is_undefined()
8912 && is_branch_reloc(r_type
))))
8914 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8915 _("relocation overflow"));
8917 gold_info(_("try relinking with a smaller --stub-group-size"));
8923 // Relocate section data.
8925 template<int size
, bool big_endian
>
8927 Target_powerpc
<size
, big_endian
>::relocate_section(
8928 const Relocate_info
<size
, big_endian
>* relinfo
,
8929 unsigned int sh_type
,
8930 const unsigned char* prelocs
,
8932 Output_section
* output_section
,
8933 bool needs_special_offset_handling
,
8934 unsigned char* view
,
8936 section_size_type view_size
,
8937 const Reloc_symbol_changes
* reloc_symbol_changes
)
8939 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
8940 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
8941 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
8942 Powerpc_comdat_behavior
;
8943 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
8946 gold_assert(sh_type
== elfcpp::SHT_RELA
);
8948 gold::relocate_section
<size
, big_endian
, Powerpc
, Powerpc_relocate
,
8949 Powerpc_comdat_behavior
, Classify_reloc
>(
8955 needs_special_offset_handling
,
8959 reloc_symbol_changes
);
8962 template<int size
, bool big_endian
>
8963 class Powerpc_scan_relocatable_reloc
8966 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
8967 static const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
8968 static const int sh_type
= elfcpp::SHT_RELA
;
8970 // Return the symbol referred to by the relocation.
8971 static inline unsigned int
8972 get_r_sym(const Reltype
* reloc
)
8973 { return elfcpp::elf_r_sym
<size
>(reloc
->get_r_info()); }
8975 // Return the type of the relocation.
8976 static inline unsigned int
8977 get_r_type(const Reltype
* reloc
)
8978 { return elfcpp::elf_r_type
<size
>(reloc
->get_r_info()); }
8980 // Return the strategy to use for a local symbol which is not a
8981 // section symbol, given the relocation type.
8982 inline Relocatable_relocs::Reloc_strategy
8983 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
8985 if (r_type
== 0 && r_sym
== 0)
8986 return Relocatable_relocs::RELOC_DISCARD
;
8987 return Relocatable_relocs::RELOC_COPY
;
8990 // Return the strategy to use for a local symbol which is a section
8991 // symbol, given the relocation type.
8992 inline Relocatable_relocs::Reloc_strategy
8993 local_section_strategy(unsigned int, Relobj
*)
8995 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
8998 // Return the strategy to use for a global symbol, given the
8999 // relocation type, the object, and the symbol index.
9000 inline Relocatable_relocs::Reloc_strategy
9001 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
9003 if (r_type
== elfcpp::R_PPC_PLTREL24
)
9004 return Relocatable_relocs::RELOC_SPECIAL
;
9005 return Relocatable_relocs::RELOC_COPY
;
9009 // Scan the relocs during a relocatable link.
9011 template<int size
, bool big_endian
>
9013 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
9014 Symbol_table
* symtab
,
9016 Sized_relobj_file
<size
, big_endian
>* object
,
9017 unsigned int data_shndx
,
9018 unsigned int sh_type
,
9019 const unsigned char* prelocs
,
9021 Output_section
* output_section
,
9022 bool needs_special_offset_handling
,
9023 size_t local_symbol_count
,
9024 const unsigned char* plocal_symbols
,
9025 Relocatable_relocs
* rr
)
9027 typedef Powerpc_scan_relocatable_reloc
<size
, big_endian
> Scan_strategy
;
9029 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9031 gold::scan_relocatable_relocs
<size
, big_endian
, Scan_strategy
>(
9039 needs_special_offset_handling
,
9045 // Scan the relocs for --emit-relocs.
9047 template<int size
, bool big_endian
>
9049 Target_powerpc
<size
, big_endian
>::emit_relocs_scan(
9050 Symbol_table
* symtab
,
9052 Sized_relobj_file
<size
, big_endian
>* object
,
9053 unsigned int data_shndx
,
9054 unsigned int sh_type
,
9055 const unsigned char* prelocs
,
9057 Output_section
* output_section
,
9058 bool needs_special_offset_handling
,
9059 size_t local_symbol_count
,
9060 const unsigned char* plocal_syms
,
9061 Relocatable_relocs
* rr
)
9063 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
9065 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
9066 Emit_relocs_strategy
;
9068 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9070 gold::scan_relocatable_relocs
<size
, big_endian
, Emit_relocs_strategy
>(
9078 needs_special_offset_handling
,
9084 // Emit relocations for a section.
9085 // This is a modified version of the function by the same name in
9086 // target-reloc.h. Using relocate_special_relocatable for
9087 // R_PPC_PLTREL24 would require duplication of the entire body of the
9088 // loop, so we may as well duplicate the whole thing.
9090 template<int size
, bool big_endian
>
9092 Target_powerpc
<size
, big_endian
>::relocate_relocs(
9093 const Relocate_info
<size
, big_endian
>* relinfo
,
9094 unsigned int sh_type
,
9095 const unsigned char* prelocs
,
9097 Output_section
* output_section
,
9098 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
9100 Address view_address
,
9102 unsigned char* reloc_view
,
9103 section_size_type reloc_view_size
)
9105 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9107 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
9108 typedef typename
elfcpp::Rela_write
<size
, big_endian
> Reltype_write
;
9109 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
9110 // Offset from start of insn to d-field reloc.
9111 const int d_offset
= big_endian
? 2 : 0;
9113 Powerpc_relobj
<size
, big_endian
>* const object
9114 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
9115 const unsigned int local_count
= object
->local_symbol_count();
9116 unsigned int got2_shndx
= object
->got2_shndx();
9117 Address got2_addend
= 0;
9118 if (got2_shndx
!= 0)
9120 got2_addend
= object
->get_output_section_offset(got2_shndx
);
9121 gold_assert(got2_addend
!= invalid_address
);
9124 unsigned char* pwrite
= reloc_view
;
9125 bool zap_next
= false;
9126 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
9128 Relocatable_relocs::Reloc_strategy strategy
= relinfo
->rr
->strategy(i
);
9129 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
9132 Reltype
reloc(prelocs
);
9133 Reltype_write
reloc_write(pwrite
);
9135 Address offset
= reloc
.get_r_offset();
9136 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
9137 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
9138 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
9139 const unsigned int orig_r_sym
= r_sym
;
9140 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
9141 = reloc
.get_r_addend();
9142 const Symbol
* gsym
= NULL
;
9146 // We could arrange to discard these and other relocs for
9147 // tls optimised sequences in the strategy methods, but for
9148 // now do as BFD ld does.
9149 r_type
= elfcpp::R_POWERPC_NONE
;
9153 // Get the new symbol index.
9154 Output_section
* os
= NULL
;
9155 if (r_sym
< local_count
)
9159 case Relocatable_relocs::RELOC_COPY
:
9160 case Relocatable_relocs::RELOC_SPECIAL
:
9163 r_sym
= object
->symtab_index(r_sym
);
9164 gold_assert(r_sym
!= -1U);
9168 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
9170 // We are adjusting a section symbol. We need to find
9171 // the symbol table index of the section symbol for
9172 // the output section corresponding to input section
9173 // in which this symbol is defined.
9174 gold_assert(r_sym
< local_count
);
9176 unsigned int shndx
=
9177 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
9178 gold_assert(is_ordinary
);
9179 os
= object
->output_section(shndx
);
9180 gold_assert(os
!= NULL
);
9181 gold_assert(os
->needs_symtab_index());
9182 r_sym
= os
->symtab_index();
9192 gsym
= object
->global_symbol(r_sym
);
9193 gold_assert(gsym
!= NULL
);
9194 if (gsym
->is_forwarder())
9195 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
9197 gold_assert(gsym
->has_symtab_index());
9198 r_sym
= gsym
->symtab_index();
9201 // Get the new offset--the location in the output section where
9202 // this relocation should be applied.
9203 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
9204 offset
+= offset_in_output_section
;
9207 section_offset_type sot_offset
=
9208 convert_types
<section_offset_type
, Address
>(offset
);
9209 section_offset_type new_sot_offset
=
9210 output_section
->output_offset(object
, relinfo
->data_shndx
,
9212 gold_assert(new_sot_offset
!= -1);
9213 offset
= new_sot_offset
;
9216 // In an object file, r_offset is an offset within the section.
9217 // In an executable or dynamic object, generated by
9218 // --emit-relocs, r_offset is an absolute address.
9219 if (!parameters
->options().relocatable())
9221 offset
+= view_address
;
9222 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
9223 offset
-= offset_in_output_section
;
9226 // Handle the reloc addend based on the strategy.
9227 if (strategy
== Relocatable_relocs::RELOC_COPY
)
9229 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
9231 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
9232 gold_assert(os
!= NULL
);
9233 addend
= psymval
->value(object
, addend
) - os
->address();
9235 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
9239 if (addend
>= 32768)
9240 addend
+= got2_addend
;
9242 else if (r_type
== elfcpp::R_POWERPC_REL16_HA
)
9244 r_type
= elfcpp::R_POWERPC_ADDR16_HA
;
9247 else if (r_type
== elfcpp::R_POWERPC_REL16_LO
)
9249 r_type
= elfcpp::R_POWERPC_ADDR16_LO
;
9250 addend
-= d_offset
+ 4;
9256 if (!parameters
->options().relocatable())
9258 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
9259 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
9260 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
9261 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
9263 // First instruction of a global dynamic sequence,
9265 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9266 switch (this->optimize_tls_gd(final
))
9268 case tls::TLSOPT_TO_IE
:
9269 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
9270 - elfcpp::R_POWERPC_GOT_TLSGD16
);
9272 case tls::TLSOPT_TO_LE
:
9273 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
9274 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
9275 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9278 r_type
= elfcpp::R_POWERPC_NONE
;
9286 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
9287 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
9288 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
9289 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
9291 // First instruction of a local dynamic sequence,
9293 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
9295 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
9296 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
9298 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9299 const Output_section
* os
= relinfo
->layout
->tls_segment()
9301 gold_assert(os
!= NULL
);
9302 gold_assert(os
->needs_symtab_index());
9303 r_sym
= os
->symtab_index();
9304 addend
= dtp_offset
;
9308 r_type
= elfcpp::R_POWERPC_NONE
;
9313 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
9314 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
9315 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
9316 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
9318 // First instruction of initial exec sequence.
9319 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9320 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
9322 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
9323 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
9324 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9327 r_type
= elfcpp::R_POWERPC_NONE
;
9332 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
9333 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
9335 // Second instruction of a global dynamic sequence,
9336 // the __tls_get_addr call
9337 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9338 switch (this->optimize_tls_gd(final
))
9340 case tls::TLSOPT_TO_IE
:
9341 r_type
= elfcpp::R_POWERPC_NONE
;
9344 case tls::TLSOPT_TO_LE
:
9345 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9353 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
9354 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
9356 // Second instruction of a local dynamic sequence,
9357 // the __tls_get_addr call
9358 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
9360 const Output_section
* os
= relinfo
->layout
->tls_segment()
9362 gold_assert(os
!= NULL
);
9363 gold_assert(os
->needs_symtab_index());
9364 r_sym
= os
->symtab_index();
9365 addend
= dtp_offset
;
9366 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9371 else if (r_type
== elfcpp::R_POWERPC_TLS
)
9373 // Second instruction of an initial exec sequence
9374 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9375 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
9377 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9383 reloc_write
.put_r_offset(offset
);
9384 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
9385 reloc_write
.put_r_addend(addend
);
9387 pwrite
+= reloc_size
;
9390 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
9391 == reloc_view_size
);
9394 // Return the value to use for a dynamic symbol which requires special
9395 // treatment. This is how we support equality comparisons of function
9396 // pointers across shared library boundaries, as described in the
9397 // processor specific ABI supplement.
9399 template<int size
, bool big_endian
>
9401 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
9405 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
9406 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
9407 p
!= this->stub_tables_
.end();
9410 Address off
= (*p
)->find_plt_call_entry(gsym
);
9411 if (off
!= invalid_address
)
9412 return (*p
)->stub_address() + off
;
9415 else if (this->abiversion() >= 2)
9417 Address off
= this->glink_section()->find_global_entry(gsym
);
9418 if (off
!= invalid_address
)
9419 return this->glink_section()->global_entry_address() + off
;
9424 // Return the PLT address to use for a local symbol.
9425 template<int size
, bool big_endian
>
9427 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
9428 const Relobj
* object
,
9429 unsigned int symndx
) const
9433 const Sized_relobj
<size
, big_endian
>* relobj
9434 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
9435 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
9436 p
!= this->stub_tables_
.end();
9439 Address off
= (*p
)->find_plt_call_entry(relobj
->sized_relobj(),
9441 if (off
!= invalid_address
)
9442 return (*p
)->stub_address() + off
;
9448 // Return the PLT address to use for a global symbol.
9449 template<int size
, bool big_endian
>
9451 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
9452 const Symbol
* gsym
) const
9456 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
9457 p
!= this->stub_tables_
.end();
9460 Address off
= (*p
)->find_plt_call_entry(gsym
);
9461 if (off
!= invalid_address
)
9462 return (*p
)->stub_address() + off
;
9465 else if (this->abiversion() >= 2)
9467 Address off
= this->glink_section()->find_global_entry(gsym
);
9468 if (off
!= invalid_address
)
9469 return this->glink_section()->global_entry_address() + off
;
9474 // Return the offset to use for the GOT_INDX'th got entry which is
9475 // for a local tls symbol specified by OBJECT, SYMNDX.
9476 template<int size
, bool big_endian
>
9478 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
9479 const Relobj
* object
,
9480 unsigned int symndx
,
9481 unsigned int got_indx
) const
9483 const Powerpc_relobj
<size
, big_endian
>* ppc_object
9484 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
9485 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
9487 for (Got_type got_type
= GOT_TYPE_TLSGD
;
9488 got_type
<= GOT_TYPE_TPREL
;
9489 got_type
= Got_type(got_type
+ 1))
9490 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
9492 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
9493 if (got_type
== GOT_TYPE_TLSGD
)
9495 if (off
== got_indx
* (size
/ 8))
9497 if (got_type
== GOT_TYPE_TPREL
)
9507 // Return the offset to use for the GOT_INDX'th got entry which is
9508 // for global tls symbol GSYM.
9509 template<int size
, bool big_endian
>
9511 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
9513 unsigned int got_indx
) const
9515 if (gsym
->type() == elfcpp::STT_TLS
)
9517 for (Got_type got_type
= GOT_TYPE_TLSGD
;
9518 got_type
<= GOT_TYPE_TPREL
;
9519 got_type
= Got_type(got_type
+ 1))
9520 if (gsym
->has_got_offset(got_type
))
9522 unsigned int off
= gsym
->got_offset(got_type
);
9523 if (got_type
== GOT_TYPE_TLSGD
)
9525 if (off
== got_indx
* (size
/ 8))
9527 if (got_type
== GOT_TYPE_TPREL
)
9537 // The selector for powerpc object files.
9539 template<int size
, bool big_endian
>
9540 class Target_selector_powerpc
: public Target_selector
9543 Target_selector_powerpc()
9544 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
9547 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
9548 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
9550 ? (big_endian
? "elf64ppc" : "elf64lppc")
9551 : (big_endian
? "elf32ppc" : "elf32lppc")))
9555 do_instantiate_target()
9556 { return new Target_powerpc
<size
, big_endian
>(); }
9559 Target_selector_powerpc
<32, true> target_selector_ppc32
;
9560 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
9561 Target_selector_powerpc
<64, true> target_selector_ppc64
;
9562 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
9564 // Instantiate these constants for -O0
9565 template<int size
, bool big_endian
>
9566 const int Output_data_glink
<size
, big_endian
>::pltresolve_size
;
9567 template<int size
, bool big_endian
>
9568 const typename Output_data_glink
<size
, big_endian
>::Address
9569 Output_data_glink
<size
, big_endian
>::invalid_address
;
9570 template<int size
, bool big_endian
>
9571 const typename Stub_table
<size
, big_endian
>::Address
9572 Stub_table
<size
, big_endian
>::invalid_address
;
9573 template<int size
, bool big_endian
>
9574 const typename Target_powerpc
<size
, big_endian
>::Address
9575 Target_powerpc
<size
, big_endian
>::invalid_address
;
9577 } // End anonymous namespace.