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 typedef Unordered_set
<Symbol_location
, Symbol_location_hash
> Tocsave_loc
;
597 static const Address invalid_address
= static_cast<Address
>(0) - 1;
598 // Offset of tp and dtp pointers from start of TLS block.
599 static const Address tp_offset
= 0x7000;
600 static const Address dtp_offset
= 0x8000;
603 : Sized_target
<size
, big_endian
>(&powerpc_info
),
604 got_(NULL
), plt_(NULL
), iplt_(NULL
), brlt_section_(NULL
),
605 glink_(NULL
), rela_dyn_(NULL
), copy_relocs_(),
606 tlsld_got_offset_(-1U),
607 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
608 plt_thread_safe_(false), relax_failed_(false), relax_fail_count_(0),
609 stub_group_size_(0), savres_section_(0)
613 // Process the relocations to determine unreferenced sections for
614 // garbage collection.
616 gc_process_relocs(Symbol_table
* symtab
,
618 Sized_relobj_file
<size
, big_endian
>* object
,
619 unsigned int data_shndx
,
620 unsigned int sh_type
,
621 const unsigned char* prelocs
,
623 Output_section
* output_section
,
624 bool needs_special_offset_handling
,
625 size_t local_symbol_count
,
626 const unsigned char* plocal_symbols
);
628 // Scan the relocations to look for symbol adjustments.
630 scan_relocs(Symbol_table
* symtab
,
632 Sized_relobj_file
<size
, big_endian
>* object
,
633 unsigned int data_shndx
,
634 unsigned int sh_type
,
635 const unsigned char* prelocs
,
637 Output_section
* output_section
,
638 bool needs_special_offset_handling
,
639 size_t local_symbol_count
,
640 const unsigned char* plocal_symbols
);
642 // Map input .toc section to output .got section.
644 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
646 if (size
== 64 && strcmp(name
, ".toc") == 0)
654 // Provide linker defined save/restore functions.
656 define_save_restore_funcs(Layout
*, Symbol_table
*);
658 // No stubs unless a final link.
661 { return !parameters
->options().relocatable(); }
664 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
667 do_plt_fde_location(const Output_data
*, unsigned char*,
668 uint64_t*, off_t
*) const;
670 // Stash info about branches, for stub generation.
672 push_branch(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
673 unsigned int data_shndx
, Address r_offset
,
674 unsigned int r_type
, unsigned int r_sym
, Address addend
)
676 Branch_info
info(ppc_object
, data_shndx
, r_offset
, r_type
, r_sym
, addend
);
677 this->branch_info_
.push_back(info
);
678 if (r_type
== elfcpp::R_POWERPC_REL14
679 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
680 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
681 ppc_object
->set_has_14bit_branch(data_shndx
);
684 // Return whether the last branch is a plt call, and if so, mark the
685 // branch as having an R_PPC64_TOCSAVE.
687 mark_pltcall(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
688 unsigned int data_shndx
, Address r_offset
, Symbol_table
* symtab
)
691 && !this->branch_info_
.empty()
692 && this->branch_info_
.back().mark_pltcall(ppc_object
, data_shndx
,
693 r_offset
, this, symtab
));
696 // Say the given location, that of a nop in a function prologue with
697 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
698 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
700 add_tocsave(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
701 unsigned int shndx
, Address offset
)
704 loc
.object
= ppc_object
;
707 this->tocsave_loc_
.insert(loc
);
714 return this->tocsave_loc_
;
718 do_define_standard_symbols(Symbol_table
*, Layout
*);
720 // Finalize the sections.
722 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
724 // Return the value to use for a dynamic which requires special
727 do_dynsym_value(const Symbol
*) const;
729 // Return the PLT address to use for a local symbol.
731 do_plt_address_for_local(const Relobj
*, unsigned int) const;
733 // Return the PLT address to use for a global symbol.
735 do_plt_address_for_global(const Symbol
*) const;
737 // Return the offset to use for the GOT_INDX'th got entry which is
738 // for a local tls symbol specified by OBJECT, SYMNDX.
740 do_tls_offset_for_local(const Relobj
* object
,
742 unsigned int got_indx
) const;
744 // Return the offset to use for the GOT_INDX'th got entry which is
745 // for global tls symbol GSYM.
747 do_tls_offset_for_global(Symbol
* gsym
, unsigned int got_indx
) const;
750 do_function_location(Symbol_location
*) const;
753 do_can_check_for_function_pointers() const
756 // Adjust -fsplit-stack code which calls non-split-stack code.
758 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
759 section_offset_type fnoffset
, section_size_type fnsize
,
760 const unsigned char* prelocs
, size_t reloc_count
,
761 unsigned char* view
, section_size_type view_size
,
762 std::string
* from
, std::string
* to
) const;
764 // Relocate a section.
766 relocate_section(const Relocate_info
<size
, big_endian
>*,
767 unsigned int sh_type
,
768 const unsigned char* prelocs
,
770 Output_section
* output_section
,
771 bool needs_special_offset_handling
,
773 Address view_address
,
774 section_size_type view_size
,
775 const Reloc_symbol_changes
*);
777 // Scan the relocs during a relocatable link.
779 scan_relocatable_relocs(Symbol_table
* symtab
,
781 Sized_relobj_file
<size
, big_endian
>* object
,
782 unsigned int data_shndx
,
783 unsigned int sh_type
,
784 const unsigned char* prelocs
,
786 Output_section
* output_section
,
787 bool needs_special_offset_handling
,
788 size_t local_symbol_count
,
789 const unsigned char* plocal_symbols
,
790 Relocatable_relocs
*);
792 // Scan the relocs for --emit-relocs.
794 emit_relocs_scan(Symbol_table
* symtab
,
796 Sized_relobj_file
<size
, big_endian
>* object
,
797 unsigned int data_shndx
,
798 unsigned int sh_type
,
799 const unsigned char* prelocs
,
801 Output_section
* output_section
,
802 bool needs_special_offset_handling
,
803 size_t local_symbol_count
,
804 const unsigned char* plocal_syms
,
805 Relocatable_relocs
* rr
);
807 // Emit relocations for a section.
809 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
810 unsigned int sh_type
,
811 const unsigned char* prelocs
,
813 Output_section
* output_section
,
814 typename
elfcpp::Elf_types
<size
>::Elf_Off
815 offset_in_output_section
,
817 Address view_address
,
819 unsigned char* reloc_view
,
820 section_size_type reloc_view_size
);
822 // Return whether SYM is defined by the ABI.
824 do_is_defined_by_abi(const Symbol
* sym
) const
826 return strcmp(sym
->name(), "__tls_get_addr") == 0;
829 // Return the size of the GOT section.
833 gold_assert(this->got_
!= NULL
);
834 return this->got_
->data_size();
837 // Get the PLT section.
838 const Output_data_plt_powerpc
<size
, big_endian
>*
841 gold_assert(this->plt_
!= NULL
);
845 // Get the IPLT section.
846 const Output_data_plt_powerpc
<size
, big_endian
>*
849 gold_assert(this->iplt_
!= NULL
);
853 // Get the .glink section.
854 const Output_data_glink
<size
, big_endian
>*
855 glink_section() const
857 gold_assert(this->glink_
!= NULL
);
861 Output_data_glink
<size
, big_endian
>*
864 gold_assert(this->glink_
!= NULL
);
868 bool has_glink() const
869 { return this->glink_
!= NULL
; }
871 // Get the GOT section.
872 const Output_data_got_powerpc
<size
, big_endian
>*
875 gold_assert(this->got_
!= NULL
);
879 // Get the GOT section, creating it if necessary.
880 Output_data_got_powerpc
<size
, big_endian
>*
881 got_section(Symbol_table
*, Layout
*);
884 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
885 const elfcpp::Ehdr
<size
, big_endian
>&);
887 // Return the number of entries in the GOT.
889 got_entry_count() const
891 if (this->got_
== NULL
)
893 return this->got_size() / (size
/ 8);
896 // Return the number of entries in the PLT.
898 plt_entry_count() const;
900 // Return the offset of the first non-reserved PLT entry.
902 first_plt_entry_offset() const
906 if (this->abiversion() >= 2)
911 // Return the size of each PLT entry.
913 plt_entry_size() const
917 if (this->abiversion() >= 2)
922 Output_data_save_res
<size
, big_endian
>*
923 savres_section() const
925 return this->savres_section_
;
928 // Add any special sections for this symbol to the gc work list.
929 // For powerpc64, this adds the code section of a function
932 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
934 // Handle target specific gc actions when adding a gc reference from
935 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
936 // and DST_OFF. For powerpc64, this adds a referenc to the code
937 // section of a function descriptor.
939 do_gc_add_reference(Symbol_table
* symtab
,
941 unsigned int src_shndx
,
943 unsigned int dst_shndx
,
944 Address dst_off
) const;
946 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
949 { return this->stub_tables_
; }
951 const Output_data_brlt_powerpc
<size
, big_endian
>*
953 { return this->brlt_section_
; }
956 add_branch_lookup_table(Address to
)
958 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
959 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
963 find_branch_lookup_table(Address to
)
965 typename
Branch_lookup_table::const_iterator p
966 = this->branch_lookup_table_
.find(to
);
967 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
971 write_branch_lookup_table(unsigned char *oview
)
973 for (typename
Branch_lookup_table::const_iterator p
974 = this->branch_lookup_table_
.begin();
975 p
!= this->branch_lookup_table_
.end();
978 elfcpp::Swap
<size
, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
982 // Wrapper used after relax to define a local symbol in output data,
983 // from the end if value < 0.
985 define_local(Symbol_table
* symtab
, const char* name
,
986 Output_data
* od
, Address value
, unsigned int symsize
)
989 = symtab
->define_in_output_data(name
, NULL
, Symbol_table::PREDEFINED
,
990 od
, value
, symsize
, elfcpp::STT_NOTYPE
,
991 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
, 0,
992 static_cast<Signed_address
>(value
) < 0,
994 // We are creating this symbol late, so need to fix up things
995 // done early in Layout::finalize.
996 sym
->set_dynsym_index(-1U);
1000 plt_thread_safe() const
1001 { return this->plt_thread_safe_
; }
1005 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI
; }
1008 set_abiversion (int ver
)
1010 elfcpp::Elf_Word flags
= this->processor_specific_flags();
1011 flags
&= ~elfcpp::EF_PPC64_ABI
;
1012 flags
|= ver
& elfcpp::EF_PPC64_ABI
;
1013 this->set_processor_specific_flags(flags
);
1016 // Offset to to save stack slot
1019 { return this->abiversion() < 2 ? 40 : 24; }
1035 : tls_get_addr_(NOT_EXPECTED
),
1036 relinfo_(NULL
), relnum_(0), r_offset_(0)
1041 if (this->tls_get_addr_
!= NOT_EXPECTED
)
1048 if (this->relinfo_
!= NULL
)
1049 gold_error_at_location(this->relinfo_
, this->relnum_
, this->r_offset_
,
1050 _("missing expected __tls_get_addr call"));
1054 expect_tls_get_addr_call(
1055 const Relocate_info
<size
, big_endian
>* relinfo
,
1059 this->tls_get_addr_
= EXPECTED
;
1060 this->relinfo_
= relinfo
;
1061 this->relnum_
= relnum
;
1062 this->r_offset_
= r_offset
;
1066 expect_tls_get_addr_call()
1067 { this->tls_get_addr_
= EXPECTED
; }
1070 skip_next_tls_get_addr_call()
1071 {this->tls_get_addr_
= SKIP
; }
1074 maybe_skip_tls_get_addr_call(unsigned int r_type
, const Symbol
* gsym
)
1076 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
1077 || r_type
== elfcpp::R_PPC_PLTREL24
)
1079 && strcmp(gsym
->name(), "__tls_get_addr") == 0);
1080 Tls_get_addr last_tls
= this->tls_get_addr_
;
1081 this->tls_get_addr_
= NOT_EXPECTED
;
1082 if (is_tls_call
&& last_tls
!= EXPECTED
)
1084 else if (!is_tls_call
&& last_tls
!= NOT_EXPECTED
)
1093 // What we're up to regarding calls to __tls_get_addr.
1094 // On powerpc, the branch and link insn making a call to
1095 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1096 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1097 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
1098 // The marker relocation always comes first, and has the same
1099 // symbol as the reloc on the insn setting up the __tls_get_addr
1100 // argument. This ties the arg setup insn with the call insn,
1101 // allowing ld to safely optimize away the call. We check that
1102 // every call to __tls_get_addr has a marker relocation, and that
1103 // every marker relocation is on a call to __tls_get_addr.
1104 Tls_get_addr tls_get_addr_
;
1105 // Info about the last reloc for error message.
1106 const Relocate_info
<size
, big_endian
>* relinfo_
;
1111 // The class which scans relocations.
1112 class Scan
: protected Track_tls
1115 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1118 : Track_tls(), issued_non_pic_error_(false)
1122 get_reference_flags(unsigned int r_type
, const Target_powerpc
* target
);
1125 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
1126 Sized_relobj_file
<size
, big_endian
>* object
,
1127 unsigned int data_shndx
,
1128 Output_section
* output_section
,
1129 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
1130 const elfcpp::Sym
<size
, big_endian
>& lsym
,
1134 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
1135 Sized_relobj_file
<size
, big_endian
>* object
,
1136 unsigned int data_shndx
,
1137 Output_section
* output_section
,
1138 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
1142 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1144 Sized_relobj_file
<size
, big_endian
>* relobj
,
1147 const elfcpp::Rela
<size
, big_endian
>& ,
1148 unsigned int r_type
,
1149 const elfcpp::Sym
<size
, big_endian
>&)
1151 // PowerPC64 .opd is not folded, so any identical function text
1152 // may be folded and we'll still keep function addresses distinct.
1153 // That means no reloc is of concern here.
1156 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1157 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1158 if (ppcobj
->abiversion() == 1)
1161 // For 32-bit and ELFv2, conservatively assume anything but calls to
1162 // function code might be taking the address of the function.
1163 return !is_branch_reloc(r_type
);
1167 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1169 Sized_relobj_file
<size
, big_endian
>* relobj
,
1172 const elfcpp::Rela
<size
, big_endian
>& ,
1173 unsigned int r_type
,
1179 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1180 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1181 if (ppcobj
->abiversion() == 1)
1184 return !is_branch_reloc(r_type
);
1188 reloc_needs_plt_for_ifunc(Target_powerpc
<size
, big_endian
>* target
,
1189 Sized_relobj_file
<size
, big_endian
>* object
,
1190 unsigned int r_type
, bool report_err
);
1194 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
1195 unsigned int r_type
);
1198 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
1199 unsigned int r_type
, Symbol
*);
1202 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
1203 Target_powerpc
* target
);
1206 check_non_pic(Relobj
*, unsigned int r_type
);
1208 // Whether we have issued an error about a non-PIC compilation.
1209 bool issued_non_pic_error_
;
1213 symval_for_branch(const Symbol_table
* symtab
,
1214 const Sized_symbol
<size
>* gsym
,
1215 Powerpc_relobj
<size
, big_endian
>* object
,
1216 Address
*value
, unsigned int *dest_shndx
);
1218 // The class which implements relocation.
1219 class Relocate
: protected Track_tls
1222 // Use 'at' branch hints when true, 'y' when false.
1223 // FIXME maybe: set this with an option.
1224 static const bool is_isa_v2
= true;
1230 // Do a relocation. Return false if the caller should not issue
1231 // any warnings about this relocation.
1233 relocate(const Relocate_info
<size
, big_endian
>*, unsigned int,
1234 Target_powerpc
*, Output_section
*, size_t, const unsigned char*,
1235 const Sized_symbol
<size
>*, const Symbol_value
<size
>*,
1236 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1240 class Relocate_comdat_behavior
1243 // Decide what the linker should do for relocations that refer to
1244 // discarded comdat sections.
1245 inline Comdat_behavior
1246 get(const char* name
)
1248 gold::Default_comdat_behavior default_behavior
;
1249 Comdat_behavior ret
= default_behavior
.get(name
);
1250 if (ret
== CB_WARNING
)
1253 && (strcmp(name
, ".fixup") == 0
1254 || strcmp(name
, ".got2") == 0))
1257 && (strcmp(name
, ".opd") == 0
1258 || strcmp(name
, ".toc") == 0
1259 || strcmp(name
, ".toc1") == 0))
1266 // Optimize the TLS relocation type based on what we know about the
1267 // symbol. IS_FINAL is true if the final address of this symbol is
1268 // known at link time.
1270 tls::Tls_optimization
1271 optimize_tls_gd(bool is_final
)
1273 // If we are generating a shared library, then we can't do anything
1275 if (parameters
->options().shared())
1276 return tls::TLSOPT_NONE
;
1279 return tls::TLSOPT_TO_IE
;
1280 return tls::TLSOPT_TO_LE
;
1283 tls::Tls_optimization
1286 if (parameters
->options().shared())
1287 return tls::TLSOPT_NONE
;
1289 return tls::TLSOPT_TO_LE
;
1292 tls::Tls_optimization
1293 optimize_tls_ie(bool is_final
)
1295 if (!is_final
|| parameters
->options().shared())
1296 return tls::TLSOPT_NONE
;
1298 return tls::TLSOPT_TO_LE
;
1303 make_glink_section(Layout
*);
1305 // Create the PLT section.
1307 make_plt_section(Symbol_table
*, Layout
*);
1310 make_iplt_section(Symbol_table
*, Layout
*);
1313 make_brlt_section(Layout
*);
1315 // Create a PLT entry for a global symbol.
1317 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1319 // Create a PLT entry for a local IFUNC symbol.
1321 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1322 Sized_relobj_file
<size
, big_endian
>*,
1326 // Create a GOT entry for local dynamic __tls_get_addr.
1328 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
1329 Sized_relobj_file
<size
, big_endian
>* object
);
1332 tlsld_got_offset() const
1334 return this->tlsld_got_offset_
;
1337 // Get the dynamic reloc section, creating it if necessary.
1339 rela_dyn_section(Layout
*);
1341 // Similarly, but for ifunc symbols get the one for ifunc.
1343 rela_dyn_section(Symbol_table
*, Layout
*, bool for_ifunc
);
1345 // Copy a relocation against a global symbol.
1347 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1348 Sized_relobj_file
<size
, big_endian
>* object
,
1349 unsigned int shndx
, Output_section
* output_section
,
1350 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
1352 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1353 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1354 symtab
->get_sized_symbol
<size
>(sym
),
1355 object
, shndx
, output_section
,
1356 r_type
, reloc
.get_r_offset(),
1357 reloc
.get_r_addend(),
1358 this->rela_dyn_section(layout
));
1361 // Look over all the input sections, deciding where to place stubs.
1363 group_sections(Layout
*, const Task
*, bool);
1365 // Sort output sections by address.
1366 struct Sort_sections
1369 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
1370 { return sec1
->address() < sec2
->address(); }
1376 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1377 unsigned int data_shndx
,
1379 unsigned int r_type
,
1382 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
1383 r_type_(r_type
), tocsave_ (0), r_sym_(r_sym
), addend_(addend
)
1389 // Return whether this branch is going via a plt call stub, and if
1390 // so, mark it as having an R_PPC64_TOCSAVE.
1392 mark_pltcall(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1393 unsigned int shndx
, Address offset
,
1394 Target_powerpc
* target
, Symbol_table
* symtab
);
1396 // If this branch needs a plt call stub, or a long branch stub, make one.
1398 make_stub(Stub_table
<size
, big_endian
>*,
1399 Stub_table
<size
, big_endian
>*,
1400 Symbol_table
*) const;
1403 // The branch location..
1404 Powerpc_relobj
<size
, big_endian
>* object_
;
1405 unsigned int shndx_
;
1407 // ..and the branch type and destination.
1408 unsigned int r_type_
: 31;
1409 unsigned int tocsave_
: 1;
1410 unsigned int r_sym_
;
1414 // Information about this specific target which we pass to the
1415 // general Target structure.
1416 static Target::Target_info powerpc_info
;
1418 // The types of GOT entries needed for this platform.
1419 // These values are exposed to the ABI in an incremental link.
1420 // Do not renumber existing values without changing the version
1421 // number of the .gnu_incremental_inputs section.
1425 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
1426 GOT_TYPE_DTPREL
, // entry for @got@dtprel
1427 GOT_TYPE_TPREL
// entry for @got@tprel
1431 Output_data_got_powerpc
<size
, big_endian
>* got_
;
1432 // The PLT section. This is a container for a table of addresses,
1433 // and their relocations. Each address in the PLT has a dynamic
1434 // relocation (R_*_JMP_SLOT) and each address will have a
1435 // corresponding entry in .glink for lazy resolution of the PLT.
1436 // ppc32 initialises the PLT to point at the .glink entry, while
1437 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1438 // linker adds a stub that loads the PLT entry into ctr then
1439 // branches to ctr. There may be more than one stub for each PLT
1440 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1441 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1442 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
1443 // The IPLT section. Like plt_, this is a container for a table of
1444 // addresses and their relocations, specifically for STT_GNU_IFUNC
1445 // functions that resolve locally (STT_GNU_IFUNC functions that
1446 // don't resolve locally go in PLT). Unlike plt_, these have no
1447 // entry in .glink for lazy resolution, and the relocation section
1448 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1449 // the relocation section may contain relocations against
1450 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1451 // relocation section will appear at the end of other dynamic
1452 // relocations, so that ld.so applies these relocations after other
1453 // dynamic relocations. In a static executable, the relocation
1454 // section is emitted and marked with __rela_iplt_start and
1455 // __rela_iplt_end symbols.
1456 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
1457 // Section holding long branch destinations.
1458 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
1459 // The .glink section.
1460 Output_data_glink
<size
, big_endian
>* glink_
;
1461 // The dynamic reloc section.
1462 Reloc_section
* rela_dyn_
;
1463 // Relocs saved to avoid a COPY reloc.
1464 Powerpc_copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
1465 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1466 unsigned int tlsld_got_offset_
;
1468 Stub_tables stub_tables_
;
1469 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
1470 Branch_lookup_table branch_lookup_table_
;
1472 typedef std::vector
<Branch_info
> Branches
;
1473 Branches branch_info_
;
1474 Tocsave_loc tocsave_loc_
;
1476 bool plt_thread_safe_
;
1479 int relax_fail_count_
;
1480 int32_t stub_group_size_
;
1482 Output_data_save_res
<size
, big_endian
> *savres_section_
;
1486 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
1489 true, // is_big_endian
1490 elfcpp::EM_PPC
, // machine_code
1491 false, // has_make_symbol
1492 false, // has_resolve
1493 false, // has_code_fill
1494 true, // is_default_stack_executable
1495 false, // can_icf_inline_merge_sections
1497 "/usr/lib/ld.so.1", // dynamic_linker
1498 0x10000000, // default_text_segment_address
1499 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1500 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1501 false, // isolate_execinstr
1503 elfcpp::SHN_UNDEF
, // small_common_shndx
1504 elfcpp::SHN_UNDEF
, // large_common_shndx
1505 0, // small_common_section_flags
1506 0, // large_common_section_flags
1507 NULL
, // attributes_section
1508 NULL
, // attributes_vendor
1509 "_start", // entry_symbol_name
1510 32, // hash_entry_size
1514 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
1517 false, // is_big_endian
1518 elfcpp::EM_PPC
, // machine_code
1519 false, // has_make_symbol
1520 false, // has_resolve
1521 false, // has_code_fill
1522 true, // is_default_stack_executable
1523 false, // can_icf_inline_merge_sections
1525 "/usr/lib/ld.so.1", // dynamic_linker
1526 0x10000000, // default_text_segment_address
1527 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1528 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1529 false, // isolate_execinstr
1531 elfcpp::SHN_UNDEF
, // small_common_shndx
1532 elfcpp::SHN_UNDEF
, // large_common_shndx
1533 0, // small_common_section_flags
1534 0, // large_common_section_flags
1535 NULL
, // attributes_section
1536 NULL
, // attributes_vendor
1537 "_start", // entry_symbol_name
1538 32, // hash_entry_size
1542 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1545 true, // is_big_endian
1546 elfcpp::EM_PPC64
, // machine_code
1547 false, // has_make_symbol
1548 false, // has_resolve
1549 false, // has_code_fill
1550 true, // is_default_stack_executable
1551 false, // can_icf_inline_merge_sections
1553 "/usr/lib/ld.so.1", // dynamic_linker
1554 0x10000000, // default_text_segment_address
1555 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1556 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1557 false, // isolate_execinstr
1559 elfcpp::SHN_UNDEF
, // small_common_shndx
1560 elfcpp::SHN_UNDEF
, // large_common_shndx
1561 0, // small_common_section_flags
1562 0, // large_common_section_flags
1563 NULL
, // attributes_section
1564 NULL
, // attributes_vendor
1565 "_start", // entry_symbol_name
1566 32, // hash_entry_size
1570 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1573 false, // is_big_endian
1574 elfcpp::EM_PPC64
, // machine_code
1575 false, // has_make_symbol
1576 false, // has_resolve
1577 false, // has_code_fill
1578 true, // is_default_stack_executable
1579 false, // can_icf_inline_merge_sections
1581 "/usr/lib/ld.so.1", // dynamic_linker
1582 0x10000000, // default_text_segment_address
1583 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1584 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1585 false, // isolate_execinstr
1587 elfcpp::SHN_UNDEF
, // small_common_shndx
1588 elfcpp::SHN_UNDEF
, // large_common_shndx
1589 0, // small_common_section_flags
1590 0, // large_common_section_flags
1591 NULL
, // attributes_section
1592 NULL
, // attributes_vendor
1593 "_start", // entry_symbol_name
1594 32, // hash_entry_size
1598 is_branch_reloc(unsigned int r_type
)
1600 return (r_type
== elfcpp::R_POWERPC_REL24
1601 || r_type
== elfcpp::R_PPC_PLTREL24
1602 || r_type
== elfcpp::R_PPC_LOCAL24PC
1603 || r_type
== elfcpp::R_POWERPC_REL14
1604 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1605 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1606 || r_type
== elfcpp::R_POWERPC_ADDR24
1607 || r_type
== elfcpp::R_POWERPC_ADDR14
1608 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1609 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1612 // If INSN is an opcode that may be used with an @tls operand, return
1613 // the transformed insn for TLS optimisation, otherwise return 0. If
1614 // REG is non-zero only match an insn with RB or RA equal to REG.
1616 at_tls_transform(uint32_t insn
, unsigned int reg
)
1618 if ((insn
& (0x3f << 26)) != 31 << 26)
1622 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1623 rtra
= insn
& ((1 << 26) - (1 << 16));
1624 else if (((insn
>> 16) & 0x1f) == reg
)
1625 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1629 if ((insn
& (0x3ff << 1)) == 266 << 1)
1632 else if ((insn
& (0x1f << 1)) == 23 << 1
1633 && ((insn
& (0x1f << 6)) < 14 << 6
1634 || ((insn
& (0x1f << 6)) >= 16 << 6
1635 && (insn
& (0x1f << 6)) < 24 << 6)))
1636 // load and store indexed -> dform
1637 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1638 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1639 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1640 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1641 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1643 insn
= (58 << 26) | 2;
1651 template<int size
, bool big_endian
>
1652 class Powerpc_relocate_functions
1672 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1673 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1674 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword SignedAddress
;
1676 template<int valsize
>
1678 has_overflow_signed(Address value
)
1680 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1681 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1682 limit
<<= ((valsize
- 1) >> 1);
1683 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1684 return value
+ limit
> (limit
<< 1) - 1;
1687 template<int valsize
>
1689 has_overflow_unsigned(Address value
)
1691 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1692 limit
<<= ((valsize
- 1) >> 1);
1693 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1694 return value
> (limit
<< 1) - 1;
1697 template<int valsize
>
1699 has_overflow_bitfield(Address value
)
1701 return (has_overflow_unsigned
<valsize
>(value
)
1702 && has_overflow_signed
<valsize
>(value
));
1705 template<int valsize
>
1706 static inline Status
1707 overflowed(Address value
, Overflow_check overflow
)
1709 if (overflow
== CHECK_SIGNED
)
1711 if (has_overflow_signed
<valsize
>(value
))
1712 return STATUS_OVERFLOW
;
1714 else if (overflow
== CHECK_UNSIGNED
)
1716 if (has_overflow_unsigned
<valsize
>(value
))
1717 return STATUS_OVERFLOW
;
1719 else if (overflow
== CHECK_BITFIELD
)
1721 if (has_overflow_bitfield
<valsize
>(value
))
1722 return STATUS_OVERFLOW
;
1727 // Do a simple RELA relocation
1728 template<int fieldsize
, int valsize
>
1729 static inline Status
1730 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1732 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1733 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1734 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, value
);
1735 return overflowed
<valsize
>(value
, overflow
);
1738 template<int fieldsize
, int valsize
>
1739 static inline Status
1740 rela(unsigned char* view
,
1741 unsigned int right_shift
,
1742 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1744 Overflow_check overflow
)
1746 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1747 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1748 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(wv
);
1749 Valtype reloc
= value
>> right_shift
;
1752 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, val
| reloc
);
1753 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1756 // Do a simple RELA relocation, unaligned.
1757 template<int fieldsize
, int valsize
>
1758 static inline Status
1759 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1761 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, value
);
1762 return overflowed
<valsize
>(value
, overflow
);
1765 template<int fieldsize
, int valsize
>
1766 static inline Status
1767 rela_ua(unsigned char* view
,
1768 unsigned int right_shift
,
1769 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1771 Overflow_check overflow
)
1773 typedef typename
elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::Valtype
1775 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(view
);
1776 Valtype reloc
= value
>> right_shift
;
1779 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, val
| reloc
);
1780 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1784 // R_PPC64_ADDR64: (Symbol + Addend)
1786 addr64(unsigned char* view
, Address value
)
1787 { This::template rela
<64,64>(view
, value
, CHECK_NONE
); }
1789 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1791 addr64_u(unsigned char* view
, Address value
)
1792 { This::template rela_ua
<64,64>(view
, value
, CHECK_NONE
); }
1794 // R_POWERPC_ADDR32: (Symbol + Addend)
1795 static inline Status
1796 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
1797 { return This::template rela
<32,32>(view
, value
, overflow
); }
1799 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1800 static inline Status
1801 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1802 { return This::template rela_ua
<32,32>(view
, value
, overflow
); }
1804 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1805 static inline Status
1806 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
1808 Status stat
= This::template rela
<32,26>(view
, 0, 0x03fffffc,
1810 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1811 stat
= STATUS_OVERFLOW
;
1815 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1816 static inline Status
1817 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
1818 { return This::template rela
<16,16>(view
, value
, overflow
); }
1820 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1821 static inline Status
1822 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1823 { return This::template rela_ua
<16,16>(view
, value
, overflow
); }
1825 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1826 static inline Status
1827 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
1829 Status stat
= This::template rela
<16,16>(view
, 0, 0xfffc, value
, overflow
);
1830 if ((value
& 3) != 0)
1831 stat
= STATUS_OVERFLOW
;
1835 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
1836 static inline Status
1837 addr16_dq(unsigned char* view
, Address value
, Overflow_check overflow
)
1839 Status stat
= This::template rela
<16,16>(view
, 0, 0xfff0, value
, overflow
);
1840 if ((value
& 15) != 0)
1841 stat
= STATUS_OVERFLOW
;
1845 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1847 addr16_hi(unsigned char* view
, Address value
)
1848 { This::template rela
<16,16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
1850 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1852 addr16_ha(unsigned char* view
, Address value
)
1853 { This::addr16_hi(view
, value
+ 0x8000); }
1855 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1857 addr16_hi2(unsigned char* view
, Address value
)
1858 { This::template rela
<16,16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
1860 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1862 addr16_ha2(unsigned char* view
, Address value
)
1863 { This::addr16_hi2(view
, value
+ 0x8000); }
1865 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1867 addr16_hi3(unsigned char* view
, Address value
)
1868 { This::template rela
<16,16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
1870 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1872 addr16_ha3(unsigned char* view
, Address value
)
1873 { This::addr16_hi3(view
, value
+ 0x8000); }
1875 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1876 static inline Status
1877 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
1879 Status stat
= This::template rela
<32,16>(view
, 0, 0xfffc, value
, overflow
);
1880 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1881 stat
= STATUS_OVERFLOW
;
1885 // R_POWERPC_REL16DX_HA
1886 static inline Status
1887 addr16dx_ha(unsigned char *view
, Address value
, Overflow_check overflow
)
1889 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1890 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1891 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1893 value
= static_cast<SignedAddress
>(value
) >> 16;
1894 val
|= (value
& 0xffc1) | ((value
& 0x3e) << 15);
1895 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1896 return overflowed
<16>(value
, overflow
);
1900 // Set ABI version for input and output.
1902 template<int size
, bool big_endian
>
1904 Powerpc_relobj
<size
, big_endian
>::set_abiversion(int ver
)
1906 this->e_flags_
|= ver
;
1907 if (this->abiversion() != 0)
1909 Target_powerpc
<size
, big_endian
>* target
=
1910 static_cast<Target_powerpc
<size
, big_endian
>*>(
1911 parameters
->sized_target
<size
, big_endian
>());
1912 if (target
->abiversion() == 0)
1913 target
->set_abiversion(this->abiversion());
1914 else if (target
->abiversion() != this->abiversion())
1915 gold_error(_("%s: ABI version %d is not compatible "
1916 "with ABI version %d output"),
1917 this->name().c_str(),
1918 this->abiversion(), target
->abiversion());
1923 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
1924 // relocatable object, if such sections exists.
1926 template<int size
, bool big_endian
>
1928 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
1929 Read_symbols_data
* sd
)
1931 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1932 const unsigned char* namesu
= sd
->section_names
->data();
1933 const char* names
= reinterpret_cast<const char*>(namesu
);
1934 section_size_type names_size
= sd
->section_names_size
;
1935 const unsigned char* s
;
1937 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
,
1938 size
== 32 ? ".got2" : ".opd",
1939 names
, names_size
, NULL
);
1942 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
1943 this->special_
= ndx
;
1946 if (this->abiversion() == 0)
1947 this->set_abiversion(1);
1948 else if (this->abiversion() > 1)
1949 gold_error(_("%s: .opd invalid in abiv%d"),
1950 this->name().c_str(), this->abiversion());
1955 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".rela.toc",
1956 names
, names_size
, NULL
);
1959 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
1960 this->relatoc_
= ndx
;
1961 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
1962 this->toc_
= this->adjust_shndx(shdr
.get_sh_info());
1965 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
1968 // Examine .rela.opd to build info about function entry points.
1970 template<int size
, bool big_endian
>
1972 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
1974 const unsigned char* prelocs
,
1975 const unsigned char* plocal_syms
)
1979 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
1980 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
1981 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1982 Address expected_off
= 0;
1983 bool regular
= true;
1984 unsigned int opd_ent_size
= 0;
1986 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
1988 Reltype
reloc(prelocs
);
1989 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
1990 = reloc
.get_r_info();
1991 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
1992 if (r_type
== elfcpp::R_PPC64_ADDR64
)
1994 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1995 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
1998 if (r_sym
< this->local_symbol_count())
2000 typename
elfcpp::Sym
<size
, big_endian
>
2001 lsym(plocal_syms
+ r_sym
* sym_size
);
2002 shndx
= lsym
.get_st_shndx();
2003 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
2004 value
= lsym
.get_st_value();
2007 shndx
= this->symbol_section_and_value(r_sym
, &value
,
2009 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
2010 value
+ reloc
.get_r_addend());
2013 expected_off
= reloc
.get_r_offset();
2014 opd_ent_size
= expected_off
;
2016 else if (expected_off
!= reloc
.get_r_offset())
2018 expected_off
+= opd_ent_size
;
2020 else if (r_type
== elfcpp::R_PPC64_TOC
)
2022 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
2027 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2028 this->name().c_str(), r_type
);
2032 if (reloc_count
<= 2)
2033 opd_ent_size
= this->section_size(this->opd_shndx());
2034 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
2038 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2039 this->name().c_str());
2045 // Returns true if a code sequence loading the TOC entry at VALUE
2046 // relative to the TOC pointer can be converted into code calculating
2047 // a TOC pointer relative offset.
2048 // If so, the TOC pointer relative offset is stored to VALUE.
2050 template<int size
, bool big_endian
>
2052 Powerpc_relobj
<size
, big_endian
>::make_toc_relative(
2053 Target_powerpc
<size
, big_endian
>* target
,
2059 // With -mcmodel=medium code it is quite possible to have
2060 // toc-relative relocs referring to objects outside the TOC.
2061 // Don't try to look at a non-existent TOC.
2062 if (this->toc_shndx() == 0)
2065 // Convert VALUE back to an address by adding got_base (see below),
2066 // then to an offset in the TOC by subtracting the TOC output
2067 // section address and the TOC output offset. Since this TOC output
2068 // section and the got output section are one and the same, we can
2069 // omit adding and subtracting the output section address.
2070 Address off
= (*value
+ this->toc_base_offset()
2071 - this->output_section_offset(this->toc_shndx()));
2072 // Is this offset in the TOC? -mcmodel=medium code may be using
2073 // TOC relative access to variables outside the TOC. Those of
2074 // course can't be optimized. We also don't try to optimize code
2075 // that is using a different object's TOC.
2076 if (off
>= this->section_size(this->toc_shndx()))
2079 if (this->no_toc_opt(off
))
2082 section_size_type vlen
;
2083 unsigned char* view
= this->get_output_view(this->toc_shndx(), &vlen
);
2084 Address addr
= elfcpp::Swap
<size
, big_endian
>::readval(view
+ off
);
2086 Address got_base
= (target
->got_section()->output_section()->address()
2087 + this->toc_base_offset());
2089 if (addr
+ (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2096 // Perform the Sized_relobj_file method, then set up opd info from
2099 template<int size
, bool big_endian
>
2101 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
2103 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
2106 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
2107 p
!= rd
->relocs
.end();
2110 if (p
->data_shndx
== this->opd_shndx())
2112 uint64_t opd_size
= this->section_size(this->opd_shndx());
2113 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
2116 this->init_opd(opd_size
);
2117 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
2118 rd
->local_symbols
->data());
2126 // Read the symbols then set up st_other vector.
2128 template<int size
, bool big_endian
>
2130 Powerpc_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
2132 this->base_read_symbols(sd
);
2135 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2136 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2137 const unsigned int loccount
= this->do_local_symbol_count();
2140 this->st_other_
.resize(loccount
);
2141 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2142 off_t locsize
= loccount
* sym_size
;
2143 const unsigned int symtab_shndx
= this->symtab_shndx();
2144 const unsigned char *psymtab
= pshdrs
+ symtab_shndx
* shdr_size
;
2145 typename
elfcpp::Shdr
<size
, big_endian
> shdr(psymtab
);
2146 const unsigned char* psyms
= this->get_view(shdr
.get_sh_offset(),
2147 locsize
, true, false);
2149 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
2151 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
2152 unsigned char st_other
= sym
.get_st_other();
2153 this->st_other_
[i
] = st_other
;
2154 if ((st_other
& elfcpp::STO_PPC64_LOCAL_MASK
) != 0)
2156 if (this->abiversion() == 0)
2157 this->set_abiversion(2);
2158 else if (this->abiversion() < 2)
2159 gold_error(_("%s: local symbol %d has invalid st_other"
2160 " for ABI version 1"),
2161 this->name().c_str(), i
);
2168 template<int size
, bool big_endian
>
2170 Powerpc_dynobj
<size
, big_endian
>::set_abiversion(int ver
)
2172 this->e_flags_
|= ver
;
2173 if (this->abiversion() != 0)
2175 Target_powerpc
<size
, big_endian
>* target
=
2176 static_cast<Target_powerpc
<size
, big_endian
>*>(
2177 parameters
->sized_target
<size
, big_endian
>());
2178 if (target
->abiversion() == 0)
2179 target
->set_abiversion(this->abiversion());
2180 else if (target
->abiversion() != this->abiversion())
2181 gold_error(_("%s: ABI version %d is not compatible "
2182 "with ABI version %d output"),
2183 this->name().c_str(),
2184 this->abiversion(), target
->abiversion());
2189 // Call Sized_dynobj::base_read_symbols to read the symbols then
2190 // read .opd from a dynamic object, filling in opd_ent_ vector,
2192 template<int size
, bool big_endian
>
2194 Powerpc_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
2196 this->base_read_symbols(sd
);
2199 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2200 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2201 const unsigned char* namesu
= sd
->section_names
->data();
2202 const char* names
= reinterpret_cast<const char*>(namesu
);
2203 const unsigned char* s
= NULL
;
2204 const unsigned char* opd
;
2205 section_size_type opd_size
;
2207 // Find and read .opd section.
2210 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".opd", names
,
2211 sd
->section_names_size
,
2216 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2217 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2218 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
2220 if (this->abiversion() == 0)
2221 this->set_abiversion(1);
2222 else if (this->abiversion() > 1)
2223 gold_error(_("%s: .opd invalid in abiv%d"),
2224 this->name().c_str(), this->abiversion());
2226 this->opd_shndx_
= (s
- pshdrs
) / shdr_size
;
2227 this->opd_address_
= shdr
.get_sh_addr();
2228 opd_size
= convert_to_section_size_type(shdr
.get_sh_size());
2229 opd
= this->get_view(shdr
.get_sh_offset(), opd_size
,
2235 // Build set of executable sections.
2236 // Using a set is probably overkill. There is likely to be only
2237 // a few executable sections, typically .init, .text and .fini,
2238 // and they are generally grouped together.
2239 typedef std::set
<Sec_info
> Exec_sections
;
2240 Exec_sections exec_sections
;
2242 for (unsigned int i
= 1; i
< this->shnum(); ++i
, s
+= shdr_size
)
2244 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2245 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2246 && ((shdr
.get_sh_flags()
2247 & (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2248 == (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2249 && shdr
.get_sh_size() != 0)
2251 exec_sections
.insert(Sec_info(shdr
.get_sh_addr(),
2252 shdr
.get_sh_size(), i
));
2255 if (exec_sections
.empty())
2258 // Look over the OPD entries. This is complicated by the fact
2259 // that some binaries will use two-word entries while others
2260 // will use the standard three-word entries. In most cases
2261 // the third word (the environment pointer for languages like
2262 // Pascal) is unused and will be zero. If the third word is
2263 // used it should not be pointing into executable sections,
2265 this->init_opd(opd_size
);
2266 for (const unsigned char* p
= opd
; p
< opd
+ opd_size
; p
+= 8)
2268 typedef typename
elfcpp::Swap
<64, big_endian
>::Valtype Valtype
;
2269 const Valtype
* valp
= reinterpret_cast<const Valtype
*>(p
);
2270 Valtype val
= elfcpp::Swap
<64, big_endian
>::readval(valp
);
2272 // Chances are that this is the third word of an OPD entry.
2274 typename
Exec_sections::const_iterator e
2275 = exec_sections
.upper_bound(Sec_info(val
, 0, 0));
2276 if (e
!= exec_sections
.begin())
2279 if (e
->start
<= val
&& val
< e
->start
+ e
->len
)
2281 // We have an address in an executable section.
2282 // VAL ought to be the function entry, set it up.
2283 this->set_opd_ent(p
- opd
, e
->shndx
, val
);
2284 // Skip second word of OPD entry, the TOC pointer.
2288 // If we didn't match any executable sections, we likely
2289 // have a non-zero third word in the OPD entry.
2294 // Relocate sections.
2296 template<int size
, bool big_endian
>
2298 Powerpc_relobj
<size
, big_endian
>::do_relocate_sections(
2299 const Symbol_table
* symtab
, const Layout
* layout
,
2300 const unsigned char* pshdrs
, Output_file
* of
,
2301 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
)
2303 unsigned int start
= 1;
2305 && this->relatoc_
!= 0
2306 && !parameters
->options().relocatable())
2308 // Relocate .toc first.
2309 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2310 this->relatoc_
, this->relatoc_
);
2311 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2312 1, this->relatoc_
- 1);
2313 start
= this->relatoc_
+ 1;
2315 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2316 start
, this->shnum() - 1);
2319 // Set up some symbols.
2321 template<int size
, bool big_endian
>
2323 Target_powerpc
<size
, big_endian
>::do_define_standard_symbols(
2324 Symbol_table
* symtab
,
2329 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2330 // undefined when scanning relocs (and thus requires
2331 // non-relative dynamic relocs). The proper value will be
2333 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2334 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2336 Target_powerpc
<size
, big_endian
>* target
=
2337 static_cast<Target_powerpc
<size
, big_endian
>*>(
2338 parameters
->sized_target
<size
, big_endian
>());
2339 Output_data_got_powerpc
<size
, big_endian
>* got
2340 = target
->got_section(symtab
, layout
);
2341 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2342 Symbol_table::PREDEFINED
,
2346 elfcpp::STV_HIDDEN
, 0,
2350 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2351 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
2352 if (sdasym
!= NULL
&& sdasym
->is_undefined())
2354 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
2356 = layout
->add_output_section_data(".sdata", 0,
2358 | elfcpp::SHF_WRITE
,
2359 sdata
, ORDER_SMALL_DATA
, false);
2360 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
2361 Symbol_table::PREDEFINED
,
2362 os
, 32768, 0, elfcpp::STT_OBJECT
,
2363 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
2369 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2370 Symbol
*gotsym
= symtab
->lookup(".TOC.", NULL
);
2371 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2373 Target_powerpc
<size
, big_endian
>* target
=
2374 static_cast<Target_powerpc
<size
, big_endian
>*>(
2375 parameters
->sized_target
<size
, big_endian
>());
2376 Output_data_got_powerpc
<size
, big_endian
>* got
2377 = target
->got_section(symtab
, layout
);
2378 symtab
->define_in_output_data(".TOC.", NULL
,
2379 Symbol_table::PREDEFINED
,
2383 elfcpp::STV_HIDDEN
, 0,
2389 // Set up PowerPC target specific relobj.
2391 template<int size
, bool big_endian
>
2393 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
2394 const std::string
& name
,
2395 Input_file
* input_file
,
2396 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
2398 int et
= ehdr
.get_e_type();
2399 // ET_EXEC files are valid input for --just-symbols/-R,
2400 // and we treat them as relocatable objects.
2401 if (et
== elfcpp::ET_REL
2402 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
2404 Powerpc_relobj
<size
, big_endian
>* obj
=
2405 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2409 else if (et
== elfcpp::ET_DYN
)
2411 Powerpc_dynobj
<size
, big_endian
>* obj
=
2412 new Powerpc_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2418 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
2423 template<int size
, bool big_endian
>
2424 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
2427 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
2428 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
2430 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
2431 : Output_data_got
<size
, big_endian
>(),
2432 symtab_(symtab
), layout_(layout
),
2433 header_ent_cnt_(size
== 32 ? 3 : 1),
2434 header_index_(size
== 32 ? 0x2000 : 0)
2437 this->set_addralign(256);
2440 // Override all the Output_data_got methods we use so as to first call
2443 add_global(Symbol
* gsym
, unsigned int got_type
)
2445 this->reserve_ent();
2446 return Output_data_got
<size
, big_endian
>::add_global(gsym
, got_type
);
2450 add_global_plt(Symbol
* gsym
, unsigned int got_type
)
2452 this->reserve_ent();
2453 return Output_data_got
<size
, big_endian
>::add_global_plt(gsym
, got_type
);
2457 add_global_tls(Symbol
* gsym
, unsigned int got_type
)
2458 { return this->add_global_plt(gsym
, got_type
); }
2461 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
2462 Output_data_reloc_generic
* rel_dyn
, unsigned int r_type
)
2464 this->reserve_ent();
2465 Output_data_got
<size
, big_endian
>::
2466 add_global_with_rel(gsym
, got_type
, rel_dyn
, r_type
);
2470 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
2471 Output_data_reloc_generic
* rel_dyn
,
2472 unsigned int r_type_1
, unsigned int r_type_2
)
2474 this->reserve_ent(2);
2475 Output_data_got
<size
, big_endian
>::
2476 add_global_pair_with_rel(gsym
, got_type
, rel_dyn
, r_type_1
, r_type_2
);
2480 add_local(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2482 this->reserve_ent();
2483 return Output_data_got
<size
, big_endian
>::add_local(object
, sym_index
,
2488 add_local_plt(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2490 this->reserve_ent();
2491 return Output_data_got
<size
, big_endian
>::add_local_plt(object
, sym_index
,
2496 add_local_tls(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2497 { return this->add_local_plt(object
, sym_index
, got_type
); }
2500 add_local_tls_pair(Relobj
* object
, unsigned int sym_index
,
2501 unsigned int got_type
,
2502 Output_data_reloc_generic
* rel_dyn
,
2503 unsigned int r_type
)
2505 this->reserve_ent(2);
2506 Output_data_got
<size
, big_endian
>::
2507 add_local_tls_pair(object
, sym_index
, got_type
, rel_dyn
, r_type
);
2511 add_constant(Valtype constant
)
2513 this->reserve_ent();
2514 return Output_data_got
<size
, big_endian
>::add_constant(constant
);
2518 add_constant_pair(Valtype c1
, Valtype c2
)
2520 this->reserve_ent(2);
2521 return Output_data_got
<size
, big_endian
>::add_constant_pair(c1
, c2
);
2524 // Offset of _GLOBAL_OFFSET_TABLE_.
2528 return this->got_offset(this->header_index_
);
2531 // Offset of base used to access the GOT/TOC.
2532 // The got/toc pointer reg will be set to this value.
2534 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
2537 return this->g_o_t();
2539 return (this->output_section()->address()
2540 + object
->toc_base_offset()
2544 // Ensure our GOT has a header.
2546 set_final_data_size()
2548 if (this->header_ent_cnt_
!= 0)
2549 this->make_header();
2550 Output_data_got
<size
, big_endian
>::set_final_data_size();
2553 // First word of GOT header needs some values that are not
2554 // handled by Output_data_got so poke them in here.
2555 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2557 do_write(Output_file
* of
)
2560 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
2561 val
= this->layout_
->dynamic_section()->address();
2563 val
= this->output_section()->address() + 0x8000;
2564 this->replace_constant(this->header_index_
, val
);
2565 Output_data_got
<size
, big_endian
>::do_write(of
);
2570 reserve_ent(unsigned int cnt
= 1)
2572 if (this->header_ent_cnt_
== 0)
2574 if (this->num_entries() + cnt
> this->header_index_
)
2575 this->make_header();
2581 this->header_ent_cnt_
= 0;
2582 this->header_index_
= this->num_entries();
2585 Output_data_got
<size
, big_endian
>::add_constant(0);
2586 Output_data_got
<size
, big_endian
>::add_constant(0);
2587 Output_data_got
<size
, big_endian
>::add_constant(0);
2589 // Define _GLOBAL_OFFSET_TABLE_ at the header
2590 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2593 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
2594 sym
->set_value(this->g_o_t());
2597 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2598 Symbol_table::PREDEFINED
,
2599 this, this->g_o_t(), 0,
2602 elfcpp::STV_HIDDEN
, 0,
2606 Output_data_got
<size
, big_endian
>::add_constant(0);
2609 // Stashed pointers.
2610 Symbol_table
* symtab_
;
2614 unsigned int header_ent_cnt_
;
2615 // GOT header index.
2616 unsigned int header_index_
;
2619 // Get the GOT section, creating it if necessary.
2621 template<int size
, bool big_endian
>
2622 Output_data_got_powerpc
<size
, big_endian
>*
2623 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
2626 if (this->got_
== NULL
)
2628 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2631 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
2633 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2634 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2635 this->got_
, ORDER_DATA
, false);
2641 // Get the dynamic reloc section, creating it if necessary.
2643 template<int size
, bool big_endian
>
2644 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2645 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
2647 if (this->rela_dyn_
== NULL
)
2649 gold_assert(layout
!= NULL
);
2650 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
2651 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
2652 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
2653 ORDER_DYNAMIC_RELOCS
, false);
2655 return this->rela_dyn_
;
2658 // Similarly, but for ifunc symbols get the one for ifunc.
2660 template<int size
, bool big_endian
>
2661 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2662 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Symbol_table
* symtab
,
2667 return this->rela_dyn_section(layout
);
2669 if (this->iplt_
== NULL
)
2670 this->make_iplt_section(symtab
, layout
);
2671 return this->iplt_
->rel_plt();
2677 // Determine the stub group size. The group size is the absolute
2678 // value of the parameter --stub-group-size. If --stub-group-size
2679 // is passed a negative value, we restrict stubs to be always after
2680 // the stubbed branches.
2681 Stub_control(int32_t size
, bool no_size_errors
, bool multi_os
)
2682 : stub_group_size_(abs(size
)), stubs_always_after_branch_(size
< 0),
2683 suppress_size_errors_(no_size_errors
), multi_os_(multi_os
),
2684 state_(NO_GROUP
), group_size_(0), group_start_addr_(0),
2685 owner_(NULL
), output_section_(NULL
)
2689 // Return true iff input section can be handled by current stub
2692 can_add_to_stub_group(Output_section
* o
,
2693 const Output_section::Input_section
* i
,
2696 const Output_section::Input_section
*
2702 { return output_section_
; }
2705 set_output_and_owner(Output_section
* o
,
2706 const Output_section::Input_section
* i
)
2708 this->output_section_
= o
;
2717 // Adding group sections before the stubs.
2718 FINDING_STUB_SECTION
,
2719 // Adding group sections after the stubs.
2723 uint32_t stub_group_size_
;
2724 bool stubs_always_after_branch_
;
2725 bool suppress_size_errors_
;
2726 // True if a stub group can serve multiple output sections.
2729 // Current max size of group. Starts at stub_group_size_ but is
2730 // reduced to stub_group_size_/1024 on seeing a section with
2731 // external conditional branches.
2732 uint32_t group_size_
;
2733 uint64_t group_start_addr_
;
2734 // owner_ and output_section_ specify the section to which stubs are
2735 // attached. The stubs are placed at the end of this section.
2736 const Output_section::Input_section
* owner_
;
2737 Output_section
* output_section_
;
2740 // Return true iff input section can be handled by current stub
2741 // group. Sections are presented to this function in order,
2742 // so the first section is the head of the group.
2745 Stub_control::can_add_to_stub_group(Output_section
* o
,
2746 const Output_section::Input_section
* i
,
2749 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
2751 uint64_t start_addr
= o
->address();
2754 // .init and .fini sections are pasted together to form a single
2755 // function. We can't be adding stubs in the middle of the function.
2756 this_size
= o
->data_size();
2759 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
2760 this_size
= i
->data_size();
2763 uint64_t end_addr
= start_addr
+ this_size
;
2764 uint32_t group_size
= this->stub_group_size_
;
2766 this->group_size_
= group_size
= group_size
>> 10;
2768 if (this_size
> group_size
&& !this->suppress_size_errors_
)
2769 gold_warning(_("%s:%s exceeds group size"),
2770 i
->relobj()->name().c_str(),
2771 i
->relobj()->section_name(i
->shndx()).c_str());
2773 gold_debug(DEBUG_TARGET
, "maybe add%s %s:%s size=%#llx total=%#llx",
2774 has14
? " 14bit" : "",
2775 i
->relobj()->name().c_str(),
2776 i
->relobj()->section_name(i
->shndx()).c_str(),
2777 (long long) this_size
,
2778 (this->state_
== NO_GROUP
2780 : (long long) end_addr
- this->group_start_addr_
));
2782 if (this->state_
== NO_GROUP
)
2784 // Only here on very first use of Stub_control
2786 this->output_section_
= o
;
2787 this->state_
= FINDING_STUB_SECTION
;
2788 this->group_size_
= group_size
;
2789 this->group_start_addr_
= start_addr
;
2792 else if (!this->multi_os_
&& this->output_section_
!= o
)
2794 else if (this->state_
== HAS_STUB_SECTION
)
2796 // Can we add this section, which is after the stubs, to the
2798 if (end_addr
- this->group_start_addr_
<= this->group_size_
)
2801 else if (this->state_
== FINDING_STUB_SECTION
)
2803 if ((whole_sec
&& this->output_section_
== o
)
2804 || end_addr
- this->group_start_addr_
<= this->group_size_
)
2806 // Stubs are added at the end of "owner_".
2808 this->output_section_
= o
;
2811 // The group before the stubs has reached maximum size.
2812 // Now see about adding sections after the stubs to the
2813 // group. If the current section has a 14-bit branch and
2814 // the group before the stubs exceeds group_size_ (because
2815 // they didn't have 14-bit branches), don't add sections
2816 // after the stubs: The size of stubs for such a large
2817 // group may exceed the reach of a 14-bit branch.
2818 if (!this->stubs_always_after_branch_
2819 && this_size
<= this->group_size_
2820 && start_addr
- this->group_start_addr_
<= this->group_size_
)
2822 gold_debug(DEBUG_TARGET
, "adding after stubs");
2823 this->state_
= HAS_STUB_SECTION
;
2824 this->group_start_addr_
= start_addr
;
2831 gold_debug(DEBUG_TARGET
,
2832 !this->multi_os_
&& this->output_section_
!= o
2833 ? "nope, new output section\n"
2834 : "nope, didn't fit\n");
2836 // The section fails to fit in the current group. Set up a few
2837 // things for the next group. owner_ and output_section_ will be
2838 // set later after we've retrieved those values for the current
2840 this->state_
= FINDING_STUB_SECTION
;
2841 this->group_size_
= group_size
;
2842 this->group_start_addr_
= start_addr
;
2846 // Look over all the input sections, deciding where to place stubs.
2848 template<int size
, bool big_endian
>
2850 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
2852 bool no_size_errors
)
2854 Stub_control
stub_control(this->stub_group_size_
, no_size_errors
,
2855 parameters
->options().stub_group_multi());
2857 // Group input sections and insert stub table
2858 Stub_table_owner
* table_owner
= NULL
;
2859 std::vector
<Stub_table_owner
*> tables
;
2860 Layout::Section_list section_list
;
2861 layout
->get_executable_sections(§ion_list
);
2862 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
2863 for (Layout::Section_list::iterator o
= section_list
.begin();
2864 o
!= section_list
.end();
2867 typedef Output_section::Input_section_list Input_section_list
;
2868 for (Input_section_list::const_iterator i
2869 = (*o
)->input_sections().begin();
2870 i
!= (*o
)->input_sections().end();
2873 if (i
->is_input_section()
2874 || i
->is_relaxed_input_section())
2876 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2877 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2878 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
2879 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
2881 table_owner
->output_section
= stub_control
.output_section();
2882 table_owner
->owner
= stub_control
.owner();
2883 stub_control
.set_output_and_owner(*o
, &*i
);
2886 if (table_owner
== NULL
)
2888 table_owner
= new Stub_table_owner
;
2889 tables
.push_back(table_owner
);
2891 ppcobj
->set_stub_table(i
->shndx(), tables
.size() - 1);
2895 if (table_owner
!= NULL
)
2897 table_owner
->output_section
= stub_control
.output_section();
2898 table_owner
->owner
= stub_control
.owner();;
2900 for (typename
std::vector
<Stub_table_owner
*>::iterator t
= tables
.begin();
2904 Stub_table
<size
, big_endian
>* stub_table
;
2906 if ((*t
)->owner
->is_input_section())
2907 stub_table
= new Stub_table
<size
, big_endian
>(this,
2908 (*t
)->output_section
,
2910 this->stub_tables_
.size());
2911 else if ((*t
)->owner
->is_relaxed_input_section())
2912 stub_table
= static_cast<Stub_table
<size
, big_endian
>*>(
2913 (*t
)->owner
->relaxed_input_section());
2916 this->stub_tables_
.push_back(stub_table
);
2921 static unsigned long
2922 max_branch_delta (unsigned int r_type
)
2924 if (r_type
== elfcpp::R_POWERPC_REL14
2925 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
2926 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
2928 if (r_type
== elfcpp::R_POWERPC_REL24
2929 || r_type
== elfcpp::R_PPC_PLTREL24
2930 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
2935 // Return whether this branch is going via a plt call stub.
2937 template<int size
, bool big_endian
>
2939 Target_powerpc
<size
, big_endian
>::Branch_info::mark_pltcall(
2940 Powerpc_relobj
<size
, big_endian
>* ppc_object
,
2943 Target_powerpc
* target
,
2944 Symbol_table
* symtab
)
2946 if (this->object_
!= ppc_object
2947 || this->shndx_
!= shndx
2948 || this->offset_
!= offset
)
2951 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
2952 if (sym
!= NULL
&& sym
->is_forwarder())
2953 sym
= symtab
->resolve_forwards(sym
);
2954 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
2956 ? gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
2957 : this->object_
->local_has_plt_offset(this->r_sym_
))
2965 // If this branch needs a plt call stub, or a long branch stub, make one.
2967 template<int size
, bool big_endian
>
2969 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
2970 Stub_table
<size
, big_endian
>* stub_table
,
2971 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
2972 Symbol_table
* symtab
) const
2974 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
2975 if (sym
!= NULL
&& sym
->is_forwarder())
2976 sym
= symtab
->resolve_forwards(sym
);
2977 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
2978 Target_powerpc
<size
, big_endian
>* target
=
2979 static_cast<Target_powerpc
<size
, big_endian
>*>(
2980 parameters
->sized_target
<size
, big_endian
>());
2984 ? gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
2985 : this->object_
->local_has_plt_offset(this->r_sym_
))
2989 && target
->abiversion() >= 2
2990 && !parameters
->options().output_is_position_independent()
2991 && !is_branch_reloc(this->r_type_
))
2992 target
->glink_section()->add_global_entry(gsym
);
2995 if (stub_table
== NULL
)
2996 stub_table
= this->object_
->stub_table(this->shndx_
);
2997 if (stub_table
== NULL
)
2999 // This is a ref from a data section to an ifunc symbol.
3000 stub_table
= ifunc_stub_table
;
3002 gold_assert(stub_table
!= NULL
);
3003 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
3004 if (from
!= invalid_address
)
3005 from
+= (this->object_
->output_section(this->shndx_
)->address()
3008 ok
= stub_table
->add_plt_call_entry(from
,
3009 this->object_
, gsym
,
3010 this->r_type_
, this->addend_
,
3013 ok
= stub_table
->add_plt_call_entry(from
,
3014 this->object_
, this->r_sym_
,
3015 this->r_type_
, this->addend_
,
3021 Address max_branch_offset
= max_branch_delta(this->r_type_
);
3022 if (max_branch_offset
== 0)
3024 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
3025 gold_assert(from
!= invalid_address
);
3026 from
+= (this->object_
->output_section(this->shndx_
)->address()
3031 switch (gsym
->source())
3033 case Symbol::FROM_OBJECT
:
3035 Object
* symobj
= gsym
->object();
3036 if (symobj
->is_dynamic()
3037 || symobj
->pluginobj() != NULL
)
3040 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
3041 if (shndx
== elfcpp::SHN_UNDEF
)
3046 case Symbol::IS_UNDEFINED
:
3052 Symbol_table::Compute_final_value_status status
;
3053 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
3054 if (status
!= Symbol_table::CFVS_OK
)
3057 to
+= this->object_
->ppc64_local_entry_offset(gsym
);
3061 const Symbol_value
<size
>* psymval
3062 = this->object_
->local_symbol(this->r_sym_
);
3063 Symbol_value
<size
> symval
;
3064 if (psymval
->is_section_symbol())
3065 symval
.set_is_section_symbol();
3066 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
3067 typename
ObjType::Compute_final_local_value_status status
3068 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
3070 if (status
!= ObjType::CFLV_OK
3071 || !symval
.has_output_value())
3073 to
= symval
.value(this->object_
, 0);
3075 to
+= this->object_
->ppc64_local_entry_offset(this->r_sym_
);
3077 if (!(size
== 32 && this->r_type_
== elfcpp::R_PPC_PLTREL24
))
3078 to
+= this->addend_
;
3079 if (stub_table
== NULL
)
3080 stub_table
= this->object_
->stub_table(this->shndx_
);
3081 if (size
== 64 && target
->abiversion() < 2)
3083 unsigned int dest_shndx
;
3084 if (!target
->symval_for_branch(symtab
, gsym
, this->object_
,
3088 Address delta
= to
- from
;
3089 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
3091 if (stub_table
== NULL
)
3093 gold_warning(_("%s:%s: branch in non-executable section,"
3094 " no long branch stub for you"),
3095 this->object_
->name().c_str(),
3096 this->object_
->section_name(this->shndx_
).c_str());
3099 bool save_res
= (size
== 64
3101 && gsym
->source() == Symbol::IN_OUTPUT_DATA
3102 && gsym
->output_data() == target
->savres_section());
3103 ok
= stub_table
->add_long_branch_entry(this->object_
,
3105 from
, to
, save_res
);
3109 gold_debug(DEBUG_TARGET
,
3110 "branch at %s:%s+%#lx\n"
3111 "can't reach stub attached to %s:%s",
3112 this->object_
->name().c_str(),
3113 this->object_
->section_name(this->shndx_
).c_str(),
3114 (unsigned long) this->offset_
,
3115 stub_table
->relobj()->name().c_str(),
3116 stub_table
->relobj()->section_name(stub_table
->shndx()).c_str());
3121 // Relaxation hook. This is where we do stub generation.
3123 template<int size
, bool big_endian
>
3125 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
3126 const Input_objects
*,
3127 Symbol_table
* symtab
,
3131 unsigned int prev_brlt_size
= 0;
3135 = this->abiversion() < 2 && parameters
->options().plt_thread_safe();
3137 && this->abiversion() < 2
3139 && !parameters
->options().user_set_plt_thread_safe())
3141 static const char* const thread_starter
[] =
3145 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3147 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3148 "mq_notify", "create_timer",
3153 "GOMP_parallel_start",
3154 "GOMP_parallel_loop_static",
3155 "GOMP_parallel_loop_static_start",
3156 "GOMP_parallel_loop_dynamic",
3157 "GOMP_parallel_loop_dynamic_start",
3158 "GOMP_parallel_loop_guided",
3159 "GOMP_parallel_loop_guided_start",
3160 "GOMP_parallel_loop_runtime",
3161 "GOMP_parallel_loop_runtime_start",
3162 "GOMP_parallel_sections",
3163 "GOMP_parallel_sections_start",
3168 if (parameters
->options().shared())
3172 for (unsigned int i
= 0;
3173 i
< sizeof(thread_starter
) / sizeof(thread_starter
[0]);
3176 Symbol
* sym
= symtab
->lookup(thread_starter
[i
], NULL
);
3177 thread_safe
= (sym
!= NULL
3179 && sym
->in_real_elf());
3185 this->plt_thread_safe_
= thread_safe
;
3190 this->stub_group_size_
= parameters
->options().stub_group_size();
3191 bool no_size_errors
= true;
3192 if (this->stub_group_size_
== 1)
3193 this->stub_group_size_
= 0x1c00000;
3194 else if (this->stub_group_size_
== -1)
3195 this->stub_group_size_
= -0x1e00000;
3197 no_size_errors
= false;
3198 this->group_sections(layout
, task
, no_size_errors
);
3200 else if (this->relax_failed_
&& this->relax_fail_count_
< 3)
3202 this->branch_lookup_table_
.clear();
3203 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3204 p
!= this->stub_tables_
.end();
3207 (*p
)->clear_stubs(true);
3209 this->stub_tables_
.clear();
3210 this->stub_group_size_
= this->stub_group_size_
/ 4 * 3;
3211 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3212 program_name
, this->stub_group_size_
);
3213 this->group_sections(layout
, task
, true);
3216 // We need address of stub tables valid for make_stub.
3217 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3218 p
!= this->stub_tables_
.end();
3221 const Powerpc_relobj
<size
, big_endian
>* object
3222 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
3223 Address off
= object
->get_output_section_offset((*p
)->shndx());
3224 gold_assert(off
!= invalid_address
);
3225 Output_section
* os
= (*p
)->output_section();
3226 (*p
)->set_address_and_size(os
, off
);
3231 // Clear plt call stubs, long branch stubs and branch lookup table.
3232 prev_brlt_size
= this->branch_lookup_table_
.size();
3233 this->branch_lookup_table_
.clear();
3234 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3235 p
!= this->stub_tables_
.end();
3238 (*p
)->clear_stubs(false);
3242 // Build all the stubs.
3243 this->relax_failed_
= false;
3244 Stub_table
<size
, big_endian
>* ifunc_stub_table
3245 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
3246 Stub_table
<size
, big_endian
>* one_stub_table
3247 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
3248 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
3249 b
!= this->branch_info_
.end();
3252 if (!b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
)
3253 && !this->relax_failed_
)
3255 this->relax_failed_
= true;
3256 this->relax_fail_count_
++;
3257 if (this->relax_fail_count_
< 3)
3262 // Did anything change size?
3263 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
3264 bool again
= num_huge_branches
!= prev_brlt_size
;
3265 if (size
== 64 && num_huge_branches
!= 0)
3266 this->make_brlt_section(layout
);
3267 if (size
== 64 && again
)
3268 this->brlt_section_
->set_current_size(num_huge_branches
);
3270 typedef Unordered_set
<Output_section
*> Output_sections
;
3271 Output_sections os_need_update
;
3272 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3273 p
!= this->stub_tables_
.end();
3276 if ((*p
)->size_update())
3279 (*p
)->add_eh_frame(layout
);
3280 os_need_update
.insert((*p
)->output_section());
3284 // Set output section offsets for all input sections in an output
3285 // section that just changed size. Anything past the stubs will
3287 for (typename
Output_sections::iterator p
= os_need_update
.begin();
3288 p
!= os_need_update
.end();
3291 Output_section
* os
= *p
;
3293 typedef Output_section::Input_section_list Input_section_list
;
3294 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
3295 i
!= os
->input_sections().end();
3298 off
= align_address(off
, i
->addralign());
3299 if (i
->is_input_section() || i
->is_relaxed_input_section())
3300 i
->relobj()->set_section_offset(i
->shndx(), off
);
3301 if (i
->is_relaxed_input_section())
3303 Stub_table
<size
, big_endian
>* stub_table
3304 = static_cast<Stub_table
<size
, big_endian
>*>(
3305 i
->relaxed_input_section());
3306 Address stub_table_size
= stub_table
->set_address_and_size(os
, off
);
3307 off
+= stub_table_size
;
3308 // After a few iterations, set current stub table size
3309 // as min size threshold, so later stub tables can only
3312 stub_table
->set_min_size_threshold(stub_table_size
);
3315 off
+= i
->data_size();
3317 // If .branch_lt is part of this output section, then we have
3318 // just done the offset adjustment.
3319 os
->clear_section_offsets_need_adjustment();
3324 && num_huge_branches
!= 0
3325 && parameters
->options().output_is_position_independent())
3327 // Fill in the BRLT relocs.
3328 this->brlt_section_
->reset_brlt_sizes();
3329 for (typename
Branch_lookup_table::const_iterator p
3330 = this->branch_lookup_table_
.begin();
3331 p
!= this->branch_lookup_table_
.end();
3334 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
3336 this->brlt_section_
->finalize_brlt_sizes();
3340 && (parameters
->options().user_set_emit_stub_syms()
3341 ? parameters
->options().emit_stub_syms()
3343 || parameters
->options().output_is_position_independent()
3344 || parameters
->options().emit_relocs())))
3346 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3347 p
!= this->stub_tables_
.end();
3349 (*p
)->define_stub_syms(symtab
);
3351 if (this->glink_
!= NULL
)
3353 int stub_size
= this->glink_
->pltresolve_size
;
3354 Address value
= -stub_size
;
3360 this->define_local(symtab
, "__glink_PLTresolve",
3361 this->glink_
, value
, stub_size
);
3364 this->define_local(symtab
, "__glink", this->glink_
, 0, 0);
3371 template<int size
, bool big_endian
>
3373 Target_powerpc
<size
, big_endian
>::do_plt_fde_location(const Output_data
* plt
,
3374 unsigned char* oview
,
3378 uint64_t address
= plt
->address();
3379 off_t len
= plt
->data_size();
3381 if (plt
== this->glink_
)
3383 // See Output_data_glink::do_write() for glink contents.
3386 gold_assert(parameters
->doing_static_link());
3387 // Static linking may need stubs, to support ifunc and long
3388 // branches. We need to create an output section for
3389 // .eh_frame early in the link process, to have a place to
3390 // attach stub .eh_frame info. We also need to have
3391 // registered a CIE that matches the stub CIE. Both of
3392 // these requirements are satisfied by creating an FDE and
3393 // CIE for .glink, even though static linking will leave
3394 // .glink zero length.
3395 // ??? Hopefully generating an FDE with a zero address range
3396 // won't confuse anything that consumes .eh_frame info.
3398 else if (size
== 64)
3400 // There is one word before __glink_PLTresolve
3404 else if (parameters
->options().output_is_position_independent())
3406 // There are two FDEs for a position independent glink.
3407 // The first covers the branch table, the second
3408 // __glink_PLTresolve at the end of glink.
3409 off_t resolve_size
= this->glink_
->pltresolve_size
;
3410 if (oview
[9] == elfcpp::DW_CFA_nop
)
3411 len
-= resolve_size
;
3414 address
+= len
- resolve_size
;
3421 // Must be a stub table.
3422 const Stub_table
<size
, big_endian
>* stub_table
3423 = static_cast<const Stub_table
<size
, big_endian
>*>(plt
);
3424 uint64_t stub_address
= stub_table
->stub_address();
3425 len
-= stub_address
- address
;
3426 address
= stub_address
;
3429 *paddress
= address
;
3433 // A class to handle the PLT data.
3435 template<int size
, bool big_endian
>
3436 class Output_data_plt_powerpc
: public Output_section_data_build
3439 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3440 size
, big_endian
> Reloc_section
;
3442 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3443 Reloc_section
* plt_rel
,
3445 : Output_section_data_build(size
== 32 ? 4 : 8),
3451 // Add an entry to the PLT.
3456 add_ifunc_entry(Symbol
*);
3459 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
3461 // Return the .rela.plt section data.
3468 // Return the number of PLT entries.
3472 if (this->current_data_size() == 0)
3474 return ((this->current_data_size() - this->first_plt_entry_offset())
3475 / this->plt_entry_size());
3480 do_adjust_output_section(Output_section
* os
)
3485 // Write to a map file.
3487 do_print_to_mapfile(Mapfile
* mapfile
) const
3488 { mapfile
->print_output_data(this, this->name_
); }
3491 // Return the offset of the first non-reserved PLT entry.
3493 first_plt_entry_offset() const
3495 // IPLT has no reserved entry.
3496 if (this->name_
[3] == 'I')
3498 return this->targ_
->first_plt_entry_offset();
3501 // Return the size of each PLT entry.
3503 plt_entry_size() const
3505 return this->targ_
->plt_entry_size();
3508 // Write out the PLT data.
3510 do_write(Output_file
*);
3512 // The reloc section.
3513 Reloc_section
* rel_
;
3514 // Allows access to .glink for do_write.
3515 Target_powerpc
<size
, big_endian
>* targ_
;
3516 // What to report in map file.
3520 // Add an entry to the PLT.
3522 template<int size
, bool big_endian
>
3524 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
3526 if (!gsym
->has_plt_offset())
3528 section_size_type off
= this->current_data_size();
3530 off
+= this->first_plt_entry_offset();
3531 gsym
->set_plt_offset(off
);
3532 gsym
->set_needs_dynsym_entry();
3533 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
3534 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
3535 off
+= this->plt_entry_size();
3536 this->set_current_data_size(off
);
3540 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3542 template<int size
, bool big_endian
>
3544 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
3546 if (!gsym
->has_plt_offset())
3548 section_size_type off
= this->current_data_size();
3549 gsym
->set_plt_offset(off
);
3550 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3551 if (size
== 64 && this->targ_
->abiversion() < 2)
3552 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3553 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
3554 off
+= this->plt_entry_size();
3555 this->set_current_data_size(off
);
3559 // Add an entry for a local ifunc symbol to the IPLT.
3561 template<int size
, bool big_endian
>
3563 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
3564 Sized_relobj_file
<size
, big_endian
>* relobj
,
3565 unsigned int local_sym_index
)
3567 if (!relobj
->local_has_plt_offset(local_sym_index
))
3569 section_size_type off
= this->current_data_size();
3570 relobj
->set_local_plt_offset(local_sym_index
, off
);
3571 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3572 if (size
== 64 && this->targ_
->abiversion() < 2)
3573 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3574 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
3576 off
+= this->plt_entry_size();
3577 this->set_current_data_size(off
);
3581 static const uint32_t add_0_11_11
= 0x7c0b5a14;
3582 static const uint32_t add_2_2_11
= 0x7c425a14;
3583 static const uint32_t add_2_2_12
= 0x7c426214;
3584 static const uint32_t add_3_3_2
= 0x7c631214;
3585 static const uint32_t add_3_3_13
= 0x7c636a14;
3586 static const uint32_t add_11_0_11
= 0x7d605a14;
3587 static const uint32_t add_11_2_11
= 0x7d625a14;
3588 static const uint32_t add_11_11_2
= 0x7d6b1214;
3589 static const uint32_t addi_0_12
= 0x380c0000;
3590 static const uint32_t addi_2_2
= 0x38420000;
3591 static const uint32_t addi_3_3
= 0x38630000;
3592 static const uint32_t addi_11_11
= 0x396b0000;
3593 static const uint32_t addi_12_1
= 0x39810000;
3594 static const uint32_t addi_12_12
= 0x398c0000;
3595 static const uint32_t addis_0_2
= 0x3c020000;
3596 static const uint32_t addis_0_13
= 0x3c0d0000;
3597 static const uint32_t addis_2_12
= 0x3c4c0000;
3598 static const uint32_t addis_11_2
= 0x3d620000;
3599 static const uint32_t addis_11_11
= 0x3d6b0000;
3600 static const uint32_t addis_11_30
= 0x3d7e0000;
3601 static const uint32_t addis_12_1
= 0x3d810000;
3602 static const uint32_t addis_12_2
= 0x3d820000;
3603 static const uint32_t addis_12_12
= 0x3d8c0000;
3604 static const uint32_t b
= 0x48000000;
3605 static const uint32_t bcl_20_31
= 0x429f0005;
3606 static const uint32_t bctr
= 0x4e800420;
3607 static const uint32_t blr
= 0x4e800020;
3608 static const uint32_t bnectr_p4
= 0x4ce20420;
3609 static const uint32_t cmpld_7_12_0
= 0x7fac0040;
3610 static const uint32_t cmpldi_2_0
= 0x28220000;
3611 static const uint32_t cror_15_15_15
= 0x4def7b82;
3612 static const uint32_t cror_31_31_31
= 0x4ffffb82;
3613 static const uint32_t ld_0_1
= 0xe8010000;
3614 static const uint32_t ld_0_12
= 0xe80c0000;
3615 static const uint32_t ld_2_1
= 0xe8410000;
3616 static const uint32_t ld_2_2
= 0xe8420000;
3617 static const uint32_t ld_2_11
= 0xe84b0000;
3618 static const uint32_t ld_2_12
= 0xe84c0000;
3619 static const uint32_t ld_11_2
= 0xe9620000;
3620 static const uint32_t ld_11_11
= 0xe96b0000;
3621 static const uint32_t ld_12_2
= 0xe9820000;
3622 static const uint32_t ld_12_11
= 0xe98b0000;
3623 static const uint32_t ld_12_12
= 0xe98c0000;
3624 static const uint32_t lfd_0_1
= 0xc8010000;
3625 static const uint32_t li_0_0
= 0x38000000;
3626 static const uint32_t li_12_0
= 0x39800000;
3627 static const uint32_t lis_0
= 0x3c000000;
3628 static const uint32_t lis_2
= 0x3c400000;
3629 static const uint32_t lis_11
= 0x3d600000;
3630 static const uint32_t lis_12
= 0x3d800000;
3631 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
3632 static const uint32_t lwz_0_12
= 0x800c0000;
3633 static const uint32_t lwz_11_11
= 0x816b0000;
3634 static const uint32_t lwz_11_30
= 0x817e0000;
3635 static const uint32_t lwz_12_12
= 0x818c0000;
3636 static const uint32_t lwzu_0_12
= 0x840c0000;
3637 static const uint32_t mflr_0
= 0x7c0802a6;
3638 static const uint32_t mflr_11
= 0x7d6802a6;
3639 static const uint32_t mflr_12
= 0x7d8802a6;
3640 static const uint32_t mtctr_0
= 0x7c0903a6;
3641 static const uint32_t mtctr_11
= 0x7d6903a6;
3642 static const uint32_t mtctr_12
= 0x7d8903a6;
3643 static const uint32_t mtlr_0
= 0x7c0803a6;
3644 static const uint32_t mtlr_12
= 0x7d8803a6;
3645 static const uint32_t nop
= 0x60000000;
3646 static const uint32_t ori_0_0_0
= 0x60000000;
3647 static const uint32_t srdi_0_0_2
= 0x7800f082;
3648 static const uint32_t std_0_1
= 0xf8010000;
3649 static const uint32_t std_0_12
= 0xf80c0000;
3650 static const uint32_t std_2_1
= 0xf8410000;
3651 static const uint32_t stfd_0_1
= 0xd8010000;
3652 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
3653 static const uint32_t sub_11_11_12
= 0x7d6c5850;
3654 static const uint32_t sub_12_12_11
= 0x7d8b6050;
3655 static const uint32_t xor_2_12_12
= 0x7d826278;
3656 static const uint32_t xor_11_12_12
= 0x7d8b6278;
3658 // Write out the PLT.
3660 template<int size
, bool big_endian
>
3662 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3664 if (size
== 32 && this->name_
[3] != 'I')
3666 const section_size_type offset
= this->offset();
3667 const section_size_type oview_size
3668 = convert_to_section_size_type(this->data_size());
3669 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3670 unsigned char* pov
= oview
;
3671 unsigned char* endpov
= oview
+ oview_size
;
3673 // The address of the .glink branch table
3674 const Output_data_glink
<size
, big_endian
>* glink
3675 = this->targ_
->glink_section();
3676 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
3678 while (pov
< endpov
)
3680 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
3685 of
->write_output_view(offset
, oview_size
, oview
);
3689 // Create the PLT section.
3691 template<int size
, bool big_endian
>
3693 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
3696 if (this->plt_
== NULL
)
3698 if (this->got_
== NULL
)
3699 this->got_section(symtab
, layout
);
3701 if (this->glink_
== NULL
)
3702 make_glink_section(layout
);
3704 // Ensure that .rela.dyn always appears before .rela.plt This is
3705 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3706 // needs to include .rela.plt in its range.
3707 this->rela_dyn_section(layout
);
3709 Reloc_section
* plt_rel
= new Reloc_section(false);
3710 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
3711 elfcpp::SHF_ALLOC
, plt_rel
,
3712 ORDER_DYNAMIC_PLT_RELOCS
, false);
3714 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
3716 layout
->add_output_section_data(".plt",
3718 ? elfcpp::SHT_PROGBITS
3719 : elfcpp::SHT_NOBITS
),
3720 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3727 Output_section
* rela_plt_os
= plt_rel
->output_section();
3728 rela_plt_os
->set_info_section(this->plt_
->output_section());
3732 // Create the IPLT section.
3734 template<int size
, bool big_endian
>
3736 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
3739 if (this->iplt_
== NULL
)
3741 this->make_plt_section(symtab
, layout
);
3743 Reloc_section
* iplt_rel
= new Reloc_section(false);
3744 if (this->rela_dyn_
->output_section())
3745 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
3747 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
3749 if (this->plt_
->output_section())
3750 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
3754 // A section for huge long branch addresses, similar to plt section.
3756 template<int size
, bool big_endian
>
3757 class Output_data_brlt_powerpc
: public Output_section_data_build
3760 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3761 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3762 size
, big_endian
> Reloc_section
;
3764 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3765 Reloc_section
* brlt_rel
)
3766 : Output_section_data_build(size
== 32 ? 4 : 8),
3774 this->reset_data_size();
3775 this->rel_
->reset_data_size();
3779 finalize_brlt_sizes()
3781 this->finalize_data_size();
3782 this->rel_
->finalize_data_size();
3785 // Add a reloc for an entry in the BRLT.
3787 add_reloc(Address to
, unsigned int off
)
3788 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
3790 // Update section and reloc section size.
3792 set_current_size(unsigned int num_branches
)
3794 this->reset_address_and_file_offset();
3795 this->set_current_data_size(num_branches
* 16);
3796 this->finalize_data_size();
3797 Output_section
* os
= this->output_section();
3798 os
->set_section_offsets_need_adjustment();
3799 if (this->rel_
!= NULL
)
3801 const unsigned int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
3802 this->rel_
->reset_address_and_file_offset();
3803 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
3804 this->rel_
->finalize_data_size();
3805 Output_section
* os
= this->rel_
->output_section();
3806 os
->set_section_offsets_need_adjustment();
3812 do_adjust_output_section(Output_section
* os
)
3817 // Write to a map file.
3819 do_print_to_mapfile(Mapfile
* mapfile
) const
3820 { mapfile
->print_output_data(this, "** BRLT"); }
3823 // Write out the BRLT data.
3825 do_write(Output_file
*);
3827 // The reloc section.
3828 Reloc_section
* rel_
;
3829 Target_powerpc
<size
, big_endian
>* targ_
;
3832 // Make the branch lookup table section.
3834 template<int size
, bool big_endian
>
3836 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
3838 if (size
== 64 && this->brlt_section_
== NULL
)
3840 Reloc_section
* brlt_rel
= NULL
;
3841 bool is_pic
= parameters
->options().output_is_position_independent();
3844 // When PIC we can't fill in .branch_lt (like .plt it can be
3845 // a bss style section) but must initialise at runtime via
3846 // dynamic relocations.
3847 this->rela_dyn_section(layout
);
3848 brlt_rel
= new Reloc_section(false);
3849 if (this->rela_dyn_
->output_section())
3850 this->rela_dyn_
->output_section()
3851 ->add_output_section_data(brlt_rel
);
3854 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
3855 if (this->plt_
&& is_pic
&& this->plt_
->output_section())
3856 this->plt_
->output_section()
3857 ->add_output_section_data(this->brlt_section_
);
3859 layout
->add_output_section_data(".branch_lt",
3860 (is_pic
? elfcpp::SHT_NOBITS
3861 : elfcpp::SHT_PROGBITS
),
3862 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3863 this->brlt_section_
,
3864 (is_pic
? ORDER_SMALL_BSS
3865 : ORDER_SMALL_DATA
),
3870 // Write out .branch_lt when non-PIC.
3872 template<int size
, bool big_endian
>
3874 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3876 if (size
== 64 && !parameters
->options().output_is_position_independent())
3878 const section_size_type offset
= this->offset();
3879 const section_size_type oview_size
3880 = convert_to_section_size_type(this->data_size());
3881 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3883 this->targ_
->write_branch_lookup_table(oview
);
3884 of
->write_output_view(offset
, oview_size
, oview
);
3888 static inline uint32_t
3894 static inline uint32_t
3900 static inline uint32_t
3903 return hi(a
+ 0x8000);
3909 static const unsigned char eh_frame_cie
[12];
3913 const unsigned char Eh_cie
<size
>::eh_frame_cie
[] =
3916 'z', 'R', 0, // Augmentation string.
3917 4, // Code alignment.
3918 0x80 - size
/ 8 , // Data alignment.
3920 1, // Augmentation size.
3921 (elfcpp::DW_EH_PE_pcrel
3922 | elfcpp::DW_EH_PE_sdata4
), // FDE encoding.
3923 elfcpp::DW_CFA_def_cfa
, 1, 0 // def_cfa: r1 offset 0.
3926 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3927 static const unsigned char glink_eh_frame_fde_64v1
[] =
3929 0, 0, 0, 0, // Replaced with offset to .glink.
3930 0, 0, 0, 0, // Replaced with size of .glink.
3931 0, // Augmentation size.
3932 elfcpp::DW_CFA_advance_loc
+ 1,
3933 elfcpp::DW_CFA_register
, 65, 12,
3934 elfcpp::DW_CFA_advance_loc
+ 4,
3935 elfcpp::DW_CFA_restore_extended
, 65
3938 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
3939 static const unsigned char glink_eh_frame_fde_64v2
[] =
3941 0, 0, 0, 0, // Replaced with offset to .glink.
3942 0, 0, 0, 0, // Replaced with size of .glink.
3943 0, // Augmentation size.
3944 elfcpp::DW_CFA_advance_loc
+ 1,
3945 elfcpp::DW_CFA_register
, 65, 0,
3946 elfcpp::DW_CFA_advance_loc
+ 4,
3947 elfcpp::DW_CFA_restore_extended
, 65
3950 // Describe __glink_PLTresolve use of LR, 32-bit version.
3951 static const unsigned char glink_eh_frame_fde_32
[] =
3953 0, 0, 0, 0, // Replaced with offset to .glink.
3954 0, 0, 0, 0, // Replaced with size of .glink.
3955 0, // Augmentation size.
3956 elfcpp::DW_CFA_advance_loc
+ 2,
3957 elfcpp::DW_CFA_register
, 65, 0,
3958 elfcpp::DW_CFA_advance_loc
+ 4,
3959 elfcpp::DW_CFA_restore_extended
, 65
3962 static const unsigned char default_fde
[] =
3964 0, 0, 0, 0, // Replaced with offset to stubs.
3965 0, 0, 0, 0, // Replaced with size of stubs.
3966 0, // Augmentation size.
3967 elfcpp::DW_CFA_nop
, // Pad.
3972 template<bool big_endian
>
3974 write_insn(unsigned char* p
, uint32_t v
)
3976 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
3979 // Stub_table holds information about plt and long branch stubs.
3980 // Stubs are built in an area following some input section determined
3981 // by group_sections(). This input section is converted to a relaxed
3982 // input section allowing it to be resized to accommodate the stubs
3984 template<int size
, bool big_endian
>
3985 class Stub_table
: public Output_relaxed_input_section
3990 Plt_stub_ent(unsigned int off
, unsigned int indx
)
3991 : off_(off
), indx_(indx
), r2save_(0)
3995 unsigned int indx_
: 31;
3996 unsigned int r2save_
: 1;
3998 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3999 static const Address invalid_address
= static_cast<Address
>(0) - 1;
4001 Stub_table(Target_powerpc
<size
, big_endian
>* targ
,
4002 Output_section
* output_section
,
4003 const Output_section::Input_section
* owner
,
4005 : Output_relaxed_input_section(owner
->relobj(), owner
->shndx(),
4007 ->section_addralign(owner
->shndx())),
4008 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
4009 orig_data_size_(owner
->current_data_size()),
4010 plt_size_(0), last_plt_size_(0),
4011 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4012 eh_frame_added_(false), need_save_res_(false), uniq_(id
)
4014 this->set_output_section(output_section
);
4016 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
4017 new_relaxed
.push_back(this);
4018 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
4021 // Add a plt call stub.
4023 add_plt_call_entry(Address
,
4024 const Sized_relobj_file
<size
, big_endian
>*,
4031 add_plt_call_entry(Address
,
4032 const Sized_relobj_file
<size
, big_endian
>*,
4038 // Find a given plt call stub.
4040 find_plt_call_entry(const Symbol
*) const;
4043 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4044 unsigned int) const;
4047 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4053 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4058 // Add a long branch stub.
4060 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
4061 unsigned int, Address
, Address
, bool);
4064 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
4068 can_reach_stub(Address from
, unsigned int off
, unsigned int r_type
)
4070 Address max_branch_offset
= max_branch_delta(r_type
);
4071 if (max_branch_offset
== 0)
4073 gold_assert(from
!= invalid_address
);
4074 Address loc
= off
+ this->stub_address();
4075 return loc
- from
+ max_branch_offset
< 2 * max_branch_offset
;
4079 clear_stubs(bool all
)
4081 this->plt_call_stubs_
.clear();
4082 this->plt_size_
= 0;
4083 this->long_branch_stubs_
.clear();
4084 this->branch_size_
= 0;
4085 this->need_save_res_
= false;
4088 this->last_plt_size_
= 0;
4089 this->last_branch_size_
= 0;
4094 set_address_and_size(const Output_section
* os
, Address off
)
4096 Address start_off
= off
;
4097 off
+= this->orig_data_size_
;
4098 Address my_size
= this->plt_size_
+ this->branch_size_
;
4099 if (this->need_save_res_
)
4100 my_size
+= this->targ_
->savres_section()->data_size();
4102 off
= align_address(off
, this->stub_align());
4103 // Include original section size and alignment padding in size
4104 my_size
+= off
- start_off
;
4105 // Ensure new size is always larger than min size
4106 // threshold. Alignment requirement is included in "my_size", so
4107 // increase "my_size" does not invalidate alignment.
4108 if (my_size
< this->min_size_threshold_
)
4109 my_size
= this->min_size_threshold_
;
4110 this->reset_address_and_file_offset();
4111 this->set_current_data_size(my_size
);
4112 this->set_address_and_file_offset(os
->address() + start_off
,
4113 os
->offset() + start_off
);
4118 stub_address() const
4120 return align_address(this->address() + this->orig_data_size_
,
4121 this->stub_align());
4127 return align_address(this->offset() + this->orig_data_size_
,
4128 this->stub_align());
4133 { return this->plt_size_
; }
4136 set_min_size_threshold(Address min_size
)
4137 { this->min_size_threshold_
= min_size
; }
4140 define_stub_syms(Symbol_table
*);
4145 Output_section
* os
= this->output_section();
4146 if (os
->addralign() < this->stub_align())
4148 os
->set_addralign(this->stub_align());
4149 // FIXME: get rid of the insane checkpointing.
4150 // We can't increase alignment of the input section to which
4151 // stubs are attached; The input section may be .init which
4152 // is pasted together with other .init sections to form a
4153 // function. Aligning might insert zero padding resulting in
4154 // sigill. However we do need to increase alignment of the
4155 // output section so that the align_address() on offset in
4156 // set_address_and_size() adds the same padding as the
4157 // align_address() on address in stub_address().
4158 // What's more, we need this alignment for the layout done in
4159 // relaxation_loop_body() so that the output section starts at
4160 // a suitably aligned address.
4161 os
->checkpoint_set_addralign(this->stub_align());
4163 if (this->last_plt_size_
!= this->plt_size_
4164 || this->last_branch_size_
!= this->branch_size_
)
4166 this->last_plt_size_
= this->plt_size_
;
4167 this->last_branch_size_
= this->branch_size_
;
4173 // Add .eh_frame info for this stub section. Unlike other linker
4174 // generated .eh_frame this is added late in the link, because we
4175 // only want the .eh_frame info if this particular stub section is
4178 add_eh_frame(Layout
* layout
)
4180 if (!this->eh_frame_added_
)
4182 if (!parameters
->options().ld_generated_unwind_info())
4185 // Since we add stub .eh_frame info late, it must be placed
4186 // after all other linker generated .eh_frame info so that
4187 // merge mapping need not be updated for input sections.
4188 // There is no provision to use a different CIE to that used
4190 if (!this->targ_
->has_glink())
4193 layout
->add_eh_frame_for_plt(this,
4194 Eh_cie
<size
>::eh_frame_cie
,
4195 sizeof (Eh_cie
<size
>::eh_frame_cie
),
4197 sizeof (default_fde
));
4198 this->eh_frame_added_
= true;
4202 Target_powerpc
<size
, big_endian
>*
4208 class Plt_stub_key_hash
;
4209 typedef Unordered_map
<Plt_stub_key
, Plt_stub_ent
,
4210 Plt_stub_key_hash
> Plt_stub_entries
;
4211 class Branch_stub_ent
;
4212 class Branch_stub_ent_hash
;
4213 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
4214 Branch_stub_ent_hash
> Branch_stub_entries
;
4216 // Alignment of stub section.
4222 unsigned int min_align
= 32;
4223 unsigned int user_align
= 1 << parameters
->options().plt_align();
4224 return std::max(user_align
, min_align
);
4227 // Return the plt offset for the given call stub.
4229 plt_off(typename
Plt_stub_entries::const_iterator p
, bool* is_iplt
) const
4231 const Symbol
* gsym
= p
->first
.sym_
;
4234 *is_iplt
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
4235 && gsym
->can_use_relative_reloc(false));
4236 return gsym
->plt_offset();
4241 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
4242 unsigned int local_sym_index
= p
->first
.locsym_
;
4243 return relobj
->local_plt_offset(local_sym_index
);
4247 // Size of a given plt call stub.
4249 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
4255 Address plt_addr
= this->plt_off(p
, &is_iplt
);
4257 plt_addr
+= this->targ_
->iplt_section()->address();
4259 plt_addr
+= this->targ_
->plt_section()->address();
4260 Address got_addr
= this->targ_
->got_section()->output_section()->address();
4261 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4262 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
4263 got_addr
+= ppcobj
->toc_base_offset();
4264 Address off
= plt_addr
- got_addr
;
4265 unsigned int bytes
= 4 * 4 + 4 * (ha(off
) != 0);
4266 if (this->targ_
->abiversion() < 2)
4268 bool static_chain
= parameters
->options().plt_static_chain();
4269 bool thread_safe
= this->targ_
->plt_thread_safe();
4273 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
4275 unsigned int align
= 1 << parameters
->options().plt_align();
4277 bytes
= (bytes
+ align
- 1) & -align
;
4281 // Return long branch stub size.
4283 branch_stub_size(typename
Branch_stub_entries::const_iterator p
)
4285 Address loc
= this->stub_address() + this->last_plt_size_
+ p
->second
;
4286 if (p
->first
.dest_
- loc
+ (1 << 25) < 2 << 25)
4288 if (size
== 64 || !parameters
->options().output_is_position_independent())
4295 do_write(Output_file
*);
4297 // Plt call stub keys.
4301 Plt_stub_key(const Symbol
* sym
)
4302 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4305 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4306 unsigned int locsym_index
)
4307 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4310 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4312 unsigned int r_type
,
4314 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4317 this->addend_
= addend
;
4318 else if (parameters
->options().output_is_position_independent()
4319 && r_type
== elfcpp::R_PPC_PLTREL24
)
4321 this->addend_
= addend
;
4322 if (this->addend_
>= 32768)
4323 this->object_
= object
;
4327 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4328 unsigned int locsym_index
,
4329 unsigned int r_type
,
4331 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4334 this->addend_
= addend
;
4335 else if (parameters
->options().output_is_position_independent()
4336 && r_type
== elfcpp::R_PPC_PLTREL24
)
4337 this->addend_
= addend
;
4340 bool operator==(const Plt_stub_key
& that
) const
4342 return (this->sym_
== that
.sym_
4343 && this->object_
== that
.object_
4344 && this->addend_
== that
.addend_
4345 && this->locsym_
== that
.locsym_
);
4349 const Sized_relobj_file
<size
, big_endian
>* object_
;
4350 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
4351 unsigned int locsym_
;
4354 class Plt_stub_key_hash
4357 size_t operator()(const Plt_stub_key
& ent
) const
4359 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
4360 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
4366 // Long branch stub keys.
4367 class Branch_stub_ent
4370 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
,
4371 Address to
, bool save_res
)
4372 : dest_(to
), toc_base_off_(0), save_res_(save_res
)
4375 toc_base_off_
= obj
->toc_base_offset();
4378 bool operator==(const Branch_stub_ent
& that
) const
4380 return (this->dest_
== that
.dest_
4382 || this->toc_base_off_
== that
.toc_base_off_
));
4386 unsigned int toc_base_off_
;
4390 class Branch_stub_ent_hash
4393 size_t operator()(const Branch_stub_ent
& ent
) const
4394 { return ent
.dest_
^ ent
.toc_base_off_
; }
4397 // In a sane world this would be a global.
4398 Target_powerpc
<size
, big_endian
>* targ_
;
4399 // Map sym/object/addend to stub offset.
4400 Plt_stub_entries plt_call_stubs_
;
4401 // Map destination address to stub offset.
4402 Branch_stub_entries long_branch_stubs_
;
4403 // size of input section
4404 section_size_type orig_data_size_
;
4406 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
4407 // Some rare cases cause (PR/20529) fluctuation in stub table
4408 // size, which leads to an endless relax loop. This is to be fixed
4409 // by, after the first few iterations, allowing only increase of
4410 // stub table size. This variable sets the minimal possible size of
4411 // a stub table, it is zero for the first few iterations, then
4412 // increases monotonically.
4413 Address min_size_threshold_
;
4414 // Whether .eh_frame info has been created for this stub section.
4415 bool eh_frame_added_
;
4416 // Set if this stub group needs a copy of out-of-line register
4417 // save/restore functions.
4418 bool need_save_res_
;
4419 // Per stub table unique identifier.
4423 // Add a plt call stub, if we do not already have one for this
4424 // sym/object/addend combo.
4426 template<int size
, bool big_endian
>
4428 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4430 const Sized_relobj_file
<size
, big_endian
>* object
,
4432 unsigned int r_type
,
4436 Plt_stub_key
key(object
, gsym
, r_type
, addend
);
4437 Plt_stub_ent
ent(this->plt_size_
, this->plt_call_stubs_
.size());
4438 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4439 = this->plt_call_stubs_
.insert(std::make_pair(key
, ent
));
4441 this->plt_size_
= ent
.off_
+ this->plt_call_size(p
.first
);
4442 if (size
== 64 && !tocsave
)
4443 p
.first
->second
.r2save_
= 1;
4444 return this->can_reach_stub(from
, ent
.off_
, r_type
);
4447 template<int size
, bool big_endian
>
4449 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4451 const Sized_relobj_file
<size
, big_endian
>* object
,
4452 unsigned int locsym_index
,
4453 unsigned int r_type
,
4457 Plt_stub_key
key(object
, locsym_index
, r_type
, addend
);
4458 Plt_stub_ent
ent(this->plt_size_
, this->plt_call_stubs_
.size());
4459 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4460 = this->plt_call_stubs_
.insert(std::make_pair(key
, ent
));
4462 this->plt_size_
= ent
.off_
+ this->plt_call_size(p
.first
);
4463 if (size
== 64 && !tocsave
)
4464 p
.first
->second
.r2save_
= 1;
4465 return this->can_reach_stub(from
, ent
.off_
, r_type
);
4468 // Find a plt call stub.
4470 template<int size
, bool big_endian
>
4471 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4472 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4473 const Sized_relobj_file
<size
, big_endian
>* object
,
4475 unsigned int r_type
,
4476 Address addend
) const
4478 Plt_stub_key
key(object
, gsym
, r_type
, addend
);
4479 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4480 if (p
== this->plt_call_stubs_
.end())
4485 template<int size
, bool big_endian
>
4486 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4487 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
4489 Plt_stub_key
key(gsym
);
4490 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4491 if (p
== this->plt_call_stubs_
.end())
4496 template<int size
, bool big_endian
>
4497 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4498 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4499 const Sized_relobj_file
<size
, big_endian
>* object
,
4500 unsigned int locsym_index
,
4501 unsigned int r_type
,
4502 Address addend
) const
4504 Plt_stub_key
key(object
, locsym_index
, r_type
, addend
);
4505 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4506 if (p
== this->plt_call_stubs_
.end())
4511 template<int size
, bool big_endian
>
4512 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4513 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4514 const Sized_relobj_file
<size
, big_endian
>* object
,
4515 unsigned int locsym_index
) const
4517 Plt_stub_key
key(object
, locsym_index
);
4518 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4519 if (p
== this->plt_call_stubs_
.end())
4524 // Add a long branch stub if we don't already have one to given
4527 template<int size
, bool big_endian
>
4529 Stub_table
<size
, big_endian
>::add_long_branch_entry(
4530 const Powerpc_relobj
<size
, big_endian
>* object
,
4531 unsigned int r_type
,
4536 Branch_stub_ent
ent(object
, to
, save_res
);
4537 Address off
= this->branch_size_
;
4538 std::pair
<typename
Branch_stub_entries::iterator
, bool> p
4539 = this->long_branch_stubs_
.insert(std::make_pair(ent
, off
));
4543 this->need_save_res_
= true;
4546 unsigned int stub_size
= this->branch_stub_size(p
.first
);
4547 this->branch_size_
= off
+ stub_size
;
4548 if (size
== 64 && stub_size
!= 4)
4549 this->targ_
->add_branch_lookup_table(to
);
4552 return this->can_reach_stub(from
, off
, r_type
);
4555 // Find long branch stub offset.
4557 template<int size
, bool big_endian
>
4558 typename Stub_table
<size
, big_endian
>::Address
4559 Stub_table
<size
, big_endian
>::find_long_branch_entry(
4560 const Powerpc_relobj
<size
, big_endian
>* object
,
4563 Branch_stub_ent
ent(object
, to
, false);
4564 typename
Branch_stub_entries::const_iterator p
4565 = this->long_branch_stubs_
.find(ent
);
4566 if (p
== this->long_branch_stubs_
.end())
4567 return invalid_address
;
4568 if (p
->first
.save_res_
)
4569 return to
- this->targ_
->savres_section()->address() + this->branch_size_
;
4573 // A class to handle .glink.
4575 template<int size
, bool big_endian
>
4576 class Output_data_glink
: public Output_section_data
4579 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4580 static const Address invalid_address
= static_cast<Address
>(0) - 1;
4581 static const int pltresolve_size
= 16*4;
4583 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
4584 : Output_section_data(16), targ_(targ
), global_entry_stubs_(),
4585 end_branch_table_(), ge_size_(0)
4589 add_eh_frame(Layout
* layout
);
4592 add_global_entry(const Symbol
*);
4595 find_global_entry(const Symbol
*) const;
4598 global_entry_address() const
4600 gold_assert(this->is_data_size_valid());
4601 unsigned int global_entry_off
= (this->end_branch_table_
+ 15) & -16;
4602 return this->address() + global_entry_off
;
4606 // Write to a map file.
4608 do_print_to_mapfile(Mapfile
* mapfile
) const
4609 { mapfile
->print_output_data(this, _("** glink")); }
4613 set_final_data_size();
4617 do_write(Output_file
*);
4619 // Allows access to .got and .plt for do_write.
4620 Target_powerpc
<size
, big_endian
>* targ_
;
4622 // Map sym to stub offset.
4623 typedef Unordered_map
<const Symbol
*, unsigned int> Global_entry_stub_entries
;
4624 Global_entry_stub_entries global_entry_stubs_
;
4626 unsigned int end_branch_table_
, ge_size_
;
4629 template<int size
, bool big_endian
>
4631 Output_data_glink
<size
, big_endian
>::add_eh_frame(Layout
* layout
)
4633 if (!parameters
->options().ld_generated_unwind_info())
4638 if (this->targ_
->abiversion() < 2)
4639 layout
->add_eh_frame_for_plt(this,
4640 Eh_cie
<64>::eh_frame_cie
,
4641 sizeof (Eh_cie
<64>::eh_frame_cie
),
4642 glink_eh_frame_fde_64v1
,
4643 sizeof (glink_eh_frame_fde_64v1
));
4645 layout
->add_eh_frame_for_plt(this,
4646 Eh_cie
<64>::eh_frame_cie
,
4647 sizeof (Eh_cie
<64>::eh_frame_cie
),
4648 glink_eh_frame_fde_64v2
,
4649 sizeof (glink_eh_frame_fde_64v2
));
4653 // 32-bit .glink can use the default since the CIE return
4654 // address reg, LR, is valid.
4655 layout
->add_eh_frame_for_plt(this,
4656 Eh_cie
<32>::eh_frame_cie
,
4657 sizeof (Eh_cie
<32>::eh_frame_cie
),
4659 sizeof (default_fde
));
4660 // Except where LR is used in a PIC __glink_PLTresolve.
4661 if (parameters
->options().output_is_position_independent())
4662 layout
->add_eh_frame_for_plt(this,
4663 Eh_cie
<32>::eh_frame_cie
,
4664 sizeof (Eh_cie
<32>::eh_frame_cie
),
4665 glink_eh_frame_fde_32
,
4666 sizeof (glink_eh_frame_fde_32
));
4670 template<int size
, bool big_endian
>
4672 Output_data_glink
<size
, big_endian
>::add_global_entry(const Symbol
* gsym
)
4674 std::pair
<typename
Global_entry_stub_entries::iterator
, bool> p
4675 = this->global_entry_stubs_
.insert(std::make_pair(gsym
, this->ge_size_
));
4677 this->ge_size_
+= 16;
4680 template<int size
, bool big_endian
>
4681 typename Output_data_glink
<size
, big_endian
>::Address
4682 Output_data_glink
<size
, big_endian
>::find_global_entry(const Symbol
* gsym
) const
4684 typename
Global_entry_stub_entries::const_iterator p
4685 = this->global_entry_stubs_
.find(gsym
);
4686 return p
== this->global_entry_stubs_
.end() ? invalid_address
: p
->second
;
4689 template<int size
, bool big_endian
>
4691 Output_data_glink
<size
, big_endian
>::set_final_data_size()
4693 unsigned int count
= this->targ_
->plt_entry_count();
4694 section_size_type total
= 0;
4700 // space for branch table
4701 total
+= 4 * (count
- 1);
4703 total
+= -total
& 15;
4704 total
+= this->pltresolve_size
;
4708 total
+= this->pltresolve_size
;
4710 // space for branch table
4712 if (this->targ_
->abiversion() < 2)
4716 total
+= 4 * (count
- 0x8000);
4720 this->end_branch_table_
= total
;
4721 total
= (total
+ 15) & -16;
4722 total
+= this->ge_size_
;
4724 this->set_data_size(total
);
4727 // Define symbols on stubs, identifying the stub.
4729 template<int size
, bool big_endian
>
4731 Stub_table
<size
, big_endian
>::define_stub_syms(Symbol_table
* symtab
)
4733 if (!this->plt_call_stubs_
.empty())
4735 // The key for the plt call stub hash table includes addresses,
4736 // therefore traversal order depends on those addresses, which
4737 // can change between runs if gold is a PIE. Unfortunately the
4738 // output .symtab ordering depends on the order in which symbols
4739 // are added to the linker symtab. We want reproducible output
4740 // so must sort the call stub symbols.
4741 typedef typename
Plt_stub_entries::const_iterator plt_iter
;
4742 std::vector
<plt_iter
> sorted
;
4743 sorted
.resize(this->plt_call_stubs_
.size());
4745 for (plt_iter cs
= this->plt_call_stubs_
.begin();
4746 cs
!= this->plt_call_stubs_
.end();
4748 sorted
[cs
->second
.indx_
] = cs
;
4750 for (unsigned int i
= 0; i
< this->plt_call_stubs_
.size(); ++i
)
4752 plt_iter cs
= sorted
[i
];
4755 if (cs
->first
.addend_
!= 0)
4756 sprintf(add
, "+%x", static_cast<uint32_t>(cs
->first
.addend_
));
4759 if (cs
->first
.object_
)
4761 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4762 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
4763 sprintf(obj
, "%x:", ppcobj
->uniq());
4766 const char *symname
;
4767 if (cs
->first
.sym_
== NULL
)
4769 sprintf(localname
, "%x", cs
->first
.locsym_
);
4770 symname
= localname
;
4773 symname
= cs
->first
.sym_
->name();
4774 char* name
= new char[8 + 10 + strlen(obj
) + strlen(symname
) + strlen(add
) + 1];
4775 sprintf(name
, "%08x.plt_call.%s%s%s", this->uniq_
, obj
, symname
, add
);
4777 = this->stub_address() - this->address() + cs
->second
.off_
;
4778 unsigned int stub_size
= this->plt_call_size(cs
);
4779 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
4783 typedef typename
Branch_stub_entries::const_iterator branch_iter
;
4784 for (branch_iter bs
= this->long_branch_stubs_
.begin();
4785 bs
!= this->long_branch_stubs_
.end();
4788 if (bs
->first
.save_res_
)
4791 char* name
= new char[8 + 13 + 16 + 1];
4792 sprintf(name
, "%08x.long_branch.%llx", this->uniq_
,
4793 static_cast<unsigned long long>(bs
->first
.dest_
));
4794 Address value
= (this->stub_address() - this->address()
4795 + this->plt_size_
+ bs
->second
);
4796 unsigned int stub_size
= this->branch_stub_size(bs
);
4797 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
4801 // Write out plt and long branch stub code.
4803 template<int size
, bool big_endian
>
4805 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
4807 if (this->plt_call_stubs_
.empty()
4808 && this->long_branch_stubs_
.empty())
4811 const section_size_type start_off
= this->offset();
4812 const section_size_type off
= this->stub_offset();
4813 const section_size_type oview_size
=
4814 convert_to_section_size_type(this->data_size() - (off
- start_off
));
4815 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4820 const Output_data_got_powerpc
<size
, big_endian
>* got
4821 = this->targ_
->got_section();
4822 Address got_os_addr
= got
->output_section()->address();
4824 if (!this->plt_call_stubs_
.empty())
4826 // The base address of the .plt section.
4827 Address plt_base
= this->targ_
->plt_section()->address();
4828 Address iplt_base
= invalid_address
;
4830 // Write out plt call stubs.
4831 typename
Plt_stub_entries::const_iterator cs
;
4832 for (cs
= this->plt_call_stubs_
.begin();
4833 cs
!= this->plt_call_stubs_
.end();
4837 Address pltoff
= this->plt_off(cs
, &is_iplt
);
4838 Address plt_addr
= pltoff
;
4841 if (iplt_base
== invalid_address
)
4842 iplt_base
= this->targ_
->iplt_section()->address();
4843 plt_addr
+= iplt_base
;
4846 plt_addr
+= plt_base
;
4847 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4848 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
4849 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
4850 Address off
= plt_addr
- got_addr
;
4852 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
4853 gold_error(_("%s: linkage table error against `%s'"),
4854 cs
->first
.object_
->name().c_str(),
4855 cs
->first
.sym_
->demangled_name().c_str());
4857 bool plt_load_toc
= this->targ_
->abiversion() < 2;
4859 = plt_load_toc
&& parameters
->options().plt_static_chain();
4861 = plt_load_toc
&& this->targ_
->plt_thread_safe();
4862 bool use_fake_dep
= false;
4863 Address cmp_branch_off
= 0;
4866 unsigned int pltindex
4867 = ((pltoff
- this->targ_
->first_plt_entry_offset())
4868 / this->targ_
->plt_entry_size());
4870 = (this->targ_
->glink_section()->pltresolve_size
4872 if (pltindex
> 32768)
4873 glinkoff
+= (pltindex
- 32768) * 4;
4875 = this->targ_
->glink_section()->address() + glinkoff
;
4877 = (this->stub_address() + cs
->second
.off_
+ 20
4878 + 4 * cs
->second
.r2save_
4879 + 4 * (ha(off
) != 0)
4880 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4881 + 4 * static_chain
);
4882 cmp_branch_off
= to
- from
;
4883 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
4886 p
= oview
+ cs
->second
.off_
;
4889 if (cs
->second
.r2save_
)
4891 write_insn
<big_endian
>(p
,
4892 std_2_1
+ this->targ_
->stk_toc());
4897 write_insn
<big_endian
>(p
, addis_11_2
+ ha(off
));
4899 write_insn
<big_endian
>(p
, ld_12_11
+ l(off
));
4904 write_insn
<big_endian
>(p
, addis_12_2
+ ha(off
));
4906 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
));
4910 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4912 write_insn
<big_endian
>(p
, addi_11_11
+ l(off
));
4916 write_insn
<big_endian
>(p
, mtctr_12
);
4922 write_insn
<big_endian
>(p
, xor_2_12_12
);
4924 write_insn
<big_endian
>(p
, add_11_11_2
);
4927 write_insn
<big_endian
>(p
, ld_2_11
+ l(off
+ 8));
4931 write_insn
<big_endian
>(p
, ld_11_11
+ l(off
+ 16));
4938 if (cs
->second
.r2save_
)
4940 write_insn
<big_endian
>(p
,
4941 std_2_1
+ this->targ_
->stk_toc());
4944 write_insn
<big_endian
>(p
, ld_12_2
+ l(off
));
4947 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4949 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
));
4953 write_insn
<big_endian
>(p
, mtctr_12
);
4959 write_insn
<big_endian
>(p
, xor_11_12_12
);
4961 write_insn
<big_endian
>(p
, add_2_2_11
);
4966 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16));
4969 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8));
4973 if (thread_safe
&& !use_fake_dep
)
4975 write_insn
<big_endian
>(p
, cmpldi_2_0
);
4977 write_insn
<big_endian
>(p
, bnectr_p4
);
4979 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
4982 write_insn
<big_endian
>(p
, bctr
);
4986 // Write out long branch stubs.
4987 typename
Branch_stub_entries::const_iterator bs
;
4988 for (bs
= this->long_branch_stubs_
.begin();
4989 bs
!= this->long_branch_stubs_
.end();
4992 if (bs
->first
.save_res_
)
4994 p
= oview
+ this->plt_size_
+ bs
->second
;
4995 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
4996 Address delta
= bs
->first
.dest_
- loc
;
4997 if (delta
+ (1 << 25) < 2 << 25)
4998 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
5002 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
5003 gold_assert(brlt_addr
!= invalid_address
);
5004 brlt_addr
+= this->targ_
->brlt_section()->address();
5005 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
5006 Address brltoff
= brlt_addr
- got_addr
;
5007 if (ha(brltoff
) == 0)
5009 write_insn
<big_endian
>(p
, ld_12_2
+ l(brltoff
)), p
+= 4;
5013 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
5014 write_insn
<big_endian
>(p
, ld_12_12
+ l(brltoff
)), p
+= 4;
5016 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5017 write_insn
<big_endian
>(p
, bctr
);
5023 if (!this->plt_call_stubs_
.empty())
5025 // The base address of the .plt section.
5026 Address plt_base
= this->targ_
->plt_section()->address();
5027 Address iplt_base
= invalid_address
;
5028 // The address of _GLOBAL_OFFSET_TABLE_.
5029 Address g_o_t
= invalid_address
;
5031 // Write out plt call stubs.
5032 typename
Plt_stub_entries::const_iterator cs
;
5033 for (cs
= this->plt_call_stubs_
.begin();
5034 cs
!= this->plt_call_stubs_
.end();
5038 Address plt_addr
= this->plt_off(cs
, &is_iplt
);
5041 if (iplt_base
== invalid_address
)
5042 iplt_base
= this->targ_
->iplt_section()->address();
5043 plt_addr
+= iplt_base
;
5046 plt_addr
+= plt_base
;
5048 p
= oview
+ cs
->second
.off_
;
5049 if (parameters
->options().output_is_position_independent())
5052 const Powerpc_relobj
<size
, big_endian
>* ppcobj
5053 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
5054 (cs
->first
.object_
));
5055 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
5057 unsigned int got2
= ppcobj
->got2_shndx();
5058 got_addr
= ppcobj
->get_output_section_offset(got2
);
5059 gold_assert(got_addr
!= invalid_address
);
5060 got_addr
+= (ppcobj
->output_section(got2
)->address()
5061 + cs
->first
.addend_
);
5065 if (g_o_t
== invalid_address
)
5067 const Output_data_got_powerpc
<size
, big_endian
>* got
5068 = this->targ_
->got_section();
5069 g_o_t
= got
->address() + got
->g_o_t();
5074 Address off
= plt_addr
- got_addr
;
5077 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(off
));
5078 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
5079 write_insn
<big_endian
>(p
+ 8, bctr
);
5083 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(off
));
5084 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(off
));
5085 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
5086 write_insn
<big_endian
>(p
+ 12, bctr
);
5091 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
5092 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
5093 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
5094 write_insn
<big_endian
>(p
+ 12, bctr
);
5099 // Write out long branch stubs.
5100 typename
Branch_stub_entries::const_iterator bs
;
5101 for (bs
= this->long_branch_stubs_
.begin();
5102 bs
!= this->long_branch_stubs_
.end();
5105 if (bs
->first
.save_res_
)
5107 p
= oview
+ this->plt_size_
+ bs
->second
;
5108 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
5109 Address delta
= bs
->first
.dest_
- loc
;
5110 if (delta
+ (1 << 25) < 2 << 25)
5111 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
5112 else if (!parameters
->options().output_is_position_independent())
5114 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(bs
->first
.dest_
));
5115 write_insn
<big_endian
>(p
+ 4, addi_12_12
+ l(bs
->first
.dest_
));
5116 write_insn
<big_endian
>(p
+ 8, mtctr_12
);
5117 write_insn
<big_endian
>(p
+ 12, bctr
);
5122 write_insn
<big_endian
>(p
+ 0, mflr_0
);
5123 write_insn
<big_endian
>(p
+ 4, bcl_20_31
);
5124 write_insn
<big_endian
>(p
+ 8, mflr_12
);
5125 write_insn
<big_endian
>(p
+ 12, addis_12_12
+ ha(delta
));
5126 write_insn
<big_endian
>(p
+ 16, addi_12_12
+ l(delta
));
5127 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
5128 write_insn
<big_endian
>(p
+ 24, mtctr_12
);
5129 write_insn
<big_endian
>(p
+ 28, bctr
);
5133 if (this->need_save_res_
)
5135 p
= oview
+ this->plt_size_
+ this->branch_size_
;
5136 memcpy (p
, this->targ_
->savres_section()->contents(),
5137 this->targ_
->savres_section()->data_size());
5141 // Write out .glink.
5143 template<int size
, bool big_endian
>
5145 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
5147 const section_size_type off
= this->offset();
5148 const section_size_type oview_size
=
5149 convert_to_section_size_type(this->data_size());
5150 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5153 // The base address of the .plt section.
5154 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
5155 Address plt_base
= this->targ_
->plt_section()->address();
5159 if (this->end_branch_table_
!= 0)
5161 // Write pltresolve stub.
5163 Address after_bcl
= this->address() + 16;
5164 Address pltoff
= plt_base
- after_bcl
;
5166 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
5168 if (this->targ_
->abiversion() < 2)
5170 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
5171 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5172 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5173 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5174 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
5175 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5176 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5177 write_insn
<big_endian
>(p
, ld_2_11
+ 8), p
+= 4;
5178 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5179 write_insn
<big_endian
>(p
, ld_11_11
+ 16), p
+= 4;
5183 write_insn
<big_endian
>(p
, mflr_0
), p
+= 4;
5184 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5185 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5186 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5187 write_insn
<big_endian
>(p
, mtlr_0
), p
+= 4;
5188 write_insn
<big_endian
>(p
, sub_12_12_11
), p
+= 4;
5189 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5190 write_insn
<big_endian
>(p
, addi_0_12
+ l(-48)), p
+= 4;
5191 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5192 write_insn
<big_endian
>(p
, srdi_0_0_2
), p
+= 4;
5193 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5194 write_insn
<big_endian
>(p
, ld_11_11
+ 8), p
+= 4;
5196 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
5197 while (p
< oview
+ this->pltresolve_size
)
5198 write_insn
<big_endian
>(p
, nop
), p
+= 4;
5200 // Write lazy link call stubs.
5202 while (p
< oview
+ this->end_branch_table_
)
5204 if (this->targ_
->abiversion() < 2)
5208 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
5212 write_insn
<big_endian
>(p
, lis_0
+ hi(indx
)), p
+= 4;
5213 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
5216 uint32_t branch_off
= 8 - (p
- oview
);
5217 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
5222 Address plt_base
= this->targ_
->plt_section()->address();
5223 Address iplt_base
= invalid_address
;
5224 unsigned int global_entry_off
= (this->end_branch_table_
+ 15) & -16;
5225 Address global_entry_base
= this->address() + global_entry_off
;
5226 typename
Global_entry_stub_entries::const_iterator ge
;
5227 for (ge
= this->global_entry_stubs_
.begin();
5228 ge
!= this->global_entry_stubs_
.end();
5231 p
= oview
+ global_entry_off
+ ge
->second
;
5232 Address plt_addr
= ge
->first
->plt_offset();
5233 if (ge
->first
->type() == elfcpp::STT_GNU_IFUNC
5234 && ge
->first
->can_use_relative_reloc(false))
5236 if (iplt_base
== invalid_address
)
5237 iplt_base
= this->targ_
->iplt_section()->address();
5238 plt_addr
+= iplt_base
;
5241 plt_addr
+= plt_base
;
5242 Address my_addr
= global_entry_base
+ ge
->second
;
5243 Address off
= plt_addr
- my_addr
;
5245 if (off
+ 0x80008000 > 0xffffffff || (off
& 3) != 0)
5246 gold_error(_("%s: linkage table error against `%s'"),
5247 ge
->first
->object()->name().c_str(),
5248 ge
->first
->demangled_name().c_str());
5250 write_insn
<big_endian
>(p
, addis_12_12
+ ha(off
)), p
+= 4;
5251 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
)), p
+= 4;
5252 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5253 write_insn
<big_endian
>(p
, bctr
);
5258 const Output_data_got_powerpc
<size
, big_endian
>* got
5259 = this->targ_
->got_section();
5260 // The address of _GLOBAL_OFFSET_TABLE_.
5261 Address g_o_t
= got
->address() + got
->g_o_t();
5263 // Write out pltresolve branch table.
5265 unsigned int the_end
= oview_size
- this->pltresolve_size
;
5266 unsigned char* end_p
= oview
+ the_end
;
5267 while (p
< end_p
- 8 * 4)
5268 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
5270 write_insn
<big_endian
>(p
, nop
), p
+= 4;
5272 // Write out pltresolve call stub.
5273 if (parameters
->options().output_is_position_independent())
5275 Address res0_off
= 0;
5276 Address after_bcl_off
= the_end
+ 12;
5277 Address bcl_res0
= after_bcl_off
- res0_off
;
5279 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
5280 write_insn
<big_endian
>(p
+ 4, mflr_0
);
5281 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
5282 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
5283 write_insn
<big_endian
>(p
+ 16, mflr_12
);
5284 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
5285 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
5287 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
5289 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
5290 if (ha(got_bcl
) == ha(got_bcl
+ 4))
5292 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
5293 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
5297 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
5298 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
5300 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
5301 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
5302 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
5303 write_insn
<big_endian
>(p
+ 52, bctr
);
5304 write_insn
<big_endian
>(p
+ 56, nop
);
5305 write_insn
<big_endian
>(p
+ 60, nop
);
5309 Address res0
= this->address();
5311 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
5312 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
5313 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5314 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
5316 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
5317 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
5318 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
5319 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
5320 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5321 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
5323 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
5324 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
5325 write_insn
<big_endian
>(p
+ 32, bctr
);
5326 write_insn
<big_endian
>(p
+ 36, nop
);
5327 write_insn
<big_endian
>(p
+ 40, nop
);
5328 write_insn
<big_endian
>(p
+ 44, nop
);
5329 write_insn
<big_endian
>(p
+ 48, nop
);
5330 write_insn
<big_endian
>(p
+ 52, nop
);
5331 write_insn
<big_endian
>(p
+ 56, nop
);
5332 write_insn
<big_endian
>(p
+ 60, nop
);
5337 of
->write_output_view(off
, oview_size
, oview
);
5341 // A class to handle linker generated save/restore functions.
5343 template<int size
, bool big_endian
>
5344 class Output_data_save_res
: public Output_section_data_build
5347 Output_data_save_res(Symbol_table
* symtab
);
5349 const unsigned char*
5356 // Write to a map file.
5358 do_print_to_mapfile(Mapfile
* mapfile
) const
5359 { mapfile
->print_output_data(this, _("** save/restore")); }
5362 do_write(Output_file
*);
5365 // The maximum size of save/restore contents.
5366 static const unsigned int savres_max
= 218*4;
5369 savres_define(Symbol_table
* symtab
,
5371 unsigned int lo
, unsigned int hi
,
5372 unsigned char* write_ent(unsigned char*, int),
5373 unsigned char* write_tail(unsigned char*, int));
5375 unsigned char *contents_
;
5378 template<bool big_endian
>
5379 static unsigned char*
5380 savegpr0(unsigned char* p
, int r
)
5382 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5383 write_insn
<big_endian
>(p
, insn
);
5387 template<bool big_endian
>
5388 static unsigned char*
5389 savegpr0_tail(unsigned char* p
, int r
)
5391 p
= savegpr0
<big_endian
>(p
, r
);
5392 uint32_t insn
= std_0_1
+ 16;
5393 write_insn
<big_endian
>(p
, insn
);
5395 write_insn
<big_endian
>(p
, blr
);
5399 template<bool big_endian
>
5400 static unsigned char*
5401 restgpr0(unsigned char* p
, int r
)
5403 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5404 write_insn
<big_endian
>(p
, insn
);
5408 template<bool big_endian
>
5409 static unsigned char*
5410 restgpr0_tail(unsigned char* p
, int r
)
5412 uint32_t insn
= ld_0_1
+ 16;
5413 write_insn
<big_endian
>(p
, insn
);
5415 p
= restgpr0
<big_endian
>(p
, r
);
5416 write_insn
<big_endian
>(p
, mtlr_0
);
5420 p
= restgpr0
<big_endian
>(p
, 30);
5421 p
= restgpr0
<big_endian
>(p
, 31);
5423 write_insn
<big_endian
>(p
, blr
);
5427 template<bool big_endian
>
5428 static unsigned char*
5429 savegpr1(unsigned char* p
, int r
)
5431 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5432 write_insn
<big_endian
>(p
, insn
);
5436 template<bool big_endian
>
5437 static unsigned char*
5438 savegpr1_tail(unsigned char* p
, int r
)
5440 p
= savegpr1
<big_endian
>(p
, r
);
5441 write_insn
<big_endian
>(p
, blr
);
5445 template<bool big_endian
>
5446 static unsigned char*
5447 restgpr1(unsigned char* p
, int r
)
5449 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5450 write_insn
<big_endian
>(p
, insn
);
5454 template<bool big_endian
>
5455 static unsigned char*
5456 restgpr1_tail(unsigned char* p
, int r
)
5458 p
= restgpr1
<big_endian
>(p
, r
);
5459 write_insn
<big_endian
>(p
, blr
);
5463 template<bool big_endian
>
5464 static unsigned char*
5465 savefpr(unsigned char* p
, int r
)
5467 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5468 write_insn
<big_endian
>(p
, insn
);
5472 template<bool big_endian
>
5473 static unsigned char*
5474 savefpr0_tail(unsigned char* p
, int r
)
5476 p
= savefpr
<big_endian
>(p
, r
);
5477 write_insn
<big_endian
>(p
, std_0_1
+ 16);
5479 write_insn
<big_endian
>(p
, blr
);
5483 template<bool big_endian
>
5484 static unsigned char*
5485 restfpr(unsigned char* p
, int r
)
5487 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5488 write_insn
<big_endian
>(p
, insn
);
5492 template<bool big_endian
>
5493 static unsigned char*
5494 restfpr0_tail(unsigned char* p
, int r
)
5496 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
5498 p
= restfpr
<big_endian
>(p
, r
);
5499 write_insn
<big_endian
>(p
, mtlr_0
);
5503 p
= restfpr
<big_endian
>(p
, 30);
5504 p
= restfpr
<big_endian
>(p
, 31);
5506 write_insn
<big_endian
>(p
, blr
);
5510 template<bool big_endian
>
5511 static unsigned char*
5512 savefpr1_tail(unsigned char* p
, int r
)
5514 p
= savefpr
<big_endian
>(p
, r
);
5515 write_insn
<big_endian
>(p
, blr
);
5519 template<bool big_endian
>
5520 static unsigned char*
5521 restfpr1_tail(unsigned char* p
, int r
)
5523 p
= restfpr
<big_endian
>(p
, r
);
5524 write_insn
<big_endian
>(p
, blr
);
5528 template<bool big_endian
>
5529 static unsigned char*
5530 savevr(unsigned char* p
, int r
)
5532 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5533 write_insn
<big_endian
>(p
, insn
);
5535 insn
= stvx_0_12_0
+ (r
<< 21);
5536 write_insn
<big_endian
>(p
, insn
);
5540 template<bool big_endian
>
5541 static unsigned char*
5542 savevr_tail(unsigned char* p
, int r
)
5544 p
= savevr
<big_endian
>(p
, r
);
5545 write_insn
<big_endian
>(p
, blr
);
5549 template<bool big_endian
>
5550 static unsigned char*
5551 restvr(unsigned char* p
, int r
)
5553 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5554 write_insn
<big_endian
>(p
, insn
);
5556 insn
= lvx_0_12_0
+ (r
<< 21);
5557 write_insn
<big_endian
>(p
, insn
);
5561 template<bool big_endian
>
5562 static unsigned char*
5563 restvr_tail(unsigned char* p
, int r
)
5565 p
= restvr
<big_endian
>(p
, r
);
5566 write_insn
<big_endian
>(p
, blr
);
5571 template<int size
, bool big_endian
>
5572 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
5573 Symbol_table
* symtab
)
5574 : Output_section_data_build(4),
5577 this->savres_define(symtab
,
5578 "_savegpr0_", 14, 31,
5579 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
5580 this->savres_define(symtab
,
5581 "_restgpr0_", 14, 29,
5582 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
5583 this->savres_define(symtab
,
5584 "_restgpr0_", 30, 31,
5585 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
5586 this->savres_define(symtab
,
5587 "_savegpr1_", 14, 31,
5588 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
5589 this->savres_define(symtab
,
5590 "_restgpr1_", 14, 31,
5591 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
5592 this->savres_define(symtab
,
5593 "_savefpr_", 14, 31,
5594 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
5595 this->savres_define(symtab
,
5596 "_restfpr_", 14, 29,
5597 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
5598 this->savres_define(symtab
,
5599 "_restfpr_", 30, 31,
5600 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
5601 this->savres_define(symtab
,
5603 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
5604 this->savres_define(symtab
,
5606 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
5607 this->savres_define(symtab
,
5609 savevr
<big_endian
>, savevr_tail
<big_endian
>);
5610 this->savres_define(symtab
,
5612 restvr
<big_endian
>, restvr_tail
<big_endian
>);
5615 template<int size
, bool big_endian
>
5617 Output_data_save_res
<size
, big_endian
>::savres_define(
5618 Symbol_table
* symtab
,
5620 unsigned int lo
, unsigned int hi
,
5621 unsigned char* write_ent(unsigned char*, int),
5622 unsigned char* write_tail(unsigned char*, int))
5624 size_t len
= strlen(name
);
5625 bool writing
= false;
5628 memcpy(sym
, name
, len
);
5631 for (unsigned int i
= lo
; i
<= hi
; i
++)
5633 sym
[len
+ 0] = i
/ 10 + '0';
5634 sym
[len
+ 1] = i
% 10 + '0';
5635 Symbol
* gsym
= symtab
->lookup(sym
);
5636 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
5637 writing
= writing
|| refd
;
5640 if (this->contents_
== NULL
)
5641 this->contents_
= new unsigned char[this->savres_max
];
5643 section_size_type value
= this->current_data_size();
5644 unsigned char* p
= this->contents_
+ value
;
5646 p
= write_ent(p
, i
);
5648 p
= write_tail(p
, i
);
5649 section_size_type cur_size
= p
- this->contents_
;
5650 this->set_current_data_size(cur_size
);
5652 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
5653 this, value
, cur_size
- value
,
5654 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
5655 elfcpp::STV_HIDDEN
, 0, false, false);
5660 // Write out save/restore.
5662 template<int size
, bool big_endian
>
5664 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
5666 const section_size_type off
= this->offset();
5667 const section_size_type oview_size
=
5668 convert_to_section_size_type(this->data_size());
5669 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5670 memcpy(oview
, this->contents_
, oview_size
);
5671 of
->write_output_view(off
, oview_size
, oview
);
5675 // Create the glink section.
5677 template<int size
, bool big_endian
>
5679 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
5681 if (this->glink_
== NULL
)
5683 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
5684 this->glink_
->add_eh_frame(layout
);
5685 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
5686 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
5687 this->glink_
, ORDER_TEXT
, false);
5691 // Create a PLT entry for a global symbol.
5693 template<int size
, bool big_endian
>
5695 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
5699 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
5700 && gsym
->can_use_relative_reloc(false))
5702 if (this->iplt_
== NULL
)
5703 this->make_iplt_section(symtab
, layout
);
5704 this->iplt_
->add_ifunc_entry(gsym
);
5708 if (this->plt_
== NULL
)
5709 this->make_plt_section(symtab
, layout
);
5710 this->plt_
->add_entry(gsym
);
5714 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5716 template<int size
, bool big_endian
>
5718 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
5719 Symbol_table
* symtab
,
5721 Sized_relobj_file
<size
, big_endian
>* relobj
,
5724 if (this->iplt_
== NULL
)
5725 this->make_iplt_section(symtab
, layout
);
5726 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
5729 // Return the number of entries in the PLT.
5731 template<int size
, bool big_endian
>
5733 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
5735 if (this->plt_
== NULL
)
5737 return this->plt_
->entry_count();
5740 // Create a GOT entry for local dynamic __tls_get_addr calls.
5742 template<int size
, bool big_endian
>
5744 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
5745 Symbol_table
* symtab
,
5747 Sized_relobj_file
<size
, big_endian
>* object
)
5749 if (this->tlsld_got_offset_
== -1U)
5751 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
5752 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
5753 Output_data_got_powerpc
<size
, big_endian
>* got
5754 = this->got_section(symtab
, layout
);
5755 unsigned int got_offset
= got
->add_constant_pair(0, 0);
5756 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
5758 this->tlsld_got_offset_
= got_offset
;
5760 return this->tlsld_got_offset_
;
5763 // Get the Reference_flags for a particular relocation.
5765 template<int size
, bool big_endian
>
5767 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(
5768 unsigned int r_type
,
5769 const Target_powerpc
* target
)
5775 case elfcpp::R_POWERPC_NONE
:
5776 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5777 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5778 case elfcpp::R_PPC64_TOC
:
5779 // No symbol reference.
5782 case elfcpp::R_PPC64_ADDR64
:
5783 case elfcpp::R_PPC64_UADDR64
:
5784 case elfcpp::R_POWERPC_ADDR32
:
5785 case elfcpp::R_POWERPC_UADDR32
:
5786 case elfcpp::R_POWERPC_ADDR16
:
5787 case elfcpp::R_POWERPC_UADDR16
:
5788 case elfcpp::R_POWERPC_ADDR16_LO
:
5789 case elfcpp::R_POWERPC_ADDR16_HI
:
5790 case elfcpp::R_POWERPC_ADDR16_HA
:
5791 ref
= Symbol::ABSOLUTE_REF
;
5794 case elfcpp::R_POWERPC_ADDR24
:
5795 case elfcpp::R_POWERPC_ADDR14
:
5796 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5797 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5798 ref
= Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
5801 case elfcpp::R_PPC64_REL64
:
5802 case elfcpp::R_POWERPC_REL32
:
5803 case elfcpp::R_PPC_LOCAL24PC
:
5804 case elfcpp::R_POWERPC_REL16
:
5805 case elfcpp::R_POWERPC_REL16_LO
:
5806 case elfcpp::R_POWERPC_REL16_HI
:
5807 case elfcpp::R_POWERPC_REL16_HA
:
5808 ref
= Symbol::RELATIVE_REF
;
5811 case elfcpp::R_POWERPC_REL24
:
5812 case elfcpp::R_PPC_PLTREL24
:
5813 case elfcpp::R_POWERPC_REL14
:
5814 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5815 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5816 ref
= Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
5819 case elfcpp::R_POWERPC_GOT16
:
5820 case elfcpp::R_POWERPC_GOT16_LO
:
5821 case elfcpp::R_POWERPC_GOT16_HI
:
5822 case elfcpp::R_POWERPC_GOT16_HA
:
5823 case elfcpp::R_PPC64_GOT16_DS
:
5824 case elfcpp::R_PPC64_GOT16_LO_DS
:
5825 case elfcpp::R_PPC64_TOC16
:
5826 case elfcpp::R_PPC64_TOC16_LO
:
5827 case elfcpp::R_PPC64_TOC16_HI
:
5828 case elfcpp::R_PPC64_TOC16_HA
:
5829 case elfcpp::R_PPC64_TOC16_DS
:
5830 case elfcpp::R_PPC64_TOC16_LO_DS
:
5831 ref
= Symbol::RELATIVE_REF
;
5834 case elfcpp::R_POWERPC_GOT_TPREL16
:
5835 case elfcpp::R_POWERPC_TLS
:
5836 ref
= Symbol::TLS_REF
;
5839 case elfcpp::R_POWERPC_COPY
:
5840 case elfcpp::R_POWERPC_GLOB_DAT
:
5841 case elfcpp::R_POWERPC_JMP_SLOT
:
5842 case elfcpp::R_POWERPC_RELATIVE
:
5843 case elfcpp::R_POWERPC_DTPMOD
:
5845 // Not expected. We will give an error later.
5849 if (size
== 64 && target
->abiversion() < 2)
5850 ref
|= Symbol::FUNC_DESC_ABI
;
5854 // Report an unsupported relocation against a local symbol.
5856 template<int size
, bool big_endian
>
5858 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
5859 Sized_relobj_file
<size
, big_endian
>* object
,
5860 unsigned int r_type
)
5862 gold_error(_("%s: unsupported reloc %u against local symbol"),
5863 object
->name().c_str(), r_type
);
5866 // We are about to emit a dynamic relocation of type R_TYPE. If the
5867 // dynamic linker does not support it, issue an error.
5869 template<int size
, bool big_endian
>
5871 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
5872 unsigned int r_type
)
5874 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
5876 // These are the relocation types supported by glibc for both 32-bit
5877 // and 64-bit powerpc.
5880 case elfcpp::R_POWERPC_NONE
:
5881 case elfcpp::R_POWERPC_RELATIVE
:
5882 case elfcpp::R_POWERPC_GLOB_DAT
:
5883 case elfcpp::R_POWERPC_DTPMOD
:
5884 case elfcpp::R_POWERPC_DTPREL
:
5885 case elfcpp::R_POWERPC_TPREL
:
5886 case elfcpp::R_POWERPC_JMP_SLOT
:
5887 case elfcpp::R_POWERPC_COPY
:
5888 case elfcpp::R_POWERPC_IRELATIVE
:
5889 case elfcpp::R_POWERPC_ADDR32
:
5890 case elfcpp::R_POWERPC_UADDR32
:
5891 case elfcpp::R_POWERPC_ADDR24
:
5892 case elfcpp::R_POWERPC_ADDR16
:
5893 case elfcpp::R_POWERPC_UADDR16
:
5894 case elfcpp::R_POWERPC_ADDR16_LO
:
5895 case elfcpp::R_POWERPC_ADDR16_HI
:
5896 case elfcpp::R_POWERPC_ADDR16_HA
:
5897 case elfcpp::R_POWERPC_ADDR14
:
5898 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5899 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5900 case elfcpp::R_POWERPC_REL32
:
5901 case elfcpp::R_POWERPC_REL24
:
5902 case elfcpp::R_POWERPC_TPREL16
:
5903 case elfcpp::R_POWERPC_TPREL16_LO
:
5904 case elfcpp::R_POWERPC_TPREL16_HI
:
5905 case elfcpp::R_POWERPC_TPREL16_HA
:
5916 // These are the relocation types supported only on 64-bit.
5917 case elfcpp::R_PPC64_ADDR64
:
5918 case elfcpp::R_PPC64_UADDR64
:
5919 case elfcpp::R_PPC64_JMP_IREL
:
5920 case elfcpp::R_PPC64_ADDR16_DS
:
5921 case elfcpp::R_PPC64_ADDR16_LO_DS
:
5922 case elfcpp::R_PPC64_ADDR16_HIGH
:
5923 case elfcpp::R_PPC64_ADDR16_HIGHA
:
5924 case elfcpp::R_PPC64_ADDR16_HIGHER
:
5925 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
5926 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
5927 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
5928 case elfcpp::R_PPC64_REL64
:
5929 case elfcpp::R_POWERPC_ADDR30
:
5930 case elfcpp::R_PPC64_TPREL16_DS
:
5931 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5932 case elfcpp::R_PPC64_TPREL16_HIGH
:
5933 case elfcpp::R_PPC64_TPREL16_HIGHA
:
5934 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5935 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5936 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5937 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5948 // These are the relocation types supported only on 32-bit.
5949 // ??? glibc ld.so doesn't need to support these.
5950 case elfcpp::R_POWERPC_DTPREL16
:
5951 case elfcpp::R_POWERPC_DTPREL16_LO
:
5952 case elfcpp::R_POWERPC_DTPREL16_HI
:
5953 case elfcpp::R_POWERPC_DTPREL16_HA
:
5961 // This prevents us from issuing more than one error per reloc
5962 // section. But we can still wind up issuing more than one
5963 // error per object file.
5964 if (this->issued_non_pic_error_
)
5966 gold_assert(parameters
->options().output_is_position_independent());
5967 object
->error(_("requires unsupported dynamic reloc; "
5968 "recompile with -fPIC"));
5969 this->issued_non_pic_error_
= true;
5973 // Return whether we need to make a PLT entry for a relocation of the
5974 // given type against a STT_GNU_IFUNC symbol.
5976 template<int size
, bool big_endian
>
5978 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
5979 Target_powerpc
<size
, big_endian
>* target
,
5980 Sized_relobj_file
<size
, big_endian
>* object
,
5981 unsigned int r_type
,
5984 // In non-pic code any reference will resolve to the plt call stub
5985 // for the ifunc symbol.
5986 if ((size
== 32 || target
->abiversion() >= 2)
5987 && !parameters
->options().output_is_position_independent())
5992 // Word size refs from data sections are OK, but don't need a PLT entry.
5993 case elfcpp::R_POWERPC_ADDR32
:
5994 case elfcpp::R_POWERPC_UADDR32
:
5999 case elfcpp::R_PPC64_ADDR64
:
6000 case elfcpp::R_PPC64_UADDR64
:
6005 // GOT refs are good, but also don't need a PLT entry.
6006 case elfcpp::R_POWERPC_GOT16
:
6007 case elfcpp::R_POWERPC_GOT16_LO
:
6008 case elfcpp::R_POWERPC_GOT16_HI
:
6009 case elfcpp::R_POWERPC_GOT16_HA
:
6010 case elfcpp::R_PPC64_GOT16_DS
:
6011 case elfcpp::R_PPC64_GOT16_LO_DS
:
6014 // Function calls are good, and these do need a PLT entry.
6015 case elfcpp::R_POWERPC_ADDR24
:
6016 case elfcpp::R_POWERPC_ADDR14
:
6017 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6018 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6019 case elfcpp::R_POWERPC_REL24
:
6020 case elfcpp::R_PPC_PLTREL24
:
6021 case elfcpp::R_POWERPC_REL14
:
6022 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6023 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6030 // Anything else is a problem.
6031 // If we are building a static executable, the libc startup function
6032 // responsible for applying indirect function relocations is going
6033 // to complain about the reloc type.
6034 // If we are building a dynamic executable, we will have a text
6035 // relocation. The dynamic loader will set the text segment
6036 // writable and non-executable to apply text relocations. So we'll
6037 // segfault when trying to run the indirection function to resolve
6040 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
6041 object
->name().c_str(), r_type
);
6045 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6049 ok_lo_toc_insn(uint32_t insn
, unsigned int r_type
)
6051 return ((insn
& (0x3f << 26)) == 12u << 26 /* addic */
6052 || (insn
& (0x3f << 26)) == 14u << 26 /* addi */
6053 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
6054 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
6055 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
6056 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
6057 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
6058 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
6059 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
6060 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
6061 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
6062 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
6063 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
6064 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
6065 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
6066 || (insn
& (0x3f << 26)) == 56u << 26 /* lq,lfq */
6067 || ((insn
& (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
6068 /* Exclude lfqu by testing reloc. If relocs are ever
6069 defined for the reduced D field in psq_lu then those
6070 will need testing too. */
6071 && r_type
!= elfcpp::R_PPC64_TOC16_LO
6072 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
6073 || ((insn
& (0x3f << 26)) == 58u << 26 /* ld,lwa */
6075 || (insn
& (0x3f << 26)) == 60u << 26 /* stfq */
6076 || ((insn
& (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
6077 /* Exclude stfqu. psq_stu as above for psq_lu. */
6078 && r_type
!= elfcpp::R_PPC64_TOC16_LO
6079 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
6080 || ((insn
& (0x3f << 26)) == 62u << 26 /* std,stq */
6081 && (insn
& 1) == 0));
6084 // Scan a relocation for a local symbol.
6086 template<int size
, bool big_endian
>
6088 Target_powerpc
<size
, big_endian
>::Scan::local(
6089 Symbol_table
* symtab
,
6091 Target_powerpc
<size
, big_endian
>* target
,
6092 Sized_relobj_file
<size
, big_endian
>* object
,
6093 unsigned int data_shndx
,
6094 Output_section
* output_section
,
6095 const elfcpp::Rela
<size
, big_endian
>& reloc
,
6096 unsigned int r_type
,
6097 const elfcpp::Sym
<size
, big_endian
>& lsym
,
6100 this->maybe_skip_tls_get_addr_call(r_type
, NULL
);
6102 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6103 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6105 this->expect_tls_get_addr_call();
6106 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
6107 if (tls_type
!= tls::TLSOPT_NONE
)
6108 this->skip_next_tls_get_addr_call();
6110 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6111 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6113 this->expect_tls_get_addr_call();
6114 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6115 if (tls_type
!= tls::TLSOPT_NONE
)
6116 this->skip_next_tls_get_addr_call();
6119 Powerpc_relobj
<size
, big_endian
>* ppc_object
6120 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6125 && data_shndx
== ppc_object
->opd_shndx()
6126 && r_type
== elfcpp::R_PPC64_ADDR64
)
6127 ppc_object
->set_opd_discard(reloc
.get_r_offset());
6131 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6132 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
6133 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
6135 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6136 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6137 r_type
, r_sym
, reloc
.get_r_addend());
6138 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
6143 case elfcpp::R_POWERPC_NONE
:
6144 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6145 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6146 case elfcpp::R_POWERPC_TLS
:
6147 case elfcpp::R_PPC64_ENTRY
:
6150 case elfcpp::R_PPC64_TOC
:
6152 Output_data_got_powerpc
<size
, big_endian
>* got
6153 = target
->got_section(symtab
, layout
);
6154 if (parameters
->options().output_is_position_independent())
6156 Address off
= reloc
.get_r_offset();
6158 && target
->abiversion() < 2
6159 && data_shndx
== ppc_object
->opd_shndx()
6160 && ppc_object
->get_opd_discard(off
- 8))
6163 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6164 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
6165 rela_dyn
->add_output_section_relative(got
->output_section(),
6166 elfcpp::R_POWERPC_RELATIVE
,
6168 object
, data_shndx
, off
,
6169 symobj
->toc_base_offset());
6174 case elfcpp::R_PPC64_ADDR64
:
6175 case elfcpp::R_PPC64_UADDR64
:
6176 case elfcpp::R_POWERPC_ADDR32
:
6177 case elfcpp::R_POWERPC_UADDR32
:
6178 case elfcpp::R_POWERPC_ADDR24
:
6179 case elfcpp::R_POWERPC_ADDR16
:
6180 case elfcpp::R_POWERPC_ADDR16_LO
:
6181 case elfcpp::R_POWERPC_ADDR16_HI
:
6182 case elfcpp::R_POWERPC_ADDR16_HA
:
6183 case elfcpp::R_POWERPC_UADDR16
:
6184 case elfcpp::R_PPC64_ADDR16_HIGH
:
6185 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6186 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6187 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6188 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6189 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6190 case elfcpp::R_PPC64_ADDR16_DS
:
6191 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6192 case elfcpp::R_POWERPC_ADDR14
:
6193 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6194 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6195 // If building a shared library (or a position-independent
6196 // executable), we need to create a dynamic relocation for
6198 if (parameters
->options().output_is_position_independent()
6199 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
6201 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
6203 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6204 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
6205 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
6207 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6208 : elfcpp::R_POWERPC_RELATIVE
);
6209 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
6210 output_section
, data_shndx
,
6211 reloc
.get_r_offset(),
6212 reloc
.get_r_addend(), false);
6214 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
6216 check_non_pic(object
, r_type
);
6217 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
6218 data_shndx
, reloc
.get_r_offset(),
6219 reloc
.get_r_addend());
6223 gold_assert(lsym
.get_st_value() == 0);
6224 unsigned int shndx
= lsym
.get_st_shndx();
6226 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
6229 object
->error(_("section symbol %u has bad shndx %u"),
6232 rela_dyn
->add_local_section(object
, shndx
, r_type
,
6233 output_section
, data_shndx
,
6234 reloc
.get_r_offset());
6239 case elfcpp::R_POWERPC_REL24
:
6240 case elfcpp::R_PPC_PLTREL24
:
6241 case elfcpp::R_PPC_LOCAL24PC
:
6242 case elfcpp::R_POWERPC_REL14
:
6243 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6244 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6247 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6248 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6249 r_type
, r_sym
, reloc
.get_r_addend());
6253 case elfcpp::R_PPC64_TOCSAVE
:
6254 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6255 // caller has already saved r2 and thus a plt call stub need not
6258 && target
->mark_pltcall(ppc_object
, data_shndx
,
6259 reloc
.get_r_offset() - 4, symtab
))
6261 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6262 unsigned int shndx
= lsym
.get_st_shndx();
6264 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6266 object
->error(_("tocsave symbol %u has bad shndx %u"),
6269 target
->add_tocsave(ppc_object
, shndx
,
6270 lsym
.get_st_value() + reloc
.get_r_addend());
6274 case elfcpp::R_PPC64_REL64
:
6275 case elfcpp::R_POWERPC_REL32
:
6276 case elfcpp::R_POWERPC_REL16
:
6277 case elfcpp::R_POWERPC_REL16_LO
:
6278 case elfcpp::R_POWERPC_REL16_HI
:
6279 case elfcpp::R_POWERPC_REL16_HA
:
6280 case elfcpp::R_POWERPC_REL16DX_HA
:
6281 case elfcpp::R_POWERPC_SECTOFF
:
6282 case elfcpp::R_POWERPC_SECTOFF_LO
:
6283 case elfcpp::R_POWERPC_SECTOFF_HI
:
6284 case elfcpp::R_POWERPC_SECTOFF_HA
:
6285 case elfcpp::R_PPC64_SECTOFF_DS
:
6286 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6287 case elfcpp::R_POWERPC_TPREL16
:
6288 case elfcpp::R_POWERPC_TPREL16_LO
:
6289 case elfcpp::R_POWERPC_TPREL16_HI
:
6290 case elfcpp::R_POWERPC_TPREL16_HA
:
6291 case elfcpp::R_PPC64_TPREL16_DS
:
6292 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6293 case elfcpp::R_PPC64_TPREL16_HIGH
:
6294 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6295 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6296 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6297 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6298 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6299 case elfcpp::R_POWERPC_DTPREL16
:
6300 case elfcpp::R_POWERPC_DTPREL16_LO
:
6301 case elfcpp::R_POWERPC_DTPREL16_HI
:
6302 case elfcpp::R_POWERPC_DTPREL16_HA
:
6303 case elfcpp::R_PPC64_DTPREL16_DS
:
6304 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6305 case elfcpp::R_PPC64_DTPREL16_HIGH
:
6306 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
6307 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6308 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6309 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6310 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6311 case elfcpp::R_PPC64_TLSGD
:
6312 case elfcpp::R_PPC64_TLSLD
:
6313 case elfcpp::R_PPC64_ADDR64_LOCAL
:
6316 case elfcpp::R_POWERPC_GOT16
:
6317 case elfcpp::R_POWERPC_GOT16_LO
:
6318 case elfcpp::R_POWERPC_GOT16_HI
:
6319 case elfcpp::R_POWERPC_GOT16_HA
:
6320 case elfcpp::R_PPC64_GOT16_DS
:
6321 case elfcpp::R_PPC64_GOT16_LO_DS
:
6323 // The symbol requires a GOT entry.
6324 Output_data_got_powerpc
<size
, big_endian
>* got
6325 = target
->got_section(symtab
, layout
);
6326 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6328 if (!parameters
->options().output_is_position_independent())
6331 && (size
== 32 || target
->abiversion() >= 2))
6332 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
6334 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
6336 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
6338 // If we are generating a shared object or a pie, this
6339 // symbol's GOT entry will be set by a dynamic relocation.
6341 off
= got
->add_constant(0);
6342 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
6344 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
6346 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6347 : elfcpp::R_POWERPC_RELATIVE
);
6348 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
6349 got
, off
, 0, false);
6354 case elfcpp::R_PPC64_TOC16
:
6355 case elfcpp::R_PPC64_TOC16_LO
:
6356 case elfcpp::R_PPC64_TOC16_HI
:
6357 case elfcpp::R_PPC64_TOC16_HA
:
6358 case elfcpp::R_PPC64_TOC16_DS
:
6359 case elfcpp::R_PPC64_TOC16_LO_DS
:
6360 // We need a GOT section.
6361 target
->got_section(symtab
, layout
);
6364 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6365 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6366 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6367 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6369 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
6370 if (tls_type
== tls::TLSOPT_NONE
)
6372 Output_data_got_powerpc
<size
, big_endian
>* got
6373 = target
->got_section(symtab
, layout
);
6374 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6375 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6376 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
6377 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
6379 else if (tls_type
== tls::TLSOPT_TO_LE
)
6381 // no GOT relocs needed for Local Exec.
6388 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6389 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6390 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6391 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6393 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6394 if (tls_type
== tls::TLSOPT_NONE
)
6395 target
->tlsld_got_offset(symtab
, layout
, object
);
6396 else if (tls_type
== tls::TLSOPT_TO_LE
)
6398 // no GOT relocs needed for Local Exec.
6399 if (parameters
->options().emit_relocs())
6401 Output_section
* os
= layout
->tls_segment()->first_section();
6402 gold_assert(os
!= NULL
);
6403 os
->set_needs_symtab_index();
6411 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6412 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6413 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6414 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6416 Output_data_got_powerpc
<size
, big_endian
>* got
6417 = target
->got_section(symtab
, layout
);
6418 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6419 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
6423 case elfcpp::R_POWERPC_GOT_TPREL16
:
6424 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6425 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6426 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6428 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
6429 if (tls_type
== tls::TLSOPT_NONE
)
6431 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6432 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
6434 Output_data_got_powerpc
<size
, big_endian
>* got
6435 = target
->got_section(symtab
, layout
);
6436 unsigned int off
= got
->add_constant(0);
6437 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
6439 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6440 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
6441 elfcpp::R_POWERPC_TPREL
,
6445 else if (tls_type
== tls::TLSOPT_TO_LE
)
6447 // no GOT relocs needed for Local Exec.
6455 unsupported_reloc_local(object
, r_type
);
6460 && parameters
->options().toc_optimize())
6462 if (data_shndx
== ppc_object
->toc_shndx())
6465 if (r_type
!= elfcpp::R_PPC64_ADDR64
6466 || (is_ifunc
&& target
->abiversion() < 2))
6468 else if (parameters
->options().output_is_position_independent())
6474 unsigned int shndx
= lsym
.get_st_shndx();
6475 if (shndx
>= elfcpp::SHN_LORESERVE
6476 && shndx
!= elfcpp::SHN_XINDEX
)
6481 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
6484 enum {no_check
, check_lo
, check_ha
} insn_check
;
6488 insn_check
= no_check
;
6491 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6492 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6493 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6494 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6495 case elfcpp::R_POWERPC_GOT16_HA
:
6496 case elfcpp::R_PPC64_TOC16_HA
:
6497 insn_check
= check_ha
;
6500 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6501 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6502 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6503 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6504 case elfcpp::R_POWERPC_GOT16_LO
:
6505 case elfcpp::R_PPC64_GOT16_LO_DS
:
6506 case elfcpp::R_PPC64_TOC16_LO
:
6507 case elfcpp::R_PPC64_TOC16_LO_DS
:
6508 insn_check
= check_lo
;
6512 section_size_type slen
;
6513 const unsigned char* view
= NULL
;
6514 if (insn_check
!= no_check
)
6516 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
6517 section_size_type off
=
6518 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
6521 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
6522 if (insn_check
== check_lo
6523 ? !ok_lo_toc_insn(insn
, r_type
)
6524 : ((insn
& ((0x3f << 26) | 0x1f << 16))
6525 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
6527 ppc_object
->set_no_toc_opt();
6528 gold_warning(_("%s: toc optimization is not supported "
6529 "for %#08x instruction"),
6530 ppc_object
->name().c_str(), insn
);
6539 case elfcpp::R_PPC64_TOC16
:
6540 case elfcpp::R_PPC64_TOC16_LO
:
6541 case elfcpp::R_PPC64_TOC16_HI
:
6542 case elfcpp::R_PPC64_TOC16_HA
:
6543 case elfcpp::R_PPC64_TOC16_DS
:
6544 case elfcpp::R_PPC64_TOC16_LO_DS
:
6545 unsigned int shndx
= lsym
.get_st_shndx();
6546 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6548 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6549 if (is_ordinary
&& shndx
== ppc_object
->toc_shndx())
6551 Address dst_off
= lsym
.get_st_value() + reloc
.get_r_offset();
6552 if (dst_off
< ppc_object
->section_size(shndx
))
6555 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
6557 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
6559 // Need to check that the insn is a ld
6561 view
= ppc_object
->section_contents(data_shndx
,
6564 section_size_type off
=
6565 (convert_to_section_size_type(reloc
.get_r_offset())
6566 + (big_endian
? -2 : 3));
6568 && (view
[off
] & (0x3f << 2)) == 58u << 2)
6572 ppc_object
->set_no_toc_opt(dst_off
);
6583 case elfcpp::R_POWERPC_REL32
:
6584 if (ppc_object
->got2_shndx() != 0
6585 && parameters
->options().output_is_position_independent())
6587 unsigned int shndx
= lsym
.get_st_shndx();
6588 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6590 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6591 if (is_ordinary
&& shndx
== ppc_object
->got2_shndx()
6592 && (ppc_object
->section_flags(data_shndx
)
6593 & elfcpp::SHF_EXECINSTR
) != 0)
6594 gold_error(_("%s: unsupported -mbss-plt code"),
6595 ppc_object
->name().c_str());
6605 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6606 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6607 case elfcpp::R_POWERPC_GOT_TPREL16
:
6608 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6609 case elfcpp::R_POWERPC_GOT16
:
6610 case elfcpp::R_PPC64_GOT16_DS
:
6611 case elfcpp::R_PPC64_TOC16
:
6612 case elfcpp::R_PPC64_TOC16_DS
:
6613 ppc_object
->set_has_small_toc_reloc();
6619 // Report an unsupported relocation against a global symbol.
6621 template<int size
, bool big_endian
>
6623 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
6624 Sized_relobj_file
<size
, big_endian
>* object
,
6625 unsigned int r_type
,
6628 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6629 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
6632 // Scan a relocation for a global symbol.
6634 template<int size
, bool big_endian
>
6636 Target_powerpc
<size
, big_endian
>::Scan::global(
6637 Symbol_table
* symtab
,
6639 Target_powerpc
<size
, big_endian
>* target
,
6640 Sized_relobj_file
<size
, big_endian
>* object
,
6641 unsigned int data_shndx
,
6642 Output_section
* output_section
,
6643 const elfcpp::Rela
<size
, big_endian
>& reloc
,
6644 unsigned int r_type
,
6647 if (this->maybe_skip_tls_get_addr_call(r_type
, gsym
) == Track_tls::SKIP
)
6650 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6651 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6653 this->expect_tls_get_addr_call();
6654 const bool final
= gsym
->final_value_is_known();
6655 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6656 if (tls_type
!= tls::TLSOPT_NONE
)
6657 this->skip_next_tls_get_addr_call();
6659 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6660 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6662 this->expect_tls_get_addr_call();
6663 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6664 if (tls_type
!= tls::TLSOPT_NONE
)
6665 this->skip_next_tls_get_addr_call();
6668 Powerpc_relobj
<size
, big_endian
>* ppc_object
6669 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6671 // A STT_GNU_IFUNC symbol may require a PLT entry.
6672 bool is_ifunc
= gsym
->type() == elfcpp::STT_GNU_IFUNC
;
6673 bool pushed_ifunc
= false;
6674 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
6676 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6677 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6678 r_type
, r_sym
, reloc
.get_r_addend());
6679 target
->make_plt_entry(symtab
, layout
, gsym
);
6680 pushed_ifunc
= true;
6685 case elfcpp::R_POWERPC_NONE
:
6686 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6687 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6688 case elfcpp::R_PPC_LOCAL24PC
:
6689 case elfcpp::R_POWERPC_TLS
:
6690 case elfcpp::R_PPC64_ENTRY
:
6693 case elfcpp::R_PPC64_TOC
:
6695 Output_data_got_powerpc
<size
, big_endian
>* got
6696 = target
->got_section(symtab
, layout
);
6697 if (parameters
->options().output_is_position_independent())
6699 Address off
= reloc
.get_r_offset();
6701 && data_shndx
== ppc_object
->opd_shndx()
6702 && ppc_object
->get_opd_discard(off
- 8))
6705 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6706 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
6707 if (data_shndx
!= ppc_object
->opd_shndx())
6708 symobj
= static_cast
6709 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
6710 rela_dyn
->add_output_section_relative(got
->output_section(),
6711 elfcpp::R_POWERPC_RELATIVE
,
6713 object
, data_shndx
, off
,
6714 symobj
->toc_base_offset());
6719 case elfcpp::R_PPC64_ADDR64
:
6721 && target
->abiversion() < 2
6722 && data_shndx
== ppc_object
->opd_shndx()
6723 && (gsym
->is_defined_in_discarded_section()
6724 || gsym
->object() != object
))
6726 ppc_object
->set_opd_discard(reloc
.get_r_offset());
6730 case elfcpp::R_PPC64_UADDR64
:
6731 case elfcpp::R_POWERPC_ADDR32
:
6732 case elfcpp::R_POWERPC_UADDR32
:
6733 case elfcpp::R_POWERPC_ADDR24
:
6734 case elfcpp::R_POWERPC_ADDR16
:
6735 case elfcpp::R_POWERPC_ADDR16_LO
:
6736 case elfcpp::R_POWERPC_ADDR16_HI
:
6737 case elfcpp::R_POWERPC_ADDR16_HA
:
6738 case elfcpp::R_POWERPC_UADDR16
:
6739 case elfcpp::R_PPC64_ADDR16_HIGH
:
6740 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6741 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6742 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6743 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6744 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6745 case elfcpp::R_PPC64_ADDR16_DS
:
6746 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6747 case elfcpp::R_POWERPC_ADDR14
:
6748 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6749 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6751 // Make a PLT entry if necessary.
6752 if (gsym
->needs_plt_entry())
6754 // Since this is not a PC-relative relocation, we may be
6755 // taking the address of a function. In that case we need to
6756 // set the entry in the dynamic symbol table to the address of
6757 // the PLT call stub.
6758 bool need_ifunc_plt
= false;
6759 if ((size
== 32 || target
->abiversion() >= 2)
6760 && gsym
->is_from_dynobj()
6761 && !parameters
->options().output_is_position_independent())
6763 gsym
->set_needs_dynsym_value();
6764 need_ifunc_plt
= true;
6766 if (!is_ifunc
|| (!pushed_ifunc
&& need_ifunc_plt
))
6768 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6769 target
->push_branch(ppc_object
, data_shndx
,
6770 reloc
.get_r_offset(), r_type
, r_sym
,
6771 reloc
.get_r_addend());
6772 target
->make_plt_entry(symtab
, layout
, gsym
);
6775 // Make a dynamic relocation if necessary.
6776 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
))
6777 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
6779 if (!parameters
->options().output_is_position_independent()
6780 && gsym
->may_need_copy_reloc())
6782 target
->copy_reloc(symtab
, layout
, object
,
6783 data_shndx
, output_section
, gsym
, reloc
);
6785 else if ((((size
== 32
6786 && r_type
== elfcpp::R_POWERPC_ADDR32
)
6788 && r_type
== elfcpp::R_PPC64_ADDR64
6789 && target
->abiversion() >= 2))
6790 && gsym
->can_use_relative_reloc(false)
6791 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
6792 && parameters
->options().shared()))
6794 && r_type
== elfcpp::R_PPC64_ADDR64
6795 && target
->abiversion() < 2
6796 && (gsym
->can_use_relative_reloc(false)
6797 || data_shndx
== ppc_object
->opd_shndx())))
6799 Reloc_section
* rela_dyn
6800 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6801 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6802 : elfcpp::R_POWERPC_RELATIVE
);
6803 rela_dyn
->add_symbolless_global_addend(
6804 gsym
, dynrel
, output_section
, object
, data_shndx
,
6805 reloc
.get_r_offset(), reloc
.get_r_addend());
6809 Reloc_section
* rela_dyn
6810 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6811 check_non_pic(object
, r_type
);
6812 rela_dyn
->add_global(gsym
, r_type
, output_section
,
6814 reloc
.get_r_offset(),
6815 reloc
.get_r_addend());
6818 && parameters
->options().toc_optimize()
6819 && data_shndx
== ppc_object
->toc_shndx())
6820 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
6826 case elfcpp::R_PPC_PLTREL24
:
6827 case elfcpp::R_POWERPC_REL24
:
6830 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6831 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6832 r_type
, r_sym
, reloc
.get_r_addend());
6833 if (gsym
->needs_plt_entry()
6834 || (!gsym
->final_value_is_known()
6835 && (gsym
->is_undefined()
6836 || gsym
->is_from_dynobj()
6837 || gsym
->is_preemptible())))
6838 target
->make_plt_entry(symtab
, layout
, gsym
);
6842 case elfcpp::R_PPC64_REL64
:
6843 case elfcpp::R_POWERPC_REL32
:
6844 // Make a dynamic relocation if necessary.
6845 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
)))
6847 if (!parameters
->options().output_is_position_independent()
6848 && gsym
->may_need_copy_reloc())
6850 target
->copy_reloc(symtab
, layout
, object
,
6851 data_shndx
, output_section
, gsym
,
6856 Reloc_section
* rela_dyn
6857 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6858 check_non_pic(object
, r_type
);
6859 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
6860 data_shndx
, reloc
.get_r_offset(),
6861 reloc
.get_r_addend());
6866 case elfcpp::R_POWERPC_REL14
:
6867 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6868 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6871 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6872 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6873 r_type
, r_sym
, reloc
.get_r_addend());
6877 case elfcpp::R_PPC64_TOCSAVE
:
6878 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6879 // caller has already saved r2 and thus a plt call stub need not
6882 && target
->mark_pltcall(ppc_object
, data_shndx
,
6883 reloc
.get_r_offset() - 4, symtab
))
6885 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6887 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
6889 object
->error(_("tocsave symbol %u has bad shndx %u"),
6893 Sized_symbol
<size
>* sym
= symtab
->get_sized_symbol
<size
>(gsym
);
6894 target
->add_tocsave(ppc_object
, shndx
,
6895 sym
->value() + reloc
.get_r_addend());
6900 case elfcpp::R_POWERPC_REL16
:
6901 case elfcpp::R_POWERPC_REL16_LO
:
6902 case elfcpp::R_POWERPC_REL16_HI
:
6903 case elfcpp::R_POWERPC_REL16_HA
:
6904 case elfcpp::R_POWERPC_REL16DX_HA
:
6905 case elfcpp::R_POWERPC_SECTOFF
:
6906 case elfcpp::R_POWERPC_SECTOFF_LO
:
6907 case elfcpp::R_POWERPC_SECTOFF_HI
:
6908 case elfcpp::R_POWERPC_SECTOFF_HA
:
6909 case elfcpp::R_PPC64_SECTOFF_DS
:
6910 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6911 case elfcpp::R_POWERPC_TPREL16
:
6912 case elfcpp::R_POWERPC_TPREL16_LO
:
6913 case elfcpp::R_POWERPC_TPREL16_HI
:
6914 case elfcpp::R_POWERPC_TPREL16_HA
:
6915 case elfcpp::R_PPC64_TPREL16_DS
:
6916 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6917 case elfcpp::R_PPC64_TPREL16_HIGH
:
6918 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6919 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6920 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6921 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6922 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6923 case elfcpp::R_POWERPC_DTPREL16
:
6924 case elfcpp::R_POWERPC_DTPREL16_LO
:
6925 case elfcpp::R_POWERPC_DTPREL16_HI
:
6926 case elfcpp::R_POWERPC_DTPREL16_HA
:
6927 case elfcpp::R_PPC64_DTPREL16_DS
:
6928 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6929 case elfcpp::R_PPC64_DTPREL16_HIGH
:
6930 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
6931 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6932 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6933 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6934 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6935 case elfcpp::R_PPC64_TLSGD
:
6936 case elfcpp::R_PPC64_TLSLD
:
6937 case elfcpp::R_PPC64_ADDR64_LOCAL
:
6940 case elfcpp::R_POWERPC_GOT16
:
6941 case elfcpp::R_POWERPC_GOT16_LO
:
6942 case elfcpp::R_POWERPC_GOT16_HI
:
6943 case elfcpp::R_POWERPC_GOT16_HA
:
6944 case elfcpp::R_PPC64_GOT16_DS
:
6945 case elfcpp::R_PPC64_GOT16_LO_DS
:
6947 // The symbol requires a GOT entry.
6948 Output_data_got_powerpc
<size
, big_endian
>* got
;
6950 got
= target
->got_section(symtab
, layout
);
6951 if (gsym
->final_value_is_known())
6954 && (size
== 32 || target
->abiversion() >= 2))
6955 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
6957 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
6959 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
6961 // If we are generating a shared object or a pie, this
6962 // symbol's GOT entry will be set by a dynamic relocation.
6963 unsigned int off
= got
->add_constant(0);
6964 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
6966 Reloc_section
* rela_dyn
6967 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6969 if (gsym
->can_use_relative_reloc(false)
6971 || target
->abiversion() >= 2)
6972 && gsym
->visibility() == elfcpp::STV_PROTECTED
6973 && parameters
->options().shared()))
6975 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6976 : elfcpp::R_POWERPC_RELATIVE
);
6977 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
6981 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
6982 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
6988 case elfcpp::R_PPC64_TOC16
:
6989 case elfcpp::R_PPC64_TOC16_LO
:
6990 case elfcpp::R_PPC64_TOC16_HI
:
6991 case elfcpp::R_PPC64_TOC16_HA
:
6992 case elfcpp::R_PPC64_TOC16_DS
:
6993 case elfcpp::R_PPC64_TOC16_LO_DS
:
6994 // We need a GOT section.
6995 target
->got_section(symtab
, layout
);
6998 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6999 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7000 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
7001 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7003 const bool final
= gsym
->final_value_is_known();
7004 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7005 if (tls_type
== tls::TLSOPT_NONE
)
7007 Output_data_got_powerpc
<size
, big_endian
>* got
7008 = target
->got_section(symtab
, layout
);
7009 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7010 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
, rela_dyn
,
7011 elfcpp::R_POWERPC_DTPMOD
,
7012 elfcpp::R_POWERPC_DTPREL
);
7014 else if (tls_type
== tls::TLSOPT_TO_IE
)
7016 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
7018 Output_data_got_powerpc
<size
, big_endian
>* got
7019 = target
->got_section(symtab
, layout
);
7020 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7021 if (gsym
->is_undefined()
7022 || gsym
->is_from_dynobj())
7024 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
7025 elfcpp::R_POWERPC_TPREL
);
7029 unsigned int off
= got
->add_constant(0);
7030 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
7031 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
7032 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
7037 else if (tls_type
== tls::TLSOPT_TO_LE
)
7039 // no GOT relocs needed for Local Exec.
7046 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7047 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7048 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
7049 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7051 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7052 if (tls_type
== tls::TLSOPT_NONE
)
7053 target
->tlsld_got_offset(symtab
, layout
, object
);
7054 else if (tls_type
== tls::TLSOPT_TO_LE
)
7056 // no GOT relocs needed for Local Exec.
7057 if (parameters
->options().emit_relocs())
7059 Output_section
* os
= layout
->tls_segment()->first_section();
7060 gold_assert(os
!= NULL
);
7061 os
->set_needs_symtab_index();
7069 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7070 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7071 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
7072 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7074 Output_data_got_powerpc
<size
, big_endian
>* got
7075 = target
->got_section(symtab
, layout
);
7076 if (!gsym
->final_value_is_known()
7077 && (gsym
->is_from_dynobj()
7078 || gsym
->is_undefined()
7079 || gsym
->is_preemptible()))
7080 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
7081 target
->rela_dyn_section(layout
),
7082 elfcpp::R_POWERPC_DTPREL
);
7084 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
7088 case elfcpp::R_POWERPC_GOT_TPREL16
:
7089 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7090 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7091 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7093 const bool final
= gsym
->final_value_is_known();
7094 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
7095 if (tls_type
== tls::TLSOPT_NONE
)
7097 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
7099 Output_data_got_powerpc
<size
, big_endian
>* got
7100 = target
->got_section(symtab
, layout
);
7101 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7102 if (gsym
->is_undefined()
7103 || gsym
->is_from_dynobj())
7105 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
7106 elfcpp::R_POWERPC_TPREL
);
7110 unsigned int off
= got
->add_constant(0);
7111 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
7112 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
7113 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
7118 else if (tls_type
== tls::TLSOPT_TO_LE
)
7120 // no GOT relocs needed for Local Exec.
7128 unsupported_reloc_global(object
, r_type
, gsym
);
7133 && parameters
->options().toc_optimize())
7135 if (data_shndx
== ppc_object
->toc_shndx())
7138 if (r_type
!= elfcpp::R_PPC64_ADDR64
7139 || (is_ifunc
&& target
->abiversion() < 2))
7141 else if (parameters
->options().output_is_position_independent()
7142 && (is_ifunc
|| gsym
->is_absolute() || gsym
->is_undefined()))
7145 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
7148 enum {no_check
, check_lo
, check_ha
} insn_check
;
7152 insn_check
= no_check
;
7155 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7156 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7157 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7158 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7159 case elfcpp::R_POWERPC_GOT16_HA
:
7160 case elfcpp::R_PPC64_TOC16_HA
:
7161 insn_check
= check_ha
;
7164 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7165 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7166 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7167 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7168 case elfcpp::R_POWERPC_GOT16_LO
:
7169 case elfcpp::R_PPC64_GOT16_LO_DS
:
7170 case elfcpp::R_PPC64_TOC16_LO
:
7171 case elfcpp::R_PPC64_TOC16_LO_DS
:
7172 insn_check
= check_lo
;
7176 section_size_type slen
;
7177 const unsigned char* view
= NULL
;
7178 if (insn_check
!= no_check
)
7180 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
7181 section_size_type off
=
7182 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
7185 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
7186 if (insn_check
== check_lo
7187 ? !ok_lo_toc_insn(insn
, r_type
)
7188 : ((insn
& ((0x3f << 26) | 0x1f << 16))
7189 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7191 ppc_object
->set_no_toc_opt();
7192 gold_warning(_("%s: toc optimization is not supported "
7193 "for %#08x instruction"),
7194 ppc_object
->name().c_str(), insn
);
7203 case elfcpp::R_PPC64_TOC16
:
7204 case elfcpp::R_PPC64_TOC16_LO
:
7205 case elfcpp::R_PPC64_TOC16_HI
:
7206 case elfcpp::R_PPC64_TOC16_HA
:
7207 case elfcpp::R_PPC64_TOC16_DS
:
7208 case elfcpp::R_PPC64_TOC16_LO_DS
:
7209 if (gsym
->source() == Symbol::FROM_OBJECT
7210 && !gsym
->object()->is_dynamic())
7212 Powerpc_relobj
<size
, big_endian
>* sym_object
7213 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7215 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
7216 if (shndx
== sym_object
->toc_shndx())
7218 Sized_symbol
<size
>* sym
= symtab
->get_sized_symbol
<size
>(gsym
);
7219 Address dst_off
= sym
->value() + reloc
.get_r_offset();
7220 if (dst_off
< sym_object
->section_size(shndx
))
7223 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
7225 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
7227 // Need to check that the insn is a ld
7229 view
= ppc_object
->section_contents(data_shndx
,
7232 section_size_type off
=
7233 (convert_to_section_size_type(reloc
.get_r_offset())
7234 + (big_endian
? -2 : 3));
7236 && (view
[off
] & (0x3f << 2)) == (58u << 2))
7240 sym_object
->set_no_toc_opt(dst_off
);
7252 case elfcpp::R_PPC_LOCAL24PC
:
7253 if (strcmp(gsym
->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7254 gold_error(_("%s: unsupported -mbss-plt code"),
7255 ppc_object
->name().c_str());
7264 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7265 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7266 case elfcpp::R_POWERPC_GOT_TPREL16
:
7267 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7268 case elfcpp::R_POWERPC_GOT16
:
7269 case elfcpp::R_PPC64_GOT16_DS
:
7270 case elfcpp::R_PPC64_TOC16
:
7271 case elfcpp::R_PPC64_TOC16_DS
:
7272 ppc_object
->set_has_small_toc_reloc();
7278 // Process relocations for gc.
7280 template<int size
, bool big_endian
>
7282 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
7283 Symbol_table
* symtab
,
7285 Sized_relobj_file
<size
, big_endian
>* object
,
7286 unsigned int data_shndx
,
7288 const unsigned char* prelocs
,
7290 Output_section
* output_section
,
7291 bool needs_special_offset_handling
,
7292 size_t local_symbol_count
,
7293 const unsigned char* plocal_symbols
)
7295 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
7296 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
7299 Powerpc_relobj
<size
, big_endian
>* ppc_object
7300 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
7302 ppc_object
->set_opd_valid();
7303 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
7305 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
7306 for (p
= ppc_object
->access_from_map()->begin();
7307 p
!= ppc_object
->access_from_map()->end();
7310 Address dst_off
= p
->first
;
7311 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
7312 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
7313 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
7315 Relobj
* src_obj
= s
->first
;
7316 unsigned int src_indx
= s
->second
;
7317 symtab
->gc()->add_reference(src_obj
, src_indx
,
7318 ppc_object
, dst_indx
);
7322 ppc_object
->access_from_map()->clear();
7323 ppc_object
->process_gc_mark(symtab
);
7324 // Don't look at .opd relocs as .opd will reference everything.
7328 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
7337 needs_special_offset_handling
,
7342 // Handle target specific gc actions when adding a gc reference from
7343 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7344 // and DST_OFF. For powerpc64, this adds a referenc to the code
7345 // section of a function descriptor.
7347 template<int size
, bool big_endian
>
7349 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
7350 Symbol_table
* symtab
,
7352 unsigned int src_shndx
,
7354 unsigned int dst_shndx
,
7355 Address dst_off
) const
7357 if (size
!= 64 || dst_obj
->is_dynamic())
7360 Powerpc_relobj
<size
, big_endian
>* ppc_object
7361 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
7362 if (dst_shndx
!= 0 && dst_shndx
== ppc_object
->opd_shndx())
7364 if (ppc_object
->opd_valid())
7366 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
7367 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
7371 // If we haven't run scan_opd_relocs, we must delay
7372 // processing this function descriptor reference.
7373 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
7378 // Add any special sections for this symbol to the gc work list.
7379 // For powerpc64, this adds the code section of a function
7382 template<int size
, bool big_endian
>
7384 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
7385 Symbol_table
* symtab
,
7390 Powerpc_relobj
<size
, big_endian
>* ppc_object
7391 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
7393 unsigned int shndx
= sym
->shndx(&is_ordinary
);
7394 if (is_ordinary
&& shndx
!= 0 && shndx
== ppc_object
->opd_shndx())
7396 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
7397 Address dst_off
= gsym
->value();
7398 if (ppc_object
->opd_valid())
7400 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
7401 symtab
->gc()->worklist().push_back(Section_id(ppc_object
,
7405 ppc_object
->add_gc_mark(dst_off
);
7410 // For a symbol location in .opd, set LOC to the location of the
7413 template<int size
, bool big_endian
>
7415 Target_powerpc
<size
, big_endian
>::do_function_location(
7416 Symbol_location
* loc
) const
7418 if (size
== 64 && loc
->shndx
!= 0)
7420 if (loc
->object
->is_dynamic())
7422 Powerpc_dynobj
<size
, big_endian
>* ppc_object
7423 = static_cast<Powerpc_dynobj
<size
, big_endian
>*>(loc
->object
);
7424 if (loc
->shndx
== ppc_object
->opd_shndx())
7427 Address off
= loc
->offset
- ppc_object
->opd_address();
7428 loc
->shndx
= ppc_object
->get_opd_ent(off
, &dest_off
);
7429 loc
->offset
= dest_off
;
7434 const Powerpc_relobj
<size
, big_endian
>* ppc_object
7435 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(loc
->object
);
7436 if (loc
->shndx
== ppc_object
->opd_shndx())
7439 loc
->shndx
= ppc_object
->get_opd_ent(loc
->offset
, &dest_off
);
7440 loc
->offset
= dest_off
;
7446 // FNOFFSET in section SHNDX in OBJECT is the start of a function
7447 // compiled with -fsplit-stack. The function calls non-split-stack
7448 // code. Change the function to ensure it has enough stack space to
7449 // call some random function.
7451 template<int size
, bool big_endian
>
7453 Target_powerpc
<size
, big_endian
>::do_calls_non_split(
7456 section_offset_type fnoffset
,
7457 section_size_type fnsize
,
7458 const unsigned char* prelocs
,
7460 unsigned char* view
,
7461 section_size_type view_size
,
7463 std::string
* to
) const
7465 // 32-bit not supported.
7469 Target::do_calls_non_split(object
, shndx
, fnoffset
, fnsize
,
7470 prelocs
, reloc_count
, view
, view_size
,
7475 // The function always starts with
7476 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
7477 // addis %r12,%r1,-allocate@ha
7478 // addi %r12,%r12,-allocate@l
7480 // but note that the addis or addi may be replaced with a nop
7482 unsigned char *entry
= view
+ fnoffset
;
7483 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
7485 if ((insn
& 0xffff0000) == addis_2_12
)
7487 /* Skip ELFv2 global entry code. */
7489 insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
7492 unsigned char *pinsn
= entry
;
7494 const uint32_t ld_private_ss
= 0xe80d8fc0;
7495 if (insn
== ld_private_ss
)
7497 int32_t allocate
= 0;
7501 insn
= elfcpp::Swap
<32, big_endian
>::readval(pinsn
);
7502 if ((insn
& 0xffff0000) == addis_12_1
)
7503 allocate
+= (insn
& 0xffff) << 16;
7504 else if ((insn
& 0xffff0000) == addi_12_1
7505 || (insn
& 0xffff0000) == addi_12_12
)
7506 allocate
+= ((insn
& 0xffff) ^ 0x8000) - 0x8000;
7507 else if (insn
!= nop
)
7510 if (insn
== cmpld_7_12_0
&& pinsn
== entry
+ 12)
7512 int extra
= parameters
->options().split_stack_adjust_size();
7514 if (allocate
>= 0 || extra
< 0)
7516 object
->error(_("split-stack stack size overflow at "
7517 "section %u offset %0zx"),
7518 shndx
, static_cast<size_t>(fnoffset
));
7522 insn
= addis_12_1
| (((allocate
+ 0x8000) >> 16) & 0xffff);
7523 if (insn
!= addis_12_1
)
7525 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7527 insn
= addi_12_12
| (allocate
& 0xffff);
7528 if (insn
!= addi_12_12
)
7530 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7536 insn
= addi_12_1
| (allocate
& 0xffff);
7537 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7540 if (pinsn
!= entry
+ 12)
7541 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, nop
);
7549 if (!object
->has_no_split_stack())
7550 object
->error(_("failed to match split-stack sequence at "
7551 "section %u offset %0zx"),
7552 shndx
, static_cast<size_t>(fnoffset
));
7556 // Scan relocations for a section.
7558 template<int size
, bool big_endian
>
7560 Target_powerpc
<size
, big_endian
>::scan_relocs(
7561 Symbol_table
* symtab
,
7563 Sized_relobj_file
<size
, big_endian
>* object
,
7564 unsigned int data_shndx
,
7565 unsigned int sh_type
,
7566 const unsigned char* prelocs
,
7568 Output_section
* output_section
,
7569 bool needs_special_offset_handling
,
7570 size_t local_symbol_count
,
7571 const unsigned char* plocal_symbols
)
7573 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
7574 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
7577 if (sh_type
== elfcpp::SHT_REL
)
7579 gold_error(_("%s: unsupported REL reloc section"),
7580 object
->name().c_str());
7584 gold::scan_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
7593 needs_special_offset_handling
,
7598 // Functor class for processing the global symbol table.
7599 // Removes symbols defined on discarded opd entries.
7601 template<bool big_endian
>
7602 class Global_symbol_visitor_opd
7605 Global_symbol_visitor_opd()
7609 operator()(Sized_symbol
<64>* sym
)
7611 if (sym
->has_symtab_index()
7612 || sym
->source() != Symbol::FROM_OBJECT
7613 || !sym
->in_real_elf())
7616 if (sym
->object()->is_dynamic())
7619 Powerpc_relobj
<64, big_endian
>* symobj
7620 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
7621 if (symobj
->opd_shndx() == 0)
7625 unsigned int shndx
= sym
->shndx(&is_ordinary
);
7626 if (shndx
== symobj
->opd_shndx()
7627 && symobj
->get_opd_discard(sym
->value()))
7629 sym
->set_undefined();
7630 sym
->set_visibility(elfcpp::STV_DEFAULT
);
7631 sym
->set_is_defined_in_discarded_section();
7632 sym
->set_symtab_index(-1U);
7637 template<int size
, bool big_endian
>
7639 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
7641 Symbol_table
* symtab
)
7645 Output_data_save_res
<size
, big_endian
>* savres
7646 = new Output_data_save_res
<size
, big_endian
>(symtab
);
7647 this->savres_section_
= savres
;
7648 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
7649 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
7650 savres
, ORDER_TEXT
, false);
7654 // Sort linker created .got section first (for the header), then input
7655 // sections belonging to files using small model code.
7657 template<bool big_endian
>
7658 class Sort_toc_sections
7662 operator()(const Output_section::Input_section
& is1
,
7663 const Output_section::Input_section
& is2
) const
7665 if (!is1
.is_input_section() && is2
.is_input_section())
7668 = (is1
.is_input_section()
7669 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
7670 ->has_small_toc_reloc()));
7672 = (is2
.is_input_section()
7673 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
7674 ->has_small_toc_reloc()));
7675 return small1
&& !small2
;
7679 // Finalize the sections.
7681 template<int size
, bool big_endian
>
7683 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
7685 const Input_objects
*,
7686 Symbol_table
* symtab
)
7688 if (parameters
->doing_static_link())
7690 // At least some versions of glibc elf-init.o have a strong
7691 // reference to __rela_iplt marker syms. A weak ref would be
7693 if (this->iplt_
!= NULL
)
7695 Reloc_section
* rel
= this->iplt_
->rel_plt();
7696 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
7697 Symbol_table::PREDEFINED
, rel
, 0, 0,
7698 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7699 elfcpp::STV_HIDDEN
, 0, false, true);
7700 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
7701 Symbol_table::PREDEFINED
, rel
, 0, 0,
7702 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7703 elfcpp::STV_HIDDEN
, 0, true, true);
7707 symtab
->define_as_constant("__rela_iplt_start", NULL
,
7708 Symbol_table::PREDEFINED
, 0, 0,
7709 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7710 elfcpp::STV_HIDDEN
, 0, true, false);
7711 symtab
->define_as_constant("__rela_iplt_end", NULL
,
7712 Symbol_table::PREDEFINED
, 0, 0,
7713 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7714 elfcpp::STV_HIDDEN
, 0, true, false);
7720 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
7721 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
7723 if (!parameters
->options().relocatable())
7725 this->define_save_restore_funcs(layout
, symtab
);
7727 // Annoyingly, we need to make these sections now whether or
7728 // not we need them. If we delay until do_relax then we
7729 // need to mess with the relaxation machinery checkpointing.
7730 this->got_section(symtab
, layout
);
7731 this->make_brlt_section(layout
);
7733 if (parameters
->options().toc_sort())
7735 Output_section
* os
= this->got_
->output_section();
7736 if (os
!= NULL
&& os
->input_sections().size() > 1)
7737 std::stable_sort(os
->input_sections().begin(),
7738 os
->input_sections().end(),
7739 Sort_toc_sections
<big_endian
>());
7744 // Fill in some more dynamic tags.
7745 Output_data_dynamic
* odyn
= layout
->dynamic_data();
7748 const Reloc_section
* rel_plt
= (this->plt_
== NULL
7750 : this->plt_
->rel_plt());
7751 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
7752 this->rela_dyn_
, true, size
== 32);
7756 if (this->got_
!= NULL
)
7758 this->got_
->finalize_data_size();
7759 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
7760 this->got_
, this->got_
->g_o_t());
7765 if (this->glink_
!= NULL
)
7767 this->glink_
->finalize_data_size();
7768 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
7770 (this->glink_
->pltresolve_size
7776 // Emit any relocs we saved in an attempt to avoid generating COPY
7778 if (this->copy_relocs_
.any_saved_relocs())
7779 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
7782 // Emit any saved relocs, and mark toc entries using any of these
7783 // relocs as not optimizable.
7785 template<int sh_type
, int size
, bool big_endian
>
7787 Powerpc_copy_relocs
<sh_type
, size
, big_endian
>::emit(
7788 Output_data_reloc
<sh_type
, true, size
, big_endian
>* reloc_section
)
7791 && parameters
->options().toc_optimize())
7793 for (typename Copy_relocs
<sh_type
, size
, big_endian
>::
7794 Copy_reloc_entries::iterator p
= this->entries_
.begin();
7795 p
!= this->entries_
.end();
7798 typename Copy_relocs
<sh_type
, size
, big_endian
>::Copy_reloc_entry
&
7801 // If the symbol is no longer defined in a dynamic object,
7802 // then we emitted a COPY relocation. If it is still
7803 // dynamic then we'll need dynamic relocations and thus
7804 // can't optimize toc entries.
7805 if (entry
.sym_
->is_from_dynobj())
7807 Powerpc_relobj
<size
, big_endian
>* ppc_object
7808 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(entry
.relobj_
);
7809 if (entry
.shndx_
== ppc_object
->toc_shndx())
7810 ppc_object
->set_no_toc_opt(entry
.address_
);
7815 Copy_relocs
<sh_type
, size
, big_endian
>::emit(reloc_section
);
7818 // Return the value to use for a branch relocation.
7820 template<int size
, bool big_endian
>
7822 Target_powerpc
<size
, big_endian
>::symval_for_branch(
7823 const Symbol_table
* symtab
,
7824 const Sized_symbol
<size
>* gsym
,
7825 Powerpc_relobj
<size
, big_endian
>* object
,
7827 unsigned int *dest_shndx
)
7829 if (size
== 32 || this->abiversion() >= 2)
7833 // If the symbol is defined in an opd section, ie. is a function
7834 // descriptor, use the function descriptor code entry address
7835 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
7837 && (gsym
->source() != Symbol::FROM_OBJECT
7838 || gsym
->object()->is_dynamic()))
7841 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7842 unsigned int shndx
= symobj
->opd_shndx();
7845 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
7846 if (opd_addr
== invalid_address
)
7848 opd_addr
+= symobj
->output_section_address(shndx
);
7849 if (*value
>= opd_addr
&& *value
< opd_addr
+ symobj
->section_size(shndx
))
7852 *dest_shndx
= symobj
->get_opd_ent(*value
- opd_addr
, &sec_off
);
7853 if (symtab
->is_section_folded(symobj
, *dest_shndx
))
7856 = symtab
->icf()->get_folded_section(symobj
, *dest_shndx
);
7857 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(folded
.first
);
7858 *dest_shndx
= folded
.second
;
7860 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
7861 if (sec_addr
== invalid_address
)
7864 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
7865 *value
= sec_addr
+ sec_off
;
7870 // Perform a relocation.
7872 template<int size
, bool big_endian
>
7874 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
7875 const Relocate_info
<size
, big_endian
>* relinfo
,
7877 Target_powerpc
* target
,
7880 const unsigned char* preloc
,
7881 const Sized_symbol
<size
>* gsym
,
7882 const Symbol_value
<size
>* psymval
,
7883 unsigned char* view
,
7885 section_size_type view_size
)
7890 const elfcpp::Rela
<size
, big_endian
> rela(preloc
);
7891 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
7892 switch (this->maybe_skip_tls_get_addr_call(r_type
, gsym
))
7894 case Track_tls::NOT_EXPECTED
:
7895 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7896 _("__tls_get_addr call lacks marker reloc"));
7898 case Track_tls::EXPECTED
:
7899 // We have already complained.
7901 case Track_tls::SKIP
:
7903 case Track_tls::NORMAL
:
7907 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
7908 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
7909 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
7910 // Offset from start of insn to d-field reloc.
7911 const int d_offset
= big_endian
? 2 : 0;
7913 Powerpc_relobj
<size
, big_endian
>* const object
7914 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
7916 bool has_stub_value
= false;
7917 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7919 ? gsym
->use_plt_offset(Scan::get_reference_flags(r_type
, target
))
7920 : object
->local_has_plt_offset(r_sym
))
7921 && (!psymval
->is_ifunc_symbol()
7922 || Scan::reloc_needs_plt_for_ifunc(target
, object
, r_type
, false)))
7926 && target
->abiversion() >= 2
7927 && !parameters
->options().output_is_position_independent()
7928 && !is_branch_reloc(r_type
))
7930 Address off
= target
->glink_section()->find_global_entry(gsym
);
7931 if (off
!= invalid_address
)
7933 value
= target
->glink_section()->global_entry_address() + off
;
7934 has_stub_value
= true;
7939 Stub_table
<size
, big_endian
>* stub_table
7940 = object
->stub_table(relinfo
->data_shndx
);
7941 if (stub_table
== NULL
)
7943 // This is a ref from a data section to an ifunc symbol.
7944 if (target
->stub_tables().size() != 0)
7945 stub_table
= target
->stub_tables()[0];
7947 if (stub_table
!= NULL
)
7949 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
;
7951 ent
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
7952 rela
.get_r_addend());
7954 ent
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
7955 rela
.get_r_addend());
7958 value
= stub_table
->stub_address() + ent
->off_
;
7959 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
7960 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->reloc_shdr
);
7961 size_t reloc_count
= shdr
.get_sh_size() / reloc_size
;
7964 && relnum
+ 1 < reloc_count
)
7966 Reltype
next_rela(preloc
+ reloc_size
);
7967 if (elfcpp::elf_r_type
<size
>(next_rela
.get_r_info())
7968 == elfcpp::R_PPC64_TOCSAVE
7969 && next_rela
.get_r_offset() == rela
.get_r_offset() + 4)
7972 has_stub_value
= true;
7976 // We don't care too much about bogus debug references to
7977 // non-local functions, but otherwise there had better be a plt
7978 // call stub or global entry stub as appropriate.
7979 gold_assert(has_stub_value
|| !(os
->flags() & elfcpp::SHF_ALLOC
));
7982 if (r_type
== elfcpp::R_POWERPC_GOT16
7983 || r_type
== elfcpp::R_POWERPC_GOT16_LO
7984 || r_type
== elfcpp::R_POWERPC_GOT16_HI
7985 || r_type
== elfcpp::R_POWERPC_GOT16_HA
7986 || r_type
== elfcpp::R_PPC64_GOT16_DS
7987 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
7991 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
7992 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
7996 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
7997 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
7999 value
-= target
->got_section()->got_base_offset(object
);
8001 else if (r_type
== elfcpp::R_PPC64_TOC
)
8003 value
= (target
->got_section()->output_section()->address()
8004 + object
->toc_base_offset());
8006 else if (gsym
!= NULL
8007 && (r_type
== elfcpp::R_POWERPC_REL24
8008 || r_type
== elfcpp::R_PPC_PLTREL24
)
8013 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
8014 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
8015 bool can_plt_call
= false;
8016 if (rela
.get_r_offset() + 8 <= view_size
)
8018 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
8019 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
8022 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
8024 elfcpp::Swap
<32, big_endian
>::
8025 writeval(wv
+ 1, ld_2_1
+ target
->stk_toc());
8026 can_plt_call
= true;
8031 // If we don't have a branch and link followed by a nop,
8032 // we can't go via the plt because there is no place to
8033 // put a toc restoring instruction.
8034 // Unless we know we won't be returning.
8035 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
8036 can_plt_call
= true;
8040 // g++ as of 20130507 emits self-calls without a
8041 // following nop. This is arguably wrong since we have
8042 // conflicting information. On the one hand a global
8043 // symbol and on the other a local call sequence, but
8044 // don't error for this special case.
8045 // It isn't possible to cheaply verify we have exactly
8046 // such a call. Allow all calls to the same section.
8048 Address code
= value
;
8049 if (gsym
->source() == Symbol::FROM_OBJECT
8050 && gsym
->object() == object
)
8052 unsigned int dest_shndx
= 0;
8053 if (target
->abiversion() < 2)
8055 Address addend
= rela
.get_r_addend();
8056 code
= psymval
->value(object
, addend
);
8057 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
8058 &code
, &dest_shndx
);
8061 if (dest_shndx
== 0)
8062 dest_shndx
= gsym
->shndx(&is_ordinary
);
8063 ok
= dest_shndx
== relinfo
->data_shndx
;
8067 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8068 _("call lacks nop, can't restore toc; "
8069 "recompile with -fPIC"));
8075 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8076 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
8077 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
8078 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
8080 // First instruction of a global dynamic sequence, arg setup insn.
8081 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8082 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
8083 enum Got_type got_type
= GOT_TYPE_STANDARD
;
8084 if (tls_type
== tls::TLSOPT_NONE
)
8085 got_type
= GOT_TYPE_TLSGD
;
8086 else if (tls_type
== tls::TLSOPT_TO_IE
)
8087 got_type
= GOT_TYPE_TPREL
;
8088 if (got_type
!= GOT_TYPE_STANDARD
)
8092 gold_assert(gsym
->has_got_offset(got_type
));
8093 value
= gsym
->got_offset(got_type
);
8097 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
8098 value
= object
->local_got_offset(r_sym
, got_type
);
8100 value
-= target
->got_section()->got_base_offset(object
);
8102 if (tls_type
== tls::TLSOPT_TO_IE
)
8104 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8105 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
8107 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8108 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8109 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
8111 insn
|= 32 << 26; // lwz
8113 insn
|= 58 << 26; // ld
8114 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8116 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
8117 - elfcpp::R_POWERPC_GOT_TLSGD16
);
8119 else if (tls_type
== tls::TLSOPT_TO_LE
)
8121 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8122 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
8124 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8125 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8126 insn
&= (1 << 26) - (1 << 21); // extract rt
8131 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8132 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8133 value
= psymval
->value(object
, rela
.get_r_addend());
8137 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8139 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8140 r_type
= elfcpp::R_POWERPC_NONE
;
8144 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
8145 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
8146 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
8147 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
8149 // First instruction of a local dynamic sequence, arg setup insn.
8150 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
8151 if (tls_type
== tls::TLSOPT_NONE
)
8153 value
= target
->tlsld_got_offset();
8154 value
-= target
->got_section()->got_base_offset(object
);
8158 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
8159 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
8160 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
8162 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8163 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8164 insn
&= (1 << 26) - (1 << 21); // extract rt
8169 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8170 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8175 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8177 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8178 r_type
= elfcpp::R_POWERPC_NONE
;
8182 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
8183 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
8184 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
8185 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
8187 // Accesses relative to a local dynamic sequence address,
8188 // no optimisation here.
8191 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
8192 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
8196 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
8197 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
8199 value
-= target
->got_section()->got_base_offset(object
);
8201 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8202 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
8203 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
8204 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
8206 // First instruction of initial exec sequence.
8207 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8208 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
8209 if (tls_type
== tls::TLSOPT_NONE
)
8213 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
8214 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
8218 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
8219 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
8221 value
-= target
->got_section()->got_base_offset(object
);
8225 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
8226 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8227 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
8229 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8230 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8231 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
8236 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8237 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8238 value
= psymval
->value(object
, rela
.get_r_addend());
8242 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8244 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8245 r_type
= elfcpp::R_POWERPC_NONE
;
8249 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
8250 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
8252 // Second instruction of a global dynamic sequence,
8253 // the __tls_get_addr call
8254 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
8255 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8256 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
8257 if (tls_type
!= tls::TLSOPT_NONE
)
8259 if (tls_type
== tls::TLSOPT_TO_IE
)
8261 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8262 Insn insn
= add_3_3_13
;
8265 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8266 r_type
= elfcpp::R_POWERPC_NONE
;
8270 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8271 Insn insn
= addi_3_3
;
8272 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8273 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8275 value
= psymval
->value(object
, rela
.get_r_addend());
8277 this->skip_next_tls_get_addr_call();
8280 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
8281 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
8283 // Second instruction of a local dynamic sequence,
8284 // the __tls_get_addr call
8285 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
8286 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
8287 if (tls_type
== tls::TLSOPT_TO_LE
)
8289 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8290 Insn insn
= addi_3_3
;
8291 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8292 this->skip_next_tls_get_addr_call();
8293 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8298 else if (r_type
== elfcpp::R_POWERPC_TLS
)
8300 // Second instruction of an initial exec sequence
8301 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8302 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
8303 if (tls_type
== tls::TLSOPT_TO_LE
)
8305 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8306 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8307 unsigned int reg
= size
== 32 ? 2 : 13;
8308 insn
= at_tls_transform(insn
, reg
);
8309 gold_assert(insn
!= 0);
8310 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8311 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8313 value
= psymval
->value(object
, rela
.get_r_addend());
8316 else if (!has_stub_value
)
8319 if (!(size
== 32 && r_type
== elfcpp::R_PPC_PLTREL24
))
8320 addend
= rela
.get_r_addend();
8321 value
= psymval
->value(object
, addend
);
8322 if (size
== 64 && is_branch_reloc(r_type
))
8324 if (target
->abiversion() >= 2)
8327 value
+= object
->ppc64_local_entry_offset(gsym
);
8329 value
+= object
->ppc64_local_entry_offset(r_sym
);
8333 unsigned int dest_shndx
;
8334 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
8335 &value
, &dest_shndx
);
8338 Address max_branch_offset
= max_branch_delta(r_type
);
8339 if (max_branch_offset
!= 0
8340 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
8342 Stub_table
<size
, big_endian
>* stub_table
8343 = object
->stub_table(relinfo
->data_shndx
);
8344 if (stub_table
!= NULL
)
8346 Address off
= stub_table
->find_long_branch_entry(object
, value
);
8347 if (off
!= invalid_address
)
8349 value
= (stub_table
->stub_address() + stub_table
->plt_size()
8351 has_stub_value
= true;
8359 case elfcpp::R_PPC64_REL64
:
8360 case elfcpp::R_POWERPC_REL32
:
8361 case elfcpp::R_POWERPC_REL24
:
8362 case elfcpp::R_PPC_PLTREL24
:
8363 case elfcpp::R_PPC_LOCAL24PC
:
8364 case elfcpp::R_POWERPC_REL16
:
8365 case elfcpp::R_POWERPC_REL16_LO
:
8366 case elfcpp::R_POWERPC_REL16_HI
:
8367 case elfcpp::R_POWERPC_REL16_HA
:
8368 case elfcpp::R_POWERPC_REL16DX_HA
:
8369 case elfcpp::R_POWERPC_REL14
:
8370 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8371 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8375 case elfcpp::R_PPC64_TOC16
:
8376 case elfcpp::R_PPC64_TOC16_LO
:
8377 case elfcpp::R_PPC64_TOC16_HI
:
8378 case elfcpp::R_PPC64_TOC16_HA
:
8379 case elfcpp::R_PPC64_TOC16_DS
:
8380 case elfcpp::R_PPC64_TOC16_LO_DS
:
8381 // Subtract the TOC base address.
8382 value
-= (target
->got_section()->output_section()->address()
8383 + object
->toc_base_offset());
8386 case elfcpp::R_POWERPC_SECTOFF
:
8387 case elfcpp::R_POWERPC_SECTOFF_LO
:
8388 case elfcpp::R_POWERPC_SECTOFF_HI
:
8389 case elfcpp::R_POWERPC_SECTOFF_HA
:
8390 case elfcpp::R_PPC64_SECTOFF_DS
:
8391 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
8393 value
-= os
->address();
8396 case elfcpp::R_PPC64_TPREL16_DS
:
8397 case elfcpp::R_PPC64_TPREL16_LO_DS
:
8398 case elfcpp::R_PPC64_TPREL16_HIGH
:
8399 case elfcpp::R_PPC64_TPREL16_HIGHA
:
8401 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
8404 case elfcpp::R_POWERPC_TPREL16
:
8405 case elfcpp::R_POWERPC_TPREL16_LO
:
8406 case elfcpp::R_POWERPC_TPREL16_HI
:
8407 case elfcpp::R_POWERPC_TPREL16_HA
:
8408 case elfcpp::R_POWERPC_TPREL
:
8409 case elfcpp::R_PPC64_TPREL16_HIGHER
:
8410 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
8411 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
8412 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
8413 // tls symbol values are relative to tls_segment()->vaddr()
8417 case elfcpp::R_PPC64_DTPREL16_DS
:
8418 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
8419 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
8420 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
8421 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
8422 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
8424 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
8425 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
8428 case elfcpp::R_POWERPC_DTPREL16
:
8429 case elfcpp::R_POWERPC_DTPREL16_LO
:
8430 case elfcpp::R_POWERPC_DTPREL16_HI
:
8431 case elfcpp::R_POWERPC_DTPREL16_HA
:
8432 case elfcpp::R_POWERPC_DTPREL
:
8433 case elfcpp::R_PPC64_DTPREL16_HIGH
:
8434 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
8435 // tls symbol values are relative to tls_segment()->vaddr()
8436 value
-= dtp_offset
;
8439 case elfcpp::R_PPC64_ADDR64_LOCAL
:
8441 value
+= object
->ppc64_local_entry_offset(gsym
);
8443 value
+= object
->ppc64_local_entry_offset(r_sym
);
8450 Insn branch_bit
= 0;
8453 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
8454 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8455 branch_bit
= 1 << 21;
8457 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
8458 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8460 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8461 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8464 if (this->is_isa_v2
)
8466 // Set 'a' bit. This is 0b00010 in BO field for branch
8467 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
8468 // for branch on CTR insns (BO == 1a00t or 1a01t).
8469 if ((insn
& (0x14 << 21)) == (0x04 << 21))
8471 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
8478 // Invert 'y' bit if not the default.
8479 if (static_cast<Signed_address
>(value
) < 0)
8482 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8497 // Multi-instruction sequences that access the GOT/TOC can
8498 // be optimized, eg.
8499 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
8500 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
8502 // addis ra,r2,0; addi rb,ra,x@toc@l;
8503 // to nop; addi rb,r2,x@toc;
8504 // FIXME: the @got sequence shown above is not yet
8505 // optimized. Note that gcc as of 2017-01-07 doesn't use
8506 // the ELF @got relocs except for TLS, instead using the
8507 // PowerOpen variant of a compiler managed GOT (called TOC).
8508 // The PowerOpen TOC sequence equivalent to the first
8509 // example is optimized.
8510 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
8511 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
8512 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
8513 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
8514 case elfcpp::R_POWERPC_GOT16_HA
:
8515 case elfcpp::R_PPC64_TOC16_HA
:
8516 if (parameters
->options().toc_optimize())
8518 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8519 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8520 if (r_type
== elfcpp::R_PPC64_TOC16_HA
8521 && object
->make_toc_relative(target
, &value
))
8523 gold_assert((insn
& ((0x3f << 26) | 0x1f << 16))
8524 == ((15u << 26) | (2 << 16)));
8526 if (((insn
& ((0x3f << 26) | 0x1f << 16))
8527 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8528 && value
+ 0x8000 < 0x10000)
8530 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
8536 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
8537 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
8538 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
8539 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
8540 case elfcpp::R_POWERPC_GOT16_LO
:
8541 case elfcpp::R_PPC64_GOT16_LO_DS
:
8542 case elfcpp::R_PPC64_TOC16_LO
:
8543 case elfcpp::R_PPC64_TOC16_LO_DS
:
8544 if (parameters
->options().toc_optimize())
8546 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8547 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8548 bool changed
= false;
8549 if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
8550 && object
->make_toc_relative(target
, &value
))
8552 gold_assert ((insn
& (0x3f << 26)) == 58u << 26 /* ld */);
8553 insn
^= (14u << 26) ^ (58u << 26);
8554 r_type
= elfcpp::R_PPC64_TOC16_LO
;
8557 if (ok_lo_toc_insn(insn
, r_type
)
8558 && value
+ 0x8000 < 0x10000)
8560 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
8562 // Transform addic to addi when we change reg.
8563 insn
&= ~((0x3f << 26) | (0x1f << 16));
8564 insn
|= (14u << 26) | (2 << 16);
8568 insn
&= ~(0x1f << 16);
8574 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8578 case elfcpp::R_PPC64_ENTRY
:
8579 value
= (target
->got_section()->output_section()->address()
8580 + object
->toc_base_offset());
8581 if (value
+ 0x80008000 <= 0xffffffff
8582 && !parameters
->options().output_is_position_independent())
8584 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8585 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8586 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
8588 if ((insn1
& ~0xfffc) == ld_2_12
8589 && insn2
== add_2_2_12
)
8591 insn1
= lis_2
+ ha(value
);
8592 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
8593 insn2
= addi_2_2
+ l(value
);
8594 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
8601 if (value
+ 0x80008000 <= 0xffffffff)
8603 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8604 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8605 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
8607 if ((insn1
& ~0xfffc) == ld_2_12
8608 && insn2
== add_2_2_12
)
8610 insn1
= addis_2_12
+ ha(value
);
8611 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
8612 insn2
= addi_2_2
+ l(value
);
8613 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
8620 case elfcpp::R_POWERPC_REL16_LO
:
8621 // If we are generating a non-PIC executable, edit
8622 // 0: addis 2,12,.TOC.-0b@ha
8623 // addi 2,2,.TOC.-0b@l
8624 // used by ELFv2 global entry points to set up r2, to
8627 // if .TOC. is in range. */
8628 if (value
+ address
- 4 + 0x80008000 <= 0xffffffff
8631 && target
->abiversion() >= 2
8632 && !parameters
->options().output_is_position_independent()
8633 && rela
.get_r_addend() == d_offset
+ 4
8635 && strcmp(gsym
->name(), ".TOC.") == 0)
8637 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
8638 Reltype
prev_rela(preloc
- reloc_size
);
8639 if ((prev_rela
.get_r_info()
8640 == elfcpp::elf_r_info
<size
>(r_sym
,
8641 elfcpp::R_POWERPC_REL16_HA
))
8642 && prev_rela
.get_r_offset() + 4 == rela
.get_r_offset()
8643 && prev_rela
.get_r_addend() + 4 == rela
.get_r_addend())
8645 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8646 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
- 1);
8647 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8649 if ((insn1
& 0xffff0000) == addis_2_12
8650 && (insn2
& 0xffff0000) == addi_2_2
)
8652 insn1
= lis_2
+ ha(value
+ address
- 4);
8653 elfcpp::Swap
<32, big_endian
>::writeval(iview
- 1, insn1
);
8654 insn2
= addi_2_2
+ l(value
+ address
- 4);
8655 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn2
);
8658 relinfo
->rr
->set_strategy(relnum
- 1,
8659 Relocatable_relocs::RELOC_SPECIAL
);
8660 relinfo
->rr
->set_strategy(relnum
,
8661 Relocatable_relocs::RELOC_SPECIAL
);
8671 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
8672 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->data_shdr
);
8675 case elfcpp::R_POWERPC_ADDR32
:
8676 case elfcpp::R_POWERPC_UADDR32
:
8678 overflow
= Reloc::CHECK_BITFIELD
;
8681 case elfcpp::R_POWERPC_REL32
:
8682 case elfcpp::R_POWERPC_REL16DX_HA
:
8684 overflow
= Reloc::CHECK_SIGNED
;
8687 case elfcpp::R_POWERPC_UADDR16
:
8688 overflow
= Reloc::CHECK_BITFIELD
;
8691 case elfcpp::R_POWERPC_ADDR16
:
8692 // We really should have three separate relocations,
8693 // one for 16-bit data, one for insns with 16-bit signed fields,
8694 // and one for insns with 16-bit unsigned fields.
8695 overflow
= Reloc::CHECK_BITFIELD
;
8696 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
8697 overflow
= Reloc::CHECK_LOW_INSN
;
8700 case elfcpp::R_POWERPC_ADDR16_HI
:
8701 case elfcpp::R_POWERPC_ADDR16_HA
:
8702 case elfcpp::R_POWERPC_GOT16_HI
:
8703 case elfcpp::R_POWERPC_GOT16_HA
:
8704 case elfcpp::R_POWERPC_PLT16_HI
:
8705 case elfcpp::R_POWERPC_PLT16_HA
:
8706 case elfcpp::R_POWERPC_SECTOFF_HI
:
8707 case elfcpp::R_POWERPC_SECTOFF_HA
:
8708 case elfcpp::R_PPC64_TOC16_HI
:
8709 case elfcpp::R_PPC64_TOC16_HA
:
8710 case elfcpp::R_PPC64_PLTGOT16_HI
:
8711 case elfcpp::R_PPC64_PLTGOT16_HA
:
8712 case elfcpp::R_POWERPC_TPREL16_HI
:
8713 case elfcpp::R_POWERPC_TPREL16_HA
:
8714 case elfcpp::R_POWERPC_DTPREL16_HI
:
8715 case elfcpp::R_POWERPC_DTPREL16_HA
:
8716 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
8717 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
8718 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
8719 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
8720 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
8721 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
8722 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
8723 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
8724 case elfcpp::R_POWERPC_REL16_HI
:
8725 case elfcpp::R_POWERPC_REL16_HA
:
8727 overflow
= Reloc::CHECK_HIGH_INSN
;
8730 case elfcpp::R_POWERPC_REL16
:
8731 case elfcpp::R_PPC64_TOC16
:
8732 case elfcpp::R_POWERPC_GOT16
:
8733 case elfcpp::R_POWERPC_SECTOFF
:
8734 case elfcpp::R_POWERPC_TPREL16
:
8735 case elfcpp::R_POWERPC_DTPREL16
:
8736 case elfcpp::R_POWERPC_GOT_TLSGD16
:
8737 case elfcpp::R_POWERPC_GOT_TLSLD16
:
8738 case elfcpp::R_POWERPC_GOT_TPREL16
:
8739 case elfcpp::R_POWERPC_GOT_DTPREL16
:
8740 overflow
= Reloc::CHECK_LOW_INSN
;
8743 case elfcpp::R_POWERPC_ADDR24
:
8744 case elfcpp::R_POWERPC_ADDR14
:
8745 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
8746 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
8747 case elfcpp::R_PPC64_ADDR16_DS
:
8748 case elfcpp::R_POWERPC_REL24
:
8749 case elfcpp::R_PPC_PLTREL24
:
8750 case elfcpp::R_PPC_LOCAL24PC
:
8751 case elfcpp::R_PPC64_TPREL16_DS
:
8752 case elfcpp::R_PPC64_DTPREL16_DS
:
8753 case elfcpp::R_PPC64_TOC16_DS
:
8754 case elfcpp::R_PPC64_GOT16_DS
:
8755 case elfcpp::R_PPC64_SECTOFF_DS
:
8756 case elfcpp::R_POWERPC_REL14
:
8757 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8758 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8759 overflow
= Reloc::CHECK_SIGNED
;
8763 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8766 if (overflow
== Reloc::CHECK_LOW_INSN
8767 || overflow
== Reloc::CHECK_HIGH_INSN
)
8769 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8771 if ((insn
& (0x3f << 26)) == 10u << 26 /* cmpli */)
8772 overflow
= Reloc::CHECK_BITFIELD
;
8773 else if (overflow
== Reloc::CHECK_LOW_INSN
8774 ? ((insn
& (0x3f << 26)) == 28u << 26 /* andi */
8775 || (insn
& (0x3f << 26)) == 24u << 26 /* ori */
8776 || (insn
& (0x3f << 26)) == 26u << 26 /* xori */)
8777 : ((insn
& (0x3f << 26)) == 29u << 26 /* andis */
8778 || (insn
& (0x3f << 26)) == 25u << 26 /* oris */
8779 || (insn
& (0x3f << 26)) == 27u << 26 /* xoris */))
8780 overflow
= Reloc::CHECK_UNSIGNED
;
8782 overflow
= Reloc::CHECK_SIGNED
;
8785 bool maybe_dq_reloc
= false;
8786 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
8787 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
8790 case elfcpp::R_POWERPC_NONE
:
8791 case elfcpp::R_POWERPC_TLS
:
8792 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
8793 case elfcpp::R_POWERPC_GNU_VTENTRY
:
8796 case elfcpp::R_PPC64_ADDR64
:
8797 case elfcpp::R_PPC64_REL64
:
8798 case elfcpp::R_PPC64_TOC
:
8799 case elfcpp::R_PPC64_ADDR64_LOCAL
:
8800 Reloc::addr64(view
, value
);
8803 case elfcpp::R_POWERPC_TPREL
:
8804 case elfcpp::R_POWERPC_DTPREL
:
8806 Reloc::addr64(view
, value
);
8808 status
= Reloc::addr32(view
, value
, overflow
);
8811 case elfcpp::R_PPC64_UADDR64
:
8812 Reloc::addr64_u(view
, value
);
8815 case elfcpp::R_POWERPC_ADDR32
:
8816 status
= Reloc::addr32(view
, value
, overflow
);
8819 case elfcpp::R_POWERPC_REL32
:
8820 case elfcpp::R_POWERPC_UADDR32
:
8821 status
= Reloc::addr32_u(view
, value
, overflow
);
8824 case elfcpp::R_POWERPC_ADDR24
:
8825 case elfcpp::R_POWERPC_REL24
:
8826 case elfcpp::R_PPC_PLTREL24
:
8827 case elfcpp::R_PPC_LOCAL24PC
:
8828 status
= Reloc::addr24(view
, value
, overflow
);
8831 case elfcpp::R_POWERPC_GOT_DTPREL16
:
8832 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
8833 case elfcpp::R_POWERPC_GOT_TPREL16
:
8834 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
8837 // On ppc64 these are all ds form
8838 maybe_dq_reloc
= true;
8842 case elfcpp::R_POWERPC_ADDR16
:
8843 case elfcpp::R_POWERPC_REL16
:
8844 case elfcpp::R_PPC64_TOC16
:
8845 case elfcpp::R_POWERPC_GOT16
:
8846 case elfcpp::R_POWERPC_SECTOFF
:
8847 case elfcpp::R_POWERPC_TPREL16
:
8848 case elfcpp::R_POWERPC_DTPREL16
:
8849 case elfcpp::R_POWERPC_GOT_TLSGD16
:
8850 case elfcpp::R_POWERPC_GOT_TLSLD16
:
8851 case elfcpp::R_POWERPC_ADDR16_LO
:
8852 case elfcpp::R_POWERPC_REL16_LO
:
8853 case elfcpp::R_PPC64_TOC16_LO
:
8854 case elfcpp::R_POWERPC_GOT16_LO
:
8855 case elfcpp::R_POWERPC_SECTOFF_LO
:
8856 case elfcpp::R_POWERPC_TPREL16_LO
:
8857 case elfcpp::R_POWERPC_DTPREL16_LO
:
8858 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
8859 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
8861 status
= Reloc::addr16(view
, value
, overflow
);
8863 maybe_dq_reloc
= true;
8866 case elfcpp::R_POWERPC_UADDR16
:
8867 status
= Reloc::addr16_u(view
, value
, overflow
);
8870 case elfcpp::R_PPC64_ADDR16_HIGH
:
8871 case elfcpp::R_PPC64_TPREL16_HIGH
:
8872 case elfcpp::R_PPC64_DTPREL16_HIGH
:
8874 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
8877 case elfcpp::R_POWERPC_ADDR16_HI
:
8878 case elfcpp::R_POWERPC_REL16_HI
:
8879 case elfcpp::R_PPC64_TOC16_HI
:
8880 case elfcpp::R_POWERPC_GOT16_HI
:
8881 case elfcpp::R_POWERPC_SECTOFF_HI
:
8882 case elfcpp::R_POWERPC_TPREL16_HI
:
8883 case elfcpp::R_POWERPC_DTPREL16_HI
:
8884 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
8885 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
8886 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
8887 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
8888 Reloc::addr16_hi(view
, value
);
8891 case elfcpp::R_PPC64_ADDR16_HIGHA
:
8892 case elfcpp::R_PPC64_TPREL16_HIGHA
:
8893 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
8895 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
8898 case elfcpp::R_POWERPC_ADDR16_HA
:
8899 case elfcpp::R_POWERPC_REL16_HA
:
8900 case elfcpp::R_PPC64_TOC16_HA
:
8901 case elfcpp::R_POWERPC_GOT16_HA
:
8902 case elfcpp::R_POWERPC_SECTOFF_HA
:
8903 case elfcpp::R_POWERPC_TPREL16_HA
:
8904 case elfcpp::R_POWERPC_DTPREL16_HA
:
8905 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
8906 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
8907 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
8908 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
8909 Reloc::addr16_ha(view
, value
);
8912 case elfcpp::R_POWERPC_REL16DX_HA
:
8913 status
= Reloc::addr16dx_ha(view
, value
, overflow
);
8916 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
8918 // R_PPC_EMB_NADDR16_LO
8921 case elfcpp::R_PPC64_ADDR16_HIGHER
:
8922 case elfcpp::R_PPC64_TPREL16_HIGHER
:
8923 Reloc::addr16_hi2(view
, value
);
8926 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
8928 // R_PPC_EMB_NADDR16_HI
8931 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
8932 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
8933 Reloc::addr16_ha2(view
, value
);
8936 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
8938 // R_PPC_EMB_NADDR16_HA
8941 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
8942 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
8943 Reloc::addr16_hi3(view
, value
);
8946 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
8951 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
8952 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
8953 Reloc::addr16_ha3(view
, value
);
8956 case elfcpp::R_PPC64_DTPREL16_DS
:
8957 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
8959 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
8962 case elfcpp::R_PPC64_TPREL16_DS
:
8963 case elfcpp::R_PPC64_TPREL16_LO_DS
:
8965 // R_PPC_TLSGD, R_PPC_TLSLD
8968 case elfcpp::R_PPC64_ADDR16_DS
:
8969 case elfcpp::R_PPC64_ADDR16_LO_DS
:
8970 case elfcpp::R_PPC64_TOC16_DS
:
8971 case elfcpp::R_PPC64_TOC16_LO_DS
:
8972 case elfcpp::R_PPC64_GOT16_DS
:
8973 case elfcpp::R_PPC64_GOT16_LO_DS
:
8974 case elfcpp::R_PPC64_SECTOFF_DS
:
8975 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
8976 maybe_dq_reloc
= true;
8979 case elfcpp::R_POWERPC_ADDR14
:
8980 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
8981 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
8982 case elfcpp::R_POWERPC_REL14
:
8983 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8984 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8985 status
= Reloc::addr14(view
, value
, overflow
);
8988 case elfcpp::R_POWERPC_COPY
:
8989 case elfcpp::R_POWERPC_GLOB_DAT
:
8990 case elfcpp::R_POWERPC_JMP_SLOT
:
8991 case elfcpp::R_POWERPC_RELATIVE
:
8992 case elfcpp::R_POWERPC_DTPMOD
:
8993 case elfcpp::R_PPC64_JMP_IREL
:
8994 case elfcpp::R_POWERPC_IRELATIVE
:
8995 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8996 _("unexpected reloc %u in object file"),
9000 case elfcpp::R_PPC64_TOCSAVE
:
9006 Symbol_location loc
;
9007 loc
.object
= relinfo
->object
;
9008 loc
.shndx
= relinfo
->data_shndx
;
9009 loc
.offset
= rela
.get_r_offset();
9010 Tocsave_loc::const_iterator p
= target
->tocsave_loc().find(loc
);
9011 if (p
!= target
->tocsave_loc().end())
9013 // If we've generated plt calls using this tocsave, then
9014 // the nop needs to be changed to save r2.
9015 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9016 if (elfcpp::Swap
<32, big_endian
>::readval(iview
) == nop
)
9017 elfcpp::Swap
<32, big_endian
>::
9018 writeval(iview
, std_2_1
+ target
->stk_toc());
9023 case elfcpp::R_PPC_EMB_SDA2I16
:
9024 case elfcpp::R_PPC_EMB_SDA2REL
:
9027 // R_PPC64_TLSGD, R_PPC64_TLSLD
9030 case elfcpp::R_POWERPC_PLT32
:
9031 case elfcpp::R_POWERPC_PLTREL32
:
9032 case elfcpp::R_POWERPC_PLT16_LO
:
9033 case elfcpp::R_POWERPC_PLT16_HI
:
9034 case elfcpp::R_POWERPC_PLT16_HA
:
9035 case elfcpp::R_PPC_SDAREL16
:
9036 case elfcpp::R_POWERPC_ADDR30
:
9037 case elfcpp::R_PPC64_PLT64
:
9038 case elfcpp::R_PPC64_PLTREL64
:
9039 case elfcpp::R_PPC64_PLTGOT16
:
9040 case elfcpp::R_PPC64_PLTGOT16_LO
:
9041 case elfcpp::R_PPC64_PLTGOT16_HI
:
9042 case elfcpp::R_PPC64_PLTGOT16_HA
:
9043 case elfcpp::R_PPC64_PLT16_LO_DS
:
9044 case elfcpp::R_PPC64_PLTGOT16_DS
:
9045 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
9046 case elfcpp::R_PPC_EMB_RELSDA
:
9047 case elfcpp::R_PPC_TOC16
:
9050 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
9051 _("unsupported reloc %u"),
9059 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9061 if ((insn
& (0x3f << 26)) == 56u << 26 /* lq */
9062 || ((insn
& (0x3f << 26)) == (61u << 26) /* lxv, stxv */
9063 && (insn
& 3) == 1))
9064 status
= Reloc::addr16_dq(view
, value
, overflow
);
9066 || (insn
& (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
9067 || (insn
& (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
9068 || (insn
& (0x3f << 26)) == 57u << 26 /* lfdp */
9069 || (insn
& (0x3f << 26)) == 61u << 26 /* stfdp */)
9070 status
= Reloc::addr16_ds(view
, value
, overflow
);
9072 status
= Reloc::addr16(view
, value
, overflow
);
9075 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
9078 && gsym
->is_undefined()
9079 && is_branch_reloc(r_type
))))
9081 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
9082 _("relocation overflow"));
9084 gold_info(_("try relinking with a smaller --stub-group-size"));
9090 // Relocate section data.
9092 template<int size
, bool big_endian
>
9094 Target_powerpc
<size
, big_endian
>::relocate_section(
9095 const Relocate_info
<size
, big_endian
>* relinfo
,
9096 unsigned int sh_type
,
9097 const unsigned char* prelocs
,
9099 Output_section
* output_section
,
9100 bool needs_special_offset_handling
,
9101 unsigned char* view
,
9103 section_size_type view_size
,
9104 const Reloc_symbol_changes
* reloc_symbol_changes
)
9106 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
9107 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
9108 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
9109 Powerpc_comdat_behavior
;
9110 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
9113 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9115 gold::relocate_section
<size
, big_endian
, Powerpc
, Powerpc_relocate
,
9116 Powerpc_comdat_behavior
, Classify_reloc
>(
9122 needs_special_offset_handling
,
9126 reloc_symbol_changes
);
9129 template<int size
, bool big_endian
>
9130 class Powerpc_scan_relocatable_reloc
9133 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
9134 static const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
9135 static const int sh_type
= elfcpp::SHT_RELA
;
9137 // Return the symbol referred to by the relocation.
9138 static inline unsigned int
9139 get_r_sym(const Reltype
* reloc
)
9140 { return elfcpp::elf_r_sym
<size
>(reloc
->get_r_info()); }
9142 // Return the type of the relocation.
9143 static inline unsigned int
9144 get_r_type(const Reltype
* reloc
)
9145 { return elfcpp::elf_r_type
<size
>(reloc
->get_r_info()); }
9147 // Return the strategy to use for a local symbol which is not a
9148 // section symbol, given the relocation type.
9149 inline Relocatable_relocs::Reloc_strategy
9150 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
9152 if (r_type
== 0 && r_sym
== 0)
9153 return Relocatable_relocs::RELOC_DISCARD
;
9154 return Relocatable_relocs::RELOC_COPY
;
9157 // Return the strategy to use for a local symbol which is a section
9158 // symbol, given the relocation type.
9159 inline Relocatable_relocs::Reloc_strategy
9160 local_section_strategy(unsigned int, Relobj
*)
9162 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
9165 // Return the strategy to use for a global symbol, given the
9166 // relocation type, the object, and the symbol index.
9167 inline Relocatable_relocs::Reloc_strategy
9168 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
9170 if (r_type
== elfcpp::R_PPC_PLTREL24
)
9171 return Relocatable_relocs::RELOC_SPECIAL
;
9172 return Relocatable_relocs::RELOC_COPY
;
9176 // Scan the relocs during a relocatable link.
9178 template<int size
, bool big_endian
>
9180 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
9181 Symbol_table
* symtab
,
9183 Sized_relobj_file
<size
, big_endian
>* object
,
9184 unsigned int data_shndx
,
9185 unsigned int sh_type
,
9186 const unsigned char* prelocs
,
9188 Output_section
* output_section
,
9189 bool needs_special_offset_handling
,
9190 size_t local_symbol_count
,
9191 const unsigned char* plocal_symbols
,
9192 Relocatable_relocs
* rr
)
9194 typedef Powerpc_scan_relocatable_reloc
<size
, big_endian
> Scan_strategy
;
9196 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9198 gold::scan_relocatable_relocs
<size
, big_endian
, Scan_strategy
>(
9206 needs_special_offset_handling
,
9212 // Scan the relocs for --emit-relocs.
9214 template<int size
, bool big_endian
>
9216 Target_powerpc
<size
, big_endian
>::emit_relocs_scan(
9217 Symbol_table
* symtab
,
9219 Sized_relobj_file
<size
, big_endian
>* object
,
9220 unsigned int data_shndx
,
9221 unsigned int sh_type
,
9222 const unsigned char* prelocs
,
9224 Output_section
* output_section
,
9225 bool needs_special_offset_handling
,
9226 size_t local_symbol_count
,
9227 const unsigned char* plocal_syms
,
9228 Relocatable_relocs
* rr
)
9230 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
9232 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
9233 Emit_relocs_strategy
;
9235 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9237 gold::scan_relocatable_relocs
<size
, big_endian
, Emit_relocs_strategy
>(
9245 needs_special_offset_handling
,
9251 // Emit relocations for a section.
9252 // This is a modified version of the function by the same name in
9253 // target-reloc.h. Using relocate_special_relocatable for
9254 // R_PPC_PLTREL24 would require duplication of the entire body of the
9255 // loop, so we may as well duplicate the whole thing.
9257 template<int size
, bool big_endian
>
9259 Target_powerpc
<size
, big_endian
>::relocate_relocs(
9260 const Relocate_info
<size
, big_endian
>* relinfo
,
9261 unsigned int sh_type
,
9262 const unsigned char* prelocs
,
9264 Output_section
* output_section
,
9265 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
9267 Address view_address
,
9269 unsigned char* reloc_view
,
9270 section_size_type reloc_view_size
)
9272 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9274 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
9275 typedef typename
elfcpp::Rela_write
<size
, big_endian
> Reltype_write
;
9276 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
9277 // Offset from start of insn to d-field reloc.
9278 const int d_offset
= big_endian
? 2 : 0;
9280 Powerpc_relobj
<size
, big_endian
>* const object
9281 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
9282 const unsigned int local_count
= object
->local_symbol_count();
9283 unsigned int got2_shndx
= object
->got2_shndx();
9284 Address got2_addend
= 0;
9285 if (got2_shndx
!= 0)
9287 got2_addend
= object
->get_output_section_offset(got2_shndx
);
9288 gold_assert(got2_addend
!= invalid_address
);
9291 unsigned char* pwrite
= reloc_view
;
9292 bool zap_next
= false;
9293 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
9295 Relocatable_relocs::Reloc_strategy strategy
= relinfo
->rr
->strategy(i
);
9296 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
9299 Reltype
reloc(prelocs
);
9300 Reltype_write
reloc_write(pwrite
);
9302 Address offset
= reloc
.get_r_offset();
9303 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
9304 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
9305 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
9306 const unsigned int orig_r_sym
= r_sym
;
9307 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
9308 = reloc
.get_r_addend();
9309 const Symbol
* gsym
= NULL
;
9313 // We could arrange to discard these and other relocs for
9314 // tls optimised sequences in the strategy methods, but for
9315 // now do as BFD ld does.
9316 r_type
= elfcpp::R_POWERPC_NONE
;
9320 // Get the new symbol index.
9321 Output_section
* os
= NULL
;
9322 if (r_sym
< local_count
)
9326 case Relocatable_relocs::RELOC_COPY
:
9327 case Relocatable_relocs::RELOC_SPECIAL
:
9330 r_sym
= object
->symtab_index(r_sym
);
9331 gold_assert(r_sym
!= -1U);
9335 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
9337 // We are adjusting a section symbol. We need to find
9338 // the symbol table index of the section symbol for
9339 // the output section corresponding to input section
9340 // in which this symbol is defined.
9341 gold_assert(r_sym
< local_count
);
9343 unsigned int shndx
=
9344 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
9345 gold_assert(is_ordinary
);
9346 os
= object
->output_section(shndx
);
9347 gold_assert(os
!= NULL
);
9348 gold_assert(os
->needs_symtab_index());
9349 r_sym
= os
->symtab_index();
9359 gsym
= object
->global_symbol(r_sym
);
9360 gold_assert(gsym
!= NULL
);
9361 if (gsym
->is_forwarder())
9362 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
9364 gold_assert(gsym
->has_symtab_index());
9365 r_sym
= gsym
->symtab_index();
9368 // Get the new offset--the location in the output section where
9369 // this relocation should be applied.
9370 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
9371 offset
+= offset_in_output_section
;
9374 section_offset_type sot_offset
=
9375 convert_types
<section_offset_type
, Address
>(offset
);
9376 section_offset_type new_sot_offset
=
9377 output_section
->output_offset(object
, relinfo
->data_shndx
,
9379 gold_assert(new_sot_offset
!= -1);
9380 offset
= new_sot_offset
;
9383 // In an object file, r_offset is an offset within the section.
9384 // In an executable or dynamic object, generated by
9385 // --emit-relocs, r_offset is an absolute address.
9386 if (!parameters
->options().relocatable())
9388 offset
+= view_address
;
9389 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
9390 offset
-= offset_in_output_section
;
9393 // Handle the reloc addend based on the strategy.
9394 if (strategy
== Relocatable_relocs::RELOC_COPY
)
9396 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
9398 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
9399 gold_assert(os
!= NULL
);
9400 addend
= psymval
->value(object
, addend
) - os
->address();
9402 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
9406 if (addend
>= 32768)
9407 addend
+= got2_addend
;
9409 else if (r_type
== elfcpp::R_POWERPC_REL16_HA
)
9411 r_type
= elfcpp::R_POWERPC_ADDR16_HA
;
9414 else if (r_type
== elfcpp::R_POWERPC_REL16_LO
)
9416 r_type
= elfcpp::R_POWERPC_ADDR16_LO
;
9417 addend
-= d_offset
+ 4;
9423 if (!parameters
->options().relocatable())
9425 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
9426 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
9427 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
9428 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
9430 // First instruction of a global dynamic sequence,
9432 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9433 switch (this->optimize_tls_gd(final
))
9435 case tls::TLSOPT_TO_IE
:
9436 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
9437 - elfcpp::R_POWERPC_GOT_TLSGD16
);
9439 case tls::TLSOPT_TO_LE
:
9440 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
9441 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
9442 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9445 r_type
= elfcpp::R_POWERPC_NONE
;
9453 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
9454 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
9455 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
9456 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
9458 // First instruction of a local dynamic sequence,
9460 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
9462 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
9463 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
9465 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9466 const Output_section
* os
= relinfo
->layout
->tls_segment()
9468 gold_assert(os
!= NULL
);
9469 gold_assert(os
->needs_symtab_index());
9470 r_sym
= os
->symtab_index();
9471 addend
= dtp_offset
;
9475 r_type
= elfcpp::R_POWERPC_NONE
;
9480 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
9481 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
9482 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
9483 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
9485 // First instruction of initial exec sequence.
9486 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9487 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
9489 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
9490 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
9491 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9494 r_type
= elfcpp::R_POWERPC_NONE
;
9499 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
9500 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
9502 // Second instruction of a global dynamic sequence,
9503 // the __tls_get_addr call
9504 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9505 switch (this->optimize_tls_gd(final
))
9507 case tls::TLSOPT_TO_IE
:
9508 r_type
= elfcpp::R_POWERPC_NONE
;
9511 case tls::TLSOPT_TO_LE
:
9512 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9520 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
9521 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
9523 // Second instruction of a local dynamic sequence,
9524 // the __tls_get_addr call
9525 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
9527 const Output_section
* os
= relinfo
->layout
->tls_segment()
9529 gold_assert(os
!= NULL
);
9530 gold_assert(os
->needs_symtab_index());
9531 r_sym
= os
->symtab_index();
9532 addend
= dtp_offset
;
9533 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9538 else if (r_type
== elfcpp::R_POWERPC_TLS
)
9540 // Second instruction of an initial exec sequence
9541 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9542 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
9544 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9550 reloc_write
.put_r_offset(offset
);
9551 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
9552 reloc_write
.put_r_addend(addend
);
9554 pwrite
+= reloc_size
;
9557 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
9558 == reloc_view_size
);
9561 // Return the value to use for a dynamic symbol which requires special
9562 // treatment. This is how we support equality comparisons of function
9563 // pointers across shared library boundaries, as described in the
9564 // processor specific ABI supplement.
9566 template<int size
, bool big_endian
>
9568 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
9572 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
9573 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
9574 p
!= this->stub_tables_
.end();
9577 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
9578 = (*p
)->find_plt_call_entry(gsym
);
9580 return (*p
)->stub_address() + ent
->off_
;
9583 else if (this->abiversion() >= 2)
9585 Address off
= this->glink_section()->find_global_entry(gsym
);
9586 if (off
!= invalid_address
)
9587 return this->glink_section()->global_entry_address() + off
;
9592 // Return the PLT address to use for a local symbol.
9593 template<int size
, bool big_endian
>
9595 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
9596 const Relobj
* object
,
9597 unsigned int symndx
) const
9601 const Sized_relobj
<size
, big_endian
>* relobj
9602 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
9603 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
9604 p
!= this->stub_tables_
.end();
9607 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
9608 = (*p
)->find_plt_call_entry(relobj
->sized_relobj(), symndx
);
9610 return (*p
)->stub_address() + ent
->off_
;
9616 // Return the PLT address to use for a global symbol.
9617 template<int size
, bool big_endian
>
9619 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
9620 const Symbol
* gsym
) const
9624 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
9625 p
!= this->stub_tables_
.end();
9628 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
9629 = (*p
)->find_plt_call_entry(gsym
);
9631 return (*p
)->stub_address() + ent
->off_
;
9634 else if (this->abiversion() >= 2)
9636 Address off
= this->glink_section()->find_global_entry(gsym
);
9637 if (off
!= invalid_address
)
9638 return this->glink_section()->global_entry_address() + off
;
9643 // Return the offset to use for the GOT_INDX'th got entry which is
9644 // for a local tls symbol specified by OBJECT, SYMNDX.
9645 template<int size
, bool big_endian
>
9647 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
9648 const Relobj
* object
,
9649 unsigned int symndx
,
9650 unsigned int got_indx
) const
9652 const Powerpc_relobj
<size
, big_endian
>* ppc_object
9653 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
9654 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
9656 for (Got_type got_type
= GOT_TYPE_TLSGD
;
9657 got_type
<= GOT_TYPE_TPREL
;
9658 got_type
= Got_type(got_type
+ 1))
9659 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
9661 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
9662 if (got_type
== GOT_TYPE_TLSGD
)
9664 if (off
== got_indx
* (size
/ 8))
9666 if (got_type
== GOT_TYPE_TPREL
)
9676 // Return the offset to use for the GOT_INDX'th got entry which is
9677 // for global tls symbol GSYM.
9678 template<int size
, bool big_endian
>
9680 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
9682 unsigned int got_indx
) const
9684 if (gsym
->type() == elfcpp::STT_TLS
)
9686 for (Got_type got_type
= GOT_TYPE_TLSGD
;
9687 got_type
<= GOT_TYPE_TPREL
;
9688 got_type
= Got_type(got_type
+ 1))
9689 if (gsym
->has_got_offset(got_type
))
9691 unsigned int off
= gsym
->got_offset(got_type
);
9692 if (got_type
== GOT_TYPE_TLSGD
)
9694 if (off
== got_indx
* (size
/ 8))
9696 if (got_type
== GOT_TYPE_TPREL
)
9706 // The selector for powerpc object files.
9708 template<int size
, bool big_endian
>
9709 class Target_selector_powerpc
: public Target_selector
9712 Target_selector_powerpc()
9713 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
9716 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
9717 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
9719 ? (big_endian
? "elf64ppc" : "elf64lppc")
9720 : (big_endian
? "elf32ppc" : "elf32lppc")))
9724 do_instantiate_target()
9725 { return new Target_powerpc
<size
, big_endian
>(); }
9728 Target_selector_powerpc
<32, true> target_selector_ppc32
;
9729 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
9730 Target_selector_powerpc
<64, true> target_selector_ppc64
;
9731 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
9733 // Instantiate these constants for -O0
9734 template<int size
, bool big_endian
>
9735 const int Output_data_glink
<size
, big_endian
>::pltresolve_size
;
9736 template<int size
, bool big_endian
>
9737 const typename Output_data_glink
<size
, big_endian
>::Address
9738 Output_data_glink
<size
, big_endian
>::invalid_address
;
9739 template<int size
, bool big_endian
>
9740 const typename Stub_table
<size
, big_endian
>::Address
9741 Stub_table
<size
, big_endian
>::invalid_address
;
9742 template<int size
, bool big_endian
>
9743 const typename Target_powerpc
<size
, big_endian
>::Address
9744 Target_powerpc
<size
, big_endian
>::invalid_address
;
9746 } // End anonymous namespace.