1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009, 2010, 2011, 2012, 2013 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
>
66 is_branch_reloc(unsigned int r_type
);
68 template<int size
, bool big_endian
>
69 class Powerpc_relobj
: public Sized_relobj_file
<size
, big_endian
>
72 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
73 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_refs
;
74 typedef Unordered_map
<Address
, Section_refs
> Access_from
;
76 Powerpc_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
77 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
78 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
79 special_(0), has_small_toc_reloc_(false), opd_valid_(false),
80 opd_ent_(), access_from_map_(), has14_(), stub_table_(),
81 e_flags_(ehdr
.get_e_flags()), st_other_()
83 this->set_abiversion(0);
89 // Read the symbols then set up st_other vector.
91 do_read_symbols(Read_symbols_data
*);
93 // The .got2 section shndx.
98 return this->special_
;
103 // The .opd section shndx.
110 return this->special_
;
113 // Init OPD entry arrays.
115 init_opd(size_t opd_size
)
117 size_t count
= this->opd_ent_ndx(opd_size
);
118 this->opd_ent_
.resize(count
);
121 // Return section and offset of function entry for .opd + R_OFF.
123 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
125 size_t ndx
= this->opd_ent_ndx(r_off
);
126 gold_assert(ndx
< this->opd_ent_
.size());
127 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
129 *value
= this->opd_ent_
[ndx
].off
;
130 return this->opd_ent_
[ndx
].shndx
;
133 // Set section and offset of function entry for .opd + R_OFF.
135 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
137 size_t ndx
= this->opd_ent_ndx(r_off
);
138 gold_assert(ndx
< this->opd_ent_
.size());
139 this->opd_ent_
[ndx
].shndx
= shndx
;
140 this->opd_ent_
[ndx
].off
= value
;
143 // Return discard flag for .opd + R_OFF.
145 get_opd_discard(Address r_off
) const
147 size_t ndx
= this->opd_ent_ndx(r_off
);
148 gold_assert(ndx
< this->opd_ent_
.size());
149 return this->opd_ent_
[ndx
].discard
;
152 // Set discard flag for .opd + R_OFF.
154 set_opd_discard(Address r_off
)
156 size_t ndx
= this->opd_ent_ndx(r_off
);
157 gold_assert(ndx
< this->opd_ent_
.size());
158 this->opd_ent_
[ndx
].discard
= true;
163 { return this->opd_valid_
; }
167 { this->opd_valid_
= true; }
169 // Examine .rela.opd to build info about function entry points.
171 scan_opd_relocs(size_t reloc_count
,
172 const unsigned char* prelocs
,
173 const unsigned char* plocal_syms
);
175 // Perform the Sized_relobj_file method, then set up opd info from
178 do_read_relocs(Read_relocs_data
*);
181 do_find_special_sections(Read_symbols_data
* sd
);
183 // Adjust this local symbol value. Return false if the symbol
184 // should be discarded from the output file.
186 do_adjust_local_symbol(Symbol_value
<size
>* lv
) const
188 if (size
== 64 && this->opd_shndx() != 0)
191 if (lv
->input_shndx(&is_ordinary
) != this->opd_shndx())
193 if (this->get_opd_discard(lv
->input_value()))
201 { return &this->access_from_map_
; }
203 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
204 // section at DST_OFF.
206 add_reference(Object
* src_obj
,
207 unsigned int src_indx
,
208 typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
210 Section_id
src_id(src_obj
, src_indx
);
211 this->access_from_map_
[dst_off
].insert(src_id
);
214 // Add a reference to the code section specified by the .opd entry
217 add_gc_mark(typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
219 size_t ndx
= this->opd_ent_ndx(dst_off
);
220 if (ndx
>= this->opd_ent_
.size())
221 this->opd_ent_
.resize(ndx
+ 1);
222 this->opd_ent_
[ndx
].gc_mark
= true;
226 process_gc_mark(Symbol_table
* symtab
)
228 for (size_t i
= 0; i
< this->opd_ent_
.size(); i
++)
229 if (this->opd_ent_
[i
].gc_mark
)
231 unsigned int shndx
= this->opd_ent_
[i
].shndx
;
232 symtab
->gc()->worklist().push(Section_id(this, shndx
));
236 // Return offset in output GOT section that this object will use
237 // as a TOC pointer. Won't be just a constant with multi-toc support.
239 toc_base_offset() const
243 set_has_small_toc_reloc()
244 { has_small_toc_reloc_
= true; }
247 has_small_toc_reloc() const
248 { return has_small_toc_reloc_
; }
251 set_has_14bit_branch(unsigned int shndx
)
253 if (shndx
>= this->has14_
.size())
254 this->has14_
.resize(shndx
+ 1);
255 this->has14_
[shndx
] = true;
259 has_14bit_branch(unsigned int shndx
) const
260 { return shndx
< this->has14_
.size() && this->has14_
[shndx
]; }
263 set_stub_table(unsigned int shndx
, Stub_table
<size
, big_endian
>* stub_table
)
265 if (shndx
>= this->stub_table_
.size())
266 this->stub_table_
.resize(shndx
+ 1);
267 this->stub_table_
[shndx
] = stub_table
;
270 Stub_table
<size
, big_endian
>*
271 stub_table(unsigned int shndx
)
273 if (shndx
< this->stub_table_
.size())
274 return this->stub_table_
[shndx
];
280 { return this->e_flags_
& elfcpp::EF_PPC64_ABI
; }
282 // Set ABI version for input and output
284 set_abiversion(int ver
);
287 ppc64_local_entry_offset(const Symbol
* sym
) const
288 { return elfcpp::ppc64_decode_local_entry(sym
->nonvis() >> 3); }
291 ppc64_local_entry_offset(unsigned int symndx
) const
292 { return elfcpp::ppc64_decode_local_entry(this->st_other_
[symndx
] >> 5); }
303 // Return index into opd_ent_ array for .opd entry at OFF.
304 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
305 // apart when the language doesn't use the last 8-byte word, the
306 // environment pointer. Thus dividing the entry section offset by
307 // 16 will give an index into opd_ent_ that works for either layout
308 // of .opd. (It leaves some elements of the vector unused when .opd
309 // entries are spaced 24 bytes apart, but we don't know the spacing
310 // until relocations are processed, and in any case it is possible
311 // for an object to have some entries spaced 16 bytes apart and
312 // others 24 bytes apart.)
314 opd_ent_ndx(size_t off
) const
317 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
318 unsigned int special_
;
320 // For 64-bit, whether this object uses small model relocs to access
322 bool has_small_toc_reloc_
;
324 // Set at the start of gc_process_relocs, when we know opd_ent_
325 // vector is valid. The flag could be made atomic and set in
326 // do_read_relocs with memory_order_release and then tested with
327 // memory_order_acquire, potentially resulting in fewer entries in
331 // The first 8-byte word of an OPD entry gives the address of the
332 // entry point of the function. Relocatable object files have a
333 // relocation on this word. The following vector records the
334 // section and offset specified by these relocations.
335 std::vector
<Opd_ent
> opd_ent_
;
337 // References made to this object's .opd section when running
338 // gc_process_relocs for another object, before the opd_ent_ vector
339 // is valid for this object.
340 Access_from access_from_map_
;
342 // Whether input section has a 14-bit branch reloc.
343 std::vector
<bool> has14_
;
345 // The stub table to use for a given input section.
346 std::vector
<Stub_table
<size
, big_endian
>*> stub_table_
;
349 elfcpp::Elf_Word e_flags_
;
351 // ELF st_other field for local symbols.
352 std::vector
<unsigned char> st_other_
;
355 template<int size
, bool big_endian
>
356 class Powerpc_dynobj
: public Sized_dynobj
<size
, big_endian
>
359 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
361 Powerpc_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
362 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
363 : Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
364 opd_shndx_(0), opd_ent_(), e_flags_(ehdr
.get_e_flags())
366 this->set_abiversion(0);
372 // Call Sized_dynobj::do_read_symbols to read the symbols then
373 // read .opd from a dynamic object, filling in opd_ent_ vector,
375 do_read_symbols(Read_symbols_data
*);
377 // The .opd section shndx.
381 return this->opd_shndx_
;
384 // The .opd section address.
388 return this->opd_address_
;
391 // Init OPD entry arrays.
393 init_opd(size_t opd_size
)
395 size_t count
= this->opd_ent_ndx(opd_size
);
396 this->opd_ent_
.resize(count
);
399 // Return section and offset of function entry for .opd + R_OFF.
401 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
403 size_t ndx
= this->opd_ent_ndx(r_off
);
404 gold_assert(ndx
< this->opd_ent_
.size());
405 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
407 *value
= this->opd_ent_
[ndx
].off
;
408 return this->opd_ent_
[ndx
].shndx
;
411 // Set section and offset of function entry for .opd + R_OFF.
413 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
415 size_t ndx
= this->opd_ent_ndx(r_off
);
416 gold_assert(ndx
< this->opd_ent_
.size());
417 this->opd_ent_
[ndx
].shndx
= shndx
;
418 this->opd_ent_
[ndx
].off
= value
;
423 { return this->e_flags_
& elfcpp::EF_PPC64_ABI
; }
425 // Set ABI version for input and output.
427 set_abiversion(int ver
);
430 // Used to specify extent of executable sections.
433 Sec_info(Address start_
, Address len_
, unsigned int shndx_
)
434 : start(start_
), len(len_
), shndx(shndx_
)
438 operator<(const Sec_info
& that
) const
439 { return this->start
< that
.start
; }
452 // Return index into opd_ent_ array for .opd entry at OFF.
454 opd_ent_ndx(size_t off
) const
457 // For 64-bit the .opd section shndx and address.
458 unsigned int opd_shndx_
;
459 Address opd_address_
;
461 // The first 8-byte word of an OPD entry gives the address of the
462 // entry point of the function. Records the section and offset
463 // corresponding to the address. Note that in dynamic objects,
464 // offset is *not* relative to the section.
465 std::vector
<Opd_ent
> opd_ent_
;
468 elfcpp::Elf_Word e_flags_
;
471 template<int size
, bool big_endian
>
472 class Target_powerpc
: public Sized_target
<size
, big_endian
>
476 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
477 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
478 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Signed_address
;
479 static const Address invalid_address
= static_cast<Address
>(0) - 1;
480 // Offset of tp and dtp pointers from start of TLS block.
481 static const Address tp_offset
= 0x7000;
482 static const Address dtp_offset
= 0x8000;
485 : Sized_target
<size
, big_endian
>(&powerpc_info
),
486 got_(NULL
), plt_(NULL
), iplt_(NULL
), brlt_section_(NULL
),
487 glink_(NULL
), rela_dyn_(NULL
), copy_relocs_(elfcpp::R_POWERPC_COPY
),
488 tlsld_got_offset_(-1U),
489 stub_tables_(), branch_lookup_table_(), branch_info_(),
490 plt_thread_safe_(false)
494 // Process the relocations to determine unreferenced sections for
495 // garbage collection.
497 gc_process_relocs(Symbol_table
* symtab
,
499 Sized_relobj_file
<size
, big_endian
>* object
,
500 unsigned int data_shndx
,
501 unsigned int sh_type
,
502 const unsigned char* prelocs
,
504 Output_section
* output_section
,
505 bool needs_special_offset_handling
,
506 size_t local_symbol_count
,
507 const unsigned char* plocal_symbols
);
509 // Scan the relocations to look for symbol adjustments.
511 scan_relocs(Symbol_table
* symtab
,
513 Sized_relobj_file
<size
, big_endian
>* object
,
514 unsigned int data_shndx
,
515 unsigned int sh_type
,
516 const unsigned char* prelocs
,
518 Output_section
* output_section
,
519 bool needs_special_offset_handling
,
520 size_t local_symbol_count
,
521 const unsigned char* plocal_symbols
);
523 // Map input .toc section to output .got section.
525 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
527 if (size
== 64 && strcmp(name
, ".toc") == 0)
535 // Provide linker defined save/restore functions.
537 define_save_restore_funcs(Layout
*, Symbol_table
*);
539 // No stubs unless a final link.
542 { return !parameters
->options().relocatable(); }
545 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
548 do_plt_fde_location(const Output_data
*, unsigned char*,
549 uint64_t*, off_t
*) const;
551 // Stash info about branches, for stub generation.
553 push_branch(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
554 unsigned int data_shndx
, Address r_offset
,
555 unsigned int r_type
, unsigned int r_sym
, Address addend
)
557 Branch_info
info(ppc_object
, data_shndx
, r_offset
, r_type
, r_sym
, addend
);
558 this->branch_info_
.push_back(info
);
559 if (r_type
== elfcpp::R_POWERPC_REL14
560 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
561 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
562 ppc_object
->set_has_14bit_branch(data_shndx
);
565 Stub_table
<size
, big_endian
>*
569 do_define_standard_symbols(Symbol_table
*, Layout
*);
571 // Finalize the sections.
573 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
575 // Return the value to use for a dynamic which requires special
578 do_dynsym_value(const Symbol
*) const;
580 // Return the PLT address to use for a local symbol.
582 do_plt_address_for_local(const Relobj
*, unsigned int) const;
584 // Return the PLT address to use for a global symbol.
586 do_plt_address_for_global(const Symbol
*) const;
588 // Return the offset to use for the GOT_INDX'th got entry which is
589 // for a local tls symbol specified by OBJECT, SYMNDX.
591 do_tls_offset_for_local(const Relobj
* object
,
593 unsigned int got_indx
) const;
595 // Return the offset to use for the GOT_INDX'th got entry which is
596 // for global tls symbol GSYM.
598 do_tls_offset_for_global(Symbol
* gsym
, unsigned int got_indx
) const;
601 do_function_location(Symbol_location
*) const;
604 do_can_check_for_function_pointers() const
607 // Relocate a section.
609 relocate_section(const Relocate_info
<size
, big_endian
>*,
610 unsigned int sh_type
,
611 const unsigned char* prelocs
,
613 Output_section
* output_section
,
614 bool needs_special_offset_handling
,
616 Address view_address
,
617 section_size_type view_size
,
618 const Reloc_symbol_changes
*);
620 // Scan the relocs during a relocatable link.
622 scan_relocatable_relocs(Symbol_table
* symtab
,
624 Sized_relobj_file
<size
, big_endian
>* object
,
625 unsigned int data_shndx
,
626 unsigned int sh_type
,
627 const unsigned char* prelocs
,
629 Output_section
* output_section
,
630 bool needs_special_offset_handling
,
631 size_t local_symbol_count
,
632 const unsigned char* plocal_symbols
,
633 Relocatable_relocs
*);
635 // Emit relocations for a section.
637 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
638 unsigned int sh_type
,
639 const unsigned char* prelocs
,
641 Output_section
* output_section
,
642 typename
elfcpp::Elf_types
<size
>::Elf_Off
643 offset_in_output_section
,
644 const Relocatable_relocs
*,
646 Address view_address
,
648 unsigned char* reloc_view
,
649 section_size_type reloc_view_size
);
651 // Return whether SYM is defined by the ABI.
653 do_is_defined_by_abi(const Symbol
* sym
) const
655 return strcmp(sym
->name(), "__tls_get_addr") == 0;
658 // Return the size of the GOT section.
662 gold_assert(this->got_
!= NULL
);
663 return this->got_
->data_size();
666 // Get the PLT section.
667 const Output_data_plt_powerpc
<size
, big_endian
>*
670 gold_assert(this->plt_
!= NULL
);
674 // Get the IPLT section.
675 const Output_data_plt_powerpc
<size
, big_endian
>*
678 gold_assert(this->iplt_
!= NULL
);
682 // Get the .glink section.
683 const Output_data_glink
<size
, big_endian
>*
684 glink_section() const
686 gold_assert(this->glink_
!= NULL
);
690 bool has_glink() const
691 { return this->glink_
!= NULL
; }
693 // Get the GOT section.
694 const Output_data_got_powerpc
<size
, big_endian
>*
697 gold_assert(this->got_
!= NULL
);
701 // Get the GOT section, creating it if necessary.
702 Output_data_got_powerpc
<size
, big_endian
>*
703 got_section(Symbol_table
*, Layout
*);
706 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
707 const elfcpp::Ehdr
<size
, big_endian
>&);
709 // Return the number of entries in the GOT.
711 got_entry_count() const
713 if (this->got_
== NULL
)
715 return this->got_size() / (size
/ 8);
718 // Return the number of entries in the PLT.
720 plt_entry_count() const;
722 // Return the offset of the first non-reserved PLT entry.
724 first_plt_entry_offset() const
728 if (this->abiversion() >= 2)
733 // Return the size of each PLT entry.
735 plt_entry_size() const
739 if (this->abiversion() >= 2)
744 // Add any special sections for this symbol to the gc work list.
745 // For powerpc64, this adds the code section of a function
748 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
750 // Handle target specific gc actions when adding a gc reference from
751 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
752 // and DST_OFF. For powerpc64, this adds a referenc to the code
753 // section of a function descriptor.
755 do_gc_add_reference(Symbol_table
* symtab
,
757 unsigned int src_shndx
,
759 unsigned int dst_shndx
,
760 Address dst_off
) const;
762 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
765 { return this->stub_tables_
; }
767 const Output_data_brlt_powerpc
<size
, big_endian
>*
769 { return this->brlt_section_
; }
772 add_branch_lookup_table(Address to
)
774 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
775 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
779 find_branch_lookup_table(Address to
)
781 typename
Branch_lookup_table::const_iterator p
782 = this->branch_lookup_table_
.find(to
);
783 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
787 write_branch_lookup_table(unsigned char *oview
)
789 for (typename
Branch_lookup_table::const_iterator p
790 = this->branch_lookup_table_
.begin();
791 p
!= this->branch_lookup_table_
.end();
794 elfcpp::Swap
<size
, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
799 plt_thread_safe() const
800 { return this->plt_thread_safe_
; }
804 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI
; }
807 set_abiversion (int ver
)
809 elfcpp::Elf_Word flags
= this->processor_specific_flags();
810 flags
&= ~elfcpp::EF_PPC64_ABI
;
811 flags
|= ver
& elfcpp::EF_PPC64_ABI
;
812 this->set_processor_specific_flags(flags
);
815 // Offset to to save stack slot
818 { return this->abiversion() < 2 ? 40 : 24; }
834 : tls_get_addr_(NOT_EXPECTED
),
835 relinfo_(NULL
), relnum_(0), r_offset_(0)
840 if (this->tls_get_addr_
!= NOT_EXPECTED
)
847 if (this->relinfo_
!= NULL
)
848 gold_error_at_location(this->relinfo_
, this->relnum_
, this->r_offset_
,
849 _("missing expected __tls_get_addr call"));
853 expect_tls_get_addr_call(
854 const Relocate_info
<size
, big_endian
>* relinfo
,
858 this->tls_get_addr_
= EXPECTED
;
859 this->relinfo_
= relinfo
;
860 this->relnum_
= relnum
;
861 this->r_offset_
= r_offset
;
865 expect_tls_get_addr_call()
866 { this->tls_get_addr_
= EXPECTED
; }
869 skip_next_tls_get_addr_call()
870 {this->tls_get_addr_
= SKIP
; }
873 maybe_skip_tls_get_addr_call(unsigned int r_type
, const Symbol
* gsym
)
875 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
876 || r_type
== elfcpp::R_PPC_PLTREL24
)
878 && strcmp(gsym
->name(), "__tls_get_addr") == 0);
879 Tls_get_addr last_tls
= this->tls_get_addr_
;
880 this->tls_get_addr_
= NOT_EXPECTED
;
881 if (is_tls_call
&& last_tls
!= EXPECTED
)
883 else if (!is_tls_call
&& last_tls
!= NOT_EXPECTED
)
892 // What we're up to regarding calls to __tls_get_addr.
893 // On powerpc, the branch and link insn making a call to
894 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
895 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
896 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
897 // The marker relocation always comes first, and has the same
898 // symbol as the reloc on the insn setting up the __tls_get_addr
899 // argument. This ties the arg setup insn with the call insn,
900 // allowing ld to safely optimize away the call. We check that
901 // every call to __tls_get_addr has a marker relocation, and that
902 // every marker relocation is on a call to __tls_get_addr.
903 Tls_get_addr tls_get_addr_
;
904 // Info about the last reloc for error message.
905 const Relocate_info
<size
, big_endian
>* relinfo_
;
910 // The class which scans relocations.
911 class Scan
: protected Track_tls
914 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
917 : Track_tls(), issued_non_pic_error_(false)
921 get_reference_flags(unsigned int r_type
, const Target_powerpc
* target
);
924 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
925 Sized_relobj_file
<size
, big_endian
>* object
,
926 unsigned int data_shndx
,
927 Output_section
* output_section
,
928 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
929 const elfcpp::Sym
<size
, big_endian
>& lsym
,
933 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
934 Sized_relobj_file
<size
, big_endian
>* object
,
935 unsigned int data_shndx
,
936 Output_section
* output_section
,
937 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
941 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
943 Sized_relobj_file
<size
, big_endian
>* ,
946 const elfcpp::Rela
<size
, big_endian
>& ,
948 const elfcpp::Sym
<size
, big_endian
>&)
950 // PowerPC64 .opd is not folded, so any identical function text
951 // may be folded and we'll still keep function addresses distinct.
952 // That means no reloc is of concern here.
955 // For 32-bit, conservatively assume anything but calls to
956 // function code might be taking the address of the function.
957 return !is_branch_reloc(r_type
);
961 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
963 Sized_relobj_file
<size
, big_endian
>* ,
966 const elfcpp::Rela
<size
, big_endian
>& ,
973 return !is_branch_reloc(r_type
);
977 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, big_endian
>* object
,
978 unsigned int r_type
, bool report_err
);
982 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
983 unsigned int r_type
);
986 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
987 unsigned int r_type
, Symbol
*);
990 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
991 Target_powerpc
* target
);
994 check_non_pic(Relobj
*, unsigned int r_type
);
996 // Whether we have issued an error about a non-PIC compilation.
997 bool issued_non_pic_error_
;
1001 symval_for_branch(const Symbol_table
* symtab
, Address value
,
1002 const Sized_symbol
<size
>* gsym
,
1003 Powerpc_relobj
<size
, big_endian
>* object
,
1004 unsigned int *dest_shndx
);
1006 // The class which implements relocation.
1007 class Relocate
: protected Track_tls
1010 // Use 'at' branch hints when true, 'y' when false.
1011 // FIXME maybe: set this with an option.
1012 static const bool is_isa_v2
= true;
1018 // Do a relocation. Return false if the caller should not issue
1019 // any warnings about this relocation.
1021 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
1022 Output_section
*, size_t relnum
,
1023 const elfcpp::Rela
<size
, big_endian
>&,
1024 unsigned int r_type
, const Sized_symbol
<size
>*,
1025 const Symbol_value
<size
>*,
1027 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1031 class Relocate_comdat_behavior
1034 // Decide what the linker should do for relocations that refer to
1035 // discarded comdat sections.
1036 inline Comdat_behavior
1037 get(const char* name
)
1039 gold::Default_comdat_behavior default_behavior
;
1040 Comdat_behavior ret
= default_behavior
.get(name
);
1041 if (ret
== CB_WARNING
)
1044 && (strcmp(name
, ".fixup") == 0
1045 || strcmp(name
, ".got2") == 0))
1048 && (strcmp(name
, ".opd") == 0
1049 || strcmp(name
, ".toc") == 0
1050 || strcmp(name
, ".toc1") == 0))
1057 // A class which returns the size required for a relocation type,
1058 // used while scanning relocs during a relocatable link.
1059 class Relocatable_size_for_reloc
1063 get_size_for_reloc(unsigned int, Relobj
*)
1070 // Optimize the TLS relocation type based on what we know about the
1071 // symbol. IS_FINAL is true if the final address of this symbol is
1072 // known at link time.
1074 tls::Tls_optimization
1075 optimize_tls_gd(bool is_final
)
1077 // If we are generating a shared library, then we can't do anything
1079 if (parameters
->options().shared())
1080 return tls::TLSOPT_NONE
;
1083 return tls::TLSOPT_TO_IE
;
1084 return tls::TLSOPT_TO_LE
;
1087 tls::Tls_optimization
1090 if (parameters
->options().shared())
1091 return tls::TLSOPT_NONE
;
1093 return tls::TLSOPT_TO_LE
;
1096 tls::Tls_optimization
1097 optimize_tls_ie(bool is_final
)
1099 if (!is_final
|| parameters
->options().shared())
1100 return tls::TLSOPT_NONE
;
1102 return tls::TLSOPT_TO_LE
;
1107 make_glink_section(Layout
*);
1109 // Create the PLT section.
1111 make_plt_section(Symbol_table
*, Layout
*);
1114 make_iplt_section(Symbol_table
*, Layout
*);
1117 make_brlt_section(Layout
*);
1119 // Create a PLT entry for a global symbol.
1121 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1123 // Create a PLT entry for a local IFUNC symbol.
1125 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1126 Sized_relobj_file
<size
, big_endian
>*,
1130 // Create a GOT entry for local dynamic __tls_get_addr.
1132 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
1133 Sized_relobj_file
<size
, big_endian
>* object
);
1136 tlsld_got_offset() const
1138 return this->tlsld_got_offset_
;
1141 // Get the dynamic reloc section, creating it if necessary.
1143 rela_dyn_section(Layout
*);
1145 // Similarly, but for ifunc symbols get the one for ifunc.
1147 rela_dyn_section(Symbol_table
*, Layout
*, bool for_ifunc
);
1149 // Copy a relocation against a global symbol.
1151 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1152 Sized_relobj_file
<size
, big_endian
>* object
,
1153 unsigned int shndx
, Output_section
* output_section
,
1154 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
1156 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1157 symtab
->get_sized_symbol
<size
>(sym
),
1158 object
, shndx
, output_section
,
1159 reloc
, this->rela_dyn_section(layout
));
1162 // Look over all the input sections, deciding where to place stubs.
1164 group_sections(Layout
*, const Task
*);
1166 // Sort output sections by address.
1167 struct Sort_sections
1170 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
1171 { return sec1
->address() < sec2
->address(); }
1177 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1178 unsigned int data_shndx
,
1180 unsigned int r_type
,
1183 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
1184 r_type_(r_type
), r_sym_(r_sym
), addend_(addend
)
1190 // If this branch needs a plt call stub, or a long branch stub, make one.
1192 make_stub(Stub_table
<size
, big_endian
>*,
1193 Stub_table
<size
, big_endian
>*,
1194 Symbol_table
*) const;
1197 // The branch location..
1198 Powerpc_relobj
<size
, big_endian
>* object_
;
1199 unsigned int shndx_
;
1201 // ..and the branch type and destination.
1202 unsigned int r_type_
;
1203 unsigned int r_sym_
;
1207 // Information about this specific target which we pass to the
1208 // general Target structure.
1209 static Target::Target_info powerpc_info
;
1211 // The types of GOT entries needed for this platform.
1212 // These values are exposed to the ABI in an incremental link.
1213 // Do not renumber existing values without changing the version
1214 // number of the .gnu_incremental_inputs section.
1218 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
1219 GOT_TYPE_DTPREL
, // entry for @got@dtprel
1220 GOT_TYPE_TPREL
// entry for @got@tprel
1224 Output_data_got_powerpc
<size
, big_endian
>* got_
;
1225 // The PLT section. This is a container for a table of addresses,
1226 // and their relocations. Each address in the PLT has a dynamic
1227 // relocation (R_*_JMP_SLOT) and each address will have a
1228 // corresponding entry in .glink for lazy resolution of the PLT.
1229 // ppc32 initialises the PLT to point at the .glink entry, while
1230 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1231 // linker adds a stub that loads the PLT entry into ctr then
1232 // branches to ctr. There may be more than one stub for each PLT
1233 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1234 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1235 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
1236 // The IPLT section. Like plt_, this is a container for a table of
1237 // addresses and their relocations, specifically for STT_GNU_IFUNC
1238 // functions that resolve locally (STT_GNU_IFUNC functions that
1239 // don't resolve locally go in PLT). Unlike plt_, these have no
1240 // entry in .glink for lazy resolution, and the relocation section
1241 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1242 // the relocation section may contain relocations against
1243 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1244 // relocation section will appear at the end of other dynamic
1245 // relocations, so that ld.so applies these relocations after other
1246 // dynamic relocations. In a static executable, the relocation
1247 // section is emitted and marked with __rela_iplt_start and
1248 // __rela_iplt_end symbols.
1249 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
1250 // Section holding long branch destinations.
1251 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
1252 // The .glink section.
1253 Output_data_glink
<size
, big_endian
>* glink_
;
1254 // The dynamic reloc section.
1255 Reloc_section
* rela_dyn_
;
1256 // Relocs saved to avoid a COPY reloc.
1257 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
1258 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1259 unsigned int tlsld_got_offset_
;
1261 Stub_tables stub_tables_
;
1262 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
1263 Branch_lookup_table branch_lookup_table_
;
1265 typedef std::vector
<Branch_info
> Branches
;
1266 Branches branch_info_
;
1268 bool plt_thread_safe_
;
1272 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
1275 true, // is_big_endian
1276 elfcpp::EM_PPC
, // machine_code
1277 false, // has_make_symbol
1278 false, // has_resolve
1279 false, // has_code_fill
1280 true, // is_default_stack_executable
1281 false, // can_icf_inline_merge_sections
1283 "/usr/lib/ld.so.1", // dynamic_linker
1284 0x10000000, // default_text_segment_address
1285 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1286 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1287 false, // isolate_execinstr
1289 elfcpp::SHN_UNDEF
, // small_common_shndx
1290 elfcpp::SHN_UNDEF
, // large_common_shndx
1291 0, // small_common_section_flags
1292 0, // large_common_section_flags
1293 NULL
, // attributes_section
1294 NULL
, // attributes_vendor
1295 "_start" // entry_symbol_name
1299 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
1302 false, // is_big_endian
1303 elfcpp::EM_PPC
, // machine_code
1304 false, // has_make_symbol
1305 false, // has_resolve
1306 false, // has_code_fill
1307 true, // is_default_stack_executable
1308 false, // can_icf_inline_merge_sections
1310 "/usr/lib/ld.so.1", // dynamic_linker
1311 0x10000000, // default_text_segment_address
1312 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1313 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1314 false, // isolate_execinstr
1316 elfcpp::SHN_UNDEF
, // small_common_shndx
1317 elfcpp::SHN_UNDEF
, // large_common_shndx
1318 0, // small_common_section_flags
1319 0, // large_common_section_flags
1320 NULL
, // attributes_section
1321 NULL
, // attributes_vendor
1322 "_start" // entry_symbol_name
1326 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1329 true, // is_big_endian
1330 elfcpp::EM_PPC64
, // machine_code
1331 false, // has_make_symbol
1332 false, // has_resolve
1333 false, // has_code_fill
1334 true, // is_default_stack_executable
1335 false, // can_icf_inline_merge_sections
1337 "/usr/lib/ld.so.1", // dynamic_linker
1338 0x10000000, // default_text_segment_address
1339 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1340 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1341 false, // isolate_execinstr
1343 elfcpp::SHN_UNDEF
, // small_common_shndx
1344 elfcpp::SHN_UNDEF
, // large_common_shndx
1345 0, // small_common_section_flags
1346 0, // large_common_section_flags
1347 NULL
, // attributes_section
1348 NULL
, // attributes_vendor
1349 "_start" // entry_symbol_name
1353 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1356 false, // is_big_endian
1357 elfcpp::EM_PPC64
, // machine_code
1358 false, // has_make_symbol
1359 false, // has_resolve
1360 false, // has_code_fill
1361 true, // is_default_stack_executable
1362 false, // can_icf_inline_merge_sections
1364 "/usr/lib/ld.so.1", // dynamic_linker
1365 0x10000000, // default_text_segment_address
1366 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1367 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1368 false, // isolate_execinstr
1370 elfcpp::SHN_UNDEF
, // small_common_shndx
1371 elfcpp::SHN_UNDEF
, // large_common_shndx
1372 0, // small_common_section_flags
1373 0, // large_common_section_flags
1374 NULL
, // attributes_section
1375 NULL
, // attributes_vendor
1376 "_start" // entry_symbol_name
1380 is_branch_reloc(unsigned int r_type
)
1382 return (r_type
== elfcpp::R_POWERPC_REL24
1383 || r_type
== elfcpp::R_PPC_PLTREL24
1384 || r_type
== elfcpp::R_PPC_LOCAL24PC
1385 || r_type
== elfcpp::R_POWERPC_REL14
1386 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1387 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1388 || r_type
== elfcpp::R_POWERPC_ADDR24
1389 || r_type
== elfcpp::R_POWERPC_ADDR14
1390 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1391 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1394 // If INSN is an opcode that may be used with an @tls operand, return
1395 // the transformed insn for TLS optimisation, otherwise return 0. If
1396 // REG is non-zero only match an insn with RB or RA equal to REG.
1398 at_tls_transform(uint32_t insn
, unsigned int reg
)
1400 if ((insn
& (0x3f << 26)) != 31 << 26)
1404 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1405 rtra
= insn
& ((1 << 26) - (1 << 16));
1406 else if (((insn
>> 16) & 0x1f) == reg
)
1407 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1411 if ((insn
& (0x3ff << 1)) == 266 << 1)
1414 else if ((insn
& (0x1f << 1)) == 23 << 1
1415 && ((insn
& (0x1f << 6)) < 14 << 6
1416 || ((insn
& (0x1f << 6)) >= 16 << 6
1417 && (insn
& (0x1f << 6)) < 24 << 6)))
1418 // load and store indexed -> dform
1419 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1420 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1421 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1422 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1423 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1425 insn
= (58 << 26) | 2;
1433 template<int size
, bool big_endian
>
1434 class Powerpc_relocate_functions
1451 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1452 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1454 template<int valsize
>
1456 has_overflow_signed(Address value
)
1458 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1459 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1460 limit
<<= ((valsize
- 1) >> 1);
1461 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1462 return value
+ limit
> (limit
<< 1) - 1;
1465 template<int valsize
>
1467 has_overflow_bitfield(Address value
)
1469 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1470 limit
<<= ((valsize
- 1) >> 1);
1471 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1472 return value
> (limit
<< 1) - 1 && value
+ limit
> (limit
<< 1) - 1;
1475 template<int valsize
>
1476 static inline Status
1477 overflowed(Address value
, Overflow_check overflow
)
1479 if (overflow
== CHECK_SIGNED
)
1481 if (has_overflow_signed
<valsize
>(value
))
1482 return STATUS_OVERFLOW
;
1484 else if (overflow
== CHECK_BITFIELD
)
1486 if (has_overflow_bitfield
<valsize
>(value
))
1487 return STATUS_OVERFLOW
;
1492 // Do a simple RELA relocation
1493 template<int valsize
>
1494 static inline Status
1495 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1497 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1498 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1499 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
);
1500 return overflowed
<valsize
>(value
, overflow
);
1503 template<int valsize
>
1504 static inline Status
1505 rela(unsigned char* view
,
1506 unsigned int right_shift
,
1507 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
1509 Overflow_check overflow
)
1511 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1512 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1513 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
1514 Valtype reloc
= value
>> right_shift
;
1517 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
1518 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1521 // Do a simple RELA relocation, unaligned.
1522 template<int valsize
>
1523 static inline Status
1524 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1526 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
, value
);
1527 return overflowed
<valsize
>(value
, overflow
);
1530 template<int valsize
>
1531 static inline Status
1532 rela_ua(unsigned char* view
,
1533 unsigned int right_shift
,
1534 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
1536 Overflow_check overflow
)
1538 typedef typename
elfcpp::Swap_unaligned
<valsize
, big_endian
>::Valtype
1540 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(view
);
1541 Valtype reloc
= value
>> right_shift
;
1544 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
, val
| reloc
);
1545 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1549 // R_PPC64_ADDR64: (Symbol + Addend)
1551 addr64(unsigned char* view
, Address value
)
1552 { This::template rela
<64>(view
, value
, CHECK_NONE
); }
1554 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1556 addr64_u(unsigned char* view
, Address value
)
1557 { This::template rela_ua
<64>(view
, value
, CHECK_NONE
); }
1559 // R_POWERPC_ADDR32: (Symbol + Addend)
1560 static inline Status
1561 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
1562 { return This::template rela
<32>(view
, value
, overflow
); }
1564 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1565 static inline Status
1566 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1567 { return This::template rela_ua
<32>(view
, value
, overflow
); }
1569 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1570 static inline Status
1571 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
1573 Status stat
= This::template rela
<32>(view
, 0, 0x03fffffc, value
, overflow
);
1574 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1575 stat
= STATUS_OVERFLOW
;
1579 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1580 static inline Status
1581 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
1582 { return This::template rela
<16>(view
, value
, overflow
); }
1584 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1585 static inline Status
1586 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1587 { return This::template rela_ua
<16>(view
, value
, overflow
); }
1589 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1590 static inline Status
1591 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
1593 Status stat
= This::template rela
<16>(view
, 0, 0xfffc, value
, overflow
);
1594 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1595 stat
= STATUS_OVERFLOW
;
1599 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1601 addr16_hi(unsigned char* view
, Address value
)
1602 { This::template rela
<16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
1604 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1606 addr16_ha(unsigned char* view
, Address value
)
1607 { This::addr16_hi(view
, value
+ 0x8000); }
1609 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1611 addr16_hi2(unsigned char* view
, Address value
)
1612 { This::template rela
<16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
1614 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1616 addr16_ha2(unsigned char* view
, Address value
)
1617 { This::addr16_hi2(view
, value
+ 0x8000); }
1619 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1621 addr16_hi3(unsigned char* view
, Address value
)
1622 { This::template rela
<16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
1624 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1626 addr16_ha3(unsigned char* view
, Address value
)
1627 { This::addr16_hi3(view
, value
+ 0x8000); }
1629 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1630 static inline Status
1631 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
1633 Status stat
= This::template rela
<32>(view
, 0, 0xfffc, value
, overflow
);
1634 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1635 stat
= STATUS_OVERFLOW
;
1640 // Set ABI version for input and output.
1642 template<int size
, bool big_endian
>
1644 Powerpc_relobj
<size
, big_endian
>::set_abiversion(int ver
)
1646 this->e_flags_
|= ver
;
1647 if (this->abiversion() != 0)
1649 Target_powerpc
<size
, big_endian
>* target
=
1650 static_cast<Target_powerpc
<size
, big_endian
>*>(
1651 parameters
->sized_target
<size
, big_endian
>());
1652 if (target
->abiversion() == 0)
1653 target
->set_abiversion(this->abiversion());
1654 else if (target
->abiversion() != this->abiversion())
1655 gold_error(_("%s: ABI version %d is not compatible "
1656 "with ABI version %d output"),
1657 this->name().c_str(),
1658 this->abiversion(), target
->abiversion());
1663 // Stash away the index of .got2 or .opd in a relocatable object, if
1664 // such a section exists.
1666 template<int size
, bool big_endian
>
1668 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
1669 Read_symbols_data
* sd
)
1671 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1672 const unsigned char* namesu
= sd
->section_names
->data();
1673 const char* names
= reinterpret_cast<const char*>(namesu
);
1674 section_size_type names_size
= sd
->section_names_size
;
1675 const unsigned char* s
;
1677 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
,
1678 size
== 32 ? ".got2" : ".opd",
1679 names
, names_size
, NULL
);
1682 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
1683 this->special_
= ndx
;
1686 if (this->abiversion() == 0)
1687 this->set_abiversion(1);
1688 else if (this->abiversion() > 1)
1689 gold_error(_("%s: .opd invalid in abiv%d"),
1690 this->name().c_str(), this->abiversion());
1693 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
1696 // Examine .rela.opd to build info about function entry points.
1698 template<int size
, bool big_endian
>
1700 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
1702 const unsigned char* prelocs
,
1703 const unsigned char* plocal_syms
)
1707 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
1709 const int reloc_size
1710 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
1711 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1712 Address expected_off
= 0;
1713 bool regular
= true;
1714 unsigned int opd_ent_size
= 0;
1716 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
1718 Reltype
reloc(prelocs
);
1719 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
1720 = reloc
.get_r_info();
1721 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
1722 if (r_type
== elfcpp::R_PPC64_ADDR64
)
1724 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1725 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
1728 if (r_sym
< this->local_symbol_count())
1730 typename
elfcpp::Sym
<size
, big_endian
>
1731 lsym(plocal_syms
+ r_sym
* sym_size
);
1732 shndx
= lsym
.get_st_shndx();
1733 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1734 value
= lsym
.get_st_value();
1737 shndx
= this->symbol_section_and_value(r_sym
, &value
,
1739 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
1740 value
+ reloc
.get_r_addend());
1743 expected_off
= reloc
.get_r_offset();
1744 opd_ent_size
= expected_off
;
1746 else if (expected_off
!= reloc
.get_r_offset())
1748 expected_off
+= opd_ent_size
;
1750 else if (r_type
== elfcpp::R_PPC64_TOC
)
1752 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
1757 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1758 this->name().c_str(), r_type
);
1762 if (reloc_count
<= 2)
1763 opd_ent_size
= this->section_size(this->opd_shndx());
1764 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
1768 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1769 this->name().c_str());
1775 template<int size
, bool big_endian
>
1777 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
1779 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
1782 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
1783 p
!= rd
->relocs
.end();
1786 if (p
->data_shndx
== this->opd_shndx())
1788 uint64_t opd_size
= this->section_size(this->opd_shndx());
1789 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
1792 this->init_opd(opd_size
);
1793 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
1794 rd
->local_symbols
->data());
1802 // Read the symbols then set up st_other vector.
1804 template<int size
, bool big_endian
>
1806 Powerpc_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
1808 Sized_relobj_file
<size
, big_endian
>::do_read_symbols(sd
);
1811 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
1812 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1813 const unsigned int loccount
= this->do_local_symbol_count();
1816 this->st_other_
.resize(loccount
);
1817 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1818 off_t locsize
= loccount
* sym_size
;
1819 const unsigned int symtab_shndx
= this->symtab_shndx();
1820 const unsigned char *psymtab
= pshdrs
+ symtab_shndx
* shdr_size
;
1821 typename
elfcpp::Shdr
<size
, big_endian
> shdr(psymtab
);
1822 const unsigned char* psyms
= this->get_view(shdr
.get_sh_offset(),
1823 locsize
, true, false);
1825 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
1827 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
1828 unsigned char st_other
= sym
.get_st_other();
1829 this->st_other_
[i
] = st_other
;
1830 if ((st_other
& elfcpp::STO_PPC64_LOCAL_MASK
) != 0)
1832 if (this->abiversion() == 0)
1833 this->set_abiversion(2);
1834 else if (this->abiversion() < 2)
1835 gold_error(_("%s: local symbol %d has invalid st_other"
1836 " for ABI version 1"),
1837 this->name().c_str(), i
);
1844 template<int size
, bool big_endian
>
1846 Powerpc_dynobj
<size
, big_endian
>::set_abiversion(int ver
)
1848 this->e_flags_
|= ver
;
1849 if (this->abiversion() != 0)
1851 Target_powerpc
<size
, big_endian
>* target
=
1852 static_cast<Target_powerpc
<size
, big_endian
>*>(
1853 parameters
->sized_target
<size
, big_endian
>());
1854 if (target
->abiversion() == 0)
1855 target
->set_abiversion(this->abiversion());
1856 else if (target
->abiversion() != this->abiversion())
1857 gold_error(_("%s: ABI version %d is not compatible "
1858 "with ABI version %d output"),
1859 this->name().c_str(),
1860 this->abiversion(), target
->abiversion());
1865 // Call Sized_dynobj::do_read_symbols to read the symbols then
1866 // read .opd from a dynamic object, filling in opd_ent_ vector,
1868 template<int size
, bool big_endian
>
1870 Powerpc_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
1872 Sized_dynobj
<size
, big_endian
>::do_read_symbols(sd
);
1875 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
1876 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1877 const unsigned char* namesu
= sd
->section_names
->data();
1878 const char* names
= reinterpret_cast<const char*>(namesu
);
1879 const unsigned char* s
= NULL
;
1880 const unsigned char* opd
;
1881 section_size_type opd_size
;
1883 // Find and read .opd section.
1886 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".opd", names
,
1887 sd
->section_names_size
,
1892 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
1893 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
1894 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
1896 if (this->abiversion() == 0)
1897 this->set_abiversion(1);
1898 else if (this->abiversion() > 1)
1899 gold_error(_("%s: .opd invalid in abiv%d"),
1900 this->name().c_str(), this->abiversion());
1902 this->opd_shndx_
= (s
- pshdrs
) / shdr_size
;
1903 this->opd_address_
= shdr
.get_sh_addr();
1904 opd_size
= convert_to_section_size_type(shdr
.get_sh_size());
1905 opd
= this->get_view(shdr
.get_sh_offset(), opd_size
,
1911 // Build set of executable sections.
1912 // Using a set is probably overkill. There is likely to be only
1913 // a few executable sections, typically .init, .text and .fini,
1914 // and they are generally grouped together.
1915 typedef std::set
<Sec_info
> Exec_sections
;
1916 Exec_sections exec_sections
;
1918 for (unsigned int i
= 1; i
< this->shnum(); ++i
, s
+= shdr_size
)
1920 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
1921 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
1922 && ((shdr
.get_sh_flags()
1923 & (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
1924 == (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
1925 && shdr
.get_sh_size() != 0)
1927 exec_sections
.insert(Sec_info(shdr
.get_sh_addr(),
1928 shdr
.get_sh_size(), i
));
1931 if (exec_sections
.empty())
1934 // Look over the OPD entries. This is complicated by the fact
1935 // that some binaries will use two-word entries while others
1936 // will use the standard three-word entries. In most cases
1937 // the third word (the environment pointer for languages like
1938 // Pascal) is unused and will be zero. If the third word is
1939 // used it should not be pointing into executable sections,
1941 this->init_opd(opd_size
);
1942 for (const unsigned char* p
= opd
; p
< opd
+ opd_size
; p
+= 8)
1944 typedef typename
elfcpp::Swap
<64, big_endian
>::Valtype Valtype
;
1945 const Valtype
* valp
= reinterpret_cast<const Valtype
*>(p
);
1946 Valtype val
= elfcpp::Swap
<64, big_endian
>::readval(valp
);
1948 // Chances are that this is the third word of an OPD entry.
1950 typename
Exec_sections::const_iterator e
1951 = exec_sections
.upper_bound(Sec_info(val
, 0, 0));
1952 if (e
!= exec_sections
.begin())
1955 if (e
->start
<= val
&& val
< e
->start
+ e
->len
)
1957 // We have an address in an executable section.
1958 // VAL ought to be the function entry, set it up.
1959 this->set_opd_ent(p
- opd
, e
->shndx
, val
);
1960 // Skip second word of OPD entry, the TOC pointer.
1964 // If we didn't match any executable sections, we likely
1965 // have a non-zero third word in the OPD entry.
1970 // Set up some symbols.
1972 template<int size
, bool big_endian
>
1974 Target_powerpc
<size
, big_endian
>::do_define_standard_symbols(
1975 Symbol_table
* symtab
,
1980 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
1981 // undefined when scanning relocs (and thus requires
1982 // non-relative dynamic relocs). The proper value will be
1984 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
1985 if (gotsym
!= NULL
&& gotsym
->is_undefined())
1987 Target_powerpc
<size
, big_endian
>* target
=
1988 static_cast<Target_powerpc
<size
, big_endian
>*>(
1989 parameters
->sized_target
<size
, big_endian
>());
1990 Output_data_got_powerpc
<size
, big_endian
>* got
1991 = target
->got_section(symtab
, layout
);
1992 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1993 Symbol_table::PREDEFINED
,
1997 elfcpp::STV_HIDDEN
, 0,
2001 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2002 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
2003 if (sdasym
!= NULL
&& sdasym
->is_undefined())
2005 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
2007 = layout
->add_output_section_data(".sdata", 0,
2009 | elfcpp::SHF_WRITE
,
2010 sdata
, ORDER_SMALL_DATA
, false);
2011 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
2012 Symbol_table::PREDEFINED
,
2013 os
, 32768, 0, elfcpp::STT_OBJECT
,
2014 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
2020 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2021 Symbol
*gotsym
= symtab
->lookup(".TOC.", NULL
);
2022 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2024 Target_powerpc
<size
, big_endian
>* target
=
2025 static_cast<Target_powerpc
<size
, big_endian
>*>(
2026 parameters
->sized_target
<size
, big_endian
>());
2027 Output_data_got_powerpc
<size
, big_endian
>* got
2028 = target
->got_section(symtab
, layout
);
2029 symtab
->define_in_output_data(".TOC.", NULL
,
2030 Symbol_table::PREDEFINED
,
2034 elfcpp::STV_HIDDEN
, 0,
2040 // Set up PowerPC target specific relobj.
2042 template<int size
, bool big_endian
>
2044 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
2045 const std::string
& name
,
2046 Input_file
* input_file
,
2047 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
2049 int et
= ehdr
.get_e_type();
2050 // ET_EXEC files are valid input for --just-symbols/-R,
2051 // and we treat them as relocatable objects.
2052 if (et
== elfcpp::ET_REL
2053 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
2055 Powerpc_relobj
<size
, big_endian
>* obj
=
2056 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2060 else if (et
== elfcpp::ET_DYN
)
2062 Powerpc_dynobj
<size
, big_endian
>* obj
=
2063 new Powerpc_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2069 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
2074 template<int size
, bool big_endian
>
2075 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
2078 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
2079 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
2081 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
2082 : Output_data_got
<size
, big_endian
>(),
2083 symtab_(symtab
), layout_(layout
),
2084 header_ent_cnt_(size
== 32 ? 3 : 1),
2085 header_index_(size
== 32 ? 0x2000 : 0)
2088 // Override all the Output_data_got methods we use so as to first call
2091 add_global(Symbol
* gsym
, unsigned int got_type
)
2093 this->reserve_ent();
2094 return Output_data_got
<size
, big_endian
>::add_global(gsym
, got_type
);
2098 add_global_plt(Symbol
* gsym
, unsigned int got_type
)
2100 this->reserve_ent();
2101 return Output_data_got
<size
, big_endian
>::add_global_plt(gsym
, got_type
);
2105 add_global_tls(Symbol
* gsym
, unsigned int got_type
)
2106 { return this->add_global_plt(gsym
, got_type
); }
2109 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
2110 Output_data_reloc_generic
* rel_dyn
, unsigned int r_type
)
2112 this->reserve_ent();
2113 Output_data_got
<size
, big_endian
>::
2114 add_global_with_rel(gsym
, got_type
, rel_dyn
, r_type
);
2118 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
2119 Output_data_reloc_generic
* rel_dyn
,
2120 unsigned int r_type_1
, unsigned int r_type_2
)
2122 this->reserve_ent(2);
2123 Output_data_got
<size
, big_endian
>::
2124 add_global_pair_with_rel(gsym
, got_type
, rel_dyn
, r_type_1
, r_type_2
);
2128 add_local(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2130 this->reserve_ent();
2131 return Output_data_got
<size
, big_endian
>::add_local(object
, sym_index
,
2136 add_local_plt(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2138 this->reserve_ent();
2139 return Output_data_got
<size
, big_endian
>::add_local_plt(object
, sym_index
,
2144 add_local_tls(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2145 { return this->add_local_plt(object
, sym_index
, got_type
); }
2148 add_local_tls_pair(Relobj
* object
, unsigned int sym_index
,
2149 unsigned int got_type
,
2150 Output_data_reloc_generic
* rel_dyn
,
2151 unsigned int r_type
)
2153 this->reserve_ent(2);
2154 Output_data_got
<size
, big_endian
>::
2155 add_local_tls_pair(object
, sym_index
, got_type
, rel_dyn
, r_type
);
2159 add_constant(Valtype constant
)
2161 this->reserve_ent();
2162 return Output_data_got
<size
, big_endian
>::add_constant(constant
);
2166 add_constant_pair(Valtype c1
, Valtype c2
)
2168 this->reserve_ent(2);
2169 return Output_data_got
<size
, big_endian
>::add_constant_pair(c1
, c2
);
2172 // Offset of _GLOBAL_OFFSET_TABLE_.
2176 return this->got_offset(this->header_index_
);
2179 // Offset of base used to access the GOT/TOC.
2180 // The got/toc pointer reg will be set to this value.
2182 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
2185 return this->g_o_t();
2187 return (this->output_section()->address()
2188 + object
->toc_base_offset()
2192 // Ensure our GOT has a header.
2194 set_final_data_size()
2196 if (this->header_ent_cnt_
!= 0)
2197 this->make_header();
2198 Output_data_got
<size
, big_endian
>::set_final_data_size();
2201 // First word of GOT header needs some values that are not
2202 // handled by Output_data_got so poke them in here.
2203 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2205 do_write(Output_file
* of
)
2208 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
2209 val
= this->layout_
->dynamic_section()->address();
2211 val
= this->output_section()->address() + 0x8000;
2212 this->replace_constant(this->header_index_
, val
);
2213 Output_data_got
<size
, big_endian
>::do_write(of
);
2218 reserve_ent(unsigned int cnt
= 1)
2220 if (this->header_ent_cnt_
== 0)
2222 if (this->num_entries() + cnt
> this->header_index_
)
2223 this->make_header();
2229 this->header_ent_cnt_
= 0;
2230 this->header_index_
= this->num_entries();
2233 Output_data_got
<size
, big_endian
>::add_constant(0);
2234 Output_data_got
<size
, big_endian
>::add_constant(0);
2235 Output_data_got
<size
, big_endian
>::add_constant(0);
2237 // Define _GLOBAL_OFFSET_TABLE_ at the header
2238 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2241 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
2242 sym
->set_value(this->g_o_t());
2245 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2246 Symbol_table::PREDEFINED
,
2247 this, this->g_o_t(), 0,
2250 elfcpp::STV_HIDDEN
, 0,
2254 Output_data_got
<size
, big_endian
>::add_constant(0);
2257 // Stashed pointers.
2258 Symbol_table
* symtab_
;
2262 unsigned int header_ent_cnt_
;
2263 // GOT header index.
2264 unsigned int header_index_
;
2267 // Get the GOT section, creating it if necessary.
2269 template<int size
, bool big_endian
>
2270 Output_data_got_powerpc
<size
, big_endian
>*
2271 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
2274 if (this->got_
== NULL
)
2276 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2279 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
2281 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2282 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2283 this->got_
, ORDER_DATA
, false);
2289 // Get the dynamic reloc section, creating it if necessary.
2291 template<int size
, bool big_endian
>
2292 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2293 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
2295 if (this->rela_dyn_
== NULL
)
2297 gold_assert(layout
!= NULL
);
2298 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
2299 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
2300 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
2301 ORDER_DYNAMIC_RELOCS
, false);
2303 return this->rela_dyn_
;
2306 // Similarly, but for ifunc symbols get the one for ifunc.
2308 template<int size
, bool big_endian
>
2309 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2310 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Symbol_table
* symtab
,
2315 return this->rela_dyn_section(layout
);
2317 if (this->iplt_
== NULL
)
2318 this->make_iplt_section(symtab
, layout
);
2319 return this->iplt_
->rel_plt();
2325 // Determine the stub group size. The group size is the absolute
2326 // value of the parameter --stub-group-size. If --stub-group-size
2327 // is passed a negative value, we restrict stubs to be always before
2328 // the stubbed branches.
2329 Stub_control(int32_t size
)
2330 : state_(NO_GROUP
), stub_group_size_(abs(size
)),
2331 stub14_group_size_(abs(size
)),
2332 stubs_always_before_branch_(size
< 0), suppress_size_errors_(false),
2333 group_end_addr_(0), owner_(NULL
), output_section_(NULL
)
2335 if (stub_group_size_
== 1)
2338 if (stubs_always_before_branch_
)
2340 stub_group_size_
= 0x1e00000;
2341 stub14_group_size_
= 0x7800;
2345 stub_group_size_
= 0x1c00000;
2346 stub14_group_size_
= 0x7000;
2348 suppress_size_errors_
= true;
2352 // Return true iff input section can be handled by current stub
2355 can_add_to_stub_group(Output_section
* o
,
2356 const Output_section::Input_section
* i
,
2359 const Output_section::Input_section
*
2365 { return output_section_
; }
2371 FINDING_STUB_SECTION
,
2376 uint32_t stub_group_size_
;
2377 uint32_t stub14_group_size_
;
2378 bool stubs_always_before_branch_
;
2379 bool suppress_size_errors_
;
2380 uint64_t group_end_addr_
;
2381 const Output_section::Input_section
* owner_
;
2382 Output_section
* output_section_
;
2385 // Return true iff input section can be handled by current stub
2389 Stub_control::can_add_to_stub_group(Output_section
* o
,
2390 const Output_section::Input_section
* i
,
2394 = has14
? this->stub14_group_size_
: this->stub_group_size_
;
2395 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
2397 uint64_t start_addr
= o
->address();
2400 // .init and .fini sections are pasted together to form a single
2401 // function. We can't be adding stubs in the middle of the function.
2402 this_size
= o
->data_size();
2405 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
2406 this_size
= i
->data_size();
2408 uint64_t end_addr
= start_addr
+ this_size
;
2409 bool toobig
= this_size
> group_size
;
2411 if (toobig
&& !this->suppress_size_errors_
)
2412 gold_warning(_("%s:%s exceeds group size"),
2413 i
->relobj()->name().c_str(),
2414 i
->relobj()->section_name(i
->shndx()).c_str());
2416 if (this->state_
!= HAS_STUB_SECTION
2417 && (!whole_sec
|| this->output_section_
!= o
)
2418 && (this->state_
== NO_GROUP
2419 || this->group_end_addr_
- end_addr
< group_size
))
2422 this->output_section_
= o
;
2425 if (this->state_
== NO_GROUP
)
2427 this->state_
= FINDING_STUB_SECTION
;
2428 this->group_end_addr_
= end_addr
;
2430 else if (this->group_end_addr_
- start_addr
< group_size
)
2432 // Adding this section would make the group larger than GROUP_SIZE.
2433 else if (this->state_
== FINDING_STUB_SECTION
2434 && !this->stubs_always_before_branch_
2437 // But wait, there's more! Input sections up to GROUP_SIZE
2438 // bytes before the stub table can be handled by it too.
2439 this->state_
= HAS_STUB_SECTION
;
2440 this->group_end_addr_
= end_addr
;
2444 this->state_
= NO_GROUP
;
2450 // Look over all the input sections, deciding where to place stubs.
2452 template<int size
, bool big_endian
>
2454 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
2457 Stub_control
stub_control(parameters
->options().stub_group_size());
2459 // Group input sections and insert stub table
2460 Stub_table
<size
, big_endian
>* stub_table
= NULL
;
2461 Layout::Section_list section_list
;
2462 layout
->get_executable_sections(§ion_list
);
2463 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
2464 for (Layout::Section_list::reverse_iterator o
= section_list
.rbegin();
2465 o
!= section_list
.rend();
2468 typedef Output_section::Input_section_list Input_section_list
;
2469 for (Input_section_list::const_reverse_iterator i
2470 = (*o
)->input_sections().rbegin();
2471 i
!= (*o
)->input_sections().rend();
2474 if (i
->is_input_section())
2476 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2477 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2478 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
2479 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
2481 stub_table
->init(stub_control
.owner(),
2482 stub_control
.output_section());
2485 if (stub_table
== NULL
)
2486 stub_table
= this->new_stub_table();
2487 ppcobj
->set_stub_table(i
->shndx(), stub_table
);
2491 if (stub_table
!= NULL
)
2493 const Output_section::Input_section
* i
= stub_control
.owner();
2494 if (!i
->is_input_section())
2496 // Corner case. A new stub group was made for the first
2497 // section (last one looked at here) for some reason, but
2498 // the first section is already being used as the owner for
2499 // a stub table for following sections. Force it into that
2501 gold_assert(this->stub_tables_
.size() >= 2);
2502 this->stub_tables_
.pop_back();
2504 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2505 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2506 ppcobj
->set_stub_table(i
->shndx(), this->stub_tables_
.back());
2509 stub_table
->init(i
, stub_control
.output_section());
2513 // If this branch needs a plt call stub, or a long branch stub, make one.
2515 template<int size
, bool big_endian
>
2517 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
2518 Stub_table
<size
, big_endian
>* stub_table
,
2519 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
2520 Symbol_table
* symtab
) const
2522 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
2523 if (sym
!= NULL
&& sym
->is_forwarder())
2524 sym
= symtab
->resolve_forwards(sym
);
2525 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
2526 Target_powerpc
<size
, big_endian
>* target
=
2527 static_cast<Target_powerpc
<size
, big_endian
>*>(
2528 parameters
->sized_target
<size
, big_endian
>());
2530 ? gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
2531 : this->object_
->local_has_plt_offset(this->r_sym_
))
2533 if (stub_table
== NULL
)
2534 stub_table
= this->object_
->stub_table(this->shndx_
);
2535 if (stub_table
== NULL
)
2537 // This is a ref from a data section to an ifunc symbol.
2538 stub_table
= ifunc_stub_table
;
2540 gold_assert(stub_table
!= NULL
);
2542 stub_table
->add_plt_call_entry(this->object_
, gsym
,
2543 this->r_type_
, this->addend_
);
2545 stub_table
->add_plt_call_entry(this->object_
, this->r_sym_
,
2546 this->r_type_
, this->addend_
);
2550 unsigned long max_branch_offset
;
2551 if (this->r_type_
== elfcpp::R_POWERPC_REL14
2552 || this->r_type_
== elfcpp::R_POWERPC_REL14_BRTAKEN
2553 || this->r_type_
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
2554 max_branch_offset
= 1 << 15;
2555 else if (this->r_type_
== elfcpp::R_POWERPC_REL24
2556 || this->r_type_
== elfcpp::R_PPC_PLTREL24
2557 || this->r_type_
== elfcpp::R_PPC_LOCAL24PC
)
2558 max_branch_offset
= 1 << 25;
2561 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
2562 gold_assert(from
!= invalid_address
);
2563 from
+= (this->object_
->output_section(this->shndx_
)->address()
2568 switch (gsym
->source())
2570 case Symbol::FROM_OBJECT
:
2572 Object
* symobj
= gsym
->object();
2573 if (symobj
->is_dynamic()
2574 || symobj
->pluginobj() != NULL
)
2577 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
2578 if (shndx
== elfcpp::SHN_UNDEF
)
2583 case Symbol::IS_UNDEFINED
:
2589 Symbol_table::Compute_final_value_status status
;
2590 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
2591 if (status
!= Symbol_table::CFVS_OK
)
2593 to
+= this->object_
->ppc64_local_entry_offset(gsym
);
2597 const Symbol_value
<size
>* psymval
2598 = this->object_
->local_symbol(this->r_sym_
);
2599 Symbol_value
<size
> symval
;
2600 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
2601 typename
ObjType::Compute_final_local_value_status status
2602 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
2604 if (status
!= ObjType::CFLV_OK
2605 || !symval
.has_output_value())
2607 to
= symval
.value(this->object_
, 0);
2608 to
+= this->object_
->ppc64_local_entry_offset(this->r_sym_
);
2610 to
+= this->addend_
;
2611 if (stub_table
== NULL
)
2612 stub_table
= this->object_
->stub_table(this->shndx_
);
2613 if (size
== 64 && is_branch_reloc(this->r_type_
))
2615 unsigned int dest_shndx
;
2616 to
= target
->symval_for_branch(symtab
, to
, gsym
,
2617 this->object_
, &dest_shndx
);
2619 Address delta
= to
- from
;
2620 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
2622 if (stub_table
== NULL
)
2624 gold_warning(_("%s:%s: branch in non-executable section,"
2625 " no long branch stub for you"),
2626 this->object_
->name().c_str(),
2627 this->object_
->section_name(this->shndx_
).c_str());
2630 stub_table
->add_long_branch_entry(this->object_
, to
);
2635 // Relaxation hook. This is where we do stub generation.
2637 template<int size
, bool big_endian
>
2639 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
2640 const Input_objects
*,
2641 Symbol_table
* symtab
,
2645 unsigned int prev_brlt_size
= 0;
2649 = this->abiversion() < 2 && parameters
->options().plt_thread_safe();
2651 && this->abiversion() < 2
2653 && !parameters
->options().user_set_plt_thread_safe())
2655 static const char* const thread_starter
[] =
2659 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2661 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2662 "mq_notify", "create_timer",
2666 "GOMP_parallel_start",
2667 "GOMP_parallel_loop_static_start",
2668 "GOMP_parallel_loop_dynamic_start",
2669 "GOMP_parallel_loop_guided_start",
2670 "GOMP_parallel_loop_runtime_start",
2671 "GOMP_parallel_sections_start",
2674 if (parameters
->options().shared())
2678 for (unsigned int i
= 0;
2679 i
< sizeof(thread_starter
) / sizeof(thread_starter
[0]);
2682 Symbol
* sym
= symtab
->lookup(thread_starter
[i
], NULL
);
2683 thread_safe
= (sym
!= NULL
2685 && sym
->in_real_elf());
2691 this->plt_thread_safe_
= thread_safe
;
2692 this->group_sections(layout
, task
);
2695 // We need address of stub tables valid for make_stub.
2696 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2697 p
!= this->stub_tables_
.end();
2700 const Powerpc_relobj
<size
, big_endian
>* object
2701 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
2702 Address off
= object
->get_output_section_offset((*p
)->shndx());
2703 gold_assert(off
!= invalid_address
);
2704 Output_section
* os
= (*p
)->output_section();
2705 (*p
)->set_address_and_size(os
, off
);
2710 // Clear plt call stubs, long branch stubs and branch lookup table.
2711 prev_brlt_size
= this->branch_lookup_table_
.size();
2712 this->branch_lookup_table_
.clear();
2713 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2714 p
!= this->stub_tables_
.end();
2717 (*p
)->clear_stubs();
2721 // Build all the stubs.
2722 Stub_table
<size
, big_endian
>* ifunc_stub_table
2723 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
2724 Stub_table
<size
, big_endian
>* one_stub_table
2725 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
2726 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
2727 b
!= this->branch_info_
.end();
2730 b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
);
2733 // Did anything change size?
2734 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
2735 bool again
= num_huge_branches
!= prev_brlt_size
;
2736 if (size
== 64 && num_huge_branches
!= 0)
2737 this->make_brlt_section(layout
);
2738 if (size
== 64 && again
)
2739 this->brlt_section_
->set_current_size(num_huge_branches
);
2741 typedef Unordered_set
<Output_section
*> Output_sections
;
2742 Output_sections os_need_update
;
2743 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2744 p
!= this->stub_tables_
.end();
2747 if ((*p
)->size_update())
2750 (*p
)->add_eh_frame(layout
);
2751 os_need_update
.insert((*p
)->output_section());
2755 // Set output section offsets for all input sections in an output
2756 // section that just changed size. Anything past the stubs will
2758 for (typename
Output_sections::iterator p
= os_need_update
.begin();
2759 p
!= os_need_update
.end();
2762 Output_section
* os
= *p
;
2764 typedef Output_section::Input_section_list Input_section_list
;
2765 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
2766 i
!= os
->input_sections().end();
2769 off
= align_address(off
, i
->addralign());
2770 if (i
->is_input_section() || i
->is_relaxed_input_section())
2771 i
->relobj()->set_section_offset(i
->shndx(), off
);
2772 if (i
->is_relaxed_input_section())
2774 Stub_table
<size
, big_endian
>* stub_table
2775 = static_cast<Stub_table
<size
, big_endian
>*>(
2776 i
->relaxed_input_section());
2777 off
+= stub_table
->set_address_and_size(os
, off
);
2780 off
+= i
->data_size();
2782 // If .branch_lt is part of this output section, then we have
2783 // just done the offset adjustment.
2784 os
->clear_section_offsets_need_adjustment();
2789 && num_huge_branches
!= 0
2790 && parameters
->options().output_is_position_independent())
2792 // Fill in the BRLT relocs.
2793 this->brlt_section_
->reset_brlt_sizes();
2794 for (typename
Branch_lookup_table::const_iterator p
2795 = this->branch_lookup_table_
.begin();
2796 p
!= this->branch_lookup_table_
.end();
2799 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
2801 this->brlt_section_
->finalize_brlt_sizes();
2806 template<int size
, bool big_endian
>
2808 Target_powerpc
<size
, big_endian
>::do_plt_fde_location(const Output_data
* plt
,
2809 unsigned char* oview
,
2813 uint64_t address
= plt
->address();
2814 off_t len
= plt
->data_size();
2816 if (plt
== this->glink_
)
2818 // See Output_data_glink::do_write() for glink contents.
2821 // There is one word before __glink_PLTresolve
2825 else if (parameters
->options().output_is_position_independent())
2827 // There are two FDEs for a position independent glink.
2828 // The first covers the branch table, the second
2829 // __glink_PLTresolve at the end of glink.
2830 off_t resolve_size
= this->glink_
->pltresolve_size
;
2832 len
-= resolve_size
;
2835 address
+= len
- resolve_size
;
2842 // Must be a stub table.
2843 const Stub_table
<size
, big_endian
>* stub_table
2844 = static_cast<const Stub_table
<size
, big_endian
>*>(plt
);
2845 uint64_t stub_address
= stub_table
->stub_address();
2846 len
-= stub_address
- address
;
2847 address
= stub_address
;
2850 *paddress
= address
;
2854 // A class to handle the PLT data.
2856 template<int size
, bool big_endian
>
2857 class Output_data_plt_powerpc
: public Output_section_data_build
2860 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
2861 size
, big_endian
> Reloc_section
;
2863 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
2864 Reloc_section
* plt_rel
,
2866 : Output_section_data_build(size
== 32 ? 4 : 8),
2872 // Add an entry to the PLT.
2877 add_ifunc_entry(Symbol
*);
2880 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
2882 // Return the .rela.plt section data.
2889 // Return the number of PLT entries.
2893 if (this->current_data_size() == 0)
2895 return ((this->current_data_size() - this->first_plt_entry_offset())
2896 / this->plt_entry_size());
2901 do_adjust_output_section(Output_section
* os
)
2906 // Write to a map file.
2908 do_print_to_mapfile(Mapfile
* mapfile
) const
2909 { mapfile
->print_output_data(this, this->name_
); }
2912 // Return the offset of the first non-reserved PLT entry.
2914 first_plt_entry_offset() const
2916 // IPLT has no reserved entry.
2917 if (this->name_
[3] == 'I')
2919 return this->targ_
->first_plt_entry_offset();
2922 // Return the size of each PLT entry.
2924 plt_entry_size() const
2926 return this->targ_
->plt_entry_size();
2929 // Write out the PLT data.
2931 do_write(Output_file
*);
2933 // The reloc section.
2934 Reloc_section
* rel_
;
2935 // Allows access to .glink for do_write.
2936 Target_powerpc
<size
, big_endian
>* targ_
;
2937 // What to report in map file.
2941 // Add an entry to the PLT.
2943 template<int size
, bool big_endian
>
2945 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
2947 if (!gsym
->has_plt_offset())
2949 section_size_type off
= this->current_data_size();
2951 off
+= this->first_plt_entry_offset();
2952 gsym
->set_plt_offset(off
);
2953 gsym
->set_needs_dynsym_entry();
2954 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
2955 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
2956 off
+= this->plt_entry_size();
2957 this->set_current_data_size(off
);
2961 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
2963 template<int size
, bool big_endian
>
2965 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
2967 if (!gsym
->has_plt_offset())
2969 section_size_type off
= this->current_data_size();
2970 gsym
->set_plt_offset(off
);
2971 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
2972 if (size
== 64 && this->targ_
->abiversion() < 2)
2973 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
2974 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
2975 off
+= this->plt_entry_size();
2976 this->set_current_data_size(off
);
2980 // Add an entry for a local ifunc symbol to the IPLT.
2982 template<int size
, bool big_endian
>
2984 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
2985 Sized_relobj_file
<size
, big_endian
>* relobj
,
2986 unsigned int local_sym_index
)
2988 if (!relobj
->local_has_plt_offset(local_sym_index
))
2990 section_size_type off
= this->current_data_size();
2991 relobj
->set_local_plt_offset(local_sym_index
, off
);
2992 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
2993 if (size
== 64 && this->targ_
->abiversion() < 2)
2994 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
2995 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
2997 off
+= this->plt_entry_size();
2998 this->set_current_data_size(off
);
3002 static const uint32_t add_0_11_11
= 0x7c0b5a14;
3003 static const uint32_t add_2_2_11
= 0x7c425a14;
3004 static const uint32_t add_3_3_2
= 0x7c631214;
3005 static const uint32_t add_3_3_13
= 0x7c636a14;
3006 static const uint32_t add_11_0_11
= 0x7d605a14;
3007 static const uint32_t add_11_2_11
= 0x7d625a14;
3008 static const uint32_t add_11_11_2
= 0x7d6b1214;
3009 static const uint32_t addi_0_12
= 0x380c0000;
3010 static const uint32_t addi_2_2
= 0x38420000;
3011 static const uint32_t addi_3_3
= 0x38630000;
3012 static const uint32_t addi_11_11
= 0x396b0000;
3013 static const uint32_t addi_12_12
= 0x398c0000;
3014 static const uint32_t addis_0_2
= 0x3c020000;
3015 static const uint32_t addis_0_13
= 0x3c0d0000;
3016 static const uint32_t addis_3_2
= 0x3c620000;
3017 static const uint32_t addis_3_13
= 0x3c6d0000;
3018 static const uint32_t addis_11_2
= 0x3d620000;
3019 static const uint32_t addis_11_11
= 0x3d6b0000;
3020 static const uint32_t addis_11_30
= 0x3d7e0000;
3021 static const uint32_t addis_12_12
= 0x3d8c0000;
3022 static const uint32_t b
= 0x48000000;
3023 static const uint32_t bcl_20_31
= 0x429f0005;
3024 static const uint32_t bctr
= 0x4e800420;
3025 static const uint32_t blr
= 0x4e800020;
3026 static const uint32_t bnectr_p4
= 0x4ce20420;
3027 static const uint32_t cmpldi_2_0
= 0x28220000;
3028 static const uint32_t cror_15_15_15
= 0x4def7b82;
3029 static const uint32_t cror_31_31_31
= 0x4ffffb82;
3030 static const uint32_t ld_0_1
= 0xe8010000;
3031 static const uint32_t ld_0_12
= 0xe80c0000;
3032 static const uint32_t ld_2_1
= 0xe8410000;
3033 static const uint32_t ld_2_2
= 0xe8420000;
3034 static const uint32_t ld_2_11
= 0xe84b0000;
3035 static const uint32_t ld_11_2
= 0xe9620000;
3036 static const uint32_t ld_11_11
= 0xe96b0000;
3037 static const uint32_t ld_12_2
= 0xe9820000;
3038 static const uint32_t ld_12_11
= 0xe98b0000;
3039 static const uint32_t lfd_0_1
= 0xc8010000;
3040 static const uint32_t li_0_0
= 0x38000000;
3041 static const uint32_t li_12_0
= 0x39800000;
3042 static const uint32_t lis_0_0
= 0x3c000000;
3043 static const uint32_t lis_11
= 0x3d600000;
3044 static const uint32_t lis_12
= 0x3d800000;
3045 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
3046 static const uint32_t lwz_0_12
= 0x800c0000;
3047 static const uint32_t lwz_11_11
= 0x816b0000;
3048 static const uint32_t lwz_11_30
= 0x817e0000;
3049 static const uint32_t lwz_12_12
= 0x818c0000;
3050 static const uint32_t lwzu_0_12
= 0x840c0000;
3051 static const uint32_t mflr_0
= 0x7c0802a6;
3052 static const uint32_t mflr_11
= 0x7d6802a6;
3053 static const uint32_t mflr_12
= 0x7d8802a6;
3054 static const uint32_t mtctr_0
= 0x7c0903a6;
3055 static const uint32_t mtctr_11
= 0x7d6903a6;
3056 static const uint32_t mtctr_12
= 0x7d8903a6;
3057 static const uint32_t mtlr_0
= 0x7c0803a6;
3058 static const uint32_t mtlr_12
= 0x7d8803a6;
3059 static const uint32_t nop
= 0x60000000;
3060 static const uint32_t ori_0_0_0
= 0x60000000;
3061 static const uint32_t srdi_0_0_2
= 0x7800f082;
3062 static const uint32_t std_0_1
= 0xf8010000;
3063 static const uint32_t std_0_12
= 0xf80c0000;
3064 static const uint32_t std_2_1
= 0xf8410000;
3065 static const uint32_t stfd_0_1
= 0xd8010000;
3066 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
3067 static const uint32_t sub_11_11_12
= 0x7d6c5850;
3068 static const uint32_t sub_12_12_11
= 0x7d8b6050;
3069 static const uint32_t xor_2_12_12
= 0x7d826278;
3070 static const uint32_t xor_11_12_12
= 0x7d8b6278;
3072 // Write out the PLT.
3074 template<int size
, bool big_endian
>
3076 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3078 if (size
== 32 && this->name_
[3] != 'I')
3080 const section_size_type offset
= this->offset();
3081 const section_size_type oview_size
3082 = convert_to_section_size_type(this->data_size());
3083 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3084 unsigned char* pov
= oview
;
3085 unsigned char* endpov
= oview
+ oview_size
;
3087 // The address of the .glink branch table
3088 const Output_data_glink
<size
, big_endian
>* glink
3089 = this->targ_
->glink_section();
3090 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
3092 while (pov
< endpov
)
3094 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
3099 of
->write_output_view(offset
, oview_size
, oview
);
3103 // Create the PLT section.
3105 template<int size
, bool big_endian
>
3107 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
3110 if (this->plt_
== NULL
)
3112 if (this->got_
== NULL
)
3113 this->got_section(symtab
, layout
);
3115 if (this->glink_
== NULL
)
3116 make_glink_section(layout
);
3118 // Ensure that .rela.dyn always appears before .rela.plt This is
3119 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3120 // needs to include .rela.plt in its range.
3121 this->rela_dyn_section(layout
);
3123 Reloc_section
* plt_rel
= new Reloc_section(false);
3124 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
3125 elfcpp::SHF_ALLOC
, plt_rel
,
3126 ORDER_DYNAMIC_PLT_RELOCS
, false);
3128 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
3130 layout
->add_output_section_data(".plt",
3132 ? elfcpp::SHT_PROGBITS
3133 : elfcpp::SHT_NOBITS
),
3134 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3143 // Create the IPLT section.
3145 template<int size
, bool big_endian
>
3147 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
3150 if (this->iplt_
== NULL
)
3152 this->make_plt_section(symtab
, layout
);
3154 Reloc_section
* iplt_rel
= new Reloc_section(false);
3155 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
3157 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
3159 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
3163 // A section for huge long branch addresses, similar to plt section.
3165 template<int size
, bool big_endian
>
3166 class Output_data_brlt_powerpc
: public Output_section_data_build
3169 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3170 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3171 size
, big_endian
> Reloc_section
;
3173 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3174 Reloc_section
* brlt_rel
)
3175 : Output_section_data_build(size
== 32 ? 4 : 8),
3183 this->reset_data_size();
3184 this->rel_
->reset_data_size();
3188 finalize_brlt_sizes()
3190 this->finalize_data_size();
3191 this->rel_
->finalize_data_size();
3194 // Add a reloc for an entry in the BRLT.
3196 add_reloc(Address to
, unsigned int off
)
3197 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
3199 // Update section and reloc section size.
3201 set_current_size(unsigned int num_branches
)
3203 this->reset_address_and_file_offset();
3204 this->set_current_data_size(num_branches
* 16);
3205 this->finalize_data_size();
3206 Output_section
* os
= this->output_section();
3207 os
->set_section_offsets_need_adjustment();
3208 if (this->rel_
!= NULL
)
3210 unsigned int reloc_size
3211 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
3212 this->rel_
->reset_address_and_file_offset();
3213 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
3214 this->rel_
->finalize_data_size();
3215 Output_section
* os
= this->rel_
->output_section();
3216 os
->set_section_offsets_need_adjustment();
3222 do_adjust_output_section(Output_section
* os
)
3227 // Write to a map file.
3229 do_print_to_mapfile(Mapfile
* mapfile
) const
3230 { mapfile
->print_output_data(this, "** BRLT"); }
3233 // Write out the BRLT data.
3235 do_write(Output_file
*);
3237 // The reloc section.
3238 Reloc_section
* rel_
;
3239 Target_powerpc
<size
, big_endian
>* targ_
;
3242 // Make the branch lookup table section.
3244 template<int size
, bool big_endian
>
3246 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
3248 if (size
== 64 && this->brlt_section_
== NULL
)
3250 Reloc_section
* brlt_rel
= NULL
;
3251 bool is_pic
= parameters
->options().output_is_position_independent();
3254 // When PIC we can't fill in .branch_lt (like .plt it can be
3255 // a bss style section) but must initialise at runtime via
3256 // dynamic relocats.
3257 this->rela_dyn_section(layout
);
3258 brlt_rel
= new Reloc_section(false);
3259 this->rela_dyn_
->output_section()->add_output_section_data(brlt_rel
);
3262 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
3263 if (this->plt_
&& is_pic
)
3264 this->plt_
->output_section()
3265 ->add_output_section_data(this->brlt_section_
);
3267 layout
->add_output_section_data(".branch_lt",
3268 (is_pic
? elfcpp::SHT_NOBITS
3269 : elfcpp::SHT_PROGBITS
),
3270 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3271 this->brlt_section_
,
3272 (is_pic
? ORDER_SMALL_BSS
3273 : ORDER_SMALL_DATA
),
3278 // Write out .branch_lt when non-PIC.
3280 template<int size
, bool big_endian
>
3282 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3284 if (size
== 64 && !parameters
->options().output_is_position_independent())
3286 const section_size_type offset
= this->offset();
3287 const section_size_type oview_size
3288 = convert_to_section_size_type(this->data_size());
3289 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3291 this->targ_
->write_branch_lookup_table(oview
);
3292 of
->write_output_view(offset
, oview_size
, oview
);
3296 static inline uint32_t
3302 static inline uint32_t
3308 static inline uint32_t
3311 return hi(a
+ 0x8000);
3317 static const unsigned char eh_frame_cie
[12];
3321 const unsigned char Eh_cie
<size
>::eh_frame_cie
[] =
3324 'z', 'R', 0, // Augmentation string.
3325 4, // Code alignment.
3326 0x80 - size
/ 8 , // Data alignment.
3328 1, // Augmentation size.
3329 (elfcpp::DW_EH_PE_pcrel
3330 | elfcpp::DW_EH_PE_sdata4
), // FDE encoding.
3331 elfcpp::DW_CFA_def_cfa
, 1, 0 // def_cfa: r1 offset 0.
3334 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3335 static const unsigned char glink_eh_frame_fde_64v1
[] =
3337 0, 0, 0, 0, // Replaced with offset to .glink.
3338 0, 0, 0, 0, // Replaced with size of .glink.
3339 0, // Augmentation size.
3340 elfcpp::DW_CFA_advance_loc
+ 1,
3341 elfcpp::DW_CFA_register
, 65, 12,
3342 elfcpp::DW_CFA_advance_loc
+ 4,
3343 elfcpp::DW_CFA_restore_extended
, 65
3346 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
3347 static const unsigned char glink_eh_frame_fde_64v2
[] =
3349 0, 0, 0, 0, // Replaced with offset to .glink.
3350 0, 0, 0, 0, // Replaced with size of .glink.
3351 0, // Augmentation size.
3352 elfcpp::DW_CFA_advance_loc
+ 1,
3353 elfcpp::DW_CFA_register
, 65, 0,
3354 elfcpp::DW_CFA_advance_loc
+ 4,
3355 elfcpp::DW_CFA_restore_extended
, 65
3358 // Describe __glink_PLTresolve use of LR, 32-bit version.
3359 static const unsigned char glink_eh_frame_fde_32
[] =
3361 0, 0, 0, 0, // Replaced with offset to .glink.
3362 0, 0, 0, 0, // Replaced with size of .glink.
3363 0, // Augmentation size.
3364 elfcpp::DW_CFA_advance_loc
+ 2,
3365 elfcpp::DW_CFA_register
, 65, 0,
3366 elfcpp::DW_CFA_advance_loc
+ 4,
3367 elfcpp::DW_CFA_restore_extended
, 65
3370 static const unsigned char default_fde
[] =
3372 0, 0, 0, 0, // Replaced with offset to stubs.
3373 0, 0, 0, 0, // Replaced with size of stubs.
3374 0, // Augmentation size.
3375 elfcpp::DW_CFA_nop
, // Pad.
3380 template<bool big_endian
>
3382 write_insn(unsigned char* p
, uint32_t v
)
3384 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
3387 // Stub_table holds information about plt and long branch stubs.
3388 // Stubs are built in an area following some input section determined
3389 // by group_sections(). This input section is converted to a relaxed
3390 // input section allowing it to be resized to accommodate the stubs
3392 template<int size
, bool big_endian
>
3393 class Stub_table
: public Output_relaxed_input_section
3396 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3397 static const Address invalid_address
= static_cast<Address
>(0) - 1;
3399 Stub_table(Target_powerpc
<size
, big_endian
>* targ
)
3400 : Output_relaxed_input_section(NULL
, 0, 0),
3401 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
3402 orig_data_size_(0), plt_size_(0), last_plt_size_(0),
3403 branch_size_(0), last_branch_size_(0), eh_frame_added_(false)
3406 // Delayed Output_relaxed_input_section init.
3408 init(const Output_section::Input_section
*, Output_section
*);
3410 // Add a plt call stub.
3412 add_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3418 add_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3423 // Find a given plt call stub.
3425 find_plt_call_entry(const Symbol
*) const;
3428 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3429 unsigned int) const;
3432 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3438 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3443 // Add a long branch stub.
3445 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*, Address
);
3448 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
3454 this->plt_call_stubs_
.clear();
3455 this->plt_size_
= 0;
3456 this->long_branch_stubs_
.clear();
3457 this->branch_size_
= 0;
3461 set_address_and_size(const Output_section
* os
, Address off
)
3463 Address start_off
= off
;
3464 off
+= this->orig_data_size_
;
3465 Address my_size
= this->plt_size_
+ this->branch_size_
;
3467 off
= align_address(off
, this->stub_align());
3468 // Include original section size and alignment padding in size
3469 my_size
+= off
- start_off
;
3470 this->reset_address_and_file_offset();
3471 this->set_current_data_size(my_size
);
3472 this->set_address_and_file_offset(os
->address() + start_off
,
3473 os
->offset() + start_off
);
3478 stub_address() const
3480 return align_address(this->address() + this->orig_data_size_
,
3481 this->stub_align());
3487 return align_address(this->offset() + this->orig_data_size_
,
3488 this->stub_align());
3493 { return this->plt_size_
; }
3498 Output_section
* os
= this->output_section();
3499 if (os
->addralign() < this->stub_align())
3501 os
->set_addralign(this->stub_align());
3502 // FIXME: get rid of the insane checkpointing.
3503 // We can't increase alignment of the input section to which
3504 // stubs are attached; The input section may be .init which
3505 // is pasted together with other .init sections to form a
3506 // function. Aligning might insert zero padding resulting in
3507 // sigill. However we do need to increase alignment of the
3508 // output section so that the align_address() on offset in
3509 // set_address_and_size() adds the same padding as the
3510 // align_address() on address in stub_address().
3511 // What's more, we need this alignment for the layout done in
3512 // relaxation_loop_body() so that the output section starts at
3513 // a suitably aligned address.
3514 os
->checkpoint_set_addralign(this->stub_align());
3516 if (this->last_plt_size_
!= this->plt_size_
3517 || this->last_branch_size_
!= this->branch_size_
)
3519 this->last_plt_size_
= this->plt_size_
;
3520 this->last_branch_size_
= this->branch_size_
;
3526 // Add .eh_frame info for this stub section. Unlike other linker
3527 // generated .eh_frame this is added late in the link, because we
3528 // only want the .eh_frame info if this particular stub section is
3531 add_eh_frame(Layout
* layout
)
3533 if (!this->eh_frame_added_
)
3535 if (!parameters
->options().ld_generated_unwind_info())
3538 // Since we add stub .eh_frame info late, it must be placed
3539 // after all other linker generated .eh_frame info so that
3540 // merge mapping need not be updated for input sections.
3541 // There is no provision to use a different CIE to that used
3543 if (!this->targ_
->has_glink())
3546 layout
->add_eh_frame_for_plt(this,
3547 Eh_cie
<size
>::eh_frame_cie
,
3548 sizeof (Eh_cie
<size
>::eh_frame_cie
),
3550 sizeof (default_fde
));
3551 this->eh_frame_added_
= true;
3555 Target_powerpc
<size
, big_endian
>*
3561 class Plt_stub_ent_hash
;
3562 typedef Unordered_map
<Plt_stub_ent
, unsigned int,
3563 Plt_stub_ent_hash
> Plt_stub_entries
;
3565 // Alignment of stub section.
3571 unsigned int min_align
= 32;
3572 unsigned int user_align
= 1 << parameters
->options().plt_align();
3573 return std::max(user_align
, min_align
);
3576 // Return the plt offset for the given call stub.
3578 plt_off(typename
Plt_stub_entries::const_iterator p
, bool* is_iplt
) const
3580 const Symbol
* gsym
= p
->first
.sym_
;
3583 *is_iplt
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
3584 && gsym
->can_use_relative_reloc(false));
3585 return gsym
->plt_offset();
3590 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
3591 unsigned int local_sym_index
= p
->first
.locsym_
;
3592 return relobj
->local_plt_offset(local_sym_index
);
3596 // Size of a given plt call stub.
3598 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
3604 Address plt_addr
= this->plt_off(p
, &is_iplt
);
3606 plt_addr
+= this->targ_
->iplt_section()->address();
3608 plt_addr
+= this->targ_
->plt_section()->address();
3609 Address got_addr
= this->targ_
->got_section()->output_section()->address();
3610 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3611 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
3612 got_addr
+= ppcobj
->toc_base_offset();
3613 Address off
= plt_addr
- got_addr
;
3614 unsigned int bytes
= 4 * 4 + 4 * (ha(off
) != 0);
3615 if (this->targ_
->abiversion() < 2)
3617 bool static_chain
= parameters
->options().plt_static_chain();
3618 bool thread_safe
= this->targ_
->plt_thread_safe();
3622 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
3624 unsigned int align
= 1 << parameters
->options().plt_align();
3626 bytes
= (bytes
+ align
- 1) & -align
;
3630 // Return long branch stub size.
3632 branch_stub_size(Address to
)
3635 = this->stub_address() + this->last_plt_size_
+ this->branch_size_
;
3636 if (to
- loc
+ (1 << 25) < 2 << 25)
3638 if (size
== 64 || !parameters
->options().output_is_position_independent())
3645 do_write(Output_file
*);
3647 // Plt call stub keys.
3651 Plt_stub_ent(const Symbol
* sym
)
3652 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3655 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3656 unsigned int locsym_index
)
3657 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3660 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3662 unsigned int r_type
,
3664 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3667 this->addend_
= addend
;
3668 else if (parameters
->options().output_is_position_independent()
3669 && r_type
== elfcpp::R_PPC_PLTREL24
)
3671 this->addend_
= addend
;
3672 if (this->addend_
>= 32768)
3673 this->object_
= object
;
3677 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3678 unsigned int locsym_index
,
3679 unsigned int r_type
,
3681 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3684 this->addend_
= addend
;
3685 else if (parameters
->options().output_is_position_independent()
3686 && r_type
== elfcpp::R_PPC_PLTREL24
)
3687 this->addend_
= addend
;
3690 bool operator==(const Plt_stub_ent
& that
) const
3692 return (this->sym_
== that
.sym_
3693 && this->object_
== that
.object_
3694 && this->addend_
== that
.addend_
3695 && this->locsym_
== that
.locsym_
);
3699 const Sized_relobj_file
<size
, big_endian
>* object_
;
3700 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
3701 unsigned int locsym_
;
3704 class Plt_stub_ent_hash
3707 size_t operator()(const Plt_stub_ent
& ent
) const
3709 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
3710 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
3716 // Long branch stub keys.
3717 class Branch_stub_ent
3720 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
, Address to
)
3721 : dest_(to
), toc_base_off_(0)
3724 toc_base_off_
= obj
->toc_base_offset();
3727 bool operator==(const Branch_stub_ent
& that
) const
3729 return (this->dest_
== that
.dest_
3731 || this->toc_base_off_
== that
.toc_base_off_
));
3735 unsigned int toc_base_off_
;
3738 class Branch_stub_ent_hash
3741 size_t operator()(const Branch_stub_ent
& ent
) const
3742 { return ent
.dest_
^ ent
.toc_base_off_
; }
3745 // In a sane world this would be a global.
3746 Target_powerpc
<size
, big_endian
>* targ_
;
3747 // Map sym/object/addend to stub offset.
3748 Plt_stub_entries plt_call_stubs_
;
3749 // Map destination address to stub offset.
3750 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
3751 Branch_stub_ent_hash
> Branch_stub_entries
;
3752 Branch_stub_entries long_branch_stubs_
;
3753 // size of input section
3754 section_size_type orig_data_size_
;
3756 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
3757 // Whether .eh_frame info has been created for this stub section.
3758 bool eh_frame_added_
;
3761 // Make a new stub table, and record.
3763 template<int size
, bool big_endian
>
3764 Stub_table
<size
, big_endian
>*
3765 Target_powerpc
<size
, big_endian
>::new_stub_table()
3767 Stub_table
<size
, big_endian
>* stub_table
3768 = new Stub_table
<size
, big_endian
>(this);
3769 this->stub_tables_
.push_back(stub_table
);
3773 // Delayed stub table initialisation, because we create the stub table
3774 // before we know to which section it will be attached.
3776 template<int size
, bool big_endian
>
3778 Stub_table
<size
, big_endian
>::init(
3779 const Output_section::Input_section
* owner
,
3780 Output_section
* output_section
)
3782 this->set_relobj(owner
->relobj());
3783 this->set_shndx(owner
->shndx());
3784 this->set_addralign(this->relobj()->section_addralign(this->shndx()));
3785 this->set_output_section(output_section
);
3786 this->orig_data_size_
= owner
->current_data_size();
3788 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
3789 new_relaxed
.push_back(this);
3790 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
3793 // Add a plt call stub, if we do not already have one for this
3794 // sym/object/addend combo.
3796 template<int size
, bool big_endian
>
3798 Stub_table
<size
, big_endian
>::add_plt_call_entry(
3799 const Sized_relobj_file
<size
, big_endian
>* object
,
3801 unsigned int r_type
,
3804 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
3805 Address off
= this->plt_size_
;
3806 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
3807 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
3809 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
3812 template<int size
, bool big_endian
>
3814 Stub_table
<size
, big_endian
>::add_plt_call_entry(
3815 const Sized_relobj_file
<size
, big_endian
>* object
,
3816 unsigned int locsym_index
,
3817 unsigned int r_type
,
3820 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
3821 Address off
= this->plt_size_
;
3822 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
3823 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
3825 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
3828 // Find a plt call stub.
3830 template<int size
, bool big_endian
>
3831 typename Stub_table
<size
, big_endian
>::Address
3832 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3833 const Sized_relobj_file
<size
, big_endian
>* object
,
3835 unsigned int r_type
,
3836 Address addend
) const
3838 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
3839 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3840 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3843 template<int size
, bool big_endian
>
3844 typename Stub_table
<size
, big_endian
>::Address
3845 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
3847 Plt_stub_ent
ent(gsym
);
3848 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3849 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3852 template<int size
, bool big_endian
>
3853 typename Stub_table
<size
, big_endian
>::Address
3854 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3855 const Sized_relobj_file
<size
, big_endian
>* object
,
3856 unsigned int locsym_index
,
3857 unsigned int r_type
,
3858 Address addend
) const
3860 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
3861 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3862 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3865 template<int size
, bool big_endian
>
3866 typename Stub_table
<size
, big_endian
>::Address
3867 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3868 const Sized_relobj_file
<size
, big_endian
>* object
,
3869 unsigned int locsym_index
) const
3871 Plt_stub_ent
ent(object
, locsym_index
);
3872 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3873 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3876 // Add a long branch stub if we don't already have one to given
3879 template<int size
, bool big_endian
>
3881 Stub_table
<size
, big_endian
>::add_long_branch_entry(
3882 const Powerpc_relobj
<size
, big_endian
>* object
,
3885 Branch_stub_ent
ent(object
, to
);
3886 Address off
= this->branch_size_
;
3887 if (this->long_branch_stubs_
.insert(std::make_pair(ent
, off
)).second
)
3889 unsigned int stub_size
= this->branch_stub_size(to
);
3890 this->branch_size_
= off
+ stub_size
;
3891 if (size
== 64 && stub_size
!= 4)
3892 this->targ_
->add_branch_lookup_table(to
);
3896 // Find long branch stub.
3898 template<int size
, bool big_endian
>
3899 typename Stub_table
<size
, big_endian
>::Address
3900 Stub_table
<size
, big_endian
>::find_long_branch_entry(
3901 const Powerpc_relobj
<size
, big_endian
>* object
,
3904 Branch_stub_ent
ent(object
, to
);
3905 typename
Branch_stub_entries::const_iterator p
3906 = this->long_branch_stubs_
.find(ent
);
3907 return p
== this->long_branch_stubs_
.end() ? invalid_address
: p
->second
;
3910 // A class to handle .glink.
3912 template<int size
, bool big_endian
>
3913 class Output_data_glink
: public Output_section_data
3916 static const int pltresolve_size
= 16*4;
3918 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
3919 : Output_section_data(16), targ_(targ
)
3923 add_eh_frame(Layout
* layout
)
3925 if (!parameters
->options().ld_generated_unwind_info())
3930 if (this->targ_
->abiversion() < 2)
3931 layout
->add_eh_frame_for_plt(this,
3932 Eh_cie
<64>::eh_frame_cie
,
3933 sizeof (Eh_cie
<64>::eh_frame_cie
),
3934 glink_eh_frame_fde_64v1
,
3935 sizeof (glink_eh_frame_fde_64v1
));
3937 layout
->add_eh_frame_for_plt(this,
3938 Eh_cie
<64>::eh_frame_cie
,
3939 sizeof (Eh_cie
<64>::eh_frame_cie
),
3940 glink_eh_frame_fde_64v2
,
3941 sizeof (glink_eh_frame_fde_64v2
));
3945 // 32-bit .glink can use the default since the CIE return
3946 // address reg, LR, is valid.
3947 layout
->add_eh_frame_for_plt(this,
3948 Eh_cie
<32>::eh_frame_cie
,
3949 sizeof (Eh_cie
<32>::eh_frame_cie
),
3951 sizeof (default_fde
));
3952 // Except where LR is used in a PIC __glink_PLTresolve.
3953 if (parameters
->options().output_is_position_independent())
3954 layout
->add_eh_frame_for_plt(this,
3955 Eh_cie
<32>::eh_frame_cie
,
3956 sizeof (Eh_cie
<32>::eh_frame_cie
),
3957 glink_eh_frame_fde_32
,
3958 sizeof (glink_eh_frame_fde_32
));
3963 // Write to a map file.
3965 do_print_to_mapfile(Mapfile
* mapfile
) const
3966 { mapfile
->print_output_data(this, _("** glink")); }
3970 set_final_data_size();
3974 do_write(Output_file
*);
3976 // Allows access to .got and .plt for do_write.
3977 Target_powerpc
<size
, big_endian
>* targ_
;
3980 template<int size
, bool big_endian
>
3982 Output_data_glink
<size
, big_endian
>::set_final_data_size()
3984 unsigned int count
= this->targ_
->plt_entry_count();
3985 section_size_type total
= 0;
3991 // space for branch table
3992 total
+= 4 * (count
- 1);
3994 total
+= -total
& 15;
3995 total
+= this->pltresolve_size
;
3999 total
+= this->pltresolve_size
;
4001 // space for branch table
4003 if (this->targ_
->abiversion() < 2)
4007 total
+= 4 * (count
- 0x8000);
4012 this->set_data_size(total
);
4015 // Write out plt and long branch stub code.
4017 template<int size
, bool big_endian
>
4019 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
4021 if (this->plt_call_stubs_
.empty()
4022 && this->long_branch_stubs_
.empty())
4025 const section_size_type start_off
= this->offset();
4026 const section_size_type off
= this->stub_offset();
4027 const section_size_type oview_size
=
4028 convert_to_section_size_type(this->data_size() - (off
- start_off
));
4029 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4034 const Output_data_got_powerpc
<size
, big_endian
>* got
4035 = this->targ_
->got_section();
4036 Address got_os_addr
= got
->output_section()->address();
4038 if (!this->plt_call_stubs_
.empty())
4040 // The base address of the .plt section.
4041 Address plt_base
= this->targ_
->plt_section()->address();
4042 Address iplt_base
= invalid_address
;
4044 // Write out plt call stubs.
4045 typename
Plt_stub_entries::const_iterator cs
;
4046 for (cs
= this->plt_call_stubs_
.begin();
4047 cs
!= this->plt_call_stubs_
.end();
4051 Address pltoff
= this->plt_off(cs
, &is_iplt
);
4052 Address plt_addr
= pltoff
;
4055 if (iplt_base
== invalid_address
)
4056 iplt_base
= this->targ_
->iplt_section()->address();
4057 plt_addr
+= iplt_base
;
4060 plt_addr
+= plt_base
;
4061 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4062 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
4063 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
4064 Address off
= plt_addr
- got_addr
;
4066 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
4067 gold_error(_("%s: linkage table error against `%s'"),
4068 cs
->first
.object_
->name().c_str(),
4069 cs
->first
.sym_
->demangled_name().c_str());
4071 bool plt_load_toc
= this->targ_
->abiversion() < 2;
4073 = plt_load_toc
&& parameters
->options().plt_static_chain();
4075 = plt_load_toc
&& this->targ_
->plt_thread_safe();
4076 bool use_fake_dep
= false;
4077 Address cmp_branch_off
= 0;
4080 unsigned int pltindex
4081 = ((pltoff
- this->targ_
->first_plt_entry_offset())
4082 / this->targ_
->plt_entry_size());
4084 = (this->targ_
->glink_section()->pltresolve_size
4086 if (pltindex
> 32768)
4087 glinkoff
+= (pltindex
- 32768) * 4;
4089 = this->targ_
->glink_section()->address() + glinkoff
;
4091 = (this->stub_address() + cs
->second
+ 24
4092 + 4 * (ha(off
) != 0)
4093 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4094 + 4 * static_chain
);
4095 cmp_branch_off
= to
- from
;
4096 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
4099 p
= oview
+ cs
->second
;
4102 write_insn
<big_endian
>(p
, std_2_1
+ this->targ_
->stk_toc());
4104 write_insn
<big_endian
>(p
, addis_11_2
+ ha(off
));
4106 write_insn
<big_endian
>(p
, ld_12_11
+ l(off
));
4109 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4111 write_insn
<big_endian
>(p
, addi_11_11
+ l(off
));
4115 write_insn
<big_endian
>(p
, mtctr_12
);
4121 write_insn
<big_endian
>(p
, xor_2_12_12
);
4123 write_insn
<big_endian
>(p
, add_11_11_2
);
4126 write_insn
<big_endian
>(p
, ld_2_11
+ l(off
+ 8));
4130 write_insn
<big_endian
>(p
, ld_11_11
+ l(off
+ 16));
4137 write_insn
<big_endian
>(p
, std_2_1
+ this->targ_
->stk_toc());
4139 write_insn
<big_endian
>(p
, ld_12_2
+ l(off
));
4142 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4144 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
));
4148 write_insn
<big_endian
>(p
, mtctr_12
);
4154 write_insn
<big_endian
>(p
, xor_11_12_12
);
4156 write_insn
<big_endian
>(p
, add_2_2_11
);
4161 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16));
4164 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8));
4168 if (thread_safe
&& !use_fake_dep
)
4170 write_insn
<big_endian
>(p
, cmpldi_2_0
);
4172 write_insn
<big_endian
>(p
, bnectr_p4
);
4174 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
4177 write_insn
<big_endian
>(p
, bctr
);
4181 // Write out long branch stubs.
4182 typename
Branch_stub_entries::const_iterator bs
;
4183 for (bs
= this->long_branch_stubs_
.begin();
4184 bs
!= this->long_branch_stubs_
.end();
4187 p
= oview
+ this->plt_size_
+ bs
->second
;
4188 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
4189 Address delta
= bs
->first
.dest_
- loc
;
4190 if (delta
+ (1 << 25) < 2 << 25)
4191 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
4195 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
4196 gold_assert(brlt_addr
!= invalid_address
);
4197 brlt_addr
+= this->targ_
->brlt_section()->address();
4198 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
4199 Address brltoff
= brlt_addr
- got_addr
;
4200 if (ha(brltoff
) == 0)
4202 write_insn
<big_endian
>(p
, ld_12_2
+ l(brltoff
)), p
+= 4;
4206 write_insn
<big_endian
>(p
, addis_11_2
+ ha(brltoff
)), p
+= 4;
4207 write_insn
<big_endian
>(p
, ld_12_11
+ l(brltoff
)), p
+= 4;
4209 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
4210 write_insn
<big_endian
>(p
, bctr
);
4216 if (!this->plt_call_stubs_
.empty())
4218 // The base address of the .plt section.
4219 Address plt_base
= this->targ_
->plt_section()->address();
4220 Address iplt_base
= invalid_address
;
4221 // The address of _GLOBAL_OFFSET_TABLE_.
4222 Address g_o_t
= invalid_address
;
4224 // Write out plt call stubs.
4225 typename
Plt_stub_entries::const_iterator cs
;
4226 for (cs
= this->plt_call_stubs_
.begin();
4227 cs
!= this->plt_call_stubs_
.end();
4231 Address plt_addr
= this->plt_off(cs
, &is_iplt
);
4234 if (iplt_base
== invalid_address
)
4235 iplt_base
= this->targ_
->iplt_section()->address();
4236 plt_addr
+= iplt_base
;
4239 plt_addr
+= plt_base
;
4241 p
= oview
+ cs
->second
;
4242 if (parameters
->options().output_is_position_independent())
4245 const Powerpc_relobj
<size
, big_endian
>* ppcobj
4246 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
4247 (cs
->first
.object_
));
4248 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
4250 unsigned int got2
= ppcobj
->got2_shndx();
4251 got_addr
= ppcobj
->get_output_section_offset(got2
);
4252 gold_assert(got_addr
!= invalid_address
);
4253 got_addr
+= (ppcobj
->output_section(got2
)->address()
4254 + cs
->first
.addend_
);
4258 if (g_o_t
== invalid_address
)
4260 const Output_data_got_powerpc
<size
, big_endian
>* got
4261 = this->targ_
->got_section();
4262 g_o_t
= got
->address() + got
->g_o_t();
4267 Address off
= plt_addr
- got_addr
;
4270 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(off
));
4271 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
4272 write_insn
<big_endian
>(p
+ 8, bctr
);
4276 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(off
));
4277 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(off
));
4278 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
4279 write_insn
<big_endian
>(p
+ 12, bctr
);
4284 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
4285 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
4286 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
4287 write_insn
<big_endian
>(p
+ 12, bctr
);
4292 // Write out long branch stubs.
4293 typename
Branch_stub_entries::const_iterator bs
;
4294 for (bs
= this->long_branch_stubs_
.begin();
4295 bs
!= this->long_branch_stubs_
.end();
4298 p
= oview
+ this->plt_size_
+ bs
->second
;
4299 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
4300 Address delta
= bs
->first
.dest_
- loc
;
4301 if (delta
+ (1 << 25) < 2 << 25)
4302 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
4303 else if (!parameters
->options().output_is_position_independent())
4305 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(bs
->first
.dest_
));
4306 write_insn
<big_endian
>(p
+ 4, addi_12_12
+ l(bs
->first
.dest_
));
4307 write_insn
<big_endian
>(p
+ 8, mtctr_12
);
4308 write_insn
<big_endian
>(p
+ 12, bctr
);
4313 write_insn
<big_endian
>(p
+ 0, mflr_0
);
4314 write_insn
<big_endian
>(p
+ 4, bcl_20_31
);
4315 write_insn
<big_endian
>(p
+ 8, mflr_12
);
4316 write_insn
<big_endian
>(p
+ 12, addis_12_12
+ ha(delta
));
4317 write_insn
<big_endian
>(p
+ 16, addi_12_12
+ l(delta
));
4318 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
4319 write_insn
<big_endian
>(p
+ 24, mtctr_12
);
4320 write_insn
<big_endian
>(p
+ 28, bctr
);
4326 // Write out .glink.
4328 template<int size
, bool big_endian
>
4330 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
4332 const section_size_type off
= this->offset();
4333 const section_size_type oview_size
=
4334 convert_to_section_size_type(this->data_size());
4335 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4338 // The base address of the .plt section.
4339 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4340 Address plt_base
= this->targ_
->plt_section()->address();
4344 // Write pltresolve stub.
4346 Address after_bcl
= this->address() + 16;
4347 Address pltoff
= plt_base
- after_bcl
;
4349 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
4351 if (this->targ_
->abiversion() < 2)
4353 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
4354 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
4355 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
4356 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
4357 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
4358 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
4359 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
4360 write_insn
<big_endian
>(p
, ld_2_11
+ 8), p
+= 4;
4361 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
4362 write_insn
<big_endian
>(p
, ld_11_11
+ 16), p
+= 4;
4366 write_insn
<big_endian
>(p
, mflr_0
), p
+= 4;
4367 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
4368 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
4369 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
4370 write_insn
<big_endian
>(p
, mtlr_0
), p
+= 4;
4371 write_insn
<big_endian
>(p
, sub_12_12_11
), p
+= 4;
4372 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
4373 write_insn
<big_endian
>(p
, addi_0_12
+ l(-48)), p
+= 4;
4374 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
4375 write_insn
<big_endian
>(p
, srdi_0_0_2
), p
+= 4;
4376 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
4377 write_insn
<big_endian
>(p
, ld_11_11
+ 8), p
+= 4;
4379 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
4380 while (p
< oview
+ this->pltresolve_size
)
4381 write_insn
<big_endian
>(p
, nop
), p
+= 4;
4383 // Write lazy link call stubs.
4385 while (p
< oview
+ oview_size
)
4387 if (this->targ_
->abiversion() < 2)
4391 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
4395 write_insn
<big_endian
>(p
, lis_0_0
+ hi(indx
)), p
+= 4;
4396 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
4399 uint32_t branch_off
= 8 - (p
- oview
);
4400 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
4406 const Output_data_got_powerpc
<size
, big_endian
>* got
4407 = this->targ_
->got_section();
4408 // The address of _GLOBAL_OFFSET_TABLE_.
4409 Address g_o_t
= got
->address() + got
->g_o_t();
4411 // Write out pltresolve branch table.
4413 unsigned int the_end
= oview_size
- this->pltresolve_size
;
4414 unsigned char* end_p
= oview
+ the_end
;
4415 while (p
< end_p
- 8 * 4)
4416 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
4418 write_insn
<big_endian
>(p
, nop
), p
+= 4;
4420 // Write out pltresolve call stub.
4421 if (parameters
->options().output_is_position_independent())
4423 Address res0_off
= 0;
4424 Address after_bcl_off
= the_end
+ 12;
4425 Address bcl_res0
= after_bcl_off
- res0_off
;
4427 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
4428 write_insn
<big_endian
>(p
+ 4, mflr_0
);
4429 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
4430 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
4431 write_insn
<big_endian
>(p
+ 16, mflr_12
);
4432 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
4433 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
4435 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
4437 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
4438 if (ha(got_bcl
) == ha(got_bcl
+ 4))
4440 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
4441 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
4445 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
4446 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
4448 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
4449 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
4450 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
4451 write_insn
<big_endian
>(p
+ 52, bctr
);
4452 write_insn
<big_endian
>(p
+ 56, nop
);
4453 write_insn
<big_endian
>(p
+ 60, nop
);
4457 Address res0
= this->address();
4459 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
4460 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
4461 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
4462 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
4464 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
4465 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
4466 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
4467 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
4468 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
4469 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
4471 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
4472 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
4473 write_insn
<big_endian
>(p
+ 32, bctr
);
4474 write_insn
<big_endian
>(p
+ 36, nop
);
4475 write_insn
<big_endian
>(p
+ 40, nop
);
4476 write_insn
<big_endian
>(p
+ 44, nop
);
4477 write_insn
<big_endian
>(p
+ 48, nop
);
4478 write_insn
<big_endian
>(p
+ 52, nop
);
4479 write_insn
<big_endian
>(p
+ 56, nop
);
4480 write_insn
<big_endian
>(p
+ 60, nop
);
4485 of
->write_output_view(off
, oview_size
, oview
);
4489 // A class to handle linker generated save/restore functions.
4491 template<int size
, bool big_endian
>
4492 class Output_data_save_res
: public Output_section_data_build
4495 Output_data_save_res(Symbol_table
* symtab
);
4498 // Write to a map file.
4500 do_print_to_mapfile(Mapfile
* mapfile
) const
4501 { mapfile
->print_output_data(this, _("** save/restore")); }
4504 do_write(Output_file
*);
4507 // The maximum size of save/restore contents.
4508 static const unsigned int savres_max
= 218*4;
4511 savres_define(Symbol_table
* symtab
,
4513 unsigned int lo
, unsigned int hi
,
4514 unsigned char* write_ent(unsigned char*, int),
4515 unsigned char* write_tail(unsigned char*, int));
4517 unsigned char *contents_
;
4520 template<bool big_endian
>
4521 static unsigned char*
4522 savegpr0(unsigned char* p
, int r
)
4524 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4525 write_insn
<big_endian
>(p
, insn
);
4529 template<bool big_endian
>
4530 static unsigned char*
4531 savegpr0_tail(unsigned char* p
, int r
)
4533 p
= savegpr0
<big_endian
>(p
, r
);
4534 uint32_t insn
= std_0_1
+ 16;
4535 write_insn
<big_endian
>(p
, insn
);
4537 write_insn
<big_endian
>(p
, blr
);
4541 template<bool big_endian
>
4542 static unsigned char*
4543 restgpr0(unsigned char* p
, int r
)
4545 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4546 write_insn
<big_endian
>(p
, insn
);
4550 template<bool big_endian
>
4551 static unsigned char*
4552 restgpr0_tail(unsigned char* p
, int r
)
4554 uint32_t insn
= ld_0_1
+ 16;
4555 write_insn
<big_endian
>(p
, insn
);
4557 p
= restgpr0
<big_endian
>(p
, r
);
4558 write_insn
<big_endian
>(p
, mtlr_0
);
4562 p
= restgpr0
<big_endian
>(p
, 30);
4563 p
= restgpr0
<big_endian
>(p
, 31);
4565 write_insn
<big_endian
>(p
, blr
);
4569 template<bool big_endian
>
4570 static unsigned char*
4571 savegpr1(unsigned char* p
, int r
)
4573 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4574 write_insn
<big_endian
>(p
, insn
);
4578 template<bool big_endian
>
4579 static unsigned char*
4580 savegpr1_tail(unsigned char* p
, int r
)
4582 p
= savegpr1
<big_endian
>(p
, r
);
4583 write_insn
<big_endian
>(p
, blr
);
4587 template<bool big_endian
>
4588 static unsigned char*
4589 restgpr1(unsigned char* p
, int r
)
4591 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4592 write_insn
<big_endian
>(p
, insn
);
4596 template<bool big_endian
>
4597 static unsigned char*
4598 restgpr1_tail(unsigned char* p
, int r
)
4600 p
= restgpr1
<big_endian
>(p
, r
);
4601 write_insn
<big_endian
>(p
, blr
);
4605 template<bool big_endian
>
4606 static unsigned char*
4607 savefpr(unsigned char* p
, int r
)
4609 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4610 write_insn
<big_endian
>(p
, insn
);
4614 template<bool big_endian
>
4615 static unsigned char*
4616 savefpr0_tail(unsigned char* p
, int r
)
4618 p
= savefpr
<big_endian
>(p
, r
);
4619 write_insn
<big_endian
>(p
, std_0_1
+ 16);
4621 write_insn
<big_endian
>(p
, blr
);
4625 template<bool big_endian
>
4626 static unsigned char*
4627 restfpr(unsigned char* p
, int r
)
4629 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4630 write_insn
<big_endian
>(p
, insn
);
4634 template<bool big_endian
>
4635 static unsigned char*
4636 restfpr0_tail(unsigned char* p
, int r
)
4638 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
4640 p
= restfpr
<big_endian
>(p
, r
);
4641 write_insn
<big_endian
>(p
, mtlr_0
);
4645 p
= restfpr
<big_endian
>(p
, 30);
4646 p
= restfpr
<big_endian
>(p
, 31);
4648 write_insn
<big_endian
>(p
, blr
);
4652 template<bool big_endian
>
4653 static unsigned char*
4654 savefpr1_tail(unsigned char* p
, int r
)
4656 p
= savefpr
<big_endian
>(p
, r
);
4657 write_insn
<big_endian
>(p
, blr
);
4661 template<bool big_endian
>
4662 static unsigned char*
4663 restfpr1_tail(unsigned char* p
, int r
)
4665 p
= restfpr
<big_endian
>(p
, r
);
4666 write_insn
<big_endian
>(p
, blr
);
4670 template<bool big_endian
>
4671 static unsigned char*
4672 savevr(unsigned char* p
, int r
)
4674 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
4675 write_insn
<big_endian
>(p
, insn
);
4677 insn
= stvx_0_12_0
+ (r
<< 21);
4678 write_insn
<big_endian
>(p
, insn
);
4682 template<bool big_endian
>
4683 static unsigned char*
4684 savevr_tail(unsigned char* p
, int r
)
4686 p
= savevr
<big_endian
>(p
, r
);
4687 write_insn
<big_endian
>(p
, blr
);
4691 template<bool big_endian
>
4692 static unsigned char*
4693 restvr(unsigned char* p
, int r
)
4695 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
4696 write_insn
<big_endian
>(p
, insn
);
4698 insn
= lvx_0_12_0
+ (r
<< 21);
4699 write_insn
<big_endian
>(p
, insn
);
4703 template<bool big_endian
>
4704 static unsigned char*
4705 restvr_tail(unsigned char* p
, int r
)
4707 p
= restvr
<big_endian
>(p
, r
);
4708 write_insn
<big_endian
>(p
, blr
);
4713 template<int size
, bool big_endian
>
4714 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
4715 Symbol_table
* symtab
)
4716 : Output_section_data_build(4),
4719 this->savres_define(symtab
,
4720 "_savegpr0_", 14, 31,
4721 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
4722 this->savres_define(symtab
,
4723 "_restgpr0_", 14, 29,
4724 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
4725 this->savres_define(symtab
,
4726 "_restgpr0_", 30, 31,
4727 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
4728 this->savres_define(symtab
,
4729 "_savegpr1_", 14, 31,
4730 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
4731 this->savres_define(symtab
,
4732 "_restgpr1_", 14, 31,
4733 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
4734 this->savres_define(symtab
,
4735 "_savefpr_", 14, 31,
4736 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
4737 this->savres_define(symtab
,
4738 "_restfpr_", 14, 29,
4739 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
4740 this->savres_define(symtab
,
4741 "_restfpr_", 30, 31,
4742 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
4743 this->savres_define(symtab
,
4745 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
4746 this->savres_define(symtab
,
4748 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
4749 this->savres_define(symtab
,
4751 savevr
<big_endian
>, savevr_tail
<big_endian
>);
4752 this->savres_define(symtab
,
4754 restvr
<big_endian
>, restvr_tail
<big_endian
>);
4757 template<int size
, bool big_endian
>
4759 Output_data_save_res
<size
, big_endian
>::savres_define(
4760 Symbol_table
* symtab
,
4762 unsigned int lo
, unsigned int hi
,
4763 unsigned char* write_ent(unsigned char*, int),
4764 unsigned char* write_tail(unsigned char*, int))
4766 size_t len
= strlen(name
);
4767 bool writing
= false;
4770 memcpy(sym
, name
, len
);
4773 for (unsigned int i
= lo
; i
<= hi
; i
++)
4775 sym
[len
+ 0] = i
/ 10 + '0';
4776 sym
[len
+ 1] = i
% 10 + '0';
4777 Symbol
* gsym
= symtab
->lookup(sym
);
4778 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
4779 writing
= writing
|| refd
;
4782 if (this->contents_
== NULL
)
4783 this->contents_
= new unsigned char[this->savres_max
];
4785 section_size_type value
= this->current_data_size();
4786 unsigned char* p
= this->contents_
+ value
;
4788 p
= write_ent(p
, i
);
4790 p
= write_tail(p
, i
);
4791 section_size_type cur_size
= p
- this->contents_
;
4792 this->set_current_data_size(cur_size
);
4794 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
4795 this, value
, cur_size
- value
,
4796 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
4797 elfcpp::STV_HIDDEN
, 0, false, false);
4802 // Write out save/restore.
4804 template<int size
, bool big_endian
>
4806 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
4808 const section_size_type off
= this->offset();
4809 const section_size_type oview_size
=
4810 convert_to_section_size_type(this->data_size());
4811 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4812 memcpy(oview
, this->contents_
, oview_size
);
4813 of
->write_output_view(off
, oview_size
, oview
);
4817 // Create the glink section.
4819 template<int size
, bool big_endian
>
4821 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
4823 if (this->glink_
== NULL
)
4825 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
4826 this->glink_
->add_eh_frame(layout
);
4827 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
4828 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
4829 this->glink_
, ORDER_TEXT
, false);
4833 // Create a PLT entry for a global symbol.
4835 template<int size
, bool big_endian
>
4837 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
4841 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4842 && gsym
->can_use_relative_reloc(false))
4844 if (this->iplt_
== NULL
)
4845 this->make_iplt_section(symtab
, layout
);
4846 this->iplt_
->add_ifunc_entry(gsym
);
4850 if (this->plt_
== NULL
)
4851 this->make_plt_section(symtab
, layout
);
4852 this->plt_
->add_entry(gsym
);
4856 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
4858 template<int size
, bool big_endian
>
4860 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
4861 Symbol_table
* symtab
,
4863 Sized_relobj_file
<size
, big_endian
>* relobj
,
4866 if (this->iplt_
== NULL
)
4867 this->make_iplt_section(symtab
, layout
);
4868 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
4871 // Return the number of entries in the PLT.
4873 template<int size
, bool big_endian
>
4875 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
4877 if (this->plt_
== NULL
)
4879 return this->plt_
->entry_count();
4882 // Create a GOT entry for local dynamic __tls_get_addr calls.
4884 template<int size
, bool big_endian
>
4886 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
4887 Symbol_table
* symtab
,
4889 Sized_relobj_file
<size
, big_endian
>* object
)
4891 if (this->tlsld_got_offset_
== -1U)
4893 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
4894 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
4895 Output_data_got_powerpc
<size
, big_endian
>* got
4896 = this->got_section(symtab
, layout
);
4897 unsigned int got_offset
= got
->add_constant_pair(0, 0);
4898 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
4900 this->tlsld_got_offset_
= got_offset
;
4902 return this->tlsld_got_offset_
;
4905 // Get the Reference_flags for a particular relocation.
4907 template<int size
, bool big_endian
>
4909 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(
4910 unsigned int r_type
,
4911 const Target_powerpc
* target
)
4917 case elfcpp::R_POWERPC_NONE
:
4918 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
4919 case elfcpp::R_POWERPC_GNU_VTENTRY
:
4920 case elfcpp::R_PPC64_TOC
:
4921 // No symbol reference.
4924 case elfcpp::R_PPC64_ADDR64
:
4925 case elfcpp::R_PPC64_UADDR64
:
4926 case elfcpp::R_POWERPC_ADDR32
:
4927 case elfcpp::R_POWERPC_UADDR32
:
4928 case elfcpp::R_POWERPC_ADDR16
:
4929 case elfcpp::R_POWERPC_UADDR16
:
4930 case elfcpp::R_POWERPC_ADDR16_LO
:
4931 case elfcpp::R_POWERPC_ADDR16_HI
:
4932 case elfcpp::R_POWERPC_ADDR16_HA
:
4933 ref
= Symbol::ABSOLUTE_REF
;
4936 case elfcpp::R_POWERPC_ADDR24
:
4937 case elfcpp::R_POWERPC_ADDR14
:
4938 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4939 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4940 ref
= Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
4943 case elfcpp::R_PPC64_REL64
:
4944 case elfcpp::R_POWERPC_REL32
:
4945 case elfcpp::R_PPC_LOCAL24PC
:
4946 case elfcpp::R_POWERPC_REL16
:
4947 case elfcpp::R_POWERPC_REL16_LO
:
4948 case elfcpp::R_POWERPC_REL16_HI
:
4949 case elfcpp::R_POWERPC_REL16_HA
:
4950 ref
= Symbol::RELATIVE_REF
;
4953 case elfcpp::R_POWERPC_REL24
:
4954 case elfcpp::R_PPC_PLTREL24
:
4955 case elfcpp::R_POWERPC_REL14
:
4956 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4957 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4958 ref
= Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
4961 case elfcpp::R_POWERPC_GOT16
:
4962 case elfcpp::R_POWERPC_GOT16_LO
:
4963 case elfcpp::R_POWERPC_GOT16_HI
:
4964 case elfcpp::R_POWERPC_GOT16_HA
:
4965 case elfcpp::R_PPC64_GOT16_DS
:
4966 case elfcpp::R_PPC64_GOT16_LO_DS
:
4967 case elfcpp::R_PPC64_TOC16
:
4968 case elfcpp::R_PPC64_TOC16_LO
:
4969 case elfcpp::R_PPC64_TOC16_HI
:
4970 case elfcpp::R_PPC64_TOC16_HA
:
4971 case elfcpp::R_PPC64_TOC16_DS
:
4972 case elfcpp::R_PPC64_TOC16_LO_DS
:
4974 ref
= Symbol::ABSOLUTE_REF
;
4977 case elfcpp::R_POWERPC_GOT_TPREL16
:
4978 case elfcpp::R_POWERPC_TLS
:
4979 ref
= Symbol::TLS_REF
;
4982 case elfcpp::R_POWERPC_COPY
:
4983 case elfcpp::R_POWERPC_GLOB_DAT
:
4984 case elfcpp::R_POWERPC_JMP_SLOT
:
4985 case elfcpp::R_POWERPC_RELATIVE
:
4986 case elfcpp::R_POWERPC_DTPMOD
:
4988 // Not expected. We will give an error later.
4992 if (size
== 64 && target
->abiversion() < 2)
4993 ref
|= Symbol::FUNC_DESC_ABI
;
4997 // Report an unsupported relocation against a local symbol.
4999 template<int size
, bool big_endian
>
5001 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
5002 Sized_relobj_file
<size
, big_endian
>* object
,
5003 unsigned int r_type
)
5005 gold_error(_("%s: unsupported reloc %u against local symbol"),
5006 object
->name().c_str(), r_type
);
5009 // We are about to emit a dynamic relocation of type R_TYPE. If the
5010 // dynamic linker does not support it, issue an error.
5012 template<int size
, bool big_endian
>
5014 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
5015 unsigned int r_type
)
5017 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
5019 // These are the relocation types supported by glibc for both 32-bit
5020 // and 64-bit powerpc.
5023 case elfcpp::R_POWERPC_NONE
:
5024 case elfcpp::R_POWERPC_RELATIVE
:
5025 case elfcpp::R_POWERPC_GLOB_DAT
:
5026 case elfcpp::R_POWERPC_DTPMOD
:
5027 case elfcpp::R_POWERPC_DTPREL
:
5028 case elfcpp::R_POWERPC_TPREL
:
5029 case elfcpp::R_POWERPC_JMP_SLOT
:
5030 case elfcpp::R_POWERPC_COPY
:
5031 case elfcpp::R_POWERPC_IRELATIVE
:
5032 case elfcpp::R_POWERPC_ADDR32
:
5033 case elfcpp::R_POWERPC_UADDR32
:
5034 case elfcpp::R_POWERPC_ADDR24
:
5035 case elfcpp::R_POWERPC_ADDR16
:
5036 case elfcpp::R_POWERPC_UADDR16
:
5037 case elfcpp::R_POWERPC_ADDR16_LO
:
5038 case elfcpp::R_POWERPC_ADDR16_HI
:
5039 case elfcpp::R_POWERPC_ADDR16_HA
:
5040 case elfcpp::R_POWERPC_ADDR14
:
5041 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5042 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5043 case elfcpp::R_POWERPC_REL32
:
5044 case elfcpp::R_POWERPC_REL24
:
5045 case elfcpp::R_POWERPC_TPREL16
:
5046 case elfcpp::R_POWERPC_TPREL16_LO
:
5047 case elfcpp::R_POWERPC_TPREL16_HI
:
5048 case elfcpp::R_POWERPC_TPREL16_HA
:
5059 // These are the relocation types supported only on 64-bit.
5060 case elfcpp::R_PPC64_ADDR64
:
5061 case elfcpp::R_PPC64_UADDR64
:
5062 case elfcpp::R_PPC64_JMP_IREL
:
5063 case elfcpp::R_PPC64_ADDR16_DS
:
5064 case elfcpp::R_PPC64_ADDR16_LO_DS
:
5065 case elfcpp::R_PPC64_ADDR16_HIGH
:
5066 case elfcpp::R_PPC64_ADDR16_HIGHA
:
5067 case elfcpp::R_PPC64_ADDR16_HIGHER
:
5068 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
5069 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
5070 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
5071 case elfcpp::R_PPC64_REL64
:
5072 case elfcpp::R_POWERPC_ADDR30
:
5073 case elfcpp::R_PPC64_TPREL16_DS
:
5074 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5075 case elfcpp::R_PPC64_TPREL16_HIGH
:
5076 case elfcpp::R_PPC64_TPREL16_HIGHA
:
5077 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5078 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5079 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5080 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5091 // These are the relocation types supported only on 32-bit.
5092 // ??? glibc ld.so doesn't need to support these.
5093 case elfcpp::R_POWERPC_DTPREL16
:
5094 case elfcpp::R_POWERPC_DTPREL16_LO
:
5095 case elfcpp::R_POWERPC_DTPREL16_HI
:
5096 case elfcpp::R_POWERPC_DTPREL16_HA
:
5104 // This prevents us from issuing more than one error per reloc
5105 // section. But we can still wind up issuing more than one
5106 // error per object file.
5107 if (this->issued_non_pic_error_
)
5109 gold_assert(parameters
->options().output_is_position_independent());
5110 object
->error(_("requires unsupported dynamic reloc; "
5111 "recompile with -fPIC"));
5112 this->issued_non_pic_error_
= true;
5116 // Return whether we need to make a PLT entry for a relocation of the
5117 // given type against a STT_GNU_IFUNC symbol.
5119 template<int size
, bool big_endian
>
5121 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
5122 Sized_relobj_file
<size
, big_endian
>* object
,
5123 unsigned int r_type
,
5126 // In non-pic code any reference will resolve to the plt call stub
5127 // for the ifunc symbol.
5128 if (size
== 32 && !parameters
->options().output_is_position_independent())
5133 // Word size refs from data sections are OK, but don't need a PLT entry.
5134 case elfcpp::R_POWERPC_ADDR32
:
5135 case elfcpp::R_POWERPC_UADDR32
:
5140 case elfcpp::R_PPC64_ADDR64
:
5141 case elfcpp::R_PPC64_UADDR64
:
5146 // GOT refs are good, but also don't need a PLT entry.
5147 case elfcpp::R_POWERPC_GOT16
:
5148 case elfcpp::R_POWERPC_GOT16_LO
:
5149 case elfcpp::R_POWERPC_GOT16_HI
:
5150 case elfcpp::R_POWERPC_GOT16_HA
:
5151 case elfcpp::R_PPC64_GOT16_DS
:
5152 case elfcpp::R_PPC64_GOT16_LO_DS
:
5155 // Function calls are good, and these do need a PLT entry.
5156 case elfcpp::R_POWERPC_ADDR24
:
5157 case elfcpp::R_POWERPC_ADDR14
:
5158 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5159 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5160 case elfcpp::R_POWERPC_REL24
:
5161 case elfcpp::R_PPC_PLTREL24
:
5162 case elfcpp::R_POWERPC_REL14
:
5163 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5164 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5171 // Anything else is a problem.
5172 // If we are building a static executable, the libc startup function
5173 // responsible for applying indirect function relocations is going
5174 // to complain about the reloc type.
5175 // If we are building a dynamic executable, we will have a text
5176 // relocation. The dynamic loader will set the text segment
5177 // writable and non-executable to apply text relocations. So we'll
5178 // segfault when trying to run the indirection function to resolve
5181 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
5182 object
->name().c_str(), r_type
);
5186 // Scan a relocation for a local symbol.
5188 template<int size
, bool big_endian
>
5190 Target_powerpc
<size
, big_endian
>::Scan::local(
5191 Symbol_table
* symtab
,
5193 Target_powerpc
<size
, big_endian
>* target
,
5194 Sized_relobj_file
<size
, big_endian
>* object
,
5195 unsigned int data_shndx
,
5196 Output_section
* output_section
,
5197 const elfcpp::Rela
<size
, big_endian
>& reloc
,
5198 unsigned int r_type
,
5199 const elfcpp::Sym
<size
, big_endian
>& lsym
,
5202 this->maybe_skip_tls_get_addr_call(r_type
, NULL
);
5204 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
5205 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
5207 this->expect_tls_get_addr_call();
5208 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
5209 if (tls_type
!= tls::TLSOPT_NONE
)
5210 this->skip_next_tls_get_addr_call();
5212 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
5213 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
5215 this->expect_tls_get_addr_call();
5216 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5217 if (tls_type
!= tls::TLSOPT_NONE
)
5218 this->skip_next_tls_get_addr_call();
5221 Powerpc_relobj
<size
, big_endian
>* ppc_object
5222 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5227 && data_shndx
== ppc_object
->opd_shndx()
5228 && r_type
== elfcpp::R_PPC64_ADDR64
)
5229 ppc_object
->set_opd_discard(reloc
.get_r_offset());
5233 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5234 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
5235 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
, true))
5237 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5238 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5239 r_type
, r_sym
, reloc
.get_r_addend());
5240 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
5245 case elfcpp::R_POWERPC_NONE
:
5246 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5247 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5248 case elfcpp::R_PPC64_TOCSAVE
:
5249 case elfcpp::R_POWERPC_TLS
:
5252 case elfcpp::R_PPC64_TOC
:
5254 Output_data_got_powerpc
<size
, big_endian
>* got
5255 = target
->got_section(symtab
, layout
);
5256 if (parameters
->options().output_is_position_independent())
5258 Address off
= reloc
.get_r_offset();
5260 && data_shndx
== ppc_object
->opd_shndx()
5261 && ppc_object
->get_opd_discard(off
- 8))
5264 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5265 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
5266 rela_dyn
->add_output_section_relative(got
->output_section(),
5267 elfcpp::R_POWERPC_RELATIVE
,
5269 object
, data_shndx
, off
,
5270 symobj
->toc_base_offset());
5275 case elfcpp::R_PPC64_ADDR64
:
5276 case elfcpp::R_PPC64_UADDR64
:
5277 case elfcpp::R_POWERPC_ADDR32
:
5278 case elfcpp::R_POWERPC_UADDR32
:
5279 case elfcpp::R_POWERPC_ADDR24
:
5280 case elfcpp::R_POWERPC_ADDR16
:
5281 case elfcpp::R_POWERPC_ADDR16_LO
:
5282 case elfcpp::R_POWERPC_ADDR16_HI
:
5283 case elfcpp::R_POWERPC_ADDR16_HA
:
5284 case elfcpp::R_POWERPC_UADDR16
:
5285 case elfcpp::R_PPC64_ADDR16_HIGH
:
5286 case elfcpp::R_PPC64_ADDR16_HIGHA
:
5287 case elfcpp::R_PPC64_ADDR16_HIGHER
:
5288 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
5289 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
5290 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
5291 case elfcpp::R_PPC64_ADDR16_DS
:
5292 case elfcpp::R_PPC64_ADDR16_LO_DS
:
5293 case elfcpp::R_POWERPC_ADDR14
:
5294 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5295 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5296 // If building a shared library (or a position-independent
5297 // executable), we need to create a dynamic relocation for
5299 if (parameters
->options().output_is_position_independent()
5300 || (size
== 64 && is_ifunc
))
5302 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
5304 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
5305 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
5307 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5308 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
5309 : elfcpp::R_POWERPC_RELATIVE
);
5310 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
5311 output_section
, data_shndx
,
5312 reloc
.get_r_offset(),
5313 reloc
.get_r_addend(), false);
5317 check_non_pic(object
, r_type
);
5318 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5319 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
5320 data_shndx
, reloc
.get_r_offset(),
5321 reloc
.get_r_addend());
5326 case elfcpp::R_POWERPC_REL24
:
5327 case elfcpp::R_PPC_PLTREL24
:
5328 case elfcpp::R_PPC_LOCAL24PC
:
5329 case elfcpp::R_POWERPC_REL14
:
5330 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5331 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5333 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5334 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5335 reloc
.get_r_addend());
5338 case elfcpp::R_PPC64_REL64
:
5339 case elfcpp::R_POWERPC_REL32
:
5340 case elfcpp::R_POWERPC_REL16
:
5341 case elfcpp::R_POWERPC_REL16_LO
:
5342 case elfcpp::R_POWERPC_REL16_HI
:
5343 case elfcpp::R_POWERPC_REL16_HA
:
5344 case elfcpp::R_POWERPC_SECTOFF
:
5345 case elfcpp::R_POWERPC_SECTOFF_LO
:
5346 case elfcpp::R_POWERPC_SECTOFF_HI
:
5347 case elfcpp::R_POWERPC_SECTOFF_HA
:
5348 case elfcpp::R_PPC64_SECTOFF_DS
:
5349 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
5350 case elfcpp::R_POWERPC_TPREL16
:
5351 case elfcpp::R_POWERPC_TPREL16_LO
:
5352 case elfcpp::R_POWERPC_TPREL16_HI
:
5353 case elfcpp::R_POWERPC_TPREL16_HA
:
5354 case elfcpp::R_PPC64_TPREL16_DS
:
5355 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5356 case elfcpp::R_PPC64_TPREL16_HIGH
:
5357 case elfcpp::R_PPC64_TPREL16_HIGHA
:
5358 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5359 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5360 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5361 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5362 case elfcpp::R_POWERPC_DTPREL16
:
5363 case elfcpp::R_POWERPC_DTPREL16_LO
:
5364 case elfcpp::R_POWERPC_DTPREL16_HI
:
5365 case elfcpp::R_POWERPC_DTPREL16_HA
:
5366 case elfcpp::R_PPC64_DTPREL16_DS
:
5367 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
5368 case elfcpp::R_PPC64_DTPREL16_HIGH
:
5369 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
5370 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
5371 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
5372 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
5373 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
5374 case elfcpp::R_PPC64_TLSGD
:
5375 case elfcpp::R_PPC64_TLSLD
:
5378 case elfcpp::R_POWERPC_GOT16
:
5379 case elfcpp::R_POWERPC_GOT16_LO
:
5380 case elfcpp::R_POWERPC_GOT16_HI
:
5381 case elfcpp::R_POWERPC_GOT16_HA
:
5382 case elfcpp::R_PPC64_GOT16_DS
:
5383 case elfcpp::R_PPC64_GOT16_LO_DS
:
5385 // The symbol requires a GOT entry.
5386 Output_data_got_powerpc
<size
, big_endian
>* got
5387 = target
->got_section(symtab
, layout
);
5388 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5390 if (!parameters
->options().output_is_position_independent())
5392 if (size
== 32 && is_ifunc
)
5393 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
5395 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
5397 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
5399 // If we are generating a shared object or a pie, this
5400 // symbol's GOT entry will be set by a dynamic relocation.
5402 off
= got
->add_constant(0);
5403 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
5405 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
5407 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
5408 : elfcpp::R_POWERPC_RELATIVE
);
5409 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
5410 got
, off
, 0, false);
5415 case elfcpp::R_PPC64_TOC16
:
5416 case elfcpp::R_PPC64_TOC16_LO
:
5417 case elfcpp::R_PPC64_TOC16_HI
:
5418 case elfcpp::R_PPC64_TOC16_HA
:
5419 case elfcpp::R_PPC64_TOC16_DS
:
5420 case elfcpp::R_PPC64_TOC16_LO_DS
:
5421 // We need a GOT section.
5422 target
->got_section(symtab
, layout
);
5425 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5426 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
5427 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
5428 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
5430 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
5431 if (tls_type
== tls::TLSOPT_NONE
)
5433 Output_data_got_powerpc
<size
, big_endian
>* got
5434 = target
->got_section(symtab
, layout
);
5435 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5436 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5437 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
5438 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
5440 else if (tls_type
== tls::TLSOPT_TO_LE
)
5442 // no GOT relocs needed for Local Exec.
5449 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5450 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
5451 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
5452 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
5454 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5455 if (tls_type
== tls::TLSOPT_NONE
)
5456 target
->tlsld_got_offset(symtab
, layout
, object
);
5457 else if (tls_type
== tls::TLSOPT_TO_LE
)
5459 // no GOT relocs needed for Local Exec.
5460 if (parameters
->options().emit_relocs())
5462 Output_section
* os
= layout
->tls_segment()->first_section();
5463 gold_assert(os
!= NULL
);
5464 os
->set_needs_symtab_index();
5472 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5473 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
5474 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
5475 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
5477 Output_data_got_powerpc
<size
, big_endian
>* got
5478 = target
->got_section(symtab
, layout
);
5479 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5480 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
5484 case elfcpp::R_POWERPC_GOT_TPREL16
:
5485 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
5486 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
5487 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
5489 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
5490 if (tls_type
== tls::TLSOPT_NONE
)
5492 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5493 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
5495 Output_data_got_powerpc
<size
, big_endian
>* got
5496 = target
->got_section(symtab
, layout
);
5497 unsigned int off
= got
->add_constant(0);
5498 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
5500 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5501 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
5502 elfcpp::R_POWERPC_TPREL
,
5506 else if (tls_type
== tls::TLSOPT_TO_LE
)
5508 // no GOT relocs needed for Local Exec.
5516 unsupported_reloc_local(object
, r_type
);
5522 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5523 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5524 case elfcpp::R_POWERPC_GOT_TPREL16
:
5525 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5526 case elfcpp::R_POWERPC_GOT16
:
5527 case elfcpp::R_PPC64_GOT16_DS
:
5528 case elfcpp::R_PPC64_TOC16
:
5529 case elfcpp::R_PPC64_TOC16_DS
:
5530 ppc_object
->set_has_small_toc_reloc();
5536 // Report an unsupported relocation against a global symbol.
5538 template<int size
, bool big_endian
>
5540 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
5541 Sized_relobj_file
<size
, big_endian
>* object
,
5542 unsigned int r_type
,
5545 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5546 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
5549 // Scan a relocation for a global symbol.
5551 template<int size
, bool big_endian
>
5553 Target_powerpc
<size
, big_endian
>::Scan::global(
5554 Symbol_table
* symtab
,
5556 Target_powerpc
<size
, big_endian
>* target
,
5557 Sized_relobj_file
<size
, big_endian
>* object
,
5558 unsigned int data_shndx
,
5559 Output_section
* output_section
,
5560 const elfcpp::Rela
<size
, big_endian
>& reloc
,
5561 unsigned int r_type
,
5564 if (this->maybe_skip_tls_get_addr_call(r_type
, gsym
) == Track_tls::SKIP
)
5567 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
5568 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
5570 this->expect_tls_get_addr_call();
5571 const bool final
= gsym
->final_value_is_known();
5572 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
5573 if (tls_type
!= tls::TLSOPT_NONE
)
5574 this->skip_next_tls_get_addr_call();
5576 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
5577 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
5579 this->expect_tls_get_addr_call();
5580 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5581 if (tls_type
!= tls::TLSOPT_NONE
)
5582 this->skip_next_tls_get_addr_call();
5585 Powerpc_relobj
<size
, big_endian
>* ppc_object
5586 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5588 // A STT_GNU_IFUNC symbol may require a PLT entry.
5589 bool is_ifunc
= gsym
->type() == elfcpp::STT_GNU_IFUNC
;
5590 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
, true))
5592 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5593 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5594 reloc
.get_r_addend());
5595 target
->make_plt_entry(symtab
, layout
, gsym
);
5600 case elfcpp::R_POWERPC_NONE
:
5601 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5602 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5603 case elfcpp::R_PPC_LOCAL24PC
:
5604 case elfcpp::R_POWERPC_TLS
:
5607 case elfcpp::R_PPC64_TOC
:
5609 Output_data_got_powerpc
<size
, big_endian
>* got
5610 = target
->got_section(symtab
, layout
);
5611 if (parameters
->options().output_is_position_independent())
5613 Address off
= reloc
.get_r_offset();
5615 && data_shndx
== ppc_object
->opd_shndx()
5616 && ppc_object
->get_opd_discard(off
- 8))
5619 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5620 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
5621 if (data_shndx
!= ppc_object
->opd_shndx())
5622 symobj
= static_cast
5623 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
5624 rela_dyn
->add_output_section_relative(got
->output_section(),
5625 elfcpp::R_POWERPC_RELATIVE
,
5627 object
, data_shndx
, off
,
5628 symobj
->toc_base_offset());
5633 case elfcpp::R_PPC64_ADDR64
:
5635 && data_shndx
== ppc_object
->opd_shndx()
5636 && (gsym
->is_defined_in_discarded_section()
5637 || gsym
->object() != object
))
5639 ppc_object
->set_opd_discard(reloc
.get_r_offset());
5643 case elfcpp::R_PPC64_UADDR64
:
5644 case elfcpp::R_POWERPC_ADDR32
:
5645 case elfcpp::R_POWERPC_UADDR32
:
5646 case elfcpp::R_POWERPC_ADDR24
:
5647 case elfcpp::R_POWERPC_ADDR16
:
5648 case elfcpp::R_POWERPC_ADDR16_LO
:
5649 case elfcpp::R_POWERPC_ADDR16_HI
:
5650 case elfcpp::R_POWERPC_ADDR16_HA
:
5651 case elfcpp::R_POWERPC_UADDR16
:
5652 case elfcpp::R_PPC64_ADDR16_HIGH
:
5653 case elfcpp::R_PPC64_ADDR16_HIGHA
:
5654 case elfcpp::R_PPC64_ADDR16_HIGHER
:
5655 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
5656 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
5657 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
5658 case elfcpp::R_PPC64_ADDR16_DS
:
5659 case elfcpp::R_PPC64_ADDR16_LO_DS
:
5660 case elfcpp::R_POWERPC_ADDR14
:
5661 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5662 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5664 // Make a PLT entry if necessary.
5665 if (gsym
->needs_plt_entry())
5669 target
->push_branch(ppc_object
, data_shndx
,
5670 reloc
.get_r_offset(), r_type
,
5671 elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5672 reloc
.get_r_addend());
5673 target
->make_plt_entry(symtab
, layout
, gsym
);
5675 // Since this is not a PC-relative relocation, we may be
5676 // taking the address of a function. In that case we need to
5677 // set the entry in the dynamic symbol table to the address of
5678 // the PLT call stub.
5680 && gsym
->is_from_dynobj()
5681 && !parameters
->options().output_is_position_independent())
5682 gsym
->set_needs_dynsym_value();
5684 // Make a dynamic relocation if necessary.
5685 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
))
5686 || (size
== 64 && is_ifunc
))
5688 if (gsym
->may_need_copy_reloc())
5690 target
->copy_reloc(symtab
, layout
, object
,
5691 data_shndx
, output_section
, gsym
, reloc
);
5693 else if ((size
== 32
5694 && r_type
== elfcpp::R_POWERPC_ADDR32
5695 && gsym
->can_use_relative_reloc(false)
5696 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
5697 && parameters
->options().shared()))
5699 && r_type
== elfcpp::R_PPC64_ADDR64
5700 && (gsym
->can_use_relative_reloc(false)
5701 || data_shndx
== ppc_object
->opd_shndx())))
5703 Reloc_section
* rela_dyn
5704 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
5705 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
5706 : elfcpp::R_POWERPC_RELATIVE
);
5707 rela_dyn
->add_symbolless_global_addend(
5708 gsym
, dynrel
, output_section
, object
, data_shndx
,
5709 reloc
.get_r_offset(), reloc
.get_r_addend());
5713 Reloc_section
* rela_dyn
5714 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
5715 check_non_pic(object
, r_type
);
5716 rela_dyn
->add_global(gsym
, r_type
, output_section
,
5718 reloc
.get_r_offset(),
5719 reloc
.get_r_addend());
5725 case elfcpp::R_PPC_PLTREL24
:
5726 case elfcpp::R_POWERPC_REL24
:
5729 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5731 elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5732 reloc
.get_r_addend());
5733 if (gsym
->needs_plt_entry()
5734 || (!gsym
->final_value_is_known()
5735 && (gsym
->is_undefined()
5736 || gsym
->is_from_dynobj()
5737 || gsym
->is_preemptible())))
5738 target
->make_plt_entry(symtab
, layout
, gsym
);
5742 case elfcpp::R_PPC64_REL64
:
5743 case elfcpp::R_POWERPC_REL32
:
5744 // Make a dynamic relocation if necessary.
5745 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
)))
5747 if (gsym
->may_need_copy_reloc())
5749 target
->copy_reloc(symtab
, layout
, object
,
5750 data_shndx
, output_section
, gsym
,
5755 Reloc_section
* rela_dyn
5756 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
5757 check_non_pic(object
, r_type
);
5758 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
5759 data_shndx
, reloc
.get_r_offset(),
5760 reloc
.get_r_addend());
5765 case elfcpp::R_POWERPC_REL14
:
5766 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5767 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5769 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5770 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5771 reloc
.get_r_addend());
5774 case elfcpp::R_POWERPC_REL16
:
5775 case elfcpp::R_POWERPC_REL16_LO
:
5776 case elfcpp::R_POWERPC_REL16_HI
:
5777 case elfcpp::R_POWERPC_REL16_HA
:
5778 case elfcpp::R_POWERPC_SECTOFF
:
5779 case elfcpp::R_POWERPC_SECTOFF_LO
:
5780 case elfcpp::R_POWERPC_SECTOFF_HI
:
5781 case elfcpp::R_POWERPC_SECTOFF_HA
:
5782 case elfcpp::R_PPC64_SECTOFF_DS
:
5783 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
5784 case elfcpp::R_POWERPC_TPREL16
:
5785 case elfcpp::R_POWERPC_TPREL16_LO
:
5786 case elfcpp::R_POWERPC_TPREL16_HI
:
5787 case elfcpp::R_POWERPC_TPREL16_HA
:
5788 case elfcpp::R_PPC64_TPREL16_DS
:
5789 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5790 case elfcpp::R_PPC64_TPREL16_HIGH
:
5791 case elfcpp::R_PPC64_TPREL16_HIGHA
:
5792 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5793 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5794 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5795 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5796 case elfcpp::R_POWERPC_DTPREL16
:
5797 case elfcpp::R_POWERPC_DTPREL16_LO
:
5798 case elfcpp::R_POWERPC_DTPREL16_HI
:
5799 case elfcpp::R_POWERPC_DTPREL16_HA
:
5800 case elfcpp::R_PPC64_DTPREL16_DS
:
5801 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
5802 case elfcpp::R_PPC64_DTPREL16_HIGH
:
5803 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
5804 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
5805 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
5806 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
5807 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
5808 case elfcpp::R_PPC64_TLSGD
:
5809 case elfcpp::R_PPC64_TLSLD
:
5812 case elfcpp::R_POWERPC_GOT16
:
5813 case elfcpp::R_POWERPC_GOT16_LO
:
5814 case elfcpp::R_POWERPC_GOT16_HI
:
5815 case elfcpp::R_POWERPC_GOT16_HA
:
5816 case elfcpp::R_PPC64_GOT16_DS
:
5817 case elfcpp::R_PPC64_GOT16_LO_DS
:
5819 // The symbol requires a GOT entry.
5820 Output_data_got_powerpc
<size
, big_endian
>* got
;
5822 got
= target
->got_section(symtab
, layout
);
5823 if (gsym
->final_value_is_known())
5825 if (size
== 32 && is_ifunc
)
5826 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
5828 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
5830 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
5832 // If we are generating a shared object or a pie, this
5833 // symbol's GOT entry will be set by a dynamic relocation.
5834 unsigned int off
= got
->add_constant(0);
5835 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
5837 Reloc_section
* rela_dyn
5838 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
5840 if (gsym
->can_use_relative_reloc(false)
5842 && gsym
->visibility() == elfcpp::STV_PROTECTED
5843 && parameters
->options().shared()))
5845 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
5846 : elfcpp::R_POWERPC_RELATIVE
);
5847 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
5851 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
5852 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
5858 case elfcpp::R_PPC64_TOC16
:
5859 case elfcpp::R_PPC64_TOC16_LO
:
5860 case elfcpp::R_PPC64_TOC16_HI
:
5861 case elfcpp::R_PPC64_TOC16_HA
:
5862 case elfcpp::R_PPC64_TOC16_DS
:
5863 case elfcpp::R_PPC64_TOC16_LO_DS
:
5864 // We need a GOT section.
5865 target
->got_section(symtab
, layout
);
5868 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5869 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
5870 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
5871 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
5873 const bool final
= gsym
->final_value_is_known();
5874 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
5875 if (tls_type
== tls::TLSOPT_NONE
)
5877 Output_data_got_powerpc
<size
, big_endian
>* got
5878 = target
->got_section(symtab
, layout
);
5879 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5880 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
, rela_dyn
,
5881 elfcpp::R_POWERPC_DTPMOD
,
5882 elfcpp::R_POWERPC_DTPREL
);
5884 else if (tls_type
== tls::TLSOPT_TO_IE
)
5886 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
5888 Output_data_got_powerpc
<size
, big_endian
>* got
5889 = target
->got_section(symtab
, layout
);
5890 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5891 if (gsym
->is_undefined()
5892 || gsym
->is_from_dynobj())
5894 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
5895 elfcpp::R_POWERPC_TPREL
);
5899 unsigned int off
= got
->add_constant(0);
5900 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
5901 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
5902 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
5907 else if (tls_type
== tls::TLSOPT_TO_LE
)
5909 // no GOT relocs needed for Local Exec.
5916 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5917 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
5918 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
5919 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
5921 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5922 if (tls_type
== tls::TLSOPT_NONE
)
5923 target
->tlsld_got_offset(symtab
, layout
, object
);
5924 else if (tls_type
== tls::TLSOPT_TO_LE
)
5926 // no GOT relocs needed for Local Exec.
5927 if (parameters
->options().emit_relocs())
5929 Output_section
* os
= layout
->tls_segment()->first_section();
5930 gold_assert(os
!= NULL
);
5931 os
->set_needs_symtab_index();
5939 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5940 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
5941 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
5942 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
5944 Output_data_got_powerpc
<size
, big_endian
>* got
5945 = target
->got_section(symtab
, layout
);
5946 if (!gsym
->final_value_is_known()
5947 && (gsym
->is_from_dynobj()
5948 || gsym
->is_undefined()
5949 || gsym
->is_preemptible()))
5950 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
5951 target
->rela_dyn_section(layout
),
5952 elfcpp::R_POWERPC_DTPREL
);
5954 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
5958 case elfcpp::R_POWERPC_GOT_TPREL16
:
5959 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
5960 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
5961 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
5963 const bool final
= gsym
->final_value_is_known();
5964 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
5965 if (tls_type
== tls::TLSOPT_NONE
)
5967 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
5969 Output_data_got_powerpc
<size
, big_endian
>* got
5970 = target
->got_section(symtab
, layout
);
5971 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5972 if (gsym
->is_undefined()
5973 || gsym
->is_from_dynobj())
5975 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
5976 elfcpp::R_POWERPC_TPREL
);
5980 unsigned int off
= got
->add_constant(0);
5981 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
5982 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
5983 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
5988 else if (tls_type
== tls::TLSOPT_TO_LE
)
5990 // no GOT relocs needed for Local Exec.
5998 unsupported_reloc_global(object
, r_type
, gsym
);
6004 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6005 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6006 case elfcpp::R_POWERPC_GOT_TPREL16
:
6007 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6008 case elfcpp::R_POWERPC_GOT16
:
6009 case elfcpp::R_PPC64_GOT16_DS
:
6010 case elfcpp::R_PPC64_TOC16
:
6011 case elfcpp::R_PPC64_TOC16_DS
:
6012 ppc_object
->set_has_small_toc_reloc();
6018 // Process relocations for gc.
6020 template<int size
, bool big_endian
>
6022 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
6023 Symbol_table
* symtab
,
6025 Sized_relobj_file
<size
, big_endian
>* object
,
6026 unsigned int data_shndx
,
6028 const unsigned char* prelocs
,
6030 Output_section
* output_section
,
6031 bool needs_special_offset_handling
,
6032 size_t local_symbol_count
,
6033 const unsigned char* plocal_symbols
)
6035 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
6036 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
6037 Powerpc_relobj
<size
, big_endian
>* ppc_object
6038 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6040 ppc_object
->set_opd_valid();
6041 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
6043 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
6044 for (p
= ppc_object
->access_from_map()->begin();
6045 p
!= ppc_object
->access_from_map()->end();
6048 Address dst_off
= p
->first
;
6049 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
6050 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
6051 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
6053 Object
* src_obj
= s
->first
;
6054 unsigned int src_indx
= s
->second
;
6055 symtab
->gc()->add_reference(src_obj
, src_indx
,
6056 ppc_object
, dst_indx
);
6060 ppc_object
->access_from_map()->clear();
6061 ppc_object
->process_gc_mark(symtab
);
6062 // Don't look at .opd relocs as .opd will reference everything.
6066 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
6067 typename
Target_powerpc::Relocatable_size_for_reloc
>(
6076 needs_special_offset_handling
,
6081 // Handle target specific gc actions when adding a gc reference from
6082 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
6083 // and DST_OFF. For powerpc64, this adds a referenc to the code
6084 // section of a function descriptor.
6086 template<int size
, bool big_endian
>
6088 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
6089 Symbol_table
* symtab
,
6091 unsigned int src_shndx
,
6093 unsigned int dst_shndx
,
6094 Address dst_off
) const
6096 if (size
!= 64 || dst_obj
->is_dynamic())
6099 Powerpc_relobj
<size
, big_endian
>* ppc_object
6100 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
6101 if (dst_shndx
!= 0 && dst_shndx
== ppc_object
->opd_shndx())
6103 if (ppc_object
->opd_valid())
6105 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
6106 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
6110 // If we haven't run scan_opd_relocs, we must delay
6111 // processing this function descriptor reference.
6112 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
6117 // Add any special sections for this symbol to the gc work list.
6118 // For powerpc64, this adds the code section of a function
6121 template<int size
, bool big_endian
>
6123 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
6124 Symbol_table
* symtab
,
6129 Powerpc_relobj
<size
, big_endian
>* ppc_object
6130 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
6132 unsigned int shndx
= sym
->shndx(&is_ordinary
);
6133 if (is_ordinary
&& shndx
!= 0 && shndx
== ppc_object
->opd_shndx())
6135 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
6136 Address dst_off
= gsym
->value();
6137 if (ppc_object
->opd_valid())
6139 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
6140 symtab
->gc()->worklist().push(Section_id(ppc_object
, dst_indx
));
6143 ppc_object
->add_gc_mark(dst_off
);
6148 // For a symbol location in .opd, set LOC to the location of the
6151 template<int size
, bool big_endian
>
6153 Target_powerpc
<size
, big_endian
>::do_function_location(
6154 Symbol_location
* loc
) const
6156 if (size
== 64 && loc
->shndx
!= 0)
6158 if (loc
->object
->is_dynamic())
6160 Powerpc_dynobj
<size
, big_endian
>* ppc_object
6161 = static_cast<Powerpc_dynobj
<size
, big_endian
>*>(loc
->object
);
6162 if (loc
->shndx
== ppc_object
->opd_shndx())
6165 Address off
= loc
->offset
- ppc_object
->opd_address();
6166 loc
->shndx
= ppc_object
->get_opd_ent(off
, &dest_off
);
6167 loc
->offset
= dest_off
;
6172 const Powerpc_relobj
<size
, big_endian
>* ppc_object
6173 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(loc
->object
);
6174 if (loc
->shndx
== ppc_object
->opd_shndx())
6177 loc
->shndx
= ppc_object
->get_opd_ent(loc
->offset
, &dest_off
);
6178 loc
->offset
= dest_off
;
6184 // Scan relocations for a section.
6186 template<int size
, bool big_endian
>
6188 Target_powerpc
<size
, big_endian
>::scan_relocs(
6189 Symbol_table
* symtab
,
6191 Sized_relobj_file
<size
, big_endian
>* object
,
6192 unsigned int data_shndx
,
6193 unsigned int sh_type
,
6194 const unsigned char* prelocs
,
6196 Output_section
* output_section
,
6197 bool needs_special_offset_handling
,
6198 size_t local_symbol_count
,
6199 const unsigned char* plocal_symbols
)
6201 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
6202 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
6204 if (sh_type
== elfcpp::SHT_REL
)
6206 gold_error(_("%s: unsupported REL reloc section"),
6207 object
->name().c_str());
6211 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
6220 needs_special_offset_handling
,
6225 // Functor class for processing the global symbol table.
6226 // Removes symbols defined on discarded opd entries.
6228 template<bool big_endian
>
6229 class Global_symbol_visitor_opd
6232 Global_symbol_visitor_opd()
6236 operator()(Sized_symbol
<64>* sym
)
6238 if (sym
->has_symtab_index()
6239 || sym
->source() != Symbol::FROM_OBJECT
6240 || !sym
->in_real_elf())
6243 if (sym
->object()->is_dynamic())
6246 Powerpc_relobj
<64, big_endian
>* symobj
6247 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
6248 if (symobj
->opd_shndx() == 0)
6252 unsigned int shndx
= sym
->shndx(&is_ordinary
);
6253 if (shndx
== symobj
->opd_shndx()
6254 && symobj
->get_opd_discard(sym
->value()))
6255 sym
->set_symtab_index(-1U);
6259 template<int size
, bool big_endian
>
6261 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
6263 Symbol_table
* symtab
)
6267 Output_data_save_res
<64, big_endian
>* savres
6268 = new Output_data_save_res
<64, big_endian
>(symtab
);
6269 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
6270 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
6271 savres
, ORDER_TEXT
, false);
6275 // Sort linker created .got section first (for the header), then input
6276 // sections belonging to files using small model code.
6278 template<bool big_endian
>
6279 class Sort_toc_sections
6283 operator()(const Output_section::Input_section
& is1
,
6284 const Output_section::Input_section
& is2
) const
6286 if (!is1
.is_input_section() && is2
.is_input_section())
6289 = (is1
.is_input_section()
6290 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
6291 ->has_small_toc_reloc()));
6293 = (is2
.is_input_section()
6294 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
6295 ->has_small_toc_reloc()));
6296 return small1
&& !small2
;
6300 // Finalize the sections.
6302 template<int size
, bool big_endian
>
6304 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
6306 const Input_objects
*,
6307 Symbol_table
* symtab
)
6309 if (parameters
->doing_static_link())
6311 // At least some versions of glibc elf-init.o have a strong
6312 // reference to __rela_iplt marker syms. A weak ref would be
6314 if (this->iplt_
!= NULL
)
6316 Reloc_section
* rel
= this->iplt_
->rel_plt();
6317 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
6318 Symbol_table::PREDEFINED
, rel
, 0, 0,
6319 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6320 elfcpp::STV_HIDDEN
, 0, false, true);
6321 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
6322 Symbol_table::PREDEFINED
, rel
, 0, 0,
6323 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6324 elfcpp::STV_HIDDEN
, 0, true, true);
6328 symtab
->define_as_constant("__rela_iplt_start", NULL
,
6329 Symbol_table::PREDEFINED
, 0, 0,
6330 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6331 elfcpp::STV_HIDDEN
, 0, true, false);
6332 symtab
->define_as_constant("__rela_iplt_end", NULL
,
6333 Symbol_table::PREDEFINED
, 0, 0,
6334 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6335 elfcpp::STV_HIDDEN
, 0, true, false);
6341 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
6342 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
6344 if (!parameters
->options().relocatable())
6346 this->define_save_restore_funcs(layout
, symtab
);
6348 // Annoyingly, we need to make these sections now whether or
6349 // not we need them. If we delay until do_relax then we
6350 // need to mess with the relaxation machinery checkpointing.
6351 this->got_section(symtab
, layout
);
6352 this->make_brlt_section(layout
);
6354 if (parameters
->options().toc_sort())
6356 Output_section
* os
= this->got_
->output_section();
6357 if (os
!= NULL
&& os
->input_sections().size() > 1)
6358 std::stable_sort(os
->input_sections().begin(),
6359 os
->input_sections().end(),
6360 Sort_toc_sections
<big_endian
>());
6365 // Fill in some more dynamic tags.
6366 Output_data_dynamic
* odyn
= layout
->dynamic_data();
6369 const Reloc_section
* rel_plt
= (this->plt_
== NULL
6371 : this->plt_
->rel_plt());
6372 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
6373 this->rela_dyn_
, true, size
== 32);
6377 if (this->got_
!= NULL
)
6379 this->got_
->finalize_data_size();
6380 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
6381 this->got_
, this->got_
->g_o_t());
6386 if (this->glink_
!= NULL
)
6388 this->glink_
->finalize_data_size();
6389 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
6391 (this->glink_
->pltresolve_size
6397 // Emit any relocs we saved in an attempt to avoid generating COPY
6399 if (this->copy_relocs_
.any_saved_relocs())
6400 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
6403 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6407 ok_lo_toc_insn(uint32_t insn
)
6409 return ((insn
& (0x3f << 26)) == 14u << 26 /* addi */
6410 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
6411 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
6412 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
6413 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
6414 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
6415 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
6416 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
6417 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
6418 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
6419 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
6420 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
6421 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
6422 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
6423 || ((insn
& (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6425 || ((insn
& (0x3f << 26)) == 62u << 26 /* std, stmd */
6426 && ((insn
& 3) == 0 || (insn
& 3) == 3))
6427 || (insn
& (0x3f << 26)) == 12u << 26 /* addic */);
6430 // Return the value to use for a branch relocation.
6432 template<int size
, bool big_endian
>
6433 typename Target_powerpc
<size
, big_endian
>::Address
6434 Target_powerpc
<size
, big_endian
>::symval_for_branch(
6435 const Symbol_table
* symtab
,
6437 const Sized_symbol
<size
>* gsym
,
6438 Powerpc_relobj
<size
, big_endian
>* object
,
6439 unsigned int *dest_shndx
)
6445 // If the symbol is defined in an opd section, ie. is a function
6446 // descriptor, use the function descriptor code entry address
6447 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
6449 && gsym
->source() != Symbol::FROM_OBJECT
)
6452 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
6453 unsigned int shndx
= symobj
->opd_shndx();
6456 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
6457 if (opd_addr
== invalid_address
)
6459 opd_addr
+= symobj
->output_section_address(shndx
);
6460 if (value
>= opd_addr
&& value
< opd_addr
+ symobj
->section_size(shndx
))
6463 *dest_shndx
= symobj
->get_opd_ent(value
- opd_addr
, &sec_off
);
6464 if (symtab
->is_section_folded(symobj
, *dest_shndx
))
6467 = symtab
->icf()->get_folded_section(symobj
, *dest_shndx
);
6468 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(folded
.first
);
6469 *dest_shndx
= folded
.second
;
6471 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
6472 gold_assert(sec_addr
!= invalid_address
);
6473 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
6474 value
= sec_addr
+ sec_off
;
6479 // Perform a relocation.
6481 template<int size
, bool big_endian
>
6483 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
6484 const Relocate_info
<size
, big_endian
>* relinfo
,
6485 Target_powerpc
* target
,
6488 const elfcpp::Rela
<size
, big_endian
>& rela
,
6489 unsigned int r_type
,
6490 const Sized_symbol
<size
>* gsym
,
6491 const Symbol_value
<size
>* psymval
,
6492 unsigned char* view
,
6494 section_size_type view_size
)
6499 switch (this->maybe_skip_tls_get_addr_call(r_type
, gsym
))
6501 case Track_tls::NOT_EXPECTED
:
6502 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6503 _("__tls_get_addr call lacks marker reloc"));
6505 case Track_tls::EXPECTED
:
6506 // We have already complained.
6508 case Track_tls::SKIP
:
6510 case Track_tls::NORMAL
:
6514 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
6515 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
6516 Powerpc_relobj
<size
, big_endian
>* const object
6517 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
6519 bool has_plt_value
= false;
6520 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6522 ? gsym
->use_plt_offset(Scan::get_reference_flags(r_type
, target
))
6523 : object
->local_has_plt_offset(r_sym
))
6524 && (!psymval
->is_ifunc_symbol()
6525 || Scan::reloc_needs_plt_for_ifunc(object
, r_type
, false)))
6527 Stub_table
<size
, big_endian
>* stub_table
6528 = object
->stub_table(relinfo
->data_shndx
);
6529 if (stub_table
== NULL
)
6531 // This is a ref from a data section to an ifunc symbol.
6532 if (target
->stub_tables().size() != 0)
6533 stub_table
= target
->stub_tables()[0];
6535 gold_assert(stub_table
!= NULL
);
6538 off
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
6539 rela
.get_r_addend());
6541 off
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
6542 rela
.get_r_addend());
6543 gold_assert(off
!= invalid_address
);
6544 value
= stub_table
->stub_address() + off
;
6545 has_plt_value
= true;
6548 if (r_type
== elfcpp::R_POWERPC_GOT16
6549 || r_type
== elfcpp::R_POWERPC_GOT16_LO
6550 || r_type
== elfcpp::R_POWERPC_GOT16_HI
6551 || r_type
== elfcpp::R_POWERPC_GOT16_HA
6552 || r_type
== elfcpp::R_PPC64_GOT16_DS
6553 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
6557 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
6558 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
6562 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6563 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
6564 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
6566 value
-= target
->got_section()->got_base_offset(object
);
6568 else if (r_type
== elfcpp::R_PPC64_TOC
)
6570 value
= (target
->got_section()->output_section()->address()
6571 + object
->toc_base_offset());
6573 else if (gsym
!= NULL
6574 && (r_type
== elfcpp::R_POWERPC_REL24
6575 || r_type
== elfcpp::R_PPC_PLTREL24
)
6580 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
6581 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
6582 bool can_plt_call
= false;
6583 if (rela
.get_r_offset() + 8 <= view_size
)
6585 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
6586 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
6589 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
6591 elfcpp::Swap
<32, big_endian
>::
6592 writeval(wv
+ 1, ld_2_1
+ target
->stk_toc());
6593 can_plt_call
= true;
6598 // If we don't have a branch and link followed by a nop,
6599 // we can't go via the plt because there is no place to
6600 // put a toc restoring instruction.
6601 // Unless we know we won't be returning.
6602 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
6603 can_plt_call
= true;
6607 // g++ as of 20130507 emits self-calls without a
6608 // following nop. This is arguably wrong since we have
6609 // conflicting information. On the one hand a global
6610 // symbol and on the other a local call sequence, but
6611 // don't error for this special case.
6612 // It isn't possible to cheaply verify we have exactly
6613 // such a call. Allow all calls to the same section.
6615 Address code
= value
;
6616 if (gsym
->source() == Symbol::FROM_OBJECT
6617 && gsym
->object() == object
)
6619 Address addend
= rela
.get_r_addend();
6620 unsigned int dest_shndx
;
6621 Address opdent
= psymval
->value(object
, addend
);
6622 code
= target
->symval_for_branch(relinfo
->symtab
, opdent
,
6623 gsym
, object
, &dest_shndx
);
6625 if (dest_shndx
== 0)
6626 dest_shndx
= gsym
->shndx(&is_ordinary
);
6627 ok
= dest_shndx
== relinfo
->data_shndx
;
6631 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6632 _("call lacks nop, can't restore toc; "
6633 "recompile with -fPIC"));
6639 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6640 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
6641 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
6642 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
6644 // First instruction of a global dynamic sequence, arg setup insn.
6645 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6646 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6647 enum Got_type got_type
= GOT_TYPE_STANDARD
;
6648 if (tls_type
== tls::TLSOPT_NONE
)
6649 got_type
= GOT_TYPE_TLSGD
;
6650 else if (tls_type
== tls::TLSOPT_TO_IE
)
6651 got_type
= GOT_TYPE_TPREL
;
6652 if (got_type
!= GOT_TYPE_STANDARD
)
6656 gold_assert(gsym
->has_got_offset(got_type
));
6657 value
= gsym
->got_offset(got_type
);
6661 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6662 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
6663 value
= object
->local_got_offset(r_sym
, got_type
);
6665 value
-= target
->got_section()->got_base_offset(object
);
6667 if (tls_type
== tls::TLSOPT_TO_IE
)
6669 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6670 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
6672 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6673 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6674 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
6676 insn
|= 32 << 26; // lwz
6678 insn
|= 58 << 26; // ld
6679 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6681 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
6682 - elfcpp::R_POWERPC_GOT_TLSGD16
);
6684 else if (tls_type
== tls::TLSOPT_TO_LE
)
6686 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6687 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
6689 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6690 Insn insn
= addis_3_13
;
6693 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6694 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6695 value
= psymval
->value(object
, rela
.get_r_addend());
6699 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6701 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6702 r_type
= elfcpp::R_POWERPC_NONE
;
6706 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
6707 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
6708 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
6709 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
6711 // First instruction of a local dynamic sequence, arg setup insn.
6712 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6713 if (tls_type
== tls::TLSOPT_NONE
)
6715 value
= target
->tlsld_got_offset();
6716 value
-= target
->got_section()->got_base_offset(object
);
6720 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
6721 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
6722 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
6724 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6725 Insn insn
= addis_3_13
;
6728 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6729 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6734 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6736 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6737 r_type
= elfcpp::R_POWERPC_NONE
;
6741 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
6742 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
6743 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
6744 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
6746 // Accesses relative to a local dynamic sequence address,
6747 // no optimisation here.
6750 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
6751 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
6755 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6756 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
6757 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
6759 value
-= target
->got_section()->got_base_offset(object
);
6761 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
6762 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
6763 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
6764 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
6766 // First instruction of initial exec sequence.
6767 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6768 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
6769 if (tls_type
== tls::TLSOPT_NONE
)
6773 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
6774 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
6778 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6779 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
6780 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
6782 value
-= target
->got_section()->got_base_offset(object
);
6786 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
6787 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
6788 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
6790 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6791 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6792 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
6797 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6798 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6799 value
= psymval
->value(object
, rela
.get_r_addend());
6803 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6805 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6806 r_type
= elfcpp::R_POWERPC_NONE
;
6810 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6811 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6813 // Second instruction of a global dynamic sequence,
6814 // the __tls_get_addr call
6815 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
6816 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6817 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6818 if (tls_type
!= tls::TLSOPT_NONE
)
6820 if (tls_type
== tls::TLSOPT_TO_IE
)
6822 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6823 Insn insn
= add_3_3_13
;
6826 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6827 r_type
= elfcpp::R_POWERPC_NONE
;
6831 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6832 Insn insn
= addi_3_3
;
6833 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6834 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6835 view
+= 2 * big_endian
;
6836 value
= psymval
->value(object
, rela
.get_r_addend());
6838 this->skip_next_tls_get_addr_call();
6841 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6842 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6844 // Second instruction of a local dynamic sequence,
6845 // the __tls_get_addr call
6846 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
6847 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6848 if (tls_type
== tls::TLSOPT_TO_LE
)
6850 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6851 Insn insn
= addi_3_3
;
6852 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6853 this->skip_next_tls_get_addr_call();
6854 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6855 view
+= 2 * big_endian
;
6859 else if (r_type
== elfcpp::R_POWERPC_TLS
)
6861 // Second instruction of an initial exec sequence
6862 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6863 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
6864 if (tls_type
== tls::TLSOPT_TO_LE
)
6866 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6867 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6868 unsigned int reg
= size
== 32 ? 2 : 13;
6869 insn
= at_tls_transform(insn
, reg
);
6870 gold_assert(insn
!= 0);
6871 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6872 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6873 view
+= 2 * big_endian
;
6874 value
= psymval
->value(object
, rela
.get_r_addend());
6877 else if (!has_plt_value
)
6880 unsigned int dest_shndx
;
6881 if (r_type
!= elfcpp::R_PPC_PLTREL24
)
6882 addend
= rela
.get_r_addend();
6883 value
= psymval
->value(object
, addend
);
6885 value
+= object
->ppc64_local_entry_offset(gsym
);
6887 value
+= object
->ppc64_local_entry_offset(r_sym
);
6888 if (size
== 64 && is_branch_reloc(r_type
))
6889 value
= target
->symval_for_branch(relinfo
->symtab
, value
,
6890 gsym
, object
, &dest_shndx
);
6891 unsigned int max_branch_offset
= 0;
6892 if (r_type
== elfcpp::R_POWERPC_REL24
6893 || r_type
== elfcpp::R_PPC_PLTREL24
6894 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
6895 max_branch_offset
= 1 << 25;
6896 else if (r_type
== elfcpp::R_POWERPC_REL14
6897 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
6898 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
6899 max_branch_offset
= 1 << 15;
6900 if (max_branch_offset
!= 0
6901 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
6903 Stub_table
<size
, big_endian
>* stub_table
6904 = object
->stub_table(relinfo
->data_shndx
);
6905 if (stub_table
!= NULL
)
6907 Address off
= stub_table
->find_long_branch_entry(object
, value
);
6908 if (off
!= invalid_address
)
6909 value
= (stub_table
->stub_address() + stub_table
->plt_size()
6917 case elfcpp::R_PPC64_REL64
:
6918 case elfcpp::R_POWERPC_REL32
:
6919 case elfcpp::R_POWERPC_REL24
:
6920 case elfcpp::R_PPC_PLTREL24
:
6921 case elfcpp::R_PPC_LOCAL24PC
:
6922 case elfcpp::R_POWERPC_REL16
:
6923 case elfcpp::R_POWERPC_REL16_LO
:
6924 case elfcpp::R_POWERPC_REL16_HI
:
6925 case elfcpp::R_POWERPC_REL16_HA
:
6926 case elfcpp::R_POWERPC_REL14
:
6927 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6928 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6932 case elfcpp::R_PPC64_TOC16
:
6933 case elfcpp::R_PPC64_TOC16_LO
:
6934 case elfcpp::R_PPC64_TOC16_HI
:
6935 case elfcpp::R_PPC64_TOC16_HA
:
6936 case elfcpp::R_PPC64_TOC16_DS
:
6937 case elfcpp::R_PPC64_TOC16_LO_DS
:
6938 // Subtract the TOC base address.
6939 value
-= (target
->got_section()->output_section()->address()
6940 + object
->toc_base_offset());
6943 case elfcpp::R_POWERPC_SECTOFF
:
6944 case elfcpp::R_POWERPC_SECTOFF_LO
:
6945 case elfcpp::R_POWERPC_SECTOFF_HI
:
6946 case elfcpp::R_POWERPC_SECTOFF_HA
:
6947 case elfcpp::R_PPC64_SECTOFF_DS
:
6948 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6950 value
-= os
->address();
6953 case elfcpp::R_PPC64_TPREL16_DS
:
6954 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6955 case elfcpp::R_PPC64_TPREL16_HIGH
:
6956 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6958 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
6960 case elfcpp::R_POWERPC_TPREL16
:
6961 case elfcpp::R_POWERPC_TPREL16_LO
:
6962 case elfcpp::R_POWERPC_TPREL16_HI
:
6963 case elfcpp::R_POWERPC_TPREL16_HA
:
6964 case elfcpp::R_POWERPC_TPREL
:
6965 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6966 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6967 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6968 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6969 // tls symbol values are relative to tls_segment()->vaddr()
6973 case elfcpp::R_PPC64_DTPREL16_DS
:
6974 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6975 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6976 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6977 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6978 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6980 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
6981 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
6983 case elfcpp::R_POWERPC_DTPREL16
:
6984 case elfcpp::R_POWERPC_DTPREL16_LO
:
6985 case elfcpp::R_POWERPC_DTPREL16_HI
:
6986 case elfcpp::R_POWERPC_DTPREL16_HA
:
6987 case elfcpp::R_POWERPC_DTPREL
:
6988 case elfcpp::R_PPC64_DTPREL16_HIGH
:
6989 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
6990 // tls symbol values are relative to tls_segment()->vaddr()
6991 value
-= dtp_offset
;
6998 Insn branch_bit
= 0;
7001 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
7002 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7003 branch_bit
= 1 << 21;
7004 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
7005 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7007 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
7008 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7011 if (this->is_isa_v2
)
7013 // Set 'a' bit. This is 0b00010 in BO field for branch
7014 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
7015 // for branch on CTR insns (BO == 1a00t or 1a01t).
7016 if ((insn
& (0x14 << 21)) == (0x04 << 21))
7018 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
7025 // Invert 'y' bit if not the default.
7026 if (static_cast<Signed_address
>(value
) < 0)
7029 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7039 // Multi-instruction sequences that access the TOC can be
7040 // optimized, eg. addis ra,r2,0; addi rb,ra,x;
7041 // to nop; addi rb,r2,x;
7047 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7048 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7049 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7050 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7051 case elfcpp::R_POWERPC_GOT16_HA
:
7052 case elfcpp::R_PPC64_TOC16_HA
:
7053 if (parameters
->options().toc_optimize())
7055 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7056 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7057 if ((insn
& ((0x3f << 26) | 0x1f << 16))
7058 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
7059 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7060 _("toc optimization is not supported "
7061 "for %#08x instruction"), insn
);
7062 else if (value
+ 0x8000 < 0x10000)
7064 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
7070 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7071 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7072 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7073 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7074 case elfcpp::R_POWERPC_GOT16_LO
:
7075 case elfcpp::R_PPC64_GOT16_LO_DS
:
7076 case elfcpp::R_PPC64_TOC16_LO
:
7077 case elfcpp::R_PPC64_TOC16_LO_DS
:
7078 if (parameters
->options().toc_optimize())
7080 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7081 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7082 if (!ok_lo_toc_insn(insn
))
7083 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7084 _("toc optimization is not supported "
7085 "for %#08x instruction"), insn
);
7086 else if (value
+ 0x8000 < 0x10000)
7088 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
7090 // Transform addic to addi when we change reg.
7091 insn
&= ~((0x3f << 26) | (0x1f << 16));
7092 insn
|= (14u << 26) | (2 << 16);
7096 insn
&= ~(0x1f << 16);
7099 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7106 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
7109 case elfcpp::R_POWERPC_ADDR32
:
7110 case elfcpp::R_POWERPC_UADDR32
:
7112 overflow
= Reloc::CHECK_BITFIELD
;
7115 case elfcpp::R_POWERPC_REL32
:
7117 overflow
= Reloc::CHECK_SIGNED
;
7120 case elfcpp::R_POWERPC_ADDR24
:
7121 case elfcpp::R_POWERPC_ADDR16
:
7122 case elfcpp::R_POWERPC_UADDR16
:
7123 case elfcpp::R_PPC64_ADDR16_DS
:
7124 case elfcpp::R_POWERPC_ADDR14
:
7125 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
7126 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
7127 overflow
= Reloc::CHECK_BITFIELD
;
7130 case elfcpp::R_POWERPC_ADDR16_HI
:
7131 case elfcpp::R_POWERPC_ADDR16_HA
:
7132 case elfcpp::R_POWERPC_GOT16_HI
:
7133 case elfcpp::R_POWERPC_GOT16_HA
:
7134 case elfcpp::R_POWERPC_PLT16_HI
:
7135 case elfcpp::R_POWERPC_PLT16_HA
:
7136 case elfcpp::R_POWERPC_SECTOFF_HI
:
7137 case elfcpp::R_POWERPC_SECTOFF_HA
:
7138 case elfcpp::R_PPC64_TOC16_HI
:
7139 case elfcpp::R_PPC64_TOC16_HA
:
7140 case elfcpp::R_PPC64_PLTGOT16_HI
:
7141 case elfcpp::R_PPC64_PLTGOT16_HA
:
7142 case elfcpp::R_POWERPC_TPREL16_HI
:
7143 case elfcpp::R_POWERPC_TPREL16_HA
:
7144 case elfcpp::R_POWERPC_DTPREL16_HI
:
7145 case elfcpp::R_POWERPC_DTPREL16_HA
:
7146 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
7147 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7148 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
7149 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7150 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7151 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7152 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
7153 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7154 case elfcpp::R_POWERPC_REL16_HI
:
7155 case elfcpp::R_POWERPC_REL16_HA
:
7158 case elfcpp::R_POWERPC_REL24
:
7159 case elfcpp::R_PPC_PLTREL24
:
7160 case elfcpp::R_PPC_LOCAL24PC
:
7161 case elfcpp::R_POWERPC_REL16
:
7162 case elfcpp::R_PPC64_TOC16
:
7163 case elfcpp::R_POWERPC_GOT16
:
7164 case elfcpp::R_POWERPC_SECTOFF
:
7165 case elfcpp::R_POWERPC_TPREL16
:
7166 case elfcpp::R_POWERPC_DTPREL16
:
7167 case elfcpp::R_PPC64_TPREL16_DS
:
7168 case elfcpp::R_PPC64_DTPREL16_DS
:
7169 case elfcpp::R_PPC64_TOC16_DS
:
7170 case elfcpp::R_PPC64_GOT16_DS
:
7171 case elfcpp::R_PPC64_SECTOFF_DS
:
7172 case elfcpp::R_POWERPC_REL14
:
7173 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7174 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7175 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7176 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7177 case elfcpp::R_POWERPC_GOT_TPREL16
:
7178 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7179 overflow
= Reloc::CHECK_SIGNED
;
7183 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
7184 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
7187 case elfcpp::R_POWERPC_NONE
:
7188 case elfcpp::R_POWERPC_TLS
:
7189 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
7190 case elfcpp::R_POWERPC_GNU_VTENTRY
:
7193 case elfcpp::R_PPC64_ADDR64
:
7194 case elfcpp::R_PPC64_REL64
:
7195 case elfcpp::R_PPC64_TOC
:
7196 Reloc::addr64(view
, value
);
7199 case elfcpp::R_POWERPC_TPREL
:
7200 case elfcpp::R_POWERPC_DTPREL
:
7202 Reloc::addr64(view
, value
);
7204 status
= Reloc::addr32(view
, value
, overflow
);
7207 case elfcpp::R_PPC64_UADDR64
:
7208 Reloc::addr64_u(view
, value
);
7211 case elfcpp::R_POWERPC_ADDR32
:
7212 status
= Reloc::addr32(view
, value
, overflow
);
7215 case elfcpp::R_POWERPC_REL32
:
7216 case elfcpp::R_POWERPC_UADDR32
:
7217 status
= Reloc::addr32_u(view
, value
, overflow
);
7220 case elfcpp::R_POWERPC_ADDR24
:
7221 case elfcpp::R_POWERPC_REL24
:
7222 case elfcpp::R_PPC_PLTREL24
:
7223 case elfcpp::R_PPC_LOCAL24PC
:
7224 status
= Reloc::addr24(view
, value
, overflow
);
7227 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7228 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7231 status
= Reloc::addr16_ds(view
, value
, overflow
);
7234 case elfcpp::R_POWERPC_ADDR16
:
7235 case elfcpp::R_POWERPC_REL16
:
7236 case elfcpp::R_PPC64_TOC16
:
7237 case elfcpp::R_POWERPC_GOT16
:
7238 case elfcpp::R_POWERPC_SECTOFF
:
7239 case elfcpp::R_POWERPC_TPREL16
:
7240 case elfcpp::R_POWERPC_DTPREL16
:
7241 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7242 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7243 case elfcpp::R_POWERPC_GOT_TPREL16
:
7244 case elfcpp::R_POWERPC_ADDR16_LO
:
7245 case elfcpp::R_POWERPC_REL16_LO
:
7246 case elfcpp::R_PPC64_TOC16_LO
:
7247 case elfcpp::R_POWERPC_GOT16_LO
:
7248 case elfcpp::R_POWERPC_SECTOFF_LO
:
7249 case elfcpp::R_POWERPC_TPREL16_LO
:
7250 case elfcpp::R_POWERPC_DTPREL16_LO
:
7251 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7252 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7253 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7254 status
= Reloc::addr16(view
, value
, overflow
);
7257 case elfcpp::R_POWERPC_UADDR16
:
7258 status
= Reloc::addr16_u(view
, value
, overflow
);
7261 case elfcpp::R_PPC64_ADDR16_HIGH
:
7262 case elfcpp::R_PPC64_TPREL16_HIGH
:
7263 case elfcpp::R_PPC64_DTPREL16_HIGH
:
7265 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
7267 case elfcpp::R_POWERPC_ADDR16_HI
:
7268 case elfcpp::R_POWERPC_REL16_HI
:
7269 case elfcpp::R_PPC64_TOC16_HI
:
7270 case elfcpp::R_POWERPC_GOT16_HI
:
7271 case elfcpp::R_POWERPC_SECTOFF_HI
:
7272 case elfcpp::R_POWERPC_TPREL16_HI
:
7273 case elfcpp::R_POWERPC_DTPREL16_HI
:
7274 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
7275 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
7276 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7277 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
7278 Reloc::addr16_hi(view
, value
);
7281 case elfcpp::R_PPC64_ADDR16_HIGHA
:
7282 case elfcpp::R_PPC64_TPREL16_HIGHA
:
7283 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
7285 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
7287 case elfcpp::R_POWERPC_ADDR16_HA
:
7288 case elfcpp::R_POWERPC_REL16_HA
:
7289 case elfcpp::R_PPC64_TOC16_HA
:
7290 case elfcpp::R_POWERPC_GOT16_HA
:
7291 case elfcpp::R_POWERPC_SECTOFF_HA
:
7292 case elfcpp::R_POWERPC_TPREL16_HA
:
7293 case elfcpp::R_POWERPC_DTPREL16_HA
:
7294 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7295 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7296 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7297 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7298 Reloc::addr16_ha(view
, value
);
7301 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
7303 // R_PPC_EMB_NADDR16_LO
7305 case elfcpp::R_PPC64_ADDR16_HIGHER
:
7306 case elfcpp::R_PPC64_TPREL16_HIGHER
:
7307 Reloc::addr16_hi2(view
, value
);
7310 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
7312 // R_PPC_EMB_NADDR16_HI
7314 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
7315 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
7316 Reloc::addr16_ha2(view
, value
);
7319 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
7321 // R_PPC_EMB_NADDR16_HA
7323 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
7324 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
7325 Reloc::addr16_hi3(view
, value
);
7328 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
7332 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
7333 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
7334 Reloc::addr16_ha3(view
, value
);
7337 case elfcpp::R_PPC64_DTPREL16_DS
:
7338 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
7340 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
7342 case elfcpp::R_PPC64_TPREL16_DS
:
7343 case elfcpp::R_PPC64_TPREL16_LO_DS
:
7345 // R_PPC_TLSGD, R_PPC_TLSLD
7347 case elfcpp::R_PPC64_ADDR16_DS
:
7348 case elfcpp::R_PPC64_ADDR16_LO_DS
:
7349 case elfcpp::R_PPC64_TOC16_DS
:
7350 case elfcpp::R_PPC64_TOC16_LO_DS
:
7351 case elfcpp::R_PPC64_GOT16_DS
:
7352 case elfcpp::R_PPC64_GOT16_LO_DS
:
7353 case elfcpp::R_PPC64_SECTOFF_DS
:
7354 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
7355 status
= Reloc::addr16_ds(view
, value
, overflow
);
7358 case elfcpp::R_POWERPC_ADDR14
:
7359 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
7360 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
7361 case elfcpp::R_POWERPC_REL14
:
7362 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7363 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7364 status
= Reloc::addr14(view
, value
, overflow
);
7367 case elfcpp::R_POWERPC_COPY
:
7368 case elfcpp::R_POWERPC_GLOB_DAT
:
7369 case elfcpp::R_POWERPC_JMP_SLOT
:
7370 case elfcpp::R_POWERPC_RELATIVE
:
7371 case elfcpp::R_POWERPC_DTPMOD
:
7372 case elfcpp::R_PPC64_JMP_IREL
:
7373 case elfcpp::R_POWERPC_IRELATIVE
:
7374 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7375 _("unexpected reloc %u in object file"),
7379 case elfcpp::R_PPC_EMB_SDA21
:
7384 // R_PPC64_TOCSAVE. For the time being this can be ignored.
7388 case elfcpp::R_PPC_EMB_SDA2I16
:
7389 case elfcpp::R_PPC_EMB_SDA2REL
:
7392 // R_PPC64_TLSGD, R_PPC64_TLSLD
7395 case elfcpp::R_POWERPC_PLT32
:
7396 case elfcpp::R_POWERPC_PLTREL32
:
7397 case elfcpp::R_POWERPC_PLT16_LO
:
7398 case elfcpp::R_POWERPC_PLT16_HI
:
7399 case elfcpp::R_POWERPC_PLT16_HA
:
7400 case elfcpp::R_PPC_SDAREL16
:
7401 case elfcpp::R_POWERPC_ADDR30
:
7402 case elfcpp::R_PPC64_PLT64
:
7403 case elfcpp::R_PPC64_PLTREL64
:
7404 case elfcpp::R_PPC64_PLTGOT16
:
7405 case elfcpp::R_PPC64_PLTGOT16_LO
:
7406 case elfcpp::R_PPC64_PLTGOT16_HI
:
7407 case elfcpp::R_PPC64_PLTGOT16_HA
:
7408 case elfcpp::R_PPC64_PLT16_LO_DS
:
7409 case elfcpp::R_PPC64_PLTGOT16_DS
:
7410 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
7411 case elfcpp::R_PPC_EMB_RELSDA
:
7412 case elfcpp::R_PPC_TOC16
:
7415 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7416 _("unsupported reloc %u"),
7420 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
)
7421 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7422 _("relocation overflow"));
7427 // Relocate section data.
7429 template<int size
, bool big_endian
>
7431 Target_powerpc
<size
, big_endian
>::relocate_section(
7432 const Relocate_info
<size
, big_endian
>* relinfo
,
7433 unsigned int sh_type
,
7434 const unsigned char* prelocs
,
7436 Output_section
* output_section
,
7437 bool needs_special_offset_handling
,
7438 unsigned char* view
,
7440 section_size_type view_size
,
7441 const Reloc_symbol_changes
* reloc_symbol_changes
)
7443 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
7444 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
7445 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
7446 Powerpc_comdat_behavior
;
7448 gold_assert(sh_type
== elfcpp::SHT_RELA
);
7450 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
7451 Powerpc_relocate
, Powerpc_comdat_behavior
>(
7457 needs_special_offset_handling
,
7461 reloc_symbol_changes
);
7464 class Powerpc_scan_relocatable_reloc
7467 // Return the strategy to use for a local symbol which is not a
7468 // section symbol, given the relocation type.
7469 inline Relocatable_relocs::Reloc_strategy
7470 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
7472 if (r_type
== 0 && r_sym
== 0)
7473 return Relocatable_relocs::RELOC_DISCARD
;
7474 return Relocatable_relocs::RELOC_COPY
;
7477 // Return the strategy to use for a local symbol which is a section
7478 // symbol, given the relocation type.
7479 inline Relocatable_relocs::Reloc_strategy
7480 local_section_strategy(unsigned int, Relobj
*)
7482 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
7485 // Return the strategy to use for a global symbol, given the
7486 // relocation type, the object, and the symbol index.
7487 inline Relocatable_relocs::Reloc_strategy
7488 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
7490 if (r_type
== elfcpp::R_PPC_PLTREL24
)
7491 return Relocatable_relocs::RELOC_SPECIAL
;
7492 return Relocatable_relocs::RELOC_COPY
;
7496 // Scan the relocs during a relocatable link.
7498 template<int size
, bool big_endian
>
7500 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
7501 Symbol_table
* symtab
,
7503 Sized_relobj_file
<size
, big_endian
>* object
,
7504 unsigned int data_shndx
,
7505 unsigned int sh_type
,
7506 const unsigned char* prelocs
,
7508 Output_section
* output_section
,
7509 bool needs_special_offset_handling
,
7510 size_t local_symbol_count
,
7511 const unsigned char* plocal_symbols
,
7512 Relocatable_relocs
* rr
)
7514 gold_assert(sh_type
== elfcpp::SHT_RELA
);
7516 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
7517 Powerpc_scan_relocatable_reloc
>(
7525 needs_special_offset_handling
,
7531 // Emit relocations for a section.
7532 // This is a modified version of the function by the same name in
7533 // target-reloc.h. Using relocate_special_relocatable for
7534 // R_PPC_PLTREL24 would require duplication of the entire body of the
7535 // loop, so we may as well duplicate the whole thing.
7537 template<int size
, bool big_endian
>
7539 Target_powerpc
<size
, big_endian
>::relocate_relocs(
7540 const Relocate_info
<size
, big_endian
>* relinfo
,
7541 unsigned int sh_type
,
7542 const unsigned char* prelocs
,
7544 Output_section
* output_section
,
7545 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
7546 const Relocatable_relocs
* rr
,
7548 Address view_address
,
7550 unsigned char* reloc_view
,
7551 section_size_type reloc_view_size
)
7553 gold_assert(sh_type
== elfcpp::SHT_RELA
);
7555 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
7557 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc_write
7559 const int reloc_size
7560 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
7562 Powerpc_relobj
<size
, big_endian
>* const object
7563 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
7564 const unsigned int local_count
= object
->local_symbol_count();
7565 unsigned int got2_shndx
= object
->got2_shndx();
7566 Address got2_addend
= 0;
7567 if (got2_shndx
!= 0)
7569 got2_addend
= object
->get_output_section_offset(got2_shndx
);
7570 gold_assert(got2_addend
!= invalid_address
);
7573 unsigned char* pwrite
= reloc_view
;
7574 bool zap_next
= false;
7575 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
7577 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
7578 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
7581 Reltype
reloc(prelocs
);
7582 Reltype_write
reloc_write(pwrite
);
7584 Address offset
= reloc
.get_r_offset();
7585 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
7586 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
7587 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
7588 const unsigned int orig_r_sym
= r_sym
;
7589 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
7590 = reloc
.get_r_addend();
7591 const Symbol
* gsym
= NULL
;
7595 // We could arrange to discard these and other relocs for
7596 // tls optimised sequences in the strategy methods, but for
7597 // now do as BFD ld does.
7598 r_type
= elfcpp::R_POWERPC_NONE
;
7602 // Get the new symbol index.
7603 if (r_sym
< local_count
)
7607 case Relocatable_relocs::RELOC_COPY
:
7608 case Relocatable_relocs::RELOC_SPECIAL
:
7611 r_sym
= object
->symtab_index(r_sym
);
7612 gold_assert(r_sym
!= -1U);
7616 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
7618 // We are adjusting a section symbol. We need to find
7619 // the symbol table index of the section symbol for
7620 // the output section corresponding to input section
7621 // in which this symbol is defined.
7622 gold_assert(r_sym
< local_count
);
7624 unsigned int shndx
=
7625 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
7626 gold_assert(is_ordinary
);
7627 Output_section
* os
= object
->output_section(shndx
);
7628 gold_assert(os
!= NULL
);
7629 gold_assert(os
->needs_symtab_index());
7630 r_sym
= os
->symtab_index();
7640 gsym
= object
->global_symbol(r_sym
);
7641 gold_assert(gsym
!= NULL
);
7642 if (gsym
->is_forwarder())
7643 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
7645 gold_assert(gsym
->has_symtab_index());
7646 r_sym
= gsym
->symtab_index();
7649 // Get the new offset--the location in the output section where
7650 // this relocation should be applied.
7651 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
7652 offset
+= offset_in_output_section
;
7655 section_offset_type sot_offset
=
7656 convert_types
<section_offset_type
, Address
>(offset
);
7657 section_offset_type new_sot_offset
=
7658 output_section
->output_offset(object
, relinfo
->data_shndx
,
7660 gold_assert(new_sot_offset
!= -1);
7661 offset
= new_sot_offset
;
7664 // In an object file, r_offset is an offset within the section.
7665 // In an executable or dynamic object, generated by
7666 // --emit-relocs, r_offset is an absolute address.
7667 if (!parameters
->options().relocatable())
7669 offset
+= view_address
;
7670 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
7671 offset
-= offset_in_output_section
;
7674 // Handle the reloc addend based on the strategy.
7675 if (strategy
== Relocatable_relocs::RELOC_COPY
)
7677 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
7679 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
7680 addend
= psymval
->value(object
, addend
);
7682 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
7684 if (addend
>= 32768)
7685 addend
+= got2_addend
;
7690 if (!parameters
->options().relocatable())
7692 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7693 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
7694 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
7695 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
7697 // First instruction of a global dynamic sequence,
7699 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7700 switch (this->optimize_tls_gd(final
))
7702 case tls::TLSOPT_TO_IE
:
7703 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
7704 - elfcpp::R_POWERPC_GOT_TLSGD16
);
7706 case tls::TLSOPT_TO_LE
:
7707 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7708 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
7709 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7712 r_type
= elfcpp::R_POWERPC_NONE
;
7713 offset
-= 2 * big_endian
;
7720 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7721 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
7722 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
7723 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
7725 // First instruction of a local dynamic sequence,
7727 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
7729 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7730 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
7732 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7733 const Output_section
* os
= relinfo
->layout
->tls_segment()
7735 gold_assert(os
!= NULL
);
7736 gold_assert(os
->needs_symtab_index());
7737 r_sym
= os
->symtab_index();
7738 addend
= dtp_offset
;
7742 r_type
= elfcpp::R_POWERPC_NONE
;
7743 offset
-= 2 * big_endian
;
7747 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
7748 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
7749 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
7750 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
7752 // First instruction of initial exec sequence.
7753 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7754 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
7756 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
7757 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
7758 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7761 r_type
= elfcpp::R_POWERPC_NONE
;
7762 offset
-= 2 * big_endian
;
7766 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
7767 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
7769 // Second instruction of a global dynamic sequence,
7770 // the __tls_get_addr call
7771 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7772 switch (this->optimize_tls_gd(final
))
7774 case tls::TLSOPT_TO_IE
:
7775 r_type
= elfcpp::R_POWERPC_NONE
;
7778 case tls::TLSOPT_TO_LE
:
7779 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7780 offset
+= 2 * big_endian
;
7787 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
7788 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
7790 // Second instruction of a local dynamic sequence,
7791 // the __tls_get_addr call
7792 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
7794 const Output_section
* os
= relinfo
->layout
->tls_segment()
7796 gold_assert(os
!= NULL
);
7797 gold_assert(os
->needs_symtab_index());
7798 r_sym
= os
->symtab_index();
7799 addend
= dtp_offset
;
7800 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7801 offset
+= 2 * big_endian
;
7805 else if (r_type
== elfcpp::R_POWERPC_TLS
)
7807 // Second instruction of an initial exec sequence
7808 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7809 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
7811 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7812 offset
+= 2 * big_endian
;
7817 reloc_write
.put_r_offset(offset
);
7818 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
7819 reloc_write
.put_r_addend(addend
);
7821 pwrite
+= reloc_size
;
7824 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
7825 == reloc_view_size
);
7828 // Return the value to use for a dynamic symbol which requires special
7829 // treatment. This is how we support equality comparisons of function
7830 // pointers across shared library boundaries, as described in the
7831 // processor specific ABI supplement.
7833 template<int size
, bool big_endian
>
7835 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
7839 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
7840 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7841 p
!= this->stub_tables_
.end();
7844 Address off
= (*p
)->find_plt_call_entry(gsym
);
7845 if (off
!= invalid_address
)
7846 return (*p
)->stub_address() + off
;
7852 // Return the PLT address to use for a local symbol.
7853 template<int size
, bool big_endian
>
7855 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
7856 const Relobj
* object
,
7857 unsigned int symndx
) const
7861 const Sized_relobj
<size
, big_endian
>* relobj
7862 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
7863 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7864 p
!= this->stub_tables_
.end();
7867 Address off
= (*p
)->find_plt_call_entry(relobj
->sized_relobj(),
7869 if (off
!= invalid_address
)
7870 return (*p
)->stub_address() + off
;
7876 // Return the PLT address to use for a global symbol.
7877 template<int size
, bool big_endian
>
7879 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
7880 const Symbol
* gsym
) const
7884 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7885 p
!= this->stub_tables_
.end();
7888 Address off
= (*p
)->find_plt_call_entry(gsym
);
7889 if (off
!= invalid_address
)
7890 return (*p
)->stub_address() + off
;
7896 // Return the offset to use for the GOT_INDX'th got entry which is
7897 // for a local tls symbol specified by OBJECT, SYMNDX.
7898 template<int size
, bool big_endian
>
7900 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
7901 const Relobj
* object
,
7902 unsigned int symndx
,
7903 unsigned int got_indx
) const
7905 const Powerpc_relobj
<size
, big_endian
>* ppc_object
7906 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
7907 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
7909 for (Got_type got_type
= GOT_TYPE_TLSGD
;
7910 got_type
<= GOT_TYPE_TPREL
;
7911 got_type
= Got_type(got_type
+ 1))
7912 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
7914 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
7915 if (got_type
== GOT_TYPE_TLSGD
)
7917 if (off
== got_indx
* (size
/ 8))
7919 if (got_type
== GOT_TYPE_TPREL
)
7929 // Return the offset to use for the GOT_INDX'th got entry which is
7930 // for global tls symbol GSYM.
7931 template<int size
, bool big_endian
>
7933 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
7935 unsigned int got_indx
) const
7937 if (gsym
->type() == elfcpp::STT_TLS
)
7939 for (Got_type got_type
= GOT_TYPE_TLSGD
;
7940 got_type
<= GOT_TYPE_TPREL
;
7941 got_type
= Got_type(got_type
+ 1))
7942 if (gsym
->has_got_offset(got_type
))
7944 unsigned int off
= gsym
->got_offset(got_type
);
7945 if (got_type
== GOT_TYPE_TLSGD
)
7947 if (off
== got_indx
* (size
/ 8))
7949 if (got_type
== GOT_TYPE_TPREL
)
7959 // The selector for powerpc object files.
7961 template<int size
, bool big_endian
>
7962 class Target_selector_powerpc
: public Target_selector
7965 Target_selector_powerpc()
7966 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
7969 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
7970 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
7972 ? (big_endian
? "elf64ppc" : "elf64lppc")
7973 : (big_endian
? "elf32ppc" : "elf32lppc")))
7977 do_instantiate_target()
7978 { return new Target_powerpc
<size
, big_endian
>(); }
7981 Target_selector_powerpc
<32, true> target_selector_ppc32
;
7982 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
7983 Target_selector_powerpc
<64, true> target_selector_ppc64
;
7984 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
7986 // Instantiate these constants for -O0
7987 template<int size
, bool big_endian
>
7988 const int Output_data_glink
<size
, big_endian
>::pltresolve_size
;
7989 template<int size
, bool big_endian
>
7990 const typename Stub_table
<size
, big_endian
>::Address
7991 Stub_table
<size
, big_endian
>::invalid_address
;
7992 template<int size
, bool big_endian
>
7993 const typename Target_powerpc
<size
, big_endian
>::Address
7994 Target_powerpc
<size
, big_endian
>::invalid_address
;
7996 } // End anonymous namespace.