1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright (C) 2008-2019 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"
44 #include "attributes.h"
51 template<int size
, bool big_endian
>
52 class Output_data_plt_powerpc
;
54 template<int size
, bool big_endian
>
55 class Output_data_brlt_powerpc
;
57 template<int size
, bool big_endian
>
58 class Output_data_got_powerpc
;
60 template<int size
, bool big_endian
>
61 class Output_data_glink
;
63 template<int size
, bool big_endian
>
66 template<int size
, bool big_endian
>
67 class Output_data_save_res
;
69 template<int size
, bool big_endian
>
72 struct Stub_table_owner
75 : output_section(NULL
), owner(NULL
)
78 Output_section
* output_section
;
79 const Output_section::Input_section
* owner
;
82 inline bool is_branch_reloc(unsigned int);
85 inline bool is_plt16_reloc(unsigned int);
87 // Counter incremented on every Powerpc_relobj constructed.
88 static uint32_t object_id
= 0;
90 template<int size
, bool big_endian
>
91 class Powerpc_relobj
: public Sized_relobj_file
<size
, big_endian
>
94 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
95 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_refs
;
96 typedef Unordered_map
<Address
, Section_refs
> Access_from
;
98 Powerpc_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
99 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
100 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
101 uniq_(object_id
++), special_(0), relatoc_(0), toc_(0),
102 has_small_toc_reloc_(false), opd_valid_(false),
103 e_flags_(ehdr
.get_e_flags()), no_toc_opt_(), opd_ent_(),
104 access_from_map_(), has14_(), stub_table_index_(), st_other_(),
105 attributes_section_data_(NULL
)
107 this->set_abiversion(0);
111 { delete this->attributes_section_data_
; }
113 // Read the symbols then set up st_other vector.
115 do_read_symbols(Read_symbols_data
*);
117 // Arrange to always relocate .toc first.
119 do_relocate_sections(
120 const Symbol_table
* symtab
, const Layout
* layout
,
121 const unsigned char* pshdrs
, Output_file
* of
,
122 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
);
124 // The .toc section index.
131 // Mark .toc entry at OFF as not optimizable.
133 set_no_toc_opt(Address off
)
135 if (this->no_toc_opt_
.empty())
136 this->no_toc_opt_
.resize(this->section_size(this->toc_shndx())
139 if (off
< this->no_toc_opt_
.size())
140 this->no_toc_opt_
[off
] = true;
143 // Mark the entire .toc as not optimizable.
147 this->no_toc_opt_
.resize(1);
148 this->no_toc_opt_
[0] = true;
151 // Return true if code using the .toc entry at OFF should not be edited.
153 no_toc_opt(Address off
) const
155 if (this->no_toc_opt_
.empty())
158 if (off
>= this->no_toc_opt_
.size())
160 return this->no_toc_opt_
[off
];
163 // The .got2 section shndx.
168 return this->special_
;
173 // The .opd section shndx.
180 return this->special_
;
183 // Init OPD entry arrays.
185 init_opd(size_t opd_size
)
187 size_t count
= this->opd_ent_ndx(opd_size
);
188 this->opd_ent_
.resize(count
);
191 // Return section and offset of function entry for .opd + R_OFF.
193 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
195 size_t ndx
= this->opd_ent_ndx(r_off
);
196 gold_assert(ndx
< this->opd_ent_
.size());
197 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
199 *value
= this->opd_ent_
[ndx
].off
;
200 return this->opd_ent_
[ndx
].shndx
;
203 // Set section and offset of function entry for .opd + R_OFF.
205 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
207 size_t ndx
= this->opd_ent_ndx(r_off
);
208 gold_assert(ndx
< this->opd_ent_
.size());
209 this->opd_ent_
[ndx
].shndx
= shndx
;
210 this->opd_ent_
[ndx
].off
= value
;
213 // Return discard flag for .opd + R_OFF.
215 get_opd_discard(Address r_off
) const
217 size_t ndx
= this->opd_ent_ndx(r_off
);
218 gold_assert(ndx
< this->opd_ent_
.size());
219 return this->opd_ent_
[ndx
].discard
;
222 // Set discard flag for .opd + R_OFF.
224 set_opd_discard(Address r_off
)
226 size_t ndx
= this->opd_ent_ndx(r_off
);
227 gold_assert(ndx
< this->opd_ent_
.size());
228 this->opd_ent_
[ndx
].discard
= true;
233 { return this->opd_valid_
; }
237 { this->opd_valid_
= true; }
239 // Examine .rela.opd to build info about function entry points.
241 scan_opd_relocs(size_t reloc_count
,
242 const unsigned char* prelocs
,
243 const unsigned char* plocal_syms
);
245 // Returns true if a code sequence loading a TOC entry can be
246 // converted into code calculating a TOC pointer relative offset.
248 make_toc_relative(Target_powerpc
<size
, big_endian
>* target
,
251 // Perform the Sized_relobj_file method, then set up opd info from
254 do_read_relocs(Read_relocs_data
*);
257 do_find_special_sections(Read_symbols_data
* sd
);
259 // Adjust this local symbol value. Return false if the symbol
260 // should be discarded from the output file.
262 do_adjust_local_symbol(Symbol_value
<size
>* lv
) const
264 if (size
== 64 && this->opd_shndx() != 0)
267 if (lv
->input_shndx(&is_ordinary
) != this->opd_shndx())
269 if (this->get_opd_discard(lv
->input_value()))
277 { return &this->access_from_map_
; }
279 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
280 // section at DST_OFF.
282 add_reference(Relobj
* src_obj
,
283 unsigned int src_indx
,
284 typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
286 Section_id
src_id(src_obj
, src_indx
);
287 this->access_from_map_
[dst_off
].insert(src_id
);
290 // Add a reference to the code section specified by the .opd entry
293 add_gc_mark(typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
295 size_t ndx
= this->opd_ent_ndx(dst_off
);
296 if (ndx
>= this->opd_ent_
.size())
297 this->opd_ent_
.resize(ndx
+ 1);
298 this->opd_ent_
[ndx
].gc_mark
= true;
302 process_gc_mark(Symbol_table
* symtab
)
304 for (size_t i
= 0; i
< this->opd_ent_
.size(); i
++)
305 if (this->opd_ent_
[i
].gc_mark
)
307 unsigned int shndx
= this->opd_ent_
[i
].shndx
;
308 symtab
->gc()->worklist().push_back(Section_id(this, shndx
));
312 // Return offset in output GOT section that this object will use
313 // as a TOC pointer. Won't be just a constant with multi-toc support.
315 toc_base_offset() const
319 set_has_small_toc_reloc()
320 { has_small_toc_reloc_
= true; }
323 has_small_toc_reloc() const
324 { return has_small_toc_reloc_
; }
327 set_has_14bit_branch(unsigned int shndx
)
329 if (shndx
>= this->has14_
.size())
330 this->has14_
.resize(shndx
+ 1);
331 this->has14_
[shndx
] = true;
335 has_14bit_branch(unsigned int shndx
) const
336 { return shndx
< this->has14_
.size() && this->has14_
[shndx
]; }
339 set_stub_table(unsigned int shndx
, unsigned int stub_index
)
341 if (shndx
>= this->stub_table_index_
.size())
342 this->stub_table_index_
.resize(shndx
+ 1, -1);
343 this->stub_table_index_
[shndx
] = stub_index
;
346 Stub_table
<size
, big_endian
>*
347 stub_table(unsigned int shndx
)
349 if (shndx
< this->stub_table_index_
.size())
351 Target_powerpc
<size
, big_endian
>* target
352 = static_cast<Target_powerpc
<size
, big_endian
>*>(
353 parameters
->sized_target
<size
, big_endian
>());
354 unsigned int indx
= this->stub_table_index_
[shndx
];
355 if (indx
< target
->stub_tables().size())
356 return target
->stub_tables()[indx
];
364 this->stub_table_index_
.clear();
369 { return this->uniq_
; }
373 { return this->e_flags_
& elfcpp::EF_PPC64_ABI
; }
375 // Set ABI version for input and output
377 set_abiversion(int ver
);
380 st_other (unsigned int symndx
) const
382 return this->st_other_
[symndx
];
386 ppc64_local_entry_offset(const Symbol
* sym
) const
387 { return elfcpp::ppc64_decode_local_entry(sym
->nonvis() >> 3); }
390 ppc64_local_entry_offset(unsigned int symndx
) const
391 { return elfcpp::ppc64_decode_local_entry(this->st_other_
[symndx
] >> 5); }
393 // The contents of the .gnu.attributes section if there is one.
394 const Attributes_section_data
*
395 attributes_section_data() const
396 { return this->attributes_section_data_
; }
407 // Return index into opd_ent_ array for .opd entry at OFF.
408 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
409 // apart when the language doesn't use the last 8-byte word, the
410 // environment pointer. Thus dividing the entry section offset by
411 // 16 will give an index into opd_ent_ that works for either layout
412 // of .opd. (It leaves some elements of the vector unused when .opd
413 // entries are spaced 24 bytes apart, but we don't know the spacing
414 // until relocations are processed, and in any case it is possible
415 // for an object to have some entries spaced 16 bytes apart and
416 // others 24 bytes apart.)
418 opd_ent_ndx(size_t off
) const
421 // Per object unique identifier
424 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
425 unsigned int special_
;
427 // For 64-bit the .rela.toc and .toc section shdnx.
428 unsigned int relatoc_
;
431 // For 64-bit, whether this object uses small model relocs to access
433 bool has_small_toc_reloc_
;
435 // Set at the start of gc_process_relocs, when we know opd_ent_
436 // vector is valid. The flag could be made atomic and set in
437 // do_read_relocs with memory_order_release and then tested with
438 // memory_order_acquire, potentially resulting in fewer entries in
443 elfcpp::Elf_Word e_flags_
;
445 // For 64-bit, an array with one entry per 64-bit word in the .toc
446 // section, set if accesses using that word cannot be optimised.
447 std::vector
<bool> no_toc_opt_
;
449 // The first 8-byte word of an OPD entry gives the address of the
450 // entry point of the function. Relocatable object files have a
451 // relocation on this word. The following vector records the
452 // section and offset specified by these relocations.
453 std::vector
<Opd_ent
> opd_ent_
;
455 // References made to this object's .opd section when running
456 // gc_process_relocs for another object, before the opd_ent_ vector
457 // is valid for this object.
458 Access_from access_from_map_
;
460 // Whether input section has a 14-bit branch reloc.
461 std::vector
<bool> has14_
;
463 // The stub table to use for a given input section.
464 std::vector
<unsigned int> stub_table_index_
;
466 // ELF st_other field for local symbols.
467 std::vector
<unsigned char> st_other_
;
469 // Object attributes if there is a .gnu.attributes section.
470 Attributes_section_data
* attributes_section_data_
;
473 template<int size
, bool big_endian
>
474 class Powerpc_dynobj
: public Sized_dynobj
<size
, big_endian
>
477 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
479 Powerpc_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
480 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
481 : Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
482 opd_shndx_(0), e_flags_(ehdr
.get_e_flags()), opd_ent_(),
483 attributes_section_data_(NULL
)
485 this->set_abiversion(0);
489 { delete this->attributes_section_data_
; }
491 // Call Sized_dynobj::do_read_symbols to read the symbols then
492 // read .opd from a dynamic object, filling in opd_ent_ vector,
494 do_read_symbols(Read_symbols_data
*);
496 // The .opd section shndx.
500 return this->opd_shndx_
;
503 // The .opd section address.
507 return this->opd_address_
;
510 // Init OPD entry arrays.
512 init_opd(size_t opd_size
)
514 size_t count
= this->opd_ent_ndx(opd_size
);
515 this->opd_ent_
.resize(count
);
518 // Return section and offset of function entry for .opd + R_OFF.
520 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
522 size_t ndx
= this->opd_ent_ndx(r_off
);
523 gold_assert(ndx
< this->opd_ent_
.size());
524 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
526 *value
= this->opd_ent_
[ndx
].off
;
527 return this->opd_ent_
[ndx
].shndx
;
530 // Set section and offset of function entry for .opd + R_OFF.
532 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
534 size_t ndx
= this->opd_ent_ndx(r_off
);
535 gold_assert(ndx
< this->opd_ent_
.size());
536 this->opd_ent_
[ndx
].shndx
= shndx
;
537 this->opd_ent_
[ndx
].off
= value
;
542 { return this->e_flags_
& elfcpp::EF_PPC64_ABI
; }
544 // Set ABI version for input and output.
546 set_abiversion(int ver
);
548 // The contents of the .gnu.attributes section if there is one.
549 const Attributes_section_data
*
550 attributes_section_data() const
551 { return this->attributes_section_data_
; }
554 // Used to specify extent of executable sections.
557 Sec_info(Address start_
, Address len_
, unsigned int shndx_
)
558 : start(start_
), len(len_
), shndx(shndx_
)
562 operator<(const Sec_info
& that
) const
563 { return this->start
< that
.start
; }
576 // Return index into opd_ent_ array for .opd entry at OFF.
578 opd_ent_ndx(size_t off
) const
581 // For 64-bit the .opd section shndx and address.
582 unsigned int opd_shndx_
;
583 Address opd_address_
;
586 elfcpp::Elf_Word e_flags_
;
588 // The first 8-byte word of an OPD entry gives the address of the
589 // entry point of the function. Records the section and offset
590 // corresponding to the address. Note that in dynamic objects,
591 // offset is *not* relative to the section.
592 std::vector
<Opd_ent
> opd_ent_
;
594 // Object attributes if there is a .gnu.attributes section.
595 Attributes_section_data
* attributes_section_data_
;
598 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
599 // base class will emit.
601 template<int sh_type
, int size
, bool big_endian
>
602 class Powerpc_copy_relocs
: public Copy_relocs
<sh_type
, size
, big_endian
>
605 Powerpc_copy_relocs()
606 : Copy_relocs
<sh_type
, size
, big_endian
>(elfcpp::R_POWERPC_COPY
)
609 // Emit any saved relocations which turn out to be needed. This is
610 // called after all the relocs have been scanned.
612 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
615 template<int size
, bool big_endian
>
616 class Target_powerpc
: public Sized_target
<size
, big_endian
>
620 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
621 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
622 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Signed_address
;
623 typedef Unordered_set
<Symbol_location
, Symbol_location_hash
> Tocsave_loc
;
624 static const Address invalid_address
= static_cast<Address
>(0) - 1;
625 // Offset of tp and dtp pointers from start of TLS block.
626 static const Address tp_offset
= 0x7000;
627 static const Address dtp_offset
= 0x8000;
630 : Sized_target
<size
, big_endian
>(&powerpc_info
),
631 got_(NULL
), plt_(NULL
), iplt_(NULL
), lplt_(NULL
), brlt_section_(NULL
),
632 glink_(NULL
), rela_dyn_(NULL
), copy_relocs_(),
633 tlsld_got_offset_(-1U),
634 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
635 plt_thread_safe_(false), plt_localentry0_(false),
636 plt_localentry0_init_(false), has_localentry0_(false),
637 has_tls_get_addr_opt_(false),
638 relax_failed_(false), relax_fail_count_(0),
639 stub_group_size_(0), savres_section_(0),
640 tls_get_addr_(NULL
), tls_get_addr_opt_(NULL
),
641 attributes_section_data_(NULL
),
642 last_fp_(NULL
), last_ld_(NULL
), last_vec_(NULL
), last_struct_(NULL
)
646 // Process the relocations to determine unreferenced sections for
647 // garbage collection.
649 gc_process_relocs(Symbol_table
* symtab
,
651 Sized_relobj_file
<size
, big_endian
>* object
,
652 unsigned int data_shndx
,
653 unsigned int sh_type
,
654 const unsigned char* prelocs
,
656 Output_section
* output_section
,
657 bool needs_special_offset_handling
,
658 size_t local_symbol_count
,
659 const unsigned char* plocal_symbols
);
661 // Scan the relocations to look for symbol adjustments.
663 scan_relocs(Symbol_table
* symtab
,
665 Sized_relobj_file
<size
, big_endian
>* object
,
666 unsigned int data_shndx
,
667 unsigned int sh_type
,
668 const unsigned char* prelocs
,
670 Output_section
* output_section
,
671 bool needs_special_offset_handling
,
672 size_t local_symbol_count
,
673 const unsigned char* plocal_symbols
);
675 // Map input .toc section to output .got section.
677 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
679 if (size
== 64 && strcmp(name
, ".toc") == 0)
687 // Provide linker defined save/restore functions.
689 define_save_restore_funcs(Layout
*, Symbol_table
*);
691 // No stubs unless a final link.
694 { return !parameters
->options().relocatable(); }
697 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
700 do_plt_fde_location(const Output_data
*, unsigned char*,
701 uint64_t*, off_t
*) const;
703 // Stash info about branches, for stub generation.
705 push_branch(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
706 unsigned int data_shndx
, Address r_offset
,
707 unsigned int r_type
, unsigned int r_sym
, Address addend
)
709 Branch_info
info(ppc_object
, data_shndx
, r_offset
, r_type
, r_sym
, addend
);
710 this->branch_info_
.push_back(info
);
711 if (r_type
== elfcpp::R_POWERPC_REL14
712 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
713 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
714 ppc_object
->set_has_14bit_branch(data_shndx
);
717 // Return whether the last branch is a plt call, and if so, mark the
718 // branch as having an R_PPC64_TOCSAVE.
720 mark_pltcall(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
721 unsigned int data_shndx
, Address r_offset
, Symbol_table
* symtab
)
724 && !this->branch_info_
.empty()
725 && this->branch_info_
.back().mark_pltcall(ppc_object
, data_shndx
,
726 r_offset
, this, symtab
));
729 // Say the given location, that of a nop in a function prologue with
730 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
731 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
733 add_tocsave(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
734 unsigned int shndx
, Address offset
)
737 loc
.object
= ppc_object
;
740 this->tocsave_loc_
.insert(loc
);
747 return this->tocsave_loc_
;
751 do_define_standard_symbols(Symbol_table
*, Layout
*);
753 // Finalize the sections.
755 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
757 // Return the value to use for a dynamic which requires special
760 do_dynsym_value(const Symbol
*) const;
762 // Return the PLT address to use for a local symbol.
764 do_plt_address_for_local(const Relobj
*, unsigned int) const;
766 // Return the PLT address to use for a global symbol.
768 do_plt_address_for_global(const Symbol
*) const;
770 // Return the offset to use for the GOT_INDX'th got entry which is
771 // for a local tls symbol specified by OBJECT, SYMNDX.
773 do_tls_offset_for_local(const Relobj
* object
,
775 unsigned int got_indx
) const;
777 // Return the offset to use for the GOT_INDX'th got entry which is
778 // for global tls symbol GSYM.
780 do_tls_offset_for_global(Symbol
* gsym
, unsigned int got_indx
) const;
783 do_function_location(Symbol_location
*) const;
786 do_can_check_for_function_pointers() const
789 // Adjust -fsplit-stack code which calls non-split-stack code.
791 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
792 section_offset_type fnoffset
, section_size_type fnsize
,
793 const unsigned char* prelocs
, size_t reloc_count
,
794 unsigned char* view
, section_size_type view_size
,
795 std::string
* from
, std::string
* to
) const;
797 // Relocate a section.
799 relocate_section(const Relocate_info
<size
, big_endian
>*,
800 unsigned int sh_type
,
801 const unsigned char* prelocs
,
803 Output_section
* output_section
,
804 bool needs_special_offset_handling
,
806 Address view_address
,
807 section_size_type view_size
,
808 const Reloc_symbol_changes
*);
810 // Scan the relocs during a relocatable link.
812 scan_relocatable_relocs(Symbol_table
* symtab
,
814 Sized_relobj_file
<size
, big_endian
>* object
,
815 unsigned int data_shndx
,
816 unsigned int sh_type
,
817 const unsigned char* prelocs
,
819 Output_section
* output_section
,
820 bool needs_special_offset_handling
,
821 size_t local_symbol_count
,
822 const unsigned char* plocal_symbols
,
823 Relocatable_relocs
*);
825 // Scan the relocs for --emit-relocs.
827 emit_relocs_scan(Symbol_table
* symtab
,
829 Sized_relobj_file
<size
, big_endian
>* object
,
830 unsigned int data_shndx
,
831 unsigned int sh_type
,
832 const unsigned char* prelocs
,
834 Output_section
* output_section
,
835 bool needs_special_offset_handling
,
836 size_t local_symbol_count
,
837 const unsigned char* plocal_syms
,
838 Relocatable_relocs
* rr
);
840 // Emit relocations for a section.
842 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
843 unsigned int sh_type
,
844 const unsigned char* prelocs
,
846 Output_section
* output_section
,
847 typename
elfcpp::Elf_types
<size
>::Elf_Off
848 offset_in_output_section
,
850 Address view_address
,
852 unsigned char* reloc_view
,
853 section_size_type reloc_view_size
);
855 // Return whether SYM is defined by the ABI.
857 do_is_defined_by_abi(const Symbol
* sym
) const
859 return strcmp(sym
->name(), "__tls_get_addr") == 0;
862 // Return the size of the GOT section.
866 gold_assert(this->got_
!= NULL
);
867 return this->got_
->data_size();
870 // Get the PLT section.
871 const Output_data_plt_powerpc
<size
, big_endian
>*
874 gold_assert(this->plt_
!= NULL
);
878 // Get the IPLT section.
879 const Output_data_plt_powerpc
<size
, big_endian
>*
882 gold_assert(this->iplt_
!= NULL
);
886 // Get the LPLT section.
887 const Output_data_plt_powerpc
<size
, big_endian
>*
893 // Return the plt offset and section for the given global sym.
895 plt_off(const Symbol
* gsym
,
896 const Output_data_plt_powerpc
<size
, big_endian
>** sec
) const
898 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
899 && gsym
->can_use_relative_reloc(false))
900 *sec
= this->iplt_section();
902 *sec
= this->plt_section();
903 return gsym
->plt_offset();
906 // Return the plt offset and section for the given local sym.
908 plt_off(const Sized_relobj_file
<size
, big_endian
>* relobj
,
909 unsigned int local_sym_index
,
910 const Output_data_plt_powerpc
<size
, big_endian
>** sec
) const
912 const Symbol_value
<size
>* lsym
= relobj
->local_symbol(local_sym_index
);
913 if (lsym
->is_ifunc_symbol())
914 *sec
= this->iplt_section();
916 *sec
= this->lplt_section();
917 return relobj
->local_plt_offset(local_sym_index
);
920 // Get the .glink section.
921 const Output_data_glink
<size
, big_endian
>*
922 glink_section() const
924 gold_assert(this->glink_
!= NULL
);
928 Output_data_glink
<size
, big_endian
>*
931 gold_assert(this->glink_
!= NULL
);
935 bool has_glink() const
936 { return this->glink_
!= NULL
; }
938 // Get the GOT section.
939 const Output_data_got_powerpc
<size
, big_endian
>*
942 gold_assert(this->got_
!= NULL
);
946 // Get the GOT section, creating it if necessary.
947 Output_data_got_powerpc
<size
, big_endian
>*
948 got_section(Symbol_table
*, Layout
*);
951 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
952 const elfcpp::Ehdr
<size
, big_endian
>&);
954 // Return the number of entries in the GOT.
956 got_entry_count() const
958 if (this->got_
== NULL
)
960 return this->got_size() / (size
/ 8);
963 // Return the number of entries in the PLT.
965 plt_entry_count() const;
967 // Return the offset of the first non-reserved PLT entry.
969 first_plt_entry_offset() const
973 if (this->abiversion() >= 2)
978 // Return the size of each PLT entry.
980 plt_entry_size() const
984 if (this->abiversion() >= 2)
989 Output_data_save_res
<size
, big_endian
>*
990 savres_section() const
992 return this->savres_section_
;
995 // Add any special sections for this symbol to the gc work list.
996 // For powerpc64, this adds the code section of a function
999 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
1001 // Handle target specific gc actions when adding a gc reference from
1002 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1003 // and DST_OFF. For powerpc64, this adds a referenc to the code
1004 // section of a function descriptor.
1006 do_gc_add_reference(Symbol_table
* symtab
,
1008 unsigned int src_shndx
,
1010 unsigned int dst_shndx
,
1011 Address dst_off
) const;
1013 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
1016 { return this->stub_tables_
; }
1018 const Output_data_brlt_powerpc
<size
, big_endian
>*
1019 brlt_section() const
1020 { return this->brlt_section_
; }
1023 add_branch_lookup_table(Address to
)
1025 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
1026 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
1030 find_branch_lookup_table(Address to
)
1032 typename
Branch_lookup_table::const_iterator p
1033 = this->branch_lookup_table_
.find(to
);
1034 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
1038 write_branch_lookup_table(unsigned char *oview
)
1040 for (typename
Branch_lookup_table::const_iterator p
1041 = this->branch_lookup_table_
.begin();
1042 p
!= this->branch_lookup_table_
.end();
1045 elfcpp::Swap
<size
, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
1049 // Wrapper used after relax to define a local symbol in output data,
1050 // from the end if value < 0.
1052 define_local(Symbol_table
* symtab
, const char* name
,
1053 Output_data
* od
, Address value
, unsigned int symsize
)
1056 = symtab
->define_in_output_data(name
, NULL
, Symbol_table::PREDEFINED
,
1057 od
, value
, symsize
, elfcpp::STT_NOTYPE
,
1058 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
, 0,
1059 static_cast<Signed_address
>(value
) < 0,
1061 // We are creating this symbol late, so need to fix up things
1062 // done early in Layout::finalize.
1063 sym
->set_dynsym_index(-1U);
1067 plt_thread_safe() const
1068 { return this->plt_thread_safe_
; }
1071 plt_localentry0() const
1072 { return this->plt_localentry0_
; }
1075 set_has_localentry0()
1077 this->has_localentry0_
= true;
1081 is_elfv2_localentry0(const Symbol
* gsym
) const
1084 && this->abiversion() >= 2
1085 && this->plt_localentry0()
1086 && gsym
->type() == elfcpp::STT_FUNC
1087 && gsym
->is_defined()
1088 && gsym
->nonvis() >> 3 == 0
1089 && !gsym
->non_zero_localentry());
1093 is_elfv2_localentry0(const Sized_relobj_file
<size
, big_endian
>* object
,
1094 unsigned int r_sym
) const
1096 const Powerpc_relobj
<size
, big_endian
>* ppc_object
1097 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
1100 && this->abiversion() >= 2
1101 && this->plt_localentry0()
1102 && ppc_object
->st_other(r_sym
) >> 5 == 0)
1104 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
1106 if (!psymval
->is_ifunc_symbol()
1107 && psymval
->input_shndx(&is_ordinary
) != elfcpp::SHN_UNDEF
1114 // Remember any symbols seen with non-zero localentry, even those
1115 // not providing a definition
1117 resolve(Symbol
* to
, const elfcpp::Sym
<size
, big_endian
>& sym
, Object
*,
1122 unsigned char st_other
= sym
.get_st_other();
1123 if ((st_other
& elfcpp::STO_PPC64_LOCAL_MASK
) != 0)
1124 to
->set_non_zero_localentry();
1126 // We haven't resolved anything, continue normal processing.
1132 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI
; }
1135 set_abiversion(int ver
)
1137 elfcpp::Elf_Word flags
= this->processor_specific_flags();
1138 flags
&= ~elfcpp::EF_PPC64_ABI
;
1139 flags
|= ver
& elfcpp::EF_PPC64_ABI
;
1140 this->set_processor_specific_flags(flags
);
1144 tls_get_addr_opt() const
1145 { return this->tls_get_addr_opt_
; }
1148 tls_get_addr() const
1149 { return this->tls_get_addr_
; }
1151 // If optimizing __tls_get_addr calls, whether this is the
1152 // "__tls_get_addr" symbol.
1154 is_tls_get_addr_opt(const Symbol
* gsym
) const
1156 return this->tls_get_addr_opt_
&& (gsym
== this->tls_get_addr_
1157 || gsym
== this->tls_get_addr_opt_
);
1161 replace_tls_get_addr(const Symbol
* gsym
) const
1162 { return this->tls_get_addr_opt_
&& gsym
== this->tls_get_addr_
; }
1165 set_has_tls_get_addr_opt()
1166 { this->has_tls_get_addr_opt_
= true; }
1168 // Offset to toc save stack slot
1171 { return this->abiversion() < 2 ? 40 : 24; }
1173 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1174 // so use the CR save slot. Used only by __tls_get_addr call stub,
1175 // relying on __tls_get_addr not saving CR itself.
1178 { return this->abiversion() < 2 ? 32 : 8; }
1180 // Merge object attributes from input object with those in the output.
1182 merge_object_attributes(const char*, const Attributes_section_data
*);
1198 : tls_get_addr_state_(NOT_EXPECTED
),
1199 relinfo_(NULL
), relnum_(0), r_offset_(0)
1204 if (this->tls_get_addr_state_
!= NOT_EXPECTED
)
1211 if (this->relinfo_
!= NULL
)
1212 gold_error_at_location(this->relinfo_
, this->relnum_
, this->r_offset_
,
1213 _("missing expected __tls_get_addr call"));
1217 expect_tls_get_addr_call(
1218 const Relocate_info
<size
, big_endian
>* relinfo
,
1222 this->tls_get_addr_state_
= EXPECTED
;
1223 this->relinfo_
= relinfo
;
1224 this->relnum_
= relnum
;
1225 this->r_offset_
= r_offset
;
1229 expect_tls_get_addr_call()
1230 { this->tls_get_addr_state_
= EXPECTED
; }
1233 skip_next_tls_get_addr_call()
1234 {this->tls_get_addr_state_
= SKIP
; }
1237 maybe_skip_tls_get_addr_call(Target_powerpc
<size
, big_endian
>* target
,
1238 unsigned int r_type
, const Symbol
* gsym
)
1240 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
1241 || r_type
== elfcpp::R_PPC_PLTREL24
1242 || is_plt16_reloc
<size
>(r_type
)
1243 || r_type
== elfcpp::R_POWERPC_PLTSEQ
1244 || r_type
== elfcpp::R_POWERPC_PLTCALL
)
1246 && (gsym
== target
->tls_get_addr()
1247 || gsym
== target
->tls_get_addr_opt()));
1248 Tls_get_addr last_tls
= this->tls_get_addr_state_
;
1249 this->tls_get_addr_state_
= NOT_EXPECTED
;
1250 if (is_tls_call
&& last_tls
!= EXPECTED
)
1252 else if (!is_tls_call
&& last_tls
!= NOT_EXPECTED
)
1261 // What we're up to regarding calls to __tls_get_addr.
1262 // On powerpc, the branch and link insn making a call to
1263 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1264 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1265 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
1266 // The marker relocation always comes first, and has the same
1267 // symbol as the reloc on the insn setting up the __tls_get_addr
1268 // argument. This ties the arg setup insn with the call insn,
1269 // allowing ld to safely optimize away the call. We check that
1270 // every call to __tls_get_addr has a marker relocation, and that
1271 // every marker relocation is on a call to __tls_get_addr.
1272 Tls_get_addr tls_get_addr_state_
;
1273 // Info about the last reloc for error message.
1274 const Relocate_info
<size
, big_endian
>* relinfo_
;
1279 // The class which scans relocations.
1280 class Scan
: protected Track_tls
1283 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1286 : Track_tls(), issued_non_pic_error_(false)
1290 get_reference_flags(unsigned int r_type
, const Target_powerpc
* target
);
1293 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
1294 Sized_relobj_file
<size
, big_endian
>* object
,
1295 unsigned int data_shndx
,
1296 Output_section
* output_section
,
1297 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
1298 const elfcpp::Sym
<size
, big_endian
>& lsym
,
1302 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
1303 Sized_relobj_file
<size
, big_endian
>* object
,
1304 unsigned int data_shndx
,
1305 Output_section
* output_section
,
1306 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
1310 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1312 Sized_relobj_file
<size
, big_endian
>* relobj
,
1315 const elfcpp::Rela
<size
, big_endian
>& ,
1316 unsigned int r_type
,
1317 const elfcpp::Sym
<size
, big_endian
>&)
1319 // PowerPC64 .opd is not folded, so any identical function text
1320 // may be folded and we'll still keep function addresses distinct.
1321 // That means no reloc is of concern here.
1324 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1325 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1326 if (ppcobj
->abiversion() == 1)
1329 // For 32-bit and ELFv2, conservatively assume anything but calls to
1330 // function code might be taking the address of the function.
1331 return !is_branch_reloc(r_type
);
1335 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1337 Sized_relobj_file
<size
, big_endian
>* relobj
,
1340 const elfcpp::Rela
<size
, big_endian
>& ,
1341 unsigned int r_type
,
1347 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1348 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1349 if (ppcobj
->abiversion() == 1)
1352 return !is_branch_reloc(r_type
);
1356 reloc_needs_plt_for_ifunc(Target_powerpc
<size
, big_endian
>* target
,
1357 Sized_relobj_file
<size
, big_endian
>* object
,
1358 unsigned int r_type
, bool report_err
);
1362 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
1363 unsigned int r_type
);
1366 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
1367 unsigned int r_type
, Symbol
*);
1370 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
1371 Target_powerpc
* target
);
1374 check_non_pic(Relobj
*, unsigned int r_type
);
1376 // Whether we have issued an error about a non-PIC compilation.
1377 bool issued_non_pic_error_
;
1381 symval_for_branch(const Symbol_table
* symtab
,
1382 const Sized_symbol
<size
>* gsym
,
1383 Powerpc_relobj
<size
, big_endian
>* object
,
1384 Address
*value
, unsigned int *dest_shndx
);
1386 // The class which implements relocation.
1387 class Relocate
: protected Track_tls
1390 // Use 'at' branch hints when true, 'y' when false.
1391 // FIXME maybe: set this with an option.
1392 static const bool is_isa_v2
= true;
1398 // Do a relocation. Return false if the caller should not issue
1399 // any warnings about this relocation.
1401 relocate(const Relocate_info
<size
, big_endian
>*, unsigned int,
1402 Target_powerpc
*, Output_section
*, size_t, const unsigned char*,
1403 const Sized_symbol
<size
>*, const Symbol_value
<size
>*,
1404 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1408 class Relocate_comdat_behavior
1411 // Decide what the linker should do for relocations that refer to
1412 // discarded comdat sections.
1413 inline Comdat_behavior
1414 get(const char* name
)
1416 gold::Default_comdat_behavior default_behavior
;
1417 Comdat_behavior ret
= default_behavior
.get(name
);
1418 if (ret
== CB_ERROR
)
1421 && (strcmp(name
, ".fixup") == 0
1422 || strcmp(name
, ".got2") == 0))
1425 && (strcmp(name
, ".opd") == 0
1426 || strcmp(name
, ".toc") == 0
1427 || strcmp(name
, ".toc1") == 0))
1434 // Optimize the TLS relocation type based on what we know about the
1435 // symbol. IS_FINAL is true if the final address of this symbol is
1436 // known at link time.
1438 tls::Tls_optimization
1439 optimize_tls_gd(bool is_final
)
1441 // If we are generating a shared library, then we can't do anything
1443 if (parameters
->options().shared()
1444 || !parameters
->options().tls_optimize())
1445 return tls::TLSOPT_NONE
;
1448 return tls::TLSOPT_TO_IE
;
1449 return tls::TLSOPT_TO_LE
;
1452 tls::Tls_optimization
1455 if (parameters
->options().shared()
1456 || !parameters
->options().tls_optimize())
1457 return tls::TLSOPT_NONE
;
1459 return tls::TLSOPT_TO_LE
;
1462 tls::Tls_optimization
1463 optimize_tls_ie(bool is_final
)
1466 || parameters
->options().shared()
1467 || !parameters
->options().tls_optimize())
1468 return tls::TLSOPT_NONE
;
1470 return tls::TLSOPT_TO_LE
;
1475 make_glink_section(Layout
*);
1477 // Create the PLT section.
1479 make_plt_section(Symbol_table
*, Layout
*);
1482 make_iplt_section(Symbol_table
*, Layout
*);
1485 make_lplt_section(Layout
*);
1488 make_brlt_section(Layout
*);
1490 // Create a PLT entry for a global symbol.
1492 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1494 // Create a PLT entry for a local IFUNC symbol.
1496 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1497 Sized_relobj_file
<size
, big_endian
>*,
1500 // Create a PLT entry for a local non-IFUNC symbol.
1502 make_local_plt_entry(Layout
*,
1503 Sized_relobj_file
<size
, big_endian
>*,
1507 // Create a GOT entry for local dynamic __tls_get_addr.
1509 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
1510 Sized_relobj_file
<size
, big_endian
>* object
);
1513 tlsld_got_offset() const
1515 return this->tlsld_got_offset_
;
1518 // Get the dynamic reloc section, creating it if necessary.
1520 rela_dyn_section(Layout
*);
1522 // Similarly, but for ifunc symbols get the one for ifunc.
1524 rela_dyn_section(Symbol_table
*, Layout
*, bool for_ifunc
);
1526 // Copy a relocation against a global symbol.
1528 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1529 Sized_relobj_file
<size
, big_endian
>* object
,
1530 unsigned int shndx
, Output_section
* output_section
,
1531 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
1533 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1534 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1535 symtab
->get_sized_symbol
<size
>(sym
),
1536 object
, shndx
, output_section
,
1537 r_type
, reloc
.get_r_offset(),
1538 reloc
.get_r_addend(),
1539 this->rela_dyn_section(layout
));
1542 // Look over all the input sections, deciding where to place stubs.
1544 group_sections(Layout
*, const Task
*, bool);
1546 // Sort output sections by address.
1547 struct Sort_sections
1550 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
1551 { return sec1
->address() < sec2
->address(); }
1557 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1558 unsigned int data_shndx
,
1560 unsigned int r_type
,
1563 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
1564 r_type_(r_type
), tocsave_ (0), r_sym_(r_sym
), addend_(addend
)
1570 // Return whether this branch is going via a plt call stub, and if
1571 // so, mark it as having an R_PPC64_TOCSAVE.
1573 mark_pltcall(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1574 unsigned int shndx
, Address offset
,
1575 Target_powerpc
* target
, Symbol_table
* symtab
);
1577 // If this branch needs a plt call stub, or a long branch stub, make one.
1579 make_stub(Stub_table
<size
, big_endian
>*,
1580 Stub_table
<size
, big_endian
>*,
1581 Symbol_table
*) const;
1584 // The branch location..
1585 Powerpc_relobj
<size
, big_endian
>* object_
;
1586 unsigned int shndx_
;
1588 // ..and the branch type and destination.
1589 unsigned int r_type_
: 31;
1590 unsigned int tocsave_
: 1;
1591 unsigned int r_sym_
;
1595 // Information about this specific target which we pass to the
1596 // general Target structure.
1597 static Target::Target_info powerpc_info
;
1599 // The types of GOT entries needed for this platform.
1600 // These values are exposed to the ABI in an incremental link.
1601 // Do not renumber existing values without changing the version
1602 // number of the .gnu_incremental_inputs section.
1606 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
1607 GOT_TYPE_DTPREL
, // entry for @got@dtprel
1608 GOT_TYPE_TPREL
// entry for @got@tprel
1612 Output_data_got_powerpc
<size
, big_endian
>* got_
;
1613 // The PLT section. This is a container for a table of addresses,
1614 // and their relocations. Each address in the PLT has a dynamic
1615 // relocation (R_*_JMP_SLOT) and each address will have a
1616 // corresponding entry in .glink for lazy resolution of the PLT.
1617 // ppc32 initialises the PLT to point at the .glink entry, while
1618 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1619 // linker adds a stub that loads the PLT entry into ctr then
1620 // branches to ctr. There may be more than one stub for each PLT
1621 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1622 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1623 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
1624 // The IPLT section. Like plt_, this is a container for a table of
1625 // addresses and their relocations, specifically for STT_GNU_IFUNC
1626 // functions that resolve locally (STT_GNU_IFUNC functions that
1627 // don't resolve locally go in PLT). Unlike plt_, these have no
1628 // entry in .glink for lazy resolution, and the relocation section
1629 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1630 // the relocation section may contain relocations against
1631 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1632 // relocation section will appear at the end of other dynamic
1633 // relocations, so that ld.so applies these relocations after other
1634 // dynamic relocations. In a static executable, the relocation
1635 // section is emitted and marked with __rela_iplt_start and
1636 // __rela_iplt_end symbols.
1637 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
1638 // A PLT style section for local, non-ifunc symbols
1639 Output_data_plt_powerpc
<size
, big_endian
>* lplt_
;
1640 // Section holding long branch destinations.
1641 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
1642 // The .glink section.
1643 Output_data_glink
<size
, big_endian
>* glink_
;
1644 // The dynamic reloc section.
1645 Reloc_section
* rela_dyn_
;
1646 // Relocs saved to avoid a COPY reloc.
1647 Powerpc_copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
1648 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1649 unsigned int tlsld_got_offset_
;
1651 Stub_tables stub_tables_
;
1652 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
1653 Branch_lookup_table branch_lookup_table_
;
1655 typedef std::vector
<Branch_info
> Branches
;
1656 Branches branch_info_
;
1657 Tocsave_loc tocsave_loc_
;
1659 bool plt_thread_safe_
;
1660 bool plt_localentry0_
;
1661 bool plt_localentry0_init_
;
1662 bool has_localentry0_
;
1663 bool has_tls_get_addr_opt_
;
1666 int relax_fail_count_
;
1667 int32_t stub_group_size_
;
1669 Output_data_save_res
<size
, big_endian
> *savres_section_
;
1671 // The "__tls_get_addr" symbol, if present
1672 Symbol
* tls_get_addr_
;
1673 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1674 Symbol
* tls_get_addr_opt_
;
1676 // Attributes in output.
1677 Attributes_section_data
* attributes_section_data_
;
1679 // Last input file to change various attribute tags
1680 const char* last_fp_
;
1681 const char* last_ld_
;
1682 const char* last_vec_
;
1683 const char* last_struct_
;
1687 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
1690 true, // is_big_endian
1691 elfcpp::EM_PPC
, // machine_code
1692 false, // has_make_symbol
1693 false, // has_resolve
1694 false, // has_code_fill
1695 true, // is_default_stack_executable
1696 false, // can_icf_inline_merge_sections
1698 "/usr/lib/ld.so.1", // dynamic_linker
1699 0x10000000, // default_text_segment_address
1700 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1701 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1702 false, // isolate_execinstr
1704 elfcpp::SHN_UNDEF
, // small_common_shndx
1705 elfcpp::SHN_UNDEF
, // large_common_shndx
1706 0, // small_common_section_flags
1707 0, // large_common_section_flags
1708 NULL
, // attributes_section
1709 NULL
, // attributes_vendor
1710 "_start", // entry_symbol_name
1711 32, // hash_entry_size
1712 elfcpp::SHT_PROGBITS
, // unwind_section_type
1716 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
1719 false, // is_big_endian
1720 elfcpp::EM_PPC
, // machine_code
1721 false, // has_make_symbol
1722 false, // has_resolve
1723 false, // has_code_fill
1724 true, // is_default_stack_executable
1725 false, // can_icf_inline_merge_sections
1727 "/usr/lib/ld.so.1", // dynamic_linker
1728 0x10000000, // default_text_segment_address
1729 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1730 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1731 false, // isolate_execinstr
1733 elfcpp::SHN_UNDEF
, // small_common_shndx
1734 elfcpp::SHN_UNDEF
, // large_common_shndx
1735 0, // small_common_section_flags
1736 0, // large_common_section_flags
1737 NULL
, // attributes_section
1738 NULL
, // attributes_vendor
1739 "_start", // entry_symbol_name
1740 32, // hash_entry_size
1741 elfcpp::SHT_PROGBITS
, // unwind_section_type
1745 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1748 true, // is_big_endian
1749 elfcpp::EM_PPC64
, // machine_code
1750 false, // has_make_symbol
1751 true, // has_resolve
1752 false, // has_code_fill
1753 false, // is_default_stack_executable
1754 false, // can_icf_inline_merge_sections
1756 "/usr/lib/ld.so.1", // dynamic_linker
1757 0x10000000, // default_text_segment_address
1758 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1759 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1760 false, // isolate_execinstr
1762 elfcpp::SHN_UNDEF
, // small_common_shndx
1763 elfcpp::SHN_UNDEF
, // large_common_shndx
1764 0, // small_common_section_flags
1765 0, // large_common_section_flags
1766 NULL
, // attributes_section
1767 NULL
, // attributes_vendor
1768 "_start", // entry_symbol_name
1769 32, // hash_entry_size
1770 elfcpp::SHT_PROGBITS
, // unwind_section_type
1774 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1777 false, // is_big_endian
1778 elfcpp::EM_PPC64
, // machine_code
1779 false, // has_make_symbol
1780 true, // has_resolve
1781 false, // has_code_fill
1782 false, // is_default_stack_executable
1783 false, // can_icf_inline_merge_sections
1785 "/usr/lib/ld.so.1", // dynamic_linker
1786 0x10000000, // default_text_segment_address
1787 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1788 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1789 false, // isolate_execinstr
1791 elfcpp::SHN_UNDEF
, // small_common_shndx
1792 elfcpp::SHN_UNDEF
, // large_common_shndx
1793 0, // small_common_section_flags
1794 0, // large_common_section_flags
1795 NULL
, // attributes_section
1796 NULL
, // attributes_vendor
1797 "_start", // entry_symbol_name
1798 32, // hash_entry_size
1799 elfcpp::SHT_PROGBITS
, // unwind_section_type
1803 is_branch_reloc(unsigned int r_type
)
1805 return (r_type
== elfcpp::R_POWERPC_REL24
1806 || r_type
== elfcpp::R_PPC_PLTREL24
1807 || r_type
== elfcpp::R_PPC_LOCAL24PC
1808 || r_type
== elfcpp::R_POWERPC_REL14
1809 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1810 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1811 || r_type
== elfcpp::R_POWERPC_ADDR24
1812 || r_type
== elfcpp::R_POWERPC_ADDR14
1813 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1814 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1817 // Reloc resolves to plt entry.
1820 is_plt16_reloc(unsigned int r_type
)
1822 return (r_type
== elfcpp::R_POWERPC_PLT16_LO
1823 || r_type
== elfcpp::R_POWERPC_PLT16_HI
1824 || r_type
== elfcpp::R_POWERPC_PLT16_HA
1825 || (size
== 64 && r_type
== elfcpp::R_PPC64_PLT16_LO_DS
));
1828 // If INSN is an opcode that may be used with an @tls operand, return
1829 // the transformed insn for TLS optimisation, otherwise return 0. If
1830 // REG is non-zero only match an insn with RB or RA equal to REG.
1832 at_tls_transform(uint32_t insn
, unsigned int reg
)
1834 if ((insn
& (0x3f << 26)) != 31 << 26)
1838 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1839 rtra
= insn
& ((1 << 26) - (1 << 16));
1840 else if (((insn
>> 16) & 0x1f) == reg
)
1841 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1845 if ((insn
& (0x3ff << 1)) == 266 << 1)
1848 else if ((insn
& (0x1f << 1)) == 23 << 1
1849 && ((insn
& (0x1f << 6)) < 14 << 6
1850 || ((insn
& (0x1f << 6)) >= 16 << 6
1851 && (insn
& (0x1f << 6)) < 24 << 6)))
1852 // load and store indexed -> dform
1853 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1854 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1855 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1856 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1857 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1859 insn
= (58 << 26) | 2;
1867 template<int size
, bool big_endian
>
1868 class Powerpc_relocate_functions
1888 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1889 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1890 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword SignedAddress
;
1892 template<int valsize
>
1894 has_overflow_signed(Address value
)
1896 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1897 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1898 limit
<<= ((valsize
- 1) >> 1);
1899 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1900 return value
+ limit
> (limit
<< 1) - 1;
1903 template<int valsize
>
1905 has_overflow_unsigned(Address value
)
1907 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1908 limit
<<= ((valsize
- 1) >> 1);
1909 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1910 return value
> (limit
<< 1) - 1;
1913 template<int valsize
>
1915 has_overflow_bitfield(Address value
)
1917 return (has_overflow_unsigned
<valsize
>(value
)
1918 && has_overflow_signed
<valsize
>(value
));
1921 template<int valsize
>
1922 static inline Status
1923 overflowed(Address value
, Overflow_check overflow
)
1925 if (overflow
== CHECK_SIGNED
)
1927 if (has_overflow_signed
<valsize
>(value
))
1928 return STATUS_OVERFLOW
;
1930 else if (overflow
== CHECK_UNSIGNED
)
1932 if (has_overflow_unsigned
<valsize
>(value
))
1933 return STATUS_OVERFLOW
;
1935 else if (overflow
== CHECK_BITFIELD
)
1937 if (has_overflow_bitfield
<valsize
>(value
))
1938 return STATUS_OVERFLOW
;
1943 // Do a simple RELA relocation
1944 template<int fieldsize
, int valsize
>
1945 static inline Status
1946 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1948 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1949 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1950 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, value
);
1951 return overflowed
<valsize
>(value
, overflow
);
1954 template<int fieldsize
, int valsize
>
1955 static inline Status
1956 rela(unsigned char* view
,
1957 unsigned int right_shift
,
1958 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1960 Overflow_check overflow
)
1962 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1963 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1964 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(wv
);
1965 Valtype reloc
= value
>> right_shift
;
1968 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, val
| reloc
);
1969 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1972 // Do a simple RELA relocation, unaligned.
1973 template<int fieldsize
, int valsize
>
1974 static inline Status
1975 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1977 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, value
);
1978 return overflowed
<valsize
>(value
, overflow
);
1981 template<int fieldsize
, int valsize
>
1982 static inline Status
1983 rela_ua(unsigned char* view
,
1984 unsigned int right_shift
,
1985 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1987 Overflow_check overflow
)
1989 typedef typename
elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::Valtype
1991 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(view
);
1992 Valtype reloc
= value
>> right_shift
;
1995 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, val
| reloc
);
1996 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
2000 // R_PPC64_ADDR64: (Symbol + Addend)
2002 addr64(unsigned char* view
, Address value
)
2003 { This::template rela
<64,64>(view
, value
, CHECK_NONE
); }
2005 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
2007 addr64_u(unsigned char* view
, Address value
)
2008 { This::template rela_ua
<64,64>(view
, value
, CHECK_NONE
); }
2010 // R_POWERPC_ADDR32: (Symbol + Addend)
2011 static inline Status
2012 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
2013 { return This::template rela
<32,32>(view
, value
, overflow
); }
2015 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
2016 static inline Status
2017 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
2018 { return This::template rela_ua
<32,32>(view
, value
, overflow
); }
2020 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
2021 static inline Status
2022 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
2024 Status stat
= This::template rela
<32,26>(view
, 0, 0x03fffffc,
2026 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
2027 stat
= STATUS_OVERFLOW
;
2031 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
2032 static inline Status
2033 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
2034 { return This::template rela
<16,16>(view
, value
, overflow
); }
2036 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
2037 static inline Status
2038 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
2039 { return This::template rela_ua
<16,16>(view
, value
, overflow
); }
2041 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
2042 static inline Status
2043 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
2045 Status stat
= This::template rela
<16,16>(view
, 0, 0xfffc, value
, overflow
);
2046 if ((value
& 3) != 0)
2047 stat
= STATUS_OVERFLOW
;
2051 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2052 static inline Status
2053 addr16_dq(unsigned char* view
, Address value
, Overflow_check overflow
)
2055 Status stat
= This::template rela
<16,16>(view
, 0, 0xfff0, value
, overflow
);
2056 if ((value
& 15) != 0)
2057 stat
= STATUS_OVERFLOW
;
2061 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2063 addr16_hi(unsigned char* view
, Address value
)
2064 { This::template rela
<16,16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
2066 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
2068 addr16_ha(unsigned char* view
, Address value
)
2069 { This::addr16_hi(view
, value
+ 0x8000); }
2071 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
2073 addr16_hi2(unsigned char* view
, Address value
)
2074 { This::template rela
<16,16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
2076 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
2078 addr16_ha2(unsigned char* view
, Address value
)
2079 { This::addr16_hi2(view
, value
+ 0x8000); }
2081 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
2083 addr16_hi3(unsigned char* view
, Address value
)
2084 { This::template rela
<16,16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
2086 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
2088 addr16_ha3(unsigned char* view
, Address value
)
2089 { This::addr16_hi3(view
, value
+ 0x8000); }
2091 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
2092 static inline Status
2093 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
2095 Status stat
= This::template rela
<32,16>(view
, 0, 0xfffc, value
, overflow
);
2096 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
2097 stat
= STATUS_OVERFLOW
;
2101 // R_POWERPC_REL16DX_HA
2102 static inline Status
2103 addr16dx_ha(unsigned char *view
, Address value
, Overflow_check overflow
)
2105 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
2106 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
2107 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
2109 value
= static_cast<SignedAddress
>(value
) >> 16;
2110 val
|= (value
& 0xffc1) | ((value
& 0x3e) << 15);
2111 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
2112 return overflowed
<16>(value
, overflow
);
2116 // Set ABI version for input and output.
2118 template<int size
, bool big_endian
>
2120 Powerpc_relobj
<size
, big_endian
>::set_abiversion(int ver
)
2122 this->e_flags_
|= ver
;
2123 if (this->abiversion() != 0)
2125 Target_powerpc
<size
, big_endian
>* target
=
2126 static_cast<Target_powerpc
<size
, big_endian
>*>(
2127 parameters
->sized_target
<size
, big_endian
>());
2128 if (target
->abiversion() == 0)
2129 target
->set_abiversion(this->abiversion());
2130 else if (target
->abiversion() != this->abiversion())
2131 gold_error(_("%s: ABI version %d is not compatible "
2132 "with ABI version %d output"),
2133 this->name().c_str(),
2134 this->abiversion(), target
->abiversion());
2139 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2140 // relocatable object, if such sections exists.
2142 template<int size
, bool big_endian
>
2144 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
2145 Read_symbols_data
* sd
)
2147 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2148 const unsigned char* namesu
= sd
->section_names
->data();
2149 const char* names
= reinterpret_cast<const char*>(namesu
);
2150 section_size_type names_size
= sd
->section_names_size
;
2151 const unsigned char* s
;
2153 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
,
2154 size
== 32 ? ".got2" : ".opd",
2155 names
, names_size
, NULL
);
2158 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
2159 this->special_
= ndx
;
2162 if (this->abiversion() == 0)
2163 this->set_abiversion(1);
2164 else if (this->abiversion() > 1)
2165 gold_error(_("%s: .opd invalid in abiv%d"),
2166 this->name().c_str(), this->abiversion());
2171 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".rela.toc",
2172 names
, names_size
, NULL
);
2175 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
2176 this->relatoc_
= ndx
;
2177 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2178 this->toc_
= this->adjust_shndx(shdr
.get_sh_info());
2181 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
2184 // Examine .rela.opd to build info about function entry points.
2186 template<int size
, bool big_endian
>
2188 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
2190 const unsigned char* prelocs
,
2191 const unsigned char* plocal_syms
)
2195 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
2196 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
2197 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2198 Address expected_off
= 0;
2199 bool regular
= true;
2200 unsigned int opd_ent_size
= 0;
2202 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
2204 Reltype
reloc(prelocs
);
2205 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
2206 = reloc
.get_r_info();
2207 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
2208 if (r_type
== elfcpp::R_PPC64_ADDR64
)
2210 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
2211 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
2214 if (r_sym
< this->local_symbol_count())
2216 typename
elfcpp::Sym
<size
, big_endian
>
2217 lsym(plocal_syms
+ r_sym
* sym_size
);
2218 shndx
= lsym
.get_st_shndx();
2219 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
2220 value
= lsym
.get_st_value();
2223 shndx
= this->symbol_section_and_value(r_sym
, &value
,
2225 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
2226 value
+ reloc
.get_r_addend());
2229 expected_off
= reloc
.get_r_offset();
2230 opd_ent_size
= expected_off
;
2232 else if (expected_off
!= reloc
.get_r_offset())
2234 expected_off
+= opd_ent_size
;
2236 else if (r_type
== elfcpp::R_PPC64_TOC
)
2238 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
2243 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2244 this->name().c_str(), r_type
);
2248 if (reloc_count
<= 2)
2249 opd_ent_size
= this->section_size(this->opd_shndx());
2250 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
2254 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2255 this->name().c_str());
2261 // Returns true if a code sequence loading the TOC entry at VALUE
2262 // relative to the TOC pointer can be converted into code calculating
2263 // a TOC pointer relative offset.
2264 // If so, the TOC pointer relative offset is stored to VALUE.
2266 template<int size
, bool big_endian
>
2268 Powerpc_relobj
<size
, big_endian
>::make_toc_relative(
2269 Target_powerpc
<size
, big_endian
>* target
,
2275 // With -mcmodel=medium code it is quite possible to have
2276 // toc-relative relocs referring to objects outside the TOC.
2277 // Don't try to look at a non-existent TOC.
2278 if (this->toc_shndx() == 0)
2281 // Convert VALUE back to an address by adding got_base (see below),
2282 // then to an offset in the TOC by subtracting the TOC output
2283 // section address and the TOC output offset. Since this TOC output
2284 // section and the got output section are one and the same, we can
2285 // omit adding and subtracting the output section address.
2286 Address off
= (*value
+ this->toc_base_offset()
2287 - this->output_section_offset(this->toc_shndx()));
2288 // Is this offset in the TOC? -mcmodel=medium code may be using
2289 // TOC relative access to variables outside the TOC. Those of
2290 // course can't be optimized. We also don't try to optimize code
2291 // that is using a different object's TOC.
2292 if (off
>= this->section_size(this->toc_shndx()))
2295 if (this->no_toc_opt(off
))
2298 section_size_type vlen
;
2299 unsigned char* view
= this->get_output_view(this->toc_shndx(), &vlen
);
2300 Address addr
= elfcpp::Swap
<size
, big_endian
>::readval(view
+ off
);
2302 Address got_base
= (target
->got_section()->output_section()->address()
2303 + this->toc_base_offset());
2305 if (addr
+ (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2312 // Perform the Sized_relobj_file method, then set up opd info from
2315 template<int size
, bool big_endian
>
2317 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
2319 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
2322 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
2323 p
!= rd
->relocs
.end();
2326 if (p
->data_shndx
== this->opd_shndx())
2328 uint64_t opd_size
= this->section_size(this->opd_shndx());
2329 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
2332 this->init_opd(opd_size
);
2333 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
2334 rd
->local_symbols
->data());
2342 // Read the symbols then set up st_other vector.
2344 template<int size
, bool big_endian
>
2346 Powerpc_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
2348 this->base_read_symbols(sd
);
2349 if (this->input_file()->format() != Input_file::FORMAT_ELF
)
2353 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2354 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2355 const unsigned int loccount
= this->do_local_symbol_count();
2358 this->st_other_
.resize(loccount
);
2359 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2360 off_t locsize
= loccount
* sym_size
;
2361 const unsigned int symtab_shndx
= this->symtab_shndx();
2362 const unsigned char *psymtab
= pshdrs
+ symtab_shndx
* shdr_size
;
2363 typename
elfcpp::Shdr
<size
, big_endian
> shdr(psymtab
);
2364 const unsigned char* psyms
= this->get_view(shdr
.get_sh_offset(),
2365 locsize
, true, false);
2367 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
2369 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
2370 unsigned char st_other
= sym
.get_st_other();
2371 this->st_other_
[i
] = st_other
;
2372 if ((st_other
& elfcpp::STO_PPC64_LOCAL_MASK
) != 0)
2374 if (this->abiversion() == 0)
2375 this->set_abiversion(2);
2376 else if (this->abiversion() < 2)
2377 gold_error(_("%s: local symbol %d has invalid st_other"
2378 " for ABI version 1"),
2379 this->name().c_str(), i
);
2385 const size_t shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2386 const unsigned char* ps
= sd
->section_headers
->data() + shdr_size
;
2387 bool merge_attributes
= false;
2388 for (unsigned int i
= 1; i
< this->shnum(); ++i
, ps
+= shdr_size
)
2390 elfcpp::Shdr
<size
, big_endian
> shdr(ps
);
2391 switch (shdr
.get_sh_type())
2393 case elfcpp::SHT_GNU_ATTRIBUTES
:
2395 gold_assert(this->attributes_section_data_
== NULL
);
2396 section_offset_type section_offset
= shdr
.get_sh_offset();
2397 section_size_type section_size
=
2398 convert_to_section_size_type(shdr
.get_sh_size());
2399 const unsigned char* view
=
2400 this->get_view(section_offset
, section_size
, true, false);
2401 this->attributes_section_data_
=
2402 new Attributes_section_data(view
, section_size
);
2406 case elfcpp::SHT_SYMTAB
:
2408 // Sometimes an object has no contents except the section
2409 // name string table and an empty symbol table with the
2410 // undefined symbol. We don't want to merge
2411 // processor-specific flags from such an object.
2412 const typename
elfcpp::Elf_types
<size
>::Elf_WXword sym_size
=
2413 elfcpp::Elf_sizes
<size
>::sym_size
;
2414 if (shdr
.get_sh_size() > sym_size
)
2415 merge_attributes
= true;
2419 case elfcpp::SHT_STRTAB
:
2423 merge_attributes
= true;
2428 if (!merge_attributes
)
2430 // Should rarely happen.
2431 delete this->attributes_section_data_
;
2432 this->attributes_section_data_
= NULL
;
2436 template<int size
, bool big_endian
>
2438 Powerpc_dynobj
<size
, big_endian
>::set_abiversion(int ver
)
2440 this->e_flags_
|= ver
;
2441 if (this->abiversion() != 0)
2443 Target_powerpc
<size
, big_endian
>* target
=
2444 static_cast<Target_powerpc
<size
, big_endian
>*>(
2445 parameters
->sized_target
<size
, big_endian
>());
2446 if (target
->abiversion() == 0)
2447 target
->set_abiversion(this->abiversion());
2448 else if (target
->abiversion() != this->abiversion())
2449 gold_error(_("%s: ABI version %d is not compatible "
2450 "with ABI version %d output"),
2451 this->name().c_str(),
2452 this->abiversion(), target
->abiversion());
2457 // Call Sized_dynobj::base_read_symbols to read the symbols then
2458 // read .opd from a dynamic object, filling in opd_ent_ vector,
2460 template<int size
, bool big_endian
>
2462 Powerpc_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
2464 this->base_read_symbols(sd
);
2465 const size_t shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
2466 const unsigned char* ps
=
2467 sd
->section_headers
->data() + shdr_size
* (this->shnum() - 1);
2468 for (unsigned int i
= this->shnum(); i
> 0; --i
, ps
-= shdr_size
)
2470 elfcpp::Shdr
<size
, big_endian
> shdr(ps
);
2471 if (shdr
.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES
)
2473 section_offset_type section_offset
= shdr
.get_sh_offset();
2474 section_size_type section_size
=
2475 convert_to_section_size_type(shdr
.get_sh_size());
2476 const unsigned char* view
=
2477 this->get_view(section_offset
, section_size
, true, false);
2478 this->attributes_section_data_
=
2479 new Attributes_section_data(view
, section_size
);
2485 const unsigned char* const pshdrs
= sd
->section_headers
->data();
2486 const unsigned char* namesu
= sd
->section_names
->data();
2487 const char* names
= reinterpret_cast<const char*>(namesu
);
2488 const unsigned char* s
= NULL
;
2489 const unsigned char* opd
;
2490 section_size_type opd_size
;
2492 // Find and read .opd section.
2495 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".opd", names
,
2496 sd
->section_names_size
,
2501 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2502 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2503 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
2505 if (this->abiversion() == 0)
2506 this->set_abiversion(1);
2507 else if (this->abiversion() > 1)
2508 gold_error(_("%s: .opd invalid in abiv%d"),
2509 this->name().c_str(), this->abiversion());
2511 this->opd_shndx_
= (s
- pshdrs
) / shdr_size
;
2512 this->opd_address_
= shdr
.get_sh_addr();
2513 opd_size
= convert_to_section_size_type(shdr
.get_sh_size());
2514 opd
= this->get_view(shdr
.get_sh_offset(), opd_size
,
2520 // Build set of executable sections.
2521 // Using a set is probably overkill. There is likely to be only
2522 // a few executable sections, typically .init, .text and .fini,
2523 // and they are generally grouped together.
2524 typedef std::set
<Sec_info
> Exec_sections
;
2525 Exec_sections exec_sections
;
2527 for (unsigned int i
= 1; i
< this->shnum(); ++i
, s
+= shdr_size
)
2529 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2530 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2531 && ((shdr
.get_sh_flags()
2532 & (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2533 == (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2534 && shdr
.get_sh_size() != 0)
2536 exec_sections
.insert(Sec_info(shdr
.get_sh_addr(),
2537 shdr
.get_sh_size(), i
));
2540 if (exec_sections
.empty())
2543 // Look over the OPD entries. This is complicated by the fact
2544 // that some binaries will use two-word entries while others
2545 // will use the standard three-word entries. In most cases
2546 // the third word (the environment pointer for languages like
2547 // Pascal) is unused and will be zero. If the third word is
2548 // used it should not be pointing into executable sections,
2550 this->init_opd(opd_size
);
2551 for (const unsigned char* p
= opd
; p
< opd
+ opd_size
; p
+= 8)
2553 typedef typename
elfcpp::Swap
<64, big_endian
>::Valtype Valtype
;
2554 const Valtype
* valp
= reinterpret_cast<const Valtype
*>(p
);
2555 Valtype val
= elfcpp::Swap
<64, big_endian
>::readval(valp
);
2557 // Chances are that this is the third word of an OPD entry.
2559 typename
Exec_sections::const_iterator e
2560 = exec_sections
.upper_bound(Sec_info(val
, 0, 0));
2561 if (e
!= exec_sections
.begin())
2564 if (e
->start
<= val
&& val
< e
->start
+ e
->len
)
2566 // We have an address in an executable section.
2567 // VAL ought to be the function entry, set it up.
2568 this->set_opd_ent(p
- opd
, e
->shndx
, val
);
2569 // Skip second word of OPD entry, the TOC pointer.
2573 // If we didn't match any executable sections, we likely
2574 // have a non-zero third word in the OPD entry.
2579 // Relocate sections.
2581 template<int size
, bool big_endian
>
2583 Powerpc_relobj
<size
, big_endian
>::do_relocate_sections(
2584 const Symbol_table
* symtab
, const Layout
* layout
,
2585 const unsigned char* pshdrs
, Output_file
* of
,
2586 typename Sized_relobj_file
<size
, big_endian
>::Views
* pviews
)
2588 unsigned int start
= 1;
2590 && this->relatoc_
!= 0
2591 && !parameters
->options().relocatable())
2593 // Relocate .toc first.
2594 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2595 this->relatoc_
, this->relatoc_
);
2596 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2597 1, this->relatoc_
- 1);
2598 start
= this->relatoc_
+ 1;
2600 this->relocate_section_range(symtab
, layout
, pshdrs
, of
, pviews
,
2601 start
, this->shnum() - 1);
2603 if (!parameters
->options().output_is_position_independent())
2605 Target_powerpc
<size
, big_endian
>* target
2606 = static_cast<Target_powerpc
<size
, big_endian
>*>(
2607 parameters
->sized_target
<size
, big_endian
>());
2608 if (target
->lplt_section() && target
->lplt_section()->data_size() != 0)
2610 const section_size_type offset
= target
->lplt_section()->offset();
2611 const section_size_type oview_size
2612 = convert_to_section_size_type(target
->lplt_section()->data_size());
2613 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2615 bool modified
= false;
2616 unsigned int nsyms
= this->local_symbol_count();
2617 for (unsigned int i
= 0; i
< nsyms
; i
++)
2618 if (this->local_has_plt_offset(i
))
2620 Address value
= this->local_symbol_value(i
, 0);
2622 value
+= ppc64_local_entry_offset(i
);
2623 size_t off
= this->local_plt_offset(i
);
2624 elfcpp::Swap
<size
, big_endian
>::writeval(oview
+ off
, value
);
2628 of
->write_output_view(offset
, oview_size
, oview
);
2633 // Set up some symbols.
2635 template<int size
, bool big_endian
>
2637 Target_powerpc
<size
, big_endian
>::do_define_standard_symbols(
2638 Symbol_table
* symtab
,
2643 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2644 // undefined when scanning relocs (and thus requires
2645 // non-relative dynamic relocs). The proper value will be
2647 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2648 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2650 Target_powerpc
<size
, big_endian
>* target
=
2651 static_cast<Target_powerpc
<size
, big_endian
>*>(
2652 parameters
->sized_target
<size
, big_endian
>());
2653 Output_data_got_powerpc
<size
, big_endian
>* got
2654 = target
->got_section(symtab
, layout
);
2655 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2656 Symbol_table::PREDEFINED
,
2660 elfcpp::STV_HIDDEN
, 0,
2664 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2665 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
2666 if (sdasym
!= NULL
&& sdasym
->is_undefined())
2668 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
2670 = layout
->add_output_section_data(".sdata", 0,
2672 | elfcpp::SHF_WRITE
,
2673 sdata
, ORDER_SMALL_DATA
, false);
2674 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
2675 Symbol_table::PREDEFINED
,
2676 os
, 32768, 0, elfcpp::STT_OBJECT
,
2677 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
2683 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2684 Symbol
*gotsym
= symtab
->lookup(".TOC.", NULL
);
2685 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2687 Target_powerpc
<size
, big_endian
>* target
=
2688 static_cast<Target_powerpc
<size
, big_endian
>*>(
2689 parameters
->sized_target
<size
, big_endian
>());
2690 Output_data_got_powerpc
<size
, big_endian
>* got
2691 = target
->got_section(symtab
, layout
);
2692 symtab
->define_in_output_data(".TOC.", NULL
,
2693 Symbol_table::PREDEFINED
,
2697 elfcpp::STV_HIDDEN
, 0,
2702 this->tls_get_addr_
= symtab
->lookup("__tls_get_addr");
2703 if (parameters
->options().tls_get_addr_optimize()
2704 && this->tls_get_addr_
!= NULL
2705 && this->tls_get_addr_
->in_reg())
2706 this->tls_get_addr_opt_
= symtab
->lookup("__tls_get_addr_opt");
2707 if (this->tls_get_addr_opt_
!= NULL
)
2709 if (this->tls_get_addr_
->is_undefined()
2710 || this->tls_get_addr_
->is_from_dynobj())
2712 // Make it seem as if references to __tls_get_addr are
2713 // really to __tls_get_addr_opt, so the latter symbol is
2714 // made dynamic, not the former.
2715 this->tls_get_addr_
->clear_in_reg();
2716 this->tls_get_addr_opt_
->set_in_reg();
2718 // We have a non-dynamic definition for __tls_get_addr.
2719 // Make __tls_get_addr_opt the same, if it does not already have
2720 // a non-dynamic definition.
2721 else if (this->tls_get_addr_opt_
->is_undefined()
2722 || this->tls_get_addr_opt_
->is_from_dynobj())
2724 Sized_symbol
<size
>* from
2725 = static_cast<Sized_symbol
<size
>*>(this->tls_get_addr_
);
2726 Sized_symbol
<size
>* to
2727 = static_cast<Sized_symbol
<size
>*>(this->tls_get_addr_opt_
);
2728 symtab
->clone
<size
>(to
, from
);
2733 // Set up PowerPC target specific relobj.
2735 template<int size
, bool big_endian
>
2737 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
2738 const std::string
& name
,
2739 Input_file
* input_file
,
2740 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
2742 int et
= ehdr
.get_e_type();
2743 // ET_EXEC files are valid input for --just-symbols/-R,
2744 // and we treat them as relocatable objects.
2745 if (et
== elfcpp::ET_REL
2746 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
2748 Powerpc_relobj
<size
, big_endian
>* obj
=
2749 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2753 else if (et
== elfcpp::ET_DYN
)
2755 Powerpc_dynobj
<size
, big_endian
>* obj
=
2756 new Powerpc_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2762 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
2767 template<int size
, bool big_endian
>
2768 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
2771 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
2772 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
2774 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
2775 : Output_data_got
<size
, big_endian
>(),
2776 symtab_(symtab
), layout_(layout
),
2777 header_ent_cnt_(size
== 32 ? 3 : 1),
2778 header_index_(size
== 32 ? 0x2000 : 0)
2781 this->set_addralign(256);
2784 // Override all the Output_data_got methods we use so as to first call
2787 add_global(Symbol
* gsym
, unsigned int got_type
)
2789 this->reserve_ent();
2790 return Output_data_got
<size
, big_endian
>::add_global(gsym
, got_type
);
2794 add_global_plt(Symbol
* gsym
, unsigned int got_type
)
2796 this->reserve_ent();
2797 return Output_data_got
<size
, big_endian
>::add_global_plt(gsym
, got_type
);
2801 add_global_tls(Symbol
* gsym
, unsigned int got_type
)
2802 { return this->add_global_plt(gsym
, got_type
); }
2805 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
2806 Output_data_reloc_generic
* rel_dyn
, unsigned int r_type
)
2808 this->reserve_ent();
2809 Output_data_got
<size
, big_endian
>::
2810 add_global_with_rel(gsym
, got_type
, rel_dyn
, r_type
);
2814 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
2815 Output_data_reloc_generic
* rel_dyn
,
2816 unsigned int r_type_1
, unsigned int r_type_2
)
2818 if (gsym
->has_got_offset(got_type
))
2821 this->reserve_ent(2);
2822 Output_data_got
<size
, big_endian
>::
2823 add_global_pair_with_rel(gsym
, got_type
, rel_dyn
, r_type_1
, r_type_2
);
2827 add_local(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2829 this->reserve_ent();
2830 return Output_data_got
<size
, big_endian
>::add_local(object
, sym_index
,
2835 add_local_plt(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2837 this->reserve_ent();
2838 return Output_data_got
<size
, big_endian
>::add_local_plt(object
, sym_index
,
2843 add_local_tls(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2844 { return this->add_local_plt(object
, sym_index
, got_type
); }
2847 add_local_tls_pair(Relobj
* object
, unsigned int sym_index
,
2848 unsigned int got_type
,
2849 Output_data_reloc_generic
* rel_dyn
,
2850 unsigned int r_type
)
2852 if (object
->local_has_got_offset(sym_index
, got_type
))
2855 this->reserve_ent(2);
2856 Output_data_got
<size
, big_endian
>::
2857 add_local_tls_pair(object
, sym_index
, got_type
, rel_dyn
, r_type
);
2861 add_constant(Valtype constant
)
2863 this->reserve_ent();
2864 return Output_data_got
<size
, big_endian
>::add_constant(constant
);
2868 add_constant_pair(Valtype c1
, Valtype c2
)
2870 this->reserve_ent(2);
2871 return Output_data_got
<size
, big_endian
>::add_constant_pair(c1
, c2
);
2874 // Offset of _GLOBAL_OFFSET_TABLE_.
2878 return this->got_offset(this->header_index_
);
2881 // Offset of base used to access the GOT/TOC.
2882 // The got/toc pointer reg will be set to this value.
2884 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
2887 return this->g_o_t();
2889 return (this->output_section()->address()
2890 + object
->toc_base_offset()
2894 // Ensure our GOT has a header.
2896 set_final_data_size()
2898 if (this->header_ent_cnt_
!= 0)
2899 this->make_header();
2900 Output_data_got
<size
, big_endian
>::set_final_data_size();
2903 // First word of GOT header needs some values that are not
2904 // handled by Output_data_got so poke them in here.
2905 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2907 do_write(Output_file
* of
)
2910 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
2911 val
= this->layout_
->dynamic_section()->address();
2913 val
= this->output_section()->address() + 0x8000;
2914 this->replace_constant(this->header_index_
, val
);
2915 Output_data_got
<size
, big_endian
>::do_write(of
);
2920 reserve_ent(unsigned int cnt
= 1)
2922 if (this->header_ent_cnt_
== 0)
2924 if (this->num_entries() + cnt
> this->header_index_
)
2925 this->make_header();
2931 this->header_ent_cnt_
= 0;
2932 this->header_index_
= this->num_entries();
2935 Output_data_got
<size
, big_endian
>::add_constant(0);
2936 Output_data_got
<size
, big_endian
>::add_constant(0);
2937 Output_data_got
<size
, big_endian
>::add_constant(0);
2939 // Define _GLOBAL_OFFSET_TABLE_ at the header
2940 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2943 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
2944 sym
->set_value(this->g_o_t());
2947 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2948 Symbol_table::PREDEFINED
,
2949 this, this->g_o_t(), 0,
2952 elfcpp::STV_HIDDEN
, 0,
2956 Output_data_got
<size
, big_endian
>::add_constant(0);
2959 // Stashed pointers.
2960 Symbol_table
* symtab_
;
2964 unsigned int header_ent_cnt_
;
2965 // GOT header index.
2966 unsigned int header_index_
;
2969 // Get the GOT section, creating it if necessary.
2971 template<int size
, bool big_endian
>
2972 Output_data_got_powerpc
<size
, big_endian
>*
2973 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
2976 if (this->got_
== NULL
)
2978 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2981 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
2983 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2984 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2985 this->got_
, ORDER_DATA
, false);
2991 // Get the dynamic reloc section, creating it if necessary.
2993 template<int size
, bool big_endian
>
2994 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2995 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
2997 if (this->rela_dyn_
== NULL
)
2999 gold_assert(layout
!= NULL
);
3000 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
3001 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
3002 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
3003 ORDER_DYNAMIC_RELOCS
, false);
3005 return this->rela_dyn_
;
3008 // Similarly, but for ifunc symbols get the one for ifunc.
3010 template<int size
, bool big_endian
>
3011 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
3012 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Symbol_table
* symtab
,
3017 return this->rela_dyn_section(layout
);
3019 if (this->iplt_
== NULL
)
3020 this->make_iplt_section(symtab
, layout
);
3021 return this->iplt_
->rel_plt();
3027 // Determine the stub group size. The group size is the absolute
3028 // value of the parameter --stub-group-size. If --stub-group-size
3029 // is passed a negative value, we restrict stubs to be always after
3030 // the stubbed branches.
3031 Stub_control(int32_t size
, bool no_size_errors
, bool multi_os
)
3032 : stub_group_size_(abs(size
)), stubs_always_after_branch_(size
< 0),
3033 suppress_size_errors_(no_size_errors
), multi_os_(multi_os
),
3034 state_(NO_GROUP
), group_size_(0), group_start_addr_(0),
3035 owner_(NULL
), output_section_(NULL
)
3039 // Return true iff input section can be handled by current stub
3042 can_add_to_stub_group(Output_section
* o
,
3043 const Output_section::Input_section
* i
,
3046 const Output_section::Input_section
*
3052 { return output_section_
; }
3055 set_output_and_owner(Output_section
* o
,
3056 const Output_section::Input_section
* i
)
3058 this->output_section_
= o
;
3067 // Adding group sections before the stubs.
3068 FINDING_STUB_SECTION
,
3069 // Adding group sections after the stubs.
3073 uint32_t stub_group_size_
;
3074 bool stubs_always_after_branch_
;
3075 bool suppress_size_errors_
;
3076 // True if a stub group can serve multiple output sections.
3079 // Current max size of group. Starts at stub_group_size_ but is
3080 // reduced to stub_group_size_/1024 on seeing a section with
3081 // external conditional branches.
3082 uint32_t group_size_
;
3083 uint64_t group_start_addr_
;
3084 // owner_ and output_section_ specify the section to which stubs are
3085 // attached. The stubs are placed at the end of this section.
3086 const Output_section::Input_section
* owner_
;
3087 Output_section
* output_section_
;
3090 // Return true iff input section can be handled by current stub
3091 // group. Sections are presented to this function in order,
3092 // so the first section is the head of the group.
3095 Stub_control::can_add_to_stub_group(Output_section
* o
,
3096 const Output_section::Input_section
* i
,
3099 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
3101 uint64_t start_addr
= o
->address();
3104 // .init and .fini sections are pasted together to form a single
3105 // function. We can't be adding stubs in the middle of the function.
3106 this_size
= o
->data_size();
3109 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
3110 this_size
= i
->data_size();
3113 uint64_t end_addr
= start_addr
+ this_size
;
3114 uint32_t group_size
= this->stub_group_size_
;
3116 this->group_size_
= group_size
= group_size
>> 10;
3118 if (this_size
> group_size
&& !this->suppress_size_errors_
)
3119 gold_warning(_("%s:%s exceeds group size"),
3120 i
->relobj()->name().c_str(),
3121 i
->relobj()->section_name(i
->shndx()).c_str());
3123 gold_debug(DEBUG_TARGET
, "maybe add%s %s:%s size=%#llx total=%#llx",
3124 has14
? " 14bit" : "",
3125 i
->relobj()->name().c_str(),
3126 i
->relobj()->section_name(i
->shndx()).c_str(),
3127 (long long) this_size
,
3128 (this->state_
== NO_GROUP
3130 : (long long) end_addr
- this->group_start_addr_
));
3132 if (this->state_
== NO_GROUP
)
3134 // Only here on very first use of Stub_control
3136 this->output_section_
= o
;
3137 this->state_
= FINDING_STUB_SECTION
;
3138 this->group_size_
= group_size
;
3139 this->group_start_addr_
= start_addr
;
3142 else if (!this->multi_os_
&& this->output_section_
!= o
)
3144 else if (this->state_
== HAS_STUB_SECTION
)
3146 // Can we add this section, which is after the stubs, to the
3148 if (end_addr
- this->group_start_addr_
<= this->group_size_
)
3151 else if (this->state_
== FINDING_STUB_SECTION
)
3153 if ((whole_sec
&& this->output_section_
== o
)
3154 || end_addr
- this->group_start_addr_
<= this->group_size_
)
3156 // Stubs are added at the end of "owner_".
3158 this->output_section_
= o
;
3161 // The group before the stubs has reached maximum size.
3162 // Now see about adding sections after the stubs to the
3163 // group. If the current section has a 14-bit branch and
3164 // the group before the stubs exceeds group_size_ (because
3165 // they didn't have 14-bit branches), don't add sections
3166 // after the stubs: The size of stubs for such a large
3167 // group may exceed the reach of a 14-bit branch.
3168 if (!this->stubs_always_after_branch_
3169 && this_size
<= this->group_size_
3170 && start_addr
- this->group_start_addr_
<= this->group_size_
)
3172 gold_debug(DEBUG_TARGET
, "adding after stubs");
3173 this->state_
= HAS_STUB_SECTION
;
3174 this->group_start_addr_
= start_addr
;
3181 gold_debug(DEBUG_TARGET
,
3182 !this->multi_os_
&& this->output_section_
!= o
3183 ? "nope, new output section\n"
3184 : "nope, didn't fit\n");
3186 // The section fails to fit in the current group. Set up a few
3187 // things for the next group. owner_ and output_section_ will be
3188 // set later after we've retrieved those values for the current
3190 this->state_
= FINDING_STUB_SECTION
;
3191 this->group_size_
= group_size
;
3192 this->group_start_addr_
= start_addr
;
3196 // Look over all the input sections, deciding where to place stubs.
3198 template<int size
, bool big_endian
>
3200 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
3202 bool no_size_errors
)
3204 Stub_control
stub_control(this->stub_group_size_
, no_size_errors
,
3205 parameters
->options().stub_group_multi());
3207 // Group input sections and insert stub table
3208 Stub_table_owner
* table_owner
= NULL
;
3209 std::vector
<Stub_table_owner
*> tables
;
3210 Layout::Section_list section_list
;
3211 layout
->get_executable_sections(§ion_list
);
3212 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
3213 for (Layout::Section_list::iterator o
= section_list
.begin();
3214 o
!= section_list
.end();
3217 typedef Output_section::Input_section_list Input_section_list
;
3218 for (Input_section_list::const_iterator i
3219 = (*o
)->input_sections().begin();
3220 i
!= (*o
)->input_sections().end();
3223 if (i
->is_input_section()
3224 || i
->is_relaxed_input_section())
3226 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3227 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
3228 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
3229 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
3231 table_owner
->output_section
= stub_control
.output_section();
3232 table_owner
->owner
= stub_control
.owner();
3233 stub_control
.set_output_and_owner(*o
, &*i
);
3236 if (table_owner
== NULL
)
3238 table_owner
= new Stub_table_owner
;
3239 tables
.push_back(table_owner
);
3241 ppcobj
->set_stub_table(i
->shndx(), tables
.size() - 1);
3245 if (table_owner
!= NULL
)
3247 table_owner
->output_section
= stub_control
.output_section();
3248 table_owner
->owner
= stub_control
.owner();;
3250 for (typename
std::vector
<Stub_table_owner
*>::iterator t
= tables
.begin();
3254 Stub_table
<size
, big_endian
>* stub_table
;
3256 if ((*t
)->owner
->is_input_section())
3257 stub_table
= new Stub_table
<size
, big_endian
>(this,
3258 (*t
)->output_section
,
3260 this->stub_tables_
.size());
3261 else if ((*t
)->owner
->is_relaxed_input_section())
3262 stub_table
= static_cast<Stub_table
<size
, big_endian
>*>(
3263 (*t
)->owner
->relaxed_input_section());
3266 this->stub_tables_
.push_back(stub_table
);
3271 static unsigned long
3272 max_branch_delta (unsigned int r_type
)
3274 if (r_type
== elfcpp::R_POWERPC_REL14
3275 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
3276 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
3278 if (r_type
== elfcpp::R_POWERPC_REL24
3279 || r_type
== elfcpp::R_PPC_PLTREL24
3280 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
3285 // Return whether this branch is going via a plt call stub.
3287 template<int size
, bool big_endian
>
3289 Target_powerpc
<size
, big_endian
>::Branch_info::mark_pltcall(
3290 Powerpc_relobj
<size
, big_endian
>* ppc_object
,
3293 Target_powerpc
* target
,
3294 Symbol_table
* symtab
)
3296 if (this->object_
!= ppc_object
3297 || this->shndx_
!= shndx
3298 || this->offset_
!= offset
)
3301 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
3302 if (sym
!= NULL
&& sym
->is_forwarder())
3303 sym
= symtab
->resolve_forwards(sym
);
3304 if (target
->replace_tls_get_addr(sym
))
3305 sym
= target
->tls_get_addr_opt();
3306 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
3308 ? (gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
3309 && !target
->is_elfv2_localentry0(gsym
))
3310 : (this->object_
->local_has_plt_offset(this->r_sym_
)
3311 && !target
->is_elfv2_localentry0(this->object_
, this->r_sym_
)))
3319 // If this branch needs a plt call stub, or a long branch stub, make one.
3321 template<int size
, bool big_endian
>
3323 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
3324 Stub_table
<size
, big_endian
>* stub_table
,
3325 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
3326 Symbol_table
* symtab
) const
3328 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
3329 Target_powerpc
<size
, big_endian
>* target
=
3330 static_cast<Target_powerpc
<size
, big_endian
>*>(
3331 parameters
->sized_target
<size
, big_endian
>());
3332 if (sym
!= NULL
&& sym
->is_forwarder())
3333 sym
= symtab
->resolve_forwards(sym
);
3334 if (target
->replace_tls_get_addr(sym
))
3335 sym
= target
->tls_get_addr_opt();
3336 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
3340 ? gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
3341 : this->object_
->local_has_plt_offset(this->r_sym_
))
3345 && target
->abiversion() >= 2
3346 && !parameters
->options().output_is_position_independent()
3347 && !is_branch_reloc(this->r_type_
))
3348 target
->glink_section()->add_global_entry(gsym
);
3351 if (stub_table
== NULL
3354 && !parameters
->options().output_is_position_independent()
3355 && !is_branch_reloc(this->r_type_
)))
3356 stub_table
= this->object_
->stub_table(this->shndx_
);
3357 if (stub_table
== NULL
)
3359 // This is a ref from a data section to an ifunc symbol,
3360 // or a non-branch reloc for which we always want to use
3361 // one set of stubs for resolving function addresses.
3362 stub_table
= ifunc_stub_table
;
3364 gold_assert(stub_table
!= NULL
);
3365 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
3366 if (from
!= invalid_address
)
3367 from
+= (this->object_
->output_section(this->shndx_
)->address()
3370 ok
= stub_table
->add_plt_call_entry(from
,
3371 this->object_
, gsym
,
3372 this->r_type_
, this->addend_
,
3375 ok
= stub_table
->add_plt_call_entry(from
,
3376 this->object_
, this->r_sym_
,
3377 this->r_type_
, this->addend_
,
3383 Address max_branch_offset
= max_branch_delta(this->r_type_
);
3384 if (max_branch_offset
== 0)
3386 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
3387 gold_assert(from
!= invalid_address
);
3388 from
+= (this->object_
->output_section(this->shndx_
)->address()
3393 switch (gsym
->source())
3395 case Symbol::FROM_OBJECT
:
3397 Object
* symobj
= gsym
->object();
3398 if (symobj
->is_dynamic()
3399 || symobj
->pluginobj() != NULL
)
3402 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
3403 if (shndx
== elfcpp::SHN_UNDEF
)
3408 case Symbol::IS_UNDEFINED
:
3414 Symbol_table::Compute_final_value_status status
;
3415 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
3416 if (status
!= Symbol_table::CFVS_OK
)
3419 to
+= this->object_
->ppc64_local_entry_offset(gsym
);
3423 const Symbol_value
<size
>* psymval
3424 = this->object_
->local_symbol(this->r_sym_
);
3425 Symbol_value
<size
> symval
;
3426 if (psymval
->is_section_symbol())
3427 symval
.set_is_section_symbol();
3428 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
3429 typename
ObjType::Compute_final_local_value_status status
3430 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
3432 if (status
!= ObjType::CFLV_OK
3433 || !symval
.has_output_value())
3435 to
= symval
.value(this->object_
, 0);
3437 to
+= this->object_
->ppc64_local_entry_offset(this->r_sym_
);
3439 if (!(size
== 32 && this->r_type_
== elfcpp::R_PPC_PLTREL24
))
3440 to
+= this->addend_
;
3441 if (stub_table
== NULL
)
3442 stub_table
= this->object_
->stub_table(this->shndx_
);
3443 if (size
== 64 && target
->abiversion() < 2)
3445 unsigned int dest_shndx
;
3446 if (!target
->symval_for_branch(symtab
, gsym
, this->object_
,
3450 Address delta
= to
- from
;
3451 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
3453 if (stub_table
== NULL
)
3455 gold_warning(_("%s:%s: branch in non-executable section,"
3456 " no long branch stub for you"),
3457 this->object_
->name().c_str(),
3458 this->object_
->section_name(this->shndx_
).c_str());
3461 bool save_res
= (size
== 64
3463 && gsym
->source() == Symbol::IN_OUTPUT_DATA
3464 && gsym
->output_data() == target
->savres_section());
3465 ok
= stub_table
->add_long_branch_entry(this->object_
,
3467 from
, to
, save_res
);
3471 gold_debug(DEBUG_TARGET
,
3472 "branch at %s:%s+%#lx\n"
3473 "can't reach stub attached to %s:%s",
3474 this->object_
->name().c_str(),
3475 this->object_
->section_name(this->shndx_
).c_str(),
3476 (unsigned long) this->offset_
,
3477 stub_table
->relobj()->name().c_str(),
3478 stub_table
->relobj()->section_name(stub_table
->shndx()).c_str());
3483 // Relaxation hook. This is where we do stub generation.
3485 template<int size
, bool big_endian
>
3487 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
3488 const Input_objects
*,
3489 Symbol_table
* symtab
,
3493 unsigned int prev_brlt_size
= 0;
3497 = this->abiversion() < 2 && parameters
->options().plt_thread_safe();
3499 && this->abiversion() < 2
3501 && !parameters
->options().user_set_plt_thread_safe())
3503 static const char* const thread_starter
[] =
3507 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3509 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3510 "mq_notify", "create_timer",
3515 "GOMP_parallel_start",
3516 "GOMP_parallel_loop_static",
3517 "GOMP_parallel_loop_static_start",
3518 "GOMP_parallel_loop_dynamic",
3519 "GOMP_parallel_loop_dynamic_start",
3520 "GOMP_parallel_loop_guided",
3521 "GOMP_parallel_loop_guided_start",
3522 "GOMP_parallel_loop_runtime",
3523 "GOMP_parallel_loop_runtime_start",
3524 "GOMP_parallel_sections",
3525 "GOMP_parallel_sections_start",
3530 if (parameters
->options().shared())
3534 for (unsigned int i
= 0;
3535 i
< sizeof(thread_starter
) / sizeof(thread_starter
[0]);
3538 Symbol
* sym
= symtab
->lookup(thread_starter
[i
], NULL
);
3539 thread_safe
= (sym
!= NULL
3541 && sym
->in_real_elf());
3547 this->plt_thread_safe_
= thread_safe
;
3552 this->stub_group_size_
= parameters
->options().stub_group_size();
3553 bool no_size_errors
= true;
3554 if (this->stub_group_size_
== 1)
3555 this->stub_group_size_
= 0x1c00000;
3556 else if (this->stub_group_size_
== -1)
3557 this->stub_group_size_
= -0x1e00000;
3559 no_size_errors
= false;
3560 this->group_sections(layout
, task
, no_size_errors
);
3562 else if (this->relax_failed_
&& this->relax_fail_count_
< 3)
3564 this->branch_lookup_table_
.clear();
3565 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3566 p
!= this->stub_tables_
.end();
3569 (*p
)->clear_stubs(true);
3571 this->stub_tables_
.clear();
3572 this->stub_group_size_
= this->stub_group_size_
/ 4 * 3;
3573 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3574 program_name
, this->stub_group_size_
);
3575 this->group_sections(layout
, task
, true);
3578 // We need address of stub tables valid for make_stub.
3579 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3580 p
!= this->stub_tables_
.end();
3583 const Powerpc_relobj
<size
, big_endian
>* object
3584 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
3585 Address off
= object
->get_output_section_offset((*p
)->shndx());
3586 gold_assert(off
!= invalid_address
);
3587 Output_section
* os
= (*p
)->output_section();
3588 (*p
)->set_address_and_size(os
, off
);
3593 // Clear plt call stubs, long branch stubs and branch lookup table.
3594 prev_brlt_size
= this->branch_lookup_table_
.size();
3595 this->branch_lookup_table_
.clear();
3596 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3597 p
!= this->stub_tables_
.end();
3600 (*p
)->clear_stubs(false);
3604 // Build all the stubs.
3605 this->relax_failed_
= false;
3606 Stub_table
<size
, big_endian
>* ifunc_stub_table
3607 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
3608 Stub_table
<size
, big_endian
>* one_stub_table
3609 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
3610 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
3611 b
!= this->branch_info_
.end();
3614 if (!b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
)
3615 && !this->relax_failed_
)
3617 this->relax_failed_
= true;
3618 this->relax_fail_count_
++;
3619 if (this->relax_fail_count_
< 3)
3624 // Did anything change size?
3625 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
3626 bool again
= num_huge_branches
!= prev_brlt_size
;
3627 if (size
== 64 && num_huge_branches
!= 0)
3628 this->make_brlt_section(layout
);
3629 if (size
== 64 && again
)
3630 this->brlt_section_
->set_current_size(num_huge_branches
);
3632 for (typename
Stub_tables::reverse_iterator p
= this->stub_tables_
.rbegin();
3633 p
!= this->stub_tables_
.rend();
3635 (*p
)->remove_eh_frame(layout
);
3637 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3638 p
!= this->stub_tables_
.end();
3640 (*p
)->add_eh_frame(layout
);
3642 typedef Unordered_set
<Output_section
*> Output_sections
;
3643 Output_sections os_need_update
;
3644 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3645 p
!= this->stub_tables_
.end();
3648 if ((*p
)->size_update())
3651 os_need_update
.insert((*p
)->output_section());
3655 // Set output section offsets for all input sections in an output
3656 // section that just changed size. Anything past the stubs will
3658 for (typename
Output_sections::iterator p
= os_need_update
.begin();
3659 p
!= os_need_update
.end();
3662 Output_section
* os
= *p
;
3664 typedef Output_section::Input_section_list Input_section_list
;
3665 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
3666 i
!= os
->input_sections().end();
3669 off
= align_address(off
, i
->addralign());
3670 if (i
->is_input_section() || i
->is_relaxed_input_section())
3671 i
->relobj()->set_section_offset(i
->shndx(), off
);
3672 if (i
->is_relaxed_input_section())
3674 Stub_table
<size
, big_endian
>* stub_table
3675 = static_cast<Stub_table
<size
, big_endian
>*>(
3676 i
->relaxed_input_section());
3677 Address stub_table_size
= stub_table
->set_address_and_size(os
, off
);
3678 off
+= stub_table_size
;
3679 // After a few iterations, set current stub table size
3680 // as min size threshold, so later stub tables can only
3683 stub_table
->set_min_size_threshold(stub_table_size
);
3686 off
+= i
->data_size();
3688 // If .branch_lt is part of this output section, then we have
3689 // just done the offset adjustment.
3690 os
->clear_section_offsets_need_adjustment();
3695 && num_huge_branches
!= 0
3696 && parameters
->options().output_is_position_independent())
3698 // Fill in the BRLT relocs.
3699 this->brlt_section_
->reset_brlt_sizes();
3700 for (typename
Branch_lookup_table::const_iterator p
3701 = this->branch_lookup_table_
.begin();
3702 p
!= this->branch_lookup_table_
.end();
3705 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
3707 this->brlt_section_
->finalize_brlt_sizes();
3711 && (parameters
->options().user_set_emit_stub_syms()
3712 ? parameters
->options().emit_stub_syms()
3714 || parameters
->options().output_is_position_independent()
3715 || parameters
->options().emit_relocs())))
3717 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
3718 p
!= this->stub_tables_
.end();
3720 (*p
)->define_stub_syms(symtab
);
3722 if (this->glink_
!= NULL
)
3724 int stub_size
= this->glink_
->pltresolve_size();
3725 Address value
= -stub_size
;
3731 this->define_local(symtab
, "__glink_PLTresolve",
3732 this->glink_
, value
, stub_size
);
3735 this->define_local(symtab
, "__glink", this->glink_
, 0, 0);
3742 template<int size
, bool big_endian
>
3744 Target_powerpc
<size
, big_endian
>::do_plt_fde_location(const Output_data
* plt
,
3745 unsigned char* oview
,
3749 uint64_t address
= plt
->address();
3750 off_t len
= plt
->data_size();
3752 if (plt
== this->glink_
)
3754 // See Output_data_glink::do_write() for glink contents.
3757 gold_assert(parameters
->doing_static_link());
3758 // Static linking may need stubs, to support ifunc and long
3759 // branches. We need to create an output section for
3760 // .eh_frame early in the link process, to have a place to
3761 // attach stub .eh_frame info. We also need to have
3762 // registered a CIE that matches the stub CIE. Both of
3763 // these requirements are satisfied by creating an FDE and
3764 // CIE for .glink, even though static linking will leave
3765 // .glink zero length.
3766 // ??? Hopefully generating an FDE with a zero address range
3767 // won't confuse anything that consumes .eh_frame info.
3769 else if (size
== 64)
3771 // There is one word before __glink_PLTresolve
3775 else if (parameters
->options().output_is_position_independent())
3777 // There are two FDEs for a position independent glink.
3778 // The first covers the branch table, the second
3779 // __glink_PLTresolve at the end of glink.
3780 off_t resolve_size
= this->glink_
->pltresolve_size();
3781 if (oview
[9] == elfcpp::DW_CFA_nop
)
3782 len
-= resolve_size
;
3785 address
+= len
- resolve_size
;
3792 // Must be a stub table.
3793 const Stub_table
<size
, big_endian
>* stub_table
3794 = static_cast<const Stub_table
<size
, big_endian
>*>(plt
);
3795 uint64_t stub_address
= stub_table
->stub_address();
3796 len
-= stub_address
- address
;
3797 address
= stub_address
;
3800 *paddress
= address
;
3804 // A class to handle the PLT data.
3806 template<int size
, bool big_endian
>
3807 class Output_data_plt_powerpc
: public Output_section_data_build
3810 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3811 size
, big_endian
> Reloc_section
;
3813 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3814 Reloc_section
* plt_rel
,
3816 : Output_section_data_build(size
== 32 ? 4 : 8),
3822 // Add an entry to the PLT.
3827 add_ifunc_entry(Symbol
*);
3830 add_local_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
3833 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
3835 // Return the .rela.plt section data.
3842 // Return the number of PLT entries.
3846 if (this->current_data_size() == 0)
3848 return ((this->current_data_size() - this->first_plt_entry_offset())
3849 / this->plt_entry_size());
3854 do_adjust_output_section(Output_section
* os
)
3859 // Write to a map file.
3861 do_print_to_mapfile(Mapfile
* mapfile
) const
3862 { mapfile
->print_output_data(this, this->name_
); }
3865 // Return the offset of the first non-reserved PLT entry.
3867 first_plt_entry_offset() const
3869 // IPLT and LPLT have no reserved entry.
3870 if (this->name_
[3] == 'I' || this->name_
[3] == 'L')
3872 return this->targ_
->first_plt_entry_offset();
3875 // Return the size of each PLT entry.
3877 plt_entry_size() const
3879 return this->targ_
->plt_entry_size();
3882 // Write out the PLT data.
3884 do_write(Output_file
*);
3886 // The reloc section.
3887 Reloc_section
* rel_
;
3888 // Allows access to .glink for do_write.
3889 Target_powerpc
<size
, big_endian
>* targ_
;
3890 // What to report in map file.
3894 // Add an entry to the PLT.
3896 template<int size
, bool big_endian
>
3898 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
3900 if (!gsym
->has_plt_offset())
3902 section_size_type off
= this->current_data_size();
3904 off
+= this->first_plt_entry_offset();
3905 gsym
->set_plt_offset(off
);
3906 gsym
->set_needs_dynsym_entry();
3907 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
3908 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
3909 off
+= this->plt_entry_size();
3910 this->set_current_data_size(off
);
3914 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3916 template<int size
, bool big_endian
>
3918 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
3920 if (!gsym
->has_plt_offset())
3922 section_size_type off
= this->current_data_size();
3923 gsym
->set_plt_offset(off
);
3924 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3925 if (size
== 64 && this->targ_
->abiversion() < 2)
3926 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3927 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
3928 off
+= this->plt_entry_size();
3929 this->set_current_data_size(off
);
3933 // Add an entry for a local symbol to the PLT.
3935 template<int size
, bool big_endian
>
3937 Output_data_plt_powerpc
<size
, big_endian
>::add_local_entry(
3938 Sized_relobj_file
<size
, big_endian
>* relobj
,
3939 unsigned int local_sym_index
)
3941 if (!relobj
->local_has_plt_offset(local_sym_index
))
3943 section_size_type off
= this->current_data_size();
3944 relobj
->set_local_plt_offset(local_sym_index
, off
);
3947 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
3948 if (size
== 64 && this->targ_
->abiversion() < 2)
3949 dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
3950 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
,
3951 dynrel
, this, off
, 0);
3953 off
+= this->plt_entry_size();
3954 this->set_current_data_size(off
);
3958 // Add an entry for a local ifunc symbol to the IPLT.
3960 template<int size
, bool big_endian
>
3962 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
3963 Sized_relobj_file
<size
, big_endian
>* relobj
,
3964 unsigned int local_sym_index
)
3966 if (!relobj
->local_has_plt_offset(local_sym_index
))
3968 section_size_type off
= this->current_data_size();
3969 relobj
->set_local_plt_offset(local_sym_index
, off
);
3970 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3971 if (size
== 64 && this->targ_
->abiversion() < 2)
3972 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3973 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
3975 off
+= this->plt_entry_size();
3976 this->set_current_data_size(off
);
3980 static const uint32_t add_0_11_11
= 0x7c0b5a14;
3981 static const uint32_t add_2_2_11
= 0x7c425a14;
3982 static const uint32_t add_2_2_12
= 0x7c426214;
3983 static const uint32_t add_3_3_2
= 0x7c631214;
3984 static const uint32_t add_3_3_13
= 0x7c636a14;
3985 static const uint32_t add_3_12_2
= 0x7c6c1214;
3986 static const uint32_t add_3_12_13
= 0x7c6c6a14;
3987 static const uint32_t add_11_0_11
= 0x7d605a14;
3988 static const uint32_t add_11_2_11
= 0x7d625a14;
3989 static const uint32_t add_11_11_2
= 0x7d6b1214;
3990 static const uint32_t addi_0_12
= 0x380c0000;
3991 static const uint32_t addi_2_2
= 0x38420000;
3992 static const uint32_t addi_3_3
= 0x38630000;
3993 static const uint32_t addi_11_11
= 0x396b0000;
3994 static const uint32_t addi_12_1
= 0x39810000;
3995 static const uint32_t addi_12_12
= 0x398c0000;
3996 static const uint32_t addis_0_2
= 0x3c020000;
3997 static const uint32_t addis_0_13
= 0x3c0d0000;
3998 static const uint32_t addis_2_12
= 0x3c4c0000;
3999 static const uint32_t addis_11_2
= 0x3d620000;
4000 static const uint32_t addis_11_11
= 0x3d6b0000;
4001 static const uint32_t addis_11_30
= 0x3d7e0000;
4002 static const uint32_t addis_12_1
= 0x3d810000;
4003 static const uint32_t addis_12_2
= 0x3d820000;
4004 static const uint32_t addis_12_12
= 0x3d8c0000;
4005 static const uint32_t b
= 0x48000000;
4006 static const uint32_t bcl_20_31
= 0x429f0005;
4007 static const uint32_t bctr
= 0x4e800420;
4008 static const uint32_t bctrl
= 0x4e800421;
4009 static const uint32_t beqlr
= 0x4d820020;
4010 static const uint32_t blr
= 0x4e800020;
4011 static const uint32_t bnectr_p4
= 0x4ce20420;
4012 static const uint32_t cmpld_7_12_0
= 0x7fac0040;
4013 static const uint32_t cmpldi_2_0
= 0x28220000;
4014 static const uint32_t cmpdi_11_0
= 0x2c2b0000;
4015 static const uint32_t cmpwi_11_0
= 0x2c0b0000;
4016 static const uint32_t cror_15_15_15
= 0x4def7b82;
4017 static const uint32_t cror_31_31_31
= 0x4ffffb82;
4018 static const uint32_t ld_0_1
= 0xe8010000;
4019 static const uint32_t ld_0_12
= 0xe80c0000;
4020 static const uint32_t ld_2_1
= 0xe8410000;
4021 static const uint32_t ld_2_2
= 0xe8420000;
4022 static const uint32_t ld_2_11
= 0xe84b0000;
4023 static const uint32_t ld_2_12
= 0xe84c0000;
4024 static const uint32_t ld_11_1
= 0xe9610000;
4025 static const uint32_t ld_11_2
= 0xe9620000;
4026 static const uint32_t ld_11_3
= 0xe9630000;
4027 static const uint32_t ld_11_11
= 0xe96b0000;
4028 static const uint32_t ld_12_2
= 0xe9820000;
4029 static const uint32_t ld_12_3
= 0xe9830000;
4030 static const uint32_t ld_12_11
= 0xe98b0000;
4031 static const uint32_t ld_12_12
= 0xe98c0000;
4032 static const uint32_t lfd_0_1
= 0xc8010000;
4033 static const uint32_t li_0_0
= 0x38000000;
4034 static const uint32_t li_12_0
= 0x39800000;
4035 static const uint32_t lis_0
= 0x3c000000;
4036 static const uint32_t lis_2
= 0x3c400000;
4037 static const uint32_t lis_11
= 0x3d600000;
4038 static const uint32_t lis_12
= 0x3d800000;
4039 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
4040 static const uint32_t lwz_0_12
= 0x800c0000;
4041 static const uint32_t lwz_11_3
= 0x81630000;
4042 static const uint32_t lwz_11_11
= 0x816b0000;
4043 static const uint32_t lwz_11_30
= 0x817e0000;
4044 static const uint32_t lwz_12_3
= 0x81830000;
4045 static const uint32_t lwz_12_12
= 0x818c0000;
4046 static const uint32_t lwzu_0_12
= 0x840c0000;
4047 static const uint32_t mflr_0
= 0x7c0802a6;
4048 static const uint32_t mflr_11
= 0x7d6802a6;
4049 static const uint32_t mflr_12
= 0x7d8802a6;
4050 static const uint32_t mr_0_3
= 0x7c601b78;
4051 static const uint32_t mr_3_0
= 0x7c030378;
4052 static const uint32_t mtctr_0
= 0x7c0903a6;
4053 static const uint32_t mtctr_11
= 0x7d6903a6;
4054 static const uint32_t mtctr_12
= 0x7d8903a6;
4055 static const uint32_t mtlr_0
= 0x7c0803a6;
4056 static const uint32_t mtlr_11
= 0x7d6803a6;
4057 static const uint32_t mtlr_12
= 0x7d8803a6;
4058 static const uint32_t nop
= 0x60000000;
4059 static const uint32_t ori_0_0_0
= 0x60000000;
4060 static const uint32_t srdi_0_0_2
= 0x7800f082;
4061 static const uint32_t std_0_1
= 0xf8010000;
4062 static const uint32_t std_0_12
= 0xf80c0000;
4063 static const uint32_t std_2_1
= 0xf8410000;
4064 static const uint32_t std_11_1
= 0xf9610000;
4065 static const uint32_t stfd_0_1
= 0xd8010000;
4066 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
4067 static const uint32_t sub_11_11_12
= 0x7d6c5850;
4068 static const uint32_t sub_12_12_11
= 0x7d8b6050;
4069 static const uint32_t xor_2_12_12
= 0x7d826278;
4070 static const uint32_t xor_11_12_12
= 0x7d8b6278;
4072 // Write out the PLT.
4074 template<int size
, bool big_endian
>
4076 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
4078 if (size
== 32 && (this->name_
[3] != 'I' && this->name_
[3] != 'L'))
4080 const section_size_type offset
= this->offset();
4081 const section_size_type oview_size
4082 = convert_to_section_size_type(this->data_size());
4083 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4084 unsigned char* pov
= oview
;
4085 unsigned char* endpov
= oview
+ oview_size
;
4087 // The address of the .glink branch table
4088 const Output_data_glink
<size
, big_endian
>* glink
4089 = this->targ_
->glink_section();
4090 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
4092 while (pov
< endpov
)
4094 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
4099 of
->write_output_view(offset
, oview_size
, oview
);
4103 // Create the PLT section.
4105 template<int size
, bool big_endian
>
4107 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
4110 if (this->plt_
== NULL
)
4112 if (this->got_
== NULL
)
4113 this->got_section(symtab
, layout
);
4115 if (this->glink_
== NULL
)
4116 make_glink_section(layout
);
4118 // Ensure that .rela.dyn always appears before .rela.plt This is
4119 // necessary due to how, on PowerPC and some other targets, .rela.dyn
4120 // needs to include .rela.plt in its range.
4121 this->rela_dyn_section(layout
);
4123 Reloc_section
* plt_rel
= new Reloc_section(false);
4124 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
4125 elfcpp::SHF_ALLOC
, plt_rel
,
4126 ORDER_DYNAMIC_PLT_RELOCS
, false);
4128 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
4130 layout
->add_output_section_data(".plt",
4132 ? elfcpp::SHT_PROGBITS
4133 : elfcpp::SHT_NOBITS
),
4134 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
4141 Output_section
* rela_plt_os
= plt_rel
->output_section();
4142 rela_plt_os
->set_info_section(this->plt_
->output_section());
4146 // Create the IPLT section.
4148 template<int size
, bool big_endian
>
4150 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
4153 if (this->iplt_
== NULL
)
4155 this->make_plt_section(symtab
, layout
);
4156 this->make_lplt_section(layout
);
4158 Reloc_section
* iplt_rel
= new Reloc_section(false);
4159 if (this->rela_dyn_
->output_section())
4160 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
4162 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
4164 if (this->plt_
->output_section())
4165 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
4169 // Create the LPLT section.
4171 template<int size
, bool big_endian
>
4173 Target_powerpc
<size
, big_endian
>::make_lplt_section(Layout
* layout
)
4175 if (this->lplt_
== NULL
)
4177 Reloc_section
* lplt_rel
= NULL
;
4178 if (parameters
->options().output_is_position_independent())
4180 lplt_rel
= new Reloc_section(false);
4181 this->rela_dyn_section(layout
);
4182 if (this->rela_dyn_
->output_section())
4183 this->rela_dyn_
->output_section()
4184 ->add_output_section_data(lplt_rel
);
4187 = new Output_data_plt_powerpc
<size
, big_endian
>(this, lplt_rel
,
4189 this->make_brlt_section(layout
);
4190 if (this->brlt_section_
&& this->brlt_section_
->output_section())
4191 this->brlt_section_
->output_section()
4192 ->add_output_section_data(this->lplt_
);
4194 layout
->add_output_section_data(".branch_lt",
4195 elfcpp::SHT_PROGBITS
,
4196 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
4203 // A section for huge long branch addresses, similar to plt section.
4205 template<int size
, bool big_endian
>
4206 class Output_data_brlt_powerpc
: public Output_section_data_build
4209 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4210 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
4211 size
, big_endian
> Reloc_section
;
4213 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
4214 Reloc_section
* brlt_rel
)
4215 : Output_section_data_build(size
== 32 ? 4 : 8),
4223 this->reset_data_size();
4224 this->rel_
->reset_data_size();
4228 finalize_brlt_sizes()
4230 this->finalize_data_size();
4231 this->rel_
->finalize_data_size();
4234 // Add a reloc for an entry in the BRLT.
4236 add_reloc(Address to
, unsigned int off
)
4237 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
4239 // Update section and reloc section size.
4241 set_current_size(unsigned int num_branches
)
4243 this->reset_address_and_file_offset();
4244 this->set_current_data_size(num_branches
* 16);
4245 this->finalize_data_size();
4246 Output_section
* os
= this->output_section();
4247 os
->set_section_offsets_need_adjustment();
4248 if (this->rel_
!= NULL
)
4250 const unsigned int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
4251 this->rel_
->reset_address_and_file_offset();
4252 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
4253 this->rel_
->finalize_data_size();
4254 Output_section
* os
= this->rel_
->output_section();
4255 os
->set_section_offsets_need_adjustment();
4261 do_adjust_output_section(Output_section
* os
)
4266 // Write to a map file.
4268 do_print_to_mapfile(Mapfile
* mapfile
) const
4269 { mapfile
->print_output_data(this, "** BRLT"); }
4272 // Write out the BRLT data.
4274 do_write(Output_file
*);
4276 // The reloc section.
4277 Reloc_section
* rel_
;
4278 Target_powerpc
<size
, big_endian
>* targ_
;
4281 // Make the branch lookup table section.
4283 template<int size
, bool big_endian
>
4285 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
4287 if (size
== 64 && this->brlt_section_
== NULL
)
4289 Reloc_section
* brlt_rel
= NULL
;
4290 bool is_pic
= parameters
->options().output_is_position_independent();
4293 // When PIC we can't fill in .branch_lt but must initialise at
4294 // runtime via dynamic relocations.
4295 this->rela_dyn_section(layout
);
4296 brlt_rel
= new Reloc_section(false);
4297 if (this->rela_dyn_
->output_section())
4298 this->rela_dyn_
->output_section()
4299 ->add_output_section_data(brlt_rel
);
4302 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
4303 if (this->plt_
&& is_pic
&& this->plt_
->output_section())
4304 this->plt_
->output_section()
4305 ->add_output_section_data(this->brlt_section_
);
4307 layout
->add_output_section_data(".branch_lt",
4308 elfcpp::SHT_PROGBITS
,
4309 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
4310 this->brlt_section_
,
4316 // Write out .branch_lt when non-PIC.
4318 template<int size
, bool big_endian
>
4320 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
4322 if (size
== 64 && !parameters
->options().output_is_position_independent())
4324 const section_size_type offset
= this->offset();
4325 const section_size_type oview_size
4326 = convert_to_section_size_type(this->data_size());
4327 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
4329 this->targ_
->write_branch_lookup_table(oview
);
4330 of
->write_output_view(offset
, oview_size
, oview
);
4334 static inline uint32_t
4340 static inline uint32_t
4346 static inline uint32_t
4349 return hi(a
+ 0x8000);
4355 static const unsigned char eh_frame_cie
[12];
4359 const unsigned char Eh_cie
<size
>::eh_frame_cie
[] =
4362 'z', 'R', 0, // Augmentation string.
4363 4, // Code alignment.
4364 0x80 - size
/ 8 , // Data alignment.
4366 1, // Augmentation size.
4367 (elfcpp::DW_EH_PE_pcrel
4368 | elfcpp::DW_EH_PE_sdata4
), // FDE encoding.
4369 elfcpp::DW_CFA_def_cfa
, 1, 0 // def_cfa: r1 offset 0.
4372 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4373 static const unsigned char glink_eh_frame_fde_64v1
[] =
4375 0, 0, 0, 0, // Replaced with offset to .glink.
4376 0, 0, 0, 0, // Replaced with size of .glink.
4377 0, // Augmentation size.
4378 elfcpp::DW_CFA_advance_loc
+ 1,
4379 elfcpp::DW_CFA_register
, 65, 12,
4380 elfcpp::DW_CFA_advance_loc
+ 5,
4381 elfcpp::DW_CFA_restore_extended
, 65
4384 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4385 static const unsigned char glink_eh_frame_fde_64v2
[] =
4387 0, 0, 0, 0, // Replaced with offset to .glink.
4388 0, 0, 0, 0, // Replaced with size of .glink.
4389 0, // Augmentation size.
4390 elfcpp::DW_CFA_advance_loc
+ 1,
4391 elfcpp::DW_CFA_register
, 65, 0,
4392 elfcpp::DW_CFA_advance_loc
+ 7,
4393 elfcpp::DW_CFA_restore_extended
, 65
4396 // Describe __glink_PLTresolve use of LR, 32-bit version.
4397 static const unsigned char glink_eh_frame_fde_32
[] =
4399 0, 0, 0, 0, // Replaced with offset to .glink.
4400 0, 0, 0, 0, // Replaced with size of .glink.
4401 0, // Augmentation size.
4402 elfcpp::DW_CFA_advance_loc
+ 2,
4403 elfcpp::DW_CFA_register
, 65, 0,
4404 elfcpp::DW_CFA_advance_loc
+ 4,
4405 elfcpp::DW_CFA_restore_extended
, 65
4408 static const unsigned char default_fde
[] =
4410 0, 0, 0, 0, // Replaced with offset to stubs.
4411 0, 0, 0, 0, // Replaced with size of stubs.
4412 0, // Augmentation size.
4413 elfcpp::DW_CFA_nop
, // Pad.
4418 template<bool big_endian
>
4420 write_insn(unsigned char* p
, uint32_t v
)
4422 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
4426 static inline unsigned int
4429 if (!parameters
->options().user_set_plt_align())
4430 return size
== 64 ? 32 : 8;
4431 return 1 << parameters
->options().plt_align();
4434 // Stub_table holds information about plt and long branch stubs.
4435 // Stubs are built in an area following some input section determined
4436 // by group_sections(). This input section is converted to a relaxed
4437 // input section allowing it to be resized to accommodate the stubs
4439 template<int size
, bool big_endian
>
4440 class Stub_table
: public Output_relaxed_input_section
4445 Plt_stub_ent(unsigned int off
, unsigned int indx
)
4446 : off_(off
), indx_(indx
), r2save_(0), localentry0_(0)
4450 unsigned int indx_
: 30;
4451 unsigned int r2save_
: 1;
4452 unsigned int localentry0_
: 1;
4454 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4455 static const Address invalid_address
= static_cast<Address
>(0) - 1;
4457 Stub_table(Target_powerpc
<size
, big_endian
>* targ
,
4458 Output_section
* output_section
,
4459 const Output_section::Input_section
* owner
,
4461 : Output_relaxed_input_section(owner
->relobj(), owner
->shndx(),
4463 ->section_addralign(owner
->shndx())),
4464 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
4465 orig_data_size_(owner
->current_data_size()),
4466 plt_size_(0), last_plt_size_(0),
4467 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4468 need_save_res_(false), uniq_(id
), tls_get_addr_opt_bctrl_(-1u),
4471 this->set_output_section(output_section
);
4473 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
4474 new_relaxed
.push_back(this);
4475 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
4478 // Add a plt call stub.
4480 add_plt_call_entry(Address
,
4481 const Sized_relobj_file
<size
, big_endian
>*,
4488 add_plt_call_entry(Address
,
4489 const Sized_relobj_file
<size
, big_endian
>*,
4495 // Find a given plt call stub.
4497 find_plt_call_entry(const Symbol
*) const;
4500 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4501 unsigned int) const;
4504 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4510 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
4515 // Add a long branch stub.
4517 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
4518 unsigned int, Address
, Address
, bool);
4521 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
4525 can_reach_stub(Address from
, unsigned int off
, unsigned int r_type
)
4527 Address max_branch_offset
= max_branch_delta(r_type
);
4528 if (max_branch_offset
== 0)
4530 gold_assert(from
!= invalid_address
);
4531 Address loc
= off
+ this->stub_address();
4532 return loc
- from
+ max_branch_offset
< 2 * max_branch_offset
;
4536 clear_stubs(bool all
)
4538 this->plt_call_stubs_
.clear();
4539 this->plt_size_
= 0;
4540 this->long_branch_stubs_
.clear();
4541 this->branch_size_
= 0;
4542 this->need_save_res_
= false;
4545 this->last_plt_size_
= 0;
4546 this->last_branch_size_
= 0;
4551 set_address_and_size(const Output_section
* os
, Address off
)
4553 Address start_off
= off
;
4554 off
+= this->orig_data_size_
;
4555 Address my_size
= this->plt_size_
+ this->branch_size_
;
4556 if (this->need_save_res_
)
4557 my_size
+= this->targ_
->savres_section()->data_size();
4559 off
= align_address(off
, this->stub_align());
4560 // Include original section size and alignment padding in size
4561 my_size
+= off
- start_off
;
4562 // Ensure new size is always larger than min size
4563 // threshold. Alignment requirement is included in "my_size", so
4564 // increase "my_size" does not invalidate alignment.
4565 if (my_size
< this->min_size_threshold_
)
4566 my_size
= this->min_size_threshold_
;
4567 this->reset_address_and_file_offset();
4568 this->set_current_data_size(my_size
);
4569 this->set_address_and_file_offset(os
->address() + start_off
,
4570 os
->offset() + start_off
);
4575 stub_address() const
4577 return align_address(this->address() + this->orig_data_size_
,
4578 this->stub_align());
4584 return align_address(this->offset() + this->orig_data_size_
,
4585 this->stub_align());
4590 { return this->plt_size_
; }
4593 set_min_size_threshold(Address min_size
)
4594 { this->min_size_threshold_
= min_size
; }
4597 define_stub_syms(Symbol_table
*);
4602 Output_section
* os
= this->output_section();
4603 if (os
->addralign() < this->stub_align())
4605 os
->set_addralign(this->stub_align());
4606 // FIXME: get rid of the insane checkpointing.
4607 // We can't increase alignment of the input section to which
4608 // stubs are attached; The input section may be .init which
4609 // is pasted together with other .init sections to form a
4610 // function. Aligning might insert zero padding resulting in
4611 // sigill. However we do need to increase alignment of the
4612 // output section so that the align_address() on offset in
4613 // set_address_and_size() adds the same padding as the
4614 // align_address() on address in stub_address().
4615 // What's more, we need this alignment for the layout done in
4616 // relaxation_loop_body() so that the output section starts at
4617 // a suitably aligned address.
4618 os
->checkpoint_set_addralign(this->stub_align());
4620 if (this->last_plt_size_
!= this->plt_size_
4621 || this->last_branch_size_
!= this->branch_size_
)
4623 this->last_plt_size_
= this->plt_size_
;
4624 this->last_branch_size_
= this->branch_size_
;
4630 // Generate a suitable FDE to describe code in this stub group.
4634 // Add .eh_frame info for this stub section.
4636 add_eh_frame(Layout
* layout
);
4638 // Remove .eh_frame info for this stub section.
4640 remove_eh_frame(Layout
* layout
);
4642 Target_powerpc
<size
, big_endian
>*
4648 class Plt_stub_key_hash
;
4649 typedef Unordered_map
<Plt_stub_key
, Plt_stub_ent
,
4650 Plt_stub_key_hash
> Plt_stub_entries
;
4651 class Branch_stub_ent
;
4652 class Branch_stub_ent_hash
;
4653 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
4654 Branch_stub_ent_hash
> Branch_stub_entries
;
4656 // Alignment of stub section.
4660 unsigned int min_align
= size
== 64 ? 32 : 16;
4661 unsigned int user_align
= 1 << parameters
->options().plt_align();
4662 return std::max(user_align
, min_align
);
4665 // Return the plt offset for the given call stub.
4667 plt_off(typename
Plt_stub_entries::const_iterator p
,
4668 const Output_data_plt_powerpc
<size
, big_endian
>** sec
) const
4670 const Symbol
* gsym
= p
->first
.sym_
;
4672 return this->targ_
->plt_off(gsym
, sec
);
4675 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
4676 unsigned int local_sym_index
= p
->first
.locsym_
;
4677 return this->targ_
->plt_off(relobj
, local_sym_index
, sec
);
4681 // Size of a given plt call stub.
4683 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
4687 const Symbol
* gsym
= p
->first
.sym_
;
4689 + (this->targ_
->is_tls_get_addr_opt(gsym
) ? 8 * 4 : 0));
4692 const Output_data_plt_powerpc
<size
, big_endian
>* plt
;
4693 Address plt_addr
= this->plt_off(p
, &plt
);
4694 plt_addr
+= plt
->address();
4695 Address got_addr
= this->targ_
->got_section()->output_section()->address();
4696 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4697 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
4698 got_addr
+= ppcobj
->toc_base_offset();
4699 Address off
= plt_addr
- got_addr
;
4700 unsigned int bytes
= 4 * 4 + 4 * (ha(off
) != 0);
4701 const Symbol
* gsym
= p
->first
.sym_
;
4702 if (this->targ_
->is_tls_get_addr_opt(gsym
))
4704 if (this->targ_
->abiversion() < 2)
4706 bool static_chain
= parameters
->options().plt_static_chain();
4707 bool thread_safe
= this->targ_
->plt_thread_safe();
4711 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
4717 plt_call_align(unsigned int bytes
) const
4719 unsigned int align
= param_plt_align
<size
>();
4720 return (bytes
+ align
- 1) & -align
;
4723 // Return long branch stub size.
4725 branch_stub_size(typename
Branch_stub_entries::const_iterator p
)
4727 Address loc
= this->stub_address() + this->last_plt_size_
+ p
->second
;
4728 if (p
->first
.dest_
- loc
+ (1 << 25) < 2 << 25)
4730 unsigned int bytes
= 16;
4731 if (size
== 32 && parameters
->options().output_is_position_independent())
4738 do_write(Output_file
*);
4740 // Plt call stub keys.
4744 Plt_stub_key(const Symbol
* sym
)
4745 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4748 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4749 unsigned int locsym_index
)
4750 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4753 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4755 unsigned int r_type
,
4757 : sym_(sym
), object_(0), addend_(0), locsym_(0)
4760 this->addend_
= addend
;
4761 else if (parameters
->options().output_is_position_independent()
4762 && (r_type
== elfcpp::R_PPC_PLTREL24
4763 || r_type
== elfcpp::R_POWERPC_PLTCALL
))
4765 this->addend_
= addend
;
4766 if (this->addend_
>= 32768)
4767 this->object_
= object
;
4771 Plt_stub_key(const Sized_relobj_file
<size
, big_endian
>* object
,
4772 unsigned int locsym_index
,
4773 unsigned int r_type
,
4775 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
4778 this->addend_
= addend
;
4779 else if (parameters
->options().output_is_position_independent()
4780 && (r_type
== elfcpp::R_PPC_PLTREL24
4781 || r_type
== elfcpp::R_POWERPC_PLTCALL
))
4782 this->addend_
= addend
;
4785 bool operator==(const Plt_stub_key
& that
) const
4787 return (this->sym_
== that
.sym_
4788 && this->object_
== that
.object_
4789 && this->addend_
== that
.addend_
4790 && this->locsym_
== that
.locsym_
);
4794 const Sized_relobj_file
<size
, big_endian
>* object_
;
4795 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
4796 unsigned int locsym_
;
4799 class Plt_stub_key_hash
4802 size_t operator()(const Plt_stub_key
& ent
) const
4804 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
4805 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
4811 // Long branch stub keys.
4812 class Branch_stub_ent
4815 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
,
4816 Address to
, bool save_res
)
4817 : dest_(to
), toc_base_off_(0), save_res_(save_res
)
4820 toc_base_off_
= obj
->toc_base_offset();
4823 bool operator==(const Branch_stub_ent
& that
) const
4825 return (this->dest_
== that
.dest_
4827 || this->toc_base_off_
== that
.toc_base_off_
));
4831 unsigned int toc_base_off_
;
4835 class Branch_stub_ent_hash
4838 size_t operator()(const Branch_stub_ent
& ent
) const
4839 { return ent
.dest_
^ ent
.toc_base_off_
; }
4842 // In a sane world this would be a global.
4843 Target_powerpc
<size
, big_endian
>* targ_
;
4844 // Map sym/object/addend to stub offset.
4845 Plt_stub_entries plt_call_stubs_
;
4846 // Map destination address to stub offset.
4847 Branch_stub_entries long_branch_stubs_
;
4848 // size of input section
4849 section_size_type orig_data_size_
;
4851 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
4852 // Some rare cases cause (PR/20529) fluctuation in stub table
4853 // size, which leads to an endless relax loop. This is to be fixed
4854 // by, after the first few iterations, allowing only increase of
4855 // stub table size. This variable sets the minimal possible size of
4856 // a stub table, it is zero for the first few iterations, then
4857 // increases monotonically.
4858 Address min_size_threshold_
;
4859 // Set if this stub group needs a copy of out-of-line register
4860 // save/restore functions.
4861 bool need_save_res_
;
4862 // Per stub table unique identifier.
4864 // The bctrl in the __tls_get_addr_opt stub, if present.
4865 unsigned int tls_get_addr_opt_bctrl_
;
4866 // FDE unwind info for this stub group.
4867 unsigned int plt_fde_len_
;
4868 unsigned char plt_fde_
[20];
4871 // Add a plt call stub, if we do not already have one for this
4872 // sym/object/addend combo.
4874 template<int size
, bool big_endian
>
4876 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4878 const Sized_relobj_file
<size
, big_endian
>* object
,
4880 unsigned int r_type
,
4884 Plt_stub_key
key(object
, gsym
, r_type
, addend
);
4885 Plt_stub_ent
ent(this->plt_size_
, this->plt_call_stubs_
.size());
4886 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4887 = this->plt_call_stubs_
.insert(std::make_pair(key
, ent
));
4890 this->plt_size_
= ent
.off_
+ this->plt_call_size(p
.first
);
4892 && this->targ_
->is_elfv2_localentry0(gsym
))
4894 p
.first
->second
.localentry0_
= 1;
4895 this->targ_
->set_has_localentry0();
4897 if (this->targ_
->is_tls_get_addr_opt(gsym
))
4899 this->targ_
->set_has_tls_get_addr_opt();
4900 this->tls_get_addr_opt_bctrl_
= this->plt_size_
- 5 * 4;
4902 this->plt_size_
= this->plt_call_align(this->plt_size_
);
4906 && !p
.first
->second
.localentry0_
)
4907 p
.first
->second
.r2save_
= 1;
4908 return this->can_reach_stub(from
, ent
.off_
, r_type
);
4911 template<int size
, bool big_endian
>
4913 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4915 const Sized_relobj_file
<size
, big_endian
>* object
,
4916 unsigned int locsym_index
,
4917 unsigned int r_type
,
4921 Plt_stub_key
key(object
, locsym_index
, r_type
, addend
);
4922 Plt_stub_ent
ent(this->plt_size_
, this->plt_call_stubs_
.size());
4923 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4924 = this->plt_call_stubs_
.insert(std::make_pair(key
, ent
));
4927 this->plt_size_
= ent
.off_
+ this->plt_call_size(p
.first
);
4928 this->plt_size_
= this->plt_call_align(this->plt_size_
);
4930 && this->targ_
->is_elfv2_localentry0(object
, locsym_index
))
4932 p
.first
->second
.localentry0_
= 1;
4933 this->targ_
->set_has_localentry0();
4938 && !p
.first
->second
.localentry0_
)
4939 p
.first
->second
.r2save_
= 1;
4940 return this->can_reach_stub(from
, ent
.off_
, r_type
);
4943 // Find a plt call stub.
4945 template<int size
, bool big_endian
>
4946 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4947 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4948 const Sized_relobj_file
<size
, big_endian
>* object
,
4950 unsigned int r_type
,
4951 Address addend
) const
4953 Plt_stub_key
key(object
, gsym
, r_type
, addend
);
4954 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4955 if (p
== this->plt_call_stubs_
.end())
4960 template<int size
, bool big_endian
>
4961 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4962 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
4964 Plt_stub_key
key(gsym
);
4965 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4966 if (p
== this->plt_call_stubs_
.end())
4971 template<int size
, bool big_endian
>
4972 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4973 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4974 const Sized_relobj_file
<size
, big_endian
>* object
,
4975 unsigned int locsym_index
,
4976 unsigned int r_type
,
4977 Address addend
) const
4979 Plt_stub_key
key(object
, locsym_index
, r_type
, addend
);
4980 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4981 if (p
== this->plt_call_stubs_
.end())
4986 template<int size
, bool big_endian
>
4987 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
*
4988 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4989 const Sized_relobj_file
<size
, big_endian
>* object
,
4990 unsigned int locsym_index
) const
4992 Plt_stub_key
key(object
, locsym_index
);
4993 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(key
);
4994 if (p
== this->plt_call_stubs_
.end())
4999 // Add a long branch stub if we don't already have one to given
5002 template<int size
, bool big_endian
>
5004 Stub_table
<size
, big_endian
>::add_long_branch_entry(
5005 const Powerpc_relobj
<size
, big_endian
>* object
,
5006 unsigned int r_type
,
5011 Branch_stub_ent
ent(object
, to
, save_res
);
5012 Address off
= this->branch_size_
;
5013 std::pair
<typename
Branch_stub_entries::iterator
, bool> p
5014 = this->long_branch_stubs_
.insert(std::make_pair(ent
, off
));
5018 this->need_save_res_
= true;
5021 unsigned int stub_size
= this->branch_stub_size(p
.first
);
5022 this->branch_size_
= off
+ stub_size
;
5023 if (size
== 64 && stub_size
!= 4)
5024 this->targ_
->add_branch_lookup_table(to
);
5027 return this->can_reach_stub(from
, off
, r_type
);
5030 // Find long branch stub offset.
5032 template<int size
, bool big_endian
>
5033 typename Stub_table
<size
, big_endian
>::Address
5034 Stub_table
<size
, big_endian
>::find_long_branch_entry(
5035 const Powerpc_relobj
<size
, big_endian
>* object
,
5038 Branch_stub_ent
ent(object
, to
, false);
5039 typename
Branch_stub_entries::const_iterator p
5040 = this->long_branch_stubs_
.find(ent
);
5041 if (p
== this->long_branch_stubs_
.end())
5042 return invalid_address
;
5043 if (p
->first
.save_res_
)
5044 return to
- this->targ_
->savres_section()->address() + this->branch_size_
;
5048 // Generate a suitable FDE to describe code in this stub group.
5049 // The __tls_get_addr_opt call stub needs to describe where it saves
5050 // LR, to support exceptions that might be thrown from __tls_get_addr.
5052 template<int size
, bool big_endian
>
5054 Stub_table
<size
, big_endian
>::init_plt_fde()
5056 unsigned char* p
= this->plt_fde_
;
5057 // offset pcrel sdata4, size udata4, and augmentation size byte.
5060 if (this->tls_get_addr_opt_bctrl_
!= -1u)
5062 unsigned int to_bctrl
= this->tls_get_addr_opt_bctrl_
/ 4;
5064 *p
++ = elfcpp::DW_CFA_advance_loc
+ to_bctrl
;
5065 else if (to_bctrl
< 256)
5067 *p
++ = elfcpp::DW_CFA_advance_loc1
;
5070 else if (to_bctrl
< 65536)
5072 *p
++ = elfcpp::DW_CFA_advance_loc2
;
5073 elfcpp::Swap
<16, big_endian
>::writeval(p
, to_bctrl
);
5078 *p
++ = elfcpp::DW_CFA_advance_loc4
;
5079 elfcpp::Swap
<32, big_endian
>::writeval(p
, to_bctrl
);
5082 *p
++ = elfcpp::DW_CFA_offset_extended_sf
;
5084 *p
++ = -(this->targ_
->stk_linker() / 8) & 0x7f;
5085 *p
++ = elfcpp::DW_CFA_advance_loc
+ 4;
5086 *p
++ = elfcpp::DW_CFA_restore_extended
;
5089 this->plt_fde_len_
= p
- this->plt_fde_
;
5092 // Add .eh_frame info for this stub section. Unlike other linker
5093 // generated .eh_frame this is added late in the link, because we
5094 // only want the .eh_frame info if this particular stub section is
5097 template<int size
, bool big_endian
>
5099 Stub_table
<size
, big_endian
>::add_eh_frame(Layout
* layout
)
5101 if (!parameters
->options().ld_generated_unwind_info())
5104 // Since we add stub .eh_frame info late, it must be placed
5105 // after all other linker generated .eh_frame info so that
5106 // merge mapping need not be updated for input sections.
5107 // There is no provision to use a different CIE to that used
5109 if (!this->targ_
->has_glink())
5112 if (this->plt_size_
+ this->branch_size_
+ this->need_save_res_
== 0)
5115 this->init_plt_fde();
5116 layout
->add_eh_frame_for_plt(this,
5117 Eh_cie
<size
>::eh_frame_cie
,
5118 sizeof (Eh_cie
<size
>::eh_frame_cie
),
5119 this->plt_fde_
, this->plt_fde_len_
);
5122 template<int size
, bool big_endian
>
5124 Stub_table
<size
, big_endian
>::remove_eh_frame(Layout
* layout
)
5126 if (this->plt_fde_len_
!= 0)
5128 layout
->remove_eh_frame_for_plt(this,
5129 Eh_cie
<size
>::eh_frame_cie
,
5130 sizeof (Eh_cie
<size
>::eh_frame_cie
),
5131 this->plt_fde_
, this->plt_fde_len_
);
5132 this->plt_fde_len_
= 0;
5136 // A class to handle .glink.
5138 template<int size
, bool big_endian
>
5139 class Output_data_glink
: public Output_section_data
5142 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
5143 static const Address invalid_address
= static_cast<Address
>(0) - 1;
5145 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
5146 : Output_section_data(16), targ_(targ
), global_entry_stubs_(),
5147 end_branch_table_(), ge_size_(0)
5151 add_eh_frame(Layout
* layout
);
5154 add_global_entry(const Symbol
*);
5157 find_global_entry(const Symbol
*) const;
5160 global_entry_align(unsigned int off
) const
5162 unsigned int align
= param_plt_align
<size
>();
5163 return (off
+ align
- 1) & -align
;
5167 global_entry_off() const
5169 return this->global_entry_align(this->end_branch_table_
);
5173 global_entry_address() const
5175 gold_assert(this->is_data_size_valid());
5176 return this->address() + this->global_entry_off();
5180 pltresolve_size() const
5184 + (this->targ_
->abiversion() < 2 ? 11 * 4 : 14 * 4));
5189 // Write to a map file.
5191 do_print_to_mapfile(Mapfile
* mapfile
) const
5192 { mapfile
->print_output_data(this, _("** glink")); }
5196 set_final_data_size();
5200 do_write(Output_file
*);
5202 // Allows access to .got and .plt for do_write.
5203 Target_powerpc
<size
, big_endian
>* targ_
;
5205 // Map sym to stub offset.
5206 typedef Unordered_map
<const Symbol
*, unsigned int> Global_entry_stub_entries
;
5207 Global_entry_stub_entries global_entry_stubs_
;
5209 unsigned int end_branch_table_
, ge_size_
;
5212 template<int size
, bool big_endian
>
5214 Output_data_glink
<size
, big_endian
>::add_eh_frame(Layout
* layout
)
5216 if (!parameters
->options().ld_generated_unwind_info())
5221 if (this->targ_
->abiversion() < 2)
5222 layout
->add_eh_frame_for_plt(this,
5223 Eh_cie
<64>::eh_frame_cie
,
5224 sizeof (Eh_cie
<64>::eh_frame_cie
),
5225 glink_eh_frame_fde_64v1
,
5226 sizeof (glink_eh_frame_fde_64v1
));
5228 layout
->add_eh_frame_for_plt(this,
5229 Eh_cie
<64>::eh_frame_cie
,
5230 sizeof (Eh_cie
<64>::eh_frame_cie
),
5231 glink_eh_frame_fde_64v2
,
5232 sizeof (glink_eh_frame_fde_64v2
));
5236 // 32-bit .glink can use the default since the CIE return
5237 // address reg, LR, is valid.
5238 layout
->add_eh_frame_for_plt(this,
5239 Eh_cie
<32>::eh_frame_cie
,
5240 sizeof (Eh_cie
<32>::eh_frame_cie
),
5242 sizeof (default_fde
));
5243 // Except where LR is used in a PIC __glink_PLTresolve.
5244 if (parameters
->options().output_is_position_independent())
5245 layout
->add_eh_frame_for_plt(this,
5246 Eh_cie
<32>::eh_frame_cie
,
5247 sizeof (Eh_cie
<32>::eh_frame_cie
),
5248 glink_eh_frame_fde_32
,
5249 sizeof (glink_eh_frame_fde_32
));
5253 template<int size
, bool big_endian
>
5255 Output_data_glink
<size
, big_endian
>::add_global_entry(const Symbol
* gsym
)
5257 unsigned int off
= this->global_entry_align(this->ge_size_
);
5258 std::pair
<typename
Global_entry_stub_entries::iterator
, bool> p
5259 = this->global_entry_stubs_
.insert(std::make_pair(gsym
, off
));
5261 this->ge_size_
= off
+ 16;
5264 template<int size
, bool big_endian
>
5265 typename Output_data_glink
<size
, big_endian
>::Address
5266 Output_data_glink
<size
, big_endian
>::find_global_entry(const Symbol
* gsym
) const
5268 typename
Global_entry_stub_entries::const_iterator p
5269 = this->global_entry_stubs_
.find(gsym
);
5270 return p
== this->global_entry_stubs_
.end() ? invalid_address
: p
->second
;
5273 template<int size
, bool big_endian
>
5275 Output_data_glink
<size
, big_endian
>::set_final_data_size()
5277 unsigned int count
= this->targ_
->plt_entry_count();
5278 section_size_type total
= 0;
5284 // space for branch table
5285 total
+= 4 * (count
- 1);
5287 total
+= -total
& 15;
5288 total
+= this->pltresolve_size();
5292 total
+= this->pltresolve_size();
5294 // space for branch table
5296 if (this->targ_
->abiversion() < 2)
5300 total
+= 4 * (count
- 0x8000);
5304 this->end_branch_table_
= total
;
5305 total
= this->global_entry_align(total
);
5306 total
+= this->ge_size_
;
5308 this->set_data_size(total
);
5311 // Define symbols on stubs, identifying the stub.
5313 template<int size
, bool big_endian
>
5315 Stub_table
<size
, big_endian
>::define_stub_syms(Symbol_table
* symtab
)
5317 if (!this->plt_call_stubs_
.empty())
5319 // The key for the plt call stub hash table includes addresses,
5320 // therefore traversal order depends on those addresses, which
5321 // can change between runs if gold is a PIE. Unfortunately the
5322 // output .symtab ordering depends on the order in which symbols
5323 // are added to the linker symtab. We want reproducible output
5324 // so must sort the call stub symbols.
5325 typedef typename
Plt_stub_entries::const_iterator plt_iter
;
5326 std::vector
<plt_iter
> sorted
;
5327 sorted
.resize(this->plt_call_stubs_
.size());
5329 for (plt_iter cs
= this->plt_call_stubs_
.begin();
5330 cs
!= this->plt_call_stubs_
.end();
5332 sorted
[cs
->second
.indx_
] = cs
;
5334 for (unsigned int i
= 0; i
< this->plt_call_stubs_
.size(); ++i
)
5336 plt_iter cs
= sorted
[i
];
5339 if (cs
->first
.addend_
!= 0)
5340 sprintf(add
, "+%x", static_cast<uint32_t>(cs
->first
.addend_
));
5343 if (cs
->first
.object_
)
5345 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
5346 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
5347 sprintf(obj
, "%x:", ppcobj
->uniq());
5350 const char *symname
;
5351 if (cs
->first
.sym_
== NULL
)
5353 sprintf(localname
, "%x", cs
->first
.locsym_
);
5354 symname
= localname
;
5356 else if (this->targ_
->is_tls_get_addr_opt(cs
->first
.sym_
))
5357 symname
= this->targ_
->tls_get_addr_opt()->name();
5359 symname
= cs
->first
.sym_
->name();
5360 char* name
= new char[8 + 10 + strlen(obj
) + strlen(symname
) + strlen(add
) + 1];
5361 sprintf(name
, "%08x.plt_call.%s%s%s", this->uniq_
, obj
, symname
, add
);
5363 = this->stub_address() - this->address() + cs
->second
.off_
;
5364 unsigned int stub_size
= this->plt_call_align(this->plt_call_size(cs
));
5365 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
5369 typedef typename
Branch_stub_entries::const_iterator branch_iter
;
5370 for (branch_iter bs
= this->long_branch_stubs_
.begin();
5371 bs
!= this->long_branch_stubs_
.end();
5374 if (bs
->first
.save_res_
)
5377 char* name
= new char[8 + 13 + 16 + 1];
5378 sprintf(name
, "%08x.long_branch.%llx", this->uniq_
,
5379 static_cast<unsigned long long>(bs
->first
.dest_
));
5380 Address value
= (this->stub_address() - this->address()
5381 + this->plt_size_
+ bs
->second
);
5382 unsigned int stub_size
= this->branch_stub_size(bs
);
5383 this->targ_
->define_local(symtab
, name
, this, value
, stub_size
);
5387 // Write out plt and long branch stub code.
5389 template<int size
, bool big_endian
>
5391 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
5393 if (this->plt_call_stubs_
.empty()
5394 && this->long_branch_stubs_
.empty())
5397 const section_size_type start_off
= this->offset();
5398 const section_size_type off
= this->stub_offset();
5399 const section_size_type oview_size
=
5400 convert_to_section_size_type(this->data_size() - (off
- start_off
));
5401 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5406 const Output_data_got_powerpc
<size
, big_endian
>* got
5407 = this->targ_
->got_section();
5408 Address got_os_addr
= got
->output_section()->address();
5410 if (!this->plt_call_stubs_
.empty())
5412 // Write out plt call stubs.
5413 typename
Plt_stub_entries::const_iterator cs
;
5414 for (cs
= this->plt_call_stubs_
.begin();
5415 cs
!= this->plt_call_stubs_
.end();
5418 const Output_data_plt_powerpc
<size
, big_endian
>* plt
;
5419 Address pltoff
= this->plt_off(cs
, &plt
);
5420 Address plt_addr
= pltoff
+ plt
->address();
5421 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
5422 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
5423 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
5424 Address off
= plt_addr
- got_addr
;
5426 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
5427 gold_error(_("%s: linkage table error against `%s'"),
5428 cs
->first
.object_
->name().c_str(),
5429 cs
->first
.sym_
->demangled_name().c_str());
5431 bool plt_load_toc
= this->targ_
->abiversion() < 2;
5433 = plt_load_toc
&& parameters
->options().plt_static_chain();
5435 = plt_load_toc
&& this->targ_
->plt_thread_safe();
5436 bool use_fake_dep
= false;
5437 Address cmp_branch_off
= 0;
5440 unsigned int pltindex
5441 = ((pltoff
- this->targ_
->first_plt_entry_offset())
5442 / this->targ_
->plt_entry_size());
5444 = (this->targ_
->glink_section()->pltresolve_size()
5446 if (pltindex
> 32768)
5447 glinkoff
+= (pltindex
- 32768) * 4;
5449 = this->targ_
->glink_section()->address() + glinkoff
;
5451 = (this->stub_address() + cs
->second
.off_
+ 20
5452 + 4 * cs
->second
.r2save_
5453 + 4 * (ha(off
) != 0)
5454 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
5455 + 4 * static_chain
);
5456 cmp_branch_off
= to
- from
;
5457 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
5460 p
= oview
+ cs
->second
.off_
;
5461 const Symbol
* gsym
= cs
->first
.sym_
;
5462 if (this->targ_
->is_tls_get_addr_opt(gsym
))
5464 write_insn
<big_endian
>(p
, ld_11_3
+ 0);
5466 write_insn
<big_endian
>(p
, ld_12_3
+ 8);
5468 write_insn
<big_endian
>(p
, mr_0_3
);
5470 write_insn
<big_endian
>(p
, cmpdi_11_0
);
5472 write_insn
<big_endian
>(p
, add_3_12_13
);
5474 write_insn
<big_endian
>(p
, beqlr
);
5476 write_insn
<big_endian
>(p
, mr_3_0
);
5478 if (!cs
->second
.localentry0_
)
5480 write_insn
<big_endian
>(p
, mflr_11
);
5482 write_insn
<big_endian
>(p
, (std_11_1
5483 + this->targ_
->stk_linker()));
5486 use_fake_dep
= thread_safe
;
5490 if (cs
->second
.r2save_
)
5492 write_insn
<big_endian
>(p
,
5493 std_2_1
+ this->targ_
->stk_toc());
5498 write_insn
<big_endian
>(p
, addis_11_2
+ ha(off
));
5500 write_insn
<big_endian
>(p
, ld_12_11
+ l(off
));
5505 write_insn
<big_endian
>(p
, addis_12_2
+ ha(off
));
5507 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
));
5511 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
5513 write_insn
<big_endian
>(p
, addi_11_11
+ l(off
));
5517 write_insn
<big_endian
>(p
, mtctr_12
);
5523 write_insn
<big_endian
>(p
, xor_2_12_12
);
5525 write_insn
<big_endian
>(p
, add_11_11_2
);
5528 write_insn
<big_endian
>(p
, ld_2_11
+ l(off
+ 8));
5532 write_insn
<big_endian
>(p
, ld_11_11
+ l(off
+ 16));
5539 if (cs
->second
.r2save_
)
5541 write_insn
<big_endian
>(p
,
5542 std_2_1
+ this->targ_
->stk_toc());
5545 write_insn
<big_endian
>(p
, ld_12_2
+ l(off
));
5548 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
5550 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
));
5554 write_insn
<big_endian
>(p
, mtctr_12
);
5560 write_insn
<big_endian
>(p
, xor_11_12_12
);
5562 write_insn
<big_endian
>(p
, add_2_2_11
);
5567 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16));
5570 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8));
5574 if (!cs
->second
.localentry0_
5575 && this->targ_
->is_tls_get_addr_opt(gsym
))
5577 write_insn
<big_endian
>(p
, bctrl
);
5579 write_insn
<big_endian
>(p
, ld_2_1
+ this->targ_
->stk_toc());
5581 write_insn
<big_endian
>(p
, ld_11_1
+ this->targ_
->stk_linker());
5583 write_insn
<big_endian
>(p
, mtlr_11
);
5585 write_insn
<big_endian
>(p
, blr
);
5587 else if (thread_safe
&& !use_fake_dep
)
5589 write_insn
<big_endian
>(p
, cmpldi_2_0
);
5591 write_insn
<big_endian
>(p
, bnectr_p4
);
5593 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
5596 write_insn
<big_endian
>(p
, bctr
);
5600 // Write out long branch stubs.
5601 typename
Branch_stub_entries::const_iterator bs
;
5602 for (bs
= this->long_branch_stubs_
.begin();
5603 bs
!= this->long_branch_stubs_
.end();
5606 if (bs
->first
.save_res_
)
5608 p
= oview
+ this->plt_size_
+ bs
->second
;
5609 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
5610 Address delta
= bs
->first
.dest_
- loc
;
5611 if (delta
+ (1 << 25) < 2 << 25)
5612 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
5616 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
5617 gold_assert(brlt_addr
!= invalid_address
);
5618 brlt_addr
+= this->targ_
->brlt_section()->address();
5619 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
5620 Address brltoff
= brlt_addr
- got_addr
;
5621 if (ha(brltoff
) == 0)
5623 write_insn
<big_endian
>(p
, ld_12_2
+ l(brltoff
)), p
+= 4;
5627 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
5628 write_insn
<big_endian
>(p
, ld_12_12
+ l(brltoff
)), p
+= 4;
5630 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5631 write_insn
<big_endian
>(p
, bctr
);
5637 if (!this->plt_call_stubs_
.empty())
5639 // The address of _GLOBAL_OFFSET_TABLE_.
5640 Address g_o_t
= invalid_address
;
5642 // Write out plt call stubs.
5643 typename
Plt_stub_entries::const_iterator cs
;
5644 for (cs
= this->plt_call_stubs_
.begin();
5645 cs
!= this->plt_call_stubs_
.end();
5648 const Output_data_plt_powerpc
<size
, big_endian
>* plt
;
5649 Address plt_addr
= this->plt_off(cs
, &plt
);
5650 plt_addr
+= plt
->address();
5652 p
= oview
+ cs
->second
.off_
;
5653 const Symbol
* gsym
= cs
->first
.sym_
;
5654 if (this->targ_
->is_tls_get_addr_opt(gsym
))
5656 write_insn
<big_endian
>(p
, lwz_11_3
+ 0);
5658 write_insn
<big_endian
>(p
, lwz_12_3
+ 4);
5660 write_insn
<big_endian
>(p
, mr_0_3
);
5662 write_insn
<big_endian
>(p
, cmpwi_11_0
);
5664 write_insn
<big_endian
>(p
, add_3_12_2
);
5666 write_insn
<big_endian
>(p
, beqlr
);
5668 write_insn
<big_endian
>(p
, mr_3_0
);
5670 write_insn
<big_endian
>(p
, nop
);
5673 if (parameters
->options().output_is_position_independent())
5676 const Powerpc_relobj
<size
, big_endian
>* ppcobj
5677 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
5678 (cs
->first
.object_
));
5679 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
5681 unsigned int got2
= ppcobj
->got2_shndx();
5682 got_addr
= ppcobj
->get_output_section_offset(got2
);
5683 gold_assert(got_addr
!= invalid_address
);
5684 got_addr
+= (ppcobj
->output_section(got2
)->address()
5685 + cs
->first
.addend_
);
5689 if (g_o_t
== invalid_address
)
5691 const Output_data_got_powerpc
<size
, big_endian
>* got
5692 = this->targ_
->got_section();
5693 g_o_t
= got
->address() + got
->g_o_t();
5698 Address off
= plt_addr
- got_addr
;
5700 write_insn
<big_endian
>(p
, lwz_11_30
+ l(off
));
5703 write_insn
<big_endian
>(p
, addis_11_30
+ ha(off
));
5705 write_insn
<big_endian
>(p
, lwz_11_11
+ l(off
));
5710 write_insn
<big_endian
>(p
, lis_11
+ ha(plt_addr
));
5712 write_insn
<big_endian
>(p
, lwz_11_11
+ l(plt_addr
));
5715 write_insn
<big_endian
>(p
, mtctr_11
);
5717 write_insn
<big_endian
>(p
, bctr
);
5721 // Write out long branch stubs.
5722 typename
Branch_stub_entries::const_iterator bs
;
5723 for (bs
= this->long_branch_stubs_
.begin();
5724 bs
!= this->long_branch_stubs_
.end();
5727 if (bs
->first
.save_res_
)
5729 p
= oview
+ this->plt_size_
+ bs
->second
;
5730 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
5731 Address delta
= bs
->first
.dest_
- loc
;
5732 if (delta
+ (1 << 25) < 2 << 25)
5733 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
5734 else if (!parameters
->options().output_is_position_independent())
5736 write_insn
<big_endian
>(p
, lis_12
+ ha(bs
->first
.dest_
));
5738 write_insn
<big_endian
>(p
, addi_12_12
+ l(bs
->first
.dest_
));
5743 write_insn
<big_endian
>(p
, mflr_0
);
5745 write_insn
<big_endian
>(p
, bcl_20_31
);
5747 write_insn
<big_endian
>(p
, mflr_12
);
5749 write_insn
<big_endian
>(p
, addis_12_12
+ ha(delta
));
5751 write_insn
<big_endian
>(p
, addi_12_12
+ l(delta
));
5753 write_insn
<big_endian
>(p
, mtlr_0
);
5756 write_insn
<big_endian
>(p
, mtctr_12
);
5758 write_insn
<big_endian
>(p
, bctr
);
5761 if (this->need_save_res_
)
5763 p
= oview
+ this->plt_size_
+ this->branch_size_
;
5764 memcpy (p
, this->targ_
->savres_section()->contents(),
5765 this->targ_
->savres_section()->data_size());
5769 // Write out .glink.
5771 template<int size
, bool big_endian
>
5773 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
5775 const section_size_type off
= this->offset();
5776 const section_size_type oview_size
=
5777 convert_to_section_size_type(this->data_size());
5778 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5781 // The base address of the .plt section.
5782 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
5783 Address plt_base
= this->targ_
->plt_section()->address();
5787 if (this->end_branch_table_
!= 0)
5789 // Write pltresolve stub.
5791 Address after_bcl
= this->address() + 16;
5792 Address pltoff
= plt_base
- after_bcl
;
5794 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
5796 if (this->targ_
->abiversion() < 2)
5798 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
5799 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5800 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5801 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5802 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
5803 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5804 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5805 write_insn
<big_endian
>(p
, ld_2_11
+ 8), p
+= 4;
5806 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5807 write_insn
<big_endian
>(p
, ld_11_11
+ 16), p
+= 4;
5811 write_insn
<big_endian
>(p
, mflr_0
), p
+= 4;
5812 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
5813 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
5814 write_insn
<big_endian
>(p
, std_2_1
+ 24), p
+= 4;
5815 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
5816 write_insn
<big_endian
>(p
, mtlr_0
), p
+= 4;
5817 write_insn
<big_endian
>(p
, sub_12_12_11
), p
+= 4;
5818 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
5819 write_insn
<big_endian
>(p
, addi_0_12
+ l(-48)), p
+= 4;
5820 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
5821 write_insn
<big_endian
>(p
, srdi_0_0_2
), p
+= 4;
5822 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5823 write_insn
<big_endian
>(p
, ld_11_11
+ 8), p
+= 4;
5825 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
5826 gold_assert(p
== oview
+ this->pltresolve_size());
5828 // Write lazy link call stubs.
5830 while (p
< oview
+ this->end_branch_table_
)
5832 if (this->targ_
->abiversion() < 2)
5836 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
5840 write_insn
<big_endian
>(p
, lis_0
+ hi(indx
)), p
+= 4;
5841 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
5844 uint32_t branch_off
= 8 - (p
- oview
);
5845 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
5850 Address plt_base
= this->targ_
->plt_section()->address();
5851 Address iplt_base
= invalid_address
;
5852 unsigned int global_entry_off
= this->global_entry_off();
5853 Address global_entry_base
= this->address() + global_entry_off
;
5854 typename
Global_entry_stub_entries::const_iterator ge
;
5855 for (ge
= this->global_entry_stubs_
.begin();
5856 ge
!= this->global_entry_stubs_
.end();
5859 p
= oview
+ global_entry_off
+ ge
->second
;
5860 Address plt_addr
= ge
->first
->plt_offset();
5861 if (ge
->first
->type() == elfcpp::STT_GNU_IFUNC
5862 && ge
->first
->can_use_relative_reloc(false))
5864 if (iplt_base
== invalid_address
)
5865 iplt_base
= this->targ_
->iplt_section()->address();
5866 plt_addr
+= iplt_base
;
5869 plt_addr
+= plt_base
;
5870 Address my_addr
= global_entry_base
+ ge
->second
;
5871 Address off
= plt_addr
- my_addr
;
5873 if (off
+ 0x80008000 > 0xffffffff || (off
& 3) != 0)
5874 gold_error(_("%s: linkage table error against `%s'"),
5875 ge
->first
->object()->name().c_str(),
5876 ge
->first
->demangled_name().c_str());
5878 write_insn
<big_endian
>(p
, addis_12_12
+ ha(off
)), p
+= 4;
5879 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
)), p
+= 4;
5880 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
5881 write_insn
<big_endian
>(p
, bctr
);
5886 const Output_data_got_powerpc
<size
, big_endian
>* got
5887 = this->targ_
->got_section();
5888 // The address of _GLOBAL_OFFSET_TABLE_.
5889 Address g_o_t
= got
->address() + got
->g_o_t();
5891 // Write out pltresolve branch table.
5893 unsigned int the_end
= oview_size
- this->pltresolve_size();
5894 unsigned char* end_p
= oview
+ the_end
;
5895 while (p
< end_p
- 8 * 4)
5896 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
5898 write_insn
<big_endian
>(p
, nop
), p
+= 4;
5900 // Write out pltresolve call stub.
5901 end_p
= oview
+ oview_size
;
5902 if (parameters
->options().output_is_position_independent())
5904 Address res0_off
= 0;
5905 Address after_bcl_off
= the_end
+ 12;
5906 Address bcl_res0
= after_bcl_off
- res0_off
;
5908 write_insn
<big_endian
>(p
, addis_11_11
+ ha(bcl_res0
));
5910 write_insn
<big_endian
>(p
, mflr_0
);
5912 write_insn
<big_endian
>(p
, bcl_20_31
);
5914 write_insn
<big_endian
>(p
, addi_11_11
+ l(bcl_res0
));
5916 write_insn
<big_endian
>(p
, mflr_12
);
5918 write_insn
<big_endian
>(p
, mtlr_0
);
5920 write_insn
<big_endian
>(p
, sub_11_11_12
);
5923 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
5925 write_insn
<big_endian
>(p
, addis_12_12
+ ha(got_bcl
));
5927 if (ha(got_bcl
) == ha(got_bcl
+ 4))
5929 write_insn
<big_endian
>(p
, lwz_0_12
+ l(got_bcl
));
5931 write_insn
<big_endian
>(p
, lwz_12_12
+ l(got_bcl
+ 4));
5935 write_insn
<big_endian
>(p
, lwzu_0_12
+ l(got_bcl
));
5937 write_insn
<big_endian
>(p
, lwz_12_12
+ 4);
5940 write_insn
<big_endian
>(p
, mtctr_0
);
5942 write_insn
<big_endian
>(p
, add_0_11_11
);
5944 write_insn
<big_endian
>(p
, add_11_0_11
);
5948 Address res0
= this->address();
5950 write_insn
<big_endian
>(p
, lis_12
+ ha(g_o_t
+ 4));
5952 write_insn
<big_endian
>(p
, addis_11_11
+ ha(-res0
));
5954 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5955 write_insn
<big_endian
>(p
, lwz_0_12
+ l(g_o_t
+ 4));
5957 write_insn
<big_endian
>(p
, lwzu_0_12
+ l(g_o_t
+ 4));
5959 write_insn
<big_endian
>(p
, addi_11_11
+ l(-res0
));
5961 write_insn
<big_endian
>(p
, mtctr_0
);
5963 write_insn
<big_endian
>(p
, add_0_11_11
);
5965 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
5966 write_insn
<big_endian
>(p
, lwz_12_12
+ l(g_o_t
+ 8));
5968 write_insn
<big_endian
>(p
, lwz_12_12
+ 4);
5970 write_insn
<big_endian
>(p
, add_11_0_11
);
5973 write_insn
<big_endian
>(p
, bctr
);
5977 write_insn
<big_endian
>(p
, nop
);
5982 of
->write_output_view(off
, oview_size
, oview
);
5986 // A class to handle linker generated save/restore functions.
5988 template<int size
, bool big_endian
>
5989 class Output_data_save_res
: public Output_section_data_build
5992 Output_data_save_res(Symbol_table
* symtab
);
5994 const unsigned char*
6001 // Write to a map file.
6003 do_print_to_mapfile(Mapfile
* mapfile
) const
6004 { mapfile
->print_output_data(this, _("** save/restore")); }
6007 do_write(Output_file
*);
6010 // The maximum size of save/restore contents.
6011 static const unsigned int savres_max
= 218*4;
6014 savres_define(Symbol_table
* symtab
,
6016 unsigned int lo
, unsigned int hi
,
6017 unsigned char* write_ent(unsigned char*, int),
6018 unsigned char* write_tail(unsigned char*, int));
6020 unsigned char *contents_
;
6023 template<bool big_endian
>
6024 static unsigned char*
6025 savegpr0(unsigned char* p
, int r
)
6027 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
6028 write_insn
<big_endian
>(p
, insn
);
6032 template<bool big_endian
>
6033 static unsigned char*
6034 savegpr0_tail(unsigned char* p
, int r
)
6036 p
= savegpr0
<big_endian
>(p
, r
);
6037 uint32_t insn
= std_0_1
+ 16;
6038 write_insn
<big_endian
>(p
, insn
);
6040 write_insn
<big_endian
>(p
, blr
);
6044 template<bool big_endian
>
6045 static unsigned char*
6046 restgpr0(unsigned char* p
, int r
)
6048 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
6049 write_insn
<big_endian
>(p
, insn
);
6053 template<bool big_endian
>
6054 static unsigned char*
6055 restgpr0_tail(unsigned char* p
, int r
)
6057 uint32_t insn
= ld_0_1
+ 16;
6058 write_insn
<big_endian
>(p
, insn
);
6060 p
= restgpr0
<big_endian
>(p
, r
);
6061 write_insn
<big_endian
>(p
, mtlr_0
);
6065 p
= restgpr0
<big_endian
>(p
, 30);
6066 p
= restgpr0
<big_endian
>(p
, 31);
6068 write_insn
<big_endian
>(p
, blr
);
6072 template<bool big_endian
>
6073 static unsigned char*
6074 savegpr1(unsigned char* p
, int r
)
6076 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
6077 write_insn
<big_endian
>(p
, insn
);
6081 template<bool big_endian
>
6082 static unsigned char*
6083 savegpr1_tail(unsigned char* p
, int r
)
6085 p
= savegpr1
<big_endian
>(p
, r
);
6086 write_insn
<big_endian
>(p
, blr
);
6090 template<bool big_endian
>
6091 static unsigned char*
6092 restgpr1(unsigned char* p
, int r
)
6094 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
6095 write_insn
<big_endian
>(p
, insn
);
6099 template<bool big_endian
>
6100 static unsigned char*
6101 restgpr1_tail(unsigned char* p
, int r
)
6103 p
= restgpr1
<big_endian
>(p
, r
);
6104 write_insn
<big_endian
>(p
, blr
);
6108 template<bool big_endian
>
6109 static unsigned char*
6110 savefpr(unsigned char* p
, int r
)
6112 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
6113 write_insn
<big_endian
>(p
, insn
);
6117 template<bool big_endian
>
6118 static unsigned char*
6119 savefpr0_tail(unsigned char* p
, int r
)
6121 p
= savefpr
<big_endian
>(p
, r
);
6122 write_insn
<big_endian
>(p
, std_0_1
+ 16);
6124 write_insn
<big_endian
>(p
, blr
);
6128 template<bool big_endian
>
6129 static unsigned char*
6130 restfpr(unsigned char* p
, int r
)
6132 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
6133 write_insn
<big_endian
>(p
, insn
);
6137 template<bool big_endian
>
6138 static unsigned char*
6139 restfpr0_tail(unsigned char* p
, int r
)
6141 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
6143 p
= restfpr
<big_endian
>(p
, r
);
6144 write_insn
<big_endian
>(p
, mtlr_0
);
6148 p
= restfpr
<big_endian
>(p
, 30);
6149 p
= restfpr
<big_endian
>(p
, 31);
6151 write_insn
<big_endian
>(p
, blr
);
6155 template<bool big_endian
>
6156 static unsigned char*
6157 savefpr1_tail(unsigned char* p
, int r
)
6159 p
= savefpr
<big_endian
>(p
, r
);
6160 write_insn
<big_endian
>(p
, blr
);
6164 template<bool big_endian
>
6165 static unsigned char*
6166 restfpr1_tail(unsigned char* p
, int r
)
6168 p
= restfpr
<big_endian
>(p
, r
);
6169 write_insn
<big_endian
>(p
, blr
);
6173 template<bool big_endian
>
6174 static unsigned char*
6175 savevr(unsigned char* p
, int r
)
6177 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
6178 write_insn
<big_endian
>(p
, insn
);
6180 insn
= stvx_0_12_0
+ (r
<< 21);
6181 write_insn
<big_endian
>(p
, insn
);
6185 template<bool big_endian
>
6186 static unsigned char*
6187 savevr_tail(unsigned char* p
, int r
)
6189 p
= savevr
<big_endian
>(p
, r
);
6190 write_insn
<big_endian
>(p
, blr
);
6194 template<bool big_endian
>
6195 static unsigned char*
6196 restvr(unsigned char* p
, int r
)
6198 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
6199 write_insn
<big_endian
>(p
, insn
);
6201 insn
= lvx_0_12_0
+ (r
<< 21);
6202 write_insn
<big_endian
>(p
, insn
);
6206 template<bool big_endian
>
6207 static unsigned char*
6208 restvr_tail(unsigned char* p
, int r
)
6210 p
= restvr
<big_endian
>(p
, r
);
6211 write_insn
<big_endian
>(p
, blr
);
6216 template<int size
, bool big_endian
>
6217 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
6218 Symbol_table
* symtab
)
6219 : Output_section_data_build(4),
6222 this->savres_define(symtab
,
6223 "_savegpr0_", 14, 31,
6224 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
6225 this->savres_define(symtab
,
6226 "_restgpr0_", 14, 29,
6227 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
6228 this->savres_define(symtab
,
6229 "_restgpr0_", 30, 31,
6230 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
6231 this->savres_define(symtab
,
6232 "_savegpr1_", 14, 31,
6233 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
6234 this->savres_define(symtab
,
6235 "_restgpr1_", 14, 31,
6236 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
6237 this->savres_define(symtab
,
6238 "_savefpr_", 14, 31,
6239 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
6240 this->savres_define(symtab
,
6241 "_restfpr_", 14, 29,
6242 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
6243 this->savres_define(symtab
,
6244 "_restfpr_", 30, 31,
6245 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
6246 this->savres_define(symtab
,
6248 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
6249 this->savres_define(symtab
,
6251 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
6252 this->savres_define(symtab
,
6254 savevr
<big_endian
>, savevr_tail
<big_endian
>);
6255 this->savres_define(symtab
,
6257 restvr
<big_endian
>, restvr_tail
<big_endian
>);
6260 template<int size
, bool big_endian
>
6262 Output_data_save_res
<size
, big_endian
>::savres_define(
6263 Symbol_table
* symtab
,
6265 unsigned int lo
, unsigned int hi
,
6266 unsigned char* write_ent(unsigned char*, int),
6267 unsigned char* write_tail(unsigned char*, int))
6269 size_t len
= strlen(name
);
6270 bool writing
= false;
6273 memcpy(sym
, name
, len
);
6276 for (unsigned int i
= lo
; i
<= hi
; i
++)
6278 sym
[len
+ 0] = i
/ 10 + '0';
6279 sym
[len
+ 1] = i
% 10 + '0';
6280 Symbol
* gsym
= symtab
->lookup(sym
);
6281 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
6282 writing
= writing
|| refd
;
6285 if (this->contents_
== NULL
)
6286 this->contents_
= new unsigned char[this->savres_max
];
6288 section_size_type value
= this->current_data_size();
6289 unsigned char* p
= this->contents_
+ value
;
6291 p
= write_ent(p
, i
);
6293 p
= write_tail(p
, i
);
6294 section_size_type cur_size
= p
- this->contents_
;
6295 this->set_current_data_size(cur_size
);
6297 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
6298 this, value
, cur_size
- value
,
6299 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
6300 elfcpp::STV_HIDDEN
, 0, false, false);
6305 // Write out save/restore.
6307 template<int size
, bool big_endian
>
6309 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
6311 const section_size_type off
= this->offset();
6312 const section_size_type oview_size
=
6313 convert_to_section_size_type(this->data_size());
6314 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
6315 memcpy(oview
, this->contents_
, oview_size
);
6316 of
->write_output_view(off
, oview_size
, oview
);
6320 // Create the glink section.
6322 template<int size
, bool big_endian
>
6324 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
6326 if (this->glink_
== NULL
)
6328 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
6329 this->glink_
->add_eh_frame(layout
);
6330 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
6331 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
6332 this->glink_
, ORDER_TEXT
, false);
6336 // Create a PLT entry for a global symbol.
6338 template<int size
, bool big_endian
>
6340 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
6344 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
6345 && gsym
->can_use_relative_reloc(false))
6347 if (this->iplt_
== NULL
)
6348 this->make_iplt_section(symtab
, layout
);
6349 this->iplt_
->add_ifunc_entry(gsym
);
6353 if (this->plt_
== NULL
)
6354 this->make_plt_section(symtab
, layout
);
6355 this->plt_
->add_entry(gsym
);
6359 // Make a PLT entry for a local symbol.
6361 template<int size
, bool big_endian
>
6363 Target_powerpc
<size
, big_endian
>::make_local_plt_entry(
6365 Sized_relobj_file
<size
, big_endian
>* relobj
,
6368 if (this->lplt_
== NULL
)
6369 this->make_lplt_section(layout
);
6370 this->lplt_
->add_local_entry(relobj
, r_sym
);
6373 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
6375 template<int size
, bool big_endian
>
6377 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
6378 Symbol_table
* symtab
,
6380 Sized_relobj_file
<size
, big_endian
>* relobj
,
6383 if (this->iplt_
== NULL
)
6384 this->make_iplt_section(symtab
, layout
);
6385 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
6388 // Return the number of entries in the PLT.
6390 template<int size
, bool big_endian
>
6392 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
6394 if (this->plt_
== NULL
)
6396 return this->plt_
->entry_count();
6399 // Create a GOT entry for local dynamic __tls_get_addr calls.
6401 template<int size
, bool big_endian
>
6403 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
6404 Symbol_table
* symtab
,
6406 Sized_relobj_file
<size
, big_endian
>* object
)
6408 if (this->tlsld_got_offset_
== -1U)
6410 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
6411 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
6412 Output_data_got_powerpc
<size
, big_endian
>* got
6413 = this->got_section(symtab
, layout
);
6414 unsigned int got_offset
= got
->add_constant_pair(0, 0);
6415 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
6417 this->tlsld_got_offset_
= got_offset
;
6419 return this->tlsld_got_offset_
;
6422 // Get the Reference_flags for a particular relocation.
6424 template<int size
, bool big_endian
>
6426 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(
6427 unsigned int r_type
,
6428 const Target_powerpc
* target
)
6434 case elfcpp::R_POWERPC_NONE
:
6435 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6436 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6437 case elfcpp::R_PPC64_TOC
:
6438 // No symbol reference.
6441 case elfcpp::R_PPC64_ADDR64
:
6442 case elfcpp::R_PPC64_UADDR64
:
6443 case elfcpp::R_POWERPC_ADDR32
:
6444 case elfcpp::R_POWERPC_UADDR32
:
6445 case elfcpp::R_POWERPC_ADDR16
:
6446 case elfcpp::R_POWERPC_UADDR16
:
6447 case elfcpp::R_POWERPC_ADDR16_LO
:
6448 case elfcpp::R_POWERPC_ADDR16_HI
:
6449 case elfcpp::R_POWERPC_ADDR16_HA
:
6450 ref
= Symbol::ABSOLUTE_REF
;
6453 case elfcpp::R_POWERPC_ADDR24
:
6454 case elfcpp::R_POWERPC_ADDR14
:
6455 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6456 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6457 ref
= Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
6460 case elfcpp::R_PPC64_REL64
:
6461 case elfcpp::R_POWERPC_REL32
:
6462 case elfcpp::R_PPC_LOCAL24PC
:
6463 case elfcpp::R_POWERPC_REL16
:
6464 case elfcpp::R_POWERPC_REL16_LO
:
6465 case elfcpp::R_POWERPC_REL16_HI
:
6466 case elfcpp::R_POWERPC_REL16_HA
:
6467 case elfcpp::R_PPC64_REL16_HIGH
:
6468 case elfcpp::R_PPC64_REL16_HIGHA
:
6469 case elfcpp::R_PPC64_REL16_HIGHER
:
6470 case elfcpp::R_PPC64_REL16_HIGHERA
:
6471 case elfcpp::R_PPC64_REL16_HIGHEST
:
6472 case elfcpp::R_PPC64_REL16_HIGHESTA
:
6473 ref
= Symbol::RELATIVE_REF
;
6476 case elfcpp::R_POWERPC_REL24
:
6477 case elfcpp::R_PPC_PLTREL24
:
6478 case elfcpp::R_POWERPC_REL14
:
6479 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6480 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6481 ref
= Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
6484 case elfcpp::R_POWERPC_GOT16
:
6485 case elfcpp::R_POWERPC_GOT16_LO
:
6486 case elfcpp::R_POWERPC_GOT16_HI
:
6487 case elfcpp::R_POWERPC_GOT16_HA
:
6488 case elfcpp::R_PPC64_GOT16_DS
:
6489 case elfcpp::R_PPC64_GOT16_LO_DS
:
6490 case elfcpp::R_PPC64_TOC16
:
6491 case elfcpp::R_PPC64_TOC16_LO
:
6492 case elfcpp::R_PPC64_TOC16_HI
:
6493 case elfcpp::R_PPC64_TOC16_HA
:
6494 case elfcpp::R_PPC64_TOC16_DS
:
6495 case elfcpp::R_PPC64_TOC16_LO_DS
:
6496 case elfcpp::R_POWERPC_PLT16_LO
:
6497 case elfcpp::R_POWERPC_PLT16_HI
:
6498 case elfcpp::R_POWERPC_PLT16_HA
:
6499 case elfcpp::R_PPC64_PLT16_LO_DS
:
6500 ref
= Symbol::RELATIVE_REF
;
6503 case elfcpp::R_POWERPC_GOT_TPREL16
:
6504 case elfcpp::R_POWERPC_TLS
:
6505 ref
= Symbol::TLS_REF
;
6508 case elfcpp::R_POWERPC_COPY
:
6509 case elfcpp::R_POWERPC_GLOB_DAT
:
6510 case elfcpp::R_POWERPC_JMP_SLOT
:
6511 case elfcpp::R_POWERPC_RELATIVE
:
6512 case elfcpp::R_POWERPC_DTPMOD
:
6514 // Not expected. We will give an error later.
6518 if (size
== 64 && target
->abiversion() < 2)
6519 ref
|= Symbol::FUNC_DESC_ABI
;
6523 // Report an unsupported relocation against a local symbol.
6525 template<int size
, bool big_endian
>
6527 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
6528 Sized_relobj_file
<size
, big_endian
>* object
,
6529 unsigned int r_type
)
6531 gold_error(_("%s: unsupported reloc %u against local symbol"),
6532 object
->name().c_str(), r_type
);
6535 // We are about to emit a dynamic relocation of type R_TYPE. If the
6536 // dynamic linker does not support it, issue an error.
6538 template<int size
, bool big_endian
>
6540 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
6541 unsigned int r_type
)
6543 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
6545 // These are the relocation types supported by glibc for both 32-bit
6546 // and 64-bit powerpc.
6549 case elfcpp::R_POWERPC_NONE
:
6550 case elfcpp::R_POWERPC_RELATIVE
:
6551 case elfcpp::R_POWERPC_GLOB_DAT
:
6552 case elfcpp::R_POWERPC_DTPMOD
:
6553 case elfcpp::R_POWERPC_DTPREL
:
6554 case elfcpp::R_POWERPC_TPREL
:
6555 case elfcpp::R_POWERPC_JMP_SLOT
:
6556 case elfcpp::R_POWERPC_COPY
:
6557 case elfcpp::R_POWERPC_IRELATIVE
:
6558 case elfcpp::R_POWERPC_ADDR32
:
6559 case elfcpp::R_POWERPC_UADDR32
:
6560 case elfcpp::R_POWERPC_ADDR24
:
6561 case elfcpp::R_POWERPC_ADDR16
:
6562 case elfcpp::R_POWERPC_UADDR16
:
6563 case elfcpp::R_POWERPC_ADDR16_LO
:
6564 case elfcpp::R_POWERPC_ADDR16_HI
:
6565 case elfcpp::R_POWERPC_ADDR16_HA
:
6566 case elfcpp::R_POWERPC_ADDR14
:
6567 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6568 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6569 case elfcpp::R_POWERPC_REL32
:
6570 case elfcpp::R_POWERPC_REL24
:
6571 case elfcpp::R_POWERPC_TPREL16
:
6572 case elfcpp::R_POWERPC_TPREL16_LO
:
6573 case elfcpp::R_POWERPC_TPREL16_HI
:
6574 case elfcpp::R_POWERPC_TPREL16_HA
:
6585 // These are the relocation types supported only on 64-bit.
6586 case elfcpp::R_PPC64_ADDR64
:
6587 case elfcpp::R_PPC64_UADDR64
:
6588 case elfcpp::R_PPC64_JMP_IREL
:
6589 case elfcpp::R_PPC64_ADDR16_DS
:
6590 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6591 case elfcpp::R_PPC64_ADDR16_HIGH
:
6592 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6593 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6594 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6595 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6596 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6597 case elfcpp::R_PPC64_REL64
:
6598 case elfcpp::R_POWERPC_ADDR30
:
6599 case elfcpp::R_PPC64_TPREL16_DS
:
6600 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6601 case elfcpp::R_PPC64_TPREL16_HIGH
:
6602 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6603 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6604 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6605 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6606 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6617 // These are the relocation types supported only on 32-bit.
6618 // ??? glibc ld.so doesn't need to support these.
6619 case elfcpp::R_POWERPC_DTPREL16
:
6620 case elfcpp::R_POWERPC_DTPREL16_LO
:
6621 case elfcpp::R_POWERPC_DTPREL16_HI
:
6622 case elfcpp::R_POWERPC_DTPREL16_HA
:
6630 // This prevents us from issuing more than one error per reloc
6631 // section. But we can still wind up issuing more than one
6632 // error per object file.
6633 if (this->issued_non_pic_error_
)
6635 gold_assert(parameters
->options().output_is_position_independent());
6636 object
->error(_("requires unsupported dynamic reloc; "
6637 "recompile with -fPIC"));
6638 this->issued_non_pic_error_
= true;
6642 // Return whether we need to make a PLT entry for a relocation of the
6643 // given type against a STT_GNU_IFUNC symbol.
6645 template<int size
, bool big_endian
>
6647 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
6648 Target_powerpc
<size
, big_endian
>* target
,
6649 Sized_relobj_file
<size
, big_endian
>* object
,
6650 unsigned int r_type
,
6653 // In non-pic code any reference will resolve to the plt call stub
6654 // for the ifunc symbol.
6655 if ((size
== 32 || target
->abiversion() >= 2)
6656 && !parameters
->options().output_is_position_independent())
6661 // Word size refs from data sections are OK, but don't need a PLT entry.
6662 case elfcpp::R_POWERPC_ADDR32
:
6663 case elfcpp::R_POWERPC_UADDR32
:
6668 case elfcpp::R_PPC64_ADDR64
:
6669 case elfcpp::R_PPC64_UADDR64
:
6674 // GOT refs are good, but also don't need a PLT entry.
6675 case elfcpp::R_POWERPC_GOT16
:
6676 case elfcpp::R_POWERPC_GOT16_LO
:
6677 case elfcpp::R_POWERPC_GOT16_HI
:
6678 case elfcpp::R_POWERPC_GOT16_HA
:
6679 case elfcpp::R_PPC64_GOT16_DS
:
6680 case elfcpp::R_PPC64_GOT16_LO_DS
:
6683 // PLT relocs are OK and need a PLT entry.
6684 case elfcpp::R_POWERPC_PLT16_LO
:
6685 case elfcpp::R_POWERPC_PLT16_HI
:
6686 case elfcpp::R_POWERPC_PLT16_HA
:
6687 case elfcpp::R_PPC64_PLT16_LO_DS
:
6688 case elfcpp::R_POWERPC_PLTSEQ
:
6689 case elfcpp::R_POWERPC_PLTCALL
:
6693 // Function calls are good, and these do need a PLT entry.
6694 case elfcpp::R_POWERPC_ADDR24
:
6695 case elfcpp::R_POWERPC_ADDR14
:
6696 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6697 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6698 case elfcpp::R_POWERPC_REL24
:
6699 case elfcpp::R_PPC_PLTREL24
:
6700 case elfcpp::R_POWERPC_REL14
:
6701 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6702 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6709 // Anything else is a problem.
6710 // If we are building a static executable, the libc startup function
6711 // responsible for applying indirect function relocations is going
6712 // to complain about the reloc type.
6713 // If we are building a dynamic executable, we will have a text
6714 // relocation. The dynamic loader will set the text segment
6715 // writable and non-executable to apply text relocations. So we'll
6716 // segfault when trying to run the indirection function to resolve
6719 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
6720 object
->name().c_str(), r_type
);
6724 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6728 ok_lo_toc_insn(uint32_t insn
, unsigned int r_type
)
6730 return ((insn
& (0x3f << 26)) == 12u << 26 /* addic */
6731 || (insn
& (0x3f << 26)) == 14u << 26 /* addi */
6732 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
6733 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
6734 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
6735 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
6736 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
6737 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
6738 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
6739 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
6740 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
6741 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
6742 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
6743 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
6744 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
6745 || (insn
& (0x3f << 26)) == 56u << 26 /* lq,lfq */
6746 || ((insn
& (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
6747 /* Exclude lfqu by testing reloc. If relocs are ever
6748 defined for the reduced D field in psq_lu then those
6749 will need testing too. */
6750 && r_type
!= elfcpp::R_PPC64_TOC16_LO
6751 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
6752 || ((insn
& (0x3f << 26)) == 58u << 26 /* ld,lwa */
6754 || (insn
& (0x3f << 26)) == 60u << 26 /* stfq */
6755 || ((insn
& (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
6756 /* Exclude stfqu. psq_stu as above for psq_lu. */
6757 && r_type
!= elfcpp::R_PPC64_TOC16_LO
6758 && r_type
!= elfcpp::R_POWERPC_GOT16_LO
)
6759 || ((insn
& (0x3f << 26)) == 62u << 26 /* std,stq */
6760 && (insn
& 1) == 0));
6763 // Scan a relocation for a local symbol.
6765 template<int size
, bool big_endian
>
6767 Target_powerpc
<size
, big_endian
>::Scan::local(
6768 Symbol_table
* symtab
,
6770 Target_powerpc
<size
, big_endian
>* target
,
6771 Sized_relobj_file
<size
, big_endian
>* object
,
6772 unsigned int data_shndx
,
6773 Output_section
* output_section
,
6774 const elfcpp::Rela
<size
, big_endian
>& reloc
,
6775 unsigned int r_type
,
6776 const elfcpp::Sym
<size
, big_endian
>& lsym
,
6779 this->maybe_skip_tls_get_addr_call(target
, r_type
, NULL
);
6781 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6782 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6784 this->expect_tls_get_addr_call();
6785 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
6786 if (tls_type
!= tls::TLSOPT_NONE
)
6787 this->skip_next_tls_get_addr_call();
6789 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6790 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6792 this->expect_tls_get_addr_call();
6793 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6794 if (tls_type
!= tls::TLSOPT_NONE
)
6795 this->skip_next_tls_get_addr_call();
6798 Powerpc_relobj
<size
, big_endian
>* ppc_object
6799 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6804 && data_shndx
== ppc_object
->opd_shndx()
6805 && r_type
== elfcpp::R_PPC64_ADDR64
)
6806 ppc_object
->set_opd_discard(reloc
.get_r_offset());
6810 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6811 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
6812 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
6814 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6815 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6816 r_type
, r_sym
, reloc
.get_r_addend());
6817 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
6822 case elfcpp::R_POWERPC_NONE
:
6823 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6824 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6825 case elfcpp::R_POWERPC_TLS
:
6826 case elfcpp::R_PPC64_ENTRY
:
6827 case elfcpp::R_POWERPC_PLTSEQ
:
6828 case elfcpp::R_POWERPC_PLTCALL
:
6831 case elfcpp::R_PPC64_TOC
:
6833 Output_data_got_powerpc
<size
, big_endian
>* got
6834 = target
->got_section(symtab
, layout
);
6835 if (parameters
->options().output_is_position_independent())
6837 Address off
= reloc
.get_r_offset();
6839 && target
->abiversion() < 2
6840 && data_shndx
== ppc_object
->opd_shndx()
6841 && ppc_object
->get_opd_discard(off
- 8))
6844 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6845 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
6846 rela_dyn
->add_output_section_relative(got
->output_section(),
6847 elfcpp::R_POWERPC_RELATIVE
,
6849 object
, data_shndx
, off
,
6850 symobj
->toc_base_offset());
6855 case elfcpp::R_PPC64_ADDR64
:
6856 case elfcpp::R_PPC64_UADDR64
:
6857 case elfcpp::R_POWERPC_ADDR32
:
6858 case elfcpp::R_POWERPC_UADDR32
:
6859 case elfcpp::R_POWERPC_ADDR24
:
6860 case elfcpp::R_POWERPC_ADDR16
:
6861 case elfcpp::R_POWERPC_ADDR16_LO
:
6862 case elfcpp::R_POWERPC_ADDR16_HI
:
6863 case elfcpp::R_POWERPC_ADDR16_HA
:
6864 case elfcpp::R_POWERPC_UADDR16
:
6865 case elfcpp::R_PPC64_ADDR16_HIGH
:
6866 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6867 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6868 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6869 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6870 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6871 case elfcpp::R_PPC64_ADDR16_DS
:
6872 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6873 case elfcpp::R_POWERPC_ADDR14
:
6874 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6875 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6876 // If building a shared library (or a position-independent
6877 // executable), we need to create a dynamic relocation for
6879 if (parameters
->options().output_is_position_independent()
6880 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
6882 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
6884 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6885 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
6886 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
6888 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6889 : elfcpp::R_POWERPC_RELATIVE
);
6890 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
6891 output_section
, data_shndx
,
6892 reloc
.get_r_offset(),
6893 reloc
.get_r_addend(), false);
6895 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
6897 check_non_pic(object
, r_type
);
6898 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
6899 data_shndx
, reloc
.get_r_offset(),
6900 reloc
.get_r_addend());
6904 gold_assert(lsym
.get_st_value() == 0);
6905 unsigned int shndx
= lsym
.get_st_shndx();
6907 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
6910 object
->error(_("section symbol %u has bad shndx %u"),
6913 rela_dyn
->add_local_section(object
, shndx
, r_type
,
6914 output_section
, data_shndx
,
6915 reloc
.get_r_offset());
6920 case elfcpp::R_POWERPC_PLT16_LO
:
6921 case elfcpp::R_POWERPC_PLT16_HI
:
6922 case elfcpp::R_POWERPC_PLT16_HA
:
6923 case elfcpp::R_PPC64_PLT16_LO_DS
:
6926 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6927 target
->make_local_plt_entry(layout
, object
, r_sym
);
6931 case elfcpp::R_POWERPC_REL24
:
6932 case elfcpp::R_PPC_PLTREL24
:
6933 case elfcpp::R_PPC_LOCAL24PC
:
6934 case elfcpp::R_POWERPC_REL14
:
6935 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6936 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6939 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6940 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6941 r_type
, r_sym
, reloc
.get_r_addend());
6945 case elfcpp::R_PPC64_TOCSAVE
:
6946 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6947 // caller has already saved r2 and thus a plt call stub need not
6950 && target
->mark_pltcall(ppc_object
, data_shndx
,
6951 reloc
.get_r_offset() - 4, symtab
))
6953 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
6954 unsigned int shndx
= lsym
.get_st_shndx();
6956 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
6958 object
->error(_("tocsave symbol %u has bad shndx %u"),
6961 target
->add_tocsave(ppc_object
, shndx
,
6962 lsym
.get_st_value() + reloc
.get_r_addend());
6966 case elfcpp::R_PPC64_REL64
:
6967 case elfcpp::R_POWERPC_REL32
:
6968 case elfcpp::R_POWERPC_REL16
:
6969 case elfcpp::R_POWERPC_REL16_LO
:
6970 case elfcpp::R_POWERPC_REL16_HI
:
6971 case elfcpp::R_POWERPC_REL16_HA
:
6972 case elfcpp::R_POWERPC_REL16DX_HA
:
6973 case elfcpp::R_PPC64_REL16_HIGH
:
6974 case elfcpp::R_PPC64_REL16_HIGHA
:
6975 case elfcpp::R_PPC64_REL16_HIGHER
:
6976 case elfcpp::R_PPC64_REL16_HIGHERA
:
6977 case elfcpp::R_PPC64_REL16_HIGHEST
:
6978 case elfcpp::R_PPC64_REL16_HIGHESTA
:
6979 case elfcpp::R_POWERPC_SECTOFF
:
6980 case elfcpp::R_POWERPC_SECTOFF_LO
:
6981 case elfcpp::R_POWERPC_SECTOFF_HI
:
6982 case elfcpp::R_POWERPC_SECTOFF_HA
:
6983 case elfcpp::R_PPC64_SECTOFF_DS
:
6984 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6985 case elfcpp::R_POWERPC_TPREL16
:
6986 case elfcpp::R_POWERPC_TPREL16_LO
:
6987 case elfcpp::R_POWERPC_TPREL16_HI
:
6988 case elfcpp::R_POWERPC_TPREL16_HA
:
6989 case elfcpp::R_PPC64_TPREL16_DS
:
6990 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6991 case elfcpp::R_PPC64_TPREL16_HIGH
:
6992 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6993 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6994 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6995 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6996 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6997 case elfcpp::R_POWERPC_DTPREL16
:
6998 case elfcpp::R_POWERPC_DTPREL16_LO
:
6999 case elfcpp::R_POWERPC_DTPREL16_HI
:
7000 case elfcpp::R_POWERPC_DTPREL16_HA
:
7001 case elfcpp::R_PPC64_DTPREL16_DS
:
7002 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
7003 case elfcpp::R_PPC64_DTPREL16_HIGH
:
7004 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
7005 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
7006 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
7007 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
7008 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
7009 case elfcpp::R_PPC64_TLSGD
:
7010 case elfcpp::R_PPC64_TLSLD
:
7011 case elfcpp::R_PPC64_ADDR64_LOCAL
:
7014 case elfcpp::R_POWERPC_GOT16
:
7015 case elfcpp::R_POWERPC_GOT16_LO
:
7016 case elfcpp::R_POWERPC_GOT16_HI
:
7017 case elfcpp::R_POWERPC_GOT16_HA
:
7018 case elfcpp::R_PPC64_GOT16_DS
:
7019 case elfcpp::R_PPC64_GOT16_LO_DS
:
7021 // The symbol requires a GOT entry.
7022 Output_data_got_powerpc
<size
, big_endian
>* got
7023 = target
->got_section(symtab
, layout
);
7024 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7026 if (!parameters
->options().output_is_position_independent())
7029 && (size
== 32 || target
->abiversion() >= 2))
7030 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
7032 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
7034 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
7036 // If we are generating a shared object or a pie, this
7037 // symbol's GOT entry will be set by a dynamic relocation.
7039 off
= got
->add_constant(0);
7040 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
7042 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
7044 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
7045 : elfcpp::R_POWERPC_RELATIVE
);
7046 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
7047 got
, off
, 0, false);
7052 case elfcpp::R_PPC64_TOC16
:
7053 case elfcpp::R_PPC64_TOC16_LO
:
7054 case elfcpp::R_PPC64_TOC16_HI
:
7055 case elfcpp::R_PPC64_TOC16_HA
:
7056 case elfcpp::R_PPC64_TOC16_DS
:
7057 case elfcpp::R_PPC64_TOC16_LO_DS
:
7058 // We need a GOT section.
7059 target
->got_section(symtab
, layout
);
7062 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7063 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7064 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
7065 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7067 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
7068 if (tls_type
== tls::TLSOPT_NONE
)
7070 Output_data_got_powerpc
<size
, big_endian
>* got
7071 = target
->got_section(symtab
, layout
);
7072 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7073 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7074 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
7075 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
7077 else if (tls_type
== tls::TLSOPT_TO_LE
)
7079 // no GOT relocs needed for Local Exec.
7086 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7087 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7088 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
7089 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7091 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7092 if (tls_type
== tls::TLSOPT_NONE
)
7093 target
->tlsld_got_offset(symtab
, layout
, object
);
7094 else if (tls_type
== tls::TLSOPT_TO_LE
)
7096 // no GOT relocs needed for Local Exec.
7097 if (parameters
->options().emit_relocs())
7099 Output_section
* os
= layout
->tls_segment()->first_section();
7100 gold_assert(os
!= NULL
);
7101 os
->set_needs_symtab_index();
7109 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7110 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7111 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
7112 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7114 Output_data_got_powerpc
<size
, big_endian
>* got
7115 = target
->got_section(symtab
, layout
);
7116 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7117 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
7121 case elfcpp::R_POWERPC_GOT_TPREL16
:
7122 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7123 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7124 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7126 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
7127 if (tls_type
== tls::TLSOPT_NONE
)
7129 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7130 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
7132 Output_data_got_powerpc
<size
, big_endian
>* got
7133 = target
->got_section(symtab
, layout
);
7134 unsigned int off
= got
->add_constant(0);
7135 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
7137 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7138 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
7139 elfcpp::R_POWERPC_TPREL
,
7143 else if (tls_type
== tls::TLSOPT_TO_LE
)
7145 // no GOT relocs needed for Local Exec.
7153 unsupported_reloc_local(object
, r_type
);
7158 && parameters
->options().toc_optimize())
7160 if (data_shndx
== ppc_object
->toc_shndx())
7163 if (r_type
!= elfcpp::R_PPC64_ADDR64
7164 || (is_ifunc
&& target
->abiversion() < 2))
7166 else if (parameters
->options().output_is_position_independent())
7172 unsigned int shndx
= lsym
.get_st_shndx();
7173 if (shndx
>= elfcpp::SHN_LORESERVE
7174 && shndx
!= elfcpp::SHN_XINDEX
)
7179 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
7182 enum {no_check
, check_lo
, check_ha
} insn_check
;
7186 insn_check
= no_check
;
7189 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7190 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7191 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7192 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7193 case elfcpp::R_POWERPC_GOT16_HA
:
7194 case elfcpp::R_PPC64_TOC16_HA
:
7195 insn_check
= check_ha
;
7198 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7199 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7200 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7201 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7202 case elfcpp::R_POWERPC_GOT16_LO
:
7203 case elfcpp::R_PPC64_GOT16_LO_DS
:
7204 case elfcpp::R_PPC64_TOC16_LO
:
7205 case elfcpp::R_PPC64_TOC16_LO_DS
:
7206 insn_check
= check_lo
;
7210 section_size_type slen
;
7211 const unsigned char* view
= NULL
;
7212 if (insn_check
!= no_check
)
7214 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
7215 section_size_type off
=
7216 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
7219 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
7220 if (insn_check
== check_lo
7221 ? !ok_lo_toc_insn(insn
, r_type
)
7222 : ((insn
& ((0x3f << 26) | 0x1f << 16))
7223 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7225 ppc_object
->set_no_toc_opt();
7226 gold_warning(_("%s: toc optimization is not supported "
7227 "for %#08x instruction"),
7228 ppc_object
->name().c_str(), insn
);
7237 case elfcpp::R_PPC64_TOC16
:
7238 case elfcpp::R_PPC64_TOC16_LO
:
7239 case elfcpp::R_PPC64_TOC16_HI
:
7240 case elfcpp::R_PPC64_TOC16_HA
:
7241 case elfcpp::R_PPC64_TOC16_DS
:
7242 case elfcpp::R_PPC64_TOC16_LO_DS
:
7243 unsigned int shndx
= lsym
.get_st_shndx();
7244 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7246 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
7247 if (is_ordinary
&& shndx
== ppc_object
->toc_shndx())
7249 Address dst_off
= lsym
.get_st_value() + reloc
.get_r_addend();
7250 if (dst_off
< ppc_object
->section_size(shndx
))
7253 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
7255 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
7257 // Need to check that the insn is a ld
7259 view
= ppc_object
->section_contents(data_shndx
,
7262 section_size_type off
=
7263 (convert_to_section_size_type(reloc
.get_r_offset())
7264 + (big_endian
? -2 : 3));
7266 && (view
[off
] & (0x3f << 2)) == 58u << 2)
7270 ppc_object
->set_no_toc_opt(dst_off
);
7281 case elfcpp::R_POWERPC_REL32
:
7282 if (ppc_object
->got2_shndx() != 0
7283 && parameters
->options().output_is_position_independent())
7285 unsigned int shndx
= lsym
.get_st_shndx();
7286 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7288 shndx
= ppc_object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
7289 if (is_ordinary
&& shndx
== ppc_object
->got2_shndx()
7290 && (ppc_object
->section_flags(data_shndx
)
7291 & elfcpp::SHF_EXECINSTR
) != 0)
7292 gold_error(_("%s: unsupported -mbss-plt code"),
7293 ppc_object
->name().c_str());
7303 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7304 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7305 case elfcpp::R_POWERPC_GOT_TPREL16
:
7306 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7307 case elfcpp::R_POWERPC_GOT16
:
7308 case elfcpp::R_PPC64_GOT16_DS
:
7309 case elfcpp::R_PPC64_TOC16
:
7310 case elfcpp::R_PPC64_TOC16_DS
:
7311 ppc_object
->set_has_small_toc_reloc();
7317 // Report an unsupported relocation against a global symbol.
7319 template<int size
, bool big_endian
>
7321 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
7322 Sized_relobj_file
<size
, big_endian
>* object
,
7323 unsigned int r_type
,
7326 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
7327 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
7330 // Scan a relocation for a global symbol.
7332 template<int size
, bool big_endian
>
7334 Target_powerpc
<size
, big_endian
>::Scan::global(
7335 Symbol_table
* symtab
,
7337 Target_powerpc
<size
, big_endian
>* target
,
7338 Sized_relobj_file
<size
, big_endian
>* object
,
7339 unsigned int data_shndx
,
7340 Output_section
* output_section
,
7341 const elfcpp::Rela
<size
, big_endian
>& reloc
,
7342 unsigned int r_type
,
7345 if (this->maybe_skip_tls_get_addr_call(target
, r_type
, gsym
)
7349 if (target
->replace_tls_get_addr(gsym
))
7350 // Change a __tls_get_addr reference to __tls_get_addr_opt
7351 // so dynamic relocs are emitted against the latter symbol.
7352 gsym
= target
->tls_get_addr_opt();
7354 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
7355 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
7357 this->expect_tls_get_addr_call();
7358 const bool final
= gsym
->final_value_is_known();
7359 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7360 if (tls_type
!= tls::TLSOPT_NONE
)
7361 this->skip_next_tls_get_addr_call();
7363 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
7364 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
7366 this->expect_tls_get_addr_call();
7367 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7368 if (tls_type
!= tls::TLSOPT_NONE
)
7369 this->skip_next_tls_get_addr_call();
7372 Powerpc_relobj
<size
, big_endian
>* ppc_object
7373 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
7375 // A STT_GNU_IFUNC symbol may require a PLT entry.
7376 bool is_ifunc
= gsym
->type() == elfcpp::STT_GNU_IFUNC
;
7377 bool pushed_ifunc
= false;
7378 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
7380 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7381 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
7382 r_type
, r_sym
, reloc
.get_r_addend());
7383 target
->make_plt_entry(symtab
, layout
, gsym
);
7384 pushed_ifunc
= true;
7389 case elfcpp::R_POWERPC_NONE
:
7390 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
7391 case elfcpp::R_POWERPC_GNU_VTENTRY
:
7392 case elfcpp::R_PPC_LOCAL24PC
:
7393 case elfcpp::R_POWERPC_TLS
:
7394 case elfcpp::R_PPC64_ENTRY
:
7395 case elfcpp::R_POWERPC_PLTSEQ
:
7396 case elfcpp::R_POWERPC_PLTCALL
:
7399 case elfcpp::R_PPC64_TOC
:
7401 Output_data_got_powerpc
<size
, big_endian
>* got
7402 = target
->got_section(symtab
, layout
);
7403 if (parameters
->options().output_is_position_independent())
7405 Address off
= reloc
.get_r_offset();
7407 && data_shndx
== ppc_object
->opd_shndx()
7408 && ppc_object
->get_opd_discard(off
- 8))
7411 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7412 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
7413 if (data_shndx
!= ppc_object
->opd_shndx())
7414 symobj
= static_cast
7415 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7416 rela_dyn
->add_output_section_relative(got
->output_section(),
7417 elfcpp::R_POWERPC_RELATIVE
,
7419 object
, data_shndx
, off
,
7420 symobj
->toc_base_offset());
7425 case elfcpp::R_PPC64_ADDR64
:
7427 && target
->abiversion() < 2
7428 && data_shndx
== ppc_object
->opd_shndx()
7429 && (gsym
->is_defined_in_discarded_section()
7430 || gsym
->object() != object
))
7432 ppc_object
->set_opd_discard(reloc
.get_r_offset());
7436 case elfcpp::R_PPC64_UADDR64
:
7437 case elfcpp::R_POWERPC_ADDR32
:
7438 case elfcpp::R_POWERPC_UADDR32
:
7439 case elfcpp::R_POWERPC_ADDR24
:
7440 case elfcpp::R_POWERPC_ADDR16
:
7441 case elfcpp::R_POWERPC_ADDR16_LO
:
7442 case elfcpp::R_POWERPC_ADDR16_HI
:
7443 case elfcpp::R_POWERPC_ADDR16_HA
:
7444 case elfcpp::R_POWERPC_UADDR16
:
7445 case elfcpp::R_PPC64_ADDR16_HIGH
:
7446 case elfcpp::R_PPC64_ADDR16_HIGHA
:
7447 case elfcpp::R_PPC64_ADDR16_HIGHER
:
7448 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
7449 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
7450 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
7451 case elfcpp::R_PPC64_ADDR16_DS
:
7452 case elfcpp::R_PPC64_ADDR16_LO_DS
:
7453 case elfcpp::R_POWERPC_ADDR14
:
7454 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
7455 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
7457 // Make a PLT entry if necessary.
7458 if (gsym
->needs_plt_entry())
7460 // Since this is not a PC-relative relocation, we may be
7461 // taking the address of a function. In that case we need to
7462 // set the entry in the dynamic symbol table to the address of
7463 // the PLT call stub.
7464 bool need_ifunc_plt
= false;
7465 if ((size
== 32 || target
->abiversion() >= 2)
7466 && gsym
->is_from_dynobj()
7467 && !parameters
->options().output_is_position_independent())
7469 gsym
->set_needs_dynsym_value();
7470 need_ifunc_plt
= true;
7472 if (!is_ifunc
|| (!pushed_ifunc
&& need_ifunc_plt
))
7474 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7475 target
->push_branch(ppc_object
, data_shndx
,
7476 reloc
.get_r_offset(), r_type
, r_sym
,
7477 reloc
.get_r_addend());
7478 target
->make_plt_entry(symtab
, layout
, gsym
);
7481 // Make a dynamic relocation if necessary.
7482 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
))
7483 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
7485 if (!parameters
->options().output_is_position_independent()
7486 && gsym
->may_need_copy_reloc())
7488 target
->copy_reloc(symtab
, layout
, object
,
7489 data_shndx
, output_section
, gsym
, reloc
);
7491 else if ((((size
== 32
7492 && r_type
== elfcpp::R_POWERPC_ADDR32
)
7494 && r_type
== elfcpp::R_PPC64_ADDR64
7495 && target
->abiversion() >= 2))
7496 && gsym
->can_use_relative_reloc(false)
7497 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
7498 && parameters
->options().shared()))
7500 && r_type
== elfcpp::R_PPC64_ADDR64
7501 && target
->abiversion() < 2
7502 && (gsym
->can_use_relative_reloc(false)
7503 || data_shndx
== ppc_object
->opd_shndx())))
7505 Reloc_section
* rela_dyn
7506 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
7507 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
7508 : elfcpp::R_POWERPC_RELATIVE
);
7509 rela_dyn
->add_symbolless_global_addend(
7510 gsym
, dynrel
, output_section
, object
, data_shndx
,
7511 reloc
.get_r_offset(), reloc
.get_r_addend());
7515 Reloc_section
* rela_dyn
7516 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
7517 check_non_pic(object
, r_type
);
7518 rela_dyn
->add_global(gsym
, r_type
, output_section
,
7520 reloc
.get_r_offset(),
7521 reloc
.get_r_addend());
7524 && parameters
->options().toc_optimize()
7525 && data_shndx
== ppc_object
->toc_shndx())
7526 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
7532 case elfcpp::R_POWERPC_PLT16_LO
:
7533 case elfcpp::R_POWERPC_PLT16_HI
:
7534 case elfcpp::R_POWERPC_PLT16_HA
:
7535 case elfcpp::R_PPC64_PLT16_LO_DS
:
7537 target
->make_plt_entry(symtab
, layout
, gsym
);
7540 case elfcpp::R_PPC_PLTREL24
:
7541 case elfcpp::R_POWERPC_REL24
:
7544 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7545 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
7546 r_type
, r_sym
, reloc
.get_r_addend());
7547 if (gsym
->needs_plt_entry()
7548 || (!gsym
->final_value_is_known()
7549 && (gsym
->is_undefined()
7550 || gsym
->is_from_dynobj()
7551 || gsym
->is_preemptible())))
7552 target
->make_plt_entry(symtab
, layout
, gsym
);
7556 case elfcpp::R_PPC64_REL64
:
7557 case elfcpp::R_POWERPC_REL32
:
7558 // Make a dynamic relocation if necessary.
7559 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
)))
7561 if (!parameters
->options().output_is_position_independent()
7562 && gsym
->may_need_copy_reloc())
7564 target
->copy_reloc(symtab
, layout
, object
,
7565 data_shndx
, output_section
, gsym
,
7570 Reloc_section
* rela_dyn
7571 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
7572 check_non_pic(object
, r_type
);
7573 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
7574 data_shndx
, reloc
.get_r_offset(),
7575 reloc
.get_r_addend());
7580 case elfcpp::R_POWERPC_REL14
:
7581 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7582 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7585 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7586 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
7587 r_type
, r_sym
, reloc
.get_r_addend());
7591 case elfcpp::R_PPC64_TOCSAVE
:
7592 // R_PPC64_TOCSAVE follows a call instruction to indicate the
7593 // caller has already saved r2 and thus a plt call stub need not
7596 && target
->mark_pltcall(ppc_object
, data_shndx
,
7597 reloc
.get_r_offset() - 4, symtab
))
7599 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
7601 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
7603 object
->error(_("tocsave symbol %u has bad shndx %u"),
7607 Sized_symbol
<size
>* sym
= symtab
->get_sized_symbol
<size
>(gsym
);
7608 target
->add_tocsave(ppc_object
, shndx
,
7609 sym
->value() + reloc
.get_r_addend());
7614 case elfcpp::R_POWERPC_REL16
:
7615 case elfcpp::R_POWERPC_REL16_LO
:
7616 case elfcpp::R_POWERPC_REL16_HI
:
7617 case elfcpp::R_POWERPC_REL16_HA
:
7618 case elfcpp::R_POWERPC_REL16DX_HA
:
7619 case elfcpp::R_PPC64_REL16_HIGH
:
7620 case elfcpp::R_PPC64_REL16_HIGHA
:
7621 case elfcpp::R_PPC64_REL16_HIGHER
:
7622 case elfcpp::R_PPC64_REL16_HIGHERA
:
7623 case elfcpp::R_PPC64_REL16_HIGHEST
:
7624 case elfcpp::R_PPC64_REL16_HIGHESTA
:
7625 case elfcpp::R_POWERPC_SECTOFF
:
7626 case elfcpp::R_POWERPC_SECTOFF_LO
:
7627 case elfcpp::R_POWERPC_SECTOFF_HI
:
7628 case elfcpp::R_POWERPC_SECTOFF_HA
:
7629 case elfcpp::R_PPC64_SECTOFF_DS
:
7630 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
7631 case elfcpp::R_POWERPC_TPREL16
:
7632 case elfcpp::R_POWERPC_TPREL16_LO
:
7633 case elfcpp::R_POWERPC_TPREL16_HI
:
7634 case elfcpp::R_POWERPC_TPREL16_HA
:
7635 case elfcpp::R_PPC64_TPREL16_DS
:
7636 case elfcpp::R_PPC64_TPREL16_LO_DS
:
7637 case elfcpp::R_PPC64_TPREL16_HIGH
:
7638 case elfcpp::R_PPC64_TPREL16_HIGHA
:
7639 case elfcpp::R_PPC64_TPREL16_HIGHER
:
7640 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
7641 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
7642 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
7643 case elfcpp::R_POWERPC_DTPREL16
:
7644 case elfcpp::R_POWERPC_DTPREL16_LO
:
7645 case elfcpp::R_POWERPC_DTPREL16_HI
:
7646 case elfcpp::R_POWERPC_DTPREL16_HA
:
7647 case elfcpp::R_PPC64_DTPREL16_DS
:
7648 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
7649 case elfcpp::R_PPC64_DTPREL16_HIGH
:
7650 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
7651 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
7652 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
7653 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
7654 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
7655 case elfcpp::R_PPC64_TLSGD
:
7656 case elfcpp::R_PPC64_TLSLD
:
7657 case elfcpp::R_PPC64_ADDR64_LOCAL
:
7660 case elfcpp::R_POWERPC_GOT16
:
7661 case elfcpp::R_POWERPC_GOT16_LO
:
7662 case elfcpp::R_POWERPC_GOT16_HI
:
7663 case elfcpp::R_POWERPC_GOT16_HA
:
7664 case elfcpp::R_PPC64_GOT16_DS
:
7665 case elfcpp::R_PPC64_GOT16_LO_DS
:
7667 // The symbol requires a GOT entry.
7668 Output_data_got_powerpc
<size
, big_endian
>* got
;
7670 got
= target
->got_section(symtab
, layout
);
7671 if (gsym
->final_value_is_known())
7674 && (size
== 32 || target
->abiversion() >= 2))
7675 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
7677 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
7679 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
7681 // If we are generating a shared object or a pie, this
7682 // symbol's GOT entry will be set by a dynamic relocation.
7683 unsigned int off
= got
->add_constant(0);
7684 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
7686 Reloc_section
* rela_dyn
7687 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
7689 if (gsym
->can_use_relative_reloc(false)
7691 || target
->abiversion() >= 2)
7692 && gsym
->visibility() == elfcpp::STV_PROTECTED
7693 && parameters
->options().shared()))
7695 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
7696 : elfcpp::R_POWERPC_RELATIVE
);
7697 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
7701 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
7702 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
7708 case elfcpp::R_PPC64_TOC16
:
7709 case elfcpp::R_PPC64_TOC16_LO
:
7710 case elfcpp::R_PPC64_TOC16_HI
:
7711 case elfcpp::R_PPC64_TOC16_HA
:
7712 case elfcpp::R_PPC64_TOC16_DS
:
7713 case elfcpp::R_PPC64_TOC16_LO_DS
:
7714 // We need a GOT section.
7715 target
->got_section(symtab
, layout
);
7718 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7719 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7720 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
7721 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7723 const bool final
= gsym
->final_value_is_known();
7724 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7725 if (tls_type
== tls::TLSOPT_NONE
)
7727 Output_data_got_powerpc
<size
, big_endian
>* got
7728 = target
->got_section(symtab
, layout
);
7729 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7730 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
, rela_dyn
,
7731 elfcpp::R_POWERPC_DTPMOD
,
7732 elfcpp::R_POWERPC_DTPREL
);
7734 else if (tls_type
== tls::TLSOPT_TO_IE
)
7736 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
7738 Output_data_got_powerpc
<size
, big_endian
>* got
7739 = target
->got_section(symtab
, layout
);
7740 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7741 if (gsym
->is_undefined()
7742 || gsym
->is_from_dynobj())
7744 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
7745 elfcpp::R_POWERPC_TPREL
);
7749 unsigned int off
= got
->add_constant(0);
7750 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
7751 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
7752 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
7757 else if (tls_type
== tls::TLSOPT_TO_LE
)
7759 // no GOT relocs needed for Local Exec.
7766 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7767 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7768 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
7769 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7771 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7772 if (tls_type
== tls::TLSOPT_NONE
)
7773 target
->tlsld_got_offset(symtab
, layout
, object
);
7774 else if (tls_type
== tls::TLSOPT_TO_LE
)
7776 // no GOT relocs needed for Local Exec.
7777 if (parameters
->options().emit_relocs())
7779 Output_section
* os
= layout
->tls_segment()->first_section();
7780 gold_assert(os
!= NULL
);
7781 os
->set_needs_symtab_index();
7789 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7790 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7791 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
7792 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7794 Output_data_got_powerpc
<size
, big_endian
>* got
7795 = target
->got_section(symtab
, layout
);
7796 if (!gsym
->final_value_is_known()
7797 && (gsym
->is_from_dynobj()
7798 || gsym
->is_undefined()
7799 || gsym
->is_preemptible()))
7800 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
7801 target
->rela_dyn_section(layout
),
7802 elfcpp::R_POWERPC_DTPREL
);
7804 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
7808 case elfcpp::R_POWERPC_GOT_TPREL16
:
7809 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7810 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7811 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7813 const bool final
= gsym
->final_value_is_known();
7814 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
7815 if (tls_type
== tls::TLSOPT_NONE
)
7817 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
7819 Output_data_got_powerpc
<size
, big_endian
>* got
7820 = target
->got_section(symtab
, layout
);
7821 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
7822 if (gsym
->is_undefined()
7823 || gsym
->is_from_dynobj())
7825 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
7826 elfcpp::R_POWERPC_TPREL
);
7830 unsigned int off
= got
->add_constant(0);
7831 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
7832 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
7833 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
7838 else if (tls_type
== tls::TLSOPT_TO_LE
)
7840 // no GOT relocs needed for Local Exec.
7848 unsupported_reloc_global(object
, r_type
, gsym
);
7853 && parameters
->options().toc_optimize())
7855 if (data_shndx
== ppc_object
->toc_shndx())
7858 if (r_type
!= elfcpp::R_PPC64_ADDR64
7859 || (is_ifunc
&& target
->abiversion() < 2))
7861 else if (parameters
->options().output_is_position_independent()
7862 && (is_ifunc
|| gsym
->is_absolute() || gsym
->is_undefined()))
7865 ppc_object
->set_no_toc_opt(reloc
.get_r_offset());
7868 enum {no_check
, check_lo
, check_ha
} insn_check
;
7872 insn_check
= no_check
;
7875 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7876 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7877 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7878 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7879 case elfcpp::R_POWERPC_GOT16_HA
:
7880 case elfcpp::R_PPC64_TOC16_HA
:
7881 insn_check
= check_ha
;
7884 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7885 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7886 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7887 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7888 case elfcpp::R_POWERPC_GOT16_LO
:
7889 case elfcpp::R_PPC64_GOT16_LO_DS
:
7890 case elfcpp::R_PPC64_TOC16_LO
:
7891 case elfcpp::R_PPC64_TOC16_LO_DS
:
7892 insn_check
= check_lo
;
7896 section_size_type slen
;
7897 const unsigned char* view
= NULL
;
7898 if (insn_check
!= no_check
)
7900 view
= ppc_object
->section_contents(data_shndx
, &slen
, false);
7901 section_size_type off
=
7902 convert_to_section_size_type(reloc
.get_r_offset()) & -4;
7905 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(view
+ off
);
7906 if (insn_check
== check_lo
7907 ? !ok_lo_toc_insn(insn
, r_type
)
7908 : ((insn
& ((0x3f << 26) | 0x1f << 16))
7909 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7911 ppc_object
->set_no_toc_opt();
7912 gold_warning(_("%s: toc optimization is not supported "
7913 "for %#08x instruction"),
7914 ppc_object
->name().c_str(), insn
);
7923 case elfcpp::R_PPC64_TOC16
:
7924 case elfcpp::R_PPC64_TOC16_LO
:
7925 case elfcpp::R_PPC64_TOC16_HI
:
7926 case elfcpp::R_PPC64_TOC16_HA
:
7927 case elfcpp::R_PPC64_TOC16_DS
:
7928 case elfcpp::R_PPC64_TOC16_LO_DS
:
7929 if (gsym
->source() == Symbol::FROM_OBJECT
7930 && !gsym
->object()->is_dynamic())
7932 Powerpc_relobj
<size
, big_endian
>* sym_object
7933 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
7935 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
7936 if (shndx
== sym_object
->toc_shndx())
7938 Sized_symbol
<size
>* sym
= symtab
->get_sized_symbol
<size
>(gsym
);
7939 Address dst_off
= sym
->value() + reloc
.get_r_addend();
7940 if (dst_off
< sym_object
->section_size(shndx
))
7943 if (r_type
== elfcpp::R_PPC64_TOC16_HA
)
7945 else if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
)
7947 // Need to check that the insn is a ld
7949 view
= ppc_object
->section_contents(data_shndx
,
7952 section_size_type off
=
7953 (convert_to_section_size_type(reloc
.get_r_offset())
7954 + (big_endian
? -2 : 3));
7956 && (view
[off
] & (0x3f << 2)) == (58u << 2))
7960 sym_object
->set_no_toc_opt(dst_off
);
7972 case elfcpp::R_PPC_LOCAL24PC
:
7973 if (strcmp(gsym
->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7974 gold_error(_("%s: unsupported -mbss-plt code"),
7975 ppc_object
->name().c_str());
7984 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7985 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7986 case elfcpp::R_POWERPC_GOT_TPREL16
:
7987 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7988 case elfcpp::R_POWERPC_GOT16
:
7989 case elfcpp::R_PPC64_GOT16_DS
:
7990 case elfcpp::R_PPC64_TOC16
:
7991 case elfcpp::R_PPC64_TOC16_DS
:
7992 ppc_object
->set_has_small_toc_reloc();
7998 // Process relocations for gc.
8000 template<int size
, bool big_endian
>
8002 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
8003 Symbol_table
* symtab
,
8005 Sized_relobj_file
<size
, big_endian
>* object
,
8006 unsigned int data_shndx
,
8008 const unsigned char* prelocs
,
8010 Output_section
* output_section
,
8011 bool needs_special_offset_handling
,
8012 size_t local_symbol_count
,
8013 const unsigned char* plocal_symbols
)
8015 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
8016 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
8019 Powerpc_relobj
<size
, big_endian
>* ppc_object
8020 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
8022 ppc_object
->set_opd_valid();
8023 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
8025 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
8026 for (p
= ppc_object
->access_from_map()->begin();
8027 p
!= ppc_object
->access_from_map()->end();
8030 Address dst_off
= p
->first
;
8031 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
8032 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
8033 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
8035 Relobj
* src_obj
= s
->first
;
8036 unsigned int src_indx
= s
->second
;
8037 symtab
->gc()->add_reference(src_obj
, src_indx
,
8038 ppc_object
, dst_indx
);
8042 ppc_object
->access_from_map()->clear();
8043 ppc_object
->process_gc_mark(symtab
);
8044 // Don't look at .opd relocs as .opd will reference everything.
8048 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
8057 needs_special_offset_handling
,
8062 // Handle target specific gc actions when adding a gc reference from
8063 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
8064 // and DST_OFF. For powerpc64, this adds a referenc to the code
8065 // section of a function descriptor.
8067 template<int size
, bool big_endian
>
8069 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
8070 Symbol_table
* symtab
,
8072 unsigned int src_shndx
,
8074 unsigned int dst_shndx
,
8075 Address dst_off
) const
8077 if (size
!= 64 || dst_obj
->is_dynamic())
8080 Powerpc_relobj
<size
, big_endian
>* ppc_object
8081 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
8082 if (dst_shndx
!= 0 && dst_shndx
== ppc_object
->opd_shndx())
8084 if (ppc_object
->opd_valid())
8086 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
8087 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
8091 // If we haven't run scan_opd_relocs, we must delay
8092 // processing this function descriptor reference.
8093 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
8098 // Add any special sections for this symbol to the gc work list.
8099 // For powerpc64, this adds the code section of a function
8102 template<int size
, bool big_endian
>
8104 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
8105 Symbol_table
* symtab
,
8110 Powerpc_relobj
<size
, big_endian
>* ppc_object
8111 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
8113 unsigned int shndx
= sym
->shndx(&is_ordinary
);
8114 if (is_ordinary
&& shndx
!= 0 && shndx
== ppc_object
->opd_shndx())
8116 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
8117 Address dst_off
= gsym
->value();
8118 if (ppc_object
->opd_valid())
8120 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
8121 symtab
->gc()->worklist().push_back(Section_id(ppc_object
,
8125 ppc_object
->add_gc_mark(dst_off
);
8130 // For a symbol location in .opd, set LOC to the location of the
8133 template<int size
, bool big_endian
>
8135 Target_powerpc
<size
, big_endian
>::do_function_location(
8136 Symbol_location
* loc
) const
8138 if (size
== 64 && loc
->shndx
!= 0)
8140 if (loc
->object
->is_dynamic())
8142 Powerpc_dynobj
<size
, big_endian
>* ppc_object
8143 = static_cast<Powerpc_dynobj
<size
, big_endian
>*>(loc
->object
);
8144 if (loc
->shndx
== ppc_object
->opd_shndx())
8147 Address off
= loc
->offset
- ppc_object
->opd_address();
8148 loc
->shndx
= ppc_object
->get_opd_ent(off
, &dest_off
);
8149 loc
->offset
= dest_off
;
8154 const Powerpc_relobj
<size
, big_endian
>* ppc_object
8155 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(loc
->object
);
8156 if (loc
->shndx
== ppc_object
->opd_shndx())
8159 loc
->shndx
= ppc_object
->get_opd_ent(loc
->offset
, &dest_off
);
8160 loc
->offset
= dest_off
;
8166 // FNOFFSET in section SHNDX in OBJECT is the start of a function
8167 // compiled with -fsplit-stack. The function calls non-split-stack
8168 // code. Change the function to ensure it has enough stack space to
8169 // call some random function.
8171 template<int size
, bool big_endian
>
8173 Target_powerpc
<size
, big_endian
>::do_calls_non_split(
8176 section_offset_type fnoffset
,
8177 section_size_type fnsize
,
8178 const unsigned char* prelocs
,
8180 unsigned char* view
,
8181 section_size_type view_size
,
8183 std::string
* to
) const
8185 // 32-bit not supported.
8189 Target::do_calls_non_split(object
, shndx
, fnoffset
, fnsize
,
8190 prelocs
, reloc_count
, view
, view_size
,
8195 // The function always starts with
8196 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
8197 // addis %r12,%r1,-allocate@ha
8198 // addi %r12,%r12,-allocate@l
8200 // but note that the addis or addi may be replaced with a nop
8202 unsigned char *entry
= view
+ fnoffset
;
8203 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
8205 if ((insn
& 0xffff0000) == addis_2_12
)
8207 /* Skip ELFv2 global entry code. */
8209 insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
8212 unsigned char *pinsn
= entry
;
8214 const uint32_t ld_private_ss
= 0xe80d8fc0;
8215 if (insn
== ld_private_ss
)
8217 int32_t allocate
= 0;
8221 insn
= elfcpp::Swap
<32, big_endian
>::readval(pinsn
);
8222 if ((insn
& 0xffff0000) == addis_12_1
)
8223 allocate
+= (insn
& 0xffff) << 16;
8224 else if ((insn
& 0xffff0000) == addi_12_1
8225 || (insn
& 0xffff0000) == addi_12_12
)
8226 allocate
+= ((insn
& 0xffff) ^ 0x8000) - 0x8000;
8227 else if (insn
!= nop
)
8230 if (insn
== cmpld_7_12_0
&& pinsn
== entry
+ 12)
8232 int extra
= parameters
->options().split_stack_adjust_size();
8234 if (allocate
>= 0 || extra
< 0)
8236 object
->error(_("split-stack stack size overflow at "
8237 "section %u offset %0zx"),
8238 shndx
, static_cast<size_t>(fnoffset
));
8242 insn
= addis_12_1
| (((allocate
+ 0x8000) >> 16) & 0xffff);
8243 if (insn
!= addis_12_1
)
8245 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
8247 insn
= addi_12_12
| (allocate
& 0xffff);
8248 if (insn
!= addi_12_12
)
8250 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
8256 insn
= addi_12_1
| (allocate
& 0xffff);
8257 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
8260 if (pinsn
!= entry
+ 12)
8261 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, nop
);
8269 if (!object
->has_no_split_stack())
8270 object
->error(_("failed to match split-stack sequence at "
8271 "section %u offset %0zx"),
8272 shndx
, static_cast<size_t>(fnoffset
));
8276 // Scan relocations for a section.
8278 template<int size
, bool big_endian
>
8280 Target_powerpc
<size
, big_endian
>::scan_relocs(
8281 Symbol_table
* symtab
,
8283 Sized_relobj_file
<size
, big_endian
>* object
,
8284 unsigned int data_shndx
,
8285 unsigned int sh_type
,
8286 const unsigned char* prelocs
,
8288 Output_section
* output_section
,
8289 bool needs_special_offset_handling
,
8290 size_t local_symbol_count
,
8291 const unsigned char* plocal_symbols
)
8293 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
8294 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
8297 if (!this->plt_localentry0_init_
)
8299 bool plt_localentry0
= false;
8301 && this->abiversion() >= 2)
8303 if (parameters
->options().user_set_plt_localentry())
8304 plt_localentry0
= parameters
->options().plt_localentry();
8306 && symtab
->lookup("GLIBC_2.26", NULL
) == NULL
)
8307 gold_warning(_("--plt-localentry is especially dangerous without "
8308 "ld.so support to detect ABI violations"));
8310 this->plt_localentry0_
= plt_localentry0
;
8311 this->plt_localentry0_init_
= true;
8314 if (sh_type
== elfcpp::SHT_REL
)
8316 gold_error(_("%s: unsupported REL reloc section"),
8317 object
->name().c_str());
8321 gold::scan_relocs
<size
, big_endian
, Powerpc
, Scan
, Classify_reloc
>(
8330 needs_special_offset_handling
,
8335 // Functor class for processing the global symbol table.
8336 // Removes symbols defined on discarded opd entries.
8338 template<bool big_endian
>
8339 class Global_symbol_visitor_opd
8342 Global_symbol_visitor_opd()
8346 operator()(Sized_symbol
<64>* sym
)
8348 if (sym
->has_symtab_index()
8349 || sym
->source() != Symbol::FROM_OBJECT
8350 || !sym
->in_real_elf())
8353 if (sym
->object()->is_dynamic())
8356 Powerpc_relobj
<64, big_endian
>* symobj
8357 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
8358 if (symobj
->opd_shndx() == 0)
8362 unsigned int shndx
= sym
->shndx(&is_ordinary
);
8363 if (shndx
== symobj
->opd_shndx()
8364 && symobj
->get_opd_discard(sym
->value()))
8366 sym
->set_undefined();
8367 sym
->set_visibility(elfcpp::STV_DEFAULT
);
8368 sym
->set_is_defined_in_discarded_section();
8369 sym
->set_symtab_index(-1U);
8374 template<int size
, bool big_endian
>
8376 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
8378 Symbol_table
* symtab
)
8382 Output_data_save_res
<size
, big_endian
>* savres
8383 = new Output_data_save_res
<size
, big_endian
>(symtab
);
8384 this->savres_section_
= savres
;
8385 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
8386 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
8387 savres
, ORDER_TEXT
, false);
8391 // Sort linker created .got section first (for the header), then input
8392 // sections belonging to files using small model code.
8394 template<bool big_endian
>
8395 class Sort_toc_sections
8399 operator()(const Output_section::Input_section
& is1
,
8400 const Output_section::Input_section
& is2
) const
8402 if (!is1
.is_input_section() && is2
.is_input_section())
8405 = (is1
.is_input_section()
8406 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
8407 ->has_small_toc_reloc()));
8409 = (is2
.is_input_section()
8410 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
8411 ->has_small_toc_reloc()));
8412 return small1
&& !small2
;
8416 // Finalize the sections.
8418 template<int size
, bool big_endian
>
8420 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
8422 const Input_objects
* input_objects
,
8423 Symbol_table
* symtab
)
8425 if (parameters
->doing_static_link())
8427 // At least some versions of glibc elf-init.o have a strong
8428 // reference to __rela_iplt marker syms. A weak ref would be
8430 if (this->iplt_
!= NULL
)
8432 Reloc_section
* rel
= this->iplt_
->rel_plt();
8433 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
8434 Symbol_table::PREDEFINED
, rel
, 0, 0,
8435 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
8436 elfcpp::STV_HIDDEN
, 0, false, true);
8437 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
8438 Symbol_table::PREDEFINED
, rel
, 0, 0,
8439 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
8440 elfcpp::STV_HIDDEN
, 0, true, true);
8444 symtab
->define_as_constant("__rela_iplt_start", NULL
,
8445 Symbol_table::PREDEFINED
, 0, 0,
8446 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
8447 elfcpp::STV_HIDDEN
, 0, true, false);
8448 symtab
->define_as_constant("__rela_iplt_end", NULL
,
8449 Symbol_table::PREDEFINED
, 0, 0,
8450 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
8451 elfcpp::STV_HIDDEN
, 0, true, false);
8457 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
8458 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
8460 if (!parameters
->options().relocatable())
8462 this->define_save_restore_funcs(layout
, symtab
);
8464 // Annoyingly, we need to make these sections now whether or
8465 // not we need them. If we delay until do_relax then we
8466 // need to mess with the relaxation machinery checkpointing.
8467 this->got_section(symtab
, layout
);
8468 this->make_brlt_section(layout
);
8470 if (parameters
->options().toc_sort())
8472 Output_section
* os
= this->got_
->output_section();
8473 if (os
!= NULL
&& os
->input_sections().size() > 1)
8474 std::stable_sort(os
->input_sections().begin(),
8475 os
->input_sections().end(),
8476 Sort_toc_sections
<big_endian
>());
8481 // Fill in some more dynamic tags.
8482 Output_data_dynamic
* odyn
= layout
->dynamic_data();
8485 const Reloc_section
* rel_plt
= (this->plt_
== NULL
8487 : this->plt_
->rel_plt());
8488 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
8489 this->rela_dyn_
, true, size
== 32);
8493 if (this->got_
!= NULL
)
8495 this->got_
->finalize_data_size();
8496 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
8497 this->got_
, this->got_
->g_o_t());
8499 if (this->has_tls_get_addr_opt_
)
8500 odyn
->add_constant(elfcpp::DT_PPC_OPT
, elfcpp::PPC_OPT_TLS
);
8504 if (this->glink_
!= NULL
)
8506 this->glink_
->finalize_data_size();
8507 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
8509 (this->glink_
->pltresolve_size()
8512 if (this->has_localentry0_
|| this->has_tls_get_addr_opt_
)
8513 odyn
->add_constant(elfcpp::DT_PPC64_OPT
,
8514 ((this->has_localentry0_
8515 ? elfcpp::PPC64_OPT_LOCALENTRY
: 0)
8516 | (this->has_tls_get_addr_opt_
8517 ? elfcpp::PPC64_OPT_TLS
: 0)));
8521 // Emit any relocs we saved in an attempt to avoid generating COPY
8523 if (this->copy_relocs_
.any_saved_relocs())
8524 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
8526 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
8527 p
!= input_objects
->relobj_end();
8530 Powerpc_relobj
<size
, big_endian
>* ppc_relobj
8531 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(*p
);
8532 if (ppc_relobj
->attributes_section_data())
8533 this->merge_object_attributes(ppc_relobj
->name().c_str(),
8534 ppc_relobj
->attributes_section_data());
8536 for (Input_objects::Dynobj_iterator p
= input_objects
->dynobj_begin();
8537 p
!= input_objects
->dynobj_end();
8540 Powerpc_dynobj
<size
, big_endian
>* ppc_dynobj
8541 = static_cast<Powerpc_dynobj
<size
, big_endian
>*>(*p
);
8542 if (ppc_dynobj
->attributes_section_data())
8543 this->merge_object_attributes(ppc_dynobj
->name().c_str(),
8544 ppc_dynobj
->attributes_section_data());
8547 // Create a .gnu.attributes section if we have merged any attributes
8549 if (this->attributes_section_data_
!= NULL
8550 && this->attributes_section_data_
->size() != 0)
8552 Output_attributes_section_data
* attributes_section
8553 = new Output_attributes_section_data(*this->attributes_section_data_
);
8554 layout
->add_output_section_data(".gnu.attributes",
8555 elfcpp::SHT_GNU_ATTRIBUTES
, 0,
8556 attributes_section
, ORDER_INVALID
, false);
8560 // Merge object attributes from input file called NAME with those of the
8561 // output. The input object attributes are in the object pointed by PASD.
8563 template<int size
, bool big_endian
>
8565 Target_powerpc
<size
, big_endian
>::merge_object_attributes(
8567 const Attributes_section_data
* pasd
)
8569 // Return if there is no attributes section data.
8573 // Create output object attributes.
8574 if (this->attributes_section_data_
== NULL
)
8575 this->attributes_section_data_
= new Attributes_section_data(NULL
, 0);
8577 const int vendor
= Object_attribute::OBJ_ATTR_GNU
;
8578 const Object_attribute
* in_attr
= pasd
->known_attributes(vendor
);
8579 Object_attribute
* out_attr
8580 = this->attributes_section_data_
->known_attributes(vendor
);
8585 int tag
= elfcpp::Tag_GNU_Power_ABI_FP
;
8586 int in_fp
= in_attr
[tag
].int_value() & 0xf;
8587 int out_fp
= out_attr
[tag
].int_value() & 0xf;
8588 if (in_fp
!= out_fp
)
8591 if ((in_fp
& 3) == 0)
8593 else if ((out_fp
& 3) == 0)
8595 out_fp
|= in_fp
& 3;
8596 out_attr
[tag
].set_int_value(out_fp
);
8597 out_attr
[tag
].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
8598 this->last_fp_
= name
;
8600 else if ((out_fp
& 3) != 2 && (in_fp
& 3) == 2)
8602 err
= N_("%s uses hard float, %s uses soft float");
8603 first
= this->last_fp_
;
8606 else if ((out_fp
& 3) == 2 && (in_fp
& 3) != 2)
8608 err
= N_("%s uses hard float, %s uses soft float");
8610 second
= this->last_fp_
;
8612 else if ((out_fp
& 3) == 1 && (in_fp
& 3) == 3)
8614 err
= N_("%s uses double-precision hard float, "
8615 "%s uses single-precision hard float");
8616 first
= this->last_fp_
;
8619 else if ((out_fp
& 3) == 3 && (in_fp
& 3) == 1)
8621 err
= N_("%s uses double-precision hard float, "
8622 "%s uses single-precision hard float");
8624 second
= this->last_fp_
;
8627 if (err
|| (in_fp
& 0xc) == 0)
8629 else if ((out_fp
& 0xc) == 0)
8631 out_fp
|= in_fp
& 0xc;
8632 out_attr
[tag
].set_int_value(out_fp
);
8633 out_attr
[tag
].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
8634 this->last_ld_
= name
;
8636 else if ((out_fp
& 0xc) != 2 * 4 && (in_fp
& 0xc) == 2 * 4)
8638 err
= N_("%s uses 64-bit long double, %s uses 128-bit long double");
8640 second
= this->last_ld_
;
8642 else if ((in_fp
& 0xc) != 2 * 4 && (out_fp
& 0xc) == 2 * 4)
8644 err
= N_("%s uses 64-bit long double, %s uses 128-bit long double");
8645 first
= this->last_ld_
;
8648 else if ((out_fp
& 0xc) == 1 * 4 && (in_fp
& 0xc) == 3 * 4)
8650 err
= N_("%s uses IBM long double, %s uses IEEE long double");
8651 first
= this->last_ld_
;
8654 else if ((out_fp
& 0xc) == 3 * 4 && (in_fp
& 0xc) == 1 * 4)
8656 err
= N_("%s uses IBM long double, %s uses IEEE long double");
8658 second
= this->last_ld_
;
8663 if (parameters
->options().warn_mismatch())
8664 gold_error(_(err
), first
, second
);
8665 // Arrange for this attribute to be deleted. It's better to
8666 // say "don't know" about a file than to wrongly claim compliance.
8667 out_attr
[tag
].set_type(0);
8673 tag
= elfcpp::Tag_GNU_Power_ABI_Vector
;
8674 int in_vec
= in_attr
[tag
].int_value() & 3;
8675 int out_vec
= out_attr
[tag
].int_value() & 3;
8676 if (in_vec
!= out_vec
)
8681 else if (out_vec
== 0)
8684 out_attr
[tag
].set_int_value(out_vec
);
8685 out_attr
[tag
].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
8686 this->last_vec_
= name
;
8688 // For now, allow generic to transition to AltiVec or SPE
8689 // without a warning. If GCC marked files with their stack
8690 // alignment and used don't-care markings for files which are
8691 // not affected by the vector ABI, we could warn about this
8693 else if (in_vec
== 1)
8695 else if (out_vec
== 1)
8698 out_attr
[tag
].set_int_value(out_vec
);
8699 out_attr
[tag
].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
8700 this->last_vec_
= name
;
8702 else if (out_vec
< in_vec
)
8704 err
= N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
8705 first
= this->last_vec_
;
8708 else if (out_vec
> in_vec
)
8710 err
= N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
8712 second
= this->last_vec_
;
8716 if (parameters
->options().warn_mismatch())
8717 gold_error(_(err
), first
, second
);
8718 out_attr
[tag
].set_type(0);
8722 tag
= elfcpp::Tag_GNU_Power_ABI_Struct_Return
;
8723 int in_struct
= in_attr
[tag
].int_value() & 3;
8724 int out_struct
= out_attr
[tag
].int_value() & 3;
8725 if (in_struct
!= out_struct
)
8728 if (in_struct
== 0 || in_struct
== 3)
8730 else if (out_struct
== 0)
8732 out_struct
= in_struct
;
8733 out_attr
[tag
].set_int_value(out_struct
);
8734 out_attr
[tag
].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL
);
8735 this->last_struct_
= name
;
8737 else if (out_struct
< in_struct
)
8739 err
= N_("%s uses r3/r4 for small structure returns, "
8741 first
= this->last_struct_
;
8744 else if (out_struct
> in_struct
)
8746 err
= N_("%s uses r3/r4 for small structure returns, "
8749 second
= this->last_struct_
;
8753 if (parameters
->options().warn_mismatch())
8754 gold_error(_(err
), first
, second
);
8755 out_attr
[tag
].set_type(0);
8760 // Merge Tag_compatibility attributes and any common GNU ones.
8761 this->attributes_section_data_
->merge(name
, pasd
);
8764 // Emit any saved relocs, and mark toc entries using any of these
8765 // relocs as not optimizable.
8767 template<int sh_type
, int size
, bool big_endian
>
8769 Powerpc_copy_relocs
<sh_type
, size
, big_endian
>::emit(
8770 Output_data_reloc
<sh_type
, true, size
, big_endian
>* reloc_section
)
8773 && parameters
->options().toc_optimize())
8775 for (typename Copy_relocs
<sh_type
, size
, big_endian
>::
8776 Copy_reloc_entries::iterator p
= this->entries_
.begin();
8777 p
!= this->entries_
.end();
8780 typename Copy_relocs
<sh_type
, size
, big_endian
>::Copy_reloc_entry
&
8783 // If the symbol is no longer defined in a dynamic object,
8784 // then we emitted a COPY relocation. If it is still
8785 // dynamic then we'll need dynamic relocations and thus
8786 // can't optimize toc entries.
8787 if (entry
.sym_
->is_from_dynobj())
8789 Powerpc_relobj
<size
, big_endian
>* ppc_object
8790 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(entry
.relobj_
);
8791 if (entry
.shndx_
== ppc_object
->toc_shndx())
8792 ppc_object
->set_no_toc_opt(entry
.address_
);
8797 Copy_relocs
<sh_type
, size
, big_endian
>::emit(reloc_section
);
8800 // Return the value to use for a branch relocation.
8802 template<int size
, bool big_endian
>
8804 Target_powerpc
<size
, big_endian
>::symval_for_branch(
8805 const Symbol_table
* symtab
,
8806 const Sized_symbol
<size
>* gsym
,
8807 Powerpc_relobj
<size
, big_endian
>* object
,
8809 unsigned int *dest_shndx
)
8811 if (size
== 32 || this->abiversion() >= 2)
8815 // If the symbol is defined in an opd section, ie. is a function
8816 // descriptor, use the function descriptor code entry address
8817 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
8819 && (gsym
->source() != Symbol::FROM_OBJECT
8820 || gsym
->object()->is_dynamic()))
8823 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
8824 unsigned int shndx
= symobj
->opd_shndx();
8827 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
8828 if (opd_addr
== invalid_address
)
8830 opd_addr
+= symobj
->output_section_address(shndx
);
8831 if (*value
>= opd_addr
&& *value
< opd_addr
+ symobj
->section_size(shndx
))
8834 *dest_shndx
= symobj
->get_opd_ent(*value
- opd_addr
, &sec_off
);
8835 if (symtab
->is_section_folded(symobj
, *dest_shndx
))
8838 = symtab
->icf()->get_folded_section(symobj
, *dest_shndx
);
8839 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(folded
.first
);
8840 *dest_shndx
= folded
.second
;
8842 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
8843 if (sec_addr
== invalid_address
)
8846 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
8847 *value
= sec_addr
+ sec_off
;
8852 // Perform a relocation.
8854 template<int size
, bool big_endian
>
8856 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
8857 const Relocate_info
<size
, big_endian
>* relinfo
,
8859 Target_powerpc
* target
,
8862 const unsigned char* preloc
,
8863 const Sized_symbol
<size
>* gsym
,
8864 const Symbol_value
<size
>* psymval
,
8865 unsigned char* view
,
8867 section_size_type view_size
)
8869 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
8870 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
8871 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
8876 if (target
->replace_tls_get_addr(gsym
))
8877 gsym
= static_cast<const Sized_symbol
<size
>*>(target
->tls_get_addr_opt());
8879 const elfcpp::Rela
<size
, big_endian
> rela(preloc
);
8880 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
8881 switch (this->maybe_skip_tls_get_addr_call(target
, r_type
, gsym
))
8883 case Track_tls::NOT_EXPECTED
:
8884 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8885 _("__tls_get_addr call lacks marker reloc"));
8887 case Track_tls::EXPECTED
:
8888 // We have already complained.
8890 case Track_tls::SKIP
:
8891 if (is_plt16_reloc
<size
>(r_type
)
8892 || r_type
== elfcpp::R_POWERPC_PLTSEQ
)
8894 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8895 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
8897 else if (size
== 64 && r_type
== elfcpp::R_POWERPC_PLTCALL
)
8899 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
8900 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, nop
);
8903 case Track_tls::NORMAL
:
8907 // Offset from start of insn to d-field reloc.
8908 const int d_offset
= big_endian
? 2 : 0;
8910 Powerpc_relobj
<size
, big_endian
>* const object
8911 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
8913 bool has_stub_value
= false;
8914 bool localentry0
= false;
8915 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
8918 ? gsym
->use_plt_offset(Scan::get_reference_flags(r_type
, target
))
8919 : object
->local_has_plt_offset(r_sym
));
8921 && !is_plt16_reloc
<size
>(r_type
)
8922 && r_type
!= elfcpp::R_POWERPC_PLTSEQ
8923 && r_type
!= elfcpp::R_POWERPC_PLTCALL
8924 && (!psymval
->is_ifunc_symbol()
8925 || Scan::reloc_needs_plt_for_ifunc(target
, object
, r_type
, false)))
8929 && target
->abiversion() >= 2
8930 && !parameters
->options().output_is_position_independent()
8931 && !is_branch_reloc(r_type
))
8933 Address off
= target
->glink_section()->find_global_entry(gsym
);
8934 if (off
!= invalid_address
)
8936 value
= target
->glink_section()->global_entry_address() + off
;
8937 has_stub_value
= true;
8942 Stub_table
<size
, big_endian
>* stub_table
= NULL
;
8943 if (target
->stub_tables().size() == 1)
8944 stub_table
= target
->stub_tables()[0];
8945 if (stub_table
== NULL
8948 && !parameters
->options().output_is_position_independent()
8949 && !is_branch_reloc(r_type
)))
8950 stub_table
= object
->stub_table(relinfo
->data_shndx
);
8951 if (stub_table
== NULL
)
8953 // This is a ref from a data section to an ifunc symbol,
8954 // or a non-branch reloc for which we always want to use
8955 // one set of stubs for resolving function addresses.
8956 if (target
->stub_tables().size() != 0)
8957 stub_table
= target
->stub_tables()[0];
8959 if (stub_table
!= NULL
)
8961 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
;
8963 ent
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
8964 rela
.get_r_addend());
8966 ent
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
8967 rela
.get_r_addend());
8970 value
= stub_table
->stub_address() + ent
->off_
;
8971 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
8972 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->reloc_shdr
);
8973 size_t reloc_count
= shdr
.get_sh_size() / reloc_size
;
8976 && relnum
+ 1 < reloc_count
)
8978 Reltype
next_rela(preloc
+ reloc_size
);
8979 if (elfcpp::elf_r_type
<size
>(next_rela
.get_r_info())
8980 == elfcpp::R_PPC64_TOCSAVE
8981 && next_rela
.get_r_offset() == rela
.get_r_offset() + 4)
8984 localentry0
= ent
->localentry0_
;
8985 has_stub_value
= true;
8989 // We don't care too much about bogus debug references to
8990 // non-local functions, but otherwise there had better be a plt
8991 // call stub or global entry stub as appropriate.
8992 gold_assert(has_stub_value
|| !(os
->flags() & elfcpp::SHF_ALLOC
));
8995 if (has_plt_offset
&& is_plt16_reloc
<size
>(r_type
))
8997 const Output_data_plt_powerpc
<size
, big_endian
>* plt
;
8999 value
= target
->plt_off(gsym
, &plt
);
9001 value
= target
->plt_off(object
, r_sym
, &plt
);
9002 value
+= plt
->address();
9005 value
-= (target
->got_section()->output_section()->address()
9006 + object
->toc_base_offset());
9007 else if (parameters
->options().output_is_position_independent())
9009 if (rela
.get_r_addend() >= 32768)
9011 unsigned int got2
= object
->got2_shndx();
9012 value
-= (object
->get_output_section_offset(got2
)
9013 + object
->output_section(got2
)->address()
9014 + rela
.get_r_addend());
9017 value
-= (target
->got_section()->address()
9018 + target
->got_section()->g_o_t());
9021 else if (!has_plt_offset
9022 && (is_plt16_reloc
<size
>(r_type
)
9023 || r_type
== elfcpp::R_POWERPC_PLTSEQ
))
9025 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9026 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
9027 r_type
= elfcpp::R_POWERPC_NONE
;
9029 else if (r_type
== elfcpp::R_POWERPC_GOT16
9030 || r_type
== elfcpp::R_POWERPC_GOT16_LO
9031 || r_type
== elfcpp::R_POWERPC_GOT16_HI
9032 || r_type
== elfcpp::R_POWERPC_GOT16_HA
9033 || r_type
== elfcpp::R_PPC64_GOT16_DS
9034 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
9038 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
9039 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
9043 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
9044 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
9046 value
-= target
->got_section()->got_base_offset(object
);
9048 else if (r_type
== elfcpp::R_PPC64_TOC
)
9050 value
= (target
->got_section()->output_section()->address()
9051 + object
->toc_base_offset());
9053 else if (gsym
!= NULL
9054 && (r_type
== elfcpp::R_POWERPC_REL24
9055 || r_type
== elfcpp::R_PPC_PLTREL24
)
9060 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
9061 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
9062 bool can_plt_call
= localentry0
|| target
->is_tls_get_addr_opt(gsym
);
9063 if (!can_plt_call
&& rela
.get_r_offset() + 8 <= view_size
)
9065 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
9066 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
9069 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
9071 elfcpp::Swap
<32, big_endian
>::
9072 writeval(wv
+ 1, ld_2_1
+ target
->stk_toc());
9073 can_plt_call
= true;
9078 // If we don't have a branch and link followed by a nop,
9079 // we can't go via the plt because there is no place to
9080 // put a toc restoring instruction.
9081 // Unless we know we won't be returning.
9082 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
9083 can_plt_call
= true;
9087 // g++ as of 20130507 emits self-calls without a
9088 // following nop. This is arguably wrong since we have
9089 // conflicting information. On the one hand a global
9090 // symbol and on the other a local call sequence, but
9091 // don't error for this special case.
9092 // It isn't possible to cheaply verify we have exactly
9093 // such a call. Allow all calls to the same section.
9095 Address code
= value
;
9096 if (gsym
->source() == Symbol::FROM_OBJECT
9097 && gsym
->object() == object
)
9099 unsigned int dest_shndx
= 0;
9100 if (target
->abiversion() < 2)
9102 Address addend
= rela
.get_r_addend();
9103 code
= psymval
->value(object
, addend
);
9104 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
9105 &code
, &dest_shndx
);
9108 if (dest_shndx
== 0)
9109 dest_shndx
= gsym
->shndx(&is_ordinary
);
9110 ok
= dest_shndx
== relinfo
->data_shndx
;
9114 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
9115 _("call lacks nop, can't restore toc; "
9116 "recompile with -fPIC"));
9122 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
9123 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
9124 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
9125 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
9127 // First instruction of a global dynamic sequence, arg setup insn.
9128 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9129 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
9130 enum Got_type got_type
= GOT_TYPE_STANDARD
;
9131 if (tls_type
== tls::TLSOPT_NONE
)
9132 got_type
= GOT_TYPE_TLSGD
;
9133 else if (tls_type
== tls::TLSOPT_TO_IE
)
9134 got_type
= GOT_TYPE_TPREL
;
9135 if (got_type
!= GOT_TYPE_STANDARD
)
9139 gold_assert(gsym
->has_got_offset(got_type
));
9140 value
= gsym
->got_offset(got_type
);
9144 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
9145 value
= object
->local_got_offset(r_sym
, got_type
);
9147 value
-= target
->got_section()->got_base_offset(object
);
9149 if (tls_type
== tls::TLSOPT_TO_IE
)
9151 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
9152 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
9154 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9155 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9156 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
9158 insn
|= 32 << 26; // lwz
9160 insn
|= 58 << 26; // ld
9161 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9163 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
9164 - elfcpp::R_POWERPC_GOT_TLSGD16
);
9166 else if (tls_type
== tls::TLSOPT_TO_LE
)
9168 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
9169 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
9171 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9172 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9173 insn
&= (1 << 26) - (1 << 21); // extract rt
9178 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9179 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9180 value
= psymval
->value(object
, rela
.get_r_addend());
9184 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9186 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9187 r_type
= elfcpp::R_POWERPC_NONE
;
9191 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
9192 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
9193 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
9194 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
9196 // First instruction of a local dynamic sequence, arg setup insn.
9197 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
9198 if (tls_type
== tls::TLSOPT_NONE
)
9200 value
= target
->tlsld_got_offset();
9201 value
-= target
->got_section()->got_base_offset(object
);
9205 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
9206 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
9207 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
9209 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9210 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9211 insn
&= (1 << 26) - (1 << 21); // extract rt
9216 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9217 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9222 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9224 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9225 r_type
= elfcpp::R_POWERPC_NONE
;
9229 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
9230 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
9231 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
9232 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
9234 // Accesses relative to a local dynamic sequence address,
9235 // no optimisation here.
9238 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
9239 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
9243 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
9244 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
9246 value
-= target
->got_section()->got_base_offset(object
);
9248 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
9249 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
9250 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
9251 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
9253 // First instruction of initial exec sequence.
9254 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9255 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
9256 if (tls_type
== tls::TLSOPT_NONE
)
9260 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
9261 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
9265 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
9266 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
9268 value
-= target
->got_section()->got_base_offset(object
);
9272 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
9273 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
9274 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
9276 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9277 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9278 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
9283 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9284 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
9285 value
= psymval
->value(object
, rela
.get_r_addend());
9289 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9291 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9292 r_type
= elfcpp::R_POWERPC_NONE
;
9296 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
9297 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
9299 // Second instruction of a global dynamic sequence,
9300 // the __tls_get_addr call
9301 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
9302 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9303 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
9304 if (tls_type
!= tls::TLSOPT_NONE
)
9306 if (tls_type
== tls::TLSOPT_TO_IE
)
9308 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9309 Insn insn
= add_3_3_13
;
9312 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9313 r_type
= elfcpp::R_POWERPC_NONE
;
9317 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9318 Insn insn
= addi_3_3
;
9319 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9320 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9322 value
= psymval
->value(object
, rela
.get_r_addend());
9324 this->skip_next_tls_get_addr_call();
9327 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
9328 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
9330 // Second instruction of a local dynamic sequence,
9331 // the __tls_get_addr call
9332 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
9333 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
9334 if (tls_type
== tls::TLSOPT_TO_LE
)
9336 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9337 Insn insn
= addi_3_3
;
9338 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9339 this->skip_next_tls_get_addr_call();
9340 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9345 else if (r_type
== elfcpp::R_POWERPC_TLS
)
9347 // Second instruction of an initial exec sequence
9348 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
9349 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
9350 if (tls_type
== tls::TLSOPT_TO_LE
)
9352 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9353 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9354 unsigned int reg
= size
== 32 ? 2 : 13;
9355 insn
= at_tls_transform(insn
, reg
);
9356 gold_assert(insn
!= 0);
9357 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9358 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
9360 value
= psymval
->value(object
, rela
.get_r_addend());
9363 else if (!has_stub_value
)
9365 if (!has_plt_offset
&& r_type
== elfcpp::R_POWERPC_PLTCALL
)
9367 // PLTCALL without plt entry => convert to direct call
9368 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9369 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9370 insn
= (insn
& 1) | b
;
9371 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9373 r_type
= elfcpp::R_PPC_PLTREL24
;
9375 r_type
= elfcpp::R_POWERPC_REL24
;
9379 && (r_type
== elfcpp::R_PPC_PLTREL24
9380 || r_type
== elfcpp::R_POWERPC_PLT16_LO
9381 || r_type
== elfcpp::R_POWERPC_PLT16_HI
9382 || r_type
== elfcpp::R_POWERPC_PLT16_HA
)))
9383 addend
= rela
.get_r_addend();
9384 value
= psymval
->value(object
, addend
);
9385 if (size
== 64 && is_branch_reloc(r_type
))
9387 if (target
->abiversion() >= 2)
9390 value
+= object
->ppc64_local_entry_offset(gsym
);
9392 value
+= object
->ppc64_local_entry_offset(r_sym
);
9396 unsigned int dest_shndx
;
9397 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
9398 &value
, &dest_shndx
);
9401 Address max_branch_offset
= max_branch_delta(r_type
);
9402 if (max_branch_offset
!= 0
9403 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
9405 Stub_table
<size
, big_endian
>* stub_table
9406 = object
->stub_table(relinfo
->data_shndx
);
9407 if (stub_table
!= NULL
)
9409 Address off
= stub_table
->find_long_branch_entry(object
, value
);
9410 if (off
!= invalid_address
)
9412 value
= (stub_table
->stub_address() + stub_table
->plt_size()
9414 has_stub_value
= true;
9422 case elfcpp::R_PPC64_REL64
:
9423 case elfcpp::R_POWERPC_REL32
:
9424 case elfcpp::R_POWERPC_REL24
:
9425 case elfcpp::R_PPC_PLTREL24
:
9426 case elfcpp::R_PPC_LOCAL24PC
:
9427 case elfcpp::R_POWERPC_REL16
:
9428 case elfcpp::R_POWERPC_REL16_LO
:
9429 case elfcpp::R_POWERPC_REL16_HI
:
9430 case elfcpp::R_POWERPC_REL16_HA
:
9431 case elfcpp::R_POWERPC_REL16DX_HA
:
9432 case elfcpp::R_PPC64_REL16_HIGH
:
9433 case elfcpp::R_PPC64_REL16_HIGHA
:
9434 case elfcpp::R_PPC64_REL16_HIGHER
:
9435 case elfcpp::R_PPC64_REL16_HIGHERA
:
9436 case elfcpp::R_PPC64_REL16_HIGHEST
:
9437 case elfcpp::R_PPC64_REL16_HIGHESTA
:
9438 case elfcpp::R_POWERPC_REL14
:
9439 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
9440 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
9444 case elfcpp::R_PPC64_TOC16
:
9445 case elfcpp::R_PPC64_TOC16_LO
:
9446 case elfcpp::R_PPC64_TOC16_HI
:
9447 case elfcpp::R_PPC64_TOC16_HA
:
9448 case elfcpp::R_PPC64_TOC16_DS
:
9449 case elfcpp::R_PPC64_TOC16_LO_DS
:
9450 // Subtract the TOC base address.
9451 value
-= (target
->got_section()->output_section()->address()
9452 + object
->toc_base_offset());
9455 case elfcpp::R_POWERPC_SECTOFF
:
9456 case elfcpp::R_POWERPC_SECTOFF_LO
:
9457 case elfcpp::R_POWERPC_SECTOFF_HI
:
9458 case elfcpp::R_POWERPC_SECTOFF_HA
:
9459 case elfcpp::R_PPC64_SECTOFF_DS
:
9460 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
9462 value
-= os
->address();
9465 case elfcpp::R_PPC64_TPREL16_DS
:
9466 case elfcpp::R_PPC64_TPREL16_LO_DS
:
9467 case elfcpp::R_PPC64_TPREL16_HIGH
:
9468 case elfcpp::R_PPC64_TPREL16_HIGHA
:
9470 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
9473 case elfcpp::R_POWERPC_TPREL16
:
9474 case elfcpp::R_POWERPC_TPREL16_LO
:
9475 case elfcpp::R_POWERPC_TPREL16_HI
:
9476 case elfcpp::R_POWERPC_TPREL16_HA
:
9477 case elfcpp::R_POWERPC_TPREL
:
9478 case elfcpp::R_PPC64_TPREL16_HIGHER
:
9479 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
9480 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
9481 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
9482 // tls symbol values are relative to tls_segment()->vaddr()
9486 case elfcpp::R_PPC64_DTPREL16_DS
:
9487 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
9488 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
9489 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
9490 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
9491 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
9493 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
9494 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
9497 case elfcpp::R_POWERPC_DTPREL16
:
9498 case elfcpp::R_POWERPC_DTPREL16_LO
:
9499 case elfcpp::R_POWERPC_DTPREL16_HI
:
9500 case elfcpp::R_POWERPC_DTPREL16_HA
:
9501 case elfcpp::R_POWERPC_DTPREL
:
9502 case elfcpp::R_PPC64_DTPREL16_HIGH
:
9503 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
9504 // tls symbol values are relative to tls_segment()->vaddr()
9505 value
-= dtp_offset
;
9508 case elfcpp::R_PPC64_ADDR64_LOCAL
:
9510 value
+= object
->ppc64_local_entry_offset(gsym
);
9512 value
+= object
->ppc64_local_entry_offset(r_sym
);
9519 Insn branch_bit
= 0;
9522 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
9523 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
9524 branch_bit
= 1 << 21;
9526 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
9527 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
9529 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9530 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9533 if (this->is_isa_v2
)
9535 // Set 'a' bit. This is 0b00010 in BO field for branch
9536 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
9537 // for branch on CTR insns (BO == 1a00t or 1a01t).
9538 if ((insn
& (0x14 << 21)) == (0x04 << 21))
9540 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
9547 // Invert 'y' bit if not the default.
9548 if (static_cast<Signed_address
>(value
) < 0)
9551 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9555 case elfcpp::R_POWERPC_PLT16_HA
:
9557 && !parameters
->options().output_is_position_independent())
9559 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9560 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9562 // Convert addis to lis.
9563 if ((insn
& (0x3f << 26)) == 15u << 26
9564 && (insn
& (0x1f << 16)) != 0)
9566 insn
&= ~(0x1f << 16);
9567 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9583 // Multi-instruction sequences that access the GOT/TOC can
9584 // be optimized, eg.
9585 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
9586 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
9588 // addis ra,r2,0; addi rb,ra,x@toc@l;
9589 // to nop; addi rb,r2,x@toc;
9590 // FIXME: the @got sequence shown above is not yet
9591 // optimized. Note that gcc as of 2017-01-07 doesn't use
9592 // the ELF @got relocs except for TLS, instead using the
9593 // PowerOpen variant of a compiler managed GOT (called TOC).
9594 // The PowerOpen TOC sequence equivalent to the first
9595 // example is optimized.
9596 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
9597 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
9598 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
9599 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
9600 case elfcpp::R_POWERPC_GOT16_HA
:
9601 case elfcpp::R_PPC64_TOC16_HA
:
9602 if (parameters
->options().toc_optimize())
9604 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9605 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9606 if (r_type
== elfcpp::R_PPC64_TOC16_HA
9607 && object
->make_toc_relative(target
, &value
))
9609 gold_assert((insn
& ((0x3f << 26) | 0x1f << 16))
9610 == ((15u << 26) | (2 << 16)));
9612 if (((insn
& ((0x3f << 26) | 0x1f << 16))
9613 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
9614 && value
+ 0x8000 < 0x10000)
9616 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
9622 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
9623 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
9624 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
9625 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
9626 case elfcpp::R_POWERPC_GOT16_LO
:
9627 case elfcpp::R_PPC64_GOT16_LO_DS
:
9628 case elfcpp::R_PPC64_TOC16_LO
:
9629 case elfcpp::R_PPC64_TOC16_LO_DS
:
9630 if (parameters
->options().toc_optimize())
9632 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9633 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9634 bool changed
= false;
9635 if (r_type
== elfcpp::R_PPC64_TOC16_LO_DS
9636 && object
->make_toc_relative(target
, &value
))
9638 gold_assert ((insn
& (0x3f << 26)) == 58u << 26 /* ld */);
9639 insn
^= (14u << 26) ^ (58u << 26);
9640 r_type
= elfcpp::R_PPC64_TOC16_LO
;
9643 if (ok_lo_toc_insn(insn
, r_type
)
9644 && value
+ 0x8000 < 0x10000)
9646 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
9648 // Transform addic to addi when we change reg.
9649 insn
&= ~((0x3f << 26) | (0x1f << 16));
9650 insn
|= (14u << 26) | (2 << 16);
9654 insn
&= ~(0x1f << 16);
9660 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9664 case elfcpp::R_POWERPC_TPREL16_HA
:
9665 if (parameters
->options().tls_optimize() && value
+ 0x8000 < 0x10000)
9667 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9668 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9669 if ((insn
& ((0x3f << 26) | 0x1f << 16))
9670 != ((15u << 26) | ((size
== 32 ? 2 : 13) << 16)))
9674 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
9680 case elfcpp::R_PPC64_TPREL16_LO_DS
:
9682 // R_PPC_TLSGD, R_PPC_TLSLD
9685 case elfcpp::R_POWERPC_TPREL16_LO
:
9686 if (parameters
->options().tls_optimize() && value
+ 0x8000 < 0x10000)
9688 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9689 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9690 insn
&= ~(0x1f << 16);
9691 insn
|= (size
== 32 ? 2 : 13) << 16;
9692 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
9696 case elfcpp::R_PPC64_ENTRY
:
9697 value
= (target
->got_section()->output_section()->address()
9698 + object
->toc_base_offset());
9699 if (value
+ 0x80008000 <= 0xffffffff
9700 && !parameters
->options().output_is_position_independent())
9702 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9703 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9704 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
9706 if ((insn1
& ~0xfffc) == ld_2_12
9707 && insn2
== add_2_2_12
)
9709 insn1
= lis_2
+ ha(value
);
9710 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
9711 insn2
= addi_2_2
+ l(value
);
9712 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
9719 if (value
+ 0x80008000 <= 0xffffffff)
9721 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
9722 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9723 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
9725 if ((insn1
& ~0xfffc) == ld_2_12
9726 && insn2
== add_2_2_12
)
9728 insn1
= addis_2_12
+ ha(value
);
9729 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
9730 insn2
= addi_2_2
+ l(value
);
9731 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
9738 case elfcpp::R_POWERPC_REL16_LO
:
9739 // If we are generating a non-PIC executable, edit
9740 // 0: addis 2,12,.TOC.-0b@ha
9741 // addi 2,2,.TOC.-0b@l
9742 // used by ELFv2 global entry points to set up r2, to
9745 // if .TOC. is in range. */
9746 if (value
+ address
- 4 + 0x80008000 <= 0xffffffff
9749 && target
->abiversion() >= 2
9750 && !parameters
->options().output_is_position_independent()
9751 && rela
.get_r_addend() == d_offset
+ 4
9753 && strcmp(gsym
->name(), ".TOC.") == 0)
9755 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
9756 Reltype
prev_rela(preloc
- reloc_size
);
9757 if ((prev_rela
.get_r_info()
9758 == elfcpp::elf_r_info
<size
>(r_sym
,
9759 elfcpp::R_POWERPC_REL16_HA
))
9760 && prev_rela
.get_r_offset() + 4 == rela
.get_r_offset()
9761 && prev_rela
.get_r_addend() + 4 == rela
.get_r_addend())
9763 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9764 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
- 1);
9765 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9767 if ((insn1
& 0xffff0000) == addis_2_12
9768 && (insn2
& 0xffff0000) == addi_2_2
)
9770 insn1
= lis_2
+ ha(value
+ address
- 4);
9771 elfcpp::Swap
<32, big_endian
>::writeval(iview
- 1, insn1
);
9772 insn2
= addi_2_2
+ l(value
+ address
- 4);
9773 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn2
);
9776 relinfo
->rr
->set_strategy(relnum
- 1,
9777 Relocatable_relocs::RELOC_SPECIAL
);
9778 relinfo
->rr
->set_strategy(relnum
,
9779 Relocatable_relocs::RELOC_SPECIAL
);
9789 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
9790 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->data_shdr
);
9793 case elfcpp::R_POWERPC_ADDR32
:
9794 case elfcpp::R_POWERPC_UADDR32
:
9796 overflow
= Reloc::CHECK_BITFIELD
;
9799 case elfcpp::R_POWERPC_REL32
:
9800 case elfcpp::R_POWERPC_REL16DX_HA
:
9802 overflow
= Reloc::CHECK_SIGNED
;
9805 case elfcpp::R_POWERPC_UADDR16
:
9806 overflow
= Reloc::CHECK_BITFIELD
;
9809 case elfcpp::R_POWERPC_ADDR16
:
9810 // We really should have three separate relocations,
9811 // one for 16-bit data, one for insns with 16-bit signed fields,
9812 // and one for insns with 16-bit unsigned fields.
9813 overflow
= Reloc::CHECK_BITFIELD
;
9814 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
9815 overflow
= Reloc::CHECK_LOW_INSN
;
9818 case elfcpp::R_POWERPC_ADDR16_HI
:
9819 case elfcpp::R_POWERPC_ADDR16_HA
:
9820 case elfcpp::R_POWERPC_GOT16_HI
:
9821 case elfcpp::R_POWERPC_GOT16_HA
:
9822 case elfcpp::R_POWERPC_PLT16_HI
:
9823 case elfcpp::R_POWERPC_PLT16_HA
:
9824 case elfcpp::R_POWERPC_SECTOFF_HI
:
9825 case elfcpp::R_POWERPC_SECTOFF_HA
:
9826 case elfcpp::R_PPC64_TOC16_HI
:
9827 case elfcpp::R_PPC64_TOC16_HA
:
9828 case elfcpp::R_PPC64_PLTGOT16_HI
:
9829 case elfcpp::R_PPC64_PLTGOT16_HA
:
9830 case elfcpp::R_POWERPC_TPREL16_HI
:
9831 case elfcpp::R_POWERPC_TPREL16_HA
:
9832 case elfcpp::R_POWERPC_DTPREL16_HI
:
9833 case elfcpp::R_POWERPC_DTPREL16_HA
:
9834 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
9835 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
9836 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
9837 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
9838 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
9839 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
9840 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
9841 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
9842 case elfcpp::R_POWERPC_REL16_HI
:
9843 case elfcpp::R_POWERPC_REL16_HA
:
9845 overflow
= Reloc::CHECK_HIGH_INSN
;
9848 case elfcpp::R_POWERPC_REL16
:
9849 case elfcpp::R_PPC64_TOC16
:
9850 case elfcpp::R_POWERPC_GOT16
:
9851 case elfcpp::R_POWERPC_SECTOFF
:
9852 case elfcpp::R_POWERPC_TPREL16
:
9853 case elfcpp::R_POWERPC_DTPREL16
:
9854 case elfcpp::R_POWERPC_GOT_TLSGD16
:
9855 case elfcpp::R_POWERPC_GOT_TLSLD16
:
9856 case elfcpp::R_POWERPC_GOT_TPREL16
:
9857 case elfcpp::R_POWERPC_GOT_DTPREL16
:
9858 overflow
= Reloc::CHECK_LOW_INSN
;
9861 case elfcpp::R_POWERPC_ADDR24
:
9862 case elfcpp::R_POWERPC_ADDR14
:
9863 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
9864 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
9865 case elfcpp::R_PPC64_ADDR16_DS
:
9866 case elfcpp::R_POWERPC_REL24
:
9867 case elfcpp::R_PPC_PLTREL24
:
9868 case elfcpp::R_PPC_LOCAL24PC
:
9869 case elfcpp::R_PPC64_TPREL16_DS
:
9870 case elfcpp::R_PPC64_DTPREL16_DS
:
9871 case elfcpp::R_PPC64_TOC16_DS
:
9872 case elfcpp::R_PPC64_GOT16_DS
:
9873 case elfcpp::R_PPC64_SECTOFF_DS
:
9874 case elfcpp::R_POWERPC_REL14
:
9875 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
9876 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
9877 overflow
= Reloc::CHECK_SIGNED
;
9881 Insn
* iview
= reinterpret_cast<Insn
*>(view
- d_offset
);
9884 if (overflow
== Reloc::CHECK_LOW_INSN
9885 || overflow
== Reloc::CHECK_HIGH_INSN
)
9887 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
9889 if ((insn
& (0x3f << 26)) == 10u << 26 /* cmpli */)
9890 overflow
= Reloc::CHECK_BITFIELD
;
9891 else if (overflow
== Reloc::CHECK_LOW_INSN
9892 ? ((insn
& (0x3f << 26)) == 28u << 26 /* andi */
9893 || (insn
& (0x3f << 26)) == 24u << 26 /* ori */
9894 || (insn
& (0x3f << 26)) == 26u << 26 /* xori */)
9895 : ((insn
& (0x3f << 26)) == 29u << 26 /* andis */
9896 || (insn
& (0x3f << 26)) == 25u << 26 /* oris */
9897 || (insn
& (0x3f << 26)) == 27u << 26 /* xoris */))
9898 overflow
= Reloc::CHECK_UNSIGNED
;
9900 overflow
= Reloc::CHECK_SIGNED
;
9903 bool maybe_dq_reloc
= false;
9904 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
9905 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
9908 case elfcpp::R_POWERPC_NONE
:
9909 case elfcpp::R_POWERPC_TLS
:
9910 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
9911 case elfcpp::R_POWERPC_GNU_VTENTRY
:
9912 case elfcpp::R_POWERPC_PLTSEQ
:
9913 case elfcpp::R_POWERPC_PLTCALL
:
9916 case elfcpp::R_PPC64_ADDR64
:
9917 case elfcpp::R_PPC64_REL64
:
9918 case elfcpp::R_PPC64_TOC
:
9919 case elfcpp::R_PPC64_ADDR64_LOCAL
:
9920 Reloc::addr64(view
, value
);
9923 case elfcpp::R_POWERPC_TPREL
:
9924 case elfcpp::R_POWERPC_DTPREL
:
9926 Reloc::addr64(view
, value
);
9928 status
= Reloc::addr32(view
, value
, overflow
);
9931 case elfcpp::R_PPC64_UADDR64
:
9932 Reloc::addr64_u(view
, value
);
9935 case elfcpp::R_POWERPC_ADDR32
:
9936 status
= Reloc::addr32(view
, value
, overflow
);
9939 case elfcpp::R_POWERPC_REL32
:
9940 case elfcpp::R_POWERPC_UADDR32
:
9941 status
= Reloc::addr32_u(view
, value
, overflow
);
9944 case elfcpp::R_POWERPC_ADDR24
:
9945 case elfcpp::R_POWERPC_REL24
:
9946 case elfcpp::R_PPC_PLTREL24
:
9947 case elfcpp::R_PPC_LOCAL24PC
:
9948 status
= Reloc::addr24(view
, value
, overflow
);
9951 case elfcpp::R_POWERPC_GOT_DTPREL16
:
9952 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
9953 case elfcpp::R_POWERPC_GOT_TPREL16
:
9954 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
9957 // On ppc64 these are all ds form
9958 maybe_dq_reloc
= true;
9962 case elfcpp::R_POWERPC_ADDR16
:
9963 case elfcpp::R_POWERPC_REL16
:
9964 case elfcpp::R_PPC64_TOC16
:
9965 case elfcpp::R_POWERPC_GOT16
:
9966 case elfcpp::R_POWERPC_SECTOFF
:
9967 case elfcpp::R_POWERPC_TPREL16
:
9968 case elfcpp::R_POWERPC_DTPREL16
:
9969 case elfcpp::R_POWERPC_GOT_TLSGD16
:
9970 case elfcpp::R_POWERPC_GOT_TLSLD16
:
9971 case elfcpp::R_POWERPC_ADDR16_LO
:
9972 case elfcpp::R_POWERPC_REL16_LO
:
9973 case elfcpp::R_PPC64_TOC16_LO
:
9974 case elfcpp::R_POWERPC_GOT16_LO
:
9975 case elfcpp::R_POWERPC_PLT16_LO
:
9976 case elfcpp::R_POWERPC_SECTOFF_LO
:
9977 case elfcpp::R_POWERPC_TPREL16_LO
:
9978 case elfcpp::R_POWERPC_DTPREL16_LO
:
9979 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
9980 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
9982 status
= Reloc::addr16(view
, value
, overflow
);
9984 maybe_dq_reloc
= true;
9987 case elfcpp::R_POWERPC_UADDR16
:
9988 status
= Reloc::addr16_u(view
, value
, overflow
);
9991 case elfcpp::R_PPC64_ADDR16_HIGH
:
9992 case elfcpp::R_PPC64_TPREL16_HIGH
:
9993 case elfcpp::R_PPC64_DTPREL16_HIGH
:
9995 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
9998 case elfcpp::R_POWERPC_ADDR16_HI
:
9999 case elfcpp::R_POWERPC_REL16_HI
:
10000 case elfcpp::R_PPC64_REL16_HIGH
:
10001 case elfcpp::R_PPC64_TOC16_HI
:
10002 case elfcpp::R_POWERPC_GOT16_HI
:
10003 case elfcpp::R_POWERPC_PLT16_HI
:
10004 case elfcpp::R_POWERPC_SECTOFF_HI
:
10005 case elfcpp::R_POWERPC_TPREL16_HI
:
10006 case elfcpp::R_POWERPC_DTPREL16_HI
:
10007 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
10008 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
10009 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
10010 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
10011 Reloc::addr16_hi(view
, value
);
10014 case elfcpp::R_PPC64_ADDR16_HIGHA
:
10015 case elfcpp::R_PPC64_TPREL16_HIGHA
:
10016 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
10018 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
10021 case elfcpp::R_POWERPC_ADDR16_HA
:
10022 case elfcpp::R_POWERPC_REL16_HA
:
10023 case elfcpp::R_PPC64_REL16_HIGHA
:
10024 case elfcpp::R_PPC64_TOC16_HA
:
10025 case elfcpp::R_POWERPC_GOT16_HA
:
10026 case elfcpp::R_POWERPC_PLT16_HA
:
10027 case elfcpp::R_POWERPC_SECTOFF_HA
:
10028 case elfcpp::R_POWERPC_TPREL16_HA
:
10029 case elfcpp::R_POWERPC_DTPREL16_HA
:
10030 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
10031 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
10032 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
10033 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
10034 Reloc::addr16_ha(view
, value
);
10037 case elfcpp::R_POWERPC_REL16DX_HA
:
10038 status
= Reloc::addr16dx_ha(view
, value
, overflow
);
10041 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
10043 // R_PPC_EMB_NADDR16_LO
10046 case elfcpp::R_PPC64_ADDR16_HIGHER
:
10047 case elfcpp::R_PPC64_REL16_HIGHER
:
10048 case elfcpp::R_PPC64_TPREL16_HIGHER
:
10049 Reloc::addr16_hi2(view
, value
);
10052 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
10054 // R_PPC_EMB_NADDR16_HI
10057 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
10058 case elfcpp::R_PPC64_REL16_HIGHERA
:
10059 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
10060 Reloc::addr16_ha2(view
, value
);
10063 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
10065 // R_PPC_EMB_NADDR16_HA
10068 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
10069 case elfcpp::R_PPC64_REL16_HIGHEST
:
10070 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
10071 Reloc::addr16_hi3(view
, value
);
10074 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
10076 // R_PPC_EMB_SDAI16
10079 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
10080 case elfcpp::R_PPC64_REL16_HIGHESTA
:
10081 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
10082 Reloc::addr16_ha3(view
, value
);
10085 case elfcpp::R_PPC64_DTPREL16_DS
:
10086 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
10088 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
10091 case elfcpp::R_PPC64_TPREL16_DS
:
10092 case elfcpp::R_PPC64_TPREL16_LO_DS
:
10094 // R_PPC_TLSGD, R_PPC_TLSLD
10097 case elfcpp::R_PPC64_ADDR16_DS
:
10098 case elfcpp::R_PPC64_ADDR16_LO_DS
:
10099 case elfcpp::R_PPC64_TOC16_DS
:
10100 case elfcpp::R_PPC64_TOC16_LO_DS
:
10101 case elfcpp::R_PPC64_GOT16_DS
:
10102 case elfcpp::R_PPC64_GOT16_LO_DS
:
10103 case elfcpp::R_PPC64_PLT16_LO_DS
:
10104 case elfcpp::R_PPC64_SECTOFF_DS
:
10105 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
10106 maybe_dq_reloc
= true;
10109 case elfcpp::R_POWERPC_ADDR14
:
10110 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
10111 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
10112 case elfcpp::R_POWERPC_REL14
:
10113 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
10114 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
10115 status
= Reloc::addr14(view
, value
, overflow
);
10118 case elfcpp::R_POWERPC_COPY
:
10119 case elfcpp::R_POWERPC_GLOB_DAT
:
10120 case elfcpp::R_POWERPC_JMP_SLOT
:
10121 case elfcpp::R_POWERPC_RELATIVE
:
10122 case elfcpp::R_POWERPC_DTPMOD
:
10123 case elfcpp::R_PPC64_JMP_IREL
:
10124 case elfcpp::R_POWERPC_IRELATIVE
:
10125 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
10126 _("unexpected reloc %u in object file"),
10130 case elfcpp::R_PPC64_TOCSAVE
:
10136 Symbol_location loc
;
10137 loc
.object
= relinfo
->object
;
10138 loc
.shndx
= relinfo
->data_shndx
;
10139 loc
.offset
= rela
.get_r_offset();
10140 Tocsave_loc::const_iterator p
= target
->tocsave_loc().find(loc
);
10141 if (p
!= target
->tocsave_loc().end())
10143 // If we've generated plt calls using this tocsave, then
10144 // the nop needs to be changed to save r2.
10145 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
10146 if (elfcpp::Swap
<32, big_endian
>::readval(iview
) == nop
)
10147 elfcpp::Swap
<32, big_endian
>::
10148 writeval(iview
, std_2_1
+ target
->stk_toc());
10153 case elfcpp::R_PPC_EMB_SDA2I16
:
10154 case elfcpp::R_PPC_EMB_SDA2REL
:
10157 // R_PPC64_TLSGD, R_PPC64_TLSLD
10160 case elfcpp::R_POWERPC_PLT32
:
10161 case elfcpp::R_POWERPC_PLTREL32
:
10162 case elfcpp::R_PPC_SDAREL16
:
10163 case elfcpp::R_POWERPC_ADDR30
:
10164 case elfcpp::R_PPC64_PLT64
:
10165 case elfcpp::R_PPC64_PLTREL64
:
10166 case elfcpp::R_PPC64_PLTGOT16
:
10167 case elfcpp::R_PPC64_PLTGOT16_LO
:
10168 case elfcpp::R_PPC64_PLTGOT16_HI
:
10169 case elfcpp::R_PPC64_PLTGOT16_HA
:
10170 case elfcpp::R_PPC64_PLTGOT16_DS
:
10171 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
10172 case elfcpp::R_PPC_EMB_RELSDA
:
10173 case elfcpp::R_PPC_TOC16
:
10176 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
10177 _("unsupported reloc %u"),
10182 if (maybe_dq_reloc
)
10185 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
10187 if ((insn
& (0x3f << 26)) == 56u << 26 /* lq */
10188 || ((insn
& (0x3f << 26)) == (61u << 26) /* lxv, stxv */
10189 && (insn
& 3) == 1))
10190 status
= Reloc::addr16_dq(view
, value
, overflow
);
10191 else if (size
== 64
10192 || (insn
& (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
10193 || (insn
& (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
10194 || (insn
& (0x3f << 26)) == 57u << 26 /* lfdp */
10195 || (insn
& (0x3f << 26)) == 61u << 26 /* stfdp */)
10196 status
= Reloc::addr16_ds(view
, value
, overflow
);
10198 status
= Reloc::addr16(view
, value
, overflow
);
10201 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
10204 && gsym
->is_undefined()
10205 && is_branch_reloc(r_type
))))
10207 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
10208 _("relocation overflow"));
10209 if (has_stub_value
)
10210 gold_info(_("try relinking with a smaller --stub-group-size"));
10216 // Relocate section data.
10218 template<int size
, bool big_endian
>
10220 Target_powerpc
<size
, big_endian
>::relocate_section(
10221 const Relocate_info
<size
, big_endian
>* relinfo
,
10222 unsigned int sh_type
,
10223 const unsigned char* prelocs
,
10224 size_t reloc_count
,
10225 Output_section
* output_section
,
10226 bool needs_special_offset_handling
,
10227 unsigned char* view
,
10229 section_size_type view_size
,
10230 const Reloc_symbol_changes
* reloc_symbol_changes
)
10232 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
10233 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
10234 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
10235 Powerpc_comdat_behavior
;
10236 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
10239 gold_assert(sh_type
== elfcpp::SHT_RELA
);
10241 gold::relocate_section
<size
, big_endian
, Powerpc
, Powerpc_relocate
,
10242 Powerpc_comdat_behavior
, Classify_reloc
>(
10248 needs_special_offset_handling
,
10252 reloc_symbol_changes
);
10255 template<int size
, bool big_endian
>
10256 class Powerpc_scan_relocatable_reloc
10259 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
10260 static const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
10261 static const int sh_type
= elfcpp::SHT_RELA
;
10263 // Return the symbol referred to by the relocation.
10264 static inline unsigned int
10265 get_r_sym(const Reltype
* reloc
)
10266 { return elfcpp::elf_r_sym
<size
>(reloc
->get_r_info()); }
10268 // Return the type of the relocation.
10269 static inline unsigned int
10270 get_r_type(const Reltype
* reloc
)
10271 { return elfcpp::elf_r_type
<size
>(reloc
->get_r_info()); }
10273 // Return the strategy to use for a local symbol which is not a
10274 // section symbol, given the relocation type.
10275 inline Relocatable_relocs::Reloc_strategy
10276 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
10278 if (r_type
== 0 && r_sym
== 0)
10279 return Relocatable_relocs::RELOC_DISCARD
;
10280 return Relocatable_relocs::RELOC_COPY
;
10283 // Return the strategy to use for a local symbol which is a section
10284 // symbol, given the relocation type.
10285 inline Relocatable_relocs::Reloc_strategy
10286 local_section_strategy(unsigned int, Relobj
*)
10288 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
10291 // Return the strategy to use for a global symbol, given the
10292 // relocation type, the object, and the symbol index.
10293 inline Relocatable_relocs::Reloc_strategy
10294 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
10297 && (r_type
== elfcpp::R_PPC_PLTREL24
10298 || r_type
== elfcpp::R_POWERPC_PLT16_LO
10299 || r_type
== elfcpp::R_POWERPC_PLT16_HI
10300 || r_type
== elfcpp::R_POWERPC_PLT16_HA
))
10301 return Relocatable_relocs::RELOC_SPECIAL
;
10302 return Relocatable_relocs::RELOC_COPY
;
10306 // Scan the relocs during a relocatable link.
10308 template<int size
, bool big_endian
>
10310 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
10311 Symbol_table
* symtab
,
10313 Sized_relobj_file
<size
, big_endian
>* object
,
10314 unsigned int data_shndx
,
10315 unsigned int sh_type
,
10316 const unsigned char* prelocs
,
10317 size_t reloc_count
,
10318 Output_section
* output_section
,
10319 bool needs_special_offset_handling
,
10320 size_t local_symbol_count
,
10321 const unsigned char* plocal_symbols
,
10322 Relocatable_relocs
* rr
)
10324 typedef Powerpc_scan_relocatable_reloc
<size
, big_endian
> Scan_strategy
;
10326 gold_assert(sh_type
== elfcpp::SHT_RELA
);
10328 gold::scan_relocatable_relocs
<size
, big_endian
, Scan_strategy
>(
10336 needs_special_offset_handling
,
10337 local_symbol_count
,
10342 // Scan the relocs for --emit-relocs.
10344 template<int size
, bool big_endian
>
10346 Target_powerpc
<size
, big_endian
>::emit_relocs_scan(
10347 Symbol_table
* symtab
,
10349 Sized_relobj_file
<size
, big_endian
>* object
,
10350 unsigned int data_shndx
,
10351 unsigned int sh_type
,
10352 const unsigned char* prelocs
,
10353 size_t reloc_count
,
10354 Output_section
* output_section
,
10355 bool needs_special_offset_handling
,
10356 size_t local_symbol_count
,
10357 const unsigned char* plocal_syms
,
10358 Relocatable_relocs
* rr
)
10360 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, big_endian
>
10362 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
10363 Emit_relocs_strategy
;
10365 gold_assert(sh_type
== elfcpp::SHT_RELA
);
10367 gold::scan_relocatable_relocs
<size
, big_endian
, Emit_relocs_strategy
>(
10375 needs_special_offset_handling
,
10376 local_symbol_count
,
10381 // Emit relocations for a section.
10382 // This is a modified version of the function by the same name in
10383 // target-reloc.h. Using relocate_special_relocatable for
10384 // R_PPC_PLTREL24 would require duplication of the entire body of the
10385 // loop, so we may as well duplicate the whole thing.
10387 template<int size
, bool big_endian
>
10389 Target_powerpc
<size
, big_endian
>::relocate_relocs(
10390 const Relocate_info
<size
, big_endian
>* relinfo
,
10391 unsigned int sh_type
,
10392 const unsigned char* prelocs
,
10393 size_t reloc_count
,
10394 Output_section
* output_section
,
10395 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
10397 Address view_address
,
10399 unsigned char* reloc_view
,
10400 section_size_type reloc_view_size
)
10402 gold_assert(sh_type
== elfcpp::SHT_RELA
);
10404 typedef typename
elfcpp::Rela
<size
, big_endian
> Reltype
;
10405 typedef typename
elfcpp::Rela_write
<size
, big_endian
> Reltype_write
;
10406 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
10407 // Offset from start of insn to d-field reloc.
10408 const int d_offset
= big_endian
? 2 : 0;
10410 Powerpc_relobj
<size
, big_endian
>* const object
10411 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
10412 const unsigned int local_count
= object
->local_symbol_count();
10413 unsigned int got2_shndx
= object
->got2_shndx();
10414 Address got2_addend
= 0;
10415 if (got2_shndx
!= 0)
10417 got2_addend
= object
->get_output_section_offset(got2_shndx
);
10418 gold_assert(got2_addend
!= invalid_address
);
10421 const bool relocatable
= parameters
->options().relocatable();
10423 unsigned char* pwrite
= reloc_view
;
10424 bool zap_next
= false;
10425 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
10427 Relocatable_relocs::Reloc_strategy strategy
= relinfo
->rr
->strategy(i
);
10428 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
10431 Reltype
reloc(prelocs
);
10432 Reltype_write
reloc_write(pwrite
);
10434 Address offset
= reloc
.get_r_offset();
10435 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
10436 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
10437 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
10438 const unsigned int orig_r_sym
= r_sym
;
10439 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
10440 = reloc
.get_r_addend();
10441 const Symbol
* gsym
= NULL
;
10445 // We could arrange to discard these and other relocs for
10446 // tls optimised sequences in the strategy methods, but for
10447 // now do as BFD ld does.
10448 r_type
= elfcpp::R_POWERPC_NONE
;
10452 // Get the new symbol index.
10453 Output_section
* os
= NULL
;
10454 if (r_sym
< local_count
)
10458 case Relocatable_relocs::RELOC_COPY
:
10459 case Relocatable_relocs::RELOC_SPECIAL
:
10462 r_sym
= object
->symtab_index(r_sym
);
10463 gold_assert(r_sym
!= -1U);
10467 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
10469 // We are adjusting a section symbol. We need to find
10470 // the symbol table index of the section symbol for
10471 // the output section corresponding to input section
10472 // in which this symbol is defined.
10473 gold_assert(r_sym
< local_count
);
10475 unsigned int shndx
=
10476 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
10477 gold_assert(is_ordinary
);
10478 os
= object
->output_section(shndx
);
10479 gold_assert(os
!= NULL
);
10480 gold_assert(os
->needs_symtab_index());
10481 r_sym
= os
->symtab_index();
10486 gold_unreachable();
10491 gsym
= object
->global_symbol(r_sym
);
10492 gold_assert(gsym
!= NULL
);
10493 if (gsym
->is_forwarder())
10494 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
10496 gold_assert(gsym
->has_symtab_index());
10497 r_sym
= gsym
->symtab_index();
10500 // Get the new offset--the location in the output section where
10501 // this relocation should be applied.
10502 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
10503 offset
+= offset_in_output_section
;
10506 section_offset_type sot_offset
=
10507 convert_types
<section_offset_type
, Address
>(offset
);
10508 section_offset_type new_sot_offset
=
10509 output_section
->output_offset(object
, relinfo
->data_shndx
,
10511 gold_assert(new_sot_offset
!= -1);
10512 offset
= new_sot_offset
;
10515 // In an object file, r_offset is an offset within the section.
10516 // In an executable or dynamic object, generated by
10517 // --emit-relocs, r_offset is an absolute address.
10520 offset
+= view_address
;
10521 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
10522 offset
-= offset_in_output_section
;
10525 // Handle the reloc addend based on the strategy.
10526 if (strategy
== Relocatable_relocs::RELOC_COPY
)
10528 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
10530 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
10531 addend
= psymval
->value(object
, addend
);
10532 // In a relocatable link, the symbol value is relative to
10533 // the start of the output section. For a non-relocatable
10534 // link, we need to adjust the addend.
10537 gold_assert(os
!= NULL
);
10538 addend
-= os
->address();
10541 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
10545 if (addend
>= 32768)
10546 addend
+= got2_addend
;
10548 else if (r_type
== elfcpp::R_POWERPC_REL16_HA
)
10550 r_type
= elfcpp::R_POWERPC_ADDR16_HA
;
10551 addend
-= d_offset
;
10553 else if (r_type
== elfcpp::R_POWERPC_REL16_LO
)
10555 r_type
= elfcpp::R_POWERPC_ADDR16_LO
;
10556 addend
-= d_offset
+ 4;
10560 gold_unreachable();
10564 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
10565 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
10566 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
10567 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
10569 // First instruction of a global dynamic sequence,
10571 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
10572 switch (this->optimize_tls_gd(final
))
10574 case tls::TLSOPT_TO_IE
:
10575 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
10576 - elfcpp::R_POWERPC_GOT_TLSGD16
);
10578 case tls::TLSOPT_TO_LE
:
10579 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
10580 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
10581 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
10584 r_type
= elfcpp::R_POWERPC_NONE
;
10585 offset
-= d_offset
;
10592 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
10593 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
10594 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
10595 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
10597 // First instruction of a local dynamic sequence,
10599 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
10601 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
10602 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
10604 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
10605 const Output_section
* os
= relinfo
->layout
->tls_segment()
10607 gold_assert(os
!= NULL
);
10608 gold_assert(os
->needs_symtab_index());
10609 r_sym
= os
->symtab_index();
10610 addend
= dtp_offset
;
10614 r_type
= elfcpp::R_POWERPC_NONE
;
10615 offset
-= d_offset
;
10619 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
10620 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
10621 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
10622 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
10624 // First instruction of initial exec sequence.
10625 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
10626 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
10628 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
10629 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
10630 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
10633 r_type
= elfcpp::R_POWERPC_NONE
;
10634 offset
-= d_offset
;
10638 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
10639 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
10641 // Second instruction of a global dynamic sequence,
10642 // the __tls_get_addr call
10643 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
10644 switch (this->optimize_tls_gd(final
))
10646 case tls::TLSOPT_TO_IE
:
10647 r_type
= elfcpp::R_POWERPC_NONE
;
10650 case tls::TLSOPT_TO_LE
:
10651 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
10652 offset
+= d_offset
;
10659 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
10660 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
10662 // Second instruction of a local dynamic sequence,
10663 // the __tls_get_addr call
10664 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
10666 const Output_section
* os
= relinfo
->layout
->tls_segment()
10668 gold_assert(os
!= NULL
);
10669 gold_assert(os
->needs_symtab_index());
10670 r_sym
= os
->symtab_index();
10671 addend
= dtp_offset
;
10672 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
10673 offset
+= d_offset
;
10677 else if (r_type
== elfcpp::R_POWERPC_TLS
)
10679 // Second instruction of an initial exec sequence
10680 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
10681 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
10683 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
10684 offset
+= d_offset
;
10689 reloc_write
.put_r_offset(offset
);
10690 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
10691 reloc_write
.put_r_addend(addend
);
10693 pwrite
+= reloc_size
;
10696 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
10697 == reloc_view_size
);
10700 // Return the value to use for a dynamic symbol which requires special
10701 // treatment. This is how we support equality comparisons of function
10702 // pointers across shared library boundaries, as described in the
10703 // processor specific ABI supplement.
10705 template<int size
, bool big_endian
>
10707 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
10711 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
10712 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
10713 p
!= this->stub_tables_
.end();
10716 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
10717 = (*p
)->find_plt_call_entry(gsym
);
10719 return (*p
)->stub_address() + ent
->off_
;
10722 else if (this->abiversion() >= 2)
10724 Address off
= this->glink_section()->find_global_entry(gsym
);
10725 if (off
!= invalid_address
)
10726 return this->glink_section()->global_entry_address() + off
;
10728 gold_unreachable();
10731 // Return the PLT address to use for a local symbol.
10732 template<int size
, bool big_endian
>
10734 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
10735 const Relobj
* object
,
10736 unsigned int symndx
) const
10740 const Sized_relobj
<size
, big_endian
>* relobj
10741 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
10742 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
10743 p
!= this->stub_tables_
.end();
10746 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
10747 = (*p
)->find_plt_call_entry(relobj
->sized_relobj(), symndx
);
10749 return (*p
)->stub_address() + ent
->off_
;
10752 gold_unreachable();
10755 // Return the PLT address to use for a global symbol.
10756 template<int size
, bool big_endian
>
10758 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
10759 const Symbol
* gsym
) const
10763 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
10764 p
!= this->stub_tables_
.end();
10767 const typename Stub_table
<size
, big_endian
>::Plt_stub_ent
* ent
10768 = (*p
)->find_plt_call_entry(gsym
);
10770 return (*p
)->stub_address() + ent
->off_
;
10773 else if (this->abiversion() >= 2)
10775 Address off
= this->glink_section()->find_global_entry(gsym
);
10776 if (off
!= invalid_address
)
10777 return this->glink_section()->global_entry_address() + off
;
10779 gold_unreachable();
10782 // Return the offset to use for the GOT_INDX'th got entry which is
10783 // for a local tls symbol specified by OBJECT, SYMNDX.
10784 template<int size
, bool big_endian
>
10786 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
10787 const Relobj
* object
,
10788 unsigned int symndx
,
10789 unsigned int got_indx
) const
10791 const Powerpc_relobj
<size
, big_endian
>* ppc_object
10792 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
10793 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
10795 for (Got_type got_type
= GOT_TYPE_TLSGD
;
10796 got_type
<= GOT_TYPE_TPREL
;
10797 got_type
= Got_type(got_type
+ 1))
10798 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
10800 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
10801 if (got_type
== GOT_TYPE_TLSGD
)
10803 if (off
== got_indx
* (size
/ 8))
10805 if (got_type
== GOT_TYPE_TPREL
)
10808 return -dtp_offset
;
10812 gold_unreachable();
10815 // Return the offset to use for the GOT_INDX'th got entry which is
10816 // for global tls symbol GSYM.
10817 template<int size
, bool big_endian
>
10819 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
10821 unsigned int got_indx
) const
10823 if (gsym
->type() == elfcpp::STT_TLS
)
10825 for (Got_type got_type
= GOT_TYPE_TLSGD
;
10826 got_type
<= GOT_TYPE_TPREL
;
10827 got_type
= Got_type(got_type
+ 1))
10828 if (gsym
->has_got_offset(got_type
))
10830 unsigned int off
= gsym
->got_offset(got_type
);
10831 if (got_type
== GOT_TYPE_TLSGD
)
10833 if (off
== got_indx
* (size
/ 8))
10835 if (got_type
== GOT_TYPE_TPREL
)
10838 return -dtp_offset
;
10842 gold_unreachable();
10845 // The selector for powerpc object files.
10847 template<int size
, bool big_endian
>
10848 class Target_selector_powerpc
: public Target_selector
10851 Target_selector_powerpc()
10852 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
10855 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
10856 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
10858 ? (big_endian
? "elf64ppc" : "elf64lppc")
10859 : (big_endian
? "elf32ppc" : "elf32lppc")))
10863 do_instantiate_target()
10864 { return new Target_powerpc
<size
, big_endian
>(); }
10867 Target_selector_powerpc
<32, true> target_selector_ppc32
;
10868 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
10869 Target_selector_powerpc
<64, true> target_selector_ppc64
;
10870 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
10872 // Instantiate these constants for -O0
10873 template<int size
, bool big_endian
>
10874 const typename Output_data_glink
<size
, big_endian
>::Address
10875 Output_data_glink
<size
, big_endian
>::invalid_address
;
10876 template<int size
, bool big_endian
>
10877 const typename Stub_table
<size
, big_endian
>::Address
10878 Stub_table
<size
, big_endian
>::invalid_address
;
10879 template<int size
, bool big_endian
>
10880 const typename Target_powerpc
<size
, big_endian
>::Address
10881 Target_powerpc
<size
, big_endian
>::invalid_address
;
10883 } // End anonymous namespace.