1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright (C) 2008-2018 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
;
81 inline bool is_branch_reloc(unsigned int);
84 inline bool is_plt16_reloc(unsigned int);
86 // Counter incremented on every Powerpc_relobj constructed.
87 static uint32_t object_id
= 0;
89 template<int size
, bool big_endian
>
90 class Powerpc_relobj
: public Sized_relobj_file
<size
, big_endian
>
93 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
94 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_refs
;
95 typedef Unordered_map
<Address
, Section_refs
> Access_from
;
97 Powerpc_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
98 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
99 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
100 uniq_(object_id
++), special_(0), relatoc_(0), toc_(0),
101 has_small_toc_reloc_(false), opd_valid_(false),
102 e_flags_(ehdr
.get_e_flags()), no_toc_opt_(), opd_ent_(),
103 access_from_map_(), has14_(), stub_table_index_(), st_other_()
105 this->set_abiversion(0);
111 // Read the symbols then set up st_other vector.
113 do_read_symbols(Read_symbols_data
*);
115 // Arrange to always relocate .toc first.
117 do_relocate_sections(
118 const Symbol_table
* symtab
, const Layout
* layout
,
119 const unsigned char* pshdrs
, Output_file
* of
,
120 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
);
122 // The .toc section index.
129 // Mark .toc entry at OFF as not optimizable.
131 set_no_toc_opt(Address off
)
133 if (this->no_toc_opt_
.empty())
134 this->no_toc_opt_
.resize(this->section_size(this->toc_shndx())
137 if (off
< this->no_toc_opt_
.size())
138 this->no_toc_opt_
[off
] = true;
141 // Mark the entire .toc as not optimizable.
145 this->no_toc_opt_
.resize(1);
146 this->no_toc_opt_
[0] = true;
149 // Return true if code using the .toc entry at OFF should not be edited.
151 no_toc_opt(Address off
) const
153 if (this->no_toc_opt_
.empty())
156 if (off
>= this->no_toc_opt_
.size())
158 return this->no_toc_opt_
[off
];
161 // The .got2 section shndx.
166 return this->special_
;
171 // The .opd section shndx.
178 return this->special_
;
181 // Init OPD entry arrays.
183 init_opd(size_t opd_size
)
185 size_t count
= this->opd_ent_ndx(opd_size
);
186 this->opd_ent_
.resize(count
);
189 // Return section and offset of function entry for .opd + R_OFF.
191 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
193 size_t ndx
= this->opd_ent_ndx(r_off
);
194 gold_assert(ndx
< this->opd_ent_
.size());
195 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
197 *value
= this->opd_ent_
[ndx
].off
;
198 return this->opd_ent_
[ndx
].shndx
;
201 // Set section and offset of function entry for .opd + R_OFF.
203 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
205 size_t ndx
= this->opd_ent_ndx(r_off
);
206 gold_assert(ndx
< this->opd_ent_
.size());
207 this->opd_ent_
[ndx
].shndx
= shndx
;
208 this->opd_ent_
[ndx
].off
= value
;
211 // Return discard flag for .opd + R_OFF.
213 get_opd_discard(Address r_off
) const
215 size_t ndx
= this->opd_ent_ndx(r_off
);
216 gold_assert(ndx
< this->opd_ent_
.size());
217 return this->opd_ent_
[ndx
].discard
;
220 // Set discard flag for .opd + R_OFF.
222 set_opd_discard(Address r_off
)
224 size_t ndx
= this->opd_ent_ndx(r_off
);
225 gold_assert(ndx
< this->opd_ent_
.size());
226 this->opd_ent_
[ndx
].discard
= true;
231 { return this->opd_valid_
; }
235 { this->opd_valid_
= true; }
237 // Examine .rela.opd to build info about function entry points.
239 scan_opd_relocs(size_t reloc_count
,
240 const unsigned char* prelocs
,
241 const unsigned char* plocal_syms
);
243 // Returns true if a code sequence loading a TOC entry can be
244 // converted into code calculating a TOC pointer relative offset.
246 make_toc_relative(Target_powerpc
<size
, big_endian
>* target
,
249 // Perform the Sized_relobj_file method, then set up opd info from
252 do_read_relocs(Read_relocs_data
*);
255 do_find_special_sections(Read_symbols_data
* sd
);
257 // Adjust this local symbol value. Return false if the symbol
258 // should be discarded from the output file.
260 do_adjust_local_symbol(Symbol_value
<size
>* lv
) const
262 if (size
== 64 && this->opd_shndx() != 0)
265 if (lv
->input_shndx(&is_ordinary
) != this->opd_shndx())
267 if (this->get_opd_discard(lv
->input_value()))
275 { return &this->access_from_map_
; }
277 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
278 // section at DST_OFF.
280 add_reference(Relobj
* src_obj
,
281 unsigned int src_indx
,
282 typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
284 Section_id
src_id(src_obj
, src_indx
);
285 this->access_from_map_
[dst_off
].insert(src_id
);
288 // Add a reference to the code section specified by the .opd entry
291 add_gc_mark(typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
293 size_t ndx
= this->opd_ent_ndx(dst_off
);
294 if (ndx
>= this->opd_ent_
.size())
295 this->opd_ent_
.resize(ndx
+ 1);
296 this->opd_ent_
[ndx
].gc_mark
= true;
300 process_gc_mark(Symbol_table
* symtab
)
302 for (size_t i
= 0; i
< this->opd_ent_
.size(); i
++)
303 if (this->opd_ent_
[i
].gc_mark
)
305 unsigned int shndx
= this->opd_ent_
[i
].shndx
;
306 symtab
->gc()->worklist().push_back(Section_id(this, shndx
));
310 // Return offset in output GOT section that this object will use
311 // as a TOC pointer. Won't be just a constant with multi-toc support.
313 toc_base_offset() const
317 set_has_small_toc_reloc()
318 { has_small_toc_reloc_
= true; }
321 has_small_toc_reloc() const
322 { return has_small_toc_reloc_
; }
325 set_has_14bit_branch(unsigned int shndx
)
327 if (shndx
>= this->has14_
.size())
328 this->has14_
.resize(shndx
+ 1);
329 this->has14_
[shndx
] = true;
333 has_14bit_branch(unsigned int shndx
) const
334 { return shndx
< this->has14_
.size() && this->has14_
[shndx
]; }
337 set_stub_table(unsigned int shndx
, unsigned int stub_index
)
339 if (shndx
>= this->stub_table_index_
.size())
340 this->stub_table_index_
.resize(shndx
+ 1, -1);
341 this->stub_table_index_
[shndx
] = stub_index
;
344 Stub_table
<size
, big_endian
>*
345 stub_table(unsigned int shndx
)
347 if (shndx
< this->stub_table_index_
.size())
349 Target_powerpc
<size
, big_endian
>* target
350 = static_cast<Target_powerpc
<size
, big_endian
>*>(
351 parameters
->sized_target
<size
, big_endian
>());
352 unsigned int indx
= this->stub_table_index_
[shndx
];
353 if (indx
< target
->stub_tables().size())
354 return target
->stub_tables()[indx
];
362 this->stub_table_index_
.clear();
367 { return this->uniq_
; }
371 { return this->e_flags_
& elfcpp::EF_PPC64_ABI
; }
373 // Set ABI version for input and output
375 set_abiversion(int ver
);
378 st_other (unsigned int symndx
) const
380 return this->st_other_
[symndx
];
384 ppc64_local_entry_offset(const Symbol
* sym
) const
385 { return elfcpp::ppc64_decode_local_entry(sym
->nonvis() >> 3); }
388 ppc64_local_entry_offset(unsigned int symndx
) const
389 { return elfcpp::ppc64_decode_local_entry(this->st_other_
[symndx
] >> 5); }
400 // Return index into opd_ent_ array for .opd entry at OFF.
401 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
402 // apart when the language doesn't use the last 8-byte word, the
403 // environment pointer. Thus dividing the entry section offset by
404 // 16 will give an index into opd_ent_ that works for either layout
405 // of .opd. (It leaves some elements of the vector unused when .opd
406 // entries are spaced 24 bytes apart, but we don't know the spacing
407 // until relocations are processed, and in any case it is possible
408 // for an object to have some entries spaced 16 bytes apart and
409 // others 24 bytes apart.)
411 opd_ent_ndx(size_t off
) const
414 // Per object unique identifier
417 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
418 unsigned int special_
;
420 // For 64-bit the .rela.toc and .toc section shdnx.
421 unsigned int relatoc_
;
424 // For 64-bit, whether this object uses small model relocs to access
426 bool has_small_toc_reloc_
;
428 // Set at the start of gc_process_relocs, when we know opd_ent_
429 // vector is valid. The flag could be made atomic and set in
430 // do_read_relocs with memory_order_release and then tested with
431 // memory_order_acquire, potentially resulting in fewer entries in
436 elfcpp::Elf_Word e_flags_
;
438 // For 64-bit, an array with one entry per 64-bit word in the .toc
439 // section, set if accesses using that word cannot be optimised.
440 std::vector
<bool> no_toc_opt_
;
442 // The first 8-byte word of an OPD entry gives the address of the
443 // entry point of the function. Relocatable object files have a
444 // relocation on this word. The following vector records the
445 // section and offset specified by these relocations.
446 std::vector
<Opd_ent
> opd_ent_
;
448 // References made to this object's .opd section when running
449 // gc_process_relocs for another object, before the opd_ent_ vector
450 // is valid for this object.
451 Access_from access_from_map_
;
453 // Whether input section has a 14-bit branch reloc.
454 std::vector
<bool> has14_
;
456 // The stub table to use for a given input section.
457 std::vector
<unsigned int> stub_table_index_
;
459 // ELF st_other field for local symbols.
460 std::vector
<unsigned char> st_other_
;
463 template<int size
, bool big_endian
>
464 class Powerpc_dynobj
: public Sized_dynobj
<size
, big_endian
>
467 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
469 Powerpc_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
470 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
471 : Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
472 opd_shndx_(0), e_flags_(ehdr
.get_e_flags()), opd_ent_()
474 this->set_abiversion(0);
480 // Call Sized_dynobj::do_read_symbols to read the symbols then
481 // read .opd from a dynamic object, filling in opd_ent_ vector,
483 do_read_symbols(Read_symbols_data
*);
485 // The .opd section shndx.
489 return this->opd_shndx_
;
492 // The .opd section address.
496 return this->opd_address_
;
499 // Init OPD entry arrays.
501 init_opd(size_t opd_size
)
503 size_t count
= this->opd_ent_ndx(opd_size
);
504 this->opd_ent_
.resize(count
);
507 // Return section and offset of function entry for .opd + R_OFF.
509 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
511 size_t ndx
= this->opd_ent_ndx(r_off
);
512 gold_assert(ndx
< this->opd_ent_
.size());
513 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
515 *value
= this->opd_ent_
[ndx
].off
;
516 return this->opd_ent_
[ndx
].shndx
;
519 // Set section and offset of function entry for .opd + R_OFF.
521 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
523 size_t ndx
= this->opd_ent_ndx(r_off
);
524 gold_assert(ndx
< this->opd_ent_
.size());
525 this->opd_ent_
[ndx
].shndx
= shndx
;
526 this->opd_ent_
[ndx
].off
= value
;
531 { return this->e_flags_
& elfcpp::EF_PPC64_ABI
; }
533 // Set ABI version for input and output.
535 set_abiversion(int ver
);
538 // Used to specify extent of executable sections.
541 Sec_info(Address start_
, Address len_
, unsigned int shndx_
)
542 : start(start_
), len(len_
), shndx(shndx_
)
546 operator<(const Sec_info
& that
) const
547 { return this->start
< that
.start
; }
560 // Return index into opd_ent_ array for .opd entry at OFF.
562 opd_ent_ndx(size_t off
) const
565 // For 64-bit the .opd section shndx and address.
566 unsigned int opd_shndx_
;
567 Address opd_address_
;
570 elfcpp::Elf_Word e_flags_
;
572 // The first 8-byte word of an OPD entry gives the address of the
573 // entry point of the function. Records the section and offset
574 // corresponding to the address. Note that in dynamic objects,
575 // offset is *not* relative to the section.
576 std::vector
<Opd_ent
> opd_ent_
;
579 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
580 // base class will emit.
582 template<int sh_type
, int size
, bool big_endian
>
583 class Powerpc_copy_relocs
: public Copy_relocs
<sh_type
, size
, big_endian
>
586 Powerpc_copy_relocs()
587 : Copy_relocs
<sh_type
, size
, big_endian
>(elfcpp::R_POWERPC_COPY
)
590 // Emit any saved relocations which turn out to be needed. This is
591 // called after all the relocs have been scanned.
593 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
596 template<int size
, bool big_endian
>
597 class Target_powerpc
: public Sized_target
<size
, big_endian
>
601 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
602 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
603 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Signed_address
;
604 typedef Unordered_set
<Symbol_location
, Symbol_location_hash
> Tocsave_loc
;
605 static const Address invalid_address
= static_cast<Address
>(0) - 1;
606 // Offset of tp and dtp pointers from start of TLS block.
607 static const Address tp_offset
= 0x7000;
608 static const Address dtp_offset
= 0x8000;
611 : Sized_target
<size
, big_endian
>(&powerpc_info
),
612 got_(NULL
), plt_(NULL
), iplt_(NULL
), lplt_(NULL
), brlt_section_(NULL
),
613 glink_(NULL
), rela_dyn_(NULL
), copy_relocs_(),
614 tlsld_got_offset_(-1U),
615 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
616 plt_thread_safe_(false), plt_localentry0_(false),
617 plt_localentry0_init_(false), has_localentry0_(false),
618 has_tls_get_addr_opt_(false),
619 relax_failed_(false), relax_fail_count_(0),
620 stub_group_size_(0), savres_section_(0),
621 tls_get_addr_(NULL
), tls_get_addr_opt_(NULL
)
625 // Process the relocations to determine unreferenced sections for
626 // garbage collection.
628 gc_process_relocs(Symbol_table
* symtab
,
630 Sized_relobj_file
<size
, big_endian
>* object
,
631 unsigned int data_shndx
,
632 unsigned int sh_type
,
633 const unsigned char* prelocs
,
635 Output_section
* output_section
,
636 bool needs_special_offset_handling
,
637 size_t local_symbol_count
,
638 const unsigned char* plocal_symbols
);
640 // Scan the relocations to look for symbol adjustments.
642 scan_relocs(Symbol_table
* symtab
,
644 Sized_relobj_file
<size
, big_endian
>* object
,
645 unsigned int data_shndx
,
646 unsigned int sh_type
,
647 const unsigned char* prelocs
,
649 Output_section
* output_section
,
650 bool needs_special_offset_handling
,
651 size_t local_symbol_count
,
652 const unsigned char* plocal_symbols
);
654 // Map input .toc section to output .got section.
656 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
658 if (size
== 64 && strcmp(name
, ".toc") == 0)
666 // Provide linker defined save/restore functions.
668 define_save_restore_funcs(Layout
*, Symbol_table
*);
670 // No stubs unless a final link.
673 { return !parameters
->options().relocatable(); }
676 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
679 do_plt_fde_location(const Output_data
*, unsigned char*,
680 uint64_t*, off_t
*) const;
682 // Stash info about branches, for stub generation.
684 push_branch(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
685 unsigned int data_shndx
, Address r_offset
,
686 unsigned int r_type
, unsigned int r_sym
, Address addend
)
688 Branch_info
info(ppc_object
, data_shndx
, r_offset
, r_type
, r_sym
, addend
);
689 this->branch_info_
.push_back(info
);
690 if (r_type
== elfcpp::R_POWERPC_REL14
691 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
692 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
693 ppc_object
->set_has_14bit_branch(data_shndx
);
696 // Return whether the last branch is a plt call, and if so, mark the
697 // branch as having an R_PPC64_TOCSAVE.
699 mark_pltcall(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
700 unsigned int data_shndx
, Address r_offset
, Symbol_table
* symtab
)
703 && !this->branch_info_
.empty()
704 && this->branch_info_
.back().mark_pltcall(ppc_object
, data_shndx
,
705 r_offset
, this, symtab
));
708 // Say the given location, that of a nop in a function prologue with
709 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
710 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
712 add_tocsave(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
713 unsigned int shndx
, Address offset
)
716 loc
.object
= ppc_object
;
719 this->tocsave_loc_
.insert(loc
);
726 return this->tocsave_loc_
;
730 do_define_standard_symbols(Symbol_table
*, Layout
*);
732 // Finalize the sections.
734 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
736 // Return the value to use for a dynamic which requires special
739 do_dynsym_value(const Symbol
*) const;
741 // Return the PLT address to use for a local symbol.
743 do_plt_address_for_local(const Relobj
*, unsigned int) const;
745 // Return the PLT address to use for a global symbol.
747 do_plt_address_for_global(const Symbol
*) const;
749 // Return the offset to use for the GOT_INDX'th got entry which is
750 // for a local tls symbol specified by OBJECT, SYMNDX.
752 do_tls_offset_for_local(const Relobj
* object
,
754 unsigned int got_indx
) const;
756 // Return the offset to use for the GOT_INDX'th got entry which is
757 // for global tls symbol GSYM.
759 do_tls_offset_for_global(Symbol
* gsym
, unsigned int got_indx
) const;
762 do_function_location(Symbol_location
*) const;
765 do_can_check_for_function_pointers() const
768 // Adjust -fsplit-stack code which calls non-split-stack code.
770 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
771 section_offset_type fnoffset
, section_size_type fnsize
,
772 const unsigned char* prelocs
, size_t reloc_count
,
773 unsigned char* view
, section_size_type view_size
,
774 std::string
* from
, std::string
* to
) const;
776 // Relocate a section.
778 relocate_section(const Relocate_info
<size
, big_endian
>*,
779 unsigned int sh_type
,
780 const unsigned char* prelocs
,
782 Output_section
* output_section
,
783 bool needs_special_offset_handling
,
785 Address view_address
,
786 section_size_type view_size
,
787 const Reloc_symbol_changes
*);
789 // Scan the relocs during a relocatable link.
791 scan_relocatable_relocs(Symbol_table
* symtab
,
793 Sized_relobj_file
<size
, big_endian
>* object
,
794 unsigned int data_shndx
,
795 unsigned int sh_type
,
796 const unsigned char* prelocs
,
798 Output_section
* output_section
,
799 bool needs_special_offset_handling
,
800 size_t local_symbol_count
,
801 const unsigned char* plocal_symbols
,
802 Relocatable_relocs
*);
804 // Scan the relocs for --emit-relocs.
806 emit_relocs_scan(Symbol_table
* symtab
,
808 Sized_relobj_file
<size
, big_endian
>* object
,
809 unsigned int data_shndx
,
810 unsigned int sh_type
,
811 const unsigned char* prelocs
,
813 Output_section
* output_section
,
814 bool needs_special_offset_handling
,
815 size_t local_symbol_count
,
816 const unsigned char* plocal_syms
,
817 Relocatable_relocs
* rr
);
819 // Emit relocations for a section.
821 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
822 unsigned int sh_type
,
823 const unsigned char* prelocs
,
825 Output_section
* output_section
,
826 typename
elfcpp::Elf_types
<size
>::Elf_Off
827 offset_in_output_section
,
829 Address view_address
,
831 unsigned char* reloc_view
,
832 section_size_type reloc_view_size
);
834 // Return whether SYM is defined by the ABI.
836 do_is_defined_by_abi(const Symbol
* sym
) const
838 return strcmp(sym
->name(), "__tls_get_addr") == 0;
841 // Return the size of the GOT section.
845 gold_assert(this->got_
!= NULL
);
846 return this->got_
->data_size();
849 // Get the PLT section.
850 const Output_data_plt_powerpc
<size
, big_endian
>*
853 gold_assert(this->plt_
!= NULL
);
857 // Get the IPLT section.
858 const Output_data_plt_powerpc
<size
, big_endian
>*
861 gold_assert(this->iplt_
!= NULL
);
865 // Get the LPLT section.
866 const Output_data_plt_powerpc
<size
, big_endian
>*
872 // Return the plt offset and section for the given global sym.
874 plt_off(const Symbol
* gsym
,
875 const Output_data_plt_powerpc
<size
, big_endian
>** sec
) const
877 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
878 && gsym
->can_use_relative_reloc(false))
879 *sec
= this->iplt_section();
881 *sec
= this->plt_section();
882 return gsym
->plt_offset();
885 // Return the plt offset and section for the given local sym.
887 plt_off(const Sized_relobj_file
<size
, big_endian
>* relobj
,
888 unsigned int local_sym_index
,
889 const Output_data_plt_powerpc
<size
, big_endian
>** sec
) const
891 const Symbol_value
<size
>* lsym
= relobj
->local_symbol(local_sym_index
);
892 if (lsym
->is_ifunc_symbol())
893 *sec
= this->iplt_section();
895 *sec
= this->lplt_section();
896 return relobj
->local_plt_offset(local_sym_index
);
899 // Get the .glink section.
900 const Output_data_glink
<size
, big_endian
>*
901 glink_section() const
903 gold_assert(this->glink_
!= NULL
);
907 Output_data_glink
<size
, big_endian
>*
910 gold_assert(this->glink_
!= NULL
);
914 bool has_glink() const
915 { return this->glink_
!= NULL
; }
917 // Get the GOT section.
918 const Output_data_got_powerpc
<size
, big_endian
>*
921 gold_assert(this->got_
!= NULL
);
925 // Get the GOT section, creating it if necessary.
926 Output_data_got_powerpc
<size
, big_endian
>*
927 got_section(Symbol_table
*, Layout
*);
930 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
931 const elfcpp::Ehdr
<size
, big_endian
>&);
933 // Return the number of entries in the GOT.
935 got_entry_count() const
937 if (this->got_
== NULL
)
939 return this->got_size() / (size
/ 8);
942 // Return the number of entries in the PLT.
944 plt_entry_count() const;
946 // Return the offset of the first non-reserved PLT entry.
948 first_plt_entry_offset() const
952 if (this->abiversion() >= 2)
957 // Return the size of each PLT entry.
959 plt_entry_size() const
963 if (this->abiversion() >= 2)
968 Output_data_save_res
<size
, big_endian
>*
969 savres_section() const
971 return this->savres_section_
;
974 // Add any special sections for this symbol to the gc work list.
975 // For powerpc64, this adds the code section of a function
978 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
980 // Handle target specific gc actions when adding a gc reference from
981 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
982 // and DST_OFF. For powerpc64, this adds a referenc to the code
983 // section of a function descriptor.
985 do_gc_add_reference(Symbol_table
* symtab
,
987 unsigned int src_shndx
,
989 unsigned int dst_shndx
,
990 Address dst_off
) const;
992 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
995 { return this->stub_tables_
; }
997 const Output_data_brlt_powerpc
<size
, big_endian
>*
999 { return this->brlt_section_
; }
1002 add_branch_lookup_table(Address to
)
1004 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
1005 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
1009 find_branch_lookup_table(Address to
)
1011 typename
Branch_lookup_table::const_iterator p
1012 = this->branch_lookup_table_
.find(to
);
1013 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
1017 write_branch_lookup_table(unsigned char *oview
)
1019 for (typename
Branch_lookup_table::const_iterator p
1020 = this->branch_lookup_table_
.begin();
1021 p
!= this->branch_lookup_table_
.end();
1024 elfcpp::Swap
<size
, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
1028 // Wrapper used after relax to define a local symbol in output data,
1029 // from the end if value < 0.
1031 define_local(Symbol_table
* symtab
, const char* name
,
1032 Output_data
* od
, Address value
, unsigned int symsize
)
1035 = symtab
->define_in_output_data(name
, NULL
, Symbol_table::PREDEFINED
,
1036 od
, value
, symsize
, elfcpp::STT_NOTYPE
,
1037 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
, 0,
1038 static_cast<Signed_address
>(value
) < 0,
1040 // We are creating this symbol late, so need to fix up things
1041 // done early in Layout::finalize.
1042 sym
->set_dynsym_index(-1U);
1046 plt_thread_safe() const
1047 { return this->plt_thread_safe_
; }
1050 plt_localentry0() const
1051 { return this->plt_localentry0_
; }
1054 set_has_localentry0()
1056 this->has_localentry0_
= true;
1060 is_elfv2_localentry0(const Symbol
* gsym
) const
1063 && this->abiversion() >= 2
1064 && this->plt_localentry0()
1065 && gsym
->type() == elfcpp::STT_FUNC
1066 && gsym
->is_defined()
1067 && gsym
->nonvis() >> 3 == 0
1068 && !gsym
->non_zero_localentry());
1072 is_elfv2_localentry0(const Sized_relobj_file
<size
, big_endian
>* object
,
1073 unsigned int r_sym
) const
1075 const Powerpc_relobj
<size
, big_endian
>* ppc_object
1076 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
1079 && this->abiversion() >= 2
1080 && this->plt_localentry0()
1081 && ppc_object
->st_other(r_sym
) >> 5 == 0)
1083 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
1085 if (!psymval
->is_ifunc_symbol()
1086 && psymval
->input_shndx(&is_ordinary
) != elfcpp::SHN_UNDEF
1093 // Remember any symbols seen with non-zero localentry, even those
1094 // not providing a definition
1096 resolve(Symbol
* to
, const elfcpp::Sym
<size
, big_endian
>& sym
, Object
*,
1101 unsigned char st_other
= sym
.get_st_other();
1102 if ((st_other
& elfcpp::STO_PPC64_LOCAL_MASK
) != 0)
1103 to
->set_non_zero_localentry();
1105 // We haven't resolved anything, continue normal processing.
1111 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI
; }
1114 set_abiversion(int ver
)
1116 elfcpp::Elf_Word flags
= this->processor_specific_flags();
1117 flags
&= ~elfcpp::EF_PPC64_ABI
;
1118 flags
|= ver
& elfcpp::EF_PPC64_ABI
;
1119 this->set_processor_specific_flags(flags
);
1123 tls_get_addr_opt() const
1124 { return this->tls_get_addr_opt_
; }
1127 tls_get_addr() const
1128 { return this->tls_get_addr_
; }
1130 // If optimizing __tls_get_addr calls, whether this is the
1131 // "__tls_get_addr" symbol.
1133 is_tls_get_addr_opt(const Symbol
* gsym
) const
1135 return this->tls_get_addr_opt_
&& (gsym
== this->tls_get_addr_
1136 || gsym
== this->tls_get_addr_opt_
);
1140 replace_tls_get_addr(const Symbol
* gsym
) const
1141 { return this->tls_get_addr_opt_
&& gsym
== this->tls_get_addr_
; }
1144 set_has_tls_get_addr_opt()
1145 { this->has_tls_get_addr_opt_
= true; }
1147 // Offset to toc save stack slot
1150 { return this->abiversion() < 2 ? 40 : 24; }
1152 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1153 // so use the CR save slot. Used only by __tls_get_addr call stub,
1154 // relying on __tls_get_addr not saving CR itself.
1157 { return this->abiversion() < 2 ? 32 : 8; }
1173 : tls_get_addr_state_(NOT_EXPECTED
),
1174 relinfo_(NULL
), relnum_(0), r_offset_(0)
1179 if (this->tls_get_addr_state_
!= NOT_EXPECTED
)
1186 if (this->relinfo_
!= NULL
)
1187 gold_error_at_location(this->relinfo_
, this->relnum_
, this->r_offset_
,
1188 _("missing expected __tls_get_addr call"));
1192 expect_tls_get_addr_call(
1193 const Relocate_info
<size
, big_endian
>* relinfo
,
1197 this->tls_get_addr_state_
= EXPECTED
;
1198 this->relinfo_
= relinfo
;
1199 this->relnum_
= relnum
;
1200 this->r_offset_
= r_offset
;
1204 expect_tls_get_addr_call()
1205 { this->tls_get_addr_state_
= EXPECTED
; }
1208 skip_next_tls_get_addr_call()
1209 {this->tls_get_addr_state_
= SKIP
; }
1212 maybe_skip_tls_get_addr_call(Target_powerpc
<size
, big_endian
>* target
,
1213 unsigned int r_type
, const Symbol
* gsym
)
1215 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
1216 || r_type
== elfcpp::R_PPC_PLTREL24
1217 || is_plt16_reloc
<size
>(r_type
)
1218 || r_type
== elfcpp::R_POWERPC_PLTSEQ
1219 || r_type
== elfcpp::R_POWERPC_PLTCALL
)
1221 && (gsym
== target
->tls_get_addr()
1222 || gsym
== target
->tls_get_addr_opt()));
1223 Tls_get_addr last_tls
= this->tls_get_addr_state_
;
1224 this->tls_get_addr_state_
= NOT_EXPECTED
;
1225 if (is_tls_call
&& last_tls
!= EXPECTED
)
1227 else if (!is_tls_call
&& last_tls
!= NOT_EXPECTED
)
1236 // What we're up to regarding calls to __tls_get_addr.
1237 // On powerpc, the branch and link insn making a call to
1238 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1239 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1240 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
1241 // The marker relocation always comes first, and has the same
1242 // symbol as the reloc on the insn setting up the __tls_get_addr
1243 // argument. This ties the arg setup insn with the call insn,
1244 // allowing ld to safely optimize away the call. We check that
1245 // every call to __tls_get_addr has a marker relocation, and that
1246 // every marker relocation is on a call to __tls_get_addr.
1247 Tls_get_addr tls_get_addr_state_
;
1248 // Info about the last reloc for error message.
1249 const Relocate_info
<size
, big_endian
>* relinfo_
;
1254 // The class which scans relocations.
1255 class Scan
: protected Track_tls
1258 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1261 : Track_tls(), issued_non_pic_error_(false)
1265 get_reference_flags(unsigned int r_type
, const Target_powerpc
* target
);
1268 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
1269 Sized_relobj_file
<size
, big_endian
>* object
,
1270 unsigned int data_shndx
,
1271 Output_section
* output_section
,
1272 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
1273 const elfcpp::Sym
<size
, big_endian
>& lsym
,
1277 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
1278 Sized_relobj_file
<size
, big_endian
>* object
,
1279 unsigned int data_shndx
,
1280 Output_section
* output_section
,
1281 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
1285 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1287 Sized_relobj_file
<size
, big_endian
>* relobj
,
1290 const elfcpp::Rela
<size
, big_endian
>& ,
1291 unsigned int r_type
,
1292 const elfcpp::Sym
<size
, big_endian
>&)
1294 // PowerPC64 .opd is not folded, so any identical function text
1295 // may be folded and we'll still keep function addresses distinct.
1296 // That means no reloc is of concern here.
1299 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1300 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1301 if (ppcobj
->abiversion() == 1)
1304 // For 32-bit and ELFv2, conservatively assume anything but calls to
1305 // function code might be taking the address of the function.
1306 return !is_branch_reloc(r_type
);
1310 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1312 Sized_relobj_file
<size
, big_endian
>* relobj
,
1315 const elfcpp::Rela
<size
, big_endian
>& ,
1316 unsigned int r_type
,
1322 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1323 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1324 if (ppcobj
->abiversion() == 1)
1327 return !is_branch_reloc(r_type
);
1331 reloc_needs_plt_for_ifunc(Target_powerpc
<size
, big_endian
>* target
,
1332 Sized_relobj_file
<size
, big_endian
>* object
,
1333 unsigned int r_type
, bool report_err
);
1337 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
1338 unsigned int r_type
);
1341 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
1342 unsigned int r_type
, Symbol
*);
1345 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
1346 Target_powerpc
* target
);
1349 check_non_pic(Relobj
*, unsigned int r_type
);
1351 // Whether we have issued an error about a non-PIC compilation.
1352 bool issued_non_pic_error_
;
1356 symval_for_branch(const Symbol_table
* symtab
,
1357 const Sized_symbol
<size
>* gsym
,
1358 Powerpc_relobj
<size
, big_endian
>* object
,
1359 Address
*value
, unsigned int *dest_shndx
);
1361 // The class which implements relocation.
1362 class Relocate
: protected Track_tls
1365 // Use 'at' branch hints when true, 'y' when false.
1366 // FIXME maybe: set this with an option.
1367 static const bool is_isa_v2
= true;
1373 // Do a relocation. Return false if the caller should not issue
1374 // any warnings about this relocation.
1376 relocate(const Relocate_info
<size
, big_endian
>*, unsigned int,
1377 Target_powerpc
*, Output_section
*, size_t, const unsigned char*,
1378 const Sized_symbol
<size
>*, const Symbol_value
<size
>*,
1379 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1383 class Relocate_comdat_behavior
1386 // Decide what the linker should do for relocations that refer to
1387 // discarded comdat sections.
1388 inline Comdat_behavior
1389 get(const char* name
)
1391 gold::Default_comdat_behavior default_behavior
;
1392 Comdat_behavior ret
= default_behavior
.get(name
);
1393 if (ret
== CB_ERROR
)
1396 && (strcmp(name
, ".fixup") == 0
1397 || strcmp(name
, ".got2") == 0))
1400 && (strcmp(name
, ".opd") == 0
1401 || strcmp(name
, ".toc") == 0
1402 || strcmp(name
, ".toc1") == 0))
1409 // Optimize the TLS relocation type based on what we know about the
1410 // symbol. IS_FINAL is true if the final address of this symbol is
1411 // known at link time.
1413 tls::Tls_optimization
1414 optimize_tls_gd(bool is_final
)
1416 // If we are generating a shared library, then we can't do anything
1418 if (parameters
->options().shared()
1419 || !parameters
->options().tls_optimize())
1420 return tls::TLSOPT_NONE
;
1423 return tls::TLSOPT_TO_IE
;
1424 return tls::TLSOPT_TO_LE
;
1427 tls::Tls_optimization
1430 if (parameters
->options().shared()
1431 || !parameters
->options().tls_optimize())
1432 return tls::TLSOPT_NONE
;
1434 return tls::TLSOPT_TO_LE
;
1437 tls::Tls_optimization
1438 optimize_tls_ie(bool is_final
)
1441 || parameters
->options().shared()
1442 || !parameters
->options().tls_optimize())
1443 return tls::TLSOPT_NONE
;
1445 return tls::TLSOPT_TO_LE
;
1450 make_glink_section(Layout
*);
1452 // Create the PLT section.
1454 make_plt_section(Symbol_table
*, Layout
*);
1457 make_iplt_section(Symbol_table
*, Layout
*);
1460 make_lplt_section(Layout
*);
1463 make_brlt_section(Layout
*);
1465 // Create a PLT entry for a global symbol.
1467 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1469 // Create a PLT entry for a local IFUNC symbol.
1471 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1472 Sized_relobj_file
<size
, big_endian
>*,
1475 // Create a PLT entry for a local non-IFUNC symbol.
1477 make_local_plt_entry(Layout
*,
1478 Sized_relobj_file
<size
, big_endian
>*,
1482 // Create a GOT entry for local dynamic __tls_get_addr.
1484 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
1485 Sized_relobj_file
<size
, big_endian
>* object
);
1488 tlsld_got_offset() const
1490 return this->tlsld_got_offset_
;
1493 // Get the dynamic reloc section, creating it if necessary.
1495 rela_dyn_section(Layout
*);
1497 // Similarly, but for ifunc symbols get the one for ifunc.
1499 rela_dyn_section(Symbol_table
*, Layout
*, bool for_ifunc
);
1501 // Copy a relocation against a global symbol.
1503 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1504 Sized_relobj_file
<size
, big_endian
>* object
,
1505 unsigned int shndx
, Output_section
* output_section
,
1506 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
1508 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1509 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1510 symtab
->get_sized_symbol
<size
>(sym
),
1511 object
, shndx
, output_section
,
1512 r_type
, reloc
.get_r_offset(),
1513 reloc
.get_r_addend(),
1514 this->rela_dyn_section(layout
));
1517 // Look over all the input sections, deciding where to place stubs.
1519 group_sections(Layout
*, const Task
*, bool);
1521 // Sort output sections by address.
1522 struct Sort_sections
1525 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
1526 { return sec1
->address() < sec2
->address(); }
1532 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1533 unsigned int data_shndx
,
1535 unsigned int r_type
,
1538 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
1539 r_type_(r_type
), tocsave_ (0), r_sym_(r_sym
), addend_(addend
)
1545 // Return whether this branch is going via a plt call stub, and if
1546 // so, mark it as having an R_PPC64_TOCSAVE.
1548 mark_pltcall(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1549 unsigned int shndx
, Address offset
,
1550 Target_powerpc
* target
, Symbol_table
* symtab
);
1552 // If this branch needs a plt call stub, or a long branch stub, make one.
1554 make_stub(Stub_table
<size
, big_endian
>*,
1555 Stub_table
<size
, big_endian
>*,
1556 Symbol_table
*) const;
1559 // The branch location..
1560 Powerpc_relobj
<size
, big_endian
>* object_
;
1561 unsigned int shndx_
;
1563 // ..and the branch type and destination.
1564 unsigned int r_type_
: 31;
1565 unsigned int tocsave_
: 1;
1566 unsigned int r_sym_
;
1570 // Information about this specific target which we pass to the
1571 // general Target structure.
1572 static Target::Target_info powerpc_info
;
1574 // The types of GOT entries needed for this platform.
1575 // These values are exposed to the ABI in an incremental link.
1576 // Do not renumber existing values without changing the version
1577 // number of the .gnu_incremental_inputs section.
1581 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
1582 GOT_TYPE_DTPREL
, // entry for @got@dtprel
1583 GOT_TYPE_TPREL
// entry for @got@tprel
1587 Output_data_got_powerpc
<size
, big_endian
>* got_
;
1588 // The PLT section. This is a container for a table of addresses,
1589 // and their relocations. Each address in the PLT has a dynamic
1590 // relocation (R_*_JMP_SLOT) and each address will have a
1591 // corresponding entry in .glink for lazy resolution of the PLT.
1592 // ppc32 initialises the PLT to point at the .glink entry, while
1593 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1594 // linker adds a stub that loads the PLT entry into ctr then
1595 // branches to ctr. There may be more than one stub for each PLT
1596 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1597 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1598 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
1599 // The IPLT section. Like plt_, this is a container for a table of
1600 // addresses and their relocations, specifically for STT_GNU_IFUNC
1601 // functions that resolve locally (STT_GNU_IFUNC functions that
1602 // don't resolve locally go in PLT). Unlike plt_, these have no
1603 // entry in .glink for lazy resolution, and the relocation section
1604 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1605 // the relocation section may contain relocations against
1606 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1607 // relocation section will appear at the end of other dynamic
1608 // relocations, so that ld.so applies these relocations after other
1609 // dynamic relocations. In a static executable, the relocation
1610 // section is emitted and marked with __rela_iplt_start and
1611 // __rela_iplt_end symbols.
1612 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
1613 // A PLT style section for local, non-ifunc symbols
1614 Output_data_plt_powerpc
<size
, big_endian
>* lplt_
;
1615 // Section holding long branch destinations.
1616 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
1617 // The .glink section.
1618 Output_data_glink
<size
, big_endian
>* glink_
;
1619 // The dynamic reloc section.
1620 Reloc_section
* rela_dyn_
;
1621 // Relocs saved to avoid a COPY reloc.
1622 Powerpc_copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
1623 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1624 unsigned int tlsld_got_offset_
;
1626 Stub_tables stub_tables_
;
1627 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
1628 Branch_lookup_table branch_lookup_table_
;
1630 typedef std::vector
<Branch_info
> Branches
;
1631 Branches branch_info_
;
1632 Tocsave_loc tocsave_loc_
;
1634 bool plt_thread_safe_
;
1635 bool plt_localentry0_
;
1636 bool plt_localentry0_init_
;
1637 bool has_localentry0_
;
1638 bool has_tls_get_addr_opt_
;
1641 int relax_fail_count_
;
1642 int32_t stub_group_size_
;
1644 Output_data_save_res
<size
, big_endian
> *savres_section_
;
1646 // The "__tls_get_addr" symbol, if present
1647 Symbol
* tls_get_addr_
;
1648 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1649 Symbol
* tls_get_addr_opt_
;
1653 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
1656 true, // is_big_endian
1657 elfcpp::EM_PPC
, // machine_code
1658 false, // has_make_symbol
1659 false, // has_resolve
1660 false, // has_code_fill
1661 true, // is_default_stack_executable
1662 false, // can_icf_inline_merge_sections
1664 "/usr/lib/ld.so.1", // dynamic_linker
1665 0x10000000, // default_text_segment_address
1666 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1667 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1668 false, // isolate_execinstr
1670 elfcpp::SHN_UNDEF
, // small_common_shndx
1671 elfcpp::SHN_UNDEF
, // large_common_shndx
1672 0, // small_common_section_flags
1673 0, // large_common_section_flags
1674 NULL
, // attributes_section
1675 NULL
, // attributes_vendor
1676 "_start", // entry_symbol_name
1677 32, // hash_entry_size
1678 elfcpp::SHT_PROGBITS
, // unwind_section_type
1682 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
1685 false, // is_big_endian
1686 elfcpp::EM_PPC
, // machine_code
1687 false, // has_make_symbol
1688 false, // has_resolve
1689 false, // has_code_fill
1690 true, // is_default_stack_executable
1691 false, // can_icf_inline_merge_sections
1693 "/usr/lib/ld.so.1", // dynamic_linker
1694 0x10000000, // default_text_segment_address
1695 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1696 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1697 false, // isolate_execinstr
1699 elfcpp::SHN_UNDEF
, // small_common_shndx
1700 elfcpp::SHN_UNDEF
, // large_common_shndx
1701 0, // small_common_section_flags
1702 0, // large_common_section_flags
1703 NULL
, // attributes_section
1704 NULL
, // attributes_vendor
1705 "_start", // entry_symbol_name
1706 32, // hash_entry_size
1707 elfcpp::SHT_PROGBITS
, // unwind_section_type
1711 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1714 true, // is_big_endian
1715 elfcpp::EM_PPC64
, // machine_code
1716 false, // has_make_symbol
1717 true, // has_resolve
1718 false, // has_code_fill
1719 false, // is_default_stack_executable
1720 false, // can_icf_inline_merge_sections
1722 "/usr/lib/ld.so.1", // dynamic_linker
1723 0x10000000, // default_text_segment_address
1724 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1725 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1726 false, // isolate_execinstr
1728 elfcpp::SHN_UNDEF
, // small_common_shndx
1729 elfcpp::SHN_UNDEF
, // large_common_shndx
1730 0, // small_common_section_flags
1731 0, // large_common_section_flags
1732 NULL
, // attributes_section
1733 NULL
, // attributes_vendor
1734 "_start", // entry_symbol_name
1735 32, // hash_entry_size
1736 elfcpp::SHT_PROGBITS
, // unwind_section_type
1740 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1743 false, // is_big_endian
1744 elfcpp::EM_PPC64
, // machine_code
1745 false, // has_make_symbol
1746 true, // has_resolve
1747 false, // has_code_fill
1748 false, // is_default_stack_executable
1749 false, // can_icf_inline_merge_sections
1751 "/usr/lib/ld.so.1", // dynamic_linker
1752 0x10000000, // default_text_segment_address
1753 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1754 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1755 false, // isolate_execinstr
1757 elfcpp::SHN_UNDEF
, // small_common_shndx
1758 elfcpp::SHN_UNDEF
, // large_common_shndx
1759 0, // small_common_section_flags
1760 0, // large_common_section_flags
1761 NULL
, // attributes_section
1762 NULL
, // attributes_vendor
1763 "_start", // entry_symbol_name
1764 32, // hash_entry_size
1765 elfcpp::SHT_PROGBITS
, // unwind_section_type
1769 is_branch_reloc(unsigned int r_type
)
1771 return (r_type
== elfcpp::R_POWERPC_REL24
1772 || r_type
== elfcpp::R_PPC_PLTREL24
1773 || r_type
== elfcpp::R_PPC_LOCAL24PC
1774 || r_type
== elfcpp::R_POWERPC_REL14
1775 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1776 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1777 || r_type
== elfcpp::R_POWERPC_ADDR24
1778 || r_type
== elfcpp::R_POWERPC_ADDR14
1779 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1780 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1783 // Reloc resolves to plt entry.
1786 is_plt16_reloc(unsigned int r_type
)
1788 return (r_type
== elfcpp::R_POWERPC_PLT16_LO
1789 || r_type
== elfcpp::R_POWERPC_PLT16_HI
1790 || r_type
== elfcpp::R_POWERPC_PLT16_HA
1791 || (size
== 64 && r_type
== elfcpp::R_PPC64_PLT16_LO_DS
));
1794 // If INSN is an opcode that may be used with an @tls operand, return
1795 // the transformed insn for TLS optimisation, otherwise return 0. If
1796 // REG is non-zero only match an insn with RB or RA equal to REG.
1798 at_tls_transform(uint32_t insn
, unsigned int reg
)
1800 if ((insn
& (0x3f << 26)) != 31 << 26)
1804 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1805 rtra
= insn
& ((1 << 26) - (1 << 16));
1806 else if (((insn
>> 16) & 0x1f) == reg
)
1807 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1811 if ((insn
& (0x3ff << 1)) == 266 << 1)
1814 else if ((insn
& (0x1f << 1)) == 23 << 1
1815 && ((insn
& (0x1f << 6)) < 14 << 6
1816 || ((insn
& (0x1f << 6)) >= 16 << 6
1817 && (insn
& (0x1f << 6)) < 24 << 6)))
1818 // load and store indexed -> dform
1819 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1820 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1821 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1822 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1823 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1825 insn
= (58 << 26) | 2;
1833 template<int size
, bool big_endian
>
1834 class Powerpc_relocate_functions
1854 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1855 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1856 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword SignedAddress
;
1858 template<int valsize
>
1860 has_overflow_signed(Address value
)
1862 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1863 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1864 limit
<<= ((valsize
- 1) >> 1);
1865 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1866 return value
+ limit
> (limit
<< 1) - 1;
1869 template<int valsize
>
1871 has_overflow_unsigned(Address value
)
1873 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1874 limit
<<= ((valsize
- 1) >> 1);
1875 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1876 return value
> (limit
<< 1) - 1;
1879 template<int valsize
>
1881 has_overflow_bitfield(Address value
)
1883 return (has_overflow_unsigned
<valsize
>(value
)
1884 && has_overflow_signed
<valsize
>(value
));
1887 template<int valsize
>
1888 static inline Status
1889 overflowed(Address value
, Overflow_check overflow
)
1891 if (overflow
== CHECK_SIGNED
)
1893 if (has_overflow_signed
<valsize
>(value
))
1894 return STATUS_OVERFLOW
;
1896 else if (overflow
== CHECK_UNSIGNED
)
1898 if (has_overflow_unsigned
<valsize
>(value
))
1899 return STATUS_OVERFLOW
;
1901 else if (overflow
== CHECK_BITFIELD
)
1903 if (has_overflow_bitfield
<valsize
>(value
))
1904 return STATUS_OVERFLOW
;
1909 // Do a simple RELA relocation
1910 template<int fieldsize
, int valsize
>
1911 static inline Status
1912 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1914 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1915 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1916 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, value
);
1917 return overflowed
<valsize
>(value
, overflow
);
1920 template<int fieldsize
, int valsize
>
1921 static inline Status
1922 rela(unsigned char* view
,
1923 unsigned int right_shift
,
1924 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1926 Overflow_check overflow
)
1928 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1929 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1930 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(wv
);
1931 Valtype reloc
= value
>> right_shift
;
1934 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, val
| reloc
);
1935 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1938 // Do a simple RELA relocation, unaligned.
1939 template<int fieldsize
, int valsize
>
1940 static inline Status
1941 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1943 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, value
);
1944 return overflowed
<valsize
>(value
, overflow
);
1947 template<int fieldsize
, int valsize
>
1948 static inline Status
1949 rela_ua(unsigned char* view
,
1950 unsigned int right_shift
,
1951 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1953 Overflow_check overflow
)
1955 typedef typename
elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::Valtype
1957 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(view
);
1958 Valtype reloc
= value
>> right_shift
;
1961 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, val
| reloc
);
1962 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1966 // R_PPC64_ADDR64: (Symbol + Addend)
1968 addr64(unsigned char* view
, Address value
)
1969 { This::template rela
<64,64>(view
, value
, CHECK_NONE
); }
1971 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1973 addr64_u(unsigned char* view
, Address value
)
1974 { This::template rela_ua
<64,64>(view
, value
, CHECK_NONE
); }
1976 // R_POWERPC_ADDR32: (Symbol + Addend)
1977 static inline Status
1978 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
1979 { return This::template rela
<32,32>(view
, value
, overflow
); }
1981 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1982 static inline Status
1983 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1984 { return This::template rela_ua
<32,32>(view
, value
, overflow
); }
1986 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1987 static inline Status
1988 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
1990 Status stat
= This::template rela
<32,26>(view
, 0, 0x03fffffc,
1992 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1993 stat
= STATUS_OVERFLOW
;
1997 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1998 static inline Status
1999 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
2000 { return This::template rela
<16,16>(view
, value
, overflow
); }
2002 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
2003 static inline Status
2004 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
2005 { return This::template rela_ua
<16,16>(view
, value
, overflow
); }
2007 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
2008 static inline Status
2009 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
2011 Status stat
= This::template rela
<16,16>(view
, 0, 0xfffc, value
, overflow
);
2012 if ((value
& 3) != 0)
2013 stat
= STATUS_OVERFLOW
;
2017 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2018 static inline Status
2019 addr16_dq(unsigned char* view
, Address value
, Overflow_check overflow
)
2021 Status stat
= This::template rela
<16,16>(view
, 0, 0xfff0, value
, overflow
);
2022 if ((value
& 15) != 0)
2023 stat
= STATUS_OVERFLOW
;
2027 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2029 addr16_hi(unsigned char* view
, Address value
)
2030 { This::template rela
<16,16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
2032 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
2034 addr16_ha(unsigned char* view
, Address value
)
2035 { This::addr16_hi(view
, value
+ 0x8000); }
2037 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
2039 addr16_hi2(unsigned char* view
, Address value
)
2040 { This::template rela
<16,16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
2042 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
2044 addr16_ha2(unsigned char* view
, Address value
)
2045 { This::addr16_hi2(view
, value
+ 0x8000); }
2047 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
2049 addr16_hi3(unsigned char* view
, Address value
)
2050 { This::template rela
<16,16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
2052 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
2054 addr16_ha3(unsigned char* view
, Address value
)
2055 { This::addr16_hi3(view
, value
+ 0x8000); }
2057 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
2058 static inline Status
2059 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
2061 Status stat
= This::template rela
<32,16>(view
, 0, 0xfffc, value
, overflow
);
2062 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
2063 stat
= STATUS_OVERFLOW
;
2067 // R_POWERPC_REL16DX_HA
2068 static inline Status
2069 addr16dx_ha(unsigned char *view
, Address value
, Overflow_check overflow
)
2071 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2072 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2073 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2075 value
= static_cast<SignedAddress
>(value
) >> 16;
2076 val
|= (value
& 0xffc1) | ((value
& 0x3e) << 15);
2077 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2078 return overflowed
<16>(value
, overflow
);
2082 // Set ABI version for input and output.
2084 template<int size
, bool big_endian
>
2086 Powerpc_relobj
<size
, big_endian
>::set_abiversion(int ver
)
2088 this->e_flags_
|= ver
;
2089 if (this->abiversion() != 0)
2091 Target_powerpc
<size
, big_endian
>* target
=
2092 static_cast<Target_powerpc
<size
, big_endian
>*>(
2093 parameters
->sized_target
<size
, big_endian
>());
2094 if (target
->abiversion() == 0)
2095 target
->set_abiversion(this->abiversion());
2096 else if (target
->abiversion() != this->abiversion())
2097 gold_error(_("%s: ABI version %d is not compatible "
2098 "with ABI version %d output"),
2099 this->name().c_str(),
2100 this->abiversion(), target
->abiversion());
2105 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2106 // relocatable object, if such sections exists.
2108 template<int size
, bool big_endian
>
2110 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
2111 Read_symbols_data
* sd
)
2113 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2114 const unsigned char* namesu
= sd
->section_names
->data();
2115 const char* names
= reinterpret_cast<const char*>(namesu
);
2116 section_size_type names_size
= sd
->section_names_size
;
2117 const unsigned char* s
;
2119 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
,
2120 size
== 32 ? ".got2" : ".opd",
2121 names
, names_size
, NULL
);
2124 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
2125 this->special_
= ndx
;
2128 if (this->abiversion() == 0)
2129 this->set_abiversion(1);
2130 else if (this->abiversion() > 1)
2131 gold_error(_("%s: .opd invalid in abiv%d"),
2132 this->name().c_str(), this->abiversion());
2137 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".rela.toc",
2138 names
, names_size
, NULL
);
2141 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
2142 this->relatoc_
= ndx
;
2143 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2144 this->toc_
= this->adjust_shndx(shdr
.get_sh_info());
2147 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
2150 // Examine .rela.opd to build info about function entry points.
2152 template<int size
, bool big_endian
>
2154 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
2156 const unsigned char* prelocs
,
2157 const unsigned char* plocal_syms
)
2161 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
2162 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
2163 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2164 Address expected_off
= 0;
2165 bool regular
= true;
2166 unsigned int opd_ent_size
= 0;
2168 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
2170 Reltype
reloc(prelocs
);
2171 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
2172 = reloc
.get_r_info();
2173 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
2174 if (r_type
== elfcpp::R_PPC64_ADDR64
)
2176 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
2177 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
2180 if (r_sym
< this->local_symbol_count())
2182 typename
elfcpp::Sym
<size
, big_endian
>
2183 lsym(plocal_syms
+ r_sym
* sym_size
);
2184 shndx
= lsym
.get_st_shndx();
2185 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
2186 value
= lsym
.get_st_value();
2189 shndx
= this->symbol_section_and_value(r_sym
, &value
,
2191 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
2192 value
+ reloc
.get_r_addend());
2195 expected_off
= reloc
.get_r_offset();
2196 opd_ent_size
= expected_off
;
2198 else if (expected_off
!= reloc
.get_r_offset())
2200 expected_off
+= opd_ent_size
;
2202 else if (r_type
== elfcpp::R_PPC64_TOC
)
2204 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
2209 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2210 this->name().c_str(), r_type
);
2214 if (reloc_count
<= 2)
2215 opd_ent_size
= this->section_size(this->opd_shndx());
2216 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
2220 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2221 this->name().c_str());
2227 // Returns true if a code sequence loading the TOC entry at VALUE
2228 // relative to the TOC pointer can be converted into code calculating
2229 // a TOC pointer relative offset.
2230 // If so, the TOC pointer relative offset is stored to VALUE.
2232 template<int size
, bool big_endian
>
2234 Powerpc_relobj
<size
, big_endian
>::make_toc_relative(
2235 Target_powerpc
<size
, big_endian
>* target
,
2241 // With -mcmodel=medium code it is quite possible to have
2242 // toc-relative relocs referring to objects outside the TOC.
2243 // Don't try to look at a non-existent TOC.
2244 if (this->toc_shndx() == 0)
2247 // Convert VALUE back to an address by adding got_base (see below),
2248 // then to an offset in the TOC by subtracting the TOC output
2249 // section address and the TOC output offset. Since this TOC output
2250 // section and the got output section are one and the same, we can
2251 // omit adding and subtracting the output section address.
2252 Address off
= (*value
+ this->toc_base_offset()
2253 - this->output_section_offset(this->toc_shndx()));
2254 // Is this offset in the TOC? -mcmodel=medium code may be using
2255 // TOC relative access to variables outside the TOC. Those of
2256 // course can't be optimized. We also don't try to optimize code
2257 // that is using a different object's TOC.
2258 if (off
>= this->section_size(this->toc_shndx()))
2261 if (this->no_toc_opt(off
))
2264 section_size_type vlen
;
2265 unsigned char* view
= this->get_output_view(this->toc_shndx(), &vlen
);
2266 Address addr
= elfcpp::Swap
<size
, big_endian
>::readval(view
+ off
);
2268 Address got_base
= (target
->got_section()->output_section()->address()
2269 + this->toc_base_offset());
2271 if (addr
+ (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2278 // Perform the Sized_relobj_file method, then set up opd info from
2281 template<int size
, bool big_endian
>
2283 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
2285 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
2288 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
2289 p
!= rd
->relocs
.end();
2292 if (p
->data_shndx
== this->opd_shndx())
2294 uint64_t opd_size
= this->section_size(this->opd_shndx());
2295 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
2298 this->init_opd(opd_size
);
2299 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
2300 rd
->local_symbols
->data());
2308 // Read the symbols then set up st_other vector.
2310 template<int size
, bool big_endian
>
2312 Powerpc_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
2314 this->base_read_symbols(sd
);
2317 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2318 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2319 const unsigned int loccount
= this->do_local_symbol_count();
2322 this->st_other_
.resize(loccount
);
2323 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2324 off_t locsize
= loccount
* sym_size
;
2325 const unsigned int symtab_shndx
= this->symtab_shndx();
2326 const unsigned char *psymtab
= pshdrs
+ symtab_shndx
* shdr_size
;
2327 typename
elfcpp::Shdr
<size
, big_endian
> shdr(psymtab
);
2328 const unsigned char* psyms
= this->get_view(shdr
.get_sh_offset(),
2329 locsize
, true, false);
2331 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
2333 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
2334 unsigned char st_other
= sym
.get_st_other();
2335 this->st_other_
[i
] = st_other
;
2336 if ((st_other
& elfcpp::STO_PPC64_LOCAL_MASK
) != 0)
2338 if (this->abiversion() == 0)
2339 this->set_abiversion(2);
2340 else if (this->abiversion() < 2)
2341 gold_error(_("%s: local symbol %d has invalid st_other"
2342 " for ABI version 1"),
2343 this->name().c_str(), i
);
2350 template<int size
, bool big_endian
>
2352 Powerpc_dynobj
<size
, big_endian
>::set_abiversion(int ver
)
2354 this->e_flags_
|= ver
;
2355 if (this->abiversion() != 0)
2357 Target_powerpc
<size
, big_endian
>* target
=
2358 static_cast<Target_powerpc
<size
, big_endian
>*>(
2359 parameters
->sized_target
<size
, big_endian
>());
2360 if (target
->abiversion() == 0)
2361 target
->set_abiversion(this->abiversion());
2362 else if (target
->abiversion() != this->abiversion())
2363 gold_error(_("%s: ABI version %d is not compatible "
2364 "with ABI version %d output"),
2365 this->name().c_str(),
2366 this->abiversion(), target
->abiversion());
2371 // Call Sized_dynobj::base_read_symbols to read the symbols then
2372 // read .opd from a dynamic object, filling in opd_ent_ vector,
2374 template<int size
, bool big_endian
>
2376 Powerpc_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
2378 this->base_read_symbols(sd
);
2381 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2382 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2383 const unsigned char* namesu
= sd
->section_names
->data();
2384 const char* names
= reinterpret_cast<const char*>(namesu
);
2385 const unsigned char* s
= NULL
;
2386 const unsigned char* opd
;
2387 section_size_type opd_size
;
2389 // Find and read .opd section.
2392 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".opd", names
,
2393 sd
->section_names_size
,
2398 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2399 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2400 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
2402 if (this->abiversion() == 0)
2403 this->set_abiversion(1);
2404 else if (this->abiversion() > 1)
2405 gold_error(_("%s: .opd invalid in abiv%d"),
2406 this->name().c_str(), this->abiversion());
2408 this->opd_shndx_
= (s
- pshdrs
) / shdr_size
;
2409 this->opd_address_
= shdr
.get_sh_addr();
2410 opd_size
= convert_to_section_size_type(shdr
.get_sh_size());
2411 opd
= this->get_view(shdr
.get_sh_offset(), opd_size
,
2417 // Build set of executable sections.
2418 // Using a set is probably overkill. There is likely to be only
2419 // a few executable sections, typically .init, .text and .fini,
2420 // and they are generally grouped together.
2421 typedef std::set
<Sec_info
> Exec_sections
;
2422 Exec_sections exec_sections
;
2424 for (unsigned int i
= 1; i
< this->shnum(); ++i
, s
+= shdr_size
)
2426 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2427 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2428 && ((shdr
.get_sh_flags()
2429 & (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2430 == (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2431 && shdr
.get_sh_size() != 0)
2433 exec_sections
.insert(Sec_info(shdr
.get_sh_addr(),
2434 shdr
.get_sh_size(), i
));
2437 if (exec_sections
.empty())
2440 // Look over the OPD entries. This is complicated by the fact
2441 // that some binaries will use two-word entries while others
2442 // will use the standard three-word entries. In most cases
2443 // the third word (the environment pointer for languages like
2444 // Pascal) is unused and will be zero. If the third word is
2445 // used it should not be pointing into executable sections,
2447 this->init_opd(opd_size
);
2448 for (const unsigned char* p
= opd
; p
< opd
+ opd_size
; p
+= 8)
2450 typedef typename
elfcpp::Swap
<64, big_endian
>::Valtype Valtype
;
2451 const Valtype
* valp
= reinterpret_cast<const Valtype
*>(p
);
2452 Valtype val
= elfcpp::Swap
<64, big_endian
>::readval(valp
);
2454 // Chances are that this is the third word of an OPD entry.
2456 typename
Exec_sections::const_iterator e
2457 = exec_sections
.upper_bound(Sec_info(val
, 0, 0));
2458 if (e
!= exec_sections
.begin())
2461 if (e
->start
<= val
&& val
< e
->start
+ e
->len
)
2463 // We have an address in an executable section.
2464 // VAL ought to be the function entry, set it up.
2465 this->set_opd_ent(p
- opd
, e
->shndx
, val
);
2466 // Skip second word of OPD entry, the TOC pointer.
2470 // If we didn't match any executable sections, we likely
2471 // have a non-zero third word in the OPD entry.
2476 // Relocate sections.
2478 template<int size
, bool big_endian
>
2480 Powerpc_relobj
<size
, big_endian
>::do_relocate_sections(
2481 const Symbol_table
* symtab
, const Layout
* layout
,
2482 const unsigned char* pshdrs
, Output_file
* of
,
2483 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
)
2485 unsigned int start
= 1;
2487 && this->relatoc_
!= 0
2488 && !parameters
->options().relocatable())
2490 // Relocate .toc first.
2491 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2492 this->relatoc_
, this->relatoc_
);
2493 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2494 1, this->relatoc_
- 1);
2495 start
= this->relatoc_
+ 1;
2497 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2498 start
, this->shnum() - 1);
2500 if (!parameters
->options().output_is_position_independent())
2502 Target_powerpc
<size
, big_endian
>* target
2503 = static_cast<Target_powerpc
<size
, big_endian
>*>(
2504 parameters
->sized_target
<size
, big_endian
>());
2505 if (target
->lplt_section() && target
->lplt_section()->data_size() != 0)
2507 const section_size_type offset
= target
->lplt_section()->offset();
2508 const section_size_type oview_size
2509 = convert_to_section_size_type(target
->lplt_section()->data_size());
2510 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2512 bool modified
= false;
2513 unsigned int nsyms
= this->local_symbol_count();
2514 for (unsigned int i
= 0; i
< nsyms
; i
++)
2515 if (this->local_has_plt_offset(i
))
2517 Address value
= this->local_symbol_value(i
, 0);
2519 value
+= ppc64_local_entry_offset(i
);
2520 size_t off
= this->local_plt_offset(i
);
2521 elfcpp::Swap
<size
, big_endian
>::writeval(oview
+ off
, value
);
2525 of
->write_output_view(offset
, oview_size
, oview
);
2530 // Set up some symbols.
2532 template<int size
, bool big_endian
>
2534 Target_powerpc
<size
, big_endian
>::do_define_standard_symbols(
2535 Symbol_table
* symtab
,
2540 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2541 // undefined when scanning relocs (and thus requires
2542 // non-relative dynamic relocs). The proper value will be
2544 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2545 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2547 Target_powerpc
<size
, big_endian
>* target
=
2548 static_cast<Target_powerpc
<size
, big_endian
>*>(
2549 parameters
->sized_target
<size
, big_endian
>());
2550 Output_data_got_powerpc
<size
, big_endian
>* got
2551 = target
->got_section(symtab
, layout
);
2552 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2553 Symbol_table::PREDEFINED
,
2557 elfcpp::STV_HIDDEN
, 0,
2561 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2562 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
2563 if (sdasym
!= NULL
&& sdasym
->is_undefined())
2565 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
2567 = layout
->add_output_section_data(".sdata", 0,
2569 | elfcpp::SHF_WRITE
,
2570 sdata
, ORDER_SMALL_DATA
, false);
2571 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
2572 Symbol_table::PREDEFINED
,
2573 os
, 32768, 0, elfcpp::STT_OBJECT
,
2574 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
2580 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2581 Symbol
*gotsym
= symtab
->lookup(".TOC.", NULL
);
2582 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2584 Target_powerpc
<size
, big_endian
>* target
=
2585 static_cast<Target_powerpc
<size
, big_endian
>*>(
2586 parameters
->sized_target
<size
, big_endian
>());
2587 Output_data_got_powerpc
<size
, big_endian
>* got
2588 = target
->got_section(symtab
, layout
);
2589 symtab
->define_in_output_data(".TOC.", NULL
,
2590 Symbol_table::PREDEFINED
,
2594 elfcpp::STV_HIDDEN
, 0,
2599 this->tls_get_addr_
= symtab
->lookup("__tls_get_addr");
2600 if (parameters
->options().tls_get_addr_optimize()
2601 && this->tls_get_addr_
!= NULL
2602 && this->tls_get_addr_
->in_reg())
2603 this->tls_get_addr_opt_
= symtab
->lookup("__tls_get_addr_opt");
2604 if (this->tls_get_addr_opt_
!= NULL
)
2606 if (this->tls_get_addr_
->is_undefined()
2607 || this->tls_get_addr_
->is_from_dynobj())
2609 // Make it seem as if references to __tls_get_addr are
2610 // really to __tls_get_addr_opt, so the latter symbol is
2611 // made dynamic, not the former.
2612 this->tls_get_addr_
->clear_in_reg();
2613 this->tls_get_addr_opt_
->set_in_reg();
2615 // We have a non-dynamic definition for __tls_get_addr.
2616 // Make __tls_get_addr_opt the same, if it does not already have
2617 // a non-dynamic definition.
2618 else if (this->tls_get_addr_opt_
->is_undefined()
2619 || this->tls_get_addr_opt_
->is_from_dynobj())
2621 Sized_symbol
<size
>* from
2622 = static_cast<Sized_symbol
<size
>*>(this->tls_get_addr_
);
2623 Sized_symbol
<size
>* to
2624 = static_cast<Sized_symbol
<size
>*>(this->tls_get_addr_opt_
);
2625 symtab
->clone
<size
>(to
, from
);
2630 // Set up PowerPC target specific relobj.
2632 template<int size
, bool big_endian
>
2634 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
2635 const std::string
& name
,
2636 Input_file
* input_file
,
2637 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
2639 int et
= ehdr
.get_e_type();
2640 // ET_EXEC files are valid input for --just-symbols/-R,
2641 // and we treat them as relocatable objects.
2642 if (et
== elfcpp::ET_REL
2643 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
2645 Powerpc_relobj
<size
, big_endian
>* obj
=
2646 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2650 else if (et
== elfcpp::ET_DYN
)
2652 Powerpc_dynobj
<size
, big_endian
>* obj
=
2653 new Powerpc_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2659 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
2664 template<int size
, bool big_endian
>
2665 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
2668 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
2669 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
2671 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
2672 : Output_data_got
<size
, big_endian
>(),
2673 symtab_(symtab
), layout_(layout
),
2674 header_ent_cnt_(size
== 32 ? 3 : 1),
2675 header_index_(size
== 32 ? 0x2000 : 0)
2678 this->set_addralign(256);
2681 // Override all the Output_data_got methods we use so as to first call
2684 add_global(Symbol
* gsym
, unsigned int got_type
)
2686 this->reserve_ent();
2687 return Output_data_got
<size
, big_endian
>::add_global(gsym
, got_type
);
2691 add_global_plt(Symbol
* gsym
, unsigned int got_type
)
2693 this->reserve_ent();
2694 return Output_data_got
<size
, big_endian
>::add_global_plt(gsym
, got_type
);
2698 add_global_tls(Symbol
* gsym
, unsigned int got_type
)
2699 { return this->add_global_plt(gsym
, got_type
); }
2702 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
2703 Output_data_reloc_generic
* rel_dyn
, unsigned int r_type
)
2705 this->reserve_ent();
2706 Output_data_got
<size
, big_endian
>::
2707 add_global_with_rel(gsym
, got_type
, rel_dyn
, r_type
);
2711 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
2712 Output_data_reloc_generic
* rel_dyn
,
2713 unsigned int r_type_1
, unsigned int r_type_2
)
2715 if (gsym
->has_got_offset(got_type
))
2718 this->reserve_ent(2);
2719 Output_data_got
<size
, big_endian
>::
2720 add_global_pair_with_rel(gsym
, got_type
, rel_dyn
, r_type_1
, r_type_2
);
2724 add_local(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2726 this->reserve_ent();
2727 return Output_data_got
<size
, big_endian
>::add_local(object
, sym_index
,
2732 add_local_plt(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2734 this->reserve_ent();
2735 return Output_data_got
<size
, big_endian
>::add_local_plt(object
, sym_index
,
2740 add_local_tls(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2741 { return this->add_local_plt(object
, sym_index
, got_type
); }
2744 add_local_tls_pair(Relobj
* object
, unsigned int sym_index
,
2745 unsigned int got_type
,
2746 Output_data_reloc_generic
* rel_dyn
,
2747 unsigned int r_type
)
2749 if (object
->local_has_got_offset(sym_index
, got_type
))
2752 this->reserve_ent(2);
2753 Output_data_got
<size
, big_endian
>::
2754 add_local_tls_pair(object
, sym_index
, got_type
, rel_dyn
, r_type
);
2758 add_constant(Valtype constant
)
2760 this->reserve_ent();
2761 return Output_data_got
<size
, big_endian
>::add_constant(constant
);
2765 add_constant_pair(Valtype c1
, Valtype c2
)
2767 this->reserve_ent(2);
2768 return Output_data_got
<size
, big_endian
>::add_constant_pair(c1
, c2
);
2771 // Offset of _GLOBAL_OFFSET_TABLE_.
2775 return this->got_offset(this->header_index_
);
2778 // Offset of base used to access the GOT/TOC.
2779 // The got/toc pointer reg will be set to this value.
2781 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
2784 return this->g_o_t();
2786 return (this->output_section()->address()
2787 + object
->toc_base_offset()
2791 // Ensure our GOT has a header.
2793 set_final_data_size()
2795 if (this->header_ent_cnt_
!= 0)
2796 this->make_header();
2797 Output_data_got
<size
, big_endian
>::set_final_data_size();
2800 // First word of GOT header needs some values that are not
2801 // handled by Output_data_got so poke them in here.
2802 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2804 do_write(Output_file
* of
)
2807 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
2808 val
= this->layout_
->dynamic_section()->address();
2810 val
= this->output_section()->address() + 0x8000;
2811 this->replace_constant(this->header_index_
, val
);
2812 Output_data_got
<size
, big_endian
>::do_write(of
);
2817 reserve_ent(unsigned int cnt
= 1)
2819 if (this->header_ent_cnt_
== 0)
2821 if (this->num_entries() + cnt
> this->header_index_
)
2822 this->make_header();
2828 this->header_ent_cnt_
= 0;
2829 this->header_index_
= this->num_entries();
2832 Output_data_got
<size
, big_endian
>::add_constant(0);
2833 Output_data_got
<size
, big_endian
>::add_constant(0);
2834 Output_data_got
<size
, big_endian
>::add_constant(0);
2836 // Define _GLOBAL_OFFSET_TABLE_ at the header
2837 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2840 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
2841 sym
->set_value(this->g_o_t());
2844 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2845 Symbol_table::PREDEFINED
,
2846 this, this->g_o_t(), 0,
2849 elfcpp::STV_HIDDEN
, 0,
2853 Output_data_got
<size
, big_endian
>::add_constant(0);
2856 // Stashed pointers.
2857 Symbol_table
* symtab_
;
2861 unsigned int header_ent_cnt_
;
2862 // GOT header index.
2863 unsigned int header_index_
;
2866 // Get the GOT section, creating it if necessary.
2868 template<int size
, bool big_endian
>
2869 Output_data_got_powerpc
<size
, big_endian
>*
2870 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
2873 if (this->got_
== NULL
)
2875 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2878 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
2880 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2881 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2882 this->got_
, ORDER_DATA
, false);
2888 // Get the dynamic reloc section, creating it if necessary.
2890 template<int size
, bool big_endian
>
2891 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2892 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
2894 if (this->rela_dyn_
== NULL
)
2896 gold_assert(layout
!= NULL
);
2897 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
2898 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
2899 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
2900 ORDER_DYNAMIC_RELOCS
, false);
2902 return this->rela_dyn_
;
2905 // Similarly, but for ifunc symbols get the one for ifunc.
2907 template<int size
, bool big_endian
>
2908 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2909 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Symbol_table
* symtab
,
2914 return this->rela_dyn_section(layout
);
2916 if (this->iplt_
== NULL
)
2917 this->make_iplt_section(symtab
, layout
);
2918 return this->iplt_
->rel_plt();
2924 // Determine the stub group size. The group size is the absolute
2925 // value of the parameter --stub-group-size. If --stub-group-size
2926 // is passed a negative value, we restrict stubs to be always after
2927 // the stubbed branches.
2928 Stub_control(int32_t size
, bool no_size_errors
, bool multi_os
)
2929 : stub_group_size_(abs(size
)), stubs_always_after_branch_(size
< 0),
2930 suppress_size_errors_(no_size_errors
), multi_os_(multi_os
),
2931 state_(NO_GROUP
), group_size_(0), group_start_addr_(0),
2932 owner_(NULL
), output_section_(NULL
)
2936 // Return true iff input section can be handled by current stub
2939 can_add_to_stub_group(Output_section
* o
,
2940 const Output_section::Input_section
* i
,
2943 const Output_section::Input_section
*
2949 { return output_section_
; }
2952 set_output_and_owner(Output_section
* o
,
2953 const Output_section::Input_section
* i
)
2955 this->output_section_
= o
;
2964 // Adding group sections before the stubs.
2965 FINDING_STUB_SECTION
,
2966 // Adding group sections after the stubs.
2970 uint32_t stub_group_size_
;
2971 bool stubs_always_after_branch_
;
2972 bool suppress_size_errors_
;
2973 // True if a stub group can serve multiple output sections.
2976 // Current max size of group. Starts at stub_group_size_ but is
2977 // reduced to stub_group_size_/1024 on seeing a section with
2978 // external conditional branches.
2979 uint32_t group_size_
;
2980 uint64_t group_start_addr_
;
2981 // owner_ and output_section_ specify the section to which stubs are
2982 // attached. The stubs are placed at the end of this section.
2983 const Output_section::Input_section
* owner_
;
2984 Output_section
* output_section_
;
2987 // Return true iff input section can be handled by current stub
2988 // group. Sections are presented to this function in order,
2989 // so the first section is the head of the group.
2992 Stub_control::can_add_to_stub_group(Output_section
* o
,
2993 const Output_section::Input_section
* i
,
2996 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
2998 uint64_t start_addr
= o
->address();
3001 // .init and .fini sections are pasted together to form a single
3002 // function. We can't be adding stubs in the middle of the function.
3003 this_size
= o
->data_size();
3006 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
3007 this_size
= i
->data_size();
3010 uint64_t end_addr
= start_addr
+ this_size
;
3011 uint32_t group_size
= this->stub_group_size_
;
3013 this->group_size_
= group_size
= group_size
>> 10;
3015 if (this_size
> group_size
&& !this->suppress_size_errors_
)
3016 gold_warning(_("%s:%s exceeds group size"),
3017 i
->relobj()->name().c_str(),
3018 i
->relobj()->section_name(i
->shndx()).c_str());
3020 gold_debug(DEBUG_TARGET
, "maybe add%s %s:%s size=%#llx total=%#llx",
3021 has14
? " 14bit" : "",
3022 i
->relobj()->name().c_str(),
3023 i
->relobj()->section_name(i
->shndx()).c_str(),
3024 (long long) this_size
,
3025 (this->state_
== NO_GROUP
3027 : (long long) end_addr
- this->group_start_addr_
));
3029 if (this->state_
== NO_GROUP
)
3031 // Only here on very first use of Stub_control
3033 this->output_section_
= o
;
3034 this->state_
= FINDING_STUB_SECTION
;
3035 this->group_size_
= group_size
;
3036 this->group_start_addr_
= start_addr
;
3039 else if (!this->multi_os_
&& this->output_section_
!= o
)
3041 else if (this->state_
== HAS_STUB_SECTION
)
3043 // Can we add this section, which is after the stubs, to the
3045 if (end_addr
- this->group_start_addr_
<= this->group_size_
)
3048 else if (this->state_
== FINDING_STUB_SECTION
)
3050 if ((whole_sec
&& this->output_section_
== o
)
3051 || end_addr
- this->group_start_addr_
<= this->group_size_
)
3053 // Stubs are added at the end of "owner_".
3055 this->output_section_
= o
;
3058 // The group before the stubs has reached maximum size.
3059 // Now see about adding sections after the stubs to the
3060 // group. If the current section has a 14-bit branch and
3061 // the group before the stubs exceeds group_size_ (because
3062 // they didn't have 14-bit branches), don't add sections
3063 // after the stubs: The size of stubs for such a large
3064 // group may exceed the reach of a 14-bit branch.
3065 if (!this->stubs_always_after_branch_
3066 && this_size
<= this->group_size_
3067 && start_addr
- this->group_start_addr_
<= this->group_size_
)
3069 gold_debug(DEBUG_TARGET
, "adding after stubs");
3070 this->state_
= HAS_STUB_SECTION
;
3071 this->group_start_addr_
= start_addr
;
3078 gold_debug(DEBUG_TARGET
,
3079 !this->multi_os_
&& this->output_section_
!= o
3080 ? "nope, new output section\n"
3081 : "nope, didn't fit\n");
3083 // The section fails to fit in the current group. Set up a few
3084 // things for the next group. owner_ and output_section_ will be
3085 // set later after we've retrieved those values for the current
3087 this->state_
= FINDING_STUB_SECTION
;
3088 this->group_size_
= group_size
;
3089 this->group_start_addr_
= start_addr
;
3093 // Look over all the input sections, deciding where to place stubs.
3095 template<int size
, bool big_endian
>
3097 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
3099 bool no_size_errors
)
3101 Stub_control
stub_control(this->stub_group_size_
, no_size_errors
,
3102 parameters
->options().stub_group_multi());
3104 // Group input sections and insert stub table
3105 Stub_table_owner
* table_owner
= NULL
;
3106 std::vector
<Stub_table_owner
*> tables
;
3107 Layout::Section_list section_list
;
3108 layout
->get_executable_sections(§ion_list
);
3109 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
3110 for (Layout::Section_list::iterator o
= section_list
.begin();
3111 o
!= section_list
.end();
3114 typedef Output_section::Input_section_list Input_section_list
;
3115 for (Input_section_list::const_iterator i
3116 = (*o
)->input_sections().begin();
3117 i
!= (*o
)->input_sections().end();
3120 if (i
->is_input_section()
3121 || i
->is_relaxed_input_section())
3123 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3124 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
3125 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
3126 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
3128 table_owner
->output_section
= stub_control
.output_section();
3129 table_owner
->owner
= stub_control
.owner();
3130 stub_control
.set_output_and_owner(*o
, &*i
);
3133 if (table_owner
== NULL
)
3135 table_owner
= new Stub_table_owner
;
3136 tables
.push_back(table_owner
);
3138 ppcobj
->set_stub_table(i
->shndx(), tables
.size() - 1);
3142 if (table_owner
!= NULL
)
3144 table_owner
->output_section
= stub_control
.output_section();
3145 table_owner
->owner
= stub_control
.owner();;
3147 for (typename
std::vector
<Stub_table_owner
*>::iterator t
= tables
.begin();
3151 Stub_table
<size
, big_endian
>* stub_table
;
3153 if ((*t
)->owner
->is_input_section())
3154 stub_table
= new Stub_table
<size
, big_endian
>(this,
3155 (*t
)->output_section
,
3157 this->stub_tables_
.size());
3158 else if ((*t
)->owner
->is_relaxed_input_section())
3159 stub_table
= static_cast<Stub_table
<size
, big_endian
>*>(
3160 (*t
)->owner
->relaxed_input_section());
3163 this->stub_tables_
.push_back(stub_table
);
3168 static unsigned long
3169 max_branch_delta (unsigned int r_type
)
3171 if (r_type
== elfcpp::R_POWERPC_REL14
3172 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
3173 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
3175 if (r_type
== elfcpp::R_POWERPC_REL24
3176 || r_type
== elfcpp::R_PPC_PLTREL24
3177 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
3182 // Return whether this branch is going via a plt call stub.
3184 template<int size
, bool big_endian
>
3186 Target_powerpc
<size
, big_endian
>::Branch_info::mark_pltcall(
3187 Powerpc_relobj
<size
, big_endian
>* ppc_object
,
3190 Target_powerpc
* target
,
3191 Symbol_table
* symtab
)
3193 if (this->object_
!= ppc_object
3194 || this->shndx_
!= shndx
3195 || this->offset_
!= offset
)
3198 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
3199 if (sym
!= NULL
&& sym
->is_forwarder())
3200 sym
= symtab
->resolve_forwards(sym
);
3201 if (target
->replace_tls_get_addr(sym
))
3202 sym
= target
->tls_get_addr_opt();
3203 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
3205 ? (gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
3206 && !target
->is_elfv2_localentry0(gsym
))
3207 : (this->object_
->local_has_plt_offset(this->r_sym_
)
3208 && !target
->is_elfv2_localentry0(this->object_
, this->r_sym_
)))
3216 // If this branch needs a plt call stub, or a long branch stub, make one.
3218 template<int size
, bool big_endian
>
3220 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
3221 Stub_table
<size
, big_endian
>* stub_table
,
3222 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
3223 Symbol_table
* symtab
) const
3225 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
3226 Target_powerpc
<size
, big_endian
>* target
=
3227 static_cast<Target_powerpc
<size
, big_endian
>*>(
3228 parameters
->sized_target
<size
, big_endian
>());
3229 if (sym
!= NULL
&& sym
->is_forwarder())
3230 sym
= symtab
->resolve_forwards(sym
);
3231 if (target
->replace_tls_get_addr(sym
))
3232 sym
= target
->tls_get_addr_opt();
3233 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
3237 ? gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
3238 : this->object_
->local_has_plt_offset(this->r_sym_
))
3242 && target
->abiversion() >= 2
3243 && !parameters
->options().output_is_position_independent()
3244 && !is_branch_reloc(this->r_type_
))
3245 target
->glink_section()->add_global_entry(gsym
);
3248 if (stub_table
== NULL
3251 && !parameters
->options().output_is_position_independent()
3252 && !is_branch_reloc(this->r_type_
)))
3253 stub_table
= this->object_
->stub_table(this->shndx_
);
3254 if (stub_table
== NULL
)
3256 // This is a ref from a data section to an ifunc symbol,
3257 // or a non-branch reloc for which we always want to use
3258 // one set of stubs for resolving function addresses.
3259 stub_table
= ifunc_stub_table
;
3261 gold_assert(stub_table
!= NULL
);
3262 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
3263 if (from
!= invalid_address
)
3264 from
+= (this->object_
->output_section(this->shndx_
)->address()
3267 ok
= stub_table
->add_plt_call_entry(from
,
3268 this->object_
, gsym
,
3269 this->r_type_
, this->addend_
,
3272 ok
= stub_table
->add_plt_call_entry(from
,
3273 this->object_
, this->r_sym_
,
3274 this->r_type_
, this->addend_
,
3280 Address max_branch_offset
= max_branch_delta(this->r_type_
);
3281 if (max_branch_offset
== 0)
3283 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
3284 gold_assert(from
!= invalid_address
);
3285 from
+= (this->object_
->output_section(this->shndx_
)->address()
3290 switch (gsym
->source())
3292 case Symbol::FROM_OBJECT
:
3294 Object
* symobj
= gsym
->object();
3295 if (symobj
->is_dynamic()
3296 || symobj
->pluginobj() != NULL
)
3299 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
3300 if (shndx
== elfcpp::SHN_UNDEF
)
3305 case Symbol::IS_UNDEFINED
:
3311 Symbol_table::Compute_final_value_status status
;
3312 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
3313 if (status
!= Symbol_table::CFVS_OK
)
3316 to
+= this->object_
->ppc64_local_entry_offset(gsym
);
3320 const Symbol_value
<size
>* psymval
3321 = this->object_
->local_symbol(this->r_sym_
);
3322 Symbol_value
<size
> symval
;
3323 if (psymval
->is_section_symbol())
3324 symval
.set_is_section_symbol();
3325 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
3326 typename
ObjType::Compute_final_local_value_status status
3327 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
3329 if (status
!= ObjType::CFLV_OK
3330 || !symval
.has_output_value())
3332 to
= symval
.value(this->object_
, 0);
3334 to
+= this->object_
->ppc64_local_entry_offset(this->r_sym_
);
3336 if (!(size
== 32 && this->r_type_
== elfcpp::R_PPC_PLTREL24
))
3337 to
+= this->addend_
;
3338 if (stub_table
== NULL
)
3339 stub_table
= this->object_
->stub_table(this->shndx_
);
3340 if (size
== 64 && target
->abiversion() < 2)
3342 unsigned int dest_shndx
;
3343 if (!target
->symval_for_branch(symtab
, gsym
, this->object_
,
3347 Address delta
= to
- from
;
3348 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
3350 if (stub_table
== NULL
)
3352 gold_warning(_("%s:%s: branch in non-executable section,"
3353 " no long branch stub for you"),
3354 this->object_
->name().c_str(),
3355 this->object_
->section_name(this->shndx_
).c_str());
3358 bool save_res
= (size
== 64
3360 && gsym
->source() == Symbol::IN_OUTPUT_DATA
3361 && gsym
->output_data() == target
->savres_section());
3362 ok
= stub_table
->add_long_branch_entry(this->object_
,
3364 from
, to
, save_res
);
3368 gold_debug(DEBUG_TARGET
,
3369 "branch at %s:%s+%#lx\n"
3370 "can't reach stub attached to %s:%s",
3371 this->object_
->name().c_str(),
3372 this->object_
->section_name(this->shndx_
).c_str(),
3373 (unsigned long) this->offset_
,
3374 stub_table
->relobj()->name().c_str(),
3375 stub_table
->relobj()->section_name(stub_table
->shndx()).c_str());
3380 // Relaxation hook. This is where we do stub generation.
3382 template<int size
, bool big_endian
>
3384 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
3385 const Input_objects
*,
3386 Symbol_table
* symtab
,
3390 unsigned int prev_brlt_size
= 0;
3394 = this->abiversion() < 2 && parameters
->options().plt_thread_safe();
3396 && this->abiversion() < 2
3398 && !parameters
->options().user_set_plt_thread_safe())
3400 static const char* const thread_starter
[] =
3404 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3406 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3407 "mq_notify", "create_timer",
3412 "GOMP_parallel_start",
3413 "GOMP_parallel_loop_static",
3414 "GOMP_parallel_loop_static_start",
3415 "GOMP_parallel_loop_dynamic",
3416 "GOMP_parallel_loop_dynamic_start",
3417 "GOMP_parallel_loop_guided",
3418 "GOMP_parallel_loop_guided_start",
3419 "GOMP_parallel_loop_runtime",
3420 "GOMP_parallel_loop_runtime_start",
3421 "GOMP_parallel_sections",
3422 "GOMP_parallel_sections_start",
3427 if (parameters
->options().shared())
3431 for (unsigned int i
= 0;
3432 i
< sizeof(thread_starter
) / sizeof(thread_starter
[0]);
3435 Symbol
* sym
= symtab
->lookup(thread_starter
[i
], NULL
);
3436 thread_safe
= (sym
!= NULL
3438 && sym
->in_real_elf());
3444 this->plt_thread_safe_
= thread_safe
;
3449 this->stub_group_size_
= parameters
->options().stub_group_size();
3450 bool no_size_errors
= true;
3451 if (this->stub_group_size_
== 1)
3452 this->stub_group_size_
= 0x1c00000;
3453 else if (this->stub_group_size_
== -1)
3454 this->stub_group_size_
= -0x1e00000;
3456 no_size_errors
= false;
3457 this->group_sections(layout
, task
, no_size_errors
);
3459 else if (this->relax_failed_
&& this->relax_fail_count_
< 3)
3461 this->branch_lookup_table_
.clear();
3462 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3463 p
!= this->stub_tables_
.end();
3466 (*p
)->clear_stubs(true);
3468 this->stub_tables_
.clear();
3469 this->stub_group_size_
= this->stub_group_size_
/ 4 * 3;
3470 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3471 program_name
, this->stub_group_size_
);
3472 this->group_sections(layout
, task
, true);
3475 // We need address of stub tables valid for make_stub.
3476 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3477 p
!= this->stub_tables_
.end();
3480 const Powerpc_relobj
<size
, big_endian
>* object
3481 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
3482 Address off
= object
->get_output_section_offset((*p
)->shndx());
3483 gold_assert(off
!= invalid_address
);
3484 Output_section
* os
= (*p
)->output_section();
3485 (*p
)->set_address_and_size(os
, off
);
3490 // Clear plt call stubs, long branch stubs and branch lookup table.
3491 prev_brlt_size
= this->branch_lookup_table_
.size();
3492 this->branch_lookup_table_
.clear();
3493 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3494 p
!= this->stub_tables_
.end();
3497 (*p
)->clear_stubs(false);
3501 // Build all the stubs.
3502 this->relax_failed_
= false;
3503 Stub_table
<size
, big_endian
>* ifunc_stub_table
3504 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
3505 Stub_table
<size
, big_endian
>* one_stub_table
3506 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
3507 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
3508 b
!= this->branch_info_
.end();
3511 if (!b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
)
3512 && !this->relax_failed_
)
3514 this->relax_failed_
= true;
3515 this->relax_fail_count_
++;
3516 if (this->relax_fail_count_
< 3)
3521 // Did anything change size?
3522 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
3523 bool again
= num_huge_branches
!= prev_brlt_size
;
3524 if (size
== 64 && num_huge_branches
!= 0)
3525 this->make_brlt_section(layout
);
3526 if (size
== 64 && again
)
3527 this->brlt_section_
->set_current_size(num_huge_branches
);
3529 for (typename
Stub_tables::reverse_iterator p
= this->stub_tables_
.rbegin();
3530 p
!= this->stub_tables_
.rend();
3532 (*p
)->remove_eh_frame(layout
);
3534 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3535 p
!= this->stub_tables_
.end();
3537 (*p
)->add_eh_frame(layout
);
3539 typedef Unordered_set
<Output_section
*> Output_sections
;
3540 Output_sections os_need_update
;
3541 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3542 p
!= this->stub_tables_
.end();
3545 if ((*p
)->size_update())
3548 os_need_update
.insert((*p
)->output_section());
3552 // Set output section offsets for all input sections in an output
3553 // section that just changed size. Anything past the stubs will
3555 for (typename
Output_sections::iterator p
= os_need_update
.begin();
3556 p
!= os_need_update
.end();
3559 Output_section
* os
= *p
;
3561 typedef Output_section::Input_section_list Input_section_list
;
3562 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
3563 i
!= os
->input_sections().end();
3566 off
= align_address(off
, i
->addralign());
3567 if (i
->is_input_section() || i
->is_relaxed_input_section())
3568 i
->relobj()->set_section_offset(i
->shndx(), off
);
3569 if (i
->is_relaxed_input_section())
3571 Stub_table
<size
, big_endian
>* stub_table
3572 = static_cast<Stub_table
<size
, big_endian
>*>(
3573 i
->relaxed_input_section());
3574 Address stub_table_size
= stub_table
->set_address_and_size(os
, off
);
3575 off
+= stub_table_size
;
3576 // After a few iterations, set current stub table size
3577 // as min size threshold, so later stub tables can only
3580 stub_table
->set_min_size_threshold(stub_table_size
);
3583 off
+= i
->data_size();
3585 // If .branch_lt is part of this output section, then we have
3586 // just done the offset adjustment.
3587 os
->clear_section_offsets_need_adjustment();
3592 && num_huge_branches
!= 0
3593 && parameters
->options().output_is_position_independent())
3595 // Fill in the BRLT relocs.
3596 this->brlt_section_
->reset_brlt_sizes();
3597 for (typename
Branch_lookup_table::const_iterator p
3598 = this->branch_lookup_table_
.begin();
3599 p
!= this->branch_lookup_table_
.end();
3602 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
3604 this->brlt_section_
->finalize_brlt_sizes();
3608 && (parameters
->options().user_set_emit_stub_syms()
3609 ? parameters
->options().emit_stub_syms()
3611 || parameters
->options().output_is_position_independent()
3612 || parameters
->options().emit_relocs())))
3614 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3615 p
!= this->stub_tables_
.end();
3617 (*p
)->define_stub_syms(symtab
);
3619 if (this->glink_
!= NULL
)
3621 int stub_size
= this->glink_
->pltresolve_size();
3622 Address value
= -stub_size
;
3628 this->define_local(symtab
, "__glink_PLTresolve",
3629 this->glink_
, value
, stub_size
);
3632 this->define_local(symtab
, "__glink", this->glink_
, 0, 0);
3639 template<int size
, bool big_endian
>
3641 Target_powerpc
<size
, big_endian
>::do_plt_fde_location(const Output_data
* plt
,
3642 unsigned char* oview
,
3646 uint64_t address
= plt
->address();
3647 off_t len
= plt
->data_size();
3649 if (plt
== this->glink_
)
3651 // See Output_data_glink::do_write() for glink contents.
3654 gold_assert(parameters
->doing_static_link());
3655 // Static linking may need stubs, to support ifunc and long
3656 // branches. We need to create an output section for
3657 // .eh_frame early in the link process, to have a place to
3658 // attach stub .eh_frame info. We also need to have
3659 // registered a CIE that matches the stub CIE. Both of
3660 // these requirements are satisfied by creating an FDE and
3661 // CIE for .glink, even though static linking will leave
3662 // .glink zero length.
3663 // ??? Hopefully generating an FDE with a zero address range
3664 // won't confuse anything that consumes .eh_frame info.
3666 else if (size
== 64)
3668 // There is one word before __glink_PLTresolve
3672 else if (parameters
->options().output_is_position_independent())
3674 // There are two FDEs for a position independent glink.
3675 // The first covers the branch table, the second
3676 // __glink_PLTresolve at the end of glink.
3677 off_t resolve_size
= this->glink_
->pltresolve_size();
3678 if (oview
[9] == elfcpp::DW_CFA_nop
)
3679 len
-= resolve_size
;
3682 address
+= len
- resolve_size
;
3689 // Must be a stub table.
3690 const Stub_table
<size
, big_endian
>* stub_table
3691 = static_cast<const Stub_table
<size
, big_endian
>*>(plt
);
3692 uint64_t stub_address
= stub_table
->stub_address();
3693 len
-= stub_address
- address
;
3694 address
= stub_address
;
3697 *paddress
= address
;
3701 // A class to handle the PLT data.
3703 template<int size
, bool big_endian
>
3704 class Output_data_plt_powerpc
: public Output_section_data_build
3707 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3708 size
, big_endian
> Reloc_section
;
3710 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3711 Reloc_section
* plt_rel
,
3713 : Output_section_data_build(size
== 32 ? 4 : 8),
3719 // Add an entry to the PLT.
3724 add_ifunc_entry(Symbol
*);
3727 add_local_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
3730 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
3732 // Return the .rela.plt section data.
3739 // Return the number of PLT entries.
3743 if (this->current_data_size() == 0)
3745 return ((this->current_data_size() - this->first_plt_entry_offset())
3746 / this->plt_entry_size());
3751 do_adjust_output_section(Output_section
* os
)
3756 // Write to a map file.
3758 do_print_to_mapfile(Mapfile
* mapfile
) const
3759 { mapfile
->print_output_data(this, this->name_
); }
3762 // Return the offset of the first non-reserved PLT entry.
3764 first_plt_entry_offset() const
3766 // IPLT and LPLT have no reserved entry.
3767 if (this->name_
[3] == 'I' || this->name_
[3] == 'L')
3769 return this->targ_
->first_plt_entry_offset();
3772 // Return the size of each PLT entry.
3774 plt_entry_size() const
3776 return this->targ_
->plt_entry_size();
3779 // Write out the PLT data.
3781 do_write(Output_file
*);
3783 // The reloc section.
3784 Reloc_section
* rel_
;
3785 // Allows access to .glink for do_write.
3786 Target_powerpc
<size
, big_endian
>* targ_
;
3787 // What to report in map file.
3791 // Add an entry to the PLT.
3793 template<int size
, bool big_endian
>
3795 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
3797 if (!gsym
->has_plt_offset())
3799 section_size_type off
= this->current_data_size();
3801 off
+= this->first_plt_entry_offset();
3802 gsym
->set_plt_offset(off
);
3803 gsym
->set_needs_dynsym_entry();
3804 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
3805 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
3806 off
+= this->plt_entry_size();
3807 this->set_current_data_size(off
);
3811 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3813 template<int size
, bool big_endian
>
3815 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
3817 if (!gsym
->has_plt_offset())
3819 section_size_type off
= this->current_data_size();
3820 gsym
->set_plt_offset(off
);
3821 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3822 if (size
== 64 && this->targ_
->abiversion() < 2)
3823 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3824 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
3825 off
+= this->plt_entry_size();
3826 this->set_current_data_size(off
);
3830 // Add an entry for a local symbol to the PLT.
3832 template<int size
, bool big_endian
>
3834 Output_data_plt_powerpc
<size
, big_endian
>::add_local_entry(
3835 Sized_relobj_file
<size
, big_endian
>* relobj
,
3836 unsigned int local_sym_index
)
3838 if (!relobj
->local_has_plt_offset(local_sym_index
))
3840 section_size_type off
= this->current_data_size();
3841 relobj
->set_local_plt_offset(local_sym_index
, off
);
3844 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
3845 if (size
== 64 && this->targ_
->abiversion() < 2)
3846 dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
3847 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
,
3848 dynrel
, this, off
, 0);
3850 off
+= this->plt_entry_size();
3851 this->set_current_data_size(off
);
3855 // Add an entry for a local ifunc symbol to the IPLT.
3857 template<int size
, bool big_endian
>
3859 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
3860 Sized_relobj_file
<size
, big_endian
>* relobj
,
3861 unsigned int local_sym_index
)
3863 if (!relobj
->local_has_plt_offset(local_sym_index
))
3865 section_size_type off
= this->current_data_size();
3866 relobj
->set_local_plt_offset(local_sym_index
, off
);
3867 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3868 if (size
== 64 && this->targ_
->abiversion() < 2)
3869 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3870 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
3872 off
+= this->plt_entry_size();
3873 this->set_current_data_size(off
);
3877 static const uint32_t add_0_11_11
= 0x7c0b5a14;
3878 static const uint32_t add_2_2_11
= 0x7c425a14;
3879 static const uint32_t add_2_2_12
= 0x7c426214;
3880 static const uint32_t add_3_3_2
= 0x7c631214;
3881 static const uint32_t add_3_3_13
= 0x7c636a14;
3882 static const uint32_t add_3_12_2
= 0x7c6c1214;
3883 static const uint32_t add_3_12_13
= 0x7c6c6a14;
3884 static const uint32_t add_11_0_11
= 0x7d605a14;
3885 static const uint32_t add_11_2_11
= 0x7d625a14;
3886 static const uint32_t add_11_11_2
= 0x7d6b1214;
3887 static const uint32_t addi_0_12
= 0x380c0000;
3888 static const uint32_t addi_2_2
= 0x38420000;
3889 static const uint32_t addi_3_3
= 0x38630000;
3890 static const uint32_t addi_11_11
= 0x396b0000;
3891 static const uint32_t addi_12_1
= 0x39810000;
3892 static const uint32_t addi_12_12
= 0x398c0000;
3893 static const uint32_t addis_0_2
= 0x3c020000;
3894 static const uint32_t addis_0_13
= 0x3c0d0000;
3895 static const uint32_t addis_2_12
= 0x3c4c0000;
3896 static const uint32_t addis_11_2
= 0x3d620000;
3897 static const uint32_t addis_11_11
= 0x3d6b0000;
3898 static const uint32_t addis_11_30
= 0x3d7e0000;
3899 static const uint32_t addis_12_1
= 0x3d810000;
3900 static const uint32_t addis_12_2
= 0x3d820000;
3901 static const uint32_t addis_12_12
= 0x3d8c0000;
3902 static const uint32_t b
= 0x48000000;
3903 static const uint32_t bcl_20_31
= 0x429f0005;
3904 static const uint32_t bctr
= 0x4e800420;
3905 static const uint32_t bctrl
= 0x4e800421;
3906 static const uint32_t beqlr
= 0x4d820020;
3907 static const uint32_t blr
= 0x4e800020;
3908 static const uint32_t bnectr_p4
= 0x4ce20420;
3909 static const uint32_t cmpld_7_12_0
= 0x7fac0040;
3910 static const uint32_t cmpldi_2_0
= 0x28220000;
3911 static const uint32_t cmpdi_11_0
= 0x2c2b0000;
3912 static const uint32_t cmpwi_11_0
= 0x2c0b0000;
3913 static const uint32_t cror_15_15_15
= 0x4def7b82;
3914 static const uint32_t cror_31_31_31
= 0x4ffffb82;
3915 static const uint32_t ld_0_1
= 0xe8010000;
3916 static const uint32_t ld_0_12
= 0xe80c0000;
3917 static const uint32_t ld_2_1
= 0xe8410000;
3918 static const uint32_t ld_2_2
= 0xe8420000;
3919 static const uint32_t ld_2_11
= 0xe84b0000;
3920 static const uint32_t ld_2_12
= 0xe84c0000;
3921 static const uint32_t ld_11_1
= 0xe9610000;
3922 static const uint32_t ld_11_2
= 0xe9620000;
3923 static const uint32_t ld_11_3
= 0xe9630000;
3924 static const uint32_t ld_11_11
= 0xe96b0000;
3925 static const uint32_t ld_12_2
= 0xe9820000;
3926 static const uint32_t ld_12_3
= 0xe9830000;
3927 static const uint32_t ld_12_11
= 0xe98b0000;
3928 static const uint32_t ld_12_12
= 0xe98c0000;
3929 static const uint32_t lfd_0_1
= 0xc8010000;
3930 static const uint32_t li_0_0
= 0x38000000;
3931 static const uint32_t li_12_0
= 0x39800000;
3932 static const uint32_t lis_0
= 0x3c000000;
3933 static const uint32_t lis_2
= 0x3c400000;
3934 static const uint32_t lis_11
= 0x3d600000;
3935 static const uint32_t lis_12
= 0x3d800000;
3936 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
3937 static const uint32_t lwz_0_12
= 0x800c0000;
3938 static const uint32_t lwz_11_3
= 0x81630000;
3939 static const uint32_t lwz_11_11
= 0x816b0000;
3940 static const uint32_t lwz_11_30
= 0x817e0000;
3941 static const uint32_t lwz_12_3
= 0x81830000;
3942 static const uint32_t lwz_12_12
= 0x818c0000;
3943 static const uint32_t lwzu_0_12
= 0x840c0000;
3944 static const uint32_t mflr_0
= 0x7c0802a6;
3945 static const uint32_t mflr_11
= 0x7d6802a6;
3946 static const uint32_t mflr_12
= 0x7d8802a6;
3947 static const uint32_t mr_0_3
= 0x7c601b78;
3948 static const uint32_t mr_3_0
= 0x7c030378;
3949 static const uint32_t mtctr_0
= 0x7c0903a6;
3950 static const uint32_t mtctr_11
= 0x7d6903a6;
3951 static const uint32_t mtctr_12
= 0x7d8903a6;
3952 static const uint32_t mtlr_0
= 0x7c0803a6;
3953 static const uint32_t mtlr_11
= 0x7d6803a6;
3954 static const uint32_t mtlr_12
= 0x7d8803a6;
3955 static const uint32_t nop
= 0x60000000;
3956 static const uint32_t ori_0_0_0
= 0x60000000;
3957 static const uint32_t srdi_0_0_2
= 0x7800f082;
3958 static const uint32_t std_0_1
= 0xf8010000;
3959 static const uint32_t std_0_12
= 0xf80c0000;
3960 static const uint32_t std_2_1
= 0xf8410000;
3961 static const uint32_t std_11_1
= 0xf9610000;
3962 static const uint32_t stfd_0_1
= 0xd8010000;
3963 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
3964 static const uint32_t sub_11_11_12
= 0x7d6c5850;
3965 static const uint32_t sub_12_12_11
= 0x7d8b6050;
3966 static const uint32_t xor_2_12_12
= 0x7d826278;
3967 static const uint32_t xor_11_12_12
= 0x7d8b6278;
3969 // Write out the PLT.
3971 template<int size
, bool big_endian
>
3973 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3975 if (size
== 32 && (this->name_
[3] != 'I' && this->name_
[3] != 'L'))
3977 const section_size_type offset
= this->offset();
3978 const section_size_type oview_size
3979 = convert_to_section_size_type(this->data_size());
3980 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3981 unsigned char* pov
= oview
;
3982 unsigned char* endpov
= oview
+ oview_size
;
3984 // The address of the .glink branch table
3985 const Output_data_glink
<size
, big_endian
>* glink
3986 = this->targ_
->glink_section();
3987 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
3989 while (pov
< endpov
)
3991 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
3996 of
->write_output_view(offset
, oview_size
, oview
);
4000 // Create the PLT section.
4002 template<int size
, bool big_endian
>
4004 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
4007 if (this->plt_
== NULL
)
4009 if (this->got_
== NULL
)
4010 this->got_section(symtab
, layout
);
4012 if (this->glink_
== NULL
)
4013 make_glink_section(layout
);
4015 // Ensure that .rela.dyn always appears before .rela.plt This is
4016 // necessary due to how, on PowerPC and some other targets, .rela.dyn
4017 // needs to include .rela.plt in its range.
4018 this->rela_dyn_section(layout
);
4020 Reloc_section
* plt_rel
= new Reloc_section(false);
4021 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
4022 elfcpp::SHF_ALLOC
, plt_rel
,
4023 ORDER_DYNAMIC_PLT_RELOCS
, false);
4025 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
4027 layout
->add_output_section_data(".plt",
4029 ? elfcpp::SHT_PROGBITS
4030 : elfcpp::SHT_NOBITS
),
4031 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
4038 Output_section
* rela_plt_os
= plt_rel
->output_section();
4039 rela_plt_os
->set_info_section(this->plt_
->output_section());
4043 // Create the IPLT section.
4045 template<int size
, bool big_endian
>
4047 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
4050 if (this->iplt_
== NULL
)
4052 this->make_plt_section(symtab
, layout
);
4053 this->make_lplt_section(layout
);
4055 Reloc_section
* iplt_rel
= new Reloc_section(false);
4056 if (this->rela_dyn_
->output_section())
4057 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
4059 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
4061 if (this->plt_
->output_section())
4062 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
4066 // Create the LPLT section.
4068 template<int size
, bool big_endian
>
4070 Target_powerpc
<size
, big_endian
>::make_lplt_section(Layout
* layout
)
4072 if (this->lplt_
== NULL
)
4074 Reloc_section
* lplt_rel
= NULL
;
4075 if (parameters
->options().output_is_position_independent())
4077 lplt_rel
= new Reloc_section(false);
4078 this->rela_dyn_section(layout
);
4079 if (this->rela_dyn_
->output_section())
4080 this->rela_dyn_
->output_section()
4081 ->add_output_section_data(lplt_rel
);
4084 = new Output_data_plt_powerpc
<size
, big_endian
>(this, lplt_rel
,
4086 this->make_brlt_section(layout
);
4087 if (this->brlt_section_
&& this->brlt_section_
->output_section())
4088 this->brlt_section_
->output_section()
4089 ->add_output_section_data(this->lplt_
);
4091 layout
->add_output_section_data(".branch_lt",
4092 elfcpp::SHT_PROGBITS
,
4093 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
4100 // A section for huge long branch addresses, similar to plt section.
4102 template<int size
, bool big_endian
>
4103 class Output_data_brlt_powerpc
: public Output_section_data_build
4106 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4107 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
4108 size
, big_endian
> Reloc_section
;
4110 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
4111 Reloc_section
* brlt_rel
)
4112 : Output_section_data_build(size
== 32 ? 4 : 8),
4120 this->reset_data_size();
4121 this->rel_
->reset_data_size();
4125 finalize_brlt_sizes()
4127 this->finalize_data_size();
4128 this->rel_
->finalize_data_size();
4131 // Add a reloc for an entry in the BRLT.
4133 add_reloc(Address to
, unsigned int off
)
4134 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
4136 // Update section and reloc section size.
4138 set_current_size(unsigned int num_branches
)
4140 this->reset_address_and_file_offset();
4141 this->set_current_data_size(num_branches
* 16);
4142 this->finalize_data_size();
4143 Output_section
* os
= this->output_section();
4144 os
->set_section_offsets_need_adjustment();
4145 if (this->rel_
!= NULL
)
4147 const unsigned int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
4148 this->rel_
->reset_address_and_file_offset();
4149 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
4150 this->rel_
->finalize_data_size();
4151 Output_section
* os
= this->rel_
->output_section();
4152 os
->set_section_offsets_need_adjustment();
4158 do_adjust_output_section(Output_section
* os
)
4163 // Write to a map file.
4165 do_print_to_mapfile(Mapfile
* mapfile
) const
4166 { mapfile
->print_output_data(this, "** BRLT"); }
4169 // Write out the BRLT data.
4171 do_write(Output_file
*);
4173 // The reloc section.
4174 Reloc_section
* rel_
;
4175 Target_powerpc
<size
, big_endian
>* targ_
;
4178 // Make the branch lookup table section.
4180 template<int size
, bool big_endian
>
4182 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
4184 if (size
== 64 && this->brlt_section_
== NULL
)
4186 Reloc_section
* brlt_rel
= NULL
;
4187 bool is_pic
= parameters
->options().output_is_position_independent();
4190 // When PIC we can't fill in .branch_lt but must initialise at
4191 // runtime via dynamic relocations.
4192 this->rela_dyn_section(layout
);
4193 brlt_rel
= new Reloc_section(false);
4194 if (this->rela_dyn_
->output_section())
4195 this->rela_dyn_
->output_section()
4196 ->add_output_section_data(brlt_rel
);
4199 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
4200 if (this->plt_
&& is_pic
&& this->plt_
->output_section())
4201 this->plt_
->output_section()
4202 ->add_output_section_data(this->brlt_section_
);
4204 layout
->add_output_section_data(".branch_lt",
4205 elfcpp::SHT_PROGBITS
,
4206 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
4207 this->brlt_section_
,
4213 // Write out .branch_lt when non-PIC.
4215 template<int size
, bool big_endian
>
4217 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
4219 if (size
== 64 && !parameters
->options().output_is_position_independent())
4221 const section_size_type offset
= this->offset();
4222 const section_size_type oview_size
4223 = convert_to_section_size_type(this->data_size());
4224 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4226 this->targ_
->write_branch_lookup_table(oview
);
4227 of
->write_output_view(offset
, oview_size
, oview
);
4231 static inline uint32_t
4237 static inline uint32_t
4243 static inline uint32_t
4246 return hi(a
+ 0x8000);
4252 static const unsigned char eh_frame_cie
[12];
4256 const unsigned char Eh_cie
<size
>::eh_frame_cie
[] =
4259 'z', 'R', 0, // Augmentation string.
4260 4, // Code alignment.
4261 0x80 - size
/ 8 , // Data alignment.
4263 1, // Augmentation size.
4264 (elfcpp::DW_EH_PE_pcrel
4265 | elfcpp::DW_EH_PE_sdata4
), // FDE encoding.
4266 elfcpp::DW_CFA_def_cfa
, 1, 0 // def_cfa: r1 offset 0.
4269 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4270 static const unsigned char glink_eh_frame_fde_64v1
[] =
4272 0, 0, 0, 0, // Replaced with offset to .glink.
4273 0, 0, 0, 0, // Replaced with size of .glink.
4274 0, // Augmentation size.
4275 elfcpp::DW_CFA_advance_loc
+ 1,
4276 elfcpp::DW_CFA_register
, 65, 12,
4277 elfcpp::DW_CFA_advance_loc
+ 5,
4278 elfcpp::DW_CFA_restore_extended
, 65
4281 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4282 static const unsigned char glink_eh_frame_fde_64v2
[] =
4284 0, 0, 0, 0, // Replaced with offset to .glink.
4285 0, 0, 0, 0, // Replaced with size of .glink.
4286 0, // Augmentation size.
4287 elfcpp::DW_CFA_advance_loc
+ 1,
4288 elfcpp::DW_CFA_register
, 65, 0,
4289 elfcpp::DW_CFA_advance_loc
+ 7,
4290 elfcpp::DW_CFA_restore_extended
, 65
4293 // Describe __glink_PLTresolve use of LR, 32-bit version.
4294 static const unsigned char glink_eh_frame_fde_32
[] =
4296 0, 0, 0, 0, // Replaced with offset to .glink.
4297 0, 0, 0, 0, // Replaced with size of .glink.
4298 0, // Augmentation size.
4299 elfcpp::DW_CFA_advance_loc
+ 2,
4300 elfcpp::DW_CFA_register
, 65, 0,
4301 elfcpp::DW_CFA_advance_loc
+ 4,
4302 elfcpp::DW_CFA_restore_extended
, 65
4305 static const unsigned char default_fde
[] =
4307 0, 0, 0, 0, // Replaced with offset to stubs.
4308 0, 0, 0, 0, // Replaced with size of stubs.
4309 0, // Augmentation size.
4310 elfcpp::DW_CFA_nop
, // Pad.
4315 template<bool big_endian
>
4317 write_insn(unsigned char* p
, uint32_t v
)
4319 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
4323 static inline unsigned int
4326 if (!parameters
->options().user_set_plt_align())
4327 return size
== 64 ? 32 : 8;
4328 return 1 << parameters
->options().plt_align();
4331 // Stub_table holds information about plt and long branch stubs.
4332 // Stubs are built in an area following some input section determined
4333 // by group_sections(). This input section is converted to a relaxed
4334 // input section allowing it to be resized to accommodate the stubs
4336 template<int size
, bool big_endian
>
4337 class Stub_table
: public Output_relaxed_input_section
4342 Plt_stub_ent(unsigned int off
, unsigned int indx
)
4343 : off_(off
), indx_(indx
), r2save_(0), localentry0_(0)
4347 unsigned int indx_
: 30;
4348 unsigned int r2save_
: 1;
4349 unsigned int localentry0_
: 1;
4351 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4352 static const Address invalid_address
= static_cast<Address
>(0) - 1;
4354 Stub_table(Target_powerpc
<size
, big_endian
>* targ
,
4355 Output_section
* output_section
,
4356 const Output_section::Input_section
* owner
,
4358 : Output_relaxed_input_section(owner
->relobj(), owner
->shndx(),
4360 ->section_addralign(owner
->shndx())),
4361 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
4362 orig_data_size_(owner
->current_data_size()),
4363 plt_size_(0), last_plt_size_(0),
4364 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4365 need_save_res_(false), uniq_(id
), tls_get_addr_opt_bctrl_(-1u),
4368 this->set_output_section(output_section
);
4370 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
4371 new_relaxed
.push_back(this);
4372 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
4375 // Add a plt call stub.
4377 add_plt_call_entry(Address
,
4378 const Sized_relobj_file
<size
, big_endian
>*,
4385 add_plt_call_entry(Address
,
4386 const Sized_relobj_file
<size
, big_endian
>*,
4392 // Find a given plt call stub.
4394 find_plt_call_entry(const Symbol
*) const;
4397 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4398 unsigned int) const;
4401 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4407 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4412 // Add a long branch stub.
4414 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
4415 unsigned int, Address
, Address
, bool);
4418 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
4422 can_reach_stub(Address from
, unsigned int off
, unsigned int r_type
)
4424 Address max_branch_offset
= max_branch_delta(r_type
);
4425 if (max_branch_offset
== 0)
4427 gold_assert(from
!= invalid_address
);
4428 Address loc
= off
+ this->stub_address();
4429 return loc
- from
+ max_branch_offset
< 2 * max_branch_offset
;
4433 clear_stubs(bool all
)
4435 this->plt_call_stubs_
.clear();
4436 this->plt_size_
= 0;
4437 this->long_branch_stubs_
.clear();
4438 this->branch_size_
= 0;
4439 this->need_save_res_
= false;
4442 this->last_plt_size_
= 0;
4443 this->last_branch_size_
= 0;
4448 set_address_and_size(const Output_section
* os
, Address off
)
4450 Address start_off
= off
;
4451 off
+= this->orig_data_size_
;
4452 Address my_size
= this->plt_size_
+ this->branch_size_
;
4453 if (this->need_save_res_
)
4454 my_size
+= this->targ_
->savres_section()->data_size();
4456 off
= align_address(off
, this->stub_align());
4457 // Include original section size and alignment padding in size
4458 my_size
+= off
- start_off
;
4459 // Ensure new size is always larger than min size
4460 // threshold. Alignment requirement is included in "my_size", so
4461 // increase "my_size" does not invalidate alignment.
4462 if (my_size
< this->min_size_threshold_
)
4463 my_size
= this->min_size_threshold_
;
4464 this->reset_address_and_file_offset();
4465 this->set_current_data_size(my_size
);
4466 this->set_address_and_file_offset(os
->address() + start_off
,
4467 os
->offset() + start_off
);
4472 stub_address() const
4474 return align_address(this->address() + this->orig_data_size_
,
4475 this->stub_align());
4481 return align_address(this->offset() + this->orig_data_size_
,
4482 this->stub_align());
4487 { return this->plt_size_
; }
4490 set_min_size_threshold(Address min_size
)
4491 { this->min_size_threshold_
= min_size
; }
4494 define_stub_syms(Symbol_table
*);
4499 Output_section
* os
= this->output_section();
4500 if (os
->addralign() < this->stub_align())
4502 os
->set_addralign(this->stub_align());
4503 // FIXME: get rid of the insane checkpointing.
4504 // We can't increase alignment of the input section to which
4505 // stubs are attached; The input section may be .init which
4506 // is pasted together with other .init sections to form a
4507 // function. Aligning might insert zero padding resulting in
4508 // sigill. However we do need to increase alignment of the
4509 // output section so that the align_address() on offset in
4510 // set_address_and_size() adds the same padding as the
4511 // align_address() on address in stub_address().
4512 // What's more, we need this alignment for the layout done in
4513 // relaxation_loop_body() so that the output section starts at
4514 // a suitably aligned address.
4515 os
->checkpoint_set_addralign(this->stub_align());
4517 if (this->last_plt_size_
!= this->plt_size_
4518 || this->last_branch_size_
!= this->branch_size_
)
4520 this->last_plt_size_
= this->plt_size_
;
4521 this->last_branch_size_
= this->branch_size_
;
4527 // Generate a suitable FDE to describe code in this stub group.
4531 // Add .eh_frame info for this stub section.
4533 add_eh_frame(Layout
* layout
);
4535 // Remove .eh_frame info for this stub section.
4537 remove_eh_frame(Layout
* layout
);
4539 Target_powerpc
<size
, big_endian
>*
4545 class Plt_stub_key_hash
;
4546 typedef Unordered_map
<Plt_stub_key
, Plt_stub_ent
,
4547 Plt_stub_key_hash
> Plt_stub_entries
;
4548 class Branch_stub_ent
;
4549 class Branch_stub_ent_hash
;
4550 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
4551 Branch_stub_ent_hash
> Branch_stub_entries
;
4553 // Alignment of stub section.
4557 unsigned int min_align
= size
== 64 ? 32 : 16;
4558 unsigned int user_align
= 1 << parameters
->options().plt_align();
4559 return std::max(user_align
, min_align
);
4562 // Return the plt offset for the given call stub.
4564 plt_off(typename
Plt_stub_entries::const_iterator p
,
4565 const Output_data_plt_powerpc
<size
, big_endian
>** sec
) const
4567 const Symbol
* gsym
= p
->first
.sym_
;
4569 return this->targ_
->plt_off(gsym
, sec
);
4572 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
4573 unsigned int local_sym_index
= p
->first
.locsym_
;
4574 return this->targ_
->plt_off(relobj
, local_sym_index
, sec
);
4578 // Size of a given plt call stub.
4580 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
4584 const Symbol
* gsym
= p
->first
.sym_
;
4586 + (this->targ_
->is_tls_get_addr_opt(gsym
) ? 8 * 4 : 0));
4589 const Output_data_plt_powerpc
<size
, big_endian
>* plt
;
4590 Address plt_addr
= this->plt_off(p
, &plt
);
4591 plt_addr
+= plt
->address();
4592 Address got_addr
= this->targ_
->got_section()->output_section()->address();
4593 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4594 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
4595 got_addr
+= ppcobj
->toc_base_offset();
4596 Address off
= plt_addr
- got_addr
;
4597 unsigned int bytes
= 4 * 4 + 4 * (ha(off
) != 0);
4598 const Symbol
* gsym
= p
->first
.sym_
;
4599 if (this->targ_
->is_tls_get_addr_opt(gsym
))
4601 if (this->targ_
->abiversion() < 2)
4603 bool static_chain
= parameters
->options().plt_static_chain();
4604 bool thread_safe
= this->targ_
->plt_thread_safe();
4608 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
4614 plt_call_align(unsigned int bytes
) const
4616 unsigned int align
= param_plt_align
<size
>();
4617 return (bytes
+ align
- 1) & -align
;
4620 // Return long branch stub size.
4622 branch_stub_size(typename
Branch_stub_entries::const_iterator p
)
4624 Address loc
= this->stub_address() + this->last_plt_size_
+ p
->second
;
4625 if (p
->first
.dest_
- loc
+ (1 << 25) < 2 << 25)
4627 unsigned int bytes
= 16;
4628 if (size
== 32 && parameters
->options().output_is_position_independent())
4635 do_write(Output_file
*);
4637 // Plt call stub keys.
4641 Plt_stub_key(const Symbol
* sym
)
4642 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4645 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4646 unsigned int locsym_index
)
4647 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4650 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4652 unsigned int r_type
,
4654 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4657 this->addend_
= addend
;
4658 else if (parameters
->options().output_is_position_independent()
4659 && (r_type
== elfcpp::R_PPC_PLTREL24
4660 || r_type
== elfcpp::R_POWERPC_PLTCALL
))
4662 this->addend_
= addend
;
4663 if (this->addend_
>= 32768)
4664 this->object_
= object
;
4668 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4669 unsigned int locsym_index
,
4670 unsigned int r_type
,
4672 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4675 this->addend_
= addend
;
4676 else if (parameters
->options().output_is_position_independent()
4677 && (r_type
== elfcpp::R_PPC_PLTREL24
4678 || r_type
== elfcpp::R_POWERPC_PLTCALL
))
4679 this->addend_
= addend
;
4682 bool operator==(const Plt_stub_key
& that
) const
4684 return (this->sym_
== that
.sym_
4685 && this->object_
== that
.object_
4686 && this->addend_
== that
.addend_
4687 && this->locsym_
== that
.locsym_
);
4691 const Sized_relobj_file
<size
, big_endian
>* object_
;
4692 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
4693 unsigned int locsym_
;
4696 class Plt_stub_key_hash
4699 size_t operator()(const Plt_stub_key
& ent
) const
4701 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
4702 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
4708 // Long branch stub keys.
4709 class Branch_stub_ent
4712 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
,
4713 Address to
, bool save_res
)
4714 : dest_(to
), toc_base_off_(0), save_res_(save_res
)
4717 toc_base_off_
= obj
->toc_base_offset();
4720 bool operator==(const Branch_stub_ent
& that
) const
4722 return (this->dest_
== that
.dest_
4724 || this->toc_base_off_
== that
.toc_base_off_
));
4728 unsigned int toc_base_off_
;
4732 class Branch_stub_ent_hash
4735 size_t operator()(const Branch_stub_ent
& ent
) const
4736 { return ent
.dest_
^ ent
.toc_base_off_
; }
4739 // In a sane world this would be a global.
4740 Target_powerpc
<size
, big_endian
>* targ_
;
4741 // Map sym/object/addend to stub offset.
4742 Plt_stub_entries plt_call_stubs_
;
4743 // Map destination address to stub offset.
4744 Branch_stub_entries long_branch_stubs_
;
4745 // size of input section
4746 section_size_type orig_data_size_
;
4748 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
4749 // Some rare cases cause (PR/20529) fluctuation in stub table
4750 // size, which leads to an endless relax loop. This is to be fixed
4751 // by, after the first few iterations, allowing only increase of
4752 // stub table size. This variable sets the minimal possible size of
4753 // a stub table, it is zero for the first few iterations, then
4754 // increases monotonically.
4755 Address min_size_threshold_
;
4756 // Set if this stub group needs a copy of out-of-line register
4757 // save/restore functions.
4758 bool need_save_res_
;
4759 // Per stub table unique identifier.
4761 // The bctrl in the __tls_get_addr_opt stub, if present.
4762 unsigned int tls_get_addr_opt_bctrl_
;
4763 // FDE unwind info for this stub group.
4764 unsigned int plt_fde_len_
;
4765 unsigned char plt_fde_
[20];
4768 // Add a plt call stub, if we do not already have one for this
4769 // sym/object/addend combo.
4771 template<int size
, bool big_endian
>
4773 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4775 const Sized_relobj_file
<size
, big_endian
>* object
,
4777 unsigned int r_type
,
4781 Plt_stub_key
key(object
, gsym
, r_type
, addend
);
4782 Plt_stub_ent
ent(this->plt_size_
, this->plt_call_stubs_
.size());
4783 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4784 = this->plt_call_stubs_
.insert(std::make_pair(key
, ent
));
4787 this->plt_size_
= ent
.off_
+ this->plt_call_size(p
.first
);
4789 && this->targ_
->is_elfv2_localentry0(gsym
))
4791 p
.first
->second
.localentry0_
= 1;
4792 this->targ_
->set_has_localentry0();
4794 if (this->targ_
->is_tls_get_addr_opt(gsym
))
4796 this->targ_
->set_has_tls_get_addr_opt();
4797 this->tls_get_addr_opt_bctrl_
= this->plt_size_
- 5 * 4;
4799 this->plt_size_
= this->plt_call_align(this->plt_size_
);
4803 && !p
.first
->second
.localentry0_
)
4804 p
.first
->second
.r2save_
= 1;
4805 return this->can_reach_stub(from
, ent
.off_
, r_type
);
4808 template<int size
, bool big_endian
>
4810 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4812 const Sized_relobj_file
<size
, big_endian
>* object
,
4813 unsigned int locsym_index
,
4814 unsigned int r_type
,
4818 Plt_stub_key
key(object
, locsym_index
, r_type
, addend
);
4819 Plt_stub_ent
ent(this->plt_size_
, this->plt_call_stubs_
.size());
4820 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4821 = this->plt_call_stubs_
.insert(std::make_pair(key
, ent
));
4824 this->plt_size_
= ent
.off_
+ this->plt_call_size(p
.first
);
4825 this->plt_size_
= this->plt_call_align(this->plt_size_
);
4827 && this->targ_
->is_elfv2_localentry0(object
, locsym_index
))
4829 p
.first
->second
.localentry0_
= 1;
4830 this->targ_
->set_has_localentry0();
4835 && !p
.first
->second
.localentry0_
)
4836 p
.first
->second
.r2save_
= 1;
4837 return this->can_reach_stub(from
, ent
.off_
, r_type
);
4840 // Find a plt call stub.
4842 template<int size
, bool big_endian
>
4843 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4844 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4845 const Sized_relobj_file
<size
, big_endian
>* object
,
4847 unsigned int r_type
,
4848 Address addend
) const
4850 Plt_stub_key
key(object
, gsym
, r_type
, addend
);
4851 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4852 if (p
== this->plt_call_stubs_
.end())
4857 template<int size
, bool big_endian
>
4858 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4859 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
4861 Plt_stub_key
key(gsym
);
4862 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4863 if (p
== this->plt_call_stubs_
.end())
4868 template<int size
, bool big_endian
>
4869 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4870 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4871 const Sized_relobj_file
<size
, big_endian
>* object
,
4872 unsigned int locsym_index
,
4873 unsigned int r_type
,
4874 Address addend
) const
4876 Plt_stub_key
key(object
, locsym_index
, r_type
, addend
);
4877 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4878 if (p
== this->plt_call_stubs_
.end())
4883 template<int size
, bool big_endian
>
4884 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4885 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4886 const Sized_relobj_file
<size
, big_endian
>* object
,
4887 unsigned int locsym_index
) const
4889 Plt_stub_key
key(object
, locsym_index
);
4890 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4891 if (p
== this->plt_call_stubs_
.end())
4896 // Add a long branch stub if we don't already have one to given
4899 template<int size
, bool big_endian
>
4901 Stub_table
<size
, big_endian
>::add_long_branch_entry(
4902 const Powerpc_relobj
<size
, big_endian
>* object
,
4903 unsigned int r_type
,
4908 Branch_stub_ent
ent(object
, to
, save_res
);
4909 Address off
= this->branch_size_
;
4910 std::pair
<typename
Branch_stub_entries::iterator
, bool> p
4911 = this->long_branch_stubs_
.insert(std::make_pair(ent
, off
));
4915 this->need_save_res_
= true;
4918 unsigned int stub_size
= this->branch_stub_size(p
.first
);
4919 this->branch_size_
= off
+ stub_size
;
4920 if (size
== 64 && stub_size
!= 4)
4921 this->targ_
->add_branch_lookup_table(to
);
4924 return this->can_reach_stub(from
, off
, r_type
);
4927 // Find long branch stub offset.
4929 template<int size
, bool big_endian
>
4930 typename Stub_table
<size
, big_endian
>::Address
4931 Stub_table
<size
, big_endian
>::find_long_branch_entry(
4932 const Powerpc_relobj
<size
, big_endian
>* object
,
4935 Branch_stub_ent
ent(object
, to
, false);
4936 typename
Branch_stub_entries::const_iterator p
4937 = this->long_branch_stubs_
.find(ent
);
4938 if (p
== this->long_branch_stubs_
.end())
4939 return invalid_address
;
4940 if (p
->first
.save_res_
)
4941 return to
- this->targ_
->savres_section()->address() + this->branch_size_
;
4945 // Generate a suitable FDE to describe code in this stub group.
4946 // The __tls_get_addr_opt call stub needs to describe where it saves
4947 // LR, to support exceptions that might be thrown from __tls_get_addr.
4949 template<int size
, bool big_endian
>
4951 Stub_table
<size
, big_endian
>::init_plt_fde()
4953 unsigned char* p
= this->plt_fde_
;
4954 // offset pcrel sdata4, size udata4, and augmentation size byte.
4957 if (this->tls_get_addr_opt_bctrl_
!= -1u)
4959 unsigned int to_bctrl
= this->tls_get_addr_opt_bctrl_
/ 4;
4961 *p
++ = elfcpp::DW_CFA_advance_loc
+ to_bctrl
;
4962 else if (to_bctrl
< 256)
4964 *p
++ = elfcpp::DW_CFA_advance_loc1
;
4967 else if (to_bctrl
< 65536)
4969 *p
++ = elfcpp::DW_CFA_advance_loc2
;
4970 elfcpp::Swap
<16, big_endian
>::writeval(p
, to_bctrl
);
4975 *p
++ = elfcpp::DW_CFA_advance_loc4
;
4976 elfcpp::Swap
<32, big_endian
>::writeval(p
, to_bctrl
);
4979 *p
++ = elfcpp::DW_CFA_offset_extended_sf
;
4981 *p
++ = -(this->targ_
->stk_linker() / 8) & 0x7f;
4982 *p
++ = elfcpp::DW_CFA_advance_loc
+ 4;
4983 *p
++ = elfcpp::DW_CFA_restore_extended
;
4986 this->plt_fde_len_
= p
- this->plt_fde_
;
4989 // Add .eh_frame info for this stub section. Unlike other linker
4990 // generated .eh_frame this is added late in the link, because we
4991 // only want the .eh_frame info if this particular stub section is
4994 template<int size
, bool big_endian
>
4996 Stub_table
<size
, big_endian
>::add_eh_frame(Layout
* layout
)
4998 if (!parameters
->options().ld_generated_unwind_info())
5001 // Since we add stub .eh_frame info late, it must be placed
5002 // after all other linker generated .eh_frame info so that
5003 // merge mapping need not be updated for input sections.
5004 // There is no provision to use a different CIE to that used
5006 if (!this->targ_
->has_glink())
5009 if (this->plt_size_
+ this->branch_size_
+ this->need_save_res_
== 0)
5012 this->init_plt_fde();
5013 layout
->add_eh_frame_for_plt(this,
5014 Eh_cie
<size
>::eh_frame_cie
,
5015 sizeof (Eh_cie
<size
>::eh_frame_cie
),
5016 this->plt_fde_
, this->plt_fde_len_
);
5019 template<int size
, bool big_endian
>
5021 Stub_table
<size
, big_endian
>::remove_eh_frame(Layout
* layout
)
5023 if (this->plt_fde_len_
!= 0)
5025 layout
->remove_eh_frame_for_plt(this,
5026 Eh_cie
<size
>::eh_frame_cie
,
5027 sizeof (Eh_cie
<size
>::eh_frame_cie
),
5028 this->plt_fde_
, this->plt_fde_len_
);
5029 this->plt_fde_len_
= 0;
5033 // A class to handle .glink.
5035 template<int size
, bool big_endian
>
5036 class Output_data_glink
: public Output_section_data
5039 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
5040 static const Address invalid_address
= static_cast<Address
>(0) - 1;
5042 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
5043 : Output_section_data(16), targ_(targ
), global_entry_stubs_(),
5044 end_branch_table_(), ge_size_(0)
5048 add_eh_frame(Layout
* layout
);
5051 add_global_entry(const Symbol
*);
5054 find_global_entry(const Symbol
*) const;
5057 global_entry_align(unsigned int off
) const
5059 unsigned int align
= param_plt_align
<size
>();
5060 return (off
+ align
- 1) & -align
;
5064 global_entry_off() const
5066 return this->global_entry_align(this->end_branch_table_
);
5070 global_entry_address() const
5072 gold_assert(this->is_data_size_valid());
5073 return this->address() + this->global_entry_off();
5077 pltresolve_size() const
5081 + (this->targ_
->abiversion() < 2 ? 11 * 4 : 14 * 4));
5086 // Write to a map file.
5088 do_print_to_mapfile(Mapfile
* mapfile
) const
5089 { mapfile
->print_output_data(this, _("** glink")); }
5093 set_final_data_size();
5097 do_write(Output_file
*);
5099 // Allows access to .got and .plt for do_write.
5100 Target_powerpc
<size
, big_endian
>* targ_
;
5102 // Map sym to stub offset.
5103 typedef Unordered_map
<const Symbol
*, unsigned int> Global_entry_stub_entries
;
5104 Global_entry_stub_entries global_entry_stubs_
;
5106 unsigned int end_branch_table_
, ge_size_
;
5109 template<int size
, bool big_endian
>
5111 Output_data_glink
<size
, big_endian
>::add_eh_frame(Layout
* layout
)
5113 if (!parameters
->options().ld_generated_unwind_info())
5118 if (this->targ_
->abiversion() < 2)
5119 layout
->add_eh_frame_for_plt(this,
5120 Eh_cie
<64>::eh_frame_cie
,
5121 sizeof (Eh_cie
<64>::eh_frame_cie
),
5122 glink_eh_frame_fde_64v1
,
5123 sizeof (glink_eh_frame_fde_64v1
));
5125 layout
->add_eh_frame_for_plt(this,
5126 Eh_cie
<64>::eh_frame_cie
,
5127 sizeof (Eh_cie
<64>::eh_frame_cie
),
5128 glink_eh_frame_fde_64v2
,
5129 sizeof (glink_eh_frame_fde_64v2
));
5133 // 32-bit .glink can use the default since the CIE return
5134 // address reg, LR, is valid.
5135 layout
->add_eh_frame_for_plt(this,
5136 Eh_cie
<32>::eh_frame_cie
,
5137 sizeof (Eh_cie
<32>::eh_frame_cie
),
5139 sizeof (default_fde
));
5140 // Except where LR is used in a PIC __glink_PLTresolve.
5141 if (parameters
->options().output_is_position_independent())
5142 layout
->add_eh_frame_for_plt(this,
5143 Eh_cie
<32>::eh_frame_cie
,
5144 sizeof (Eh_cie
<32>::eh_frame_cie
),
5145 glink_eh_frame_fde_32
,
5146 sizeof (glink_eh_frame_fde_32
));
5150 template<int size
, bool big_endian
>
5152 Output_data_glink
<size
, big_endian
>::add_global_entry(const Symbol
* gsym
)
5154 unsigned int off
= this->global_entry_align(this->ge_size_
);
5155 std::pair
<typename
Global_entry_stub_entries::iterator
, bool> p
5156 = this->global_entry_stubs_
.insert(std::make_pair(gsym
, off
));
5158 this->ge_size_
= off
+ 16;
5161 template<int size
, bool big_endian
>
5162 typename Output_data_glink
<size
, big_endian
>::Address
5163 Output_data_glink
<size
, big_endian
>::find_global_entry(const Symbol
* gsym
) const
5165 typename
Global_entry_stub_entries::const_iterator p
5166 = this->global_entry_stubs_
.find(gsym
);
5167 return p
== this->global_entry_stubs_
.end() ? invalid_address
: p
->second
;
5170 template<int size
, bool big_endian
>
5172 Output_data_glink
<size
, big_endian
>::set_final_data_size()
5174 unsigned int count
= this->targ_
->plt_entry_count();
5175 section_size_type total
= 0;
5181 // space for branch table
5182 total
+= 4 * (count
- 1);
5184 total
+= -total
& 15;
5185 total
+= this->pltresolve_size();
5189 total
+= this->pltresolve_size();
5191 // space for branch table
5193 if (this->targ_
->abiversion() < 2)
5197 total
+= 4 * (count
- 0x8000);
5201 this->end_branch_table_
= total
;
5202 total
= this->global_entry_align(total
);
5203 total
+= this->ge_size_
;
5205 this->set_data_size(total
);
5208 // Define symbols on stubs, identifying the stub.
5210 template<int size
, bool big_endian
>
5212 Stub_table
<size
, big_endian
>::define_stub_syms(Symbol_table
* symtab
)
5214 if (!this->plt_call_stubs_
.empty())
5216 // The key for the plt call stub hash table includes addresses,
5217 // therefore traversal order depends on those addresses, which
5218 // can change between runs if gold is a PIE. Unfortunately the
5219 // output .symtab ordering depends on the order in which symbols
5220 // are added to the linker symtab. We want reproducible output
5221 // so must sort the call stub symbols.
5222 typedef typename
Plt_stub_entries::const_iterator plt_iter
;
5223 std::vector
<plt_iter
> sorted
;
5224 sorted
.resize(this->plt_call_stubs_
.size());
5226 for (plt_iter cs
= this->plt_call_stubs_
.begin();
5227 cs
!= this->plt_call_stubs_
.end();
5229 sorted
[cs
->second
.indx_
] = cs
;
5231 for (unsigned int i
= 0; i
< this->plt_call_stubs_
.size(); ++i
)
5233 plt_iter cs
= sorted
[i
];
5236 if (cs
->first
.addend_
!= 0)
5237 sprintf(add
, "+%x", static_cast<uint32_t>(cs
->first
.addend_
));
5240 if (cs
->first
.object_
)
5242 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
5243 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
5244 sprintf(obj
, "%x:", ppcobj
->uniq());
5247 const char *symname
;
5248 if (cs
->first
.sym_
== NULL
)
5250 sprintf(localname
, "%x", cs
->first
.locsym_
);
5251 symname
= localname
;
5253 else if (this->targ_
->is_tls_get_addr_opt(cs
->first
.sym_
))
5254 symname
= this->targ_
->tls_get_addr_opt()->name();
5256 symname
= cs
->first
.sym_
->name();
5257 char* name
= new char[8 + 10 + strlen(obj
) + strlen(symname
) + strlen(add
) + 1];
5258 sprintf(name
, "%08x.plt_call.%s%s%s", this->uniq_
, obj
, symname
, add
);
5260 = this->stub_address() - this->address() + cs
->second
.off_
;
5261 unsigned int stub_size
= this->plt_call_align(this->plt_call_size(cs
));
5262 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
5266 typedef typename
Branch_stub_entries::const_iterator branch_iter
;
5267 for (branch_iter bs
= this->long_branch_stubs_
.begin();
5268 bs
!= this->long_branch_stubs_
.end();
5271 if (bs
->first
.save_res_
)
5274 char* name
= new char[8 + 13 + 16 + 1];
5275 sprintf(name
, "%08x.long_branch.%llx", this->uniq_
,
5276 static_cast<unsigned long long>(bs
->first
.dest_
));
5277 Address value
= (this->stub_address() - this->address()
5278 + this->plt_size_
+ bs
->second
);
5279 unsigned int stub_size
= this->branch_stub_size(bs
);
5280 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
5284 // Write out plt and long branch stub code.
5286 template<int size
, bool big_endian
>
5288 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
5290 if (this->plt_call_stubs_
.empty()
5291 && this->long_branch_stubs_
.empty())
5294 const section_size_type start_off
= this->offset();
5295 const section_size_type off
= this->stub_offset();
5296 const section_size_type oview_size
=
5297 convert_to_section_size_type(this->data_size() - (off
- start_off
));
5298 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5303 const Output_data_got_powerpc
<size
, big_endian
>* got
5304 = this->targ_
->got_section();
5305 Address got_os_addr
= got
->output_section()->address();
5307 if (!this->plt_call_stubs_
.empty())
5309 // Write out plt call stubs.
5310 typename
Plt_stub_entries::const_iterator cs
;
5311 for (cs
= this->plt_call_stubs_
.begin();
5312 cs
!= this->plt_call_stubs_
.end();
5315 const Output_data_plt_powerpc
<size
, big_endian
>* plt
;
5316 Address pltoff
= this->plt_off(cs
, &plt
);
5317 Address plt_addr
= pltoff
+ plt
->address();
5318 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
5319 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
5320 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
5321 Address off
= plt_addr
- got_addr
;
5323 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
5324 gold_error(_("%s: linkage table error against `%s'"),
5325 cs
->first
.object_
->name().c_str(),
5326 cs
->first
.sym_
->demangled_name().c_str());
5328 bool plt_load_toc
= this->targ_
->abiversion() < 2;
5330 = plt_load_toc
&& parameters
->options().plt_static_chain();
5332 = plt_load_toc
&& this->targ_
->plt_thread_safe();
5333 bool use_fake_dep
= false;
5334 Address cmp_branch_off
= 0;
5337 unsigned int pltindex
5338 = ((pltoff
- this->targ_
->first_plt_entry_offset())
5339 / this->targ_
->plt_entry_size());
5341 = (this->targ_
->glink_section()->pltresolve_size()
5343 if (pltindex
> 32768)
5344 glinkoff
+= (pltindex
- 32768) * 4;
5346 = this->targ_
->glink_section()->address() + glinkoff
;
5348 = (this->stub_address() + cs
->second
.off_
+ 20
5349 + 4 * cs
->second
.r2save_
5350 + 4 * (ha(off
) != 0)
5351 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
5352 + 4 * static_chain
);
5353 cmp_branch_off
= to
- from
;
5354 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
5357 p
= oview
+ cs
->second
.off_
;
5358 const Symbol
* gsym
= cs
->first
.sym_
;
5359 if (this->targ_
->is_tls_get_addr_opt(gsym
))
5361 write_insn
<big_endian
>(p
, ld_11_3
+ 0);
5363 write_insn
<big_endian
>(p
, ld_12_3
+ 8);
5365 write_insn
<big_endian
>(p
, mr_0_3
);
5367 write_insn
<big_endian
>(p
, cmpdi_11_0
);
5369 write_insn
<big_endian
>(p
, add_3_12_13
);
5371 write_insn
<big_endian
>(p
, beqlr
);
5373 write_insn
<big_endian
>(p
, mr_3_0
);
5375 if (!cs
->second
.localentry0_
)
5377 write_insn
<big_endian
>(p
, mflr_11
);
5379 write_insn
<big_endian
>(p
, (std_11_1
5380 + this->targ_
->stk_linker()));
5383 use_fake_dep
= thread_safe
;
5387 if (cs
->second
.r2save_
)
5389 write_insn
<big_endian
>(p
,
5390 std_2_1
+ this->targ_
->stk_toc());
5395 write_insn
<big_endian
>(p
, addis_11_2
+ ha(off
));
5397 write_insn
<big_endian
>(p
, ld_12_11
+ l(off
));
5402 write_insn
<big_endian
>(p
, addis_12_2
+ ha(off
));
5404 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
));
5408 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
5410 write_insn
<big_endian
>(p
, addi_11_11
+ l(off
));
5414 write_insn
<big_endian
>(p
, mtctr_12
);
5420 write_insn
<big_endian
>(p
, xor_2_12_12
);
5422 write_insn
<big_endian
>(p
, add_11_11_2
);
5425 write_insn
<big_endian
>(p
, ld_2_11
+ l(off
+ 8));
5429 write_insn
<big_endian
>(p
, ld_11_11
+ l(off
+ 16));
5436 if (cs
->second
.r2save_
)
5438 write_insn
<big_endian
>(p
,
5439 std_2_1
+ this->targ_
->stk_toc());
5442 write_insn
<big_endian
>(p
, ld_12_2
+ l(off
));
5445 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
5447 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
));
5451 write_insn
<big_endian
>(p
, mtctr_12
);
5457 write_insn
<big_endian
>(p
, xor_11_12_12
);
5459 write_insn
<big_endian
>(p
, add_2_2_11
);
5464 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16));
5467 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8));
5471 if (!cs
->second
.localentry0_
5472 && this->targ_
->is_tls_get_addr_opt(gsym
))
5474 write_insn
<big_endian
>(p
, bctrl
);
5476 write_insn
<big_endian
>(p
, ld_2_1
+ this->targ_
->stk_toc());
5478 write_insn
<big_endian
>(p
, ld_11_1
+ this->targ_
->stk_linker());
5480 write_insn
<big_endian
>(p
, mtlr_11
);
5482 write_insn
<big_endian
>(p
, blr
);
5484 else if (thread_safe
&& !use_fake_dep
)
5486 write_insn
<big_endian
>(p
, cmpldi_2_0
);
5488 write_insn
<big_endian
>(p
, bnectr_p4
);
5490 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
5493 write_insn
<big_endian
>(p
, bctr
);
5497 // Write out long branch stubs.
5498 typename
Branch_stub_entries::const_iterator bs
;
5499 for (bs
= this->long_branch_stubs_
.begin();
5500 bs
!= this->long_branch_stubs_
.end();
5503 if (bs
->first
.save_res_
)
5505 p
= oview
+ this->plt_size_
+ bs
->second
;
5506 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
5507 Address delta
= bs
->first
.dest_
- loc
;
5508 if (delta
+ (1 << 25) < 2 << 25)
5509 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
5513 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
5514 gold_assert(brlt_addr
!= invalid_address
);
5515 brlt_addr
+= this->targ_
->brlt_section()->address();
5516 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
5517 Address brltoff
= brlt_addr
- got_addr
;
5518 if (ha(brltoff
) == 0)
5520 write_insn
<big_endian
>(p
, ld_12_2
+ l(brltoff
)), p
+= 4;
5524 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
5525 write_insn
<big_endian
>(p
, ld_12_12
+ l(brltoff
)), p
+= 4;
5527 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5528 write_insn
<big_endian
>(p
, bctr
);
5534 if (!this->plt_call_stubs_
.empty())
5536 // The address of _GLOBAL_OFFSET_TABLE_.
5537 Address g_o_t
= invalid_address
;
5539 // Write out plt call stubs.
5540 typename
Plt_stub_entries::const_iterator cs
;
5541 for (cs
= this->plt_call_stubs_
.begin();
5542 cs
!= this->plt_call_stubs_
.end();
5545 const Output_data_plt_powerpc
<size
, big_endian
>* plt
;
5546 Address plt_addr
= this->plt_off(cs
, &plt
);
5547 plt_addr
+= plt
->address();
5549 p
= oview
+ cs
->second
.off_
;
5550 const Symbol
* gsym
= cs
->first
.sym_
;
5551 if (this->targ_
->is_tls_get_addr_opt(gsym
))
5553 write_insn
<big_endian
>(p
, lwz_11_3
+ 0);
5555 write_insn
<big_endian
>(p
, lwz_12_3
+ 4);
5557 write_insn
<big_endian
>(p
, mr_0_3
);
5559 write_insn
<big_endian
>(p
, cmpwi_11_0
);
5561 write_insn
<big_endian
>(p
, add_3_12_2
);
5563 write_insn
<big_endian
>(p
, beqlr
);
5565 write_insn
<big_endian
>(p
, mr_3_0
);
5567 write_insn
<big_endian
>(p
, nop
);
5570 if (parameters
->options().output_is_position_independent())
5573 const Powerpc_relobj
<size
, big_endian
>* ppcobj
5574 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
5575 (cs
->first
.object_
));
5576 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
5578 unsigned int got2
= ppcobj
->got2_shndx();
5579 got_addr
= ppcobj
->get_output_section_offset(got2
);
5580 gold_assert(got_addr
!= invalid_address
);
5581 got_addr
+= (ppcobj
->output_section(got2
)->address()
5582 + cs
->first
.addend_
);
5586 if (g_o_t
== invalid_address
)
5588 const Output_data_got_powerpc
<size
, big_endian
>* got
5589 = this->targ_
->got_section();
5590 g_o_t
= got
->address() + got
->g_o_t();
5595 Address off
= plt_addr
- got_addr
;
5597 write_insn
<big_endian
>(p
, lwz_11_30
+ l(off
));
5600 write_insn
<big_endian
>(p
, addis_11_30
+ ha(off
));
5602 write_insn
<big_endian
>(p
, lwz_11_11
+ l(off
));
5607 write_insn
<big_endian
>(p
, lis_11
+ ha(plt_addr
));
5609 write_insn
<big_endian
>(p
, lwz_11_11
+ l(plt_addr
));
5612 write_insn
<big_endian
>(p
, mtctr_11
);
5614 write_insn
<big_endian
>(p
, bctr
);
5618 // Write out long branch stubs.
5619 typename
Branch_stub_entries::const_iterator bs
;
5620 for (bs
= this->long_branch_stubs_
.begin();
5621 bs
!= this->long_branch_stubs_
.end();
5624 if (bs
->first
.save_res_
)
5626 p
= oview
+ this->plt_size_
+ bs
->second
;
5627 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
5628 Address delta
= bs
->first
.dest_
- loc
;
5629 if (delta
+ (1 << 25) < 2 << 25)
5630 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
5631 else if (!parameters
->options().output_is_position_independent())
5633 write_insn
<big_endian
>(p
, lis_12
+ ha(bs
->first
.dest_
));
5635 write_insn
<big_endian
>(p
, addi_12_12
+ l(bs
->first
.dest_
));
5640 write_insn
<big_endian
>(p
, mflr_0
);
5642 write_insn
<big_endian
>(p
, bcl_20_31
);
5644 write_insn
<big_endian
>(p
, mflr_12
);
5646 write_insn
<big_endian
>(p
, addis_12_12
+ ha(delta
));
5648 write_insn
<big_endian
>(p
, addi_12_12
+ l(delta
));
5650 write_insn
<big_endian
>(p
, mtlr_0
);
5653 write_insn
<big_endian
>(p
, mtctr_12
);
5655 write_insn
<big_endian
>(p
, bctr
);
5658 if (this->need_save_res_
)
5660 p
= oview
+ this->plt_size_
+ this->branch_size_
;
5661 memcpy (p
, this->targ_
->savres_section()->contents(),
5662 this->targ_
->savres_section()->data_size());
5666 // Write out .glink.
5668 template<int size
, bool big_endian
>
5670 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
5672 const section_size_type off
= this->offset();
5673 const section_size_type oview_size
=
5674 convert_to_section_size_type(this->data_size());
5675 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5678 // The base address of the .plt section.
5679 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
5680 Address plt_base
= this->targ_
->plt_section()->address();
5684 if (this->end_branch_table_
!= 0)
5686 // Write pltresolve stub.
5688 Address after_bcl
= this->address() + 16;
5689 Address pltoff
= plt_base
- after_bcl
;
5691 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
5693 if (this->targ_
->abiversion() < 2)
5695 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
5696 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5697 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5698 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5699 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
5700 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5701 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5702 write_insn
<big_endian
>(p
, ld_2_11
+ 8), p
+= 4;
5703 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5704 write_insn
<big_endian
>(p
, ld_11_11
+ 16), p
+= 4;
5708 write_insn
<big_endian
>(p
, mflr_0
), p
+= 4;
5709 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5710 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5711 write_insn
<big_endian
>(p
, std_2_1
+ 24), p
+= 4;
5712 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5713 write_insn
<big_endian
>(p
, mtlr_0
), p
+= 4;
5714 write_insn
<big_endian
>(p
, sub_12_12_11
), p
+= 4;
5715 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5716 write_insn
<big_endian
>(p
, addi_0_12
+ l(-48)), p
+= 4;
5717 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5718 write_insn
<big_endian
>(p
, srdi_0_0_2
), p
+= 4;
5719 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5720 write_insn
<big_endian
>(p
, ld_11_11
+ 8), p
+= 4;
5722 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
5723 gold_assert(p
== oview
+ this->pltresolve_size());
5725 // Write lazy link call stubs.
5727 while (p
< oview
+ this->end_branch_table_
)
5729 if (this->targ_
->abiversion() < 2)
5733 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
5737 write_insn
<big_endian
>(p
, lis_0
+ hi(indx
)), p
+= 4;
5738 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
5741 uint32_t branch_off
= 8 - (p
- oview
);
5742 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
5747 Address plt_base
= this->targ_
->plt_section()->address();
5748 Address iplt_base
= invalid_address
;
5749 unsigned int global_entry_off
= this->global_entry_off();
5750 Address global_entry_base
= this->address() + global_entry_off
;
5751 typename
Global_entry_stub_entries::const_iterator ge
;
5752 for (ge
= this->global_entry_stubs_
.begin();
5753 ge
!= this->global_entry_stubs_
.end();
5756 p
= oview
+ global_entry_off
+ ge
->second
;
5757 Address plt_addr
= ge
->first
->plt_offset();
5758 if (ge
->first
->type() == elfcpp::STT_GNU_IFUNC
5759 && ge
->first
->can_use_relative_reloc(false))
5761 if (iplt_base
== invalid_address
)
5762 iplt_base
= this->targ_
->iplt_section()->address();
5763 plt_addr
+= iplt_base
;
5766 plt_addr
+= plt_base
;
5767 Address my_addr
= global_entry_base
+ ge
->second
;
5768 Address off
= plt_addr
- my_addr
;
5770 if (off
+ 0x80008000 > 0xffffffff || (off
& 3) != 0)
5771 gold_error(_("%s: linkage table error against `%s'"),
5772 ge
->first
->object()->name().c_str(),
5773 ge
->first
->demangled_name().c_str());
5775 write_insn
<big_endian
>(p
, addis_12_12
+ ha(off
)), p
+= 4;
5776 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
)), p
+= 4;
5777 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5778 write_insn
<big_endian
>(p
, bctr
);
5783 const Output_data_got_powerpc
<size
, big_endian
>* got
5784 = this->targ_
->got_section();
5785 // The address of _GLOBAL_OFFSET_TABLE_.
5786 Address g_o_t
= got
->address() + got
->g_o_t();
5788 // Write out pltresolve branch table.
5790 unsigned int the_end
= oview_size
- this->pltresolve_size();
5791 unsigned char* end_p
= oview
+ the_end
;
5792 while (p
< end_p
- 8 * 4)
5793 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
5795 write_insn
<big_endian
>(p
, nop
), p
+= 4;
5797 // Write out pltresolve call stub.
5798 end_p
= oview
+ oview_size
;
5799 if (parameters
->options().output_is_position_independent())
5801 Address res0_off
= 0;
5802 Address after_bcl_off
= the_end
+ 12;
5803 Address bcl_res0
= after_bcl_off
- res0_off
;
5805 write_insn
<big_endian
>(p
, addis_11_11
+ ha(bcl_res0
));
5807 write_insn
<big_endian
>(p
, mflr_0
);
5809 write_insn
<big_endian
>(p
, bcl_20_31
);
5811 write_insn
<big_endian
>(p
, addi_11_11
+ l(bcl_res0
));
5813 write_insn
<big_endian
>(p
, mflr_12
);
5815 write_insn
<big_endian
>(p
, mtlr_0
);
5817 write_insn
<big_endian
>(p
, sub_11_11_12
);
5820 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
5822 write_insn
<big_endian
>(p
, addis_12_12
+ ha(got_bcl
));
5824 if (ha(got_bcl
) == ha(got_bcl
+ 4))
5826 write_insn
<big_endian
>(p
, lwz_0_12
+ l(got_bcl
));
5828 write_insn
<big_endian
>(p
, lwz_12_12
+ l(got_bcl
+ 4));
5832 write_insn
<big_endian
>(p
, lwzu_0_12
+ l(got_bcl
));
5834 write_insn
<big_endian
>(p
, lwz_12_12
+ 4);
5837 write_insn
<big_endian
>(p
, mtctr_0
);
5839 write_insn
<big_endian
>(p
, add_0_11_11
);
5841 write_insn
<big_endian
>(p
, add_11_0_11
);
5845 Address res0
= this->address();
5847 write_insn
<big_endian
>(p
, lis_12
+ ha(g_o_t
+ 4));
5849 write_insn
<big_endian
>(p
, addis_11_11
+ ha(-res0
));
5851 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5852 write_insn
<big_endian
>(p
, lwz_0_12
+ l(g_o_t
+ 4));
5854 write_insn
<big_endian
>(p
, lwzu_0_12
+ l(g_o_t
+ 4));
5856 write_insn
<big_endian
>(p
, addi_11_11
+ l(-res0
));
5858 write_insn
<big_endian
>(p
, mtctr_0
);
5860 write_insn
<big_endian
>(p
, add_0_11_11
);
5862 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5863 write_insn
<big_endian
>(p
, lwz_12_12
+ l(g_o_t
+ 8));
5865 write_insn
<big_endian
>(p
, lwz_12_12
+ 4);
5867 write_insn
<big_endian
>(p
, add_11_0_11
);
5870 write_insn
<big_endian
>(p
, bctr
);
5874 write_insn
<big_endian
>(p
, nop
);
5879 of
->write_output_view(off
, oview_size
, oview
);
5883 // A class to handle linker generated save/restore functions.
5885 template<int size
, bool big_endian
>
5886 class Output_data_save_res
: public Output_section_data_build
5889 Output_data_save_res(Symbol_table
* symtab
);
5891 const unsigned char*
5898 // Write to a map file.
5900 do_print_to_mapfile(Mapfile
* mapfile
) const
5901 { mapfile
->print_output_data(this, _("** save/restore")); }
5904 do_write(Output_file
*);
5907 // The maximum size of save/restore contents.
5908 static const unsigned int savres_max
= 218*4;
5911 savres_define(Symbol_table
* symtab
,
5913 unsigned int lo
, unsigned int hi
,
5914 unsigned char* write_ent(unsigned char*, int),
5915 unsigned char* write_tail(unsigned char*, int));
5917 unsigned char *contents_
;
5920 template<bool big_endian
>
5921 static unsigned char*
5922 savegpr0(unsigned char* p
, int r
)
5924 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5925 write_insn
<big_endian
>(p
, insn
);
5929 template<bool big_endian
>
5930 static unsigned char*
5931 savegpr0_tail(unsigned char* p
, int r
)
5933 p
= savegpr0
<big_endian
>(p
, r
);
5934 uint32_t insn
= std_0_1
+ 16;
5935 write_insn
<big_endian
>(p
, insn
);
5937 write_insn
<big_endian
>(p
, blr
);
5941 template<bool big_endian
>
5942 static unsigned char*
5943 restgpr0(unsigned char* p
, int r
)
5945 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5946 write_insn
<big_endian
>(p
, insn
);
5950 template<bool big_endian
>
5951 static unsigned char*
5952 restgpr0_tail(unsigned char* p
, int r
)
5954 uint32_t insn
= ld_0_1
+ 16;
5955 write_insn
<big_endian
>(p
, insn
);
5957 p
= restgpr0
<big_endian
>(p
, r
);
5958 write_insn
<big_endian
>(p
, mtlr_0
);
5962 p
= restgpr0
<big_endian
>(p
, 30);
5963 p
= restgpr0
<big_endian
>(p
, 31);
5965 write_insn
<big_endian
>(p
, blr
);
5969 template<bool big_endian
>
5970 static unsigned char*
5971 savegpr1(unsigned char* p
, int r
)
5973 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5974 write_insn
<big_endian
>(p
, insn
);
5978 template<bool big_endian
>
5979 static unsigned char*
5980 savegpr1_tail(unsigned char* p
, int r
)
5982 p
= savegpr1
<big_endian
>(p
, r
);
5983 write_insn
<big_endian
>(p
, blr
);
5987 template<bool big_endian
>
5988 static unsigned char*
5989 restgpr1(unsigned char* p
, int r
)
5991 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5992 write_insn
<big_endian
>(p
, insn
);
5996 template<bool big_endian
>
5997 static unsigned char*
5998 restgpr1_tail(unsigned char* p
, int r
)
6000 p
= restgpr1
<big_endian
>(p
, r
);
6001 write_insn
<big_endian
>(p
, blr
);
6005 template<bool big_endian
>
6006 static unsigned char*
6007 savefpr(unsigned char* p
, int r
)
6009 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
6010 write_insn
<big_endian
>(p
, insn
);
6014 template<bool big_endian
>
6015 static unsigned char*
6016 savefpr0_tail(unsigned char* p
, int r
)
6018 p
= savefpr
<big_endian
>(p
, r
);
6019 write_insn
<big_endian
>(p
, std_0_1
+ 16);
6021 write_insn
<big_endian
>(p
, blr
);
6025 template<bool big_endian
>
6026 static unsigned char*
6027 restfpr(unsigned char* p
, int r
)
6029 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
6030 write_insn
<big_endian
>(p
, insn
);
6034 template<bool big_endian
>
6035 static unsigned char*
6036 restfpr0_tail(unsigned char* p
, int r
)
6038 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
6040 p
= restfpr
<big_endian
>(p
, r
);
6041 write_insn
<big_endian
>(p
, mtlr_0
);
6045 p
= restfpr
<big_endian
>(p
, 30);
6046 p
= restfpr
<big_endian
>(p
, 31);
6048 write_insn
<big_endian
>(p
, blr
);
6052 template<bool big_endian
>
6053 static unsigned char*
6054 savefpr1_tail(unsigned char* p
, int r
)
6056 p
= savefpr
<big_endian
>(p
, r
);
6057 write_insn
<big_endian
>(p
, blr
);
6061 template<bool big_endian
>
6062 static unsigned char*
6063 restfpr1_tail(unsigned char* p
, int r
)
6065 p
= restfpr
<big_endian
>(p
, r
);
6066 write_insn
<big_endian
>(p
, blr
);
6070 template<bool big_endian
>
6071 static unsigned char*
6072 savevr(unsigned char* p
, int r
)
6074 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
6075 write_insn
<big_endian
>(p
, insn
);
6077 insn
= stvx_0_12_0
+ (r
<< 21);
6078 write_insn
<big_endian
>(p
, insn
);
6082 template<bool big_endian
>
6083 static unsigned char*
6084 savevr_tail(unsigned char* p
, int r
)
6086 p
= savevr
<big_endian
>(p
, r
);
6087 write_insn
<big_endian
>(p
, blr
);
6091 template<bool big_endian
>
6092 static unsigned char*
6093 restvr(unsigned char* p
, int r
)
6095 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
6096 write_insn
<big_endian
>(p
, insn
);
6098 insn
= lvx_0_12_0
+ (r
<< 21);
6099 write_insn
<big_endian
>(p
, insn
);
6103 template<bool big_endian
>
6104 static unsigned char*
6105 restvr_tail(unsigned char* p
, int r
)
6107 p
= restvr
<big_endian
>(p
, r
);
6108 write_insn
<big_endian
>(p
, blr
);
6113 template<int size
, bool big_endian
>
6114 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
6115 Symbol_table
* symtab
)
6116 : Output_section_data_build(4),
6119 this->savres_define(symtab
,
6120 "_savegpr0_", 14, 31,
6121 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
6122 this->savres_define(symtab
,
6123 "_restgpr0_", 14, 29,
6124 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
6125 this->savres_define(symtab
,
6126 "_restgpr0_", 30, 31,
6127 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
6128 this->savres_define(symtab
,
6129 "_savegpr1_", 14, 31,
6130 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
6131 this->savres_define(symtab
,
6132 "_restgpr1_", 14, 31,
6133 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
6134 this->savres_define(symtab
,
6135 "_savefpr_", 14, 31,
6136 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
6137 this->savres_define(symtab
,
6138 "_restfpr_", 14, 29,
6139 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
6140 this->savres_define(symtab
,
6141 "_restfpr_", 30, 31,
6142 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
6143 this->savres_define(symtab
,
6145 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
6146 this->savres_define(symtab
,
6148 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
6149 this->savres_define(symtab
,
6151 savevr
<big_endian
>, savevr_tail
<big_endian
>);
6152 this->savres_define(symtab
,
6154 restvr
<big_endian
>, restvr_tail
<big_endian
>);
6157 template<int size
, bool big_endian
>
6159 Output_data_save_res
<size
, big_endian
>::savres_define(
6160 Symbol_table
* symtab
,
6162 unsigned int lo
, unsigned int hi
,
6163 unsigned char* write_ent(unsigned char*, int),
6164 unsigned char* write_tail(unsigned char*, int))
6166 size_t len
= strlen(name
);
6167 bool writing
= false;
6170 memcpy(sym
, name
, len
);
6173 for (unsigned int i
= lo
; i
<= hi
; i
++)
6175 sym
[len
+ 0] = i
/ 10 + '0';
6176 sym
[len
+ 1] = i
% 10 + '0';
6177 Symbol
* gsym
= symtab
->lookup(sym
);
6178 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
6179 writing
= writing
|| refd
;
6182 if (this->contents_
== NULL
)
6183 this->contents_
= new unsigned char[this->savres_max
];
6185 section_size_type value
= this->current_data_size();
6186 unsigned char* p
= this->contents_
+ value
;
6188 p
= write_ent(p
, i
);
6190 p
= write_tail(p
, i
);
6191 section_size_type cur_size
= p
- this->contents_
;
6192 this->set_current_data_size(cur_size
);
6194 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
6195 this, value
, cur_size
- value
,
6196 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
6197 elfcpp::STV_HIDDEN
, 0, false, false);
6202 // Write out save/restore.
6204 template<int size
, bool big_endian
>
6206 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
6208 const section_size_type off
= this->offset();
6209 const section_size_type oview_size
=
6210 convert_to_section_size_type(this->data_size());
6211 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
6212 memcpy(oview
, this->contents_
, oview_size
);
6213 of
->write_output_view(off
, oview_size
, oview
);
6217 // Create the glink section.
6219 template<int size
, bool big_endian
>
6221 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
6223 if (this->glink_
== NULL
)
6225 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
6226 this->glink_
->add_eh_frame(layout
);
6227 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
6228 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
6229 this->glink_
, ORDER_TEXT
, false);
6233 // Create a PLT entry for a global symbol.
6235 template<int size
, bool big_endian
>
6237 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
6241 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
6242 && gsym
->can_use_relative_reloc(false))
6244 if (this->iplt_
== NULL
)
6245 this->make_iplt_section(symtab
, layout
);
6246 this->iplt_
->add_ifunc_entry(gsym
);
6250 if (this->plt_
== NULL
)
6251 this->make_plt_section(symtab
, layout
);
6252 this->plt_
->add_entry(gsym
);
6256 // Make a PLT entry for a local symbol.
6258 template<int size
, bool big_endian
>
6260 Target_powerpc
<size
, big_endian
>::make_local_plt_entry(
6262 Sized_relobj_file
<size
, big_endian
>* relobj
,
6265 if (this->lplt_
== NULL
)
6266 this->make_lplt_section(layout
);
6267 this->lplt_
->add_local_entry(relobj
, r_sym
);
6270 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
6272 template<int size
, bool big_endian
>
6274 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
6275 Symbol_table
* symtab
,
6277 Sized_relobj_file
<size
, big_endian
>* relobj
,
6280 if (this->iplt_
== NULL
)
6281 this->make_iplt_section(symtab
, layout
);
6282 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
6285 // Return the number of entries in the PLT.
6287 template<int size
, bool big_endian
>
6289 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
6291 if (this->plt_
== NULL
)
6293 return this->plt_
->entry_count();
6296 // Create a GOT entry for local dynamic __tls_get_addr calls.
6298 template<int size
, bool big_endian
>
6300 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
6301 Symbol_table
* symtab
,
6303 Sized_relobj_file
<size
, big_endian
>* object
)
6305 if (this->tlsld_got_offset_
== -1U)
6307 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
6308 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
6309 Output_data_got_powerpc
<size
, big_endian
>* got
6310 = this->got_section(symtab
, layout
);
6311 unsigned int got_offset
= got
->add_constant_pair(0, 0);
6312 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
6314 this->tlsld_got_offset_
= got_offset
;
6316 return this->tlsld_got_offset_
;
6319 // Get the Reference_flags for a particular relocation.
6321 template<int size
, bool big_endian
>
6323 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(
6324 unsigned int r_type
,
6325 const Target_powerpc
* target
)
6331 case elfcpp::R_POWERPC_NONE
:
6332 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6333 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6334 case elfcpp::R_PPC64_TOC
:
6335 // No symbol reference.
6338 case elfcpp::R_PPC64_ADDR64
:
6339 case elfcpp::R_PPC64_UADDR64
:
6340 case elfcpp::R_POWERPC_ADDR32
:
6341 case elfcpp::R_POWERPC_UADDR32
:
6342 case elfcpp::R_POWERPC_ADDR16
:
6343 case elfcpp::R_POWERPC_UADDR16
:
6344 case elfcpp::R_POWERPC_ADDR16_LO
:
6345 case elfcpp::R_POWERPC_ADDR16_HI
:
6346 case elfcpp::R_POWERPC_ADDR16_HA
:
6347 ref
= Symbol::ABSOLUTE_REF
;
6350 case elfcpp::R_POWERPC_ADDR24
:
6351 case elfcpp::R_POWERPC_ADDR14
:
6352 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6353 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6354 ref
= Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
6357 case elfcpp::R_PPC64_REL64
:
6358 case elfcpp::R_POWERPC_REL32
:
6359 case elfcpp::R_PPC_LOCAL24PC
:
6360 case elfcpp::R_POWERPC_REL16
:
6361 case elfcpp::R_POWERPC_REL16_LO
:
6362 case elfcpp::R_POWERPC_REL16_HI
:
6363 case elfcpp::R_POWERPC_REL16_HA
:
6364 ref
= Symbol::RELATIVE_REF
;
6367 case elfcpp::R_POWERPC_REL24
:
6368 case elfcpp::R_PPC_PLTREL24
:
6369 case elfcpp::R_POWERPC_REL14
:
6370 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6371 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6372 ref
= Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
6375 case elfcpp::R_POWERPC_GOT16
:
6376 case elfcpp::R_POWERPC_GOT16_LO
:
6377 case elfcpp::R_POWERPC_GOT16_HI
:
6378 case elfcpp::R_POWERPC_GOT16_HA
:
6379 case elfcpp::R_PPC64_GOT16_DS
:
6380 case elfcpp::R_PPC64_GOT16_LO_DS
:
6381 case elfcpp::R_PPC64_TOC16
:
6382 case elfcpp::R_PPC64_TOC16_LO
:
6383 case elfcpp::R_PPC64_TOC16_HI
:
6384 case elfcpp::R_PPC64_TOC16_HA
:
6385 case elfcpp::R_PPC64_TOC16_DS
:
6386 case elfcpp::R_PPC64_TOC16_LO_DS
:
6387 case elfcpp::R_POWERPC_PLT16_LO
:
6388 case elfcpp::R_POWERPC_PLT16_HI
:
6389 case elfcpp::R_POWERPC_PLT16_HA
:
6390 case elfcpp::R_PPC64_PLT16_LO_DS
:
6391 ref
= Symbol::RELATIVE_REF
;
6394 case elfcpp::R_POWERPC_GOT_TPREL16
:
6395 case elfcpp::R_POWERPC_TLS
:
6396 ref
= Symbol::TLS_REF
;
6399 case elfcpp::R_POWERPC_COPY
:
6400 case elfcpp::R_POWERPC_GLOB_DAT
:
6401 case elfcpp::R_POWERPC_JMP_SLOT
:
6402 case elfcpp::R_POWERPC_RELATIVE
:
6403 case elfcpp::R_POWERPC_DTPMOD
:
6405 // Not expected. We will give an error later.
6409 if (size
== 64 && target
->abiversion() < 2)
6410 ref
|= Symbol::FUNC_DESC_ABI
;
6414 // Report an unsupported relocation against a local symbol.
6416 template<int size
, bool big_endian
>
6418 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
6419 Sized_relobj_file
<size
, big_endian
>* object
,
6420 unsigned int r_type
)
6422 gold_error(_("%s: unsupported reloc %u against local symbol"),
6423 object
->name().c_str(), r_type
);
6426 // We are about to emit a dynamic relocation of type R_TYPE. If the
6427 // dynamic linker does not support it, issue an error.
6429 template<int size
, bool big_endian
>
6431 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
6432 unsigned int r_type
)
6434 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
6436 // These are the relocation types supported by glibc for both 32-bit
6437 // and 64-bit powerpc.
6440 case elfcpp::R_POWERPC_NONE
:
6441 case elfcpp::R_POWERPC_RELATIVE
:
6442 case elfcpp::R_POWERPC_GLOB_DAT
:
6443 case elfcpp::R_POWERPC_DTPMOD
:
6444 case elfcpp::R_POWERPC_DTPREL
:
6445 case elfcpp::R_POWERPC_TPREL
:
6446 case elfcpp::R_POWERPC_JMP_SLOT
:
6447 case elfcpp::R_POWERPC_COPY
:
6448 case elfcpp::R_POWERPC_IRELATIVE
:
6449 case elfcpp::R_POWERPC_ADDR32
:
6450 case elfcpp::R_POWERPC_UADDR32
:
6451 case elfcpp::R_POWERPC_ADDR24
:
6452 case elfcpp::R_POWERPC_ADDR16
:
6453 case elfcpp::R_POWERPC_UADDR16
:
6454 case elfcpp::R_POWERPC_ADDR16_LO
:
6455 case elfcpp::R_POWERPC_ADDR16_HI
:
6456 case elfcpp::R_POWERPC_ADDR16_HA
:
6457 case elfcpp::R_POWERPC_ADDR14
:
6458 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6459 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6460 case elfcpp::R_POWERPC_REL32
:
6461 case elfcpp::R_POWERPC_REL24
:
6462 case elfcpp::R_POWERPC_TPREL16
:
6463 case elfcpp::R_POWERPC_TPREL16_LO
:
6464 case elfcpp::R_POWERPC_TPREL16_HI
:
6465 case elfcpp::R_POWERPC_TPREL16_HA
:
6476 // These are the relocation types supported only on 64-bit.
6477 case elfcpp::R_PPC64_ADDR64
:
6478 case elfcpp::R_PPC64_UADDR64
:
6479 case elfcpp::R_PPC64_JMP_IREL
:
6480 case elfcpp::R_PPC64_ADDR16_DS
:
6481 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6482 case elfcpp::R_PPC64_ADDR16_HIGH
:
6483 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6484 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6485 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6486 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6487 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6488 case elfcpp::R_PPC64_REL64
:
6489 case elfcpp::R_POWERPC_ADDR30
:
6490 case elfcpp::R_PPC64_TPREL16_DS
:
6491 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6492 case elfcpp::R_PPC64_TPREL16_HIGH
:
6493 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6494 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6495 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6496 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6497 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6508 // These are the relocation types supported only on 32-bit.
6509 // ??? glibc ld.so doesn't need to support these.
6510 case elfcpp::R_POWERPC_DTPREL16
:
6511 case elfcpp::R_POWERPC_DTPREL16_LO
:
6512 case elfcpp::R_POWERPC_DTPREL16_HI
:
6513 case elfcpp::R_POWERPC_DTPREL16_HA
:
6521 // This prevents us from issuing more than one error per reloc
6522 // section. But we can still wind up issuing more than one
6523 // error per object file.
6524 if (this->issued_non_pic_error_
)
6526 gold_assert(parameters
->options().output_is_position_independent());
6527 object
->error(_("requires unsupported dynamic reloc; "
6528 "recompile with -fPIC"));
6529 this->issued_non_pic_error_
= true;
6533 // Return whether we need to make a PLT entry for a relocation of the
6534 // given type against a STT_GNU_IFUNC symbol.
6536 template<int size
, bool big_endian
>
6538 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
6539 Target_powerpc
<size
, big_endian
>* target
,
6540 Sized_relobj_file
<size
, big_endian
>* object
,
6541 unsigned int r_type
,
6544 // In non-pic code any reference will resolve to the plt call stub
6545 // for the ifunc symbol.
6546 if ((size
== 32 || target
->abiversion() >= 2)
6547 && !parameters
->options().output_is_position_independent())
6552 // Word size refs from data sections are OK, but don't need a PLT entry.
6553 case elfcpp::R_POWERPC_ADDR32
:
6554 case elfcpp::R_POWERPC_UADDR32
:
6559 case elfcpp::R_PPC64_ADDR64
:
6560 case elfcpp::R_PPC64_UADDR64
:
6565 // GOT refs are good, but also don't need a PLT entry.
6566 case elfcpp::R_POWERPC_GOT16
:
6567 case elfcpp::R_POWERPC_GOT16_LO
:
6568 case elfcpp::R_POWERPC_GOT16_HI
:
6569 case elfcpp::R_POWERPC_GOT16_HA
:
6570 case elfcpp::R_PPC64_GOT16_DS
:
6571 case elfcpp::R_PPC64_GOT16_LO_DS
:
6574 // PLT relocs are OK and need a PLT entry.
6575 case elfcpp::R_POWERPC_PLT16_LO
:
6576 case elfcpp::R_POWERPC_PLT16_HI
:
6577 case elfcpp::R_POWERPC_PLT16_HA
:
6578 case elfcpp::R_PPC64_PLT16_LO_DS
:
6579 case elfcpp::R_POWERPC_PLTSEQ
:
6580 case elfcpp::R_POWERPC_PLTCALL
:
6584 // Function calls are good, and these do need a PLT entry.
6585 case elfcpp::R_POWERPC_ADDR24
:
6586 case elfcpp::R_POWERPC_ADDR14
:
6587 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6588 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6589 case elfcpp::R_POWERPC_REL24
:
6590 case elfcpp::R_PPC_PLTREL24
:
6591 case elfcpp::R_POWERPC_REL14
:
6592 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6593 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6600 // Anything else is a problem.
6601 // If we are building a static executable, the libc startup function
6602 // responsible for applying indirect function relocations is going
6603 // to complain about the reloc type.
6604 // If we are building a dynamic executable, we will have a text
6605 // relocation. The dynamic loader will set the text segment
6606 // writable and non-executable to apply text relocations. So we'll
6607 // segfault when trying to run the indirection function to resolve
6610 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
6611 object
->name().c_str(), r_type
);
6615 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6619 ok_lo_toc_insn(uint32_t insn
, unsigned int r_type
)
6621 return ((insn
& (0x3f << 26)) == 12u << 26 /* addic */
6622 || (insn
& (0x3f << 26)) == 14u << 26 /* addi */
6623 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
6624 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
6625 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
6626 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
6627 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
6628 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
6629 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
6630 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
6631 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
6632 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
6633 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
6634 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
6635 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
6636 || (insn
& (0x3f << 26)) == 56u << 26 /* lq,lfq */
6637 || ((insn
& (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
6638 /* Exclude lfqu by testing reloc. If relocs are ever
6639 defined for the reduced D field in psq_lu then those
6640 will need testing too. */
6641 && r_type
!= elfcpp::R_PPC64_TOC16_LO
6642 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
6643 || ((insn
& (0x3f << 26)) == 58u << 26 /* ld,lwa */
6645 || (insn
& (0x3f << 26)) == 60u << 26 /* stfq */
6646 || ((insn
& (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
6647 /* Exclude stfqu. psq_stu as above for psq_lu. */
6648 && r_type
!= elfcpp::R_PPC64_TOC16_LO
6649 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
6650 || ((insn
& (0x3f << 26)) == 62u << 26 /* std,stq */
6651 && (insn
& 1) == 0));
6654 // Scan a relocation for a local symbol.
6656 template<int size
, bool big_endian
>
6658 Target_powerpc
<size
, big_endian
>::Scan::local(
6659 Symbol_table
* symtab
,
6661 Target_powerpc
<size
, big_endian
>* target
,
6662 Sized_relobj_file
<size
, big_endian
>* object
,
6663 unsigned int data_shndx
,
6664 Output_section
* output_section
,
6665 const elfcpp::Rela
<size
, big_endian
>& reloc
,
6666 unsigned int r_type
,
6667 const elfcpp::Sym
<size
, big_endian
>& lsym
,
6670 this->maybe_skip_tls_get_addr_call(target
, r_type
, NULL
);
6672 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6673 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6675 this->expect_tls_get_addr_call();
6676 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
6677 if (tls_type
!= tls::TLSOPT_NONE
)
6678 this->skip_next_tls_get_addr_call();
6680 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6681 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6683 this->expect_tls_get_addr_call();
6684 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6685 if (tls_type
!= tls::TLSOPT_NONE
)
6686 this->skip_next_tls_get_addr_call();
6689 Powerpc_relobj
<size
, big_endian
>* ppc_object
6690 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6695 && data_shndx
== ppc_object
->opd_shndx()
6696 && r_type
== elfcpp::R_PPC64_ADDR64
)
6697 ppc_object
->set_opd_discard(reloc
.get_r_offset());
6701 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6702 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
6703 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
6705 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6706 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6707 r_type
, r_sym
, reloc
.get_r_addend());
6708 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
6713 case elfcpp::R_POWERPC_NONE
:
6714 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6715 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6716 case elfcpp::R_POWERPC_TLS
:
6717 case elfcpp::R_PPC64_ENTRY
:
6718 case elfcpp::R_POWERPC_PLTSEQ
:
6719 case elfcpp::R_POWERPC_PLTCALL
:
6722 case elfcpp::R_PPC64_TOC
:
6724 Output_data_got_powerpc
<size
, big_endian
>* got
6725 = target
->got_section(symtab
, layout
);
6726 if (parameters
->options().output_is_position_independent())
6728 Address off
= reloc
.get_r_offset();
6730 && target
->abiversion() < 2
6731 && data_shndx
== ppc_object
->opd_shndx()
6732 && ppc_object
->get_opd_discard(off
- 8))
6735 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6736 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
6737 rela_dyn
->add_output_section_relative(got
->output_section(),
6738 elfcpp::R_POWERPC_RELATIVE
,
6740 object
, data_shndx
, off
,
6741 symobj
->toc_base_offset());
6746 case elfcpp::R_PPC64_ADDR64
:
6747 case elfcpp::R_PPC64_UADDR64
:
6748 case elfcpp::R_POWERPC_ADDR32
:
6749 case elfcpp::R_POWERPC_UADDR32
:
6750 case elfcpp::R_POWERPC_ADDR24
:
6751 case elfcpp::R_POWERPC_ADDR16
:
6752 case elfcpp::R_POWERPC_ADDR16_LO
:
6753 case elfcpp::R_POWERPC_ADDR16_HI
:
6754 case elfcpp::R_POWERPC_ADDR16_HA
:
6755 case elfcpp::R_POWERPC_UADDR16
:
6756 case elfcpp::R_PPC64_ADDR16_HIGH
:
6757 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6758 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6759 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6760 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6761 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6762 case elfcpp::R_PPC64_ADDR16_DS
:
6763 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6764 case elfcpp::R_POWERPC_ADDR14
:
6765 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6766 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6767 // If building a shared library (or a position-independent
6768 // executable), we need to create a dynamic relocation for
6770 if (parameters
->options().output_is_position_independent()
6771 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
6773 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
6775 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6776 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
6777 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
6779 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6780 : elfcpp::R_POWERPC_RELATIVE
);
6781 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
6782 output_section
, data_shndx
,
6783 reloc
.get_r_offset(),
6784 reloc
.get_r_addend(), false);
6786 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
6788 check_non_pic(object
, r_type
);
6789 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
6790 data_shndx
, reloc
.get_r_offset(),
6791 reloc
.get_r_addend());
6795 gold_assert(lsym
.get_st_value() == 0);
6796 unsigned int shndx
= lsym
.get_st_shndx();
6798 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
6801 object
->error(_("section symbol %u has bad shndx %u"),
6804 rela_dyn
->add_local_section(object
, shndx
, r_type
,
6805 output_section
, data_shndx
,
6806 reloc
.get_r_offset());
6811 case elfcpp::R_POWERPC_PLT16_LO
:
6812 case elfcpp::R_POWERPC_PLT16_HI
:
6813 case elfcpp::R_POWERPC_PLT16_HA
:
6814 case elfcpp::R_PPC64_PLT16_LO_DS
:
6817 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6818 target
->make_local_plt_entry(layout
, object
, r_sym
);
6822 case elfcpp::R_POWERPC_REL24
:
6823 case elfcpp::R_PPC_PLTREL24
:
6824 case elfcpp::R_PPC_LOCAL24PC
:
6825 case elfcpp::R_POWERPC_REL14
:
6826 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6827 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6830 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6831 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6832 r_type
, r_sym
, reloc
.get_r_addend());
6836 case elfcpp::R_PPC64_TOCSAVE
:
6837 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6838 // caller has already saved r2 and thus a plt call stub need not
6841 && target
->mark_pltcall(ppc_object
, data_shndx
,
6842 reloc
.get_r_offset() - 4, symtab
))
6844 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6845 unsigned int shndx
= lsym
.get_st_shndx();
6847 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6849 object
->error(_("tocsave symbol %u has bad shndx %u"),
6852 target
->add_tocsave(ppc_object
, shndx
,
6853 lsym
.get_st_value() + reloc
.get_r_addend());
6857 case elfcpp::R_PPC64_REL64
:
6858 case elfcpp::R_POWERPC_REL32
:
6859 case elfcpp::R_POWERPC_REL16
:
6860 case elfcpp::R_POWERPC_REL16_LO
:
6861 case elfcpp::R_POWERPC_REL16_HI
:
6862 case elfcpp::R_POWERPC_REL16_HA
:
6863 case elfcpp::R_POWERPC_REL16DX_HA
:
6864 case elfcpp::R_POWERPC_SECTOFF
:
6865 case elfcpp::R_POWERPC_SECTOFF_LO
:
6866 case elfcpp::R_POWERPC_SECTOFF_HI
:
6867 case elfcpp::R_POWERPC_SECTOFF_HA
:
6868 case elfcpp::R_PPC64_SECTOFF_DS
:
6869 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6870 case elfcpp::R_POWERPC_TPREL16
:
6871 case elfcpp::R_POWERPC_TPREL16_LO
:
6872 case elfcpp::R_POWERPC_TPREL16_HI
:
6873 case elfcpp::R_POWERPC_TPREL16_HA
:
6874 case elfcpp::R_PPC64_TPREL16_DS
:
6875 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6876 case elfcpp::R_PPC64_TPREL16_HIGH
:
6877 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6878 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6879 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6880 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6881 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6882 case elfcpp::R_POWERPC_DTPREL16
:
6883 case elfcpp::R_POWERPC_DTPREL16_LO
:
6884 case elfcpp::R_POWERPC_DTPREL16_HI
:
6885 case elfcpp::R_POWERPC_DTPREL16_HA
:
6886 case elfcpp::R_PPC64_DTPREL16_DS
:
6887 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6888 case elfcpp::R_PPC64_DTPREL16_HIGH
:
6889 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
6890 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6891 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6892 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6893 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6894 case elfcpp::R_PPC64_TLSGD
:
6895 case elfcpp::R_PPC64_TLSLD
:
6896 case elfcpp::R_PPC64_ADDR64_LOCAL
:
6899 case elfcpp::R_POWERPC_GOT16
:
6900 case elfcpp::R_POWERPC_GOT16_LO
:
6901 case elfcpp::R_POWERPC_GOT16_HI
:
6902 case elfcpp::R_POWERPC_GOT16_HA
:
6903 case elfcpp::R_PPC64_GOT16_DS
:
6904 case elfcpp::R_PPC64_GOT16_LO_DS
:
6906 // The symbol requires a GOT entry.
6907 Output_data_got_powerpc
<size
, big_endian
>* got
6908 = target
->got_section(symtab
, layout
);
6909 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6911 if (!parameters
->options().output_is_position_independent())
6914 && (size
== 32 || target
->abiversion() >= 2))
6915 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
6917 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
6919 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
6921 // If we are generating a shared object or a pie, this
6922 // symbol's GOT entry will be set by a dynamic relocation.
6924 off
= got
->add_constant(0);
6925 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
6927 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
6929 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6930 : elfcpp::R_POWERPC_RELATIVE
);
6931 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
6932 got
, off
, 0, false);
6937 case elfcpp::R_PPC64_TOC16
:
6938 case elfcpp::R_PPC64_TOC16_LO
:
6939 case elfcpp::R_PPC64_TOC16_HI
:
6940 case elfcpp::R_PPC64_TOC16_HA
:
6941 case elfcpp::R_PPC64_TOC16_DS
:
6942 case elfcpp::R_PPC64_TOC16_LO_DS
:
6943 // We need a GOT section.
6944 target
->got_section(symtab
, layout
);
6947 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6948 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6949 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6950 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6952 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
6953 if (tls_type
== tls::TLSOPT_NONE
)
6955 Output_data_got_powerpc
<size
, big_endian
>* got
6956 = target
->got_section(symtab
, layout
);
6957 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6958 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6959 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
6960 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
6962 else if (tls_type
== tls::TLSOPT_TO_LE
)
6964 // no GOT relocs needed for Local Exec.
6971 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6972 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6973 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6974 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6976 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6977 if (tls_type
== tls::TLSOPT_NONE
)
6978 target
->tlsld_got_offset(symtab
, layout
, object
);
6979 else if (tls_type
== tls::TLSOPT_TO_LE
)
6981 // no GOT relocs needed for Local Exec.
6982 if (parameters
->options().emit_relocs())
6984 Output_section
* os
= layout
->tls_segment()->first_section();
6985 gold_assert(os
!= NULL
);
6986 os
->set_needs_symtab_index();
6994 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6995 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6996 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6997 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6999 Output_data_got_powerpc
<size
, big_endian
>* got
7000 = target
->got_section(symtab
, layout
);
7001 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7002 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
7006 case elfcpp::R_POWERPC_GOT_TPREL16
:
7007 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7008 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7009 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7011 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
7012 if (tls_type
== tls::TLSOPT_NONE
)
7014 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7015 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
7017 Output_data_got_powerpc
<size
, big_endian
>* got
7018 = target
->got_section(symtab
, layout
);
7019 unsigned int off
= got
->add_constant(0);
7020 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
7022 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7023 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
7024 elfcpp::R_POWERPC_TPREL
,
7028 else if (tls_type
== tls::TLSOPT_TO_LE
)
7030 // no GOT relocs needed for Local Exec.
7038 unsupported_reloc_local(object
, r_type
);
7043 && parameters
->options().toc_optimize())
7045 if (data_shndx
== ppc_object
->toc_shndx())
7048 if (r_type
!= elfcpp::R_PPC64_ADDR64
7049 || (is_ifunc
&& target
->abiversion() < 2))
7051 else if (parameters
->options().output_is_position_independent())
7057 unsigned int shndx
= lsym
.get_st_shndx();
7058 if (shndx
>= elfcpp::SHN_LORESERVE
7059 && shndx
!= elfcpp::SHN_XINDEX
)
7064 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
7067 enum {no_check
, check_lo
, check_ha
} insn_check
;
7071 insn_check
= no_check
;
7074 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7075 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7076 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7077 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7078 case elfcpp::R_POWERPC_GOT16_HA
:
7079 case elfcpp::R_PPC64_TOC16_HA
:
7080 insn_check
= check_ha
;
7083 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7084 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7085 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7086 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7087 case elfcpp::R_POWERPC_GOT16_LO
:
7088 case elfcpp::R_PPC64_GOT16_LO_DS
:
7089 case elfcpp::R_PPC64_TOC16_LO
:
7090 case elfcpp::R_PPC64_TOC16_LO_DS
:
7091 insn_check
= check_lo
;
7095 section_size_type slen
;
7096 const unsigned char* view
= NULL
;
7097 if (insn_check
!= no_check
)
7099 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
7100 section_size_type off
=
7101 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
7104 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
7105 if (insn_check
== check_lo
7106 ? !ok_lo_toc_insn(insn
, r_type
)
7107 : ((insn
& ((0x3f << 26) | 0x1f << 16))
7108 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7110 ppc_object
->set_no_toc_opt();
7111 gold_warning(_("%s: toc optimization is not supported "
7112 "for %#08x instruction"),
7113 ppc_object
->name().c_str(), insn
);
7122 case elfcpp::R_PPC64_TOC16
:
7123 case elfcpp::R_PPC64_TOC16_LO
:
7124 case elfcpp::R_PPC64_TOC16_HI
:
7125 case elfcpp::R_PPC64_TOC16_HA
:
7126 case elfcpp::R_PPC64_TOC16_DS
:
7127 case elfcpp::R_PPC64_TOC16_LO_DS
:
7128 unsigned int shndx
= lsym
.get_st_shndx();
7129 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7131 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
7132 if (is_ordinary
&& shndx
== ppc_object
->toc_shndx())
7134 Address dst_off
= lsym
.get_st_value() + reloc
.get_r_addend();
7135 if (dst_off
< ppc_object
->section_size(shndx
))
7138 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
7140 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
7142 // Need to check that the insn is a ld
7144 view
= ppc_object
->section_contents(data_shndx
,
7147 section_size_type off
=
7148 (convert_to_section_size_type(reloc
.get_r_offset())
7149 + (big_endian
? -2 : 3));
7151 && (view
[off
] & (0x3f << 2)) == 58u << 2)
7155 ppc_object
->set_no_toc_opt(dst_off
);
7166 case elfcpp::R_POWERPC_REL32
:
7167 if (ppc_object
->got2_shndx() != 0
7168 && parameters
->options().output_is_position_independent())
7170 unsigned int shndx
= lsym
.get_st_shndx();
7171 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7173 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
7174 if (is_ordinary
&& shndx
== ppc_object
->got2_shndx()
7175 && (ppc_object
->section_flags(data_shndx
)
7176 & elfcpp::SHF_EXECINSTR
) != 0)
7177 gold_error(_("%s: unsupported -mbss-plt code"),
7178 ppc_object
->name().c_str());
7188 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7189 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7190 case elfcpp::R_POWERPC_GOT_TPREL16
:
7191 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7192 case elfcpp::R_POWERPC_GOT16
:
7193 case elfcpp::R_PPC64_GOT16_DS
:
7194 case elfcpp::R_PPC64_TOC16
:
7195 case elfcpp::R_PPC64_TOC16_DS
:
7196 ppc_object
->set_has_small_toc_reloc();
7202 // Report an unsupported relocation against a global symbol.
7204 template<int size
, bool big_endian
>
7206 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
7207 Sized_relobj_file
<size
, big_endian
>* object
,
7208 unsigned int r_type
,
7211 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7212 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
7215 // Scan a relocation for a global symbol.
7217 template<int size
, bool big_endian
>
7219 Target_powerpc
<size
, big_endian
>::Scan::global(
7220 Symbol_table
* symtab
,
7222 Target_powerpc
<size
, big_endian
>* target
,
7223 Sized_relobj_file
<size
, big_endian
>* object
,
7224 unsigned int data_shndx
,
7225 Output_section
* output_section
,
7226 const elfcpp::Rela
<size
, big_endian
>& reloc
,
7227 unsigned int r_type
,
7230 if (this->maybe_skip_tls_get_addr_call(target
, r_type
, gsym
)
7234 if (target
->replace_tls_get_addr(gsym
))
7235 // Change a __tls_get_addr reference to __tls_get_addr_opt
7236 // so dynamic relocs are emitted against the latter symbol.
7237 gsym
= target
->tls_get_addr_opt();
7239 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
7240 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
7242 this->expect_tls_get_addr_call();
7243 const bool final
= gsym
->final_value_is_known();
7244 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7245 if (tls_type
!= tls::TLSOPT_NONE
)
7246 this->skip_next_tls_get_addr_call();
7248 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
7249 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
7251 this->expect_tls_get_addr_call();
7252 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7253 if (tls_type
!= tls::TLSOPT_NONE
)
7254 this->skip_next_tls_get_addr_call();
7257 Powerpc_relobj
<size
, big_endian
>* ppc_object
7258 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
7260 // A STT_GNU_IFUNC symbol may require a PLT entry.
7261 bool is_ifunc
= gsym
->type() == elfcpp::STT_GNU_IFUNC
;
7262 bool pushed_ifunc
= false;
7263 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
7265 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7266 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
7267 r_type
, r_sym
, reloc
.get_r_addend());
7268 target
->make_plt_entry(symtab
, layout
, gsym
);
7269 pushed_ifunc
= true;
7274 case elfcpp::R_POWERPC_NONE
:
7275 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
7276 case elfcpp::R_POWERPC_GNU_VTENTRY
:
7277 case elfcpp::R_PPC_LOCAL24PC
:
7278 case elfcpp::R_POWERPC_TLS
:
7279 case elfcpp::R_PPC64_ENTRY
:
7280 case elfcpp::R_POWERPC_PLTSEQ
:
7281 case elfcpp::R_POWERPC_PLTCALL
:
7284 case elfcpp::R_PPC64_TOC
:
7286 Output_data_got_powerpc
<size
, big_endian
>* got
7287 = target
->got_section(symtab
, layout
);
7288 if (parameters
->options().output_is_position_independent())
7290 Address off
= reloc
.get_r_offset();
7292 && data_shndx
== ppc_object
->opd_shndx()
7293 && ppc_object
->get_opd_discard(off
- 8))
7296 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7297 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
7298 if (data_shndx
!= ppc_object
->opd_shndx())
7299 symobj
= static_cast
7300 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7301 rela_dyn
->add_output_section_relative(got
->output_section(),
7302 elfcpp::R_POWERPC_RELATIVE
,
7304 object
, data_shndx
, off
,
7305 symobj
->toc_base_offset());
7310 case elfcpp::R_PPC64_ADDR64
:
7312 && target
->abiversion() < 2
7313 && data_shndx
== ppc_object
->opd_shndx()
7314 && (gsym
->is_defined_in_discarded_section()
7315 || gsym
->object() != object
))
7317 ppc_object
->set_opd_discard(reloc
.get_r_offset());
7321 case elfcpp::R_PPC64_UADDR64
:
7322 case elfcpp::R_POWERPC_ADDR32
:
7323 case elfcpp::R_POWERPC_UADDR32
:
7324 case elfcpp::R_POWERPC_ADDR24
:
7325 case elfcpp::R_POWERPC_ADDR16
:
7326 case elfcpp::R_POWERPC_ADDR16_LO
:
7327 case elfcpp::R_POWERPC_ADDR16_HI
:
7328 case elfcpp::R_POWERPC_ADDR16_HA
:
7329 case elfcpp::R_POWERPC_UADDR16
:
7330 case elfcpp::R_PPC64_ADDR16_HIGH
:
7331 case elfcpp::R_PPC64_ADDR16_HIGHA
:
7332 case elfcpp::R_PPC64_ADDR16_HIGHER
:
7333 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
7334 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
7335 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
7336 case elfcpp::R_PPC64_ADDR16_DS
:
7337 case elfcpp::R_PPC64_ADDR16_LO_DS
:
7338 case elfcpp::R_POWERPC_ADDR14
:
7339 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
7340 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
7342 // Make a PLT entry if necessary.
7343 if (gsym
->needs_plt_entry())
7345 // Since this is not a PC-relative relocation, we may be
7346 // taking the address of a function. In that case we need to
7347 // set the entry in the dynamic symbol table to the address of
7348 // the PLT call stub.
7349 bool need_ifunc_plt
= false;
7350 if ((size
== 32 || target
->abiversion() >= 2)
7351 && gsym
->is_from_dynobj()
7352 && !parameters
->options().output_is_position_independent())
7354 gsym
->set_needs_dynsym_value();
7355 need_ifunc_plt
= true;
7357 if (!is_ifunc
|| (!pushed_ifunc
&& need_ifunc_plt
))
7359 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7360 target
->push_branch(ppc_object
, data_shndx
,
7361 reloc
.get_r_offset(), r_type
, r_sym
,
7362 reloc
.get_r_addend());
7363 target
->make_plt_entry(symtab
, layout
, gsym
);
7366 // Make a dynamic relocation if necessary.
7367 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
))
7368 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
7370 if (!parameters
->options().output_is_position_independent()
7371 && gsym
->may_need_copy_reloc())
7373 target
->copy_reloc(symtab
, layout
, object
,
7374 data_shndx
, output_section
, gsym
, reloc
);
7376 else if ((((size
== 32
7377 && r_type
== elfcpp::R_POWERPC_ADDR32
)
7379 && r_type
== elfcpp::R_PPC64_ADDR64
7380 && target
->abiversion() >= 2))
7381 && gsym
->can_use_relative_reloc(false)
7382 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
7383 && parameters
->options().shared()))
7385 && r_type
== elfcpp::R_PPC64_ADDR64
7386 && target
->abiversion() < 2
7387 && (gsym
->can_use_relative_reloc(false)
7388 || data_shndx
== ppc_object
->opd_shndx())))
7390 Reloc_section
* rela_dyn
7391 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
7392 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
7393 : elfcpp::R_POWERPC_RELATIVE
);
7394 rela_dyn
->add_symbolless_global_addend(
7395 gsym
, dynrel
, output_section
, object
, data_shndx
,
7396 reloc
.get_r_offset(), reloc
.get_r_addend());
7400 Reloc_section
* rela_dyn
7401 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
7402 check_non_pic(object
, r_type
);
7403 rela_dyn
->add_global(gsym
, r_type
, output_section
,
7405 reloc
.get_r_offset(),
7406 reloc
.get_r_addend());
7409 && parameters
->options().toc_optimize()
7410 && data_shndx
== ppc_object
->toc_shndx())
7411 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
7417 case elfcpp::R_POWERPC_PLT16_LO
:
7418 case elfcpp::R_POWERPC_PLT16_HI
:
7419 case elfcpp::R_POWERPC_PLT16_HA
:
7420 case elfcpp::R_PPC64_PLT16_LO_DS
:
7422 target
->make_plt_entry(symtab
, layout
, gsym
);
7425 case elfcpp::R_PPC_PLTREL24
:
7426 case elfcpp::R_POWERPC_REL24
:
7429 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7430 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
7431 r_type
, r_sym
, reloc
.get_r_addend());
7432 if (gsym
->needs_plt_entry()
7433 || (!gsym
->final_value_is_known()
7434 && (gsym
->is_undefined()
7435 || gsym
->is_from_dynobj()
7436 || gsym
->is_preemptible())))
7437 target
->make_plt_entry(symtab
, layout
, gsym
);
7441 case elfcpp::R_PPC64_REL64
:
7442 case elfcpp::R_POWERPC_REL32
:
7443 // Make a dynamic relocation if necessary.
7444 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
)))
7446 if (!parameters
->options().output_is_position_independent()
7447 && gsym
->may_need_copy_reloc())
7449 target
->copy_reloc(symtab
, layout
, object
,
7450 data_shndx
, output_section
, gsym
,
7455 Reloc_section
* rela_dyn
7456 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
7457 check_non_pic(object
, r_type
);
7458 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
7459 data_shndx
, reloc
.get_r_offset(),
7460 reloc
.get_r_addend());
7465 case elfcpp::R_POWERPC_REL14
:
7466 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7467 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7470 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7471 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
7472 r_type
, r_sym
, reloc
.get_r_addend());
7476 case elfcpp::R_PPC64_TOCSAVE
:
7477 // R_PPC64_TOCSAVE follows a call instruction to indicate the
7478 // caller has already saved r2 and thus a plt call stub need not
7481 && target
->mark_pltcall(ppc_object
, data_shndx
,
7482 reloc
.get_r_offset() - 4, symtab
))
7484 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7486 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
7488 object
->error(_("tocsave symbol %u has bad shndx %u"),
7492 Sized_symbol
<size
>* sym
= symtab
->get_sized_symbol
<size
>(gsym
);
7493 target
->add_tocsave(ppc_object
, shndx
,
7494 sym
->value() + reloc
.get_r_addend());
7499 case elfcpp::R_POWERPC_REL16
:
7500 case elfcpp::R_POWERPC_REL16_LO
:
7501 case elfcpp::R_POWERPC_REL16_HI
:
7502 case elfcpp::R_POWERPC_REL16_HA
:
7503 case elfcpp::R_POWERPC_REL16DX_HA
:
7504 case elfcpp::R_POWERPC_SECTOFF
:
7505 case elfcpp::R_POWERPC_SECTOFF_LO
:
7506 case elfcpp::R_POWERPC_SECTOFF_HI
:
7507 case elfcpp::R_POWERPC_SECTOFF_HA
:
7508 case elfcpp::R_PPC64_SECTOFF_DS
:
7509 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
7510 case elfcpp::R_POWERPC_TPREL16
:
7511 case elfcpp::R_POWERPC_TPREL16_LO
:
7512 case elfcpp::R_POWERPC_TPREL16_HI
:
7513 case elfcpp::R_POWERPC_TPREL16_HA
:
7514 case elfcpp::R_PPC64_TPREL16_DS
:
7515 case elfcpp::R_PPC64_TPREL16_LO_DS
:
7516 case elfcpp::R_PPC64_TPREL16_HIGH
:
7517 case elfcpp::R_PPC64_TPREL16_HIGHA
:
7518 case elfcpp::R_PPC64_TPREL16_HIGHER
:
7519 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
7520 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
7521 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
7522 case elfcpp::R_POWERPC_DTPREL16
:
7523 case elfcpp::R_POWERPC_DTPREL16_LO
:
7524 case elfcpp::R_POWERPC_DTPREL16_HI
:
7525 case elfcpp::R_POWERPC_DTPREL16_HA
:
7526 case elfcpp::R_PPC64_DTPREL16_DS
:
7527 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
7528 case elfcpp::R_PPC64_DTPREL16_HIGH
:
7529 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
7530 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
7531 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
7532 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
7533 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
7534 case elfcpp::R_PPC64_TLSGD
:
7535 case elfcpp::R_PPC64_TLSLD
:
7536 case elfcpp::R_PPC64_ADDR64_LOCAL
:
7539 case elfcpp::R_POWERPC_GOT16
:
7540 case elfcpp::R_POWERPC_GOT16_LO
:
7541 case elfcpp::R_POWERPC_GOT16_HI
:
7542 case elfcpp::R_POWERPC_GOT16_HA
:
7543 case elfcpp::R_PPC64_GOT16_DS
:
7544 case elfcpp::R_PPC64_GOT16_LO_DS
:
7546 // The symbol requires a GOT entry.
7547 Output_data_got_powerpc
<size
, big_endian
>* got
;
7549 got
= target
->got_section(symtab
, layout
);
7550 if (gsym
->final_value_is_known())
7553 && (size
== 32 || target
->abiversion() >= 2))
7554 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
7556 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
7558 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
7560 // If we are generating a shared object or a pie, this
7561 // symbol's GOT entry will be set by a dynamic relocation.
7562 unsigned int off
= got
->add_constant(0);
7563 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
7565 Reloc_section
* rela_dyn
7566 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
7568 if (gsym
->can_use_relative_reloc(false)
7570 || target
->abiversion() >= 2)
7571 && gsym
->visibility() == elfcpp::STV_PROTECTED
7572 && parameters
->options().shared()))
7574 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
7575 : elfcpp::R_POWERPC_RELATIVE
);
7576 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
7580 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
7581 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
7587 case elfcpp::R_PPC64_TOC16
:
7588 case elfcpp::R_PPC64_TOC16_LO
:
7589 case elfcpp::R_PPC64_TOC16_HI
:
7590 case elfcpp::R_PPC64_TOC16_HA
:
7591 case elfcpp::R_PPC64_TOC16_DS
:
7592 case elfcpp::R_PPC64_TOC16_LO_DS
:
7593 // We need a GOT section.
7594 target
->got_section(symtab
, layout
);
7597 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7598 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7599 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
7600 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7602 const bool final
= gsym
->final_value_is_known();
7603 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7604 if (tls_type
== tls::TLSOPT_NONE
)
7606 Output_data_got_powerpc
<size
, big_endian
>* got
7607 = target
->got_section(symtab
, layout
);
7608 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7609 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
, rela_dyn
,
7610 elfcpp::R_POWERPC_DTPMOD
,
7611 elfcpp::R_POWERPC_DTPREL
);
7613 else if (tls_type
== tls::TLSOPT_TO_IE
)
7615 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
7617 Output_data_got_powerpc
<size
, big_endian
>* got
7618 = target
->got_section(symtab
, layout
);
7619 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7620 if (gsym
->is_undefined()
7621 || gsym
->is_from_dynobj())
7623 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
7624 elfcpp::R_POWERPC_TPREL
);
7628 unsigned int off
= got
->add_constant(0);
7629 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
7630 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
7631 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
7636 else if (tls_type
== tls::TLSOPT_TO_LE
)
7638 // no GOT relocs needed for Local Exec.
7645 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7646 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7647 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
7648 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7650 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7651 if (tls_type
== tls::TLSOPT_NONE
)
7652 target
->tlsld_got_offset(symtab
, layout
, object
);
7653 else if (tls_type
== tls::TLSOPT_TO_LE
)
7655 // no GOT relocs needed for Local Exec.
7656 if (parameters
->options().emit_relocs())
7658 Output_section
* os
= layout
->tls_segment()->first_section();
7659 gold_assert(os
!= NULL
);
7660 os
->set_needs_symtab_index();
7668 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7669 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7670 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
7671 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7673 Output_data_got_powerpc
<size
, big_endian
>* got
7674 = target
->got_section(symtab
, layout
);
7675 if (!gsym
->final_value_is_known()
7676 && (gsym
->is_from_dynobj()
7677 || gsym
->is_undefined()
7678 || gsym
->is_preemptible()))
7679 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
7680 target
->rela_dyn_section(layout
),
7681 elfcpp::R_POWERPC_DTPREL
);
7683 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
7687 case elfcpp::R_POWERPC_GOT_TPREL16
:
7688 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7689 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7690 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7692 const bool final
= gsym
->final_value_is_known();
7693 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
7694 if (tls_type
== tls::TLSOPT_NONE
)
7696 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
7698 Output_data_got_powerpc
<size
, big_endian
>* got
7699 = target
->got_section(symtab
, layout
);
7700 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7701 if (gsym
->is_undefined()
7702 || gsym
->is_from_dynobj())
7704 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
7705 elfcpp::R_POWERPC_TPREL
);
7709 unsigned int off
= got
->add_constant(0);
7710 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
7711 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
7712 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
7717 else if (tls_type
== tls::TLSOPT_TO_LE
)
7719 // no GOT relocs needed for Local Exec.
7727 unsupported_reloc_global(object
, r_type
, gsym
);
7732 && parameters
->options().toc_optimize())
7734 if (data_shndx
== ppc_object
->toc_shndx())
7737 if (r_type
!= elfcpp::R_PPC64_ADDR64
7738 || (is_ifunc
&& target
->abiversion() < 2))
7740 else if (parameters
->options().output_is_position_independent()
7741 && (is_ifunc
|| gsym
->is_absolute() || gsym
->is_undefined()))
7744 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
7747 enum {no_check
, check_lo
, check_ha
} insn_check
;
7751 insn_check
= no_check
;
7754 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7755 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7756 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7757 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7758 case elfcpp::R_POWERPC_GOT16_HA
:
7759 case elfcpp::R_PPC64_TOC16_HA
:
7760 insn_check
= check_ha
;
7763 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7764 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7765 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7766 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7767 case elfcpp::R_POWERPC_GOT16_LO
:
7768 case elfcpp::R_PPC64_GOT16_LO_DS
:
7769 case elfcpp::R_PPC64_TOC16_LO
:
7770 case elfcpp::R_PPC64_TOC16_LO_DS
:
7771 insn_check
= check_lo
;
7775 section_size_type slen
;
7776 const unsigned char* view
= NULL
;
7777 if (insn_check
!= no_check
)
7779 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
7780 section_size_type off
=
7781 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
7784 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
7785 if (insn_check
== check_lo
7786 ? !ok_lo_toc_insn(insn
, r_type
)
7787 : ((insn
& ((0x3f << 26) | 0x1f << 16))
7788 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7790 ppc_object
->set_no_toc_opt();
7791 gold_warning(_("%s: toc optimization is not supported "
7792 "for %#08x instruction"),
7793 ppc_object
->name().c_str(), insn
);
7802 case elfcpp::R_PPC64_TOC16
:
7803 case elfcpp::R_PPC64_TOC16_LO
:
7804 case elfcpp::R_PPC64_TOC16_HI
:
7805 case elfcpp::R_PPC64_TOC16_HA
:
7806 case elfcpp::R_PPC64_TOC16_DS
:
7807 case elfcpp::R_PPC64_TOC16_LO_DS
:
7808 if (gsym
->source() == Symbol::FROM_OBJECT
7809 && !gsym
->object()->is_dynamic())
7811 Powerpc_relobj
<size
, big_endian
>* sym_object
7812 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7814 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
7815 if (shndx
== sym_object
->toc_shndx())
7817 Sized_symbol
<size
>* sym
= symtab
->get_sized_symbol
<size
>(gsym
);
7818 Address dst_off
= sym
->value() + reloc
.get_r_addend();
7819 if (dst_off
< sym_object
->section_size(shndx
))
7822 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
7824 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
7826 // Need to check that the insn is a ld
7828 view
= ppc_object
->section_contents(data_shndx
,
7831 section_size_type off
=
7832 (convert_to_section_size_type(reloc
.get_r_offset())
7833 + (big_endian
? -2 : 3));
7835 && (view
[off
] & (0x3f << 2)) == (58u << 2))
7839 sym_object
->set_no_toc_opt(dst_off
);
7851 case elfcpp::R_PPC_LOCAL24PC
:
7852 if (strcmp(gsym
->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7853 gold_error(_("%s: unsupported -mbss-plt code"),
7854 ppc_object
->name().c_str());
7863 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7864 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7865 case elfcpp::R_POWERPC_GOT_TPREL16
:
7866 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7867 case elfcpp::R_POWERPC_GOT16
:
7868 case elfcpp::R_PPC64_GOT16_DS
:
7869 case elfcpp::R_PPC64_TOC16
:
7870 case elfcpp::R_PPC64_TOC16_DS
:
7871 ppc_object
->set_has_small_toc_reloc();
7877 // Process relocations for gc.
7879 template<int size
, bool big_endian
>
7881 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
7882 Symbol_table
* symtab
,
7884 Sized_relobj_file
<size
, big_endian
>* object
,
7885 unsigned int data_shndx
,
7887 const unsigned char* prelocs
,
7889 Output_section
* output_section
,
7890 bool needs_special_offset_handling
,
7891 size_t local_symbol_count
,
7892 const unsigned char* plocal_symbols
)
7894 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
7895 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
7898 Powerpc_relobj
<size
, big_endian
>* ppc_object
7899 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
7901 ppc_object
->set_opd_valid();
7902 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
7904 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
7905 for (p
= ppc_object
->access_from_map()->begin();
7906 p
!= ppc_object
->access_from_map()->end();
7909 Address dst_off
= p
->first
;
7910 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
7911 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
7912 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
7914 Relobj
* src_obj
= s
->first
;
7915 unsigned int src_indx
= s
->second
;
7916 symtab
->gc()->add_reference(src_obj
, src_indx
,
7917 ppc_object
, dst_indx
);
7921 ppc_object
->access_from_map()->clear();
7922 ppc_object
->process_gc_mark(symtab
);
7923 // Don't look at .opd relocs as .opd will reference everything.
7927 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
7936 needs_special_offset_handling
,
7941 // Handle target specific gc actions when adding a gc reference from
7942 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7943 // and DST_OFF. For powerpc64, this adds a referenc to the code
7944 // section of a function descriptor.
7946 template<int size
, bool big_endian
>
7948 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
7949 Symbol_table
* symtab
,
7951 unsigned int src_shndx
,
7953 unsigned int dst_shndx
,
7954 Address dst_off
) const
7956 if (size
!= 64 || dst_obj
->is_dynamic())
7959 Powerpc_relobj
<size
, big_endian
>* ppc_object
7960 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
7961 if (dst_shndx
!= 0 && dst_shndx
== ppc_object
->opd_shndx())
7963 if (ppc_object
->opd_valid())
7965 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
7966 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
7970 // If we haven't run scan_opd_relocs, we must delay
7971 // processing this function descriptor reference.
7972 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
7977 // Add any special sections for this symbol to the gc work list.
7978 // For powerpc64, this adds the code section of a function
7981 template<int size
, bool big_endian
>
7983 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
7984 Symbol_table
* symtab
,
7989 Powerpc_relobj
<size
, big_endian
>* ppc_object
7990 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
7992 unsigned int shndx
= sym
->shndx(&is_ordinary
);
7993 if (is_ordinary
&& shndx
!= 0 && shndx
== ppc_object
->opd_shndx())
7995 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
7996 Address dst_off
= gsym
->value();
7997 if (ppc_object
->opd_valid())
7999 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
8000 symtab
->gc()->worklist().push_back(Section_id(ppc_object
,
8004 ppc_object
->add_gc_mark(dst_off
);
8009 // For a symbol location in .opd, set LOC to the location of the
8012 template<int size
, bool big_endian
>
8014 Target_powerpc
<size
, big_endian
>::do_function_location(
8015 Symbol_location
* loc
) const
8017 if (size
== 64 && loc
->shndx
!= 0)
8019 if (loc
->object
->is_dynamic())
8021 Powerpc_dynobj
<size
, big_endian
>* ppc_object
8022 = static_cast<Powerpc_dynobj
<size
, big_endian
>*>(loc
->object
);
8023 if (loc
->shndx
== ppc_object
->opd_shndx())
8026 Address off
= loc
->offset
- ppc_object
->opd_address();
8027 loc
->shndx
= ppc_object
->get_opd_ent(off
, &dest_off
);
8028 loc
->offset
= dest_off
;
8033 const Powerpc_relobj
<size
, big_endian
>* ppc_object
8034 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(loc
->object
);
8035 if (loc
->shndx
== ppc_object
->opd_shndx())
8038 loc
->shndx
= ppc_object
->get_opd_ent(loc
->offset
, &dest_off
);
8039 loc
->offset
= dest_off
;
8045 // FNOFFSET in section SHNDX in OBJECT is the start of a function
8046 // compiled with -fsplit-stack. The function calls non-split-stack
8047 // code. Change the function to ensure it has enough stack space to
8048 // call some random function.
8050 template<int size
, bool big_endian
>
8052 Target_powerpc
<size
, big_endian
>::do_calls_non_split(
8055 section_offset_type fnoffset
,
8056 section_size_type fnsize
,
8057 const unsigned char* prelocs
,
8059 unsigned char* view
,
8060 section_size_type view_size
,
8062 std::string
* to
) const
8064 // 32-bit not supported.
8068 Target::do_calls_non_split(object
, shndx
, fnoffset
, fnsize
,
8069 prelocs
, reloc_count
, view
, view_size
,
8074 // The function always starts with
8075 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
8076 // addis %r12,%r1,-allocate@ha
8077 // addi %r12,%r12,-allocate@l
8079 // but note that the addis or addi may be replaced with a nop
8081 unsigned char *entry
= view
+ fnoffset
;
8082 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
8084 if ((insn
& 0xffff0000) == addis_2_12
)
8086 /* Skip ELFv2 global entry code. */
8088 insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
8091 unsigned char *pinsn
= entry
;
8093 const uint32_t ld_private_ss
= 0xe80d8fc0;
8094 if (insn
== ld_private_ss
)
8096 int32_t allocate
= 0;
8100 insn
= elfcpp::Swap
<32, big_endian
>::readval(pinsn
);
8101 if ((insn
& 0xffff0000) == addis_12_1
)
8102 allocate
+= (insn
& 0xffff) << 16;
8103 else if ((insn
& 0xffff0000) == addi_12_1
8104 || (insn
& 0xffff0000) == addi_12_12
)
8105 allocate
+= ((insn
& 0xffff) ^ 0x8000) - 0x8000;
8106 else if (insn
!= nop
)
8109 if (insn
== cmpld_7_12_0
&& pinsn
== entry
+ 12)
8111 int extra
= parameters
->options().split_stack_adjust_size();
8113 if (allocate
>= 0 || extra
< 0)
8115 object
->error(_("split-stack stack size overflow at "
8116 "section %u offset %0zx"),
8117 shndx
, static_cast<size_t>(fnoffset
));
8121 insn
= addis_12_1
| (((allocate
+ 0x8000) >> 16) & 0xffff);
8122 if (insn
!= addis_12_1
)
8124 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
8126 insn
= addi_12_12
| (allocate
& 0xffff);
8127 if (insn
!= addi_12_12
)
8129 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
8135 insn
= addi_12_1
| (allocate
& 0xffff);
8136 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
8139 if (pinsn
!= entry
+ 12)
8140 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, nop
);
8148 if (!object
->has_no_split_stack())
8149 object
->error(_("failed to match split-stack sequence at "
8150 "section %u offset %0zx"),
8151 shndx
, static_cast<size_t>(fnoffset
));
8155 // Scan relocations for a section.
8157 template<int size
, bool big_endian
>
8159 Target_powerpc
<size
, big_endian
>::scan_relocs(
8160 Symbol_table
* symtab
,
8162 Sized_relobj_file
<size
, big_endian
>* object
,
8163 unsigned int data_shndx
,
8164 unsigned int sh_type
,
8165 const unsigned char* prelocs
,
8167 Output_section
* output_section
,
8168 bool needs_special_offset_handling
,
8169 size_t local_symbol_count
,
8170 const unsigned char* plocal_symbols
)
8172 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
8173 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
8176 if (!this->plt_localentry0_init_
)
8178 bool plt_localentry0
= false;
8180 && this->abiversion() >= 2)
8182 if (parameters
->options().user_set_plt_localentry())
8183 plt_localentry0
= parameters
->options().plt_localentry();
8185 && symtab
->lookup("GLIBC_2.26", NULL
) == NULL
)
8186 gold_warning(_("--plt-localentry is especially dangerous without "
8187 "ld.so support to detect ABI violations"));
8189 this->plt_localentry0_
= plt_localentry0
;
8190 this->plt_localentry0_init_
= true;
8193 if (sh_type
== elfcpp::SHT_REL
)
8195 gold_error(_("%s: unsupported REL reloc section"),
8196 object
->name().c_str());
8200 gold::scan_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
8209 needs_special_offset_handling
,
8214 // Functor class for processing the global symbol table.
8215 // Removes symbols defined on discarded opd entries.
8217 template<bool big_endian
>
8218 class Global_symbol_visitor_opd
8221 Global_symbol_visitor_opd()
8225 operator()(Sized_symbol
<64>* sym
)
8227 if (sym
->has_symtab_index()
8228 || sym
->source() != Symbol::FROM_OBJECT
8229 || !sym
->in_real_elf())
8232 if (sym
->object()->is_dynamic())
8235 Powerpc_relobj
<64, big_endian
>* symobj
8236 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
8237 if (symobj
->opd_shndx() == 0)
8241 unsigned int shndx
= sym
->shndx(&is_ordinary
);
8242 if (shndx
== symobj
->opd_shndx()
8243 && symobj
->get_opd_discard(sym
->value()))
8245 sym
->set_undefined();
8246 sym
->set_visibility(elfcpp::STV_DEFAULT
);
8247 sym
->set_is_defined_in_discarded_section();
8248 sym
->set_symtab_index(-1U);
8253 template<int size
, bool big_endian
>
8255 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
8257 Symbol_table
* symtab
)
8261 Output_data_save_res
<size
, big_endian
>* savres
8262 = new Output_data_save_res
<size
, big_endian
>(symtab
);
8263 this->savres_section_
= savres
;
8264 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
8265 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
8266 savres
, ORDER_TEXT
, false);
8270 // Sort linker created .got section first (for the header), then input
8271 // sections belonging to files using small model code.
8273 template<bool big_endian
>
8274 class Sort_toc_sections
8278 operator()(const Output_section::Input_section
& is1
,
8279 const Output_section::Input_section
& is2
) const
8281 if (!is1
.is_input_section() && is2
.is_input_section())
8284 = (is1
.is_input_section()
8285 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
8286 ->has_small_toc_reloc()));
8288 = (is2
.is_input_section()
8289 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
8290 ->has_small_toc_reloc()));
8291 return small1
&& !small2
;
8295 // Finalize the sections.
8297 template<int size
, bool big_endian
>
8299 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
8301 const Input_objects
*,
8302 Symbol_table
* symtab
)
8304 if (parameters
->doing_static_link())
8306 // At least some versions of glibc elf-init.o have a strong
8307 // reference to __rela_iplt marker syms. A weak ref would be
8309 if (this->iplt_
!= NULL
)
8311 Reloc_section
* rel
= this->iplt_
->rel_plt();
8312 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
8313 Symbol_table::PREDEFINED
, rel
, 0, 0,
8314 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
8315 elfcpp::STV_HIDDEN
, 0, false, true);
8316 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
8317 Symbol_table::PREDEFINED
, rel
, 0, 0,
8318 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
8319 elfcpp::STV_HIDDEN
, 0, true, true);
8323 symtab
->define_as_constant("__rela_iplt_start", NULL
,
8324 Symbol_table::PREDEFINED
, 0, 0,
8325 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
8326 elfcpp::STV_HIDDEN
, 0, true, false);
8327 symtab
->define_as_constant("__rela_iplt_end", NULL
,
8328 Symbol_table::PREDEFINED
, 0, 0,
8329 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
8330 elfcpp::STV_HIDDEN
, 0, true, false);
8336 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
8337 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
8339 if (!parameters
->options().relocatable())
8341 this->define_save_restore_funcs(layout
, symtab
);
8343 // Annoyingly, we need to make these sections now whether or
8344 // not we need them. If we delay until do_relax then we
8345 // need to mess with the relaxation machinery checkpointing.
8346 this->got_section(symtab
, layout
);
8347 this->make_brlt_section(layout
);
8349 if (parameters
->options().toc_sort())
8351 Output_section
* os
= this->got_
->output_section();
8352 if (os
!= NULL
&& os
->input_sections().size() > 1)
8353 std::stable_sort(os
->input_sections().begin(),
8354 os
->input_sections().end(),
8355 Sort_toc_sections
<big_endian
>());
8360 // Fill in some more dynamic tags.
8361 Output_data_dynamic
* odyn
= layout
->dynamic_data();
8364 const Reloc_section
* rel_plt
= (this->plt_
== NULL
8366 : this->plt_
->rel_plt());
8367 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
8368 this->rela_dyn_
, true, size
== 32);
8372 if (this->got_
!= NULL
)
8374 this->got_
->finalize_data_size();
8375 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
8376 this->got_
, this->got_
->g_o_t());
8378 if (this->has_tls_get_addr_opt_
)
8379 odyn
->add_constant(elfcpp::DT_PPC_OPT
, elfcpp::PPC_OPT_TLS
);
8383 if (this->glink_
!= NULL
)
8385 this->glink_
->finalize_data_size();
8386 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
8388 (this->glink_
->pltresolve_size()
8391 if (this->has_localentry0_
|| this->has_tls_get_addr_opt_
)
8392 odyn
->add_constant(elfcpp::DT_PPC64_OPT
,
8393 ((this->has_localentry0_
8394 ? elfcpp::PPC64_OPT_LOCALENTRY
: 0)
8395 | (this->has_tls_get_addr_opt_
8396 ? elfcpp::PPC64_OPT_TLS
: 0)));
8400 // Emit any relocs we saved in an attempt to avoid generating COPY
8402 if (this->copy_relocs_
.any_saved_relocs())
8403 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
8406 // Emit any saved relocs, and mark toc entries using any of these
8407 // relocs as not optimizable.
8409 template<int sh_type
, int size
, bool big_endian
>
8411 Powerpc_copy_relocs
<sh_type
, size
, big_endian
>::emit(
8412 Output_data_reloc
<sh_type
, true, size
, big_endian
>* reloc_section
)
8415 && parameters
->options().toc_optimize())
8417 for (typename Copy_relocs
<sh_type
, size
, big_endian
>::
8418 Copy_reloc_entries::iterator p
= this->entries_
.begin();
8419 p
!= this->entries_
.end();
8422 typename Copy_relocs
<sh_type
, size
, big_endian
>::Copy_reloc_entry
&
8425 // If the symbol is no longer defined in a dynamic object,
8426 // then we emitted a COPY relocation. If it is still
8427 // dynamic then we'll need dynamic relocations and thus
8428 // can't optimize toc entries.
8429 if (entry
.sym_
->is_from_dynobj())
8431 Powerpc_relobj
<size
, big_endian
>* ppc_object
8432 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(entry
.relobj_
);
8433 if (entry
.shndx_
== ppc_object
->toc_shndx())
8434 ppc_object
->set_no_toc_opt(entry
.address_
);
8439 Copy_relocs
<sh_type
, size
, big_endian
>::emit(reloc_section
);
8442 // Return the value to use for a branch relocation.
8444 template<int size
, bool big_endian
>
8446 Target_powerpc
<size
, big_endian
>::symval_for_branch(
8447 const Symbol_table
* symtab
,
8448 const Sized_symbol
<size
>* gsym
,
8449 Powerpc_relobj
<size
, big_endian
>* object
,
8451 unsigned int *dest_shndx
)
8453 if (size
== 32 || this->abiversion() >= 2)
8457 // If the symbol is defined in an opd section, ie. is a function
8458 // descriptor, use the function descriptor code entry address
8459 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
8461 && (gsym
->source() != Symbol::FROM_OBJECT
8462 || gsym
->object()->is_dynamic()))
8465 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
8466 unsigned int shndx
= symobj
->opd_shndx();
8469 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
8470 if (opd_addr
== invalid_address
)
8472 opd_addr
+= symobj
->output_section_address(shndx
);
8473 if (*value
>= opd_addr
&& *value
< opd_addr
+ symobj
->section_size(shndx
))
8476 *dest_shndx
= symobj
->get_opd_ent(*value
- opd_addr
, &sec_off
);
8477 if (symtab
->is_section_folded(symobj
, *dest_shndx
))
8480 = symtab
->icf()->get_folded_section(symobj
, *dest_shndx
);
8481 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(folded
.first
);
8482 *dest_shndx
= folded
.second
;
8484 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
8485 if (sec_addr
== invalid_address
)
8488 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
8489 *value
= sec_addr
+ sec_off
;
8494 // Perform a relocation.
8496 template<int size
, bool big_endian
>
8498 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
8499 const Relocate_info
<size
, big_endian
>* relinfo
,
8501 Target_powerpc
* target
,
8504 const unsigned char* preloc
,
8505 const Sized_symbol
<size
>* gsym
,
8506 const Symbol_value
<size
>* psymval
,
8507 unsigned char* view
,
8509 section_size_type view_size
)
8511 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
8512 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
8513 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
8518 if (target
->replace_tls_get_addr(gsym
))
8519 gsym
= static_cast<const Sized_symbol
<size
>*>(target
->tls_get_addr_opt());
8521 const elfcpp::Rela
<size
, big_endian
> rela(preloc
);
8522 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
8523 switch (this->maybe_skip_tls_get_addr_call(target
, r_type
, gsym
))
8525 case Track_tls::NOT_EXPECTED
:
8526 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8527 _("__tls_get_addr call lacks marker reloc"));
8529 case Track_tls::EXPECTED
:
8530 // We have already complained.
8532 case Track_tls::SKIP
:
8533 if (is_plt16_reloc
<size
>(r_type
)
8534 || r_type
== elfcpp::R_POWERPC_PLTSEQ
)
8536 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8537 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
8539 else if (size
== 64 && r_type
== elfcpp::R_POWERPC_PLTCALL
)
8541 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8542 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, nop
);
8545 case Track_tls::NORMAL
:
8549 // Offset from start of insn to d-field reloc.
8550 const int d_offset
= big_endian
? 2 : 0;
8552 Powerpc_relobj
<size
, big_endian
>* const object
8553 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
8555 bool has_stub_value
= false;
8556 bool localentry0
= false;
8557 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
8560 ? gsym
->use_plt_offset(Scan::get_reference_flags(r_type
, target
))
8561 : object
->local_has_plt_offset(r_sym
));
8563 && !is_plt16_reloc
<size
>(r_type
)
8564 && r_type
!= elfcpp::R_POWERPC_PLTSEQ
8565 && r_type
!= elfcpp::R_POWERPC_PLTCALL
8566 && (!psymval
->is_ifunc_symbol()
8567 || Scan::reloc_needs_plt_for_ifunc(target
, object
, r_type
, false)))
8571 && target
->abiversion() >= 2
8572 && !parameters
->options().output_is_position_independent()
8573 && !is_branch_reloc(r_type
))
8575 Address off
= target
->glink_section()->find_global_entry(gsym
);
8576 if (off
!= invalid_address
)
8578 value
= target
->glink_section()->global_entry_address() + off
;
8579 has_stub_value
= true;
8584 Stub_table
<size
, big_endian
>* stub_table
= NULL
;
8585 if (target
->stub_tables().size() == 1)
8586 stub_table
= target
->stub_tables()[0];
8587 if (stub_table
== NULL
8590 && !parameters
->options().output_is_position_independent()
8591 && !is_branch_reloc(r_type
)))
8592 stub_table
= object
->stub_table(relinfo
->data_shndx
);
8593 if (stub_table
== NULL
)
8595 // This is a ref from a data section to an ifunc symbol,
8596 // or a non-branch reloc for which we always want to use
8597 // one set of stubs for resolving function addresses.
8598 if (target
->stub_tables().size() != 0)
8599 stub_table
= target
->stub_tables()[0];
8601 if (stub_table
!= NULL
)
8603 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
;
8605 ent
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
8606 rela
.get_r_addend());
8608 ent
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
8609 rela
.get_r_addend());
8612 value
= stub_table
->stub_address() + ent
->off_
;
8613 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
8614 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->reloc_shdr
);
8615 size_t reloc_count
= shdr
.get_sh_size() / reloc_size
;
8618 && relnum
+ 1 < reloc_count
)
8620 Reltype
next_rela(preloc
+ reloc_size
);
8621 if (elfcpp::elf_r_type
<size
>(next_rela
.get_r_info())
8622 == elfcpp::R_PPC64_TOCSAVE
8623 && next_rela
.get_r_offset() == rela
.get_r_offset() + 4)
8626 localentry0
= ent
->localentry0_
;
8627 has_stub_value
= true;
8631 // We don't care too much about bogus debug references to
8632 // non-local functions, but otherwise there had better be a plt
8633 // call stub or global entry stub as appropriate.
8634 gold_assert(has_stub_value
|| !(os
->flags() & elfcpp::SHF_ALLOC
));
8637 if (has_plt_offset
&& is_plt16_reloc
<size
>(r_type
))
8639 const Output_data_plt_powerpc
<size
, big_endian
>* plt
;
8641 value
= target
->plt_off(gsym
, &plt
);
8643 value
= target
->plt_off(object
, r_sym
, &plt
);
8644 value
+= plt
->address();
8647 value
-= (target
->got_section()->output_section()->address()
8648 + object
->toc_base_offset());
8649 else if (parameters
->options().output_is_position_independent())
8651 if (rela
.get_r_addend() >= 32768)
8653 unsigned int got2
= object
->got2_shndx();
8654 value
-= (object
->get_output_section_offset(got2
)
8655 + object
->output_section(got2
)->address()
8656 + rela
.get_r_addend());
8659 value
-= (target
->got_section()->address()
8660 + target
->got_section()->g_o_t());
8663 else if (!has_plt_offset
8664 && (is_plt16_reloc
<size
>(r_type
)
8665 || r_type
== elfcpp::R_POWERPC_PLTSEQ
))
8667 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8668 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
8669 r_type
= elfcpp::R_POWERPC_NONE
;
8671 else if (r_type
== elfcpp::R_POWERPC_GOT16
8672 || r_type
== elfcpp::R_POWERPC_GOT16_LO
8673 || r_type
== elfcpp::R_POWERPC_GOT16_HI
8674 || r_type
== elfcpp::R_POWERPC_GOT16_HA
8675 || r_type
== elfcpp::R_PPC64_GOT16_DS
8676 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
8680 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
8681 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
8685 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
8686 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
8688 value
-= target
->got_section()->got_base_offset(object
);
8690 else if (r_type
== elfcpp::R_PPC64_TOC
)
8692 value
= (target
->got_section()->output_section()->address()
8693 + object
->toc_base_offset());
8695 else if (gsym
!= NULL
8696 && (r_type
== elfcpp::R_POWERPC_REL24
8697 || r_type
== elfcpp::R_PPC_PLTREL24
)
8702 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
8703 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
8704 bool can_plt_call
= localentry0
|| target
->is_tls_get_addr_opt(gsym
);
8705 if (!can_plt_call
&& rela
.get_r_offset() + 8 <= view_size
)
8707 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
8708 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
8711 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
8713 elfcpp::Swap
<32, big_endian
>::
8714 writeval(wv
+ 1, ld_2_1
+ target
->stk_toc());
8715 can_plt_call
= true;
8720 // If we don't have a branch and link followed by a nop,
8721 // we can't go via the plt because there is no place to
8722 // put a toc restoring instruction.
8723 // Unless we know we won't be returning.
8724 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
8725 can_plt_call
= true;
8729 // g++ as of 20130507 emits self-calls without a
8730 // following nop. This is arguably wrong since we have
8731 // conflicting information. On the one hand a global
8732 // symbol and on the other a local call sequence, but
8733 // don't error for this special case.
8734 // It isn't possible to cheaply verify we have exactly
8735 // such a call. Allow all calls to the same section.
8737 Address code
= value
;
8738 if (gsym
->source() == Symbol::FROM_OBJECT
8739 && gsym
->object() == object
)
8741 unsigned int dest_shndx
= 0;
8742 if (target
->abiversion() < 2)
8744 Address addend
= rela
.get_r_addend();
8745 code
= psymval
->value(object
, addend
);
8746 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
8747 &code
, &dest_shndx
);
8750 if (dest_shndx
== 0)
8751 dest_shndx
= gsym
->shndx(&is_ordinary
);
8752 ok
= dest_shndx
== relinfo
->data_shndx
;
8756 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8757 _("call lacks nop, can't restore toc; "
8758 "recompile with -fPIC"));
8764 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8765 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
8766 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
8767 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
8769 // First instruction of a global dynamic sequence, arg setup insn.
8770 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8771 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
8772 enum Got_type got_type
= GOT_TYPE_STANDARD
;
8773 if (tls_type
== tls::TLSOPT_NONE
)
8774 got_type
= GOT_TYPE_TLSGD
;
8775 else if (tls_type
== tls::TLSOPT_TO_IE
)
8776 got_type
= GOT_TYPE_TPREL
;
8777 if (got_type
!= GOT_TYPE_STANDARD
)
8781 gold_assert(gsym
->has_got_offset(got_type
));
8782 value
= gsym
->got_offset(got_type
);
8786 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
8787 value
= object
->local_got_offset(r_sym
, got_type
);
8789 value
-= target
->got_section()->got_base_offset(object
);
8791 if (tls_type
== tls::TLSOPT_TO_IE
)
8793 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8794 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
8796 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8797 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8798 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
8800 insn
|= 32 << 26; // lwz
8802 insn
|= 58 << 26; // ld
8803 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8805 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
8806 - elfcpp::R_POWERPC_GOT_TLSGD16
);
8808 else if (tls_type
== tls::TLSOPT_TO_LE
)
8810 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8811 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
8813 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8814 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8815 insn
&= (1 << 26) - (1 << 21); // extract rt
8820 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8821 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8822 value
= psymval
->value(object
, rela
.get_r_addend());
8826 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8828 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8829 r_type
= elfcpp::R_POWERPC_NONE
;
8833 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
8834 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
8835 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
8836 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
8838 // First instruction of a local dynamic sequence, arg setup insn.
8839 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
8840 if (tls_type
== tls::TLSOPT_NONE
)
8842 value
= target
->tlsld_got_offset();
8843 value
-= target
->got_section()->got_base_offset(object
);
8847 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
8848 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
8849 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
8851 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8852 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8853 insn
&= (1 << 26) - (1 << 21); // extract rt
8858 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8859 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8864 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8866 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8867 r_type
= elfcpp::R_POWERPC_NONE
;
8871 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
8872 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
8873 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
8874 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
8876 // Accesses relative to a local dynamic sequence address,
8877 // no optimisation here.
8880 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
8881 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
8885 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
8886 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
8888 value
-= target
->got_section()->got_base_offset(object
);
8890 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8891 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
8892 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
8893 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
8895 // First instruction of initial exec sequence.
8896 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8897 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
8898 if (tls_type
== tls::TLSOPT_NONE
)
8902 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
8903 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
8907 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
8908 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
8910 value
-= target
->got_section()->got_base_offset(object
);
8914 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
8915 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8916 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
8918 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8919 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8920 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
8925 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8926 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8927 value
= psymval
->value(object
, rela
.get_r_addend());
8931 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8933 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8934 r_type
= elfcpp::R_POWERPC_NONE
;
8938 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
8939 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
8941 // Second instruction of a global dynamic sequence,
8942 // the __tls_get_addr call
8943 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
8944 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8945 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
8946 if (tls_type
!= tls::TLSOPT_NONE
)
8948 if (tls_type
== tls::TLSOPT_TO_IE
)
8950 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8951 Insn insn
= add_3_3_13
;
8954 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8955 r_type
= elfcpp::R_POWERPC_NONE
;
8959 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8960 Insn insn
= addi_3_3
;
8961 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8962 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8964 value
= psymval
->value(object
, rela
.get_r_addend());
8966 this->skip_next_tls_get_addr_call();
8969 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
8970 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
8972 // Second instruction of a local dynamic sequence,
8973 // the __tls_get_addr call
8974 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
8975 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
8976 if (tls_type
== tls::TLSOPT_TO_LE
)
8978 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8979 Insn insn
= addi_3_3
;
8980 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8981 this->skip_next_tls_get_addr_call();
8982 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8987 else if (r_type
== elfcpp::R_POWERPC_TLS
)
8989 // Second instruction of an initial exec sequence
8990 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8991 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
8992 if (tls_type
== tls::TLSOPT_TO_LE
)
8994 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8995 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8996 unsigned int reg
= size
== 32 ? 2 : 13;
8997 insn
= at_tls_transform(insn
, reg
);
8998 gold_assert(insn
!= 0);
8999 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9000 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9002 value
= psymval
->value(object
, rela
.get_r_addend());
9005 else if (!has_stub_value
)
9007 if (!has_plt_offset
&& r_type
== elfcpp::R_POWERPC_PLTCALL
)
9009 // PLTCALL without plt entry => convert to direct call
9010 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9011 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9012 insn
= (insn
& 1) | b
;
9013 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9015 r_type
= elfcpp::R_PPC_PLTREL24
;
9017 r_type
= elfcpp::R_POWERPC_REL24
;
9021 && (r_type
== elfcpp::R_PPC_PLTREL24
9022 || r_type
== elfcpp::R_POWERPC_PLT16_LO
9023 || r_type
== elfcpp::R_POWERPC_PLT16_HI
9024 || r_type
== elfcpp::R_POWERPC_PLT16_HA
)))
9025 addend
= rela
.get_r_addend();
9026 value
= psymval
->value(object
, addend
);
9027 if (size
== 64 && is_branch_reloc(r_type
))
9029 if (target
->abiversion() >= 2)
9032 value
+= object
->ppc64_local_entry_offset(gsym
);
9034 value
+= object
->ppc64_local_entry_offset(r_sym
);
9038 unsigned int dest_shndx
;
9039 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
9040 &value
, &dest_shndx
);
9043 Address max_branch_offset
= max_branch_delta(r_type
);
9044 if (max_branch_offset
!= 0
9045 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
9047 Stub_table
<size
, big_endian
>* stub_table
9048 = object
->stub_table(relinfo
->data_shndx
);
9049 if (stub_table
!= NULL
)
9051 Address off
= stub_table
->find_long_branch_entry(object
, value
);
9052 if (off
!= invalid_address
)
9054 value
= (stub_table
->stub_address() + stub_table
->plt_size()
9056 has_stub_value
= true;
9064 case elfcpp::R_PPC64_REL64
:
9065 case elfcpp::R_POWERPC_REL32
:
9066 case elfcpp::R_POWERPC_REL24
:
9067 case elfcpp::R_PPC_PLTREL24
:
9068 case elfcpp::R_PPC_LOCAL24PC
:
9069 case elfcpp::R_POWERPC_REL16
:
9070 case elfcpp::R_POWERPC_REL16_LO
:
9071 case elfcpp::R_POWERPC_REL16_HI
:
9072 case elfcpp::R_POWERPC_REL16_HA
:
9073 case elfcpp::R_POWERPC_REL16DX_HA
:
9074 case elfcpp::R_POWERPC_REL14
:
9075 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
9076 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
9080 case elfcpp::R_PPC64_TOC16
:
9081 case elfcpp::R_PPC64_TOC16_LO
:
9082 case elfcpp::R_PPC64_TOC16_HI
:
9083 case elfcpp::R_PPC64_TOC16_HA
:
9084 case elfcpp::R_PPC64_TOC16_DS
:
9085 case elfcpp::R_PPC64_TOC16_LO_DS
:
9086 // Subtract the TOC base address.
9087 value
-= (target
->got_section()->output_section()->address()
9088 + object
->toc_base_offset());
9091 case elfcpp::R_POWERPC_SECTOFF
:
9092 case elfcpp::R_POWERPC_SECTOFF_LO
:
9093 case elfcpp::R_POWERPC_SECTOFF_HI
:
9094 case elfcpp::R_POWERPC_SECTOFF_HA
:
9095 case elfcpp::R_PPC64_SECTOFF_DS
:
9096 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
9098 value
-= os
->address();
9101 case elfcpp::R_PPC64_TPREL16_DS
:
9102 case elfcpp::R_PPC64_TPREL16_LO_DS
:
9103 case elfcpp::R_PPC64_TPREL16_HIGH
:
9104 case elfcpp::R_PPC64_TPREL16_HIGHA
:
9106 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
9109 case elfcpp::R_POWERPC_TPREL16
:
9110 case elfcpp::R_POWERPC_TPREL16_LO
:
9111 case elfcpp::R_POWERPC_TPREL16_HI
:
9112 case elfcpp::R_POWERPC_TPREL16_HA
:
9113 case elfcpp::R_POWERPC_TPREL
:
9114 case elfcpp::R_PPC64_TPREL16_HIGHER
:
9115 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
9116 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
9117 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
9118 // tls symbol values are relative to tls_segment()->vaddr()
9122 case elfcpp::R_PPC64_DTPREL16_DS
:
9123 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
9124 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
9125 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
9126 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
9127 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
9129 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
9130 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
9133 case elfcpp::R_POWERPC_DTPREL16
:
9134 case elfcpp::R_POWERPC_DTPREL16_LO
:
9135 case elfcpp::R_POWERPC_DTPREL16_HI
:
9136 case elfcpp::R_POWERPC_DTPREL16_HA
:
9137 case elfcpp::R_POWERPC_DTPREL
:
9138 case elfcpp::R_PPC64_DTPREL16_HIGH
:
9139 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
9140 // tls symbol values are relative to tls_segment()->vaddr()
9141 value
-= dtp_offset
;
9144 case elfcpp::R_PPC64_ADDR64_LOCAL
:
9146 value
+= object
->ppc64_local_entry_offset(gsym
);
9148 value
+= object
->ppc64_local_entry_offset(r_sym
);
9155 Insn branch_bit
= 0;
9158 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
9159 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
9160 branch_bit
= 1 << 21;
9162 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
9163 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
9165 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9166 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9169 if (this->is_isa_v2
)
9171 // Set 'a' bit. This is 0b00010 in BO field for branch
9172 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
9173 // for branch on CTR insns (BO == 1a00t or 1a01t).
9174 if ((insn
& (0x14 << 21)) == (0x04 << 21))
9176 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
9183 // Invert 'y' bit if not the default.
9184 if (static_cast<Signed_address
>(value
) < 0)
9187 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9191 case elfcpp::R_POWERPC_PLT16_HA
:
9193 && !parameters
->options().output_is_position_independent())
9195 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9196 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9198 // Convert addis to lis.
9199 if ((insn
& (0x3f << 26)) == 15u << 26
9200 && (insn
& (0x1f << 16)) != 0)
9202 insn
&= ~(0x1f << 16);
9203 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9219 // Multi-instruction sequences that access the GOT/TOC can
9220 // be optimized, eg.
9221 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
9222 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
9224 // addis ra,r2,0; addi rb,ra,x@toc@l;
9225 // to nop; addi rb,r2,x@toc;
9226 // FIXME: the @got sequence shown above is not yet
9227 // optimized. Note that gcc as of 2017-01-07 doesn't use
9228 // the ELF @got relocs except for TLS, instead using the
9229 // PowerOpen variant of a compiler managed GOT (called TOC).
9230 // The PowerOpen TOC sequence equivalent to the first
9231 // example is optimized.
9232 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
9233 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
9234 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
9235 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
9236 case elfcpp::R_POWERPC_GOT16_HA
:
9237 case elfcpp::R_PPC64_TOC16_HA
:
9238 if (parameters
->options().toc_optimize())
9240 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9241 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9242 if (r_type
== elfcpp::R_PPC64_TOC16_HA
9243 && object
->make_toc_relative(target
, &value
))
9245 gold_assert((insn
& ((0x3f << 26) | 0x1f << 16))
9246 == ((15u << 26) | (2 << 16)));
9248 if (((insn
& ((0x3f << 26) | 0x1f << 16))
9249 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
9250 && value
+ 0x8000 < 0x10000)
9252 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
9258 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
9259 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
9260 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
9261 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
9262 case elfcpp::R_POWERPC_GOT16_LO
:
9263 case elfcpp::R_PPC64_GOT16_LO_DS
:
9264 case elfcpp::R_PPC64_TOC16_LO
:
9265 case elfcpp::R_PPC64_TOC16_LO_DS
:
9266 if (parameters
->options().toc_optimize())
9268 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9269 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9270 bool changed
= false;
9271 if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
9272 && object
->make_toc_relative(target
, &value
))
9274 gold_assert ((insn
& (0x3f << 26)) == 58u << 26 /* ld */);
9275 insn
^= (14u << 26) ^ (58u << 26);
9276 r_type
= elfcpp::R_PPC64_TOC16_LO
;
9279 if (ok_lo_toc_insn(insn
, r_type
)
9280 && value
+ 0x8000 < 0x10000)
9282 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
9284 // Transform addic to addi when we change reg.
9285 insn
&= ~((0x3f << 26) | (0x1f << 16));
9286 insn
|= (14u << 26) | (2 << 16);
9290 insn
&= ~(0x1f << 16);
9296 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9300 case elfcpp::R_POWERPC_TPREL16_HA
:
9301 if (parameters
->options().tls_optimize() && value
+ 0x8000 < 0x10000)
9303 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9304 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9305 if ((insn
& ((0x3f << 26) | 0x1f << 16))
9306 != ((15u << 26) | ((size
== 32 ? 2 : 13) << 16)))
9310 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
9316 case elfcpp::R_PPC64_TPREL16_LO_DS
:
9318 // R_PPC_TLSGD, R_PPC_TLSLD
9321 case elfcpp::R_POWERPC_TPREL16_LO
:
9322 if (parameters
->options().tls_optimize() && value
+ 0x8000 < 0x10000)
9324 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9325 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9326 insn
&= ~(0x1f << 16);
9327 insn
|= (size
== 32 ? 2 : 13) << 16;
9328 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9332 case elfcpp::R_PPC64_ENTRY
:
9333 value
= (target
->got_section()->output_section()->address()
9334 + object
->toc_base_offset());
9335 if (value
+ 0x80008000 <= 0xffffffff
9336 && !parameters
->options().output_is_position_independent())
9338 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9339 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9340 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
9342 if ((insn1
& ~0xfffc) == ld_2_12
9343 && insn2
== add_2_2_12
)
9345 insn1
= lis_2
+ ha(value
);
9346 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
9347 insn2
= addi_2_2
+ l(value
);
9348 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
9355 if (value
+ 0x80008000 <= 0xffffffff)
9357 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9358 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9359 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
9361 if ((insn1
& ~0xfffc) == ld_2_12
9362 && insn2
== add_2_2_12
)
9364 insn1
= addis_2_12
+ ha(value
);
9365 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
9366 insn2
= addi_2_2
+ l(value
);
9367 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
9374 case elfcpp::R_POWERPC_REL16_LO
:
9375 // If we are generating a non-PIC executable, edit
9376 // 0: addis 2,12,.TOC.-0b@ha
9377 // addi 2,2,.TOC.-0b@l
9378 // used by ELFv2 global entry points to set up r2, to
9381 // if .TOC. is in range. */
9382 if (value
+ address
- 4 + 0x80008000 <= 0xffffffff
9385 && target
->abiversion() >= 2
9386 && !parameters
->options().output_is_position_independent()
9387 && rela
.get_r_addend() == d_offset
+ 4
9389 && strcmp(gsym
->name(), ".TOC.") == 0)
9391 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
9392 Reltype
prev_rela(preloc
- reloc_size
);
9393 if ((prev_rela
.get_r_info()
9394 == elfcpp::elf_r_info
<size
>(r_sym
,
9395 elfcpp::R_POWERPC_REL16_HA
))
9396 && prev_rela
.get_r_offset() + 4 == rela
.get_r_offset()
9397 && prev_rela
.get_r_addend() + 4 == rela
.get_r_addend())
9399 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9400 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
- 1);
9401 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9403 if ((insn1
& 0xffff0000) == addis_2_12
9404 && (insn2
& 0xffff0000) == addi_2_2
)
9406 insn1
= lis_2
+ ha(value
+ address
- 4);
9407 elfcpp::Swap
<32, big_endian
>::writeval(iview
- 1, insn1
);
9408 insn2
= addi_2_2
+ l(value
+ address
- 4);
9409 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn2
);
9412 relinfo
->rr
->set_strategy(relnum
- 1,
9413 Relocatable_relocs::RELOC_SPECIAL
);
9414 relinfo
->rr
->set_strategy(relnum
,
9415 Relocatable_relocs::RELOC_SPECIAL
);
9425 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
9426 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->data_shdr
);
9429 case elfcpp::R_POWERPC_ADDR32
:
9430 case elfcpp::R_POWERPC_UADDR32
:
9432 overflow
= Reloc::CHECK_BITFIELD
;
9435 case elfcpp::R_POWERPC_REL32
:
9436 case elfcpp::R_POWERPC_REL16DX_HA
:
9438 overflow
= Reloc::CHECK_SIGNED
;
9441 case elfcpp::R_POWERPC_UADDR16
:
9442 overflow
= Reloc::CHECK_BITFIELD
;
9445 case elfcpp::R_POWERPC_ADDR16
:
9446 // We really should have three separate relocations,
9447 // one for 16-bit data, one for insns with 16-bit signed fields,
9448 // and one for insns with 16-bit unsigned fields.
9449 overflow
= Reloc::CHECK_BITFIELD
;
9450 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
9451 overflow
= Reloc::CHECK_LOW_INSN
;
9454 case elfcpp::R_POWERPC_ADDR16_HI
:
9455 case elfcpp::R_POWERPC_ADDR16_HA
:
9456 case elfcpp::R_POWERPC_GOT16_HI
:
9457 case elfcpp::R_POWERPC_GOT16_HA
:
9458 case elfcpp::R_POWERPC_PLT16_HI
:
9459 case elfcpp::R_POWERPC_PLT16_HA
:
9460 case elfcpp::R_POWERPC_SECTOFF_HI
:
9461 case elfcpp::R_POWERPC_SECTOFF_HA
:
9462 case elfcpp::R_PPC64_TOC16_HI
:
9463 case elfcpp::R_PPC64_TOC16_HA
:
9464 case elfcpp::R_PPC64_PLTGOT16_HI
:
9465 case elfcpp::R_PPC64_PLTGOT16_HA
:
9466 case elfcpp::R_POWERPC_TPREL16_HI
:
9467 case elfcpp::R_POWERPC_TPREL16_HA
:
9468 case elfcpp::R_POWERPC_DTPREL16_HI
:
9469 case elfcpp::R_POWERPC_DTPREL16_HA
:
9470 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
9471 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
9472 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
9473 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
9474 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
9475 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
9476 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
9477 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
9478 case elfcpp::R_POWERPC_REL16_HI
:
9479 case elfcpp::R_POWERPC_REL16_HA
:
9481 overflow
= Reloc::CHECK_HIGH_INSN
;
9484 case elfcpp::R_POWERPC_REL16
:
9485 case elfcpp::R_PPC64_TOC16
:
9486 case elfcpp::R_POWERPC_GOT16
:
9487 case elfcpp::R_POWERPC_SECTOFF
:
9488 case elfcpp::R_POWERPC_TPREL16
:
9489 case elfcpp::R_POWERPC_DTPREL16
:
9490 case elfcpp::R_POWERPC_GOT_TLSGD16
:
9491 case elfcpp::R_POWERPC_GOT_TLSLD16
:
9492 case elfcpp::R_POWERPC_GOT_TPREL16
:
9493 case elfcpp::R_POWERPC_GOT_DTPREL16
:
9494 overflow
= Reloc::CHECK_LOW_INSN
;
9497 case elfcpp::R_POWERPC_ADDR24
:
9498 case elfcpp::R_POWERPC_ADDR14
:
9499 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
9500 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
9501 case elfcpp::R_PPC64_ADDR16_DS
:
9502 case elfcpp::R_POWERPC_REL24
:
9503 case elfcpp::R_PPC_PLTREL24
:
9504 case elfcpp::R_PPC_LOCAL24PC
:
9505 case elfcpp::R_PPC64_TPREL16_DS
:
9506 case elfcpp::R_PPC64_DTPREL16_DS
:
9507 case elfcpp::R_PPC64_TOC16_DS
:
9508 case elfcpp::R_PPC64_GOT16_DS
:
9509 case elfcpp::R_PPC64_SECTOFF_DS
:
9510 case elfcpp::R_POWERPC_REL14
:
9511 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
9512 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
9513 overflow
= Reloc::CHECK_SIGNED
;
9517 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9520 if (overflow
== Reloc::CHECK_LOW_INSN
9521 || overflow
== Reloc::CHECK_HIGH_INSN
)
9523 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9525 if ((insn
& (0x3f << 26)) == 10u << 26 /* cmpli */)
9526 overflow
= Reloc::CHECK_BITFIELD
;
9527 else if (overflow
== Reloc::CHECK_LOW_INSN
9528 ? ((insn
& (0x3f << 26)) == 28u << 26 /* andi */
9529 || (insn
& (0x3f << 26)) == 24u << 26 /* ori */
9530 || (insn
& (0x3f << 26)) == 26u << 26 /* xori */)
9531 : ((insn
& (0x3f << 26)) == 29u << 26 /* andis */
9532 || (insn
& (0x3f << 26)) == 25u << 26 /* oris */
9533 || (insn
& (0x3f << 26)) == 27u << 26 /* xoris */))
9534 overflow
= Reloc::CHECK_UNSIGNED
;
9536 overflow
= Reloc::CHECK_SIGNED
;
9539 bool maybe_dq_reloc
= false;
9540 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
9541 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
9544 case elfcpp::R_POWERPC_NONE
:
9545 case elfcpp::R_POWERPC_TLS
:
9546 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
9547 case elfcpp::R_POWERPC_GNU_VTENTRY
:
9548 case elfcpp::R_POWERPC_PLTSEQ
:
9549 case elfcpp::R_POWERPC_PLTCALL
:
9552 case elfcpp::R_PPC64_ADDR64
:
9553 case elfcpp::R_PPC64_REL64
:
9554 case elfcpp::R_PPC64_TOC
:
9555 case elfcpp::R_PPC64_ADDR64_LOCAL
:
9556 Reloc::addr64(view
, value
);
9559 case elfcpp::R_POWERPC_TPREL
:
9560 case elfcpp::R_POWERPC_DTPREL
:
9562 Reloc::addr64(view
, value
);
9564 status
= Reloc::addr32(view
, value
, overflow
);
9567 case elfcpp::R_PPC64_UADDR64
:
9568 Reloc::addr64_u(view
, value
);
9571 case elfcpp::R_POWERPC_ADDR32
:
9572 status
= Reloc::addr32(view
, value
, overflow
);
9575 case elfcpp::R_POWERPC_REL32
:
9576 case elfcpp::R_POWERPC_UADDR32
:
9577 status
= Reloc::addr32_u(view
, value
, overflow
);
9580 case elfcpp::R_POWERPC_ADDR24
:
9581 case elfcpp::R_POWERPC_REL24
:
9582 case elfcpp::R_PPC_PLTREL24
:
9583 case elfcpp::R_PPC_LOCAL24PC
:
9584 status
= Reloc::addr24(view
, value
, overflow
);
9587 case elfcpp::R_POWERPC_GOT_DTPREL16
:
9588 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
9589 case elfcpp::R_POWERPC_GOT_TPREL16
:
9590 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
9593 // On ppc64 these are all ds form
9594 maybe_dq_reloc
= true;
9598 case elfcpp::R_POWERPC_ADDR16
:
9599 case elfcpp::R_POWERPC_REL16
:
9600 case elfcpp::R_PPC64_TOC16
:
9601 case elfcpp::R_POWERPC_GOT16
:
9602 case elfcpp::R_POWERPC_SECTOFF
:
9603 case elfcpp::R_POWERPC_TPREL16
:
9604 case elfcpp::R_POWERPC_DTPREL16
:
9605 case elfcpp::R_POWERPC_GOT_TLSGD16
:
9606 case elfcpp::R_POWERPC_GOT_TLSLD16
:
9607 case elfcpp::R_POWERPC_ADDR16_LO
:
9608 case elfcpp::R_POWERPC_REL16_LO
:
9609 case elfcpp::R_PPC64_TOC16_LO
:
9610 case elfcpp::R_POWERPC_GOT16_LO
:
9611 case elfcpp::R_POWERPC_PLT16_LO
:
9612 case elfcpp::R_POWERPC_SECTOFF_LO
:
9613 case elfcpp::R_POWERPC_TPREL16_LO
:
9614 case elfcpp::R_POWERPC_DTPREL16_LO
:
9615 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
9616 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
9618 status
= Reloc::addr16(view
, value
, overflow
);
9620 maybe_dq_reloc
= true;
9623 case elfcpp::R_POWERPC_UADDR16
:
9624 status
= Reloc::addr16_u(view
, value
, overflow
);
9627 case elfcpp::R_PPC64_ADDR16_HIGH
:
9628 case elfcpp::R_PPC64_TPREL16_HIGH
:
9629 case elfcpp::R_PPC64_DTPREL16_HIGH
:
9631 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
9634 case elfcpp::R_POWERPC_ADDR16_HI
:
9635 case elfcpp::R_POWERPC_REL16_HI
:
9636 case elfcpp::R_PPC64_TOC16_HI
:
9637 case elfcpp::R_POWERPC_GOT16_HI
:
9638 case elfcpp::R_POWERPC_PLT16_HI
:
9639 case elfcpp::R_POWERPC_SECTOFF_HI
:
9640 case elfcpp::R_POWERPC_TPREL16_HI
:
9641 case elfcpp::R_POWERPC_DTPREL16_HI
:
9642 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
9643 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
9644 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
9645 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
9646 Reloc::addr16_hi(view
, value
);
9649 case elfcpp::R_PPC64_ADDR16_HIGHA
:
9650 case elfcpp::R_PPC64_TPREL16_HIGHA
:
9651 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
9653 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
9656 case elfcpp::R_POWERPC_ADDR16_HA
:
9657 case elfcpp::R_POWERPC_REL16_HA
:
9658 case elfcpp::R_PPC64_TOC16_HA
:
9659 case elfcpp::R_POWERPC_GOT16_HA
:
9660 case elfcpp::R_POWERPC_PLT16_HA
:
9661 case elfcpp::R_POWERPC_SECTOFF_HA
:
9662 case elfcpp::R_POWERPC_TPREL16_HA
:
9663 case elfcpp::R_POWERPC_DTPREL16_HA
:
9664 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
9665 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
9666 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
9667 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
9668 Reloc::addr16_ha(view
, value
);
9671 case elfcpp::R_POWERPC_REL16DX_HA
:
9672 status
= Reloc::addr16dx_ha(view
, value
, overflow
);
9675 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
9677 // R_PPC_EMB_NADDR16_LO
9680 case elfcpp::R_PPC64_ADDR16_HIGHER
:
9681 case elfcpp::R_PPC64_TPREL16_HIGHER
:
9682 Reloc::addr16_hi2(view
, value
);
9685 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
9687 // R_PPC_EMB_NADDR16_HI
9690 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
9691 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
9692 Reloc::addr16_ha2(view
, value
);
9695 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
9697 // R_PPC_EMB_NADDR16_HA
9700 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
9701 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
9702 Reloc::addr16_hi3(view
, value
);
9705 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
9710 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
9711 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
9712 Reloc::addr16_ha3(view
, value
);
9715 case elfcpp::R_PPC64_DTPREL16_DS
:
9716 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
9718 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
9721 case elfcpp::R_PPC64_TPREL16_DS
:
9722 case elfcpp::R_PPC64_TPREL16_LO_DS
:
9724 // R_PPC_TLSGD, R_PPC_TLSLD
9727 case elfcpp::R_PPC64_ADDR16_DS
:
9728 case elfcpp::R_PPC64_ADDR16_LO_DS
:
9729 case elfcpp::R_PPC64_TOC16_DS
:
9730 case elfcpp::R_PPC64_TOC16_LO_DS
:
9731 case elfcpp::R_PPC64_GOT16_DS
:
9732 case elfcpp::R_PPC64_GOT16_LO_DS
:
9733 case elfcpp::R_PPC64_PLT16_LO_DS
:
9734 case elfcpp::R_PPC64_SECTOFF_DS
:
9735 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
9736 maybe_dq_reloc
= true;
9739 case elfcpp::R_POWERPC_ADDR14
:
9740 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
9741 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
9742 case elfcpp::R_POWERPC_REL14
:
9743 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
9744 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
9745 status
= Reloc::addr14(view
, value
, overflow
);
9748 case elfcpp::R_POWERPC_COPY
:
9749 case elfcpp::R_POWERPC_GLOB_DAT
:
9750 case elfcpp::R_POWERPC_JMP_SLOT
:
9751 case elfcpp::R_POWERPC_RELATIVE
:
9752 case elfcpp::R_POWERPC_DTPMOD
:
9753 case elfcpp::R_PPC64_JMP_IREL
:
9754 case elfcpp::R_POWERPC_IRELATIVE
:
9755 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
9756 _("unexpected reloc %u in object file"),
9760 case elfcpp::R_PPC64_TOCSAVE
:
9766 Symbol_location loc
;
9767 loc
.object
= relinfo
->object
;
9768 loc
.shndx
= relinfo
->data_shndx
;
9769 loc
.offset
= rela
.get_r_offset();
9770 Tocsave_loc::const_iterator p
= target
->tocsave_loc().find(loc
);
9771 if (p
!= target
->tocsave_loc().end())
9773 // If we've generated plt calls using this tocsave, then
9774 // the nop needs to be changed to save r2.
9775 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9776 if (elfcpp::Swap
<32, big_endian
>::readval(iview
) == nop
)
9777 elfcpp::Swap
<32, big_endian
>::
9778 writeval(iview
, std_2_1
+ target
->stk_toc());
9783 case elfcpp::R_PPC_EMB_SDA2I16
:
9784 case elfcpp::R_PPC_EMB_SDA2REL
:
9787 // R_PPC64_TLSGD, R_PPC64_TLSLD
9790 case elfcpp::R_POWERPC_PLT32
:
9791 case elfcpp::R_POWERPC_PLTREL32
:
9792 case elfcpp::R_PPC_SDAREL16
:
9793 case elfcpp::R_POWERPC_ADDR30
:
9794 case elfcpp::R_PPC64_PLT64
:
9795 case elfcpp::R_PPC64_PLTREL64
:
9796 case elfcpp::R_PPC64_PLTGOT16
:
9797 case elfcpp::R_PPC64_PLTGOT16_LO
:
9798 case elfcpp::R_PPC64_PLTGOT16_HI
:
9799 case elfcpp::R_PPC64_PLTGOT16_HA
:
9800 case elfcpp::R_PPC64_PLTGOT16_DS
:
9801 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
9802 case elfcpp::R_PPC_EMB_RELSDA
:
9803 case elfcpp::R_PPC_TOC16
:
9806 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
9807 _("unsupported reloc %u"),
9815 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9817 if ((insn
& (0x3f << 26)) == 56u << 26 /* lq */
9818 || ((insn
& (0x3f << 26)) == (61u << 26) /* lxv, stxv */
9819 && (insn
& 3) == 1))
9820 status
= Reloc::addr16_dq(view
, value
, overflow
);
9822 || (insn
& (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
9823 || (insn
& (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
9824 || (insn
& (0x3f << 26)) == 57u << 26 /* lfdp */
9825 || (insn
& (0x3f << 26)) == 61u << 26 /* stfdp */)
9826 status
= Reloc::addr16_ds(view
, value
, overflow
);
9828 status
= Reloc::addr16(view
, value
, overflow
);
9831 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
9834 && gsym
->is_undefined()
9835 && is_branch_reloc(r_type
))))
9837 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
9838 _("relocation overflow"));
9840 gold_info(_("try relinking with a smaller --stub-group-size"));
9846 // Relocate section data.
9848 template<int size
, bool big_endian
>
9850 Target_powerpc
<size
, big_endian
>::relocate_section(
9851 const Relocate_info
<size
, big_endian
>* relinfo
,
9852 unsigned int sh_type
,
9853 const unsigned char* prelocs
,
9855 Output_section
* output_section
,
9856 bool needs_special_offset_handling
,
9857 unsigned char* view
,
9859 section_size_type view_size
,
9860 const Reloc_symbol_changes
* reloc_symbol_changes
)
9862 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
9863 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
9864 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
9865 Powerpc_comdat_behavior
;
9866 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
9869 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9871 gold::relocate_section
<size
, big_endian
, Powerpc
, Powerpc_relocate
,
9872 Powerpc_comdat_behavior
, Classify_reloc
>(
9878 needs_special_offset_handling
,
9882 reloc_symbol_changes
);
9885 template<int size
, bool big_endian
>
9886 class Powerpc_scan_relocatable_reloc
9889 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
9890 static const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
9891 static const int sh_type
= elfcpp::SHT_RELA
;
9893 // Return the symbol referred to by the relocation.
9894 static inline unsigned int
9895 get_r_sym(const Reltype
* reloc
)
9896 { return elfcpp::elf_r_sym
<size
>(reloc
->get_r_info()); }
9898 // Return the type of the relocation.
9899 static inline unsigned int
9900 get_r_type(const Reltype
* reloc
)
9901 { return elfcpp::elf_r_type
<size
>(reloc
->get_r_info()); }
9903 // Return the strategy to use for a local symbol which is not a
9904 // section symbol, given the relocation type.
9905 inline Relocatable_relocs::Reloc_strategy
9906 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
9908 if (r_type
== 0 && r_sym
== 0)
9909 return Relocatable_relocs::RELOC_DISCARD
;
9910 return Relocatable_relocs::RELOC_COPY
;
9913 // Return the strategy to use for a local symbol which is a section
9914 // symbol, given the relocation type.
9915 inline Relocatable_relocs::Reloc_strategy
9916 local_section_strategy(unsigned int, Relobj
*)
9918 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
9921 // Return the strategy to use for a global symbol, given the
9922 // relocation type, the object, and the symbol index.
9923 inline Relocatable_relocs::Reloc_strategy
9924 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
9927 && (r_type
== elfcpp::R_PPC_PLTREL24
9928 || r_type
== elfcpp::R_POWERPC_PLT16_LO
9929 || r_type
== elfcpp::R_POWERPC_PLT16_HI
9930 || r_type
== elfcpp::R_POWERPC_PLT16_HA
))
9931 return Relocatable_relocs::RELOC_SPECIAL
;
9932 return Relocatable_relocs::RELOC_COPY
;
9936 // Scan the relocs during a relocatable link.
9938 template<int size
, bool big_endian
>
9940 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
9941 Symbol_table
* symtab
,
9943 Sized_relobj_file
<size
, big_endian
>* object
,
9944 unsigned int data_shndx
,
9945 unsigned int sh_type
,
9946 const unsigned char* prelocs
,
9948 Output_section
* output_section
,
9949 bool needs_special_offset_handling
,
9950 size_t local_symbol_count
,
9951 const unsigned char* plocal_symbols
,
9952 Relocatable_relocs
* rr
)
9954 typedef Powerpc_scan_relocatable_reloc
<size
, big_endian
> Scan_strategy
;
9956 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9958 gold::scan_relocatable_relocs
<size
, big_endian
, Scan_strategy
>(
9966 needs_special_offset_handling
,
9972 // Scan the relocs for --emit-relocs.
9974 template<int size
, bool big_endian
>
9976 Target_powerpc
<size
, big_endian
>::emit_relocs_scan(
9977 Symbol_table
* symtab
,
9979 Sized_relobj_file
<size
, big_endian
>* object
,
9980 unsigned int data_shndx
,
9981 unsigned int sh_type
,
9982 const unsigned char* prelocs
,
9984 Output_section
* output_section
,
9985 bool needs_special_offset_handling
,
9986 size_t local_symbol_count
,
9987 const unsigned char* plocal_syms
,
9988 Relocatable_relocs
* rr
)
9990 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
9992 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
9993 Emit_relocs_strategy
;
9995 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9997 gold::scan_relocatable_relocs
<size
, big_endian
, Emit_relocs_strategy
>(
10005 needs_special_offset_handling
,
10006 local_symbol_count
,
10011 // Emit relocations for a section.
10012 // This is a modified version of the function by the same name in
10013 // target-reloc.h. Using relocate_special_relocatable for
10014 // R_PPC_PLTREL24 would require duplication of the entire body of the
10015 // loop, so we may as well duplicate the whole thing.
10017 template<int size
, bool big_endian
>
10019 Target_powerpc
<size
, big_endian
>::relocate_relocs(
10020 const Relocate_info
<size
, big_endian
>* relinfo
,
10021 unsigned int sh_type
,
10022 const unsigned char* prelocs
,
10023 size_t reloc_count
,
10024 Output_section
* output_section
,
10025 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
10027 Address view_address
,
10029 unsigned char* reloc_view
,
10030 section_size_type reloc_view_size
)
10032 gold_assert(sh_type
== elfcpp::SHT_RELA
);
10034 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
10035 typedef typename
elfcpp::Rela_write
<size
, big_endian
> Reltype_write
;
10036 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
10037 // Offset from start of insn to d-field reloc.
10038 const int d_offset
= big_endian
? 2 : 0;
10040 Powerpc_relobj
<size
, big_endian
>* const object
10041 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
10042 const unsigned int local_count
= object
->local_symbol_count();
10043 unsigned int got2_shndx
= object
->got2_shndx();
10044 Address got2_addend
= 0;
10045 if (got2_shndx
!= 0)
10047 got2_addend
= object
->get_output_section_offset(got2_shndx
);
10048 gold_assert(got2_addend
!= invalid_address
);
10051 const bool relocatable
= parameters
->options().relocatable();
10053 unsigned char* pwrite
= reloc_view
;
10054 bool zap_next
= false;
10055 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
10057 Relocatable_relocs::Reloc_strategy strategy
= relinfo
->rr
->strategy(i
);
10058 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
10061 Reltype
reloc(prelocs
);
10062 Reltype_write
reloc_write(pwrite
);
10064 Address offset
= reloc
.get_r_offset();
10065 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
10066 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
10067 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
10068 const unsigned int orig_r_sym
= r_sym
;
10069 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
10070 = reloc
.get_r_addend();
10071 const Symbol
* gsym
= NULL
;
10075 // We could arrange to discard these and other relocs for
10076 // tls optimised sequences in the strategy methods, but for
10077 // now do as BFD ld does.
10078 r_type
= elfcpp::R_POWERPC_NONE
;
10082 // Get the new symbol index.
10083 Output_section
* os
= NULL
;
10084 if (r_sym
< local_count
)
10088 case Relocatable_relocs::RELOC_COPY
:
10089 case Relocatable_relocs::RELOC_SPECIAL
:
10092 r_sym
= object
->symtab_index(r_sym
);
10093 gold_assert(r_sym
!= -1U);
10097 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
10099 // We are adjusting a section symbol. We need to find
10100 // the symbol table index of the section symbol for
10101 // the output section corresponding to input section
10102 // in which this symbol is defined.
10103 gold_assert(r_sym
< local_count
);
10105 unsigned int shndx
=
10106 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
10107 gold_assert(is_ordinary
);
10108 os
= object
->output_section(shndx
);
10109 gold_assert(os
!= NULL
);
10110 gold_assert(os
->needs_symtab_index());
10111 r_sym
= os
->symtab_index();
10116 gold_unreachable();
10121 gsym
= object
->global_symbol(r_sym
);
10122 gold_assert(gsym
!= NULL
);
10123 if (gsym
->is_forwarder())
10124 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
10126 gold_assert(gsym
->has_symtab_index());
10127 r_sym
= gsym
->symtab_index();
10130 // Get the new offset--the location in the output section where
10131 // this relocation should be applied.
10132 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
10133 offset
+= offset_in_output_section
;
10136 section_offset_type sot_offset
=
10137 convert_types
<section_offset_type
, Address
>(offset
);
10138 section_offset_type new_sot_offset
=
10139 output_section
->output_offset(object
, relinfo
->data_shndx
,
10141 gold_assert(new_sot_offset
!= -1);
10142 offset
= new_sot_offset
;
10145 // In an object file, r_offset is an offset within the section.
10146 // In an executable or dynamic object, generated by
10147 // --emit-relocs, r_offset is an absolute address.
10150 offset
+= view_address
;
10151 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
10152 offset
-= offset_in_output_section
;
10155 // Handle the reloc addend based on the strategy.
10156 if (strategy
== Relocatable_relocs::RELOC_COPY
)
10158 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
10160 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
10161 addend
= psymval
->value(object
, addend
);
10162 // In a relocatable link, the symbol value is relative to
10163 // the start of the output section. For a non-relocatable
10164 // link, we need to adjust the addend.
10167 gold_assert(os
!= NULL
);
10168 addend
-= os
->address();
10171 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
10175 if (addend
>= 32768)
10176 addend
+= got2_addend
;
10178 else if (r_type
== elfcpp::R_POWERPC_REL16_HA
)
10180 r_type
= elfcpp::R_POWERPC_ADDR16_HA
;
10181 addend
-= d_offset
;
10183 else if (r_type
== elfcpp::R_POWERPC_REL16_LO
)
10185 r_type
= elfcpp::R_POWERPC_ADDR16_LO
;
10186 addend
-= d_offset
+ 4;
10190 gold_unreachable();
10194 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
10195 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
10196 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
10197 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
10199 // First instruction of a global dynamic sequence,
10201 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
10202 switch (this->optimize_tls_gd(final
))
10204 case tls::TLSOPT_TO_IE
:
10205 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
10206 - elfcpp::R_POWERPC_GOT_TLSGD16
);
10208 case tls::TLSOPT_TO_LE
:
10209 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
10210 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
10211 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
10214 r_type
= elfcpp::R_POWERPC_NONE
;
10215 offset
-= d_offset
;
10222 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
10223 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
10224 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
10225 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
10227 // First instruction of a local dynamic sequence,
10229 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
10231 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
10232 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
10234 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
10235 const Output_section
* os
= relinfo
->layout
->tls_segment()
10237 gold_assert(os
!= NULL
);
10238 gold_assert(os
->needs_symtab_index());
10239 r_sym
= os
->symtab_index();
10240 addend
= dtp_offset
;
10244 r_type
= elfcpp::R_POWERPC_NONE
;
10245 offset
-= d_offset
;
10249 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
10250 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
10251 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
10252 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
10254 // First instruction of initial exec sequence.
10255 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
10256 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
10258 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
10259 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
10260 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
10263 r_type
= elfcpp::R_POWERPC_NONE
;
10264 offset
-= d_offset
;
10268 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
10269 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
10271 // Second instruction of a global dynamic sequence,
10272 // the __tls_get_addr call
10273 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
10274 switch (this->optimize_tls_gd(final
))
10276 case tls::TLSOPT_TO_IE
:
10277 r_type
= elfcpp::R_POWERPC_NONE
;
10280 case tls::TLSOPT_TO_LE
:
10281 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
10282 offset
+= d_offset
;
10289 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
10290 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
10292 // Second instruction of a local dynamic sequence,
10293 // the __tls_get_addr call
10294 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
10296 const Output_section
* os
= relinfo
->layout
->tls_segment()
10298 gold_assert(os
!= NULL
);
10299 gold_assert(os
->needs_symtab_index());
10300 r_sym
= os
->symtab_index();
10301 addend
= dtp_offset
;
10302 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
10303 offset
+= d_offset
;
10307 else if (r_type
== elfcpp::R_POWERPC_TLS
)
10309 // Second instruction of an initial exec sequence
10310 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
10311 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
10313 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
10314 offset
+= d_offset
;
10319 reloc_write
.put_r_offset(offset
);
10320 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
10321 reloc_write
.put_r_addend(addend
);
10323 pwrite
+= reloc_size
;
10326 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
10327 == reloc_view_size
);
10330 // Return the value to use for a dynamic symbol which requires special
10331 // treatment. This is how we support equality comparisons of function
10332 // pointers across shared library boundaries, as described in the
10333 // processor specific ABI supplement.
10335 template<int size
, bool big_endian
>
10337 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
10341 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
10342 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
10343 p
!= this->stub_tables_
.end();
10346 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
10347 = (*p
)->find_plt_call_entry(gsym
);
10349 return (*p
)->stub_address() + ent
->off_
;
10352 else if (this->abiversion() >= 2)
10354 Address off
= this->glink_section()->find_global_entry(gsym
);
10355 if (off
!= invalid_address
)
10356 return this->glink_section()->global_entry_address() + off
;
10358 gold_unreachable();
10361 // Return the PLT address to use for a local symbol.
10362 template<int size
, bool big_endian
>
10364 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
10365 const Relobj
* object
,
10366 unsigned int symndx
) const
10370 const Sized_relobj
<size
, big_endian
>* relobj
10371 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
10372 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
10373 p
!= this->stub_tables_
.end();
10376 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
10377 = (*p
)->find_plt_call_entry(relobj
->sized_relobj(), symndx
);
10379 return (*p
)->stub_address() + ent
->off_
;
10382 gold_unreachable();
10385 // Return the PLT address to use for a global symbol.
10386 template<int size
, bool big_endian
>
10388 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
10389 const Symbol
* gsym
) const
10393 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
10394 p
!= this->stub_tables_
.end();
10397 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
10398 = (*p
)->find_plt_call_entry(gsym
);
10400 return (*p
)->stub_address() + ent
->off_
;
10403 else if (this->abiversion() >= 2)
10405 Address off
= this->glink_section()->find_global_entry(gsym
);
10406 if (off
!= invalid_address
)
10407 return this->glink_section()->global_entry_address() + off
;
10409 gold_unreachable();
10412 // Return the offset to use for the GOT_INDX'th got entry which is
10413 // for a local tls symbol specified by OBJECT, SYMNDX.
10414 template<int size
, bool big_endian
>
10416 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
10417 const Relobj
* object
,
10418 unsigned int symndx
,
10419 unsigned int got_indx
) const
10421 const Powerpc_relobj
<size
, big_endian
>* ppc_object
10422 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
10423 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
10425 for (Got_type got_type
= GOT_TYPE_TLSGD
;
10426 got_type
<= GOT_TYPE_TPREL
;
10427 got_type
= Got_type(got_type
+ 1))
10428 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
10430 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
10431 if (got_type
== GOT_TYPE_TLSGD
)
10433 if (off
== got_indx
* (size
/ 8))
10435 if (got_type
== GOT_TYPE_TPREL
)
10438 return -dtp_offset
;
10442 gold_unreachable();
10445 // Return the offset to use for the GOT_INDX'th got entry which is
10446 // for global tls symbol GSYM.
10447 template<int size
, bool big_endian
>
10449 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
10451 unsigned int got_indx
) const
10453 if (gsym
->type() == elfcpp::STT_TLS
)
10455 for (Got_type got_type
= GOT_TYPE_TLSGD
;
10456 got_type
<= GOT_TYPE_TPREL
;
10457 got_type
= Got_type(got_type
+ 1))
10458 if (gsym
->has_got_offset(got_type
))
10460 unsigned int off
= gsym
->got_offset(got_type
);
10461 if (got_type
== GOT_TYPE_TLSGD
)
10463 if (off
== got_indx
* (size
/ 8))
10465 if (got_type
== GOT_TYPE_TPREL
)
10468 return -dtp_offset
;
10472 gold_unreachable();
10475 // The selector for powerpc object files.
10477 template<int size
, bool big_endian
>
10478 class Target_selector_powerpc
: public Target_selector
10481 Target_selector_powerpc()
10482 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
10485 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
10486 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
10488 ? (big_endian
? "elf64ppc" : "elf64lppc")
10489 : (big_endian
? "elf32ppc" : "elf32lppc")))
10493 do_instantiate_target()
10494 { return new Target_powerpc
<size
, big_endian
>(); }
10497 Target_selector_powerpc
<32, true> target_selector_ppc32
;
10498 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
10499 Target_selector_powerpc
<64, true> target_selector_ppc64
;
10500 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
10502 // Instantiate these constants for -O0
10503 template<int size
, bool big_endian
>
10504 const typename Output_data_glink
<size
, big_endian
>::Address
10505 Output_data_glink
<size
, big_endian
>::invalid_address
;
10506 template<int size
, bool big_endian
>
10507 const typename Stub_table
<size
, big_endian
>::Address
10508 Stub_table
<size
, big_endian
>::invalid_address
;
10509 template<int size
, bool big_endian
>
10510 const typename Target_powerpc
<size
, big_endian
>::Address
10511 Target_powerpc
<size
, big_endian
>::invalid_address
;
10513 } // End anonymous namespace.