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
;
82 is_branch_reloc(unsigned int r_type
);
84 // Counter incremented on every Powerpc_relobj constructed.
85 static uint32_t object_id
= 0;
87 template<int size
, bool big_endian
>
88 class Powerpc_relobj
: public Sized_relobj_file
<size
, big_endian
>
91 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
92 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_refs
;
93 typedef Unordered_map
<Address
, Section_refs
> Access_from
;
95 Powerpc_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
96 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
97 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
98 uniq_(object_id
++), special_(0), relatoc_(0), toc_(0),
99 has_small_toc_reloc_(false), opd_valid_(false),
100 e_flags_(ehdr
.get_e_flags()), no_toc_opt_(), opd_ent_(),
101 access_from_map_(), has14_(), stub_table_index_(), st_other_()
103 this->set_abiversion(0);
109 // Read the symbols then set up st_other vector.
111 do_read_symbols(Read_symbols_data
*);
113 // Arrange to always relocate .toc first.
115 do_relocate_sections(
116 const Symbol_table
* symtab
, const Layout
* layout
,
117 const unsigned char* pshdrs
, Output_file
* of
,
118 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
);
120 // The .toc section index.
127 // Mark .toc entry at OFF as not optimizable.
129 set_no_toc_opt(Address off
)
131 if (this->no_toc_opt_
.empty())
132 this->no_toc_opt_
.resize(this->section_size(this->toc_shndx())
135 if (off
< this->no_toc_opt_
.size())
136 this->no_toc_opt_
[off
] = true;
139 // Mark the entire .toc as not optimizable.
143 this->no_toc_opt_
.resize(1);
144 this->no_toc_opt_
[0] = true;
147 // Return true if code using the .toc entry at OFF should not be edited.
149 no_toc_opt(Address off
) const
151 if (this->no_toc_opt_
.empty())
154 if (off
>= this->no_toc_opt_
.size())
156 return this->no_toc_opt_
[off
];
159 // The .got2 section shndx.
164 return this->special_
;
169 // The .opd section shndx.
176 return this->special_
;
179 // Init OPD entry arrays.
181 init_opd(size_t opd_size
)
183 size_t count
= this->opd_ent_ndx(opd_size
);
184 this->opd_ent_
.resize(count
);
187 // Return section and offset of function entry for .opd + R_OFF.
189 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
191 size_t ndx
= this->opd_ent_ndx(r_off
);
192 gold_assert(ndx
< this->opd_ent_
.size());
193 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
195 *value
= this->opd_ent_
[ndx
].off
;
196 return this->opd_ent_
[ndx
].shndx
;
199 // Set section and offset of function entry for .opd + R_OFF.
201 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
203 size_t ndx
= this->opd_ent_ndx(r_off
);
204 gold_assert(ndx
< this->opd_ent_
.size());
205 this->opd_ent_
[ndx
].shndx
= shndx
;
206 this->opd_ent_
[ndx
].off
= value
;
209 // Return discard flag for .opd + R_OFF.
211 get_opd_discard(Address r_off
) const
213 size_t ndx
= this->opd_ent_ndx(r_off
);
214 gold_assert(ndx
< this->opd_ent_
.size());
215 return this->opd_ent_
[ndx
].discard
;
218 // Set discard flag for .opd + R_OFF.
220 set_opd_discard(Address r_off
)
222 size_t ndx
= this->opd_ent_ndx(r_off
);
223 gold_assert(ndx
< this->opd_ent_
.size());
224 this->opd_ent_
[ndx
].discard
= true;
229 { return this->opd_valid_
; }
233 { this->opd_valid_
= true; }
235 // Examine .rela.opd to build info about function entry points.
237 scan_opd_relocs(size_t reloc_count
,
238 const unsigned char* prelocs
,
239 const unsigned char* plocal_syms
);
241 // Returns true if a code sequence loading a TOC entry can be
242 // converted into code calculating a TOC pointer relative offset.
244 make_toc_relative(Target_powerpc
<size
, big_endian
>* target
,
247 // Perform the Sized_relobj_file method, then set up opd info from
250 do_read_relocs(Read_relocs_data
*);
253 do_find_special_sections(Read_symbols_data
* sd
);
255 // Adjust this local symbol value. Return false if the symbol
256 // should be discarded from the output file.
258 do_adjust_local_symbol(Symbol_value
<size
>* lv
) const
260 if (size
== 64 && this->opd_shndx() != 0)
263 if (lv
->input_shndx(&is_ordinary
) != this->opd_shndx())
265 if (this->get_opd_discard(lv
->input_value()))
273 { return &this->access_from_map_
; }
275 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
276 // section at DST_OFF.
278 add_reference(Relobj
* src_obj
,
279 unsigned int src_indx
,
280 typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
282 Section_id
src_id(src_obj
, src_indx
);
283 this->access_from_map_
[dst_off
].insert(src_id
);
286 // Add a reference to the code section specified by the .opd entry
289 add_gc_mark(typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
291 size_t ndx
= this->opd_ent_ndx(dst_off
);
292 if (ndx
>= this->opd_ent_
.size())
293 this->opd_ent_
.resize(ndx
+ 1);
294 this->opd_ent_
[ndx
].gc_mark
= true;
298 process_gc_mark(Symbol_table
* symtab
)
300 for (size_t i
= 0; i
< this->opd_ent_
.size(); i
++)
301 if (this->opd_ent_
[i
].gc_mark
)
303 unsigned int shndx
= this->opd_ent_
[i
].shndx
;
304 symtab
->gc()->worklist().push_back(Section_id(this, shndx
));
308 // Return offset in output GOT section that this object will use
309 // as a TOC pointer. Won't be just a constant with multi-toc support.
311 toc_base_offset() const
315 set_has_small_toc_reloc()
316 { has_small_toc_reloc_
= true; }
319 has_small_toc_reloc() const
320 { return has_small_toc_reloc_
; }
323 set_has_14bit_branch(unsigned int shndx
)
325 if (shndx
>= this->has14_
.size())
326 this->has14_
.resize(shndx
+ 1);
327 this->has14_
[shndx
] = true;
331 has_14bit_branch(unsigned int shndx
) const
332 { return shndx
< this->has14_
.size() && this->has14_
[shndx
]; }
335 set_stub_table(unsigned int shndx
, unsigned int stub_index
)
337 if (shndx
>= this->stub_table_index_
.size())
338 this->stub_table_index_
.resize(shndx
+ 1, -1);
339 this->stub_table_index_
[shndx
] = stub_index
;
342 Stub_table
<size
, big_endian
>*
343 stub_table(unsigned int shndx
)
345 if (shndx
< this->stub_table_index_
.size())
347 Target_powerpc
<size
, big_endian
>* target
348 = static_cast<Target_powerpc
<size
, big_endian
>*>(
349 parameters
->sized_target
<size
, big_endian
>());
350 unsigned int indx
= this->stub_table_index_
[shndx
];
351 if (indx
< target
->stub_tables().size())
352 return target
->stub_tables()[indx
];
360 this->stub_table_index_
.clear();
365 { return this->uniq_
; }
369 { return this->e_flags_
& elfcpp::EF_PPC64_ABI
; }
371 // Set ABI version for input and output
373 set_abiversion(int ver
);
376 st_other (unsigned int symndx
) const
378 return this->st_other_
[symndx
];
382 ppc64_local_entry_offset(const Symbol
* sym
) const
383 { return elfcpp::ppc64_decode_local_entry(sym
->nonvis() >> 3); }
386 ppc64_local_entry_offset(unsigned int symndx
) const
387 { return elfcpp::ppc64_decode_local_entry(this->st_other_
[symndx
] >> 5); }
398 // Return index into opd_ent_ array for .opd entry at OFF.
399 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
400 // apart when the language doesn't use the last 8-byte word, the
401 // environment pointer. Thus dividing the entry section offset by
402 // 16 will give an index into opd_ent_ that works for either layout
403 // of .opd. (It leaves some elements of the vector unused when .opd
404 // entries are spaced 24 bytes apart, but we don't know the spacing
405 // until relocations are processed, and in any case it is possible
406 // for an object to have some entries spaced 16 bytes apart and
407 // others 24 bytes apart.)
409 opd_ent_ndx(size_t off
) const
412 // Per object unique identifier
415 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
416 unsigned int special_
;
418 // For 64-bit the .rela.toc and .toc section shdnx.
419 unsigned int relatoc_
;
422 // For 64-bit, whether this object uses small model relocs to access
424 bool has_small_toc_reloc_
;
426 // Set at the start of gc_process_relocs, when we know opd_ent_
427 // vector is valid. The flag could be made atomic and set in
428 // do_read_relocs with memory_order_release and then tested with
429 // memory_order_acquire, potentially resulting in fewer entries in
434 elfcpp::Elf_Word e_flags_
;
436 // For 64-bit, an array with one entry per 64-bit word in the .toc
437 // section, set if accesses using that word cannot be optimised.
438 std::vector
<bool> no_toc_opt_
;
440 // The first 8-byte word of an OPD entry gives the address of the
441 // entry point of the function. Relocatable object files have a
442 // relocation on this word. The following vector records the
443 // section and offset specified by these relocations.
444 std::vector
<Opd_ent
> opd_ent_
;
446 // References made to this object's .opd section when running
447 // gc_process_relocs for another object, before the opd_ent_ vector
448 // is valid for this object.
449 Access_from access_from_map_
;
451 // Whether input section has a 14-bit branch reloc.
452 std::vector
<bool> has14_
;
454 // The stub table to use for a given input section.
455 std::vector
<unsigned int> stub_table_index_
;
457 // ELF st_other field for local symbols.
458 std::vector
<unsigned char> st_other_
;
461 template<int size
, bool big_endian
>
462 class Powerpc_dynobj
: public Sized_dynobj
<size
, big_endian
>
465 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
467 Powerpc_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
468 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
469 : Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
470 opd_shndx_(0), e_flags_(ehdr
.get_e_flags()), opd_ent_()
472 this->set_abiversion(0);
478 // Call Sized_dynobj::do_read_symbols to read the symbols then
479 // read .opd from a dynamic object, filling in opd_ent_ vector,
481 do_read_symbols(Read_symbols_data
*);
483 // The .opd section shndx.
487 return this->opd_shndx_
;
490 // The .opd section address.
494 return this->opd_address_
;
497 // Init OPD entry arrays.
499 init_opd(size_t opd_size
)
501 size_t count
= this->opd_ent_ndx(opd_size
);
502 this->opd_ent_
.resize(count
);
505 // Return section and offset of function entry for .opd + R_OFF.
507 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
509 size_t ndx
= this->opd_ent_ndx(r_off
);
510 gold_assert(ndx
< this->opd_ent_
.size());
511 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
513 *value
= this->opd_ent_
[ndx
].off
;
514 return this->opd_ent_
[ndx
].shndx
;
517 // Set section and offset of function entry for .opd + R_OFF.
519 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
521 size_t ndx
= this->opd_ent_ndx(r_off
);
522 gold_assert(ndx
< this->opd_ent_
.size());
523 this->opd_ent_
[ndx
].shndx
= shndx
;
524 this->opd_ent_
[ndx
].off
= value
;
529 { return this->e_flags_
& elfcpp::EF_PPC64_ABI
; }
531 // Set ABI version for input and output.
533 set_abiversion(int ver
);
536 // Used to specify extent of executable sections.
539 Sec_info(Address start_
, Address len_
, unsigned int shndx_
)
540 : start(start_
), len(len_
), shndx(shndx_
)
544 operator<(const Sec_info
& that
) const
545 { return this->start
< that
.start
; }
558 // Return index into opd_ent_ array for .opd entry at OFF.
560 opd_ent_ndx(size_t off
) const
563 // For 64-bit the .opd section shndx and address.
564 unsigned int opd_shndx_
;
565 Address opd_address_
;
568 elfcpp::Elf_Word e_flags_
;
570 // The first 8-byte word of an OPD entry gives the address of the
571 // entry point of the function. Records the section and offset
572 // corresponding to the address. Note that in dynamic objects,
573 // offset is *not* relative to the section.
574 std::vector
<Opd_ent
> opd_ent_
;
577 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
578 // base class will emit.
580 template<int sh_type
, int size
, bool big_endian
>
581 class Powerpc_copy_relocs
: public Copy_relocs
<sh_type
, size
, big_endian
>
584 Powerpc_copy_relocs()
585 : Copy_relocs
<sh_type
, size
, big_endian
>(elfcpp::R_POWERPC_COPY
)
588 // Emit any saved relocations which turn out to be needed. This is
589 // called after all the relocs have been scanned.
591 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
594 template<int size
, bool big_endian
>
595 class Target_powerpc
: public Sized_target
<size
, big_endian
>
599 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
600 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
601 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Signed_address
;
602 typedef Unordered_set
<Symbol_location
, Symbol_location_hash
> Tocsave_loc
;
603 static const Address invalid_address
= static_cast<Address
>(0) - 1;
604 // Offset of tp and dtp pointers from start of TLS block.
605 static const Address tp_offset
= 0x7000;
606 static const Address dtp_offset
= 0x8000;
609 : Sized_target
<size
, big_endian
>(&powerpc_info
),
610 got_(NULL
), plt_(NULL
), iplt_(NULL
), brlt_section_(NULL
),
611 glink_(NULL
), rela_dyn_(NULL
), copy_relocs_(),
612 tlsld_got_offset_(-1U),
613 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
614 plt_thread_safe_(false), plt_localentry0_(false),
615 plt_localentry0_init_(false), has_localentry0_(false),
616 has_tls_get_addr_opt_(false),
617 relax_failed_(false), relax_fail_count_(0),
618 stub_group_size_(0), savres_section_(0),
619 tls_get_addr_(NULL
), tls_get_addr_opt_(NULL
)
623 // Process the relocations to determine unreferenced sections for
624 // garbage collection.
626 gc_process_relocs(Symbol_table
* symtab
,
628 Sized_relobj_file
<size
, big_endian
>* object
,
629 unsigned int data_shndx
,
630 unsigned int sh_type
,
631 const unsigned char* prelocs
,
633 Output_section
* output_section
,
634 bool needs_special_offset_handling
,
635 size_t local_symbol_count
,
636 const unsigned char* plocal_symbols
);
638 // Scan the relocations to look for symbol adjustments.
640 scan_relocs(Symbol_table
* symtab
,
642 Sized_relobj_file
<size
, big_endian
>* object
,
643 unsigned int data_shndx
,
644 unsigned int sh_type
,
645 const unsigned char* prelocs
,
647 Output_section
* output_section
,
648 bool needs_special_offset_handling
,
649 size_t local_symbol_count
,
650 const unsigned char* plocal_symbols
);
652 // Map input .toc section to output .got section.
654 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
656 if (size
== 64 && strcmp(name
, ".toc") == 0)
664 // Provide linker defined save/restore functions.
666 define_save_restore_funcs(Layout
*, Symbol_table
*);
668 // No stubs unless a final link.
671 { return !parameters
->options().relocatable(); }
674 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
677 do_plt_fde_location(const Output_data
*, unsigned char*,
678 uint64_t*, off_t
*) const;
680 // Stash info about branches, for stub generation.
682 push_branch(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
683 unsigned int data_shndx
, Address r_offset
,
684 unsigned int r_type
, unsigned int r_sym
, Address addend
)
686 Branch_info
info(ppc_object
, data_shndx
, r_offset
, r_type
, r_sym
, addend
);
687 this->branch_info_
.push_back(info
);
688 if (r_type
== elfcpp::R_POWERPC_REL14
689 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
690 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
691 ppc_object
->set_has_14bit_branch(data_shndx
);
694 // Return whether the last branch is a plt call, and if so, mark the
695 // branch as having an R_PPC64_TOCSAVE.
697 mark_pltcall(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
698 unsigned int data_shndx
, Address r_offset
, Symbol_table
* symtab
)
701 && !this->branch_info_
.empty()
702 && this->branch_info_
.back().mark_pltcall(ppc_object
, data_shndx
,
703 r_offset
, this, symtab
));
706 // Say the given location, that of a nop in a function prologue with
707 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
708 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
710 add_tocsave(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
711 unsigned int shndx
, Address offset
)
714 loc
.object
= ppc_object
;
717 this->tocsave_loc_
.insert(loc
);
724 return this->tocsave_loc_
;
728 do_define_standard_symbols(Symbol_table
*, Layout
*);
730 // Finalize the sections.
732 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
734 // Return the value to use for a dynamic which requires special
737 do_dynsym_value(const Symbol
*) const;
739 // Return the PLT address to use for a local symbol.
741 do_plt_address_for_local(const Relobj
*, unsigned int) const;
743 // Return the PLT address to use for a global symbol.
745 do_plt_address_for_global(const Symbol
*) const;
747 // Return the offset to use for the GOT_INDX'th got entry which is
748 // for a local tls symbol specified by OBJECT, SYMNDX.
750 do_tls_offset_for_local(const Relobj
* object
,
752 unsigned int got_indx
) const;
754 // Return the offset to use for the GOT_INDX'th got entry which is
755 // for global tls symbol GSYM.
757 do_tls_offset_for_global(Symbol
* gsym
, unsigned int got_indx
) const;
760 do_function_location(Symbol_location
*) const;
763 do_can_check_for_function_pointers() const
766 // Adjust -fsplit-stack code which calls non-split-stack code.
768 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
769 section_offset_type fnoffset
, section_size_type fnsize
,
770 const unsigned char* prelocs
, size_t reloc_count
,
771 unsigned char* view
, section_size_type view_size
,
772 std::string
* from
, std::string
* to
) const;
774 // Relocate a section.
776 relocate_section(const Relocate_info
<size
, big_endian
>*,
777 unsigned int sh_type
,
778 const unsigned char* prelocs
,
780 Output_section
* output_section
,
781 bool needs_special_offset_handling
,
783 Address view_address
,
784 section_size_type view_size
,
785 const Reloc_symbol_changes
*);
787 // Scan the relocs during a relocatable link.
789 scan_relocatable_relocs(Symbol_table
* symtab
,
791 Sized_relobj_file
<size
, big_endian
>* object
,
792 unsigned int data_shndx
,
793 unsigned int sh_type
,
794 const unsigned char* prelocs
,
796 Output_section
* output_section
,
797 bool needs_special_offset_handling
,
798 size_t local_symbol_count
,
799 const unsigned char* plocal_symbols
,
800 Relocatable_relocs
*);
802 // Scan the relocs for --emit-relocs.
804 emit_relocs_scan(Symbol_table
* symtab
,
806 Sized_relobj_file
<size
, big_endian
>* object
,
807 unsigned int data_shndx
,
808 unsigned int sh_type
,
809 const unsigned char* prelocs
,
811 Output_section
* output_section
,
812 bool needs_special_offset_handling
,
813 size_t local_symbol_count
,
814 const unsigned char* plocal_syms
,
815 Relocatable_relocs
* rr
);
817 // Emit relocations for a section.
819 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
820 unsigned int sh_type
,
821 const unsigned char* prelocs
,
823 Output_section
* output_section
,
824 typename
elfcpp::Elf_types
<size
>::Elf_Off
825 offset_in_output_section
,
827 Address view_address
,
829 unsigned char* reloc_view
,
830 section_size_type reloc_view_size
);
832 // Return whether SYM is defined by the ABI.
834 do_is_defined_by_abi(const Symbol
* sym
) const
836 return strcmp(sym
->name(), "__tls_get_addr") == 0;
839 // Return the size of the GOT section.
843 gold_assert(this->got_
!= NULL
);
844 return this->got_
->data_size();
847 // Get the PLT section.
848 const Output_data_plt_powerpc
<size
, big_endian
>*
851 gold_assert(this->plt_
!= NULL
);
855 // Get the IPLT section.
856 const Output_data_plt_powerpc
<size
, big_endian
>*
859 gold_assert(this->iplt_
!= NULL
);
863 // Get the .glink section.
864 const Output_data_glink
<size
, big_endian
>*
865 glink_section() const
867 gold_assert(this->glink_
!= NULL
);
871 Output_data_glink
<size
, big_endian
>*
874 gold_assert(this->glink_
!= NULL
);
878 bool has_glink() const
879 { return this->glink_
!= NULL
; }
881 // Get the GOT section.
882 const Output_data_got_powerpc
<size
, big_endian
>*
885 gold_assert(this->got_
!= NULL
);
889 // Get the GOT section, creating it if necessary.
890 Output_data_got_powerpc
<size
, big_endian
>*
891 got_section(Symbol_table
*, Layout
*);
894 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
895 const elfcpp::Ehdr
<size
, big_endian
>&);
897 // Return the number of entries in the GOT.
899 got_entry_count() const
901 if (this->got_
== NULL
)
903 return this->got_size() / (size
/ 8);
906 // Return the number of entries in the PLT.
908 plt_entry_count() const;
910 // Return the offset of the first non-reserved PLT entry.
912 first_plt_entry_offset() const
916 if (this->abiversion() >= 2)
921 // Return the size of each PLT entry.
923 plt_entry_size() const
927 if (this->abiversion() >= 2)
932 Output_data_save_res
<size
, big_endian
>*
933 savres_section() const
935 return this->savres_section_
;
938 // Add any special sections for this symbol to the gc work list.
939 // For powerpc64, this adds the code section of a function
942 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
944 // Handle target specific gc actions when adding a gc reference from
945 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
946 // and DST_OFF. For powerpc64, this adds a referenc to the code
947 // section of a function descriptor.
949 do_gc_add_reference(Symbol_table
* symtab
,
951 unsigned int src_shndx
,
953 unsigned int dst_shndx
,
954 Address dst_off
) const;
956 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
959 { return this->stub_tables_
; }
961 const Output_data_brlt_powerpc
<size
, big_endian
>*
963 { return this->brlt_section_
; }
966 add_branch_lookup_table(Address to
)
968 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
969 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
973 find_branch_lookup_table(Address to
)
975 typename
Branch_lookup_table::const_iterator p
976 = this->branch_lookup_table_
.find(to
);
977 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
981 write_branch_lookup_table(unsigned char *oview
)
983 for (typename
Branch_lookup_table::const_iterator p
984 = this->branch_lookup_table_
.begin();
985 p
!= this->branch_lookup_table_
.end();
988 elfcpp::Swap
<size
, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
992 // Wrapper used after relax to define a local symbol in output data,
993 // from the end if value < 0.
995 define_local(Symbol_table
* symtab
, const char* name
,
996 Output_data
* od
, Address value
, unsigned int symsize
)
999 = symtab
->define_in_output_data(name
, NULL
, Symbol_table::PREDEFINED
,
1000 od
, value
, symsize
, elfcpp::STT_NOTYPE
,
1001 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
, 0,
1002 static_cast<Signed_address
>(value
) < 0,
1004 // We are creating this symbol late, so need to fix up things
1005 // done early in Layout::finalize.
1006 sym
->set_dynsym_index(-1U);
1010 plt_thread_safe() const
1011 { return this->plt_thread_safe_
; }
1014 plt_localentry0() const
1015 { return this->plt_localentry0_
; }
1018 set_has_localentry0()
1020 this->has_localentry0_
= true;
1024 is_elfv2_localentry0(const Symbol
* gsym
) const
1027 && this->abiversion() >= 2
1028 && this->plt_localentry0()
1029 && gsym
->type() == elfcpp::STT_FUNC
1030 && gsym
->is_defined()
1031 && gsym
->nonvis() >> 3 == 0
1032 && !gsym
->non_zero_localentry());
1036 is_elfv2_localentry0(const Sized_relobj_file
<size
, big_endian
>* object
,
1037 unsigned int r_sym
) const
1039 const Powerpc_relobj
<size
, big_endian
>* ppc_object
1040 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
1043 && this->abiversion() >= 2
1044 && this->plt_localentry0()
1045 && ppc_object
->st_other(r_sym
) >> 5 == 0)
1047 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
1049 if (!psymval
->is_ifunc_symbol()
1050 && psymval
->input_shndx(&is_ordinary
) != elfcpp::SHN_UNDEF
1057 // Remember any symbols seen with non-zero localentry, even those
1058 // not providing a definition
1060 resolve(Symbol
* to
, const elfcpp::Sym
<size
, big_endian
>& sym
, Object
*,
1065 unsigned char st_other
= sym
.get_st_other();
1066 if ((st_other
& elfcpp::STO_PPC64_LOCAL_MASK
) != 0)
1067 to
->set_non_zero_localentry();
1069 // We haven't resolved anything, continue normal processing.
1075 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI
; }
1078 set_abiversion(int ver
)
1080 elfcpp::Elf_Word flags
= this->processor_specific_flags();
1081 flags
&= ~elfcpp::EF_PPC64_ABI
;
1082 flags
|= ver
& elfcpp::EF_PPC64_ABI
;
1083 this->set_processor_specific_flags(flags
);
1087 tls_get_addr_opt() const
1088 { return this->tls_get_addr_opt_
; }
1091 tls_get_addr() const
1092 { return this->tls_get_addr_
; }
1094 // If optimizing __tls_get_addr calls, whether this is the
1095 // "__tls_get_addr" symbol.
1097 is_tls_get_addr_opt(const Symbol
* gsym
) const
1099 return this->tls_get_addr_opt_
&& (gsym
== this->tls_get_addr_
1100 || gsym
== this->tls_get_addr_opt_
);
1104 replace_tls_get_addr(const Symbol
* gsym
) const
1105 { return this->tls_get_addr_opt_
&& gsym
== this->tls_get_addr_
; }
1108 set_has_tls_get_addr_opt()
1109 { this->has_tls_get_addr_opt_
= true; }
1111 // Offset to toc save stack slot
1114 { return this->abiversion() < 2 ? 40 : 24; }
1116 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1117 // so use the CR save slot. Used only by __tls_get_addr call stub,
1118 // relying on __tls_get_addr not saving CR itself.
1121 { return this->abiversion() < 2 ? 32 : 8; }
1137 : tls_get_addr_state_(NOT_EXPECTED
),
1138 relinfo_(NULL
), relnum_(0), r_offset_(0)
1143 if (this->tls_get_addr_state_
!= NOT_EXPECTED
)
1150 if (this->relinfo_
!= NULL
)
1151 gold_error_at_location(this->relinfo_
, this->relnum_
, this->r_offset_
,
1152 _("missing expected __tls_get_addr call"));
1156 expect_tls_get_addr_call(
1157 const Relocate_info
<size
, big_endian
>* relinfo
,
1161 this->tls_get_addr_state_
= EXPECTED
;
1162 this->relinfo_
= relinfo
;
1163 this->relnum_
= relnum
;
1164 this->r_offset_
= r_offset
;
1168 expect_tls_get_addr_call()
1169 { this->tls_get_addr_state_
= EXPECTED
; }
1172 skip_next_tls_get_addr_call()
1173 {this->tls_get_addr_state_
= SKIP
; }
1176 maybe_skip_tls_get_addr_call(Target_powerpc
<size
, big_endian
>* target
,
1177 unsigned int r_type
, const Symbol
* gsym
)
1179 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
1180 || r_type
== elfcpp::R_PPC_PLTREL24
)
1182 && (gsym
== target
->tls_get_addr()
1183 || gsym
== target
->tls_get_addr_opt()));
1184 Tls_get_addr last_tls
= this->tls_get_addr_state_
;
1185 this->tls_get_addr_state_
= NOT_EXPECTED
;
1186 if (is_tls_call
&& last_tls
!= EXPECTED
)
1188 else if (!is_tls_call
&& last_tls
!= NOT_EXPECTED
)
1197 // What we're up to regarding calls to __tls_get_addr.
1198 // On powerpc, the branch and link insn making a call to
1199 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1200 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1201 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
1202 // The marker relocation always comes first, and has the same
1203 // symbol as the reloc on the insn setting up the __tls_get_addr
1204 // argument. This ties the arg setup insn with the call insn,
1205 // allowing ld to safely optimize away the call. We check that
1206 // every call to __tls_get_addr has a marker relocation, and that
1207 // every marker relocation is on a call to __tls_get_addr.
1208 Tls_get_addr tls_get_addr_state_
;
1209 // Info about the last reloc for error message.
1210 const Relocate_info
<size
, big_endian
>* relinfo_
;
1215 // The class which scans relocations.
1216 class Scan
: protected Track_tls
1219 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1222 : Track_tls(), issued_non_pic_error_(false)
1226 get_reference_flags(unsigned int r_type
, const Target_powerpc
* target
);
1229 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
1230 Sized_relobj_file
<size
, big_endian
>* object
,
1231 unsigned int data_shndx
,
1232 Output_section
* output_section
,
1233 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
1234 const elfcpp::Sym
<size
, big_endian
>& lsym
,
1238 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
1239 Sized_relobj_file
<size
, big_endian
>* object
,
1240 unsigned int data_shndx
,
1241 Output_section
* output_section
,
1242 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
1246 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1248 Sized_relobj_file
<size
, big_endian
>* relobj
,
1251 const elfcpp::Rela
<size
, big_endian
>& ,
1252 unsigned int r_type
,
1253 const elfcpp::Sym
<size
, big_endian
>&)
1255 // PowerPC64 .opd is not folded, so any identical function text
1256 // may be folded and we'll still keep function addresses distinct.
1257 // That means no reloc is of concern here.
1260 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1261 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1262 if (ppcobj
->abiversion() == 1)
1265 // For 32-bit and ELFv2, conservatively assume anything but calls to
1266 // function code might be taking the address of the function.
1267 return !is_branch_reloc(r_type
);
1271 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1273 Sized_relobj_file
<size
, big_endian
>* relobj
,
1276 const elfcpp::Rela
<size
, big_endian
>& ,
1277 unsigned int r_type
,
1283 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1284 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1285 if (ppcobj
->abiversion() == 1)
1288 return !is_branch_reloc(r_type
);
1292 reloc_needs_plt_for_ifunc(Target_powerpc
<size
, big_endian
>* target
,
1293 Sized_relobj_file
<size
, big_endian
>* object
,
1294 unsigned int r_type
, bool report_err
);
1298 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
1299 unsigned int r_type
);
1302 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
1303 unsigned int r_type
, Symbol
*);
1306 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
1307 Target_powerpc
* target
);
1310 check_non_pic(Relobj
*, unsigned int r_type
);
1312 // Whether we have issued an error about a non-PIC compilation.
1313 bool issued_non_pic_error_
;
1317 symval_for_branch(const Symbol_table
* symtab
,
1318 const Sized_symbol
<size
>* gsym
,
1319 Powerpc_relobj
<size
, big_endian
>* object
,
1320 Address
*value
, unsigned int *dest_shndx
);
1322 // The class which implements relocation.
1323 class Relocate
: protected Track_tls
1326 // Use 'at' branch hints when true, 'y' when false.
1327 // FIXME maybe: set this with an option.
1328 static const bool is_isa_v2
= true;
1334 // Do a relocation. Return false if the caller should not issue
1335 // any warnings about this relocation.
1337 relocate(const Relocate_info
<size
, big_endian
>*, unsigned int,
1338 Target_powerpc
*, Output_section
*, size_t, const unsigned char*,
1339 const Sized_symbol
<size
>*, const Symbol_value
<size
>*,
1340 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1344 class Relocate_comdat_behavior
1347 // Decide what the linker should do for relocations that refer to
1348 // discarded comdat sections.
1349 inline Comdat_behavior
1350 get(const char* name
)
1352 gold::Default_comdat_behavior default_behavior
;
1353 Comdat_behavior ret
= default_behavior
.get(name
);
1354 if (ret
== CB_ERROR
)
1357 && (strcmp(name
, ".fixup") == 0
1358 || strcmp(name
, ".got2") == 0))
1361 && (strcmp(name
, ".opd") == 0
1362 || strcmp(name
, ".toc") == 0
1363 || strcmp(name
, ".toc1") == 0))
1370 // Optimize the TLS relocation type based on what we know about the
1371 // symbol. IS_FINAL is true if the final address of this symbol is
1372 // known at link time.
1374 tls::Tls_optimization
1375 optimize_tls_gd(bool is_final
)
1377 // If we are generating a shared library, then we can't do anything
1379 if (parameters
->options().shared()
1380 || !parameters
->options().tls_optimize())
1381 return tls::TLSOPT_NONE
;
1384 return tls::TLSOPT_TO_IE
;
1385 return tls::TLSOPT_TO_LE
;
1388 tls::Tls_optimization
1391 if (parameters
->options().shared()
1392 || !parameters
->options().tls_optimize())
1393 return tls::TLSOPT_NONE
;
1395 return tls::TLSOPT_TO_LE
;
1398 tls::Tls_optimization
1399 optimize_tls_ie(bool is_final
)
1402 || parameters
->options().shared()
1403 || !parameters
->options().tls_optimize())
1404 return tls::TLSOPT_NONE
;
1406 return tls::TLSOPT_TO_LE
;
1411 make_glink_section(Layout
*);
1413 // Create the PLT section.
1415 make_plt_section(Symbol_table
*, Layout
*);
1418 make_iplt_section(Symbol_table
*, Layout
*);
1421 make_brlt_section(Layout
*);
1423 // Create a PLT entry for a global symbol.
1425 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1427 // Create a PLT entry for a local IFUNC symbol.
1429 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1430 Sized_relobj_file
<size
, big_endian
>*,
1434 // Create a GOT entry for local dynamic __tls_get_addr.
1436 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
1437 Sized_relobj_file
<size
, big_endian
>* object
);
1440 tlsld_got_offset() const
1442 return this->tlsld_got_offset_
;
1445 // Get the dynamic reloc section, creating it if necessary.
1447 rela_dyn_section(Layout
*);
1449 // Similarly, but for ifunc symbols get the one for ifunc.
1451 rela_dyn_section(Symbol_table
*, Layout
*, bool for_ifunc
);
1453 // Copy a relocation against a global symbol.
1455 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1456 Sized_relobj_file
<size
, big_endian
>* object
,
1457 unsigned int shndx
, Output_section
* output_section
,
1458 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
1460 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1461 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1462 symtab
->get_sized_symbol
<size
>(sym
),
1463 object
, shndx
, output_section
,
1464 r_type
, reloc
.get_r_offset(),
1465 reloc
.get_r_addend(),
1466 this->rela_dyn_section(layout
));
1469 // Look over all the input sections, deciding where to place stubs.
1471 group_sections(Layout
*, const Task
*, bool);
1473 // Sort output sections by address.
1474 struct Sort_sections
1477 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
1478 { return sec1
->address() < sec2
->address(); }
1484 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1485 unsigned int data_shndx
,
1487 unsigned int r_type
,
1490 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
1491 r_type_(r_type
), tocsave_ (0), r_sym_(r_sym
), addend_(addend
)
1497 // Return whether this branch is going via a plt call stub, and if
1498 // so, mark it as having an R_PPC64_TOCSAVE.
1500 mark_pltcall(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1501 unsigned int shndx
, Address offset
,
1502 Target_powerpc
* target
, Symbol_table
* symtab
);
1504 // If this branch needs a plt call stub, or a long branch stub, make one.
1506 make_stub(Stub_table
<size
, big_endian
>*,
1507 Stub_table
<size
, big_endian
>*,
1508 Symbol_table
*) const;
1511 // The branch location..
1512 Powerpc_relobj
<size
, big_endian
>* object_
;
1513 unsigned int shndx_
;
1515 // ..and the branch type and destination.
1516 unsigned int r_type_
: 31;
1517 unsigned int tocsave_
: 1;
1518 unsigned int r_sym_
;
1522 // Information about this specific target which we pass to the
1523 // general Target structure.
1524 static Target::Target_info powerpc_info
;
1526 // The types of GOT entries needed for this platform.
1527 // These values are exposed to the ABI in an incremental link.
1528 // Do not renumber existing values without changing the version
1529 // number of the .gnu_incremental_inputs section.
1533 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
1534 GOT_TYPE_DTPREL
, // entry for @got@dtprel
1535 GOT_TYPE_TPREL
// entry for @got@tprel
1539 Output_data_got_powerpc
<size
, big_endian
>* got_
;
1540 // The PLT section. This is a container for a table of addresses,
1541 // and their relocations. Each address in the PLT has a dynamic
1542 // relocation (R_*_JMP_SLOT) and each address will have a
1543 // corresponding entry in .glink for lazy resolution of the PLT.
1544 // ppc32 initialises the PLT to point at the .glink entry, while
1545 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1546 // linker adds a stub that loads the PLT entry into ctr then
1547 // branches to ctr. There may be more than one stub for each PLT
1548 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1549 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1550 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
1551 // The IPLT section. Like plt_, this is a container for a table of
1552 // addresses and their relocations, specifically for STT_GNU_IFUNC
1553 // functions that resolve locally (STT_GNU_IFUNC functions that
1554 // don't resolve locally go in PLT). Unlike plt_, these have no
1555 // entry in .glink for lazy resolution, and the relocation section
1556 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1557 // the relocation section may contain relocations against
1558 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1559 // relocation section will appear at the end of other dynamic
1560 // relocations, so that ld.so applies these relocations after other
1561 // dynamic relocations. In a static executable, the relocation
1562 // section is emitted and marked with __rela_iplt_start and
1563 // __rela_iplt_end symbols.
1564 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
1565 // Section holding long branch destinations.
1566 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
1567 // The .glink section.
1568 Output_data_glink
<size
, big_endian
>* glink_
;
1569 // The dynamic reloc section.
1570 Reloc_section
* rela_dyn_
;
1571 // Relocs saved to avoid a COPY reloc.
1572 Powerpc_copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
1573 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1574 unsigned int tlsld_got_offset_
;
1576 Stub_tables stub_tables_
;
1577 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
1578 Branch_lookup_table branch_lookup_table_
;
1580 typedef std::vector
<Branch_info
> Branches
;
1581 Branches branch_info_
;
1582 Tocsave_loc tocsave_loc_
;
1584 bool plt_thread_safe_
;
1585 bool plt_localentry0_
;
1586 bool plt_localentry0_init_
;
1587 bool has_localentry0_
;
1588 bool has_tls_get_addr_opt_
;
1591 int relax_fail_count_
;
1592 int32_t stub_group_size_
;
1594 Output_data_save_res
<size
, big_endian
> *savres_section_
;
1596 // The "__tls_get_addr" symbol, if present
1597 Symbol
* tls_get_addr_
;
1598 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1599 Symbol
* tls_get_addr_opt_
;
1603 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
1606 true, // is_big_endian
1607 elfcpp::EM_PPC
, // machine_code
1608 false, // has_make_symbol
1609 false, // has_resolve
1610 false, // has_code_fill
1611 true, // is_default_stack_executable
1612 false, // can_icf_inline_merge_sections
1614 "/usr/lib/ld.so.1", // dynamic_linker
1615 0x10000000, // default_text_segment_address
1616 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1617 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1618 false, // isolate_execinstr
1620 elfcpp::SHN_UNDEF
, // small_common_shndx
1621 elfcpp::SHN_UNDEF
, // large_common_shndx
1622 0, // small_common_section_flags
1623 0, // large_common_section_flags
1624 NULL
, // attributes_section
1625 NULL
, // attributes_vendor
1626 "_start", // entry_symbol_name
1627 32, // hash_entry_size
1628 elfcpp::SHT_PROGBITS
, // unwind_section_type
1632 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
1635 false, // is_big_endian
1636 elfcpp::EM_PPC
, // machine_code
1637 false, // has_make_symbol
1638 false, // has_resolve
1639 false, // has_code_fill
1640 true, // is_default_stack_executable
1641 false, // can_icf_inline_merge_sections
1643 "/usr/lib/ld.so.1", // dynamic_linker
1644 0x10000000, // default_text_segment_address
1645 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1646 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1647 false, // isolate_execinstr
1649 elfcpp::SHN_UNDEF
, // small_common_shndx
1650 elfcpp::SHN_UNDEF
, // large_common_shndx
1651 0, // small_common_section_flags
1652 0, // large_common_section_flags
1653 NULL
, // attributes_section
1654 NULL
, // attributes_vendor
1655 "_start", // entry_symbol_name
1656 32, // hash_entry_size
1657 elfcpp::SHT_PROGBITS
, // unwind_section_type
1661 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1664 true, // is_big_endian
1665 elfcpp::EM_PPC64
, // machine_code
1666 false, // has_make_symbol
1667 true, // has_resolve
1668 false, // has_code_fill
1669 false, // is_default_stack_executable
1670 false, // can_icf_inline_merge_sections
1672 "/usr/lib/ld.so.1", // dynamic_linker
1673 0x10000000, // default_text_segment_address
1674 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1675 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1676 false, // isolate_execinstr
1678 elfcpp::SHN_UNDEF
, // small_common_shndx
1679 elfcpp::SHN_UNDEF
, // large_common_shndx
1680 0, // small_common_section_flags
1681 0, // large_common_section_flags
1682 NULL
, // attributes_section
1683 NULL
, // attributes_vendor
1684 "_start", // entry_symbol_name
1685 32, // hash_entry_size
1686 elfcpp::SHT_PROGBITS
, // unwind_section_type
1690 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1693 false, // is_big_endian
1694 elfcpp::EM_PPC64
, // machine_code
1695 false, // has_make_symbol
1696 true, // has_resolve
1697 false, // has_code_fill
1698 false, // is_default_stack_executable
1699 false, // can_icf_inline_merge_sections
1701 "/usr/lib/ld.so.1", // dynamic_linker
1702 0x10000000, // default_text_segment_address
1703 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1704 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1705 false, // isolate_execinstr
1707 elfcpp::SHN_UNDEF
, // small_common_shndx
1708 elfcpp::SHN_UNDEF
, // large_common_shndx
1709 0, // small_common_section_flags
1710 0, // large_common_section_flags
1711 NULL
, // attributes_section
1712 NULL
, // attributes_vendor
1713 "_start", // entry_symbol_name
1714 32, // hash_entry_size
1715 elfcpp::SHT_PROGBITS
, // unwind_section_type
1719 is_branch_reloc(unsigned int r_type
)
1721 return (r_type
== elfcpp::R_POWERPC_REL24
1722 || r_type
== elfcpp::R_PPC_PLTREL24
1723 || r_type
== elfcpp::R_PPC_LOCAL24PC
1724 || r_type
== elfcpp::R_POWERPC_REL14
1725 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1726 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1727 || r_type
== elfcpp::R_POWERPC_ADDR24
1728 || r_type
== elfcpp::R_POWERPC_ADDR14
1729 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1730 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1733 // If INSN is an opcode that may be used with an @tls operand, return
1734 // the transformed insn for TLS optimisation, otherwise return 0. If
1735 // REG is non-zero only match an insn with RB or RA equal to REG.
1737 at_tls_transform(uint32_t insn
, unsigned int reg
)
1739 if ((insn
& (0x3f << 26)) != 31 << 26)
1743 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1744 rtra
= insn
& ((1 << 26) - (1 << 16));
1745 else if (((insn
>> 16) & 0x1f) == reg
)
1746 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1750 if ((insn
& (0x3ff << 1)) == 266 << 1)
1753 else if ((insn
& (0x1f << 1)) == 23 << 1
1754 && ((insn
& (0x1f << 6)) < 14 << 6
1755 || ((insn
& (0x1f << 6)) >= 16 << 6
1756 && (insn
& (0x1f << 6)) < 24 << 6)))
1757 // load and store indexed -> dform
1758 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1759 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1760 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1761 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1762 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1764 insn
= (58 << 26) | 2;
1772 template<int size
, bool big_endian
>
1773 class Powerpc_relocate_functions
1793 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1794 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1795 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword SignedAddress
;
1797 template<int valsize
>
1799 has_overflow_signed(Address value
)
1801 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1802 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1803 limit
<<= ((valsize
- 1) >> 1);
1804 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1805 return value
+ limit
> (limit
<< 1) - 1;
1808 template<int valsize
>
1810 has_overflow_unsigned(Address value
)
1812 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1813 limit
<<= ((valsize
- 1) >> 1);
1814 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1815 return value
> (limit
<< 1) - 1;
1818 template<int valsize
>
1820 has_overflow_bitfield(Address value
)
1822 return (has_overflow_unsigned
<valsize
>(value
)
1823 && has_overflow_signed
<valsize
>(value
));
1826 template<int valsize
>
1827 static inline Status
1828 overflowed(Address value
, Overflow_check overflow
)
1830 if (overflow
== CHECK_SIGNED
)
1832 if (has_overflow_signed
<valsize
>(value
))
1833 return STATUS_OVERFLOW
;
1835 else if (overflow
== CHECK_UNSIGNED
)
1837 if (has_overflow_unsigned
<valsize
>(value
))
1838 return STATUS_OVERFLOW
;
1840 else if (overflow
== CHECK_BITFIELD
)
1842 if (has_overflow_bitfield
<valsize
>(value
))
1843 return STATUS_OVERFLOW
;
1848 // Do a simple RELA relocation
1849 template<int fieldsize
, int valsize
>
1850 static inline Status
1851 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1853 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1854 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1855 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, value
);
1856 return overflowed
<valsize
>(value
, overflow
);
1859 template<int fieldsize
, int valsize
>
1860 static inline Status
1861 rela(unsigned char* view
,
1862 unsigned int right_shift
,
1863 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1865 Overflow_check overflow
)
1867 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1868 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1869 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(wv
);
1870 Valtype reloc
= value
>> right_shift
;
1873 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, val
| reloc
);
1874 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1877 // Do a simple RELA relocation, unaligned.
1878 template<int fieldsize
, int valsize
>
1879 static inline Status
1880 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1882 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, value
);
1883 return overflowed
<valsize
>(value
, overflow
);
1886 template<int fieldsize
, int valsize
>
1887 static inline Status
1888 rela_ua(unsigned char* view
,
1889 unsigned int right_shift
,
1890 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1892 Overflow_check overflow
)
1894 typedef typename
elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::Valtype
1896 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(view
);
1897 Valtype reloc
= value
>> right_shift
;
1900 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, val
| reloc
);
1901 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1905 // R_PPC64_ADDR64: (Symbol + Addend)
1907 addr64(unsigned char* view
, Address value
)
1908 { This::template rela
<64,64>(view
, value
, CHECK_NONE
); }
1910 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1912 addr64_u(unsigned char* view
, Address value
)
1913 { This::template rela_ua
<64,64>(view
, value
, CHECK_NONE
); }
1915 // R_POWERPC_ADDR32: (Symbol + Addend)
1916 static inline Status
1917 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
1918 { return This::template rela
<32,32>(view
, value
, overflow
); }
1920 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1921 static inline Status
1922 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1923 { return This::template rela_ua
<32,32>(view
, value
, overflow
); }
1925 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1926 static inline Status
1927 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
1929 Status stat
= This::template rela
<32,26>(view
, 0, 0x03fffffc,
1931 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1932 stat
= STATUS_OVERFLOW
;
1936 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1937 static inline Status
1938 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
1939 { return This::template rela
<16,16>(view
, value
, overflow
); }
1941 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1942 static inline Status
1943 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1944 { return This::template rela_ua
<16,16>(view
, value
, overflow
); }
1946 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1947 static inline Status
1948 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
1950 Status stat
= This::template rela
<16,16>(view
, 0, 0xfffc, value
, overflow
);
1951 if ((value
& 3) != 0)
1952 stat
= STATUS_OVERFLOW
;
1956 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
1957 static inline Status
1958 addr16_dq(unsigned char* view
, Address value
, Overflow_check overflow
)
1960 Status stat
= This::template rela
<16,16>(view
, 0, 0xfff0, value
, overflow
);
1961 if ((value
& 15) != 0)
1962 stat
= STATUS_OVERFLOW
;
1966 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1968 addr16_hi(unsigned char* view
, Address value
)
1969 { This::template rela
<16,16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
1971 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1973 addr16_ha(unsigned char* view
, Address value
)
1974 { This::addr16_hi(view
, value
+ 0x8000); }
1976 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1978 addr16_hi2(unsigned char* view
, Address value
)
1979 { This::template rela
<16,16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
1981 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1983 addr16_ha2(unsigned char* view
, Address value
)
1984 { This::addr16_hi2(view
, value
+ 0x8000); }
1986 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1988 addr16_hi3(unsigned char* view
, Address value
)
1989 { This::template rela
<16,16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
1991 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1993 addr16_ha3(unsigned char* view
, Address value
)
1994 { This::addr16_hi3(view
, value
+ 0x8000); }
1996 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1997 static inline Status
1998 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
2000 Status stat
= This::template rela
<32,16>(view
, 0, 0xfffc, value
, overflow
);
2001 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
2002 stat
= STATUS_OVERFLOW
;
2006 // R_POWERPC_REL16DX_HA
2007 static inline Status
2008 addr16dx_ha(unsigned char *view
, Address value
, Overflow_check overflow
)
2010 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2011 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2012 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2014 value
= static_cast<SignedAddress
>(value
) >> 16;
2015 val
|= (value
& 0xffc1) | ((value
& 0x3e) << 15);
2016 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2017 return overflowed
<16>(value
, overflow
);
2021 // Set ABI version for input and output.
2023 template<int size
, bool big_endian
>
2025 Powerpc_relobj
<size
, big_endian
>::set_abiversion(int ver
)
2027 this->e_flags_
|= ver
;
2028 if (this->abiversion() != 0)
2030 Target_powerpc
<size
, big_endian
>* target
=
2031 static_cast<Target_powerpc
<size
, big_endian
>*>(
2032 parameters
->sized_target
<size
, big_endian
>());
2033 if (target
->abiversion() == 0)
2034 target
->set_abiversion(this->abiversion());
2035 else if (target
->abiversion() != this->abiversion())
2036 gold_error(_("%s: ABI version %d is not compatible "
2037 "with ABI version %d output"),
2038 this->name().c_str(),
2039 this->abiversion(), target
->abiversion());
2044 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2045 // relocatable object, if such sections exists.
2047 template<int size
, bool big_endian
>
2049 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
2050 Read_symbols_data
* sd
)
2052 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2053 const unsigned char* namesu
= sd
->section_names
->data();
2054 const char* names
= reinterpret_cast<const char*>(namesu
);
2055 section_size_type names_size
= sd
->section_names_size
;
2056 const unsigned char* s
;
2058 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
,
2059 size
== 32 ? ".got2" : ".opd",
2060 names
, names_size
, NULL
);
2063 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
2064 this->special_
= ndx
;
2067 if (this->abiversion() == 0)
2068 this->set_abiversion(1);
2069 else if (this->abiversion() > 1)
2070 gold_error(_("%s: .opd invalid in abiv%d"),
2071 this->name().c_str(), this->abiversion());
2076 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".rela.toc",
2077 names
, names_size
, NULL
);
2080 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
2081 this->relatoc_
= ndx
;
2082 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2083 this->toc_
= this->adjust_shndx(shdr
.get_sh_info());
2086 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
2089 // Examine .rela.opd to build info about function entry points.
2091 template<int size
, bool big_endian
>
2093 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
2095 const unsigned char* prelocs
,
2096 const unsigned char* plocal_syms
)
2100 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
2101 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
2102 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2103 Address expected_off
= 0;
2104 bool regular
= true;
2105 unsigned int opd_ent_size
= 0;
2107 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
2109 Reltype
reloc(prelocs
);
2110 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
2111 = reloc
.get_r_info();
2112 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
2113 if (r_type
== elfcpp::R_PPC64_ADDR64
)
2115 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
2116 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
2119 if (r_sym
< this->local_symbol_count())
2121 typename
elfcpp::Sym
<size
, big_endian
>
2122 lsym(plocal_syms
+ r_sym
* sym_size
);
2123 shndx
= lsym
.get_st_shndx();
2124 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
2125 value
= lsym
.get_st_value();
2128 shndx
= this->symbol_section_and_value(r_sym
, &value
,
2130 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
2131 value
+ reloc
.get_r_addend());
2134 expected_off
= reloc
.get_r_offset();
2135 opd_ent_size
= expected_off
;
2137 else if (expected_off
!= reloc
.get_r_offset())
2139 expected_off
+= opd_ent_size
;
2141 else if (r_type
== elfcpp::R_PPC64_TOC
)
2143 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
2148 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2149 this->name().c_str(), r_type
);
2153 if (reloc_count
<= 2)
2154 opd_ent_size
= this->section_size(this->opd_shndx());
2155 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
2159 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2160 this->name().c_str());
2166 // Returns true if a code sequence loading the TOC entry at VALUE
2167 // relative to the TOC pointer can be converted into code calculating
2168 // a TOC pointer relative offset.
2169 // If so, the TOC pointer relative offset is stored to VALUE.
2171 template<int size
, bool big_endian
>
2173 Powerpc_relobj
<size
, big_endian
>::make_toc_relative(
2174 Target_powerpc
<size
, big_endian
>* target
,
2180 // With -mcmodel=medium code it is quite possible to have
2181 // toc-relative relocs referring to objects outside the TOC.
2182 // Don't try to look at a non-existent TOC.
2183 if (this->toc_shndx() == 0)
2186 // Convert VALUE back to an address by adding got_base (see below),
2187 // then to an offset in the TOC by subtracting the TOC output
2188 // section address and the TOC output offset. Since this TOC output
2189 // section and the got output section are one and the same, we can
2190 // omit adding and subtracting the output section address.
2191 Address off
= (*value
+ this->toc_base_offset()
2192 - this->output_section_offset(this->toc_shndx()));
2193 // Is this offset in the TOC? -mcmodel=medium code may be using
2194 // TOC relative access to variables outside the TOC. Those of
2195 // course can't be optimized. We also don't try to optimize code
2196 // that is using a different object's TOC.
2197 if (off
>= this->section_size(this->toc_shndx()))
2200 if (this->no_toc_opt(off
))
2203 section_size_type vlen
;
2204 unsigned char* view
= this->get_output_view(this->toc_shndx(), &vlen
);
2205 Address addr
= elfcpp::Swap
<size
, big_endian
>::readval(view
+ off
);
2207 Address got_base
= (target
->got_section()->output_section()->address()
2208 + this->toc_base_offset());
2210 if (addr
+ (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2217 // Perform the Sized_relobj_file method, then set up opd info from
2220 template<int size
, bool big_endian
>
2222 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
2224 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
2227 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
2228 p
!= rd
->relocs
.end();
2231 if (p
->data_shndx
== this->opd_shndx())
2233 uint64_t opd_size
= this->section_size(this->opd_shndx());
2234 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
2237 this->init_opd(opd_size
);
2238 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
2239 rd
->local_symbols
->data());
2247 // Read the symbols then set up st_other vector.
2249 template<int size
, bool big_endian
>
2251 Powerpc_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
2253 this->base_read_symbols(sd
);
2256 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2257 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2258 const unsigned int loccount
= this->do_local_symbol_count();
2261 this->st_other_
.resize(loccount
);
2262 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2263 off_t locsize
= loccount
* sym_size
;
2264 const unsigned int symtab_shndx
= this->symtab_shndx();
2265 const unsigned char *psymtab
= pshdrs
+ symtab_shndx
* shdr_size
;
2266 typename
elfcpp::Shdr
<size
, big_endian
> shdr(psymtab
);
2267 const unsigned char* psyms
= this->get_view(shdr
.get_sh_offset(),
2268 locsize
, true, false);
2270 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
2272 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
2273 unsigned char st_other
= sym
.get_st_other();
2274 this->st_other_
[i
] = st_other
;
2275 if ((st_other
& elfcpp::STO_PPC64_LOCAL_MASK
) != 0)
2277 if (this->abiversion() == 0)
2278 this->set_abiversion(2);
2279 else if (this->abiversion() < 2)
2280 gold_error(_("%s: local symbol %d has invalid st_other"
2281 " for ABI version 1"),
2282 this->name().c_str(), i
);
2289 template<int size
, bool big_endian
>
2291 Powerpc_dynobj
<size
, big_endian
>::set_abiversion(int ver
)
2293 this->e_flags_
|= ver
;
2294 if (this->abiversion() != 0)
2296 Target_powerpc
<size
, big_endian
>* target
=
2297 static_cast<Target_powerpc
<size
, big_endian
>*>(
2298 parameters
->sized_target
<size
, big_endian
>());
2299 if (target
->abiversion() == 0)
2300 target
->set_abiversion(this->abiversion());
2301 else if (target
->abiversion() != this->abiversion())
2302 gold_error(_("%s: ABI version %d is not compatible "
2303 "with ABI version %d output"),
2304 this->name().c_str(),
2305 this->abiversion(), target
->abiversion());
2310 // Call Sized_dynobj::base_read_symbols to read the symbols then
2311 // read .opd from a dynamic object, filling in opd_ent_ vector,
2313 template<int size
, bool big_endian
>
2315 Powerpc_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
2317 this->base_read_symbols(sd
);
2320 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2321 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2322 const unsigned char* namesu
= sd
->section_names
->data();
2323 const char* names
= reinterpret_cast<const char*>(namesu
);
2324 const unsigned char* s
= NULL
;
2325 const unsigned char* opd
;
2326 section_size_type opd_size
;
2328 // Find and read .opd section.
2331 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".opd", names
,
2332 sd
->section_names_size
,
2337 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2338 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2339 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
2341 if (this->abiversion() == 0)
2342 this->set_abiversion(1);
2343 else if (this->abiversion() > 1)
2344 gold_error(_("%s: .opd invalid in abiv%d"),
2345 this->name().c_str(), this->abiversion());
2347 this->opd_shndx_
= (s
- pshdrs
) / shdr_size
;
2348 this->opd_address_
= shdr
.get_sh_addr();
2349 opd_size
= convert_to_section_size_type(shdr
.get_sh_size());
2350 opd
= this->get_view(shdr
.get_sh_offset(), opd_size
,
2356 // Build set of executable sections.
2357 // Using a set is probably overkill. There is likely to be only
2358 // a few executable sections, typically .init, .text and .fini,
2359 // and they are generally grouped together.
2360 typedef std::set
<Sec_info
> Exec_sections
;
2361 Exec_sections exec_sections
;
2363 for (unsigned int i
= 1; i
< this->shnum(); ++i
, s
+= shdr_size
)
2365 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2366 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2367 && ((shdr
.get_sh_flags()
2368 & (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2369 == (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2370 && shdr
.get_sh_size() != 0)
2372 exec_sections
.insert(Sec_info(shdr
.get_sh_addr(),
2373 shdr
.get_sh_size(), i
));
2376 if (exec_sections
.empty())
2379 // Look over the OPD entries. This is complicated by the fact
2380 // that some binaries will use two-word entries while others
2381 // will use the standard three-word entries. In most cases
2382 // the third word (the environment pointer for languages like
2383 // Pascal) is unused and will be zero. If the third word is
2384 // used it should not be pointing into executable sections,
2386 this->init_opd(opd_size
);
2387 for (const unsigned char* p
= opd
; p
< opd
+ opd_size
; p
+= 8)
2389 typedef typename
elfcpp::Swap
<64, big_endian
>::Valtype Valtype
;
2390 const Valtype
* valp
= reinterpret_cast<const Valtype
*>(p
);
2391 Valtype val
= elfcpp::Swap
<64, big_endian
>::readval(valp
);
2393 // Chances are that this is the third word of an OPD entry.
2395 typename
Exec_sections::const_iterator e
2396 = exec_sections
.upper_bound(Sec_info(val
, 0, 0));
2397 if (e
!= exec_sections
.begin())
2400 if (e
->start
<= val
&& val
< e
->start
+ e
->len
)
2402 // We have an address in an executable section.
2403 // VAL ought to be the function entry, set it up.
2404 this->set_opd_ent(p
- opd
, e
->shndx
, val
);
2405 // Skip second word of OPD entry, the TOC pointer.
2409 // If we didn't match any executable sections, we likely
2410 // have a non-zero third word in the OPD entry.
2415 // Relocate sections.
2417 template<int size
, bool big_endian
>
2419 Powerpc_relobj
<size
, big_endian
>::do_relocate_sections(
2420 const Symbol_table
* symtab
, const Layout
* layout
,
2421 const unsigned char* pshdrs
, Output_file
* of
,
2422 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
)
2424 unsigned int start
= 1;
2426 && this->relatoc_
!= 0
2427 && !parameters
->options().relocatable())
2429 // Relocate .toc first.
2430 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2431 this->relatoc_
, this->relatoc_
);
2432 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2433 1, this->relatoc_
- 1);
2434 start
= this->relatoc_
+ 1;
2436 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2437 start
, this->shnum() - 1);
2440 // Set up some symbols.
2442 template<int size
, bool big_endian
>
2444 Target_powerpc
<size
, big_endian
>::do_define_standard_symbols(
2445 Symbol_table
* symtab
,
2450 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2451 // undefined when scanning relocs (and thus requires
2452 // non-relative dynamic relocs). The proper value will be
2454 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2455 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2457 Target_powerpc
<size
, big_endian
>* target
=
2458 static_cast<Target_powerpc
<size
, big_endian
>*>(
2459 parameters
->sized_target
<size
, big_endian
>());
2460 Output_data_got_powerpc
<size
, big_endian
>* got
2461 = target
->got_section(symtab
, layout
);
2462 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2463 Symbol_table::PREDEFINED
,
2467 elfcpp::STV_HIDDEN
, 0,
2471 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2472 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
2473 if (sdasym
!= NULL
&& sdasym
->is_undefined())
2475 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
2477 = layout
->add_output_section_data(".sdata", 0,
2479 | elfcpp::SHF_WRITE
,
2480 sdata
, ORDER_SMALL_DATA
, false);
2481 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
2482 Symbol_table::PREDEFINED
,
2483 os
, 32768, 0, elfcpp::STT_OBJECT
,
2484 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
2490 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2491 Symbol
*gotsym
= symtab
->lookup(".TOC.", NULL
);
2492 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2494 Target_powerpc
<size
, big_endian
>* target
=
2495 static_cast<Target_powerpc
<size
, big_endian
>*>(
2496 parameters
->sized_target
<size
, big_endian
>());
2497 Output_data_got_powerpc
<size
, big_endian
>* got
2498 = target
->got_section(symtab
, layout
);
2499 symtab
->define_in_output_data(".TOC.", NULL
,
2500 Symbol_table::PREDEFINED
,
2504 elfcpp::STV_HIDDEN
, 0,
2509 this->tls_get_addr_
= symtab
->lookup("__tls_get_addr");
2510 if (parameters
->options().tls_get_addr_optimize()
2511 && this->tls_get_addr_
!= NULL
2512 && this->tls_get_addr_
->in_reg())
2513 this->tls_get_addr_opt_
= symtab
->lookup("__tls_get_addr_opt");
2514 if (this->tls_get_addr_opt_
!= NULL
)
2516 if (this->tls_get_addr_
->is_undefined()
2517 || this->tls_get_addr_
->is_from_dynobj())
2519 // Make it seem as if references to __tls_get_addr are
2520 // really to __tls_get_addr_opt, so the latter symbol is
2521 // made dynamic, not the former.
2522 this->tls_get_addr_
->clear_in_reg();
2523 this->tls_get_addr_opt_
->set_in_reg();
2525 // We have a non-dynamic definition for __tls_get_addr.
2526 // Make __tls_get_addr_opt the same, if it does not already have
2527 // a non-dynamic definition.
2528 else if (this->tls_get_addr_opt_
->is_undefined()
2529 || this->tls_get_addr_opt_
->is_from_dynobj())
2531 Sized_symbol
<size
>* from
2532 = static_cast<Sized_symbol
<size
>*>(this->tls_get_addr_
);
2533 Sized_symbol
<size
>* to
2534 = static_cast<Sized_symbol
<size
>*>(this->tls_get_addr_opt_
);
2535 symtab
->clone
<size
>(to
, from
);
2540 // Set up PowerPC target specific relobj.
2542 template<int size
, bool big_endian
>
2544 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
2545 const std::string
& name
,
2546 Input_file
* input_file
,
2547 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
2549 int et
= ehdr
.get_e_type();
2550 // ET_EXEC files are valid input for --just-symbols/-R,
2551 // and we treat them as relocatable objects.
2552 if (et
== elfcpp::ET_REL
2553 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
2555 Powerpc_relobj
<size
, big_endian
>* obj
=
2556 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2560 else if (et
== elfcpp::ET_DYN
)
2562 Powerpc_dynobj
<size
, big_endian
>* obj
=
2563 new Powerpc_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2569 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
2574 template<int size
, bool big_endian
>
2575 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
2578 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
2579 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
2581 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
2582 : Output_data_got
<size
, big_endian
>(),
2583 symtab_(symtab
), layout_(layout
),
2584 header_ent_cnt_(size
== 32 ? 3 : 1),
2585 header_index_(size
== 32 ? 0x2000 : 0)
2588 this->set_addralign(256);
2591 // Override all the Output_data_got methods we use so as to first call
2594 add_global(Symbol
* gsym
, unsigned int got_type
)
2596 this->reserve_ent();
2597 return Output_data_got
<size
, big_endian
>::add_global(gsym
, got_type
);
2601 add_global_plt(Symbol
* gsym
, unsigned int got_type
)
2603 this->reserve_ent();
2604 return Output_data_got
<size
, big_endian
>::add_global_plt(gsym
, got_type
);
2608 add_global_tls(Symbol
* gsym
, unsigned int got_type
)
2609 { return this->add_global_plt(gsym
, got_type
); }
2612 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
2613 Output_data_reloc_generic
* rel_dyn
, unsigned int r_type
)
2615 this->reserve_ent();
2616 Output_data_got
<size
, big_endian
>::
2617 add_global_with_rel(gsym
, got_type
, rel_dyn
, r_type
);
2621 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
2622 Output_data_reloc_generic
* rel_dyn
,
2623 unsigned int r_type_1
, unsigned int r_type_2
)
2625 if (gsym
->has_got_offset(got_type
))
2628 this->reserve_ent(2);
2629 Output_data_got
<size
, big_endian
>::
2630 add_global_pair_with_rel(gsym
, got_type
, rel_dyn
, r_type_1
, r_type_2
);
2634 add_local(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2636 this->reserve_ent();
2637 return Output_data_got
<size
, big_endian
>::add_local(object
, sym_index
,
2642 add_local_plt(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2644 this->reserve_ent();
2645 return Output_data_got
<size
, big_endian
>::add_local_plt(object
, sym_index
,
2650 add_local_tls(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2651 { return this->add_local_plt(object
, sym_index
, got_type
); }
2654 add_local_tls_pair(Relobj
* object
, unsigned int sym_index
,
2655 unsigned int got_type
,
2656 Output_data_reloc_generic
* rel_dyn
,
2657 unsigned int r_type
)
2659 if (object
->local_has_got_offset(sym_index
, got_type
))
2662 this->reserve_ent(2);
2663 Output_data_got
<size
, big_endian
>::
2664 add_local_tls_pair(object
, sym_index
, got_type
, rel_dyn
, r_type
);
2668 add_constant(Valtype constant
)
2670 this->reserve_ent();
2671 return Output_data_got
<size
, big_endian
>::add_constant(constant
);
2675 add_constant_pair(Valtype c1
, Valtype c2
)
2677 this->reserve_ent(2);
2678 return Output_data_got
<size
, big_endian
>::add_constant_pair(c1
, c2
);
2681 // Offset of _GLOBAL_OFFSET_TABLE_.
2685 return this->got_offset(this->header_index_
);
2688 // Offset of base used to access the GOT/TOC.
2689 // The got/toc pointer reg will be set to this value.
2691 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
2694 return this->g_o_t();
2696 return (this->output_section()->address()
2697 + object
->toc_base_offset()
2701 // Ensure our GOT has a header.
2703 set_final_data_size()
2705 if (this->header_ent_cnt_
!= 0)
2706 this->make_header();
2707 Output_data_got
<size
, big_endian
>::set_final_data_size();
2710 // First word of GOT header needs some values that are not
2711 // handled by Output_data_got so poke them in here.
2712 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2714 do_write(Output_file
* of
)
2717 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
2718 val
= this->layout_
->dynamic_section()->address();
2720 val
= this->output_section()->address() + 0x8000;
2721 this->replace_constant(this->header_index_
, val
);
2722 Output_data_got
<size
, big_endian
>::do_write(of
);
2727 reserve_ent(unsigned int cnt
= 1)
2729 if (this->header_ent_cnt_
== 0)
2731 if (this->num_entries() + cnt
> this->header_index_
)
2732 this->make_header();
2738 this->header_ent_cnt_
= 0;
2739 this->header_index_
= this->num_entries();
2742 Output_data_got
<size
, big_endian
>::add_constant(0);
2743 Output_data_got
<size
, big_endian
>::add_constant(0);
2744 Output_data_got
<size
, big_endian
>::add_constant(0);
2746 // Define _GLOBAL_OFFSET_TABLE_ at the header
2747 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2750 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
2751 sym
->set_value(this->g_o_t());
2754 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2755 Symbol_table::PREDEFINED
,
2756 this, this->g_o_t(), 0,
2759 elfcpp::STV_HIDDEN
, 0,
2763 Output_data_got
<size
, big_endian
>::add_constant(0);
2766 // Stashed pointers.
2767 Symbol_table
* symtab_
;
2771 unsigned int header_ent_cnt_
;
2772 // GOT header index.
2773 unsigned int header_index_
;
2776 // Get the GOT section, creating it if necessary.
2778 template<int size
, bool big_endian
>
2779 Output_data_got_powerpc
<size
, big_endian
>*
2780 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
2783 if (this->got_
== NULL
)
2785 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2788 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
2790 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2791 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2792 this->got_
, ORDER_DATA
, false);
2798 // Get the dynamic reloc section, creating it if necessary.
2800 template<int size
, bool big_endian
>
2801 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2802 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
2804 if (this->rela_dyn_
== NULL
)
2806 gold_assert(layout
!= NULL
);
2807 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
2808 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
2809 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
2810 ORDER_DYNAMIC_RELOCS
, false);
2812 return this->rela_dyn_
;
2815 // Similarly, but for ifunc symbols get the one for ifunc.
2817 template<int size
, bool big_endian
>
2818 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2819 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Symbol_table
* symtab
,
2824 return this->rela_dyn_section(layout
);
2826 if (this->iplt_
== NULL
)
2827 this->make_iplt_section(symtab
, layout
);
2828 return this->iplt_
->rel_plt();
2834 // Determine the stub group size. The group size is the absolute
2835 // value of the parameter --stub-group-size. If --stub-group-size
2836 // is passed a negative value, we restrict stubs to be always after
2837 // the stubbed branches.
2838 Stub_control(int32_t size
, bool no_size_errors
, bool multi_os
)
2839 : stub_group_size_(abs(size
)), stubs_always_after_branch_(size
< 0),
2840 suppress_size_errors_(no_size_errors
), multi_os_(multi_os
),
2841 state_(NO_GROUP
), group_size_(0), group_start_addr_(0),
2842 owner_(NULL
), output_section_(NULL
)
2846 // Return true iff input section can be handled by current stub
2849 can_add_to_stub_group(Output_section
* o
,
2850 const Output_section::Input_section
* i
,
2853 const Output_section::Input_section
*
2859 { return output_section_
; }
2862 set_output_and_owner(Output_section
* o
,
2863 const Output_section::Input_section
* i
)
2865 this->output_section_
= o
;
2874 // Adding group sections before the stubs.
2875 FINDING_STUB_SECTION
,
2876 // Adding group sections after the stubs.
2880 uint32_t stub_group_size_
;
2881 bool stubs_always_after_branch_
;
2882 bool suppress_size_errors_
;
2883 // True if a stub group can serve multiple output sections.
2886 // Current max size of group. Starts at stub_group_size_ but is
2887 // reduced to stub_group_size_/1024 on seeing a section with
2888 // external conditional branches.
2889 uint32_t group_size_
;
2890 uint64_t group_start_addr_
;
2891 // owner_ and output_section_ specify the section to which stubs are
2892 // attached. The stubs are placed at the end of this section.
2893 const Output_section::Input_section
* owner_
;
2894 Output_section
* output_section_
;
2897 // Return true iff input section can be handled by current stub
2898 // group. Sections are presented to this function in order,
2899 // so the first section is the head of the group.
2902 Stub_control::can_add_to_stub_group(Output_section
* o
,
2903 const Output_section::Input_section
* i
,
2906 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
2908 uint64_t start_addr
= o
->address();
2911 // .init and .fini sections are pasted together to form a single
2912 // function. We can't be adding stubs in the middle of the function.
2913 this_size
= o
->data_size();
2916 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
2917 this_size
= i
->data_size();
2920 uint64_t end_addr
= start_addr
+ this_size
;
2921 uint32_t group_size
= this->stub_group_size_
;
2923 this->group_size_
= group_size
= group_size
>> 10;
2925 if (this_size
> group_size
&& !this->suppress_size_errors_
)
2926 gold_warning(_("%s:%s exceeds group size"),
2927 i
->relobj()->name().c_str(),
2928 i
->relobj()->section_name(i
->shndx()).c_str());
2930 gold_debug(DEBUG_TARGET
, "maybe add%s %s:%s size=%#llx total=%#llx",
2931 has14
? " 14bit" : "",
2932 i
->relobj()->name().c_str(),
2933 i
->relobj()->section_name(i
->shndx()).c_str(),
2934 (long long) this_size
,
2935 (this->state_
== NO_GROUP
2937 : (long long) end_addr
- this->group_start_addr_
));
2939 if (this->state_
== NO_GROUP
)
2941 // Only here on very first use of Stub_control
2943 this->output_section_
= o
;
2944 this->state_
= FINDING_STUB_SECTION
;
2945 this->group_size_
= group_size
;
2946 this->group_start_addr_
= start_addr
;
2949 else if (!this->multi_os_
&& this->output_section_
!= o
)
2951 else if (this->state_
== HAS_STUB_SECTION
)
2953 // Can we add this section, which is after the stubs, to the
2955 if (end_addr
- this->group_start_addr_
<= this->group_size_
)
2958 else if (this->state_
== FINDING_STUB_SECTION
)
2960 if ((whole_sec
&& this->output_section_
== o
)
2961 || end_addr
- this->group_start_addr_
<= this->group_size_
)
2963 // Stubs are added at the end of "owner_".
2965 this->output_section_
= o
;
2968 // The group before the stubs has reached maximum size.
2969 // Now see about adding sections after the stubs to the
2970 // group. If the current section has a 14-bit branch and
2971 // the group before the stubs exceeds group_size_ (because
2972 // they didn't have 14-bit branches), don't add sections
2973 // after the stubs: The size of stubs for such a large
2974 // group may exceed the reach of a 14-bit branch.
2975 if (!this->stubs_always_after_branch_
2976 && this_size
<= this->group_size_
2977 && start_addr
- this->group_start_addr_
<= this->group_size_
)
2979 gold_debug(DEBUG_TARGET
, "adding after stubs");
2980 this->state_
= HAS_STUB_SECTION
;
2981 this->group_start_addr_
= start_addr
;
2988 gold_debug(DEBUG_TARGET
,
2989 !this->multi_os_
&& this->output_section_
!= o
2990 ? "nope, new output section\n"
2991 : "nope, didn't fit\n");
2993 // The section fails to fit in the current group. Set up a few
2994 // things for the next group. owner_ and output_section_ will be
2995 // set later after we've retrieved those values for the current
2997 this->state_
= FINDING_STUB_SECTION
;
2998 this->group_size_
= group_size
;
2999 this->group_start_addr_
= start_addr
;
3003 // Look over all the input sections, deciding where to place stubs.
3005 template<int size
, bool big_endian
>
3007 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
3009 bool no_size_errors
)
3011 Stub_control
stub_control(this->stub_group_size_
, no_size_errors
,
3012 parameters
->options().stub_group_multi());
3014 // Group input sections and insert stub table
3015 Stub_table_owner
* table_owner
= NULL
;
3016 std::vector
<Stub_table_owner
*> tables
;
3017 Layout::Section_list section_list
;
3018 layout
->get_executable_sections(§ion_list
);
3019 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
3020 for (Layout::Section_list::iterator o
= section_list
.begin();
3021 o
!= section_list
.end();
3024 typedef Output_section::Input_section_list Input_section_list
;
3025 for (Input_section_list::const_iterator i
3026 = (*o
)->input_sections().begin();
3027 i
!= (*o
)->input_sections().end();
3030 if (i
->is_input_section()
3031 || i
->is_relaxed_input_section())
3033 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3034 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
3035 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
3036 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
3038 table_owner
->output_section
= stub_control
.output_section();
3039 table_owner
->owner
= stub_control
.owner();
3040 stub_control
.set_output_and_owner(*o
, &*i
);
3043 if (table_owner
== NULL
)
3045 table_owner
= new Stub_table_owner
;
3046 tables
.push_back(table_owner
);
3048 ppcobj
->set_stub_table(i
->shndx(), tables
.size() - 1);
3052 if (table_owner
!= NULL
)
3054 table_owner
->output_section
= stub_control
.output_section();
3055 table_owner
->owner
= stub_control
.owner();;
3057 for (typename
std::vector
<Stub_table_owner
*>::iterator t
= tables
.begin();
3061 Stub_table
<size
, big_endian
>* stub_table
;
3063 if ((*t
)->owner
->is_input_section())
3064 stub_table
= new Stub_table
<size
, big_endian
>(this,
3065 (*t
)->output_section
,
3067 this->stub_tables_
.size());
3068 else if ((*t
)->owner
->is_relaxed_input_section())
3069 stub_table
= static_cast<Stub_table
<size
, big_endian
>*>(
3070 (*t
)->owner
->relaxed_input_section());
3073 this->stub_tables_
.push_back(stub_table
);
3078 static unsigned long
3079 max_branch_delta (unsigned int r_type
)
3081 if (r_type
== elfcpp::R_POWERPC_REL14
3082 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
3083 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
3085 if (r_type
== elfcpp::R_POWERPC_REL24
3086 || r_type
== elfcpp::R_PPC_PLTREL24
3087 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
3092 // Return whether this branch is going via a plt call stub.
3094 template<int size
, bool big_endian
>
3096 Target_powerpc
<size
, big_endian
>::Branch_info::mark_pltcall(
3097 Powerpc_relobj
<size
, big_endian
>* ppc_object
,
3100 Target_powerpc
* target
,
3101 Symbol_table
* symtab
)
3103 if (this->object_
!= ppc_object
3104 || this->shndx_
!= shndx
3105 || this->offset_
!= offset
)
3108 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
3109 if (sym
!= NULL
&& sym
->is_forwarder())
3110 sym
= symtab
->resolve_forwards(sym
);
3111 if (target
->replace_tls_get_addr(sym
))
3112 sym
= target
->tls_get_addr_opt();
3113 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
3115 ? (gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
3116 && !target
->is_elfv2_localentry0(gsym
))
3117 : (this->object_
->local_has_plt_offset(this->r_sym_
)
3118 && !target
->is_elfv2_localentry0(this->object_
, this->r_sym_
)))
3126 // If this branch needs a plt call stub, or a long branch stub, make one.
3128 template<int size
, bool big_endian
>
3130 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
3131 Stub_table
<size
, big_endian
>* stub_table
,
3132 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
3133 Symbol_table
* symtab
) const
3135 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
3136 Target_powerpc
<size
, big_endian
>* target
=
3137 static_cast<Target_powerpc
<size
, big_endian
>*>(
3138 parameters
->sized_target
<size
, big_endian
>());
3139 if (sym
!= NULL
&& sym
->is_forwarder())
3140 sym
= symtab
->resolve_forwards(sym
);
3141 if (target
->replace_tls_get_addr(sym
))
3142 sym
= target
->tls_get_addr_opt();
3143 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
3147 ? gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
3148 : this->object_
->local_has_plt_offset(this->r_sym_
))
3152 && target
->abiversion() >= 2
3153 && !parameters
->options().output_is_position_independent()
3154 && !is_branch_reloc(this->r_type_
))
3155 target
->glink_section()->add_global_entry(gsym
);
3158 if (stub_table
== NULL
3161 && !parameters
->options().output_is_position_independent()
3162 && !is_branch_reloc(this->r_type_
)))
3163 stub_table
= this->object_
->stub_table(this->shndx_
);
3164 if (stub_table
== NULL
)
3166 // This is a ref from a data section to an ifunc symbol,
3167 // or a non-branch reloc for which we always want to use
3168 // one set of stubs for resolving function addresses.
3169 stub_table
= ifunc_stub_table
;
3171 gold_assert(stub_table
!= NULL
);
3172 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
3173 if (from
!= invalid_address
)
3174 from
+= (this->object_
->output_section(this->shndx_
)->address()
3177 ok
= stub_table
->add_plt_call_entry(from
,
3178 this->object_
, gsym
,
3179 this->r_type_
, this->addend_
,
3182 ok
= stub_table
->add_plt_call_entry(from
,
3183 this->object_
, this->r_sym_
,
3184 this->r_type_
, this->addend_
,
3190 Address max_branch_offset
= max_branch_delta(this->r_type_
);
3191 if (max_branch_offset
== 0)
3193 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
3194 gold_assert(from
!= invalid_address
);
3195 from
+= (this->object_
->output_section(this->shndx_
)->address()
3200 switch (gsym
->source())
3202 case Symbol::FROM_OBJECT
:
3204 Object
* symobj
= gsym
->object();
3205 if (symobj
->is_dynamic()
3206 || symobj
->pluginobj() != NULL
)
3209 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
3210 if (shndx
== elfcpp::SHN_UNDEF
)
3215 case Symbol::IS_UNDEFINED
:
3221 Symbol_table::Compute_final_value_status status
;
3222 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
3223 if (status
!= Symbol_table::CFVS_OK
)
3226 to
+= this->object_
->ppc64_local_entry_offset(gsym
);
3230 const Symbol_value
<size
>* psymval
3231 = this->object_
->local_symbol(this->r_sym_
);
3232 Symbol_value
<size
> symval
;
3233 if (psymval
->is_section_symbol())
3234 symval
.set_is_section_symbol();
3235 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
3236 typename
ObjType::Compute_final_local_value_status status
3237 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
3239 if (status
!= ObjType::CFLV_OK
3240 || !symval
.has_output_value())
3242 to
= symval
.value(this->object_
, 0);
3244 to
+= this->object_
->ppc64_local_entry_offset(this->r_sym_
);
3246 if (!(size
== 32 && this->r_type_
== elfcpp::R_PPC_PLTREL24
))
3247 to
+= this->addend_
;
3248 if (stub_table
== NULL
)
3249 stub_table
= this->object_
->stub_table(this->shndx_
);
3250 if (size
== 64 && target
->abiversion() < 2)
3252 unsigned int dest_shndx
;
3253 if (!target
->symval_for_branch(symtab
, gsym
, this->object_
,
3257 Address delta
= to
- from
;
3258 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
3260 if (stub_table
== NULL
)
3262 gold_warning(_("%s:%s: branch in non-executable section,"
3263 " no long branch stub for you"),
3264 this->object_
->name().c_str(),
3265 this->object_
->section_name(this->shndx_
).c_str());
3268 bool save_res
= (size
== 64
3270 && gsym
->source() == Symbol::IN_OUTPUT_DATA
3271 && gsym
->output_data() == target
->savres_section());
3272 ok
= stub_table
->add_long_branch_entry(this->object_
,
3274 from
, to
, save_res
);
3278 gold_debug(DEBUG_TARGET
,
3279 "branch at %s:%s+%#lx\n"
3280 "can't reach stub attached to %s:%s",
3281 this->object_
->name().c_str(),
3282 this->object_
->section_name(this->shndx_
).c_str(),
3283 (unsigned long) this->offset_
,
3284 stub_table
->relobj()->name().c_str(),
3285 stub_table
->relobj()->section_name(stub_table
->shndx()).c_str());
3290 // Relaxation hook. This is where we do stub generation.
3292 template<int size
, bool big_endian
>
3294 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
3295 const Input_objects
*,
3296 Symbol_table
* symtab
,
3300 unsigned int prev_brlt_size
= 0;
3304 = this->abiversion() < 2 && parameters
->options().plt_thread_safe();
3306 && this->abiversion() < 2
3308 && !parameters
->options().user_set_plt_thread_safe())
3310 static const char* const thread_starter
[] =
3314 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3316 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3317 "mq_notify", "create_timer",
3322 "GOMP_parallel_start",
3323 "GOMP_parallel_loop_static",
3324 "GOMP_parallel_loop_static_start",
3325 "GOMP_parallel_loop_dynamic",
3326 "GOMP_parallel_loop_dynamic_start",
3327 "GOMP_parallel_loop_guided",
3328 "GOMP_parallel_loop_guided_start",
3329 "GOMP_parallel_loop_runtime",
3330 "GOMP_parallel_loop_runtime_start",
3331 "GOMP_parallel_sections",
3332 "GOMP_parallel_sections_start",
3337 if (parameters
->options().shared())
3341 for (unsigned int i
= 0;
3342 i
< sizeof(thread_starter
) / sizeof(thread_starter
[0]);
3345 Symbol
* sym
= symtab
->lookup(thread_starter
[i
], NULL
);
3346 thread_safe
= (sym
!= NULL
3348 && sym
->in_real_elf());
3354 this->plt_thread_safe_
= thread_safe
;
3359 this->stub_group_size_
= parameters
->options().stub_group_size();
3360 bool no_size_errors
= true;
3361 if (this->stub_group_size_
== 1)
3362 this->stub_group_size_
= 0x1c00000;
3363 else if (this->stub_group_size_
== -1)
3364 this->stub_group_size_
= -0x1e00000;
3366 no_size_errors
= false;
3367 this->group_sections(layout
, task
, no_size_errors
);
3369 else if (this->relax_failed_
&& this->relax_fail_count_
< 3)
3371 this->branch_lookup_table_
.clear();
3372 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3373 p
!= this->stub_tables_
.end();
3376 (*p
)->clear_stubs(true);
3378 this->stub_tables_
.clear();
3379 this->stub_group_size_
= this->stub_group_size_
/ 4 * 3;
3380 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3381 program_name
, this->stub_group_size_
);
3382 this->group_sections(layout
, task
, true);
3385 // We need address of stub tables valid for make_stub.
3386 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3387 p
!= this->stub_tables_
.end();
3390 const Powerpc_relobj
<size
, big_endian
>* object
3391 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
3392 Address off
= object
->get_output_section_offset((*p
)->shndx());
3393 gold_assert(off
!= invalid_address
);
3394 Output_section
* os
= (*p
)->output_section();
3395 (*p
)->set_address_and_size(os
, off
);
3400 // Clear plt call stubs, long branch stubs and branch lookup table.
3401 prev_brlt_size
= this->branch_lookup_table_
.size();
3402 this->branch_lookup_table_
.clear();
3403 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3404 p
!= this->stub_tables_
.end();
3407 (*p
)->clear_stubs(false);
3411 // Build all the stubs.
3412 this->relax_failed_
= false;
3413 Stub_table
<size
, big_endian
>* ifunc_stub_table
3414 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
3415 Stub_table
<size
, big_endian
>* one_stub_table
3416 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
3417 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
3418 b
!= this->branch_info_
.end();
3421 if (!b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
)
3422 && !this->relax_failed_
)
3424 this->relax_failed_
= true;
3425 this->relax_fail_count_
++;
3426 if (this->relax_fail_count_
< 3)
3431 // Did anything change size?
3432 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
3433 bool again
= num_huge_branches
!= prev_brlt_size
;
3434 if (size
== 64 && num_huge_branches
!= 0)
3435 this->make_brlt_section(layout
);
3436 if (size
== 64 && again
)
3437 this->brlt_section_
->set_current_size(num_huge_branches
);
3439 for (typename
Stub_tables::reverse_iterator p
= this->stub_tables_
.rbegin();
3440 p
!= this->stub_tables_
.rend();
3442 (*p
)->remove_eh_frame(layout
);
3444 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3445 p
!= this->stub_tables_
.end();
3447 (*p
)->add_eh_frame(layout
);
3449 typedef Unordered_set
<Output_section
*> Output_sections
;
3450 Output_sections os_need_update
;
3451 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3452 p
!= this->stub_tables_
.end();
3455 if ((*p
)->size_update())
3458 os_need_update
.insert((*p
)->output_section());
3462 // Set output section offsets for all input sections in an output
3463 // section that just changed size. Anything past the stubs will
3465 for (typename
Output_sections::iterator p
= os_need_update
.begin();
3466 p
!= os_need_update
.end();
3469 Output_section
* os
= *p
;
3471 typedef Output_section::Input_section_list Input_section_list
;
3472 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
3473 i
!= os
->input_sections().end();
3476 off
= align_address(off
, i
->addralign());
3477 if (i
->is_input_section() || i
->is_relaxed_input_section())
3478 i
->relobj()->set_section_offset(i
->shndx(), off
);
3479 if (i
->is_relaxed_input_section())
3481 Stub_table
<size
, big_endian
>* stub_table
3482 = static_cast<Stub_table
<size
, big_endian
>*>(
3483 i
->relaxed_input_section());
3484 Address stub_table_size
= stub_table
->set_address_and_size(os
, off
);
3485 off
+= stub_table_size
;
3486 // After a few iterations, set current stub table size
3487 // as min size threshold, so later stub tables can only
3490 stub_table
->set_min_size_threshold(stub_table_size
);
3493 off
+= i
->data_size();
3495 // If .branch_lt is part of this output section, then we have
3496 // just done the offset adjustment.
3497 os
->clear_section_offsets_need_adjustment();
3502 && num_huge_branches
!= 0
3503 && parameters
->options().output_is_position_independent())
3505 // Fill in the BRLT relocs.
3506 this->brlt_section_
->reset_brlt_sizes();
3507 for (typename
Branch_lookup_table::const_iterator p
3508 = this->branch_lookup_table_
.begin();
3509 p
!= this->branch_lookup_table_
.end();
3512 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
3514 this->brlt_section_
->finalize_brlt_sizes();
3518 && (parameters
->options().user_set_emit_stub_syms()
3519 ? parameters
->options().emit_stub_syms()
3521 || parameters
->options().output_is_position_independent()
3522 || parameters
->options().emit_relocs())))
3524 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3525 p
!= this->stub_tables_
.end();
3527 (*p
)->define_stub_syms(symtab
);
3529 if (this->glink_
!= NULL
)
3531 int stub_size
= this->glink_
->pltresolve_size();
3532 Address value
= -stub_size
;
3538 this->define_local(symtab
, "__glink_PLTresolve",
3539 this->glink_
, value
, stub_size
);
3542 this->define_local(symtab
, "__glink", this->glink_
, 0, 0);
3549 template<int size
, bool big_endian
>
3551 Target_powerpc
<size
, big_endian
>::do_plt_fde_location(const Output_data
* plt
,
3552 unsigned char* oview
,
3556 uint64_t address
= plt
->address();
3557 off_t len
= plt
->data_size();
3559 if (plt
== this->glink_
)
3561 // See Output_data_glink::do_write() for glink contents.
3564 gold_assert(parameters
->doing_static_link());
3565 // Static linking may need stubs, to support ifunc and long
3566 // branches. We need to create an output section for
3567 // .eh_frame early in the link process, to have a place to
3568 // attach stub .eh_frame info. We also need to have
3569 // registered a CIE that matches the stub CIE. Both of
3570 // these requirements are satisfied by creating an FDE and
3571 // CIE for .glink, even though static linking will leave
3572 // .glink zero length.
3573 // ??? Hopefully generating an FDE with a zero address range
3574 // won't confuse anything that consumes .eh_frame info.
3576 else if (size
== 64)
3578 // There is one word before __glink_PLTresolve
3582 else if (parameters
->options().output_is_position_independent())
3584 // There are two FDEs for a position independent glink.
3585 // The first covers the branch table, the second
3586 // __glink_PLTresolve at the end of glink.
3587 off_t resolve_size
= this->glink_
->pltresolve_size();
3588 if (oview
[9] == elfcpp::DW_CFA_nop
)
3589 len
-= resolve_size
;
3592 address
+= len
- resolve_size
;
3599 // Must be a stub table.
3600 const Stub_table
<size
, big_endian
>* stub_table
3601 = static_cast<const Stub_table
<size
, big_endian
>*>(plt
);
3602 uint64_t stub_address
= stub_table
->stub_address();
3603 len
-= stub_address
- address
;
3604 address
= stub_address
;
3607 *paddress
= address
;
3611 // A class to handle the PLT data.
3613 template<int size
, bool big_endian
>
3614 class Output_data_plt_powerpc
: public Output_section_data_build
3617 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3618 size
, big_endian
> Reloc_section
;
3620 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3621 Reloc_section
* plt_rel
,
3623 : Output_section_data_build(size
== 32 ? 4 : 8),
3629 // Add an entry to the PLT.
3634 add_ifunc_entry(Symbol
*);
3637 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
3639 // Return the .rela.plt section data.
3646 // Return the number of PLT entries.
3650 if (this->current_data_size() == 0)
3652 return ((this->current_data_size() - this->first_plt_entry_offset())
3653 / this->plt_entry_size());
3658 do_adjust_output_section(Output_section
* os
)
3663 // Write to a map file.
3665 do_print_to_mapfile(Mapfile
* mapfile
) const
3666 { mapfile
->print_output_data(this, this->name_
); }
3669 // Return the offset of the first non-reserved PLT entry.
3671 first_plt_entry_offset() const
3673 // IPLT has no reserved entry.
3674 if (this->name_
[3] == 'I')
3676 return this->targ_
->first_plt_entry_offset();
3679 // Return the size of each PLT entry.
3681 plt_entry_size() const
3683 return this->targ_
->plt_entry_size();
3686 // Write out the PLT data.
3688 do_write(Output_file
*);
3690 // The reloc section.
3691 Reloc_section
* rel_
;
3692 // Allows access to .glink for do_write.
3693 Target_powerpc
<size
, big_endian
>* targ_
;
3694 // What to report in map file.
3698 // Add an entry to the PLT.
3700 template<int size
, bool big_endian
>
3702 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
3704 if (!gsym
->has_plt_offset())
3706 section_size_type off
= this->current_data_size();
3708 off
+= this->first_plt_entry_offset();
3709 gsym
->set_plt_offset(off
);
3710 gsym
->set_needs_dynsym_entry();
3711 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
3712 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
3713 off
+= this->plt_entry_size();
3714 this->set_current_data_size(off
);
3718 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3720 template<int size
, bool big_endian
>
3722 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
3724 if (!gsym
->has_plt_offset())
3726 section_size_type off
= this->current_data_size();
3727 gsym
->set_plt_offset(off
);
3728 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3729 if (size
== 64 && this->targ_
->abiversion() < 2)
3730 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3731 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
3732 off
+= this->plt_entry_size();
3733 this->set_current_data_size(off
);
3737 // Add an entry for a local ifunc symbol to the IPLT.
3739 template<int size
, bool big_endian
>
3741 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
3742 Sized_relobj_file
<size
, big_endian
>* relobj
,
3743 unsigned int local_sym_index
)
3745 if (!relobj
->local_has_plt_offset(local_sym_index
))
3747 section_size_type off
= this->current_data_size();
3748 relobj
->set_local_plt_offset(local_sym_index
, off
);
3749 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3750 if (size
== 64 && this->targ_
->abiversion() < 2)
3751 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3752 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
3754 off
+= this->plt_entry_size();
3755 this->set_current_data_size(off
);
3759 static const uint32_t add_0_11_11
= 0x7c0b5a14;
3760 static const uint32_t add_2_2_11
= 0x7c425a14;
3761 static const uint32_t add_2_2_12
= 0x7c426214;
3762 static const uint32_t add_3_3_2
= 0x7c631214;
3763 static const uint32_t add_3_3_13
= 0x7c636a14;
3764 static const uint32_t add_3_12_2
= 0x7c6c1214;
3765 static const uint32_t add_3_12_13
= 0x7c6c6a14;
3766 static const uint32_t add_11_0_11
= 0x7d605a14;
3767 static const uint32_t add_11_2_11
= 0x7d625a14;
3768 static const uint32_t add_11_11_2
= 0x7d6b1214;
3769 static const uint32_t addi_0_12
= 0x380c0000;
3770 static const uint32_t addi_2_2
= 0x38420000;
3771 static const uint32_t addi_3_3
= 0x38630000;
3772 static const uint32_t addi_11_11
= 0x396b0000;
3773 static const uint32_t addi_12_1
= 0x39810000;
3774 static const uint32_t addi_12_12
= 0x398c0000;
3775 static const uint32_t addis_0_2
= 0x3c020000;
3776 static const uint32_t addis_0_13
= 0x3c0d0000;
3777 static const uint32_t addis_2_12
= 0x3c4c0000;
3778 static const uint32_t addis_11_2
= 0x3d620000;
3779 static const uint32_t addis_11_11
= 0x3d6b0000;
3780 static const uint32_t addis_11_30
= 0x3d7e0000;
3781 static const uint32_t addis_12_1
= 0x3d810000;
3782 static const uint32_t addis_12_2
= 0x3d820000;
3783 static const uint32_t addis_12_12
= 0x3d8c0000;
3784 static const uint32_t b
= 0x48000000;
3785 static const uint32_t bcl_20_31
= 0x429f0005;
3786 static const uint32_t bctr
= 0x4e800420;
3787 static const uint32_t bctrl
= 0x4e800421;
3788 static const uint32_t beqlr
= 0x4d820020;
3789 static const uint32_t blr
= 0x4e800020;
3790 static const uint32_t bnectr_p4
= 0x4ce20420;
3791 static const uint32_t cmpld_7_12_0
= 0x7fac0040;
3792 static const uint32_t cmpldi_2_0
= 0x28220000;
3793 static const uint32_t cmpdi_11_0
= 0x2c2b0000;
3794 static const uint32_t cmpwi_11_0
= 0x2c0b0000;
3795 static const uint32_t cror_15_15_15
= 0x4def7b82;
3796 static const uint32_t cror_31_31_31
= 0x4ffffb82;
3797 static const uint32_t ld_0_1
= 0xe8010000;
3798 static const uint32_t ld_0_12
= 0xe80c0000;
3799 static const uint32_t ld_2_1
= 0xe8410000;
3800 static const uint32_t ld_2_2
= 0xe8420000;
3801 static const uint32_t ld_2_11
= 0xe84b0000;
3802 static const uint32_t ld_2_12
= 0xe84c0000;
3803 static const uint32_t ld_11_1
= 0xe9610000;
3804 static const uint32_t ld_11_2
= 0xe9620000;
3805 static const uint32_t ld_11_3
= 0xe9630000;
3806 static const uint32_t ld_11_11
= 0xe96b0000;
3807 static const uint32_t ld_12_2
= 0xe9820000;
3808 static const uint32_t ld_12_3
= 0xe9830000;
3809 static const uint32_t ld_12_11
= 0xe98b0000;
3810 static const uint32_t ld_12_12
= 0xe98c0000;
3811 static const uint32_t lfd_0_1
= 0xc8010000;
3812 static const uint32_t li_0_0
= 0x38000000;
3813 static const uint32_t li_12_0
= 0x39800000;
3814 static const uint32_t lis_0
= 0x3c000000;
3815 static const uint32_t lis_2
= 0x3c400000;
3816 static const uint32_t lis_11
= 0x3d600000;
3817 static const uint32_t lis_12
= 0x3d800000;
3818 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
3819 static const uint32_t lwz_0_12
= 0x800c0000;
3820 static const uint32_t lwz_11_3
= 0x81630000;
3821 static const uint32_t lwz_11_11
= 0x816b0000;
3822 static const uint32_t lwz_11_30
= 0x817e0000;
3823 static const uint32_t lwz_12_3
= 0x81830000;
3824 static const uint32_t lwz_12_12
= 0x818c0000;
3825 static const uint32_t lwzu_0_12
= 0x840c0000;
3826 static const uint32_t mflr_0
= 0x7c0802a6;
3827 static const uint32_t mflr_11
= 0x7d6802a6;
3828 static const uint32_t mflr_12
= 0x7d8802a6;
3829 static const uint32_t mr_0_3
= 0x7c601b78;
3830 static const uint32_t mr_3_0
= 0x7c030378;
3831 static const uint32_t mtctr_0
= 0x7c0903a6;
3832 static const uint32_t mtctr_11
= 0x7d6903a6;
3833 static const uint32_t mtctr_12
= 0x7d8903a6;
3834 static const uint32_t mtlr_0
= 0x7c0803a6;
3835 static const uint32_t mtlr_11
= 0x7d6803a6;
3836 static const uint32_t mtlr_12
= 0x7d8803a6;
3837 static const uint32_t nop
= 0x60000000;
3838 static const uint32_t ori_0_0_0
= 0x60000000;
3839 static const uint32_t srdi_0_0_2
= 0x7800f082;
3840 static const uint32_t std_0_1
= 0xf8010000;
3841 static const uint32_t std_0_12
= 0xf80c0000;
3842 static const uint32_t std_2_1
= 0xf8410000;
3843 static const uint32_t std_11_1
= 0xf9610000;
3844 static const uint32_t stfd_0_1
= 0xd8010000;
3845 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
3846 static const uint32_t sub_11_11_12
= 0x7d6c5850;
3847 static const uint32_t sub_12_12_11
= 0x7d8b6050;
3848 static const uint32_t xor_2_12_12
= 0x7d826278;
3849 static const uint32_t xor_11_12_12
= 0x7d8b6278;
3851 // Write out the PLT.
3853 template<int size
, bool big_endian
>
3855 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3857 if (size
== 32 && this->name_
[3] != 'I')
3859 const section_size_type offset
= this->offset();
3860 const section_size_type oview_size
3861 = convert_to_section_size_type(this->data_size());
3862 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3863 unsigned char* pov
= oview
;
3864 unsigned char* endpov
= oview
+ oview_size
;
3866 // The address of the .glink branch table
3867 const Output_data_glink
<size
, big_endian
>* glink
3868 = this->targ_
->glink_section();
3869 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
3871 while (pov
< endpov
)
3873 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
3878 of
->write_output_view(offset
, oview_size
, oview
);
3882 // Create the PLT section.
3884 template<int size
, bool big_endian
>
3886 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
3889 if (this->plt_
== NULL
)
3891 if (this->got_
== NULL
)
3892 this->got_section(symtab
, layout
);
3894 if (this->glink_
== NULL
)
3895 make_glink_section(layout
);
3897 // Ensure that .rela.dyn always appears before .rela.plt This is
3898 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3899 // needs to include .rela.plt in its range.
3900 this->rela_dyn_section(layout
);
3902 Reloc_section
* plt_rel
= new Reloc_section(false);
3903 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
3904 elfcpp::SHF_ALLOC
, plt_rel
,
3905 ORDER_DYNAMIC_PLT_RELOCS
, false);
3907 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
3909 layout
->add_output_section_data(".plt",
3911 ? elfcpp::SHT_PROGBITS
3912 : elfcpp::SHT_NOBITS
),
3913 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3920 Output_section
* rela_plt_os
= plt_rel
->output_section();
3921 rela_plt_os
->set_info_section(this->plt_
->output_section());
3925 // Create the IPLT section.
3927 template<int size
, bool big_endian
>
3929 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
3932 if (this->iplt_
== NULL
)
3934 this->make_plt_section(symtab
, layout
);
3936 Reloc_section
* iplt_rel
= new Reloc_section(false);
3937 if (this->rela_dyn_
->output_section())
3938 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
3940 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
3942 if (this->plt_
->output_section())
3943 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
3947 // A section for huge long branch addresses, similar to plt section.
3949 template<int size
, bool big_endian
>
3950 class Output_data_brlt_powerpc
: public Output_section_data_build
3953 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3954 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3955 size
, big_endian
> Reloc_section
;
3957 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3958 Reloc_section
* brlt_rel
)
3959 : Output_section_data_build(size
== 32 ? 4 : 8),
3967 this->reset_data_size();
3968 this->rel_
->reset_data_size();
3972 finalize_brlt_sizes()
3974 this->finalize_data_size();
3975 this->rel_
->finalize_data_size();
3978 // Add a reloc for an entry in the BRLT.
3980 add_reloc(Address to
, unsigned int off
)
3981 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
3983 // Update section and reloc section size.
3985 set_current_size(unsigned int num_branches
)
3987 this->reset_address_and_file_offset();
3988 this->set_current_data_size(num_branches
* 16);
3989 this->finalize_data_size();
3990 Output_section
* os
= this->output_section();
3991 os
->set_section_offsets_need_adjustment();
3992 if (this->rel_
!= NULL
)
3994 const unsigned int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
3995 this->rel_
->reset_address_and_file_offset();
3996 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
3997 this->rel_
->finalize_data_size();
3998 Output_section
* os
= this->rel_
->output_section();
3999 os
->set_section_offsets_need_adjustment();
4005 do_adjust_output_section(Output_section
* os
)
4010 // Write to a map file.
4012 do_print_to_mapfile(Mapfile
* mapfile
) const
4013 { mapfile
->print_output_data(this, "** BRLT"); }
4016 // Write out the BRLT data.
4018 do_write(Output_file
*);
4020 // The reloc section.
4021 Reloc_section
* rel_
;
4022 Target_powerpc
<size
, big_endian
>* targ_
;
4025 // Make the branch lookup table section.
4027 template<int size
, bool big_endian
>
4029 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
4031 if (size
== 64 && this->brlt_section_
== NULL
)
4033 Reloc_section
* brlt_rel
= NULL
;
4034 bool is_pic
= parameters
->options().output_is_position_independent();
4037 // When PIC we can't fill in .branch_lt but must initialise at
4038 // runtime via dynamic relocations.
4039 this->rela_dyn_section(layout
);
4040 brlt_rel
= new Reloc_section(false);
4041 if (this->rela_dyn_
->output_section())
4042 this->rela_dyn_
->output_section()
4043 ->add_output_section_data(brlt_rel
);
4046 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
4047 if (this->plt_
&& is_pic
&& this->plt_
->output_section())
4048 this->plt_
->output_section()
4049 ->add_output_section_data(this->brlt_section_
);
4051 layout
->add_output_section_data(".branch_lt",
4052 elfcpp::SHT_PROGBITS
,
4053 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
4054 this->brlt_section_
,
4060 // Write out .branch_lt when non-PIC.
4062 template<int size
, bool big_endian
>
4064 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
4066 if (size
== 64 && !parameters
->options().output_is_position_independent())
4068 const section_size_type offset
= this->offset();
4069 const section_size_type oview_size
4070 = convert_to_section_size_type(this->data_size());
4071 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4073 this->targ_
->write_branch_lookup_table(oview
);
4074 of
->write_output_view(offset
, oview_size
, oview
);
4078 static inline uint32_t
4084 static inline uint32_t
4090 static inline uint32_t
4093 return hi(a
+ 0x8000);
4099 static const unsigned char eh_frame_cie
[12];
4103 const unsigned char Eh_cie
<size
>::eh_frame_cie
[] =
4106 'z', 'R', 0, // Augmentation string.
4107 4, // Code alignment.
4108 0x80 - size
/ 8 , // Data alignment.
4110 1, // Augmentation size.
4111 (elfcpp::DW_EH_PE_pcrel
4112 | elfcpp::DW_EH_PE_sdata4
), // FDE encoding.
4113 elfcpp::DW_CFA_def_cfa
, 1, 0 // def_cfa: r1 offset 0.
4116 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4117 static const unsigned char glink_eh_frame_fde_64v1
[] =
4119 0, 0, 0, 0, // Replaced with offset to .glink.
4120 0, 0, 0, 0, // Replaced with size of .glink.
4121 0, // Augmentation size.
4122 elfcpp::DW_CFA_advance_loc
+ 1,
4123 elfcpp::DW_CFA_register
, 65, 12,
4124 elfcpp::DW_CFA_advance_loc
+ 5,
4125 elfcpp::DW_CFA_restore_extended
, 65
4128 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4129 static const unsigned char glink_eh_frame_fde_64v2
[] =
4131 0, 0, 0, 0, // Replaced with offset to .glink.
4132 0, 0, 0, 0, // Replaced with size of .glink.
4133 0, // Augmentation size.
4134 elfcpp::DW_CFA_advance_loc
+ 1,
4135 elfcpp::DW_CFA_register
, 65, 0,
4136 elfcpp::DW_CFA_advance_loc
+ 7,
4137 elfcpp::DW_CFA_restore_extended
, 65
4140 // Describe __glink_PLTresolve use of LR, 32-bit version.
4141 static const unsigned char glink_eh_frame_fde_32
[] =
4143 0, 0, 0, 0, // Replaced with offset to .glink.
4144 0, 0, 0, 0, // Replaced with size of .glink.
4145 0, // Augmentation size.
4146 elfcpp::DW_CFA_advance_loc
+ 2,
4147 elfcpp::DW_CFA_register
, 65, 0,
4148 elfcpp::DW_CFA_advance_loc
+ 4,
4149 elfcpp::DW_CFA_restore_extended
, 65
4152 static const unsigned char default_fde
[] =
4154 0, 0, 0, 0, // Replaced with offset to stubs.
4155 0, 0, 0, 0, // Replaced with size of stubs.
4156 0, // Augmentation size.
4157 elfcpp::DW_CFA_nop
, // Pad.
4162 template<bool big_endian
>
4164 write_insn(unsigned char* p
, uint32_t v
)
4166 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
4170 static inline unsigned int
4173 if (!parameters
->options().user_set_plt_align())
4174 return size
== 64 ? 32 : 8;
4175 return 1 << parameters
->options().plt_align();
4178 // Stub_table holds information about plt and long branch stubs.
4179 // Stubs are built in an area following some input section determined
4180 // by group_sections(). This input section is converted to a relaxed
4181 // input section allowing it to be resized to accommodate the stubs
4183 template<int size
, bool big_endian
>
4184 class Stub_table
: public Output_relaxed_input_section
4189 Plt_stub_ent(unsigned int off
, unsigned int indx
)
4190 : off_(off
), indx_(indx
), r2save_(0), localentry0_(0)
4194 unsigned int indx_
: 30;
4195 unsigned int r2save_
: 1;
4196 unsigned int localentry0_
: 1;
4198 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4199 static const Address invalid_address
= static_cast<Address
>(0) - 1;
4201 Stub_table(Target_powerpc
<size
, big_endian
>* targ
,
4202 Output_section
* output_section
,
4203 const Output_section::Input_section
* owner
,
4205 : Output_relaxed_input_section(owner
->relobj(), owner
->shndx(),
4207 ->section_addralign(owner
->shndx())),
4208 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
4209 orig_data_size_(owner
->current_data_size()),
4210 plt_size_(0), last_plt_size_(0),
4211 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4212 need_save_res_(false), uniq_(id
), tls_get_addr_opt_bctrl_(-1u),
4215 this->set_output_section(output_section
);
4217 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
4218 new_relaxed
.push_back(this);
4219 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
4222 // Add a plt call stub.
4224 add_plt_call_entry(Address
,
4225 const Sized_relobj_file
<size
, big_endian
>*,
4232 add_plt_call_entry(Address
,
4233 const Sized_relobj_file
<size
, big_endian
>*,
4239 // Find a given plt call stub.
4241 find_plt_call_entry(const Symbol
*) const;
4244 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4245 unsigned int) const;
4248 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4254 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4259 // Add a long branch stub.
4261 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
4262 unsigned int, Address
, Address
, bool);
4265 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
4269 can_reach_stub(Address from
, unsigned int off
, unsigned int r_type
)
4271 Address max_branch_offset
= max_branch_delta(r_type
);
4272 if (max_branch_offset
== 0)
4274 gold_assert(from
!= invalid_address
);
4275 Address loc
= off
+ this->stub_address();
4276 return loc
- from
+ max_branch_offset
< 2 * max_branch_offset
;
4280 clear_stubs(bool all
)
4282 this->plt_call_stubs_
.clear();
4283 this->plt_size_
= 0;
4284 this->long_branch_stubs_
.clear();
4285 this->branch_size_
= 0;
4286 this->need_save_res_
= false;
4289 this->last_plt_size_
= 0;
4290 this->last_branch_size_
= 0;
4295 set_address_and_size(const Output_section
* os
, Address off
)
4297 Address start_off
= off
;
4298 off
+= this->orig_data_size_
;
4299 Address my_size
= this->plt_size_
+ this->branch_size_
;
4300 if (this->need_save_res_
)
4301 my_size
+= this->targ_
->savres_section()->data_size();
4303 off
= align_address(off
, this->stub_align());
4304 // Include original section size and alignment padding in size
4305 my_size
+= off
- start_off
;
4306 // Ensure new size is always larger than min size
4307 // threshold. Alignment requirement is included in "my_size", so
4308 // increase "my_size" does not invalidate alignment.
4309 if (my_size
< this->min_size_threshold_
)
4310 my_size
= this->min_size_threshold_
;
4311 this->reset_address_and_file_offset();
4312 this->set_current_data_size(my_size
);
4313 this->set_address_and_file_offset(os
->address() + start_off
,
4314 os
->offset() + start_off
);
4319 stub_address() const
4321 return align_address(this->address() + this->orig_data_size_
,
4322 this->stub_align());
4328 return align_address(this->offset() + this->orig_data_size_
,
4329 this->stub_align());
4334 { return this->plt_size_
; }
4337 set_min_size_threshold(Address min_size
)
4338 { this->min_size_threshold_
= min_size
; }
4341 define_stub_syms(Symbol_table
*);
4346 Output_section
* os
= this->output_section();
4347 if (os
->addralign() < this->stub_align())
4349 os
->set_addralign(this->stub_align());
4350 // FIXME: get rid of the insane checkpointing.
4351 // We can't increase alignment of the input section to which
4352 // stubs are attached; The input section may be .init which
4353 // is pasted together with other .init sections to form a
4354 // function. Aligning might insert zero padding resulting in
4355 // sigill. However we do need to increase alignment of the
4356 // output section so that the align_address() on offset in
4357 // set_address_and_size() adds the same padding as the
4358 // align_address() on address in stub_address().
4359 // What's more, we need this alignment for the layout done in
4360 // relaxation_loop_body() so that the output section starts at
4361 // a suitably aligned address.
4362 os
->checkpoint_set_addralign(this->stub_align());
4364 if (this->last_plt_size_
!= this->plt_size_
4365 || this->last_branch_size_
!= this->branch_size_
)
4367 this->last_plt_size_
= this->plt_size_
;
4368 this->last_branch_size_
= this->branch_size_
;
4374 // Generate a suitable FDE to describe code in this stub group.
4378 // Add .eh_frame info for this stub section.
4380 add_eh_frame(Layout
* layout
);
4382 // Remove .eh_frame info for this stub section.
4384 remove_eh_frame(Layout
* layout
);
4386 Target_powerpc
<size
, big_endian
>*
4392 class Plt_stub_key_hash
;
4393 typedef Unordered_map
<Plt_stub_key
, Plt_stub_ent
,
4394 Plt_stub_key_hash
> Plt_stub_entries
;
4395 class Branch_stub_ent
;
4396 class Branch_stub_ent_hash
;
4397 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
4398 Branch_stub_ent_hash
> Branch_stub_entries
;
4400 // Alignment of stub section.
4404 unsigned int min_align
= size
== 64 ? 32 : 16;
4405 unsigned int user_align
= 1 << parameters
->options().plt_align();
4406 return std::max(user_align
, min_align
);
4409 // Return the plt offset for the given call stub.
4411 plt_off(typename
Plt_stub_entries::const_iterator p
, bool* is_iplt
) const
4413 const Symbol
* gsym
= p
->first
.sym_
;
4416 *is_iplt
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
4417 && gsym
->can_use_relative_reloc(false));
4418 return gsym
->plt_offset();
4423 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
4424 unsigned int local_sym_index
= p
->first
.locsym_
;
4425 return relobj
->local_plt_offset(local_sym_index
);
4429 // Size of a given plt call stub.
4431 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
4435 const Symbol
* gsym
= p
->first
.sym_
;
4437 + (this->targ_
->is_tls_get_addr_opt(gsym
) ? 8 * 4 : 0));
4441 Address plt_addr
= this->plt_off(p
, &is_iplt
);
4443 plt_addr
+= this->targ_
->iplt_section()->address();
4445 plt_addr
+= this->targ_
->plt_section()->address();
4446 Address got_addr
= this->targ_
->got_section()->output_section()->address();
4447 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4448 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
4449 got_addr
+= ppcobj
->toc_base_offset();
4450 Address off
= plt_addr
- got_addr
;
4451 unsigned int bytes
= 4 * 4 + 4 * (ha(off
) != 0);
4452 const Symbol
* gsym
= p
->first
.sym_
;
4453 if (this->targ_
->is_tls_get_addr_opt(gsym
))
4455 if (this->targ_
->abiversion() < 2)
4457 bool static_chain
= parameters
->options().plt_static_chain();
4458 bool thread_safe
= this->targ_
->plt_thread_safe();
4462 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
4468 plt_call_align(unsigned int bytes
) const
4470 unsigned int align
= param_plt_align
<size
>();
4471 return (bytes
+ align
- 1) & -align
;
4474 // Return long branch stub size.
4476 branch_stub_size(typename
Branch_stub_entries::const_iterator p
)
4478 Address loc
= this->stub_address() + this->last_plt_size_
+ p
->second
;
4479 if (p
->first
.dest_
- loc
+ (1 << 25) < 2 << 25)
4481 unsigned int bytes
= 16;
4482 if (size
== 32 && parameters
->options().output_is_position_independent())
4489 do_write(Output_file
*);
4491 // Plt call stub keys.
4495 Plt_stub_key(const Symbol
* sym
)
4496 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4499 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4500 unsigned int locsym_index
)
4501 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4504 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4506 unsigned int r_type
,
4508 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4511 this->addend_
= addend
;
4512 else if (parameters
->options().output_is_position_independent()
4513 && r_type
== elfcpp::R_PPC_PLTREL24
)
4515 this->addend_
= addend
;
4516 if (this->addend_
>= 32768)
4517 this->object_
= object
;
4521 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4522 unsigned int locsym_index
,
4523 unsigned int r_type
,
4525 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4528 this->addend_
= addend
;
4529 else if (parameters
->options().output_is_position_independent()
4530 && r_type
== elfcpp::R_PPC_PLTREL24
)
4531 this->addend_
= addend
;
4534 bool operator==(const Plt_stub_key
& that
) const
4536 return (this->sym_
== that
.sym_
4537 && this->object_
== that
.object_
4538 && this->addend_
== that
.addend_
4539 && this->locsym_
== that
.locsym_
);
4543 const Sized_relobj_file
<size
, big_endian
>* object_
;
4544 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
4545 unsigned int locsym_
;
4548 class Plt_stub_key_hash
4551 size_t operator()(const Plt_stub_key
& ent
) const
4553 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
4554 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
4560 // Long branch stub keys.
4561 class Branch_stub_ent
4564 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
,
4565 Address to
, bool save_res
)
4566 : dest_(to
), toc_base_off_(0), save_res_(save_res
)
4569 toc_base_off_
= obj
->toc_base_offset();
4572 bool operator==(const Branch_stub_ent
& that
) const
4574 return (this->dest_
== that
.dest_
4576 || this->toc_base_off_
== that
.toc_base_off_
));
4580 unsigned int toc_base_off_
;
4584 class Branch_stub_ent_hash
4587 size_t operator()(const Branch_stub_ent
& ent
) const
4588 { return ent
.dest_
^ ent
.toc_base_off_
; }
4591 // In a sane world this would be a global.
4592 Target_powerpc
<size
, big_endian
>* targ_
;
4593 // Map sym/object/addend to stub offset.
4594 Plt_stub_entries plt_call_stubs_
;
4595 // Map destination address to stub offset.
4596 Branch_stub_entries long_branch_stubs_
;
4597 // size of input section
4598 section_size_type orig_data_size_
;
4600 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
4601 // Some rare cases cause (PR/20529) fluctuation in stub table
4602 // size, which leads to an endless relax loop. This is to be fixed
4603 // by, after the first few iterations, allowing only increase of
4604 // stub table size. This variable sets the minimal possible size of
4605 // a stub table, it is zero for the first few iterations, then
4606 // increases monotonically.
4607 Address min_size_threshold_
;
4608 // Set if this stub group needs a copy of out-of-line register
4609 // save/restore functions.
4610 bool need_save_res_
;
4611 // Per stub table unique identifier.
4613 // The bctrl in the __tls_get_addr_opt stub, if present.
4614 unsigned int tls_get_addr_opt_bctrl_
;
4615 // FDE unwind info for this stub group.
4616 unsigned int plt_fde_len_
;
4617 unsigned char plt_fde_
[20];
4620 // Add a plt call stub, if we do not already have one for this
4621 // sym/object/addend combo.
4623 template<int size
, bool big_endian
>
4625 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4627 const Sized_relobj_file
<size
, big_endian
>* object
,
4629 unsigned int r_type
,
4633 Plt_stub_key
key(object
, gsym
, r_type
, addend
);
4634 Plt_stub_ent
ent(this->plt_size_
, this->plt_call_stubs_
.size());
4635 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4636 = this->plt_call_stubs_
.insert(std::make_pair(key
, ent
));
4639 this->plt_size_
= ent
.off_
+ this->plt_call_size(p
.first
);
4641 && this->targ_
->is_elfv2_localentry0(gsym
))
4643 p
.first
->second
.localentry0_
= 1;
4644 this->targ_
->set_has_localentry0();
4646 if (this->targ_
->is_tls_get_addr_opt(gsym
))
4648 this->targ_
->set_has_tls_get_addr_opt();
4649 this->tls_get_addr_opt_bctrl_
= this->plt_size_
- 5 * 4;
4651 this->plt_size_
= this->plt_call_align(this->plt_size_
);
4655 && !p
.first
->second
.localentry0_
)
4656 p
.first
->second
.r2save_
= 1;
4657 return this->can_reach_stub(from
, ent
.off_
, r_type
);
4660 template<int size
, bool big_endian
>
4662 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4664 const Sized_relobj_file
<size
, big_endian
>* object
,
4665 unsigned int locsym_index
,
4666 unsigned int r_type
,
4670 Plt_stub_key
key(object
, locsym_index
, r_type
, addend
);
4671 Plt_stub_ent
ent(this->plt_size_
, this->plt_call_stubs_
.size());
4672 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4673 = this->plt_call_stubs_
.insert(std::make_pair(key
, ent
));
4676 this->plt_size_
= ent
.off_
+ this->plt_call_size(p
.first
);
4677 this->plt_size_
= this->plt_call_align(this->plt_size_
);
4679 && this->targ_
->is_elfv2_localentry0(object
, locsym_index
))
4681 p
.first
->second
.localentry0_
= 1;
4682 this->targ_
->set_has_localentry0();
4687 && !p
.first
->second
.localentry0_
)
4688 p
.first
->second
.r2save_
= 1;
4689 return this->can_reach_stub(from
, ent
.off_
, r_type
);
4692 // Find a plt call stub.
4694 template<int size
, bool big_endian
>
4695 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4696 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4697 const Sized_relobj_file
<size
, big_endian
>* object
,
4699 unsigned int r_type
,
4700 Address addend
) const
4702 Plt_stub_key
key(object
, gsym
, r_type
, addend
);
4703 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4704 if (p
== this->plt_call_stubs_
.end())
4709 template<int size
, bool big_endian
>
4710 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4711 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
4713 Plt_stub_key
key(gsym
);
4714 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4715 if (p
== this->plt_call_stubs_
.end())
4720 template<int size
, bool big_endian
>
4721 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4722 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4723 const Sized_relobj_file
<size
, big_endian
>* object
,
4724 unsigned int locsym_index
,
4725 unsigned int r_type
,
4726 Address addend
) const
4728 Plt_stub_key
key(object
, locsym_index
, r_type
, addend
);
4729 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4730 if (p
== this->plt_call_stubs_
.end())
4735 template<int size
, bool big_endian
>
4736 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4737 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4738 const Sized_relobj_file
<size
, big_endian
>* object
,
4739 unsigned int locsym_index
) const
4741 Plt_stub_key
key(object
, locsym_index
);
4742 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4743 if (p
== this->plt_call_stubs_
.end())
4748 // Add a long branch stub if we don't already have one to given
4751 template<int size
, bool big_endian
>
4753 Stub_table
<size
, big_endian
>::add_long_branch_entry(
4754 const Powerpc_relobj
<size
, big_endian
>* object
,
4755 unsigned int r_type
,
4760 Branch_stub_ent
ent(object
, to
, save_res
);
4761 Address off
= this->branch_size_
;
4762 std::pair
<typename
Branch_stub_entries::iterator
, bool> p
4763 = this->long_branch_stubs_
.insert(std::make_pair(ent
, off
));
4767 this->need_save_res_
= true;
4770 unsigned int stub_size
= this->branch_stub_size(p
.first
);
4771 this->branch_size_
= off
+ stub_size
;
4772 if (size
== 64 && stub_size
!= 4)
4773 this->targ_
->add_branch_lookup_table(to
);
4776 return this->can_reach_stub(from
, off
, r_type
);
4779 // Find long branch stub offset.
4781 template<int size
, bool big_endian
>
4782 typename Stub_table
<size
, big_endian
>::Address
4783 Stub_table
<size
, big_endian
>::find_long_branch_entry(
4784 const Powerpc_relobj
<size
, big_endian
>* object
,
4787 Branch_stub_ent
ent(object
, to
, false);
4788 typename
Branch_stub_entries::const_iterator p
4789 = this->long_branch_stubs_
.find(ent
);
4790 if (p
== this->long_branch_stubs_
.end())
4791 return invalid_address
;
4792 if (p
->first
.save_res_
)
4793 return to
- this->targ_
->savres_section()->address() + this->branch_size_
;
4797 // Generate a suitable FDE to describe code in this stub group.
4798 // The __tls_get_addr_opt call stub needs to describe where it saves
4799 // LR, to support exceptions that might be thrown from __tls_get_addr.
4801 template<int size
, bool big_endian
>
4803 Stub_table
<size
, big_endian
>::init_plt_fde()
4805 unsigned char* p
= this->plt_fde_
;
4806 // offset pcrel sdata4, size udata4, and augmentation size byte.
4809 if (this->tls_get_addr_opt_bctrl_
!= -1u)
4811 unsigned int to_bctrl
= this->tls_get_addr_opt_bctrl_
/ 4;
4813 *p
++ = elfcpp::DW_CFA_advance_loc
+ to_bctrl
;
4814 else if (to_bctrl
< 256)
4816 *p
++ = elfcpp::DW_CFA_advance_loc1
;
4819 else if (to_bctrl
< 65536)
4821 *p
++ = elfcpp::DW_CFA_advance_loc2
;
4822 elfcpp::Swap
<16, big_endian
>::writeval(p
, to_bctrl
);
4827 *p
++ = elfcpp::DW_CFA_advance_loc4
;
4828 elfcpp::Swap
<32, big_endian
>::writeval(p
, to_bctrl
);
4831 *p
++ = elfcpp::DW_CFA_offset_extended_sf
;
4833 *p
++ = -(this->targ_
->stk_linker() / 8) & 0x7f;
4834 *p
++ = elfcpp::DW_CFA_advance_loc
+ 4;
4835 *p
++ = elfcpp::DW_CFA_restore_extended
;
4838 this->plt_fde_len_
= p
- this->plt_fde_
;
4841 // Add .eh_frame info for this stub section. Unlike other linker
4842 // generated .eh_frame this is added late in the link, because we
4843 // only want the .eh_frame info if this particular stub section is
4846 template<int size
, bool big_endian
>
4848 Stub_table
<size
, big_endian
>::add_eh_frame(Layout
* layout
)
4850 if (!parameters
->options().ld_generated_unwind_info())
4853 // Since we add stub .eh_frame info late, it must be placed
4854 // after all other linker generated .eh_frame info so that
4855 // merge mapping need not be updated for input sections.
4856 // There is no provision to use a different CIE to that used
4858 if (!this->targ_
->has_glink())
4861 if (this->plt_size_
+ this->branch_size_
+ this->need_save_res_
== 0)
4864 this->init_plt_fde();
4865 layout
->add_eh_frame_for_plt(this,
4866 Eh_cie
<size
>::eh_frame_cie
,
4867 sizeof (Eh_cie
<size
>::eh_frame_cie
),
4868 this->plt_fde_
, this->plt_fde_len_
);
4871 template<int size
, bool big_endian
>
4873 Stub_table
<size
, big_endian
>::remove_eh_frame(Layout
* layout
)
4875 if (this->plt_fde_len_
!= 0)
4877 layout
->remove_eh_frame_for_plt(this,
4878 Eh_cie
<size
>::eh_frame_cie
,
4879 sizeof (Eh_cie
<size
>::eh_frame_cie
),
4880 this->plt_fde_
, this->plt_fde_len_
);
4881 this->plt_fde_len_
= 0;
4885 // A class to handle .glink.
4887 template<int size
, bool big_endian
>
4888 class Output_data_glink
: public Output_section_data
4891 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4892 static const Address invalid_address
= static_cast<Address
>(0) - 1;
4894 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
4895 : Output_section_data(16), targ_(targ
), global_entry_stubs_(),
4896 end_branch_table_(), ge_size_(0)
4900 add_eh_frame(Layout
* layout
);
4903 add_global_entry(const Symbol
*);
4906 find_global_entry(const Symbol
*) const;
4909 global_entry_align(unsigned int off
) const
4911 unsigned int align
= param_plt_align
<size
>();
4912 return (off
+ align
- 1) & -align
;
4916 global_entry_off() const
4918 return this->global_entry_align(this->end_branch_table_
);
4922 global_entry_address() const
4924 gold_assert(this->is_data_size_valid());
4925 return this->address() + this->global_entry_off();
4929 pltresolve_size() const
4933 + (this->targ_
->abiversion() < 2 ? 11 * 4 : 14 * 4));
4938 // Write to a map file.
4940 do_print_to_mapfile(Mapfile
* mapfile
) const
4941 { mapfile
->print_output_data(this, _("** glink")); }
4945 set_final_data_size();
4949 do_write(Output_file
*);
4951 // Allows access to .got and .plt for do_write.
4952 Target_powerpc
<size
, big_endian
>* targ_
;
4954 // Map sym to stub offset.
4955 typedef Unordered_map
<const Symbol
*, unsigned int> Global_entry_stub_entries
;
4956 Global_entry_stub_entries global_entry_stubs_
;
4958 unsigned int end_branch_table_
, ge_size_
;
4961 template<int size
, bool big_endian
>
4963 Output_data_glink
<size
, big_endian
>::add_eh_frame(Layout
* layout
)
4965 if (!parameters
->options().ld_generated_unwind_info())
4970 if (this->targ_
->abiversion() < 2)
4971 layout
->add_eh_frame_for_plt(this,
4972 Eh_cie
<64>::eh_frame_cie
,
4973 sizeof (Eh_cie
<64>::eh_frame_cie
),
4974 glink_eh_frame_fde_64v1
,
4975 sizeof (glink_eh_frame_fde_64v1
));
4977 layout
->add_eh_frame_for_plt(this,
4978 Eh_cie
<64>::eh_frame_cie
,
4979 sizeof (Eh_cie
<64>::eh_frame_cie
),
4980 glink_eh_frame_fde_64v2
,
4981 sizeof (glink_eh_frame_fde_64v2
));
4985 // 32-bit .glink can use the default since the CIE return
4986 // address reg, LR, is valid.
4987 layout
->add_eh_frame_for_plt(this,
4988 Eh_cie
<32>::eh_frame_cie
,
4989 sizeof (Eh_cie
<32>::eh_frame_cie
),
4991 sizeof (default_fde
));
4992 // Except where LR is used in a PIC __glink_PLTresolve.
4993 if (parameters
->options().output_is_position_independent())
4994 layout
->add_eh_frame_for_plt(this,
4995 Eh_cie
<32>::eh_frame_cie
,
4996 sizeof (Eh_cie
<32>::eh_frame_cie
),
4997 glink_eh_frame_fde_32
,
4998 sizeof (glink_eh_frame_fde_32
));
5002 template<int size
, bool big_endian
>
5004 Output_data_glink
<size
, big_endian
>::add_global_entry(const Symbol
* gsym
)
5006 unsigned int off
= this->global_entry_align(this->ge_size_
);
5007 std::pair
<typename
Global_entry_stub_entries::iterator
, bool> p
5008 = this->global_entry_stubs_
.insert(std::make_pair(gsym
, off
));
5010 this->ge_size_
= off
+ 16;
5013 template<int size
, bool big_endian
>
5014 typename Output_data_glink
<size
, big_endian
>::Address
5015 Output_data_glink
<size
, big_endian
>::find_global_entry(const Symbol
* gsym
) const
5017 typename
Global_entry_stub_entries::const_iterator p
5018 = this->global_entry_stubs_
.find(gsym
);
5019 return p
== this->global_entry_stubs_
.end() ? invalid_address
: p
->second
;
5022 template<int size
, bool big_endian
>
5024 Output_data_glink
<size
, big_endian
>::set_final_data_size()
5026 unsigned int count
= this->targ_
->plt_entry_count();
5027 section_size_type total
= 0;
5033 // space for branch table
5034 total
+= 4 * (count
- 1);
5036 total
+= -total
& 15;
5037 total
+= this->pltresolve_size();
5041 total
+= this->pltresolve_size();
5043 // space for branch table
5045 if (this->targ_
->abiversion() < 2)
5049 total
+= 4 * (count
- 0x8000);
5053 this->end_branch_table_
= total
;
5054 total
= this->global_entry_align(total
);
5055 total
+= this->ge_size_
;
5057 this->set_data_size(total
);
5060 // Define symbols on stubs, identifying the stub.
5062 template<int size
, bool big_endian
>
5064 Stub_table
<size
, big_endian
>::define_stub_syms(Symbol_table
* symtab
)
5066 if (!this->plt_call_stubs_
.empty())
5068 // The key for the plt call stub hash table includes addresses,
5069 // therefore traversal order depends on those addresses, which
5070 // can change between runs if gold is a PIE. Unfortunately the
5071 // output .symtab ordering depends on the order in which symbols
5072 // are added to the linker symtab. We want reproducible output
5073 // so must sort the call stub symbols.
5074 typedef typename
Plt_stub_entries::const_iterator plt_iter
;
5075 std::vector
<plt_iter
> sorted
;
5076 sorted
.resize(this->plt_call_stubs_
.size());
5078 for (plt_iter cs
= this->plt_call_stubs_
.begin();
5079 cs
!= this->plt_call_stubs_
.end();
5081 sorted
[cs
->second
.indx_
] = cs
;
5083 for (unsigned int i
= 0; i
< this->plt_call_stubs_
.size(); ++i
)
5085 plt_iter cs
= sorted
[i
];
5088 if (cs
->first
.addend_
!= 0)
5089 sprintf(add
, "+%x", static_cast<uint32_t>(cs
->first
.addend_
));
5092 if (cs
->first
.object_
)
5094 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
5095 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
5096 sprintf(obj
, "%x:", ppcobj
->uniq());
5099 const char *symname
;
5100 if (cs
->first
.sym_
== NULL
)
5102 sprintf(localname
, "%x", cs
->first
.locsym_
);
5103 symname
= localname
;
5105 else if (this->targ_
->is_tls_get_addr_opt(cs
->first
.sym_
))
5106 symname
= this->targ_
->tls_get_addr_opt()->name();
5108 symname
= cs
->first
.sym_
->name();
5109 char* name
= new char[8 + 10 + strlen(obj
) + strlen(symname
) + strlen(add
) + 1];
5110 sprintf(name
, "%08x.plt_call.%s%s%s", this->uniq_
, obj
, symname
, add
);
5112 = this->stub_address() - this->address() + cs
->second
.off_
;
5113 unsigned int stub_size
= this->plt_call_align(this->plt_call_size(cs
));
5114 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
5118 typedef typename
Branch_stub_entries::const_iterator branch_iter
;
5119 for (branch_iter bs
= this->long_branch_stubs_
.begin();
5120 bs
!= this->long_branch_stubs_
.end();
5123 if (bs
->first
.save_res_
)
5126 char* name
= new char[8 + 13 + 16 + 1];
5127 sprintf(name
, "%08x.long_branch.%llx", this->uniq_
,
5128 static_cast<unsigned long long>(bs
->first
.dest_
));
5129 Address value
= (this->stub_address() - this->address()
5130 + this->plt_size_
+ bs
->second
);
5131 unsigned int stub_size
= this->branch_stub_size(bs
);
5132 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
5136 // Write out plt and long branch stub code.
5138 template<int size
, bool big_endian
>
5140 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
5142 if (this->plt_call_stubs_
.empty()
5143 && this->long_branch_stubs_
.empty())
5146 const section_size_type start_off
= this->offset();
5147 const section_size_type off
= this->stub_offset();
5148 const section_size_type oview_size
=
5149 convert_to_section_size_type(this->data_size() - (off
- start_off
));
5150 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5155 const Output_data_got_powerpc
<size
, big_endian
>* got
5156 = this->targ_
->got_section();
5157 Address got_os_addr
= got
->output_section()->address();
5159 if (!this->plt_call_stubs_
.empty())
5161 // The base address of the .plt section.
5162 Address plt_base
= this->targ_
->plt_section()->address();
5163 Address iplt_base
= invalid_address
;
5165 // Write out plt call stubs.
5166 typename
Plt_stub_entries::const_iterator cs
;
5167 for (cs
= this->plt_call_stubs_
.begin();
5168 cs
!= this->plt_call_stubs_
.end();
5172 Address pltoff
= this->plt_off(cs
, &is_iplt
);
5173 Address plt_addr
= pltoff
;
5176 if (iplt_base
== invalid_address
)
5177 iplt_base
= this->targ_
->iplt_section()->address();
5178 plt_addr
+= iplt_base
;
5181 plt_addr
+= plt_base
;
5182 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
5183 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
5184 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
5185 Address off
= plt_addr
- got_addr
;
5187 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
5188 gold_error(_("%s: linkage table error against `%s'"),
5189 cs
->first
.object_
->name().c_str(),
5190 cs
->first
.sym_
->demangled_name().c_str());
5192 bool plt_load_toc
= this->targ_
->abiversion() < 2;
5194 = plt_load_toc
&& parameters
->options().plt_static_chain();
5196 = plt_load_toc
&& this->targ_
->plt_thread_safe();
5197 bool use_fake_dep
= false;
5198 Address cmp_branch_off
= 0;
5201 unsigned int pltindex
5202 = ((pltoff
- this->targ_
->first_plt_entry_offset())
5203 / this->targ_
->plt_entry_size());
5205 = (this->targ_
->glink_section()->pltresolve_size()
5207 if (pltindex
> 32768)
5208 glinkoff
+= (pltindex
- 32768) * 4;
5210 = this->targ_
->glink_section()->address() + glinkoff
;
5212 = (this->stub_address() + cs
->second
.off_
+ 20
5213 + 4 * cs
->second
.r2save_
5214 + 4 * (ha(off
) != 0)
5215 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
5216 + 4 * static_chain
);
5217 cmp_branch_off
= to
- from
;
5218 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
5221 p
= oview
+ cs
->second
.off_
;
5222 const Symbol
* gsym
= cs
->first
.sym_
;
5223 if (this->targ_
->is_tls_get_addr_opt(gsym
))
5225 write_insn
<big_endian
>(p
, ld_11_3
+ 0);
5227 write_insn
<big_endian
>(p
, ld_12_3
+ 8);
5229 write_insn
<big_endian
>(p
, mr_0_3
);
5231 write_insn
<big_endian
>(p
, cmpdi_11_0
);
5233 write_insn
<big_endian
>(p
, add_3_12_13
);
5235 write_insn
<big_endian
>(p
, beqlr
);
5237 write_insn
<big_endian
>(p
, mr_3_0
);
5239 if (!cs
->second
.localentry0_
)
5241 write_insn
<big_endian
>(p
, mflr_11
);
5243 write_insn
<big_endian
>(p
, (std_11_1
5244 + this->targ_
->stk_linker()));
5247 use_fake_dep
= thread_safe
;
5251 if (cs
->second
.r2save_
)
5253 write_insn
<big_endian
>(p
,
5254 std_2_1
+ this->targ_
->stk_toc());
5259 write_insn
<big_endian
>(p
, addis_11_2
+ ha(off
));
5261 write_insn
<big_endian
>(p
, ld_12_11
+ l(off
));
5266 write_insn
<big_endian
>(p
, addis_12_2
+ ha(off
));
5268 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
));
5272 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
5274 write_insn
<big_endian
>(p
, addi_11_11
+ l(off
));
5278 write_insn
<big_endian
>(p
, mtctr_12
);
5284 write_insn
<big_endian
>(p
, xor_2_12_12
);
5286 write_insn
<big_endian
>(p
, add_11_11_2
);
5289 write_insn
<big_endian
>(p
, ld_2_11
+ l(off
+ 8));
5293 write_insn
<big_endian
>(p
, ld_11_11
+ l(off
+ 16));
5300 if (cs
->second
.r2save_
)
5302 write_insn
<big_endian
>(p
,
5303 std_2_1
+ this->targ_
->stk_toc());
5306 write_insn
<big_endian
>(p
, ld_12_2
+ l(off
));
5309 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
5311 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
));
5315 write_insn
<big_endian
>(p
, mtctr_12
);
5321 write_insn
<big_endian
>(p
, xor_11_12_12
);
5323 write_insn
<big_endian
>(p
, add_2_2_11
);
5328 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16));
5331 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8));
5335 if (!cs
->second
.localentry0_
5336 && this->targ_
->is_tls_get_addr_opt(gsym
))
5338 write_insn
<big_endian
>(p
, bctrl
);
5340 write_insn
<big_endian
>(p
, ld_2_1
+ this->targ_
->stk_toc());
5342 write_insn
<big_endian
>(p
, ld_11_1
+ this->targ_
->stk_linker());
5344 write_insn
<big_endian
>(p
, mtlr_11
);
5346 write_insn
<big_endian
>(p
, blr
);
5348 else if (thread_safe
&& !use_fake_dep
)
5350 write_insn
<big_endian
>(p
, cmpldi_2_0
);
5352 write_insn
<big_endian
>(p
, bnectr_p4
);
5354 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
5357 write_insn
<big_endian
>(p
, bctr
);
5361 // Write out long branch stubs.
5362 typename
Branch_stub_entries::const_iterator bs
;
5363 for (bs
= this->long_branch_stubs_
.begin();
5364 bs
!= this->long_branch_stubs_
.end();
5367 if (bs
->first
.save_res_
)
5369 p
= oview
+ this->plt_size_
+ bs
->second
;
5370 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
5371 Address delta
= bs
->first
.dest_
- loc
;
5372 if (delta
+ (1 << 25) < 2 << 25)
5373 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
5377 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
5378 gold_assert(brlt_addr
!= invalid_address
);
5379 brlt_addr
+= this->targ_
->brlt_section()->address();
5380 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
5381 Address brltoff
= brlt_addr
- got_addr
;
5382 if (ha(brltoff
) == 0)
5384 write_insn
<big_endian
>(p
, ld_12_2
+ l(brltoff
)), p
+= 4;
5388 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
5389 write_insn
<big_endian
>(p
, ld_12_12
+ l(brltoff
)), p
+= 4;
5391 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5392 write_insn
<big_endian
>(p
, bctr
);
5398 if (!this->plt_call_stubs_
.empty())
5400 // The base address of the .plt section.
5401 Address plt_base
= this->targ_
->plt_section()->address();
5402 Address iplt_base
= invalid_address
;
5403 // The address of _GLOBAL_OFFSET_TABLE_.
5404 Address g_o_t
= invalid_address
;
5406 // Write out plt call stubs.
5407 typename
Plt_stub_entries::const_iterator cs
;
5408 for (cs
= this->plt_call_stubs_
.begin();
5409 cs
!= this->plt_call_stubs_
.end();
5413 Address plt_addr
= this->plt_off(cs
, &is_iplt
);
5416 if (iplt_base
== invalid_address
)
5417 iplt_base
= this->targ_
->iplt_section()->address();
5418 plt_addr
+= iplt_base
;
5421 plt_addr
+= plt_base
;
5423 p
= oview
+ cs
->second
.off_
;
5424 const Symbol
* gsym
= cs
->first
.sym_
;
5425 if (this->targ_
->is_tls_get_addr_opt(gsym
))
5427 write_insn
<big_endian
>(p
, lwz_11_3
+ 0);
5429 write_insn
<big_endian
>(p
, lwz_12_3
+ 4);
5431 write_insn
<big_endian
>(p
, mr_0_3
);
5433 write_insn
<big_endian
>(p
, cmpwi_11_0
);
5435 write_insn
<big_endian
>(p
, add_3_12_2
);
5437 write_insn
<big_endian
>(p
, beqlr
);
5439 write_insn
<big_endian
>(p
, mr_3_0
);
5441 write_insn
<big_endian
>(p
, nop
);
5444 if (parameters
->options().output_is_position_independent())
5447 const Powerpc_relobj
<size
, big_endian
>* ppcobj
5448 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
5449 (cs
->first
.object_
));
5450 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
5452 unsigned int got2
= ppcobj
->got2_shndx();
5453 got_addr
= ppcobj
->get_output_section_offset(got2
);
5454 gold_assert(got_addr
!= invalid_address
);
5455 got_addr
+= (ppcobj
->output_section(got2
)->address()
5456 + cs
->first
.addend_
);
5460 if (g_o_t
== invalid_address
)
5462 const Output_data_got_powerpc
<size
, big_endian
>* got
5463 = this->targ_
->got_section();
5464 g_o_t
= got
->address() + got
->g_o_t();
5469 Address off
= plt_addr
- got_addr
;
5471 write_insn
<big_endian
>(p
, lwz_11_30
+ l(off
));
5474 write_insn
<big_endian
>(p
, addis_11_30
+ ha(off
));
5476 write_insn
<big_endian
>(p
, lwz_11_11
+ l(off
));
5481 write_insn
<big_endian
>(p
, lis_11
+ ha(plt_addr
));
5483 write_insn
<big_endian
>(p
, lwz_11_11
+ l(plt_addr
));
5486 write_insn
<big_endian
>(p
, mtctr_11
);
5488 write_insn
<big_endian
>(p
, bctr
);
5492 // Write out long branch stubs.
5493 typename
Branch_stub_entries::const_iterator bs
;
5494 for (bs
= this->long_branch_stubs_
.begin();
5495 bs
!= this->long_branch_stubs_
.end();
5498 if (bs
->first
.save_res_
)
5500 p
= oview
+ this->plt_size_
+ bs
->second
;
5501 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
5502 Address delta
= bs
->first
.dest_
- loc
;
5503 if (delta
+ (1 << 25) < 2 << 25)
5504 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
5505 else if (!parameters
->options().output_is_position_independent())
5507 write_insn
<big_endian
>(p
, lis_12
+ ha(bs
->first
.dest_
));
5509 write_insn
<big_endian
>(p
, addi_12_12
+ l(bs
->first
.dest_
));
5514 write_insn
<big_endian
>(p
, mflr_0
);
5516 write_insn
<big_endian
>(p
, bcl_20_31
);
5518 write_insn
<big_endian
>(p
, mflr_12
);
5520 write_insn
<big_endian
>(p
, addis_12_12
+ ha(delta
));
5522 write_insn
<big_endian
>(p
, addi_12_12
+ l(delta
));
5524 write_insn
<big_endian
>(p
, mtlr_0
);
5527 write_insn
<big_endian
>(p
, mtctr_12
);
5529 write_insn
<big_endian
>(p
, bctr
);
5532 if (this->need_save_res_
)
5534 p
= oview
+ this->plt_size_
+ this->branch_size_
;
5535 memcpy (p
, this->targ_
->savres_section()->contents(),
5536 this->targ_
->savres_section()->data_size());
5540 // Write out .glink.
5542 template<int size
, bool big_endian
>
5544 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
5546 const section_size_type off
= this->offset();
5547 const section_size_type oview_size
=
5548 convert_to_section_size_type(this->data_size());
5549 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5552 // The base address of the .plt section.
5553 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
5554 Address plt_base
= this->targ_
->plt_section()->address();
5558 if (this->end_branch_table_
!= 0)
5560 // Write pltresolve stub.
5562 Address after_bcl
= this->address() + 16;
5563 Address pltoff
= plt_base
- after_bcl
;
5565 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
5567 if (this->targ_
->abiversion() < 2)
5569 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
5570 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5571 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5572 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5573 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
5574 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5575 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5576 write_insn
<big_endian
>(p
, ld_2_11
+ 8), p
+= 4;
5577 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5578 write_insn
<big_endian
>(p
, ld_11_11
+ 16), p
+= 4;
5582 write_insn
<big_endian
>(p
, mflr_0
), p
+= 4;
5583 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5584 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5585 write_insn
<big_endian
>(p
, std_2_1
+ 24), p
+= 4;
5586 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5587 write_insn
<big_endian
>(p
, mtlr_0
), p
+= 4;
5588 write_insn
<big_endian
>(p
, sub_12_12_11
), p
+= 4;
5589 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5590 write_insn
<big_endian
>(p
, addi_0_12
+ l(-48)), p
+= 4;
5591 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5592 write_insn
<big_endian
>(p
, srdi_0_0_2
), p
+= 4;
5593 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5594 write_insn
<big_endian
>(p
, ld_11_11
+ 8), p
+= 4;
5596 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
5597 gold_assert(p
== oview
+ this->pltresolve_size());
5599 // Write lazy link call stubs.
5601 while (p
< oview
+ this->end_branch_table_
)
5603 if (this->targ_
->abiversion() < 2)
5607 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
5611 write_insn
<big_endian
>(p
, lis_0
+ hi(indx
)), p
+= 4;
5612 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
5615 uint32_t branch_off
= 8 - (p
- oview
);
5616 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
5621 Address plt_base
= this->targ_
->plt_section()->address();
5622 Address iplt_base
= invalid_address
;
5623 unsigned int global_entry_off
= this->global_entry_off();
5624 Address global_entry_base
= this->address() + global_entry_off
;
5625 typename
Global_entry_stub_entries::const_iterator ge
;
5626 for (ge
= this->global_entry_stubs_
.begin();
5627 ge
!= this->global_entry_stubs_
.end();
5630 p
= oview
+ global_entry_off
+ ge
->second
;
5631 Address plt_addr
= ge
->first
->plt_offset();
5632 if (ge
->first
->type() == elfcpp::STT_GNU_IFUNC
5633 && ge
->first
->can_use_relative_reloc(false))
5635 if (iplt_base
== invalid_address
)
5636 iplt_base
= this->targ_
->iplt_section()->address();
5637 plt_addr
+= iplt_base
;
5640 plt_addr
+= plt_base
;
5641 Address my_addr
= global_entry_base
+ ge
->second
;
5642 Address off
= plt_addr
- my_addr
;
5644 if (off
+ 0x80008000 > 0xffffffff || (off
& 3) != 0)
5645 gold_error(_("%s: linkage table error against `%s'"),
5646 ge
->first
->object()->name().c_str(),
5647 ge
->first
->demangled_name().c_str());
5649 write_insn
<big_endian
>(p
, addis_12_12
+ ha(off
)), p
+= 4;
5650 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
)), p
+= 4;
5651 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5652 write_insn
<big_endian
>(p
, bctr
);
5657 const Output_data_got_powerpc
<size
, big_endian
>* got
5658 = this->targ_
->got_section();
5659 // The address of _GLOBAL_OFFSET_TABLE_.
5660 Address g_o_t
= got
->address() + got
->g_o_t();
5662 // Write out pltresolve branch table.
5664 unsigned int the_end
= oview_size
- this->pltresolve_size();
5665 unsigned char* end_p
= oview
+ the_end
;
5666 while (p
< end_p
- 8 * 4)
5667 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
5669 write_insn
<big_endian
>(p
, nop
), p
+= 4;
5671 // Write out pltresolve call stub.
5672 end_p
= oview
+ oview_size
;
5673 if (parameters
->options().output_is_position_independent())
5675 Address res0_off
= 0;
5676 Address after_bcl_off
= the_end
+ 12;
5677 Address bcl_res0
= after_bcl_off
- res0_off
;
5679 write_insn
<big_endian
>(p
, addis_11_11
+ ha(bcl_res0
));
5681 write_insn
<big_endian
>(p
, mflr_0
);
5683 write_insn
<big_endian
>(p
, bcl_20_31
);
5685 write_insn
<big_endian
>(p
, addi_11_11
+ l(bcl_res0
));
5687 write_insn
<big_endian
>(p
, mflr_12
);
5689 write_insn
<big_endian
>(p
, mtlr_0
);
5691 write_insn
<big_endian
>(p
, sub_11_11_12
);
5694 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
5696 write_insn
<big_endian
>(p
, addis_12_12
+ ha(got_bcl
));
5698 if (ha(got_bcl
) == ha(got_bcl
+ 4))
5700 write_insn
<big_endian
>(p
, lwz_0_12
+ l(got_bcl
));
5702 write_insn
<big_endian
>(p
, lwz_12_12
+ l(got_bcl
+ 4));
5706 write_insn
<big_endian
>(p
, lwzu_0_12
+ l(got_bcl
));
5708 write_insn
<big_endian
>(p
, lwz_12_12
+ 4);
5711 write_insn
<big_endian
>(p
, mtctr_0
);
5713 write_insn
<big_endian
>(p
, add_0_11_11
);
5715 write_insn
<big_endian
>(p
, add_11_0_11
);
5719 Address res0
= this->address();
5721 write_insn
<big_endian
>(p
, lis_12
+ ha(g_o_t
+ 4));
5723 write_insn
<big_endian
>(p
, addis_11_11
+ ha(-res0
));
5725 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5726 write_insn
<big_endian
>(p
, lwz_0_12
+ l(g_o_t
+ 4));
5728 write_insn
<big_endian
>(p
, lwzu_0_12
+ l(g_o_t
+ 4));
5730 write_insn
<big_endian
>(p
, addi_11_11
+ l(-res0
));
5732 write_insn
<big_endian
>(p
, mtctr_0
);
5734 write_insn
<big_endian
>(p
, add_0_11_11
);
5736 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5737 write_insn
<big_endian
>(p
, lwz_12_12
+ l(g_o_t
+ 8));
5739 write_insn
<big_endian
>(p
, lwz_12_12
+ 4);
5741 write_insn
<big_endian
>(p
, add_11_0_11
);
5744 write_insn
<big_endian
>(p
, bctr
);
5748 write_insn
<big_endian
>(p
, nop
);
5753 of
->write_output_view(off
, oview_size
, oview
);
5757 // A class to handle linker generated save/restore functions.
5759 template<int size
, bool big_endian
>
5760 class Output_data_save_res
: public Output_section_data_build
5763 Output_data_save_res(Symbol_table
* symtab
);
5765 const unsigned char*
5772 // Write to a map file.
5774 do_print_to_mapfile(Mapfile
* mapfile
) const
5775 { mapfile
->print_output_data(this, _("** save/restore")); }
5778 do_write(Output_file
*);
5781 // The maximum size of save/restore contents.
5782 static const unsigned int savres_max
= 218*4;
5785 savres_define(Symbol_table
* symtab
,
5787 unsigned int lo
, unsigned int hi
,
5788 unsigned char* write_ent(unsigned char*, int),
5789 unsigned char* write_tail(unsigned char*, int));
5791 unsigned char *contents_
;
5794 template<bool big_endian
>
5795 static unsigned char*
5796 savegpr0(unsigned char* p
, int r
)
5798 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5799 write_insn
<big_endian
>(p
, insn
);
5803 template<bool big_endian
>
5804 static unsigned char*
5805 savegpr0_tail(unsigned char* p
, int r
)
5807 p
= savegpr0
<big_endian
>(p
, r
);
5808 uint32_t insn
= std_0_1
+ 16;
5809 write_insn
<big_endian
>(p
, insn
);
5811 write_insn
<big_endian
>(p
, blr
);
5815 template<bool big_endian
>
5816 static unsigned char*
5817 restgpr0(unsigned char* p
, int r
)
5819 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5820 write_insn
<big_endian
>(p
, insn
);
5824 template<bool big_endian
>
5825 static unsigned char*
5826 restgpr0_tail(unsigned char* p
, int r
)
5828 uint32_t insn
= ld_0_1
+ 16;
5829 write_insn
<big_endian
>(p
, insn
);
5831 p
= restgpr0
<big_endian
>(p
, r
);
5832 write_insn
<big_endian
>(p
, mtlr_0
);
5836 p
= restgpr0
<big_endian
>(p
, 30);
5837 p
= restgpr0
<big_endian
>(p
, 31);
5839 write_insn
<big_endian
>(p
, blr
);
5843 template<bool big_endian
>
5844 static unsigned char*
5845 savegpr1(unsigned char* p
, int r
)
5847 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5848 write_insn
<big_endian
>(p
, insn
);
5852 template<bool big_endian
>
5853 static unsigned char*
5854 savegpr1_tail(unsigned char* p
, int r
)
5856 p
= savegpr1
<big_endian
>(p
, r
);
5857 write_insn
<big_endian
>(p
, blr
);
5861 template<bool big_endian
>
5862 static unsigned char*
5863 restgpr1(unsigned char* p
, int r
)
5865 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5866 write_insn
<big_endian
>(p
, insn
);
5870 template<bool big_endian
>
5871 static unsigned char*
5872 restgpr1_tail(unsigned char* p
, int r
)
5874 p
= restgpr1
<big_endian
>(p
, r
);
5875 write_insn
<big_endian
>(p
, blr
);
5879 template<bool big_endian
>
5880 static unsigned char*
5881 savefpr(unsigned char* p
, int r
)
5883 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5884 write_insn
<big_endian
>(p
, insn
);
5888 template<bool big_endian
>
5889 static unsigned char*
5890 savefpr0_tail(unsigned char* p
, int r
)
5892 p
= savefpr
<big_endian
>(p
, r
);
5893 write_insn
<big_endian
>(p
, std_0_1
+ 16);
5895 write_insn
<big_endian
>(p
, blr
);
5899 template<bool big_endian
>
5900 static unsigned char*
5901 restfpr(unsigned char* p
, int r
)
5903 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
5904 write_insn
<big_endian
>(p
, insn
);
5908 template<bool big_endian
>
5909 static unsigned char*
5910 restfpr0_tail(unsigned char* p
, int r
)
5912 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
5914 p
= restfpr
<big_endian
>(p
, r
);
5915 write_insn
<big_endian
>(p
, mtlr_0
);
5919 p
= restfpr
<big_endian
>(p
, 30);
5920 p
= restfpr
<big_endian
>(p
, 31);
5922 write_insn
<big_endian
>(p
, blr
);
5926 template<bool big_endian
>
5927 static unsigned char*
5928 savefpr1_tail(unsigned char* p
, int r
)
5930 p
= savefpr
<big_endian
>(p
, r
);
5931 write_insn
<big_endian
>(p
, blr
);
5935 template<bool big_endian
>
5936 static unsigned char*
5937 restfpr1_tail(unsigned char* p
, int r
)
5939 p
= restfpr
<big_endian
>(p
, r
);
5940 write_insn
<big_endian
>(p
, blr
);
5944 template<bool big_endian
>
5945 static unsigned char*
5946 savevr(unsigned char* p
, int r
)
5948 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5949 write_insn
<big_endian
>(p
, insn
);
5951 insn
= stvx_0_12_0
+ (r
<< 21);
5952 write_insn
<big_endian
>(p
, insn
);
5956 template<bool big_endian
>
5957 static unsigned char*
5958 savevr_tail(unsigned char* p
, int r
)
5960 p
= savevr
<big_endian
>(p
, r
);
5961 write_insn
<big_endian
>(p
, blr
);
5965 template<bool big_endian
>
5966 static unsigned char*
5967 restvr(unsigned char* p
, int r
)
5969 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5970 write_insn
<big_endian
>(p
, insn
);
5972 insn
= lvx_0_12_0
+ (r
<< 21);
5973 write_insn
<big_endian
>(p
, insn
);
5977 template<bool big_endian
>
5978 static unsigned char*
5979 restvr_tail(unsigned char* p
, int r
)
5981 p
= restvr
<big_endian
>(p
, r
);
5982 write_insn
<big_endian
>(p
, blr
);
5987 template<int size
, bool big_endian
>
5988 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
5989 Symbol_table
* symtab
)
5990 : Output_section_data_build(4),
5993 this->savres_define(symtab
,
5994 "_savegpr0_", 14, 31,
5995 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
5996 this->savres_define(symtab
,
5997 "_restgpr0_", 14, 29,
5998 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
5999 this->savres_define(symtab
,
6000 "_restgpr0_", 30, 31,
6001 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
6002 this->savres_define(symtab
,
6003 "_savegpr1_", 14, 31,
6004 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
6005 this->savres_define(symtab
,
6006 "_restgpr1_", 14, 31,
6007 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
6008 this->savres_define(symtab
,
6009 "_savefpr_", 14, 31,
6010 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
6011 this->savres_define(symtab
,
6012 "_restfpr_", 14, 29,
6013 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
6014 this->savres_define(symtab
,
6015 "_restfpr_", 30, 31,
6016 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
6017 this->savres_define(symtab
,
6019 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
6020 this->savres_define(symtab
,
6022 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
6023 this->savres_define(symtab
,
6025 savevr
<big_endian
>, savevr_tail
<big_endian
>);
6026 this->savres_define(symtab
,
6028 restvr
<big_endian
>, restvr_tail
<big_endian
>);
6031 template<int size
, bool big_endian
>
6033 Output_data_save_res
<size
, big_endian
>::savres_define(
6034 Symbol_table
* symtab
,
6036 unsigned int lo
, unsigned int hi
,
6037 unsigned char* write_ent(unsigned char*, int),
6038 unsigned char* write_tail(unsigned char*, int))
6040 size_t len
= strlen(name
);
6041 bool writing
= false;
6044 memcpy(sym
, name
, len
);
6047 for (unsigned int i
= lo
; i
<= hi
; i
++)
6049 sym
[len
+ 0] = i
/ 10 + '0';
6050 sym
[len
+ 1] = i
% 10 + '0';
6051 Symbol
* gsym
= symtab
->lookup(sym
);
6052 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
6053 writing
= writing
|| refd
;
6056 if (this->contents_
== NULL
)
6057 this->contents_
= new unsigned char[this->savres_max
];
6059 section_size_type value
= this->current_data_size();
6060 unsigned char* p
= this->contents_
+ value
;
6062 p
= write_ent(p
, i
);
6064 p
= write_tail(p
, i
);
6065 section_size_type cur_size
= p
- this->contents_
;
6066 this->set_current_data_size(cur_size
);
6068 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
6069 this, value
, cur_size
- value
,
6070 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
6071 elfcpp::STV_HIDDEN
, 0, false, false);
6076 // Write out save/restore.
6078 template<int size
, bool big_endian
>
6080 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
6082 const section_size_type off
= this->offset();
6083 const section_size_type oview_size
=
6084 convert_to_section_size_type(this->data_size());
6085 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
6086 memcpy(oview
, this->contents_
, oview_size
);
6087 of
->write_output_view(off
, oview_size
, oview
);
6091 // Create the glink section.
6093 template<int size
, bool big_endian
>
6095 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
6097 if (this->glink_
== NULL
)
6099 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
6100 this->glink_
->add_eh_frame(layout
);
6101 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
6102 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
6103 this->glink_
, ORDER_TEXT
, false);
6107 // Create a PLT entry for a global symbol.
6109 template<int size
, bool big_endian
>
6111 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
6115 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
6116 && gsym
->can_use_relative_reloc(false))
6118 if (this->iplt_
== NULL
)
6119 this->make_iplt_section(symtab
, layout
);
6120 this->iplt_
->add_ifunc_entry(gsym
);
6124 if (this->plt_
== NULL
)
6125 this->make_plt_section(symtab
, layout
);
6126 this->plt_
->add_entry(gsym
);
6130 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
6132 template<int size
, bool big_endian
>
6134 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
6135 Symbol_table
* symtab
,
6137 Sized_relobj_file
<size
, big_endian
>* relobj
,
6140 if (this->iplt_
== NULL
)
6141 this->make_iplt_section(symtab
, layout
);
6142 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
6145 // Return the number of entries in the PLT.
6147 template<int size
, bool big_endian
>
6149 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
6151 if (this->plt_
== NULL
)
6153 return this->plt_
->entry_count();
6156 // Create a GOT entry for local dynamic __tls_get_addr calls.
6158 template<int size
, bool big_endian
>
6160 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
6161 Symbol_table
* symtab
,
6163 Sized_relobj_file
<size
, big_endian
>* object
)
6165 if (this->tlsld_got_offset_
== -1U)
6167 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
6168 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
6169 Output_data_got_powerpc
<size
, big_endian
>* got
6170 = this->got_section(symtab
, layout
);
6171 unsigned int got_offset
= got
->add_constant_pair(0, 0);
6172 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
6174 this->tlsld_got_offset_
= got_offset
;
6176 return this->tlsld_got_offset_
;
6179 // Get the Reference_flags for a particular relocation.
6181 template<int size
, bool big_endian
>
6183 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(
6184 unsigned int r_type
,
6185 const Target_powerpc
* target
)
6191 case elfcpp::R_POWERPC_NONE
:
6192 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6193 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6194 case elfcpp::R_PPC64_TOC
:
6195 // No symbol reference.
6198 case elfcpp::R_PPC64_ADDR64
:
6199 case elfcpp::R_PPC64_UADDR64
:
6200 case elfcpp::R_POWERPC_ADDR32
:
6201 case elfcpp::R_POWERPC_UADDR32
:
6202 case elfcpp::R_POWERPC_ADDR16
:
6203 case elfcpp::R_POWERPC_UADDR16
:
6204 case elfcpp::R_POWERPC_ADDR16_LO
:
6205 case elfcpp::R_POWERPC_ADDR16_HI
:
6206 case elfcpp::R_POWERPC_ADDR16_HA
:
6207 ref
= Symbol::ABSOLUTE_REF
;
6210 case elfcpp::R_POWERPC_ADDR24
:
6211 case elfcpp::R_POWERPC_ADDR14
:
6212 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6213 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6214 ref
= Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
6217 case elfcpp::R_PPC64_REL64
:
6218 case elfcpp::R_POWERPC_REL32
:
6219 case elfcpp::R_PPC_LOCAL24PC
:
6220 case elfcpp::R_POWERPC_REL16
:
6221 case elfcpp::R_POWERPC_REL16_LO
:
6222 case elfcpp::R_POWERPC_REL16_HI
:
6223 case elfcpp::R_POWERPC_REL16_HA
:
6224 ref
= Symbol::RELATIVE_REF
;
6227 case elfcpp::R_POWERPC_REL24
:
6228 case elfcpp::R_PPC_PLTREL24
:
6229 case elfcpp::R_POWERPC_REL14
:
6230 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6231 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6232 ref
= Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
6235 case elfcpp::R_POWERPC_GOT16
:
6236 case elfcpp::R_POWERPC_GOT16_LO
:
6237 case elfcpp::R_POWERPC_GOT16_HI
:
6238 case elfcpp::R_POWERPC_GOT16_HA
:
6239 case elfcpp::R_PPC64_GOT16_DS
:
6240 case elfcpp::R_PPC64_GOT16_LO_DS
:
6241 case elfcpp::R_PPC64_TOC16
:
6242 case elfcpp::R_PPC64_TOC16_LO
:
6243 case elfcpp::R_PPC64_TOC16_HI
:
6244 case elfcpp::R_PPC64_TOC16_HA
:
6245 case elfcpp::R_PPC64_TOC16_DS
:
6246 case elfcpp::R_PPC64_TOC16_LO_DS
:
6247 ref
= Symbol::RELATIVE_REF
;
6250 case elfcpp::R_POWERPC_GOT_TPREL16
:
6251 case elfcpp::R_POWERPC_TLS
:
6252 ref
= Symbol::TLS_REF
;
6255 case elfcpp::R_POWERPC_COPY
:
6256 case elfcpp::R_POWERPC_GLOB_DAT
:
6257 case elfcpp::R_POWERPC_JMP_SLOT
:
6258 case elfcpp::R_POWERPC_RELATIVE
:
6259 case elfcpp::R_POWERPC_DTPMOD
:
6261 // Not expected. We will give an error later.
6265 if (size
== 64 && target
->abiversion() < 2)
6266 ref
|= Symbol::FUNC_DESC_ABI
;
6270 // Report an unsupported relocation against a local symbol.
6272 template<int size
, bool big_endian
>
6274 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
6275 Sized_relobj_file
<size
, big_endian
>* object
,
6276 unsigned int r_type
)
6278 gold_error(_("%s: unsupported reloc %u against local symbol"),
6279 object
->name().c_str(), r_type
);
6282 // We are about to emit a dynamic relocation of type R_TYPE. If the
6283 // dynamic linker does not support it, issue an error.
6285 template<int size
, bool big_endian
>
6287 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
6288 unsigned int r_type
)
6290 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
6292 // These are the relocation types supported by glibc for both 32-bit
6293 // and 64-bit powerpc.
6296 case elfcpp::R_POWERPC_NONE
:
6297 case elfcpp::R_POWERPC_RELATIVE
:
6298 case elfcpp::R_POWERPC_GLOB_DAT
:
6299 case elfcpp::R_POWERPC_DTPMOD
:
6300 case elfcpp::R_POWERPC_DTPREL
:
6301 case elfcpp::R_POWERPC_TPREL
:
6302 case elfcpp::R_POWERPC_JMP_SLOT
:
6303 case elfcpp::R_POWERPC_COPY
:
6304 case elfcpp::R_POWERPC_IRELATIVE
:
6305 case elfcpp::R_POWERPC_ADDR32
:
6306 case elfcpp::R_POWERPC_UADDR32
:
6307 case elfcpp::R_POWERPC_ADDR24
:
6308 case elfcpp::R_POWERPC_ADDR16
:
6309 case elfcpp::R_POWERPC_UADDR16
:
6310 case elfcpp::R_POWERPC_ADDR16_LO
:
6311 case elfcpp::R_POWERPC_ADDR16_HI
:
6312 case elfcpp::R_POWERPC_ADDR16_HA
:
6313 case elfcpp::R_POWERPC_ADDR14
:
6314 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6315 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6316 case elfcpp::R_POWERPC_REL32
:
6317 case elfcpp::R_POWERPC_REL24
:
6318 case elfcpp::R_POWERPC_TPREL16
:
6319 case elfcpp::R_POWERPC_TPREL16_LO
:
6320 case elfcpp::R_POWERPC_TPREL16_HI
:
6321 case elfcpp::R_POWERPC_TPREL16_HA
:
6332 // These are the relocation types supported only on 64-bit.
6333 case elfcpp::R_PPC64_ADDR64
:
6334 case elfcpp::R_PPC64_UADDR64
:
6335 case elfcpp::R_PPC64_JMP_IREL
:
6336 case elfcpp::R_PPC64_ADDR16_DS
:
6337 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6338 case elfcpp::R_PPC64_ADDR16_HIGH
:
6339 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6340 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6341 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6342 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6343 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6344 case elfcpp::R_PPC64_REL64
:
6345 case elfcpp::R_POWERPC_ADDR30
:
6346 case elfcpp::R_PPC64_TPREL16_DS
:
6347 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6348 case elfcpp::R_PPC64_TPREL16_HIGH
:
6349 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6350 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6351 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6352 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6353 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6364 // These are the relocation types supported only on 32-bit.
6365 // ??? glibc ld.so doesn't need to support these.
6366 case elfcpp::R_POWERPC_DTPREL16
:
6367 case elfcpp::R_POWERPC_DTPREL16_LO
:
6368 case elfcpp::R_POWERPC_DTPREL16_HI
:
6369 case elfcpp::R_POWERPC_DTPREL16_HA
:
6377 // This prevents us from issuing more than one error per reloc
6378 // section. But we can still wind up issuing more than one
6379 // error per object file.
6380 if (this->issued_non_pic_error_
)
6382 gold_assert(parameters
->options().output_is_position_independent());
6383 object
->error(_("requires unsupported dynamic reloc; "
6384 "recompile with -fPIC"));
6385 this->issued_non_pic_error_
= true;
6389 // Return whether we need to make a PLT entry for a relocation of the
6390 // given type against a STT_GNU_IFUNC symbol.
6392 template<int size
, bool big_endian
>
6394 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
6395 Target_powerpc
<size
, big_endian
>* target
,
6396 Sized_relobj_file
<size
, big_endian
>* object
,
6397 unsigned int r_type
,
6400 // In non-pic code any reference will resolve to the plt call stub
6401 // for the ifunc symbol.
6402 if ((size
== 32 || target
->abiversion() >= 2)
6403 && !parameters
->options().output_is_position_independent())
6408 // Word size refs from data sections are OK, but don't need a PLT entry.
6409 case elfcpp::R_POWERPC_ADDR32
:
6410 case elfcpp::R_POWERPC_UADDR32
:
6415 case elfcpp::R_PPC64_ADDR64
:
6416 case elfcpp::R_PPC64_UADDR64
:
6421 // GOT refs are good, but also don't need a PLT entry.
6422 case elfcpp::R_POWERPC_GOT16
:
6423 case elfcpp::R_POWERPC_GOT16_LO
:
6424 case elfcpp::R_POWERPC_GOT16_HI
:
6425 case elfcpp::R_POWERPC_GOT16_HA
:
6426 case elfcpp::R_PPC64_GOT16_DS
:
6427 case elfcpp::R_PPC64_GOT16_LO_DS
:
6430 // Function calls are good, and these do need a PLT entry.
6431 case elfcpp::R_POWERPC_ADDR24
:
6432 case elfcpp::R_POWERPC_ADDR14
:
6433 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6434 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6435 case elfcpp::R_POWERPC_REL24
:
6436 case elfcpp::R_PPC_PLTREL24
:
6437 case elfcpp::R_POWERPC_REL14
:
6438 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6439 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6446 // Anything else is a problem.
6447 // If we are building a static executable, the libc startup function
6448 // responsible for applying indirect function relocations is going
6449 // to complain about the reloc type.
6450 // If we are building a dynamic executable, we will have a text
6451 // relocation. The dynamic loader will set the text segment
6452 // writable and non-executable to apply text relocations. So we'll
6453 // segfault when trying to run the indirection function to resolve
6456 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
6457 object
->name().c_str(), r_type
);
6461 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6465 ok_lo_toc_insn(uint32_t insn
, unsigned int r_type
)
6467 return ((insn
& (0x3f << 26)) == 12u << 26 /* addic */
6468 || (insn
& (0x3f << 26)) == 14u << 26 /* addi */
6469 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
6470 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
6471 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
6472 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
6473 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
6474 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
6475 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
6476 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
6477 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
6478 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
6479 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
6480 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
6481 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
6482 || (insn
& (0x3f << 26)) == 56u << 26 /* lq,lfq */
6483 || ((insn
& (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
6484 /* Exclude lfqu by testing reloc. If relocs are ever
6485 defined for the reduced D field in psq_lu then those
6486 will need testing too. */
6487 && r_type
!= elfcpp::R_PPC64_TOC16_LO
6488 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
6489 || ((insn
& (0x3f << 26)) == 58u << 26 /* ld,lwa */
6491 || (insn
& (0x3f << 26)) == 60u << 26 /* stfq */
6492 || ((insn
& (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
6493 /* Exclude stfqu. psq_stu as above for psq_lu. */
6494 && r_type
!= elfcpp::R_PPC64_TOC16_LO
6495 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
6496 || ((insn
& (0x3f << 26)) == 62u << 26 /* std,stq */
6497 && (insn
& 1) == 0));
6500 // Scan a relocation for a local symbol.
6502 template<int size
, bool big_endian
>
6504 Target_powerpc
<size
, big_endian
>::Scan::local(
6505 Symbol_table
* symtab
,
6507 Target_powerpc
<size
, big_endian
>* target
,
6508 Sized_relobj_file
<size
, big_endian
>* object
,
6509 unsigned int data_shndx
,
6510 Output_section
* output_section
,
6511 const elfcpp::Rela
<size
, big_endian
>& reloc
,
6512 unsigned int r_type
,
6513 const elfcpp::Sym
<size
, big_endian
>& lsym
,
6516 this->maybe_skip_tls_get_addr_call(target
, r_type
, NULL
);
6518 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6519 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6521 this->expect_tls_get_addr_call();
6522 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
6523 if (tls_type
!= tls::TLSOPT_NONE
)
6524 this->skip_next_tls_get_addr_call();
6526 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6527 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6529 this->expect_tls_get_addr_call();
6530 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6531 if (tls_type
!= tls::TLSOPT_NONE
)
6532 this->skip_next_tls_get_addr_call();
6535 Powerpc_relobj
<size
, big_endian
>* ppc_object
6536 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6541 && data_shndx
== ppc_object
->opd_shndx()
6542 && r_type
== elfcpp::R_PPC64_ADDR64
)
6543 ppc_object
->set_opd_discard(reloc
.get_r_offset());
6547 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6548 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
6549 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
6551 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6552 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6553 r_type
, r_sym
, reloc
.get_r_addend());
6554 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
6559 case elfcpp::R_POWERPC_NONE
:
6560 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6561 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6562 case elfcpp::R_POWERPC_TLS
:
6563 case elfcpp::R_PPC64_ENTRY
:
6566 case elfcpp::R_PPC64_TOC
:
6568 Output_data_got_powerpc
<size
, big_endian
>* got
6569 = target
->got_section(symtab
, layout
);
6570 if (parameters
->options().output_is_position_independent())
6572 Address off
= reloc
.get_r_offset();
6574 && target
->abiversion() < 2
6575 && data_shndx
== ppc_object
->opd_shndx()
6576 && ppc_object
->get_opd_discard(off
- 8))
6579 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6580 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
6581 rela_dyn
->add_output_section_relative(got
->output_section(),
6582 elfcpp::R_POWERPC_RELATIVE
,
6584 object
, data_shndx
, off
,
6585 symobj
->toc_base_offset());
6590 case elfcpp::R_PPC64_ADDR64
:
6591 case elfcpp::R_PPC64_UADDR64
:
6592 case elfcpp::R_POWERPC_ADDR32
:
6593 case elfcpp::R_POWERPC_UADDR32
:
6594 case elfcpp::R_POWERPC_ADDR24
:
6595 case elfcpp::R_POWERPC_ADDR16
:
6596 case elfcpp::R_POWERPC_ADDR16_LO
:
6597 case elfcpp::R_POWERPC_ADDR16_HI
:
6598 case elfcpp::R_POWERPC_ADDR16_HA
:
6599 case elfcpp::R_POWERPC_UADDR16
:
6600 case elfcpp::R_PPC64_ADDR16_HIGH
:
6601 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6602 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6603 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6604 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6605 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6606 case elfcpp::R_PPC64_ADDR16_DS
:
6607 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6608 case elfcpp::R_POWERPC_ADDR14
:
6609 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6610 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6611 // If building a shared library (or a position-independent
6612 // executable), we need to create a dynamic relocation for
6614 if (parameters
->options().output_is_position_independent()
6615 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
6617 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
6619 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6620 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
6621 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
6623 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6624 : elfcpp::R_POWERPC_RELATIVE
);
6625 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
6626 output_section
, data_shndx
,
6627 reloc
.get_r_offset(),
6628 reloc
.get_r_addend(), false);
6630 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
6632 check_non_pic(object
, r_type
);
6633 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
6634 data_shndx
, reloc
.get_r_offset(),
6635 reloc
.get_r_addend());
6639 gold_assert(lsym
.get_st_value() == 0);
6640 unsigned int shndx
= lsym
.get_st_shndx();
6642 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
6645 object
->error(_("section symbol %u has bad shndx %u"),
6648 rela_dyn
->add_local_section(object
, shndx
, r_type
,
6649 output_section
, data_shndx
,
6650 reloc
.get_r_offset());
6655 case elfcpp::R_POWERPC_REL24
:
6656 case elfcpp::R_PPC_PLTREL24
:
6657 case elfcpp::R_PPC_LOCAL24PC
:
6658 case elfcpp::R_POWERPC_REL14
:
6659 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6660 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6663 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6664 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6665 r_type
, r_sym
, reloc
.get_r_addend());
6669 case elfcpp::R_PPC64_TOCSAVE
:
6670 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6671 // caller has already saved r2 and thus a plt call stub need not
6674 && target
->mark_pltcall(ppc_object
, data_shndx
,
6675 reloc
.get_r_offset() - 4, symtab
))
6677 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6678 unsigned int shndx
= lsym
.get_st_shndx();
6680 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6682 object
->error(_("tocsave symbol %u has bad shndx %u"),
6685 target
->add_tocsave(ppc_object
, shndx
,
6686 lsym
.get_st_value() + reloc
.get_r_addend());
6690 case elfcpp::R_PPC64_REL64
:
6691 case elfcpp::R_POWERPC_REL32
:
6692 case elfcpp::R_POWERPC_REL16
:
6693 case elfcpp::R_POWERPC_REL16_LO
:
6694 case elfcpp::R_POWERPC_REL16_HI
:
6695 case elfcpp::R_POWERPC_REL16_HA
:
6696 case elfcpp::R_POWERPC_REL16DX_HA
:
6697 case elfcpp::R_POWERPC_SECTOFF
:
6698 case elfcpp::R_POWERPC_SECTOFF_LO
:
6699 case elfcpp::R_POWERPC_SECTOFF_HI
:
6700 case elfcpp::R_POWERPC_SECTOFF_HA
:
6701 case elfcpp::R_PPC64_SECTOFF_DS
:
6702 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6703 case elfcpp::R_POWERPC_TPREL16
:
6704 case elfcpp::R_POWERPC_TPREL16_LO
:
6705 case elfcpp::R_POWERPC_TPREL16_HI
:
6706 case elfcpp::R_POWERPC_TPREL16_HA
:
6707 case elfcpp::R_PPC64_TPREL16_DS
:
6708 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6709 case elfcpp::R_PPC64_TPREL16_HIGH
:
6710 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6711 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6712 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6713 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6714 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6715 case elfcpp::R_POWERPC_DTPREL16
:
6716 case elfcpp::R_POWERPC_DTPREL16_LO
:
6717 case elfcpp::R_POWERPC_DTPREL16_HI
:
6718 case elfcpp::R_POWERPC_DTPREL16_HA
:
6719 case elfcpp::R_PPC64_DTPREL16_DS
:
6720 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6721 case elfcpp::R_PPC64_DTPREL16_HIGH
:
6722 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
6723 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6724 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6725 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6726 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6727 case elfcpp::R_PPC64_TLSGD
:
6728 case elfcpp::R_PPC64_TLSLD
:
6729 case elfcpp::R_PPC64_ADDR64_LOCAL
:
6732 case elfcpp::R_POWERPC_GOT16
:
6733 case elfcpp::R_POWERPC_GOT16_LO
:
6734 case elfcpp::R_POWERPC_GOT16_HI
:
6735 case elfcpp::R_POWERPC_GOT16_HA
:
6736 case elfcpp::R_PPC64_GOT16_DS
:
6737 case elfcpp::R_PPC64_GOT16_LO_DS
:
6739 // The symbol requires a GOT entry.
6740 Output_data_got_powerpc
<size
, big_endian
>* got
6741 = target
->got_section(symtab
, layout
);
6742 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6744 if (!parameters
->options().output_is_position_independent())
6747 && (size
== 32 || target
->abiversion() >= 2))
6748 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
6750 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
6752 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
6754 // If we are generating a shared object or a pie, this
6755 // symbol's GOT entry will be set by a dynamic relocation.
6757 off
= got
->add_constant(0);
6758 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
6760 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
6762 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6763 : elfcpp::R_POWERPC_RELATIVE
);
6764 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
6765 got
, off
, 0, false);
6770 case elfcpp::R_PPC64_TOC16
:
6771 case elfcpp::R_PPC64_TOC16_LO
:
6772 case elfcpp::R_PPC64_TOC16_HI
:
6773 case elfcpp::R_PPC64_TOC16_HA
:
6774 case elfcpp::R_PPC64_TOC16_DS
:
6775 case elfcpp::R_PPC64_TOC16_LO_DS
:
6776 // We need a GOT section.
6777 target
->got_section(symtab
, layout
);
6780 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6781 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6782 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6783 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6785 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
6786 if (tls_type
== tls::TLSOPT_NONE
)
6788 Output_data_got_powerpc
<size
, big_endian
>* got
6789 = target
->got_section(symtab
, layout
);
6790 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6791 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6792 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
6793 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
6795 else if (tls_type
== tls::TLSOPT_TO_LE
)
6797 // no GOT relocs needed for Local Exec.
6804 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6805 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6806 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6807 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6809 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6810 if (tls_type
== tls::TLSOPT_NONE
)
6811 target
->tlsld_got_offset(symtab
, layout
, object
);
6812 else if (tls_type
== tls::TLSOPT_TO_LE
)
6814 // no GOT relocs needed for Local Exec.
6815 if (parameters
->options().emit_relocs())
6817 Output_section
* os
= layout
->tls_segment()->first_section();
6818 gold_assert(os
!= NULL
);
6819 os
->set_needs_symtab_index();
6827 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6828 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6829 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6830 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6832 Output_data_got_powerpc
<size
, big_endian
>* got
6833 = target
->got_section(symtab
, layout
);
6834 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6835 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
6839 case elfcpp::R_POWERPC_GOT_TPREL16
:
6840 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6841 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6842 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6844 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
6845 if (tls_type
== tls::TLSOPT_NONE
)
6847 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6848 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
6850 Output_data_got_powerpc
<size
, big_endian
>* got
6851 = target
->got_section(symtab
, layout
);
6852 unsigned int off
= got
->add_constant(0);
6853 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
6855 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6856 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
6857 elfcpp::R_POWERPC_TPREL
,
6861 else if (tls_type
== tls::TLSOPT_TO_LE
)
6863 // no GOT relocs needed for Local Exec.
6871 unsupported_reloc_local(object
, r_type
);
6876 && parameters
->options().toc_optimize())
6878 if (data_shndx
== ppc_object
->toc_shndx())
6881 if (r_type
!= elfcpp::R_PPC64_ADDR64
6882 || (is_ifunc
&& target
->abiversion() < 2))
6884 else if (parameters
->options().output_is_position_independent())
6890 unsigned int shndx
= lsym
.get_st_shndx();
6891 if (shndx
>= elfcpp::SHN_LORESERVE
6892 && shndx
!= elfcpp::SHN_XINDEX
)
6897 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
6900 enum {no_check
, check_lo
, check_ha
} insn_check
;
6904 insn_check
= no_check
;
6907 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6908 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6909 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6910 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6911 case elfcpp::R_POWERPC_GOT16_HA
:
6912 case elfcpp::R_PPC64_TOC16_HA
:
6913 insn_check
= check_ha
;
6916 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6917 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6918 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6919 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6920 case elfcpp::R_POWERPC_GOT16_LO
:
6921 case elfcpp::R_PPC64_GOT16_LO_DS
:
6922 case elfcpp::R_PPC64_TOC16_LO
:
6923 case elfcpp::R_PPC64_TOC16_LO_DS
:
6924 insn_check
= check_lo
;
6928 section_size_type slen
;
6929 const unsigned char* view
= NULL
;
6930 if (insn_check
!= no_check
)
6932 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
6933 section_size_type off
=
6934 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
6937 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
6938 if (insn_check
== check_lo
6939 ? !ok_lo_toc_insn(insn
, r_type
)
6940 : ((insn
& ((0x3f << 26) | 0x1f << 16))
6941 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
6943 ppc_object
->set_no_toc_opt();
6944 gold_warning(_("%s: toc optimization is not supported "
6945 "for %#08x instruction"),
6946 ppc_object
->name().c_str(), insn
);
6955 case elfcpp::R_PPC64_TOC16
:
6956 case elfcpp::R_PPC64_TOC16_LO
:
6957 case elfcpp::R_PPC64_TOC16_HI
:
6958 case elfcpp::R_PPC64_TOC16_HA
:
6959 case elfcpp::R_PPC64_TOC16_DS
:
6960 case elfcpp::R_PPC64_TOC16_LO_DS
:
6961 unsigned int shndx
= lsym
.get_st_shndx();
6962 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6964 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6965 if (is_ordinary
&& shndx
== ppc_object
->toc_shndx())
6967 Address dst_off
= lsym
.get_st_value() + reloc
.get_r_addend();
6968 if (dst_off
< ppc_object
->section_size(shndx
))
6971 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
6973 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
6975 // Need to check that the insn is a ld
6977 view
= ppc_object
->section_contents(data_shndx
,
6980 section_size_type off
=
6981 (convert_to_section_size_type(reloc
.get_r_offset())
6982 + (big_endian
? -2 : 3));
6984 && (view
[off
] & (0x3f << 2)) == 58u << 2)
6988 ppc_object
->set_no_toc_opt(dst_off
);
6999 case elfcpp::R_POWERPC_REL32
:
7000 if (ppc_object
->got2_shndx() != 0
7001 && parameters
->options().output_is_position_independent())
7003 unsigned int shndx
= lsym
.get_st_shndx();
7004 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7006 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
7007 if (is_ordinary
&& shndx
== ppc_object
->got2_shndx()
7008 && (ppc_object
->section_flags(data_shndx
)
7009 & elfcpp::SHF_EXECINSTR
) != 0)
7010 gold_error(_("%s: unsupported -mbss-plt code"),
7011 ppc_object
->name().c_str());
7021 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7022 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7023 case elfcpp::R_POWERPC_GOT_TPREL16
:
7024 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7025 case elfcpp::R_POWERPC_GOT16
:
7026 case elfcpp::R_PPC64_GOT16_DS
:
7027 case elfcpp::R_PPC64_TOC16
:
7028 case elfcpp::R_PPC64_TOC16_DS
:
7029 ppc_object
->set_has_small_toc_reloc();
7035 // Report an unsupported relocation against a global symbol.
7037 template<int size
, bool big_endian
>
7039 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
7040 Sized_relobj_file
<size
, big_endian
>* object
,
7041 unsigned int r_type
,
7044 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7045 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
7048 // Scan a relocation for a global symbol.
7050 template<int size
, bool big_endian
>
7052 Target_powerpc
<size
, big_endian
>::Scan::global(
7053 Symbol_table
* symtab
,
7055 Target_powerpc
<size
, big_endian
>* target
,
7056 Sized_relobj_file
<size
, big_endian
>* object
,
7057 unsigned int data_shndx
,
7058 Output_section
* output_section
,
7059 const elfcpp::Rela
<size
, big_endian
>& reloc
,
7060 unsigned int r_type
,
7063 if (this->maybe_skip_tls_get_addr_call(target
, r_type
, gsym
)
7067 if (target
->replace_tls_get_addr(gsym
))
7068 // Change a __tls_get_addr reference to __tls_get_addr_opt
7069 // so dynamic relocs are emitted against the latter symbol.
7070 gsym
= target
->tls_get_addr_opt();
7072 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
7073 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
7075 this->expect_tls_get_addr_call();
7076 const bool final
= gsym
->final_value_is_known();
7077 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7078 if (tls_type
!= tls::TLSOPT_NONE
)
7079 this->skip_next_tls_get_addr_call();
7081 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
7082 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
7084 this->expect_tls_get_addr_call();
7085 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7086 if (tls_type
!= tls::TLSOPT_NONE
)
7087 this->skip_next_tls_get_addr_call();
7090 Powerpc_relobj
<size
, big_endian
>* ppc_object
7091 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
7093 // A STT_GNU_IFUNC symbol may require a PLT entry.
7094 bool is_ifunc
= gsym
->type() == elfcpp::STT_GNU_IFUNC
;
7095 bool pushed_ifunc
= false;
7096 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
7098 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7099 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
7100 r_type
, r_sym
, reloc
.get_r_addend());
7101 target
->make_plt_entry(symtab
, layout
, gsym
);
7102 pushed_ifunc
= true;
7107 case elfcpp::R_POWERPC_NONE
:
7108 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
7109 case elfcpp::R_POWERPC_GNU_VTENTRY
:
7110 case elfcpp::R_PPC_LOCAL24PC
:
7111 case elfcpp::R_POWERPC_TLS
:
7112 case elfcpp::R_PPC64_ENTRY
:
7115 case elfcpp::R_PPC64_TOC
:
7117 Output_data_got_powerpc
<size
, big_endian
>* got
7118 = target
->got_section(symtab
, layout
);
7119 if (parameters
->options().output_is_position_independent())
7121 Address off
= reloc
.get_r_offset();
7123 && data_shndx
== ppc_object
->opd_shndx()
7124 && ppc_object
->get_opd_discard(off
- 8))
7127 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7128 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
7129 if (data_shndx
!= ppc_object
->opd_shndx())
7130 symobj
= static_cast
7131 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7132 rela_dyn
->add_output_section_relative(got
->output_section(),
7133 elfcpp::R_POWERPC_RELATIVE
,
7135 object
, data_shndx
, off
,
7136 symobj
->toc_base_offset());
7141 case elfcpp::R_PPC64_ADDR64
:
7143 && target
->abiversion() < 2
7144 && data_shndx
== ppc_object
->opd_shndx()
7145 && (gsym
->is_defined_in_discarded_section()
7146 || gsym
->object() != object
))
7148 ppc_object
->set_opd_discard(reloc
.get_r_offset());
7152 case elfcpp::R_PPC64_UADDR64
:
7153 case elfcpp::R_POWERPC_ADDR32
:
7154 case elfcpp::R_POWERPC_UADDR32
:
7155 case elfcpp::R_POWERPC_ADDR24
:
7156 case elfcpp::R_POWERPC_ADDR16
:
7157 case elfcpp::R_POWERPC_ADDR16_LO
:
7158 case elfcpp::R_POWERPC_ADDR16_HI
:
7159 case elfcpp::R_POWERPC_ADDR16_HA
:
7160 case elfcpp::R_POWERPC_UADDR16
:
7161 case elfcpp::R_PPC64_ADDR16_HIGH
:
7162 case elfcpp::R_PPC64_ADDR16_HIGHA
:
7163 case elfcpp::R_PPC64_ADDR16_HIGHER
:
7164 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
7165 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
7166 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
7167 case elfcpp::R_PPC64_ADDR16_DS
:
7168 case elfcpp::R_PPC64_ADDR16_LO_DS
:
7169 case elfcpp::R_POWERPC_ADDR14
:
7170 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
7171 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
7173 // Make a PLT entry if necessary.
7174 if (gsym
->needs_plt_entry())
7176 // Since this is not a PC-relative relocation, we may be
7177 // taking the address of a function. In that case we need to
7178 // set the entry in the dynamic symbol table to the address of
7179 // the PLT call stub.
7180 bool need_ifunc_plt
= false;
7181 if ((size
== 32 || target
->abiversion() >= 2)
7182 && gsym
->is_from_dynobj()
7183 && !parameters
->options().output_is_position_independent())
7185 gsym
->set_needs_dynsym_value();
7186 need_ifunc_plt
= true;
7188 if (!is_ifunc
|| (!pushed_ifunc
&& need_ifunc_plt
))
7190 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7191 target
->push_branch(ppc_object
, data_shndx
,
7192 reloc
.get_r_offset(), r_type
, r_sym
,
7193 reloc
.get_r_addend());
7194 target
->make_plt_entry(symtab
, layout
, gsym
);
7197 // Make a dynamic relocation if necessary.
7198 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
))
7199 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
7201 if (!parameters
->options().output_is_position_independent()
7202 && gsym
->may_need_copy_reloc())
7204 target
->copy_reloc(symtab
, layout
, object
,
7205 data_shndx
, output_section
, gsym
, reloc
);
7207 else if ((((size
== 32
7208 && r_type
== elfcpp::R_POWERPC_ADDR32
)
7210 && r_type
== elfcpp::R_PPC64_ADDR64
7211 && target
->abiversion() >= 2))
7212 && gsym
->can_use_relative_reloc(false)
7213 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
7214 && parameters
->options().shared()))
7216 && r_type
== elfcpp::R_PPC64_ADDR64
7217 && target
->abiversion() < 2
7218 && (gsym
->can_use_relative_reloc(false)
7219 || data_shndx
== ppc_object
->opd_shndx())))
7221 Reloc_section
* rela_dyn
7222 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
7223 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
7224 : elfcpp::R_POWERPC_RELATIVE
);
7225 rela_dyn
->add_symbolless_global_addend(
7226 gsym
, dynrel
, output_section
, object
, data_shndx
,
7227 reloc
.get_r_offset(), reloc
.get_r_addend());
7231 Reloc_section
* rela_dyn
7232 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
7233 check_non_pic(object
, r_type
);
7234 rela_dyn
->add_global(gsym
, r_type
, output_section
,
7236 reloc
.get_r_offset(),
7237 reloc
.get_r_addend());
7240 && parameters
->options().toc_optimize()
7241 && data_shndx
== ppc_object
->toc_shndx())
7242 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
7248 case elfcpp::R_PPC_PLTREL24
:
7249 case elfcpp::R_POWERPC_REL24
:
7252 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7253 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
7254 r_type
, r_sym
, reloc
.get_r_addend());
7255 if (gsym
->needs_plt_entry()
7256 || (!gsym
->final_value_is_known()
7257 && (gsym
->is_undefined()
7258 || gsym
->is_from_dynobj()
7259 || gsym
->is_preemptible())))
7260 target
->make_plt_entry(symtab
, layout
, gsym
);
7264 case elfcpp::R_PPC64_REL64
:
7265 case elfcpp::R_POWERPC_REL32
:
7266 // Make a dynamic relocation if necessary.
7267 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
)))
7269 if (!parameters
->options().output_is_position_independent()
7270 && gsym
->may_need_copy_reloc())
7272 target
->copy_reloc(symtab
, layout
, object
,
7273 data_shndx
, output_section
, gsym
,
7278 Reloc_section
* rela_dyn
7279 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
7280 check_non_pic(object
, r_type
);
7281 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
7282 data_shndx
, reloc
.get_r_offset(),
7283 reloc
.get_r_addend());
7288 case elfcpp::R_POWERPC_REL14
:
7289 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7290 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7293 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7294 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
7295 r_type
, r_sym
, reloc
.get_r_addend());
7299 case elfcpp::R_PPC64_TOCSAVE
:
7300 // R_PPC64_TOCSAVE follows a call instruction to indicate the
7301 // caller has already saved r2 and thus a plt call stub need not
7304 && target
->mark_pltcall(ppc_object
, data_shndx
,
7305 reloc
.get_r_offset() - 4, symtab
))
7307 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7309 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
7311 object
->error(_("tocsave symbol %u has bad shndx %u"),
7315 Sized_symbol
<size
>* sym
= symtab
->get_sized_symbol
<size
>(gsym
);
7316 target
->add_tocsave(ppc_object
, shndx
,
7317 sym
->value() + reloc
.get_r_addend());
7322 case elfcpp::R_POWERPC_REL16
:
7323 case elfcpp::R_POWERPC_REL16_LO
:
7324 case elfcpp::R_POWERPC_REL16_HI
:
7325 case elfcpp::R_POWERPC_REL16_HA
:
7326 case elfcpp::R_POWERPC_REL16DX_HA
:
7327 case elfcpp::R_POWERPC_SECTOFF
:
7328 case elfcpp::R_POWERPC_SECTOFF_LO
:
7329 case elfcpp::R_POWERPC_SECTOFF_HI
:
7330 case elfcpp::R_POWERPC_SECTOFF_HA
:
7331 case elfcpp::R_PPC64_SECTOFF_DS
:
7332 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
7333 case elfcpp::R_POWERPC_TPREL16
:
7334 case elfcpp::R_POWERPC_TPREL16_LO
:
7335 case elfcpp::R_POWERPC_TPREL16_HI
:
7336 case elfcpp::R_POWERPC_TPREL16_HA
:
7337 case elfcpp::R_PPC64_TPREL16_DS
:
7338 case elfcpp::R_PPC64_TPREL16_LO_DS
:
7339 case elfcpp::R_PPC64_TPREL16_HIGH
:
7340 case elfcpp::R_PPC64_TPREL16_HIGHA
:
7341 case elfcpp::R_PPC64_TPREL16_HIGHER
:
7342 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
7343 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
7344 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
7345 case elfcpp::R_POWERPC_DTPREL16
:
7346 case elfcpp::R_POWERPC_DTPREL16_LO
:
7347 case elfcpp::R_POWERPC_DTPREL16_HI
:
7348 case elfcpp::R_POWERPC_DTPREL16_HA
:
7349 case elfcpp::R_PPC64_DTPREL16_DS
:
7350 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
7351 case elfcpp::R_PPC64_DTPREL16_HIGH
:
7352 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
7353 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
7354 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
7355 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
7356 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
7357 case elfcpp::R_PPC64_TLSGD
:
7358 case elfcpp::R_PPC64_TLSLD
:
7359 case elfcpp::R_PPC64_ADDR64_LOCAL
:
7362 case elfcpp::R_POWERPC_GOT16
:
7363 case elfcpp::R_POWERPC_GOT16_LO
:
7364 case elfcpp::R_POWERPC_GOT16_HI
:
7365 case elfcpp::R_POWERPC_GOT16_HA
:
7366 case elfcpp::R_PPC64_GOT16_DS
:
7367 case elfcpp::R_PPC64_GOT16_LO_DS
:
7369 // The symbol requires a GOT entry.
7370 Output_data_got_powerpc
<size
, big_endian
>* got
;
7372 got
= target
->got_section(symtab
, layout
);
7373 if (gsym
->final_value_is_known())
7376 && (size
== 32 || target
->abiversion() >= 2))
7377 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
7379 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
7381 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
7383 // If we are generating a shared object or a pie, this
7384 // symbol's GOT entry will be set by a dynamic relocation.
7385 unsigned int off
= got
->add_constant(0);
7386 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
7388 Reloc_section
* rela_dyn
7389 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
7391 if (gsym
->can_use_relative_reloc(false)
7393 || target
->abiversion() >= 2)
7394 && gsym
->visibility() == elfcpp::STV_PROTECTED
7395 && parameters
->options().shared()))
7397 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
7398 : elfcpp::R_POWERPC_RELATIVE
);
7399 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
7403 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
7404 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
7410 case elfcpp::R_PPC64_TOC16
:
7411 case elfcpp::R_PPC64_TOC16_LO
:
7412 case elfcpp::R_PPC64_TOC16_HI
:
7413 case elfcpp::R_PPC64_TOC16_HA
:
7414 case elfcpp::R_PPC64_TOC16_DS
:
7415 case elfcpp::R_PPC64_TOC16_LO_DS
:
7416 // We need a GOT section.
7417 target
->got_section(symtab
, layout
);
7420 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7421 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7422 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
7423 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7425 const bool final
= gsym
->final_value_is_known();
7426 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7427 if (tls_type
== tls::TLSOPT_NONE
)
7429 Output_data_got_powerpc
<size
, big_endian
>* got
7430 = target
->got_section(symtab
, layout
);
7431 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7432 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
, rela_dyn
,
7433 elfcpp::R_POWERPC_DTPMOD
,
7434 elfcpp::R_POWERPC_DTPREL
);
7436 else if (tls_type
== tls::TLSOPT_TO_IE
)
7438 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
7440 Output_data_got_powerpc
<size
, big_endian
>* got
7441 = target
->got_section(symtab
, layout
);
7442 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7443 if (gsym
->is_undefined()
7444 || gsym
->is_from_dynobj())
7446 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
7447 elfcpp::R_POWERPC_TPREL
);
7451 unsigned int off
= got
->add_constant(0);
7452 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
7453 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
7454 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
7459 else if (tls_type
== tls::TLSOPT_TO_LE
)
7461 // no GOT relocs needed for Local Exec.
7468 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7469 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7470 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
7471 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7473 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7474 if (tls_type
== tls::TLSOPT_NONE
)
7475 target
->tlsld_got_offset(symtab
, layout
, object
);
7476 else if (tls_type
== tls::TLSOPT_TO_LE
)
7478 // no GOT relocs needed for Local Exec.
7479 if (parameters
->options().emit_relocs())
7481 Output_section
* os
= layout
->tls_segment()->first_section();
7482 gold_assert(os
!= NULL
);
7483 os
->set_needs_symtab_index();
7491 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7492 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7493 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
7494 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7496 Output_data_got_powerpc
<size
, big_endian
>* got
7497 = target
->got_section(symtab
, layout
);
7498 if (!gsym
->final_value_is_known()
7499 && (gsym
->is_from_dynobj()
7500 || gsym
->is_undefined()
7501 || gsym
->is_preemptible()))
7502 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
7503 target
->rela_dyn_section(layout
),
7504 elfcpp::R_POWERPC_DTPREL
);
7506 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
7510 case elfcpp::R_POWERPC_GOT_TPREL16
:
7511 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7512 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7513 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7515 const bool final
= gsym
->final_value_is_known();
7516 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
7517 if (tls_type
== tls::TLSOPT_NONE
)
7519 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
7521 Output_data_got_powerpc
<size
, big_endian
>* got
7522 = target
->got_section(symtab
, layout
);
7523 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7524 if (gsym
->is_undefined()
7525 || gsym
->is_from_dynobj())
7527 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
7528 elfcpp::R_POWERPC_TPREL
);
7532 unsigned int off
= got
->add_constant(0);
7533 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
7534 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
7535 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
7540 else if (tls_type
== tls::TLSOPT_TO_LE
)
7542 // no GOT relocs needed for Local Exec.
7550 unsupported_reloc_global(object
, r_type
, gsym
);
7555 && parameters
->options().toc_optimize())
7557 if (data_shndx
== ppc_object
->toc_shndx())
7560 if (r_type
!= elfcpp::R_PPC64_ADDR64
7561 || (is_ifunc
&& target
->abiversion() < 2))
7563 else if (parameters
->options().output_is_position_independent()
7564 && (is_ifunc
|| gsym
->is_absolute() || gsym
->is_undefined()))
7567 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
7570 enum {no_check
, check_lo
, check_ha
} insn_check
;
7574 insn_check
= no_check
;
7577 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7578 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7579 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7580 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7581 case elfcpp::R_POWERPC_GOT16_HA
:
7582 case elfcpp::R_PPC64_TOC16_HA
:
7583 insn_check
= check_ha
;
7586 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7587 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7588 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7589 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7590 case elfcpp::R_POWERPC_GOT16_LO
:
7591 case elfcpp::R_PPC64_GOT16_LO_DS
:
7592 case elfcpp::R_PPC64_TOC16_LO
:
7593 case elfcpp::R_PPC64_TOC16_LO_DS
:
7594 insn_check
= check_lo
;
7598 section_size_type slen
;
7599 const unsigned char* view
= NULL
;
7600 if (insn_check
!= no_check
)
7602 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
7603 section_size_type off
=
7604 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
7607 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
7608 if (insn_check
== check_lo
7609 ? !ok_lo_toc_insn(insn
, r_type
)
7610 : ((insn
& ((0x3f << 26) | 0x1f << 16))
7611 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7613 ppc_object
->set_no_toc_opt();
7614 gold_warning(_("%s: toc optimization is not supported "
7615 "for %#08x instruction"),
7616 ppc_object
->name().c_str(), insn
);
7625 case elfcpp::R_PPC64_TOC16
:
7626 case elfcpp::R_PPC64_TOC16_LO
:
7627 case elfcpp::R_PPC64_TOC16_HI
:
7628 case elfcpp::R_PPC64_TOC16_HA
:
7629 case elfcpp::R_PPC64_TOC16_DS
:
7630 case elfcpp::R_PPC64_TOC16_LO_DS
:
7631 if (gsym
->source() == Symbol::FROM_OBJECT
7632 && !gsym
->object()->is_dynamic())
7634 Powerpc_relobj
<size
, big_endian
>* sym_object
7635 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7637 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
7638 if (shndx
== sym_object
->toc_shndx())
7640 Sized_symbol
<size
>* sym
= symtab
->get_sized_symbol
<size
>(gsym
);
7641 Address dst_off
= sym
->value() + reloc
.get_r_addend();
7642 if (dst_off
< sym_object
->section_size(shndx
))
7645 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
7647 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
7649 // Need to check that the insn is a ld
7651 view
= ppc_object
->section_contents(data_shndx
,
7654 section_size_type off
=
7655 (convert_to_section_size_type(reloc
.get_r_offset())
7656 + (big_endian
? -2 : 3));
7658 && (view
[off
] & (0x3f << 2)) == (58u << 2))
7662 sym_object
->set_no_toc_opt(dst_off
);
7674 case elfcpp::R_PPC_LOCAL24PC
:
7675 if (strcmp(gsym
->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7676 gold_error(_("%s: unsupported -mbss-plt code"),
7677 ppc_object
->name().c_str());
7686 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7687 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7688 case elfcpp::R_POWERPC_GOT_TPREL16
:
7689 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7690 case elfcpp::R_POWERPC_GOT16
:
7691 case elfcpp::R_PPC64_GOT16_DS
:
7692 case elfcpp::R_PPC64_TOC16
:
7693 case elfcpp::R_PPC64_TOC16_DS
:
7694 ppc_object
->set_has_small_toc_reloc();
7700 // Process relocations for gc.
7702 template<int size
, bool big_endian
>
7704 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
7705 Symbol_table
* symtab
,
7707 Sized_relobj_file
<size
, big_endian
>* object
,
7708 unsigned int data_shndx
,
7710 const unsigned char* prelocs
,
7712 Output_section
* output_section
,
7713 bool needs_special_offset_handling
,
7714 size_t local_symbol_count
,
7715 const unsigned char* plocal_symbols
)
7717 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
7718 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
7721 Powerpc_relobj
<size
, big_endian
>* ppc_object
7722 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
7724 ppc_object
->set_opd_valid();
7725 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
7727 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
7728 for (p
= ppc_object
->access_from_map()->begin();
7729 p
!= ppc_object
->access_from_map()->end();
7732 Address dst_off
= p
->first
;
7733 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
7734 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
7735 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
7737 Relobj
* src_obj
= s
->first
;
7738 unsigned int src_indx
= s
->second
;
7739 symtab
->gc()->add_reference(src_obj
, src_indx
,
7740 ppc_object
, dst_indx
);
7744 ppc_object
->access_from_map()->clear();
7745 ppc_object
->process_gc_mark(symtab
);
7746 // Don't look at .opd relocs as .opd will reference everything.
7750 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
7759 needs_special_offset_handling
,
7764 // Handle target specific gc actions when adding a gc reference from
7765 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7766 // and DST_OFF. For powerpc64, this adds a referenc to the code
7767 // section of a function descriptor.
7769 template<int size
, bool big_endian
>
7771 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
7772 Symbol_table
* symtab
,
7774 unsigned int src_shndx
,
7776 unsigned int dst_shndx
,
7777 Address dst_off
) const
7779 if (size
!= 64 || dst_obj
->is_dynamic())
7782 Powerpc_relobj
<size
, big_endian
>* ppc_object
7783 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
7784 if (dst_shndx
!= 0 && dst_shndx
== ppc_object
->opd_shndx())
7786 if (ppc_object
->opd_valid())
7788 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
7789 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
7793 // If we haven't run scan_opd_relocs, we must delay
7794 // processing this function descriptor reference.
7795 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
7800 // Add any special sections for this symbol to the gc work list.
7801 // For powerpc64, this adds the code section of a function
7804 template<int size
, bool big_endian
>
7806 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
7807 Symbol_table
* symtab
,
7812 Powerpc_relobj
<size
, big_endian
>* ppc_object
7813 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
7815 unsigned int shndx
= sym
->shndx(&is_ordinary
);
7816 if (is_ordinary
&& shndx
!= 0 && shndx
== ppc_object
->opd_shndx())
7818 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
7819 Address dst_off
= gsym
->value();
7820 if (ppc_object
->opd_valid())
7822 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
7823 symtab
->gc()->worklist().push_back(Section_id(ppc_object
,
7827 ppc_object
->add_gc_mark(dst_off
);
7832 // For a symbol location in .opd, set LOC to the location of the
7835 template<int size
, bool big_endian
>
7837 Target_powerpc
<size
, big_endian
>::do_function_location(
7838 Symbol_location
* loc
) const
7840 if (size
== 64 && loc
->shndx
!= 0)
7842 if (loc
->object
->is_dynamic())
7844 Powerpc_dynobj
<size
, big_endian
>* ppc_object
7845 = static_cast<Powerpc_dynobj
<size
, big_endian
>*>(loc
->object
);
7846 if (loc
->shndx
== ppc_object
->opd_shndx())
7849 Address off
= loc
->offset
- ppc_object
->opd_address();
7850 loc
->shndx
= ppc_object
->get_opd_ent(off
, &dest_off
);
7851 loc
->offset
= dest_off
;
7856 const Powerpc_relobj
<size
, big_endian
>* ppc_object
7857 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(loc
->object
);
7858 if (loc
->shndx
== ppc_object
->opd_shndx())
7861 loc
->shndx
= ppc_object
->get_opd_ent(loc
->offset
, &dest_off
);
7862 loc
->offset
= dest_off
;
7868 // FNOFFSET in section SHNDX in OBJECT is the start of a function
7869 // compiled with -fsplit-stack. The function calls non-split-stack
7870 // code. Change the function to ensure it has enough stack space to
7871 // call some random function.
7873 template<int size
, bool big_endian
>
7875 Target_powerpc
<size
, big_endian
>::do_calls_non_split(
7878 section_offset_type fnoffset
,
7879 section_size_type fnsize
,
7880 const unsigned char* prelocs
,
7882 unsigned char* view
,
7883 section_size_type view_size
,
7885 std::string
* to
) const
7887 // 32-bit not supported.
7891 Target::do_calls_non_split(object
, shndx
, fnoffset
, fnsize
,
7892 prelocs
, reloc_count
, view
, view_size
,
7897 // The function always starts with
7898 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
7899 // addis %r12,%r1,-allocate@ha
7900 // addi %r12,%r12,-allocate@l
7902 // but note that the addis or addi may be replaced with a nop
7904 unsigned char *entry
= view
+ fnoffset
;
7905 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
7907 if ((insn
& 0xffff0000) == addis_2_12
)
7909 /* Skip ELFv2 global entry code. */
7911 insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
7914 unsigned char *pinsn
= entry
;
7916 const uint32_t ld_private_ss
= 0xe80d8fc0;
7917 if (insn
== ld_private_ss
)
7919 int32_t allocate
= 0;
7923 insn
= elfcpp::Swap
<32, big_endian
>::readval(pinsn
);
7924 if ((insn
& 0xffff0000) == addis_12_1
)
7925 allocate
+= (insn
& 0xffff) << 16;
7926 else if ((insn
& 0xffff0000) == addi_12_1
7927 || (insn
& 0xffff0000) == addi_12_12
)
7928 allocate
+= ((insn
& 0xffff) ^ 0x8000) - 0x8000;
7929 else if (insn
!= nop
)
7932 if (insn
== cmpld_7_12_0
&& pinsn
== entry
+ 12)
7934 int extra
= parameters
->options().split_stack_adjust_size();
7936 if (allocate
>= 0 || extra
< 0)
7938 object
->error(_("split-stack stack size overflow at "
7939 "section %u offset %0zx"),
7940 shndx
, static_cast<size_t>(fnoffset
));
7944 insn
= addis_12_1
| (((allocate
+ 0x8000) >> 16) & 0xffff);
7945 if (insn
!= addis_12_1
)
7947 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7949 insn
= addi_12_12
| (allocate
& 0xffff);
7950 if (insn
!= addi_12_12
)
7952 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7958 insn
= addi_12_1
| (allocate
& 0xffff);
7959 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
7962 if (pinsn
!= entry
+ 12)
7963 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, nop
);
7971 if (!object
->has_no_split_stack())
7972 object
->error(_("failed to match split-stack sequence at "
7973 "section %u offset %0zx"),
7974 shndx
, static_cast<size_t>(fnoffset
));
7978 // Scan relocations for a section.
7980 template<int size
, bool big_endian
>
7982 Target_powerpc
<size
, big_endian
>::scan_relocs(
7983 Symbol_table
* symtab
,
7985 Sized_relobj_file
<size
, big_endian
>* object
,
7986 unsigned int data_shndx
,
7987 unsigned int sh_type
,
7988 const unsigned char* prelocs
,
7990 Output_section
* output_section
,
7991 bool needs_special_offset_handling
,
7992 size_t local_symbol_count
,
7993 const unsigned char* plocal_symbols
)
7995 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
7996 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
7999 if (!this->plt_localentry0_init_
)
8001 bool plt_localentry0
= false;
8003 && this->abiversion() >= 2)
8005 if (parameters
->options().user_set_plt_localentry())
8006 plt_localentry0
= parameters
->options().plt_localentry();
8008 && symtab
->lookup("GLIBC_2.26", NULL
) == NULL
)
8009 gold_warning(_("--plt-localentry is especially dangerous without "
8010 "ld.so support to detect ABI violations"));
8012 this->plt_localentry0_
= plt_localentry0
;
8013 this->plt_localentry0_init_
= true;
8016 if (sh_type
== elfcpp::SHT_REL
)
8018 gold_error(_("%s: unsupported REL reloc section"),
8019 object
->name().c_str());
8023 gold::scan_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
8032 needs_special_offset_handling
,
8037 // Functor class for processing the global symbol table.
8038 // Removes symbols defined on discarded opd entries.
8040 template<bool big_endian
>
8041 class Global_symbol_visitor_opd
8044 Global_symbol_visitor_opd()
8048 operator()(Sized_symbol
<64>* sym
)
8050 if (sym
->has_symtab_index()
8051 || sym
->source() != Symbol::FROM_OBJECT
8052 || !sym
->in_real_elf())
8055 if (sym
->object()->is_dynamic())
8058 Powerpc_relobj
<64, big_endian
>* symobj
8059 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
8060 if (symobj
->opd_shndx() == 0)
8064 unsigned int shndx
= sym
->shndx(&is_ordinary
);
8065 if (shndx
== symobj
->opd_shndx()
8066 && symobj
->get_opd_discard(sym
->value()))
8068 sym
->set_undefined();
8069 sym
->set_visibility(elfcpp::STV_DEFAULT
);
8070 sym
->set_is_defined_in_discarded_section();
8071 sym
->set_symtab_index(-1U);
8076 template<int size
, bool big_endian
>
8078 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
8080 Symbol_table
* symtab
)
8084 Output_data_save_res
<size
, big_endian
>* savres
8085 = new Output_data_save_res
<size
, big_endian
>(symtab
);
8086 this->savres_section_
= savres
;
8087 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
8088 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
8089 savres
, ORDER_TEXT
, false);
8093 // Sort linker created .got section first (for the header), then input
8094 // sections belonging to files using small model code.
8096 template<bool big_endian
>
8097 class Sort_toc_sections
8101 operator()(const Output_section::Input_section
& is1
,
8102 const Output_section::Input_section
& is2
) const
8104 if (!is1
.is_input_section() && is2
.is_input_section())
8107 = (is1
.is_input_section()
8108 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
8109 ->has_small_toc_reloc()));
8111 = (is2
.is_input_section()
8112 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
8113 ->has_small_toc_reloc()));
8114 return small1
&& !small2
;
8118 // Finalize the sections.
8120 template<int size
, bool big_endian
>
8122 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
8124 const Input_objects
*,
8125 Symbol_table
* symtab
)
8127 if (parameters
->doing_static_link())
8129 // At least some versions of glibc elf-init.o have a strong
8130 // reference to __rela_iplt marker syms. A weak ref would be
8132 if (this->iplt_
!= NULL
)
8134 Reloc_section
* rel
= this->iplt_
->rel_plt();
8135 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
8136 Symbol_table::PREDEFINED
, rel
, 0, 0,
8137 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
8138 elfcpp::STV_HIDDEN
, 0, false, true);
8139 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
8140 Symbol_table::PREDEFINED
, rel
, 0, 0,
8141 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
8142 elfcpp::STV_HIDDEN
, 0, true, true);
8146 symtab
->define_as_constant("__rela_iplt_start", NULL
,
8147 Symbol_table::PREDEFINED
, 0, 0,
8148 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
8149 elfcpp::STV_HIDDEN
, 0, true, false);
8150 symtab
->define_as_constant("__rela_iplt_end", NULL
,
8151 Symbol_table::PREDEFINED
, 0, 0,
8152 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
8153 elfcpp::STV_HIDDEN
, 0, true, false);
8159 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
8160 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
8162 if (!parameters
->options().relocatable())
8164 this->define_save_restore_funcs(layout
, symtab
);
8166 // Annoyingly, we need to make these sections now whether or
8167 // not we need them. If we delay until do_relax then we
8168 // need to mess with the relaxation machinery checkpointing.
8169 this->got_section(symtab
, layout
);
8170 this->make_brlt_section(layout
);
8172 if (parameters
->options().toc_sort())
8174 Output_section
* os
= this->got_
->output_section();
8175 if (os
!= NULL
&& os
->input_sections().size() > 1)
8176 std::stable_sort(os
->input_sections().begin(),
8177 os
->input_sections().end(),
8178 Sort_toc_sections
<big_endian
>());
8183 // Fill in some more dynamic tags.
8184 Output_data_dynamic
* odyn
= layout
->dynamic_data();
8187 const Reloc_section
* rel_plt
= (this->plt_
== NULL
8189 : this->plt_
->rel_plt());
8190 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
8191 this->rela_dyn_
, true, size
== 32);
8195 if (this->got_
!= NULL
)
8197 this->got_
->finalize_data_size();
8198 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
8199 this->got_
, this->got_
->g_o_t());
8201 if (this->has_tls_get_addr_opt_
)
8202 odyn
->add_constant(elfcpp::DT_PPC_OPT
, elfcpp::PPC_OPT_TLS
);
8206 if (this->glink_
!= NULL
)
8208 this->glink_
->finalize_data_size();
8209 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
8211 (this->glink_
->pltresolve_size()
8214 if (this->has_localentry0_
|| this->has_tls_get_addr_opt_
)
8215 odyn
->add_constant(elfcpp::DT_PPC64_OPT
,
8216 ((this->has_localentry0_
8217 ? elfcpp::PPC64_OPT_LOCALENTRY
: 0)
8218 | (this->has_tls_get_addr_opt_
8219 ? elfcpp::PPC64_OPT_TLS
: 0)));
8223 // Emit any relocs we saved in an attempt to avoid generating COPY
8225 if (this->copy_relocs_
.any_saved_relocs())
8226 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
8229 // Emit any saved relocs, and mark toc entries using any of these
8230 // relocs as not optimizable.
8232 template<int sh_type
, int size
, bool big_endian
>
8234 Powerpc_copy_relocs
<sh_type
, size
, big_endian
>::emit(
8235 Output_data_reloc
<sh_type
, true, size
, big_endian
>* reloc_section
)
8238 && parameters
->options().toc_optimize())
8240 for (typename Copy_relocs
<sh_type
, size
, big_endian
>::
8241 Copy_reloc_entries::iterator p
= this->entries_
.begin();
8242 p
!= this->entries_
.end();
8245 typename Copy_relocs
<sh_type
, size
, big_endian
>::Copy_reloc_entry
&
8248 // If the symbol is no longer defined in a dynamic object,
8249 // then we emitted a COPY relocation. If it is still
8250 // dynamic then we'll need dynamic relocations and thus
8251 // can't optimize toc entries.
8252 if (entry
.sym_
->is_from_dynobj())
8254 Powerpc_relobj
<size
, big_endian
>* ppc_object
8255 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(entry
.relobj_
);
8256 if (entry
.shndx_
== ppc_object
->toc_shndx())
8257 ppc_object
->set_no_toc_opt(entry
.address_
);
8262 Copy_relocs
<sh_type
, size
, big_endian
>::emit(reloc_section
);
8265 // Return the value to use for a branch relocation.
8267 template<int size
, bool big_endian
>
8269 Target_powerpc
<size
, big_endian
>::symval_for_branch(
8270 const Symbol_table
* symtab
,
8271 const Sized_symbol
<size
>* gsym
,
8272 Powerpc_relobj
<size
, big_endian
>* object
,
8274 unsigned int *dest_shndx
)
8276 if (size
== 32 || this->abiversion() >= 2)
8280 // If the symbol is defined in an opd section, ie. is a function
8281 // descriptor, use the function descriptor code entry address
8282 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
8284 && (gsym
->source() != Symbol::FROM_OBJECT
8285 || gsym
->object()->is_dynamic()))
8288 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
8289 unsigned int shndx
= symobj
->opd_shndx();
8292 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
8293 if (opd_addr
== invalid_address
)
8295 opd_addr
+= symobj
->output_section_address(shndx
);
8296 if (*value
>= opd_addr
&& *value
< opd_addr
+ symobj
->section_size(shndx
))
8299 *dest_shndx
= symobj
->get_opd_ent(*value
- opd_addr
, &sec_off
);
8300 if (symtab
->is_section_folded(symobj
, *dest_shndx
))
8303 = symtab
->icf()->get_folded_section(symobj
, *dest_shndx
);
8304 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(folded
.first
);
8305 *dest_shndx
= folded
.second
;
8307 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
8308 if (sec_addr
== invalid_address
)
8311 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
8312 *value
= sec_addr
+ sec_off
;
8317 // Perform a relocation.
8319 template<int size
, bool big_endian
>
8321 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
8322 const Relocate_info
<size
, big_endian
>* relinfo
,
8324 Target_powerpc
* target
,
8327 const unsigned char* preloc
,
8328 const Sized_symbol
<size
>* gsym
,
8329 const Symbol_value
<size
>* psymval
,
8330 unsigned char* view
,
8332 section_size_type view_size
)
8337 if (target
->replace_tls_get_addr(gsym
))
8338 gsym
= static_cast<const Sized_symbol
<size
>*>(target
->tls_get_addr_opt());
8340 const elfcpp::Rela
<size
, big_endian
> rela(preloc
);
8341 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
8342 switch (this->maybe_skip_tls_get_addr_call(target
, r_type
, gsym
))
8344 case Track_tls::NOT_EXPECTED
:
8345 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8346 _("__tls_get_addr call lacks marker reloc"));
8348 case Track_tls::EXPECTED
:
8349 // We have already complained.
8351 case Track_tls::SKIP
:
8353 case Track_tls::NORMAL
:
8357 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
8358 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
8359 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
8360 // Offset from start of insn to d-field reloc.
8361 const int d_offset
= big_endian
? 2 : 0;
8363 Powerpc_relobj
<size
, big_endian
>* const object
8364 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
8366 bool has_stub_value
= false;
8367 bool localentry0
= false;
8368 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
8370 ? gsym
->use_plt_offset(Scan::get_reference_flags(r_type
, target
))
8371 : object
->local_has_plt_offset(r_sym
))
8372 && (!psymval
->is_ifunc_symbol()
8373 || Scan::reloc_needs_plt_for_ifunc(target
, object
, r_type
, false)))
8377 && target
->abiversion() >= 2
8378 && !parameters
->options().output_is_position_independent()
8379 && !is_branch_reloc(r_type
))
8381 Address off
= target
->glink_section()->find_global_entry(gsym
);
8382 if (off
!= invalid_address
)
8384 value
= target
->glink_section()->global_entry_address() + off
;
8385 has_stub_value
= true;
8390 Stub_table
<size
, big_endian
>* stub_table
= NULL
;
8391 if (target
->stub_tables().size() == 1)
8392 stub_table
= target
->stub_tables()[0];
8393 if (stub_table
== NULL
8396 && !parameters
->options().output_is_position_independent()
8397 && !is_branch_reloc(r_type
)))
8398 stub_table
= object
->stub_table(relinfo
->data_shndx
);
8399 if (stub_table
== NULL
)
8401 // This is a ref from a data section to an ifunc symbol,
8402 // or a non-branch reloc for which we always want to use
8403 // one set of stubs for resolving function addresses.
8404 if (target
->stub_tables().size() != 0)
8405 stub_table
= target
->stub_tables()[0];
8407 if (stub_table
!= NULL
)
8409 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
;
8411 ent
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
8412 rela
.get_r_addend());
8414 ent
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
8415 rela
.get_r_addend());
8418 value
= stub_table
->stub_address() + ent
->off_
;
8419 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
8420 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->reloc_shdr
);
8421 size_t reloc_count
= shdr
.get_sh_size() / reloc_size
;
8424 && relnum
+ 1 < reloc_count
)
8426 Reltype
next_rela(preloc
+ reloc_size
);
8427 if (elfcpp::elf_r_type
<size
>(next_rela
.get_r_info())
8428 == elfcpp::R_PPC64_TOCSAVE
8429 && next_rela
.get_r_offset() == rela
.get_r_offset() + 4)
8432 localentry0
= ent
->localentry0_
;
8433 has_stub_value
= true;
8437 // We don't care too much about bogus debug references to
8438 // non-local functions, but otherwise there had better be a plt
8439 // call stub or global entry stub as appropriate.
8440 gold_assert(has_stub_value
|| !(os
->flags() & elfcpp::SHF_ALLOC
));
8443 if (r_type
== elfcpp::R_POWERPC_GOT16
8444 || r_type
== elfcpp::R_POWERPC_GOT16_LO
8445 || r_type
== elfcpp::R_POWERPC_GOT16_HI
8446 || r_type
== elfcpp::R_POWERPC_GOT16_HA
8447 || r_type
== elfcpp::R_PPC64_GOT16_DS
8448 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
8452 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
8453 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
8457 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
8458 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
8460 value
-= target
->got_section()->got_base_offset(object
);
8462 else if (r_type
== elfcpp::R_PPC64_TOC
)
8464 value
= (target
->got_section()->output_section()->address()
8465 + object
->toc_base_offset());
8467 else if (gsym
!= NULL
8468 && (r_type
== elfcpp::R_POWERPC_REL24
8469 || r_type
== elfcpp::R_PPC_PLTREL24
)
8474 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
8475 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
8476 bool can_plt_call
= localentry0
|| target
->is_tls_get_addr_opt(gsym
);
8477 if (!can_plt_call
&& rela
.get_r_offset() + 8 <= view_size
)
8479 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
8480 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
8483 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
8485 elfcpp::Swap
<32, big_endian
>::
8486 writeval(wv
+ 1, ld_2_1
+ target
->stk_toc());
8487 can_plt_call
= true;
8492 // If we don't have a branch and link followed by a nop,
8493 // we can't go via the plt because there is no place to
8494 // put a toc restoring instruction.
8495 // Unless we know we won't be returning.
8496 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
8497 can_plt_call
= true;
8501 // g++ as of 20130507 emits self-calls without a
8502 // following nop. This is arguably wrong since we have
8503 // conflicting information. On the one hand a global
8504 // symbol and on the other a local call sequence, but
8505 // don't error for this special case.
8506 // It isn't possible to cheaply verify we have exactly
8507 // such a call. Allow all calls to the same section.
8509 Address code
= value
;
8510 if (gsym
->source() == Symbol::FROM_OBJECT
8511 && gsym
->object() == object
)
8513 unsigned int dest_shndx
= 0;
8514 if (target
->abiversion() < 2)
8516 Address addend
= rela
.get_r_addend();
8517 code
= psymval
->value(object
, addend
);
8518 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
8519 &code
, &dest_shndx
);
8522 if (dest_shndx
== 0)
8523 dest_shndx
= gsym
->shndx(&is_ordinary
);
8524 ok
= dest_shndx
== relinfo
->data_shndx
;
8528 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8529 _("call lacks nop, can't restore toc; "
8530 "recompile with -fPIC"));
8536 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8537 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
8538 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
8539 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
8541 // First instruction of a global dynamic sequence, arg setup insn.
8542 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8543 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
8544 enum Got_type got_type
= GOT_TYPE_STANDARD
;
8545 if (tls_type
== tls::TLSOPT_NONE
)
8546 got_type
= GOT_TYPE_TLSGD
;
8547 else if (tls_type
== tls::TLSOPT_TO_IE
)
8548 got_type
= GOT_TYPE_TPREL
;
8549 if (got_type
!= GOT_TYPE_STANDARD
)
8553 gold_assert(gsym
->has_got_offset(got_type
));
8554 value
= gsym
->got_offset(got_type
);
8558 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
8559 value
= object
->local_got_offset(r_sym
, got_type
);
8561 value
-= target
->got_section()->got_base_offset(object
);
8563 if (tls_type
== tls::TLSOPT_TO_IE
)
8565 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8566 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
8568 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8569 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8570 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
8572 insn
|= 32 << 26; // lwz
8574 insn
|= 58 << 26; // ld
8575 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8577 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
8578 - elfcpp::R_POWERPC_GOT_TLSGD16
);
8580 else if (tls_type
== tls::TLSOPT_TO_LE
)
8582 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8583 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
8585 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8586 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8587 insn
&= (1 << 26) - (1 << 21); // extract rt
8592 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8593 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8594 value
= psymval
->value(object
, rela
.get_r_addend());
8598 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8600 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8601 r_type
= elfcpp::R_POWERPC_NONE
;
8605 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
8606 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
8607 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
8608 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
8610 // First instruction of a local dynamic sequence, arg setup insn.
8611 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
8612 if (tls_type
== tls::TLSOPT_NONE
)
8614 value
= target
->tlsld_got_offset();
8615 value
-= target
->got_section()->got_base_offset(object
);
8619 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
8620 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
8621 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
8623 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8624 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8625 insn
&= (1 << 26) - (1 << 21); // extract rt
8630 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8631 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8636 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8638 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8639 r_type
= elfcpp::R_POWERPC_NONE
;
8643 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
8644 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
8645 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
8646 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
8648 // Accesses relative to a local dynamic sequence address,
8649 // no optimisation here.
8652 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
8653 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
8657 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
8658 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
8660 value
-= target
->got_section()->got_base_offset(object
);
8662 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8663 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
8664 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
8665 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
8667 // First instruction of initial exec sequence.
8668 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8669 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
8670 if (tls_type
== tls::TLSOPT_NONE
)
8674 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
8675 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
8679 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
8680 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
8682 value
-= target
->got_section()->got_base_offset(object
);
8686 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
8687 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8688 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
8690 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8691 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8692 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
8697 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8698 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8699 value
= psymval
->value(object
, rela
.get_r_addend());
8703 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8705 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8706 r_type
= elfcpp::R_POWERPC_NONE
;
8710 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
8711 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
8713 // Second instruction of a global dynamic sequence,
8714 // the __tls_get_addr call
8715 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
8716 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8717 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
8718 if (tls_type
!= tls::TLSOPT_NONE
)
8720 if (tls_type
== tls::TLSOPT_TO_IE
)
8722 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8723 Insn insn
= add_3_3_13
;
8726 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8727 r_type
= elfcpp::R_POWERPC_NONE
;
8731 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8732 Insn insn
= addi_3_3
;
8733 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8734 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8736 value
= psymval
->value(object
, rela
.get_r_addend());
8738 this->skip_next_tls_get_addr_call();
8741 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
8742 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
8744 // Second instruction of a local dynamic sequence,
8745 // the __tls_get_addr call
8746 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
8747 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
8748 if (tls_type
== tls::TLSOPT_TO_LE
)
8750 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8751 Insn insn
= addi_3_3
;
8752 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8753 this->skip_next_tls_get_addr_call();
8754 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8759 else if (r_type
== elfcpp::R_POWERPC_TLS
)
8761 // Second instruction of an initial exec sequence
8762 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8763 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
8764 if (tls_type
== tls::TLSOPT_TO_LE
)
8766 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8767 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8768 unsigned int reg
= size
== 32 ? 2 : 13;
8769 insn
= at_tls_transform(insn
, reg
);
8770 gold_assert(insn
!= 0);
8771 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8772 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8774 value
= psymval
->value(object
, rela
.get_r_addend());
8777 else if (!has_stub_value
)
8780 if (!(size
== 32 && r_type
== elfcpp::R_PPC_PLTREL24
))
8781 addend
= rela
.get_r_addend();
8782 value
= psymval
->value(object
, addend
);
8783 if (size
== 64 && is_branch_reloc(r_type
))
8785 if (target
->abiversion() >= 2)
8788 value
+= object
->ppc64_local_entry_offset(gsym
);
8790 value
+= object
->ppc64_local_entry_offset(r_sym
);
8794 unsigned int dest_shndx
;
8795 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
8796 &value
, &dest_shndx
);
8799 Address max_branch_offset
= max_branch_delta(r_type
);
8800 if (max_branch_offset
!= 0
8801 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
8803 Stub_table
<size
, big_endian
>* stub_table
8804 = object
->stub_table(relinfo
->data_shndx
);
8805 if (stub_table
!= NULL
)
8807 Address off
= stub_table
->find_long_branch_entry(object
, value
);
8808 if (off
!= invalid_address
)
8810 value
= (stub_table
->stub_address() + stub_table
->plt_size()
8812 has_stub_value
= true;
8820 case elfcpp::R_PPC64_REL64
:
8821 case elfcpp::R_POWERPC_REL32
:
8822 case elfcpp::R_POWERPC_REL24
:
8823 case elfcpp::R_PPC_PLTREL24
:
8824 case elfcpp::R_PPC_LOCAL24PC
:
8825 case elfcpp::R_POWERPC_REL16
:
8826 case elfcpp::R_POWERPC_REL16_LO
:
8827 case elfcpp::R_POWERPC_REL16_HI
:
8828 case elfcpp::R_POWERPC_REL16_HA
:
8829 case elfcpp::R_POWERPC_REL16DX_HA
:
8830 case elfcpp::R_POWERPC_REL14
:
8831 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8832 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8836 case elfcpp::R_PPC64_TOC16
:
8837 case elfcpp::R_PPC64_TOC16_LO
:
8838 case elfcpp::R_PPC64_TOC16_HI
:
8839 case elfcpp::R_PPC64_TOC16_HA
:
8840 case elfcpp::R_PPC64_TOC16_DS
:
8841 case elfcpp::R_PPC64_TOC16_LO_DS
:
8842 // Subtract the TOC base address.
8843 value
-= (target
->got_section()->output_section()->address()
8844 + object
->toc_base_offset());
8847 case elfcpp::R_POWERPC_SECTOFF
:
8848 case elfcpp::R_POWERPC_SECTOFF_LO
:
8849 case elfcpp::R_POWERPC_SECTOFF_HI
:
8850 case elfcpp::R_POWERPC_SECTOFF_HA
:
8851 case elfcpp::R_PPC64_SECTOFF_DS
:
8852 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
8854 value
-= os
->address();
8857 case elfcpp::R_PPC64_TPREL16_DS
:
8858 case elfcpp::R_PPC64_TPREL16_LO_DS
:
8859 case elfcpp::R_PPC64_TPREL16_HIGH
:
8860 case elfcpp::R_PPC64_TPREL16_HIGHA
:
8862 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
8865 case elfcpp::R_POWERPC_TPREL16
:
8866 case elfcpp::R_POWERPC_TPREL16_LO
:
8867 case elfcpp::R_POWERPC_TPREL16_HI
:
8868 case elfcpp::R_POWERPC_TPREL16_HA
:
8869 case elfcpp::R_POWERPC_TPREL
:
8870 case elfcpp::R_PPC64_TPREL16_HIGHER
:
8871 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
8872 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
8873 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
8874 // tls symbol values are relative to tls_segment()->vaddr()
8878 case elfcpp::R_PPC64_DTPREL16_DS
:
8879 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
8880 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
8881 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
8882 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
8883 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
8885 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
8886 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
8889 case elfcpp::R_POWERPC_DTPREL16
:
8890 case elfcpp::R_POWERPC_DTPREL16_LO
:
8891 case elfcpp::R_POWERPC_DTPREL16_HI
:
8892 case elfcpp::R_POWERPC_DTPREL16_HA
:
8893 case elfcpp::R_POWERPC_DTPREL
:
8894 case elfcpp::R_PPC64_DTPREL16_HIGH
:
8895 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
8896 // tls symbol values are relative to tls_segment()->vaddr()
8897 value
-= dtp_offset
;
8900 case elfcpp::R_PPC64_ADDR64_LOCAL
:
8902 value
+= object
->ppc64_local_entry_offset(gsym
);
8904 value
+= object
->ppc64_local_entry_offset(r_sym
);
8911 Insn branch_bit
= 0;
8914 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
8915 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8916 branch_bit
= 1 << 21;
8918 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
8919 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8921 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8922 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8925 if (this->is_isa_v2
)
8927 // Set 'a' bit. This is 0b00010 in BO field for branch
8928 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
8929 // for branch on CTR insns (BO == 1a00t or 1a01t).
8930 if ((insn
& (0x14 << 21)) == (0x04 << 21))
8932 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
8939 // Invert 'y' bit if not the default.
8940 if (static_cast<Signed_address
>(value
) < 0)
8943 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
8958 // Multi-instruction sequences that access the GOT/TOC can
8959 // be optimized, eg.
8960 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
8961 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
8963 // addis ra,r2,0; addi rb,ra,x@toc@l;
8964 // to nop; addi rb,r2,x@toc;
8965 // FIXME: the @got sequence shown above is not yet
8966 // optimized. Note that gcc as of 2017-01-07 doesn't use
8967 // the ELF @got relocs except for TLS, instead using the
8968 // PowerOpen variant of a compiler managed GOT (called TOC).
8969 // The PowerOpen TOC sequence equivalent to the first
8970 // example is optimized.
8971 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
8972 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
8973 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
8974 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
8975 case elfcpp::R_POWERPC_GOT16_HA
:
8976 case elfcpp::R_PPC64_TOC16_HA
:
8977 if (parameters
->options().toc_optimize())
8979 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
8980 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8981 if (r_type
== elfcpp::R_PPC64_TOC16_HA
8982 && object
->make_toc_relative(target
, &value
))
8984 gold_assert((insn
& ((0x3f << 26) | 0x1f << 16))
8985 == ((15u << 26) | (2 << 16)));
8987 if (((insn
& ((0x3f << 26) | 0x1f << 16))
8988 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8989 && value
+ 0x8000 < 0x10000)
8991 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
8997 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
8998 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
8999 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
9000 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
9001 case elfcpp::R_POWERPC_GOT16_LO
:
9002 case elfcpp::R_PPC64_GOT16_LO_DS
:
9003 case elfcpp::R_PPC64_TOC16_LO
:
9004 case elfcpp::R_PPC64_TOC16_LO_DS
:
9005 if (parameters
->options().toc_optimize())
9007 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9008 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9009 bool changed
= false;
9010 if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
9011 && object
->make_toc_relative(target
, &value
))
9013 gold_assert ((insn
& (0x3f << 26)) == 58u << 26 /* ld */);
9014 insn
^= (14u << 26) ^ (58u << 26);
9015 r_type
= elfcpp::R_PPC64_TOC16_LO
;
9018 if (ok_lo_toc_insn(insn
, r_type
)
9019 && value
+ 0x8000 < 0x10000)
9021 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
9023 // Transform addic to addi when we change reg.
9024 insn
&= ~((0x3f << 26) | (0x1f << 16));
9025 insn
|= (14u << 26) | (2 << 16);
9029 insn
&= ~(0x1f << 16);
9035 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9039 case elfcpp::R_POWERPC_TPREL16_HA
:
9040 if (parameters
->options().tls_optimize() && value
+ 0x8000 < 0x10000)
9042 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9043 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9044 if ((insn
& ((0x3f << 26) | 0x1f << 16))
9045 != ((15u << 26) | ((size
== 32 ? 2 : 13) << 16)))
9049 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
9055 case elfcpp::R_PPC64_TPREL16_LO_DS
:
9057 // R_PPC_TLSGD, R_PPC_TLSLD
9060 case elfcpp::R_POWERPC_TPREL16_LO
:
9061 if (parameters
->options().tls_optimize() && value
+ 0x8000 < 0x10000)
9063 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9064 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9065 insn
&= ~(0x1f << 16);
9066 insn
|= (size
== 32 ? 2 : 13) << 16;
9067 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9071 case elfcpp::R_PPC64_ENTRY
:
9072 value
= (target
->got_section()->output_section()->address()
9073 + object
->toc_base_offset());
9074 if (value
+ 0x80008000 <= 0xffffffff
9075 && !parameters
->options().output_is_position_independent())
9077 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9078 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9079 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
9081 if ((insn1
& ~0xfffc) == ld_2_12
9082 && insn2
== add_2_2_12
)
9084 insn1
= lis_2
+ ha(value
);
9085 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
9086 insn2
= addi_2_2
+ l(value
);
9087 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
9094 if (value
+ 0x80008000 <= 0xffffffff)
9096 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9097 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9098 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
9100 if ((insn1
& ~0xfffc) == ld_2_12
9101 && insn2
== add_2_2_12
)
9103 insn1
= addis_2_12
+ ha(value
);
9104 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
9105 insn2
= addi_2_2
+ l(value
);
9106 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
9113 case elfcpp::R_POWERPC_REL16_LO
:
9114 // If we are generating a non-PIC executable, edit
9115 // 0: addis 2,12,.TOC.-0b@ha
9116 // addi 2,2,.TOC.-0b@l
9117 // used by ELFv2 global entry points to set up r2, to
9120 // if .TOC. is in range. */
9121 if (value
+ address
- 4 + 0x80008000 <= 0xffffffff
9124 && target
->abiversion() >= 2
9125 && !parameters
->options().output_is_position_independent()
9126 && rela
.get_r_addend() == d_offset
+ 4
9128 && strcmp(gsym
->name(), ".TOC.") == 0)
9130 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
9131 Reltype
prev_rela(preloc
- reloc_size
);
9132 if ((prev_rela
.get_r_info()
9133 == elfcpp::elf_r_info
<size
>(r_sym
,
9134 elfcpp::R_POWERPC_REL16_HA
))
9135 && prev_rela
.get_r_offset() + 4 == rela
.get_r_offset()
9136 && prev_rela
.get_r_addend() + 4 == rela
.get_r_addend())
9138 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9139 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
- 1);
9140 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9142 if ((insn1
& 0xffff0000) == addis_2_12
9143 && (insn2
& 0xffff0000) == addi_2_2
)
9145 insn1
= lis_2
+ ha(value
+ address
- 4);
9146 elfcpp::Swap
<32, big_endian
>::writeval(iview
- 1, insn1
);
9147 insn2
= addi_2_2
+ l(value
+ address
- 4);
9148 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn2
);
9151 relinfo
->rr
->set_strategy(relnum
- 1,
9152 Relocatable_relocs::RELOC_SPECIAL
);
9153 relinfo
->rr
->set_strategy(relnum
,
9154 Relocatable_relocs::RELOC_SPECIAL
);
9164 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
9165 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->data_shdr
);
9168 case elfcpp::R_POWERPC_ADDR32
:
9169 case elfcpp::R_POWERPC_UADDR32
:
9171 overflow
= Reloc::CHECK_BITFIELD
;
9174 case elfcpp::R_POWERPC_REL32
:
9175 case elfcpp::R_POWERPC_REL16DX_HA
:
9177 overflow
= Reloc::CHECK_SIGNED
;
9180 case elfcpp::R_POWERPC_UADDR16
:
9181 overflow
= Reloc::CHECK_BITFIELD
;
9184 case elfcpp::R_POWERPC_ADDR16
:
9185 // We really should have three separate relocations,
9186 // one for 16-bit data, one for insns with 16-bit signed fields,
9187 // and one for insns with 16-bit unsigned fields.
9188 overflow
= Reloc::CHECK_BITFIELD
;
9189 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
9190 overflow
= Reloc::CHECK_LOW_INSN
;
9193 case elfcpp::R_POWERPC_ADDR16_HI
:
9194 case elfcpp::R_POWERPC_ADDR16_HA
:
9195 case elfcpp::R_POWERPC_GOT16_HI
:
9196 case elfcpp::R_POWERPC_GOT16_HA
:
9197 case elfcpp::R_POWERPC_PLT16_HI
:
9198 case elfcpp::R_POWERPC_PLT16_HA
:
9199 case elfcpp::R_POWERPC_SECTOFF_HI
:
9200 case elfcpp::R_POWERPC_SECTOFF_HA
:
9201 case elfcpp::R_PPC64_TOC16_HI
:
9202 case elfcpp::R_PPC64_TOC16_HA
:
9203 case elfcpp::R_PPC64_PLTGOT16_HI
:
9204 case elfcpp::R_PPC64_PLTGOT16_HA
:
9205 case elfcpp::R_POWERPC_TPREL16_HI
:
9206 case elfcpp::R_POWERPC_TPREL16_HA
:
9207 case elfcpp::R_POWERPC_DTPREL16_HI
:
9208 case elfcpp::R_POWERPC_DTPREL16_HA
:
9209 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
9210 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
9211 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
9212 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
9213 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
9214 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
9215 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
9216 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
9217 case elfcpp::R_POWERPC_REL16_HI
:
9218 case elfcpp::R_POWERPC_REL16_HA
:
9220 overflow
= Reloc::CHECK_HIGH_INSN
;
9223 case elfcpp::R_POWERPC_REL16
:
9224 case elfcpp::R_PPC64_TOC16
:
9225 case elfcpp::R_POWERPC_GOT16
:
9226 case elfcpp::R_POWERPC_SECTOFF
:
9227 case elfcpp::R_POWERPC_TPREL16
:
9228 case elfcpp::R_POWERPC_DTPREL16
:
9229 case elfcpp::R_POWERPC_GOT_TLSGD16
:
9230 case elfcpp::R_POWERPC_GOT_TLSLD16
:
9231 case elfcpp::R_POWERPC_GOT_TPREL16
:
9232 case elfcpp::R_POWERPC_GOT_DTPREL16
:
9233 overflow
= Reloc::CHECK_LOW_INSN
;
9236 case elfcpp::R_POWERPC_ADDR24
:
9237 case elfcpp::R_POWERPC_ADDR14
:
9238 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
9239 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
9240 case elfcpp::R_PPC64_ADDR16_DS
:
9241 case elfcpp::R_POWERPC_REL24
:
9242 case elfcpp::R_PPC_PLTREL24
:
9243 case elfcpp::R_PPC_LOCAL24PC
:
9244 case elfcpp::R_PPC64_TPREL16_DS
:
9245 case elfcpp::R_PPC64_DTPREL16_DS
:
9246 case elfcpp::R_PPC64_TOC16_DS
:
9247 case elfcpp::R_PPC64_GOT16_DS
:
9248 case elfcpp::R_PPC64_SECTOFF_DS
:
9249 case elfcpp::R_POWERPC_REL14
:
9250 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
9251 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
9252 overflow
= Reloc::CHECK_SIGNED
;
9256 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9259 if (overflow
== Reloc::CHECK_LOW_INSN
9260 || overflow
== Reloc::CHECK_HIGH_INSN
)
9262 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9264 if ((insn
& (0x3f << 26)) == 10u << 26 /* cmpli */)
9265 overflow
= Reloc::CHECK_BITFIELD
;
9266 else if (overflow
== Reloc::CHECK_LOW_INSN
9267 ? ((insn
& (0x3f << 26)) == 28u << 26 /* andi */
9268 || (insn
& (0x3f << 26)) == 24u << 26 /* ori */
9269 || (insn
& (0x3f << 26)) == 26u << 26 /* xori */)
9270 : ((insn
& (0x3f << 26)) == 29u << 26 /* andis */
9271 || (insn
& (0x3f << 26)) == 25u << 26 /* oris */
9272 || (insn
& (0x3f << 26)) == 27u << 26 /* xoris */))
9273 overflow
= Reloc::CHECK_UNSIGNED
;
9275 overflow
= Reloc::CHECK_SIGNED
;
9278 bool maybe_dq_reloc
= false;
9279 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
9280 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
9283 case elfcpp::R_POWERPC_NONE
:
9284 case elfcpp::R_POWERPC_TLS
:
9285 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
9286 case elfcpp::R_POWERPC_GNU_VTENTRY
:
9289 case elfcpp::R_PPC64_ADDR64
:
9290 case elfcpp::R_PPC64_REL64
:
9291 case elfcpp::R_PPC64_TOC
:
9292 case elfcpp::R_PPC64_ADDR64_LOCAL
:
9293 Reloc::addr64(view
, value
);
9296 case elfcpp::R_POWERPC_TPREL
:
9297 case elfcpp::R_POWERPC_DTPREL
:
9299 Reloc::addr64(view
, value
);
9301 status
= Reloc::addr32(view
, value
, overflow
);
9304 case elfcpp::R_PPC64_UADDR64
:
9305 Reloc::addr64_u(view
, value
);
9308 case elfcpp::R_POWERPC_ADDR32
:
9309 status
= Reloc::addr32(view
, value
, overflow
);
9312 case elfcpp::R_POWERPC_REL32
:
9313 case elfcpp::R_POWERPC_UADDR32
:
9314 status
= Reloc::addr32_u(view
, value
, overflow
);
9317 case elfcpp::R_POWERPC_ADDR24
:
9318 case elfcpp::R_POWERPC_REL24
:
9319 case elfcpp::R_PPC_PLTREL24
:
9320 case elfcpp::R_PPC_LOCAL24PC
:
9321 status
= Reloc::addr24(view
, value
, overflow
);
9324 case elfcpp::R_POWERPC_GOT_DTPREL16
:
9325 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
9326 case elfcpp::R_POWERPC_GOT_TPREL16
:
9327 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
9330 // On ppc64 these are all ds form
9331 maybe_dq_reloc
= true;
9335 case elfcpp::R_POWERPC_ADDR16
:
9336 case elfcpp::R_POWERPC_REL16
:
9337 case elfcpp::R_PPC64_TOC16
:
9338 case elfcpp::R_POWERPC_GOT16
:
9339 case elfcpp::R_POWERPC_SECTOFF
:
9340 case elfcpp::R_POWERPC_TPREL16
:
9341 case elfcpp::R_POWERPC_DTPREL16
:
9342 case elfcpp::R_POWERPC_GOT_TLSGD16
:
9343 case elfcpp::R_POWERPC_GOT_TLSLD16
:
9344 case elfcpp::R_POWERPC_ADDR16_LO
:
9345 case elfcpp::R_POWERPC_REL16_LO
:
9346 case elfcpp::R_PPC64_TOC16_LO
:
9347 case elfcpp::R_POWERPC_GOT16_LO
:
9348 case elfcpp::R_POWERPC_SECTOFF_LO
:
9349 case elfcpp::R_POWERPC_TPREL16_LO
:
9350 case elfcpp::R_POWERPC_DTPREL16_LO
:
9351 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
9352 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
9354 status
= Reloc::addr16(view
, value
, overflow
);
9356 maybe_dq_reloc
= true;
9359 case elfcpp::R_POWERPC_UADDR16
:
9360 status
= Reloc::addr16_u(view
, value
, overflow
);
9363 case elfcpp::R_PPC64_ADDR16_HIGH
:
9364 case elfcpp::R_PPC64_TPREL16_HIGH
:
9365 case elfcpp::R_PPC64_DTPREL16_HIGH
:
9367 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
9370 case elfcpp::R_POWERPC_ADDR16_HI
:
9371 case elfcpp::R_POWERPC_REL16_HI
:
9372 case elfcpp::R_PPC64_TOC16_HI
:
9373 case elfcpp::R_POWERPC_GOT16_HI
:
9374 case elfcpp::R_POWERPC_SECTOFF_HI
:
9375 case elfcpp::R_POWERPC_TPREL16_HI
:
9376 case elfcpp::R_POWERPC_DTPREL16_HI
:
9377 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
9378 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
9379 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
9380 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
9381 Reloc::addr16_hi(view
, value
);
9384 case elfcpp::R_PPC64_ADDR16_HIGHA
:
9385 case elfcpp::R_PPC64_TPREL16_HIGHA
:
9386 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
9388 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
9391 case elfcpp::R_POWERPC_ADDR16_HA
:
9392 case elfcpp::R_POWERPC_REL16_HA
:
9393 case elfcpp::R_PPC64_TOC16_HA
:
9394 case elfcpp::R_POWERPC_GOT16_HA
:
9395 case elfcpp::R_POWERPC_SECTOFF_HA
:
9396 case elfcpp::R_POWERPC_TPREL16_HA
:
9397 case elfcpp::R_POWERPC_DTPREL16_HA
:
9398 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
9399 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
9400 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
9401 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
9402 Reloc::addr16_ha(view
, value
);
9405 case elfcpp::R_POWERPC_REL16DX_HA
:
9406 status
= Reloc::addr16dx_ha(view
, value
, overflow
);
9409 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
9411 // R_PPC_EMB_NADDR16_LO
9414 case elfcpp::R_PPC64_ADDR16_HIGHER
:
9415 case elfcpp::R_PPC64_TPREL16_HIGHER
:
9416 Reloc::addr16_hi2(view
, value
);
9419 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
9421 // R_PPC_EMB_NADDR16_HI
9424 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
9425 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
9426 Reloc::addr16_ha2(view
, value
);
9429 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
9431 // R_PPC_EMB_NADDR16_HA
9434 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
9435 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
9436 Reloc::addr16_hi3(view
, value
);
9439 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
9444 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
9445 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
9446 Reloc::addr16_ha3(view
, value
);
9449 case elfcpp::R_PPC64_DTPREL16_DS
:
9450 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
9452 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
9455 case elfcpp::R_PPC64_TPREL16_DS
:
9456 case elfcpp::R_PPC64_TPREL16_LO_DS
:
9458 // R_PPC_TLSGD, R_PPC_TLSLD
9461 case elfcpp::R_PPC64_ADDR16_DS
:
9462 case elfcpp::R_PPC64_ADDR16_LO_DS
:
9463 case elfcpp::R_PPC64_TOC16_DS
:
9464 case elfcpp::R_PPC64_TOC16_LO_DS
:
9465 case elfcpp::R_PPC64_GOT16_DS
:
9466 case elfcpp::R_PPC64_GOT16_LO_DS
:
9467 case elfcpp::R_PPC64_SECTOFF_DS
:
9468 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
9469 maybe_dq_reloc
= true;
9472 case elfcpp::R_POWERPC_ADDR14
:
9473 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
9474 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
9475 case elfcpp::R_POWERPC_REL14
:
9476 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
9477 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
9478 status
= Reloc::addr14(view
, value
, overflow
);
9481 case elfcpp::R_POWERPC_COPY
:
9482 case elfcpp::R_POWERPC_GLOB_DAT
:
9483 case elfcpp::R_POWERPC_JMP_SLOT
:
9484 case elfcpp::R_POWERPC_RELATIVE
:
9485 case elfcpp::R_POWERPC_DTPMOD
:
9486 case elfcpp::R_PPC64_JMP_IREL
:
9487 case elfcpp::R_POWERPC_IRELATIVE
:
9488 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
9489 _("unexpected reloc %u in object file"),
9493 case elfcpp::R_PPC64_TOCSAVE
:
9499 Symbol_location loc
;
9500 loc
.object
= relinfo
->object
;
9501 loc
.shndx
= relinfo
->data_shndx
;
9502 loc
.offset
= rela
.get_r_offset();
9503 Tocsave_loc::const_iterator p
= target
->tocsave_loc().find(loc
);
9504 if (p
!= target
->tocsave_loc().end())
9506 // If we've generated plt calls using this tocsave, then
9507 // the nop needs to be changed to save r2.
9508 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9509 if (elfcpp::Swap
<32, big_endian
>::readval(iview
) == nop
)
9510 elfcpp::Swap
<32, big_endian
>::
9511 writeval(iview
, std_2_1
+ target
->stk_toc());
9516 case elfcpp::R_PPC_EMB_SDA2I16
:
9517 case elfcpp::R_PPC_EMB_SDA2REL
:
9520 // R_PPC64_TLSGD, R_PPC64_TLSLD
9523 case elfcpp::R_POWERPC_PLT32
:
9524 case elfcpp::R_POWERPC_PLTREL32
:
9525 case elfcpp::R_POWERPC_PLT16_LO
:
9526 case elfcpp::R_POWERPC_PLT16_HI
:
9527 case elfcpp::R_POWERPC_PLT16_HA
:
9528 case elfcpp::R_PPC_SDAREL16
:
9529 case elfcpp::R_POWERPC_ADDR30
:
9530 case elfcpp::R_PPC64_PLT64
:
9531 case elfcpp::R_PPC64_PLTREL64
:
9532 case elfcpp::R_PPC64_PLTGOT16
:
9533 case elfcpp::R_PPC64_PLTGOT16_LO
:
9534 case elfcpp::R_PPC64_PLTGOT16_HI
:
9535 case elfcpp::R_PPC64_PLTGOT16_HA
:
9536 case elfcpp::R_PPC64_PLT16_LO_DS
:
9537 case elfcpp::R_PPC64_PLTGOT16_DS
:
9538 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
9539 case elfcpp::R_PPC_EMB_RELSDA
:
9540 case elfcpp::R_PPC_TOC16
:
9543 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
9544 _("unsupported reloc %u"),
9552 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9554 if ((insn
& (0x3f << 26)) == 56u << 26 /* lq */
9555 || ((insn
& (0x3f << 26)) == (61u << 26) /* lxv, stxv */
9556 && (insn
& 3) == 1))
9557 status
= Reloc::addr16_dq(view
, value
, overflow
);
9559 || (insn
& (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
9560 || (insn
& (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
9561 || (insn
& (0x3f << 26)) == 57u << 26 /* lfdp */
9562 || (insn
& (0x3f << 26)) == 61u << 26 /* stfdp */)
9563 status
= Reloc::addr16_ds(view
, value
, overflow
);
9565 status
= Reloc::addr16(view
, value
, overflow
);
9568 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
9571 && gsym
->is_undefined()
9572 && is_branch_reloc(r_type
))))
9574 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
9575 _("relocation overflow"));
9577 gold_info(_("try relinking with a smaller --stub-group-size"));
9583 // Relocate section data.
9585 template<int size
, bool big_endian
>
9587 Target_powerpc
<size
, big_endian
>::relocate_section(
9588 const Relocate_info
<size
, big_endian
>* relinfo
,
9589 unsigned int sh_type
,
9590 const unsigned char* prelocs
,
9592 Output_section
* output_section
,
9593 bool needs_special_offset_handling
,
9594 unsigned char* view
,
9596 section_size_type view_size
,
9597 const Reloc_symbol_changes
* reloc_symbol_changes
)
9599 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
9600 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
9601 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
9602 Powerpc_comdat_behavior
;
9603 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
9606 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9608 gold::relocate_section
<size
, big_endian
, Powerpc
, Powerpc_relocate
,
9609 Powerpc_comdat_behavior
, Classify_reloc
>(
9615 needs_special_offset_handling
,
9619 reloc_symbol_changes
);
9622 template<int size
, bool big_endian
>
9623 class Powerpc_scan_relocatable_reloc
9626 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
9627 static const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
9628 static const int sh_type
= elfcpp::SHT_RELA
;
9630 // Return the symbol referred to by the relocation.
9631 static inline unsigned int
9632 get_r_sym(const Reltype
* reloc
)
9633 { return elfcpp::elf_r_sym
<size
>(reloc
->get_r_info()); }
9635 // Return the type of the relocation.
9636 static inline unsigned int
9637 get_r_type(const Reltype
* reloc
)
9638 { return elfcpp::elf_r_type
<size
>(reloc
->get_r_info()); }
9640 // Return the strategy to use for a local symbol which is not a
9641 // section symbol, given the relocation type.
9642 inline Relocatable_relocs::Reloc_strategy
9643 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
9645 if (r_type
== 0 && r_sym
== 0)
9646 return Relocatable_relocs::RELOC_DISCARD
;
9647 return Relocatable_relocs::RELOC_COPY
;
9650 // Return the strategy to use for a local symbol which is a section
9651 // symbol, given the relocation type.
9652 inline Relocatable_relocs::Reloc_strategy
9653 local_section_strategy(unsigned int, Relobj
*)
9655 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
9658 // Return the strategy to use for a global symbol, given the
9659 // relocation type, the object, and the symbol index.
9660 inline Relocatable_relocs::Reloc_strategy
9661 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
9663 if (r_type
== elfcpp::R_PPC_PLTREL24
)
9664 return Relocatable_relocs::RELOC_SPECIAL
;
9665 return Relocatable_relocs::RELOC_COPY
;
9669 // Scan the relocs during a relocatable link.
9671 template<int size
, bool big_endian
>
9673 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
9674 Symbol_table
* symtab
,
9676 Sized_relobj_file
<size
, big_endian
>* object
,
9677 unsigned int data_shndx
,
9678 unsigned int sh_type
,
9679 const unsigned char* prelocs
,
9681 Output_section
* output_section
,
9682 bool needs_special_offset_handling
,
9683 size_t local_symbol_count
,
9684 const unsigned char* plocal_symbols
,
9685 Relocatable_relocs
* rr
)
9687 typedef Powerpc_scan_relocatable_reloc
<size
, big_endian
> Scan_strategy
;
9689 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9691 gold::scan_relocatable_relocs
<size
, big_endian
, Scan_strategy
>(
9699 needs_special_offset_handling
,
9705 // Scan the relocs for --emit-relocs.
9707 template<int size
, bool big_endian
>
9709 Target_powerpc
<size
, big_endian
>::emit_relocs_scan(
9710 Symbol_table
* symtab
,
9712 Sized_relobj_file
<size
, big_endian
>* object
,
9713 unsigned int data_shndx
,
9714 unsigned int sh_type
,
9715 const unsigned char* prelocs
,
9717 Output_section
* output_section
,
9718 bool needs_special_offset_handling
,
9719 size_t local_symbol_count
,
9720 const unsigned char* plocal_syms
,
9721 Relocatable_relocs
* rr
)
9723 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
9725 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
9726 Emit_relocs_strategy
;
9728 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9730 gold::scan_relocatable_relocs
<size
, big_endian
, Emit_relocs_strategy
>(
9738 needs_special_offset_handling
,
9744 // Emit relocations for a section.
9745 // This is a modified version of the function by the same name in
9746 // target-reloc.h. Using relocate_special_relocatable for
9747 // R_PPC_PLTREL24 would require duplication of the entire body of the
9748 // loop, so we may as well duplicate the whole thing.
9750 template<int size
, bool big_endian
>
9752 Target_powerpc
<size
, big_endian
>::relocate_relocs(
9753 const Relocate_info
<size
, big_endian
>* relinfo
,
9754 unsigned int sh_type
,
9755 const unsigned char* prelocs
,
9757 Output_section
* output_section
,
9758 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
9760 Address view_address
,
9762 unsigned char* reloc_view
,
9763 section_size_type reloc_view_size
)
9765 gold_assert(sh_type
== elfcpp::SHT_RELA
);
9767 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
9768 typedef typename
elfcpp::Rela_write
<size
, big_endian
> Reltype_write
;
9769 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
9770 // Offset from start of insn to d-field reloc.
9771 const int d_offset
= big_endian
? 2 : 0;
9773 Powerpc_relobj
<size
, big_endian
>* const object
9774 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
9775 const unsigned int local_count
= object
->local_symbol_count();
9776 unsigned int got2_shndx
= object
->got2_shndx();
9777 Address got2_addend
= 0;
9778 if (got2_shndx
!= 0)
9780 got2_addend
= object
->get_output_section_offset(got2_shndx
);
9781 gold_assert(got2_addend
!= invalid_address
);
9784 const bool relocatable
= parameters
->options().relocatable();
9786 unsigned char* pwrite
= reloc_view
;
9787 bool zap_next
= false;
9788 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
9790 Relocatable_relocs::Reloc_strategy strategy
= relinfo
->rr
->strategy(i
);
9791 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
9794 Reltype
reloc(prelocs
);
9795 Reltype_write
reloc_write(pwrite
);
9797 Address offset
= reloc
.get_r_offset();
9798 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
9799 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
9800 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
9801 const unsigned int orig_r_sym
= r_sym
;
9802 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
9803 = reloc
.get_r_addend();
9804 const Symbol
* gsym
= NULL
;
9808 // We could arrange to discard these and other relocs for
9809 // tls optimised sequences in the strategy methods, but for
9810 // now do as BFD ld does.
9811 r_type
= elfcpp::R_POWERPC_NONE
;
9815 // Get the new symbol index.
9816 Output_section
* os
= NULL
;
9817 if (r_sym
< local_count
)
9821 case Relocatable_relocs::RELOC_COPY
:
9822 case Relocatable_relocs::RELOC_SPECIAL
:
9825 r_sym
= object
->symtab_index(r_sym
);
9826 gold_assert(r_sym
!= -1U);
9830 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
9832 // We are adjusting a section symbol. We need to find
9833 // the symbol table index of the section symbol for
9834 // the output section corresponding to input section
9835 // in which this symbol is defined.
9836 gold_assert(r_sym
< local_count
);
9838 unsigned int shndx
=
9839 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
9840 gold_assert(is_ordinary
);
9841 os
= object
->output_section(shndx
);
9842 gold_assert(os
!= NULL
);
9843 gold_assert(os
->needs_symtab_index());
9844 r_sym
= os
->symtab_index();
9854 gsym
= object
->global_symbol(r_sym
);
9855 gold_assert(gsym
!= NULL
);
9856 if (gsym
->is_forwarder())
9857 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
9859 gold_assert(gsym
->has_symtab_index());
9860 r_sym
= gsym
->symtab_index();
9863 // Get the new offset--the location in the output section where
9864 // this relocation should be applied.
9865 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
9866 offset
+= offset_in_output_section
;
9869 section_offset_type sot_offset
=
9870 convert_types
<section_offset_type
, Address
>(offset
);
9871 section_offset_type new_sot_offset
=
9872 output_section
->output_offset(object
, relinfo
->data_shndx
,
9874 gold_assert(new_sot_offset
!= -1);
9875 offset
= new_sot_offset
;
9878 // In an object file, r_offset is an offset within the section.
9879 // In an executable or dynamic object, generated by
9880 // --emit-relocs, r_offset is an absolute address.
9883 offset
+= view_address
;
9884 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
9885 offset
-= offset_in_output_section
;
9888 // Handle the reloc addend based on the strategy.
9889 if (strategy
== Relocatable_relocs::RELOC_COPY
)
9891 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
9893 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
9894 addend
= psymval
->value(object
, addend
);
9895 // In a relocatable link, the symbol value is relative to
9896 // the start of the output section. For a non-relocatable
9897 // link, we need to adjust the addend.
9900 gold_assert(os
!= NULL
);
9901 addend
-= os
->address();
9904 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
9908 if (addend
>= 32768)
9909 addend
+= got2_addend
;
9911 else if (r_type
== elfcpp::R_POWERPC_REL16_HA
)
9913 r_type
= elfcpp::R_POWERPC_ADDR16_HA
;
9916 else if (r_type
== elfcpp::R_POWERPC_REL16_LO
)
9918 r_type
= elfcpp::R_POWERPC_ADDR16_LO
;
9919 addend
-= d_offset
+ 4;
9927 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
9928 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
9929 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
9930 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
9932 // First instruction of a global dynamic sequence,
9934 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9935 switch (this->optimize_tls_gd(final
))
9937 case tls::TLSOPT_TO_IE
:
9938 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
9939 - elfcpp::R_POWERPC_GOT_TLSGD16
);
9941 case tls::TLSOPT_TO_LE
:
9942 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
9943 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
9944 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9947 r_type
= elfcpp::R_POWERPC_NONE
;
9955 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
9956 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
9957 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
9958 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
9960 // First instruction of a local dynamic sequence,
9962 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
9964 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
9965 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
9967 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9968 const Output_section
* os
= relinfo
->layout
->tls_segment()
9970 gold_assert(os
!= NULL
);
9971 gold_assert(os
->needs_symtab_index());
9972 r_sym
= os
->symtab_index();
9973 addend
= dtp_offset
;
9977 r_type
= elfcpp::R_POWERPC_NONE
;
9982 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
9983 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
9984 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
9985 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
9987 // First instruction of initial exec sequence.
9988 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9989 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
9991 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
9992 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
9993 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9996 r_type
= elfcpp::R_POWERPC_NONE
;
10001 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
10002 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
10004 // Second instruction of a global dynamic sequence,
10005 // the __tls_get_addr call
10006 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
10007 switch (this->optimize_tls_gd(final
))
10009 case tls::TLSOPT_TO_IE
:
10010 r_type
= elfcpp::R_POWERPC_NONE
;
10013 case tls::TLSOPT_TO_LE
:
10014 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
10015 offset
+= d_offset
;
10022 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
10023 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
10025 // Second instruction of a local dynamic sequence,
10026 // the __tls_get_addr call
10027 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
10029 const Output_section
* os
= relinfo
->layout
->tls_segment()
10031 gold_assert(os
!= NULL
);
10032 gold_assert(os
->needs_symtab_index());
10033 r_sym
= os
->symtab_index();
10034 addend
= dtp_offset
;
10035 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
10036 offset
+= d_offset
;
10040 else if (r_type
== elfcpp::R_POWERPC_TLS
)
10042 // Second instruction of an initial exec sequence
10043 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
10044 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
10046 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
10047 offset
+= d_offset
;
10052 reloc_write
.put_r_offset(offset
);
10053 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
10054 reloc_write
.put_r_addend(addend
);
10056 pwrite
+= reloc_size
;
10059 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
10060 == reloc_view_size
);
10063 // Return the value to use for a dynamic symbol which requires special
10064 // treatment. This is how we support equality comparisons of function
10065 // pointers across shared library boundaries, as described in the
10066 // processor specific ABI supplement.
10068 template<int size
, bool big_endian
>
10070 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
10074 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
10075 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
10076 p
!= this->stub_tables_
.end();
10079 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
10080 = (*p
)->find_plt_call_entry(gsym
);
10082 return (*p
)->stub_address() + ent
->off_
;
10085 else if (this->abiversion() >= 2)
10087 Address off
= this->glink_section()->find_global_entry(gsym
);
10088 if (off
!= invalid_address
)
10089 return this->glink_section()->global_entry_address() + off
;
10091 gold_unreachable();
10094 // Return the PLT address to use for a local symbol.
10095 template<int size
, bool big_endian
>
10097 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
10098 const Relobj
* object
,
10099 unsigned int symndx
) const
10103 const Sized_relobj
<size
, big_endian
>* relobj
10104 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
10105 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
10106 p
!= this->stub_tables_
.end();
10109 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
10110 = (*p
)->find_plt_call_entry(relobj
->sized_relobj(), symndx
);
10112 return (*p
)->stub_address() + ent
->off_
;
10115 gold_unreachable();
10118 // Return the PLT address to use for a global symbol.
10119 template<int size
, bool big_endian
>
10121 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
10122 const Symbol
* gsym
) const
10126 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
10127 p
!= this->stub_tables_
.end();
10130 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
10131 = (*p
)->find_plt_call_entry(gsym
);
10133 return (*p
)->stub_address() + ent
->off_
;
10136 else if (this->abiversion() >= 2)
10138 Address off
= this->glink_section()->find_global_entry(gsym
);
10139 if (off
!= invalid_address
)
10140 return this->glink_section()->global_entry_address() + off
;
10142 gold_unreachable();
10145 // Return the offset to use for the GOT_INDX'th got entry which is
10146 // for a local tls symbol specified by OBJECT, SYMNDX.
10147 template<int size
, bool big_endian
>
10149 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
10150 const Relobj
* object
,
10151 unsigned int symndx
,
10152 unsigned int got_indx
) const
10154 const Powerpc_relobj
<size
, big_endian
>* ppc_object
10155 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
10156 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
10158 for (Got_type got_type
= GOT_TYPE_TLSGD
;
10159 got_type
<= GOT_TYPE_TPREL
;
10160 got_type
= Got_type(got_type
+ 1))
10161 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
10163 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
10164 if (got_type
== GOT_TYPE_TLSGD
)
10166 if (off
== got_indx
* (size
/ 8))
10168 if (got_type
== GOT_TYPE_TPREL
)
10171 return -dtp_offset
;
10175 gold_unreachable();
10178 // Return the offset to use for the GOT_INDX'th got entry which is
10179 // for global tls symbol GSYM.
10180 template<int size
, bool big_endian
>
10182 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
10184 unsigned int got_indx
) const
10186 if (gsym
->type() == elfcpp::STT_TLS
)
10188 for (Got_type got_type
= GOT_TYPE_TLSGD
;
10189 got_type
<= GOT_TYPE_TPREL
;
10190 got_type
= Got_type(got_type
+ 1))
10191 if (gsym
->has_got_offset(got_type
))
10193 unsigned int off
= gsym
->got_offset(got_type
);
10194 if (got_type
== GOT_TYPE_TLSGD
)
10196 if (off
== got_indx
* (size
/ 8))
10198 if (got_type
== GOT_TYPE_TPREL
)
10201 return -dtp_offset
;
10205 gold_unreachable();
10208 // The selector for powerpc object files.
10210 template<int size
, bool big_endian
>
10211 class Target_selector_powerpc
: public Target_selector
10214 Target_selector_powerpc()
10215 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
10218 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
10219 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
10221 ? (big_endian
? "elf64ppc" : "elf64lppc")
10222 : (big_endian
? "elf32ppc" : "elf32lppc")))
10226 do_instantiate_target()
10227 { return new Target_powerpc
<size
, big_endian
>(); }
10230 Target_selector_powerpc
<32, true> target_selector_ppc32
;
10231 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
10232 Target_selector_powerpc
<64, true> target_selector_ppc64
;
10233 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
10235 // Instantiate these constants for -O0
10236 template<int size
, bool big_endian
>
10237 const typename Output_data_glink
<size
, big_endian
>::Address
10238 Output_data_glink
<size
, big_endian
>::invalid_address
;
10239 template<int size
, bool big_endian
>
10240 const typename Stub_table
<size
, big_endian
>::Address
10241 Stub_table
<size
, big_endian
>::invalid_address
;
10242 template<int size
, bool big_endian
>
10243 const typename Target_powerpc
<size
, big_endian
>::Address
10244 Target_powerpc
<size
, big_endian
>::invalid_address
;
10246 } // End anonymous namespace.