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 st_other (unsigned int symndx
) const
378 return this->st_other_
[symndx
];
382 ppc64_local_entry_offset(const Symbol
* sym
) const
383 { return elfcpp::ppc64_decode_local_entry(sym
->nonvis() >> 3); }
386 ppc64_local_entry_offset(unsigned int symndx
) const
387 { return elfcpp::ppc64_decode_local_entry(this->st_other_
[symndx
] >> 5); }
398 // Return index into opd_ent_ array for .opd entry at OFF.
399 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
400 // apart when the language doesn't use the last 8-byte word, the
401 // environment pointer. Thus dividing the entry section offset by
402 // 16 will give an index into opd_ent_ that works for either layout
403 // of .opd. (It leaves some elements of the vector unused when .opd
404 // entries are spaced 24 bytes apart, but we don't know the spacing
405 // until relocations are processed, and in any case it is possible
406 // for an object to have some entries spaced 16 bytes apart and
407 // others 24 bytes apart.)
409 opd_ent_ndx(size_t off
) const
412 // Per object unique identifier
415 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
416 unsigned int special_
;
418 // For 64-bit the .rela.toc and .toc section shdnx.
419 unsigned int relatoc_
;
422 // For 64-bit, whether this object uses small model relocs to access
424 bool has_small_toc_reloc_
;
426 // Set at the start of gc_process_relocs, when we know opd_ent_
427 // vector is valid. The flag could be made atomic and set in
428 // do_read_relocs with memory_order_release and then tested with
429 // memory_order_acquire, potentially resulting in fewer entries in
434 elfcpp::Elf_Word e_flags_
;
436 // For 64-bit, an array with one entry per 64-bit word in the .toc
437 // section, set if accesses using that word cannot be optimised.
438 std::vector
<bool> no_toc_opt_
;
440 // The first 8-byte word of an OPD entry gives the address of the
441 // entry point of the function. Relocatable object files have a
442 // relocation on this word. The following vector records the
443 // section and offset specified by these relocations.
444 std::vector
<Opd_ent
> opd_ent_
;
446 // References made to this object's .opd section when running
447 // gc_process_relocs for another object, before the opd_ent_ vector
448 // is valid for this object.
449 Access_from access_from_map_
;
451 // Whether input section has a 14-bit branch reloc.
452 std::vector
<bool> has14_
;
454 // The stub table to use for a given input section.
455 std::vector
<unsigned int> stub_table_index_
;
457 // ELF st_other field for local symbols.
458 std::vector
<unsigned char> st_other_
;
461 template<int size
, bool big_endian
>
462 class Powerpc_dynobj
: public Sized_dynobj
<size
, big_endian
>
465 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
467 Powerpc_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
468 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
469 : Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
470 opd_shndx_(0), e_flags_(ehdr
.get_e_flags()), opd_ent_()
472 this->set_abiversion(0);
478 // Call Sized_dynobj::do_read_symbols to read the symbols then
479 // read .opd from a dynamic object, filling in opd_ent_ vector,
481 do_read_symbols(Read_symbols_data
*);
483 // The .opd section shndx.
487 return this->opd_shndx_
;
490 // The .opd section address.
494 return this->opd_address_
;
497 // Init OPD entry arrays.
499 init_opd(size_t opd_size
)
501 size_t count
= this->opd_ent_ndx(opd_size
);
502 this->opd_ent_
.resize(count
);
505 // Return section and offset of function entry for .opd + R_OFF.
507 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
509 size_t ndx
= this->opd_ent_ndx(r_off
);
510 gold_assert(ndx
< this->opd_ent_
.size());
511 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
513 *value
= this->opd_ent_
[ndx
].off
;
514 return this->opd_ent_
[ndx
].shndx
;
517 // Set section and offset of function entry for .opd + R_OFF.
519 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
521 size_t ndx
= this->opd_ent_ndx(r_off
);
522 gold_assert(ndx
< this->opd_ent_
.size());
523 this->opd_ent_
[ndx
].shndx
= shndx
;
524 this->opd_ent_
[ndx
].off
= value
;
529 { return this->e_flags_
& elfcpp::EF_PPC64_ABI
; }
531 // Set ABI version for input and output.
533 set_abiversion(int ver
);
536 // Used to specify extent of executable sections.
539 Sec_info(Address start_
, Address len_
, unsigned int shndx_
)
540 : start(start_
), len(len_
), shndx(shndx_
)
544 operator<(const Sec_info
& that
) const
545 { return this->start
< that
.start
; }
558 // Return index into opd_ent_ array for .opd entry at OFF.
560 opd_ent_ndx(size_t off
) const
563 // For 64-bit the .opd section shndx and address.
564 unsigned int opd_shndx_
;
565 Address opd_address_
;
568 elfcpp::Elf_Word e_flags_
;
570 // The first 8-byte word of an OPD entry gives the address of the
571 // entry point of the function. Records the section and offset
572 // corresponding to the address. Note that in dynamic objects,
573 // offset is *not* relative to the section.
574 std::vector
<Opd_ent
> opd_ent_
;
577 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
578 // base class will emit.
580 template<int sh_type
, int size
, bool big_endian
>
581 class Powerpc_copy_relocs
: public Copy_relocs
<sh_type
, size
, big_endian
>
584 Powerpc_copy_relocs()
585 : Copy_relocs
<sh_type
, size
, big_endian
>(elfcpp::R_POWERPC_COPY
)
588 // Emit any saved relocations which turn out to be needed. This is
589 // called after all the relocs have been scanned.
591 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
594 template<int size
, bool big_endian
>
595 class Target_powerpc
: public Sized_target
<size
, big_endian
>
599 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
600 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
601 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Signed_address
;
602 typedef Unordered_set
<Symbol_location
, Symbol_location_hash
> Tocsave_loc
;
603 static const Address invalid_address
= static_cast<Address
>(0) - 1;
604 // Offset of tp and dtp pointers from start of TLS block.
605 static const Address tp_offset
= 0x7000;
606 static const Address dtp_offset
= 0x8000;
609 : Sized_target
<size
, big_endian
>(&powerpc_info
),
610 got_(NULL
), plt_(NULL
), iplt_(NULL
), brlt_section_(NULL
),
611 glink_(NULL
), rela_dyn_(NULL
), copy_relocs_(),
612 tlsld_got_offset_(-1U),
613 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
614 plt_thread_safe_(false), plt_localentry0_(false),
615 plt_localentry0_init_(false), has_localentry0_(false),
616 relax_failed_(false), relax_fail_count_(0),
617 stub_group_size_(0), savres_section_(0)
621 // Process the relocations to determine unreferenced sections for
622 // garbage collection.
624 gc_process_relocs(Symbol_table
* symtab
,
626 Sized_relobj_file
<size
, big_endian
>* object
,
627 unsigned int data_shndx
,
628 unsigned int sh_type
,
629 const unsigned char* prelocs
,
631 Output_section
* output_section
,
632 bool needs_special_offset_handling
,
633 size_t local_symbol_count
,
634 const unsigned char* plocal_symbols
);
636 // Scan the relocations to look for symbol adjustments.
638 scan_relocs(Symbol_table
* symtab
,
640 Sized_relobj_file
<size
, big_endian
>* object
,
641 unsigned int data_shndx
,
642 unsigned int sh_type
,
643 const unsigned char* prelocs
,
645 Output_section
* output_section
,
646 bool needs_special_offset_handling
,
647 size_t local_symbol_count
,
648 const unsigned char* plocal_symbols
);
650 // Map input .toc section to output .got section.
652 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
654 if (size
== 64 && strcmp(name
, ".toc") == 0)
662 // Provide linker defined save/restore functions.
664 define_save_restore_funcs(Layout
*, Symbol_table
*);
666 // No stubs unless a final link.
669 { return !parameters
->options().relocatable(); }
672 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
675 do_plt_fde_location(const Output_data
*, unsigned char*,
676 uint64_t*, off_t
*) const;
678 // Stash info about branches, for stub generation.
680 push_branch(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
681 unsigned int data_shndx
, Address r_offset
,
682 unsigned int r_type
, unsigned int r_sym
, Address addend
)
684 Branch_info
info(ppc_object
, data_shndx
, r_offset
, r_type
, r_sym
, addend
);
685 this->branch_info_
.push_back(info
);
686 if (r_type
== elfcpp::R_POWERPC_REL14
687 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
688 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
689 ppc_object
->set_has_14bit_branch(data_shndx
);
692 // Return whether the last branch is a plt call, and if so, mark the
693 // branch as having an R_PPC64_TOCSAVE.
695 mark_pltcall(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
696 unsigned int data_shndx
, Address r_offset
, Symbol_table
* symtab
)
699 && !this->branch_info_
.empty()
700 && this->branch_info_
.back().mark_pltcall(ppc_object
, data_shndx
,
701 r_offset
, this, symtab
));
704 // Say the given location, that of a nop in a function prologue with
705 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
706 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
708 add_tocsave(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
709 unsigned int shndx
, Address offset
)
712 loc
.object
= ppc_object
;
715 this->tocsave_loc_
.insert(loc
);
722 return this->tocsave_loc_
;
726 do_define_standard_symbols(Symbol_table
*, Layout
*);
728 // Finalize the sections.
730 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
732 // Return the value to use for a dynamic which requires special
735 do_dynsym_value(const Symbol
*) const;
737 // Return the PLT address to use for a local symbol.
739 do_plt_address_for_local(const Relobj
*, unsigned int) const;
741 // Return the PLT address to use for a global symbol.
743 do_plt_address_for_global(const Symbol
*) const;
745 // Return the offset to use for the GOT_INDX'th got entry which is
746 // for a local tls symbol specified by OBJECT, SYMNDX.
748 do_tls_offset_for_local(const Relobj
* object
,
750 unsigned int got_indx
) const;
752 // Return the offset to use for the GOT_INDX'th got entry which is
753 // for global tls symbol GSYM.
755 do_tls_offset_for_global(Symbol
* gsym
, unsigned int got_indx
) const;
758 do_function_location(Symbol_location
*) const;
761 do_can_check_for_function_pointers() const
764 // Adjust -fsplit-stack code which calls non-split-stack code.
766 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
767 section_offset_type fnoffset
, section_size_type fnsize
,
768 const unsigned char* prelocs
, size_t reloc_count
,
769 unsigned char* view
, section_size_type view_size
,
770 std::string
* from
, std::string
* to
) const;
772 // Relocate a section.
774 relocate_section(const Relocate_info
<size
, big_endian
>*,
775 unsigned int sh_type
,
776 const unsigned char* prelocs
,
778 Output_section
* output_section
,
779 bool needs_special_offset_handling
,
781 Address view_address
,
782 section_size_type view_size
,
783 const Reloc_symbol_changes
*);
785 // Scan the relocs during a relocatable link.
787 scan_relocatable_relocs(Symbol_table
* symtab
,
789 Sized_relobj_file
<size
, big_endian
>* object
,
790 unsigned int data_shndx
,
791 unsigned int sh_type
,
792 const unsigned char* prelocs
,
794 Output_section
* output_section
,
795 bool needs_special_offset_handling
,
796 size_t local_symbol_count
,
797 const unsigned char* plocal_symbols
,
798 Relocatable_relocs
*);
800 // Scan the relocs for --emit-relocs.
802 emit_relocs_scan(Symbol_table
* symtab
,
804 Sized_relobj_file
<size
, big_endian
>* object
,
805 unsigned int data_shndx
,
806 unsigned int sh_type
,
807 const unsigned char* prelocs
,
809 Output_section
* output_section
,
810 bool needs_special_offset_handling
,
811 size_t local_symbol_count
,
812 const unsigned char* plocal_syms
,
813 Relocatable_relocs
* rr
);
815 // Emit relocations for a section.
817 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
818 unsigned int sh_type
,
819 const unsigned char* prelocs
,
821 Output_section
* output_section
,
822 typename
elfcpp::Elf_types
<size
>::Elf_Off
823 offset_in_output_section
,
825 Address view_address
,
827 unsigned char* reloc_view
,
828 section_size_type reloc_view_size
);
830 // Return whether SYM is defined by the ABI.
832 do_is_defined_by_abi(const Symbol
* sym
) const
834 return strcmp(sym
->name(), "__tls_get_addr") == 0;
837 // Return the size of the GOT section.
841 gold_assert(this->got_
!= NULL
);
842 return this->got_
->data_size();
845 // Get the PLT section.
846 const Output_data_plt_powerpc
<size
, big_endian
>*
849 gold_assert(this->plt_
!= NULL
);
853 // Get the IPLT section.
854 const Output_data_plt_powerpc
<size
, big_endian
>*
857 gold_assert(this->iplt_
!= NULL
);
861 // Get the .glink section.
862 const Output_data_glink
<size
, big_endian
>*
863 glink_section() const
865 gold_assert(this->glink_
!= NULL
);
869 Output_data_glink
<size
, big_endian
>*
872 gold_assert(this->glink_
!= NULL
);
876 bool has_glink() const
877 { return this->glink_
!= NULL
; }
879 // Get the GOT section.
880 const Output_data_got_powerpc
<size
, big_endian
>*
883 gold_assert(this->got_
!= NULL
);
887 // Get the GOT section, creating it if necessary.
888 Output_data_got_powerpc
<size
, big_endian
>*
889 got_section(Symbol_table
*, Layout
*);
892 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
893 const elfcpp::Ehdr
<size
, big_endian
>&);
895 // Return the number of entries in the GOT.
897 got_entry_count() const
899 if (this->got_
== NULL
)
901 return this->got_size() / (size
/ 8);
904 // Return the number of entries in the PLT.
906 plt_entry_count() const;
908 // Return the offset of the first non-reserved PLT entry.
910 first_plt_entry_offset() const
914 if (this->abiversion() >= 2)
919 // Return the size of each PLT entry.
921 plt_entry_size() const
925 if (this->abiversion() >= 2)
930 Output_data_save_res
<size
, big_endian
>*
931 savres_section() const
933 return this->savres_section_
;
936 // Add any special sections for this symbol to the gc work list.
937 // For powerpc64, this adds the code section of a function
940 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
942 // Handle target specific gc actions when adding a gc reference from
943 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
944 // and DST_OFF. For powerpc64, this adds a referenc to the code
945 // section of a function descriptor.
947 do_gc_add_reference(Symbol_table
* symtab
,
949 unsigned int src_shndx
,
951 unsigned int dst_shndx
,
952 Address dst_off
) const;
954 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
957 { return this->stub_tables_
; }
959 const Output_data_brlt_powerpc
<size
, big_endian
>*
961 { return this->brlt_section_
; }
964 add_branch_lookup_table(Address to
)
966 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
967 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
971 find_branch_lookup_table(Address to
)
973 typename
Branch_lookup_table::const_iterator p
974 = this->branch_lookup_table_
.find(to
);
975 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
979 write_branch_lookup_table(unsigned char *oview
)
981 for (typename
Branch_lookup_table::const_iterator p
982 = this->branch_lookup_table_
.begin();
983 p
!= this->branch_lookup_table_
.end();
986 elfcpp::Swap
<size
, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
990 // Wrapper used after relax to define a local symbol in output data,
991 // from the end if value < 0.
993 define_local(Symbol_table
* symtab
, const char* name
,
994 Output_data
* od
, Address value
, unsigned int symsize
)
997 = symtab
->define_in_output_data(name
, NULL
, Symbol_table::PREDEFINED
,
998 od
, value
, symsize
, elfcpp::STT_NOTYPE
,
999 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
, 0,
1000 static_cast<Signed_address
>(value
) < 0,
1002 // We are creating this symbol late, so need to fix up things
1003 // done early in Layout::finalize.
1004 sym
->set_dynsym_index(-1U);
1008 plt_thread_safe() const
1009 { return this->plt_thread_safe_
; }
1012 plt_localentry0() const
1013 { return this->plt_localentry0_
; }
1016 set_has_localentry0()
1018 this->has_localentry0_
= true;
1022 is_elfv2_localentry0(const Symbol
* gsym
) const
1025 && this->abiversion() >= 2
1026 && this->plt_localentry0()
1027 && gsym
->type() == elfcpp::STT_FUNC
1028 && gsym
->is_defined()
1029 && gsym
->nonvis() >> 3 == 0);
1033 is_elfv2_localentry0(const Sized_relobj_file
<size
, big_endian
>* object
,
1034 unsigned int r_sym
) const
1036 const Powerpc_relobj
<size
, big_endian
>* ppc_object
1037 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
1040 && this->abiversion() >= 2
1041 && this->plt_localentry0()
1042 && ppc_object
->st_other(r_sym
) >> 5 == 0)
1044 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
1046 if (!psymval
->is_ifunc_symbol()
1047 && psymval
->input_shndx(&is_ordinary
) != elfcpp::SHN_UNDEF
1056 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI
; }
1059 set_abiversion(int ver
)
1061 elfcpp::Elf_Word flags
= this->processor_specific_flags();
1062 flags
&= ~elfcpp::EF_PPC64_ABI
;
1063 flags
|= ver
& elfcpp::EF_PPC64_ABI
;
1064 this->set_processor_specific_flags(flags
);
1067 // Offset to toc save stack slot
1070 { return this->abiversion() < 2 ? 40 : 24; }
1086 : tls_get_addr_state_(NOT_EXPECTED
),
1087 relinfo_(NULL
), relnum_(0), r_offset_(0)
1092 if (this->tls_get_addr_state_
!= NOT_EXPECTED
)
1099 if (this->relinfo_
!= NULL
)
1100 gold_error_at_location(this->relinfo_
, this->relnum_
, this->r_offset_
,
1101 _("missing expected __tls_get_addr call"));
1105 expect_tls_get_addr_call(
1106 const Relocate_info
<size
, big_endian
>* relinfo
,
1110 this->tls_get_addr_state_
= EXPECTED
;
1111 this->relinfo_
= relinfo
;
1112 this->relnum_
= relnum
;
1113 this->r_offset_
= r_offset
;
1117 expect_tls_get_addr_call()
1118 { this->tls_get_addr_state_
= EXPECTED
; }
1121 skip_next_tls_get_addr_call()
1122 {this->tls_get_addr_state_
= SKIP
; }
1125 maybe_skip_tls_get_addr_call(unsigned int r_type
, const Symbol
* gsym
)
1127 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
1128 || r_type
== elfcpp::R_PPC_PLTREL24
)
1130 && strcmp(gsym
->name(), "__tls_get_addr") == 0);
1131 Tls_get_addr last_tls
= this->tls_get_addr_state_
;
1132 this->tls_get_addr_state_
= NOT_EXPECTED
;
1133 if (is_tls_call
&& last_tls
!= EXPECTED
)
1135 else if (!is_tls_call
&& last_tls
!= NOT_EXPECTED
)
1144 // What we're up to regarding calls to __tls_get_addr.
1145 // On powerpc, the branch and link insn making a call to
1146 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1147 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1148 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
1149 // The marker relocation always comes first, and has the same
1150 // symbol as the reloc on the insn setting up the __tls_get_addr
1151 // argument. This ties the arg setup insn with the call insn,
1152 // allowing ld to safely optimize away the call. We check that
1153 // every call to __tls_get_addr has a marker relocation, and that
1154 // every marker relocation is on a call to __tls_get_addr.
1155 Tls_get_addr tls_get_addr_state_
;
1156 // Info about the last reloc for error message.
1157 const Relocate_info
<size
, big_endian
>* relinfo_
;
1162 // The class which scans relocations.
1163 class Scan
: protected Track_tls
1166 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1169 : Track_tls(), issued_non_pic_error_(false)
1173 get_reference_flags(unsigned int r_type
, const Target_powerpc
* target
);
1176 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
1177 Sized_relobj_file
<size
, big_endian
>* object
,
1178 unsigned int data_shndx
,
1179 Output_section
* output_section
,
1180 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
1181 const elfcpp::Sym
<size
, big_endian
>& lsym
,
1185 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
1186 Sized_relobj_file
<size
, big_endian
>* object
,
1187 unsigned int data_shndx
,
1188 Output_section
* output_section
,
1189 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
1193 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1195 Sized_relobj_file
<size
, big_endian
>* relobj
,
1198 const elfcpp::Rela
<size
, big_endian
>& ,
1199 unsigned int r_type
,
1200 const elfcpp::Sym
<size
, big_endian
>&)
1202 // PowerPC64 .opd is not folded, so any identical function text
1203 // may be folded and we'll still keep function addresses distinct.
1204 // That means no reloc is of concern here.
1207 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1208 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1209 if (ppcobj
->abiversion() == 1)
1212 // For 32-bit and ELFv2, conservatively assume anything but calls to
1213 // function code might be taking the address of the function.
1214 return !is_branch_reloc(r_type
);
1218 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1220 Sized_relobj_file
<size
, big_endian
>* relobj
,
1223 const elfcpp::Rela
<size
, big_endian
>& ,
1224 unsigned int r_type
,
1230 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1231 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1232 if (ppcobj
->abiversion() == 1)
1235 return !is_branch_reloc(r_type
);
1239 reloc_needs_plt_for_ifunc(Target_powerpc
<size
, big_endian
>* target
,
1240 Sized_relobj_file
<size
, big_endian
>* object
,
1241 unsigned int r_type
, bool report_err
);
1245 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
1246 unsigned int r_type
);
1249 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
1250 unsigned int r_type
, Symbol
*);
1253 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
1254 Target_powerpc
* target
);
1257 check_non_pic(Relobj
*, unsigned int r_type
);
1259 // Whether we have issued an error about a non-PIC compilation.
1260 bool issued_non_pic_error_
;
1264 symval_for_branch(const Symbol_table
* symtab
,
1265 const Sized_symbol
<size
>* gsym
,
1266 Powerpc_relobj
<size
, big_endian
>* object
,
1267 Address
*value
, unsigned int *dest_shndx
);
1269 // The class which implements relocation.
1270 class Relocate
: protected Track_tls
1273 // Use 'at' branch hints when true, 'y' when false.
1274 // FIXME maybe: set this with an option.
1275 static const bool is_isa_v2
= true;
1281 // Do a relocation. Return false if the caller should not issue
1282 // any warnings about this relocation.
1284 relocate(const Relocate_info
<size
, big_endian
>*, unsigned int,
1285 Target_powerpc
*, Output_section
*, size_t, const unsigned char*,
1286 const Sized_symbol
<size
>*, const Symbol_value
<size
>*,
1287 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1291 class Relocate_comdat_behavior
1294 // Decide what the linker should do for relocations that refer to
1295 // discarded comdat sections.
1296 inline Comdat_behavior
1297 get(const char* name
)
1299 gold::Default_comdat_behavior default_behavior
;
1300 Comdat_behavior ret
= default_behavior
.get(name
);
1301 if (ret
== CB_WARNING
)
1304 && (strcmp(name
, ".fixup") == 0
1305 || strcmp(name
, ".got2") == 0))
1308 && (strcmp(name
, ".opd") == 0
1309 || strcmp(name
, ".toc") == 0
1310 || strcmp(name
, ".toc1") == 0))
1317 // Optimize the TLS relocation type based on what we know about the
1318 // symbol. IS_FINAL is true if the final address of this symbol is
1319 // known at link time.
1321 tls::Tls_optimization
1322 optimize_tls_gd(bool is_final
)
1324 // If we are generating a shared library, then we can't do anything
1326 if (parameters
->options().shared()
1327 || !parameters
->options().tls_optimize())
1328 return tls::TLSOPT_NONE
;
1331 return tls::TLSOPT_TO_IE
;
1332 return tls::TLSOPT_TO_LE
;
1335 tls::Tls_optimization
1338 if (parameters
->options().shared()
1339 || !parameters
->options().tls_optimize())
1340 return tls::TLSOPT_NONE
;
1342 return tls::TLSOPT_TO_LE
;
1345 tls::Tls_optimization
1346 optimize_tls_ie(bool is_final
)
1349 || parameters
->options().shared()
1350 || !parameters
->options().tls_optimize())
1351 return tls::TLSOPT_NONE
;
1353 return tls::TLSOPT_TO_LE
;
1358 make_glink_section(Layout
*);
1360 // Create the PLT section.
1362 make_plt_section(Symbol_table
*, Layout
*);
1365 make_iplt_section(Symbol_table
*, Layout
*);
1368 make_brlt_section(Layout
*);
1370 // Create a PLT entry for a global symbol.
1372 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1374 // Create a PLT entry for a local IFUNC symbol.
1376 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1377 Sized_relobj_file
<size
, big_endian
>*,
1381 // Create a GOT entry for local dynamic __tls_get_addr.
1383 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
1384 Sized_relobj_file
<size
, big_endian
>* object
);
1387 tlsld_got_offset() const
1389 return this->tlsld_got_offset_
;
1392 // Get the dynamic reloc section, creating it if necessary.
1394 rela_dyn_section(Layout
*);
1396 // Similarly, but for ifunc symbols get the one for ifunc.
1398 rela_dyn_section(Symbol_table
*, Layout
*, bool for_ifunc
);
1400 // Copy a relocation against a global symbol.
1402 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1403 Sized_relobj_file
<size
, big_endian
>* object
,
1404 unsigned int shndx
, Output_section
* output_section
,
1405 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
1407 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1408 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1409 symtab
->get_sized_symbol
<size
>(sym
),
1410 object
, shndx
, output_section
,
1411 r_type
, reloc
.get_r_offset(),
1412 reloc
.get_r_addend(),
1413 this->rela_dyn_section(layout
));
1416 // Look over all the input sections, deciding where to place stubs.
1418 group_sections(Layout
*, const Task
*, bool);
1420 // Sort output sections by address.
1421 struct Sort_sections
1424 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
1425 { return sec1
->address() < sec2
->address(); }
1431 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1432 unsigned int data_shndx
,
1434 unsigned int r_type
,
1437 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
1438 r_type_(r_type
), tocsave_ (0), r_sym_(r_sym
), addend_(addend
)
1444 // Return whether this branch is going via a plt call stub, and if
1445 // so, mark it as having an R_PPC64_TOCSAVE.
1447 mark_pltcall(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1448 unsigned int shndx
, Address offset
,
1449 Target_powerpc
* target
, Symbol_table
* symtab
);
1451 // If this branch needs a plt call stub, or a long branch stub, make one.
1453 make_stub(Stub_table
<size
, big_endian
>*,
1454 Stub_table
<size
, big_endian
>*,
1455 Symbol_table
*) const;
1458 // The branch location..
1459 Powerpc_relobj
<size
, big_endian
>* object_
;
1460 unsigned int shndx_
;
1462 // ..and the branch type and destination.
1463 unsigned int r_type_
: 31;
1464 unsigned int tocsave_
: 1;
1465 unsigned int r_sym_
;
1469 // Information about this specific target which we pass to the
1470 // general Target structure.
1471 static Target::Target_info powerpc_info
;
1473 // The types of GOT entries needed for this platform.
1474 // These values are exposed to the ABI in an incremental link.
1475 // Do not renumber existing values without changing the version
1476 // number of the .gnu_incremental_inputs section.
1480 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
1481 GOT_TYPE_DTPREL
, // entry for @got@dtprel
1482 GOT_TYPE_TPREL
// entry for @got@tprel
1486 Output_data_got_powerpc
<size
, big_endian
>* got_
;
1487 // The PLT section. This is a container for a table of addresses,
1488 // and their relocations. Each address in the PLT has a dynamic
1489 // relocation (R_*_JMP_SLOT) and each address will have a
1490 // corresponding entry in .glink for lazy resolution of the PLT.
1491 // ppc32 initialises the PLT to point at the .glink entry, while
1492 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1493 // linker adds a stub that loads the PLT entry into ctr then
1494 // branches to ctr. There may be more than one stub for each PLT
1495 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1496 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1497 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
1498 // The IPLT section. Like plt_, this is a container for a table of
1499 // addresses and their relocations, specifically for STT_GNU_IFUNC
1500 // functions that resolve locally (STT_GNU_IFUNC functions that
1501 // don't resolve locally go in PLT). Unlike plt_, these have no
1502 // entry in .glink for lazy resolution, and the relocation section
1503 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1504 // the relocation section may contain relocations against
1505 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1506 // relocation section will appear at the end of other dynamic
1507 // relocations, so that ld.so applies these relocations after other
1508 // dynamic relocations. In a static executable, the relocation
1509 // section is emitted and marked with __rela_iplt_start and
1510 // __rela_iplt_end symbols.
1511 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
1512 // Section holding long branch destinations.
1513 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
1514 // The .glink section.
1515 Output_data_glink
<size
, big_endian
>* glink_
;
1516 // The dynamic reloc section.
1517 Reloc_section
* rela_dyn_
;
1518 // Relocs saved to avoid a COPY reloc.
1519 Powerpc_copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
1520 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1521 unsigned int tlsld_got_offset_
;
1523 Stub_tables stub_tables_
;
1524 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
1525 Branch_lookup_table branch_lookup_table_
;
1527 typedef std::vector
<Branch_info
> Branches
;
1528 Branches branch_info_
;
1529 Tocsave_loc tocsave_loc_
;
1531 bool plt_thread_safe_
;
1532 bool plt_localentry0_
;
1533 bool plt_localentry0_init_
;
1534 bool has_localentry0_
;
1537 int relax_fail_count_
;
1538 int32_t stub_group_size_
;
1540 Output_data_save_res
<size
, big_endian
> *savres_section_
;
1544 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
1547 true, // is_big_endian
1548 elfcpp::EM_PPC
, // machine_code
1549 false, // has_make_symbol
1550 false, // has_resolve
1551 false, // has_code_fill
1552 true, // is_default_stack_executable
1553 false, // can_icf_inline_merge_sections
1555 "/usr/lib/ld.so.1", // dynamic_linker
1556 0x10000000, // default_text_segment_address
1557 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1558 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1559 false, // isolate_execinstr
1561 elfcpp::SHN_UNDEF
, // small_common_shndx
1562 elfcpp::SHN_UNDEF
, // large_common_shndx
1563 0, // small_common_section_flags
1564 0, // large_common_section_flags
1565 NULL
, // attributes_section
1566 NULL
, // attributes_vendor
1567 "_start", // entry_symbol_name
1568 32, // hash_entry_size
1572 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
1575 false, // is_big_endian
1576 elfcpp::EM_PPC
, // machine_code
1577 false, // has_make_symbol
1578 false, // has_resolve
1579 false, // has_code_fill
1580 true, // is_default_stack_executable
1581 false, // can_icf_inline_merge_sections
1583 "/usr/lib/ld.so.1", // dynamic_linker
1584 0x10000000, // default_text_segment_address
1585 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1586 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1587 false, // isolate_execinstr
1589 elfcpp::SHN_UNDEF
, // small_common_shndx
1590 elfcpp::SHN_UNDEF
, // large_common_shndx
1591 0, // small_common_section_flags
1592 0, // large_common_section_flags
1593 NULL
, // attributes_section
1594 NULL
, // attributes_vendor
1595 "_start", // entry_symbol_name
1596 32, // hash_entry_size
1600 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1603 true, // is_big_endian
1604 elfcpp::EM_PPC64
, // machine_code
1605 false, // has_make_symbol
1606 false, // has_resolve
1607 false, // has_code_fill
1608 true, // is_default_stack_executable
1609 false, // can_icf_inline_merge_sections
1611 "/usr/lib/ld.so.1", // dynamic_linker
1612 0x10000000, // default_text_segment_address
1613 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1614 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1615 false, // isolate_execinstr
1617 elfcpp::SHN_UNDEF
, // small_common_shndx
1618 elfcpp::SHN_UNDEF
, // large_common_shndx
1619 0, // small_common_section_flags
1620 0, // large_common_section_flags
1621 NULL
, // attributes_section
1622 NULL
, // attributes_vendor
1623 "_start", // entry_symbol_name
1624 32, // hash_entry_size
1628 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1631 false, // is_big_endian
1632 elfcpp::EM_PPC64
, // machine_code
1633 false, // has_make_symbol
1634 false, // has_resolve
1635 false, // has_code_fill
1636 true, // is_default_stack_executable
1637 false, // can_icf_inline_merge_sections
1639 "/usr/lib/ld.so.1", // dynamic_linker
1640 0x10000000, // default_text_segment_address
1641 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1642 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1643 false, // isolate_execinstr
1645 elfcpp::SHN_UNDEF
, // small_common_shndx
1646 elfcpp::SHN_UNDEF
, // large_common_shndx
1647 0, // small_common_section_flags
1648 0, // large_common_section_flags
1649 NULL
, // attributes_section
1650 NULL
, // attributes_vendor
1651 "_start", // entry_symbol_name
1652 32, // hash_entry_size
1656 is_branch_reloc(unsigned int r_type
)
1658 return (r_type
== elfcpp::R_POWERPC_REL24
1659 || r_type
== elfcpp::R_PPC_PLTREL24
1660 || r_type
== elfcpp::R_PPC_LOCAL24PC
1661 || r_type
== elfcpp::R_POWERPC_REL14
1662 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1663 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1664 || r_type
== elfcpp::R_POWERPC_ADDR24
1665 || r_type
== elfcpp::R_POWERPC_ADDR14
1666 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1667 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1670 // If INSN is an opcode that may be used with an @tls operand, return
1671 // the transformed insn for TLS optimisation, otherwise return 0. If
1672 // REG is non-zero only match an insn with RB or RA equal to REG.
1674 at_tls_transform(uint32_t insn
, unsigned int reg
)
1676 if ((insn
& (0x3f << 26)) != 31 << 26)
1680 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1681 rtra
= insn
& ((1 << 26) - (1 << 16));
1682 else if (((insn
>> 16) & 0x1f) == reg
)
1683 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1687 if ((insn
& (0x3ff << 1)) == 266 << 1)
1690 else if ((insn
& (0x1f << 1)) == 23 << 1
1691 && ((insn
& (0x1f << 6)) < 14 << 6
1692 || ((insn
& (0x1f << 6)) >= 16 << 6
1693 && (insn
& (0x1f << 6)) < 24 << 6)))
1694 // load and store indexed -> dform
1695 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1696 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1697 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1698 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1699 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1701 insn
= (58 << 26) | 2;
1709 template<int size
, bool big_endian
>
1710 class Powerpc_relocate_functions
1730 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1731 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1732 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword SignedAddress
;
1734 template<int valsize
>
1736 has_overflow_signed(Address value
)
1738 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1739 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1740 limit
<<= ((valsize
- 1) >> 1);
1741 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1742 return value
+ limit
> (limit
<< 1) - 1;
1745 template<int valsize
>
1747 has_overflow_unsigned(Address value
)
1749 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1750 limit
<<= ((valsize
- 1) >> 1);
1751 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1752 return value
> (limit
<< 1) - 1;
1755 template<int valsize
>
1757 has_overflow_bitfield(Address value
)
1759 return (has_overflow_unsigned
<valsize
>(value
)
1760 && has_overflow_signed
<valsize
>(value
));
1763 template<int valsize
>
1764 static inline Status
1765 overflowed(Address value
, Overflow_check overflow
)
1767 if (overflow
== CHECK_SIGNED
)
1769 if (has_overflow_signed
<valsize
>(value
))
1770 return STATUS_OVERFLOW
;
1772 else if (overflow
== CHECK_UNSIGNED
)
1774 if (has_overflow_unsigned
<valsize
>(value
))
1775 return STATUS_OVERFLOW
;
1777 else if (overflow
== CHECK_BITFIELD
)
1779 if (has_overflow_bitfield
<valsize
>(value
))
1780 return STATUS_OVERFLOW
;
1785 // Do a simple RELA relocation
1786 template<int fieldsize
, int valsize
>
1787 static inline Status
1788 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1790 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1791 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1792 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, value
);
1793 return overflowed
<valsize
>(value
, overflow
);
1796 template<int fieldsize
, int valsize
>
1797 static inline Status
1798 rela(unsigned char* view
,
1799 unsigned int right_shift
,
1800 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1802 Overflow_check overflow
)
1804 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1805 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1806 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(wv
);
1807 Valtype reloc
= value
>> right_shift
;
1810 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, val
| reloc
);
1811 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1814 // Do a simple RELA relocation, unaligned.
1815 template<int fieldsize
, int valsize
>
1816 static inline Status
1817 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1819 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, value
);
1820 return overflowed
<valsize
>(value
, overflow
);
1823 template<int fieldsize
, int valsize
>
1824 static inline Status
1825 rela_ua(unsigned char* view
,
1826 unsigned int right_shift
,
1827 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1829 Overflow_check overflow
)
1831 typedef typename
elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::Valtype
1833 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(view
);
1834 Valtype reloc
= value
>> right_shift
;
1837 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, val
| reloc
);
1838 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1842 // R_PPC64_ADDR64: (Symbol + Addend)
1844 addr64(unsigned char* view
, Address value
)
1845 { This::template rela
<64,64>(view
, value
, CHECK_NONE
); }
1847 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1849 addr64_u(unsigned char* view
, Address value
)
1850 { This::template rela_ua
<64,64>(view
, value
, CHECK_NONE
); }
1852 // R_POWERPC_ADDR32: (Symbol + Addend)
1853 static inline Status
1854 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
1855 { return This::template rela
<32,32>(view
, value
, overflow
); }
1857 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1858 static inline Status
1859 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1860 { return This::template rela_ua
<32,32>(view
, value
, overflow
); }
1862 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1863 static inline Status
1864 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
1866 Status stat
= This::template rela
<32,26>(view
, 0, 0x03fffffc,
1868 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1869 stat
= STATUS_OVERFLOW
;
1873 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1874 static inline Status
1875 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
1876 { return This::template rela
<16,16>(view
, value
, overflow
); }
1878 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1879 static inline Status
1880 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1881 { return This::template rela_ua
<16,16>(view
, value
, overflow
); }
1883 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1884 static inline Status
1885 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
1887 Status stat
= This::template rela
<16,16>(view
, 0, 0xfffc, value
, overflow
);
1888 if ((value
& 3) != 0)
1889 stat
= STATUS_OVERFLOW
;
1893 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
1894 static inline Status
1895 addr16_dq(unsigned char* view
, Address value
, Overflow_check overflow
)
1897 Status stat
= This::template rela
<16,16>(view
, 0, 0xfff0, value
, overflow
);
1898 if ((value
& 15) != 0)
1899 stat
= STATUS_OVERFLOW
;
1903 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1905 addr16_hi(unsigned char* view
, Address value
)
1906 { This::template rela
<16,16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
1908 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1910 addr16_ha(unsigned char* view
, Address value
)
1911 { This::addr16_hi(view
, value
+ 0x8000); }
1913 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1915 addr16_hi2(unsigned char* view
, Address value
)
1916 { This::template rela
<16,16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
1918 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1920 addr16_ha2(unsigned char* view
, Address value
)
1921 { This::addr16_hi2(view
, value
+ 0x8000); }
1923 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1925 addr16_hi3(unsigned char* view
, Address value
)
1926 { This::template rela
<16,16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
1928 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1930 addr16_ha3(unsigned char* view
, Address value
)
1931 { This::addr16_hi3(view
, value
+ 0x8000); }
1933 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1934 static inline Status
1935 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
1937 Status stat
= This::template rela
<32,16>(view
, 0, 0xfffc, value
, overflow
);
1938 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1939 stat
= STATUS_OVERFLOW
;
1943 // R_POWERPC_REL16DX_HA
1944 static inline Status
1945 addr16dx_ha(unsigned char *view
, Address value
, Overflow_check overflow
)
1947 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1948 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1949 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1951 value
= static_cast<SignedAddress
>(value
) >> 16;
1952 val
|= (value
& 0xffc1) | ((value
& 0x3e) << 15);
1953 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1954 return overflowed
<16>(value
, overflow
);
1958 // Set ABI version for input and output.
1960 template<int size
, bool big_endian
>
1962 Powerpc_relobj
<size
, big_endian
>::set_abiversion(int ver
)
1964 this->e_flags_
|= ver
;
1965 if (this->abiversion() != 0)
1967 Target_powerpc
<size
, big_endian
>* target
=
1968 static_cast<Target_powerpc
<size
, big_endian
>*>(
1969 parameters
->sized_target
<size
, big_endian
>());
1970 if (target
->abiversion() == 0)
1971 target
->set_abiversion(this->abiversion());
1972 else if (target
->abiversion() != this->abiversion())
1973 gold_error(_("%s: ABI version %d is not compatible "
1974 "with ABI version %d output"),
1975 this->name().c_str(),
1976 this->abiversion(), target
->abiversion());
1981 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
1982 // relocatable object, if such sections exists.
1984 template<int size
, bool big_endian
>
1986 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
1987 Read_symbols_data
* sd
)
1989 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1990 const unsigned char* namesu
= sd
->section_names
->data();
1991 const char* names
= reinterpret_cast<const char*>(namesu
);
1992 section_size_type names_size
= sd
->section_names_size
;
1993 const unsigned char* s
;
1995 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
,
1996 size
== 32 ? ".got2" : ".opd",
1997 names
, names_size
, NULL
);
2000 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
2001 this->special_
= ndx
;
2004 if (this->abiversion() == 0)
2005 this->set_abiversion(1);
2006 else if (this->abiversion() > 1)
2007 gold_error(_("%s: .opd invalid in abiv%d"),
2008 this->name().c_str(), this->abiversion());
2013 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".rela.toc",
2014 names
, names_size
, NULL
);
2017 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
2018 this->relatoc_
= ndx
;
2019 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2020 this->toc_
= this->adjust_shndx(shdr
.get_sh_info());
2023 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
2026 // Examine .rela.opd to build info about function entry points.
2028 template<int size
, bool big_endian
>
2030 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
2032 const unsigned char* prelocs
,
2033 const unsigned char* plocal_syms
)
2037 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
2038 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
2039 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2040 Address expected_off
= 0;
2041 bool regular
= true;
2042 unsigned int opd_ent_size
= 0;
2044 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
2046 Reltype
reloc(prelocs
);
2047 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
2048 = reloc
.get_r_info();
2049 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
2050 if (r_type
== elfcpp::R_PPC64_ADDR64
)
2052 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
2053 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
2056 if (r_sym
< this->local_symbol_count())
2058 typename
elfcpp::Sym
<size
, big_endian
>
2059 lsym(plocal_syms
+ r_sym
* sym_size
);
2060 shndx
= lsym
.get_st_shndx();
2061 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
2062 value
= lsym
.get_st_value();
2065 shndx
= this->symbol_section_and_value(r_sym
, &value
,
2067 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
2068 value
+ reloc
.get_r_addend());
2071 expected_off
= reloc
.get_r_offset();
2072 opd_ent_size
= expected_off
;
2074 else if (expected_off
!= reloc
.get_r_offset())
2076 expected_off
+= opd_ent_size
;
2078 else if (r_type
== elfcpp::R_PPC64_TOC
)
2080 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
2085 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2086 this->name().c_str(), r_type
);
2090 if (reloc_count
<= 2)
2091 opd_ent_size
= this->section_size(this->opd_shndx());
2092 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
2096 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2097 this->name().c_str());
2103 // Returns true if a code sequence loading the TOC entry at VALUE
2104 // relative to the TOC pointer can be converted into code calculating
2105 // a TOC pointer relative offset.
2106 // If so, the TOC pointer relative offset is stored to VALUE.
2108 template<int size
, bool big_endian
>
2110 Powerpc_relobj
<size
, big_endian
>::make_toc_relative(
2111 Target_powerpc
<size
, big_endian
>* target
,
2117 // With -mcmodel=medium code it is quite possible to have
2118 // toc-relative relocs referring to objects outside the TOC.
2119 // Don't try to look at a non-existent TOC.
2120 if (this->toc_shndx() == 0)
2123 // Convert VALUE back to an address by adding got_base (see below),
2124 // then to an offset in the TOC by subtracting the TOC output
2125 // section address and the TOC output offset. Since this TOC output
2126 // section and the got output section are one and the same, we can
2127 // omit adding and subtracting the output section address.
2128 Address off
= (*value
+ this->toc_base_offset()
2129 - this->output_section_offset(this->toc_shndx()));
2130 // Is this offset in the TOC? -mcmodel=medium code may be using
2131 // TOC relative access to variables outside the TOC. Those of
2132 // course can't be optimized. We also don't try to optimize code
2133 // that is using a different object's TOC.
2134 if (off
>= this->section_size(this->toc_shndx()))
2137 if (this->no_toc_opt(off
))
2140 section_size_type vlen
;
2141 unsigned char* view
= this->get_output_view(this->toc_shndx(), &vlen
);
2142 Address addr
= elfcpp::Swap
<size
, big_endian
>::readval(view
+ off
);
2144 Address got_base
= (target
->got_section()->output_section()->address()
2145 + this->toc_base_offset());
2147 if (addr
+ (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2154 // Perform the Sized_relobj_file method, then set up opd info from
2157 template<int size
, bool big_endian
>
2159 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
2161 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
2164 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
2165 p
!= rd
->relocs
.end();
2168 if (p
->data_shndx
== this->opd_shndx())
2170 uint64_t opd_size
= this->section_size(this->opd_shndx());
2171 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
2174 this->init_opd(opd_size
);
2175 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
2176 rd
->local_symbols
->data());
2184 // Read the symbols then set up st_other vector.
2186 template<int size
, bool big_endian
>
2188 Powerpc_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
2190 this->base_read_symbols(sd
);
2193 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2194 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2195 const unsigned int loccount
= this->do_local_symbol_count();
2198 this->st_other_
.resize(loccount
);
2199 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2200 off_t locsize
= loccount
* sym_size
;
2201 const unsigned int symtab_shndx
= this->symtab_shndx();
2202 const unsigned char *psymtab
= pshdrs
+ symtab_shndx
* shdr_size
;
2203 typename
elfcpp::Shdr
<size
, big_endian
> shdr(psymtab
);
2204 const unsigned char* psyms
= this->get_view(shdr
.get_sh_offset(),
2205 locsize
, true, false);
2207 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
2209 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
2210 unsigned char st_other
= sym
.get_st_other();
2211 this->st_other_
[i
] = st_other
;
2212 if ((st_other
& elfcpp::STO_PPC64_LOCAL_MASK
) != 0)
2214 if (this->abiversion() == 0)
2215 this->set_abiversion(2);
2216 else if (this->abiversion() < 2)
2217 gold_error(_("%s: local symbol %d has invalid st_other"
2218 " for ABI version 1"),
2219 this->name().c_str(), i
);
2226 template<int size
, bool big_endian
>
2228 Powerpc_dynobj
<size
, big_endian
>::set_abiversion(int ver
)
2230 this->e_flags_
|= ver
;
2231 if (this->abiversion() != 0)
2233 Target_powerpc
<size
, big_endian
>* target
=
2234 static_cast<Target_powerpc
<size
, big_endian
>*>(
2235 parameters
->sized_target
<size
, big_endian
>());
2236 if (target
->abiversion() == 0)
2237 target
->set_abiversion(this->abiversion());
2238 else if (target
->abiversion() != this->abiversion())
2239 gold_error(_("%s: ABI version %d is not compatible "
2240 "with ABI version %d output"),
2241 this->name().c_str(),
2242 this->abiversion(), target
->abiversion());
2247 // Call Sized_dynobj::base_read_symbols to read the symbols then
2248 // read .opd from a dynamic object, filling in opd_ent_ vector,
2250 template<int size
, bool big_endian
>
2252 Powerpc_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
2254 this->base_read_symbols(sd
);
2257 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2258 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2259 const unsigned char* namesu
= sd
->section_names
->data();
2260 const char* names
= reinterpret_cast<const char*>(namesu
);
2261 const unsigned char* s
= NULL
;
2262 const unsigned char* opd
;
2263 section_size_type opd_size
;
2265 // Find and read .opd section.
2268 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".opd", names
,
2269 sd
->section_names_size
,
2274 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2275 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2276 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
2278 if (this->abiversion() == 0)
2279 this->set_abiversion(1);
2280 else if (this->abiversion() > 1)
2281 gold_error(_("%s: .opd invalid in abiv%d"),
2282 this->name().c_str(), this->abiversion());
2284 this->opd_shndx_
= (s
- pshdrs
) / shdr_size
;
2285 this->opd_address_
= shdr
.get_sh_addr();
2286 opd_size
= convert_to_section_size_type(shdr
.get_sh_size());
2287 opd
= this->get_view(shdr
.get_sh_offset(), opd_size
,
2293 // Build set of executable sections.
2294 // Using a set is probably overkill. There is likely to be only
2295 // a few executable sections, typically .init, .text and .fini,
2296 // and they are generally grouped together.
2297 typedef std::set
<Sec_info
> Exec_sections
;
2298 Exec_sections exec_sections
;
2300 for (unsigned int i
= 1; i
< this->shnum(); ++i
, s
+= shdr_size
)
2302 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2303 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2304 && ((shdr
.get_sh_flags()
2305 & (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2306 == (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2307 && shdr
.get_sh_size() != 0)
2309 exec_sections
.insert(Sec_info(shdr
.get_sh_addr(),
2310 shdr
.get_sh_size(), i
));
2313 if (exec_sections
.empty())
2316 // Look over the OPD entries. This is complicated by the fact
2317 // that some binaries will use two-word entries while others
2318 // will use the standard three-word entries. In most cases
2319 // the third word (the environment pointer for languages like
2320 // Pascal) is unused and will be zero. If the third word is
2321 // used it should not be pointing into executable sections,
2323 this->init_opd(opd_size
);
2324 for (const unsigned char* p
= opd
; p
< opd
+ opd_size
; p
+= 8)
2326 typedef typename
elfcpp::Swap
<64, big_endian
>::Valtype Valtype
;
2327 const Valtype
* valp
= reinterpret_cast<const Valtype
*>(p
);
2328 Valtype val
= elfcpp::Swap
<64, big_endian
>::readval(valp
);
2330 // Chances are that this is the third word of an OPD entry.
2332 typename
Exec_sections::const_iterator e
2333 = exec_sections
.upper_bound(Sec_info(val
, 0, 0));
2334 if (e
!= exec_sections
.begin())
2337 if (e
->start
<= val
&& val
< e
->start
+ e
->len
)
2339 // We have an address in an executable section.
2340 // VAL ought to be the function entry, set it up.
2341 this->set_opd_ent(p
- opd
, e
->shndx
, val
);
2342 // Skip second word of OPD entry, the TOC pointer.
2346 // If we didn't match any executable sections, we likely
2347 // have a non-zero third word in the OPD entry.
2352 // Relocate sections.
2354 template<int size
, bool big_endian
>
2356 Powerpc_relobj
<size
, big_endian
>::do_relocate_sections(
2357 const Symbol_table
* symtab
, const Layout
* layout
,
2358 const unsigned char* pshdrs
, Output_file
* of
,
2359 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
)
2361 unsigned int start
= 1;
2363 && this->relatoc_
!= 0
2364 && !parameters
->options().relocatable())
2366 // Relocate .toc first.
2367 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2368 this->relatoc_
, this->relatoc_
);
2369 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2370 1, this->relatoc_
- 1);
2371 start
= this->relatoc_
+ 1;
2373 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2374 start
, this->shnum() - 1);
2377 // Set up some symbols.
2379 template<int size
, bool big_endian
>
2381 Target_powerpc
<size
, big_endian
>::do_define_standard_symbols(
2382 Symbol_table
* symtab
,
2387 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2388 // undefined when scanning relocs (and thus requires
2389 // non-relative dynamic relocs). The proper value will be
2391 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2392 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2394 Target_powerpc
<size
, big_endian
>* target
=
2395 static_cast<Target_powerpc
<size
, big_endian
>*>(
2396 parameters
->sized_target
<size
, big_endian
>());
2397 Output_data_got_powerpc
<size
, big_endian
>* got
2398 = target
->got_section(symtab
, layout
);
2399 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2400 Symbol_table::PREDEFINED
,
2404 elfcpp::STV_HIDDEN
, 0,
2408 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2409 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
2410 if (sdasym
!= NULL
&& sdasym
->is_undefined())
2412 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
2414 = layout
->add_output_section_data(".sdata", 0,
2416 | elfcpp::SHF_WRITE
,
2417 sdata
, ORDER_SMALL_DATA
, false);
2418 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
2419 Symbol_table::PREDEFINED
,
2420 os
, 32768, 0, elfcpp::STT_OBJECT
,
2421 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
2427 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2428 Symbol
*gotsym
= symtab
->lookup(".TOC.", NULL
);
2429 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2431 Target_powerpc
<size
, big_endian
>* target
=
2432 static_cast<Target_powerpc
<size
, big_endian
>*>(
2433 parameters
->sized_target
<size
, big_endian
>());
2434 Output_data_got_powerpc
<size
, big_endian
>* got
2435 = target
->got_section(symtab
, layout
);
2436 symtab
->define_in_output_data(".TOC.", NULL
,
2437 Symbol_table::PREDEFINED
,
2441 elfcpp::STV_HIDDEN
, 0,
2447 // Set up PowerPC target specific relobj.
2449 template<int size
, bool big_endian
>
2451 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
2452 const std::string
& name
,
2453 Input_file
* input_file
,
2454 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
2456 int et
= ehdr
.get_e_type();
2457 // ET_EXEC files are valid input for --just-symbols/-R,
2458 // and we treat them as relocatable objects.
2459 if (et
== elfcpp::ET_REL
2460 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
2462 Powerpc_relobj
<size
, big_endian
>* obj
=
2463 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2467 else if (et
== elfcpp::ET_DYN
)
2469 Powerpc_dynobj
<size
, big_endian
>* obj
=
2470 new Powerpc_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2476 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
2481 template<int size
, bool big_endian
>
2482 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
2485 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
2486 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
2488 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
2489 : Output_data_got
<size
, big_endian
>(),
2490 symtab_(symtab
), layout_(layout
),
2491 header_ent_cnt_(size
== 32 ? 3 : 1),
2492 header_index_(size
== 32 ? 0x2000 : 0)
2495 this->set_addralign(256);
2498 // Override all the Output_data_got methods we use so as to first call
2501 add_global(Symbol
* gsym
, unsigned int got_type
)
2503 this->reserve_ent();
2504 return Output_data_got
<size
, big_endian
>::add_global(gsym
, got_type
);
2508 add_global_plt(Symbol
* gsym
, unsigned int got_type
)
2510 this->reserve_ent();
2511 return Output_data_got
<size
, big_endian
>::add_global_plt(gsym
, got_type
);
2515 add_global_tls(Symbol
* gsym
, unsigned int got_type
)
2516 { return this->add_global_plt(gsym
, got_type
); }
2519 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
2520 Output_data_reloc_generic
* rel_dyn
, unsigned int r_type
)
2522 this->reserve_ent();
2523 Output_data_got
<size
, big_endian
>::
2524 add_global_with_rel(gsym
, got_type
, rel_dyn
, r_type
);
2528 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
2529 Output_data_reloc_generic
* rel_dyn
,
2530 unsigned int r_type_1
, unsigned int r_type_2
)
2532 if (gsym
->has_got_offset(got_type
))
2535 this->reserve_ent(2);
2536 Output_data_got
<size
, big_endian
>::
2537 add_global_pair_with_rel(gsym
, got_type
, rel_dyn
, r_type_1
, r_type_2
);
2541 add_local(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2543 this->reserve_ent();
2544 return Output_data_got
<size
, big_endian
>::add_local(object
, sym_index
,
2549 add_local_plt(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2551 this->reserve_ent();
2552 return Output_data_got
<size
, big_endian
>::add_local_plt(object
, sym_index
,
2557 add_local_tls(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2558 { return this->add_local_plt(object
, sym_index
, got_type
); }
2561 add_local_tls_pair(Relobj
* object
, unsigned int sym_index
,
2562 unsigned int got_type
,
2563 Output_data_reloc_generic
* rel_dyn
,
2564 unsigned int r_type
)
2566 if (object
->local_has_got_offset(sym_index
, got_type
))
2569 this->reserve_ent(2);
2570 Output_data_got
<size
, big_endian
>::
2571 add_local_tls_pair(object
, sym_index
, got_type
, rel_dyn
, r_type
);
2575 add_constant(Valtype constant
)
2577 this->reserve_ent();
2578 return Output_data_got
<size
, big_endian
>::add_constant(constant
);
2582 add_constant_pair(Valtype c1
, Valtype c2
)
2584 this->reserve_ent(2);
2585 return Output_data_got
<size
, big_endian
>::add_constant_pair(c1
, c2
);
2588 // Offset of _GLOBAL_OFFSET_TABLE_.
2592 return this->got_offset(this->header_index_
);
2595 // Offset of base used to access the GOT/TOC.
2596 // The got/toc pointer reg will be set to this value.
2598 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
2601 return this->g_o_t();
2603 return (this->output_section()->address()
2604 + object
->toc_base_offset()
2608 // Ensure our GOT has a header.
2610 set_final_data_size()
2612 if (this->header_ent_cnt_
!= 0)
2613 this->make_header();
2614 Output_data_got
<size
, big_endian
>::set_final_data_size();
2617 // First word of GOT header needs some values that are not
2618 // handled by Output_data_got so poke them in here.
2619 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2621 do_write(Output_file
* of
)
2624 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
2625 val
= this->layout_
->dynamic_section()->address();
2627 val
= this->output_section()->address() + 0x8000;
2628 this->replace_constant(this->header_index_
, val
);
2629 Output_data_got
<size
, big_endian
>::do_write(of
);
2634 reserve_ent(unsigned int cnt
= 1)
2636 if (this->header_ent_cnt_
== 0)
2638 if (this->num_entries() + cnt
> this->header_index_
)
2639 this->make_header();
2645 this->header_ent_cnt_
= 0;
2646 this->header_index_
= this->num_entries();
2649 Output_data_got
<size
, big_endian
>::add_constant(0);
2650 Output_data_got
<size
, big_endian
>::add_constant(0);
2651 Output_data_got
<size
, big_endian
>::add_constant(0);
2653 // Define _GLOBAL_OFFSET_TABLE_ at the header
2654 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2657 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
2658 sym
->set_value(this->g_o_t());
2661 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2662 Symbol_table::PREDEFINED
,
2663 this, this->g_o_t(), 0,
2666 elfcpp::STV_HIDDEN
, 0,
2670 Output_data_got
<size
, big_endian
>::add_constant(0);
2673 // Stashed pointers.
2674 Symbol_table
* symtab_
;
2678 unsigned int header_ent_cnt_
;
2679 // GOT header index.
2680 unsigned int header_index_
;
2683 // Get the GOT section, creating it if necessary.
2685 template<int size
, bool big_endian
>
2686 Output_data_got_powerpc
<size
, big_endian
>*
2687 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
2690 if (this->got_
== NULL
)
2692 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2695 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
2697 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2698 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2699 this->got_
, ORDER_DATA
, false);
2705 // Get the dynamic reloc section, creating it if necessary.
2707 template<int size
, bool big_endian
>
2708 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2709 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
2711 if (this->rela_dyn_
== NULL
)
2713 gold_assert(layout
!= NULL
);
2714 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
2715 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
2716 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
2717 ORDER_DYNAMIC_RELOCS
, false);
2719 return this->rela_dyn_
;
2722 // Similarly, but for ifunc symbols get the one for ifunc.
2724 template<int size
, bool big_endian
>
2725 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2726 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Symbol_table
* symtab
,
2731 return this->rela_dyn_section(layout
);
2733 if (this->iplt_
== NULL
)
2734 this->make_iplt_section(symtab
, layout
);
2735 return this->iplt_
->rel_plt();
2741 // Determine the stub group size. The group size is the absolute
2742 // value of the parameter --stub-group-size. If --stub-group-size
2743 // is passed a negative value, we restrict stubs to be always after
2744 // the stubbed branches.
2745 Stub_control(int32_t size
, bool no_size_errors
, bool multi_os
)
2746 : stub_group_size_(abs(size
)), stubs_always_after_branch_(size
< 0),
2747 suppress_size_errors_(no_size_errors
), multi_os_(multi_os
),
2748 state_(NO_GROUP
), group_size_(0), group_start_addr_(0),
2749 owner_(NULL
), output_section_(NULL
)
2753 // Return true iff input section can be handled by current stub
2756 can_add_to_stub_group(Output_section
* o
,
2757 const Output_section::Input_section
* i
,
2760 const Output_section::Input_section
*
2766 { return output_section_
; }
2769 set_output_and_owner(Output_section
* o
,
2770 const Output_section::Input_section
* i
)
2772 this->output_section_
= o
;
2781 // Adding group sections before the stubs.
2782 FINDING_STUB_SECTION
,
2783 // Adding group sections after the stubs.
2787 uint32_t stub_group_size_
;
2788 bool stubs_always_after_branch_
;
2789 bool suppress_size_errors_
;
2790 // True if a stub group can serve multiple output sections.
2793 // Current max size of group. Starts at stub_group_size_ but is
2794 // reduced to stub_group_size_/1024 on seeing a section with
2795 // external conditional branches.
2796 uint32_t group_size_
;
2797 uint64_t group_start_addr_
;
2798 // owner_ and output_section_ specify the section to which stubs are
2799 // attached. The stubs are placed at the end of this section.
2800 const Output_section::Input_section
* owner_
;
2801 Output_section
* output_section_
;
2804 // Return true iff input section can be handled by current stub
2805 // group. Sections are presented to this function in order,
2806 // so the first section is the head of the group.
2809 Stub_control::can_add_to_stub_group(Output_section
* o
,
2810 const Output_section::Input_section
* i
,
2813 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
2815 uint64_t start_addr
= o
->address();
2818 // .init and .fini sections are pasted together to form a single
2819 // function. We can't be adding stubs in the middle of the function.
2820 this_size
= o
->data_size();
2823 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
2824 this_size
= i
->data_size();
2827 uint64_t end_addr
= start_addr
+ this_size
;
2828 uint32_t group_size
= this->stub_group_size_
;
2830 this->group_size_
= group_size
= group_size
>> 10;
2832 if (this_size
> group_size
&& !this->suppress_size_errors_
)
2833 gold_warning(_("%s:%s exceeds group size"),
2834 i
->relobj()->name().c_str(),
2835 i
->relobj()->section_name(i
->shndx()).c_str());
2837 gold_debug(DEBUG_TARGET
, "maybe add%s %s:%s size=%#llx total=%#llx",
2838 has14
? " 14bit" : "",
2839 i
->relobj()->name().c_str(),
2840 i
->relobj()->section_name(i
->shndx()).c_str(),
2841 (long long) this_size
,
2842 (this->state_
== NO_GROUP
2844 : (long long) end_addr
- this->group_start_addr_
));
2846 if (this->state_
== NO_GROUP
)
2848 // Only here on very first use of Stub_control
2850 this->output_section_
= o
;
2851 this->state_
= FINDING_STUB_SECTION
;
2852 this->group_size_
= group_size
;
2853 this->group_start_addr_
= start_addr
;
2856 else if (!this->multi_os_
&& this->output_section_
!= o
)
2858 else if (this->state_
== HAS_STUB_SECTION
)
2860 // Can we add this section, which is after the stubs, to the
2862 if (end_addr
- this->group_start_addr_
<= this->group_size_
)
2865 else if (this->state_
== FINDING_STUB_SECTION
)
2867 if ((whole_sec
&& this->output_section_
== o
)
2868 || end_addr
- this->group_start_addr_
<= this->group_size_
)
2870 // Stubs are added at the end of "owner_".
2872 this->output_section_
= o
;
2875 // The group before the stubs has reached maximum size.
2876 // Now see about adding sections after the stubs to the
2877 // group. If the current section has a 14-bit branch and
2878 // the group before the stubs exceeds group_size_ (because
2879 // they didn't have 14-bit branches), don't add sections
2880 // after the stubs: The size of stubs for such a large
2881 // group may exceed the reach of a 14-bit branch.
2882 if (!this->stubs_always_after_branch_
2883 && this_size
<= this->group_size_
2884 && start_addr
- this->group_start_addr_
<= this->group_size_
)
2886 gold_debug(DEBUG_TARGET
, "adding after stubs");
2887 this->state_
= HAS_STUB_SECTION
;
2888 this->group_start_addr_
= start_addr
;
2895 gold_debug(DEBUG_TARGET
,
2896 !this->multi_os_
&& this->output_section_
!= o
2897 ? "nope, new output section\n"
2898 : "nope, didn't fit\n");
2900 // The section fails to fit in the current group. Set up a few
2901 // things for the next group. owner_ and output_section_ will be
2902 // set later after we've retrieved those values for the current
2904 this->state_
= FINDING_STUB_SECTION
;
2905 this->group_size_
= group_size
;
2906 this->group_start_addr_
= start_addr
;
2910 // Look over all the input sections, deciding where to place stubs.
2912 template<int size
, bool big_endian
>
2914 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
2916 bool no_size_errors
)
2918 Stub_control
stub_control(this->stub_group_size_
, no_size_errors
,
2919 parameters
->options().stub_group_multi());
2921 // Group input sections and insert stub table
2922 Stub_table_owner
* table_owner
= NULL
;
2923 std::vector
<Stub_table_owner
*> tables
;
2924 Layout::Section_list section_list
;
2925 layout
->get_executable_sections(§ion_list
);
2926 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
2927 for (Layout::Section_list::iterator o
= section_list
.begin();
2928 o
!= section_list
.end();
2931 typedef Output_section::Input_section_list Input_section_list
;
2932 for (Input_section_list::const_iterator i
2933 = (*o
)->input_sections().begin();
2934 i
!= (*o
)->input_sections().end();
2937 if (i
->is_input_section()
2938 || i
->is_relaxed_input_section())
2940 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2941 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2942 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
2943 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
2945 table_owner
->output_section
= stub_control
.output_section();
2946 table_owner
->owner
= stub_control
.owner();
2947 stub_control
.set_output_and_owner(*o
, &*i
);
2950 if (table_owner
== NULL
)
2952 table_owner
= new Stub_table_owner
;
2953 tables
.push_back(table_owner
);
2955 ppcobj
->set_stub_table(i
->shndx(), tables
.size() - 1);
2959 if (table_owner
!= NULL
)
2961 table_owner
->output_section
= stub_control
.output_section();
2962 table_owner
->owner
= stub_control
.owner();;
2964 for (typename
std::vector
<Stub_table_owner
*>::iterator t
= tables
.begin();
2968 Stub_table
<size
, big_endian
>* stub_table
;
2970 if ((*t
)->owner
->is_input_section())
2971 stub_table
= new Stub_table
<size
, big_endian
>(this,
2972 (*t
)->output_section
,
2974 this->stub_tables_
.size());
2975 else if ((*t
)->owner
->is_relaxed_input_section())
2976 stub_table
= static_cast<Stub_table
<size
, big_endian
>*>(
2977 (*t
)->owner
->relaxed_input_section());
2980 this->stub_tables_
.push_back(stub_table
);
2985 static unsigned long
2986 max_branch_delta (unsigned int r_type
)
2988 if (r_type
== elfcpp::R_POWERPC_REL14
2989 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
2990 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
2992 if (r_type
== elfcpp::R_POWERPC_REL24
2993 || r_type
== elfcpp::R_PPC_PLTREL24
2994 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
2999 // Return whether this branch is going via a plt call stub.
3001 template<int size
, bool big_endian
>
3003 Target_powerpc
<size
, big_endian
>::Branch_info::mark_pltcall(
3004 Powerpc_relobj
<size
, big_endian
>* ppc_object
,
3007 Target_powerpc
* target
,
3008 Symbol_table
* symtab
)
3010 if (this->object_
!= ppc_object
3011 || this->shndx_
!= shndx
3012 || this->offset_
!= offset
)
3015 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
3016 if (sym
!= NULL
&& sym
->is_forwarder())
3017 sym
= symtab
->resolve_forwards(sym
);
3018 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
3020 ? (gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
3021 && !target
->is_elfv2_localentry0(gsym
))
3022 : (this->object_
->local_has_plt_offset(this->r_sym_
)
3023 && !target
->is_elfv2_localentry0(this->object_
, this->r_sym_
)))
3031 // If this branch needs a plt call stub, or a long branch stub, make one.
3033 template<int size
, bool big_endian
>
3035 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
3036 Stub_table
<size
, big_endian
>* stub_table
,
3037 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
3038 Symbol_table
* symtab
) const
3040 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
3041 if (sym
!= NULL
&& sym
->is_forwarder())
3042 sym
= symtab
->resolve_forwards(sym
);
3043 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
3044 Target_powerpc
<size
, big_endian
>* target
=
3045 static_cast<Target_powerpc
<size
, big_endian
>*>(
3046 parameters
->sized_target
<size
, big_endian
>());
3050 ? gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
3051 : this->object_
->local_has_plt_offset(this->r_sym_
))
3055 && target
->abiversion() >= 2
3056 && !parameters
->options().output_is_position_independent()
3057 && !is_branch_reloc(this->r_type_
))
3058 target
->glink_section()->add_global_entry(gsym
);
3061 if (stub_table
== NULL
)
3062 stub_table
= this->object_
->stub_table(this->shndx_
);
3063 if (stub_table
== NULL
)
3065 // This is a ref from a data section to an ifunc symbol.
3066 stub_table
= ifunc_stub_table
;
3068 gold_assert(stub_table
!= NULL
);
3069 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
3070 if (from
!= invalid_address
)
3071 from
+= (this->object_
->output_section(this->shndx_
)->address()
3074 ok
= stub_table
->add_plt_call_entry(from
,
3075 this->object_
, gsym
,
3076 this->r_type_
, this->addend_
,
3079 ok
= stub_table
->add_plt_call_entry(from
,
3080 this->object_
, this->r_sym_
,
3081 this->r_type_
, this->addend_
,
3087 Address max_branch_offset
= max_branch_delta(this->r_type_
);
3088 if (max_branch_offset
== 0)
3090 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
3091 gold_assert(from
!= invalid_address
);
3092 from
+= (this->object_
->output_section(this->shndx_
)->address()
3097 switch (gsym
->source())
3099 case Symbol::FROM_OBJECT
:
3101 Object
* symobj
= gsym
->object();
3102 if (symobj
->is_dynamic()
3103 || symobj
->pluginobj() != NULL
)
3106 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
3107 if (shndx
== elfcpp::SHN_UNDEF
)
3112 case Symbol::IS_UNDEFINED
:
3118 Symbol_table::Compute_final_value_status status
;
3119 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
3120 if (status
!= Symbol_table::CFVS_OK
)
3123 to
+= this->object_
->ppc64_local_entry_offset(gsym
);
3127 const Symbol_value
<size
>* psymval
3128 = this->object_
->local_symbol(this->r_sym_
);
3129 Symbol_value
<size
> symval
;
3130 if (psymval
->is_section_symbol())
3131 symval
.set_is_section_symbol();
3132 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
3133 typename
ObjType::Compute_final_local_value_status status
3134 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
3136 if (status
!= ObjType::CFLV_OK
3137 || !symval
.has_output_value())
3139 to
= symval
.value(this->object_
, 0);
3141 to
+= this->object_
->ppc64_local_entry_offset(this->r_sym_
);
3143 if (!(size
== 32 && this->r_type_
== elfcpp::R_PPC_PLTREL24
))
3144 to
+= this->addend_
;
3145 if (stub_table
== NULL
)
3146 stub_table
= this->object_
->stub_table(this->shndx_
);
3147 if (size
== 64 && target
->abiversion() < 2)
3149 unsigned int dest_shndx
;
3150 if (!target
->symval_for_branch(symtab
, gsym
, this->object_
,
3154 Address delta
= to
- from
;
3155 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
3157 if (stub_table
== NULL
)
3159 gold_warning(_("%s:%s: branch in non-executable section,"
3160 " no long branch stub for you"),
3161 this->object_
->name().c_str(),
3162 this->object_
->section_name(this->shndx_
).c_str());
3165 bool save_res
= (size
== 64
3167 && gsym
->source() == Symbol::IN_OUTPUT_DATA
3168 && gsym
->output_data() == target
->savres_section());
3169 ok
= stub_table
->add_long_branch_entry(this->object_
,
3171 from
, to
, save_res
);
3175 gold_debug(DEBUG_TARGET
,
3176 "branch at %s:%s+%#lx\n"
3177 "can't reach stub attached to %s:%s",
3178 this->object_
->name().c_str(),
3179 this->object_
->section_name(this->shndx_
).c_str(),
3180 (unsigned long) this->offset_
,
3181 stub_table
->relobj()->name().c_str(),
3182 stub_table
->relobj()->section_name(stub_table
->shndx()).c_str());
3187 // Relaxation hook. This is where we do stub generation.
3189 template<int size
, bool big_endian
>
3191 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
3192 const Input_objects
*,
3193 Symbol_table
* symtab
,
3197 unsigned int prev_brlt_size
= 0;
3201 = this->abiversion() < 2 && parameters
->options().plt_thread_safe();
3203 && this->abiversion() < 2
3205 && !parameters
->options().user_set_plt_thread_safe())
3207 static const char* const thread_starter
[] =
3211 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3213 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3214 "mq_notify", "create_timer",
3219 "GOMP_parallel_start",
3220 "GOMP_parallel_loop_static",
3221 "GOMP_parallel_loop_static_start",
3222 "GOMP_parallel_loop_dynamic",
3223 "GOMP_parallel_loop_dynamic_start",
3224 "GOMP_parallel_loop_guided",
3225 "GOMP_parallel_loop_guided_start",
3226 "GOMP_parallel_loop_runtime",
3227 "GOMP_parallel_loop_runtime_start",
3228 "GOMP_parallel_sections",
3229 "GOMP_parallel_sections_start",
3234 if (parameters
->options().shared())
3238 for (unsigned int i
= 0;
3239 i
< sizeof(thread_starter
) / sizeof(thread_starter
[0]);
3242 Symbol
* sym
= symtab
->lookup(thread_starter
[i
], NULL
);
3243 thread_safe
= (sym
!= NULL
3245 && sym
->in_real_elf());
3251 this->plt_thread_safe_
= thread_safe
;
3256 this->stub_group_size_
= parameters
->options().stub_group_size();
3257 bool no_size_errors
= true;
3258 if (this->stub_group_size_
== 1)
3259 this->stub_group_size_
= 0x1c00000;
3260 else if (this->stub_group_size_
== -1)
3261 this->stub_group_size_
= -0x1e00000;
3263 no_size_errors
= false;
3264 this->group_sections(layout
, task
, no_size_errors
);
3266 else if (this->relax_failed_
&& this->relax_fail_count_
< 3)
3268 this->branch_lookup_table_
.clear();
3269 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3270 p
!= this->stub_tables_
.end();
3273 (*p
)->clear_stubs(true);
3275 this->stub_tables_
.clear();
3276 this->stub_group_size_
= this->stub_group_size_
/ 4 * 3;
3277 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3278 program_name
, this->stub_group_size_
);
3279 this->group_sections(layout
, task
, true);
3282 // We need address of stub tables valid for make_stub.
3283 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3284 p
!= this->stub_tables_
.end();
3287 const Powerpc_relobj
<size
, big_endian
>* object
3288 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
3289 Address off
= object
->get_output_section_offset((*p
)->shndx());
3290 gold_assert(off
!= invalid_address
);
3291 Output_section
* os
= (*p
)->output_section();
3292 (*p
)->set_address_and_size(os
, off
);
3297 // Clear plt call stubs, long branch stubs and branch lookup table.
3298 prev_brlt_size
= this->branch_lookup_table_
.size();
3299 this->branch_lookup_table_
.clear();
3300 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3301 p
!= this->stub_tables_
.end();
3304 (*p
)->clear_stubs(false);
3308 // Build all the stubs.
3309 this->relax_failed_
= false;
3310 Stub_table
<size
, big_endian
>* ifunc_stub_table
3311 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
3312 Stub_table
<size
, big_endian
>* one_stub_table
3313 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
3314 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
3315 b
!= this->branch_info_
.end();
3318 if (!b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
)
3319 && !this->relax_failed_
)
3321 this->relax_failed_
= true;
3322 this->relax_fail_count_
++;
3323 if (this->relax_fail_count_
< 3)
3328 // Did anything change size?
3329 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
3330 bool again
= num_huge_branches
!= prev_brlt_size
;
3331 if (size
== 64 && num_huge_branches
!= 0)
3332 this->make_brlt_section(layout
);
3333 if (size
== 64 && again
)
3334 this->brlt_section_
->set_current_size(num_huge_branches
);
3336 for (typename
Stub_tables::reverse_iterator p
= this->stub_tables_
.rbegin();
3337 p
!= this->stub_tables_
.rend();
3339 (*p
)->remove_eh_frame(layout
);
3341 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3342 p
!= this->stub_tables_
.end();
3344 (*p
)->add_eh_frame(layout
);
3346 typedef Unordered_set
<Output_section
*> Output_sections
;
3347 Output_sections os_need_update
;
3348 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3349 p
!= this->stub_tables_
.end();
3352 if ((*p
)->size_update())
3355 os_need_update
.insert((*p
)->output_section());
3359 // Set output section offsets for all input sections in an output
3360 // section that just changed size. Anything past the stubs will
3362 for (typename
Output_sections::iterator p
= os_need_update
.begin();
3363 p
!= os_need_update
.end();
3366 Output_section
* os
= *p
;
3368 typedef Output_section::Input_section_list Input_section_list
;
3369 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
3370 i
!= os
->input_sections().end();
3373 off
= align_address(off
, i
->addralign());
3374 if (i
->is_input_section() || i
->is_relaxed_input_section())
3375 i
->relobj()->set_section_offset(i
->shndx(), off
);
3376 if (i
->is_relaxed_input_section())
3378 Stub_table
<size
, big_endian
>* stub_table
3379 = static_cast<Stub_table
<size
, big_endian
>*>(
3380 i
->relaxed_input_section());
3381 Address stub_table_size
= stub_table
->set_address_and_size(os
, off
);
3382 off
+= stub_table_size
;
3383 // After a few iterations, set current stub table size
3384 // as min size threshold, so later stub tables can only
3387 stub_table
->set_min_size_threshold(stub_table_size
);
3390 off
+= i
->data_size();
3392 // If .branch_lt is part of this output section, then we have
3393 // just done the offset adjustment.
3394 os
->clear_section_offsets_need_adjustment();
3399 && num_huge_branches
!= 0
3400 && parameters
->options().output_is_position_independent())
3402 // Fill in the BRLT relocs.
3403 this->brlt_section_
->reset_brlt_sizes();
3404 for (typename
Branch_lookup_table::const_iterator p
3405 = this->branch_lookup_table_
.begin();
3406 p
!= this->branch_lookup_table_
.end();
3409 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
3411 this->brlt_section_
->finalize_brlt_sizes();
3415 && (parameters
->options().user_set_emit_stub_syms()
3416 ? parameters
->options().emit_stub_syms()
3418 || parameters
->options().output_is_position_independent()
3419 || parameters
->options().emit_relocs())))
3421 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3422 p
!= this->stub_tables_
.end();
3424 (*p
)->define_stub_syms(symtab
);
3426 if (this->glink_
!= NULL
)
3428 int stub_size
= this->glink_
->pltresolve_size
;
3429 Address value
= -stub_size
;
3435 this->define_local(symtab
, "__glink_PLTresolve",
3436 this->glink_
, value
, stub_size
);
3439 this->define_local(symtab
, "__glink", this->glink_
, 0, 0);
3446 template<int size
, bool big_endian
>
3448 Target_powerpc
<size
, big_endian
>::do_plt_fde_location(const Output_data
* plt
,
3449 unsigned char* oview
,
3453 uint64_t address
= plt
->address();
3454 off_t len
= plt
->data_size();
3456 if (plt
== this->glink_
)
3458 // See Output_data_glink::do_write() for glink contents.
3461 gold_assert(parameters
->doing_static_link());
3462 // Static linking may need stubs, to support ifunc and long
3463 // branches. We need to create an output section for
3464 // .eh_frame early in the link process, to have a place to
3465 // attach stub .eh_frame info. We also need to have
3466 // registered a CIE that matches the stub CIE. Both of
3467 // these requirements are satisfied by creating an FDE and
3468 // CIE for .glink, even though static linking will leave
3469 // .glink zero length.
3470 // ??? Hopefully generating an FDE with a zero address range
3471 // won't confuse anything that consumes .eh_frame info.
3473 else if (size
== 64)
3475 // There is one word before __glink_PLTresolve
3479 else if (parameters
->options().output_is_position_independent())
3481 // There are two FDEs for a position independent glink.
3482 // The first covers the branch table, the second
3483 // __glink_PLTresolve at the end of glink.
3484 off_t resolve_size
= this->glink_
->pltresolve_size
;
3485 if (oview
[9] == elfcpp::DW_CFA_nop
)
3486 len
-= resolve_size
;
3489 address
+= len
- resolve_size
;
3496 // Must be a stub table.
3497 const Stub_table
<size
, big_endian
>* stub_table
3498 = static_cast<const Stub_table
<size
, big_endian
>*>(plt
);
3499 uint64_t stub_address
= stub_table
->stub_address();
3500 len
-= stub_address
- address
;
3501 address
= stub_address
;
3504 *paddress
= address
;
3508 // A class to handle the PLT data.
3510 template<int size
, bool big_endian
>
3511 class Output_data_plt_powerpc
: public Output_section_data_build
3514 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3515 size
, big_endian
> Reloc_section
;
3517 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3518 Reloc_section
* plt_rel
,
3520 : Output_section_data_build(size
== 32 ? 4 : 8),
3526 // Add an entry to the PLT.
3531 add_ifunc_entry(Symbol
*);
3534 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
3536 // Return the .rela.plt section data.
3543 // Return the number of PLT entries.
3547 if (this->current_data_size() == 0)
3549 return ((this->current_data_size() - this->first_plt_entry_offset())
3550 / this->plt_entry_size());
3555 do_adjust_output_section(Output_section
* os
)
3560 // Write to a map file.
3562 do_print_to_mapfile(Mapfile
* mapfile
) const
3563 { mapfile
->print_output_data(this, this->name_
); }
3566 // Return the offset of the first non-reserved PLT entry.
3568 first_plt_entry_offset() const
3570 // IPLT has no reserved entry.
3571 if (this->name_
[3] == 'I')
3573 return this->targ_
->first_plt_entry_offset();
3576 // Return the size of each PLT entry.
3578 plt_entry_size() const
3580 return this->targ_
->plt_entry_size();
3583 // Write out the PLT data.
3585 do_write(Output_file
*);
3587 // The reloc section.
3588 Reloc_section
* rel_
;
3589 // Allows access to .glink for do_write.
3590 Target_powerpc
<size
, big_endian
>* targ_
;
3591 // What to report in map file.
3595 // Add an entry to the PLT.
3597 template<int size
, bool big_endian
>
3599 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
3601 if (!gsym
->has_plt_offset())
3603 section_size_type off
= this->current_data_size();
3605 off
+= this->first_plt_entry_offset();
3606 gsym
->set_plt_offset(off
);
3607 gsym
->set_needs_dynsym_entry();
3608 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
3609 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
3610 off
+= this->plt_entry_size();
3611 this->set_current_data_size(off
);
3615 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3617 template<int size
, bool big_endian
>
3619 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
3621 if (!gsym
->has_plt_offset())
3623 section_size_type off
= this->current_data_size();
3624 gsym
->set_plt_offset(off
);
3625 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3626 if (size
== 64 && this->targ_
->abiversion() < 2)
3627 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3628 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
3629 off
+= this->plt_entry_size();
3630 this->set_current_data_size(off
);
3634 // Add an entry for a local ifunc symbol to the IPLT.
3636 template<int size
, bool big_endian
>
3638 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
3639 Sized_relobj_file
<size
, big_endian
>* relobj
,
3640 unsigned int local_sym_index
)
3642 if (!relobj
->local_has_plt_offset(local_sym_index
))
3644 section_size_type off
= this->current_data_size();
3645 relobj
->set_local_plt_offset(local_sym_index
, off
);
3646 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3647 if (size
== 64 && this->targ_
->abiversion() < 2)
3648 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3649 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
3651 off
+= this->plt_entry_size();
3652 this->set_current_data_size(off
);
3656 static const uint32_t add_0_11_11
= 0x7c0b5a14;
3657 static const uint32_t add_2_2_11
= 0x7c425a14;
3658 static const uint32_t add_2_2_12
= 0x7c426214;
3659 static const uint32_t add_3_3_2
= 0x7c631214;
3660 static const uint32_t add_3_3_13
= 0x7c636a14;
3661 static const uint32_t add_11_0_11
= 0x7d605a14;
3662 static const uint32_t add_11_2_11
= 0x7d625a14;
3663 static const uint32_t add_11_11_2
= 0x7d6b1214;
3664 static const uint32_t addi_0_12
= 0x380c0000;
3665 static const uint32_t addi_2_2
= 0x38420000;
3666 static const uint32_t addi_3_3
= 0x38630000;
3667 static const uint32_t addi_11_11
= 0x396b0000;
3668 static const uint32_t addi_12_1
= 0x39810000;
3669 static const uint32_t addi_12_12
= 0x398c0000;
3670 static const uint32_t addis_0_2
= 0x3c020000;
3671 static const uint32_t addis_0_13
= 0x3c0d0000;
3672 static const uint32_t addis_2_12
= 0x3c4c0000;
3673 static const uint32_t addis_11_2
= 0x3d620000;
3674 static const uint32_t addis_11_11
= 0x3d6b0000;
3675 static const uint32_t addis_11_30
= 0x3d7e0000;
3676 static const uint32_t addis_12_1
= 0x3d810000;
3677 static const uint32_t addis_12_2
= 0x3d820000;
3678 static const uint32_t addis_12_12
= 0x3d8c0000;
3679 static const uint32_t b
= 0x48000000;
3680 static const uint32_t bcl_20_31
= 0x429f0005;
3681 static const uint32_t bctr
= 0x4e800420;
3682 static const uint32_t blr
= 0x4e800020;
3683 static const uint32_t bnectr_p4
= 0x4ce20420;
3684 static const uint32_t cmpld_7_12_0
= 0x7fac0040;
3685 static const uint32_t cmpldi_2_0
= 0x28220000;
3686 static const uint32_t cror_15_15_15
= 0x4def7b82;
3687 static const uint32_t cror_31_31_31
= 0x4ffffb82;
3688 static const uint32_t ld_0_1
= 0xe8010000;
3689 static const uint32_t ld_0_12
= 0xe80c0000;
3690 static const uint32_t ld_2_1
= 0xe8410000;
3691 static const uint32_t ld_2_2
= 0xe8420000;
3692 static const uint32_t ld_2_11
= 0xe84b0000;
3693 static const uint32_t ld_2_12
= 0xe84c0000;
3694 static const uint32_t ld_11_2
= 0xe9620000;
3695 static const uint32_t ld_11_11
= 0xe96b0000;
3696 static const uint32_t ld_12_2
= 0xe9820000;
3697 static const uint32_t ld_12_11
= 0xe98b0000;
3698 static const uint32_t ld_12_12
= 0xe98c0000;
3699 static const uint32_t lfd_0_1
= 0xc8010000;
3700 static const uint32_t li_0_0
= 0x38000000;
3701 static const uint32_t li_12_0
= 0x39800000;
3702 static const uint32_t lis_0
= 0x3c000000;
3703 static const uint32_t lis_2
= 0x3c400000;
3704 static const uint32_t lis_11
= 0x3d600000;
3705 static const uint32_t lis_12
= 0x3d800000;
3706 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
3707 static const uint32_t lwz_0_12
= 0x800c0000;
3708 static const uint32_t lwz_11_11
= 0x816b0000;
3709 static const uint32_t lwz_11_30
= 0x817e0000;
3710 static const uint32_t lwz_12_12
= 0x818c0000;
3711 static const uint32_t lwzu_0_12
= 0x840c0000;
3712 static const uint32_t mflr_0
= 0x7c0802a6;
3713 static const uint32_t mflr_11
= 0x7d6802a6;
3714 static const uint32_t mflr_12
= 0x7d8802a6;
3715 static const uint32_t mtctr_0
= 0x7c0903a6;
3716 static const uint32_t mtctr_11
= 0x7d6903a6;
3717 static const uint32_t mtctr_12
= 0x7d8903a6;
3718 static const uint32_t mtlr_0
= 0x7c0803a6;
3719 static const uint32_t mtlr_12
= 0x7d8803a6;
3720 static const uint32_t nop
= 0x60000000;
3721 static const uint32_t ori_0_0_0
= 0x60000000;
3722 static const uint32_t srdi_0_0_2
= 0x7800f082;
3723 static const uint32_t std_0_1
= 0xf8010000;
3724 static const uint32_t std_0_12
= 0xf80c0000;
3725 static const uint32_t std_2_1
= 0xf8410000;
3726 static const uint32_t stfd_0_1
= 0xd8010000;
3727 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
3728 static const uint32_t sub_11_11_12
= 0x7d6c5850;
3729 static const uint32_t sub_12_12_11
= 0x7d8b6050;
3730 static const uint32_t xor_2_12_12
= 0x7d826278;
3731 static const uint32_t xor_11_12_12
= 0x7d8b6278;
3733 // Write out the PLT.
3735 template<int size
, bool big_endian
>
3737 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3739 if (size
== 32 && this->name_
[3] != 'I')
3741 const section_size_type offset
= this->offset();
3742 const section_size_type oview_size
3743 = convert_to_section_size_type(this->data_size());
3744 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3745 unsigned char* pov
= oview
;
3746 unsigned char* endpov
= oview
+ oview_size
;
3748 // The address of the .glink branch table
3749 const Output_data_glink
<size
, big_endian
>* glink
3750 = this->targ_
->glink_section();
3751 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
3753 while (pov
< endpov
)
3755 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
3760 of
->write_output_view(offset
, oview_size
, oview
);
3764 // Create the PLT section.
3766 template<int size
, bool big_endian
>
3768 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
3771 if (this->plt_
== NULL
)
3773 if (this->got_
== NULL
)
3774 this->got_section(symtab
, layout
);
3776 if (this->glink_
== NULL
)
3777 make_glink_section(layout
);
3779 // Ensure that .rela.dyn always appears before .rela.plt This is
3780 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3781 // needs to include .rela.plt in its range.
3782 this->rela_dyn_section(layout
);
3784 Reloc_section
* plt_rel
= new Reloc_section(false);
3785 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
3786 elfcpp::SHF_ALLOC
, plt_rel
,
3787 ORDER_DYNAMIC_PLT_RELOCS
, false);
3789 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
3791 layout
->add_output_section_data(".plt",
3793 ? elfcpp::SHT_PROGBITS
3794 : elfcpp::SHT_NOBITS
),
3795 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3802 Output_section
* rela_plt_os
= plt_rel
->output_section();
3803 rela_plt_os
->set_info_section(this->plt_
->output_section());
3807 // Create the IPLT section.
3809 template<int size
, bool big_endian
>
3811 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
3814 if (this->iplt_
== NULL
)
3816 this->make_plt_section(symtab
, layout
);
3818 Reloc_section
* iplt_rel
= new Reloc_section(false);
3819 if (this->rela_dyn_
->output_section())
3820 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
3822 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
3824 if (this->plt_
->output_section())
3825 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
3829 // A section for huge long branch addresses, similar to plt section.
3831 template<int size
, bool big_endian
>
3832 class Output_data_brlt_powerpc
: public Output_section_data_build
3835 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3836 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3837 size
, big_endian
> Reloc_section
;
3839 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3840 Reloc_section
* brlt_rel
)
3841 : Output_section_data_build(size
== 32 ? 4 : 8),
3849 this->reset_data_size();
3850 this->rel_
->reset_data_size();
3854 finalize_brlt_sizes()
3856 this->finalize_data_size();
3857 this->rel_
->finalize_data_size();
3860 // Add a reloc for an entry in the BRLT.
3862 add_reloc(Address to
, unsigned int off
)
3863 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
3865 // Update section and reloc section size.
3867 set_current_size(unsigned int num_branches
)
3869 this->reset_address_and_file_offset();
3870 this->set_current_data_size(num_branches
* 16);
3871 this->finalize_data_size();
3872 Output_section
* os
= this->output_section();
3873 os
->set_section_offsets_need_adjustment();
3874 if (this->rel_
!= NULL
)
3876 const unsigned int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
3877 this->rel_
->reset_address_and_file_offset();
3878 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
3879 this->rel_
->finalize_data_size();
3880 Output_section
* os
= this->rel_
->output_section();
3881 os
->set_section_offsets_need_adjustment();
3887 do_adjust_output_section(Output_section
* os
)
3892 // Write to a map file.
3894 do_print_to_mapfile(Mapfile
* mapfile
) const
3895 { mapfile
->print_output_data(this, "** BRLT"); }
3898 // Write out the BRLT data.
3900 do_write(Output_file
*);
3902 // The reloc section.
3903 Reloc_section
* rel_
;
3904 Target_powerpc
<size
, big_endian
>* targ_
;
3907 // Make the branch lookup table section.
3909 template<int size
, bool big_endian
>
3911 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
3913 if (size
== 64 && this->brlt_section_
== NULL
)
3915 Reloc_section
* brlt_rel
= NULL
;
3916 bool is_pic
= parameters
->options().output_is_position_independent();
3919 // When PIC we can't fill in .branch_lt (like .plt it can be
3920 // a bss style section) but must initialise at runtime via
3921 // dynamic relocations.
3922 this->rela_dyn_section(layout
);
3923 brlt_rel
= new Reloc_section(false);
3924 if (this->rela_dyn_
->output_section())
3925 this->rela_dyn_
->output_section()
3926 ->add_output_section_data(brlt_rel
);
3929 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
3930 if (this->plt_
&& is_pic
&& this->plt_
->output_section())
3931 this->plt_
->output_section()
3932 ->add_output_section_data(this->brlt_section_
);
3934 layout
->add_output_section_data(".branch_lt",
3935 (is_pic
? elfcpp::SHT_NOBITS
3936 : elfcpp::SHT_PROGBITS
),
3937 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3938 this->brlt_section_
,
3939 (is_pic
? ORDER_SMALL_BSS
3940 : ORDER_SMALL_DATA
),
3945 // Write out .branch_lt when non-PIC.
3947 template<int size
, bool big_endian
>
3949 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3951 if (size
== 64 && !parameters
->options().output_is_position_independent())
3953 const section_size_type offset
= this->offset();
3954 const section_size_type oview_size
3955 = convert_to_section_size_type(this->data_size());
3956 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3958 this->targ_
->write_branch_lookup_table(oview
);
3959 of
->write_output_view(offset
, oview_size
, oview
);
3963 static inline uint32_t
3969 static inline uint32_t
3975 static inline uint32_t
3978 return hi(a
+ 0x8000);
3984 static const unsigned char eh_frame_cie
[12];
3988 const unsigned char Eh_cie
<size
>::eh_frame_cie
[] =
3991 'z', 'R', 0, // Augmentation string.
3992 4, // Code alignment.
3993 0x80 - size
/ 8 , // Data alignment.
3995 1, // Augmentation size.
3996 (elfcpp::DW_EH_PE_pcrel
3997 | elfcpp::DW_EH_PE_sdata4
), // FDE encoding.
3998 elfcpp::DW_CFA_def_cfa
, 1, 0 // def_cfa: r1 offset 0.
4001 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4002 static const unsigned char glink_eh_frame_fde_64v1
[] =
4004 0, 0, 0, 0, // Replaced with offset to .glink.
4005 0, 0, 0, 0, // Replaced with size of .glink.
4006 0, // Augmentation size.
4007 elfcpp::DW_CFA_advance_loc
+ 1,
4008 elfcpp::DW_CFA_register
, 65, 12,
4009 elfcpp::DW_CFA_advance_loc
+ 5,
4010 elfcpp::DW_CFA_restore_extended
, 65
4013 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4014 static const unsigned char glink_eh_frame_fde_64v2
[] =
4016 0, 0, 0, 0, // Replaced with offset to .glink.
4017 0, 0, 0, 0, // Replaced with size of .glink.
4018 0, // Augmentation size.
4019 elfcpp::DW_CFA_advance_loc
+ 1,
4020 elfcpp::DW_CFA_register
, 65, 0,
4021 elfcpp::DW_CFA_advance_loc
+ 7,
4022 elfcpp::DW_CFA_restore_extended
, 65
4025 // Describe __glink_PLTresolve use of LR, 32-bit version.
4026 static const unsigned char glink_eh_frame_fde_32
[] =
4028 0, 0, 0, 0, // Replaced with offset to .glink.
4029 0, 0, 0, 0, // Replaced with size of .glink.
4030 0, // Augmentation size.
4031 elfcpp::DW_CFA_advance_loc
+ 2,
4032 elfcpp::DW_CFA_register
, 65, 0,
4033 elfcpp::DW_CFA_advance_loc
+ 4,
4034 elfcpp::DW_CFA_restore_extended
, 65
4037 static const unsigned char default_fde
[] =
4039 0, 0, 0, 0, // Replaced with offset to stubs.
4040 0, 0, 0, 0, // Replaced with size of stubs.
4041 0, // Augmentation size.
4042 elfcpp::DW_CFA_nop
, // Pad.
4047 template<bool big_endian
>
4049 write_insn(unsigned char* p
, uint32_t v
)
4051 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
4054 // Stub_table holds information about plt and long branch stubs.
4055 // Stubs are built in an area following some input section determined
4056 // by group_sections(). This input section is converted to a relaxed
4057 // input section allowing it to be resized to accommodate the stubs
4059 template<int size
, bool big_endian
>
4060 class Stub_table
: public Output_relaxed_input_section
4065 Plt_stub_ent(unsigned int off
, unsigned int indx
)
4066 : off_(off
), indx_(indx
), r2save_(0), localentry0_(0)
4070 unsigned int indx_
: 30;
4071 unsigned int r2save_
: 1;
4072 unsigned int localentry0_
: 1;
4074 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4075 static const Address invalid_address
= static_cast<Address
>(0) - 1;
4077 Stub_table(Target_powerpc
<size
, big_endian
>* targ
,
4078 Output_section
* output_section
,
4079 const Output_section::Input_section
* owner
,
4081 : Output_relaxed_input_section(owner
->relobj(), owner
->shndx(),
4083 ->section_addralign(owner
->shndx())),
4084 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
4085 orig_data_size_(owner
->current_data_size()),
4086 plt_size_(0), last_plt_size_(0),
4087 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4088 eh_frame_added_(false), need_save_res_(false), uniq_(id
)
4090 this->set_output_section(output_section
);
4092 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
4093 new_relaxed
.push_back(this);
4094 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
4097 // Add a plt call stub.
4099 add_plt_call_entry(Address
,
4100 const Sized_relobj_file
<size
, big_endian
>*,
4107 add_plt_call_entry(Address
,
4108 const Sized_relobj_file
<size
, big_endian
>*,
4114 // Find a given plt call stub.
4116 find_plt_call_entry(const Symbol
*) const;
4119 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4120 unsigned int) const;
4123 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4129 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4134 // Add a long branch stub.
4136 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
4137 unsigned int, Address
, Address
, bool);
4140 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
4144 can_reach_stub(Address from
, unsigned int off
, unsigned int r_type
)
4146 Address max_branch_offset
= max_branch_delta(r_type
);
4147 if (max_branch_offset
== 0)
4149 gold_assert(from
!= invalid_address
);
4150 Address loc
= off
+ this->stub_address();
4151 return loc
- from
+ max_branch_offset
< 2 * max_branch_offset
;
4155 clear_stubs(bool all
)
4157 this->plt_call_stubs_
.clear();
4158 this->plt_size_
= 0;
4159 this->long_branch_stubs_
.clear();
4160 this->branch_size_
= 0;
4161 this->need_save_res_
= false;
4164 this->last_plt_size_
= 0;
4165 this->last_branch_size_
= 0;
4170 set_address_and_size(const Output_section
* os
, Address off
)
4172 Address start_off
= off
;
4173 off
+= this->orig_data_size_
;
4174 Address my_size
= this->plt_size_
+ this->branch_size_
;
4175 if (this->need_save_res_
)
4176 my_size
+= this->targ_
->savres_section()->data_size();
4178 off
= align_address(off
, this->stub_align());
4179 // Include original section size and alignment padding in size
4180 my_size
+= off
- start_off
;
4181 // Ensure new size is always larger than min size
4182 // threshold. Alignment requirement is included in "my_size", so
4183 // increase "my_size" does not invalidate alignment.
4184 if (my_size
< this->min_size_threshold_
)
4185 my_size
= this->min_size_threshold_
;
4186 this->reset_address_and_file_offset();
4187 this->set_current_data_size(my_size
);
4188 this->set_address_and_file_offset(os
->address() + start_off
,
4189 os
->offset() + start_off
);
4194 stub_address() const
4196 return align_address(this->address() + this->orig_data_size_
,
4197 this->stub_align());
4203 return align_address(this->offset() + this->orig_data_size_
,
4204 this->stub_align());
4209 { return this->plt_size_
; }
4212 set_min_size_threshold(Address min_size
)
4213 { this->min_size_threshold_
= min_size
; }
4216 define_stub_syms(Symbol_table
*);
4221 Output_section
* os
= this->output_section();
4222 if (os
->addralign() < this->stub_align())
4224 os
->set_addralign(this->stub_align());
4225 // FIXME: get rid of the insane checkpointing.
4226 // We can't increase alignment of the input section to which
4227 // stubs are attached; The input section may be .init which
4228 // is pasted together with other .init sections to form a
4229 // function. Aligning might insert zero padding resulting in
4230 // sigill. However we do need to increase alignment of the
4231 // output section so that the align_address() on offset in
4232 // set_address_and_size() adds the same padding as the
4233 // align_address() on address in stub_address().
4234 // What's more, we need this alignment for the layout done in
4235 // relaxation_loop_body() so that the output section starts at
4236 // a suitably aligned address.
4237 os
->checkpoint_set_addralign(this->stub_align());
4239 if (this->last_plt_size_
!= this->plt_size_
4240 || this->last_branch_size_
!= this->branch_size_
)
4242 this->last_plt_size_
= this->plt_size_
;
4243 this->last_branch_size_
= this->branch_size_
;
4249 // Add .eh_frame info for this stub section. Unlike other linker
4250 // generated .eh_frame this is added late in the link, because we
4251 // only want the .eh_frame info if this particular stub section is
4254 add_eh_frame(Layout
* layout
)
4256 if (!parameters
->options().ld_generated_unwind_info())
4259 // Since we add stub .eh_frame info late, it must be placed
4260 // after all other linker generated .eh_frame info so that
4261 // merge mapping need not be updated for input sections.
4262 // There is no provision to use a different CIE to that used
4264 if (!this->targ_
->has_glink())
4267 if (this->plt_size_
+ this->branch_size_
+ this->need_save_res_
== 0)
4270 layout
->add_eh_frame_for_plt(this,
4271 Eh_cie
<size
>::eh_frame_cie
,
4272 sizeof (Eh_cie
<size
>::eh_frame_cie
),
4274 sizeof (default_fde
));
4275 this->eh_frame_added_
= true;
4279 remove_eh_frame(Layout
* layout
)
4281 if (this->eh_frame_added_
)
4283 layout
->remove_eh_frame_for_plt(this,
4284 Eh_cie
<size
>::eh_frame_cie
,
4285 sizeof (Eh_cie
<size
>::eh_frame_cie
),
4287 sizeof (default_fde
));
4288 this->eh_frame_added_
= false;
4292 Target_powerpc
<size
, big_endian
>*
4298 class Plt_stub_key_hash
;
4299 typedef Unordered_map
<Plt_stub_key
, Plt_stub_ent
,
4300 Plt_stub_key_hash
> Plt_stub_entries
;
4301 class Branch_stub_ent
;
4302 class Branch_stub_ent_hash
;
4303 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
4304 Branch_stub_ent_hash
> Branch_stub_entries
;
4306 // Alignment of stub section.
4312 unsigned int min_align
= 32;
4313 unsigned int user_align
= 1 << parameters
->options().plt_align();
4314 return std::max(user_align
, min_align
);
4317 // Return the plt offset for the given call stub.
4319 plt_off(typename
Plt_stub_entries::const_iterator p
, bool* is_iplt
) const
4321 const Symbol
* gsym
= p
->first
.sym_
;
4324 *is_iplt
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
4325 && gsym
->can_use_relative_reloc(false));
4326 return gsym
->plt_offset();
4331 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
4332 unsigned int local_sym_index
= p
->first
.locsym_
;
4333 return relobj
->local_plt_offset(local_sym_index
);
4337 // Size of a given plt call stub.
4339 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
4345 Address plt_addr
= this->plt_off(p
, &is_iplt
);
4347 plt_addr
+= this->targ_
->iplt_section()->address();
4349 plt_addr
+= this->targ_
->plt_section()->address();
4350 Address got_addr
= this->targ_
->got_section()->output_section()->address();
4351 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4352 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
4353 got_addr
+= ppcobj
->toc_base_offset();
4354 Address off
= plt_addr
- got_addr
;
4355 unsigned int bytes
= 4 * 4 + 4 * (ha(off
) != 0);
4356 if (this->targ_
->abiversion() < 2)
4358 bool static_chain
= parameters
->options().plt_static_chain();
4359 bool thread_safe
= this->targ_
->plt_thread_safe();
4363 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
4365 unsigned int align
= 1 << parameters
->options().plt_align();
4367 bytes
= (bytes
+ align
- 1) & -align
;
4371 // Return long branch stub size.
4373 branch_stub_size(typename
Branch_stub_entries::const_iterator p
)
4375 Address loc
= this->stub_address() + this->last_plt_size_
+ p
->second
;
4376 if (p
->first
.dest_
- loc
+ (1 << 25) < 2 << 25)
4378 if (size
== 64 || !parameters
->options().output_is_position_independent())
4385 do_write(Output_file
*);
4387 // Plt call stub keys.
4391 Plt_stub_key(const Symbol
* sym
)
4392 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4395 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4396 unsigned int locsym_index
)
4397 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4400 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4402 unsigned int r_type
,
4404 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4407 this->addend_
= addend
;
4408 else if (parameters
->options().output_is_position_independent()
4409 && r_type
== elfcpp::R_PPC_PLTREL24
)
4411 this->addend_
= addend
;
4412 if (this->addend_
>= 32768)
4413 this->object_
= object
;
4417 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4418 unsigned int locsym_index
,
4419 unsigned int r_type
,
4421 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4424 this->addend_
= addend
;
4425 else if (parameters
->options().output_is_position_independent()
4426 && r_type
== elfcpp::R_PPC_PLTREL24
)
4427 this->addend_
= addend
;
4430 bool operator==(const Plt_stub_key
& that
) const
4432 return (this->sym_
== that
.sym_
4433 && this->object_
== that
.object_
4434 && this->addend_
== that
.addend_
4435 && this->locsym_
== that
.locsym_
);
4439 const Sized_relobj_file
<size
, big_endian
>* object_
;
4440 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
4441 unsigned int locsym_
;
4444 class Plt_stub_key_hash
4447 size_t operator()(const Plt_stub_key
& ent
) const
4449 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
4450 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
4456 // Long branch stub keys.
4457 class Branch_stub_ent
4460 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
,
4461 Address to
, bool save_res
)
4462 : dest_(to
), toc_base_off_(0), save_res_(save_res
)
4465 toc_base_off_
= obj
->toc_base_offset();
4468 bool operator==(const Branch_stub_ent
& that
) const
4470 return (this->dest_
== that
.dest_
4472 || this->toc_base_off_
== that
.toc_base_off_
));
4476 unsigned int toc_base_off_
;
4480 class Branch_stub_ent_hash
4483 size_t operator()(const Branch_stub_ent
& ent
) const
4484 { return ent
.dest_
^ ent
.toc_base_off_
; }
4487 // In a sane world this would be a global.
4488 Target_powerpc
<size
, big_endian
>* targ_
;
4489 // Map sym/object/addend to stub offset.
4490 Plt_stub_entries plt_call_stubs_
;
4491 // Map destination address to stub offset.
4492 Branch_stub_entries long_branch_stubs_
;
4493 // size of input section
4494 section_size_type orig_data_size_
;
4496 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
4497 // Some rare cases cause (PR/20529) fluctuation in stub table
4498 // size, which leads to an endless relax loop. This is to be fixed
4499 // by, after the first few iterations, allowing only increase of
4500 // stub table size. This variable sets the minimal possible size of
4501 // a stub table, it is zero for the first few iterations, then
4502 // increases monotonically.
4503 Address min_size_threshold_
;
4504 // Whether .eh_frame info has been created for this stub section.
4505 bool eh_frame_added_
;
4506 // Set if this stub group needs a copy of out-of-line register
4507 // save/restore functions.
4508 bool need_save_res_
;
4509 // Per stub table unique identifier.
4513 // Add a plt call stub, if we do not already have one for this
4514 // sym/object/addend combo.
4516 template<int size
, bool big_endian
>
4518 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4520 const Sized_relobj_file
<size
, big_endian
>* object
,
4522 unsigned int r_type
,
4526 Plt_stub_key
key(object
, gsym
, r_type
, addend
);
4527 Plt_stub_ent
ent(this->plt_size_
, this->plt_call_stubs_
.size());
4528 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4529 = this->plt_call_stubs_
.insert(std::make_pair(key
, ent
));
4532 this->plt_size_
= ent
.off_
+ this->plt_call_size(p
.first
);
4534 && this->targ_
->is_elfv2_localentry0(gsym
))
4536 p
.first
->second
.localentry0_
= 1;
4537 this->targ_
->set_has_localentry0();
4542 && !p
.first
->second
.localentry0_
)
4543 p
.first
->second
.r2save_
= 1;
4544 return this->can_reach_stub(from
, ent
.off_
, r_type
);
4547 template<int size
, bool big_endian
>
4549 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4551 const Sized_relobj_file
<size
, big_endian
>* object
,
4552 unsigned int locsym_index
,
4553 unsigned int r_type
,
4557 Plt_stub_key
key(object
, locsym_index
, r_type
, addend
);
4558 Plt_stub_ent
ent(this->plt_size_
, this->plt_call_stubs_
.size());
4559 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4560 = this->plt_call_stubs_
.insert(std::make_pair(key
, ent
));
4563 this->plt_size_
= ent
.off_
+ this->plt_call_size(p
.first
);
4565 && this->targ_
->is_elfv2_localentry0(object
, locsym_index
))
4567 p
.first
->second
.localentry0_
= 1;
4568 this->targ_
->set_has_localentry0();
4573 && !p
.first
->second
.localentry0_
)
4574 p
.first
->second
.r2save_
= 1;
4575 return this->can_reach_stub(from
, ent
.off_
, r_type
);
4578 // Find a plt call stub.
4580 template<int size
, bool big_endian
>
4581 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4582 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4583 const Sized_relobj_file
<size
, big_endian
>* object
,
4585 unsigned int r_type
,
4586 Address addend
) const
4588 Plt_stub_key
key(object
, gsym
, r_type
, addend
);
4589 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4590 if (p
== this->plt_call_stubs_
.end())
4595 template<int size
, bool big_endian
>
4596 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4597 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
4599 Plt_stub_key
key(gsym
);
4600 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4601 if (p
== this->plt_call_stubs_
.end())
4606 template<int size
, bool big_endian
>
4607 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4608 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4609 const Sized_relobj_file
<size
, big_endian
>* object
,
4610 unsigned int locsym_index
,
4611 unsigned int r_type
,
4612 Address addend
) const
4614 Plt_stub_key
key(object
, locsym_index
, r_type
, addend
);
4615 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4616 if (p
== this->plt_call_stubs_
.end())
4621 template<int size
, bool big_endian
>
4622 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4623 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4624 const Sized_relobj_file
<size
, big_endian
>* object
,
4625 unsigned int locsym_index
) const
4627 Plt_stub_key
key(object
, locsym_index
);
4628 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4629 if (p
== this->plt_call_stubs_
.end())
4634 // Add a long branch stub if we don't already have one to given
4637 template<int size
, bool big_endian
>
4639 Stub_table
<size
, big_endian
>::add_long_branch_entry(
4640 const Powerpc_relobj
<size
, big_endian
>* object
,
4641 unsigned int r_type
,
4646 Branch_stub_ent
ent(object
, to
, save_res
);
4647 Address off
= this->branch_size_
;
4648 std::pair
<typename
Branch_stub_entries::iterator
, bool> p
4649 = this->long_branch_stubs_
.insert(std::make_pair(ent
, off
));
4653 this->need_save_res_
= true;
4656 unsigned int stub_size
= this->branch_stub_size(p
.first
);
4657 this->branch_size_
= off
+ stub_size
;
4658 if (size
== 64 && stub_size
!= 4)
4659 this->targ_
->add_branch_lookup_table(to
);
4662 return this->can_reach_stub(from
, off
, r_type
);
4665 // Find long branch stub offset.
4667 template<int size
, bool big_endian
>
4668 typename Stub_table
<size
, big_endian
>::Address
4669 Stub_table
<size
, big_endian
>::find_long_branch_entry(
4670 const Powerpc_relobj
<size
, big_endian
>* object
,
4673 Branch_stub_ent
ent(object
, to
, false);
4674 typename
Branch_stub_entries::const_iterator p
4675 = this->long_branch_stubs_
.find(ent
);
4676 if (p
== this->long_branch_stubs_
.end())
4677 return invalid_address
;
4678 if (p
->first
.save_res_
)
4679 return to
- this->targ_
->savres_section()->address() + this->branch_size_
;
4683 // A class to handle .glink.
4685 template<int size
, bool big_endian
>
4686 class Output_data_glink
: public Output_section_data
4689 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4690 static const Address invalid_address
= static_cast<Address
>(0) - 1;
4691 static const int pltresolve_size
= 16*4;
4693 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
4694 : Output_section_data(16), targ_(targ
), global_entry_stubs_(),
4695 end_branch_table_(), ge_size_(0)
4699 add_eh_frame(Layout
* layout
);
4702 add_global_entry(const Symbol
*);
4705 find_global_entry(const Symbol
*) const;
4708 global_entry_address() const
4710 gold_assert(this->is_data_size_valid());
4711 unsigned int global_entry_off
= (this->end_branch_table_
+ 15) & -16;
4712 return this->address() + global_entry_off
;
4716 // Write to a map file.
4718 do_print_to_mapfile(Mapfile
* mapfile
) const
4719 { mapfile
->print_output_data(this, _("** glink")); }
4723 set_final_data_size();
4727 do_write(Output_file
*);
4729 // Allows access to .got and .plt for do_write.
4730 Target_powerpc
<size
, big_endian
>* targ_
;
4732 // Map sym to stub offset.
4733 typedef Unordered_map
<const Symbol
*, unsigned int> Global_entry_stub_entries
;
4734 Global_entry_stub_entries global_entry_stubs_
;
4736 unsigned int end_branch_table_
, ge_size_
;
4739 template<int size
, bool big_endian
>
4741 Output_data_glink
<size
, big_endian
>::add_eh_frame(Layout
* layout
)
4743 if (!parameters
->options().ld_generated_unwind_info())
4748 if (this->targ_
->abiversion() < 2)
4749 layout
->add_eh_frame_for_plt(this,
4750 Eh_cie
<64>::eh_frame_cie
,
4751 sizeof (Eh_cie
<64>::eh_frame_cie
),
4752 glink_eh_frame_fde_64v1
,
4753 sizeof (glink_eh_frame_fde_64v1
));
4755 layout
->add_eh_frame_for_plt(this,
4756 Eh_cie
<64>::eh_frame_cie
,
4757 sizeof (Eh_cie
<64>::eh_frame_cie
),
4758 glink_eh_frame_fde_64v2
,
4759 sizeof (glink_eh_frame_fde_64v2
));
4763 // 32-bit .glink can use the default since the CIE return
4764 // address reg, LR, is valid.
4765 layout
->add_eh_frame_for_plt(this,
4766 Eh_cie
<32>::eh_frame_cie
,
4767 sizeof (Eh_cie
<32>::eh_frame_cie
),
4769 sizeof (default_fde
));
4770 // Except where LR is used in a PIC __glink_PLTresolve.
4771 if (parameters
->options().output_is_position_independent())
4772 layout
->add_eh_frame_for_plt(this,
4773 Eh_cie
<32>::eh_frame_cie
,
4774 sizeof (Eh_cie
<32>::eh_frame_cie
),
4775 glink_eh_frame_fde_32
,
4776 sizeof (glink_eh_frame_fde_32
));
4780 template<int size
, bool big_endian
>
4782 Output_data_glink
<size
, big_endian
>::add_global_entry(const Symbol
* gsym
)
4784 std::pair
<typename
Global_entry_stub_entries::iterator
, bool> p
4785 = this->global_entry_stubs_
.insert(std::make_pair(gsym
, this->ge_size_
));
4787 this->ge_size_
+= 16;
4790 template<int size
, bool big_endian
>
4791 typename Output_data_glink
<size
, big_endian
>::Address
4792 Output_data_glink
<size
, big_endian
>::find_global_entry(const Symbol
* gsym
) const
4794 typename
Global_entry_stub_entries::const_iterator p
4795 = this->global_entry_stubs_
.find(gsym
);
4796 return p
== this->global_entry_stubs_
.end() ? invalid_address
: p
->second
;
4799 template<int size
, bool big_endian
>
4801 Output_data_glink
<size
, big_endian
>::set_final_data_size()
4803 unsigned int count
= this->targ_
->plt_entry_count();
4804 section_size_type total
= 0;
4810 // space for branch table
4811 total
+= 4 * (count
- 1);
4813 total
+= -total
& 15;
4814 total
+= this->pltresolve_size
;
4818 total
+= this->pltresolve_size
;
4820 // space for branch table
4822 if (this->targ_
->abiversion() < 2)
4826 total
+= 4 * (count
- 0x8000);
4830 this->end_branch_table_
= total
;
4831 total
= (total
+ 15) & -16;
4832 total
+= this->ge_size_
;
4834 this->set_data_size(total
);
4837 // Define symbols on stubs, identifying the stub.
4839 template<int size
, bool big_endian
>
4841 Stub_table
<size
, big_endian
>::define_stub_syms(Symbol_table
* symtab
)
4843 if (!this->plt_call_stubs_
.empty())
4845 // The key for the plt call stub hash table includes addresses,
4846 // therefore traversal order depends on those addresses, which
4847 // can change between runs if gold is a PIE. Unfortunately the
4848 // output .symtab ordering depends on the order in which symbols
4849 // are added to the linker symtab. We want reproducible output
4850 // so must sort the call stub symbols.
4851 typedef typename
Plt_stub_entries::const_iterator plt_iter
;
4852 std::vector
<plt_iter
> sorted
;
4853 sorted
.resize(this->plt_call_stubs_
.size());
4855 for (plt_iter cs
= this->plt_call_stubs_
.begin();
4856 cs
!= this->plt_call_stubs_
.end();
4858 sorted
[cs
->second
.indx_
] = cs
;
4860 for (unsigned int i
= 0; i
< this->plt_call_stubs_
.size(); ++i
)
4862 plt_iter cs
= sorted
[i
];
4865 if (cs
->first
.addend_
!= 0)
4866 sprintf(add
, "+%x", static_cast<uint32_t>(cs
->first
.addend_
));
4869 if (cs
->first
.object_
)
4871 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4872 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
4873 sprintf(obj
, "%x:", ppcobj
->uniq());
4876 const char *symname
;
4877 if (cs
->first
.sym_
== NULL
)
4879 sprintf(localname
, "%x", cs
->first
.locsym_
);
4880 symname
= localname
;
4883 symname
= cs
->first
.sym_
->name();
4884 char* name
= new char[8 + 10 + strlen(obj
) + strlen(symname
) + strlen(add
) + 1];
4885 sprintf(name
, "%08x.plt_call.%s%s%s", this->uniq_
, obj
, symname
, add
);
4887 = this->stub_address() - this->address() + cs
->second
.off_
;
4888 unsigned int stub_size
= this->plt_call_size(cs
);
4889 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
4893 typedef typename
Branch_stub_entries::const_iterator branch_iter
;
4894 for (branch_iter bs
= this->long_branch_stubs_
.begin();
4895 bs
!= this->long_branch_stubs_
.end();
4898 if (bs
->first
.save_res_
)
4901 char* name
= new char[8 + 13 + 16 + 1];
4902 sprintf(name
, "%08x.long_branch.%llx", this->uniq_
,
4903 static_cast<unsigned long long>(bs
->first
.dest_
));
4904 Address value
= (this->stub_address() - this->address()
4905 + this->plt_size_
+ bs
->second
);
4906 unsigned int stub_size
= this->branch_stub_size(bs
);
4907 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
4911 // Write out plt and long branch stub code.
4913 template<int size
, bool big_endian
>
4915 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
4917 if (this->plt_call_stubs_
.empty()
4918 && this->long_branch_stubs_
.empty())
4921 const section_size_type start_off
= this->offset();
4922 const section_size_type off
= this->stub_offset();
4923 const section_size_type oview_size
=
4924 convert_to_section_size_type(this->data_size() - (off
- start_off
));
4925 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4930 const Output_data_got_powerpc
<size
, big_endian
>* got
4931 = this->targ_
->got_section();
4932 Address got_os_addr
= got
->output_section()->address();
4934 if (!this->plt_call_stubs_
.empty())
4936 // The base address of the .plt section.
4937 Address plt_base
= this->targ_
->plt_section()->address();
4938 Address iplt_base
= invalid_address
;
4940 // Write out plt call stubs.
4941 typename
Plt_stub_entries::const_iterator cs
;
4942 for (cs
= this->plt_call_stubs_
.begin();
4943 cs
!= this->plt_call_stubs_
.end();
4947 Address pltoff
= this->plt_off(cs
, &is_iplt
);
4948 Address plt_addr
= pltoff
;
4951 if (iplt_base
== invalid_address
)
4952 iplt_base
= this->targ_
->iplt_section()->address();
4953 plt_addr
+= iplt_base
;
4956 plt_addr
+= plt_base
;
4957 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4958 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
4959 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
4960 Address off
= plt_addr
- got_addr
;
4962 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
4963 gold_error(_("%s: linkage table error against `%s'"),
4964 cs
->first
.object_
->name().c_str(),
4965 cs
->first
.sym_
->demangled_name().c_str());
4967 bool plt_load_toc
= this->targ_
->abiversion() < 2;
4969 = plt_load_toc
&& parameters
->options().plt_static_chain();
4971 = plt_load_toc
&& this->targ_
->plt_thread_safe();
4972 bool use_fake_dep
= false;
4973 Address cmp_branch_off
= 0;
4976 unsigned int pltindex
4977 = ((pltoff
- this->targ_
->first_plt_entry_offset())
4978 / this->targ_
->plt_entry_size());
4980 = (this->targ_
->glink_section()->pltresolve_size
4982 if (pltindex
> 32768)
4983 glinkoff
+= (pltindex
- 32768) * 4;
4985 = this->targ_
->glink_section()->address() + glinkoff
;
4987 = (this->stub_address() + cs
->second
.off_
+ 20
4988 + 4 * cs
->second
.r2save_
4989 + 4 * (ha(off
) != 0)
4990 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4991 + 4 * static_chain
);
4992 cmp_branch_off
= to
- from
;
4993 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
4996 p
= oview
+ cs
->second
.off_
;
4999 if (cs
->second
.r2save_
)
5001 write_insn
<big_endian
>(p
,
5002 std_2_1
+ this->targ_
->stk_toc());
5007 write_insn
<big_endian
>(p
, addis_11_2
+ ha(off
));
5009 write_insn
<big_endian
>(p
, ld_12_11
+ l(off
));
5014 write_insn
<big_endian
>(p
, addis_12_2
+ ha(off
));
5016 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
));
5020 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
5022 write_insn
<big_endian
>(p
, addi_11_11
+ l(off
));
5026 write_insn
<big_endian
>(p
, mtctr_12
);
5032 write_insn
<big_endian
>(p
, xor_2_12_12
);
5034 write_insn
<big_endian
>(p
, add_11_11_2
);
5037 write_insn
<big_endian
>(p
, ld_2_11
+ l(off
+ 8));
5041 write_insn
<big_endian
>(p
, ld_11_11
+ l(off
+ 16));
5048 if (cs
->second
.r2save_
)
5050 write_insn
<big_endian
>(p
,
5051 std_2_1
+ this->targ_
->stk_toc());
5054 write_insn
<big_endian
>(p
, ld_12_2
+ l(off
));
5057 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
5059 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
));
5063 write_insn
<big_endian
>(p
, mtctr_12
);
5069 write_insn
<big_endian
>(p
, xor_11_12_12
);
5071 write_insn
<big_endian
>(p
, add_2_2_11
);
5076 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16));
5079 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8));
5083 if (thread_safe
&& !use_fake_dep
)
5085 write_insn
<big_endian
>(p
, cmpldi_2_0
);
5087 write_insn
<big_endian
>(p
, bnectr_p4
);
5089 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
5092 write_insn
<big_endian
>(p
, bctr
);
5096 // Write out long branch stubs.
5097 typename
Branch_stub_entries::const_iterator bs
;
5098 for (bs
= this->long_branch_stubs_
.begin();
5099 bs
!= this->long_branch_stubs_
.end();
5102 if (bs
->first
.save_res_
)
5104 p
= oview
+ this->plt_size_
+ bs
->second
;
5105 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
5106 Address delta
= bs
->first
.dest_
- loc
;
5107 if (delta
+ (1 << 25) < 2 << 25)
5108 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
5112 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
5113 gold_assert(brlt_addr
!= invalid_address
);
5114 brlt_addr
+= this->targ_
->brlt_section()->address();
5115 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
5116 Address brltoff
= brlt_addr
- got_addr
;
5117 if (ha(brltoff
) == 0)
5119 write_insn
<big_endian
>(p
, ld_12_2
+ l(brltoff
)), p
+= 4;
5123 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
5124 write_insn
<big_endian
>(p
, ld_12_12
+ l(brltoff
)), p
+= 4;
5126 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5127 write_insn
<big_endian
>(p
, bctr
);
5133 if (!this->plt_call_stubs_
.empty())
5135 // The base address of the .plt section.
5136 Address plt_base
= this->targ_
->plt_section()->address();
5137 Address iplt_base
= invalid_address
;
5138 // The address of _GLOBAL_OFFSET_TABLE_.
5139 Address g_o_t
= invalid_address
;
5141 // Write out plt call stubs.
5142 typename
Plt_stub_entries::const_iterator cs
;
5143 for (cs
= this->plt_call_stubs_
.begin();
5144 cs
!= this->plt_call_stubs_
.end();
5148 Address plt_addr
= this->plt_off(cs
, &is_iplt
);
5151 if (iplt_base
== invalid_address
)
5152 iplt_base
= this->targ_
->iplt_section()->address();
5153 plt_addr
+= iplt_base
;
5156 plt_addr
+= plt_base
;
5158 p
= oview
+ cs
->second
.off_
;
5159 if (parameters
->options().output_is_position_independent())
5162 const Powerpc_relobj
<size
, big_endian
>* ppcobj
5163 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
5164 (cs
->first
.object_
));
5165 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
5167 unsigned int got2
= ppcobj
->got2_shndx();
5168 got_addr
= ppcobj
->get_output_section_offset(got2
);
5169 gold_assert(got_addr
!= invalid_address
);
5170 got_addr
+= (ppcobj
->output_section(got2
)->address()
5171 + cs
->first
.addend_
);
5175 if (g_o_t
== invalid_address
)
5177 const Output_data_got_powerpc
<size
, big_endian
>* got
5178 = this->targ_
->got_section();
5179 g_o_t
= got
->address() + got
->g_o_t();
5184 Address off
= plt_addr
- got_addr
;
5187 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(off
));
5188 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
5189 write_insn
<big_endian
>(p
+ 8, bctr
);
5193 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(off
));
5194 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(off
));
5195 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
5196 write_insn
<big_endian
>(p
+ 12, bctr
);
5201 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
5202 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
5203 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
5204 write_insn
<big_endian
>(p
+ 12, bctr
);
5209 // Write out long branch stubs.
5210 typename
Branch_stub_entries::const_iterator bs
;
5211 for (bs
= this->long_branch_stubs_
.begin();
5212 bs
!= this->long_branch_stubs_
.end();
5215 if (bs
->first
.save_res_
)
5217 p
= oview
+ this->plt_size_
+ bs
->second
;
5218 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
5219 Address delta
= bs
->first
.dest_
- loc
;
5220 if (delta
+ (1 << 25) < 2 << 25)
5221 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
5222 else if (!parameters
->options().output_is_position_independent())
5224 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(bs
->first
.dest_
));
5225 write_insn
<big_endian
>(p
+ 4, addi_12_12
+ l(bs
->first
.dest_
));
5226 write_insn
<big_endian
>(p
+ 8, mtctr_12
);
5227 write_insn
<big_endian
>(p
+ 12, bctr
);
5232 write_insn
<big_endian
>(p
+ 0, mflr_0
);
5233 write_insn
<big_endian
>(p
+ 4, bcl_20_31
);
5234 write_insn
<big_endian
>(p
+ 8, mflr_12
);
5235 write_insn
<big_endian
>(p
+ 12, addis_12_12
+ ha(delta
));
5236 write_insn
<big_endian
>(p
+ 16, addi_12_12
+ l(delta
));
5237 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
5238 write_insn
<big_endian
>(p
+ 24, mtctr_12
);
5239 write_insn
<big_endian
>(p
+ 28, bctr
);
5243 if (this->need_save_res_
)
5245 p
= oview
+ this->plt_size_
+ this->branch_size_
;
5246 memcpy (p
, this->targ_
->savres_section()->contents(),
5247 this->targ_
->savres_section()->data_size());
5251 // Write out .glink.
5253 template<int size
, bool big_endian
>
5255 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
5257 const section_size_type off
= this->offset();
5258 const section_size_type oview_size
=
5259 convert_to_section_size_type(this->data_size());
5260 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5263 // The base address of the .plt section.
5264 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
5265 Address plt_base
= this->targ_
->plt_section()->address();
5269 if (this->end_branch_table_
!= 0)
5271 // Write pltresolve stub.
5273 Address after_bcl
= this->address() + 16;
5274 Address pltoff
= plt_base
- after_bcl
;
5276 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
5278 if (this->targ_
->abiversion() < 2)
5280 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
5281 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5282 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5283 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5284 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
5285 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5286 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5287 write_insn
<big_endian
>(p
, ld_2_11
+ 8), p
+= 4;
5288 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5289 write_insn
<big_endian
>(p
, ld_11_11
+ 16), p
+= 4;
5293 write_insn
<big_endian
>(p
, mflr_0
), p
+= 4;
5294 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5295 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5296 write_insn
<big_endian
>(p
, std_2_1
+ 24), p
+= 4;
5297 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5298 write_insn
<big_endian
>(p
, mtlr_0
), p
+= 4;
5299 write_insn
<big_endian
>(p
, sub_12_12_11
), p
+= 4;
5300 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5301 write_insn
<big_endian
>(p
, addi_0_12
+ l(-48)), p
+= 4;
5302 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5303 write_insn
<big_endian
>(p
, srdi_0_0_2
), p
+= 4;
5304 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5305 write_insn
<big_endian
>(p
, ld_11_11
+ 8), p
+= 4;
5307 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
5308 while (p
< oview
+ this->pltresolve_size
)
5309 write_insn
<big_endian
>(p
, nop
), p
+= 4;
5311 // Write lazy link call stubs.
5313 while (p
< oview
+ this->end_branch_table_
)
5315 if (this->targ_
->abiversion() < 2)
5319 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
5323 write_insn
<big_endian
>(p
, lis_0
+ hi(indx
)), p
+= 4;
5324 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
5327 uint32_t branch_off
= 8 - (p
- oview
);
5328 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
5333 Address plt_base
= this->targ_
->plt_section()->address();
5334 Address iplt_base
= invalid_address
;
5335 unsigned int global_entry_off
= (this->end_branch_table_
+ 15) & -16;
5336 Address global_entry_base
= this->address() + global_entry_off
;
5337 typename
Global_entry_stub_entries::const_iterator ge
;
5338 for (ge
= this->global_entry_stubs_
.begin();
5339 ge
!= this->global_entry_stubs_
.end();
5342 p
= oview
+ global_entry_off
+ ge
->second
;
5343 Address plt_addr
= ge
->first
->plt_offset();
5344 if (ge
->first
->type() == elfcpp::STT_GNU_IFUNC
5345 && ge
->first
->can_use_relative_reloc(false))
5347 if (iplt_base
== invalid_address
)
5348 iplt_base
= this->targ_
->iplt_section()->address();
5349 plt_addr
+= iplt_base
;
5352 plt_addr
+= plt_base
;
5353 Address my_addr
= global_entry_base
+ ge
->second
;
5354 Address off
= plt_addr
- my_addr
;
5356 if (off
+ 0x80008000 > 0xffffffff || (off
& 3) != 0)
5357 gold_error(_("%s: linkage table error against `%s'"),
5358 ge
->first
->object()->name().c_str(),
5359 ge
->first
->demangled_name().c_str());
5361 write_insn
<big_endian
>(p
, addis_12_12
+ ha(off
)), p
+= 4;
5362 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
)), p
+= 4;
5363 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5364 write_insn
<big_endian
>(p
, bctr
);
5369 const Output_data_got_powerpc
<size
, big_endian
>* got
5370 = this->targ_
->got_section();
5371 // The address of _GLOBAL_OFFSET_TABLE_.
5372 Address g_o_t
= got
->address() + got
->g_o_t();
5374 // Write out pltresolve branch table.
5376 unsigned int the_end
= oview_size
- this->pltresolve_size
;
5377 unsigned char* end_p
= oview
+ the_end
;
5378 while (p
< end_p
- 8 * 4)
5379 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
5381 write_insn
<big_endian
>(p
, nop
), p
+= 4;
5383 // Write out pltresolve call stub.
5384 if (parameters
->options().output_is_position_independent())
5386 Address res0_off
= 0;
5387 Address after_bcl_off
= the_end
+ 12;
5388 Address bcl_res0
= after_bcl_off
- res0_off
;
5390 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
5391 write_insn
<big_endian
>(p
+ 4, mflr_0
);
5392 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
5393 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
5394 write_insn
<big_endian
>(p
+ 16, mflr_12
);
5395 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
5396 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
5398 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
5400 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
5401 if (ha(got_bcl
) == ha(got_bcl
+ 4))
5403 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
5404 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
5408 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
5409 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
5411 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
5412 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
5413 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
5414 write_insn
<big_endian
>(p
+ 52, bctr
);
5415 write_insn
<big_endian
>(p
+ 56, nop
);
5416 write_insn
<big_endian
>(p
+ 60, nop
);
5420 Address res0
= this->address();
5422 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
5423 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
5424 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5425 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
5427 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
5428 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
5429 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
5430 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
5431 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5432 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
5434 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
5435 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
5436 write_insn
<big_endian
>(p
+ 32, bctr
);
5437 write_insn
<big_endian
>(p
+ 36, nop
);
5438 write_insn
<big_endian
>(p
+ 40, nop
);
5439 write_insn
<big_endian
>(p
+ 44, nop
);
5440 write_insn
<big_endian
>(p
+ 48, nop
);
5441 write_insn
<big_endian
>(p
+ 52, nop
);
5442 write_insn
<big_endian
>(p
+ 56, nop
);
5443 write_insn
<big_endian
>(p
+ 60, nop
);
5448 of
->write_output_view(off
, oview_size
, oview
);
5452 // A class to handle linker generated save/restore functions.
5454 template<int size
, bool big_endian
>
5455 class Output_data_save_res
: public Output_section_data_build
5458 Output_data_save_res(Symbol_table
* symtab
);
5460 const unsigned char*
5467 // Write to a map file.
5469 do_print_to_mapfile(Mapfile
* mapfile
) const
5470 { mapfile
->print_output_data(this, _("** save/restore")); }
5473 do_write(Output_file
*);
5476 // The maximum size of save/restore contents.
5477 static const unsigned int savres_max
= 218*4;
5480 savres_define(Symbol_table
* symtab
,
5482 unsigned int lo
, unsigned int hi
,
5483 unsigned char* write_ent(unsigned char*, int),
5484 unsigned char* write_tail(unsigned char*, int));
5486 unsigned char *contents_
;
5489 template<bool big_endian
>
5490 static unsigned char*
5491 savegpr0(unsigned char* p
, int r
)
5493 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5494 write_insn
<big_endian
>(p
, insn
);
5498 template<bool big_endian
>
5499 static unsigned char*
5500 savegpr0_tail(unsigned char* p
, int r
)
5502 p
= savegpr0
<big_endian
>(p
, r
);
5503 uint32_t insn
= std_0_1
+ 16;
5504 write_insn
<big_endian
>(p
, insn
);
5506 write_insn
<big_endian
>(p
, blr
);
5510 template<bool big_endian
>
5511 static unsigned char*
5512 restgpr0(unsigned char* p
, int r
)
5514 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5515 write_insn
<big_endian
>(p
, insn
);
5519 template<bool big_endian
>
5520 static unsigned char*
5521 restgpr0_tail(unsigned char* p
, int r
)
5523 uint32_t insn
= ld_0_1
+ 16;
5524 write_insn
<big_endian
>(p
, insn
);
5526 p
= restgpr0
<big_endian
>(p
, r
);
5527 write_insn
<big_endian
>(p
, mtlr_0
);
5531 p
= restgpr0
<big_endian
>(p
, 30);
5532 p
= restgpr0
<big_endian
>(p
, 31);
5534 write_insn
<big_endian
>(p
, blr
);
5538 template<bool big_endian
>
5539 static unsigned char*
5540 savegpr1(unsigned char* p
, int r
)
5542 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5543 write_insn
<big_endian
>(p
, insn
);
5547 template<bool big_endian
>
5548 static unsigned char*
5549 savegpr1_tail(unsigned char* p
, int r
)
5551 p
= savegpr1
<big_endian
>(p
, r
);
5552 write_insn
<big_endian
>(p
, blr
);
5556 template<bool big_endian
>
5557 static unsigned char*
5558 restgpr1(unsigned char* p
, int r
)
5560 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5561 write_insn
<big_endian
>(p
, insn
);
5565 template<bool big_endian
>
5566 static unsigned char*
5567 restgpr1_tail(unsigned char* p
, int r
)
5569 p
= restgpr1
<big_endian
>(p
, r
);
5570 write_insn
<big_endian
>(p
, blr
);
5574 template<bool big_endian
>
5575 static unsigned char*
5576 savefpr(unsigned char* p
, int r
)
5578 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5579 write_insn
<big_endian
>(p
, insn
);
5583 template<bool big_endian
>
5584 static unsigned char*
5585 savefpr0_tail(unsigned char* p
, int r
)
5587 p
= savefpr
<big_endian
>(p
, r
);
5588 write_insn
<big_endian
>(p
, std_0_1
+ 16);
5590 write_insn
<big_endian
>(p
, blr
);
5594 template<bool big_endian
>
5595 static unsigned char*
5596 restfpr(unsigned char* p
, int r
)
5598 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5599 write_insn
<big_endian
>(p
, insn
);
5603 template<bool big_endian
>
5604 static unsigned char*
5605 restfpr0_tail(unsigned char* p
, int r
)
5607 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
5609 p
= restfpr
<big_endian
>(p
, r
);
5610 write_insn
<big_endian
>(p
, mtlr_0
);
5614 p
= restfpr
<big_endian
>(p
, 30);
5615 p
= restfpr
<big_endian
>(p
, 31);
5617 write_insn
<big_endian
>(p
, blr
);
5621 template<bool big_endian
>
5622 static unsigned char*
5623 savefpr1_tail(unsigned char* p
, int r
)
5625 p
= savefpr
<big_endian
>(p
, r
);
5626 write_insn
<big_endian
>(p
, blr
);
5630 template<bool big_endian
>
5631 static unsigned char*
5632 restfpr1_tail(unsigned char* p
, int r
)
5634 p
= restfpr
<big_endian
>(p
, r
);
5635 write_insn
<big_endian
>(p
, blr
);
5639 template<bool big_endian
>
5640 static unsigned char*
5641 savevr(unsigned char* p
, int r
)
5643 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5644 write_insn
<big_endian
>(p
, insn
);
5646 insn
= stvx_0_12_0
+ (r
<< 21);
5647 write_insn
<big_endian
>(p
, insn
);
5651 template<bool big_endian
>
5652 static unsigned char*
5653 savevr_tail(unsigned char* p
, int r
)
5655 p
= savevr
<big_endian
>(p
, r
);
5656 write_insn
<big_endian
>(p
, blr
);
5660 template<bool big_endian
>
5661 static unsigned char*
5662 restvr(unsigned char* p
, int r
)
5664 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5665 write_insn
<big_endian
>(p
, insn
);
5667 insn
= lvx_0_12_0
+ (r
<< 21);
5668 write_insn
<big_endian
>(p
, insn
);
5672 template<bool big_endian
>
5673 static unsigned char*
5674 restvr_tail(unsigned char* p
, int r
)
5676 p
= restvr
<big_endian
>(p
, r
);
5677 write_insn
<big_endian
>(p
, blr
);
5682 template<int size
, bool big_endian
>
5683 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
5684 Symbol_table
* symtab
)
5685 : Output_section_data_build(4),
5688 this->savres_define(symtab
,
5689 "_savegpr0_", 14, 31,
5690 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
5691 this->savres_define(symtab
,
5692 "_restgpr0_", 14, 29,
5693 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
5694 this->savres_define(symtab
,
5695 "_restgpr0_", 30, 31,
5696 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
5697 this->savres_define(symtab
,
5698 "_savegpr1_", 14, 31,
5699 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
5700 this->savres_define(symtab
,
5701 "_restgpr1_", 14, 31,
5702 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
5703 this->savres_define(symtab
,
5704 "_savefpr_", 14, 31,
5705 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
5706 this->savres_define(symtab
,
5707 "_restfpr_", 14, 29,
5708 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
5709 this->savres_define(symtab
,
5710 "_restfpr_", 30, 31,
5711 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
5712 this->savres_define(symtab
,
5714 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
5715 this->savres_define(symtab
,
5717 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
5718 this->savres_define(symtab
,
5720 savevr
<big_endian
>, savevr_tail
<big_endian
>);
5721 this->savres_define(symtab
,
5723 restvr
<big_endian
>, restvr_tail
<big_endian
>);
5726 template<int size
, bool big_endian
>
5728 Output_data_save_res
<size
, big_endian
>::savres_define(
5729 Symbol_table
* symtab
,
5731 unsigned int lo
, unsigned int hi
,
5732 unsigned char* write_ent(unsigned char*, int),
5733 unsigned char* write_tail(unsigned char*, int))
5735 size_t len
= strlen(name
);
5736 bool writing
= false;
5739 memcpy(sym
, name
, len
);
5742 for (unsigned int i
= lo
; i
<= hi
; i
++)
5744 sym
[len
+ 0] = i
/ 10 + '0';
5745 sym
[len
+ 1] = i
% 10 + '0';
5746 Symbol
* gsym
= symtab
->lookup(sym
);
5747 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
5748 writing
= writing
|| refd
;
5751 if (this->contents_
== NULL
)
5752 this->contents_
= new unsigned char[this->savres_max
];
5754 section_size_type value
= this->current_data_size();
5755 unsigned char* p
= this->contents_
+ value
;
5757 p
= write_ent(p
, i
);
5759 p
= write_tail(p
, i
);
5760 section_size_type cur_size
= p
- this->contents_
;
5761 this->set_current_data_size(cur_size
);
5763 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
5764 this, value
, cur_size
- value
,
5765 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
5766 elfcpp::STV_HIDDEN
, 0, false, false);
5771 // Write out save/restore.
5773 template<int size
, bool big_endian
>
5775 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
5777 const section_size_type off
= this->offset();
5778 const section_size_type oview_size
=
5779 convert_to_section_size_type(this->data_size());
5780 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5781 memcpy(oview
, this->contents_
, oview_size
);
5782 of
->write_output_view(off
, oview_size
, oview
);
5786 // Create the glink section.
5788 template<int size
, bool big_endian
>
5790 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
5792 if (this->glink_
== NULL
)
5794 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
5795 this->glink_
->add_eh_frame(layout
);
5796 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
5797 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
5798 this->glink_
, ORDER_TEXT
, false);
5802 // Create a PLT entry for a global symbol.
5804 template<int size
, bool big_endian
>
5806 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
5810 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
5811 && gsym
->can_use_relative_reloc(false))
5813 if (this->iplt_
== NULL
)
5814 this->make_iplt_section(symtab
, layout
);
5815 this->iplt_
->add_ifunc_entry(gsym
);
5819 if (this->plt_
== NULL
)
5820 this->make_plt_section(symtab
, layout
);
5821 this->plt_
->add_entry(gsym
);
5825 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5827 template<int size
, bool big_endian
>
5829 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
5830 Symbol_table
* symtab
,
5832 Sized_relobj_file
<size
, big_endian
>* relobj
,
5835 if (this->iplt_
== NULL
)
5836 this->make_iplt_section(symtab
, layout
);
5837 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
5840 // Return the number of entries in the PLT.
5842 template<int size
, bool big_endian
>
5844 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
5846 if (this->plt_
== NULL
)
5848 return this->plt_
->entry_count();
5851 // Create a GOT entry for local dynamic __tls_get_addr calls.
5853 template<int size
, bool big_endian
>
5855 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
5856 Symbol_table
* symtab
,
5858 Sized_relobj_file
<size
, big_endian
>* object
)
5860 if (this->tlsld_got_offset_
== -1U)
5862 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
5863 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
5864 Output_data_got_powerpc
<size
, big_endian
>* got
5865 = this->got_section(symtab
, layout
);
5866 unsigned int got_offset
= got
->add_constant_pair(0, 0);
5867 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
5869 this->tlsld_got_offset_
= got_offset
;
5871 return this->tlsld_got_offset_
;
5874 // Get the Reference_flags for a particular relocation.
5876 template<int size
, bool big_endian
>
5878 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(
5879 unsigned int r_type
,
5880 const Target_powerpc
* target
)
5886 case elfcpp::R_POWERPC_NONE
:
5887 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5888 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5889 case elfcpp::R_PPC64_TOC
:
5890 // No symbol reference.
5893 case elfcpp::R_PPC64_ADDR64
:
5894 case elfcpp::R_PPC64_UADDR64
:
5895 case elfcpp::R_POWERPC_ADDR32
:
5896 case elfcpp::R_POWERPC_UADDR32
:
5897 case elfcpp::R_POWERPC_ADDR16
:
5898 case elfcpp::R_POWERPC_UADDR16
:
5899 case elfcpp::R_POWERPC_ADDR16_LO
:
5900 case elfcpp::R_POWERPC_ADDR16_HI
:
5901 case elfcpp::R_POWERPC_ADDR16_HA
:
5902 ref
= Symbol::ABSOLUTE_REF
;
5905 case elfcpp::R_POWERPC_ADDR24
:
5906 case elfcpp::R_POWERPC_ADDR14
:
5907 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5908 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5909 ref
= Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
5912 case elfcpp::R_PPC64_REL64
:
5913 case elfcpp::R_POWERPC_REL32
:
5914 case elfcpp::R_PPC_LOCAL24PC
:
5915 case elfcpp::R_POWERPC_REL16
:
5916 case elfcpp::R_POWERPC_REL16_LO
:
5917 case elfcpp::R_POWERPC_REL16_HI
:
5918 case elfcpp::R_POWERPC_REL16_HA
:
5919 ref
= Symbol::RELATIVE_REF
;
5922 case elfcpp::R_POWERPC_REL24
:
5923 case elfcpp::R_PPC_PLTREL24
:
5924 case elfcpp::R_POWERPC_REL14
:
5925 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5926 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5927 ref
= Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
5930 case elfcpp::R_POWERPC_GOT16
:
5931 case elfcpp::R_POWERPC_GOT16_LO
:
5932 case elfcpp::R_POWERPC_GOT16_HI
:
5933 case elfcpp::R_POWERPC_GOT16_HA
:
5934 case elfcpp::R_PPC64_GOT16_DS
:
5935 case elfcpp::R_PPC64_GOT16_LO_DS
:
5936 case elfcpp::R_PPC64_TOC16
:
5937 case elfcpp::R_PPC64_TOC16_LO
:
5938 case elfcpp::R_PPC64_TOC16_HI
:
5939 case elfcpp::R_PPC64_TOC16_HA
:
5940 case elfcpp::R_PPC64_TOC16_DS
:
5941 case elfcpp::R_PPC64_TOC16_LO_DS
:
5942 ref
= Symbol::RELATIVE_REF
;
5945 case elfcpp::R_POWERPC_GOT_TPREL16
:
5946 case elfcpp::R_POWERPC_TLS
:
5947 ref
= Symbol::TLS_REF
;
5950 case elfcpp::R_POWERPC_COPY
:
5951 case elfcpp::R_POWERPC_GLOB_DAT
:
5952 case elfcpp::R_POWERPC_JMP_SLOT
:
5953 case elfcpp::R_POWERPC_RELATIVE
:
5954 case elfcpp::R_POWERPC_DTPMOD
:
5956 // Not expected. We will give an error later.
5960 if (size
== 64 && target
->abiversion() < 2)
5961 ref
|= Symbol::FUNC_DESC_ABI
;
5965 // Report an unsupported relocation against a local symbol.
5967 template<int size
, bool big_endian
>
5969 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
5970 Sized_relobj_file
<size
, big_endian
>* object
,
5971 unsigned int r_type
)
5973 gold_error(_("%s: unsupported reloc %u against local symbol"),
5974 object
->name().c_str(), r_type
);
5977 // We are about to emit a dynamic relocation of type R_TYPE. If the
5978 // dynamic linker does not support it, issue an error.
5980 template<int size
, bool big_endian
>
5982 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
5983 unsigned int r_type
)
5985 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
5987 // These are the relocation types supported by glibc for both 32-bit
5988 // and 64-bit powerpc.
5991 case elfcpp::R_POWERPC_NONE
:
5992 case elfcpp::R_POWERPC_RELATIVE
:
5993 case elfcpp::R_POWERPC_GLOB_DAT
:
5994 case elfcpp::R_POWERPC_DTPMOD
:
5995 case elfcpp::R_POWERPC_DTPREL
:
5996 case elfcpp::R_POWERPC_TPREL
:
5997 case elfcpp::R_POWERPC_JMP_SLOT
:
5998 case elfcpp::R_POWERPC_COPY
:
5999 case elfcpp::R_POWERPC_IRELATIVE
:
6000 case elfcpp::R_POWERPC_ADDR32
:
6001 case elfcpp::R_POWERPC_UADDR32
:
6002 case elfcpp::R_POWERPC_ADDR24
:
6003 case elfcpp::R_POWERPC_ADDR16
:
6004 case elfcpp::R_POWERPC_UADDR16
:
6005 case elfcpp::R_POWERPC_ADDR16_LO
:
6006 case elfcpp::R_POWERPC_ADDR16_HI
:
6007 case elfcpp::R_POWERPC_ADDR16_HA
:
6008 case elfcpp::R_POWERPC_ADDR14
:
6009 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6010 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6011 case elfcpp::R_POWERPC_REL32
:
6012 case elfcpp::R_POWERPC_REL24
:
6013 case elfcpp::R_POWERPC_TPREL16
:
6014 case elfcpp::R_POWERPC_TPREL16_LO
:
6015 case elfcpp::R_POWERPC_TPREL16_HI
:
6016 case elfcpp::R_POWERPC_TPREL16_HA
:
6027 // These are the relocation types supported only on 64-bit.
6028 case elfcpp::R_PPC64_ADDR64
:
6029 case elfcpp::R_PPC64_UADDR64
:
6030 case elfcpp::R_PPC64_JMP_IREL
:
6031 case elfcpp::R_PPC64_ADDR16_DS
:
6032 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6033 case elfcpp::R_PPC64_ADDR16_HIGH
:
6034 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6035 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6036 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6037 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6038 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6039 case elfcpp::R_PPC64_REL64
:
6040 case elfcpp::R_POWERPC_ADDR30
:
6041 case elfcpp::R_PPC64_TPREL16_DS
:
6042 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6043 case elfcpp::R_PPC64_TPREL16_HIGH
:
6044 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6045 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6046 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6047 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6048 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6059 // These are the relocation types supported only on 32-bit.
6060 // ??? glibc ld.so doesn't need to support these.
6061 case elfcpp::R_POWERPC_DTPREL16
:
6062 case elfcpp::R_POWERPC_DTPREL16_LO
:
6063 case elfcpp::R_POWERPC_DTPREL16_HI
:
6064 case elfcpp::R_POWERPC_DTPREL16_HA
:
6072 // This prevents us from issuing more than one error per reloc
6073 // section. But we can still wind up issuing more than one
6074 // error per object file.
6075 if (this->issued_non_pic_error_
)
6077 gold_assert(parameters
->options().output_is_position_independent());
6078 object
->error(_("requires unsupported dynamic reloc; "
6079 "recompile with -fPIC"));
6080 this->issued_non_pic_error_
= true;
6084 // Return whether we need to make a PLT entry for a relocation of the
6085 // given type against a STT_GNU_IFUNC symbol.
6087 template<int size
, bool big_endian
>
6089 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
6090 Target_powerpc
<size
, big_endian
>* target
,
6091 Sized_relobj_file
<size
, big_endian
>* object
,
6092 unsigned int r_type
,
6095 // In non-pic code any reference will resolve to the plt call stub
6096 // for the ifunc symbol.
6097 if ((size
== 32 || target
->abiversion() >= 2)
6098 && !parameters
->options().output_is_position_independent())
6103 // Word size refs from data sections are OK, but don't need a PLT entry.
6104 case elfcpp::R_POWERPC_ADDR32
:
6105 case elfcpp::R_POWERPC_UADDR32
:
6110 case elfcpp::R_PPC64_ADDR64
:
6111 case elfcpp::R_PPC64_UADDR64
:
6116 // GOT refs are good, but also don't need a PLT entry.
6117 case elfcpp::R_POWERPC_GOT16
:
6118 case elfcpp::R_POWERPC_GOT16_LO
:
6119 case elfcpp::R_POWERPC_GOT16_HI
:
6120 case elfcpp::R_POWERPC_GOT16_HA
:
6121 case elfcpp::R_PPC64_GOT16_DS
:
6122 case elfcpp::R_PPC64_GOT16_LO_DS
:
6125 // Function calls are good, and these do need a PLT entry.
6126 case elfcpp::R_POWERPC_ADDR24
:
6127 case elfcpp::R_POWERPC_ADDR14
:
6128 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6129 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6130 case elfcpp::R_POWERPC_REL24
:
6131 case elfcpp::R_PPC_PLTREL24
:
6132 case elfcpp::R_POWERPC_REL14
:
6133 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6134 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6141 // Anything else is a problem.
6142 // If we are building a static executable, the libc startup function
6143 // responsible for applying indirect function relocations is going
6144 // to complain about the reloc type.
6145 // If we are building a dynamic executable, we will have a text
6146 // relocation. The dynamic loader will set the text segment
6147 // writable and non-executable to apply text relocations. So we'll
6148 // segfault when trying to run the indirection function to resolve
6151 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
6152 object
->name().c_str(), r_type
);
6156 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6160 ok_lo_toc_insn(uint32_t insn
, unsigned int r_type
)
6162 return ((insn
& (0x3f << 26)) == 12u << 26 /* addic */
6163 || (insn
& (0x3f << 26)) == 14u << 26 /* addi */
6164 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
6165 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
6166 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
6167 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
6168 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
6169 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
6170 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
6171 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
6172 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
6173 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
6174 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
6175 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
6176 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
6177 || (insn
& (0x3f << 26)) == 56u << 26 /* lq,lfq */
6178 || ((insn
& (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
6179 /* Exclude lfqu by testing reloc. If relocs are ever
6180 defined for the reduced D field in psq_lu then those
6181 will need testing too. */
6182 && r_type
!= elfcpp::R_PPC64_TOC16_LO
6183 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
6184 || ((insn
& (0x3f << 26)) == 58u << 26 /* ld,lwa */
6186 || (insn
& (0x3f << 26)) == 60u << 26 /* stfq */
6187 || ((insn
& (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
6188 /* Exclude stfqu. psq_stu as above for psq_lu. */
6189 && r_type
!= elfcpp::R_PPC64_TOC16_LO
6190 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
6191 || ((insn
& (0x3f << 26)) == 62u << 26 /* std,stq */
6192 && (insn
& 1) == 0));
6195 // Scan a relocation for a local symbol.
6197 template<int size
, bool big_endian
>
6199 Target_powerpc
<size
, big_endian
>::Scan::local(
6200 Symbol_table
* symtab
,
6202 Target_powerpc
<size
, big_endian
>* target
,
6203 Sized_relobj_file
<size
, big_endian
>* object
,
6204 unsigned int data_shndx
,
6205 Output_section
* output_section
,
6206 const elfcpp::Rela
<size
, big_endian
>& reloc
,
6207 unsigned int r_type
,
6208 const elfcpp::Sym
<size
, big_endian
>& lsym
,
6211 this->maybe_skip_tls_get_addr_call(r_type
, NULL
);
6213 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6214 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6216 this->expect_tls_get_addr_call();
6217 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
6218 if (tls_type
!= tls::TLSOPT_NONE
)
6219 this->skip_next_tls_get_addr_call();
6221 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6222 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6224 this->expect_tls_get_addr_call();
6225 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6226 if (tls_type
!= tls::TLSOPT_NONE
)
6227 this->skip_next_tls_get_addr_call();
6230 Powerpc_relobj
<size
, big_endian
>* ppc_object
6231 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6236 && data_shndx
== ppc_object
->opd_shndx()
6237 && r_type
== elfcpp::R_PPC64_ADDR64
)
6238 ppc_object
->set_opd_discard(reloc
.get_r_offset());
6242 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6243 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
6244 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
6246 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6247 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6248 r_type
, r_sym
, reloc
.get_r_addend());
6249 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
6254 case elfcpp::R_POWERPC_NONE
:
6255 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6256 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6257 case elfcpp::R_POWERPC_TLS
:
6258 case elfcpp::R_PPC64_ENTRY
:
6261 case elfcpp::R_PPC64_TOC
:
6263 Output_data_got_powerpc
<size
, big_endian
>* got
6264 = target
->got_section(symtab
, layout
);
6265 if (parameters
->options().output_is_position_independent())
6267 Address off
= reloc
.get_r_offset();
6269 && target
->abiversion() < 2
6270 && data_shndx
== ppc_object
->opd_shndx()
6271 && ppc_object
->get_opd_discard(off
- 8))
6274 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6275 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
6276 rela_dyn
->add_output_section_relative(got
->output_section(),
6277 elfcpp::R_POWERPC_RELATIVE
,
6279 object
, data_shndx
, off
,
6280 symobj
->toc_base_offset());
6285 case elfcpp::R_PPC64_ADDR64
:
6286 case elfcpp::R_PPC64_UADDR64
:
6287 case elfcpp::R_POWERPC_ADDR32
:
6288 case elfcpp::R_POWERPC_UADDR32
:
6289 case elfcpp::R_POWERPC_ADDR24
:
6290 case elfcpp::R_POWERPC_ADDR16
:
6291 case elfcpp::R_POWERPC_ADDR16_LO
:
6292 case elfcpp::R_POWERPC_ADDR16_HI
:
6293 case elfcpp::R_POWERPC_ADDR16_HA
:
6294 case elfcpp::R_POWERPC_UADDR16
:
6295 case elfcpp::R_PPC64_ADDR16_HIGH
:
6296 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6297 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6298 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6299 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6300 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6301 case elfcpp::R_PPC64_ADDR16_DS
:
6302 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6303 case elfcpp::R_POWERPC_ADDR14
:
6304 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6305 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6306 // If building a shared library (or a position-independent
6307 // executable), we need to create a dynamic relocation for
6309 if (parameters
->options().output_is_position_independent()
6310 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
6312 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
6314 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6315 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
6316 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
6318 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6319 : elfcpp::R_POWERPC_RELATIVE
);
6320 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
6321 output_section
, data_shndx
,
6322 reloc
.get_r_offset(),
6323 reloc
.get_r_addend(), false);
6325 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
6327 check_non_pic(object
, r_type
);
6328 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
6329 data_shndx
, reloc
.get_r_offset(),
6330 reloc
.get_r_addend());
6334 gold_assert(lsym
.get_st_value() == 0);
6335 unsigned int shndx
= lsym
.get_st_shndx();
6337 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
6340 object
->error(_("section symbol %u has bad shndx %u"),
6343 rela_dyn
->add_local_section(object
, shndx
, r_type
,
6344 output_section
, data_shndx
,
6345 reloc
.get_r_offset());
6350 case elfcpp::R_POWERPC_REL24
:
6351 case elfcpp::R_PPC_PLTREL24
:
6352 case elfcpp::R_PPC_LOCAL24PC
:
6353 case elfcpp::R_POWERPC_REL14
:
6354 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6355 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6358 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6359 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6360 r_type
, r_sym
, reloc
.get_r_addend());
6364 case elfcpp::R_PPC64_TOCSAVE
:
6365 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6366 // caller has already saved r2 and thus a plt call stub need not
6369 && target
->mark_pltcall(ppc_object
, data_shndx
,
6370 reloc
.get_r_offset() - 4, symtab
))
6372 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6373 unsigned int shndx
= lsym
.get_st_shndx();
6375 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6377 object
->error(_("tocsave symbol %u has bad shndx %u"),
6380 target
->add_tocsave(ppc_object
, shndx
,
6381 lsym
.get_st_value() + reloc
.get_r_addend());
6385 case elfcpp::R_PPC64_REL64
:
6386 case elfcpp::R_POWERPC_REL32
:
6387 case elfcpp::R_POWERPC_REL16
:
6388 case elfcpp::R_POWERPC_REL16_LO
:
6389 case elfcpp::R_POWERPC_REL16_HI
:
6390 case elfcpp::R_POWERPC_REL16_HA
:
6391 case elfcpp::R_POWERPC_REL16DX_HA
:
6392 case elfcpp::R_POWERPC_SECTOFF
:
6393 case elfcpp::R_POWERPC_SECTOFF_LO
:
6394 case elfcpp::R_POWERPC_SECTOFF_HI
:
6395 case elfcpp::R_POWERPC_SECTOFF_HA
:
6396 case elfcpp::R_PPC64_SECTOFF_DS
:
6397 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6398 case elfcpp::R_POWERPC_TPREL16
:
6399 case elfcpp::R_POWERPC_TPREL16_LO
:
6400 case elfcpp::R_POWERPC_TPREL16_HI
:
6401 case elfcpp::R_POWERPC_TPREL16_HA
:
6402 case elfcpp::R_PPC64_TPREL16_DS
:
6403 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6404 case elfcpp::R_PPC64_TPREL16_HIGH
:
6405 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6406 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6407 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6408 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6409 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6410 case elfcpp::R_POWERPC_DTPREL16
:
6411 case elfcpp::R_POWERPC_DTPREL16_LO
:
6412 case elfcpp::R_POWERPC_DTPREL16_HI
:
6413 case elfcpp::R_POWERPC_DTPREL16_HA
:
6414 case elfcpp::R_PPC64_DTPREL16_DS
:
6415 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6416 case elfcpp::R_PPC64_DTPREL16_HIGH
:
6417 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
6418 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6419 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6420 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6421 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6422 case elfcpp::R_PPC64_TLSGD
:
6423 case elfcpp::R_PPC64_TLSLD
:
6424 case elfcpp::R_PPC64_ADDR64_LOCAL
:
6427 case elfcpp::R_POWERPC_GOT16
:
6428 case elfcpp::R_POWERPC_GOT16_LO
:
6429 case elfcpp::R_POWERPC_GOT16_HI
:
6430 case elfcpp::R_POWERPC_GOT16_HA
:
6431 case elfcpp::R_PPC64_GOT16_DS
:
6432 case elfcpp::R_PPC64_GOT16_LO_DS
:
6434 // The symbol requires a GOT entry.
6435 Output_data_got_powerpc
<size
, big_endian
>* got
6436 = target
->got_section(symtab
, layout
);
6437 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6439 if (!parameters
->options().output_is_position_independent())
6442 && (size
== 32 || target
->abiversion() >= 2))
6443 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
6445 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
6447 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
6449 // If we are generating a shared object or a pie, this
6450 // symbol's GOT entry will be set by a dynamic relocation.
6452 off
= got
->add_constant(0);
6453 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
6455 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
6457 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6458 : elfcpp::R_POWERPC_RELATIVE
);
6459 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
6460 got
, off
, 0, false);
6465 case elfcpp::R_PPC64_TOC16
:
6466 case elfcpp::R_PPC64_TOC16_LO
:
6467 case elfcpp::R_PPC64_TOC16_HI
:
6468 case elfcpp::R_PPC64_TOC16_HA
:
6469 case elfcpp::R_PPC64_TOC16_DS
:
6470 case elfcpp::R_PPC64_TOC16_LO_DS
:
6471 // We need a GOT section.
6472 target
->got_section(symtab
, layout
);
6475 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6476 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6477 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6478 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6480 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
6481 if (tls_type
== tls::TLSOPT_NONE
)
6483 Output_data_got_powerpc
<size
, big_endian
>* got
6484 = target
->got_section(symtab
, layout
);
6485 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6486 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6487 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
6488 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
6490 else if (tls_type
== tls::TLSOPT_TO_LE
)
6492 // no GOT relocs needed for Local Exec.
6499 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6500 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6501 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6502 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6504 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6505 if (tls_type
== tls::TLSOPT_NONE
)
6506 target
->tlsld_got_offset(symtab
, layout
, object
);
6507 else if (tls_type
== tls::TLSOPT_TO_LE
)
6509 // no GOT relocs needed for Local Exec.
6510 if (parameters
->options().emit_relocs())
6512 Output_section
* os
= layout
->tls_segment()->first_section();
6513 gold_assert(os
!= NULL
);
6514 os
->set_needs_symtab_index();
6522 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6523 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6524 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6525 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6527 Output_data_got_powerpc
<size
, big_endian
>* got
6528 = target
->got_section(symtab
, layout
);
6529 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6530 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
6534 case elfcpp::R_POWERPC_GOT_TPREL16
:
6535 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6536 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6537 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6539 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
6540 if (tls_type
== tls::TLSOPT_NONE
)
6542 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6543 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
6545 Output_data_got_powerpc
<size
, big_endian
>* got
6546 = target
->got_section(symtab
, layout
);
6547 unsigned int off
= got
->add_constant(0);
6548 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
6550 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6551 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
6552 elfcpp::R_POWERPC_TPREL
,
6556 else if (tls_type
== tls::TLSOPT_TO_LE
)
6558 // no GOT relocs needed for Local Exec.
6566 unsupported_reloc_local(object
, r_type
);
6571 && parameters
->options().toc_optimize())
6573 if (data_shndx
== ppc_object
->toc_shndx())
6576 if (r_type
!= elfcpp::R_PPC64_ADDR64
6577 || (is_ifunc
&& target
->abiversion() < 2))
6579 else if (parameters
->options().output_is_position_independent())
6585 unsigned int shndx
= lsym
.get_st_shndx();
6586 if (shndx
>= elfcpp::SHN_LORESERVE
6587 && shndx
!= elfcpp::SHN_XINDEX
)
6592 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
6595 enum {no_check
, check_lo
, check_ha
} insn_check
;
6599 insn_check
= no_check
;
6602 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6603 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6604 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6605 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6606 case elfcpp::R_POWERPC_GOT16_HA
:
6607 case elfcpp::R_PPC64_TOC16_HA
:
6608 insn_check
= check_ha
;
6611 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6612 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6613 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6614 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6615 case elfcpp::R_POWERPC_GOT16_LO
:
6616 case elfcpp::R_PPC64_GOT16_LO_DS
:
6617 case elfcpp::R_PPC64_TOC16_LO
:
6618 case elfcpp::R_PPC64_TOC16_LO_DS
:
6619 insn_check
= check_lo
;
6623 section_size_type slen
;
6624 const unsigned char* view
= NULL
;
6625 if (insn_check
!= no_check
)
6627 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
6628 section_size_type off
=
6629 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
6632 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
6633 if (insn_check
== check_lo
6634 ? !ok_lo_toc_insn(insn
, r_type
)
6635 : ((insn
& ((0x3f << 26) | 0x1f << 16))
6636 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
6638 ppc_object
->set_no_toc_opt();
6639 gold_warning(_("%s: toc optimization is not supported "
6640 "for %#08x instruction"),
6641 ppc_object
->name().c_str(), insn
);
6650 case elfcpp::R_PPC64_TOC16
:
6651 case elfcpp::R_PPC64_TOC16_LO
:
6652 case elfcpp::R_PPC64_TOC16_HI
:
6653 case elfcpp::R_PPC64_TOC16_HA
:
6654 case elfcpp::R_PPC64_TOC16_DS
:
6655 case elfcpp::R_PPC64_TOC16_LO_DS
:
6656 unsigned int shndx
= lsym
.get_st_shndx();
6657 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6659 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6660 if (is_ordinary
&& shndx
== ppc_object
->toc_shndx())
6662 Address dst_off
= lsym
.get_st_value() + reloc
.get_r_offset();
6663 if (dst_off
< ppc_object
->section_size(shndx
))
6666 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
6668 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
6670 // Need to check that the insn is a ld
6672 view
= ppc_object
->section_contents(data_shndx
,
6675 section_size_type off
=
6676 (convert_to_section_size_type(reloc
.get_r_offset())
6677 + (big_endian
? -2 : 3));
6679 && (view
[off
] & (0x3f << 2)) == 58u << 2)
6683 ppc_object
->set_no_toc_opt(dst_off
);
6694 case elfcpp::R_POWERPC_REL32
:
6695 if (ppc_object
->got2_shndx() != 0
6696 && parameters
->options().output_is_position_independent())
6698 unsigned int shndx
= lsym
.get_st_shndx();
6699 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6701 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6702 if (is_ordinary
&& shndx
== ppc_object
->got2_shndx()
6703 && (ppc_object
->section_flags(data_shndx
)
6704 & elfcpp::SHF_EXECINSTR
) != 0)
6705 gold_error(_("%s: unsupported -mbss-plt code"),
6706 ppc_object
->name().c_str());
6716 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6717 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6718 case elfcpp::R_POWERPC_GOT_TPREL16
:
6719 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6720 case elfcpp::R_POWERPC_GOT16
:
6721 case elfcpp::R_PPC64_GOT16_DS
:
6722 case elfcpp::R_PPC64_TOC16
:
6723 case elfcpp::R_PPC64_TOC16_DS
:
6724 ppc_object
->set_has_small_toc_reloc();
6730 // Report an unsupported relocation against a global symbol.
6732 template<int size
, bool big_endian
>
6734 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
6735 Sized_relobj_file
<size
, big_endian
>* object
,
6736 unsigned int r_type
,
6739 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6740 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
6743 // Scan a relocation for a global symbol.
6745 template<int size
, bool big_endian
>
6747 Target_powerpc
<size
, big_endian
>::Scan::global(
6748 Symbol_table
* symtab
,
6750 Target_powerpc
<size
, big_endian
>* target
,
6751 Sized_relobj_file
<size
, big_endian
>* object
,
6752 unsigned int data_shndx
,
6753 Output_section
* output_section
,
6754 const elfcpp::Rela
<size
, big_endian
>& reloc
,
6755 unsigned int r_type
,
6758 if (this->maybe_skip_tls_get_addr_call(r_type
, gsym
) == Track_tls::SKIP
)
6761 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6762 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6764 this->expect_tls_get_addr_call();
6765 const bool final
= gsym
->final_value_is_known();
6766 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6767 if (tls_type
!= tls::TLSOPT_NONE
)
6768 this->skip_next_tls_get_addr_call();
6770 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6771 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6773 this->expect_tls_get_addr_call();
6774 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6775 if (tls_type
!= tls::TLSOPT_NONE
)
6776 this->skip_next_tls_get_addr_call();
6779 Powerpc_relobj
<size
, big_endian
>* ppc_object
6780 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6782 // A STT_GNU_IFUNC symbol may require a PLT entry.
6783 bool is_ifunc
= gsym
->type() == elfcpp::STT_GNU_IFUNC
;
6784 bool pushed_ifunc
= false;
6785 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
6787 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6788 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6789 r_type
, r_sym
, reloc
.get_r_addend());
6790 target
->make_plt_entry(symtab
, layout
, gsym
);
6791 pushed_ifunc
= true;
6796 case elfcpp::R_POWERPC_NONE
:
6797 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6798 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6799 case elfcpp::R_PPC_LOCAL24PC
:
6800 case elfcpp::R_POWERPC_TLS
:
6801 case elfcpp::R_PPC64_ENTRY
:
6804 case elfcpp::R_PPC64_TOC
:
6806 Output_data_got_powerpc
<size
, big_endian
>* got
6807 = target
->got_section(symtab
, layout
);
6808 if (parameters
->options().output_is_position_independent())
6810 Address off
= reloc
.get_r_offset();
6812 && data_shndx
== ppc_object
->opd_shndx()
6813 && ppc_object
->get_opd_discard(off
- 8))
6816 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6817 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
6818 if (data_shndx
!= ppc_object
->opd_shndx())
6819 symobj
= static_cast
6820 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
6821 rela_dyn
->add_output_section_relative(got
->output_section(),
6822 elfcpp::R_POWERPC_RELATIVE
,
6824 object
, data_shndx
, off
,
6825 symobj
->toc_base_offset());
6830 case elfcpp::R_PPC64_ADDR64
:
6832 && target
->abiversion() < 2
6833 && data_shndx
== ppc_object
->opd_shndx()
6834 && (gsym
->is_defined_in_discarded_section()
6835 || gsym
->object() != object
))
6837 ppc_object
->set_opd_discard(reloc
.get_r_offset());
6841 case elfcpp::R_PPC64_UADDR64
:
6842 case elfcpp::R_POWERPC_ADDR32
:
6843 case elfcpp::R_POWERPC_UADDR32
:
6844 case elfcpp::R_POWERPC_ADDR24
:
6845 case elfcpp::R_POWERPC_ADDR16
:
6846 case elfcpp::R_POWERPC_ADDR16_LO
:
6847 case elfcpp::R_POWERPC_ADDR16_HI
:
6848 case elfcpp::R_POWERPC_ADDR16_HA
:
6849 case elfcpp::R_POWERPC_UADDR16
:
6850 case elfcpp::R_PPC64_ADDR16_HIGH
:
6851 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6852 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6853 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6854 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6855 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6856 case elfcpp::R_PPC64_ADDR16_DS
:
6857 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6858 case elfcpp::R_POWERPC_ADDR14
:
6859 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6860 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6862 // Make a PLT entry if necessary.
6863 if (gsym
->needs_plt_entry())
6865 // Since this is not a PC-relative relocation, we may be
6866 // taking the address of a function. In that case we need to
6867 // set the entry in the dynamic symbol table to the address of
6868 // the PLT call stub.
6869 bool need_ifunc_plt
= false;
6870 if ((size
== 32 || target
->abiversion() >= 2)
6871 && gsym
->is_from_dynobj()
6872 && !parameters
->options().output_is_position_independent())
6874 gsym
->set_needs_dynsym_value();
6875 need_ifunc_plt
= true;
6877 if (!is_ifunc
|| (!pushed_ifunc
&& need_ifunc_plt
))
6879 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6880 target
->push_branch(ppc_object
, data_shndx
,
6881 reloc
.get_r_offset(), r_type
, r_sym
,
6882 reloc
.get_r_addend());
6883 target
->make_plt_entry(symtab
, layout
, gsym
);
6886 // Make a dynamic relocation if necessary.
6887 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
))
6888 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
6890 if (!parameters
->options().output_is_position_independent()
6891 && gsym
->may_need_copy_reloc())
6893 target
->copy_reloc(symtab
, layout
, object
,
6894 data_shndx
, output_section
, gsym
, reloc
);
6896 else if ((((size
== 32
6897 && r_type
== elfcpp::R_POWERPC_ADDR32
)
6899 && r_type
== elfcpp::R_PPC64_ADDR64
6900 && target
->abiversion() >= 2))
6901 && gsym
->can_use_relative_reloc(false)
6902 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
6903 && parameters
->options().shared()))
6905 && r_type
== elfcpp::R_PPC64_ADDR64
6906 && target
->abiversion() < 2
6907 && (gsym
->can_use_relative_reloc(false)
6908 || data_shndx
== ppc_object
->opd_shndx())))
6910 Reloc_section
* rela_dyn
6911 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6912 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6913 : elfcpp::R_POWERPC_RELATIVE
);
6914 rela_dyn
->add_symbolless_global_addend(
6915 gsym
, dynrel
, output_section
, object
, data_shndx
,
6916 reloc
.get_r_offset(), reloc
.get_r_addend());
6920 Reloc_section
* rela_dyn
6921 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6922 check_non_pic(object
, r_type
);
6923 rela_dyn
->add_global(gsym
, r_type
, output_section
,
6925 reloc
.get_r_offset(),
6926 reloc
.get_r_addend());
6929 && parameters
->options().toc_optimize()
6930 && data_shndx
== ppc_object
->toc_shndx())
6931 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
6937 case elfcpp::R_PPC_PLTREL24
:
6938 case elfcpp::R_POWERPC_REL24
:
6941 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6942 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6943 r_type
, r_sym
, reloc
.get_r_addend());
6944 if (gsym
->needs_plt_entry()
6945 || (!gsym
->final_value_is_known()
6946 && (gsym
->is_undefined()
6947 || gsym
->is_from_dynobj()
6948 || gsym
->is_preemptible())))
6949 target
->make_plt_entry(symtab
, layout
, gsym
);
6953 case elfcpp::R_PPC64_REL64
:
6954 case elfcpp::R_POWERPC_REL32
:
6955 // Make a dynamic relocation if necessary.
6956 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
)))
6958 if (!parameters
->options().output_is_position_independent()
6959 && gsym
->may_need_copy_reloc())
6961 target
->copy_reloc(symtab
, layout
, object
,
6962 data_shndx
, output_section
, gsym
,
6967 Reloc_section
* rela_dyn
6968 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6969 check_non_pic(object
, r_type
);
6970 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
6971 data_shndx
, reloc
.get_r_offset(),
6972 reloc
.get_r_addend());
6977 case elfcpp::R_POWERPC_REL14
:
6978 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6979 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6982 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6983 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6984 r_type
, r_sym
, reloc
.get_r_addend());
6988 case elfcpp::R_PPC64_TOCSAVE
:
6989 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6990 // caller has already saved r2 and thus a plt call stub need not
6993 && target
->mark_pltcall(ppc_object
, data_shndx
,
6994 reloc
.get_r_offset() - 4, symtab
))
6996 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6998 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
7000 object
->error(_("tocsave symbol %u has bad shndx %u"),
7004 Sized_symbol
<size
>* sym
= symtab
->get_sized_symbol
<size
>(gsym
);
7005 target
->add_tocsave(ppc_object
, shndx
,
7006 sym
->value() + reloc
.get_r_addend());
7011 case elfcpp::R_POWERPC_REL16
:
7012 case elfcpp::R_POWERPC_REL16_LO
:
7013 case elfcpp::R_POWERPC_REL16_HI
:
7014 case elfcpp::R_POWERPC_REL16_HA
:
7015 case elfcpp::R_POWERPC_REL16DX_HA
:
7016 case elfcpp::R_POWERPC_SECTOFF
:
7017 case elfcpp::R_POWERPC_SECTOFF_LO
:
7018 case elfcpp::R_POWERPC_SECTOFF_HI
:
7019 case elfcpp::R_POWERPC_SECTOFF_HA
:
7020 case elfcpp::R_PPC64_SECTOFF_DS
:
7021 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
7022 case elfcpp::R_POWERPC_TPREL16
:
7023 case elfcpp::R_POWERPC_TPREL16_LO
:
7024 case elfcpp::R_POWERPC_TPREL16_HI
:
7025 case elfcpp::R_POWERPC_TPREL16_HA
:
7026 case elfcpp::R_PPC64_TPREL16_DS
:
7027 case elfcpp::R_PPC64_TPREL16_LO_DS
:
7028 case elfcpp::R_PPC64_TPREL16_HIGH
:
7029 case elfcpp::R_PPC64_TPREL16_HIGHA
:
7030 case elfcpp::R_PPC64_TPREL16_HIGHER
:
7031 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
7032 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
7033 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
7034 case elfcpp::R_POWERPC_DTPREL16
:
7035 case elfcpp::R_POWERPC_DTPREL16_LO
:
7036 case elfcpp::R_POWERPC_DTPREL16_HI
:
7037 case elfcpp::R_POWERPC_DTPREL16_HA
:
7038 case elfcpp::R_PPC64_DTPREL16_DS
:
7039 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
7040 case elfcpp::R_PPC64_DTPREL16_HIGH
:
7041 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
7042 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
7043 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
7044 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
7045 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
7046 case elfcpp::R_PPC64_TLSGD
:
7047 case elfcpp::R_PPC64_TLSLD
:
7048 case elfcpp::R_PPC64_ADDR64_LOCAL
:
7051 case elfcpp::R_POWERPC_GOT16
:
7052 case elfcpp::R_POWERPC_GOT16_LO
:
7053 case elfcpp::R_POWERPC_GOT16_HI
:
7054 case elfcpp::R_POWERPC_GOT16_HA
:
7055 case elfcpp::R_PPC64_GOT16_DS
:
7056 case elfcpp::R_PPC64_GOT16_LO_DS
:
7058 // The symbol requires a GOT entry.
7059 Output_data_got_powerpc
<size
, big_endian
>* got
;
7061 got
= target
->got_section(symtab
, layout
);
7062 if (gsym
->final_value_is_known())
7065 && (size
== 32 || target
->abiversion() >= 2))
7066 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
7068 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
7070 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
7072 // If we are generating a shared object or a pie, this
7073 // symbol's GOT entry will be set by a dynamic relocation.
7074 unsigned int off
= got
->add_constant(0);
7075 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
7077 Reloc_section
* rela_dyn
7078 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
7080 if (gsym
->can_use_relative_reloc(false)
7082 || target
->abiversion() >= 2)
7083 && gsym
->visibility() == elfcpp::STV_PROTECTED
7084 && parameters
->options().shared()))
7086 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
7087 : elfcpp::R_POWERPC_RELATIVE
);
7088 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
7092 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
7093 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
7099 case elfcpp::R_PPC64_TOC16
:
7100 case elfcpp::R_PPC64_TOC16_LO
:
7101 case elfcpp::R_PPC64_TOC16_HI
:
7102 case elfcpp::R_PPC64_TOC16_HA
:
7103 case elfcpp::R_PPC64_TOC16_DS
:
7104 case elfcpp::R_PPC64_TOC16_LO_DS
:
7105 // We need a GOT section.
7106 target
->got_section(symtab
, layout
);
7109 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7110 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7111 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
7112 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7114 const bool final
= gsym
->final_value_is_known();
7115 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7116 if (tls_type
== tls::TLSOPT_NONE
)
7118 Output_data_got_powerpc
<size
, big_endian
>* got
7119 = target
->got_section(symtab
, layout
);
7120 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7121 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
, rela_dyn
,
7122 elfcpp::R_POWERPC_DTPMOD
,
7123 elfcpp::R_POWERPC_DTPREL
);
7125 else if (tls_type
== tls::TLSOPT_TO_IE
)
7127 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
7129 Output_data_got_powerpc
<size
, big_endian
>* got
7130 = target
->got_section(symtab
, layout
);
7131 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7132 if (gsym
->is_undefined()
7133 || gsym
->is_from_dynobj())
7135 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
7136 elfcpp::R_POWERPC_TPREL
);
7140 unsigned int off
= got
->add_constant(0);
7141 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
7142 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
7143 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
7148 else if (tls_type
== tls::TLSOPT_TO_LE
)
7150 // no GOT relocs needed for Local Exec.
7157 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7158 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7159 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
7160 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7162 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7163 if (tls_type
== tls::TLSOPT_NONE
)
7164 target
->tlsld_got_offset(symtab
, layout
, object
);
7165 else if (tls_type
== tls::TLSOPT_TO_LE
)
7167 // no GOT relocs needed for Local Exec.
7168 if (parameters
->options().emit_relocs())
7170 Output_section
* os
= layout
->tls_segment()->first_section();
7171 gold_assert(os
!= NULL
);
7172 os
->set_needs_symtab_index();
7180 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7181 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7182 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
7183 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7185 Output_data_got_powerpc
<size
, big_endian
>* got
7186 = target
->got_section(symtab
, layout
);
7187 if (!gsym
->final_value_is_known()
7188 && (gsym
->is_from_dynobj()
7189 || gsym
->is_undefined()
7190 || gsym
->is_preemptible()))
7191 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
7192 target
->rela_dyn_section(layout
),
7193 elfcpp::R_POWERPC_DTPREL
);
7195 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
7199 case elfcpp::R_POWERPC_GOT_TPREL16
:
7200 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7201 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7202 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7204 const bool final
= gsym
->final_value_is_known();
7205 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
7206 if (tls_type
== tls::TLSOPT_NONE
)
7208 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
7210 Output_data_got_powerpc
<size
, big_endian
>* got
7211 = target
->got_section(symtab
, layout
);
7212 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7213 if (gsym
->is_undefined()
7214 || gsym
->is_from_dynobj())
7216 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
7217 elfcpp::R_POWERPC_TPREL
);
7221 unsigned int off
= got
->add_constant(0);
7222 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
7223 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
7224 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
7229 else if (tls_type
== tls::TLSOPT_TO_LE
)
7231 // no GOT relocs needed for Local Exec.
7239 unsupported_reloc_global(object
, r_type
, gsym
);
7244 && parameters
->options().toc_optimize())
7246 if (data_shndx
== ppc_object
->toc_shndx())
7249 if (r_type
!= elfcpp::R_PPC64_ADDR64
7250 || (is_ifunc
&& target
->abiversion() < 2))
7252 else if (parameters
->options().output_is_position_independent()
7253 && (is_ifunc
|| gsym
->is_absolute() || gsym
->is_undefined()))
7256 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
7259 enum {no_check
, check_lo
, check_ha
} insn_check
;
7263 insn_check
= no_check
;
7266 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7267 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7268 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7269 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7270 case elfcpp::R_POWERPC_GOT16_HA
:
7271 case elfcpp::R_PPC64_TOC16_HA
:
7272 insn_check
= check_ha
;
7275 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7276 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7277 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7278 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7279 case elfcpp::R_POWERPC_GOT16_LO
:
7280 case elfcpp::R_PPC64_GOT16_LO_DS
:
7281 case elfcpp::R_PPC64_TOC16_LO
:
7282 case elfcpp::R_PPC64_TOC16_LO_DS
:
7283 insn_check
= check_lo
;
7287 section_size_type slen
;
7288 const unsigned char* view
= NULL
;
7289 if (insn_check
!= no_check
)
7291 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
7292 section_size_type off
=
7293 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
7296 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
7297 if (insn_check
== check_lo
7298 ? !ok_lo_toc_insn(insn
, r_type
)
7299 : ((insn
& ((0x3f << 26) | 0x1f << 16))
7300 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7302 ppc_object
->set_no_toc_opt();
7303 gold_warning(_("%s: toc optimization is not supported "
7304 "for %#08x instruction"),
7305 ppc_object
->name().c_str(), insn
);
7314 case elfcpp::R_PPC64_TOC16
:
7315 case elfcpp::R_PPC64_TOC16_LO
:
7316 case elfcpp::R_PPC64_TOC16_HI
:
7317 case elfcpp::R_PPC64_TOC16_HA
:
7318 case elfcpp::R_PPC64_TOC16_DS
:
7319 case elfcpp::R_PPC64_TOC16_LO_DS
:
7320 if (gsym
->source() == Symbol::FROM_OBJECT
7321 && !gsym
->object()->is_dynamic())
7323 Powerpc_relobj
<size
, big_endian
>* sym_object
7324 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7326 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
7327 if (shndx
== sym_object
->toc_shndx())
7329 Sized_symbol
<size
>* sym
= symtab
->get_sized_symbol
<size
>(gsym
);
7330 Address dst_off
= sym
->value() + reloc
.get_r_offset();
7331 if (dst_off
< sym_object
->section_size(shndx
))
7334 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
7336 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
7338 // Need to check that the insn is a ld
7340 view
= ppc_object
->section_contents(data_shndx
,
7343 section_size_type off
=
7344 (convert_to_section_size_type(reloc
.get_r_offset())
7345 + (big_endian
? -2 : 3));
7347 && (view
[off
] & (0x3f << 2)) == (58u << 2))
7351 sym_object
->set_no_toc_opt(dst_off
);
7363 case elfcpp::R_PPC_LOCAL24PC
:
7364 if (strcmp(gsym
->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7365 gold_error(_("%s: unsupported -mbss-plt code"),
7366 ppc_object
->name().c_str());
7375 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7376 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7377 case elfcpp::R_POWERPC_GOT_TPREL16
:
7378 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7379 case elfcpp::R_POWERPC_GOT16
:
7380 case elfcpp::R_PPC64_GOT16_DS
:
7381 case elfcpp::R_PPC64_TOC16
:
7382 case elfcpp::R_PPC64_TOC16_DS
:
7383 ppc_object
->set_has_small_toc_reloc();
7389 // Process relocations for gc.
7391 template<int size
, bool big_endian
>
7393 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
7394 Symbol_table
* symtab
,
7396 Sized_relobj_file
<size
, big_endian
>* object
,
7397 unsigned int data_shndx
,
7399 const unsigned char* prelocs
,
7401 Output_section
* output_section
,
7402 bool needs_special_offset_handling
,
7403 size_t local_symbol_count
,
7404 const unsigned char* plocal_symbols
)
7406 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
7407 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
7410 Powerpc_relobj
<size
, big_endian
>* ppc_object
7411 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
7413 ppc_object
->set_opd_valid();
7414 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
7416 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
7417 for (p
= ppc_object
->access_from_map()->begin();
7418 p
!= ppc_object
->access_from_map()->end();
7421 Address dst_off
= p
->first
;
7422 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
7423 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
7424 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
7426 Relobj
* src_obj
= s
->first
;
7427 unsigned int src_indx
= s
->second
;
7428 symtab
->gc()->add_reference(src_obj
, src_indx
,
7429 ppc_object
, dst_indx
);
7433 ppc_object
->access_from_map()->clear();
7434 ppc_object
->process_gc_mark(symtab
);
7435 // Don't look at .opd relocs as .opd will reference everything.
7439 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
7448 needs_special_offset_handling
,
7453 // Handle target specific gc actions when adding a gc reference from
7454 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7455 // and DST_OFF. For powerpc64, this adds a referenc to the code
7456 // section of a function descriptor.
7458 template<int size
, bool big_endian
>
7460 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
7461 Symbol_table
* symtab
,
7463 unsigned int src_shndx
,
7465 unsigned int dst_shndx
,
7466 Address dst_off
) const
7468 if (size
!= 64 || dst_obj
->is_dynamic())
7471 Powerpc_relobj
<size
, big_endian
>* ppc_object
7472 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
7473 if (dst_shndx
!= 0 && dst_shndx
== ppc_object
->opd_shndx())
7475 if (ppc_object
->opd_valid())
7477 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
7478 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
7482 // If we haven't run scan_opd_relocs, we must delay
7483 // processing this function descriptor reference.
7484 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
7489 // Add any special sections for this symbol to the gc work list.
7490 // For powerpc64, this adds the code section of a function
7493 template<int size
, bool big_endian
>
7495 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
7496 Symbol_table
* symtab
,
7501 Powerpc_relobj
<size
, big_endian
>* ppc_object
7502 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
7504 unsigned int shndx
= sym
->shndx(&is_ordinary
);
7505 if (is_ordinary
&& shndx
!= 0 && shndx
== ppc_object
->opd_shndx())
7507 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
7508 Address dst_off
= gsym
->value();
7509 if (ppc_object
->opd_valid())
7511 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
7512 symtab
->gc()->worklist().push_back(Section_id(ppc_object
,
7516 ppc_object
->add_gc_mark(dst_off
);
7521 // For a symbol location in .opd, set LOC to the location of the
7524 template<int size
, bool big_endian
>
7526 Target_powerpc
<size
, big_endian
>::do_function_location(
7527 Symbol_location
* loc
) const
7529 if (size
== 64 && loc
->shndx
!= 0)
7531 if (loc
->object
->is_dynamic())
7533 Powerpc_dynobj
<size
, big_endian
>* ppc_object
7534 = static_cast<Powerpc_dynobj
<size
, big_endian
>*>(loc
->object
);
7535 if (loc
->shndx
== ppc_object
->opd_shndx())
7538 Address off
= loc
->offset
- ppc_object
->opd_address();
7539 loc
->shndx
= ppc_object
->get_opd_ent(off
, &dest_off
);
7540 loc
->offset
= dest_off
;
7545 const Powerpc_relobj
<size
, big_endian
>* ppc_object
7546 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(loc
->object
);
7547 if (loc
->shndx
== ppc_object
->opd_shndx())
7550 loc
->shndx
= ppc_object
->get_opd_ent(loc
->offset
, &dest_off
);
7551 loc
->offset
= dest_off
;
7557 // FNOFFSET in section SHNDX in OBJECT is the start of a function
7558 // compiled with -fsplit-stack. The function calls non-split-stack
7559 // code. Change the function to ensure it has enough stack space to
7560 // call some random function.
7562 template<int size
, bool big_endian
>
7564 Target_powerpc
<size
, big_endian
>::do_calls_non_split(
7567 section_offset_type fnoffset
,
7568 section_size_type fnsize
,
7569 const unsigned char* prelocs
,
7571 unsigned char* view
,
7572 section_size_type view_size
,
7574 std::string
* to
) const
7576 // 32-bit not supported.
7580 Target::do_calls_non_split(object
, shndx
, fnoffset
, fnsize
,
7581 prelocs
, reloc_count
, view
, view_size
,
7586 // The function always starts with
7587 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
7588 // addis %r12,%r1,-allocate@ha
7589 // addi %r12,%r12,-allocate@l
7591 // but note that the addis or addi may be replaced with a nop
7593 unsigned char *entry
= view
+ fnoffset
;
7594 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
7596 if ((insn
& 0xffff0000) == addis_2_12
)
7598 /* Skip ELFv2 global entry code. */
7600 insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
7603 unsigned char *pinsn
= entry
;
7605 const uint32_t ld_private_ss
= 0xe80d8fc0;
7606 if (insn
== ld_private_ss
)
7608 int32_t allocate
= 0;
7612 insn
= elfcpp::Swap
<32, big_endian
>::readval(pinsn
);
7613 if ((insn
& 0xffff0000) == addis_12_1
)
7614 allocate
+= (insn
& 0xffff) << 16;
7615 else if ((insn
& 0xffff0000) == addi_12_1
7616 || (insn
& 0xffff0000) == addi_12_12
)
7617 allocate
+= ((insn
& 0xffff) ^ 0x8000) - 0x8000;
7618 else if (insn
!= nop
)
7621 if (insn
== cmpld_7_12_0
&& pinsn
== entry
+ 12)
7623 int extra
= parameters
->options().split_stack_adjust_size();
7625 if (allocate
>= 0 || extra
< 0)
7627 object
->error(_("split-stack stack size overflow at "
7628 "section %u offset %0zx"),
7629 shndx
, static_cast<size_t>(fnoffset
));
7633 insn
= addis_12_1
| (((allocate
+ 0x8000) >> 16) & 0xffff);
7634 if (insn
!= addis_12_1
)
7636 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7638 insn
= addi_12_12
| (allocate
& 0xffff);
7639 if (insn
!= addi_12_12
)
7641 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7647 insn
= addi_12_1
| (allocate
& 0xffff);
7648 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7651 if (pinsn
!= entry
+ 12)
7652 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, nop
);
7660 if (!object
->has_no_split_stack())
7661 object
->error(_("failed to match split-stack sequence at "
7662 "section %u offset %0zx"),
7663 shndx
, static_cast<size_t>(fnoffset
));
7667 // Scan relocations for a section.
7669 template<int size
, bool big_endian
>
7671 Target_powerpc
<size
, big_endian
>::scan_relocs(
7672 Symbol_table
* symtab
,
7674 Sized_relobj_file
<size
, big_endian
>* object
,
7675 unsigned int data_shndx
,
7676 unsigned int sh_type
,
7677 const unsigned char* prelocs
,
7679 Output_section
* output_section
,
7680 bool needs_special_offset_handling
,
7681 size_t local_symbol_count
,
7682 const unsigned char* plocal_symbols
)
7684 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
7685 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
7688 if (!this->plt_localentry0_init_
)
7690 bool plt_localentry0
= false;
7692 && this->abiversion() >= 2)
7694 if (parameters
->options().user_set_plt_localentry())
7695 plt_localentry0
= parameters
->options().plt_localentry();
7697 && symtab
->lookup("GLIBC_2.26", NULL
) == NULL
)
7698 gold_warning(_("--plt-localentry is especially dangerous without "
7699 "ld.so support to detect ABI violations"));
7701 this->plt_localentry0_
= plt_localentry0
;
7702 this->plt_localentry0_init_
= true;
7705 if (sh_type
== elfcpp::SHT_REL
)
7707 gold_error(_("%s: unsupported REL reloc section"),
7708 object
->name().c_str());
7712 gold::scan_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
7721 needs_special_offset_handling
,
7726 // Functor class for processing the global symbol table.
7727 // Removes symbols defined on discarded opd entries.
7729 template<bool big_endian
>
7730 class Global_symbol_visitor_opd
7733 Global_symbol_visitor_opd()
7737 operator()(Sized_symbol
<64>* sym
)
7739 if (sym
->has_symtab_index()
7740 || sym
->source() != Symbol::FROM_OBJECT
7741 || !sym
->in_real_elf())
7744 if (sym
->object()->is_dynamic())
7747 Powerpc_relobj
<64, big_endian
>* symobj
7748 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
7749 if (symobj
->opd_shndx() == 0)
7753 unsigned int shndx
= sym
->shndx(&is_ordinary
);
7754 if (shndx
== symobj
->opd_shndx()
7755 && symobj
->get_opd_discard(sym
->value()))
7757 sym
->set_undefined();
7758 sym
->set_visibility(elfcpp::STV_DEFAULT
);
7759 sym
->set_is_defined_in_discarded_section();
7760 sym
->set_symtab_index(-1U);
7765 template<int size
, bool big_endian
>
7767 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
7769 Symbol_table
* symtab
)
7773 Output_data_save_res
<size
, big_endian
>* savres
7774 = new Output_data_save_res
<size
, big_endian
>(symtab
);
7775 this->savres_section_
= savres
;
7776 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
7777 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
7778 savres
, ORDER_TEXT
, false);
7782 // Sort linker created .got section first (for the header), then input
7783 // sections belonging to files using small model code.
7785 template<bool big_endian
>
7786 class Sort_toc_sections
7790 operator()(const Output_section::Input_section
& is1
,
7791 const Output_section::Input_section
& is2
) const
7793 if (!is1
.is_input_section() && is2
.is_input_section())
7796 = (is1
.is_input_section()
7797 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
7798 ->has_small_toc_reloc()));
7800 = (is2
.is_input_section()
7801 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
7802 ->has_small_toc_reloc()));
7803 return small1
&& !small2
;
7807 // Finalize the sections.
7809 template<int size
, bool big_endian
>
7811 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
7813 const Input_objects
*,
7814 Symbol_table
* symtab
)
7816 if (parameters
->doing_static_link())
7818 // At least some versions of glibc elf-init.o have a strong
7819 // reference to __rela_iplt marker syms. A weak ref would be
7821 if (this->iplt_
!= NULL
)
7823 Reloc_section
* rel
= this->iplt_
->rel_plt();
7824 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
7825 Symbol_table::PREDEFINED
, rel
, 0, 0,
7826 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7827 elfcpp::STV_HIDDEN
, 0, false, true);
7828 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
7829 Symbol_table::PREDEFINED
, rel
, 0, 0,
7830 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7831 elfcpp::STV_HIDDEN
, 0, true, true);
7835 symtab
->define_as_constant("__rela_iplt_start", NULL
,
7836 Symbol_table::PREDEFINED
, 0, 0,
7837 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7838 elfcpp::STV_HIDDEN
, 0, true, false);
7839 symtab
->define_as_constant("__rela_iplt_end", NULL
,
7840 Symbol_table::PREDEFINED
, 0, 0,
7841 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
7842 elfcpp::STV_HIDDEN
, 0, true, false);
7848 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
7849 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
7851 if (!parameters
->options().relocatable())
7853 this->define_save_restore_funcs(layout
, symtab
);
7855 // Annoyingly, we need to make these sections now whether or
7856 // not we need them. If we delay until do_relax then we
7857 // need to mess with the relaxation machinery checkpointing.
7858 this->got_section(symtab
, layout
);
7859 this->make_brlt_section(layout
);
7861 if (parameters
->options().toc_sort())
7863 Output_section
* os
= this->got_
->output_section();
7864 if (os
!= NULL
&& os
->input_sections().size() > 1)
7865 std::stable_sort(os
->input_sections().begin(),
7866 os
->input_sections().end(),
7867 Sort_toc_sections
<big_endian
>());
7872 // Fill in some more dynamic tags.
7873 Output_data_dynamic
* odyn
= layout
->dynamic_data();
7876 const Reloc_section
* rel_plt
= (this->plt_
== NULL
7878 : this->plt_
->rel_plt());
7879 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
7880 this->rela_dyn_
, true, size
== 32);
7884 if (this->got_
!= NULL
)
7886 this->got_
->finalize_data_size();
7887 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
7888 this->got_
, this->got_
->g_o_t());
7893 if (this->glink_
!= NULL
)
7895 this->glink_
->finalize_data_size();
7896 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
7898 (this->glink_
->pltresolve_size
7901 if (this->has_localentry0_
)
7902 odyn
->add_constant(elfcpp::DT_PPC64_OPT
,
7903 elfcpp::PPC64_OPT_LOCALENTRY
);
7907 // Emit any relocs we saved in an attempt to avoid generating COPY
7909 if (this->copy_relocs_
.any_saved_relocs())
7910 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
7913 // Emit any saved relocs, and mark toc entries using any of these
7914 // relocs as not optimizable.
7916 template<int sh_type
, int size
, bool big_endian
>
7918 Powerpc_copy_relocs
<sh_type
, size
, big_endian
>::emit(
7919 Output_data_reloc
<sh_type
, true, size
, big_endian
>* reloc_section
)
7922 && parameters
->options().toc_optimize())
7924 for (typename Copy_relocs
<sh_type
, size
, big_endian
>::
7925 Copy_reloc_entries::iterator p
= this->entries_
.begin();
7926 p
!= this->entries_
.end();
7929 typename Copy_relocs
<sh_type
, size
, big_endian
>::Copy_reloc_entry
&
7932 // If the symbol is no longer defined in a dynamic object,
7933 // then we emitted a COPY relocation. If it is still
7934 // dynamic then we'll need dynamic relocations and thus
7935 // can't optimize toc entries.
7936 if (entry
.sym_
->is_from_dynobj())
7938 Powerpc_relobj
<size
, big_endian
>* ppc_object
7939 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(entry
.relobj_
);
7940 if (entry
.shndx_
== ppc_object
->toc_shndx())
7941 ppc_object
->set_no_toc_opt(entry
.address_
);
7946 Copy_relocs
<sh_type
, size
, big_endian
>::emit(reloc_section
);
7949 // Return the value to use for a branch relocation.
7951 template<int size
, bool big_endian
>
7953 Target_powerpc
<size
, big_endian
>::symval_for_branch(
7954 const Symbol_table
* symtab
,
7955 const Sized_symbol
<size
>* gsym
,
7956 Powerpc_relobj
<size
, big_endian
>* object
,
7958 unsigned int *dest_shndx
)
7960 if (size
== 32 || this->abiversion() >= 2)
7964 // If the symbol is defined in an opd section, ie. is a function
7965 // descriptor, use the function descriptor code entry address
7966 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
7968 && (gsym
->source() != Symbol::FROM_OBJECT
7969 || gsym
->object()->is_dynamic()))
7972 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7973 unsigned int shndx
= symobj
->opd_shndx();
7976 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
7977 if (opd_addr
== invalid_address
)
7979 opd_addr
+= symobj
->output_section_address(shndx
);
7980 if (*value
>= opd_addr
&& *value
< opd_addr
+ symobj
->section_size(shndx
))
7983 *dest_shndx
= symobj
->get_opd_ent(*value
- opd_addr
, &sec_off
);
7984 if (symtab
->is_section_folded(symobj
, *dest_shndx
))
7987 = symtab
->icf()->get_folded_section(symobj
, *dest_shndx
);
7988 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(folded
.first
);
7989 *dest_shndx
= folded
.second
;
7991 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
7992 if (sec_addr
== invalid_address
)
7995 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
7996 *value
= sec_addr
+ sec_off
;
8001 // Perform a relocation.
8003 template<int size
, bool big_endian
>
8005 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
8006 const Relocate_info
<size
, big_endian
>* relinfo
,
8008 Target_powerpc
* target
,
8011 const unsigned char* preloc
,
8012 const Sized_symbol
<size
>* gsym
,
8013 const Symbol_value
<size
>* psymval
,
8014 unsigned char* view
,
8016 section_size_type view_size
)
8021 const elfcpp::Rela
<size
, big_endian
> rela(preloc
);
8022 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
8023 switch (this->maybe_skip_tls_get_addr_call(r_type
, gsym
))
8025 case Track_tls::NOT_EXPECTED
:
8026 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8027 _("__tls_get_addr call lacks marker reloc"));
8029 case Track_tls::EXPECTED
:
8030 // We have already complained.
8032 case Track_tls::SKIP
:
8034 case Track_tls::NORMAL
:
8038 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
8039 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
8040 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
8041 // Offset from start of insn to d-field reloc.
8042 const int d_offset
= big_endian
? 2 : 0;
8044 Powerpc_relobj
<size
, big_endian
>* const object
8045 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
8047 bool has_stub_value
= false;
8048 bool localentry0
= false;
8049 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
8051 ? gsym
->use_plt_offset(Scan::get_reference_flags(r_type
, target
))
8052 : object
->local_has_plt_offset(r_sym
))
8053 && (!psymval
->is_ifunc_symbol()
8054 || Scan::reloc_needs_plt_for_ifunc(target
, object
, r_type
, false)))
8058 && target
->abiversion() >= 2
8059 && !parameters
->options().output_is_position_independent()
8060 && !is_branch_reloc(r_type
))
8062 Address off
= target
->glink_section()->find_global_entry(gsym
);
8063 if (off
!= invalid_address
)
8065 value
= target
->glink_section()->global_entry_address() + off
;
8066 has_stub_value
= true;
8071 Stub_table
<size
, big_endian
>* stub_table
8072 = object
->stub_table(relinfo
->data_shndx
);
8073 if (stub_table
== NULL
)
8075 // This is a ref from a data section to an ifunc symbol.
8076 if (target
->stub_tables().size() != 0)
8077 stub_table
= target
->stub_tables()[0];
8079 if (stub_table
!= NULL
)
8081 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
;
8083 ent
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
8084 rela
.get_r_addend());
8086 ent
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
8087 rela
.get_r_addend());
8090 value
= stub_table
->stub_address() + ent
->off_
;
8091 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
8092 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->reloc_shdr
);
8093 size_t reloc_count
= shdr
.get_sh_size() / reloc_size
;
8096 && relnum
+ 1 < reloc_count
)
8098 Reltype
next_rela(preloc
+ reloc_size
);
8099 if (elfcpp::elf_r_type
<size
>(next_rela
.get_r_info())
8100 == elfcpp::R_PPC64_TOCSAVE
8101 && next_rela
.get_r_offset() == rela
.get_r_offset() + 4)
8104 localentry0
= ent
->localentry0_
;
8105 has_stub_value
= true;
8109 // We don't care too much about bogus debug references to
8110 // non-local functions, but otherwise there had better be a plt
8111 // call stub or global entry stub as appropriate.
8112 gold_assert(has_stub_value
|| !(os
->flags() & elfcpp::SHF_ALLOC
));
8115 if (r_type
== elfcpp::R_POWERPC_GOT16
8116 || r_type
== elfcpp::R_POWERPC_GOT16_LO
8117 || r_type
== elfcpp::R_POWERPC_GOT16_HI
8118 || r_type
== elfcpp::R_POWERPC_GOT16_HA
8119 || r_type
== elfcpp::R_PPC64_GOT16_DS
8120 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
8124 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
8125 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
8129 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
8130 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
8132 value
-= target
->got_section()->got_base_offset(object
);
8134 else if (r_type
== elfcpp::R_PPC64_TOC
)
8136 value
= (target
->got_section()->output_section()->address()
8137 + object
->toc_base_offset());
8139 else if (gsym
!= NULL
8140 && (r_type
== elfcpp::R_POWERPC_REL24
8141 || r_type
== elfcpp::R_PPC_PLTREL24
)
8146 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
8147 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
8148 bool can_plt_call
= localentry0
;
8149 if (!localentry0
&& rela
.get_r_offset() + 8 <= view_size
)
8151 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
8152 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
8155 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
8157 elfcpp::Swap
<32, big_endian
>::
8158 writeval(wv
+ 1, ld_2_1
+ target
->stk_toc());
8159 can_plt_call
= true;
8164 // If we don't have a branch and link followed by a nop,
8165 // we can't go via the plt because there is no place to
8166 // put a toc restoring instruction.
8167 // Unless we know we won't be returning.
8168 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
8169 can_plt_call
= true;
8173 // g++ as of 20130507 emits self-calls without a
8174 // following nop. This is arguably wrong since we have
8175 // conflicting information. On the one hand a global
8176 // symbol and on the other a local call sequence, but
8177 // don't error for this special case.
8178 // It isn't possible to cheaply verify we have exactly
8179 // such a call. Allow all calls to the same section.
8181 Address code
= value
;
8182 if (gsym
->source() == Symbol::FROM_OBJECT
8183 && gsym
->object() == object
)
8185 unsigned int dest_shndx
= 0;
8186 if (target
->abiversion() < 2)
8188 Address addend
= rela
.get_r_addend();
8189 code
= psymval
->value(object
, addend
);
8190 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
8191 &code
, &dest_shndx
);
8194 if (dest_shndx
== 0)
8195 dest_shndx
= gsym
->shndx(&is_ordinary
);
8196 ok
= dest_shndx
== relinfo
->data_shndx
;
8200 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8201 _("call lacks nop, can't restore toc; "
8202 "recompile with -fPIC"));
8208 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8209 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
8210 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
8211 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
8213 // First instruction of a global dynamic sequence, arg setup insn.
8214 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8215 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
8216 enum Got_type got_type
= GOT_TYPE_STANDARD
;
8217 if (tls_type
== tls::TLSOPT_NONE
)
8218 got_type
= GOT_TYPE_TLSGD
;
8219 else if (tls_type
== tls::TLSOPT_TO_IE
)
8220 got_type
= GOT_TYPE_TPREL
;
8221 if (got_type
!= GOT_TYPE_STANDARD
)
8225 gold_assert(gsym
->has_got_offset(got_type
));
8226 value
= gsym
->got_offset(got_type
);
8230 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
8231 value
= object
->local_got_offset(r_sym
, got_type
);
8233 value
-= target
->got_section()->got_base_offset(object
);
8235 if (tls_type
== tls::TLSOPT_TO_IE
)
8237 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8238 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
8240 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8241 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8242 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
8244 insn
|= 32 << 26; // lwz
8246 insn
|= 58 << 26; // ld
8247 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8249 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
8250 - elfcpp::R_POWERPC_GOT_TLSGD16
);
8252 else if (tls_type
== tls::TLSOPT_TO_LE
)
8254 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8255 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
8257 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8258 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8259 insn
&= (1 << 26) - (1 << 21); // extract rt
8264 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8265 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8266 value
= psymval
->value(object
, rela
.get_r_addend());
8270 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8272 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8273 r_type
= elfcpp::R_POWERPC_NONE
;
8277 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
8278 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
8279 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
8280 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
8282 // First instruction of a local dynamic sequence, arg setup insn.
8283 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
8284 if (tls_type
== tls::TLSOPT_NONE
)
8286 value
= target
->tlsld_got_offset();
8287 value
-= target
->got_section()->got_base_offset(object
);
8291 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
8292 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
8293 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
8295 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8296 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8297 insn
&= (1 << 26) - (1 << 21); // extract rt
8302 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8303 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8308 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8310 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8311 r_type
= elfcpp::R_POWERPC_NONE
;
8315 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
8316 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
8317 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
8318 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
8320 // Accesses relative to a local dynamic sequence address,
8321 // no optimisation here.
8324 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
8325 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
8329 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
8330 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
8332 value
-= target
->got_section()->got_base_offset(object
);
8334 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8335 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
8336 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
8337 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
8339 // First instruction of initial exec sequence.
8340 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8341 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
8342 if (tls_type
== tls::TLSOPT_NONE
)
8346 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
8347 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
8351 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
8352 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
8354 value
-= target
->got_section()->got_base_offset(object
);
8358 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
8359 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8360 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
8362 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8363 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8364 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
8369 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8370 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8371 value
= psymval
->value(object
, rela
.get_r_addend());
8375 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8377 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8378 r_type
= elfcpp::R_POWERPC_NONE
;
8382 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
8383 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
8385 // Second instruction of a global dynamic sequence,
8386 // the __tls_get_addr call
8387 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
8388 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8389 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
8390 if (tls_type
!= tls::TLSOPT_NONE
)
8392 if (tls_type
== tls::TLSOPT_TO_IE
)
8394 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8395 Insn insn
= add_3_3_13
;
8398 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8399 r_type
= elfcpp::R_POWERPC_NONE
;
8403 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8404 Insn insn
= addi_3_3
;
8405 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8406 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8408 value
= psymval
->value(object
, rela
.get_r_addend());
8410 this->skip_next_tls_get_addr_call();
8413 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
8414 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
8416 // Second instruction of a local dynamic sequence,
8417 // the __tls_get_addr call
8418 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
8419 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
8420 if (tls_type
== tls::TLSOPT_TO_LE
)
8422 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8423 Insn insn
= addi_3_3
;
8424 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8425 this->skip_next_tls_get_addr_call();
8426 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8431 else if (r_type
== elfcpp::R_POWERPC_TLS
)
8433 // Second instruction of an initial exec sequence
8434 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8435 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
8436 if (tls_type
== tls::TLSOPT_TO_LE
)
8438 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8439 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8440 unsigned int reg
= size
== 32 ? 2 : 13;
8441 insn
= at_tls_transform(insn
, reg
);
8442 gold_assert(insn
!= 0);
8443 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8444 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8446 value
= psymval
->value(object
, rela
.get_r_addend());
8449 else if (!has_stub_value
)
8452 if (!(size
== 32 && r_type
== elfcpp::R_PPC_PLTREL24
))
8453 addend
= rela
.get_r_addend();
8454 value
= psymval
->value(object
, addend
);
8455 if (size
== 64 && is_branch_reloc(r_type
))
8457 if (target
->abiversion() >= 2)
8460 value
+= object
->ppc64_local_entry_offset(gsym
);
8462 value
+= object
->ppc64_local_entry_offset(r_sym
);
8466 unsigned int dest_shndx
;
8467 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
8468 &value
, &dest_shndx
);
8471 Address max_branch_offset
= max_branch_delta(r_type
);
8472 if (max_branch_offset
!= 0
8473 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
8475 Stub_table
<size
, big_endian
>* stub_table
8476 = object
->stub_table(relinfo
->data_shndx
);
8477 if (stub_table
!= NULL
)
8479 Address off
= stub_table
->find_long_branch_entry(object
, value
);
8480 if (off
!= invalid_address
)
8482 value
= (stub_table
->stub_address() + stub_table
->plt_size()
8484 has_stub_value
= true;
8492 case elfcpp::R_PPC64_REL64
:
8493 case elfcpp::R_POWERPC_REL32
:
8494 case elfcpp::R_POWERPC_REL24
:
8495 case elfcpp::R_PPC_PLTREL24
:
8496 case elfcpp::R_PPC_LOCAL24PC
:
8497 case elfcpp::R_POWERPC_REL16
:
8498 case elfcpp::R_POWERPC_REL16_LO
:
8499 case elfcpp::R_POWERPC_REL16_HI
:
8500 case elfcpp::R_POWERPC_REL16_HA
:
8501 case elfcpp::R_POWERPC_REL16DX_HA
:
8502 case elfcpp::R_POWERPC_REL14
:
8503 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8504 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8508 case elfcpp::R_PPC64_TOC16
:
8509 case elfcpp::R_PPC64_TOC16_LO
:
8510 case elfcpp::R_PPC64_TOC16_HI
:
8511 case elfcpp::R_PPC64_TOC16_HA
:
8512 case elfcpp::R_PPC64_TOC16_DS
:
8513 case elfcpp::R_PPC64_TOC16_LO_DS
:
8514 // Subtract the TOC base address.
8515 value
-= (target
->got_section()->output_section()->address()
8516 + object
->toc_base_offset());
8519 case elfcpp::R_POWERPC_SECTOFF
:
8520 case elfcpp::R_POWERPC_SECTOFF_LO
:
8521 case elfcpp::R_POWERPC_SECTOFF_HI
:
8522 case elfcpp::R_POWERPC_SECTOFF_HA
:
8523 case elfcpp::R_PPC64_SECTOFF_DS
:
8524 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
8526 value
-= os
->address();
8529 case elfcpp::R_PPC64_TPREL16_DS
:
8530 case elfcpp::R_PPC64_TPREL16_LO_DS
:
8531 case elfcpp::R_PPC64_TPREL16_HIGH
:
8532 case elfcpp::R_PPC64_TPREL16_HIGHA
:
8534 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
8537 case elfcpp::R_POWERPC_TPREL16
:
8538 case elfcpp::R_POWERPC_TPREL16_LO
:
8539 case elfcpp::R_POWERPC_TPREL16_HI
:
8540 case elfcpp::R_POWERPC_TPREL16_HA
:
8541 case elfcpp::R_POWERPC_TPREL
:
8542 case elfcpp::R_PPC64_TPREL16_HIGHER
:
8543 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
8544 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
8545 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
8546 // tls symbol values are relative to tls_segment()->vaddr()
8550 case elfcpp::R_PPC64_DTPREL16_DS
:
8551 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
8552 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
8553 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
8554 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
8555 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
8557 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
8558 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
8561 case elfcpp::R_POWERPC_DTPREL16
:
8562 case elfcpp::R_POWERPC_DTPREL16_LO
:
8563 case elfcpp::R_POWERPC_DTPREL16_HI
:
8564 case elfcpp::R_POWERPC_DTPREL16_HA
:
8565 case elfcpp::R_POWERPC_DTPREL
:
8566 case elfcpp::R_PPC64_DTPREL16_HIGH
:
8567 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
8568 // tls symbol values are relative to tls_segment()->vaddr()
8569 value
-= dtp_offset
;
8572 case elfcpp::R_PPC64_ADDR64_LOCAL
:
8574 value
+= object
->ppc64_local_entry_offset(gsym
);
8576 value
+= object
->ppc64_local_entry_offset(r_sym
);
8583 Insn branch_bit
= 0;
8586 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
8587 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8588 branch_bit
= 1 << 21;
8590 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
8591 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8593 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8594 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8597 if (this->is_isa_v2
)
8599 // Set 'a' bit. This is 0b00010 in BO field for branch
8600 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
8601 // for branch on CTR insns (BO == 1a00t or 1a01t).
8602 if ((insn
& (0x14 << 21)) == (0x04 << 21))
8604 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
8611 // Invert 'y' bit if not the default.
8612 if (static_cast<Signed_address
>(value
) < 0)
8615 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8630 // Multi-instruction sequences that access the GOT/TOC can
8631 // be optimized, eg.
8632 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
8633 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
8635 // addis ra,r2,0; addi rb,ra,x@toc@l;
8636 // to nop; addi rb,r2,x@toc;
8637 // FIXME: the @got sequence shown above is not yet
8638 // optimized. Note that gcc as of 2017-01-07 doesn't use
8639 // the ELF @got relocs except for TLS, instead using the
8640 // PowerOpen variant of a compiler managed GOT (called TOC).
8641 // The PowerOpen TOC sequence equivalent to the first
8642 // example is optimized.
8643 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
8644 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
8645 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
8646 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
8647 case elfcpp::R_POWERPC_GOT16_HA
:
8648 case elfcpp::R_PPC64_TOC16_HA
:
8649 if (parameters
->options().toc_optimize())
8651 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8652 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8653 if (r_type
== elfcpp::R_PPC64_TOC16_HA
8654 && object
->make_toc_relative(target
, &value
))
8656 gold_assert((insn
& ((0x3f << 26) | 0x1f << 16))
8657 == ((15u << 26) | (2 << 16)));
8659 if (((insn
& ((0x3f << 26) | 0x1f << 16))
8660 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8661 && value
+ 0x8000 < 0x10000)
8663 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
8669 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
8670 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
8671 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
8672 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
8673 case elfcpp::R_POWERPC_GOT16_LO
:
8674 case elfcpp::R_PPC64_GOT16_LO_DS
:
8675 case elfcpp::R_PPC64_TOC16_LO
:
8676 case elfcpp::R_PPC64_TOC16_LO_DS
:
8677 if (parameters
->options().toc_optimize())
8679 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8680 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8681 bool changed
= false;
8682 if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
8683 && object
->make_toc_relative(target
, &value
))
8685 gold_assert ((insn
& (0x3f << 26)) == 58u << 26 /* ld */);
8686 insn
^= (14u << 26) ^ (58u << 26);
8687 r_type
= elfcpp::R_PPC64_TOC16_LO
;
8690 if (ok_lo_toc_insn(insn
, r_type
)
8691 && value
+ 0x8000 < 0x10000)
8693 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
8695 // Transform addic to addi when we change reg.
8696 insn
&= ~((0x3f << 26) | (0x1f << 16));
8697 insn
|= (14u << 26) | (2 << 16);
8701 insn
&= ~(0x1f << 16);
8707 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8711 case elfcpp::R_PPC64_ENTRY
:
8712 value
= (target
->got_section()->output_section()->address()
8713 + object
->toc_base_offset());
8714 if (value
+ 0x80008000 <= 0xffffffff
8715 && !parameters
->options().output_is_position_independent())
8717 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8718 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8719 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
8721 if ((insn1
& ~0xfffc) == ld_2_12
8722 && insn2
== add_2_2_12
)
8724 insn1
= lis_2
+ ha(value
);
8725 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
8726 insn2
= addi_2_2
+ l(value
);
8727 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
8734 if (value
+ 0x80008000 <= 0xffffffff)
8736 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8737 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8738 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
8740 if ((insn1
& ~0xfffc) == ld_2_12
8741 && insn2
== add_2_2_12
)
8743 insn1
= addis_2_12
+ ha(value
);
8744 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
8745 insn2
= addi_2_2
+ l(value
);
8746 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
8753 case elfcpp::R_POWERPC_REL16_LO
:
8754 // If we are generating a non-PIC executable, edit
8755 // 0: addis 2,12,.TOC.-0b@ha
8756 // addi 2,2,.TOC.-0b@l
8757 // used by ELFv2 global entry points to set up r2, to
8760 // if .TOC. is in range. */
8761 if (value
+ address
- 4 + 0x80008000 <= 0xffffffff
8764 && target
->abiversion() >= 2
8765 && !parameters
->options().output_is_position_independent()
8766 && rela
.get_r_addend() == d_offset
+ 4
8768 && strcmp(gsym
->name(), ".TOC.") == 0)
8770 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
8771 Reltype
prev_rela(preloc
- reloc_size
);
8772 if ((prev_rela
.get_r_info()
8773 == elfcpp::elf_r_info
<size
>(r_sym
,
8774 elfcpp::R_POWERPC_REL16_HA
))
8775 && prev_rela
.get_r_offset() + 4 == rela
.get_r_offset()
8776 && prev_rela
.get_r_addend() + 4 == rela
.get_r_addend())
8778 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8779 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
- 1);
8780 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8782 if ((insn1
& 0xffff0000) == addis_2_12
8783 && (insn2
& 0xffff0000) == addi_2_2
)
8785 insn1
= lis_2
+ ha(value
+ address
- 4);
8786 elfcpp::Swap
<32, big_endian
>::writeval(iview
- 1, insn1
);
8787 insn2
= addi_2_2
+ l(value
+ address
- 4);
8788 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn2
);
8791 relinfo
->rr
->set_strategy(relnum
- 1,
8792 Relocatable_relocs::RELOC_SPECIAL
);
8793 relinfo
->rr
->set_strategy(relnum
,
8794 Relocatable_relocs::RELOC_SPECIAL
);
8804 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
8805 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->data_shdr
);
8808 case elfcpp::R_POWERPC_ADDR32
:
8809 case elfcpp::R_POWERPC_UADDR32
:
8811 overflow
= Reloc::CHECK_BITFIELD
;
8814 case elfcpp::R_POWERPC_REL32
:
8815 case elfcpp::R_POWERPC_REL16DX_HA
:
8817 overflow
= Reloc::CHECK_SIGNED
;
8820 case elfcpp::R_POWERPC_UADDR16
:
8821 overflow
= Reloc::CHECK_BITFIELD
;
8824 case elfcpp::R_POWERPC_ADDR16
:
8825 // We really should have three separate relocations,
8826 // one for 16-bit data, one for insns with 16-bit signed fields,
8827 // and one for insns with 16-bit unsigned fields.
8828 overflow
= Reloc::CHECK_BITFIELD
;
8829 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
8830 overflow
= Reloc::CHECK_LOW_INSN
;
8833 case elfcpp::R_POWERPC_ADDR16_HI
:
8834 case elfcpp::R_POWERPC_ADDR16_HA
:
8835 case elfcpp::R_POWERPC_GOT16_HI
:
8836 case elfcpp::R_POWERPC_GOT16_HA
:
8837 case elfcpp::R_POWERPC_PLT16_HI
:
8838 case elfcpp::R_POWERPC_PLT16_HA
:
8839 case elfcpp::R_POWERPC_SECTOFF_HI
:
8840 case elfcpp::R_POWERPC_SECTOFF_HA
:
8841 case elfcpp::R_PPC64_TOC16_HI
:
8842 case elfcpp::R_PPC64_TOC16_HA
:
8843 case elfcpp::R_PPC64_PLTGOT16_HI
:
8844 case elfcpp::R_PPC64_PLTGOT16_HA
:
8845 case elfcpp::R_POWERPC_TPREL16_HI
:
8846 case elfcpp::R_POWERPC_TPREL16_HA
:
8847 case elfcpp::R_POWERPC_DTPREL16_HI
:
8848 case elfcpp::R_POWERPC_DTPREL16_HA
:
8849 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
8850 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
8851 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
8852 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
8853 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
8854 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
8855 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
8856 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
8857 case elfcpp::R_POWERPC_REL16_HI
:
8858 case elfcpp::R_POWERPC_REL16_HA
:
8860 overflow
= Reloc::CHECK_HIGH_INSN
;
8863 case elfcpp::R_POWERPC_REL16
:
8864 case elfcpp::R_PPC64_TOC16
:
8865 case elfcpp::R_POWERPC_GOT16
:
8866 case elfcpp::R_POWERPC_SECTOFF
:
8867 case elfcpp::R_POWERPC_TPREL16
:
8868 case elfcpp::R_POWERPC_DTPREL16
:
8869 case elfcpp::R_POWERPC_GOT_TLSGD16
:
8870 case elfcpp::R_POWERPC_GOT_TLSLD16
:
8871 case elfcpp::R_POWERPC_GOT_TPREL16
:
8872 case elfcpp::R_POWERPC_GOT_DTPREL16
:
8873 overflow
= Reloc::CHECK_LOW_INSN
;
8876 case elfcpp::R_POWERPC_ADDR24
:
8877 case elfcpp::R_POWERPC_ADDR14
:
8878 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
8879 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
8880 case elfcpp::R_PPC64_ADDR16_DS
:
8881 case elfcpp::R_POWERPC_REL24
:
8882 case elfcpp::R_PPC_PLTREL24
:
8883 case elfcpp::R_PPC_LOCAL24PC
:
8884 case elfcpp::R_PPC64_TPREL16_DS
:
8885 case elfcpp::R_PPC64_DTPREL16_DS
:
8886 case elfcpp::R_PPC64_TOC16_DS
:
8887 case elfcpp::R_PPC64_GOT16_DS
:
8888 case elfcpp::R_PPC64_SECTOFF_DS
:
8889 case elfcpp::R_POWERPC_REL14
:
8890 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8891 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8892 overflow
= Reloc::CHECK_SIGNED
;
8896 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8899 if (overflow
== Reloc::CHECK_LOW_INSN
8900 || overflow
== Reloc::CHECK_HIGH_INSN
)
8902 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8904 if ((insn
& (0x3f << 26)) == 10u << 26 /* cmpli */)
8905 overflow
= Reloc::CHECK_BITFIELD
;
8906 else if (overflow
== Reloc::CHECK_LOW_INSN
8907 ? ((insn
& (0x3f << 26)) == 28u << 26 /* andi */
8908 || (insn
& (0x3f << 26)) == 24u << 26 /* ori */
8909 || (insn
& (0x3f << 26)) == 26u << 26 /* xori */)
8910 : ((insn
& (0x3f << 26)) == 29u << 26 /* andis */
8911 || (insn
& (0x3f << 26)) == 25u << 26 /* oris */
8912 || (insn
& (0x3f << 26)) == 27u << 26 /* xoris */))
8913 overflow
= Reloc::CHECK_UNSIGNED
;
8915 overflow
= Reloc::CHECK_SIGNED
;
8918 bool maybe_dq_reloc
= false;
8919 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
8920 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
8923 case elfcpp::R_POWERPC_NONE
:
8924 case elfcpp::R_POWERPC_TLS
:
8925 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
8926 case elfcpp::R_POWERPC_GNU_VTENTRY
:
8929 case elfcpp::R_PPC64_ADDR64
:
8930 case elfcpp::R_PPC64_REL64
:
8931 case elfcpp::R_PPC64_TOC
:
8932 case elfcpp::R_PPC64_ADDR64_LOCAL
:
8933 Reloc::addr64(view
, value
);
8936 case elfcpp::R_POWERPC_TPREL
:
8937 case elfcpp::R_POWERPC_DTPREL
:
8939 Reloc::addr64(view
, value
);
8941 status
= Reloc::addr32(view
, value
, overflow
);
8944 case elfcpp::R_PPC64_UADDR64
:
8945 Reloc::addr64_u(view
, value
);
8948 case elfcpp::R_POWERPC_ADDR32
:
8949 status
= Reloc::addr32(view
, value
, overflow
);
8952 case elfcpp::R_POWERPC_REL32
:
8953 case elfcpp::R_POWERPC_UADDR32
:
8954 status
= Reloc::addr32_u(view
, value
, overflow
);
8957 case elfcpp::R_POWERPC_ADDR24
:
8958 case elfcpp::R_POWERPC_REL24
:
8959 case elfcpp::R_PPC_PLTREL24
:
8960 case elfcpp::R_PPC_LOCAL24PC
:
8961 status
= Reloc::addr24(view
, value
, overflow
);
8964 case elfcpp::R_POWERPC_GOT_DTPREL16
:
8965 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
8966 case elfcpp::R_POWERPC_GOT_TPREL16
:
8967 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
8970 // On ppc64 these are all ds form
8971 maybe_dq_reloc
= true;
8975 case elfcpp::R_POWERPC_ADDR16
:
8976 case elfcpp::R_POWERPC_REL16
:
8977 case elfcpp::R_PPC64_TOC16
:
8978 case elfcpp::R_POWERPC_GOT16
:
8979 case elfcpp::R_POWERPC_SECTOFF
:
8980 case elfcpp::R_POWERPC_TPREL16
:
8981 case elfcpp::R_POWERPC_DTPREL16
:
8982 case elfcpp::R_POWERPC_GOT_TLSGD16
:
8983 case elfcpp::R_POWERPC_GOT_TLSLD16
:
8984 case elfcpp::R_POWERPC_ADDR16_LO
:
8985 case elfcpp::R_POWERPC_REL16_LO
:
8986 case elfcpp::R_PPC64_TOC16_LO
:
8987 case elfcpp::R_POWERPC_GOT16_LO
:
8988 case elfcpp::R_POWERPC_SECTOFF_LO
:
8989 case elfcpp::R_POWERPC_TPREL16_LO
:
8990 case elfcpp::R_POWERPC_DTPREL16_LO
:
8991 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
8992 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
8994 status
= Reloc::addr16(view
, value
, overflow
);
8996 maybe_dq_reloc
= true;
8999 case elfcpp::R_POWERPC_UADDR16
:
9000 status
= Reloc::addr16_u(view
, value
, overflow
);
9003 case elfcpp::R_PPC64_ADDR16_HIGH
:
9004 case elfcpp::R_PPC64_TPREL16_HIGH
:
9005 case elfcpp::R_PPC64_DTPREL16_HIGH
:
9007 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
9010 case elfcpp::R_POWERPC_ADDR16_HI
:
9011 case elfcpp::R_POWERPC_REL16_HI
:
9012 case elfcpp::R_PPC64_TOC16_HI
:
9013 case elfcpp::R_POWERPC_GOT16_HI
:
9014 case elfcpp::R_POWERPC_SECTOFF_HI
:
9015 case elfcpp::R_POWERPC_TPREL16_HI
:
9016 case elfcpp::R_POWERPC_DTPREL16_HI
:
9017 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
9018 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
9019 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
9020 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
9021 Reloc::addr16_hi(view
, value
);
9024 case elfcpp::R_PPC64_ADDR16_HIGHA
:
9025 case elfcpp::R_PPC64_TPREL16_HIGHA
:
9026 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
9028 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
9031 case elfcpp::R_POWERPC_ADDR16_HA
:
9032 case elfcpp::R_POWERPC_REL16_HA
:
9033 case elfcpp::R_PPC64_TOC16_HA
:
9034 case elfcpp::R_POWERPC_GOT16_HA
:
9035 case elfcpp::R_POWERPC_SECTOFF_HA
:
9036 case elfcpp::R_POWERPC_TPREL16_HA
:
9037 case elfcpp::R_POWERPC_DTPREL16_HA
:
9038 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
9039 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
9040 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
9041 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
9042 Reloc::addr16_ha(view
, value
);
9045 case elfcpp::R_POWERPC_REL16DX_HA
:
9046 status
= Reloc::addr16dx_ha(view
, value
, overflow
);
9049 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
9051 // R_PPC_EMB_NADDR16_LO
9054 case elfcpp::R_PPC64_ADDR16_HIGHER
:
9055 case elfcpp::R_PPC64_TPREL16_HIGHER
:
9056 Reloc::addr16_hi2(view
, value
);
9059 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
9061 // R_PPC_EMB_NADDR16_HI
9064 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
9065 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
9066 Reloc::addr16_ha2(view
, value
);
9069 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
9071 // R_PPC_EMB_NADDR16_HA
9074 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
9075 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
9076 Reloc::addr16_hi3(view
, value
);
9079 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
9084 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
9085 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
9086 Reloc::addr16_ha3(view
, value
);
9089 case elfcpp::R_PPC64_DTPREL16_DS
:
9090 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
9092 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
9095 case elfcpp::R_PPC64_TPREL16_DS
:
9096 case elfcpp::R_PPC64_TPREL16_LO_DS
:
9098 // R_PPC_TLSGD, R_PPC_TLSLD
9101 case elfcpp::R_PPC64_ADDR16_DS
:
9102 case elfcpp::R_PPC64_ADDR16_LO_DS
:
9103 case elfcpp::R_PPC64_TOC16_DS
:
9104 case elfcpp::R_PPC64_TOC16_LO_DS
:
9105 case elfcpp::R_PPC64_GOT16_DS
:
9106 case elfcpp::R_PPC64_GOT16_LO_DS
:
9107 case elfcpp::R_PPC64_SECTOFF_DS
:
9108 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
9109 maybe_dq_reloc
= true;
9112 case elfcpp::R_POWERPC_ADDR14
:
9113 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
9114 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
9115 case elfcpp::R_POWERPC_REL14
:
9116 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
9117 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
9118 status
= Reloc::addr14(view
, value
, overflow
);
9121 case elfcpp::R_POWERPC_COPY
:
9122 case elfcpp::R_POWERPC_GLOB_DAT
:
9123 case elfcpp::R_POWERPC_JMP_SLOT
:
9124 case elfcpp::R_POWERPC_RELATIVE
:
9125 case elfcpp::R_POWERPC_DTPMOD
:
9126 case elfcpp::R_PPC64_JMP_IREL
:
9127 case elfcpp::R_POWERPC_IRELATIVE
:
9128 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
9129 _("unexpected reloc %u in object file"),
9133 case elfcpp::R_PPC64_TOCSAVE
:
9139 Symbol_location loc
;
9140 loc
.object
= relinfo
->object
;
9141 loc
.shndx
= relinfo
->data_shndx
;
9142 loc
.offset
= rela
.get_r_offset();
9143 Tocsave_loc::const_iterator p
= target
->tocsave_loc().find(loc
);
9144 if (p
!= target
->tocsave_loc().end())
9146 // If we've generated plt calls using this tocsave, then
9147 // the nop needs to be changed to save r2.
9148 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9149 if (elfcpp::Swap
<32, big_endian
>::readval(iview
) == nop
)
9150 elfcpp::Swap
<32, big_endian
>::
9151 writeval(iview
, std_2_1
+ target
->stk_toc());
9156 case elfcpp::R_PPC_EMB_SDA2I16
:
9157 case elfcpp::R_PPC_EMB_SDA2REL
:
9160 // R_PPC64_TLSGD, R_PPC64_TLSLD
9163 case elfcpp::R_POWERPC_PLT32
:
9164 case elfcpp::R_POWERPC_PLTREL32
:
9165 case elfcpp::R_POWERPC_PLT16_LO
:
9166 case elfcpp::R_POWERPC_PLT16_HI
:
9167 case elfcpp::R_POWERPC_PLT16_HA
:
9168 case elfcpp::R_PPC_SDAREL16
:
9169 case elfcpp::R_POWERPC_ADDR30
:
9170 case elfcpp::R_PPC64_PLT64
:
9171 case elfcpp::R_PPC64_PLTREL64
:
9172 case elfcpp::R_PPC64_PLTGOT16
:
9173 case elfcpp::R_PPC64_PLTGOT16_LO
:
9174 case elfcpp::R_PPC64_PLTGOT16_HI
:
9175 case elfcpp::R_PPC64_PLTGOT16_HA
:
9176 case elfcpp::R_PPC64_PLT16_LO_DS
:
9177 case elfcpp::R_PPC64_PLTGOT16_DS
:
9178 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
9179 case elfcpp::R_PPC_EMB_RELSDA
:
9180 case elfcpp::R_PPC_TOC16
:
9183 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
9184 _("unsupported reloc %u"),
9192 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9194 if ((insn
& (0x3f << 26)) == 56u << 26 /* lq */
9195 || ((insn
& (0x3f << 26)) == (61u << 26) /* lxv, stxv */
9196 && (insn
& 3) == 1))
9197 status
= Reloc::addr16_dq(view
, value
, overflow
);
9199 || (insn
& (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
9200 || (insn
& (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
9201 || (insn
& (0x3f << 26)) == 57u << 26 /* lfdp */
9202 || (insn
& (0x3f << 26)) == 61u << 26 /* stfdp */)
9203 status
= Reloc::addr16_ds(view
, value
, overflow
);
9205 status
= Reloc::addr16(view
, value
, overflow
);
9208 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
9211 && gsym
->is_undefined()
9212 && is_branch_reloc(r_type
))))
9214 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
9215 _("relocation overflow"));
9217 gold_info(_("try relinking with a smaller --stub-group-size"));
9223 // Relocate section data.
9225 template<int size
, bool big_endian
>
9227 Target_powerpc
<size
, big_endian
>::relocate_section(
9228 const Relocate_info
<size
, big_endian
>* relinfo
,
9229 unsigned int sh_type
,
9230 const unsigned char* prelocs
,
9232 Output_section
* output_section
,
9233 bool needs_special_offset_handling
,
9234 unsigned char* view
,
9236 section_size_type view_size
,
9237 const Reloc_symbol_changes
* reloc_symbol_changes
)
9239 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
9240 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
9241 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
9242 Powerpc_comdat_behavior
;
9243 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
9246 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9248 gold::relocate_section
<size
, big_endian
, Powerpc
, Powerpc_relocate
,
9249 Powerpc_comdat_behavior
, Classify_reloc
>(
9255 needs_special_offset_handling
,
9259 reloc_symbol_changes
);
9262 template<int size
, bool big_endian
>
9263 class Powerpc_scan_relocatable_reloc
9266 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
9267 static const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
9268 static const int sh_type
= elfcpp::SHT_RELA
;
9270 // Return the symbol referred to by the relocation.
9271 static inline unsigned int
9272 get_r_sym(const Reltype
* reloc
)
9273 { return elfcpp::elf_r_sym
<size
>(reloc
->get_r_info()); }
9275 // Return the type of the relocation.
9276 static inline unsigned int
9277 get_r_type(const Reltype
* reloc
)
9278 { return elfcpp::elf_r_type
<size
>(reloc
->get_r_info()); }
9280 // Return the strategy to use for a local symbol which is not a
9281 // section symbol, given the relocation type.
9282 inline Relocatable_relocs::Reloc_strategy
9283 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
9285 if (r_type
== 0 && r_sym
== 0)
9286 return Relocatable_relocs::RELOC_DISCARD
;
9287 return Relocatable_relocs::RELOC_COPY
;
9290 // Return the strategy to use for a local symbol which is a section
9291 // symbol, given the relocation type.
9292 inline Relocatable_relocs::Reloc_strategy
9293 local_section_strategy(unsigned int, Relobj
*)
9295 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
9298 // Return the strategy to use for a global symbol, given the
9299 // relocation type, the object, and the symbol index.
9300 inline Relocatable_relocs::Reloc_strategy
9301 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
9303 if (r_type
== elfcpp::R_PPC_PLTREL24
)
9304 return Relocatable_relocs::RELOC_SPECIAL
;
9305 return Relocatable_relocs::RELOC_COPY
;
9309 // Scan the relocs during a relocatable link.
9311 template<int size
, bool big_endian
>
9313 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
9314 Symbol_table
* symtab
,
9316 Sized_relobj_file
<size
, big_endian
>* object
,
9317 unsigned int data_shndx
,
9318 unsigned int sh_type
,
9319 const unsigned char* prelocs
,
9321 Output_section
* output_section
,
9322 bool needs_special_offset_handling
,
9323 size_t local_symbol_count
,
9324 const unsigned char* plocal_symbols
,
9325 Relocatable_relocs
* rr
)
9327 typedef Powerpc_scan_relocatable_reloc
<size
, big_endian
> Scan_strategy
;
9329 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9331 gold::scan_relocatable_relocs
<size
, big_endian
, Scan_strategy
>(
9339 needs_special_offset_handling
,
9345 // Scan the relocs for --emit-relocs.
9347 template<int size
, bool big_endian
>
9349 Target_powerpc
<size
, big_endian
>::emit_relocs_scan(
9350 Symbol_table
* symtab
,
9352 Sized_relobj_file
<size
, big_endian
>* object
,
9353 unsigned int data_shndx
,
9354 unsigned int sh_type
,
9355 const unsigned char* prelocs
,
9357 Output_section
* output_section
,
9358 bool needs_special_offset_handling
,
9359 size_t local_symbol_count
,
9360 const unsigned char* plocal_syms
,
9361 Relocatable_relocs
* rr
)
9363 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
9365 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
9366 Emit_relocs_strategy
;
9368 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9370 gold::scan_relocatable_relocs
<size
, big_endian
, Emit_relocs_strategy
>(
9378 needs_special_offset_handling
,
9384 // Emit relocations for a section.
9385 // This is a modified version of the function by the same name in
9386 // target-reloc.h. Using relocate_special_relocatable for
9387 // R_PPC_PLTREL24 would require duplication of the entire body of the
9388 // loop, so we may as well duplicate the whole thing.
9390 template<int size
, bool big_endian
>
9392 Target_powerpc
<size
, big_endian
>::relocate_relocs(
9393 const Relocate_info
<size
, big_endian
>* relinfo
,
9394 unsigned int sh_type
,
9395 const unsigned char* prelocs
,
9397 Output_section
* output_section
,
9398 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
9400 Address view_address
,
9402 unsigned char* reloc_view
,
9403 section_size_type reloc_view_size
)
9405 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9407 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
9408 typedef typename
elfcpp::Rela_write
<size
, big_endian
> Reltype_write
;
9409 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
9410 // Offset from start of insn to d-field reloc.
9411 const int d_offset
= big_endian
? 2 : 0;
9413 Powerpc_relobj
<size
, big_endian
>* const object
9414 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
9415 const unsigned int local_count
= object
->local_symbol_count();
9416 unsigned int got2_shndx
= object
->got2_shndx();
9417 Address got2_addend
= 0;
9418 if (got2_shndx
!= 0)
9420 got2_addend
= object
->get_output_section_offset(got2_shndx
);
9421 gold_assert(got2_addend
!= invalid_address
);
9424 unsigned char* pwrite
= reloc_view
;
9425 bool zap_next
= false;
9426 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
9428 Relocatable_relocs::Reloc_strategy strategy
= relinfo
->rr
->strategy(i
);
9429 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
9432 Reltype
reloc(prelocs
);
9433 Reltype_write
reloc_write(pwrite
);
9435 Address offset
= reloc
.get_r_offset();
9436 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
9437 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
9438 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
9439 const unsigned int orig_r_sym
= r_sym
;
9440 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
9441 = reloc
.get_r_addend();
9442 const Symbol
* gsym
= NULL
;
9446 // We could arrange to discard these and other relocs for
9447 // tls optimised sequences in the strategy methods, but for
9448 // now do as BFD ld does.
9449 r_type
= elfcpp::R_POWERPC_NONE
;
9453 // Get the new symbol index.
9454 Output_section
* os
= NULL
;
9455 if (r_sym
< local_count
)
9459 case Relocatable_relocs::RELOC_COPY
:
9460 case Relocatable_relocs::RELOC_SPECIAL
:
9463 r_sym
= object
->symtab_index(r_sym
);
9464 gold_assert(r_sym
!= -1U);
9468 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
9470 // We are adjusting a section symbol. We need to find
9471 // the symbol table index of the section symbol for
9472 // the output section corresponding to input section
9473 // in which this symbol is defined.
9474 gold_assert(r_sym
< local_count
);
9476 unsigned int shndx
=
9477 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
9478 gold_assert(is_ordinary
);
9479 os
= object
->output_section(shndx
);
9480 gold_assert(os
!= NULL
);
9481 gold_assert(os
->needs_symtab_index());
9482 r_sym
= os
->symtab_index();
9492 gsym
= object
->global_symbol(r_sym
);
9493 gold_assert(gsym
!= NULL
);
9494 if (gsym
->is_forwarder())
9495 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
9497 gold_assert(gsym
->has_symtab_index());
9498 r_sym
= gsym
->symtab_index();
9501 // Get the new offset--the location in the output section where
9502 // this relocation should be applied.
9503 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
9504 offset
+= offset_in_output_section
;
9507 section_offset_type sot_offset
=
9508 convert_types
<section_offset_type
, Address
>(offset
);
9509 section_offset_type new_sot_offset
=
9510 output_section
->output_offset(object
, relinfo
->data_shndx
,
9512 gold_assert(new_sot_offset
!= -1);
9513 offset
= new_sot_offset
;
9516 // In an object file, r_offset is an offset within the section.
9517 // In an executable or dynamic object, generated by
9518 // --emit-relocs, r_offset is an absolute address.
9519 if (!parameters
->options().relocatable())
9521 offset
+= view_address
;
9522 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
9523 offset
-= offset_in_output_section
;
9526 // Handle the reloc addend based on the strategy.
9527 if (strategy
== Relocatable_relocs::RELOC_COPY
)
9529 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
9531 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
9532 gold_assert(os
!= NULL
);
9533 addend
= psymval
->value(object
, addend
) - os
->address();
9535 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
9539 if (addend
>= 32768)
9540 addend
+= got2_addend
;
9542 else if (r_type
== elfcpp::R_POWERPC_REL16_HA
)
9544 r_type
= elfcpp::R_POWERPC_ADDR16_HA
;
9547 else if (r_type
== elfcpp::R_POWERPC_REL16_LO
)
9549 r_type
= elfcpp::R_POWERPC_ADDR16_LO
;
9550 addend
-= d_offset
+ 4;
9556 if (!parameters
->options().relocatable())
9558 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
9559 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
9560 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
9561 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
9563 // First instruction of a global dynamic sequence,
9565 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9566 switch (this->optimize_tls_gd(final
))
9568 case tls::TLSOPT_TO_IE
:
9569 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
9570 - elfcpp::R_POWERPC_GOT_TLSGD16
);
9572 case tls::TLSOPT_TO_LE
:
9573 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
9574 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
9575 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9578 r_type
= elfcpp::R_POWERPC_NONE
;
9586 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
9587 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
9588 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
9589 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
9591 // First instruction of a local dynamic sequence,
9593 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
9595 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
9596 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
9598 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9599 const Output_section
* os
= relinfo
->layout
->tls_segment()
9601 gold_assert(os
!= NULL
);
9602 gold_assert(os
->needs_symtab_index());
9603 r_sym
= os
->symtab_index();
9604 addend
= dtp_offset
;
9608 r_type
= elfcpp::R_POWERPC_NONE
;
9613 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
9614 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
9615 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
9616 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
9618 // First instruction of initial exec sequence.
9619 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9620 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
9622 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
9623 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
9624 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9627 r_type
= elfcpp::R_POWERPC_NONE
;
9632 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
9633 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
9635 // Second instruction of a global dynamic sequence,
9636 // the __tls_get_addr call
9637 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9638 switch (this->optimize_tls_gd(final
))
9640 case tls::TLSOPT_TO_IE
:
9641 r_type
= elfcpp::R_POWERPC_NONE
;
9644 case tls::TLSOPT_TO_LE
:
9645 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9653 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
9654 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
9656 // Second instruction of a local dynamic sequence,
9657 // the __tls_get_addr call
9658 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
9660 const Output_section
* os
= relinfo
->layout
->tls_segment()
9662 gold_assert(os
!= NULL
);
9663 gold_assert(os
->needs_symtab_index());
9664 r_sym
= os
->symtab_index();
9665 addend
= dtp_offset
;
9666 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9671 else if (r_type
== elfcpp::R_POWERPC_TLS
)
9673 // Second instruction of an initial exec sequence
9674 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9675 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
9677 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9683 reloc_write
.put_r_offset(offset
);
9684 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
9685 reloc_write
.put_r_addend(addend
);
9687 pwrite
+= reloc_size
;
9690 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
9691 == reloc_view_size
);
9694 // Return the value to use for a dynamic symbol which requires special
9695 // treatment. This is how we support equality comparisons of function
9696 // pointers across shared library boundaries, as described in the
9697 // processor specific ABI supplement.
9699 template<int size
, bool big_endian
>
9701 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
9705 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
9706 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
9707 p
!= this->stub_tables_
.end();
9710 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
9711 = (*p
)->find_plt_call_entry(gsym
);
9713 return (*p
)->stub_address() + ent
->off_
;
9716 else if (this->abiversion() >= 2)
9718 Address off
= this->glink_section()->find_global_entry(gsym
);
9719 if (off
!= invalid_address
)
9720 return this->glink_section()->global_entry_address() + off
;
9725 // Return the PLT address to use for a local symbol.
9726 template<int size
, bool big_endian
>
9728 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
9729 const Relobj
* object
,
9730 unsigned int symndx
) const
9734 const Sized_relobj
<size
, big_endian
>* relobj
9735 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
9736 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
9737 p
!= this->stub_tables_
.end();
9740 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
9741 = (*p
)->find_plt_call_entry(relobj
->sized_relobj(), symndx
);
9743 return (*p
)->stub_address() + ent
->off_
;
9749 // Return the PLT address to use for a global symbol.
9750 template<int size
, bool big_endian
>
9752 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
9753 const Symbol
* gsym
) const
9757 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
9758 p
!= this->stub_tables_
.end();
9761 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
9762 = (*p
)->find_plt_call_entry(gsym
);
9764 return (*p
)->stub_address() + ent
->off_
;
9767 else if (this->abiversion() >= 2)
9769 Address off
= this->glink_section()->find_global_entry(gsym
);
9770 if (off
!= invalid_address
)
9771 return this->glink_section()->global_entry_address() + off
;
9776 // Return the offset to use for the GOT_INDX'th got entry which is
9777 // for a local tls symbol specified by OBJECT, SYMNDX.
9778 template<int size
, bool big_endian
>
9780 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
9781 const Relobj
* object
,
9782 unsigned int symndx
,
9783 unsigned int got_indx
) const
9785 const Powerpc_relobj
<size
, big_endian
>* ppc_object
9786 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
9787 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
9789 for (Got_type got_type
= GOT_TYPE_TLSGD
;
9790 got_type
<= GOT_TYPE_TPREL
;
9791 got_type
= Got_type(got_type
+ 1))
9792 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
9794 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
9795 if (got_type
== GOT_TYPE_TLSGD
)
9797 if (off
== got_indx
* (size
/ 8))
9799 if (got_type
== GOT_TYPE_TPREL
)
9809 // Return the offset to use for the GOT_INDX'th got entry which is
9810 // for global tls symbol GSYM.
9811 template<int size
, bool big_endian
>
9813 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
9815 unsigned int got_indx
) const
9817 if (gsym
->type() == elfcpp::STT_TLS
)
9819 for (Got_type got_type
= GOT_TYPE_TLSGD
;
9820 got_type
<= GOT_TYPE_TPREL
;
9821 got_type
= Got_type(got_type
+ 1))
9822 if (gsym
->has_got_offset(got_type
))
9824 unsigned int off
= gsym
->got_offset(got_type
);
9825 if (got_type
== GOT_TYPE_TLSGD
)
9827 if (off
== got_indx
* (size
/ 8))
9829 if (got_type
== GOT_TYPE_TPREL
)
9839 // The selector for powerpc object files.
9841 template<int size
, bool big_endian
>
9842 class Target_selector_powerpc
: public Target_selector
9845 Target_selector_powerpc()
9846 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
9849 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
9850 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
9852 ? (big_endian
? "elf64ppc" : "elf64lppc")
9853 : (big_endian
? "elf32ppc" : "elf32lppc")))
9857 do_instantiate_target()
9858 { return new Target_powerpc
<size
, big_endian
>(); }
9861 Target_selector_powerpc
<32, true> target_selector_ppc32
;
9862 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
9863 Target_selector_powerpc
<64, true> target_selector_ppc64
;
9864 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
9866 // Instantiate these constants for -O0
9867 template<int size
, bool big_endian
>
9868 const int Output_data_glink
<size
, big_endian
>::pltresolve_size
;
9869 template<int size
, bool big_endian
>
9870 const typename Output_data_glink
<size
, big_endian
>::Address
9871 Output_data_glink
<size
, big_endian
>::invalid_address
;
9872 template<int size
, bool big_endian
>
9873 const typename Stub_table
<size
, big_endian
>::Address
9874 Stub_table
<size
, big_endian
>::invalid_address
;
9875 template<int size
, bool big_endian
>
9876 const typename Target_powerpc
<size
, big_endian
>::Address
9877 Target_powerpc
<size
, big_endian
>::invalid_address
;
9879 } // End anonymous namespace.