1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright (C) 2008-2015 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
30 #include "parameters.h"
37 #include "copy-relocs.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
50 template<int size
, bool big_endian
>
51 class Output_data_plt_powerpc
;
53 template<int size
, bool big_endian
>
54 class Output_data_brlt_powerpc
;
56 template<int size
, bool big_endian
>
57 class Output_data_got_powerpc
;
59 template<int size
, bool big_endian
>
60 class Output_data_glink
;
62 template<int size
, bool big_endian
>
65 template<int size
, bool big_endian
>
66 class Output_data_save_res
;
68 template<int size
, bool big_endian
>
71 struct Stub_table_owner
73 Output_section
* output_section
;
74 const Output_section::Input_section
* owner
;
78 is_branch_reloc(unsigned int r_type
);
80 template<int size
, bool big_endian
>
81 class Powerpc_relobj
: public Sized_relobj_file
<size
, big_endian
>
84 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
85 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_refs
;
86 typedef Unordered_map
<Address
, Section_refs
> Access_from
;
88 Powerpc_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
89 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
90 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
91 special_(0), has_small_toc_reloc_(false), opd_valid_(false),
92 opd_ent_(), access_from_map_(), has14_(), stub_table_index_(),
93 e_flags_(ehdr
.get_e_flags()), st_other_()
95 this->set_abiversion(0);
101 // Read the symbols then set up st_other vector.
103 do_read_symbols(Read_symbols_data
*);
105 // The .got2 section shndx.
110 return this->special_
;
115 // The .opd section shndx.
122 return this->special_
;
125 // Init OPD entry arrays.
127 init_opd(size_t opd_size
)
129 size_t count
= this->opd_ent_ndx(opd_size
);
130 this->opd_ent_
.resize(count
);
133 // Return section and offset of function entry for .opd + R_OFF.
135 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
137 size_t ndx
= this->opd_ent_ndx(r_off
);
138 gold_assert(ndx
< this->opd_ent_
.size());
139 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
141 *value
= this->opd_ent_
[ndx
].off
;
142 return this->opd_ent_
[ndx
].shndx
;
145 // Set section and offset of function entry for .opd + R_OFF.
147 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
149 size_t ndx
= this->opd_ent_ndx(r_off
);
150 gold_assert(ndx
< this->opd_ent_
.size());
151 this->opd_ent_
[ndx
].shndx
= shndx
;
152 this->opd_ent_
[ndx
].off
= value
;
155 // Return discard flag for .opd + R_OFF.
157 get_opd_discard(Address r_off
) const
159 size_t ndx
= this->opd_ent_ndx(r_off
);
160 gold_assert(ndx
< this->opd_ent_
.size());
161 return this->opd_ent_
[ndx
].discard
;
164 // Set discard flag for .opd + R_OFF.
166 set_opd_discard(Address r_off
)
168 size_t ndx
= this->opd_ent_ndx(r_off
);
169 gold_assert(ndx
< this->opd_ent_
.size());
170 this->opd_ent_
[ndx
].discard
= true;
175 { return this->opd_valid_
; }
179 { this->opd_valid_
= true; }
181 // Examine .rela.opd to build info about function entry points.
183 scan_opd_relocs(size_t reloc_count
,
184 const unsigned char* prelocs
,
185 const unsigned char* plocal_syms
);
187 // Perform the Sized_relobj_file method, then set up opd info from
190 do_read_relocs(Read_relocs_data
*);
193 do_find_special_sections(Read_symbols_data
* sd
);
195 // Adjust this local symbol value. Return false if the symbol
196 // should be discarded from the output file.
198 do_adjust_local_symbol(Symbol_value
<size
>* lv
) const
200 if (size
== 64 && this->opd_shndx() != 0)
203 if (lv
->input_shndx(&is_ordinary
) != this->opd_shndx())
205 if (this->get_opd_discard(lv
->input_value()))
213 { return &this->access_from_map_
; }
215 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
216 // section at DST_OFF.
218 add_reference(Relobj
* src_obj
,
219 unsigned int src_indx
,
220 typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
222 Section_id
src_id(src_obj
, src_indx
);
223 this->access_from_map_
[dst_off
].insert(src_id
);
226 // Add a reference to the code section specified by the .opd entry
229 add_gc_mark(typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
231 size_t ndx
= this->opd_ent_ndx(dst_off
);
232 if (ndx
>= this->opd_ent_
.size())
233 this->opd_ent_
.resize(ndx
+ 1);
234 this->opd_ent_
[ndx
].gc_mark
= true;
238 process_gc_mark(Symbol_table
* symtab
)
240 for (size_t i
= 0; i
< this->opd_ent_
.size(); i
++)
241 if (this->opd_ent_
[i
].gc_mark
)
243 unsigned int shndx
= this->opd_ent_
[i
].shndx
;
244 symtab
->gc()->worklist().push_back(Section_id(this, shndx
));
248 // Return offset in output GOT section that this object will use
249 // as a TOC pointer. Won't be just a constant with multi-toc support.
251 toc_base_offset() const
255 set_has_small_toc_reloc()
256 { has_small_toc_reloc_
= true; }
259 has_small_toc_reloc() const
260 { return has_small_toc_reloc_
; }
263 set_has_14bit_branch(unsigned int shndx
)
265 if (shndx
>= this->has14_
.size())
266 this->has14_
.resize(shndx
+ 1);
267 this->has14_
[shndx
] = true;
271 has_14bit_branch(unsigned int shndx
) const
272 { return shndx
< this->has14_
.size() && this->has14_
[shndx
]; }
275 set_stub_table(unsigned int shndx
, unsigned int stub_index
)
277 if (shndx
>= this->stub_table_index_
.size())
278 this->stub_table_index_
.resize(shndx
+ 1);
279 this->stub_table_index_
[shndx
] = stub_index
;
282 Stub_table
<size
, big_endian
>*
283 stub_table(unsigned int shndx
)
285 if (shndx
< this->stub_table_index_
.size())
287 Target_powerpc
<size
, big_endian
>* target
288 = static_cast<Target_powerpc
<size
, big_endian
>*>(
289 parameters
->sized_target
<size
, big_endian
>());
290 unsigned int indx
= this->stub_table_index_
[shndx
];
291 gold_assert(indx
< target
->stub_tables().size());
292 return target
->stub_tables()[indx
];
300 this->stub_table_index_
.clear();
305 { return this->e_flags_
& elfcpp::EF_PPC64_ABI
; }
307 // Set ABI version for input and output
309 set_abiversion(int ver
);
312 ppc64_local_entry_offset(const Symbol
* sym
) const
313 { return elfcpp::ppc64_decode_local_entry(sym
->nonvis() >> 3); }
316 ppc64_local_entry_offset(unsigned int symndx
) const
317 { return elfcpp::ppc64_decode_local_entry(this->st_other_
[symndx
] >> 5); }
328 // Return index into opd_ent_ array for .opd entry at OFF.
329 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
330 // apart when the language doesn't use the last 8-byte word, the
331 // environment pointer. Thus dividing the entry section offset by
332 // 16 will give an index into opd_ent_ that works for either layout
333 // of .opd. (It leaves some elements of the vector unused when .opd
334 // entries are spaced 24 bytes apart, but we don't know the spacing
335 // until relocations are processed, and in any case it is possible
336 // for an object to have some entries spaced 16 bytes apart and
337 // others 24 bytes apart.)
339 opd_ent_ndx(size_t off
) const
342 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
343 unsigned int special_
;
345 // For 64-bit, whether this object uses small model relocs to access
347 bool has_small_toc_reloc_
;
349 // Set at the start of gc_process_relocs, when we know opd_ent_
350 // vector is valid. The flag could be made atomic and set in
351 // do_read_relocs with memory_order_release and then tested with
352 // memory_order_acquire, potentially resulting in fewer entries in
356 // The first 8-byte word of an OPD entry gives the address of the
357 // entry point of the function. Relocatable object files have a
358 // relocation on this word. The following vector records the
359 // section and offset specified by these relocations.
360 std::vector
<Opd_ent
> opd_ent_
;
362 // References made to this object's .opd section when running
363 // gc_process_relocs for another object, before the opd_ent_ vector
364 // is valid for this object.
365 Access_from access_from_map_
;
367 // Whether input section has a 14-bit branch reloc.
368 std::vector
<bool> has14_
;
370 // The stub table to use for a given input section.
371 std::vector
<unsigned int> stub_table_index_
;
374 elfcpp::Elf_Word e_flags_
;
376 // ELF st_other field for local symbols.
377 std::vector
<unsigned char> st_other_
;
380 template<int size
, bool big_endian
>
381 class Powerpc_dynobj
: public Sized_dynobj
<size
, big_endian
>
384 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
386 Powerpc_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
387 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
388 : Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
389 opd_shndx_(0), opd_ent_(), e_flags_(ehdr
.get_e_flags())
391 this->set_abiversion(0);
397 // Call Sized_dynobj::do_read_symbols to read the symbols then
398 // read .opd from a dynamic object, filling in opd_ent_ vector,
400 do_read_symbols(Read_symbols_data
*);
402 // The .opd section shndx.
406 return this->opd_shndx_
;
409 // The .opd section address.
413 return this->opd_address_
;
416 // Init OPD entry arrays.
418 init_opd(size_t opd_size
)
420 size_t count
= this->opd_ent_ndx(opd_size
);
421 this->opd_ent_
.resize(count
);
424 // Return section and offset of function entry for .opd + R_OFF.
426 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
428 size_t ndx
= this->opd_ent_ndx(r_off
);
429 gold_assert(ndx
< this->opd_ent_
.size());
430 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
432 *value
= this->opd_ent_
[ndx
].off
;
433 return this->opd_ent_
[ndx
].shndx
;
436 // Set section and offset of function entry for .opd + R_OFF.
438 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
440 size_t ndx
= this->opd_ent_ndx(r_off
);
441 gold_assert(ndx
< this->opd_ent_
.size());
442 this->opd_ent_
[ndx
].shndx
= shndx
;
443 this->opd_ent_
[ndx
].off
= value
;
448 { return this->e_flags_
& elfcpp::EF_PPC64_ABI
; }
450 // Set ABI version for input and output.
452 set_abiversion(int ver
);
455 // Used to specify extent of executable sections.
458 Sec_info(Address start_
, Address len_
, unsigned int shndx_
)
459 : start(start_
), len(len_
), shndx(shndx_
)
463 operator<(const Sec_info
& that
) const
464 { return this->start
< that
.start
; }
477 // Return index into opd_ent_ array for .opd entry at OFF.
479 opd_ent_ndx(size_t off
) const
482 // For 64-bit the .opd section shndx and address.
483 unsigned int opd_shndx_
;
484 Address opd_address_
;
486 // The first 8-byte word of an OPD entry gives the address of the
487 // entry point of the function. Records the section and offset
488 // corresponding to the address. Note that in dynamic objects,
489 // offset is *not* relative to the section.
490 std::vector
<Opd_ent
> opd_ent_
;
493 elfcpp::Elf_Word e_flags_
;
496 template<int size
, bool big_endian
>
497 class Target_powerpc
: public Sized_target
<size
, big_endian
>
501 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
502 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
503 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Signed_address
;
504 static const Address invalid_address
= static_cast<Address
>(0) - 1;
505 // Offset of tp and dtp pointers from start of TLS block.
506 static const Address tp_offset
= 0x7000;
507 static const Address dtp_offset
= 0x8000;
510 : Sized_target
<size
, big_endian
>(&powerpc_info
),
511 got_(NULL
), plt_(NULL
), iplt_(NULL
), brlt_section_(NULL
),
512 glink_(NULL
), rela_dyn_(NULL
), copy_relocs_(elfcpp::R_POWERPC_COPY
),
513 tlsld_got_offset_(-1U),
514 stub_tables_(), branch_lookup_table_(), branch_info_(),
515 plt_thread_safe_(false), relax_failed_(false), relax_fail_count_(0),
516 stub_group_size_(0), savres_section_(0)
520 // Process the relocations to determine unreferenced sections for
521 // garbage collection.
523 gc_process_relocs(Symbol_table
* symtab
,
525 Sized_relobj_file
<size
, big_endian
>* object
,
526 unsigned int data_shndx
,
527 unsigned int sh_type
,
528 const unsigned char* prelocs
,
530 Output_section
* output_section
,
531 bool needs_special_offset_handling
,
532 size_t local_symbol_count
,
533 const unsigned char* plocal_symbols
);
535 // Scan the relocations to look for symbol adjustments.
537 scan_relocs(Symbol_table
* symtab
,
539 Sized_relobj_file
<size
, big_endian
>* object
,
540 unsigned int data_shndx
,
541 unsigned int sh_type
,
542 const unsigned char* prelocs
,
544 Output_section
* output_section
,
545 bool needs_special_offset_handling
,
546 size_t local_symbol_count
,
547 const unsigned char* plocal_symbols
);
549 // Map input .toc section to output .got section.
551 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
553 if (size
== 64 && strcmp(name
, ".toc") == 0)
561 // Provide linker defined save/restore functions.
563 define_save_restore_funcs(Layout
*, Symbol_table
*);
565 // No stubs unless a final link.
568 { return !parameters
->options().relocatable(); }
571 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
574 do_plt_fde_location(const Output_data
*, unsigned char*,
575 uint64_t*, off_t
*) const;
577 // Stash info about branches, for stub generation.
579 push_branch(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
580 unsigned int data_shndx
, Address r_offset
,
581 unsigned int r_type
, unsigned int r_sym
, Address addend
)
583 Branch_info
info(ppc_object
, data_shndx
, r_offset
, r_type
, r_sym
, addend
);
584 this->branch_info_
.push_back(info
);
585 if (r_type
== elfcpp::R_POWERPC_REL14
586 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
587 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
588 ppc_object
->set_has_14bit_branch(data_shndx
);
592 do_define_standard_symbols(Symbol_table
*, Layout
*);
594 // Finalize the sections.
596 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
598 // Return the value to use for a dynamic which requires special
601 do_dynsym_value(const Symbol
*) const;
603 // Return the PLT address to use for a local symbol.
605 do_plt_address_for_local(const Relobj
*, unsigned int) const;
607 // Return the PLT address to use for a global symbol.
609 do_plt_address_for_global(const Symbol
*) const;
611 // Return the offset to use for the GOT_INDX'th got entry which is
612 // for a local tls symbol specified by OBJECT, SYMNDX.
614 do_tls_offset_for_local(const Relobj
* object
,
616 unsigned int got_indx
) const;
618 // Return the offset to use for the GOT_INDX'th got entry which is
619 // for global tls symbol GSYM.
621 do_tls_offset_for_global(Symbol
* gsym
, unsigned int got_indx
) const;
624 do_function_location(Symbol_location
*) const;
627 do_can_check_for_function_pointers() const
630 // Adjust -fsplit-stack code which calls non-split-stack code.
632 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
633 section_offset_type fnoffset
, section_size_type fnsize
,
634 unsigned char* view
, section_size_type view_size
,
635 std::string
* from
, std::string
* to
) const;
637 // Relocate a section.
639 relocate_section(const Relocate_info
<size
, big_endian
>*,
640 unsigned int sh_type
,
641 const unsigned char* prelocs
,
643 Output_section
* output_section
,
644 bool needs_special_offset_handling
,
646 Address view_address
,
647 section_size_type view_size
,
648 const Reloc_symbol_changes
*);
650 // Scan the relocs during a relocatable link.
652 scan_relocatable_relocs(Symbol_table
* symtab
,
654 Sized_relobj_file
<size
, big_endian
>* object
,
655 unsigned int data_shndx
,
656 unsigned int sh_type
,
657 const unsigned char* prelocs
,
659 Output_section
* output_section
,
660 bool needs_special_offset_handling
,
661 size_t local_symbol_count
,
662 const unsigned char* plocal_symbols
,
663 Relocatable_relocs
*);
665 // Emit relocations for a section.
667 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
668 unsigned int sh_type
,
669 const unsigned char* prelocs
,
671 Output_section
* output_section
,
672 typename
elfcpp::Elf_types
<size
>::Elf_Off
673 offset_in_output_section
,
675 Address view_address
,
677 unsigned char* reloc_view
,
678 section_size_type reloc_view_size
);
680 // Return whether SYM is defined by the ABI.
682 do_is_defined_by_abi(const Symbol
* sym
) const
684 return strcmp(sym
->name(), "__tls_get_addr") == 0;
687 // Return the size of the GOT section.
691 gold_assert(this->got_
!= NULL
);
692 return this->got_
->data_size();
695 // Get the PLT section.
696 const Output_data_plt_powerpc
<size
, big_endian
>*
699 gold_assert(this->plt_
!= NULL
);
703 // Get the IPLT section.
704 const Output_data_plt_powerpc
<size
, big_endian
>*
707 gold_assert(this->iplt_
!= NULL
);
711 // Get the .glink section.
712 const Output_data_glink
<size
, big_endian
>*
713 glink_section() const
715 gold_assert(this->glink_
!= NULL
);
719 Output_data_glink
<size
, big_endian
>*
722 gold_assert(this->glink_
!= NULL
);
726 bool has_glink() const
727 { return this->glink_
!= NULL
; }
729 // Get the GOT section.
730 const Output_data_got_powerpc
<size
, big_endian
>*
733 gold_assert(this->got_
!= NULL
);
737 // Get the GOT section, creating it if necessary.
738 Output_data_got_powerpc
<size
, big_endian
>*
739 got_section(Symbol_table
*, Layout
*);
742 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
743 const elfcpp::Ehdr
<size
, big_endian
>&);
745 // Return the number of entries in the GOT.
747 got_entry_count() const
749 if (this->got_
== NULL
)
751 return this->got_size() / (size
/ 8);
754 // Return the number of entries in the PLT.
756 plt_entry_count() const;
758 // Return the offset of the first non-reserved PLT entry.
760 first_plt_entry_offset() const
764 if (this->abiversion() >= 2)
769 // Return the size of each PLT entry.
771 plt_entry_size() const
775 if (this->abiversion() >= 2)
780 Output_data_save_res
<size
, big_endian
>*
781 savres_section() const
783 return this->savres_section_
;
786 // Add any special sections for this symbol to the gc work list.
787 // For powerpc64, this adds the code section of a function
790 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
792 // Handle target specific gc actions when adding a gc reference from
793 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
794 // and DST_OFF. For powerpc64, this adds a referenc to the code
795 // section of a function descriptor.
797 do_gc_add_reference(Symbol_table
* symtab
,
799 unsigned int src_shndx
,
801 unsigned int dst_shndx
,
802 Address dst_off
) const;
804 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
807 { return this->stub_tables_
; }
809 const Output_data_brlt_powerpc
<size
, big_endian
>*
811 { return this->brlt_section_
; }
814 add_branch_lookup_table(Address to
)
816 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
817 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
821 find_branch_lookup_table(Address to
)
823 typename
Branch_lookup_table::const_iterator p
824 = this->branch_lookup_table_
.find(to
);
825 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
829 write_branch_lookup_table(unsigned char *oview
)
831 for (typename
Branch_lookup_table::const_iterator p
832 = this->branch_lookup_table_
.begin();
833 p
!= this->branch_lookup_table_
.end();
836 elfcpp::Swap
<size
, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
841 plt_thread_safe() const
842 { return this->plt_thread_safe_
; }
846 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI
; }
849 set_abiversion (int ver
)
851 elfcpp::Elf_Word flags
= this->processor_specific_flags();
852 flags
&= ~elfcpp::EF_PPC64_ABI
;
853 flags
|= ver
& elfcpp::EF_PPC64_ABI
;
854 this->set_processor_specific_flags(flags
);
857 // Offset to to save stack slot
860 { return this->abiversion() < 2 ? 40 : 24; }
876 : tls_get_addr_(NOT_EXPECTED
),
877 relinfo_(NULL
), relnum_(0), r_offset_(0)
882 if (this->tls_get_addr_
!= NOT_EXPECTED
)
889 if (this->relinfo_
!= NULL
)
890 gold_error_at_location(this->relinfo_
, this->relnum_
, this->r_offset_
,
891 _("missing expected __tls_get_addr call"));
895 expect_tls_get_addr_call(
896 const Relocate_info
<size
, big_endian
>* relinfo
,
900 this->tls_get_addr_
= EXPECTED
;
901 this->relinfo_
= relinfo
;
902 this->relnum_
= relnum
;
903 this->r_offset_
= r_offset
;
907 expect_tls_get_addr_call()
908 { this->tls_get_addr_
= EXPECTED
; }
911 skip_next_tls_get_addr_call()
912 {this->tls_get_addr_
= SKIP
; }
915 maybe_skip_tls_get_addr_call(unsigned int r_type
, const Symbol
* gsym
)
917 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
918 || r_type
== elfcpp::R_PPC_PLTREL24
)
920 && strcmp(gsym
->name(), "__tls_get_addr") == 0);
921 Tls_get_addr last_tls
= this->tls_get_addr_
;
922 this->tls_get_addr_
= NOT_EXPECTED
;
923 if (is_tls_call
&& last_tls
!= EXPECTED
)
925 else if (!is_tls_call
&& last_tls
!= NOT_EXPECTED
)
934 // What we're up to regarding calls to __tls_get_addr.
935 // On powerpc, the branch and link insn making a call to
936 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
937 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
938 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
939 // The marker relocation always comes first, and has the same
940 // symbol as the reloc on the insn setting up the __tls_get_addr
941 // argument. This ties the arg setup insn with the call insn,
942 // allowing ld to safely optimize away the call. We check that
943 // every call to __tls_get_addr has a marker relocation, and that
944 // every marker relocation is on a call to __tls_get_addr.
945 Tls_get_addr tls_get_addr_
;
946 // Info about the last reloc for error message.
947 const Relocate_info
<size
, big_endian
>* relinfo_
;
952 // The class which scans relocations.
953 class Scan
: protected Track_tls
956 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
959 : Track_tls(), issued_non_pic_error_(false)
963 get_reference_flags(unsigned int r_type
, const Target_powerpc
* target
);
966 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
967 Sized_relobj_file
<size
, big_endian
>* object
,
968 unsigned int data_shndx
,
969 Output_section
* output_section
,
970 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
971 const elfcpp::Sym
<size
, big_endian
>& lsym
,
975 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
976 Sized_relobj_file
<size
, big_endian
>* object
,
977 unsigned int data_shndx
,
978 Output_section
* output_section
,
979 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
983 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
985 Sized_relobj_file
<size
, big_endian
>* relobj
,
988 const elfcpp::Rela
<size
, big_endian
>& ,
990 const elfcpp::Sym
<size
, big_endian
>&)
992 // PowerPC64 .opd is not folded, so any identical function text
993 // may be folded and we'll still keep function addresses distinct.
994 // That means no reloc is of concern here.
997 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
998 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
999 if (ppcobj
->abiversion() == 1)
1002 // For 32-bit and ELFv2, conservatively assume anything but calls to
1003 // function code might be taking the address of the function.
1004 return !is_branch_reloc(r_type
);
1008 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1010 Sized_relobj_file
<size
, big_endian
>* relobj
,
1013 const elfcpp::Rela
<size
, big_endian
>& ,
1014 unsigned int r_type
,
1020 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1021 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1022 if (ppcobj
->abiversion() == 1)
1025 return !is_branch_reloc(r_type
);
1029 reloc_needs_plt_for_ifunc(Target_powerpc
<size
, big_endian
>* target
,
1030 Sized_relobj_file
<size
, big_endian
>* object
,
1031 unsigned int r_type
, bool report_err
);
1035 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
1036 unsigned int r_type
);
1039 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
1040 unsigned int r_type
, Symbol
*);
1043 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
1044 Target_powerpc
* target
);
1047 check_non_pic(Relobj
*, unsigned int r_type
);
1049 // Whether we have issued an error about a non-PIC compilation.
1050 bool issued_non_pic_error_
;
1054 symval_for_branch(const Symbol_table
* symtab
,
1055 const Sized_symbol
<size
>* gsym
,
1056 Powerpc_relobj
<size
, big_endian
>* object
,
1057 Address
*value
, unsigned int *dest_shndx
);
1059 // The class which implements relocation.
1060 class Relocate
: protected Track_tls
1063 // Use 'at' branch hints when true, 'y' when false.
1064 // FIXME maybe: set this with an option.
1065 static const bool is_isa_v2
= true;
1071 // Do a relocation. Return false if the caller should not issue
1072 // any warnings about this relocation.
1074 relocate(const Relocate_info
<size
, big_endian
>*, unsigned int,
1075 Target_powerpc
*, Output_section
*, size_t, const unsigned char*,
1076 const Sized_symbol
<size
>*, const Symbol_value
<size
>*,
1077 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1081 class Relocate_comdat_behavior
1084 // Decide what the linker should do for relocations that refer to
1085 // discarded comdat sections.
1086 inline Comdat_behavior
1087 get(const char* name
)
1089 gold::Default_comdat_behavior default_behavior
;
1090 Comdat_behavior ret
= default_behavior
.get(name
);
1091 if (ret
== CB_WARNING
)
1094 && (strcmp(name
, ".fixup") == 0
1095 || strcmp(name
, ".got2") == 0))
1098 && (strcmp(name
, ".opd") == 0
1099 || strcmp(name
, ".toc") == 0
1100 || strcmp(name
, ".toc1") == 0))
1107 // A class which returns the size required for a relocation type,
1108 // used while scanning relocs during a relocatable link.
1109 class Relocatable_size_for_reloc
1113 get_size_for_reloc(unsigned int, Relobj
*)
1120 // Optimize the TLS relocation type based on what we know about the
1121 // symbol. IS_FINAL is true if the final address of this symbol is
1122 // known at link time.
1124 tls::Tls_optimization
1125 optimize_tls_gd(bool is_final
)
1127 // If we are generating a shared library, then we can't do anything
1129 if (parameters
->options().shared())
1130 return tls::TLSOPT_NONE
;
1133 return tls::TLSOPT_TO_IE
;
1134 return tls::TLSOPT_TO_LE
;
1137 tls::Tls_optimization
1140 if (parameters
->options().shared())
1141 return tls::TLSOPT_NONE
;
1143 return tls::TLSOPT_TO_LE
;
1146 tls::Tls_optimization
1147 optimize_tls_ie(bool is_final
)
1149 if (!is_final
|| parameters
->options().shared())
1150 return tls::TLSOPT_NONE
;
1152 return tls::TLSOPT_TO_LE
;
1157 make_glink_section(Layout
*);
1159 // Create the PLT section.
1161 make_plt_section(Symbol_table
*, Layout
*);
1164 make_iplt_section(Symbol_table
*, Layout
*);
1167 make_brlt_section(Layout
*);
1169 // Create a PLT entry for a global symbol.
1171 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1173 // Create a PLT entry for a local IFUNC symbol.
1175 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1176 Sized_relobj_file
<size
, big_endian
>*,
1180 // Create a GOT entry for local dynamic __tls_get_addr.
1182 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
1183 Sized_relobj_file
<size
, big_endian
>* object
);
1186 tlsld_got_offset() const
1188 return this->tlsld_got_offset_
;
1191 // Get the dynamic reloc section, creating it if necessary.
1193 rela_dyn_section(Layout
*);
1195 // Similarly, but for ifunc symbols get the one for ifunc.
1197 rela_dyn_section(Symbol_table
*, Layout
*, bool for_ifunc
);
1199 // Copy a relocation against a global symbol.
1201 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1202 Sized_relobj_file
<size
, big_endian
>* object
,
1203 unsigned int shndx
, Output_section
* output_section
,
1204 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
1206 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1207 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1208 symtab
->get_sized_symbol
<size
>(sym
),
1209 object
, shndx
, output_section
,
1210 r_type
, reloc
.get_r_offset(),
1211 reloc
.get_r_addend(),
1212 this->rela_dyn_section(layout
));
1215 // Look over all the input sections, deciding where to place stubs.
1217 group_sections(Layout
*, const Task
*, bool);
1219 // Sort output sections by address.
1220 struct Sort_sections
1223 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
1224 { return sec1
->address() < sec2
->address(); }
1230 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1231 unsigned int data_shndx
,
1233 unsigned int r_type
,
1236 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
1237 r_type_(r_type
), r_sym_(r_sym
), addend_(addend
)
1243 // If this branch needs a plt call stub, or a long branch stub, make one.
1245 make_stub(Stub_table
<size
, big_endian
>*,
1246 Stub_table
<size
, big_endian
>*,
1247 Symbol_table
*) const;
1250 // The branch location..
1251 Powerpc_relobj
<size
, big_endian
>* object_
;
1252 unsigned int shndx_
;
1254 // ..and the branch type and destination.
1255 unsigned int r_type_
;
1256 unsigned int r_sym_
;
1260 // Information about this specific target which we pass to the
1261 // general Target structure.
1262 static Target::Target_info powerpc_info
;
1264 // The types of GOT entries needed for this platform.
1265 // These values are exposed to the ABI in an incremental link.
1266 // Do not renumber existing values without changing the version
1267 // number of the .gnu_incremental_inputs section.
1271 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
1272 GOT_TYPE_DTPREL
, // entry for @got@dtprel
1273 GOT_TYPE_TPREL
// entry for @got@tprel
1277 Output_data_got_powerpc
<size
, big_endian
>* got_
;
1278 // The PLT section. This is a container for a table of addresses,
1279 // and their relocations. Each address in the PLT has a dynamic
1280 // relocation (R_*_JMP_SLOT) and each address will have a
1281 // corresponding entry in .glink for lazy resolution of the PLT.
1282 // ppc32 initialises the PLT to point at the .glink entry, while
1283 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1284 // linker adds a stub that loads the PLT entry into ctr then
1285 // branches to ctr. There may be more than one stub for each PLT
1286 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1287 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1288 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
1289 // The IPLT section. Like plt_, this is a container for a table of
1290 // addresses and their relocations, specifically for STT_GNU_IFUNC
1291 // functions that resolve locally (STT_GNU_IFUNC functions that
1292 // don't resolve locally go in PLT). Unlike plt_, these have no
1293 // entry in .glink for lazy resolution, and the relocation section
1294 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1295 // the relocation section may contain relocations against
1296 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1297 // relocation section will appear at the end of other dynamic
1298 // relocations, so that ld.so applies these relocations after other
1299 // dynamic relocations. In a static executable, the relocation
1300 // section is emitted and marked with __rela_iplt_start and
1301 // __rela_iplt_end symbols.
1302 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
1303 // Section holding long branch destinations.
1304 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
1305 // The .glink section.
1306 Output_data_glink
<size
, big_endian
>* glink_
;
1307 // The dynamic reloc section.
1308 Reloc_section
* rela_dyn_
;
1309 // Relocs saved to avoid a COPY reloc.
1310 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
1311 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1312 unsigned int tlsld_got_offset_
;
1314 Stub_tables stub_tables_
;
1315 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
1316 Branch_lookup_table branch_lookup_table_
;
1318 typedef std::vector
<Branch_info
> Branches
;
1319 Branches branch_info_
;
1321 bool plt_thread_safe_
;
1324 int relax_fail_count_
;
1325 int32_t stub_group_size_
;
1327 Output_data_save_res
<size
, big_endian
> *savres_section_
;
1331 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
1334 true, // is_big_endian
1335 elfcpp::EM_PPC
, // machine_code
1336 false, // has_make_symbol
1337 false, // has_resolve
1338 false, // has_code_fill
1339 true, // is_default_stack_executable
1340 false, // can_icf_inline_merge_sections
1342 "/usr/lib/ld.so.1", // dynamic_linker
1343 0x10000000, // default_text_segment_address
1344 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1345 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1346 false, // isolate_execinstr
1348 elfcpp::SHN_UNDEF
, // small_common_shndx
1349 elfcpp::SHN_UNDEF
, // large_common_shndx
1350 0, // small_common_section_flags
1351 0, // large_common_section_flags
1352 NULL
, // attributes_section
1353 NULL
, // attributes_vendor
1354 "_start", // entry_symbol_name
1355 32, // hash_entry_size
1359 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
1362 false, // is_big_endian
1363 elfcpp::EM_PPC
, // machine_code
1364 false, // has_make_symbol
1365 false, // has_resolve
1366 false, // has_code_fill
1367 true, // is_default_stack_executable
1368 false, // can_icf_inline_merge_sections
1370 "/usr/lib/ld.so.1", // dynamic_linker
1371 0x10000000, // default_text_segment_address
1372 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1373 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1374 false, // isolate_execinstr
1376 elfcpp::SHN_UNDEF
, // small_common_shndx
1377 elfcpp::SHN_UNDEF
, // large_common_shndx
1378 0, // small_common_section_flags
1379 0, // large_common_section_flags
1380 NULL
, // attributes_section
1381 NULL
, // attributes_vendor
1382 "_start", // entry_symbol_name
1383 32, // hash_entry_size
1387 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1390 true, // is_big_endian
1391 elfcpp::EM_PPC64
, // machine_code
1392 false, // has_make_symbol
1393 false, // has_resolve
1394 false, // has_code_fill
1395 true, // is_default_stack_executable
1396 false, // can_icf_inline_merge_sections
1398 "/usr/lib/ld.so.1", // dynamic_linker
1399 0x10000000, // default_text_segment_address
1400 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1401 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1402 false, // isolate_execinstr
1404 elfcpp::SHN_UNDEF
, // small_common_shndx
1405 elfcpp::SHN_UNDEF
, // large_common_shndx
1406 0, // small_common_section_flags
1407 0, // large_common_section_flags
1408 NULL
, // attributes_section
1409 NULL
, // attributes_vendor
1410 "_start", // entry_symbol_name
1411 32, // hash_entry_size
1415 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1418 false, // is_big_endian
1419 elfcpp::EM_PPC64
, // machine_code
1420 false, // has_make_symbol
1421 false, // has_resolve
1422 false, // has_code_fill
1423 true, // is_default_stack_executable
1424 false, // can_icf_inline_merge_sections
1426 "/usr/lib/ld.so.1", // dynamic_linker
1427 0x10000000, // default_text_segment_address
1428 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1429 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1430 false, // isolate_execinstr
1432 elfcpp::SHN_UNDEF
, // small_common_shndx
1433 elfcpp::SHN_UNDEF
, // large_common_shndx
1434 0, // small_common_section_flags
1435 0, // large_common_section_flags
1436 NULL
, // attributes_section
1437 NULL
, // attributes_vendor
1438 "_start", // entry_symbol_name
1439 32, // hash_entry_size
1443 is_branch_reloc(unsigned int r_type
)
1445 return (r_type
== elfcpp::R_POWERPC_REL24
1446 || r_type
== elfcpp::R_PPC_PLTREL24
1447 || r_type
== elfcpp::R_PPC_LOCAL24PC
1448 || r_type
== elfcpp::R_POWERPC_REL14
1449 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1450 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1451 || r_type
== elfcpp::R_POWERPC_ADDR24
1452 || r_type
== elfcpp::R_POWERPC_ADDR14
1453 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1454 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1457 // If INSN is an opcode that may be used with an @tls operand, return
1458 // the transformed insn for TLS optimisation, otherwise return 0. If
1459 // REG is non-zero only match an insn with RB or RA equal to REG.
1461 at_tls_transform(uint32_t insn
, unsigned int reg
)
1463 if ((insn
& (0x3f << 26)) != 31 << 26)
1467 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1468 rtra
= insn
& ((1 << 26) - (1 << 16));
1469 else if (((insn
>> 16) & 0x1f) == reg
)
1470 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1474 if ((insn
& (0x3ff << 1)) == 266 << 1)
1477 else if ((insn
& (0x1f << 1)) == 23 << 1
1478 && ((insn
& (0x1f << 6)) < 14 << 6
1479 || ((insn
& (0x1f << 6)) >= 16 << 6
1480 && (insn
& (0x1f << 6)) < 24 << 6)))
1481 // load and store indexed -> dform
1482 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1483 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1484 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1485 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1486 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1488 insn
= (58 << 26) | 2;
1496 template<int size
, bool big_endian
>
1497 class Powerpc_relocate_functions
1517 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1518 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1519 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword SignedAddress
;
1521 template<int valsize
>
1523 has_overflow_signed(Address value
)
1525 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1526 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1527 limit
<<= ((valsize
- 1) >> 1);
1528 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1529 return value
+ limit
> (limit
<< 1) - 1;
1532 template<int valsize
>
1534 has_overflow_unsigned(Address value
)
1536 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1537 limit
<<= ((valsize
- 1) >> 1);
1538 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1539 return value
> (limit
<< 1) - 1;
1542 template<int valsize
>
1544 has_overflow_bitfield(Address value
)
1546 return (has_overflow_unsigned
<valsize
>(value
)
1547 && has_overflow_signed
<valsize
>(value
));
1550 template<int valsize
>
1551 static inline Status
1552 overflowed(Address value
, Overflow_check overflow
)
1554 if (overflow
== CHECK_SIGNED
)
1556 if (has_overflow_signed
<valsize
>(value
))
1557 return STATUS_OVERFLOW
;
1559 else if (overflow
== CHECK_UNSIGNED
)
1561 if (has_overflow_unsigned
<valsize
>(value
))
1562 return STATUS_OVERFLOW
;
1564 else if (overflow
== CHECK_BITFIELD
)
1566 if (has_overflow_bitfield
<valsize
>(value
))
1567 return STATUS_OVERFLOW
;
1572 // Do a simple RELA relocation
1573 template<int fieldsize
, int valsize
>
1574 static inline Status
1575 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1577 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1578 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1579 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, value
);
1580 return overflowed
<valsize
>(value
, overflow
);
1583 template<int fieldsize
, int valsize
>
1584 static inline Status
1585 rela(unsigned char* view
,
1586 unsigned int right_shift
,
1587 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1589 Overflow_check overflow
)
1591 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1592 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1593 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(wv
);
1594 Valtype reloc
= value
>> right_shift
;
1597 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, val
| reloc
);
1598 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1601 // Do a simple RELA relocation, unaligned.
1602 template<int fieldsize
, int valsize
>
1603 static inline Status
1604 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1606 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, value
);
1607 return overflowed
<valsize
>(value
, overflow
);
1610 template<int fieldsize
, int valsize
>
1611 static inline Status
1612 rela_ua(unsigned char* view
,
1613 unsigned int right_shift
,
1614 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1616 Overflow_check overflow
)
1618 typedef typename
elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::Valtype
1620 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(view
);
1621 Valtype reloc
= value
>> right_shift
;
1624 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, val
| reloc
);
1625 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1629 // R_PPC64_ADDR64: (Symbol + Addend)
1631 addr64(unsigned char* view
, Address value
)
1632 { This::template rela
<64,64>(view
, value
, CHECK_NONE
); }
1634 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1636 addr64_u(unsigned char* view
, Address value
)
1637 { This::template rela_ua
<64,64>(view
, value
, CHECK_NONE
); }
1639 // R_POWERPC_ADDR32: (Symbol + Addend)
1640 static inline Status
1641 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
1642 { return This::template rela
<32,32>(view
, value
, overflow
); }
1644 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1645 static inline Status
1646 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1647 { return This::template rela_ua
<32,32>(view
, value
, overflow
); }
1649 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1650 static inline Status
1651 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
1653 Status stat
= This::template rela
<32,26>(view
, 0, 0x03fffffc,
1655 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1656 stat
= STATUS_OVERFLOW
;
1660 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1661 static inline Status
1662 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
1663 { return This::template rela
<16,16>(view
, value
, overflow
); }
1665 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1666 static inline Status
1667 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1668 { return This::template rela_ua
<16,16>(view
, value
, overflow
); }
1670 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1671 static inline Status
1672 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
1674 Status stat
= This::template rela
<16,16>(view
, 0, 0xfffc, value
, overflow
);
1675 if ((value
& 3) != 0)
1676 stat
= STATUS_OVERFLOW
;
1680 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
1681 static inline Status
1682 addr16_dq(unsigned char* view
, Address value
, Overflow_check overflow
)
1684 Status stat
= This::template rela
<16,16>(view
, 0, 0xfff0, value
, overflow
);
1685 if ((value
& 15) != 0)
1686 stat
= STATUS_OVERFLOW
;
1690 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1692 addr16_hi(unsigned char* view
, Address value
)
1693 { This::template rela
<16,16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
1695 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1697 addr16_ha(unsigned char* view
, Address value
)
1698 { This::addr16_hi(view
, value
+ 0x8000); }
1700 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1702 addr16_hi2(unsigned char* view
, Address value
)
1703 { This::template rela
<16,16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
1705 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1707 addr16_ha2(unsigned char* view
, Address value
)
1708 { This::addr16_hi2(view
, value
+ 0x8000); }
1710 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1712 addr16_hi3(unsigned char* view
, Address value
)
1713 { This::template rela
<16,16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
1715 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1717 addr16_ha3(unsigned char* view
, Address value
)
1718 { This::addr16_hi3(view
, value
+ 0x8000); }
1720 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1721 static inline Status
1722 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
1724 Status stat
= This::template rela
<32,16>(view
, 0, 0xfffc, value
, overflow
);
1725 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1726 stat
= STATUS_OVERFLOW
;
1730 // R_POWERPC_REL16DX_HA
1731 static inline Status
1732 addr16dx_ha(unsigned char *view
, Address value
, Overflow_check overflow
)
1734 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1735 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1736 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1738 value
= static_cast<SignedAddress
>(value
) >> 16;
1739 val
|= (value
& 0xffc1) | ((value
& 0x3e) << 15);
1740 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1741 return overflowed
<16>(value
, overflow
);
1745 // Set ABI version for input and output.
1747 template<int size
, bool big_endian
>
1749 Powerpc_relobj
<size
, big_endian
>::set_abiversion(int ver
)
1751 this->e_flags_
|= ver
;
1752 if (this->abiversion() != 0)
1754 Target_powerpc
<size
, big_endian
>* target
=
1755 static_cast<Target_powerpc
<size
, big_endian
>*>(
1756 parameters
->sized_target
<size
, big_endian
>());
1757 if (target
->abiversion() == 0)
1758 target
->set_abiversion(this->abiversion());
1759 else if (target
->abiversion() != this->abiversion())
1760 gold_error(_("%s: ABI version %d is not compatible "
1761 "with ABI version %d output"),
1762 this->name().c_str(),
1763 this->abiversion(), target
->abiversion());
1768 // Stash away the index of .got2 or .opd in a relocatable object, if
1769 // such a section exists.
1771 template<int size
, bool big_endian
>
1773 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
1774 Read_symbols_data
* sd
)
1776 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1777 const unsigned char* namesu
= sd
->section_names
->data();
1778 const char* names
= reinterpret_cast<const char*>(namesu
);
1779 section_size_type names_size
= sd
->section_names_size
;
1780 const unsigned char* s
;
1782 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
,
1783 size
== 32 ? ".got2" : ".opd",
1784 names
, names_size
, NULL
);
1787 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
1788 this->special_
= ndx
;
1791 if (this->abiversion() == 0)
1792 this->set_abiversion(1);
1793 else if (this->abiversion() > 1)
1794 gold_error(_("%s: .opd invalid in abiv%d"),
1795 this->name().c_str(), this->abiversion());
1798 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
1801 // Examine .rela.opd to build info about function entry points.
1803 template<int size
, bool big_endian
>
1805 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
1807 const unsigned char* prelocs
,
1808 const unsigned char* plocal_syms
)
1812 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
1814 const int reloc_size
1815 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
1816 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1817 Address expected_off
= 0;
1818 bool regular
= true;
1819 unsigned int opd_ent_size
= 0;
1821 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
1823 Reltype
reloc(prelocs
);
1824 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
1825 = reloc
.get_r_info();
1826 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
1827 if (r_type
== elfcpp::R_PPC64_ADDR64
)
1829 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1830 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
1833 if (r_sym
< this->local_symbol_count())
1835 typename
elfcpp::Sym
<size
, big_endian
>
1836 lsym(plocal_syms
+ r_sym
* sym_size
);
1837 shndx
= lsym
.get_st_shndx();
1838 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1839 value
= lsym
.get_st_value();
1842 shndx
= this->symbol_section_and_value(r_sym
, &value
,
1844 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
1845 value
+ reloc
.get_r_addend());
1848 expected_off
= reloc
.get_r_offset();
1849 opd_ent_size
= expected_off
;
1851 else if (expected_off
!= reloc
.get_r_offset())
1853 expected_off
+= opd_ent_size
;
1855 else if (r_type
== elfcpp::R_PPC64_TOC
)
1857 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
1862 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1863 this->name().c_str(), r_type
);
1867 if (reloc_count
<= 2)
1868 opd_ent_size
= this->section_size(this->opd_shndx());
1869 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
1873 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1874 this->name().c_str());
1880 template<int size
, bool big_endian
>
1882 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
1884 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
1887 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
1888 p
!= rd
->relocs
.end();
1891 if (p
->data_shndx
== this->opd_shndx())
1893 uint64_t opd_size
= this->section_size(this->opd_shndx());
1894 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
1897 this->init_opd(opd_size
);
1898 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
1899 rd
->local_symbols
->data());
1907 // Read the symbols then set up st_other vector.
1909 template<int size
, bool big_endian
>
1911 Powerpc_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
1913 this->base_read_symbols(sd
);
1916 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
1917 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1918 const unsigned int loccount
= this->do_local_symbol_count();
1921 this->st_other_
.resize(loccount
);
1922 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1923 off_t locsize
= loccount
* sym_size
;
1924 const unsigned int symtab_shndx
= this->symtab_shndx();
1925 const unsigned char *psymtab
= pshdrs
+ symtab_shndx
* shdr_size
;
1926 typename
elfcpp::Shdr
<size
, big_endian
> shdr(psymtab
);
1927 const unsigned char* psyms
= this->get_view(shdr
.get_sh_offset(),
1928 locsize
, true, false);
1930 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
1932 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
1933 unsigned char st_other
= sym
.get_st_other();
1934 this->st_other_
[i
] = st_other
;
1935 if ((st_other
& elfcpp::STO_PPC64_LOCAL_MASK
) != 0)
1937 if (this->abiversion() == 0)
1938 this->set_abiversion(2);
1939 else if (this->abiversion() < 2)
1940 gold_error(_("%s: local symbol %d has invalid st_other"
1941 " for ABI version 1"),
1942 this->name().c_str(), i
);
1949 template<int size
, bool big_endian
>
1951 Powerpc_dynobj
<size
, big_endian
>::set_abiversion(int ver
)
1953 this->e_flags_
|= ver
;
1954 if (this->abiversion() != 0)
1956 Target_powerpc
<size
, big_endian
>* target
=
1957 static_cast<Target_powerpc
<size
, big_endian
>*>(
1958 parameters
->sized_target
<size
, big_endian
>());
1959 if (target
->abiversion() == 0)
1960 target
->set_abiversion(this->abiversion());
1961 else if (target
->abiversion() != this->abiversion())
1962 gold_error(_("%s: ABI version %d is not compatible "
1963 "with ABI version %d output"),
1964 this->name().c_str(),
1965 this->abiversion(), target
->abiversion());
1970 // Call Sized_dynobj::base_read_symbols to read the symbols then
1971 // read .opd from a dynamic object, filling in opd_ent_ vector,
1973 template<int size
, bool big_endian
>
1975 Powerpc_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
1977 this->base_read_symbols(sd
);
1980 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
1981 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1982 const unsigned char* namesu
= sd
->section_names
->data();
1983 const char* names
= reinterpret_cast<const char*>(namesu
);
1984 const unsigned char* s
= NULL
;
1985 const unsigned char* opd
;
1986 section_size_type opd_size
;
1988 // Find and read .opd section.
1991 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".opd", names
,
1992 sd
->section_names_size
,
1997 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
1998 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
1999 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
2001 if (this->abiversion() == 0)
2002 this->set_abiversion(1);
2003 else if (this->abiversion() > 1)
2004 gold_error(_("%s: .opd invalid in abiv%d"),
2005 this->name().c_str(), this->abiversion());
2007 this->opd_shndx_
= (s
- pshdrs
) / shdr_size
;
2008 this->opd_address_
= shdr
.get_sh_addr();
2009 opd_size
= convert_to_section_size_type(shdr
.get_sh_size());
2010 opd
= this->get_view(shdr
.get_sh_offset(), opd_size
,
2016 // Build set of executable sections.
2017 // Using a set is probably overkill. There is likely to be only
2018 // a few executable sections, typically .init, .text and .fini,
2019 // and they are generally grouped together.
2020 typedef std::set
<Sec_info
> Exec_sections
;
2021 Exec_sections exec_sections
;
2023 for (unsigned int i
= 1; i
< this->shnum(); ++i
, s
+= shdr_size
)
2025 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2026 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2027 && ((shdr
.get_sh_flags()
2028 & (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2029 == (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2030 && shdr
.get_sh_size() != 0)
2032 exec_sections
.insert(Sec_info(shdr
.get_sh_addr(),
2033 shdr
.get_sh_size(), i
));
2036 if (exec_sections
.empty())
2039 // Look over the OPD entries. This is complicated by the fact
2040 // that some binaries will use two-word entries while others
2041 // will use the standard three-word entries. In most cases
2042 // the third word (the environment pointer for languages like
2043 // Pascal) is unused and will be zero. If the third word is
2044 // used it should not be pointing into executable sections,
2046 this->init_opd(opd_size
);
2047 for (const unsigned char* p
= opd
; p
< opd
+ opd_size
; p
+= 8)
2049 typedef typename
elfcpp::Swap
<64, big_endian
>::Valtype Valtype
;
2050 const Valtype
* valp
= reinterpret_cast<const Valtype
*>(p
);
2051 Valtype val
= elfcpp::Swap
<64, big_endian
>::readval(valp
);
2053 // Chances are that this is the third word of an OPD entry.
2055 typename
Exec_sections::const_iterator e
2056 = exec_sections
.upper_bound(Sec_info(val
, 0, 0));
2057 if (e
!= exec_sections
.begin())
2060 if (e
->start
<= val
&& val
< e
->start
+ e
->len
)
2062 // We have an address in an executable section.
2063 // VAL ought to be the function entry, set it up.
2064 this->set_opd_ent(p
- opd
, e
->shndx
, val
);
2065 // Skip second word of OPD entry, the TOC pointer.
2069 // If we didn't match any executable sections, we likely
2070 // have a non-zero third word in the OPD entry.
2075 // Set up some symbols.
2077 template<int size
, bool big_endian
>
2079 Target_powerpc
<size
, big_endian
>::do_define_standard_symbols(
2080 Symbol_table
* symtab
,
2085 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2086 // undefined when scanning relocs (and thus requires
2087 // non-relative dynamic relocs). The proper value will be
2089 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2090 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2092 Target_powerpc
<size
, big_endian
>* target
=
2093 static_cast<Target_powerpc
<size
, big_endian
>*>(
2094 parameters
->sized_target
<size
, big_endian
>());
2095 Output_data_got_powerpc
<size
, big_endian
>* got
2096 = target
->got_section(symtab
, layout
);
2097 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2098 Symbol_table::PREDEFINED
,
2102 elfcpp::STV_HIDDEN
, 0,
2106 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2107 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
2108 if (sdasym
!= NULL
&& sdasym
->is_undefined())
2110 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
2112 = layout
->add_output_section_data(".sdata", 0,
2114 | elfcpp::SHF_WRITE
,
2115 sdata
, ORDER_SMALL_DATA
, false);
2116 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
2117 Symbol_table::PREDEFINED
,
2118 os
, 32768, 0, elfcpp::STT_OBJECT
,
2119 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
2125 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2126 Symbol
*gotsym
= symtab
->lookup(".TOC.", NULL
);
2127 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2129 Target_powerpc
<size
, big_endian
>* target
=
2130 static_cast<Target_powerpc
<size
, big_endian
>*>(
2131 parameters
->sized_target
<size
, big_endian
>());
2132 Output_data_got_powerpc
<size
, big_endian
>* got
2133 = target
->got_section(symtab
, layout
);
2134 symtab
->define_in_output_data(".TOC.", NULL
,
2135 Symbol_table::PREDEFINED
,
2139 elfcpp::STV_HIDDEN
, 0,
2145 // Set up PowerPC target specific relobj.
2147 template<int size
, bool big_endian
>
2149 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
2150 const std::string
& name
,
2151 Input_file
* input_file
,
2152 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
2154 int et
= ehdr
.get_e_type();
2155 // ET_EXEC files are valid input for --just-symbols/-R,
2156 // and we treat them as relocatable objects.
2157 if (et
== elfcpp::ET_REL
2158 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
2160 Powerpc_relobj
<size
, big_endian
>* obj
=
2161 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2165 else if (et
== elfcpp::ET_DYN
)
2167 Powerpc_dynobj
<size
, big_endian
>* obj
=
2168 new Powerpc_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2174 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
2179 template<int size
, bool big_endian
>
2180 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
2183 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
2184 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
2186 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
2187 : Output_data_got
<size
, big_endian
>(),
2188 symtab_(symtab
), layout_(layout
),
2189 header_ent_cnt_(size
== 32 ? 3 : 1),
2190 header_index_(size
== 32 ? 0x2000 : 0)
2193 this->set_addralign(256);
2196 // Override all the Output_data_got methods we use so as to first call
2199 add_global(Symbol
* gsym
, unsigned int got_type
)
2201 this->reserve_ent();
2202 return Output_data_got
<size
, big_endian
>::add_global(gsym
, got_type
);
2206 add_global_plt(Symbol
* gsym
, unsigned int got_type
)
2208 this->reserve_ent();
2209 return Output_data_got
<size
, big_endian
>::add_global_plt(gsym
, got_type
);
2213 add_global_tls(Symbol
* gsym
, unsigned int got_type
)
2214 { return this->add_global_plt(gsym
, got_type
); }
2217 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
2218 Output_data_reloc_generic
* rel_dyn
, unsigned int r_type
)
2220 this->reserve_ent();
2221 Output_data_got
<size
, big_endian
>::
2222 add_global_with_rel(gsym
, got_type
, rel_dyn
, r_type
);
2226 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
2227 Output_data_reloc_generic
* rel_dyn
,
2228 unsigned int r_type_1
, unsigned int r_type_2
)
2230 this->reserve_ent(2);
2231 Output_data_got
<size
, big_endian
>::
2232 add_global_pair_with_rel(gsym
, got_type
, rel_dyn
, r_type_1
, r_type_2
);
2236 add_local(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2238 this->reserve_ent();
2239 return Output_data_got
<size
, big_endian
>::add_local(object
, sym_index
,
2244 add_local_plt(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2246 this->reserve_ent();
2247 return Output_data_got
<size
, big_endian
>::add_local_plt(object
, sym_index
,
2252 add_local_tls(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2253 { return this->add_local_plt(object
, sym_index
, got_type
); }
2256 add_local_tls_pair(Relobj
* object
, unsigned int sym_index
,
2257 unsigned int got_type
,
2258 Output_data_reloc_generic
* rel_dyn
,
2259 unsigned int r_type
)
2261 this->reserve_ent(2);
2262 Output_data_got
<size
, big_endian
>::
2263 add_local_tls_pair(object
, sym_index
, got_type
, rel_dyn
, r_type
);
2267 add_constant(Valtype constant
)
2269 this->reserve_ent();
2270 return Output_data_got
<size
, big_endian
>::add_constant(constant
);
2274 add_constant_pair(Valtype c1
, Valtype c2
)
2276 this->reserve_ent(2);
2277 return Output_data_got
<size
, big_endian
>::add_constant_pair(c1
, c2
);
2280 // Offset of _GLOBAL_OFFSET_TABLE_.
2284 return this->got_offset(this->header_index_
);
2287 // Offset of base used to access the GOT/TOC.
2288 // The got/toc pointer reg will be set to this value.
2290 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
2293 return this->g_o_t();
2295 return (this->output_section()->address()
2296 + object
->toc_base_offset()
2300 // Ensure our GOT has a header.
2302 set_final_data_size()
2304 if (this->header_ent_cnt_
!= 0)
2305 this->make_header();
2306 Output_data_got
<size
, big_endian
>::set_final_data_size();
2309 // First word of GOT header needs some values that are not
2310 // handled by Output_data_got so poke them in here.
2311 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2313 do_write(Output_file
* of
)
2316 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
2317 val
= this->layout_
->dynamic_section()->address();
2319 val
= this->output_section()->address() + 0x8000;
2320 this->replace_constant(this->header_index_
, val
);
2321 Output_data_got
<size
, big_endian
>::do_write(of
);
2326 reserve_ent(unsigned int cnt
= 1)
2328 if (this->header_ent_cnt_
== 0)
2330 if (this->num_entries() + cnt
> this->header_index_
)
2331 this->make_header();
2337 this->header_ent_cnt_
= 0;
2338 this->header_index_
= this->num_entries();
2341 Output_data_got
<size
, big_endian
>::add_constant(0);
2342 Output_data_got
<size
, big_endian
>::add_constant(0);
2343 Output_data_got
<size
, big_endian
>::add_constant(0);
2345 // Define _GLOBAL_OFFSET_TABLE_ at the header
2346 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2349 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
2350 sym
->set_value(this->g_o_t());
2353 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2354 Symbol_table::PREDEFINED
,
2355 this, this->g_o_t(), 0,
2358 elfcpp::STV_HIDDEN
, 0,
2362 Output_data_got
<size
, big_endian
>::add_constant(0);
2365 // Stashed pointers.
2366 Symbol_table
* symtab_
;
2370 unsigned int header_ent_cnt_
;
2371 // GOT header index.
2372 unsigned int header_index_
;
2375 // Get the GOT section, creating it if necessary.
2377 template<int size
, bool big_endian
>
2378 Output_data_got_powerpc
<size
, big_endian
>*
2379 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
2382 if (this->got_
== NULL
)
2384 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2387 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
2389 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2390 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2391 this->got_
, ORDER_DATA
, false);
2397 // Get the dynamic reloc section, creating it if necessary.
2399 template<int size
, bool big_endian
>
2400 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2401 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
2403 if (this->rela_dyn_
== NULL
)
2405 gold_assert(layout
!= NULL
);
2406 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
2407 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
2408 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
2409 ORDER_DYNAMIC_RELOCS
, false);
2411 return this->rela_dyn_
;
2414 // Similarly, but for ifunc symbols get the one for ifunc.
2416 template<int size
, bool big_endian
>
2417 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2418 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Symbol_table
* symtab
,
2423 return this->rela_dyn_section(layout
);
2425 if (this->iplt_
== NULL
)
2426 this->make_iplt_section(symtab
, layout
);
2427 return this->iplt_
->rel_plt();
2433 // Determine the stub group size. The group size is the absolute
2434 // value of the parameter --stub-group-size. If --stub-group-size
2435 // is passed a negative value, we restrict stubs to be always before
2436 // the stubbed branches.
2437 Stub_control(int32_t size
, bool no_size_errors
)
2438 : state_(NO_GROUP
), stub_group_size_(abs(size
)),
2439 stub14_group_size_(abs(size
) >> 10),
2440 stubs_always_before_branch_(size
< 0),
2441 suppress_size_errors_(no_size_errors
),
2442 group_end_addr_(0), owner_(NULL
), output_section_(NULL
)
2446 // Return true iff input section can be handled by current stub
2449 can_add_to_stub_group(Output_section
* o
,
2450 const Output_section::Input_section
* i
,
2453 const Output_section::Input_section
*
2459 { return output_section_
; }
2462 set_output_and_owner(Output_section
* o
,
2463 const Output_section::Input_section
* i
)
2465 this->output_section_
= o
;
2473 FINDING_STUB_SECTION
,
2478 uint32_t stub_group_size_
;
2479 uint32_t stub14_group_size_
;
2480 bool stubs_always_before_branch_
;
2481 bool suppress_size_errors_
;
2482 uint64_t group_end_addr_
;
2483 const Output_section::Input_section
* owner_
;
2484 Output_section
* output_section_
;
2487 // Return true iff input section can be handled by current stub
2491 Stub_control::can_add_to_stub_group(Output_section
* o
,
2492 const Output_section::Input_section
* i
,
2496 = has14
? this->stub14_group_size_
: this->stub_group_size_
;
2497 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
2499 uint64_t start_addr
= o
->address();
2502 // .init and .fini sections are pasted together to form a single
2503 // function. We can't be adding stubs in the middle of the function.
2504 this_size
= o
->data_size();
2507 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
2508 this_size
= i
->data_size();
2510 uint64_t end_addr
= start_addr
+ this_size
;
2511 bool toobig
= this_size
> group_size
;
2513 if (toobig
&& !this->suppress_size_errors_
)
2514 gold_warning(_("%s:%s exceeds group size"),
2515 i
->relobj()->name().c_str(),
2516 i
->relobj()->section_name(i
->shndx()).c_str());
2518 if (this->state_
!= HAS_STUB_SECTION
2519 && (!whole_sec
|| this->output_section_
!= o
)
2520 && (this->state_
== NO_GROUP
2521 || this->group_end_addr_
- end_addr
< group_size
))
2524 this->output_section_
= o
;
2527 if (this->state_
== NO_GROUP
)
2529 this->state_
= FINDING_STUB_SECTION
;
2530 this->group_end_addr_
= end_addr
;
2532 else if (this->group_end_addr_
- start_addr
< group_size
)
2534 // Adding this section would make the group larger than GROUP_SIZE.
2535 else if (this->state_
== FINDING_STUB_SECTION
2536 && !this->stubs_always_before_branch_
2539 // But wait, there's more! Input sections up to GROUP_SIZE
2540 // bytes before the stub table can be handled by it too.
2541 this->state_
= HAS_STUB_SECTION
;
2542 this->group_end_addr_
= end_addr
;
2546 this->state_
= NO_GROUP
;
2552 // Look over all the input sections, deciding where to place stubs.
2554 template<int size
, bool big_endian
>
2556 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
2558 bool no_size_errors
)
2560 Stub_control
stub_control(this->stub_group_size_
, no_size_errors
);
2562 // Group input sections and insert stub table
2563 Stub_table_owner
* table_owner
= NULL
;
2564 std::vector
<Stub_table_owner
*> tables
;
2565 Layout::Section_list section_list
;
2566 layout
->get_executable_sections(§ion_list
);
2567 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
2568 for (Layout::Section_list::reverse_iterator o
= section_list
.rbegin();
2569 o
!= section_list
.rend();
2572 typedef Output_section::Input_section_list Input_section_list
;
2573 for (Input_section_list::const_reverse_iterator i
2574 = (*o
)->input_sections().rbegin();
2575 i
!= (*o
)->input_sections().rend();
2578 if (i
->is_input_section()
2579 || i
->is_relaxed_input_section())
2581 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2582 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2583 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
2584 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
2586 table_owner
->output_section
= stub_control
.output_section();
2587 table_owner
->owner
= stub_control
.owner();
2588 stub_control
.set_output_and_owner(*o
, &*i
);
2591 if (table_owner
== NULL
)
2593 table_owner
= new Stub_table_owner
;
2594 tables
.push_back(table_owner
);
2596 ppcobj
->set_stub_table(i
->shndx(), tables
.size() - 1);
2600 if (table_owner
!= NULL
)
2602 const Output_section::Input_section
* i
= stub_control
.owner();
2604 if (tables
.size() >= 2 && tables
[tables
.size() - 2]->owner
== i
)
2606 // Corner case. A new stub group was made for the first
2607 // section (last one looked at here) for some reason, but
2608 // the first section is already being used as the owner for
2609 // a stub table for following sections. Force it into that
2613 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2614 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2615 ppcobj
->set_stub_table(i
->shndx(), tables
.size() - 1);
2619 table_owner
->output_section
= stub_control
.output_section();
2620 table_owner
->owner
= i
;
2623 for (typename
std::vector
<Stub_table_owner
*>::iterator t
= tables
.begin();
2627 Stub_table
<size
, big_endian
>* stub_table
;
2629 if ((*t
)->owner
->is_input_section())
2630 stub_table
= new Stub_table
<size
, big_endian
>(this,
2631 (*t
)->output_section
,
2633 else if ((*t
)->owner
->is_relaxed_input_section())
2634 stub_table
= static_cast<Stub_table
<size
, big_endian
>*>(
2635 (*t
)->owner
->relaxed_input_section());
2638 this->stub_tables_
.push_back(stub_table
);
2643 static unsigned long
2644 max_branch_delta (unsigned int r_type
)
2646 if (r_type
== elfcpp::R_POWERPC_REL14
2647 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
2648 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
2650 if (r_type
== elfcpp::R_POWERPC_REL24
2651 || r_type
== elfcpp::R_PPC_PLTREL24
2652 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
2657 // If this branch needs a plt call stub, or a long branch stub, make one.
2659 template<int size
, bool big_endian
>
2661 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
2662 Stub_table
<size
, big_endian
>* stub_table
,
2663 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
2664 Symbol_table
* symtab
) const
2666 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
2667 if (sym
!= NULL
&& sym
->is_forwarder())
2668 sym
= symtab
->resolve_forwards(sym
);
2669 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
2670 Target_powerpc
<size
, big_endian
>* target
=
2671 static_cast<Target_powerpc
<size
, big_endian
>*>(
2672 parameters
->sized_target
<size
, big_endian
>());
2674 ? gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
2675 : this->object_
->local_has_plt_offset(this->r_sym_
))
2679 && target
->abiversion() >= 2
2680 && !parameters
->options().output_is_position_independent()
2681 && !is_branch_reloc(this->r_type_
))
2682 target
->glink_section()->add_global_entry(gsym
);
2685 if (stub_table
== NULL
)
2686 stub_table
= this->object_
->stub_table(this->shndx_
);
2687 if (stub_table
== NULL
)
2689 // This is a ref from a data section to an ifunc symbol.
2690 stub_table
= ifunc_stub_table
;
2692 gold_assert(stub_table
!= NULL
);
2693 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
2694 if (from
!= invalid_address
)
2695 from
+= (this->object_
->output_section(this->shndx_
)->address()
2698 return stub_table
->add_plt_call_entry(from
,
2699 this->object_
, gsym
,
2700 this->r_type_
, this->addend_
);
2702 return stub_table
->add_plt_call_entry(from
,
2703 this->object_
, this->r_sym_
,
2704 this->r_type_
, this->addend_
);
2709 Address max_branch_offset
= max_branch_delta(this->r_type_
);
2710 if (max_branch_offset
== 0)
2712 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
2713 gold_assert(from
!= invalid_address
);
2714 from
+= (this->object_
->output_section(this->shndx_
)->address()
2719 switch (gsym
->source())
2721 case Symbol::FROM_OBJECT
:
2723 Object
* symobj
= gsym
->object();
2724 if (symobj
->is_dynamic()
2725 || symobj
->pluginobj() != NULL
)
2728 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
2729 if (shndx
== elfcpp::SHN_UNDEF
)
2734 case Symbol::IS_UNDEFINED
:
2740 Symbol_table::Compute_final_value_status status
;
2741 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
2742 if (status
!= Symbol_table::CFVS_OK
)
2745 to
+= this->object_
->ppc64_local_entry_offset(gsym
);
2749 const Symbol_value
<size
>* psymval
2750 = this->object_
->local_symbol(this->r_sym_
);
2751 Symbol_value
<size
> symval
;
2752 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
2753 typename
ObjType::Compute_final_local_value_status status
2754 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
2756 if (status
!= ObjType::CFLV_OK
2757 || !symval
.has_output_value())
2759 to
= symval
.value(this->object_
, 0);
2761 to
+= this->object_
->ppc64_local_entry_offset(this->r_sym_
);
2763 if (!(size
== 32 && this->r_type_
== elfcpp::R_PPC_PLTREL24
))
2764 to
+= this->addend_
;
2765 if (stub_table
== NULL
)
2766 stub_table
= this->object_
->stub_table(this->shndx_
);
2767 if (size
== 64 && target
->abiversion() < 2)
2769 unsigned int dest_shndx
;
2770 if (!target
->symval_for_branch(symtab
, gsym
, this->object_
,
2774 Address delta
= to
- from
;
2775 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
2777 if (stub_table
== NULL
)
2779 gold_warning(_("%s:%s: branch in non-executable section,"
2780 " no long branch stub for you"),
2781 this->object_
->name().c_str(),
2782 this->object_
->section_name(this->shndx_
).c_str());
2785 bool save_res
= (size
== 64
2787 && gsym
->source() == Symbol::IN_OUTPUT_DATA
2788 && gsym
->output_data() == target
->savres_section());
2789 return stub_table
->add_long_branch_entry(this->object_
,
2791 from
, to
, save_res
);
2797 // Relaxation hook. This is where we do stub generation.
2799 template<int size
, bool big_endian
>
2801 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
2802 const Input_objects
*,
2803 Symbol_table
* symtab
,
2807 unsigned int prev_brlt_size
= 0;
2811 = this->abiversion() < 2 && parameters
->options().plt_thread_safe();
2813 && this->abiversion() < 2
2815 && !parameters
->options().user_set_plt_thread_safe())
2817 static const char* const thread_starter
[] =
2821 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2823 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2824 "mq_notify", "create_timer",
2829 "GOMP_parallel_start",
2830 "GOMP_parallel_loop_static",
2831 "GOMP_parallel_loop_static_start",
2832 "GOMP_parallel_loop_dynamic",
2833 "GOMP_parallel_loop_dynamic_start",
2834 "GOMP_parallel_loop_guided",
2835 "GOMP_parallel_loop_guided_start",
2836 "GOMP_parallel_loop_runtime",
2837 "GOMP_parallel_loop_runtime_start",
2838 "GOMP_parallel_sections",
2839 "GOMP_parallel_sections_start",
2844 if (parameters
->options().shared())
2848 for (unsigned int i
= 0;
2849 i
< sizeof(thread_starter
) / sizeof(thread_starter
[0]);
2852 Symbol
* sym
= symtab
->lookup(thread_starter
[i
], NULL
);
2853 thread_safe
= (sym
!= NULL
2855 && sym
->in_real_elf());
2861 this->plt_thread_safe_
= thread_safe
;
2866 this->stub_group_size_
= parameters
->options().stub_group_size();
2867 bool no_size_errors
= true;
2868 if (this->stub_group_size_
== 1)
2869 this->stub_group_size_
= 0x1c00000;
2870 else if (this->stub_group_size_
== -1)
2871 this->stub_group_size_
= -0x1e00000;
2873 no_size_errors
= false;
2874 this->group_sections(layout
, task
, no_size_errors
);
2876 else if (this->relax_failed_
&& this->relax_fail_count_
< 3)
2878 this->branch_lookup_table_
.clear();
2879 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2880 p
!= this->stub_tables_
.end();
2883 (*p
)->clear_stubs(true);
2885 this->stub_tables_
.clear();
2886 this->stub_group_size_
= this->stub_group_size_
/ 4 * 3;
2887 gold_info(_("%s: stub group size is too large; retrying with %d"),
2888 program_name
, this->stub_group_size_
);
2889 this->group_sections(layout
, task
, true);
2892 // We need address of stub tables valid for make_stub.
2893 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2894 p
!= this->stub_tables_
.end();
2897 const Powerpc_relobj
<size
, big_endian
>* object
2898 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
2899 Address off
= object
->get_output_section_offset((*p
)->shndx());
2900 gold_assert(off
!= invalid_address
);
2901 Output_section
* os
= (*p
)->output_section();
2902 (*p
)->set_address_and_size(os
, off
);
2907 // Clear plt call stubs, long branch stubs and branch lookup table.
2908 prev_brlt_size
= this->branch_lookup_table_
.size();
2909 this->branch_lookup_table_
.clear();
2910 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2911 p
!= this->stub_tables_
.end();
2914 (*p
)->clear_stubs(false);
2918 // Build all the stubs.
2919 this->relax_failed_
= false;
2920 Stub_table
<size
, big_endian
>* ifunc_stub_table
2921 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
2922 Stub_table
<size
, big_endian
>* one_stub_table
2923 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
2924 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
2925 b
!= this->branch_info_
.end();
2928 if (!b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
)
2929 && !this->relax_failed_
)
2931 this->relax_failed_
= true;
2932 this->relax_fail_count_
++;
2933 if (this->relax_fail_count_
< 3)
2938 // Did anything change size?
2939 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
2940 bool again
= num_huge_branches
!= prev_brlt_size
;
2941 if (size
== 64 && num_huge_branches
!= 0)
2942 this->make_brlt_section(layout
);
2943 if (size
== 64 && again
)
2944 this->brlt_section_
->set_current_size(num_huge_branches
);
2946 typedef Unordered_set
<Output_section
*> Output_sections
;
2947 Output_sections os_need_update
;
2948 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2949 p
!= this->stub_tables_
.end();
2952 if ((*p
)->size_update())
2955 (*p
)->add_eh_frame(layout
);
2956 os_need_update
.insert((*p
)->output_section());
2960 // Set output section offsets for all input sections in an output
2961 // section that just changed size. Anything past the stubs will
2963 for (typename
Output_sections::iterator p
= os_need_update
.begin();
2964 p
!= os_need_update
.end();
2967 Output_section
* os
= *p
;
2969 typedef Output_section::Input_section_list Input_section_list
;
2970 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
2971 i
!= os
->input_sections().end();
2974 off
= align_address(off
, i
->addralign());
2975 if (i
->is_input_section() || i
->is_relaxed_input_section())
2976 i
->relobj()->set_section_offset(i
->shndx(), off
);
2977 if (i
->is_relaxed_input_section())
2979 Stub_table
<size
, big_endian
>* stub_table
2980 = static_cast<Stub_table
<size
, big_endian
>*>(
2981 i
->relaxed_input_section());
2982 off
+= stub_table
->set_address_and_size(os
, off
);
2985 off
+= i
->data_size();
2987 // If .branch_lt is part of this output section, then we have
2988 // just done the offset adjustment.
2989 os
->clear_section_offsets_need_adjustment();
2994 && num_huge_branches
!= 0
2995 && parameters
->options().output_is_position_independent())
2997 // Fill in the BRLT relocs.
2998 this->brlt_section_
->reset_brlt_sizes();
2999 for (typename
Branch_lookup_table::const_iterator p
3000 = this->branch_lookup_table_
.begin();
3001 p
!= this->branch_lookup_table_
.end();
3004 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
3006 this->brlt_section_
->finalize_brlt_sizes();
3011 template<int size
, bool big_endian
>
3013 Target_powerpc
<size
, big_endian
>::do_plt_fde_location(const Output_data
* plt
,
3014 unsigned char* oview
,
3018 uint64_t address
= plt
->address();
3019 off_t len
= plt
->data_size();
3021 if (plt
== this->glink_
)
3023 // See Output_data_glink::do_write() for glink contents.
3026 gold_assert(parameters
->doing_static_link());
3027 // Static linking may need stubs, to support ifunc and long
3028 // branches. We need to create an output section for
3029 // .eh_frame early in the link process, to have a place to
3030 // attach stub .eh_frame info. We also need to have
3031 // registered a CIE that matches the stub CIE. Both of
3032 // these requirements are satisfied by creating an FDE and
3033 // CIE for .glink, even though static linking will leave
3034 // .glink zero length.
3035 // ??? Hopefully generating an FDE with a zero address range
3036 // won't confuse anything that consumes .eh_frame info.
3038 else if (size
== 64)
3040 // There is one word before __glink_PLTresolve
3044 else if (parameters
->options().output_is_position_independent())
3046 // There are two FDEs for a position independent glink.
3047 // The first covers the branch table, the second
3048 // __glink_PLTresolve at the end of glink.
3049 off_t resolve_size
= this->glink_
->pltresolve_size
;
3050 if (oview
[9] == elfcpp::DW_CFA_nop
)
3051 len
-= resolve_size
;
3054 address
+= len
- resolve_size
;
3061 // Must be a stub table.
3062 const Stub_table
<size
, big_endian
>* stub_table
3063 = static_cast<const Stub_table
<size
, big_endian
>*>(plt
);
3064 uint64_t stub_address
= stub_table
->stub_address();
3065 len
-= stub_address
- address
;
3066 address
= stub_address
;
3069 *paddress
= address
;
3073 // A class to handle the PLT data.
3075 template<int size
, bool big_endian
>
3076 class Output_data_plt_powerpc
: public Output_section_data_build
3079 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3080 size
, big_endian
> Reloc_section
;
3082 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3083 Reloc_section
* plt_rel
,
3085 : Output_section_data_build(size
== 32 ? 4 : 8),
3091 // Add an entry to the PLT.
3096 add_ifunc_entry(Symbol
*);
3099 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
3101 // Return the .rela.plt section data.
3108 // Return the number of PLT entries.
3112 if (this->current_data_size() == 0)
3114 return ((this->current_data_size() - this->first_plt_entry_offset())
3115 / this->plt_entry_size());
3120 do_adjust_output_section(Output_section
* os
)
3125 // Write to a map file.
3127 do_print_to_mapfile(Mapfile
* mapfile
) const
3128 { mapfile
->print_output_data(this, this->name_
); }
3131 // Return the offset of the first non-reserved PLT entry.
3133 first_plt_entry_offset() const
3135 // IPLT has no reserved entry.
3136 if (this->name_
[3] == 'I')
3138 return this->targ_
->first_plt_entry_offset();
3141 // Return the size of each PLT entry.
3143 plt_entry_size() const
3145 return this->targ_
->plt_entry_size();
3148 // Write out the PLT data.
3150 do_write(Output_file
*);
3152 // The reloc section.
3153 Reloc_section
* rel_
;
3154 // Allows access to .glink for do_write.
3155 Target_powerpc
<size
, big_endian
>* targ_
;
3156 // What to report in map file.
3160 // Add an entry to the PLT.
3162 template<int size
, bool big_endian
>
3164 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
3166 if (!gsym
->has_plt_offset())
3168 section_size_type off
= this->current_data_size();
3170 off
+= this->first_plt_entry_offset();
3171 gsym
->set_plt_offset(off
);
3172 gsym
->set_needs_dynsym_entry();
3173 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
3174 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
3175 off
+= this->plt_entry_size();
3176 this->set_current_data_size(off
);
3180 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3182 template<int size
, bool big_endian
>
3184 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
3186 if (!gsym
->has_plt_offset())
3188 section_size_type off
= this->current_data_size();
3189 gsym
->set_plt_offset(off
);
3190 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3191 if (size
== 64 && this->targ_
->abiversion() < 2)
3192 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3193 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
3194 off
+= this->plt_entry_size();
3195 this->set_current_data_size(off
);
3199 // Add an entry for a local ifunc symbol to the IPLT.
3201 template<int size
, bool big_endian
>
3203 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
3204 Sized_relobj_file
<size
, big_endian
>* relobj
,
3205 unsigned int local_sym_index
)
3207 if (!relobj
->local_has_plt_offset(local_sym_index
))
3209 section_size_type off
= this->current_data_size();
3210 relobj
->set_local_plt_offset(local_sym_index
, off
);
3211 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3212 if (size
== 64 && this->targ_
->abiversion() < 2)
3213 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3214 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
3216 off
+= this->plt_entry_size();
3217 this->set_current_data_size(off
);
3221 static const uint32_t add_0_11_11
= 0x7c0b5a14;
3222 static const uint32_t add_2_2_11
= 0x7c425a14;
3223 static const uint32_t add_2_2_12
= 0x7c426214;
3224 static const uint32_t add_3_3_2
= 0x7c631214;
3225 static const uint32_t add_3_3_13
= 0x7c636a14;
3226 static const uint32_t add_11_0_11
= 0x7d605a14;
3227 static const uint32_t add_11_2_11
= 0x7d625a14;
3228 static const uint32_t add_11_11_2
= 0x7d6b1214;
3229 static const uint32_t addi_0_12
= 0x380c0000;
3230 static const uint32_t addi_2_2
= 0x38420000;
3231 static const uint32_t addi_3_3
= 0x38630000;
3232 static const uint32_t addi_11_11
= 0x396b0000;
3233 static const uint32_t addi_12_1
= 0x39810000;
3234 static const uint32_t addi_12_12
= 0x398c0000;
3235 static const uint32_t addis_0_2
= 0x3c020000;
3236 static const uint32_t addis_0_13
= 0x3c0d0000;
3237 static const uint32_t addis_2_12
= 0x3c4c0000;
3238 static const uint32_t addis_11_2
= 0x3d620000;
3239 static const uint32_t addis_11_11
= 0x3d6b0000;
3240 static const uint32_t addis_11_30
= 0x3d7e0000;
3241 static const uint32_t addis_12_1
= 0x3d810000;
3242 static const uint32_t addis_12_2
= 0x3d820000;
3243 static const uint32_t addis_12_12
= 0x3d8c0000;
3244 static const uint32_t b
= 0x48000000;
3245 static const uint32_t bcl_20_31
= 0x429f0005;
3246 static const uint32_t bctr
= 0x4e800420;
3247 static const uint32_t blr
= 0x4e800020;
3248 static const uint32_t bnectr_p4
= 0x4ce20420;
3249 static const uint32_t cmpld_7_12_0
= 0x7fac0040;
3250 static const uint32_t cmpldi_2_0
= 0x28220000;
3251 static const uint32_t cror_15_15_15
= 0x4def7b82;
3252 static const uint32_t cror_31_31_31
= 0x4ffffb82;
3253 static const uint32_t ld_0_1
= 0xe8010000;
3254 static const uint32_t ld_0_12
= 0xe80c0000;
3255 static const uint32_t ld_2_1
= 0xe8410000;
3256 static const uint32_t ld_2_2
= 0xe8420000;
3257 static const uint32_t ld_2_11
= 0xe84b0000;
3258 static const uint32_t ld_2_12
= 0xe84c0000;
3259 static const uint32_t ld_11_2
= 0xe9620000;
3260 static const uint32_t ld_11_11
= 0xe96b0000;
3261 static const uint32_t ld_12_2
= 0xe9820000;
3262 static const uint32_t ld_12_11
= 0xe98b0000;
3263 static const uint32_t ld_12_12
= 0xe98c0000;
3264 static const uint32_t lfd_0_1
= 0xc8010000;
3265 static const uint32_t li_0_0
= 0x38000000;
3266 static const uint32_t li_12_0
= 0x39800000;
3267 static const uint32_t lis_0
= 0x3c000000;
3268 static const uint32_t lis_2
= 0x3c400000;
3269 static const uint32_t lis_11
= 0x3d600000;
3270 static const uint32_t lis_12
= 0x3d800000;
3271 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
3272 static const uint32_t lwz_0_12
= 0x800c0000;
3273 static const uint32_t lwz_11_11
= 0x816b0000;
3274 static const uint32_t lwz_11_30
= 0x817e0000;
3275 static const uint32_t lwz_12_12
= 0x818c0000;
3276 static const uint32_t lwzu_0_12
= 0x840c0000;
3277 static const uint32_t mflr_0
= 0x7c0802a6;
3278 static const uint32_t mflr_11
= 0x7d6802a6;
3279 static const uint32_t mflr_12
= 0x7d8802a6;
3280 static const uint32_t mtctr_0
= 0x7c0903a6;
3281 static const uint32_t mtctr_11
= 0x7d6903a6;
3282 static const uint32_t mtctr_12
= 0x7d8903a6;
3283 static const uint32_t mtlr_0
= 0x7c0803a6;
3284 static const uint32_t mtlr_12
= 0x7d8803a6;
3285 static const uint32_t nop
= 0x60000000;
3286 static const uint32_t ori_0_0_0
= 0x60000000;
3287 static const uint32_t srdi_0_0_2
= 0x7800f082;
3288 static const uint32_t std_0_1
= 0xf8010000;
3289 static const uint32_t std_0_12
= 0xf80c0000;
3290 static const uint32_t std_2_1
= 0xf8410000;
3291 static const uint32_t stfd_0_1
= 0xd8010000;
3292 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
3293 static const uint32_t sub_11_11_12
= 0x7d6c5850;
3294 static const uint32_t sub_12_12_11
= 0x7d8b6050;
3295 static const uint32_t xor_2_12_12
= 0x7d826278;
3296 static const uint32_t xor_11_12_12
= 0x7d8b6278;
3298 // Write out the PLT.
3300 template<int size
, bool big_endian
>
3302 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3304 if (size
== 32 && this->name_
[3] != 'I')
3306 const section_size_type offset
= this->offset();
3307 const section_size_type oview_size
3308 = convert_to_section_size_type(this->data_size());
3309 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3310 unsigned char* pov
= oview
;
3311 unsigned char* endpov
= oview
+ oview_size
;
3313 // The address of the .glink branch table
3314 const Output_data_glink
<size
, big_endian
>* glink
3315 = this->targ_
->glink_section();
3316 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
3318 while (pov
< endpov
)
3320 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
3325 of
->write_output_view(offset
, oview_size
, oview
);
3329 // Create the PLT section.
3331 template<int size
, bool big_endian
>
3333 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
3336 if (this->plt_
== NULL
)
3338 if (this->got_
== NULL
)
3339 this->got_section(symtab
, layout
);
3341 if (this->glink_
== NULL
)
3342 make_glink_section(layout
);
3344 // Ensure that .rela.dyn always appears before .rela.plt This is
3345 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3346 // needs to include .rela.plt in its range.
3347 this->rela_dyn_section(layout
);
3349 Reloc_section
* plt_rel
= new Reloc_section(false);
3350 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
3351 elfcpp::SHF_ALLOC
, plt_rel
,
3352 ORDER_DYNAMIC_PLT_RELOCS
, false);
3354 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
3356 layout
->add_output_section_data(".plt",
3358 ? elfcpp::SHT_PROGBITS
3359 : elfcpp::SHT_NOBITS
),
3360 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3369 // Create the IPLT section.
3371 template<int size
, bool big_endian
>
3373 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
3376 if (this->iplt_
== NULL
)
3378 this->make_plt_section(symtab
, layout
);
3380 Reloc_section
* iplt_rel
= new Reloc_section(false);
3381 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
3383 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
3385 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
3389 // A section for huge long branch addresses, similar to plt section.
3391 template<int size
, bool big_endian
>
3392 class Output_data_brlt_powerpc
: public Output_section_data_build
3395 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3396 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3397 size
, big_endian
> Reloc_section
;
3399 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3400 Reloc_section
* brlt_rel
)
3401 : Output_section_data_build(size
== 32 ? 4 : 8),
3409 this->reset_data_size();
3410 this->rel_
->reset_data_size();
3414 finalize_brlt_sizes()
3416 this->finalize_data_size();
3417 this->rel_
->finalize_data_size();
3420 // Add a reloc for an entry in the BRLT.
3422 add_reloc(Address to
, unsigned int off
)
3423 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
3425 // Update section and reloc section size.
3427 set_current_size(unsigned int num_branches
)
3429 this->reset_address_and_file_offset();
3430 this->set_current_data_size(num_branches
* 16);
3431 this->finalize_data_size();
3432 Output_section
* os
= this->output_section();
3433 os
->set_section_offsets_need_adjustment();
3434 if (this->rel_
!= NULL
)
3436 unsigned int reloc_size
3437 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
3438 this->rel_
->reset_address_and_file_offset();
3439 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
3440 this->rel_
->finalize_data_size();
3441 Output_section
* os
= this->rel_
->output_section();
3442 os
->set_section_offsets_need_adjustment();
3448 do_adjust_output_section(Output_section
* os
)
3453 // Write to a map file.
3455 do_print_to_mapfile(Mapfile
* mapfile
) const
3456 { mapfile
->print_output_data(this, "** BRLT"); }
3459 // Write out the BRLT data.
3461 do_write(Output_file
*);
3463 // The reloc section.
3464 Reloc_section
* rel_
;
3465 Target_powerpc
<size
, big_endian
>* targ_
;
3468 // Make the branch lookup table section.
3470 template<int size
, bool big_endian
>
3472 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
3474 if (size
== 64 && this->brlt_section_
== NULL
)
3476 Reloc_section
* brlt_rel
= NULL
;
3477 bool is_pic
= parameters
->options().output_is_position_independent();
3480 // When PIC we can't fill in .branch_lt (like .plt it can be
3481 // a bss style section) but must initialise at runtime via
3482 // dynamic relocats.
3483 this->rela_dyn_section(layout
);
3484 brlt_rel
= new Reloc_section(false);
3485 this->rela_dyn_
->output_section()->add_output_section_data(brlt_rel
);
3488 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
3489 if (this->plt_
&& is_pic
)
3490 this->plt_
->output_section()
3491 ->add_output_section_data(this->brlt_section_
);
3493 layout
->add_output_section_data(".branch_lt",
3494 (is_pic
? elfcpp::SHT_NOBITS
3495 : elfcpp::SHT_PROGBITS
),
3496 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3497 this->brlt_section_
,
3498 (is_pic
? ORDER_SMALL_BSS
3499 : ORDER_SMALL_DATA
),
3504 // Write out .branch_lt when non-PIC.
3506 template<int size
, bool big_endian
>
3508 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3510 if (size
== 64 && !parameters
->options().output_is_position_independent())
3512 const section_size_type offset
= this->offset();
3513 const section_size_type oview_size
3514 = convert_to_section_size_type(this->data_size());
3515 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3517 this->targ_
->write_branch_lookup_table(oview
);
3518 of
->write_output_view(offset
, oview_size
, oview
);
3522 static inline uint32_t
3528 static inline uint32_t
3534 static inline uint32_t
3537 return hi(a
+ 0x8000);
3543 static const unsigned char eh_frame_cie
[12];
3547 const unsigned char Eh_cie
<size
>::eh_frame_cie
[] =
3550 'z', 'R', 0, // Augmentation string.
3551 4, // Code alignment.
3552 0x80 - size
/ 8 , // Data alignment.
3554 1, // Augmentation size.
3555 (elfcpp::DW_EH_PE_pcrel
3556 | elfcpp::DW_EH_PE_sdata4
), // FDE encoding.
3557 elfcpp::DW_CFA_def_cfa
, 1, 0 // def_cfa: r1 offset 0.
3560 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3561 static const unsigned char glink_eh_frame_fde_64v1
[] =
3563 0, 0, 0, 0, // Replaced with offset to .glink.
3564 0, 0, 0, 0, // Replaced with size of .glink.
3565 0, // Augmentation size.
3566 elfcpp::DW_CFA_advance_loc
+ 1,
3567 elfcpp::DW_CFA_register
, 65, 12,
3568 elfcpp::DW_CFA_advance_loc
+ 4,
3569 elfcpp::DW_CFA_restore_extended
, 65
3572 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
3573 static const unsigned char glink_eh_frame_fde_64v2
[] =
3575 0, 0, 0, 0, // Replaced with offset to .glink.
3576 0, 0, 0, 0, // Replaced with size of .glink.
3577 0, // Augmentation size.
3578 elfcpp::DW_CFA_advance_loc
+ 1,
3579 elfcpp::DW_CFA_register
, 65, 0,
3580 elfcpp::DW_CFA_advance_loc
+ 4,
3581 elfcpp::DW_CFA_restore_extended
, 65
3584 // Describe __glink_PLTresolve use of LR, 32-bit version.
3585 static const unsigned char glink_eh_frame_fde_32
[] =
3587 0, 0, 0, 0, // Replaced with offset to .glink.
3588 0, 0, 0, 0, // Replaced with size of .glink.
3589 0, // Augmentation size.
3590 elfcpp::DW_CFA_advance_loc
+ 2,
3591 elfcpp::DW_CFA_register
, 65, 0,
3592 elfcpp::DW_CFA_advance_loc
+ 4,
3593 elfcpp::DW_CFA_restore_extended
, 65
3596 static const unsigned char default_fde
[] =
3598 0, 0, 0, 0, // Replaced with offset to stubs.
3599 0, 0, 0, 0, // Replaced with size of stubs.
3600 0, // Augmentation size.
3601 elfcpp::DW_CFA_nop
, // Pad.
3606 template<bool big_endian
>
3608 write_insn(unsigned char* p
, uint32_t v
)
3610 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
3613 // Stub_table holds information about plt and long branch stubs.
3614 // Stubs are built in an area following some input section determined
3615 // by group_sections(). This input section is converted to a relaxed
3616 // input section allowing it to be resized to accommodate the stubs
3618 template<int size
, bool big_endian
>
3619 class Stub_table
: public Output_relaxed_input_section
3622 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3623 static const Address invalid_address
= static_cast<Address
>(0) - 1;
3625 Stub_table(Target_powerpc
<size
, big_endian
>* targ
,
3626 Output_section
* output_section
,
3627 const Output_section::Input_section
* owner
)
3628 : Output_relaxed_input_section(owner
->relobj(), owner
->shndx(),
3630 ->section_addralign(owner
->shndx())),
3631 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
3632 orig_data_size_(owner
->current_data_size()),
3633 plt_size_(0), last_plt_size_(0),
3634 branch_size_(0), last_branch_size_(0), eh_frame_added_(false),
3635 need_save_res_(false)
3637 this->set_output_section(output_section
);
3639 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
3640 new_relaxed
.push_back(this);
3641 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
3644 // Add a plt call stub.
3646 add_plt_call_entry(Address
,
3647 const Sized_relobj_file
<size
, big_endian
>*,
3653 add_plt_call_entry(Address
,
3654 const Sized_relobj_file
<size
, big_endian
>*,
3659 // Find a given plt call stub.
3661 find_plt_call_entry(const Symbol
*) const;
3664 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3665 unsigned int) const;
3668 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3674 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3679 // Add a long branch stub.
3681 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
3682 unsigned int, Address
, Address
, bool);
3685 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
3689 can_reach_stub(Address from
, unsigned int off
, unsigned int r_type
)
3691 Address max_branch_offset
= max_branch_delta(r_type
);
3692 if (max_branch_offset
== 0)
3694 gold_assert(from
!= invalid_address
);
3695 Address loc
= off
+ this->stub_address();
3696 return loc
- from
+ max_branch_offset
< 2 * max_branch_offset
;
3700 clear_stubs(bool all
)
3702 this->plt_call_stubs_
.clear();
3703 this->plt_size_
= 0;
3704 this->long_branch_stubs_
.clear();
3705 this->branch_size_
= 0;
3706 this->need_save_res_
= false;
3709 this->last_plt_size_
= 0;
3710 this->last_branch_size_
= 0;
3715 set_address_and_size(const Output_section
* os
, Address off
)
3717 Address start_off
= off
;
3718 off
+= this->orig_data_size_
;
3719 Address my_size
= this->plt_size_
+ this->branch_size_
;
3720 if (this->need_save_res_
)
3721 my_size
+= this->targ_
->savres_section()->data_size();
3723 off
= align_address(off
, this->stub_align());
3724 // Include original section size and alignment padding in size
3725 my_size
+= off
- start_off
;
3726 this->reset_address_and_file_offset();
3727 this->set_current_data_size(my_size
);
3728 this->set_address_and_file_offset(os
->address() + start_off
,
3729 os
->offset() + start_off
);
3734 stub_address() const
3736 return align_address(this->address() + this->orig_data_size_
,
3737 this->stub_align());
3743 return align_address(this->offset() + this->orig_data_size_
,
3744 this->stub_align());
3749 { return this->plt_size_
; }
3754 Output_section
* os
= this->output_section();
3755 if (os
->addralign() < this->stub_align())
3757 os
->set_addralign(this->stub_align());
3758 // FIXME: get rid of the insane checkpointing.
3759 // We can't increase alignment of the input section to which
3760 // stubs are attached; The input section may be .init which
3761 // is pasted together with other .init sections to form a
3762 // function. Aligning might insert zero padding resulting in
3763 // sigill. However we do need to increase alignment of the
3764 // output section so that the align_address() on offset in
3765 // set_address_and_size() adds the same padding as the
3766 // align_address() on address in stub_address().
3767 // What's more, we need this alignment for the layout done in
3768 // relaxation_loop_body() so that the output section starts at
3769 // a suitably aligned address.
3770 os
->checkpoint_set_addralign(this->stub_align());
3772 if (this->last_plt_size_
!= this->plt_size_
3773 || this->last_branch_size_
!= this->branch_size_
)
3775 this->last_plt_size_
= this->plt_size_
;
3776 this->last_branch_size_
= this->branch_size_
;
3782 // Add .eh_frame info for this stub section. Unlike other linker
3783 // generated .eh_frame this is added late in the link, because we
3784 // only want the .eh_frame info if this particular stub section is
3787 add_eh_frame(Layout
* layout
)
3789 if (!this->eh_frame_added_
)
3791 if (!parameters
->options().ld_generated_unwind_info())
3794 // Since we add stub .eh_frame info late, it must be placed
3795 // after all other linker generated .eh_frame info so that
3796 // merge mapping need not be updated for input sections.
3797 // There is no provision to use a different CIE to that used
3799 if (!this->targ_
->has_glink())
3802 layout
->add_eh_frame_for_plt(this,
3803 Eh_cie
<size
>::eh_frame_cie
,
3804 sizeof (Eh_cie
<size
>::eh_frame_cie
),
3806 sizeof (default_fde
));
3807 this->eh_frame_added_
= true;
3811 Target_powerpc
<size
, big_endian
>*
3817 class Plt_stub_ent_hash
;
3818 typedef Unordered_map
<Plt_stub_ent
, unsigned int,
3819 Plt_stub_ent_hash
> Plt_stub_entries
;
3821 // Alignment of stub section.
3827 unsigned int min_align
= 32;
3828 unsigned int user_align
= 1 << parameters
->options().plt_align();
3829 return std::max(user_align
, min_align
);
3832 // Return the plt offset for the given call stub.
3834 plt_off(typename
Plt_stub_entries::const_iterator p
, bool* is_iplt
) const
3836 const Symbol
* gsym
= p
->first
.sym_
;
3839 *is_iplt
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
3840 && gsym
->can_use_relative_reloc(false));
3841 return gsym
->plt_offset();
3846 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
3847 unsigned int local_sym_index
= p
->first
.locsym_
;
3848 return relobj
->local_plt_offset(local_sym_index
);
3852 // Size of a given plt call stub.
3854 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
3860 Address plt_addr
= this->plt_off(p
, &is_iplt
);
3862 plt_addr
+= this->targ_
->iplt_section()->address();
3864 plt_addr
+= this->targ_
->plt_section()->address();
3865 Address got_addr
= this->targ_
->got_section()->output_section()->address();
3866 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3867 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
3868 got_addr
+= ppcobj
->toc_base_offset();
3869 Address off
= plt_addr
- got_addr
;
3870 unsigned int bytes
= 4 * 4 + 4 * (ha(off
) != 0);
3871 if (this->targ_
->abiversion() < 2)
3873 bool static_chain
= parameters
->options().plt_static_chain();
3874 bool thread_safe
= this->targ_
->plt_thread_safe();
3878 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
3880 unsigned int align
= 1 << parameters
->options().plt_align();
3882 bytes
= (bytes
+ align
- 1) & -align
;
3886 // Return long branch stub size.
3888 branch_stub_size(Address to
)
3891 = this->stub_address() + this->last_plt_size_
+ this->branch_size_
;
3892 if (to
- loc
+ (1 << 25) < 2 << 25)
3894 if (size
== 64 || !parameters
->options().output_is_position_independent())
3901 do_write(Output_file
*);
3903 // Plt call stub keys.
3907 Plt_stub_ent(const Symbol
* sym
)
3908 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3911 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3912 unsigned int locsym_index
)
3913 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3916 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3918 unsigned int r_type
,
3920 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3923 this->addend_
= addend
;
3924 else if (parameters
->options().output_is_position_independent()
3925 && r_type
== elfcpp::R_PPC_PLTREL24
)
3927 this->addend_
= addend
;
3928 if (this->addend_
>= 32768)
3929 this->object_
= object
;
3933 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3934 unsigned int locsym_index
,
3935 unsigned int r_type
,
3937 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3940 this->addend_
= addend
;
3941 else if (parameters
->options().output_is_position_independent()
3942 && r_type
== elfcpp::R_PPC_PLTREL24
)
3943 this->addend_
= addend
;
3946 bool operator==(const Plt_stub_ent
& that
) const
3948 return (this->sym_
== that
.sym_
3949 && this->object_
== that
.object_
3950 && this->addend_
== that
.addend_
3951 && this->locsym_
== that
.locsym_
);
3955 const Sized_relobj_file
<size
, big_endian
>* object_
;
3956 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
3957 unsigned int locsym_
;
3960 class Plt_stub_ent_hash
3963 size_t operator()(const Plt_stub_ent
& ent
) const
3965 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
3966 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
3972 // Long branch stub keys.
3973 class Branch_stub_ent
3976 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
,
3977 Address to
, bool save_res
)
3978 : dest_(to
), toc_base_off_(0), save_res_(save_res
)
3981 toc_base_off_
= obj
->toc_base_offset();
3984 bool operator==(const Branch_stub_ent
& that
) const
3986 return (this->dest_
== that
.dest_
3988 || this->toc_base_off_
== that
.toc_base_off_
));
3992 unsigned int toc_base_off_
;
3996 class Branch_stub_ent_hash
3999 size_t operator()(const Branch_stub_ent
& ent
) const
4000 { return ent
.dest_
^ ent
.toc_base_off_
; }
4003 // In a sane world this would be a global.
4004 Target_powerpc
<size
, big_endian
>* targ_
;
4005 // Map sym/object/addend to stub offset.
4006 Plt_stub_entries plt_call_stubs_
;
4007 // Map destination address to stub offset.
4008 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
4009 Branch_stub_ent_hash
> Branch_stub_entries
;
4010 Branch_stub_entries long_branch_stubs_
;
4011 // size of input section
4012 section_size_type orig_data_size_
;
4014 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
4015 // Whether .eh_frame info has been created for this stub section.
4016 bool eh_frame_added_
;
4017 // Set if this stub group needs a copy of out-of-line register
4018 // save/restore functions.
4019 bool need_save_res_
;
4022 // Add a plt call stub, if we do not already have one for this
4023 // sym/object/addend combo.
4025 template<int size
, bool big_endian
>
4027 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4029 const Sized_relobj_file
<size
, big_endian
>* object
,
4031 unsigned int r_type
,
4034 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
4035 unsigned int off
= this->plt_size_
;
4036 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4037 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
4039 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
4040 return this->can_reach_stub(from
, off
, r_type
);
4043 template<int size
, bool big_endian
>
4045 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4047 const Sized_relobj_file
<size
, big_endian
>* object
,
4048 unsigned int locsym_index
,
4049 unsigned int r_type
,
4052 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
4053 unsigned int off
= this->plt_size_
;
4054 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4055 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
4057 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
4058 return this->can_reach_stub(from
, off
, r_type
);
4061 // Find a plt call stub.
4063 template<int size
, bool big_endian
>
4064 typename Stub_table
<size
, big_endian
>::Address
4065 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4066 const Sized_relobj_file
<size
, big_endian
>* object
,
4068 unsigned int r_type
,
4069 Address addend
) const
4071 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
4072 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
4073 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
4076 template<int size
, bool big_endian
>
4077 typename Stub_table
<size
, big_endian
>::Address
4078 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
4080 Plt_stub_ent
ent(gsym
);
4081 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
4082 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
4085 template<int size
, bool big_endian
>
4086 typename Stub_table
<size
, big_endian
>::Address
4087 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4088 const Sized_relobj_file
<size
, big_endian
>* object
,
4089 unsigned int locsym_index
,
4090 unsigned int r_type
,
4091 Address addend
) const
4093 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
4094 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
4095 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
4098 template<int size
, bool big_endian
>
4099 typename Stub_table
<size
, big_endian
>::Address
4100 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4101 const Sized_relobj_file
<size
, big_endian
>* object
,
4102 unsigned int locsym_index
) const
4104 Plt_stub_ent
ent(object
, locsym_index
);
4105 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
4106 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
4109 // Add a long branch stub if we don't already have one to given
4112 template<int size
, bool big_endian
>
4114 Stub_table
<size
, big_endian
>::add_long_branch_entry(
4115 const Powerpc_relobj
<size
, big_endian
>* object
,
4116 unsigned int r_type
,
4121 Branch_stub_ent
ent(object
, to
, save_res
);
4122 Address off
= this->branch_size_
;
4123 if (this->long_branch_stubs_
.insert(std::make_pair(ent
, off
)).second
)
4126 this->need_save_res_
= true;
4129 unsigned int stub_size
= this->branch_stub_size(to
);
4130 this->branch_size_
= off
+ stub_size
;
4131 if (size
== 64 && stub_size
!= 4)
4132 this->targ_
->add_branch_lookup_table(to
);
4135 return this->can_reach_stub(from
, off
, r_type
);
4138 // Find long branch stub offset.
4140 template<int size
, bool big_endian
>
4141 typename Stub_table
<size
, big_endian
>::Address
4142 Stub_table
<size
, big_endian
>::find_long_branch_entry(
4143 const Powerpc_relobj
<size
, big_endian
>* object
,
4146 Branch_stub_ent
ent(object
, to
, false);
4147 typename
Branch_stub_entries::const_iterator p
4148 = this->long_branch_stubs_
.find(ent
);
4149 if (p
== this->long_branch_stubs_
.end())
4150 return invalid_address
;
4151 if (p
->first
.save_res_
)
4152 return to
- this->targ_
->savres_section()->address() + this->branch_size_
;
4156 // A class to handle .glink.
4158 template<int size
, bool big_endian
>
4159 class Output_data_glink
: public Output_section_data
4162 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4163 static const Address invalid_address
= static_cast<Address
>(0) - 1;
4164 static const int pltresolve_size
= 16*4;
4166 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
4167 : Output_section_data(16), targ_(targ
), global_entry_stubs_(),
4168 end_branch_table_(), ge_size_(0)
4172 add_eh_frame(Layout
* layout
);
4175 add_global_entry(const Symbol
*);
4178 find_global_entry(const Symbol
*) const;
4181 global_entry_address() const
4183 gold_assert(this->is_data_size_valid());
4184 unsigned int global_entry_off
= (this->end_branch_table_
+ 15) & -16;
4185 return this->address() + global_entry_off
;
4189 // Write to a map file.
4191 do_print_to_mapfile(Mapfile
* mapfile
) const
4192 { mapfile
->print_output_data(this, _("** glink")); }
4196 set_final_data_size();
4200 do_write(Output_file
*);
4202 // Allows access to .got and .plt for do_write.
4203 Target_powerpc
<size
, big_endian
>* targ_
;
4205 // Map sym to stub offset.
4206 typedef Unordered_map
<const Symbol
*, unsigned int> Global_entry_stub_entries
;
4207 Global_entry_stub_entries global_entry_stubs_
;
4209 unsigned int end_branch_table_
, ge_size_
;
4212 template<int size
, bool big_endian
>
4214 Output_data_glink
<size
, big_endian
>::add_eh_frame(Layout
* layout
)
4216 if (!parameters
->options().ld_generated_unwind_info())
4221 if (this->targ_
->abiversion() < 2)
4222 layout
->add_eh_frame_for_plt(this,
4223 Eh_cie
<64>::eh_frame_cie
,
4224 sizeof (Eh_cie
<64>::eh_frame_cie
),
4225 glink_eh_frame_fde_64v1
,
4226 sizeof (glink_eh_frame_fde_64v1
));
4228 layout
->add_eh_frame_for_plt(this,
4229 Eh_cie
<64>::eh_frame_cie
,
4230 sizeof (Eh_cie
<64>::eh_frame_cie
),
4231 glink_eh_frame_fde_64v2
,
4232 sizeof (glink_eh_frame_fde_64v2
));
4236 // 32-bit .glink can use the default since the CIE return
4237 // address reg, LR, is valid.
4238 layout
->add_eh_frame_for_plt(this,
4239 Eh_cie
<32>::eh_frame_cie
,
4240 sizeof (Eh_cie
<32>::eh_frame_cie
),
4242 sizeof (default_fde
));
4243 // Except where LR is used in a PIC __glink_PLTresolve.
4244 if (parameters
->options().output_is_position_independent())
4245 layout
->add_eh_frame_for_plt(this,
4246 Eh_cie
<32>::eh_frame_cie
,
4247 sizeof (Eh_cie
<32>::eh_frame_cie
),
4248 glink_eh_frame_fde_32
,
4249 sizeof (glink_eh_frame_fde_32
));
4253 template<int size
, bool big_endian
>
4255 Output_data_glink
<size
, big_endian
>::add_global_entry(const Symbol
* gsym
)
4257 std::pair
<typename
Global_entry_stub_entries::iterator
, bool> p
4258 = this->global_entry_stubs_
.insert(std::make_pair(gsym
, this->ge_size_
));
4260 this->ge_size_
+= 16;
4263 template<int size
, bool big_endian
>
4264 typename Output_data_glink
<size
, big_endian
>::Address
4265 Output_data_glink
<size
, big_endian
>::find_global_entry(const Symbol
* gsym
) const
4267 typename
Global_entry_stub_entries::const_iterator p
4268 = this->global_entry_stubs_
.find(gsym
);
4269 return p
== this->global_entry_stubs_
.end() ? invalid_address
: p
->second
;
4272 template<int size
, bool big_endian
>
4274 Output_data_glink
<size
, big_endian
>::set_final_data_size()
4276 unsigned int count
= this->targ_
->plt_entry_count();
4277 section_size_type total
= 0;
4283 // space for branch table
4284 total
+= 4 * (count
- 1);
4286 total
+= -total
& 15;
4287 total
+= this->pltresolve_size
;
4291 total
+= this->pltresolve_size
;
4293 // space for branch table
4295 if (this->targ_
->abiversion() < 2)
4299 total
+= 4 * (count
- 0x8000);
4303 this->end_branch_table_
= total
;
4304 total
= (total
+ 15) & -16;
4305 total
+= this->ge_size_
;
4307 this->set_data_size(total
);
4310 // Write out plt and long branch stub code.
4312 template<int size
, bool big_endian
>
4314 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
4316 if (this->plt_call_stubs_
.empty()
4317 && this->long_branch_stubs_
.empty())
4320 const section_size_type start_off
= this->offset();
4321 const section_size_type off
= this->stub_offset();
4322 const section_size_type oview_size
=
4323 convert_to_section_size_type(this->data_size() - (off
- start_off
));
4324 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4329 const Output_data_got_powerpc
<size
, big_endian
>* got
4330 = this->targ_
->got_section();
4331 Address got_os_addr
= got
->output_section()->address();
4333 if (!this->plt_call_stubs_
.empty())
4335 // The base address of the .plt section.
4336 Address plt_base
= this->targ_
->plt_section()->address();
4337 Address iplt_base
= invalid_address
;
4339 // Write out plt call stubs.
4340 typename
Plt_stub_entries::const_iterator cs
;
4341 for (cs
= this->plt_call_stubs_
.begin();
4342 cs
!= this->plt_call_stubs_
.end();
4346 Address pltoff
= this->plt_off(cs
, &is_iplt
);
4347 Address plt_addr
= pltoff
;
4350 if (iplt_base
== invalid_address
)
4351 iplt_base
= this->targ_
->iplt_section()->address();
4352 plt_addr
+= iplt_base
;
4355 plt_addr
+= plt_base
;
4356 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4357 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
4358 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
4359 Address off
= plt_addr
- got_addr
;
4361 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
4362 gold_error(_("%s: linkage table error against `%s'"),
4363 cs
->first
.object_
->name().c_str(),
4364 cs
->first
.sym_
->demangled_name().c_str());
4366 bool plt_load_toc
= this->targ_
->abiversion() < 2;
4368 = plt_load_toc
&& parameters
->options().plt_static_chain();
4370 = plt_load_toc
&& this->targ_
->plt_thread_safe();
4371 bool use_fake_dep
= false;
4372 Address cmp_branch_off
= 0;
4375 unsigned int pltindex
4376 = ((pltoff
- this->targ_
->first_plt_entry_offset())
4377 / this->targ_
->plt_entry_size());
4379 = (this->targ_
->glink_section()->pltresolve_size
4381 if (pltindex
> 32768)
4382 glinkoff
+= (pltindex
- 32768) * 4;
4384 = this->targ_
->glink_section()->address() + glinkoff
;
4386 = (this->stub_address() + cs
->second
+ 24
4387 + 4 * (ha(off
) != 0)
4388 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4389 + 4 * static_chain
);
4390 cmp_branch_off
= to
- from
;
4391 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
4394 p
= oview
+ cs
->second
;
4397 write_insn
<big_endian
>(p
, std_2_1
+ this->targ_
->stk_toc());
4401 write_insn
<big_endian
>(p
, addis_11_2
+ ha(off
));
4403 write_insn
<big_endian
>(p
, ld_12_11
+ l(off
));
4408 write_insn
<big_endian
>(p
, addis_12_2
+ ha(off
));
4410 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
));
4414 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4416 write_insn
<big_endian
>(p
, addi_11_11
+ l(off
));
4420 write_insn
<big_endian
>(p
, mtctr_12
);
4426 write_insn
<big_endian
>(p
, xor_2_12_12
);
4428 write_insn
<big_endian
>(p
, add_11_11_2
);
4431 write_insn
<big_endian
>(p
, ld_2_11
+ l(off
+ 8));
4435 write_insn
<big_endian
>(p
, ld_11_11
+ l(off
+ 16));
4442 write_insn
<big_endian
>(p
, std_2_1
+ this->targ_
->stk_toc());
4444 write_insn
<big_endian
>(p
, ld_12_2
+ l(off
));
4447 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4449 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
));
4453 write_insn
<big_endian
>(p
, mtctr_12
);
4459 write_insn
<big_endian
>(p
, xor_11_12_12
);
4461 write_insn
<big_endian
>(p
, add_2_2_11
);
4466 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16));
4469 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8));
4473 if (thread_safe
&& !use_fake_dep
)
4475 write_insn
<big_endian
>(p
, cmpldi_2_0
);
4477 write_insn
<big_endian
>(p
, bnectr_p4
);
4479 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
4482 write_insn
<big_endian
>(p
, bctr
);
4486 // Write out long branch stubs.
4487 typename
Branch_stub_entries::const_iterator bs
;
4488 for (bs
= this->long_branch_stubs_
.begin();
4489 bs
!= this->long_branch_stubs_
.end();
4492 if (bs
->first
.save_res_
)
4494 p
= oview
+ this->plt_size_
+ bs
->second
;
4495 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
4496 Address delta
= bs
->first
.dest_
- loc
;
4497 if (delta
+ (1 << 25) < 2 << 25)
4498 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
4502 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
4503 gold_assert(brlt_addr
!= invalid_address
);
4504 brlt_addr
+= this->targ_
->brlt_section()->address();
4505 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
4506 Address brltoff
= brlt_addr
- got_addr
;
4507 if (ha(brltoff
) == 0)
4509 write_insn
<big_endian
>(p
, ld_12_2
+ l(brltoff
)), p
+= 4;
4513 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
4514 write_insn
<big_endian
>(p
, ld_12_12
+ l(brltoff
)), p
+= 4;
4516 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
4517 write_insn
<big_endian
>(p
, bctr
);
4523 if (!this->plt_call_stubs_
.empty())
4525 // The base address of the .plt section.
4526 Address plt_base
= this->targ_
->plt_section()->address();
4527 Address iplt_base
= invalid_address
;
4528 // The address of _GLOBAL_OFFSET_TABLE_.
4529 Address g_o_t
= invalid_address
;
4531 // Write out plt call stubs.
4532 typename
Plt_stub_entries::const_iterator cs
;
4533 for (cs
= this->plt_call_stubs_
.begin();
4534 cs
!= this->plt_call_stubs_
.end();
4538 Address plt_addr
= this->plt_off(cs
, &is_iplt
);
4541 if (iplt_base
== invalid_address
)
4542 iplt_base
= this->targ_
->iplt_section()->address();
4543 plt_addr
+= iplt_base
;
4546 plt_addr
+= plt_base
;
4548 p
= oview
+ cs
->second
;
4549 if (parameters
->options().output_is_position_independent())
4552 const Powerpc_relobj
<size
, big_endian
>* ppcobj
4553 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
4554 (cs
->first
.object_
));
4555 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
4557 unsigned int got2
= ppcobj
->got2_shndx();
4558 got_addr
= ppcobj
->get_output_section_offset(got2
);
4559 gold_assert(got_addr
!= invalid_address
);
4560 got_addr
+= (ppcobj
->output_section(got2
)->address()
4561 + cs
->first
.addend_
);
4565 if (g_o_t
== invalid_address
)
4567 const Output_data_got_powerpc
<size
, big_endian
>* got
4568 = this->targ_
->got_section();
4569 g_o_t
= got
->address() + got
->g_o_t();
4574 Address off
= plt_addr
- got_addr
;
4577 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(off
));
4578 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
4579 write_insn
<big_endian
>(p
+ 8, bctr
);
4583 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(off
));
4584 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(off
));
4585 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
4586 write_insn
<big_endian
>(p
+ 12, bctr
);
4591 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
4592 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
4593 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
4594 write_insn
<big_endian
>(p
+ 12, bctr
);
4599 // Write out long branch stubs.
4600 typename
Branch_stub_entries::const_iterator bs
;
4601 for (bs
= this->long_branch_stubs_
.begin();
4602 bs
!= this->long_branch_stubs_
.end();
4605 if (bs
->first
.save_res_
)
4607 p
= oview
+ this->plt_size_
+ bs
->second
;
4608 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
4609 Address delta
= bs
->first
.dest_
- loc
;
4610 if (delta
+ (1 << 25) < 2 << 25)
4611 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
4612 else if (!parameters
->options().output_is_position_independent())
4614 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(bs
->first
.dest_
));
4615 write_insn
<big_endian
>(p
+ 4, addi_12_12
+ l(bs
->first
.dest_
));
4616 write_insn
<big_endian
>(p
+ 8, mtctr_12
);
4617 write_insn
<big_endian
>(p
+ 12, bctr
);
4622 write_insn
<big_endian
>(p
+ 0, mflr_0
);
4623 write_insn
<big_endian
>(p
+ 4, bcl_20_31
);
4624 write_insn
<big_endian
>(p
+ 8, mflr_12
);
4625 write_insn
<big_endian
>(p
+ 12, addis_12_12
+ ha(delta
));
4626 write_insn
<big_endian
>(p
+ 16, addi_12_12
+ l(delta
));
4627 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
4628 write_insn
<big_endian
>(p
+ 24, mtctr_12
);
4629 write_insn
<big_endian
>(p
+ 28, bctr
);
4633 if (this->need_save_res_
)
4635 p
= oview
+ this->plt_size_
+ this->branch_size_
;
4636 memcpy (p
, this->targ_
->savres_section()->contents(),
4637 this->targ_
->savres_section()->data_size());
4641 // Write out .glink.
4643 template<int size
, bool big_endian
>
4645 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
4647 const section_size_type off
= this->offset();
4648 const section_size_type oview_size
=
4649 convert_to_section_size_type(this->data_size());
4650 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4653 // The base address of the .plt section.
4654 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4655 Address plt_base
= this->targ_
->plt_section()->address();
4659 if (this->end_branch_table_
!= 0)
4661 // Write pltresolve stub.
4663 Address after_bcl
= this->address() + 16;
4664 Address pltoff
= plt_base
- after_bcl
;
4666 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
4668 if (this->targ_
->abiversion() < 2)
4670 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
4671 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
4672 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
4673 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
4674 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
4675 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
4676 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
4677 write_insn
<big_endian
>(p
, ld_2_11
+ 8), p
+= 4;
4678 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
4679 write_insn
<big_endian
>(p
, ld_11_11
+ 16), p
+= 4;
4683 write_insn
<big_endian
>(p
, mflr_0
), p
+= 4;
4684 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
4685 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
4686 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
4687 write_insn
<big_endian
>(p
, mtlr_0
), p
+= 4;
4688 write_insn
<big_endian
>(p
, sub_12_12_11
), p
+= 4;
4689 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
4690 write_insn
<big_endian
>(p
, addi_0_12
+ l(-48)), p
+= 4;
4691 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
4692 write_insn
<big_endian
>(p
, srdi_0_0_2
), p
+= 4;
4693 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
4694 write_insn
<big_endian
>(p
, ld_11_11
+ 8), p
+= 4;
4696 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
4697 while (p
< oview
+ this->pltresolve_size
)
4698 write_insn
<big_endian
>(p
, nop
), p
+= 4;
4700 // Write lazy link call stubs.
4702 while (p
< oview
+ this->end_branch_table_
)
4704 if (this->targ_
->abiversion() < 2)
4708 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
4712 write_insn
<big_endian
>(p
, lis_0
+ hi(indx
)), p
+= 4;
4713 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
4716 uint32_t branch_off
= 8 - (p
- oview
);
4717 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
4722 Address plt_base
= this->targ_
->plt_section()->address();
4723 Address iplt_base
= invalid_address
;
4724 unsigned int global_entry_off
= (this->end_branch_table_
+ 15) & -16;
4725 Address global_entry_base
= this->address() + global_entry_off
;
4726 typename
Global_entry_stub_entries::const_iterator ge
;
4727 for (ge
= this->global_entry_stubs_
.begin();
4728 ge
!= this->global_entry_stubs_
.end();
4731 p
= oview
+ global_entry_off
+ ge
->second
;
4732 Address plt_addr
= ge
->first
->plt_offset();
4733 if (ge
->first
->type() == elfcpp::STT_GNU_IFUNC
4734 && ge
->first
->can_use_relative_reloc(false))
4736 if (iplt_base
== invalid_address
)
4737 iplt_base
= this->targ_
->iplt_section()->address();
4738 plt_addr
+= iplt_base
;
4741 plt_addr
+= plt_base
;
4742 Address my_addr
= global_entry_base
+ ge
->second
;
4743 Address off
= plt_addr
- my_addr
;
4745 if (off
+ 0x80008000 > 0xffffffff || (off
& 3) != 0)
4746 gold_error(_("%s: linkage table error against `%s'"),
4747 ge
->first
->object()->name().c_str(),
4748 ge
->first
->demangled_name().c_str());
4750 write_insn
<big_endian
>(p
, addis_12_12
+ ha(off
)), p
+= 4;
4751 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
)), p
+= 4;
4752 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
4753 write_insn
<big_endian
>(p
, bctr
);
4758 const Output_data_got_powerpc
<size
, big_endian
>* got
4759 = this->targ_
->got_section();
4760 // The address of _GLOBAL_OFFSET_TABLE_.
4761 Address g_o_t
= got
->address() + got
->g_o_t();
4763 // Write out pltresolve branch table.
4765 unsigned int the_end
= oview_size
- this->pltresolve_size
;
4766 unsigned char* end_p
= oview
+ the_end
;
4767 while (p
< end_p
- 8 * 4)
4768 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
4770 write_insn
<big_endian
>(p
, nop
), p
+= 4;
4772 // Write out pltresolve call stub.
4773 if (parameters
->options().output_is_position_independent())
4775 Address res0_off
= 0;
4776 Address after_bcl_off
= the_end
+ 12;
4777 Address bcl_res0
= after_bcl_off
- res0_off
;
4779 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
4780 write_insn
<big_endian
>(p
+ 4, mflr_0
);
4781 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
4782 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
4783 write_insn
<big_endian
>(p
+ 16, mflr_12
);
4784 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
4785 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
4787 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
4789 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
4790 if (ha(got_bcl
) == ha(got_bcl
+ 4))
4792 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
4793 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
4797 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
4798 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
4800 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
4801 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
4802 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
4803 write_insn
<big_endian
>(p
+ 52, bctr
);
4804 write_insn
<big_endian
>(p
+ 56, nop
);
4805 write_insn
<big_endian
>(p
+ 60, nop
);
4809 Address res0
= this->address();
4811 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
4812 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
4813 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
4814 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
4816 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
4817 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
4818 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
4819 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
4820 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
4821 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
4823 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
4824 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
4825 write_insn
<big_endian
>(p
+ 32, bctr
);
4826 write_insn
<big_endian
>(p
+ 36, nop
);
4827 write_insn
<big_endian
>(p
+ 40, nop
);
4828 write_insn
<big_endian
>(p
+ 44, nop
);
4829 write_insn
<big_endian
>(p
+ 48, nop
);
4830 write_insn
<big_endian
>(p
+ 52, nop
);
4831 write_insn
<big_endian
>(p
+ 56, nop
);
4832 write_insn
<big_endian
>(p
+ 60, nop
);
4837 of
->write_output_view(off
, oview_size
, oview
);
4841 // A class to handle linker generated save/restore functions.
4843 template<int size
, bool big_endian
>
4844 class Output_data_save_res
: public Output_section_data_build
4847 Output_data_save_res(Symbol_table
* symtab
);
4849 const unsigned char*
4856 // Write to a map file.
4858 do_print_to_mapfile(Mapfile
* mapfile
) const
4859 { mapfile
->print_output_data(this, _("** save/restore")); }
4862 do_write(Output_file
*);
4865 // The maximum size of save/restore contents.
4866 static const unsigned int savres_max
= 218*4;
4869 savres_define(Symbol_table
* symtab
,
4871 unsigned int lo
, unsigned int hi
,
4872 unsigned char* write_ent(unsigned char*, int),
4873 unsigned char* write_tail(unsigned char*, int));
4875 unsigned char *contents_
;
4878 template<bool big_endian
>
4879 static unsigned char*
4880 savegpr0(unsigned char* p
, int r
)
4882 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4883 write_insn
<big_endian
>(p
, insn
);
4887 template<bool big_endian
>
4888 static unsigned char*
4889 savegpr0_tail(unsigned char* p
, int r
)
4891 p
= savegpr0
<big_endian
>(p
, r
);
4892 uint32_t insn
= std_0_1
+ 16;
4893 write_insn
<big_endian
>(p
, insn
);
4895 write_insn
<big_endian
>(p
, blr
);
4899 template<bool big_endian
>
4900 static unsigned char*
4901 restgpr0(unsigned char* p
, int r
)
4903 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4904 write_insn
<big_endian
>(p
, insn
);
4908 template<bool big_endian
>
4909 static unsigned char*
4910 restgpr0_tail(unsigned char* p
, int r
)
4912 uint32_t insn
= ld_0_1
+ 16;
4913 write_insn
<big_endian
>(p
, insn
);
4915 p
= restgpr0
<big_endian
>(p
, r
);
4916 write_insn
<big_endian
>(p
, mtlr_0
);
4920 p
= restgpr0
<big_endian
>(p
, 30);
4921 p
= restgpr0
<big_endian
>(p
, 31);
4923 write_insn
<big_endian
>(p
, blr
);
4927 template<bool big_endian
>
4928 static unsigned char*
4929 savegpr1(unsigned char* p
, int r
)
4931 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4932 write_insn
<big_endian
>(p
, insn
);
4936 template<bool big_endian
>
4937 static unsigned char*
4938 savegpr1_tail(unsigned char* p
, int r
)
4940 p
= savegpr1
<big_endian
>(p
, r
);
4941 write_insn
<big_endian
>(p
, blr
);
4945 template<bool big_endian
>
4946 static unsigned char*
4947 restgpr1(unsigned char* p
, int r
)
4949 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4950 write_insn
<big_endian
>(p
, insn
);
4954 template<bool big_endian
>
4955 static unsigned char*
4956 restgpr1_tail(unsigned char* p
, int r
)
4958 p
= restgpr1
<big_endian
>(p
, r
);
4959 write_insn
<big_endian
>(p
, blr
);
4963 template<bool big_endian
>
4964 static unsigned char*
4965 savefpr(unsigned char* p
, int r
)
4967 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4968 write_insn
<big_endian
>(p
, insn
);
4972 template<bool big_endian
>
4973 static unsigned char*
4974 savefpr0_tail(unsigned char* p
, int r
)
4976 p
= savefpr
<big_endian
>(p
, r
);
4977 write_insn
<big_endian
>(p
, std_0_1
+ 16);
4979 write_insn
<big_endian
>(p
, blr
);
4983 template<bool big_endian
>
4984 static unsigned char*
4985 restfpr(unsigned char* p
, int r
)
4987 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4988 write_insn
<big_endian
>(p
, insn
);
4992 template<bool big_endian
>
4993 static unsigned char*
4994 restfpr0_tail(unsigned char* p
, int r
)
4996 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
4998 p
= restfpr
<big_endian
>(p
, r
);
4999 write_insn
<big_endian
>(p
, mtlr_0
);
5003 p
= restfpr
<big_endian
>(p
, 30);
5004 p
= restfpr
<big_endian
>(p
, 31);
5006 write_insn
<big_endian
>(p
, blr
);
5010 template<bool big_endian
>
5011 static unsigned char*
5012 savefpr1_tail(unsigned char* p
, int r
)
5014 p
= savefpr
<big_endian
>(p
, r
);
5015 write_insn
<big_endian
>(p
, blr
);
5019 template<bool big_endian
>
5020 static unsigned char*
5021 restfpr1_tail(unsigned char* p
, int r
)
5023 p
= restfpr
<big_endian
>(p
, r
);
5024 write_insn
<big_endian
>(p
, blr
);
5028 template<bool big_endian
>
5029 static unsigned char*
5030 savevr(unsigned char* p
, int r
)
5032 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5033 write_insn
<big_endian
>(p
, insn
);
5035 insn
= stvx_0_12_0
+ (r
<< 21);
5036 write_insn
<big_endian
>(p
, insn
);
5040 template<bool big_endian
>
5041 static unsigned char*
5042 savevr_tail(unsigned char* p
, int r
)
5044 p
= savevr
<big_endian
>(p
, r
);
5045 write_insn
<big_endian
>(p
, blr
);
5049 template<bool big_endian
>
5050 static unsigned char*
5051 restvr(unsigned char* p
, int r
)
5053 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5054 write_insn
<big_endian
>(p
, insn
);
5056 insn
= lvx_0_12_0
+ (r
<< 21);
5057 write_insn
<big_endian
>(p
, insn
);
5061 template<bool big_endian
>
5062 static unsigned char*
5063 restvr_tail(unsigned char* p
, int r
)
5065 p
= restvr
<big_endian
>(p
, r
);
5066 write_insn
<big_endian
>(p
, blr
);
5071 template<int size
, bool big_endian
>
5072 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
5073 Symbol_table
* symtab
)
5074 : Output_section_data_build(4),
5077 this->savres_define(symtab
,
5078 "_savegpr0_", 14, 31,
5079 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
5080 this->savres_define(symtab
,
5081 "_restgpr0_", 14, 29,
5082 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
5083 this->savres_define(symtab
,
5084 "_restgpr0_", 30, 31,
5085 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
5086 this->savres_define(symtab
,
5087 "_savegpr1_", 14, 31,
5088 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
5089 this->savres_define(symtab
,
5090 "_restgpr1_", 14, 31,
5091 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
5092 this->savres_define(symtab
,
5093 "_savefpr_", 14, 31,
5094 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
5095 this->savres_define(symtab
,
5096 "_restfpr_", 14, 29,
5097 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
5098 this->savres_define(symtab
,
5099 "_restfpr_", 30, 31,
5100 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
5101 this->savres_define(symtab
,
5103 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
5104 this->savres_define(symtab
,
5106 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
5107 this->savres_define(symtab
,
5109 savevr
<big_endian
>, savevr_tail
<big_endian
>);
5110 this->savres_define(symtab
,
5112 restvr
<big_endian
>, restvr_tail
<big_endian
>);
5115 template<int size
, bool big_endian
>
5117 Output_data_save_res
<size
, big_endian
>::savres_define(
5118 Symbol_table
* symtab
,
5120 unsigned int lo
, unsigned int hi
,
5121 unsigned char* write_ent(unsigned char*, int),
5122 unsigned char* write_tail(unsigned char*, int))
5124 size_t len
= strlen(name
);
5125 bool writing
= false;
5128 memcpy(sym
, name
, len
);
5131 for (unsigned int i
= lo
; i
<= hi
; i
++)
5133 sym
[len
+ 0] = i
/ 10 + '0';
5134 sym
[len
+ 1] = i
% 10 + '0';
5135 Symbol
* gsym
= symtab
->lookup(sym
);
5136 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
5137 writing
= writing
|| refd
;
5140 if (this->contents_
== NULL
)
5141 this->contents_
= new unsigned char[this->savres_max
];
5143 section_size_type value
= this->current_data_size();
5144 unsigned char* p
= this->contents_
+ value
;
5146 p
= write_ent(p
, i
);
5148 p
= write_tail(p
, i
);
5149 section_size_type cur_size
= p
- this->contents_
;
5150 this->set_current_data_size(cur_size
);
5152 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
5153 this, value
, cur_size
- value
,
5154 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
5155 elfcpp::STV_HIDDEN
, 0, false, false);
5160 // Write out save/restore.
5162 template<int size
, bool big_endian
>
5164 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
5166 const section_size_type off
= this->offset();
5167 const section_size_type oview_size
=
5168 convert_to_section_size_type(this->data_size());
5169 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5170 memcpy(oview
, this->contents_
, oview_size
);
5171 of
->write_output_view(off
, oview_size
, oview
);
5175 // Create the glink section.
5177 template<int size
, bool big_endian
>
5179 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
5181 if (this->glink_
== NULL
)
5183 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
5184 this->glink_
->add_eh_frame(layout
);
5185 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
5186 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
5187 this->glink_
, ORDER_TEXT
, false);
5191 // Create a PLT entry for a global symbol.
5193 template<int size
, bool big_endian
>
5195 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
5199 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
5200 && gsym
->can_use_relative_reloc(false))
5202 if (this->iplt_
== NULL
)
5203 this->make_iplt_section(symtab
, layout
);
5204 this->iplt_
->add_ifunc_entry(gsym
);
5208 if (this->plt_
== NULL
)
5209 this->make_plt_section(symtab
, layout
);
5210 this->plt_
->add_entry(gsym
);
5214 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5216 template<int size
, bool big_endian
>
5218 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
5219 Symbol_table
* symtab
,
5221 Sized_relobj_file
<size
, big_endian
>* relobj
,
5224 if (this->iplt_
== NULL
)
5225 this->make_iplt_section(symtab
, layout
);
5226 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
5229 // Return the number of entries in the PLT.
5231 template<int size
, bool big_endian
>
5233 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
5235 if (this->plt_
== NULL
)
5237 return this->plt_
->entry_count();
5240 // Create a GOT entry for local dynamic __tls_get_addr calls.
5242 template<int size
, bool big_endian
>
5244 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
5245 Symbol_table
* symtab
,
5247 Sized_relobj_file
<size
, big_endian
>* object
)
5249 if (this->tlsld_got_offset_
== -1U)
5251 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
5252 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
5253 Output_data_got_powerpc
<size
, big_endian
>* got
5254 = this->got_section(symtab
, layout
);
5255 unsigned int got_offset
= got
->add_constant_pair(0, 0);
5256 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
5258 this->tlsld_got_offset_
= got_offset
;
5260 return this->tlsld_got_offset_
;
5263 // Get the Reference_flags for a particular relocation.
5265 template<int size
, bool big_endian
>
5267 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(
5268 unsigned int r_type
,
5269 const Target_powerpc
* target
)
5275 case elfcpp::R_POWERPC_NONE
:
5276 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5277 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5278 case elfcpp::R_PPC64_TOC
:
5279 // No symbol reference.
5282 case elfcpp::R_PPC64_ADDR64
:
5283 case elfcpp::R_PPC64_UADDR64
:
5284 case elfcpp::R_POWERPC_ADDR32
:
5285 case elfcpp::R_POWERPC_UADDR32
:
5286 case elfcpp::R_POWERPC_ADDR16
:
5287 case elfcpp::R_POWERPC_UADDR16
:
5288 case elfcpp::R_POWERPC_ADDR16_LO
:
5289 case elfcpp::R_POWERPC_ADDR16_HI
:
5290 case elfcpp::R_POWERPC_ADDR16_HA
:
5291 ref
= Symbol::ABSOLUTE_REF
;
5294 case elfcpp::R_POWERPC_ADDR24
:
5295 case elfcpp::R_POWERPC_ADDR14
:
5296 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5297 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5298 ref
= Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
5301 case elfcpp::R_PPC64_REL64
:
5302 case elfcpp::R_POWERPC_REL32
:
5303 case elfcpp::R_PPC_LOCAL24PC
:
5304 case elfcpp::R_POWERPC_REL16
:
5305 case elfcpp::R_POWERPC_REL16_LO
:
5306 case elfcpp::R_POWERPC_REL16_HI
:
5307 case elfcpp::R_POWERPC_REL16_HA
:
5308 ref
= Symbol::RELATIVE_REF
;
5311 case elfcpp::R_POWERPC_REL24
:
5312 case elfcpp::R_PPC_PLTREL24
:
5313 case elfcpp::R_POWERPC_REL14
:
5314 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5315 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5316 ref
= Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
5319 case elfcpp::R_POWERPC_GOT16
:
5320 case elfcpp::R_POWERPC_GOT16_LO
:
5321 case elfcpp::R_POWERPC_GOT16_HI
:
5322 case elfcpp::R_POWERPC_GOT16_HA
:
5323 case elfcpp::R_PPC64_GOT16_DS
:
5324 case elfcpp::R_PPC64_GOT16_LO_DS
:
5325 case elfcpp::R_PPC64_TOC16
:
5326 case elfcpp::R_PPC64_TOC16_LO
:
5327 case elfcpp::R_PPC64_TOC16_HI
:
5328 case elfcpp::R_PPC64_TOC16_HA
:
5329 case elfcpp::R_PPC64_TOC16_DS
:
5330 case elfcpp::R_PPC64_TOC16_LO_DS
:
5331 ref
= Symbol::RELATIVE_REF
;
5334 case elfcpp::R_POWERPC_GOT_TPREL16
:
5335 case elfcpp::R_POWERPC_TLS
:
5336 ref
= Symbol::TLS_REF
;
5339 case elfcpp::R_POWERPC_COPY
:
5340 case elfcpp::R_POWERPC_GLOB_DAT
:
5341 case elfcpp::R_POWERPC_JMP_SLOT
:
5342 case elfcpp::R_POWERPC_RELATIVE
:
5343 case elfcpp::R_POWERPC_DTPMOD
:
5345 // Not expected. We will give an error later.
5349 if (size
== 64 && target
->abiversion() < 2)
5350 ref
|= Symbol::FUNC_DESC_ABI
;
5354 // Report an unsupported relocation against a local symbol.
5356 template<int size
, bool big_endian
>
5358 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
5359 Sized_relobj_file
<size
, big_endian
>* object
,
5360 unsigned int r_type
)
5362 gold_error(_("%s: unsupported reloc %u against local symbol"),
5363 object
->name().c_str(), r_type
);
5366 // We are about to emit a dynamic relocation of type R_TYPE. If the
5367 // dynamic linker does not support it, issue an error.
5369 template<int size
, bool big_endian
>
5371 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
5372 unsigned int r_type
)
5374 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
5376 // These are the relocation types supported by glibc for both 32-bit
5377 // and 64-bit powerpc.
5380 case elfcpp::R_POWERPC_NONE
:
5381 case elfcpp::R_POWERPC_RELATIVE
:
5382 case elfcpp::R_POWERPC_GLOB_DAT
:
5383 case elfcpp::R_POWERPC_DTPMOD
:
5384 case elfcpp::R_POWERPC_DTPREL
:
5385 case elfcpp::R_POWERPC_TPREL
:
5386 case elfcpp::R_POWERPC_JMP_SLOT
:
5387 case elfcpp::R_POWERPC_COPY
:
5388 case elfcpp::R_POWERPC_IRELATIVE
:
5389 case elfcpp::R_POWERPC_ADDR32
:
5390 case elfcpp::R_POWERPC_UADDR32
:
5391 case elfcpp::R_POWERPC_ADDR24
:
5392 case elfcpp::R_POWERPC_ADDR16
:
5393 case elfcpp::R_POWERPC_UADDR16
:
5394 case elfcpp::R_POWERPC_ADDR16_LO
:
5395 case elfcpp::R_POWERPC_ADDR16_HI
:
5396 case elfcpp::R_POWERPC_ADDR16_HA
:
5397 case elfcpp::R_POWERPC_ADDR14
:
5398 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5399 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5400 case elfcpp::R_POWERPC_REL32
:
5401 case elfcpp::R_POWERPC_REL24
:
5402 case elfcpp::R_POWERPC_TPREL16
:
5403 case elfcpp::R_POWERPC_TPREL16_LO
:
5404 case elfcpp::R_POWERPC_TPREL16_HI
:
5405 case elfcpp::R_POWERPC_TPREL16_HA
:
5416 // These are the relocation types supported only on 64-bit.
5417 case elfcpp::R_PPC64_ADDR64
:
5418 case elfcpp::R_PPC64_UADDR64
:
5419 case elfcpp::R_PPC64_JMP_IREL
:
5420 case elfcpp::R_PPC64_ADDR16_DS
:
5421 case elfcpp::R_PPC64_ADDR16_LO_DS
:
5422 case elfcpp::R_PPC64_ADDR16_HIGH
:
5423 case elfcpp::R_PPC64_ADDR16_HIGHA
:
5424 case elfcpp::R_PPC64_ADDR16_HIGHER
:
5425 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
5426 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
5427 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
5428 case elfcpp::R_PPC64_REL64
:
5429 case elfcpp::R_POWERPC_ADDR30
:
5430 case elfcpp::R_PPC64_TPREL16_DS
:
5431 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5432 case elfcpp::R_PPC64_TPREL16_HIGH
:
5433 case elfcpp::R_PPC64_TPREL16_HIGHA
:
5434 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5435 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5436 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5437 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5448 // These are the relocation types supported only on 32-bit.
5449 // ??? glibc ld.so doesn't need to support these.
5450 case elfcpp::R_POWERPC_DTPREL16
:
5451 case elfcpp::R_POWERPC_DTPREL16_LO
:
5452 case elfcpp::R_POWERPC_DTPREL16_HI
:
5453 case elfcpp::R_POWERPC_DTPREL16_HA
:
5461 // This prevents us from issuing more than one error per reloc
5462 // section. But we can still wind up issuing more than one
5463 // error per object file.
5464 if (this->issued_non_pic_error_
)
5466 gold_assert(parameters
->options().output_is_position_independent());
5467 object
->error(_("requires unsupported dynamic reloc; "
5468 "recompile with -fPIC"));
5469 this->issued_non_pic_error_
= true;
5473 // Return whether we need to make a PLT entry for a relocation of the
5474 // given type against a STT_GNU_IFUNC symbol.
5476 template<int size
, bool big_endian
>
5478 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
5479 Target_powerpc
<size
, big_endian
>* target
,
5480 Sized_relobj_file
<size
, big_endian
>* object
,
5481 unsigned int r_type
,
5484 // In non-pic code any reference will resolve to the plt call stub
5485 // for the ifunc symbol.
5486 if ((size
== 32 || target
->abiversion() >= 2)
5487 && !parameters
->options().output_is_position_independent())
5492 // Word size refs from data sections are OK, but don't need a PLT entry.
5493 case elfcpp::R_POWERPC_ADDR32
:
5494 case elfcpp::R_POWERPC_UADDR32
:
5499 case elfcpp::R_PPC64_ADDR64
:
5500 case elfcpp::R_PPC64_UADDR64
:
5505 // GOT refs are good, but also don't need a PLT entry.
5506 case elfcpp::R_POWERPC_GOT16
:
5507 case elfcpp::R_POWERPC_GOT16_LO
:
5508 case elfcpp::R_POWERPC_GOT16_HI
:
5509 case elfcpp::R_POWERPC_GOT16_HA
:
5510 case elfcpp::R_PPC64_GOT16_DS
:
5511 case elfcpp::R_PPC64_GOT16_LO_DS
:
5514 // Function calls are good, and these do need a PLT entry.
5515 case elfcpp::R_POWERPC_ADDR24
:
5516 case elfcpp::R_POWERPC_ADDR14
:
5517 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5518 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5519 case elfcpp::R_POWERPC_REL24
:
5520 case elfcpp::R_PPC_PLTREL24
:
5521 case elfcpp::R_POWERPC_REL14
:
5522 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5523 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5530 // Anything else is a problem.
5531 // If we are building a static executable, the libc startup function
5532 // responsible for applying indirect function relocations is going
5533 // to complain about the reloc type.
5534 // If we are building a dynamic executable, we will have a text
5535 // relocation. The dynamic loader will set the text segment
5536 // writable and non-executable to apply text relocations. So we'll
5537 // segfault when trying to run the indirection function to resolve
5540 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
5541 object
->name().c_str(), r_type
);
5545 // Scan a relocation for a local symbol.
5547 template<int size
, bool big_endian
>
5549 Target_powerpc
<size
, big_endian
>::Scan::local(
5550 Symbol_table
* symtab
,
5552 Target_powerpc
<size
, big_endian
>* target
,
5553 Sized_relobj_file
<size
, big_endian
>* object
,
5554 unsigned int data_shndx
,
5555 Output_section
* output_section
,
5556 const elfcpp::Rela
<size
, big_endian
>& reloc
,
5557 unsigned int r_type
,
5558 const elfcpp::Sym
<size
, big_endian
>& lsym
,
5561 this->maybe_skip_tls_get_addr_call(r_type
, NULL
);
5563 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
5564 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
5566 this->expect_tls_get_addr_call();
5567 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
5568 if (tls_type
!= tls::TLSOPT_NONE
)
5569 this->skip_next_tls_get_addr_call();
5571 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
5572 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
5574 this->expect_tls_get_addr_call();
5575 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5576 if (tls_type
!= tls::TLSOPT_NONE
)
5577 this->skip_next_tls_get_addr_call();
5580 Powerpc_relobj
<size
, big_endian
>* ppc_object
5581 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5586 && data_shndx
== ppc_object
->opd_shndx()
5587 && r_type
== elfcpp::R_PPC64_ADDR64
)
5588 ppc_object
->set_opd_discard(reloc
.get_r_offset());
5592 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5593 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
5594 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
5596 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5597 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5598 r_type
, r_sym
, reloc
.get_r_addend());
5599 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
5604 case elfcpp::R_POWERPC_NONE
:
5605 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5606 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5607 case elfcpp::R_PPC64_TOCSAVE
:
5608 case elfcpp::R_POWERPC_TLS
:
5609 case elfcpp::R_PPC64_ENTRY
:
5612 case elfcpp::R_PPC64_TOC
:
5614 Output_data_got_powerpc
<size
, big_endian
>* got
5615 = target
->got_section(symtab
, layout
);
5616 if (parameters
->options().output_is_position_independent())
5618 Address off
= reloc
.get_r_offset();
5620 && target
->abiversion() < 2
5621 && data_shndx
== ppc_object
->opd_shndx()
5622 && ppc_object
->get_opd_discard(off
- 8))
5625 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5626 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
5627 rela_dyn
->add_output_section_relative(got
->output_section(),
5628 elfcpp::R_POWERPC_RELATIVE
,
5630 object
, data_shndx
, off
,
5631 symobj
->toc_base_offset());
5636 case elfcpp::R_PPC64_ADDR64
:
5637 case elfcpp::R_PPC64_UADDR64
:
5638 case elfcpp::R_POWERPC_ADDR32
:
5639 case elfcpp::R_POWERPC_UADDR32
:
5640 case elfcpp::R_POWERPC_ADDR24
:
5641 case elfcpp::R_POWERPC_ADDR16
:
5642 case elfcpp::R_POWERPC_ADDR16_LO
:
5643 case elfcpp::R_POWERPC_ADDR16_HI
:
5644 case elfcpp::R_POWERPC_ADDR16_HA
:
5645 case elfcpp::R_POWERPC_UADDR16
:
5646 case elfcpp::R_PPC64_ADDR16_HIGH
:
5647 case elfcpp::R_PPC64_ADDR16_HIGHA
:
5648 case elfcpp::R_PPC64_ADDR16_HIGHER
:
5649 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
5650 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
5651 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
5652 case elfcpp::R_PPC64_ADDR16_DS
:
5653 case elfcpp::R_PPC64_ADDR16_LO_DS
:
5654 case elfcpp::R_POWERPC_ADDR14
:
5655 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5656 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5657 // If building a shared library (or a position-independent
5658 // executable), we need to create a dynamic relocation for
5660 if (parameters
->options().output_is_position_independent()
5661 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
5663 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
5665 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5666 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
5667 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
5669 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
5670 : elfcpp::R_POWERPC_RELATIVE
);
5671 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
5672 output_section
, data_shndx
,
5673 reloc
.get_r_offset(),
5674 reloc
.get_r_addend(), false);
5676 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
5678 check_non_pic(object
, r_type
);
5679 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
5680 data_shndx
, reloc
.get_r_offset(),
5681 reloc
.get_r_addend());
5685 gold_assert(lsym
.get_st_value() == 0);
5686 unsigned int shndx
= lsym
.get_st_shndx();
5688 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
5691 object
->error(_("section symbol %u has bad shndx %u"),
5694 rela_dyn
->add_local_section(object
, shndx
, r_type
,
5695 output_section
, data_shndx
,
5696 reloc
.get_r_offset());
5701 case elfcpp::R_POWERPC_REL24
:
5702 case elfcpp::R_PPC_PLTREL24
:
5703 case elfcpp::R_PPC_LOCAL24PC
:
5704 case elfcpp::R_POWERPC_REL14
:
5705 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5706 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5708 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5709 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5710 reloc
.get_r_addend());
5713 case elfcpp::R_PPC64_REL64
:
5714 case elfcpp::R_POWERPC_REL32
:
5715 case elfcpp::R_POWERPC_REL16
:
5716 case elfcpp::R_POWERPC_REL16_LO
:
5717 case elfcpp::R_POWERPC_REL16_HI
:
5718 case elfcpp::R_POWERPC_REL16_HA
:
5719 case elfcpp::R_POWERPC_REL16DX_HA
:
5720 case elfcpp::R_POWERPC_SECTOFF
:
5721 case elfcpp::R_POWERPC_SECTOFF_LO
:
5722 case elfcpp::R_POWERPC_SECTOFF_HI
:
5723 case elfcpp::R_POWERPC_SECTOFF_HA
:
5724 case elfcpp::R_PPC64_SECTOFF_DS
:
5725 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
5726 case elfcpp::R_POWERPC_TPREL16
:
5727 case elfcpp::R_POWERPC_TPREL16_LO
:
5728 case elfcpp::R_POWERPC_TPREL16_HI
:
5729 case elfcpp::R_POWERPC_TPREL16_HA
:
5730 case elfcpp::R_PPC64_TPREL16_DS
:
5731 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5732 case elfcpp::R_PPC64_TPREL16_HIGH
:
5733 case elfcpp::R_PPC64_TPREL16_HIGHA
:
5734 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5735 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5736 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5737 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5738 case elfcpp::R_POWERPC_DTPREL16
:
5739 case elfcpp::R_POWERPC_DTPREL16_LO
:
5740 case elfcpp::R_POWERPC_DTPREL16_HI
:
5741 case elfcpp::R_POWERPC_DTPREL16_HA
:
5742 case elfcpp::R_PPC64_DTPREL16_DS
:
5743 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
5744 case elfcpp::R_PPC64_DTPREL16_HIGH
:
5745 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
5746 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
5747 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
5748 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
5749 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
5750 case elfcpp::R_PPC64_TLSGD
:
5751 case elfcpp::R_PPC64_TLSLD
:
5752 case elfcpp::R_PPC64_ADDR64_LOCAL
:
5755 case elfcpp::R_POWERPC_GOT16
:
5756 case elfcpp::R_POWERPC_GOT16_LO
:
5757 case elfcpp::R_POWERPC_GOT16_HI
:
5758 case elfcpp::R_POWERPC_GOT16_HA
:
5759 case elfcpp::R_PPC64_GOT16_DS
:
5760 case elfcpp::R_PPC64_GOT16_LO_DS
:
5762 // The symbol requires a GOT entry.
5763 Output_data_got_powerpc
<size
, big_endian
>* got
5764 = target
->got_section(symtab
, layout
);
5765 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5767 if (!parameters
->options().output_is_position_independent())
5770 && (size
== 32 || target
->abiversion() >= 2))
5771 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
5773 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
5775 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
5777 // If we are generating a shared object or a pie, this
5778 // symbol's GOT entry will be set by a dynamic relocation.
5780 off
= got
->add_constant(0);
5781 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
5783 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
5785 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
5786 : elfcpp::R_POWERPC_RELATIVE
);
5787 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
5788 got
, off
, 0, false);
5793 case elfcpp::R_PPC64_TOC16
:
5794 case elfcpp::R_PPC64_TOC16_LO
:
5795 case elfcpp::R_PPC64_TOC16_HI
:
5796 case elfcpp::R_PPC64_TOC16_HA
:
5797 case elfcpp::R_PPC64_TOC16_DS
:
5798 case elfcpp::R_PPC64_TOC16_LO_DS
:
5799 // We need a GOT section.
5800 target
->got_section(symtab
, layout
);
5803 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5804 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
5805 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
5806 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
5808 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
5809 if (tls_type
== tls::TLSOPT_NONE
)
5811 Output_data_got_powerpc
<size
, big_endian
>* got
5812 = target
->got_section(symtab
, layout
);
5813 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5814 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5815 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
5816 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
5818 else if (tls_type
== tls::TLSOPT_TO_LE
)
5820 // no GOT relocs needed for Local Exec.
5827 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5828 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
5829 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
5830 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
5832 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5833 if (tls_type
== tls::TLSOPT_NONE
)
5834 target
->tlsld_got_offset(symtab
, layout
, object
);
5835 else if (tls_type
== tls::TLSOPT_TO_LE
)
5837 // no GOT relocs needed for Local Exec.
5838 if (parameters
->options().emit_relocs())
5840 Output_section
* os
= layout
->tls_segment()->first_section();
5841 gold_assert(os
!= NULL
);
5842 os
->set_needs_symtab_index();
5850 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5851 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
5852 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
5853 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
5855 Output_data_got_powerpc
<size
, big_endian
>* got
5856 = target
->got_section(symtab
, layout
);
5857 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5858 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
5862 case elfcpp::R_POWERPC_GOT_TPREL16
:
5863 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
5864 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
5865 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
5867 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
5868 if (tls_type
== tls::TLSOPT_NONE
)
5870 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5871 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
5873 Output_data_got_powerpc
<size
, big_endian
>* got
5874 = target
->got_section(symtab
, layout
);
5875 unsigned int off
= got
->add_constant(0);
5876 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
5878 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5879 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
5880 elfcpp::R_POWERPC_TPREL
,
5884 else if (tls_type
== tls::TLSOPT_TO_LE
)
5886 // no GOT relocs needed for Local Exec.
5894 unsupported_reloc_local(object
, r_type
);
5900 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5901 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5902 case elfcpp::R_POWERPC_GOT_TPREL16
:
5903 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5904 case elfcpp::R_POWERPC_GOT16
:
5905 case elfcpp::R_PPC64_GOT16_DS
:
5906 case elfcpp::R_PPC64_TOC16
:
5907 case elfcpp::R_PPC64_TOC16_DS
:
5908 ppc_object
->set_has_small_toc_reloc();
5914 // Report an unsupported relocation against a global symbol.
5916 template<int size
, bool big_endian
>
5918 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
5919 Sized_relobj_file
<size
, big_endian
>* object
,
5920 unsigned int r_type
,
5923 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5924 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
5927 // Scan a relocation for a global symbol.
5929 template<int size
, bool big_endian
>
5931 Target_powerpc
<size
, big_endian
>::Scan::global(
5932 Symbol_table
* symtab
,
5934 Target_powerpc
<size
, big_endian
>* target
,
5935 Sized_relobj_file
<size
, big_endian
>* object
,
5936 unsigned int data_shndx
,
5937 Output_section
* output_section
,
5938 const elfcpp::Rela
<size
, big_endian
>& reloc
,
5939 unsigned int r_type
,
5942 if (this->maybe_skip_tls_get_addr_call(r_type
, gsym
) == Track_tls::SKIP
)
5945 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
5946 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
5948 this->expect_tls_get_addr_call();
5949 const bool final
= gsym
->final_value_is_known();
5950 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
5951 if (tls_type
!= tls::TLSOPT_NONE
)
5952 this->skip_next_tls_get_addr_call();
5954 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
5955 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
5957 this->expect_tls_get_addr_call();
5958 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5959 if (tls_type
!= tls::TLSOPT_NONE
)
5960 this->skip_next_tls_get_addr_call();
5963 Powerpc_relobj
<size
, big_endian
>* ppc_object
5964 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5966 // A STT_GNU_IFUNC symbol may require a PLT entry.
5967 bool is_ifunc
= gsym
->type() == elfcpp::STT_GNU_IFUNC
;
5968 bool pushed_ifunc
= false;
5969 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
5971 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5972 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5973 reloc
.get_r_addend());
5974 target
->make_plt_entry(symtab
, layout
, gsym
);
5975 pushed_ifunc
= true;
5980 case elfcpp::R_POWERPC_NONE
:
5981 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5982 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5983 case elfcpp::R_PPC_LOCAL24PC
:
5984 case elfcpp::R_POWERPC_TLS
:
5985 case elfcpp::R_PPC64_ENTRY
:
5988 case elfcpp::R_PPC64_TOC
:
5990 Output_data_got_powerpc
<size
, big_endian
>* got
5991 = target
->got_section(symtab
, layout
);
5992 if (parameters
->options().output_is_position_independent())
5994 Address off
= reloc
.get_r_offset();
5996 && data_shndx
== ppc_object
->opd_shndx()
5997 && ppc_object
->get_opd_discard(off
- 8))
6000 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6001 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
6002 if (data_shndx
!= ppc_object
->opd_shndx())
6003 symobj
= static_cast
6004 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
6005 rela_dyn
->add_output_section_relative(got
->output_section(),
6006 elfcpp::R_POWERPC_RELATIVE
,
6008 object
, data_shndx
, off
,
6009 symobj
->toc_base_offset());
6014 case elfcpp::R_PPC64_ADDR64
:
6016 && target
->abiversion() < 2
6017 && data_shndx
== ppc_object
->opd_shndx()
6018 && (gsym
->is_defined_in_discarded_section()
6019 || gsym
->object() != object
))
6021 ppc_object
->set_opd_discard(reloc
.get_r_offset());
6025 case elfcpp::R_PPC64_UADDR64
:
6026 case elfcpp::R_POWERPC_ADDR32
:
6027 case elfcpp::R_POWERPC_UADDR32
:
6028 case elfcpp::R_POWERPC_ADDR24
:
6029 case elfcpp::R_POWERPC_ADDR16
:
6030 case elfcpp::R_POWERPC_ADDR16_LO
:
6031 case elfcpp::R_POWERPC_ADDR16_HI
:
6032 case elfcpp::R_POWERPC_ADDR16_HA
:
6033 case elfcpp::R_POWERPC_UADDR16
:
6034 case elfcpp::R_PPC64_ADDR16_HIGH
:
6035 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6036 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6037 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6038 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6039 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6040 case elfcpp::R_PPC64_ADDR16_DS
:
6041 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6042 case elfcpp::R_POWERPC_ADDR14
:
6043 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6044 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6046 // Make a PLT entry if necessary.
6047 if (gsym
->needs_plt_entry())
6049 // Since this is not a PC-relative relocation, we may be
6050 // taking the address of a function. In that case we need to
6051 // set the entry in the dynamic symbol table to the address of
6052 // the PLT call stub.
6053 bool need_ifunc_plt
= false;
6054 if ((size
== 32 || target
->abiversion() >= 2)
6055 && gsym
->is_from_dynobj()
6056 && !parameters
->options().output_is_position_independent())
6058 gsym
->set_needs_dynsym_value();
6059 need_ifunc_plt
= true;
6061 if (!is_ifunc
|| (!pushed_ifunc
&& need_ifunc_plt
))
6063 target
->push_branch(ppc_object
, data_shndx
,
6064 reloc
.get_r_offset(), r_type
,
6065 elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
6066 reloc
.get_r_addend());
6067 target
->make_plt_entry(symtab
, layout
, gsym
);
6070 // Make a dynamic relocation if necessary.
6071 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
))
6072 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
6074 if (!parameters
->options().output_is_position_independent()
6075 && gsym
->may_need_copy_reloc())
6077 target
->copy_reloc(symtab
, layout
, object
,
6078 data_shndx
, output_section
, gsym
, reloc
);
6080 else if ((((size
== 32
6081 && r_type
== elfcpp::R_POWERPC_ADDR32
)
6083 && r_type
== elfcpp::R_PPC64_ADDR64
6084 && target
->abiversion() >= 2))
6085 && gsym
->can_use_relative_reloc(false)
6086 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
6087 && parameters
->options().shared()))
6089 && r_type
== elfcpp::R_PPC64_ADDR64
6090 && target
->abiversion() < 2
6091 && (gsym
->can_use_relative_reloc(false)
6092 || data_shndx
== ppc_object
->opd_shndx())))
6094 Reloc_section
* rela_dyn
6095 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6096 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6097 : elfcpp::R_POWERPC_RELATIVE
);
6098 rela_dyn
->add_symbolless_global_addend(
6099 gsym
, dynrel
, output_section
, object
, data_shndx
,
6100 reloc
.get_r_offset(), reloc
.get_r_addend());
6104 Reloc_section
* rela_dyn
6105 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6106 check_non_pic(object
, r_type
);
6107 rela_dyn
->add_global(gsym
, r_type
, output_section
,
6109 reloc
.get_r_offset(),
6110 reloc
.get_r_addend());
6116 case elfcpp::R_PPC_PLTREL24
:
6117 case elfcpp::R_POWERPC_REL24
:
6120 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6122 elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
6123 reloc
.get_r_addend());
6124 if (gsym
->needs_plt_entry()
6125 || (!gsym
->final_value_is_known()
6126 && (gsym
->is_undefined()
6127 || gsym
->is_from_dynobj()
6128 || gsym
->is_preemptible())))
6129 target
->make_plt_entry(symtab
, layout
, gsym
);
6133 case elfcpp::R_PPC64_REL64
:
6134 case elfcpp::R_POWERPC_REL32
:
6135 // Make a dynamic relocation if necessary.
6136 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
)))
6138 if (!parameters
->options().output_is_position_independent()
6139 && gsym
->may_need_copy_reloc())
6141 target
->copy_reloc(symtab
, layout
, object
,
6142 data_shndx
, output_section
, gsym
,
6147 Reloc_section
* rela_dyn
6148 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6149 check_non_pic(object
, r_type
);
6150 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
6151 data_shndx
, reloc
.get_r_offset(),
6152 reloc
.get_r_addend());
6157 case elfcpp::R_POWERPC_REL14
:
6158 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6159 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6161 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6162 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
6163 reloc
.get_r_addend());
6166 case elfcpp::R_POWERPC_REL16
:
6167 case elfcpp::R_POWERPC_REL16_LO
:
6168 case elfcpp::R_POWERPC_REL16_HI
:
6169 case elfcpp::R_POWERPC_REL16_HA
:
6170 case elfcpp::R_POWERPC_REL16DX_HA
:
6171 case elfcpp::R_POWERPC_SECTOFF
:
6172 case elfcpp::R_POWERPC_SECTOFF_LO
:
6173 case elfcpp::R_POWERPC_SECTOFF_HI
:
6174 case elfcpp::R_POWERPC_SECTOFF_HA
:
6175 case elfcpp::R_PPC64_SECTOFF_DS
:
6176 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6177 case elfcpp::R_POWERPC_TPREL16
:
6178 case elfcpp::R_POWERPC_TPREL16_LO
:
6179 case elfcpp::R_POWERPC_TPREL16_HI
:
6180 case elfcpp::R_POWERPC_TPREL16_HA
:
6181 case elfcpp::R_PPC64_TPREL16_DS
:
6182 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6183 case elfcpp::R_PPC64_TPREL16_HIGH
:
6184 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6185 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6186 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6187 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6188 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6189 case elfcpp::R_POWERPC_DTPREL16
:
6190 case elfcpp::R_POWERPC_DTPREL16_LO
:
6191 case elfcpp::R_POWERPC_DTPREL16_HI
:
6192 case elfcpp::R_POWERPC_DTPREL16_HA
:
6193 case elfcpp::R_PPC64_DTPREL16_DS
:
6194 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6195 case elfcpp::R_PPC64_DTPREL16_HIGH
:
6196 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
6197 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6198 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6199 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6200 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6201 case elfcpp::R_PPC64_TLSGD
:
6202 case elfcpp::R_PPC64_TLSLD
:
6203 case elfcpp::R_PPC64_ADDR64_LOCAL
:
6206 case elfcpp::R_POWERPC_GOT16
:
6207 case elfcpp::R_POWERPC_GOT16_LO
:
6208 case elfcpp::R_POWERPC_GOT16_HI
:
6209 case elfcpp::R_POWERPC_GOT16_HA
:
6210 case elfcpp::R_PPC64_GOT16_DS
:
6211 case elfcpp::R_PPC64_GOT16_LO_DS
:
6213 // The symbol requires a GOT entry.
6214 Output_data_got_powerpc
<size
, big_endian
>* got
;
6216 got
= target
->got_section(symtab
, layout
);
6217 if (gsym
->final_value_is_known())
6220 && (size
== 32 || target
->abiversion() >= 2))
6221 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
6223 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
6225 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
6227 // If we are generating a shared object or a pie, this
6228 // symbol's GOT entry will be set by a dynamic relocation.
6229 unsigned int off
= got
->add_constant(0);
6230 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
6232 Reloc_section
* rela_dyn
6233 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6235 if (gsym
->can_use_relative_reloc(false)
6237 || target
->abiversion() >= 2)
6238 && gsym
->visibility() == elfcpp::STV_PROTECTED
6239 && parameters
->options().shared()))
6241 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6242 : elfcpp::R_POWERPC_RELATIVE
);
6243 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
6247 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
6248 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
6254 case elfcpp::R_PPC64_TOC16
:
6255 case elfcpp::R_PPC64_TOC16_LO
:
6256 case elfcpp::R_PPC64_TOC16_HI
:
6257 case elfcpp::R_PPC64_TOC16_HA
:
6258 case elfcpp::R_PPC64_TOC16_DS
:
6259 case elfcpp::R_PPC64_TOC16_LO_DS
:
6260 // We need a GOT section.
6261 target
->got_section(symtab
, layout
);
6264 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6265 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6266 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6267 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6269 const bool final
= gsym
->final_value_is_known();
6270 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6271 if (tls_type
== tls::TLSOPT_NONE
)
6273 Output_data_got_powerpc
<size
, big_endian
>* got
6274 = target
->got_section(symtab
, layout
);
6275 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6276 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
, rela_dyn
,
6277 elfcpp::R_POWERPC_DTPMOD
,
6278 elfcpp::R_POWERPC_DTPREL
);
6280 else if (tls_type
== tls::TLSOPT_TO_IE
)
6282 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
6284 Output_data_got_powerpc
<size
, big_endian
>* got
6285 = target
->got_section(symtab
, layout
);
6286 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6287 if (gsym
->is_undefined()
6288 || gsym
->is_from_dynobj())
6290 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
6291 elfcpp::R_POWERPC_TPREL
);
6295 unsigned int off
= got
->add_constant(0);
6296 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
6297 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
6298 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
6303 else if (tls_type
== tls::TLSOPT_TO_LE
)
6305 // no GOT relocs needed for Local Exec.
6312 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6313 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6314 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6315 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6317 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6318 if (tls_type
== tls::TLSOPT_NONE
)
6319 target
->tlsld_got_offset(symtab
, layout
, object
);
6320 else if (tls_type
== tls::TLSOPT_TO_LE
)
6322 // no GOT relocs needed for Local Exec.
6323 if (parameters
->options().emit_relocs())
6325 Output_section
* os
= layout
->tls_segment()->first_section();
6326 gold_assert(os
!= NULL
);
6327 os
->set_needs_symtab_index();
6335 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6336 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6337 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6338 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6340 Output_data_got_powerpc
<size
, big_endian
>* got
6341 = target
->got_section(symtab
, layout
);
6342 if (!gsym
->final_value_is_known()
6343 && (gsym
->is_from_dynobj()
6344 || gsym
->is_undefined()
6345 || gsym
->is_preemptible()))
6346 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
6347 target
->rela_dyn_section(layout
),
6348 elfcpp::R_POWERPC_DTPREL
);
6350 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
6354 case elfcpp::R_POWERPC_GOT_TPREL16
:
6355 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6356 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6357 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6359 const bool final
= gsym
->final_value_is_known();
6360 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
6361 if (tls_type
== tls::TLSOPT_NONE
)
6363 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
6365 Output_data_got_powerpc
<size
, big_endian
>* got
6366 = target
->got_section(symtab
, layout
);
6367 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6368 if (gsym
->is_undefined()
6369 || gsym
->is_from_dynobj())
6371 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
6372 elfcpp::R_POWERPC_TPREL
);
6376 unsigned int off
= got
->add_constant(0);
6377 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
6378 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
6379 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
6384 else if (tls_type
== tls::TLSOPT_TO_LE
)
6386 // no GOT relocs needed for Local Exec.
6394 unsupported_reloc_global(object
, r_type
, gsym
);
6400 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6401 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6402 case elfcpp::R_POWERPC_GOT_TPREL16
:
6403 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6404 case elfcpp::R_POWERPC_GOT16
:
6405 case elfcpp::R_PPC64_GOT16_DS
:
6406 case elfcpp::R_PPC64_TOC16
:
6407 case elfcpp::R_PPC64_TOC16_DS
:
6408 ppc_object
->set_has_small_toc_reloc();
6414 // Process relocations for gc.
6416 template<int size
, bool big_endian
>
6418 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
6419 Symbol_table
* symtab
,
6421 Sized_relobj_file
<size
, big_endian
>* object
,
6422 unsigned int data_shndx
,
6424 const unsigned char* prelocs
,
6426 Output_section
* output_section
,
6427 bool needs_special_offset_handling
,
6428 size_t local_symbol_count
,
6429 const unsigned char* plocal_symbols
)
6431 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
6432 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
6433 Powerpc_relobj
<size
, big_endian
>* ppc_object
6434 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6436 ppc_object
->set_opd_valid();
6437 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
6439 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
6440 for (p
= ppc_object
->access_from_map()->begin();
6441 p
!= ppc_object
->access_from_map()->end();
6444 Address dst_off
= p
->first
;
6445 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
6446 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
6447 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
6449 Relobj
* src_obj
= s
->first
;
6450 unsigned int src_indx
= s
->second
;
6451 symtab
->gc()->add_reference(src_obj
, src_indx
,
6452 ppc_object
, dst_indx
);
6456 ppc_object
->access_from_map()->clear();
6457 ppc_object
->process_gc_mark(symtab
);
6458 // Don't look at .opd relocs as .opd will reference everything.
6462 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
6463 typename
Target_powerpc::Relocatable_size_for_reloc
>(
6472 needs_special_offset_handling
,
6477 // Handle target specific gc actions when adding a gc reference from
6478 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
6479 // and DST_OFF. For powerpc64, this adds a referenc to the code
6480 // section of a function descriptor.
6482 template<int size
, bool big_endian
>
6484 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
6485 Symbol_table
* symtab
,
6487 unsigned int src_shndx
,
6489 unsigned int dst_shndx
,
6490 Address dst_off
) const
6492 if (size
!= 64 || dst_obj
->is_dynamic())
6495 Powerpc_relobj
<size
, big_endian
>* ppc_object
6496 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
6497 if (dst_shndx
!= 0 && dst_shndx
== ppc_object
->opd_shndx())
6499 if (ppc_object
->opd_valid())
6501 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
6502 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
6506 // If we haven't run scan_opd_relocs, we must delay
6507 // processing this function descriptor reference.
6508 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
6513 // Add any special sections for this symbol to the gc work list.
6514 // For powerpc64, this adds the code section of a function
6517 template<int size
, bool big_endian
>
6519 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
6520 Symbol_table
* symtab
,
6525 Powerpc_relobj
<size
, big_endian
>* ppc_object
6526 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
6528 unsigned int shndx
= sym
->shndx(&is_ordinary
);
6529 if (is_ordinary
&& shndx
!= 0 && shndx
== ppc_object
->opd_shndx())
6531 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
6532 Address dst_off
= gsym
->value();
6533 if (ppc_object
->opd_valid())
6535 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
6536 symtab
->gc()->worklist().push_back(Section_id(ppc_object
,
6540 ppc_object
->add_gc_mark(dst_off
);
6545 // For a symbol location in .opd, set LOC to the location of the
6548 template<int size
, bool big_endian
>
6550 Target_powerpc
<size
, big_endian
>::do_function_location(
6551 Symbol_location
* loc
) const
6553 if (size
== 64 && loc
->shndx
!= 0)
6555 if (loc
->object
->is_dynamic())
6557 Powerpc_dynobj
<size
, big_endian
>* ppc_object
6558 = static_cast<Powerpc_dynobj
<size
, big_endian
>*>(loc
->object
);
6559 if (loc
->shndx
== ppc_object
->opd_shndx())
6562 Address off
= loc
->offset
- ppc_object
->opd_address();
6563 loc
->shndx
= ppc_object
->get_opd_ent(off
, &dest_off
);
6564 loc
->offset
= dest_off
;
6569 const Powerpc_relobj
<size
, big_endian
>* ppc_object
6570 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(loc
->object
);
6571 if (loc
->shndx
== ppc_object
->opd_shndx())
6574 loc
->shndx
= ppc_object
->get_opd_ent(loc
->offset
, &dest_off
);
6575 loc
->offset
= dest_off
;
6581 // FNOFFSET in section SHNDX in OBJECT is the start of a function
6582 // compiled with -fsplit-stack. The function calls non-split-stack
6583 // code. Change the function to ensure it has enough stack space to
6584 // call some random function.
6586 template<int size
, bool big_endian
>
6588 Target_powerpc
<size
, big_endian
>::do_calls_non_split(
6591 section_offset_type fnoffset
,
6592 section_size_type fnsize
,
6593 unsigned char* view
,
6594 section_size_type view_size
,
6596 std::string
* to
) const
6598 // 32-bit not supported.
6602 Target::do_calls_non_split(object
, shndx
, fnoffset
, fnsize
,
6603 view
, view_size
, from
, to
);
6607 // The function always starts with
6608 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
6609 // addis %r12,%r1,-allocate@ha
6610 // addi %r12,%r12,-allocate@l
6612 // but note that the addis or addi may be replaced with a nop
6614 unsigned char *entry
= view
+ fnoffset
;
6615 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
6617 if ((insn
& 0xffff0000) == addis_2_12
)
6619 /* Skip ELFv2 global entry code. */
6621 insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
6624 unsigned char *pinsn
= entry
;
6626 const uint32_t ld_private_ss
= 0xe80d8fc0;
6627 if (insn
== ld_private_ss
)
6629 int32_t allocate
= 0;
6633 insn
= elfcpp::Swap
<32, big_endian
>::readval(pinsn
);
6634 if ((insn
& 0xffff0000) == addis_12_1
)
6635 allocate
+= (insn
& 0xffff) << 16;
6636 else if ((insn
& 0xffff0000) == addi_12_1
6637 || (insn
& 0xffff0000) == addi_12_12
)
6638 allocate
+= ((insn
& 0xffff) ^ 0x8000) - 0x8000;
6639 else if (insn
!= nop
)
6642 if (insn
== cmpld_7_12_0
&& pinsn
== entry
+ 12)
6644 int extra
= parameters
->options().split_stack_adjust_size();
6646 if (allocate
>= 0 || extra
< 0)
6648 object
->error(_("split-stack stack size overflow at "
6649 "section %u offset %0zx"),
6650 shndx
, static_cast<size_t>(fnoffset
));
6654 insn
= addis_12_1
| (((allocate
+ 0x8000) >> 16) & 0xffff);
6655 if (insn
!= addis_12_1
)
6657 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
6659 insn
= addi_12_12
| (allocate
& 0xffff);
6660 if (insn
!= addi_12_12
)
6662 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
6668 insn
= addi_12_1
| (allocate
& 0xffff);
6669 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
6672 if (pinsn
!= entry
+ 12)
6673 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, nop
);
6681 if (!object
->has_no_split_stack())
6682 object
->error(_("failed to match split-stack sequence at "
6683 "section %u offset %0zx"),
6684 shndx
, static_cast<size_t>(fnoffset
));
6688 // Scan relocations for a section.
6690 template<int size
, bool big_endian
>
6692 Target_powerpc
<size
, big_endian
>::scan_relocs(
6693 Symbol_table
* symtab
,
6695 Sized_relobj_file
<size
, big_endian
>* object
,
6696 unsigned int data_shndx
,
6697 unsigned int sh_type
,
6698 const unsigned char* prelocs
,
6700 Output_section
* output_section
,
6701 bool needs_special_offset_handling
,
6702 size_t local_symbol_count
,
6703 const unsigned char* plocal_symbols
)
6705 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
6706 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
6708 if (sh_type
== elfcpp::SHT_REL
)
6710 gold_error(_("%s: unsupported REL reloc section"),
6711 object
->name().c_str());
6715 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
6724 needs_special_offset_handling
,
6729 // Functor class for processing the global symbol table.
6730 // Removes symbols defined on discarded opd entries.
6732 template<bool big_endian
>
6733 class Global_symbol_visitor_opd
6736 Global_symbol_visitor_opd()
6740 operator()(Sized_symbol
<64>* sym
)
6742 if (sym
->has_symtab_index()
6743 || sym
->source() != Symbol::FROM_OBJECT
6744 || !sym
->in_real_elf())
6747 if (sym
->object()->is_dynamic())
6750 Powerpc_relobj
<64, big_endian
>* symobj
6751 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
6752 if (symobj
->opd_shndx() == 0)
6756 unsigned int shndx
= sym
->shndx(&is_ordinary
);
6757 if (shndx
== symobj
->opd_shndx()
6758 && symobj
->get_opd_discard(sym
->value()))
6760 sym
->set_undefined();
6761 sym
->set_visibility(elfcpp::STV_DEFAULT
);
6762 sym
->set_is_defined_in_discarded_section();
6763 sym
->set_symtab_index(-1U);
6768 template<int size
, bool big_endian
>
6770 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
6772 Symbol_table
* symtab
)
6776 Output_data_save_res
<size
, big_endian
>* savres
6777 = new Output_data_save_res
<size
, big_endian
>(symtab
);
6778 this->savres_section_
= savres
;
6779 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
6780 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
6781 savres
, ORDER_TEXT
, false);
6785 // Sort linker created .got section first (for the header), then input
6786 // sections belonging to files using small model code.
6788 template<bool big_endian
>
6789 class Sort_toc_sections
6793 operator()(const Output_section::Input_section
& is1
,
6794 const Output_section::Input_section
& is2
) const
6796 if (!is1
.is_input_section() && is2
.is_input_section())
6799 = (is1
.is_input_section()
6800 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
6801 ->has_small_toc_reloc()));
6803 = (is2
.is_input_section()
6804 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
6805 ->has_small_toc_reloc()));
6806 return small1
&& !small2
;
6810 // Finalize the sections.
6812 template<int size
, bool big_endian
>
6814 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
6816 const Input_objects
*,
6817 Symbol_table
* symtab
)
6819 if (parameters
->doing_static_link())
6821 // At least some versions of glibc elf-init.o have a strong
6822 // reference to __rela_iplt marker syms. A weak ref would be
6824 if (this->iplt_
!= NULL
)
6826 Reloc_section
* rel
= this->iplt_
->rel_plt();
6827 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
6828 Symbol_table::PREDEFINED
, rel
, 0, 0,
6829 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6830 elfcpp::STV_HIDDEN
, 0, false, true);
6831 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
6832 Symbol_table::PREDEFINED
, rel
, 0, 0,
6833 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6834 elfcpp::STV_HIDDEN
, 0, true, true);
6838 symtab
->define_as_constant("__rela_iplt_start", NULL
,
6839 Symbol_table::PREDEFINED
, 0, 0,
6840 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6841 elfcpp::STV_HIDDEN
, 0, true, false);
6842 symtab
->define_as_constant("__rela_iplt_end", NULL
,
6843 Symbol_table::PREDEFINED
, 0, 0,
6844 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6845 elfcpp::STV_HIDDEN
, 0, true, false);
6851 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
6852 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
6854 if (!parameters
->options().relocatable())
6856 this->define_save_restore_funcs(layout
, symtab
);
6858 // Annoyingly, we need to make these sections now whether or
6859 // not we need them. If we delay until do_relax then we
6860 // need to mess with the relaxation machinery checkpointing.
6861 this->got_section(symtab
, layout
);
6862 this->make_brlt_section(layout
);
6864 if (parameters
->options().toc_sort())
6866 Output_section
* os
= this->got_
->output_section();
6867 if (os
!= NULL
&& os
->input_sections().size() > 1)
6868 std::stable_sort(os
->input_sections().begin(),
6869 os
->input_sections().end(),
6870 Sort_toc_sections
<big_endian
>());
6875 // Fill in some more dynamic tags.
6876 Output_data_dynamic
* odyn
= layout
->dynamic_data();
6879 const Reloc_section
* rel_plt
= (this->plt_
== NULL
6881 : this->plt_
->rel_plt());
6882 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
6883 this->rela_dyn_
, true, size
== 32);
6887 if (this->got_
!= NULL
)
6889 this->got_
->finalize_data_size();
6890 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
6891 this->got_
, this->got_
->g_o_t());
6896 if (this->glink_
!= NULL
)
6898 this->glink_
->finalize_data_size();
6899 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
6901 (this->glink_
->pltresolve_size
6907 // Emit any relocs we saved in an attempt to avoid generating COPY
6909 if (this->copy_relocs_
.any_saved_relocs())
6910 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
6913 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6917 ok_lo_toc_insn(uint32_t insn
)
6919 return ((insn
& (0x3f << 26)) == 14u << 26 /* addi */
6920 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
6921 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
6922 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
6923 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
6924 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
6925 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
6926 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
6927 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
6928 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
6929 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
6930 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
6931 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
6932 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
6933 || ((insn
& (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6935 || ((insn
& (0x3f << 26)) == 62u << 26 /* std, stmd */
6936 && ((insn
& 3) == 0 || (insn
& 3) == 3))
6937 || (insn
& (0x3f << 26)) == 12u << 26 /* addic */);
6940 // Return the value to use for a branch relocation.
6942 template<int size
, bool big_endian
>
6944 Target_powerpc
<size
, big_endian
>::symval_for_branch(
6945 const Symbol_table
* symtab
,
6946 const Sized_symbol
<size
>* gsym
,
6947 Powerpc_relobj
<size
, big_endian
>* object
,
6949 unsigned int *dest_shndx
)
6951 if (size
== 32 || this->abiversion() >= 2)
6955 // If the symbol is defined in an opd section, ie. is a function
6956 // descriptor, use the function descriptor code entry address
6957 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
6959 && gsym
->source() != Symbol::FROM_OBJECT
)
6962 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
6963 unsigned int shndx
= symobj
->opd_shndx();
6966 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
6967 if (opd_addr
== invalid_address
)
6969 opd_addr
+= symobj
->output_section_address(shndx
);
6970 if (*value
>= opd_addr
&& *value
< opd_addr
+ symobj
->section_size(shndx
))
6973 *dest_shndx
= symobj
->get_opd_ent(*value
- opd_addr
, &sec_off
);
6974 if (symtab
->is_section_folded(symobj
, *dest_shndx
))
6977 = symtab
->icf()->get_folded_section(symobj
, *dest_shndx
);
6978 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(folded
.first
);
6979 *dest_shndx
= folded
.second
;
6981 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
6982 if (sec_addr
== invalid_address
)
6985 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
6986 *value
= sec_addr
+ sec_off
;
6991 // Perform a relocation.
6993 template<int size
, bool big_endian
>
6995 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
6996 const Relocate_info
<size
, big_endian
>* relinfo
,
6998 Target_powerpc
* target
,
7001 const unsigned char* preloc
,
7002 const Sized_symbol
<size
>* gsym
,
7003 const Symbol_value
<size
>* psymval
,
7004 unsigned char* view
,
7006 section_size_type view_size
)
7011 const elfcpp::Rela
<size
, big_endian
> rela(preloc
);
7012 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
7013 switch (this->maybe_skip_tls_get_addr_call(r_type
, gsym
))
7015 case Track_tls::NOT_EXPECTED
:
7016 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7017 _("__tls_get_addr call lacks marker reloc"));
7019 case Track_tls::EXPECTED
:
7020 // We have already complained.
7022 case Track_tls::SKIP
:
7024 case Track_tls::NORMAL
:
7028 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
7029 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
7030 Powerpc_relobj
<size
, big_endian
>* const object
7031 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
7033 bool has_stub_value
= false;
7034 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7036 ? gsym
->use_plt_offset(Scan::get_reference_flags(r_type
, target
))
7037 : object
->local_has_plt_offset(r_sym
))
7038 && (!psymval
->is_ifunc_symbol()
7039 || Scan::reloc_needs_plt_for_ifunc(target
, object
, r_type
, false)))
7043 && target
->abiversion() >= 2
7044 && !parameters
->options().output_is_position_independent()
7045 && !is_branch_reloc(r_type
))
7047 Address off
= target
->glink_section()->find_global_entry(gsym
);
7048 if (off
!= invalid_address
)
7050 value
= target
->glink_section()->global_entry_address() + off
;
7051 has_stub_value
= true;
7056 Stub_table
<size
, big_endian
>* stub_table
7057 = object
->stub_table(relinfo
->data_shndx
);
7058 if (stub_table
== NULL
)
7060 // This is a ref from a data section to an ifunc symbol.
7061 if (target
->stub_tables().size() != 0)
7062 stub_table
= target
->stub_tables()[0];
7064 if (stub_table
!= NULL
)
7068 off
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
7069 rela
.get_r_addend());
7071 off
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
7072 rela
.get_r_addend());
7073 if (off
!= invalid_address
)
7075 value
= stub_table
->stub_address() + off
;
7076 has_stub_value
= true;
7080 // We don't care too much about bogus debug references to
7081 // non-local functions, but otherwise there had better be a plt
7082 // call stub or global entry stub as appropriate.
7083 gold_assert(has_stub_value
|| !(os
->flags() & elfcpp::SHF_ALLOC
));
7086 if (r_type
== elfcpp::R_POWERPC_GOT16
7087 || r_type
== elfcpp::R_POWERPC_GOT16_LO
7088 || r_type
== elfcpp::R_POWERPC_GOT16_HI
7089 || r_type
== elfcpp::R_POWERPC_GOT16_HA
7090 || r_type
== elfcpp::R_PPC64_GOT16_DS
7091 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
7095 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
7096 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
7100 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7101 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
7102 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
7104 value
-= target
->got_section()->got_base_offset(object
);
7106 else if (r_type
== elfcpp::R_PPC64_TOC
)
7108 value
= (target
->got_section()->output_section()->address()
7109 + object
->toc_base_offset());
7111 else if (gsym
!= NULL
7112 && (r_type
== elfcpp::R_POWERPC_REL24
7113 || r_type
== elfcpp::R_PPC_PLTREL24
)
7118 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
7119 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
7120 bool can_plt_call
= false;
7121 if (rela
.get_r_offset() + 8 <= view_size
)
7123 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
7124 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
7127 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
7129 elfcpp::Swap
<32, big_endian
>::
7130 writeval(wv
+ 1, ld_2_1
+ target
->stk_toc());
7131 can_plt_call
= true;
7136 // If we don't have a branch and link followed by a nop,
7137 // we can't go via the plt because there is no place to
7138 // put a toc restoring instruction.
7139 // Unless we know we won't be returning.
7140 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
7141 can_plt_call
= true;
7145 // g++ as of 20130507 emits self-calls without a
7146 // following nop. This is arguably wrong since we have
7147 // conflicting information. On the one hand a global
7148 // symbol and on the other a local call sequence, but
7149 // don't error for this special case.
7150 // It isn't possible to cheaply verify we have exactly
7151 // such a call. Allow all calls to the same section.
7153 Address code
= value
;
7154 if (gsym
->source() == Symbol::FROM_OBJECT
7155 && gsym
->object() == object
)
7157 unsigned int dest_shndx
= 0;
7158 if (target
->abiversion() < 2)
7160 Address addend
= rela
.get_r_addend();
7161 code
= psymval
->value(object
, addend
);
7162 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
7163 &code
, &dest_shndx
);
7166 if (dest_shndx
== 0)
7167 dest_shndx
= gsym
->shndx(&is_ordinary
);
7168 ok
= dest_shndx
== relinfo
->data_shndx
;
7172 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7173 _("call lacks nop, can't restore toc; "
7174 "recompile with -fPIC"));
7180 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7181 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
7182 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
7183 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
7185 // First instruction of a global dynamic sequence, arg setup insn.
7186 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7187 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7188 enum Got_type got_type
= GOT_TYPE_STANDARD
;
7189 if (tls_type
== tls::TLSOPT_NONE
)
7190 got_type
= GOT_TYPE_TLSGD
;
7191 else if (tls_type
== tls::TLSOPT_TO_IE
)
7192 got_type
= GOT_TYPE_TPREL
;
7193 if (got_type
!= GOT_TYPE_STANDARD
)
7197 gold_assert(gsym
->has_got_offset(got_type
));
7198 value
= gsym
->got_offset(got_type
);
7202 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7203 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
7204 value
= object
->local_got_offset(r_sym
, got_type
);
7206 value
-= target
->got_section()->got_base_offset(object
);
7208 if (tls_type
== tls::TLSOPT_TO_IE
)
7210 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7211 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
7213 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7214 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7215 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
7217 insn
|= 32 << 26; // lwz
7219 insn
|= 58 << 26; // ld
7220 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7222 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
7223 - elfcpp::R_POWERPC_GOT_TLSGD16
);
7225 else if (tls_type
== tls::TLSOPT_TO_LE
)
7227 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7228 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
7230 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7231 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7232 insn
&= (1 << 26) - (1 << 21); // extract rt
7237 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7238 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7239 value
= psymval
->value(object
, rela
.get_r_addend());
7243 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7245 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7246 r_type
= elfcpp::R_POWERPC_NONE
;
7250 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7251 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
7252 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
7253 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
7255 // First instruction of a local dynamic sequence, arg setup insn.
7256 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7257 if (tls_type
== tls::TLSOPT_NONE
)
7259 value
= target
->tlsld_got_offset();
7260 value
-= target
->got_section()->got_base_offset(object
);
7264 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
7265 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7266 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
7268 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7269 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7270 insn
&= (1 << 26) - (1 << 21); // extract rt
7275 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7276 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7281 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7283 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7284 r_type
= elfcpp::R_POWERPC_NONE
;
7288 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
7289 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
7290 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
7291 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
7293 // Accesses relative to a local dynamic sequence address,
7294 // no optimisation here.
7297 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
7298 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
7302 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7303 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
7304 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
7306 value
-= target
->got_section()->got_base_offset(object
);
7308 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
7309 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
7310 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
7311 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
7313 // First instruction of initial exec sequence.
7314 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7315 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
7316 if (tls_type
== tls::TLSOPT_NONE
)
7320 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
7321 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
7325 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7326 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
7327 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
7329 value
-= target
->got_section()->got_base_offset(object
);
7333 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
7334 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
7335 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
7337 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7338 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7339 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
7344 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7345 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7346 value
= psymval
->value(object
, rela
.get_r_addend());
7350 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7352 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7353 r_type
= elfcpp::R_POWERPC_NONE
;
7357 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
7358 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
7360 // Second instruction of a global dynamic sequence,
7361 // the __tls_get_addr call
7362 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
7363 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7364 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7365 if (tls_type
!= tls::TLSOPT_NONE
)
7367 if (tls_type
== tls::TLSOPT_TO_IE
)
7369 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
7370 Insn insn
= add_3_3_13
;
7373 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7374 r_type
= elfcpp::R_POWERPC_NONE
;
7378 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
7379 Insn insn
= addi_3_3
;
7380 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7381 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7382 view
+= 2 * big_endian
;
7383 value
= psymval
->value(object
, rela
.get_r_addend());
7385 this->skip_next_tls_get_addr_call();
7388 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
7389 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
7391 // Second instruction of a local dynamic sequence,
7392 // the __tls_get_addr call
7393 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
7394 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7395 if (tls_type
== tls::TLSOPT_TO_LE
)
7397 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
7398 Insn insn
= addi_3_3
;
7399 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7400 this->skip_next_tls_get_addr_call();
7401 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7402 view
+= 2 * big_endian
;
7406 else if (r_type
== elfcpp::R_POWERPC_TLS
)
7408 // Second instruction of an initial exec sequence
7409 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7410 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
7411 if (tls_type
== tls::TLSOPT_TO_LE
)
7413 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
7414 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7415 unsigned int reg
= size
== 32 ? 2 : 13;
7416 insn
= at_tls_transform(insn
, reg
);
7417 gold_assert(insn
!= 0);
7418 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7419 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7420 view
+= 2 * big_endian
;
7421 value
= psymval
->value(object
, rela
.get_r_addend());
7424 else if (!has_stub_value
)
7427 if (!(size
== 32 && r_type
== elfcpp::R_PPC_PLTREL24
))
7428 addend
= rela
.get_r_addend();
7429 value
= psymval
->value(object
, addend
);
7430 if (size
== 64 && is_branch_reloc(r_type
))
7432 if (target
->abiversion() >= 2)
7435 value
+= object
->ppc64_local_entry_offset(gsym
);
7437 value
+= object
->ppc64_local_entry_offset(r_sym
);
7441 unsigned int dest_shndx
;
7442 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
7443 &value
, &dest_shndx
);
7446 Address max_branch_offset
= max_branch_delta(r_type
);
7447 if (max_branch_offset
!= 0
7448 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
7450 Stub_table
<size
, big_endian
>* stub_table
7451 = object
->stub_table(relinfo
->data_shndx
);
7452 if (stub_table
!= NULL
)
7454 Address off
= stub_table
->find_long_branch_entry(object
, value
);
7455 if (off
!= invalid_address
)
7457 value
= (stub_table
->stub_address() + stub_table
->plt_size()
7459 has_stub_value
= true;
7467 case elfcpp::R_PPC64_REL64
:
7468 case elfcpp::R_POWERPC_REL32
:
7469 case elfcpp::R_POWERPC_REL24
:
7470 case elfcpp::R_PPC_PLTREL24
:
7471 case elfcpp::R_PPC_LOCAL24PC
:
7472 case elfcpp::R_POWERPC_REL16
:
7473 case elfcpp::R_POWERPC_REL16_LO
:
7474 case elfcpp::R_POWERPC_REL16_HI
:
7475 case elfcpp::R_POWERPC_REL16_HA
:
7476 case elfcpp::R_POWERPC_REL16DX_HA
:
7477 case elfcpp::R_POWERPC_REL14
:
7478 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7479 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7483 case elfcpp::R_PPC64_TOC16
:
7484 case elfcpp::R_PPC64_TOC16_LO
:
7485 case elfcpp::R_PPC64_TOC16_HI
:
7486 case elfcpp::R_PPC64_TOC16_HA
:
7487 case elfcpp::R_PPC64_TOC16_DS
:
7488 case elfcpp::R_PPC64_TOC16_LO_DS
:
7489 // Subtract the TOC base address.
7490 value
-= (target
->got_section()->output_section()->address()
7491 + object
->toc_base_offset());
7494 case elfcpp::R_POWERPC_SECTOFF
:
7495 case elfcpp::R_POWERPC_SECTOFF_LO
:
7496 case elfcpp::R_POWERPC_SECTOFF_HI
:
7497 case elfcpp::R_POWERPC_SECTOFF_HA
:
7498 case elfcpp::R_PPC64_SECTOFF_DS
:
7499 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
7501 value
-= os
->address();
7504 case elfcpp::R_PPC64_TPREL16_DS
:
7505 case elfcpp::R_PPC64_TPREL16_LO_DS
:
7506 case elfcpp::R_PPC64_TPREL16_HIGH
:
7507 case elfcpp::R_PPC64_TPREL16_HIGHA
:
7509 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
7511 case elfcpp::R_POWERPC_TPREL16
:
7512 case elfcpp::R_POWERPC_TPREL16_LO
:
7513 case elfcpp::R_POWERPC_TPREL16_HI
:
7514 case elfcpp::R_POWERPC_TPREL16_HA
:
7515 case elfcpp::R_POWERPC_TPREL
:
7516 case elfcpp::R_PPC64_TPREL16_HIGHER
:
7517 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
7518 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
7519 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
7520 // tls symbol values are relative to tls_segment()->vaddr()
7524 case elfcpp::R_PPC64_DTPREL16_DS
:
7525 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
7526 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
7527 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
7528 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
7529 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
7531 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
7532 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
7534 case elfcpp::R_POWERPC_DTPREL16
:
7535 case elfcpp::R_POWERPC_DTPREL16_LO
:
7536 case elfcpp::R_POWERPC_DTPREL16_HI
:
7537 case elfcpp::R_POWERPC_DTPREL16_HA
:
7538 case elfcpp::R_POWERPC_DTPREL
:
7539 case elfcpp::R_PPC64_DTPREL16_HIGH
:
7540 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
7541 // tls symbol values are relative to tls_segment()->vaddr()
7542 value
-= dtp_offset
;
7545 case elfcpp::R_PPC64_ADDR64_LOCAL
:
7547 value
+= object
->ppc64_local_entry_offset(gsym
);
7549 value
+= object
->ppc64_local_entry_offset(r_sym
);
7556 Insn branch_bit
= 0;
7559 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
7560 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7561 branch_bit
= 1 << 21;
7562 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
7563 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7565 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
7566 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7569 if (this->is_isa_v2
)
7571 // Set 'a' bit. This is 0b00010 in BO field for branch
7572 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
7573 // for branch on CTR insns (BO == 1a00t or 1a01t).
7574 if ((insn
& (0x14 << 21)) == (0x04 << 21))
7576 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
7583 // Invert 'y' bit if not the default.
7584 if (static_cast<Signed_address
>(value
) < 0)
7587 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7597 // Multi-instruction sequences that access the TOC can be
7598 // optimized, eg. addis ra,r2,0; addi rb,ra,x;
7599 // to nop; addi rb,r2,x;
7605 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7606 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7607 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7608 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7609 case elfcpp::R_POWERPC_GOT16_HA
:
7610 case elfcpp::R_PPC64_TOC16_HA
:
7611 if (parameters
->options().toc_optimize())
7613 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7614 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7615 if ((insn
& ((0x3f << 26) | 0x1f << 16))
7616 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
7617 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7618 _("toc optimization is not supported "
7619 "for %#08x instruction"), insn
);
7620 else if (value
+ 0x8000 < 0x10000)
7622 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
7628 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7629 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7630 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7631 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7632 case elfcpp::R_POWERPC_GOT16_LO
:
7633 case elfcpp::R_PPC64_GOT16_LO_DS
:
7634 case elfcpp::R_PPC64_TOC16_LO
:
7635 case elfcpp::R_PPC64_TOC16_LO_DS
:
7636 if (parameters
->options().toc_optimize())
7638 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7639 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7640 if (!ok_lo_toc_insn(insn
))
7641 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7642 _("toc optimization is not supported "
7643 "for %#08x instruction"), insn
);
7644 else if (value
+ 0x8000 < 0x10000)
7646 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
7648 // Transform addic to addi when we change reg.
7649 insn
&= ~((0x3f << 26) | (0x1f << 16));
7650 insn
|= (14u << 26) | (2 << 16);
7654 insn
&= ~(0x1f << 16);
7657 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7662 case elfcpp::R_PPC64_ENTRY
:
7663 value
= (target
->got_section()->output_section()->address()
7664 + object
->toc_base_offset());
7665 if (value
+ 0x80008000 <= 0xffffffff
7666 && !parameters
->options().output_is_position_independent())
7668 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
7669 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7670 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
7672 if ((insn1
& ~0xfffc) == ld_2_12
7673 && insn2
== add_2_2_12
)
7675 insn1
= lis_2
+ ha(value
);
7676 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
7677 insn2
= addi_2_2
+ l(value
);
7678 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
7685 if (value
+ 0x80008000 <= 0xffffffff)
7687 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
7688 Insn insn1
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7689 Insn insn2
= elfcpp::Swap
<32, big_endian
>::readval(iview
+ 1);
7691 if ((insn1
& ~0xfffc) == ld_2_12
7692 && insn2
== add_2_2_12
)
7694 insn1
= addis_2_12
+ ha(value
);
7695 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn1
);
7696 insn2
= addi_2_2
+ l(value
);
7697 elfcpp::Swap
<32, big_endian
>::writeval(iview
+ 1, insn2
);
7706 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
7707 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->data_shdr
);
7710 case elfcpp::R_POWERPC_ADDR32
:
7711 case elfcpp::R_POWERPC_UADDR32
:
7713 overflow
= Reloc::CHECK_BITFIELD
;
7716 case elfcpp::R_POWERPC_REL32
:
7717 case elfcpp::R_POWERPC_REL16DX_HA
:
7719 overflow
= Reloc::CHECK_SIGNED
;
7722 case elfcpp::R_POWERPC_UADDR16
:
7723 overflow
= Reloc::CHECK_BITFIELD
;
7726 case elfcpp::R_POWERPC_ADDR16
:
7727 // We really should have three separate relocations,
7728 // one for 16-bit data, one for insns with 16-bit signed fields,
7729 // and one for insns with 16-bit unsigned fields.
7730 overflow
= Reloc::CHECK_BITFIELD
;
7731 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
7732 overflow
= Reloc::CHECK_LOW_INSN
;
7735 case elfcpp::R_POWERPC_ADDR16_HI
:
7736 case elfcpp::R_POWERPC_ADDR16_HA
:
7737 case elfcpp::R_POWERPC_GOT16_HI
:
7738 case elfcpp::R_POWERPC_GOT16_HA
:
7739 case elfcpp::R_POWERPC_PLT16_HI
:
7740 case elfcpp::R_POWERPC_PLT16_HA
:
7741 case elfcpp::R_POWERPC_SECTOFF_HI
:
7742 case elfcpp::R_POWERPC_SECTOFF_HA
:
7743 case elfcpp::R_PPC64_TOC16_HI
:
7744 case elfcpp::R_PPC64_TOC16_HA
:
7745 case elfcpp::R_PPC64_PLTGOT16_HI
:
7746 case elfcpp::R_PPC64_PLTGOT16_HA
:
7747 case elfcpp::R_POWERPC_TPREL16_HI
:
7748 case elfcpp::R_POWERPC_TPREL16_HA
:
7749 case elfcpp::R_POWERPC_DTPREL16_HI
:
7750 case elfcpp::R_POWERPC_DTPREL16_HA
:
7751 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
7752 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7753 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
7754 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7755 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7756 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7757 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
7758 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7759 case elfcpp::R_POWERPC_REL16_HI
:
7760 case elfcpp::R_POWERPC_REL16_HA
:
7762 overflow
= Reloc::CHECK_HIGH_INSN
;
7765 case elfcpp::R_POWERPC_REL16
:
7766 case elfcpp::R_PPC64_TOC16
:
7767 case elfcpp::R_POWERPC_GOT16
:
7768 case elfcpp::R_POWERPC_SECTOFF
:
7769 case elfcpp::R_POWERPC_TPREL16
:
7770 case elfcpp::R_POWERPC_DTPREL16
:
7771 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7772 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7773 case elfcpp::R_POWERPC_GOT_TPREL16
:
7774 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7775 overflow
= Reloc::CHECK_LOW_INSN
;
7778 case elfcpp::R_POWERPC_ADDR24
:
7779 case elfcpp::R_POWERPC_ADDR14
:
7780 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
7781 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
7782 case elfcpp::R_PPC64_ADDR16_DS
:
7783 case elfcpp::R_POWERPC_REL24
:
7784 case elfcpp::R_PPC_PLTREL24
:
7785 case elfcpp::R_PPC_LOCAL24PC
:
7786 case elfcpp::R_PPC64_TPREL16_DS
:
7787 case elfcpp::R_PPC64_DTPREL16_DS
:
7788 case elfcpp::R_PPC64_TOC16_DS
:
7789 case elfcpp::R_PPC64_GOT16_DS
:
7790 case elfcpp::R_PPC64_SECTOFF_DS
:
7791 case elfcpp::R_POWERPC_REL14
:
7792 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7793 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7794 overflow
= Reloc::CHECK_SIGNED
;
7798 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7801 if (overflow
== Reloc::CHECK_LOW_INSN
7802 || overflow
== Reloc::CHECK_HIGH_INSN
)
7804 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7806 if ((insn
& (0x3f << 26)) == 10u << 26 /* cmpli */)
7807 overflow
= Reloc::CHECK_BITFIELD
;
7808 else if (overflow
== Reloc::CHECK_LOW_INSN
7809 ? ((insn
& (0x3f << 26)) == 28u << 26 /* andi */
7810 || (insn
& (0x3f << 26)) == 24u << 26 /* ori */
7811 || (insn
& (0x3f << 26)) == 26u << 26 /* xori */)
7812 : ((insn
& (0x3f << 26)) == 29u << 26 /* andis */
7813 || (insn
& (0x3f << 26)) == 25u << 26 /* oris */
7814 || (insn
& (0x3f << 26)) == 27u << 26 /* xoris */))
7815 overflow
= Reloc::CHECK_UNSIGNED
;
7817 overflow
= Reloc::CHECK_SIGNED
;
7820 bool maybe_dq_reloc
= false;
7821 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
7822 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
7825 case elfcpp::R_POWERPC_NONE
:
7826 case elfcpp::R_POWERPC_TLS
:
7827 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
7828 case elfcpp::R_POWERPC_GNU_VTENTRY
:
7831 case elfcpp::R_PPC64_ADDR64
:
7832 case elfcpp::R_PPC64_REL64
:
7833 case elfcpp::R_PPC64_TOC
:
7834 case elfcpp::R_PPC64_ADDR64_LOCAL
:
7835 Reloc::addr64(view
, value
);
7838 case elfcpp::R_POWERPC_TPREL
:
7839 case elfcpp::R_POWERPC_DTPREL
:
7841 Reloc::addr64(view
, value
);
7843 status
= Reloc::addr32(view
, value
, overflow
);
7846 case elfcpp::R_PPC64_UADDR64
:
7847 Reloc::addr64_u(view
, value
);
7850 case elfcpp::R_POWERPC_ADDR32
:
7851 status
= Reloc::addr32(view
, value
, overflow
);
7854 case elfcpp::R_POWERPC_REL32
:
7855 case elfcpp::R_POWERPC_UADDR32
:
7856 status
= Reloc::addr32_u(view
, value
, overflow
);
7859 case elfcpp::R_POWERPC_ADDR24
:
7860 case elfcpp::R_POWERPC_REL24
:
7861 case elfcpp::R_PPC_PLTREL24
:
7862 case elfcpp::R_PPC_LOCAL24PC
:
7863 status
= Reloc::addr24(view
, value
, overflow
);
7866 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7867 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7868 case elfcpp::R_POWERPC_GOT_TPREL16
:
7869 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7872 // On ppc64 these are all ds form
7873 maybe_dq_reloc
= true;
7876 case elfcpp::R_POWERPC_ADDR16
:
7877 case elfcpp::R_POWERPC_REL16
:
7878 case elfcpp::R_PPC64_TOC16
:
7879 case elfcpp::R_POWERPC_GOT16
:
7880 case elfcpp::R_POWERPC_SECTOFF
:
7881 case elfcpp::R_POWERPC_TPREL16
:
7882 case elfcpp::R_POWERPC_DTPREL16
:
7883 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7884 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7885 case elfcpp::R_POWERPC_ADDR16_LO
:
7886 case elfcpp::R_POWERPC_REL16_LO
:
7887 case elfcpp::R_PPC64_TOC16_LO
:
7888 case elfcpp::R_POWERPC_GOT16_LO
:
7889 case elfcpp::R_POWERPC_SECTOFF_LO
:
7890 case elfcpp::R_POWERPC_TPREL16_LO
:
7891 case elfcpp::R_POWERPC_DTPREL16_LO
:
7892 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7893 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7895 status
= Reloc::addr16(view
, value
, overflow
);
7897 maybe_dq_reloc
= true;
7900 case elfcpp::R_POWERPC_UADDR16
:
7901 status
= Reloc::addr16_u(view
, value
, overflow
);
7904 case elfcpp::R_PPC64_ADDR16_HIGH
:
7905 case elfcpp::R_PPC64_TPREL16_HIGH
:
7906 case elfcpp::R_PPC64_DTPREL16_HIGH
:
7908 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
7910 case elfcpp::R_POWERPC_ADDR16_HI
:
7911 case elfcpp::R_POWERPC_REL16_HI
:
7912 case elfcpp::R_PPC64_TOC16_HI
:
7913 case elfcpp::R_POWERPC_GOT16_HI
:
7914 case elfcpp::R_POWERPC_SECTOFF_HI
:
7915 case elfcpp::R_POWERPC_TPREL16_HI
:
7916 case elfcpp::R_POWERPC_DTPREL16_HI
:
7917 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
7918 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
7919 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7920 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
7921 Reloc::addr16_hi(view
, value
);
7924 case elfcpp::R_PPC64_ADDR16_HIGHA
:
7925 case elfcpp::R_PPC64_TPREL16_HIGHA
:
7926 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
7928 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
7930 case elfcpp::R_POWERPC_ADDR16_HA
:
7931 case elfcpp::R_POWERPC_REL16_HA
:
7932 case elfcpp::R_PPC64_TOC16_HA
:
7933 case elfcpp::R_POWERPC_GOT16_HA
:
7934 case elfcpp::R_POWERPC_SECTOFF_HA
:
7935 case elfcpp::R_POWERPC_TPREL16_HA
:
7936 case elfcpp::R_POWERPC_DTPREL16_HA
:
7937 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7938 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7939 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7940 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7941 Reloc::addr16_ha(view
, value
);
7944 case elfcpp::R_POWERPC_REL16DX_HA
:
7945 status
= Reloc::addr16dx_ha(view
, value
, overflow
);
7948 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
7950 // R_PPC_EMB_NADDR16_LO
7952 case elfcpp::R_PPC64_ADDR16_HIGHER
:
7953 case elfcpp::R_PPC64_TPREL16_HIGHER
:
7954 Reloc::addr16_hi2(view
, value
);
7957 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
7959 // R_PPC_EMB_NADDR16_HI
7961 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
7962 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
7963 Reloc::addr16_ha2(view
, value
);
7966 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
7968 // R_PPC_EMB_NADDR16_HA
7970 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
7971 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
7972 Reloc::addr16_hi3(view
, value
);
7975 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
7979 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
7980 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
7981 Reloc::addr16_ha3(view
, value
);
7984 case elfcpp::R_PPC64_DTPREL16_DS
:
7985 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
7987 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
7989 case elfcpp::R_PPC64_TPREL16_DS
:
7990 case elfcpp::R_PPC64_TPREL16_LO_DS
:
7992 // R_PPC_TLSGD, R_PPC_TLSLD
7994 case elfcpp::R_PPC64_ADDR16_DS
:
7995 case elfcpp::R_PPC64_ADDR16_LO_DS
:
7996 case elfcpp::R_PPC64_TOC16_DS
:
7997 case elfcpp::R_PPC64_TOC16_LO_DS
:
7998 case elfcpp::R_PPC64_GOT16_DS
:
7999 case elfcpp::R_PPC64_GOT16_LO_DS
:
8000 case elfcpp::R_PPC64_SECTOFF_DS
:
8001 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
8002 maybe_dq_reloc
= true;
8005 case elfcpp::R_POWERPC_ADDR14
:
8006 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
8007 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
8008 case elfcpp::R_POWERPC_REL14
:
8009 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
8010 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
8011 status
= Reloc::addr14(view
, value
, overflow
);
8014 case elfcpp::R_POWERPC_COPY
:
8015 case elfcpp::R_POWERPC_GLOB_DAT
:
8016 case elfcpp::R_POWERPC_JMP_SLOT
:
8017 case elfcpp::R_POWERPC_RELATIVE
:
8018 case elfcpp::R_POWERPC_DTPMOD
:
8019 case elfcpp::R_PPC64_JMP_IREL
:
8020 case elfcpp::R_POWERPC_IRELATIVE
:
8021 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8022 _("unexpected reloc %u in object file"),
8026 case elfcpp::R_PPC_EMB_SDA21
:
8031 // R_PPC64_TOCSAVE. For the time being this can be ignored.
8035 case elfcpp::R_PPC_EMB_SDA2I16
:
8036 case elfcpp::R_PPC_EMB_SDA2REL
:
8039 // R_PPC64_TLSGD, R_PPC64_TLSLD
8042 case elfcpp::R_POWERPC_PLT32
:
8043 case elfcpp::R_POWERPC_PLTREL32
:
8044 case elfcpp::R_POWERPC_PLT16_LO
:
8045 case elfcpp::R_POWERPC_PLT16_HI
:
8046 case elfcpp::R_POWERPC_PLT16_HA
:
8047 case elfcpp::R_PPC_SDAREL16
:
8048 case elfcpp::R_POWERPC_ADDR30
:
8049 case elfcpp::R_PPC64_PLT64
:
8050 case elfcpp::R_PPC64_PLTREL64
:
8051 case elfcpp::R_PPC64_PLTGOT16
:
8052 case elfcpp::R_PPC64_PLTGOT16_LO
:
8053 case elfcpp::R_PPC64_PLTGOT16_HI
:
8054 case elfcpp::R_PPC64_PLTGOT16_HA
:
8055 case elfcpp::R_PPC64_PLT16_LO_DS
:
8056 case elfcpp::R_PPC64_PLTGOT16_DS
:
8057 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
8058 case elfcpp::R_PPC_EMB_RELSDA
:
8059 case elfcpp::R_PPC_TOC16
:
8062 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8063 _("unsupported reloc %u"),
8071 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8073 if ((insn
& (0x3f << 26)) == 56u << 26 /* lq */
8074 || ((insn
& (0x3f << 26)) == (61u << 26) /* lxv, stxv */
8075 && (insn
& 3) == 1))
8076 status
= Reloc::addr16_dq(view
, value
, overflow
);
8078 || (insn
& (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
8079 || (insn
& (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
8080 || (insn
& (0x3f << 26)) == 57u << 26 /* lfdp */
8081 || (insn
& (0x3f << 26)) == 61u << 26 /* stfdp */)
8082 status
= Reloc::addr16_ds(view
, value
, overflow
);
8084 status
= Reloc::addr16(view
, value
, overflow
);
8087 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
8090 && gsym
->is_undefined()
8091 && is_branch_reloc(r_type
))))
8093 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8094 _("relocation overflow"));
8096 gold_info(_("try relinking with a smaller --stub-group-size"));
8102 // Relocate section data.
8104 template<int size
, bool big_endian
>
8106 Target_powerpc
<size
, big_endian
>::relocate_section(
8107 const Relocate_info
<size
, big_endian
>* relinfo
,
8108 unsigned int sh_type
,
8109 const unsigned char* prelocs
,
8111 Output_section
* output_section
,
8112 bool needs_special_offset_handling
,
8113 unsigned char* view
,
8115 section_size_type view_size
,
8116 const Reloc_symbol_changes
* reloc_symbol_changes
)
8118 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
8119 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
8120 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
8121 Powerpc_comdat_behavior
;
8123 gold_assert(sh_type
== elfcpp::SHT_RELA
);
8125 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
8126 Powerpc_relocate
, Powerpc_comdat_behavior
>(
8132 needs_special_offset_handling
,
8136 reloc_symbol_changes
);
8139 class Powerpc_scan_relocatable_reloc
8142 // Return the strategy to use for a local symbol which is not a
8143 // section symbol, given the relocation type.
8144 inline Relocatable_relocs::Reloc_strategy
8145 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
8147 if (r_type
== 0 && r_sym
== 0)
8148 return Relocatable_relocs::RELOC_DISCARD
;
8149 return Relocatable_relocs::RELOC_COPY
;
8152 // Return the strategy to use for a local symbol which is a section
8153 // symbol, given the relocation type.
8154 inline Relocatable_relocs::Reloc_strategy
8155 local_section_strategy(unsigned int, Relobj
*)
8157 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
8160 // Return the strategy to use for a global symbol, given the
8161 // relocation type, the object, and the symbol index.
8162 inline Relocatable_relocs::Reloc_strategy
8163 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
8165 if (r_type
== elfcpp::R_PPC_PLTREL24
)
8166 return Relocatable_relocs::RELOC_SPECIAL
;
8167 return Relocatable_relocs::RELOC_COPY
;
8171 // Scan the relocs during a relocatable link.
8173 template<int size
, bool big_endian
>
8175 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
8176 Symbol_table
* symtab
,
8178 Sized_relobj_file
<size
, big_endian
>* object
,
8179 unsigned int data_shndx
,
8180 unsigned int sh_type
,
8181 const unsigned char* prelocs
,
8183 Output_section
* output_section
,
8184 bool needs_special_offset_handling
,
8185 size_t local_symbol_count
,
8186 const unsigned char* plocal_symbols
,
8187 Relocatable_relocs
* rr
)
8189 gold_assert(sh_type
== elfcpp::SHT_RELA
);
8191 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
8192 Powerpc_scan_relocatable_reloc
>(
8200 needs_special_offset_handling
,
8206 // Emit relocations for a section.
8207 // This is a modified version of the function by the same name in
8208 // target-reloc.h. Using relocate_special_relocatable for
8209 // R_PPC_PLTREL24 would require duplication of the entire body of the
8210 // loop, so we may as well duplicate the whole thing.
8212 template<int size
, bool big_endian
>
8214 Target_powerpc
<size
, big_endian
>::relocate_relocs(
8215 const Relocate_info
<size
, big_endian
>* relinfo
,
8216 unsigned int sh_type
,
8217 const unsigned char* prelocs
,
8219 Output_section
* output_section
,
8220 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
8222 Address view_address
,
8224 unsigned char* reloc_view
,
8225 section_size_type reloc_view_size
)
8227 gold_assert(sh_type
== elfcpp::SHT_RELA
);
8229 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
8231 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc_write
8233 const int reloc_size
8234 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
8236 Powerpc_relobj
<size
, big_endian
>* const object
8237 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
8238 const unsigned int local_count
= object
->local_symbol_count();
8239 unsigned int got2_shndx
= object
->got2_shndx();
8240 Address got2_addend
= 0;
8241 if (got2_shndx
!= 0)
8243 got2_addend
= object
->get_output_section_offset(got2_shndx
);
8244 gold_assert(got2_addend
!= invalid_address
);
8247 unsigned char* pwrite
= reloc_view
;
8248 bool zap_next
= false;
8249 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
8251 Relocatable_relocs::Reloc_strategy strategy
= relinfo
->rr
->strategy(i
);
8252 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
8255 Reltype
reloc(prelocs
);
8256 Reltype_write
reloc_write(pwrite
);
8258 Address offset
= reloc
.get_r_offset();
8259 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
8260 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
8261 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
8262 const unsigned int orig_r_sym
= r_sym
;
8263 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
8264 = reloc
.get_r_addend();
8265 const Symbol
* gsym
= NULL
;
8269 // We could arrange to discard these and other relocs for
8270 // tls optimised sequences in the strategy methods, but for
8271 // now do as BFD ld does.
8272 r_type
= elfcpp::R_POWERPC_NONE
;
8276 // Get the new symbol index.
8277 Output_section
* os
= NULL
;
8278 if (r_sym
< local_count
)
8282 case Relocatable_relocs::RELOC_COPY
:
8283 case Relocatable_relocs::RELOC_SPECIAL
:
8286 r_sym
= object
->symtab_index(r_sym
);
8287 gold_assert(r_sym
!= -1U);
8291 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
8293 // We are adjusting a section symbol. We need to find
8294 // the symbol table index of the section symbol for
8295 // the output section corresponding to input section
8296 // in which this symbol is defined.
8297 gold_assert(r_sym
< local_count
);
8299 unsigned int shndx
=
8300 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
8301 gold_assert(is_ordinary
);
8302 os
= object
->output_section(shndx
);
8303 gold_assert(os
!= NULL
);
8304 gold_assert(os
->needs_symtab_index());
8305 r_sym
= os
->symtab_index();
8315 gsym
= object
->global_symbol(r_sym
);
8316 gold_assert(gsym
!= NULL
);
8317 if (gsym
->is_forwarder())
8318 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
8320 gold_assert(gsym
->has_symtab_index());
8321 r_sym
= gsym
->symtab_index();
8324 // Get the new offset--the location in the output section where
8325 // this relocation should be applied.
8326 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
8327 offset
+= offset_in_output_section
;
8330 section_offset_type sot_offset
=
8331 convert_types
<section_offset_type
, Address
>(offset
);
8332 section_offset_type new_sot_offset
=
8333 output_section
->output_offset(object
, relinfo
->data_shndx
,
8335 gold_assert(new_sot_offset
!= -1);
8336 offset
= new_sot_offset
;
8339 // In an object file, r_offset is an offset within the section.
8340 // In an executable or dynamic object, generated by
8341 // --emit-relocs, r_offset is an absolute address.
8342 if (!parameters
->options().relocatable())
8344 offset
+= view_address
;
8345 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
8346 offset
-= offset_in_output_section
;
8349 // Handle the reloc addend based on the strategy.
8350 if (strategy
== Relocatable_relocs::RELOC_COPY
)
8352 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
8354 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
8355 gold_assert(os
!= NULL
);
8356 addend
= psymval
->value(object
, addend
) - os
->address();
8358 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
8360 if (addend
>= 32768)
8361 addend
+= got2_addend
;
8366 if (!parameters
->options().relocatable())
8368 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8369 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
8370 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
8371 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
8373 // First instruction of a global dynamic sequence,
8375 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8376 switch (this->optimize_tls_gd(final
))
8378 case tls::TLSOPT_TO_IE
:
8379 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
8380 - elfcpp::R_POWERPC_GOT_TLSGD16
);
8382 case tls::TLSOPT_TO_LE
:
8383 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8384 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
8385 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8388 r_type
= elfcpp::R_POWERPC_NONE
;
8389 offset
-= 2 * big_endian
;
8396 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
8397 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
8398 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
8399 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
8401 // First instruction of a local dynamic sequence,
8403 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
8405 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
8406 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
8408 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8409 const Output_section
* os
= relinfo
->layout
->tls_segment()
8411 gold_assert(os
!= NULL
);
8412 gold_assert(os
->needs_symtab_index());
8413 r_sym
= os
->symtab_index();
8414 addend
= dtp_offset
;
8418 r_type
= elfcpp::R_POWERPC_NONE
;
8419 offset
-= 2 * big_endian
;
8423 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8424 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
8425 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
8426 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
8428 // First instruction of initial exec sequence.
8429 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8430 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
8432 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8433 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
8434 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8437 r_type
= elfcpp::R_POWERPC_NONE
;
8438 offset
-= 2 * big_endian
;
8442 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
8443 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
8445 // Second instruction of a global dynamic sequence,
8446 // the __tls_get_addr call
8447 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8448 switch (this->optimize_tls_gd(final
))
8450 case tls::TLSOPT_TO_IE
:
8451 r_type
= elfcpp::R_POWERPC_NONE
;
8454 case tls::TLSOPT_TO_LE
:
8455 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8456 offset
+= 2 * big_endian
;
8463 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
8464 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
8466 // Second instruction of a local dynamic sequence,
8467 // the __tls_get_addr call
8468 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
8470 const Output_section
* os
= relinfo
->layout
->tls_segment()
8472 gold_assert(os
!= NULL
);
8473 gold_assert(os
->needs_symtab_index());
8474 r_sym
= os
->symtab_index();
8475 addend
= dtp_offset
;
8476 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8477 offset
+= 2 * big_endian
;
8481 else if (r_type
== elfcpp::R_POWERPC_TLS
)
8483 // Second instruction of an initial exec sequence
8484 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8485 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
8487 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8488 offset
+= 2 * big_endian
;
8493 reloc_write
.put_r_offset(offset
);
8494 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
8495 reloc_write
.put_r_addend(addend
);
8497 pwrite
+= reloc_size
;
8500 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
8501 == reloc_view_size
);
8504 // Return the value to use for a dynamic symbol which requires special
8505 // treatment. This is how we support equality comparisons of function
8506 // pointers across shared library boundaries, as described in the
8507 // processor specific ABI supplement.
8509 template<int size
, bool big_endian
>
8511 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
8515 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
8516 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
8517 p
!= this->stub_tables_
.end();
8520 Address off
= (*p
)->find_plt_call_entry(gsym
);
8521 if (off
!= invalid_address
)
8522 return (*p
)->stub_address() + off
;
8525 else if (this->abiversion() >= 2)
8527 Address off
= this->glink_section()->find_global_entry(gsym
);
8528 if (off
!= invalid_address
)
8529 return this->glink_section()->global_entry_address() + off
;
8534 // Return the PLT address to use for a local symbol.
8535 template<int size
, bool big_endian
>
8537 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
8538 const Relobj
* object
,
8539 unsigned int symndx
) const
8543 const Sized_relobj
<size
, big_endian
>* relobj
8544 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
8545 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
8546 p
!= this->stub_tables_
.end();
8549 Address off
= (*p
)->find_plt_call_entry(relobj
->sized_relobj(),
8551 if (off
!= invalid_address
)
8552 return (*p
)->stub_address() + off
;
8558 // Return the PLT address to use for a global symbol.
8559 template<int size
, bool big_endian
>
8561 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
8562 const Symbol
* gsym
) const
8566 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
8567 p
!= this->stub_tables_
.end();
8570 Address off
= (*p
)->find_plt_call_entry(gsym
);
8571 if (off
!= invalid_address
)
8572 return (*p
)->stub_address() + off
;
8575 else if (this->abiversion() >= 2)
8577 Address off
= this->glink_section()->find_global_entry(gsym
);
8578 if (off
!= invalid_address
)
8579 return this->glink_section()->global_entry_address() + off
;
8584 // Return the offset to use for the GOT_INDX'th got entry which is
8585 // for a local tls symbol specified by OBJECT, SYMNDX.
8586 template<int size
, bool big_endian
>
8588 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
8589 const Relobj
* object
,
8590 unsigned int symndx
,
8591 unsigned int got_indx
) const
8593 const Powerpc_relobj
<size
, big_endian
>* ppc_object
8594 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
8595 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
8597 for (Got_type got_type
= GOT_TYPE_TLSGD
;
8598 got_type
<= GOT_TYPE_TPREL
;
8599 got_type
= Got_type(got_type
+ 1))
8600 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
8602 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
8603 if (got_type
== GOT_TYPE_TLSGD
)
8605 if (off
== got_indx
* (size
/ 8))
8607 if (got_type
== GOT_TYPE_TPREL
)
8617 // Return the offset to use for the GOT_INDX'th got entry which is
8618 // for global tls symbol GSYM.
8619 template<int size
, bool big_endian
>
8621 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
8623 unsigned int got_indx
) const
8625 if (gsym
->type() == elfcpp::STT_TLS
)
8627 for (Got_type got_type
= GOT_TYPE_TLSGD
;
8628 got_type
<= GOT_TYPE_TPREL
;
8629 got_type
= Got_type(got_type
+ 1))
8630 if (gsym
->has_got_offset(got_type
))
8632 unsigned int off
= gsym
->got_offset(got_type
);
8633 if (got_type
== GOT_TYPE_TLSGD
)
8635 if (off
== got_indx
* (size
/ 8))
8637 if (got_type
== GOT_TYPE_TPREL
)
8647 // The selector for powerpc object files.
8649 template<int size
, bool big_endian
>
8650 class Target_selector_powerpc
: public Target_selector
8653 Target_selector_powerpc()
8654 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
8657 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
8658 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
8660 ? (big_endian
? "elf64ppc" : "elf64lppc")
8661 : (big_endian
? "elf32ppc" : "elf32lppc")))
8665 do_instantiate_target()
8666 { return new Target_powerpc
<size
, big_endian
>(); }
8669 Target_selector_powerpc
<32, true> target_selector_ppc32
;
8670 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
8671 Target_selector_powerpc
<64, true> target_selector_ppc64
;
8672 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
8674 // Instantiate these constants for -O0
8675 template<int size
, bool big_endian
>
8676 const int Output_data_glink
<size
, big_endian
>::pltresolve_size
;
8677 template<int size
, bool big_endian
>
8678 const typename Output_data_glink
<size
, big_endian
>::Address
8679 Output_data_glink
<size
, big_endian
>::invalid_address
;
8680 template<int size
, bool big_endian
>
8681 const typename Stub_table
<size
, big_endian
>::Address
8682 Stub_table
<size
, big_endian
>::invalid_address
;
8683 template<int size
, bool big_endian
>
8684 const typename Target_powerpc
<size
, big_endian
>::Address
8685 Target_powerpc
<size
, big_endian
>::invalid_address
;
8687 } // End anonymous namespace.