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
,
674 const Relocatable_relocs
*,
676 Address view_address
,
678 unsigned char* reloc_view
,
679 section_size_type reloc_view_size
);
681 // Return whether SYM is defined by the ABI.
683 do_is_defined_by_abi(const Symbol
* sym
) const
685 return strcmp(sym
->name(), "__tls_get_addr") == 0;
688 // Return the size of the GOT section.
692 gold_assert(this->got_
!= NULL
);
693 return this->got_
->data_size();
696 // Get the PLT section.
697 const Output_data_plt_powerpc
<size
, big_endian
>*
700 gold_assert(this->plt_
!= NULL
);
704 // Get the IPLT section.
705 const Output_data_plt_powerpc
<size
, big_endian
>*
708 gold_assert(this->iplt_
!= NULL
);
712 // Get the .glink section.
713 const Output_data_glink
<size
, big_endian
>*
714 glink_section() const
716 gold_assert(this->glink_
!= NULL
);
720 Output_data_glink
<size
, big_endian
>*
723 gold_assert(this->glink_
!= NULL
);
727 bool has_glink() const
728 { return this->glink_
!= NULL
; }
730 // Get the GOT section.
731 const Output_data_got_powerpc
<size
, big_endian
>*
734 gold_assert(this->got_
!= NULL
);
738 // Get the GOT section, creating it if necessary.
739 Output_data_got_powerpc
<size
, big_endian
>*
740 got_section(Symbol_table
*, Layout
*);
743 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
744 const elfcpp::Ehdr
<size
, big_endian
>&);
746 // Return the number of entries in the GOT.
748 got_entry_count() const
750 if (this->got_
== NULL
)
752 return this->got_size() / (size
/ 8);
755 // Return the number of entries in the PLT.
757 plt_entry_count() const;
759 // Return the offset of the first non-reserved PLT entry.
761 first_plt_entry_offset() const
765 if (this->abiversion() >= 2)
770 // Return the size of each PLT entry.
772 plt_entry_size() const
776 if (this->abiversion() >= 2)
781 Output_data_save_res
<size
, big_endian
>*
782 savres_section() const
784 return this->savres_section_
;
787 // Add any special sections for this symbol to the gc work list.
788 // For powerpc64, this adds the code section of a function
791 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
793 // Handle target specific gc actions when adding a gc reference from
794 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
795 // and DST_OFF. For powerpc64, this adds a referenc to the code
796 // section of a function descriptor.
798 do_gc_add_reference(Symbol_table
* symtab
,
800 unsigned int src_shndx
,
802 unsigned int dst_shndx
,
803 Address dst_off
) const;
805 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
808 { return this->stub_tables_
; }
810 const Output_data_brlt_powerpc
<size
, big_endian
>*
812 { return this->brlt_section_
; }
815 add_branch_lookup_table(Address to
)
817 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
818 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
822 find_branch_lookup_table(Address to
)
824 typename
Branch_lookup_table::const_iterator p
825 = this->branch_lookup_table_
.find(to
);
826 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
830 write_branch_lookup_table(unsigned char *oview
)
832 for (typename
Branch_lookup_table::const_iterator p
833 = this->branch_lookup_table_
.begin();
834 p
!= this->branch_lookup_table_
.end();
837 elfcpp::Swap
<size
, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
842 plt_thread_safe() const
843 { return this->plt_thread_safe_
; }
847 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI
; }
850 set_abiversion (int ver
)
852 elfcpp::Elf_Word flags
= this->processor_specific_flags();
853 flags
&= ~elfcpp::EF_PPC64_ABI
;
854 flags
|= ver
& elfcpp::EF_PPC64_ABI
;
855 this->set_processor_specific_flags(flags
);
858 // Offset to to save stack slot
861 { return this->abiversion() < 2 ? 40 : 24; }
877 : tls_get_addr_(NOT_EXPECTED
),
878 relinfo_(NULL
), relnum_(0), r_offset_(0)
883 if (this->tls_get_addr_
!= NOT_EXPECTED
)
890 if (this->relinfo_
!= NULL
)
891 gold_error_at_location(this->relinfo_
, this->relnum_
, this->r_offset_
,
892 _("missing expected __tls_get_addr call"));
896 expect_tls_get_addr_call(
897 const Relocate_info
<size
, big_endian
>* relinfo
,
901 this->tls_get_addr_
= EXPECTED
;
902 this->relinfo_
= relinfo
;
903 this->relnum_
= relnum
;
904 this->r_offset_
= r_offset
;
908 expect_tls_get_addr_call()
909 { this->tls_get_addr_
= EXPECTED
; }
912 skip_next_tls_get_addr_call()
913 {this->tls_get_addr_
= SKIP
; }
916 maybe_skip_tls_get_addr_call(unsigned int r_type
, const Symbol
* gsym
)
918 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
919 || r_type
== elfcpp::R_PPC_PLTREL24
)
921 && strcmp(gsym
->name(), "__tls_get_addr") == 0);
922 Tls_get_addr last_tls
= this->tls_get_addr_
;
923 this->tls_get_addr_
= NOT_EXPECTED
;
924 if (is_tls_call
&& last_tls
!= EXPECTED
)
926 else if (!is_tls_call
&& last_tls
!= NOT_EXPECTED
)
935 // What we're up to regarding calls to __tls_get_addr.
936 // On powerpc, the branch and link insn making a call to
937 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
938 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
939 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
940 // The marker relocation always comes first, and has the same
941 // symbol as the reloc on the insn setting up the __tls_get_addr
942 // argument. This ties the arg setup insn with the call insn,
943 // allowing ld to safely optimize away the call. We check that
944 // every call to __tls_get_addr has a marker relocation, and that
945 // every marker relocation is on a call to __tls_get_addr.
946 Tls_get_addr tls_get_addr_
;
947 // Info about the last reloc for error message.
948 const Relocate_info
<size
, big_endian
>* relinfo_
;
953 // The class which scans relocations.
954 class Scan
: protected Track_tls
957 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
960 : Track_tls(), issued_non_pic_error_(false)
964 get_reference_flags(unsigned int r_type
, const Target_powerpc
* target
);
967 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
968 Sized_relobj_file
<size
, big_endian
>* object
,
969 unsigned int data_shndx
,
970 Output_section
* output_section
,
971 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
972 const elfcpp::Sym
<size
, big_endian
>& lsym
,
976 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
977 Sized_relobj_file
<size
, big_endian
>* object
,
978 unsigned int data_shndx
,
979 Output_section
* output_section
,
980 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
984 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
986 Sized_relobj_file
<size
, big_endian
>* relobj
,
989 const elfcpp::Rela
<size
, big_endian
>& ,
991 const elfcpp::Sym
<size
, big_endian
>&)
993 // PowerPC64 .opd is not folded, so any identical function text
994 // may be folded and we'll still keep function addresses distinct.
995 // That means no reloc is of concern here.
998 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
999 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1000 if (ppcobj
->abiversion() == 1)
1003 // For 32-bit and ELFv2, conservatively assume anything but calls to
1004 // function code might be taking the address of the function.
1005 return !is_branch_reloc(r_type
);
1009 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
1011 Sized_relobj_file
<size
, big_endian
>* relobj
,
1014 const elfcpp::Rela
<size
, big_endian
>& ,
1015 unsigned int r_type
,
1021 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
1022 <Powerpc_relobj
<size
, big_endian
>*>(relobj
);
1023 if (ppcobj
->abiversion() == 1)
1026 return !is_branch_reloc(r_type
);
1030 reloc_needs_plt_for_ifunc(Target_powerpc
<size
, big_endian
>* target
,
1031 Sized_relobj_file
<size
, big_endian
>* object
,
1032 unsigned int r_type
, bool report_err
);
1036 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
1037 unsigned int r_type
);
1040 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
1041 unsigned int r_type
, Symbol
*);
1044 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
1045 Target_powerpc
* target
);
1048 check_non_pic(Relobj
*, unsigned int r_type
);
1050 // Whether we have issued an error about a non-PIC compilation.
1051 bool issued_non_pic_error_
;
1055 symval_for_branch(const Symbol_table
* symtab
,
1056 const Sized_symbol
<size
>* gsym
,
1057 Powerpc_relobj
<size
, big_endian
>* object
,
1058 Address
*value
, unsigned int *dest_shndx
);
1060 // The class which implements relocation.
1061 class Relocate
: protected Track_tls
1064 // Use 'at' branch hints when true, 'y' when false.
1065 // FIXME maybe: set this with an option.
1066 static const bool is_isa_v2
= true;
1072 // Do a relocation. Return false if the caller should not issue
1073 // any warnings about this relocation.
1075 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
1076 Output_section
*, size_t relnum
,
1077 const elfcpp::Rela
<size
, big_endian
>&,
1078 unsigned int r_type
, const Sized_symbol
<size
>*,
1079 const Symbol_value
<size
>*,
1081 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1085 class Relocate_comdat_behavior
1088 // Decide what the linker should do for relocations that refer to
1089 // discarded comdat sections.
1090 inline Comdat_behavior
1091 get(const char* name
)
1093 gold::Default_comdat_behavior default_behavior
;
1094 Comdat_behavior ret
= default_behavior
.get(name
);
1095 if (ret
== CB_WARNING
)
1098 && (strcmp(name
, ".fixup") == 0
1099 || strcmp(name
, ".got2") == 0))
1102 && (strcmp(name
, ".opd") == 0
1103 || strcmp(name
, ".toc") == 0
1104 || strcmp(name
, ".toc1") == 0))
1111 // A class which returns the size required for a relocation type,
1112 // used while scanning relocs during a relocatable link.
1113 class Relocatable_size_for_reloc
1117 get_size_for_reloc(unsigned int, Relobj
*)
1124 // Optimize the TLS relocation type based on what we know about the
1125 // symbol. IS_FINAL is true if the final address of this symbol is
1126 // known at link time.
1128 tls::Tls_optimization
1129 optimize_tls_gd(bool is_final
)
1131 // If we are generating a shared library, then we can't do anything
1133 if (parameters
->options().shared())
1134 return tls::TLSOPT_NONE
;
1137 return tls::TLSOPT_TO_IE
;
1138 return tls::TLSOPT_TO_LE
;
1141 tls::Tls_optimization
1144 if (parameters
->options().shared())
1145 return tls::TLSOPT_NONE
;
1147 return tls::TLSOPT_TO_LE
;
1150 tls::Tls_optimization
1151 optimize_tls_ie(bool is_final
)
1153 if (!is_final
|| parameters
->options().shared())
1154 return tls::TLSOPT_NONE
;
1156 return tls::TLSOPT_TO_LE
;
1161 make_glink_section(Layout
*);
1163 // Create the PLT section.
1165 make_plt_section(Symbol_table
*, Layout
*);
1168 make_iplt_section(Symbol_table
*, Layout
*);
1171 make_brlt_section(Layout
*);
1173 // Create a PLT entry for a global symbol.
1175 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1177 // Create a PLT entry for a local IFUNC symbol.
1179 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1180 Sized_relobj_file
<size
, big_endian
>*,
1184 // Create a GOT entry for local dynamic __tls_get_addr.
1186 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
1187 Sized_relobj_file
<size
, big_endian
>* object
);
1190 tlsld_got_offset() const
1192 return this->tlsld_got_offset_
;
1195 // Get the dynamic reloc section, creating it if necessary.
1197 rela_dyn_section(Layout
*);
1199 // Similarly, but for ifunc symbols get the one for ifunc.
1201 rela_dyn_section(Symbol_table
*, Layout
*, bool for_ifunc
);
1203 // Copy a relocation against a global symbol.
1205 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1206 Sized_relobj_file
<size
, big_endian
>* object
,
1207 unsigned int shndx
, Output_section
* output_section
,
1208 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
1210 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1211 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1212 symtab
->get_sized_symbol
<size
>(sym
),
1213 object
, shndx
, output_section
,
1214 r_type
, reloc
.get_r_offset(),
1215 reloc
.get_r_addend(),
1216 this->rela_dyn_section(layout
));
1219 // Look over all the input sections, deciding where to place stubs.
1221 group_sections(Layout
*, const Task
*, bool);
1223 // Sort output sections by address.
1224 struct Sort_sections
1227 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
1228 { return sec1
->address() < sec2
->address(); }
1234 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1235 unsigned int data_shndx
,
1237 unsigned int r_type
,
1240 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
1241 r_type_(r_type
), r_sym_(r_sym
), addend_(addend
)
1247 // If this branch needs a plt call stub, or a long branch stub, make one.
1249 make_stub(Stub_table
<size
, big_endian
>*,
1250 Stub_table
<size
, big_endian
>*,
1251 Symbol_table
*) const;
1254 // The branch location..
1255 Powerpc_relobj
<size
, big_endian
>* object_
;
1256 unsigned int shndx_
;
1258 // ..and the branch type and destination.
1259 unsigned int r_type_
;
1260 unsigned int r_sym_
;
1264 // Information about this specific target which we pass to the
1265 // general Target structure.
1266 static Target::Target_info powerpc_info
;
1268 // The types of GOT entries needed for this platform.
1269 // These values are exposed to the ABI in an incremental link.
1270 // Do not renumber existing values without changing the version
1271 // number of the .gnu_incremental_inputs section.
1275 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
1276 GOT_TYPE_DTPREL
, // entry for @got@dtprel
1277 GOT_TYPE_TPREL
// entry for @got@tprel
1281 Output_data_got_powerpc
<size
, big_endian
>* got_
;
1282 // The PLT section. This is a container for a table of addresses,
1283 // and their relocations. Each address in the PLT has a dynamic
1284 // relocation (R_*_JMP_SLOT) and each address will have a
1285 // corresponding entry in .glink for lazy resolution of the PLT.
1286 // ppc32 initialises the PLT to point at the .glink entry, while
1287 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1288 // linker adds a stub that loads the PLT entry into ctr then
1289 // branches to ctr. There may be more than one stub for each PLT
1290 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1291 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1292 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
1293 // The IPLT section. Like plt_, this is a container for a table of
1294 // addresses and their relocations, specifically for STT_GNU_IFUNC
1295 // functions that resolve locally (STT_GNU_IFUNC functions that
1296 // don't resolve locally go in PLT). Unlike plt_, these have no
1297 // entry in .glink for lazy resolution, and the relocation section
1298 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1299 // the relocation section may contain relocations against
1300 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1301 // relocation section will appear at the end of other dynamic
1302 // relocations, so that ld.so applies these relocations after other
1303 // dynamic relocations. In a static executable, the relocation
1304 // section is emitted and marked with __rela_iplt_start and
1305 // __rela_iplt_end symbols.
1306 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
1307 // Section holding long branch destinations.
1308 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
1309 // The .glink section.
1310 Output_data_glink
<size
, big_endian
>* glink_
;
1311 // The dynamic reloc section.
1312 Reloc_section
* rela_dyn_
;
1313 // Relocs saved to avoid a COPY reloc.
1314 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
1315 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1316 unsigned int tlsld_got_offset_
;
1318 Stub_tables stub_tables_
;
1319 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
1320 Branch_lookup_table branch_lookup_table_
;
1322 typedef std::vector
<Branch_info
> Branches
;
1323 Branches branch_info_
;
1325 bool plt_thread_safe_
;
1328 int relax_fail_count_
;
1329 int32_t stub_group_size_
;
1331 Output_data_save_res
<size
, big_endian
> *savres_section_
;
1335 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
1338 true, // is_big_endian
1339 elfcpp::EM_PPC
, // machine_code
1340 false, // has_make_symbol
1341 false, // has_resolve
1342 false, // has_code_fill
1343 true, // is_default_stack_executable
1344 false, // can_icf_inline_merge_sections
1346 "/usr/lib/ld.so.1", // dynamic_linker
1347 0x10000000, // default_text_segment_address
1348 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1349 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1350 false, // isolate_execinstr
1352 elfcpp::SHN_UNDEF
, // small_common_shndx
1353 elfcpp::SHN_UNDEF
, // large_common_shndx
1354 0, // small_common_section_flags
1355 0, // large_common_section_flags
1356 NULL
, // attributes_section
1357 NULL
, // attributes_vendor
1358 "_start", // entry_symbol_name
1359 32, // hash_entry_size
1363 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
1366 false, // is_big_endian
1367 elfcpp::EM_PPC
, // machine_code
1368 false, // has_make_symbol
1369 false, // has_resolve
1370 false, // has_code_fill
1371 true, // is_default_stack_executable
1372 false, // can_icf_inline_merge_sections
1374 "/usr/lib/ld.so.1", // dynamic_linker
1375 0x10000000, // default_text_segment_address
1376 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1377 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1378 false, // isolate_execinstr
1380 elfcpp::SHN_UNDEF
, // small_common_shndx
1381 elfcpp::SHN_UNDEF
, // large_common_shndx
1382 0, // small_common_section_flags
1383 0, // large_common_section_flags
1384 NULL
, // attributes_section
1385 NULL
, // attributes_vendor
1386 "_start", // entry_symbol_name
1387 32, // hash_entry_size
1391 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1394 true, // is_big_endian
1395 elfcpp::EM_PPC64
, // machine_code
1396 false, // has_make_symbol
1397 false, // has_resolve
1398 false, // has_code_fill
1399 true, // is_default_stack_executable
1400 false, // can_icf_inline_merge_sections
1402 "/usr/lib/ld.so.1", // dynamic_linker
1403 0x10000000, // default_text_segment_address
1404 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1405 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1406 false, // isolate_execinstr
1408 elfcpp::SHN_UNDEF
, // small_common_shndx
1409 elfcpp::SHN_UNDEF
, // large_common_shndx
1410 0, // small_common_section_flags
1411 0, // large_common_section_flags
1412 NULL
, // attributes_section
1413 NULL
, // attributes_vendor
1414 "_start", // entry_symbol_name
1415 32, // hash_entry_size
1419 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1422 false, // is_big_endian
1423 elfcpp::EM_PPC64
, // machine_code
1424 false, // has_make_symbol
1425 false, // has_resolve
1426 false, // has_code_fill
1427 true, // is_default_stack_executable
1428 false, // can_icf_inline_merge_sections
1430 "/usr/lib/ld.so.1", // dynamic_linker
1431 0x10000000, // default_text_segment_address
1432 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1433 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1434 false, // isolate_execinstr
1436 elfcpp::SHN_UNDEF
, // small_common_shndx
1437 elfcpp::SHN_UNDEF
, // large_common_shndx
1438 0, // small_common_section_flags
1439 0, // large_common_section_flags
1440 NULL
, // attributes_section
1441 NULL
, // attributes_vendor
1442 "_start", // entry_symbol_name
1443 32, // hash_entry_size
1447 is_branch_reloc(unsigned int r_type
)
1449 return (r_type
== elfcpp::R_POWERPC_REL24
1450 || r_type
== elfcpp::R_PPC_PLTREL24
1451 || r_type
== elfcpp::R_PPC_LOCAL24PC
1452 || r_type
== elfcpp::R_POWERPC_REL14
1453 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1454 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1455 || r_type
== elfcpp::R_POWERPC_ADDR24
1456 || r_type
== elfcpp::R_POWERPC_ADDR14
1457 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1458 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1461 // If INSN is an opcode that may be used with an @tls operand, return
1462 // the transformed insn for TLS optimisation, otherwise return 0. If
1463 // REG is non-zero only match an insn with RB or RA equal to REG.
1465 at_tls_transform(uint32_t insn
, unsigned int reg
)
1467 if ((insn
& (0x3f << 26)) != 31 << 26)
1471 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1472 rtra
= insn
& ((1 << 26) - (1 << 16));
1473 else if (((insn
>> 16) & 0x1f) == reg
)
1474 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1478 if ((insn
& (0x3ff << 1)) == 266 << 1)
1481 else if ((insn
& (0x1f << 1)) == 23 << 1
1482 && ((insn
& (0x1f << 6)) < 14 << 6
1483 || ((insn
& (0x1f << 6)) >= 16 << 6
1484 && (insn
& (0x1f << 6)) < 24 << 6)))
1485 // load and store indexed -> dform
1486 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1487 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1488 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1489 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1490 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1492 insn
= (58 << 26) | 2;
1500 template<int size
, bool big_endian
>
1501 class Powerpc_relocate_functions
1521 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1522 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1523 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword SignedAddress
;
1525 template<int valsize
>
1527 has_overflow_signed(Address value
)
1529 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1530 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1531 limit
<<= ((valsize
- 1) >> 1);
1532 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1533 return value
+ limit
> (limit
<< 1) - 1;
1536 template<int valsize
>
1538 has_overflow_unsigned(Address value
)
1540 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1541 limit
<<= ((valsize
- 1) >> 1);
1542 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1543 return value
> (limit
<< 1) - 1;
1546 template<int valsize
>
1548 has_overflow_bitfield(Address value
)
1550 return (has_overflow_unsigned
<valsize
>(value
)
1551 && has_overflow_signed
<valsize
>(value
));
1554 template<int valsize
>
1555 static inline Status
1556 overflowed(Address value
, Overflow_check overflow
)
1558 if (overflow
== CHECK_SIGNED
)
1560 if (has_overflow_signed
<valsize
>(value
))
1561 return STATUS_OVERFLOW
;
1563 else if (overflow
== CHECK_UNSIGNED
)
1565 if (has_overflow_unsigned
<valsize
>(value
))
1566 return STATUS_OVERFLOW
;
1568 else if (overflow
== CHECK_BITFIELD
)
1570 if (has_overflow_bitfield
<valsize
>(value
))
1571 return STATUS_OVERFLOW
;
1576 // Do a simple RELA relocation
1577 template<int fieldsize
, int valsize
>
1578 static inline Status
1579 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1581 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1582 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1583 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, value
);
1584 return overflowed
<valsize
>(value
, overflow
);
1587 template<int fieldsize
, int valsize
>
1588 static inline Status
1589 rela(unsigned char* view
,
1590 unsigned int right_shift
,
1591 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1593 Overflow_check overflow
)
1595 typedef typename
elfcpp::Swap
<fieldsize
, big_endian
>::Valtype Valtype
;
1596 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1597 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(wv
);
1598 Valtype reloc
= value
>> right_shift
;
1601 elfcpp::Swap
<fieldsize
, big_endian
>::writeval(wv
, val
| reloc
);
1602 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1605 // Do a simple RELA relocation, unaligned.
1606 template<int fieldsize
, int valsize
>
1607 static inline Status
1608 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1610 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, value
);
1611 return overflowed
<valsize
>(value
, overflow
);
1614 template<int fieldsize
, int valsize
>
1615 static inline Status
1616 rela_ua(unsigned char* view
,
1617 unsigned int right_shift
,
1618 typename
elfcpp::Valtype_base
<fieldsize
>::Valtype dst_mask
,
1620 Overflow_check overflow
)
1622 typedef typename
elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::Valtype
1624 Valtype val
= elfcpp::Swap
<fieldsize
, big_endian
>::readval(view
);
1625 Valtype reloc
= value
>> right_shift
;
1628 elfcpp::Swap_unaligned
<fieldsize
, big_endian
>::writeval(view
, val
| reloc
);
1629 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1633 // R_PPC64_ADDR64: (Symbol + Addend)
1635 addr64(unsigned char* view
, Address value
)
1636 { This::template rela
<64,64>(view
, value
, CHECK_NONE
); }
1638 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1640 addr64_u(unsigned char* view
, Address value
)
1641 { This::template rela_ua
<64,64>(view
, value
, CHECK_NONE
); }
1643 // R_POWERPC_ADDR32: (Symbol + Addend)
1644 static inline Status
1645 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
1646 { return This::template rela
<32,32>(view
, value
, overflow
); }
1648 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1649 static inline Status
1650 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1651 { return This::template rela_ua
<32,32>(view
, value
, overflow
); }
1653 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1654 static inline Status
1655 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
1657 Status stat
= This::template rela
<32,26>(view
, 0, 0x03fffffc,
1659 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1660 stat
= STATUS_OVERFLOW
;
1664 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1665 static inline Status
1666 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
1667 { return This::template rela
<16,16>(view
, value
, overflow
); }
1669 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1670 static inline Status
1671 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1672 { return This::template rela_ua
<16,16>(view
, value
, overflow
); }
1674 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1675 static inline Status
1676 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
1678 Status stat
= This::template rela
<16,16>(view
, 0, 0xfffc, value
, overflow
);
1679 if ((value
& 3) != 0)
1680 stat
= STATUS_OVERFLOW
;
1684 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
1685 static inline Status
1686 addr16_dq(unsigned char* view
, Address value
, Overflow_check overflow
)
1688 Status stat
= This::template rela
<16,16>(view
, 0, 0xfff0, value
, overflow
);
1689 if ((value
& 15) != 0)
1690 stat
= STATUS_OVERFLOW
;
1694 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1696 addr16_hi(unsigned char* view
, Address value
)
1697 { This::template rela
<16,16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
1699 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1701 addr16_ha(unsigned char* view
, Address value
)
1702 { This::addr16_hi(view
, value
+ 0x8000); }
1704 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1706 addr16_hi2(unsigned char* view
, Address value
)
1707 { This::template rela
<16,16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
1709 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1711 addr16_ha2(unsigned char* view
, Address value
)
1712 { This::addr16_hi2(view
, value
+ 0x8000); }
1714 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1716 addr16_hi3(unsigned char* view
, Address value
)
1717 { This::template rela
<16,16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
1719 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1721 addr16_ha3(unsigned char* view
, Address value
)
1722 { This::addr16_hi3(view
, value
+ 0x8000); }
1724 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1725 static inline Status
1726 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
1728 Status stat
= This::template rela
<32,16>(view
, 0, 0xfffc, value
, overflow
);
1729 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1730 stat
= STATUS_OVERFLOW
;
1734 // R_POWERPC_REL16DX_HA
1735 static inline Status
1736 addr16dx_ha(unsigned char *view
, Address value
, Overflow_check overflow
)
1738 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
1739 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1740 Valtype val
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
1742 value
= static_cast<SignedAddress
>(value
) >> 16;
1743 val
|= (value
& 0xffc1) | ((value
& 0x3e) << 15);
1744 elfcpp::Swap
<32, big_endian
>::writeval(wv
, val
);
1745 return overflowed
<16>(value
, overflow
);
1749 // Set ABI version for input and output.
1751 template<int size
, bool big_endian
>
1753 Powerpc_relobj
<size
, big_endian
>::set_abiversion(int ver
)
1755 this->e_flags_
|= ver
;
1756 if (this->abiversion() != 0)
1758 Target_powerpc
<size
, big_endian
>* target
=
1759 static_cast<Target_powerpc
<size
, big_endian
>*>(
1760 parameters
->sized_target
<size
, big_endian
>());
1761 if (target
->abiversion() == 0)
1762 target
->set_abiversion(this->abiversion());
1763 else if (target
->abiversion() != this->abiversion())
1764 gold_error(_("%s: ABI version %d is not compatible "
1765 "with ABI version %d output"),
1766 this->name().c_str(),
1767 this->abiversion(), target
->abiversion());
1772 // Stash away the index of .got2 or .opd in a relocatable object, if
1773 // such a section exists.
1775 template<int size
, bool big_endian
>
1777 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
1778 Read_symbols_data
* sd
)
1780 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1781 const unsigned char* namesu
= sd
->section_names
->data();
1782 const char* names
= reinterpret_cast<const char*>(namesu
);
1783 section_size_type names_size
= sd
->section_names_size
;
1784 const unsigned char* s
;
1786 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
,
1787 size
== 32 ? ".got2" : ".opd",
1788 names
, names_size
, NULL
);
1791 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
1792 this->special_
= ndx
;
1795 if (this->abiversion() == 0)
1796 this->set_abiversion(1);
1797 else if (this->abiversion() > 1)
1798 gold_error(_("%s: .opd invalid in abiv%d"),
1799 this->name().c_str(), this->abiversion());
1802 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
1805 // Examine .rela.opd to build info about function entry points.
1807 template<int size
, bool big_endian
>
1809 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
1811 const unsigned char* prelocs
,
1812 const unsigned char* plocal_syms
)
1816 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
1818 const int reloc_size
1819 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
1820 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1821 Address expected_off
= 0;
1822 bool regular
= true;
1823 unsigned int opd_ent_size
= 0;
1825 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
1827 Reltype
reloc(prelocs
);
1828 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
1829 = reloc
.get_r_info();
1830 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
1831 if (r_type
== elfcpp::R_PPC64_ADDR64
)
1833 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1834 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
1837 if (r_sym
< this->local_symbol_count())
1839 typename
elfcpp::Sym
<size
, big_endian
>
1840 lsym(plocal_syms
+ r_sym
* sym_size
);
1841 shndx
= lsym
.get_st_shndx();
1842 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1843 value
= lsym
.get_st_value();
1846 shndx
= this->symbol_section_and_value(r_sym
, &value
,
1848 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
1849 value
+ reloc
.get_r_addend());
1852 expected_off
= reloc
.get_r_offset();
1853 opd_ent_size
= expected_off
;
1855 else if (expected_off
!= reloc
.get_r_offset())
1857 expected_off
+= opd_ent_size
;
1859 else if (r_type
== elfcpp::R_PPC64_TOC
)
1861 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
1866 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1867 this->name().c_str(), r_type
);
1871 if (reloc_count
<= 2)
1872 opd_ent_size
= this->section_size(this->opd_shndx());
1873 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
1877 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1878 this->name().c_str());
1884 template<int size
, bool big_endian
>
1886 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
1888 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
1891 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
1892 p
!= rd
->relocs
.end();
1895 if (p
->data_shndx
== this->opd_shndx())
1897 uint64_t opd_size
= this->section_size(this->opd_shndx());
1898 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
1901 this->init_opd(opd_size
);
1902 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
1903 rd
->local_symbols
->data());
1911 // Read the symbols then set up st_other vector.
1913 template<int size
, bool big_endian
>
1915 Powerpc_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
1917 this->base_read_symbols(sd
);
1920 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
1921 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1922 const unsigned int loccount
= this->do_local_symbol_count();
1925 this->st_other_
.resize(loccount
);
1926 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1927 off_t locsize
= loccount
* sym_size
;
1928 const unsigned int symtab_shndx
= this->symtab_shndx();
1929 const unsigned char *psymtab
= pshdrs
+ symtab_shndx
* shdr_size
;
1930 typename
elfcpp::Shdr
<size
, big_endian
> shdr(psymtab
);
1931 const unsigned char* psyms
= this->get_view(shdr
.get_sh_offset(),
1932 locsize
, true, false);
1934 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
1936 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
1937 unsigned char st_other
= sym
.get_st_other();
1938 this->st_other_
[i
] = st_other
;
1939 if ((st_other
& elfcpp::STO_PPC64_LOCAL_MASK
) != 0)
1941 if (this->abiversion() == 0)
1942 this->set_abiversion(2);
1943 else if (this->abiversion() < 2)
1944 gold_error(_("%s: local symbol %d has invalid st_other"
1945 " for ABI version 1"),
1946 this->name().c_str(), i
);
1953 template<int size
, bool big_endian
>
1955 Powerpc_dynobj
<size
, big_endian
>::set_abiversion(int ver
)
1957 this->e_flags_
|= ver
;
1958 if (this->abiversion() != 0)
1960 Target_powerpc
<size
, big_endian
>* target
=
1961 static_cast<Target_powerpc
<size
, big_endian
>*>(
1962 parameters
->sized_target
<size
, big_endian
>());
1963 if (target
->abiversion() == 0)
1964 target
->set_abiversion(this->abiversion());
1965 else if (target
->abiversion() != this->abiversion())
1966 gold_error(_("%s: ABI version %d is not compatible "
1967 "with ABI version %d output"),
1968 this->name().c_str(),
1969 this->abiversion(), target
->abiversion());
1974 // Call Sized_dynobj::base_read_symbols to read the symbols then
1975 // read .opd from a dynamic object, filling in opd_ent_ vector,
1977 template<int size
, bool big_endian
>
1979 Powerpc_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
1981 this->base_read_symbols(sd
);
1984 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
1985 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1986 const unsigned char* namesu
= sd
->section_names
->data();
1987 const char* names
= reinterpret_cast<const char*>(namesu
);
1988 const unsigned char* s
= NULL
;
1989 const unsigned char* opd
;
1990 section_size_type opd_size
;
1992 // Find and read .opd section.
1995 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".opd", names
,
1996 sd
->section_names_size
,
2001 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2002 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2003 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
2005 if (this->abiversion() == 0)
2006 this->set_abiversion(1);
2007 else if (this->abiversion() > 1)
2008 gold_error(_("%s: .opd invalid in abiv%d"),
2009 this->name().c_str(), this->abiversion());
2011 this->opd_shndx_
= (s
- pshdrs
) / shdr_size
;
2012 this->opd_address_
= shdr
.get_sh_addr();
2013 opd_size
= convert_to_section_size_type(shdr
.get_sh_size());
2014 opd
= this->get_view(shdr
.get_sh_offset(), opd_size
,
2020 // Build set of executable sections.
2021 // Using a set is probably overkill. There is likely to be only
2022 // a few executable sections, typically .init, .text and .fini,
2023 // and they are generally grouped together.
2024 typedef std::set
<Sec_info
> Exec_sections
;
2025 Exec_sections exec_sections
;
2027 for (unsigned int i
= 1; i
< this->shnum(); ++i
, s
+= shdr_size
)
2029 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
2030 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
2031 && ((shdr
.get_sh_flags()
2032 & (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2033 == (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
2034 && shdr
.get_sh_size() != 0)
2036 exec_sections
.insert(Sec_info(shdr
.get_sh_addr(),
2037 shdr
.get_sh_size(), i
));
2040 if (exec_sections
.empty())
2043 // Look over the OPD entries. This is complicated by the fact
2044 // that some binaries will use two-word entries while others
2045 // will use the standard three-word entries. In most cases
2046 // the third word (the environment pointer for languages like
2047 // Pascal) is unused and will be zero. If the third word is
2048 // used it should not be pointing into executable sections,
2050 this->init_opd(opd_size
);
2051 for (const unsigned char* p
= opd
; p
< opd
+ opd_size
; p
+= 8)
2053 typedef typename
elfcpp::Swap
<64, big_endian
>::Valtype Valtype
;
2054 const Valtype
* valp
= reinterpret_cast<const Valtype
*>(p
);
2055 Valtype val
= elfcpp::Swap
<64, big_endian
>::readval(valp
);
2057 // Chances are that this is the third word of an OPD entry.
2059 typename
Exec_sections::const_iterator e
2060 = exec_sections
.upper_bound(Sec_info(val
, 0, 0));
2061 if (e
!= exec_sections
.begin())
2064 if (e
->start
<= val
&& val
< e
->start
+ e
->len
)
2066 // We have an address in an executable section.
2067 // VAL ought to be the function entry, set it up.
2068 this->set_opd_ent(p
- opd
, e
->shndx
, val
);
2069 // Skip second word of OPD entry, the TOC pointer.
2073 // If we didn't match any executable sections, we likely
2074 // have a non-zero third word in the OPD entry.
2079 // Set up some symbols.
2081 template<int size
, bool big_endian
>
2083 Target_powerpc
<size
, big_endian
>::do_define_standard_symbols(
2084 Symbol_table
* symtab
,
2089 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2090 // undefined when scanning relocs (and thus requires
2091 // non-relative dynamic relocs). The proper value will be
2093 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2094 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2096 Target_powerpc
<size
, big_endian
>* target
=
2097 static_cast<Target_powerpc
<size
, big_endian
>*>(
2098 parameters
->sized_target
<size
, big_endian
>());
2099 Output_data_got_powerpc
<size
, big_endian
>* got
2100 = target
->got_section(symtab
, layout
);
2101 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2102 Symbol_table::PREDEFINED
,
2106 elfcpp::STV_HIDDEN
, 0,
2110 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2111 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
2112 if (sdasym
!= NULL
&& sdasym
->is_undefined())
2114 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
2116 = layout
->add_output_section_data(".sdata", 0,
2118 | elfcpp::SHF_WRITE
,
2119 sdata
, ORDER_SMALL_DATA
, false);
2120 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
2121 Symbol_table::PREDEFINED
,
2122 os
, 32768, 0, elfcpp::STT_OBJECT
,
2123 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
2129 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2130 Symbol
*gotsym
= symtab
->lookup(".TOC.", NULL
);
2131 if (gotsym
!= NULL
&& gotsym
->is_undefined())
2133 Target_powerpc
<size
, big_endian
>* target
=
2134 static_cast<Target_powerpc
<size
, big_endian
>*>(
2135 parameters
->sized_target
<size
, big_endian
>());
2136 Output_data_got_powerpc
<size
, big_endian
>* got
2137 = target
->got_section(symtab
, layout
);
2138 symtab
->define_in_output_data(".TOC.", NULL
,
2139 Symbol_table::PREDEFINED
,
2143 elfcpp::STV_HIDDEN
, 0,
2149 // Set up PowerPC target specific relobj.
2151 template<int size
, bool big_endian
>
2153 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
2154 const std::string
& name
,
2155 Input_file
* input_file
,
2156 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
2158 int et
= ehdr
.get_e_type();
2159 // ET_EXEC files are valid input for --just-symbols/-R,
2160 // and we treat them as relocatable objects.
2161 if (et
== elfcpp::ET_REL
2162 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
2164 Powerpc_relobj
<size
, big_endian
>* obj
=
2165 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2169 else if (et
== elfcpp::ET_DYN
)
2171 Powerpc_dynobj
<size
, big_endian
>* obj
=
2172 new Powerpc_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
2178 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
2183 template<int size
, bool big_endian
>
2184 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
2187 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
2188 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
2190 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
2191 : Output_data_got
<size
, big_endian
>(),
2192 symtab_(symtab
), layout_(layout
),
2193 header_ent_cnt_(size
== 32 ? 3 : 1),
2194 header_index_(size
== 32 ? 0x2000 : 0)
2197 this->set_addralign(256);
2200 // Override all the Output_data_got methods we use so as to first call
2203 add_global(Symbol
* gsym
, unsigned int got_type
)
2205 this->reserve_ent();
2206 return Output_data_got
<size
, big_endian
>::add_global(gsym
, got_type
);
2210 add_global_plt(Symbol
* gsym
, unsigned int got_type
)
2212 this->reserve_ent();
2213 return Output_data_got
<size
, big_endian
>::add_global_plt(gsym
, got_type
);
2217 add_global_tls(Symbol
* gsym
, unsigned int got_type
)
2218 { return this->add_global_plt(gsym
, got_type
); }
2221 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
2222 Output_data_reloc_generic
* rel_dyn
, unsigned int r_type
)
2224 this->reserve_ent();
2225 Output_data_got
<size
, big_endian
>::
2226 add_global_with_rel(gsym
, got_type
, rel_dyn
, r_type
);
2230 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
2231 Output_data_reloc_generic
* rel_dyn
,
2232 unsigned int r_type_1
, unsigned int r_type_2
)
2234 this->reserve_ent(2);
2235 Output_data_got
<size
, big_endian
>::
2236 add_global_pair_with_rel(gsym
, got_type
, rel_dyn
, r_type_1
, r_type_2
);
2240 add_local(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2242 this->reserve_ent();
2243 return Output_data_got
<size
, big_endian
>::add_local(object
, sym_index
,
2248 add_local_plt(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2250 this->reserve_ent();
2251 return Output_data_got
<size
, big_endian
>::add_local_plt(object
, sym_index
,
2256 add_local_tls(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2257 { return this->add_local_plt(object
, sym_index
, got_type
); }
2260 add_local_tls_pair(Relobj
* object
, unsigned int sym_index
,
2261 unsigned int got_type
,
2262 Output_data_reloc_generic
* rel_dyn
,
2263 unsigned int r_type
)
2265 this->reserve_ent(2);
2266 Output_data_got
<size
, big_endian
>::
2267 add_local_tls_pair(object
, sym_index
, got_type
, rel_dyn
, r_type
);
2271 add_constant(Valtype constant
)
2273 this->reserve_ent();
2274 return Output_data_got
<size
, big_endian
>::add_constant(constant
);
2278 add_constant_pair(Valtype c1
, Valtype c2
)
2280 this->reserve_ent(2);
2281 return Output_data_got
<size
, big_endian
>::add_constant_pair(c1
, c2
);
2284 // Offset of _GLOBAL_OFFSET_TABLE_.
2288 return this->got_offset(this->header_index_
);
2291 // Offset of base used to access the GOT/TOC.
2292 // The got/toc pointer reg will be set to this value.
2294 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
2297 return this->g_o_t();
2299 return (this->output_section()->address()
2300 + object
->toc_base_offset()
2304 // Ensure our GOT has a header.
2306 set_final_data_size()
2308 if (this->header_ent_cnt_
!= 0)
2309 this->make_header();
2310 Output_data_got
<size
, big_endian
>::set_final_data_size();
2313 // First word of GOT header needs some values that are not
2314 // handled by Output_data_got so poke them in here.
2315 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2317 do_write(Output_file
* of
)
2320 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
2321 val
= this->layout_
->dynamic_section()->address();
2323 val
= this->output_section()->address() + 0x8000;
2324 this->replace_constant(this->header_index_
, val
);
2325 Output_data_got
<size
, big_endian
>::do_write(of
);
2330 reserve_ent(unsigned int cnt
= 1)
2332 if (this->header_ent_cnt_
== 0)
2334 if (this->num_entries() + cnt
> this->header_index_
)
2335 this->make_header();
2341 this->header_ent_cnt_
= 0;
2342 this->header_index_
= this->num_entries();
2345 Output_data_got
<size
, big_endian
>::add_constant(0);
2346 Output_data_got
<size
, big_endian
>::add_constant(0);
2347 Output_data_got
<size
, big_endian
>::add_constant(0);
2349 // Define _GLOBAL_OFFSET_TABLE_ at the header
2350 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2353 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
2354 sym
->set_value(this->g_o_t());
2357 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2358 Symbol_table::PREDEFINED
,
2359 this, this->g_o_t(), 0,
2362 elfcpp::STV_HIDDEN
, 0,
2366 Output_data_got
<size
, big_endian
>::add_constant(0);
2369 // Stashed pointers.
2370 Symbol_table
* symtab_
;
2374 unsigned int header_ent_cnt_
;
2375 // GOT header index.
2376 unsigned int header_index_
;
2379 // Get the GOT section, creating it if necessary.
2381 template<int size
, bool big_endian
>
2382 Output_data_got_powerpc
<size
, big_endian
>*
2383 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
2386 if (this->got_
== NULL
)
2388 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2391 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
2393 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2394 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2395 this->got_
, ORDER_DATA
, false);
2401 // Get the dynamic reloc section, creating it if necessary.
2403 template<int size
, bool big_endian
>
2404 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2405 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
2407 if (this->rela_dyn_
== NULL
)
2409 gold_assert(layout
!= NULL
);
2410 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
2411 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
2412 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
2413 ORDER_DYNAMIC_RELOCS
, false);
2415 return this->rela_dyn_
;
2418 // Similarly, but for ifunc symbols get the one for ifunc.
2420 template<int size
, bool big_endian
>
2421 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2422 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Symbol_table
* symtab
,
2427 return this->rela_dyn_section(layout
);
2429 if (this->iplt_
== NULL
)
2430 this->make_iplt_section(symtab
, layout
);
2431 return this->iplt_
->rel_plt();
2437 // Determine the stub group size. The group size is the absolute
2438 // value of the parameter --stub-group-size. If --stub-group-size
2439 // is passed a negative value, we restrict stubs to be always before
2440 // the stubbed branches.
2441 Stub_control(int32_t size
, bool no_size_errors
)
2442 : state_(NO_GROUP
), stub_group_size_(abs(size
)),
2443 stub14_group_size_(abs(size
) >> 10),
2444 stubs_always_before_branch_(size
< 0),
2445 suppress_size_errors_(no_size_errors
),
2446 group_end_addr_(0), owner_(NULL
), output_section_(NULL
)
2450 // Return true iff input section can be handled by current stub
2453 can_add_to_stub_group(Output_section
* o
,
2454 const Output_section::Input_section
* i
,
2457 const Output_section::Input_section
*
2463 { return output_section_
; }
2466 set_output_and_owner(Output_section
* o
,
2467 const Output_section::Input_section
* i
)
2469 this->output_section_
= o
;
2477 FINDING_STUB_SECTION
,
2482 uint32_t stub_group_size_
;
2483 uint32_t stub14_group_size_
;
2484 bool stubs_always_before_branch_
;
2485 bool suppress_size_errors_
;
2486 uint64_t group_end_addr_
;
2487 const Output_section::Input_section
* owner_
;
2488 Output_section
* output_section_
;
2491 // Return true iff input section can be handled by current stub
2495 Stub_control::can_add_to_stub_group(Output_section
* o
,
2496 const Output_section::Input_section
* i
,
2500 = has14
? this->stub14_group_size_
: this->stub_group_size_
;
2501 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
2503 uint64_t start_addr
= o
->address();
2506 // .init and .fini sections are pasted together to form a single
2507 // function. We can't be adding stubs in the middle of the function.
2508 this_size
= o
->data_size();
2511 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
2512 this_size
= i
->data_size();
2514 uint64_t end_addr
= start_addr
+ this_size
;
2515 bool toobig
= this_size
> group_size
;
2517 if (toobig
&& !this->suppress_size_errors_
)
2518 gold_warning(_("%s:%s exceeds group size"),
2519 i
->relobj()->name().c_str(),
2520 i
->relobj()->section_name(i
->shndx()).c_str());
2522 if (this->state_
!= HAS_STUB_SECTION
2523 && (!whole_sec
|| this->output_section_
!= o
)
2524 && (this->state_
== NO_GROUP
2525 || this->group_end_addr_
- end_addr
< group_size
))
2528 this->output_section_
= o
;
2531 if (this->state_
== NO_GROUP
)
2533 this->state_
= FINDING_STUB_SECTION
;
2534 this->group_end_addr_
= end_addr
;
2536 else if (this->group_end_addr_
- start_addr
< group_size
)
2538 // Adding this section would make the group larger than GROUP_SIZE.
2539 else if (this->state_
== FINDING_STUB_SECTION
2540 && !this->stubs_always_before_branch_
2543 // But wait, there's more! Input sections up to GROUP_SIZE
2544 // bytes before the stub table can be handled by it too.
2545 this->state_
= HAS_STUB_SECTION
;
2546 this->group_end_addr_
= end_addr
;
2550 this->state_
= NO_GROUP
;
2556 // Look over all the input sections, deciding where to place stubs.
2558 template<int size
, bool big_endian
>
2560 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
2562 bool no_size_errors
)
2564 Stub_control
stub_control(this->stub_group_size_
, no_size_errors
);
2566 // Group input sections and insert stub table
2567 Stub_table_owner
* table_owner
= NULL
;
2568 std::vector
<Stub_table_owner
*> tables
;
2569 Layout::Section_list section_list
;
2570 layout
->get_executable_sections(§ion_list
);
2571 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
2572 for (Layout::Section_list::reverse_iterator o
= section_list
.rbegin();
2573 o
!= section_list
.rend();
2576 typedef Output_section::Input_section_list Input_section_list
;
2577 for (Input_section_list::const_reverse_iterator i
2578 = (*o
)->input_sections().rbegin();
2579 i
!= (*o
)->input_sections().rend();
2582 if (i
->is_input_section()
2583 || i
->is_relaxed_input_section())
2585 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2586 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2587 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
2588 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
2590 table_owner
->output_section
= stub_control
.output_section();
2591 table_owner
->owner
= stub_control
.owner();
2592 stub_control
.set_output_and_owner(*o
, &*i
);
2595 if (table_owner
== NULL
)
2597 table_owner
= new Stub_table_owner
;
2598 tables
.push_back(table_owner
);
2600 ppcobj
->set_stub_table(i
->shndx(), tables
.size() - 1);
2604 if (table_owner
!= NULL
)
2606 const Output_section::Input_section
* i
= stub_control
.owner();
2608 if (tables
.size() >= 2 && tables
[tables
.size() - 2]->owner
== i
)
2610 // Corner case. A new stub group was made for the first
2611 // section (last one looked at here) for some reason, but
2612 // the first section is already being used as the owner for
2613 // a stub table for following sections. Force it into that
2617 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2618 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2619 ppcobj
->set_stub_table(i
->shndx(), tables
.size() - 1);
2623 table_owner
->output_section
= stub_control
.output_section();
2624 table_owner
->owner
= i
;
2627 for (typename
std::vector
<Stub_table_owner
*>::iterator t
= tables
.begin();
2631 Stub_table
<size
, big_endian
>* stub_table
;
2633 if ((*t
)->owner
->is_input_section())
2634 stub_table
= new Stub_table
<size
, big_endian
>(this,
2635 (*t
)->output_section
,
2637 else if ((*t
)->owner
->is_relaxed_input_section())
2638 stub_table
= static_cast<Stub_table
<size
, big_endian
>*>(
2639 (*t
)->owner
->relaxed_input_section());
2642 this->stub_tables_
.push_back(stub_table
);
2647 static unsigned long
2648 max_branch_delta (unsigned int r_type
)
2650 if (r_type
== elfcpp::R_POWERPC_REL14
2651 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
2652 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
2654 if (r_type
== elfcpp::R_POWERPC_REL24
2655 || r_type
== elfcpp::R_PPC_PLTREL24
2656 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
2661 // If this branch needs a plt call stub, or a long branch stub, make one.
2663 template<int size
, bool big_endian
>
2665 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
2666 Stub_table
<size
, big_endian
>* stub_table
,
2667 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
2668 Symbol_table
* symtab
) const
2670 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
2671 if (sym
!= NULL
&& sym
->is_forwarder())
2672 sym
= symtab
->resolve_forwards(sym
);
2673 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
2674 Target_powerpc
<size
, big_endian
>* target
=
2675 static_cast<Target_powerpc
<size
, big_endian
>*>(
2676 parameters
->sized_target
<size
, big_endian
>());
2678 ? gsym
->use_plt_offset(Scan::get_reference_flags(this->r_type_
, target
))
2679 : this->object_
->local_has_plt_offset(this->r_sym_
))
2683 && target
->abiversion() >= 2
2684 && !parameters
->options().output_is_position_independent()
2685 && !is_branch_reloc(this->r_type_
))
2686 target
->glink_section()->add_global_entry(gsym
);
2689 if (stub_table
== NULL
)
2690 stub_table
= this->object_
->stub_table(this->shndx_
);
2691 if (stub_table
== NULL
)
2693 // This is a ref from a data section to an ifunc symbol.
2694 stub_table
= ifunc_stub_table
;
2696 gold_assert(stub_table
!= NULL
);
2697 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
2698 if (from
!= invalid_address
)
2699 from
+= (this->object_
->output_section(this->shndx_
)->address()
2702 return stub_table
->add_plt_call_entry(from
,
2703 this->object_
, gsym
,
2704 this->r_type_
, this->addend_
);
2706 return stub_table
->add_plt_call_entry(from
,
2707 this->object_
, this->r_sym_
,
2708 this->r_type_
, this->addend_
);
2713 Address max_branch_offset
= max_branch_delta(this->r_type_
);
2714 if (max_branch_offset
== 0)
2716 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
2717 gold_assert(from
!= invalid_address
);
2718 from
+= (this->object_
->output_section(this->shndx_
)->address()
2723 switch (gsym
->source())
2725 case Symbol::FROM_OBJECT
:
2727 Object
* symobj
= gsym
->object();
2728 if (symobj
->is_dynamic()
2729 || symobj
->pluginobj() != NULL
)
2732 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
2733 if (shndx
== elfcpp::SHN_UNDEF
)
2738 case Symbol::IS_UNDEFINED
:
2744 Symbol_table::Compute_final_value_status status
;
2745 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
2746 if (status
!= Symbol_table::CFVS_OK
)
2749 to
+= this->object_
->ppc64_local_entry_offset(gsym
);
2753 const Symbol_value
<size
>* psymval
2754 = this->object_
->local_symbol(this->r_sym_
);
2755 Symbol_value
<size
> symval
;
2756 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
2757 typename
ObjType::Compute_final_local_value_status status
2758 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
2760 if (status
!= ObjType::CFLV_OK
2761 || !symval
.has_output_value())
2763 to
= symval
.value(this->object_
, 0);
2765 to
+= this->object_
->ppc64_local_entry_offset(this->r_sym_
);
2767 if (!(size
== 32 && this->r_type_
== elfcpp::R_PPC_PLTREL24
))
2768 to
+= this->addend_
;
2769 if (stub_table
== NULL
)
2770 stub_table
= this->object_
->stub_table(this->shndx_
);
2771 if (size
== 64 && target
->abiversion() < 2)
2773 unsigned int dest_shndx
;
2774 if (!target
->symval_for_branch(symtab
, gsym
, this->object_
,
2778 Address delta
= to
- from
;
2779 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
2781 if (stub_table
== NULL
)
2783 gold_warning(_("%s:%s: branch in non-executable section,"
2784 " no long branch stub for you"),
2785 this->object_
->name().c_str(),
2786 this->object_
->section_name(this->shndx_
).c_str());
2789 bool save_res
= (size
== 64
2791 && gsym
->source() == Symbol::IN_OUTPUT_DATA
2792 && gsym
->output_data() == target
->savres_section());
2793 return stub_table
->add_long_branch_entry(this->object_
,
2795 from
, to
, save_res
);
2801 // Relaxation hook. This is where we do stub generation.
2803 template<int size
, bool big_endian
>
2805 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
2806 const Input_objects
*,
2807 Symbol_table
* symtab
,
2811 unsigned int prev_brlt_size
= 0;
2815 = this->abiversion() < 2 && parameters
->options().plt_thread_safe();
2817 && this->abiversion() < 2
2819 && !parameters
->options().user_set_plt_thread_safe())
2821 static const char* const thread_starter
[] =
2825 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2827 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2828 "mq_notify", "create_timer",
2833 "GOMP_parallel_start",
2834 "GOMP_parallel_loop_static",
2835 "GOMP_parallel_loop_static_start",
2836 "GOMP_parallel_loop_dynamic",
2837 "GOMP_parallel_loop_dynamic_start",
2838 "GOMP_parallel_loop_guided",
2839 "GOMP_parallel_loop_guided_start",
2840 "GOMP_parallel_loop_runtime",
2841 "GOMP_parallel_loop_runtime_start",
2842 "GOMP_parallel_sections",
2843 "GOMP_parallel_sections_start",
2848 if (parameters
->options().shared())
2852 for (unsigned int i
= 0;
2853 i
< sizeof(thread_starter
) / sizeof(thread_starter
[0]);
2856 Symbol
* sym
= symtab
->lookup(thread_starter
[i
], NULL
);
2857 thread_safe
= (sym
!= NULL
2859 && sym
->in_real_elf());
2865 this->plt_thread_safe_
= thread_safe
;
2870 this->stub_group_size_
= parameters
->options().stub_group_size();
2871 bool no_size_errors
= true;
2872 if (this->stub_group_size_
== 1)
2873 this->stub_group_size_
= 0x1c00000;
2874 else if (this->stub_group_size_
== -1)
2875 this->stub_group_size_
= -0x1e00000;
2877 no_size_errors
= false;
2878 this->group_sections(layout
, task
, no_size_errors
);
2880 else if (this->relax_failed_
&& this->relax_fail_count_
< 3)
2882 this->branch_lookup_table_
.clear();
2883 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2884 p
!= this->stub_tables_
.end();
2887 (*p
)->clear_stubs(true);
2889 this->stub_tables_
.clear();
2890 this->stub_group_size_
= this->stub_group_size_
/ 4 * 3;
2891 gold_info(_("%s: stub group size is too large; retrying with %d"),
2892 program_name
, this->stub_group_size_
);
2893 this->group_sections(layout
, task
, true);
2896 // We need address of stub tables valid for make_stub.
2897 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2898 p
!= this->stub_tables_
.end();
2901 const Powerpc_relobj
<size
, big_endian
>* object
2902 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
2903 Address off
= object
->get_output_section_offset((*p
)->shndx());
2904 gold_assert(off
!= invalid_address
);
2905 Output_section
* os
= (*p
)->output_section();
2906 (*p
)->set_address_and_size(os
, off
);
2911 // Clear plt call stubs, long branch stubs and branch lookup table.
2912 prev_brlt_size
= this->branch_lookup_table_
.size();
2913 this->branch_lookup_table_
.clear();
2914 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2915 p
!= this->stub_tables_
.end();
2918 (*p
)->clear_stubs(false);
2922 // Build all the stubs.
2923 this->relax_failed_
= false;
2924 Stub_table
<size
, big_endian
>* ifunc_stub_table
2925 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
2926 Stub_table
<size
, big_endian
>* one_stub_table
2927 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
2928 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
2929 b
!= this->branch_info_
.end();
2932 if (!b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
)
2933 && !this->relax_failed_
)
2935 this->relax_failed_
= true;
2936 this->relax_fail_count_
++;
2937 if (this->relax_fail_count_
< 3)
2942 // Did anything change size?
2943 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
2944 bool again
= num_huge_branches
!= prev_brlt_size
;
2945 if (size
== 64 && num_huge_branches
!= 0)
2946 this->make_brlt_section(layout
);
2947 if (size
== 64 && again
)
2948 this->brlt_section_
->set_current_size(num_huge_branches
);
2950 typedef Unordered_set
<Output_section
*> Output_sections
;
2951 Output_sections os_need_update
;
2952 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2953 p
!= this->stub_tables_
.end();
2956 if ((*p
)->size_update())
2959 (*p
)->add_eh_frame(layout
);
2960 os_need_update
.insert((*p
)->output_section());
2964 // Set output section offsets for all input sections in an output
2965 // section that just changed size. Anything past the stubs will
2967 for (typename
Output_sections::iterator p
= os_need_update
.begin();
2968 p
!= os_need_update
.end();
2971 Output_section
* os
= *p
;
2973 typedef Output_section::Input_section_list Input_section_list
;
2974 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
2975 i
!= os
->input_sections().end();
2978 off
= align_address(off
, i
->addralign());
2979 if (i
->is_input_section() || i
->is_relaxed_input_section())
2980 i
->relobj()->set_section_offset(i
->shndx(), off
);
2981 if (i
->is_relaxed_input_section())
2983 Stub_table
<size
, big_endian
>* stub_table
2984 = static_cast<Stub_table
<size
, big_endian
>*>(
2985 i
->relaxed_input_section());
2986 off
+= stub_table
->set_address_and_size(os
, off
);
2989 off
+= i
->data_size();
2991 // If .branch_lt is part of this output section, then we have
2992 // just done the offset adjustment.
2993 os
->clear_section_offsets_need_adjustment();
2998 && num_huge_branches
!= 0
2999 && parameters
->options().output_is_position_independent())
3001 // Fill in the BRLT relocs.
3002 this->brlt_section_
->reset_brlt_sizes();
3003 for (typename
Branch_lookup_table::const_iterator p
3004 = this->branch_lookup_table_
.begin();
3005 p
!= this->branch_lookup_table_
.end();
3008 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
3010 this->brlt_section_
->finalize_brlt_sizes();
3015 template<int size
, bool big_endian
>
3017 Target_powerpc
<size
, big_endian
>::do_plt_fde_location(const Output_data
* plt
,
3018 unsigned char* oview
,
3022 uint64_t address
= plt
->address();
3023 off_t len
= plt
->data_size();
3025 if (plt
== this->glink_
)
3027 // See Output_data_glink::do_write() for glink contents.
3030 gold_assert(parameters
->doing_static_link());
3031 // Static linking may need stubs, to support ifunc and long
3032 // branches. We need to create an output section for
3033 // .eh_frame early in the link process, to have a place to
3034 // attach stub .eh_frame info. We also need to have
3035 // registered a CIE that matches the stub CIE. Both of
3036 // these requirements are satisfied by creating an FDE and
3037 // CIE for .glink, even though static linking will leave
3038 // .glink zero length.
3039 // ??? Hopefully generating an FDE with a zero address range
3040 // won't confuse anything that consumes .eh_frame info.
3042 else if (size
== 64)
3044 // There is one word before __glink_PLTresolve
3048 else if (parameters
->options().output_is_position_independent())
3050 // There are two FDEs for a position independent glink.
3051 // The first covers the branch table, the second
3052 // __glink_PLTresolve at the end of glink.
3053 off_t resolve_size
= this->glink_
->pltresolve_size
;
3054 if (oview
[9] == elfcpp::DW_CFA_nop
)
3055 len
-= resolve_size
;
3058 address
+= len
- resolve_size
;
3065 // Must be a stub table.
3066 const Stub_table
<size
, big_endian
>* stub_table
3067 = static_cast<const Stub_table
<size
, big_endian
>*>(plt
);
3068 uint64_t stub_address
= stub_table
->stub_address();
3069 len
-= stub_address
- address
;
3070 address
= stub_address
;
3073 *paddress
= address
;
3077 // A class to handle the PLT data.
3079 template<int size
, bool big_endian
>
3080 class Output_data_plt_powerpc
: public Output_section_data_build
3083 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3084 size
, big_endian
> Reloc_section
;
3086 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3087 Reloc_section
* plt_rel
,
3089 : Output_section_data_build(size
== 32 ? 4 : 8),
3095 // Add an entry to the PLT.
3100 add_ifunc_entry(Symbol
*);
3103 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
3105 // Return the .rela.plt section data.
3112 // Return the number of PLT entries.
3116 if (this->current_data_size() == 0)
3118 return ((this->current_data_size() - this->first_plt_entry_offset())
3119 / this->plt_entry_size());
3124 do_adjust_output_section(Output_section
* os
)
3129 // Write to a map file.
3131 do_print_to_mapfile(Mapfile
* mapfile
) const
3132 { mapfile
->print_output_data(this, this->name_
); }
3135 // Return the offset of the first non-reserved PLT entry.
3137 first_plt_entry_offset() const
3139 // IPLT has no reserved entry.
3140 if (this->name_
[3] == 'I')
3142 return this->targ_
->first_plt_entry_offset();
3145 // Return the size of each PLT entry.
3147 plt_entry_size() const
3149 return this->targ_
->plt_entry_size();
3152 // Write out the PLT data.
3154 do_write(Output_file
*);
3156 // The reloc section.
3157 Reloc_section
* rel_
;
3158 // Allows access to .glink for do_write.
3159 Target_powerpc
<size
, big_endian
>* targ_
;
3160 // What to report in map file.
3164 // Add an entry to the PLT.
3166 template<int size
, bool big_endian
>
3168 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
3170 if (!gsym
->has_plt_offset())
3172 section_size_type off
= this->current_data_size();
3174 off
+= this->first_plt_entry_offset();
3175 gsym
->set_plt_offset(off
);
3176 gsym
->set_needs_dynsym_entry();
3177 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
3178 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
3179 off
+= this->plt_entry_size();
3180 this->set_current_data_size(off
);
3184 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3186 template<int size
, bool big_endian
>
3188 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
3190 if (!gsym
->has_plt_offset())
3192 section_size_type off
= this->current_data_size();
3193 gsym
->set_plt_offset(off
);
3194 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3195 if (size
== 64 && this->targ_
->abiversion() < 2)
3196 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3197 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
3198 off
+= this->plt_entry_size();
3199 this->set_current_data_size(off
);
3203 // Add an entry for a local ifunc symbol to the IPLT.
3205 template<int size
, bool big_endian
>
3207 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
3208 Sized_relobj_file
<size
, big_endian
>* relobj
,
3209 unsigned int local_sym_index
)
3211 if (!relobj
->local_has_plt_offset(local_sym_index
))
3213 section_size_type off
= this->current_data_size();
3214 relobj
->set_local_plt_offset(local_sym_index
, off
);
3215 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
3216 if (size
== 64 && this->targ_
->abiversion() < 2)
3217 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
3218 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
3220 off
+= this->plt_entry_size();
3221 this->set_current_data_size(off
);
3225 static const uint32_t add_0_11_11
= 0x7c0b5a14;
3226 static const uint32_t add_2_2_11
= 0x7c425a14;
3227 static const uint32_t add_3_3_2
= 0x7c631214;
3228 static const uint32_t add_3_3_13
= 0x7c636a14;
3229 static const uint32_t add_11_0_11
= 0x7d605a14;
3230 static const uint32_t add_11_2_11
= 0x7d625a14;
3231 static const uint32_t add_11_11_2
= 0x7d6b1214;
3232 static const uint32_t addi_0_12
= 0x380c0000;
3233 static const uint32_t addi_2_2
= 0x38420000;
3234 static const uint32_t addi_3_3
= 0x38630000;
3235 static const uint32_t addi_11_11
= 0x396b0000;
3236 static const uint32_t addi_12_1
= 0x39810000;
3237 static const uint32_t addi_12_12
= 0x398c0000;
3238 static const uint32_t addis_0_2
= 0x3c020000;
3239 static const uint32_t addis_0_13
= 0x3c0d0000;
3240 static const uint32_t addis_2_12
= 0x3c4c0000;
3241 static const uint32_t addis_11_2
= 0x3d620000;
3242 static const uint32_t addis_11_11
= 0x3d6b0000;
3243 static const uint32_t addis_11_30
= 0x3d7e0000;
3244 static const uint32_t addis_12_1
= 0x3d810000;
3245 static const uint32_t addis_12_2
= 0x3d820000;
3246 static const uint32_t addis_12_12
= 0x3d8c0000;
3247 static const uint32_t b
= 0x48000000;
3248 static const uint32_t bcl_20_31
= 0x429f0005;
3249 static const uint32_t bctr
= 0x4e800420;
3250 static const uint32_t blr
= 0x4e800020;
3251 static const uint32_t bnectr_p4
= 0x4ce20420;
3252 static const uint32_t cmpld_7_12_0
= 0x7fac0040;
3253 static const uint32_t cmpldi_2_0
= 0x28220000;
3254 static const uint32_t cror_15_15_15
= 0x4def7b82;
3255 static const uint32_t cror_31_31_31
= 0x4ffffb82;
3256 static const uint32_t ld_0_1
= 0xe8010000;
3257 static const uint32_t ld_0_12
= 0xe80c0000;
3258 static const uint32_t ld_2_1
= 0xe8410000;
3259 static const uint32_t ld_2_2
= 0xe8420000;
3260 static const uint32_t ld_2_11
= 0xe84b0000;
3261 static const uint32_t ld_11_2
= 0xe9620000;
3262 static const uint32_t ld_11_11
= 0xe96b0000;
3263 static const uint32_t ld_12_2
= 0xe9820000;
3264 static const uint32_t ld_12_11
= 0xe98b0000;
3265 static const uint32_t ld_12_12
= 0xe98c0000;
3266 static const uint32_t lfd_0_1
= 0xc8010000;
3267 static const uint32_t li_0_0
= 0x38000000;
3268 static const uint32_t li_12_0
= 0x39800000;
3269 static const uint32_t lis_0
= 0x3c000000;
3270 static const uint32_t lis_11
= 0x3d600000;
3271 static const uint32_t lis_12
= 0x3d800000;
3272 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
3273 static const uint32_t lwz_0_12
= 0x800c0000;
3274 static const uint32_t lwz_11_11
= 0x816b0000;
3275 static const uint32_t lwz_11_30
= 0x817e0000;
3276 static const uint32_t lwz_12_12
= 0x818c0000;
3277 static const uint32_t lwzu_0_12
= 0x840c0000;
3278 static const uint32_t mflr_0
= 0x7c0802a6;
3279 static const uint32_t mflr_11
= 0x7d6802a6;
3280 static const uint32_t mflr_12
= 0x7d8802a6;
3281 static const uint32_t mtctr_0
= 0x7c0903a6;
3282 static const uint32_t mtctr_11
= 0x7d6903a6;
3283 static const uint32_t mtctr_12
= 0x7d8903a6;
3284 static const uint32_t mtlr_0
= 0x7c0803a6;
3285 static const uint32_t mtlr_12
= 0x7d8803a6;
3286 static const uint32_t nop
= 0x60000000;
3287 static const uint32_t ori_0_0_0
= 0x60000000;
3288 static const uint32_t srdi_0_0_2
= 0x7800f082;
3289 static const uint32_t std_0_1
= 0xf8010000;
3290 static const uint32_t std_0_12
= 0xf80c0000;
3291 static const uint32_t std_2_1
= 0xf8410000;
3292 static const uint32_t stfd_0_1
= 0xd8010000;
3293 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
3294 static const uint32_t sub_11_11_12
= 0x7d6c5850;
3295 static const uint32_t sub_12_12_11
= 0x7d8b6050;
3296 static const uint32_t xor_2_12_12
= 0x7d826278;
3297 static const uint32_t xor_11_12_12
= 0x7d8b6278;
3299 // Write out the PLT.
3301 template<int size
, bool big_endian
>
3303 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3305 if (size
== 32 && this->name_
[3] != 'I')
3307 const section_size_type offset
= this->offset();
3308 const section_size_type oview_size
3309 = convert_to_section_size_type(this->data_size());
3310 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3311 unsigned char* pov
= oview
;
3312 unsigned char* endpov
= oview
+ oview_size
;
3314 // The address of the .glink branch table
3315 const Output_data_glink
<size
, big_endian
>* glink
3316 = this->targ_
->glink_section();
3317 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
3319 while (pov
< endpov
)
3321 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
3326 of
->write_output_view(offset
, oview_size
, oview
);
3330 // Create the PLT section.
3332 template<int size
, bool big_endian
>
3334 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
3337 if (this->plt_
== NULL
)
3339 if (this->got_
== NULL
)
3340 this->got_section(symtab
, layout
);
3342 if (this->glink_
== NULL
)
3343 make_glink_section(layout
);
3345 // Ensure that .rela.dyn always appears before .rela.plt This is
3346 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3347 // needs to include .rela.plt in its range.
3348 this->rela_dyn_section(layout
);
3350 Reloc_section
* plt_rel
= new Reloc_section(false);
3351 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
3352 elfcpp::SHF_ALLOC
, plt_rel
,
3353 ORDER_DYNAMIC_PLT_RELOCS
, false);
3355 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
3357 layout
->add_output_section_data(".plt",
3359 ? elfcpp::SHT_PROGBITS
3360 : elfcpp::SHT_NOBITS
),
3361 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3370 // Create the IPLT section.
3372 template<int size
, bool big_endian
>
3374 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
3377 if (this->iplt_
== NULL
)
3379 this->make_plt_section(symtab
, layout
);
3381 Reloc_section
* iplt_rel
= new Reloc_section(false);
3382 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
3384 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
3386 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
3390 // A section for huge long branch addresses, similar to plt section.
3392 template<int size
, bool big_endian
>
3393 class Output_data_brlt_powerpc
: public Output_section_data_build
3396 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3397 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3398 size
, big_endian
> Reloc_section
;
3400 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3401 Reloc_section
* brlt_rel
)
3402 : Output_section_data_build(size
== 32 ? 4 : 8),
3410 this->reset_data_size();
3411 this->rel_
->reset_data_size();
3415 finalize_brlt_sizes()
3417 this->finalize_data_size();
3418 this->rel_
->finalize_data_size();
3421 // Add a reloc for an entry in the BRLT.
3423 add_reloc(Address to
, unsigned int off
)
3424 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
3426 // Update section and reloc section size.
3428 set_current_size(unsigned int num_branches
)
3430 this->reset_address_and_file_offset();
3431 this->set_current_data_size(num_branches
* 16);
3432 this->finalize_data_size();
3433 Output_section
* os
= this->output_section();
3434 os
->set_section_offsets_need_adjustment();
3435 if (this->rel_
!= NULL
)
3437 unsigned int reloc_size
3438 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
3439 this->rel_
->reset_address_and_file_offset();
3440 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
3441 this->rel_
->finalize_data_size();
3442 Output_section
* os
= this->rel_
->output_section();
3443 os
->set_section_offsets_need_adjustment();
3449 do_adjust_output_section(Output_section
* os
)
3454 // Write to a map file.
3456 do_print_to_mapfile(Mapfile
* mapfile
) const
3457 { mapfile
->print_output_data(this, "** BRLT"); }
3460 // Write out the BRLT data.
3462 do_write(Output_file
*);
3464 // The reloc section.
3465 Reloc_section
* rel_
;
3466 Target_powerpc
<size
, big_endian
>* targ_
;
3469 // Make the branch lookup table section.
3471 template<int size
, bool big_endian
>
3473 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
3475 if (size
== 64 && this->brlt_section_
== NULL
)
3477 Reloc_section
* brlt_rel
= NULL
;
3478 bool is_pic
= parameters
->options().output_is_position_independent();
3481 // When PIC we can't fill in .branch_lt (like .plt it can be
3482 // a bss style section) but must initialise at runtime via
3483 // dynamic relocats.
3484 this->rela_dyn_section(layout
);
3485 brlt_rel
= new Reloc_section(false);
3486 this->rela_dyn_
->output_section()->add_output_section_data(brlt_rel
);
3489 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
3490 if (this->plt_
&& is_pic
)
3491 this->plt_
->output_section()
3492 ->add_output_section_data(this->brlt_section_
);
3494 layout
->add_output_section_data(".branch_lt",
3495 (is_pic
? elfcpp::SHT_NOBITS
3496 : elfcpp::SHT_PROGBITS
),
3497 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3498 this->brlt_section_
,
3499 (is_pic
? ORDER_SMALL_BSS
3500 : ORDER_SMALL_DATA
),
3505 // Write out .branch_lt when non-PIC.
3507 template<int size
, bool big_endian
>
3509 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3511 if (size
== 64 && !parameters
->options().output_is_position_independent())
3513 const section_size_type offset
= this->offset();
3514 const section_size_type oview_size
3515 = convert_to_section_size_type(this->data_size());
3516 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3518 this->targ_
->write_branch_lookup_table(oview
);
3519 of
->write_output_view(offset
, oview_size
, oview
);
3523 static inline uint32_t
3529 static inline uint32_t
3535 static inline uint32_t
3538 return hi(a
+ 0x8000);
3544 static const unsigned char eh_frame_cie
[12];
3548 const unsigned char Eh_cie
<size
>::eh_frame_cie
[] =
3551 'z', 'R', 0, // Augmentation string.
3552 4, // Code alignment.
3553 0x80 - size
/ 8 , // Data alignment.
3555 1, // Augmentation size.
3556 (elfcpp::DW_EH_PE_pcrel
3557 | elfcpp::DW_EH_PE_sdata4
), // FDE encoding.
3558 elfcpp::DW_CFA_def_cfa
, 1, 0 // def_cfa: r1 offset 0.
3561 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3562 static const unsigned char glink_eh_frame_fde_64v1
[] =
3564 0, 0, 0, 0, // Replaced with offset to .glink.
3565 0, 0, 0, 0, // Replaced with size of .glink.
3566 0, // Augmentation size.
3567 elfcpp::DW_CFA_advance_loc
+ 1,
3568 elfcpp::DW_CFA_register
, 65, 12,
3569 elfcpp::DW_CFA_advance_loc
+ 4,
3570 elfcpp::DW_CFA_restore_extended
, 65
3573 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
3574 static const unsigned char glink_eh_frame_fde_64v2
[] =
3576 0, 0, 0, 0, // Replaced with offset to .glink.
3577 0, 0, 0, 0, // Replaced with size of .glink.
3578 0, // Augmentation size.
3579 elfcpp::DW_CFA_advance_loc
+ 1,
3580 elfcpp::DW_CFA_register
, 65, 0,
3581 elfcpp::DW_CFA_advance_loc
+ 4,
3582 elfcpp::DW_CFA_restore_extended
, 65
3585 // Describe __glink_PLTresolve use of LR, 32-bit version.
3586 static const unsigned char glink_eh_frame_fde_32
[] =
3588 0, 0, 0, 0, // Replaced with offset to .glink.
3589 0, 0, 0, 0, // Replaced with size of .glink.
3590 0, // Augmentation size.
3591 elfcpp::DW_CFA_advance_loc
+ 2,
3592 elfcpp::DW_CFA_register
, 65, 0,
3593 elfcpp::DW_CFA_advance_loc
+ 4,
3594 elfcpp::DW_CFA_restore_extended
, 65
3597 static const unsigned char default_fde
[] =
3599 0, 0, 0, 0, // Replaced with offset to stubs.
3600 0, 0, 0, 0, // Replaced with size of stubs.
3601 0, // Augmentation size.
3602 elfcpp::DW_CFA_nop
, // Pad.
3607 template<bool big_endian
>
3609 write_insn(unsigned char* p
, uint32_t v
)
3611 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
3614 // Stub_table holds information about plt and long branch stubs.
3615 // Stubs are built in an area following some input section determined
3616 // by group_sections(). This input section is converted to a relaxed
3617 // input section allowing it to be resized to accommodate the stubs
3619 template<int size
, bool big_endian
>
3620 class Stub_table
: public Output_relaxed_input_section
3623 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3624 static const Address invalid_address
= static_cast<Address
>(0) - 1;
3626 Stub_table(Target_powerpc
<size
, big_endian
>* targ
,
3627 Output_section
* output_section
,
3628 const Output_section::Input_section
* owner
)
3629 : Output_relaxed_input_section(owner
->relobj(), owner
->shndx(),
3631 ->section_addralign(owner
->shndx())),
3632 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
3633 orig_data_size_(owner
->current_data_size()),
3634 plt_size_(0), last_plt_size_(0),
3635 branch_size_(0), last_branch_size_(0), eh_frame_added_(false),
3636 need_save_res_(false)
3638 this->set_output_section(output_section
);
3640 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
3641 new_relaxed
.push_back(this);
3642 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
3645 // Add a plt call stub.
3647 add_plt_call_entry(Address
,
3648 const Sized_relobj_file
<size
, big_endian
>*,
3654 add_plt_call_entry(Address
,
3655 const Sized_relobj_file
<size
, big_endian
>*,
3660 // Find a given plt call stub.
3662 find_plt_call_entry(const Symbol
*) const;
3665 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3666 unsigned int) const;
3669 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3675 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3680 // Add a long branch stub.
3682 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
3683 unsigned int, Address
, Address
, bool);
3686 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
3690 can_reach_stub(Address from
, unsigned int off
, unsigned int r_type
)
3692 Address max_branch_offset
= max_branch_delta(r_type
);
3693 if (max_branch_offset
== 0)
3695 gold_assert(from
!= invalid_address
);
3696 Address loc
= off
+ this->stub_address();
3697 return loc
- from
+ max_branch_offset
< 2 * max_branch_offset
;
3701 clear_stubs(bool all
)
3703 this->plt_call_stubs_
.clear();
3704 this->plt_size_
= 0;
3705 this->long_branch_stubs_
.clear();
3706 this->branch_size_
= 0;
3707 this->need_save_res_
= false;
3710 this->last_plt_size_
= 0;
3711 this->last_branch_size_
= 0;
3716 set_address_and_size(const Output_section
* os
, Address off
)
3718 Address start_off
= off
;
3719 off
+= this->orig_data_size_
;
3720 Address my_size
= this->plt_size_
+ this->branch_size_
;
3721 if (this->need_save_res_
)
3722 my_size
+= this->targ_
->savres_section()->data_size();
3724 off
= align_address(off
, this->stub_align());
3725 // Include original section size and alignment padding in size
3726 my_size
+= off
- start_off
;
3727 this->reset_address_and_file_offset();
3728 this->set_current_data_size(my_size
);
3729 this->set_address_and_file_offset(os
->address() + start_off
,
3730 os
->offset() + start_off
);
3735 stub_address() const
3737 return align_address(this->address() + this->orig_data_size_
,
3738 this->stub_align());
3744 return align_address(this->offset() + this->orig_data_size_
,
3745 this->stub_align());
3750 { return this->plt_size_
; }
3755 Output_section
* os
= this->output_section();
3756 if (os
->addralign() < this->stub_align())
3758 os
->set_addralign(this->stub_align());
3759 // FIXME: get rid of the insane checkpointing.
3760 // We can't increase alignment of the input section to which
3761 // stubs are attached; The input section may be .init which
3762 // is pasted together with other .init sections to form a
3763 // function. Aligning might insert zero padding resulting in
3764 // sigill. However we do need to increase alignment of the
3765 // output section so that the align_address() on offset in
3766 // set_address_and_size() adds the same padding as the
3767 // align_address() on address in stub_address().
3768 // What's more, we need this alignment for the layout done in
3769 // relaxation_loop_body() so that the output section starts at
3770 // a suitably aligned address.
3771 os
->checkpoint_set_addralign(this->stub_align());
3773 if (this->last_plt_size_
!= this->plt_size_
3774 || this->last_branch_size_
!= this->branch_size_
)
3776 this->last_plt_size_
= this->plt_size_
;
3777 this->last_branch_size_
= this->branch_size_
;
3783 // Add .eh_frame info for this stub section. Unlike other linker
3784 // generated .eh_frame this is added late in the link, because we
3785 // only want the .eh_frame info if this particular stub section is
3788 add_eh_frame(Layout
* layout
)
3790 if (!this->eh_frame_added_
)
3792 if (!parameters
->options().ld_generated_unwind_info())
3795 // Since we add stub .eh_frame info late, it must be placed
3796 // after all other linker generated .eh_frame info so that
3797 // merge mapping need not be updated for input sections.
3798 // There is no provision to use a different CIE to that used
3800 if (!this->targ_
->has_glink())
3803 layout
->add_eh_frame_for_plt(this,
3804 Eh_cie
<size
>::eh_frame_cie
,
3805 sizeof (Eh_cie
<size
>::eh_frame_cie
),
3807 sizeof (default_fde
));
3808 this->eh_frame_added_
= true;
3812 Target_powerpc
<size
, big_endian
>*
3818 class Plt_stub_ent_hash
;
3819 typedef Unordered_map
<Plt_stub_ent
, unsigned int,
3820 Plt_stub_ent_hash
> Plt_stub_entries
;
3822 // Alignment of stub section.
3828 unsigned int min_align
= 32;
3829 unsigned int user_align
= 1 << parameters
->options().plt_align();
3830 return std::max(user_align
, min_align
);
3833 // Return the plt offset for the given call stub.
3835 plt_off(typename
Plt_stub_entries::const_iterator p
, bool* is_iplt
) const
3837 const Symbol
* gsym
= p
->first
.sym_
;
3840 *is_iplt
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
3841 && gsym
->can_use_relative_reloc(false));
3842 return gsym
->plt_offset();
3847 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
3848 unsigned int local_sym_index
= p
->first
.locsym_
;
3849 return relobj
->local_plt_offset(local_sym_index
);
3853 // Size of a given plt call stub.
3855 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
3861 Address plt_addr
= this->plt_off(p
, &is_iplt
);
3863 plt_addr
+= this->targ_
->iplt_section()->address();
3865 plt_addr
+= this->targ_
->plt_section()->address();
3866 Address got_addr
= this->targ_
->got_section()->output_section()->address();
3867 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3868 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
3869 got_addr
+= ppcobj
->toc_base_offset();
3870 Address off
= plt_addr
- got_addr
;
3871 unsigned int bytes
= 4 * 4 + 4 * (ha(off
) != 0);
3872 if (this->targ_
->abiversion() < 2)
3874 bool static_chain
= parameters
->options().plt_static_chain();
3875 bool thread_safe
= this->targ_
->plt_thread_safe();
3879 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
3881 unsigned int align
= 1 << parameters
->options().plt_align();
3883 bytes
= (bytes
+ align
- 1) & -align
;
3887 // Return long branch stub size.
3889 branch_stub_size(Address to
)
3892 = this->stub_address() + this->last_plt_size_
+ this->branch_size_
;
3893 if (to
- loc
+ (1 << 25) < 2 << 25)
3895 if (size
== 64 || !parameters
->options().output_is_position_independent())
3902 do_write(Output_file
*);
3904 // Plt call stub keys.
3908 Plt_stub_ent(const Symbol
* sym
)
3909 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3912 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3913 unsigned int locsym_index
)
3914 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3917 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3919 unsigned int r_type
,
3921 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3924 this->addend_
= addend
;
3925 else if (parameters
->options().output_is_position_independent()
3926 && r_type
== elfcpp::R_PPC_PLTREL24
)
3928 this->addend_
= addend
;
3929 if (this->addend_
>= 32768)
3930 this->object_
= object
;
3934 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3935 unsigned int locsym_index
,
3936 unsigned int r_type
,
3938 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3941 this->addend_
= addend
;
3942 else if (parameters
->options().output_is_position_independent()
3943 && r_type
== elfcpp::R_PPC_PLTREL24
)
3944 this->addend_
= addend
;
3947 bool operator==(const Plt_stub_ent
& that
) const
3949 return (this->sym_
== that
.sym_
3950 && this->object_
== that
.object_
3951 && this->addend_
== that
.addend_
3952 && this->locsym_
== that
.locsym_
);
3956 const Sized_relobj_file
<size
, big_endian
>* object_
;
3957 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
3958 unsigned int locsym_
;
3961 class Plt_stub_ent_hash
3964 size_t operator()(const Plt_stub_ent
& ent
) const
3966 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
3967 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
3973 // Long branch stub keys.
3974 class Branch_stub_ent
3977 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
,
3978 Address to
, bool save_res
)
3979 : dest_(to
), toc_base_off_(0), save_res_(save_res
)
3982 toc_base_off_
= obj
->toc_base_offset();
3985 bool operator==(const Branch_stub_ent
& that
) const
3987 return (this->dest_
== that
.dest_
3989 || this->toc_base_off_
== that
.toc_base_off_
));
3993 unsigned int toc_base_off_
;
3997 class Branch_stub_ent_hash
4000 size_t operator()(const Branch_stub_ent
& ent
) const
4001 { return ent
.dest_
^ ent
.toc_base_off_
; }
4004 // In a sane world this would be a global.
4005 Target_powerpc
<size
, big_endian
>* targ_
;
4006 // Map sym/object/addend to stub offset.
4007 Plt_stub_entries plt_call_stubs_
;
4008 // Map destination address to stub offset.
4009 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
4010 Branch_stub_ent_hash
> Branch_stub_entries
;
4011 Branch_stub_entries long_branch_stubs_
;
4012 // size of input section
4013 section_size_type orig_data_size_
;
4015 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
4016 // Whether .eh_frame info has been created for this stub section.
4017 bool eh_frame_added_
;
4018 // Set if this stub group needs a copy of out-of-line register
4019 // save/restore functions.
4020 bool need_save_res_
;
4023 // Add a plt call stub, if we do not already have one for this
4024 // sym/object/addend combo.
4026 template<int size
, bool big_endian
>
4028 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4030 const Sized_relobj_file
<size
, big_endian
>* object
,
4032 unsigned int r_type
,
4035 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
4036 unsigned int off
= this->plt_size_
;
4037 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4038 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
4040 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
4041 return this->can_reach_stub(from
, off
, r_type
);
4044 template<int size
, bool big_endian
>
4046 Stub_table
<size
, big_endian
>::add_plt_call_entry(
4048 const Sized_relobj_file
<size
, big_endian
>* object
,
4049 unsigned int locsym_index
,
4050 unsigned int r_type
,
4053 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
4054 unsigned int off
= this->plt_size_
;
4055 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
4056 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
4058 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
4059 return this->can_reach_stub(from
, off
, r_type
);
4062 // Find a plt call stub.
4064 template<int size
, bool big_endian
>
4065 typename Stub_table
<size
, big_endian
>::Address
4066 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4067 const Sized_relobj_file
<size
, big_endian
>* object
,
4069 unsigned int r_type
,
4070 Address addend
) const
4072 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
4073 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
4074 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
4077 template<int size
, bool big_endian
>
4078 typename Stub_table
<size
, big_endian
>::Address
4079 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
4081 Plt_stub_ent
ent(gsym
);
4082 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
4083 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
4086 template<int size
, bool big_endian
>
4087 typename Stub_table
<size
, big_endian
>::Address
4088 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4089 const Sized_relobj_file
<size
, big_endian
>* object
,
4090 unsigned int locsym_index
,
4091 unsigned int r_type
,
4092 Address addend
) const
4094 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
4095 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
4096 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
4099 template<int size
, bool big_endian
>
4100 typename Stub_table
<size
, big_endian
>::Address
4101 Stub_table
<size
, big_endian
>::find_plt_call_entry(
4102 const Sized_relobj_file
<size
, big_endian
>* object
,
4103 unsigned int locsym_index
) const
4105 Plt_stub_ent
ent(object
, locsym_index
);
4106 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
4107 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
4110 // Add a long branch stub if we don't already have one to given
4113 template<int size
, bool big_endian
>
4115 Stub_table
<size
, big_endian
>::add_long_branch_entry(
4116 const Powerpc_relobj
<size
, big_endian
>* object
,
4117 unsigned int r_type
,
4122 Branch_stub_ent
ent(object
, to
, save_res
);
4123 Address off
= this->branch_size_
;
4124 if (this->long_branch_stubs_
.insert(std::make_pair(ent
, off
)).second
)
4127 this->need_save_res_
= true;
4130 unsigned int stub_size
= this->branch_stub_size(to
);
4131 this->branch_size_
= off
+ stub_size
;
4132 if (size
== 64 && stub_size
!= 4)
4133 this->targ_
->add_branch_lookup_table(to
);
4136 return this->can_reach_stub(from
, off
, r_type
);
4139 // Find long branch stub offset.
4141 template<int size
, bool big_endian
>
4142 typename Stub_table
<size
, big_endian
>::Address
4143 Stub_table
<size
, big_endian
>::find_long_branch_entry(
4144 const Powerpc_relobj
<size
, big_endian
>* object
,
4147 Branch_stub_ent
ent(object
, to
, false);
4148 typename
Branch_stub_entries::const_iterator p
4149 = this->long_branch_stubs_
.find(ent
);
4150 if (p
== this->long_branch_stubs_
.end())
4151 return invalid_address
;
4152 if (p
->first
.save_res_
)
4153 return to
- this->targ_
->savres_section()->address() + this->branch_size_
;
4157 // A class to handle .glink.
4159 template<int size
, bool big_endian
>
4160 class Output_data_glink
: public Output_section_data
4163 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4164 static const Address invalid_address
= static_cast<Address
>(0) - 1;
4165 static const int pltresolve_size
= 16*4;
4167 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
4168 : Output_section_data(16), targ_(targ
), global_entry_stubs_(),
4169 end_branch_table_(), ge_size_(0)
4173 add_eh_frame(Layout
* layout
);
4176 add_global_entry(const Symbol
*);
4179 find_global_entry(const Symbol
*) const;
4182 global_entry_address() const
4184 gold_assert(this->is_data_size_valid());
4185 unsigned int global_entry_off
= (this->end_branch_table_
+ 15) & -16;
4186 return this->address() + global_entry_off
;
4190 // Write to a map file.
4192 do_print_to_mapfile(Mapfile
* mapfile
) const
4193 { mapfile
->print_output_data(this, _("** glink")); }
4197 set_final_data_size();
4201 do_write(Output_file
*);
4203 // Allows access to .got and .plt for do_write.
4204 Target_powerpc
<size
, big_endian
>* targ_
;
4206 // Map sym to stub offset.
4207 typedef Unordered_map
<const Symbol
*, unsigned int> Global_entry_stub_entries
;
4208 Global_entry_stub_entries global_entry_stubs_
;
4210 unsigned int end_branch_table_
, ge_size_
;
4213 template<int size
, bool big_endian
>
4215 Output_data_glink
<size
, big_endian
>::add_eh_frame(Layout
* layout
)
4217 if (!parameters
->options().ld_generated_unwind_info())
4222 if (this->targ_
->abiversion() < 2)
4223 layout
->add_eh_frame_for_plt(this,
4224 Eh_cie
<64>::eh_frame_cie
,
4225 sizeof (Eh_cie
<64>::eh_frame_cie
),
4226 glink_eh_frame_fde_64v1
,
4227 sizeof (glink_eh_frame_fde_64v1
));
4229 layout
->add_eh_frame_for_plt(this,
4230 Eh_cie
<64>::eh_frame_cie
,
4231 sizeof (Eh_cie
<64>::eh_frame_cie
),
4232 glink_eh_frame_fde_64v2
,
4233 sizeof (glink_eh_frame_fde_64v2
));
4237 // 32-bit .glink can use the default since the CIE return
4238 // address reg, LR, is valid.
4239 layout
->add_eh_frame_for_plt(this,
4240 Eh_cie
<32>::eh_frame_cie
,
4241 sizeof (Eh_cie
<32>::eh_frame_cie
),
4243 sizeof (default_fde
));
4244 // Except where LR is used in a PIC __glink_PLTresolve.
4245 if (parameters
->options().output_is_position_independent())
4246 layout
->add_eh_frame_for_plt(this,
4247 Eh_cie
<32>::eh_frame_cie
,
4248 sizeof (Eh_cie
<32>::eh_frame_cie
),
4249 glink_eh_frame_fde_32
,
4250 sizeof (glink_eh_frame_fde_32
));
4254 template<int size
, bool big_endian
>
4256 Output_data_glink
<size
, big_endian
>::add_global_entry(const Symbol
* gsym
)
4258 std::pair
<typename
Global_entry_stub_entries::iterator
, bool> p
4259 = this->global_entry_stubs_
.insert(std::make_pair(gsym
, this->ge_size_
));
4261 this->ge_size_
+= 16;
4264 template<int size
, bool big_endian
>
4265 typename Output_data_glink
<size
, big_endian
>::Address
4266 Output_data_glink
<size
, big_endian
>::find_global_entry(const Symbol
* gsym
) const
4268 typename
Global_entry_stub_entries::const_iterator p
4269 = this->global_entry_stubs_
.find(gsym
);
4270 return p
== this->global_entry_stubs_
.end() ? invalid_address
: p
->second
;
4273 template<int size
, bool big_endian
>
4275 Output_data_glink
<size
, big_endian
>::set_final_data_size()
4277 unsigned int count
= this->targ_
->plt_entry_count();
4278 section_size_type total
= 0;
4284 // space for branch table
4285 total
+= 4 * (count
- 1);
4287 total
+= -total
& 15;
4288 total
+= this->pltresolve_size
;
4292 total
+= this->pltresolve_size
;
4294 // space for branch table
4296 if (this->targ_
->abiversion() < 2)
4300 total
+= 4 * (count
- 0x8000);
4304 this->end_branch_table_
= total
;
4305 total
= (total
+ 15) & -16;
4306 total
+= this->ge_size_
;
4308 this->set_data_size(total
);
4311 // Write out plt and long branch stub code.
4313 template<int size
, bool big_endian
>
4315 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
4317 if (this->plt_call_stubs_
.empty()
4318 && this->long_branch_stubs_
.empty())
4321 const section_size_type start_off
= this->offset();
4322 const section_size_type off
= this->stub_offset();
4323 const section_size_type oview_size
=
4324 convert_to_section_size_type(this->data_size() - (off
- start_off
));
4325 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4330 const Output_data_got_powerpc
<size
, big_endian
>* got
4331 = this->targ_
->got_section();
4332 Address got_os_addr
= got
->output_section()->address();
4334 if (!this->plt_call_stubs_
.empty())
4336 // The base address of the .plt section.
4337 Address plt_base
= this->targ_
->plt_section()->address();
4338 Address iplt_base
= invalid_address
;
4340 // Write out plt call stubs.
4341 typename
Plt_stub_entries::const_iterator cs
;
4342 for (cs
= this->plt_call_stubs_
.begin();
4343 cs
!= this->plt_call_stubs_
.end();
4347 Address pltoff
= this->plt_off(cs
, &is_iplt
);
4348 Address plt_addr
= pltoff
;
4351 if (iplt_base
== invalid_address
)
4352 iplt_base
= this->targ_
->iplt_section()->address();
4353 plt_addr
+= iplt_base
;
4356 plt_addr
+= plt_base
;
4357 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
4358 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
4359 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
4360 Address off
= plt_addr
- got_addr
;
4362 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
4363 gold_error(_("%s: linkage table error against `%s'"),
4364 cs
->first
.object_
->name().c_str(),
4365 cs
->first
.sym_
->demangled_name().c_str());
4367 bool plt_load_toc
= this->targ_
->abiversion() < 2;
4369 = plt_load_toc
&& parameters
->options().plt_static_chain();
4371 = plt_load_toc
&& this->targ_
->plt_thread_safe();
4372 bool use_fake_dep
= false;
4373 Address cmp_branch_off
= 0;
4376 unsigned int pltindex
4377 = ((pltoff
- this->targ_
->first_plt_entry_offset())
4378 / this->targ_
->plt_entry_size());
4380 = (this->targ_
->glink_section()->pltresolve_size
4382 if (pltindex
> 32768)
4383 glinkoff
+= (pltindex
- 32768) * 4;
4385 = this->targ_
->glink_section()->address() + glinkoff
;
4387 = (this->stub_address() + cs
->second
+ 24
4388 + 4 * (ha(off
) != 0)
4389 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4390 + 4 * static_chain
);
4391 cmp_branch_off
= to
- from
;
4392 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
4395 p
= oview
+ cs
->second
;
4398 write_insn
<big_endian
>(p
, std_2_1
+ this->targ_
->stk_toc());
4402 write_insn
<big_endian
>(p
, addis_11_2
+ ha(off
));
4404 write_insn
<big_endian
>(p
, ld_12_11
+ l(off
));
4409 write_insn
<big_endian
>(p
, addis_12_2
+ ha(off
));
4411 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
));
4415 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4417 write_insn
<big_endian
>(p
, addi_11_11
+ l(off
));
4421 write_insn
<big_endian
>(p
, mtctr_12
);
4427 write_insn
<big_endian
>(p
, xor_2_12_12
);
4429 write_insn
<big_endian
>(p
, add_11_11_2
);
4432 write_insn
<big_endian
>(p
, ld_2_11
+ l(off
+ 8));
4436 write_insn
<big_endian
>(p
, ld_11_11
+ l(off
+ 16));
4443 write_insn
<big_endian
>(p
, std_2_1
+ this->targ_
->stk_toc());
4445 write_insn
<big_endian
>(p
, ld_12_2
+ l(off
));
4448 && ha(off
+ 8 + 8 * static_chain
) != ha(off
))
4450 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
));
4454 write_insn
<big_endian
>(p
, mtctr_12
);
4460 write_insn
<big_endian
>(p
, xor_11_12_12
);
4462 write_insn
<big_endian
>(p
, add_2_2_11
);
4467 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16));
4470 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8));
4474 if (thread_safe
&& !use_fake_dep
)
4476 write_insn
<big_endian
>(p
, cmpldi_2_0
);
4478 write_insn
<big_endian
>(p
, bnectr_p4
);
4480 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
4483 write_insn
<big_endian
>(p
, bctr
);
4487 // Write out long branch stubs.
4488 typename
Branch_stub_entries::const_iterator bs
;
4489 for (bs
= this->long_branch_stubs_
.begin();
4490 bs
!= this->long_branch_stubs_
.end();
4493 if (bs
->first
.save_res_
)
4495 p
= oview
+ this->plt_size_
+ bs
->second
;
4496 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
4497 Address delta
= bs
->first
.dest_
- loc
;
4498 if (delta
+ (1 << 25) < 2 << 25)
4499 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
4503 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
4504 gold_assert(brlt_addr
!= invalid_address
);
4505 brlt_addr
+= this->targ_
->brlt_section()->address();
4506 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
4507 Address brltoff
= brlt_addr
- got_addr
;
4508 if (ha(brltoff
) == 0)
4510 write_insn
<big_endian
>(p
, ld_12_2
+ l(brltoff
)), p
+= 4;
4514 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
4515 write_insn
<big_endian
>(p
, ld_12_12
+ l(brltoff
)), p
+= 4;
4517 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
4518 write_insn
<big_endian
>(p
, bctr
);
4524 if (!this->plt_call_stubs_
.empty())
4526 // The base address of the .plt section.
4527 Address plt_base
= this->targ_
->plt_section()->address();
4528 Address iplt_base
= invalid_address
;
4529 // The address of _GLOBAL_OFFSET_TABLE_.
4530 Address g_o_t
= invalid_address
;
4532 // Write out plt call stubs.
4533 typename
Plt_stub_entries::const_iterator cs
;
4534 for (cs
= this->plt_call_stubs_
.begin();
4535 cs
!= this->plt_call_stubs_
.end();
4539 Address plt_addr
= this->plt_off(cs
, &is_iplt
);
4542 if (iplt_base
== invalid_address
)
4543 iplt_base
= this->targ_
->iplt_section()->address();
4544 plt_addr
+= iplt_base
;
4547 plt_addr
+= plt_base
;
4549 p
= oview
+ cs
->second
;
4550 if (parameters
->options().output_is_position_independent())
4553 const Powerpc_relobj
<size
, big_endian
>* ppcobj
4554 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
4555 (cs
->first
.object_
));
4556 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
4558 unsigned int got2
= ppcobj
->got2_shndx();
4559 got_addr
= ppcobj
->get_output_section_offset(got2
);
4560 gold_assert(got_addr
!= invalid_address
);
4561 got_addr
+= (ppcobj
->output_section(got2
)->address()
4562 + cs
->first
.addend_
);
4566 if (g_o_t
== invalid_address
)
4568 const Output_data_got_powerpc
<size
, big_endian
>* got
4569 = this->targ_
->got_section();
4570 g_o_t
= got
->address() + got
->g_o_t();
4575 Address off
= plt_addr
- got_addr
;
4578 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(off
));
4579 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
4580 write_insn
<big_endian
>(p
+ 8, bctr
);
4584 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(off
));
4585 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(off
));
4586 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
4587 write_insn
<big_endian
>(p
+ 12, bctr
);
4592 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
4593 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
4594 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
4595 write_insn
<big_endian
>(p
+ 12, bctr
);
4600 // Write out long branch stubs.
4601 typename
Branch_stub_entries::const_iterator bs
;
4602 for (bs
= this->long_branch_stubs_
.begin();
4603 bs
!= this->long_branch_stubs_
.end();
4606 if (bs
->first
.save_res_
)
4608 p
= oview
+ this->plt_size_
+ bs
->second
;
4609 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
4610 Address delta
= bs
->first
.dest_
- loc
;
4611 if (delta
+ (1 << 25) < 2 << 25)
4612 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
4613 else if (!parameters
->options().output_is_position_independent())
4615 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(bs
->first
.dest_
));
4616 write_insn
<big_endian
>(p
+ 4, addi_12_12
+ l(bs
->first
.dest_
));
4617 write_insn
<big_endian
>(p
+ 8, mtctr_12
);
4618 write_insn
<big_endian
>(p
+ 12, bctr
);
4623 write_insn
<big_endian
>(p
+ 0, mflr_0
);
4624 write_insn
<big_endian
>(p
+ 4, bcl_20_31
);
4625 write_insn
<big_endian
>(p
+ 8, mflr_12
);
4626 write_insn
<big_endian
>(p
+ 12, addis_12_12
+ ha(delta
));
4627 write_insn
<big_endian
>(p
+ 16, addi_12_12
+ l(delta
));
4628 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
4629 write_insn
<big_endian
>(p
+ 24, mtctr_12
);
4630 write_insn
<big_endian
>(p
+ 28, bctr
);
4634 if (this->need_save_res_
)
4636 p
= oview
+ this->plt_size_
+ this->branch_size_
;
4637 memcpy (p
, this->targ_
->savres_section()->contents(),
4638 this->targ_
->savres_section()->data_size());
4642 // Write out .glink.
4644 template<int size
, bool big_endian
>
4646 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
4648 const section_size_type off
= this->offset();
4649 const section_size_type oview_size
=
4650 convert_to_section_size_type(this->data_size());
4651 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4654 // The base address of the .plt section.
4655 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4656 Address plt_base
= this->targ_
->plt_section()->address();
4660 if (this->end_branch_table_
!= 0)
4662 // Write pltresolve stub.
4664 Address after_bcl
= this->address() + 16;
4665 Address pltoff
= plt_base
- after_bcl
;
4667 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
4669 if (this->targ_
->abiversion() < 2)
4671 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
4672 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
4673 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
4674 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
4675 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
4676 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
4677 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
4678 write_insn
<big_endian
>(p
, ld_2_11
+ 8), p
+= 4;
4679 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
4680 write_insn
<big_endian
>(p
, ld_11_11
+ 16), p
+= 4;
4684 write_insn
<big_endian
>(p
, mflr_0
), p
+= 4;
4685 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
4686 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
4687 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
4688 write_insn
<big_endian
>(p
, mtlr_0
), p
+= 4;
4689 write_insn
<big_endian
>(p
, sub_12_12_11
), p
+= 4;
4690 write_insn
<big_endian
>(p
, add_11_2_11
), p
+= 4;
4691 write_insn
<big_endian
>(p
, addi_0_12
+ l(-48)), p
+= 4;
4692 write_insn
<big_endian
>(p
, ld_12_11
+ 0), p
+= 4;
4693 write_insn
<big_endian
>(p
, srdi_0_0_2
), p
+= 4;
4694 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
4695 write_insn
<big_endian
>(p
, ld_11_11
+ 8), p
+= 4;
4697 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
4698 while (p
< oview
+ this->pltresolve_size
)
4699 write_insn
<big_endian
>(p
, nop
), p
+= 4;
4701 // Write lazy link call stubs.
4703 while (p
< oview
+ this->end_branch_table_
)
4705 if (this->targ_
->abiversion() < 2)
4709 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
4713 write_insn
<big_endian
>(p
, lis_0
+ hi(indx
)), p
+= 4;
4714 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
4717 uint32_t branch_off
= 8 - (p
- oview
);
4718 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
4723 Address plt_base
= this->targ_
->plt_section()->address();
4724 Address iplt_base
= invalid_address
;
4725 unsigned int global_entry_off
= (this->end_branch_table_
+ 15) & -16;
4726 Address global_entry_base
= this->address() + global_entry_off
;
4727 typename
Global_entry_stub_entries::const_iterator ge
;
4728 for (ge
= this->global_entry_stubs_
.begin();
4729 ge
!= this->global_entry_stubs_
.end();
4732 p
= oview
+ global_entry_off
+ ge
->second
;
4733 Address plt_addr
= ge
->first
->plt_offset();
4734 if (ge
->first
->type() == elfcpp::STT_GNU_IFUNC
4735 && ge
->first
->can_use_relative_reloc(false))
4737 if (iplt_base
== invalid_address
)
4738 iplt_base
= this->targ_
->iplt_section()->address();
4739 plt_addr
+= iplt_base
;
4742 plt_addr
+= plt_base
;
4743 Address my_addr
= global_entry_base
+ ge
->second
;
4744 Address off
= plt_addr
- my_addr
;
4746 if (off
+ 0x80008000 > 0xffffffff || (off
& 3) != 0)
4747 gold_error(_("%s: linkage table error against `%s'"),
4748 ge
->first
->object()->name().c_str(),
4749 ge
->first
->demangled_name().c_str());
4751 write_insn
<big_endian
>(p
, addis_12_12
+ ha(off
)), p
+= 4;
4752 write_insn
<big_endian
>(p
, ld_12_12
+ l(off
)), p
+= 4;
4753 write_insn
<big_endian
>(p
, mtctr_12
), p
+= 4;
4754 write_insn
<big_endian
>(p
, bctr
);
4759 const Output_data_got_powerpc
<size
, big_endian
>* got
4760 = this->targ_
->got_section();
4761 // The address of _GLOBAL_OFFSET_TABLE_.
4762 Address g_o_t
= got
->address() + got
->g_o_t();
4764 // Write out pltresolve branch table.
4766 unsigned int the_end
= oview_size
- this->pltresolve_size
;
4767 unsigned char* end_p
= oview
+ the_end
;
4768 while (p
< end_p
- 8 * 4)
4769 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
4771 write_insn
<big_endian
>(p
, nop
), p
+= 4;
4773 // Write out pltresolve call stub.
4774 if (parameters
->options().output_is_position_independent())
4776 Address res0_off
= 0;
4777 Address after_bcl_off
= the_end
+ 12;
4778 Address bcl_res0
= after_bcl_off
- res0_off
;
4780 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
4781 write_insn
<big_endian
>(p
+ 4, mflr_0
);
4782 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
4783 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
4784 write_insn
<big_endian
>(p
+ 16, mflr_12
);
4785 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
4786 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
4788 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
4790 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
4791 if (ha(got_bcl
) == ha(got_bcl
+ 4))
4793 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
4794 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
4798 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
4799 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
4801 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
4802 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
4803 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
4804 write_insn
<big_endian
>(p
+ 52, bctr
);
4805 write_insn
<big_endian
>(p
+ 56, nop
);
4806 write_insn
<big_endian
>(p
+ 60, nop
);
4810 Address res0
= this->address();
4812 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
4813 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
4814 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
4815 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
4817 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
4818 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
4819 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
4820 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
4821 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
4822 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
4824 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
4825 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
4826 write_insn
<big_endian
>(p
+ 32, bctr
);
4827 write_insn
<big_endian
>(p
+ 36, nop
);
4828 write_insn
<big_endian
>(p
+ 40, nop
);
4829 write_insn
<big_endian
>(p
+ 44, nop
);
4830 write_insn
<big_endian
>(p
+ 48, nop
);
4831 write_insn
<big_endian
>(p
+ 52, nop
);
4832 write_insn
<big_endian
>(p
+ 56, nop
);
4833 write_insn
<big_endian
>(p
+ 60, nop
);
4838 of
->write_output_view(off
, oview_size
, oview
);
4842 // A class to handle linker generated save/restore functions.
4844 template<int size
, bool big_endian
>
4845 class Output_data_save_res
: public Output_section_data_build
4848 Output_data_save_res(Symbol_table
* symtab
);
4850 const unsigned char*
4857 // Write to a map file.
4859 do_print_to_mapfile(Mapfile
* mapfile
) const
4860 { mapfile
->print_output_data(this, _("** save/restore")); }
4863 do_write(Output_file
*);
4866 // The maximum size of save/restore contents.
4867 static const unsigned int savres_max
= 218*4;
4870 savres_define(Symbol_table
* symtab
,
4872 unsigned int lo
, unsigned int hi
,
4873 unsigned char* write_ent(unsigned char*, int),
4874 unsigned char* write_tail(unsigned char*, int));
4876 unsigned char *contents_
;
4879 template<bool big_endian
>
4880 static unsigned char*
4881 savegpr0(unsigned char* p
, int r
)
4883 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4884 write_insn
<big_endian
>(p
, insn
);
4888 template<bool big_endian
>
4889 static unsigned char*
4890 savegpr0_tail(unsigned char* p
, int r
)
4892 p
= savegpr0
<big_endian
>(p
, r
);
4893 uint32_t insn
= std_0_1
+ 16;
4894 write_insn
<big_endian
>(p
, insn
);
4896 write_insn
<big_endian
>(p
, blr
);
4900 template<bool big_endian
>
4901 static unsigned char*
4902 restgpr0(unsigned char* p
, int r
)
4904 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4905 write_insn
<big_endian
>(p
, insn
);
4909 template<bool big_endian
>
4910 static unsigned char*
4911 restgpr0_tail(unsigned char* p
, int r
)
4913 uint32_t insn
= ld_0_1
+ 16;
4914 write_insn
<big_endian
>(p
, insn
);
4916 p
= restgpr0
<big_endian
>(p
, r
);
4917 write_insn
<big_endian
>(p
, mtlr_0
);
4921 p
= restgpr0
<big_endian
>(p
, 30);
4922 p
= restgpr0
<big_endian
>(p
, 31);
4924 write_insn
<big_endian
>(p
, blr
);
4928 template<bool big_endian
>
4929 static unsigned char*
4930 savegpr1(unsigned char* p
, int r
)
4932 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4933 write_insn
<big_endian
>(p
, insn
);
4937 template<bool big_endian
>
4938 static unsigned char*
4939 savegpr1_tail(unsigned char* p
, int r
)
4941 p
= savegpr1
<big_endian
>(p
, r
);
4942 write_insn
<big_endian
>(p
, blr
);
4946 template<bool big_endian
>
4947 static unsigned char*
4948 restgpr1(unsigned char* p
, int r
)
4950 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4951 write_insn
<big_endian
>(p
, insn
);
4955 template<bool big_endian
>
4956 static unsigned char*
4957 restgpr1_tail(unsigned char* p
, int r
)
4959 p
= restgpr1
<big_endian
>(p
, r
);
4960 write_insn
<big_endian
>(p
, blr
);
4964 template<bool big_endian
>
4965 static unsigned char*
4966 savefpr(unsigned char* p
, int r
)
4968 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4969 write_insn
<big_endian
>(p
, insn
);
4973 template<bool big_endian
>
4974 static unsigned char*
4975 savefpr0_tail(unsigned char* p
, int r
)
4977 p
= savefpr
<big_endian
>(p
, r
);
4978 write_insn
<big_endian
>(p
, std_0_1
+ 16);
4980 write_insn
<big_endian
>(p
, blr
);
4984 template<bool big_endian
>
4985 static unsigned char*
4986 restfpr(unsigned char* p
, int r
)
4988 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4989 write_insn
<big_endian
>(p
, insn
);
4993 template<bool big_endian
>
4994 static unsigned char*
4995 restfpr0_tail(unsigned char* p
, int r
)
4997 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
4999 p
= restfpr
<big_endian
>(p
, r
);
5000 write_insn
<big_endian
>(p
, mtlr_0
);
5004 p
= restfpr
<big_endian
>(p
, 30);
5005 p
= restfpr
<big_endian
>(p
, 31);
5007 write_insn
<big_endian
>(p
, blr
);
5011 template<bool big_endian
>
5012 static unsigned char*
5013 savefpr1_tail(unsigned char* p
, int r
)
5015 p
= savefpr
<big_endian
>(p
, r
);
5016 write_insn
<big_endian
>(p
, blr
);
5020 template<bool big_endian
>
5021 static unsigned char*
5022 restfpr1_tail(unsigned char* p
, int r
)
5024 p
= restfpr
<big_endian
>(p
, r
);
5025 write_insn
<big_endian
>(p
, blr
);
5029 template<bool big_endian
>
5030 static unsigned char*
5031 savevr(unsigned char* p
, int r
)
5033 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5034 write_insn
<big_endian
>(p
, insn
);
5036 insn
= stvx_0_12_0
+ (r
<< 21);
5037 write_insn
<big_endian
>(p
, insn
);
5041 template<bool big_endian
>
5042 static unsigned char*
5043 savevr_tail(unsigned char* p
, int r
)
5045 p
= savevr
<big_endian
>(p
, r
);
5046 write_insn
<big_endian
>(p
, blr
);
5050 template<bool big_endian
>
5051 static unsigned char*
5052 restvr(unsigned char* p
, int r
)
5054 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
5055 write_insn
<big_endian
>(p
, insn
);
5057 insn
= lvx_0_12_0
+ (r
<< 21);
5058 write_insn
<big_endian
>(p
, insn
);
5062 template<bool big_endian
>
5063 static unsigned char*
5064 restvr_tail(unsigned char* p
, int r
)
5066 p
= restvr
<big_endian
>(p
, r
);
5067 write_insn
<big_endian
>(p
, blr
);
5072 template<int size
, bool big_endian
>
5073 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
5074 Symbol_table
* symtab
)
5075 : Output_section_data_build(4),
5078 this->savres_define(symtab
,
5079 "_savegpr0_", 14, 31,
5080 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
5081 this->savres_define(symtab
,
5082 "_restgpr0_", 14, 29,
5083 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
5084 this->savres_define(symtab
,
5085 "_restgpr0_", 30, 31,
5086 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
5087 this->savres_define(symtab
,
5088 "_savegpr1_", 14, 31,
5089 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
5090 this->savres_define(symtab
,
5091 "_restgpr1_", 14, 31,
5092 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
5093 this->savres_define(symtab
,
5094 "_savefpr_", 14, 31,
5095 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
5096 this->savres_define(symtab
,
5097 "_restfpr_", 14, 29,
5098 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
5099 this->savres_define(symtab
,
5100 "_restfpr_", 30, 31,
5101 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
5102 this->savres_define(symtab
,
5104 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
5105 this->savres_define(symtab
,
5107 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
5108 this->savres_define(symtab
,
5110 savevr
<big_endian
>, savevr_tail
<big_endian
>);
5111 this->savres_define(symtab
,
5113 restvr
<big_endian
>, restvr_tail
<big_endian
>);
5116 template<int size
, bool big_endian
>
5118 Output_data_save_res
<size
, big_endian
>::savres_define(
5119 Symbol_table
* symtab
,
5121 unsigned int lo
, unsigned int hi
,
5122 unsigned char* write_ent(unsigned char*, int),
5123 unsigned char* write_tail(unsigned char*, int))
5125 size_t len
= strlen(name
);
5126 bool writing
= false;
5129 memcpy(sym
, name
, len
);
5132 for (unsigned int i
= lo
; i
<= hi
; i
++)
5134 sym
[len
+ 0] = i
/ 10 + '0';
5135 sym
[len
+ 1] = i
% 10 + '0';
5136 Symbol
* gsym
= symtab
->lookup(sym
);
5137 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
5138 writing
= writing
|| refd
;
5141 if (this->contents_
== NULL
)
5142 this->contents_
= new unsigned char[this->savres_max
];
5144 section_size_type value
= this->current_data_size();
5145 unsigned char* p
= this->contents_
+ value
;
5147 p
= write_ent(p
, i
);
5149 p
= write_tail(p
, i
);
5150 section_size_type cur_size
= p
- this->contents_
;
5151 this->set_current_data_size(cur_size
);
5153 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
5154 this, value
, cur_size
- value
,
5155 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
5156 elfcpp::STV_HIDDEN
, 0, false, false);
5161 // Write out save/restore.
5163 template<int size
, bool big_endian
>
5165 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
5167 const section_size_type off
= this->offset();
5168 const section_size_type oview_size
=
5169 convert_to_section_size_type(this->data_size());
5170 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
5171 memcpy(oview
, this->contents_
, oview_size
);
5172 of
->write_output_view(off
, oview_size
, oview
);
5176 // Create the glink section.
5178 template<int size
, bool big_endian
>
5180 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
5182 if (this->glink_
== NULL
)
5184 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
5185 this->glink_
->add_eh_frame(layout
);
5186 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
5187 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
5188 this->glink_
, ORDER_TEXT
, false);
5192 // Create a PLT entry for a global symbol.
5194 template<int size
, bool big_endian
>
5196 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
5200 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
5201 && gsym
->can_use_relative_reloc(false))
5203 if (this->iplt_
== NULL
)
5204 this->make_iplt_section(symtab
, layout
);
5205 this->iplt_
->add_ifunc_entry(gsym
);
5209 if (this->plt_
== NULL
)
5210 this->make_plt_section(symtab
, layout
);
5211 this->plt_
->add_entry(gsym
);
5215 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5217 template<int size
, bool big_endian
>
5219 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
5220 Symbol_table
* symtab
,
5222 Sized_relobj_file
<size
, big_endian
>* relobj
,
5225 if (this->iplt_
== NULL
)
5226 this->make_iplt_section(symtab
, layout
);
5227 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
5230 // Return the number of entries in the PLT.
5232 template<int size
, bool big_endian
>
5234 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
5236 if (this->plt_
== NULL
)
5238 return this->plt_
->entry_count();
5241 // Create a GOT entry for local dynamic __tls_get_addr calls.
5243 template<int size
, bool big_endian
>
5245 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
5246 Symbol_table
* symtab
,
5248 Sized_relobj_file
<size
, big_endian
>* object
)
5250 if (this->tlsld_got_offset_
== -1U)
5252 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
5253 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
5254 Output_data_got_powerpc
<size
, big_endian
>* got
5255 = this->got_section(symtab
, layout
);
5256 unsigned int got_offset
= got
->add_constant_pair(0, 0);
5257 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
5259 this->tlsld_got_offset_
= got_offset
;
5261 return this->tlsld_got_offset_
;
5264 // Get the Reference_flags for a particular relocation.
5266 template<int size
, bool big_endian
>
5268 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(
5269 unsigned int r_type
,
5270 const Target_powerpc
* target
)
5276 case elfcpp::R_POWERPC_NONE
:
5277 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5278 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5279 case elfcpp::R_PPC64_TOC
:
5280 // No symbol reference.
5283 case elfcpp::R_PPC64_ADDR64
:
5284 case elfcpp::R_PPC64_UADDR64
:
5285 case elfcpp::R_POWERPC_ADDR32
:
5286 case elfcpp::R_POWERPC_UADDR32
:
5287 case elfcpp::R_POWERPC_ADDR16
:
5288 case elfcpp::R_POWERPC_UADDR16
:
5289 case elfcpp::R_POWERPC_ADDR16_LO
:
5290 case elfcpp::R_POWERPC_ADDR16_HI
:
5291 case elfcpp::R_POWERPC_ADDR16_HA
:
5292 ref
= Symbol::ABSOLUTE_REF
;
5295 case elfcpp::R_POWERPC_ADDR24
:
5296 case elfcpp::R_POWERPC_ADDR14
:
5297 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5298 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5299 ref
= Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
5302 case elfcpp::R_PPC64_REL64
:
5303 case elfcpp::R_POWERPC_REL32
:
5304 case elfcpp::R_PPC_LOCAL24PC
:
5305 case elfcpp::R_POWERPC_REL16
:
5306 case elfcpp::R_POWERPC_REL16_LO
:
5307 case elfcpp::R_POWERPC_REL16_HI
:
5308 case elfcpp::R_POWERPC_REL16_HA
:
5309 ref
= Symbol::RELATIVE_REF
;
5312 case elfcpp::R_POWERPC_REL24
:
5313 case elfcpp::R_PPC_PLTREL24
:
5314 case elfcpp::R_POWERPC_REL14
:
5315 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5316 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5317 ref
= Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
5320 case elfcpp::R_POWERPC_GOT16
:
5321 case elfcpp::R_POWERPC_GOT16_LO
:
5322 case elfcpp::R_POWERPC_GOT16_HI
:
5323 case elfcpp::R_POWERPC_GOT16_HA
:
5324 case elfcpp::R_PPC64_GOT16_DS
:
5325 case elfcpp::R_PPC64_GOT16_LO_DS
:
5326 case elfcpp::R_PPC64_TOC16
:
5327 case elfcpp::R_PPC64_TOC16_LO
:
5328 case elfcpp::R_PPC64_TOC16_HI
:
5329 case elfcpp::R_PPC64_TOC16_HA
:
5330 case elfcpp::R_PPC64_TOC16_DS
:
5331 case elfcpp::R_PPC64_TOC16_LO_DS
:
5332 ref
= Symbol::RELATIVE_REF
;
5335 case elfcpp::R_POWERPC_GOT_TPREL16
:
5336 case elfcpp::R_POWERPC_TLS
:
5337 ref
= Symbol::TLS_REF
;
5340 case elfcpp::R_POWERPC_COPY
:
5341 case elfcpp::R_POWERPC_GLOB_DAT
:
5342 case elfcpp::R_POWERPC_JMP_SLOT
:
5343 case elfcpp::R_POWERPC_RELATIVE
:
5344 case elfcpp::R_POWERPC_DTPMOD
:
5346 // Not expected. We will give an error later.
5350 if (size
== 64 && target
->abiversion() < 2)
5351 ref
|= Symbol::FUNC_DESC_ABI
;
5355 // Report an unsupported relocation against a local symbol.
5357 template<int size
, bool big_endian
>
5359 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
5360 Sized_relobj_file
<size
, big_endian
>* object
,
5361 unsigned int r_type
)
5363 gold_error(_("%s: unsupported reloc %u against local symbol"),
5364 object
->name().c_str(), r_type
);
5367 // We are about to emit a dynamic relocation of type R_TYPE. If the
5368 // dynamic linker does not support it, issue an error.
5370 template<int size
, bool big_endian
>
5372 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
5373 unsigned int r_type
)
5375 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
5377 // These are the relocation types supported by glibc for both 32-bit
5378 // and 64-bit powerpc.
5381 case elfcpp::R_POWERPC_NONE
:
5382 case elfcpp::R_POWERPC_RELATIVE
:
5383 case elfcpp::R_POWERPC_GLOB_DAT
:
5384 case elfcpp::R_POWERPC_DTPMOD
:
5385 case elfcpp::R_POWERPC_DTPREL
:
5386 case elfcpp::R_POWERPC_TPREL
:
5387 case elfcpp::R_POWERPC_JMP_SLOT
:
5388 case elfcpp::R_POWERPC_COPY
:
5389 case elfcpp::R_POWERPC_IRELATIVE
:
5390 case elfcpp::R_POWERPC_ADDR32
:
5391 case elfcpp::R_POWERPC_UADDR32
:
5392 case elfcpp::R_POWERPC_ADDR24
:
5393 case elfcpp::R_POWERPC_ADDR16
:
5394 case elfcpp::R_POWERPC_UADDR16
:
5395 case elfcpp::R_POWERPC_ADDR16_LO
:
5396 case elfcpp::R_POWERPC_ADDR16_HI
:
5397 case elfcpp::R_POWERPC_ADDR16_HA
:
5398 case elfcpp::R_POWERPC_ADDR14
:
5399 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5400 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5401 case elfcpp::R_POWERPC_REL32
:
5402 case elfcpp::R_POWERPC_REL24
:
5403 case elfcpp::R_POWERPC_TPREL16
:
5404 case elfcpp::R_POWERPC_TPREL16_LO
:
5405 case elfcpp::R_POWERPC_TPREL16_HI
:
5406 case elfcpp::R_POWERPC_TPREL16_HA
:
5417 // These are the relocation types supported only on 64-bit.
5418 case elfcpp::R_PPC64_ADDR64
:
5419 case elfcpp::R_PPC64_UADDR64
:
5420 case elfcpp::R_PPC64_JMP_IREL
:
5421 case elfcpp::R_PPC64_ADDR16_DS
:
5422 case elfcpp::R_PPC64_ADDR16_LO_DS
:
5423 case elfcpp::R_PPC64_ADDR16_HIGH
:
5424 case elfcpp::R_PPC64_ADDR16_HIGHA
:
5425 case elfcpp::R_PPC64_ADDR16_HIGHER
:
5426 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
5427 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
5428 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
5429 case elfcpp::R_PPC64_REL64
:
5430 case elfcpp::R_POWERPC_ADDR30
:
5431 case elfcpp::R_PPC64_TPREL16_DS
:
5432 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5433 case elfcpp::R_PPC64_TPREL16_HIGH
:
5434 case elfcpp::R_PPC64_TPREL16_HIGHA
:
5435 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5436 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5437 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5438 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5449 // These are the relocation types supported only on 32-bit.
5450 // ??? glibc ld.so doesn't need to support these.
5451 case elfcpp::R_POWERPC_DTPREL16
:
5452 case elfcpp::R_POWERPC_DTPREL16_LO
:
5453 case elfcpp::R_POWERPC_DTPREL16_HI
:
5454 case elfcpp::R_POWERPC_DTPREL16_HA
:
5462 // This prevents us from issuing more than one error per reloc
5463 // section. But we can still wind up issuing more than one
5464 // error per object file.
5465 if (this->issued_non_pic_error_
)
5467 gold_assert(parameters
->options().output_is_position_independent());
5468 object
->error(_("requires unsupported dynamic reloc; "
5469 "recompile with -fPIC"));
5470 this->issued_non_pic_error_
= true;
5474 // Return whether we need to make a PLT entry for a relocation of the
5475 // given type against a STT_GNU_IFUNC symbol.
5477 template<int size
, bool big_endian
>
5479 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
5480 Target_powerpc
<size
, big_endian
>* target
,
5481 Sized_relobj_file
<size
, big_endian
>* object
,
5482 unsigned int r_type
,
5485 // In non-pic code any reference will resolve to the plt call stub
5486 // for the ifunc symbol.
5487 if ((size
== 32 || target
->abiversion() >= 2)
5488 && !parameters
->options().output_is_position_independent())
5493 // Word size refs from data sections are OK, but don't need a PLT entry.
5494 case elfcpp::R_POWERPC_ADDR32
:
5495 case elfcpp::R_POWERPC_UADDR32
:
5500 case elfcpp::R_PPC64_ADDR64
:
5501 case elfcpp::R_PPC64_UADDR64
:
5506 // GOT refs are good, but also don't need a PLT entry.
5507 case elfcpp::R_POWERPC_GOT16
:
5508 case elfcpp::R_POWERPC_GOT16_LO
:
5509 case elfcpp::R_POWERPC_GOT16_HI
:
5510 case elfcpp::R_POWERPC_GOT16_HA
:
5511 case elfcpp::R_PPC64_GOT16_DS
:
5512 case elfcpp::R_PPC64_GOT16_LO_DS
:
5515 // Function calls are good, and these do need a PLT entry.
5516 case elfcpp::R_POWERPC_ADDR24
:
5517 case elfcpp::R_POWERPC_ADDR14
:
5518 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5519 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5520 case elfcpp::R_POWERPC_REL24
:
5521 case elfcpp::R_PPC_PLTREL24
:
5522 case elfcpp::R_POWERPC_REL14
:
5523 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5524 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5531 // Anything else is a problem.
5532 // If we are building a static executable, the libc startup function
5533 // responsible for applying indirect function relocations is going
5534 // to complain about the reloc type.
5535 // If we are building a dynamic executable, we will have a text
5536 // relocation. The dynamic loader will set the text segment
5537 // writable and non-executable to apply text relocations. So we'll
5538 // segfault when trying to run the indirection function to resolve
5541 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
5542 object
->name().c_str(), r_type
);
5546 // Scan a relocation for a local symbol.
5548 template<int size
, bool big_endian
>
5550 Target_powerpc
<size
, big_endian
>::Scan::local(
5551 Symbol_table
* symtab
,
5553 Target_powerpc
<size
, big_endian
>* target
,
5554 Sized_relobj_file
<size
, big_endian
>* object
,
5555 unsigned int data_shndx
,
5556 Output_section
* output_section
,
5557 const elfcpp::Rela
<size
, big_endian
>& reloc
,
5558 unsigned int r_type
,
5559 const elfcpp::Sym
<size
, big_endian
>& lsym
,
5562 this->maybe_skip_tls_get_addr_call(r_type
, NULL
);
5564 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
5565 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
5567 this->expect_tls_get_addr_call();
5568 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
5569 if (tls_type
!= tls::TLSOPT_NONE
)
5570 this->skip_next_tls_get_addr_call();
5572 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
5573 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
5575 this->expect_tls_get_addr_call();
5576 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5577 if (tls_type
!= tls::TLSOPT_NONE
)
5578 this->skip_next_tls_get_addr_call();
5581 Powerpc_relobj
<size
, big_endian
>* ppc_object
5582 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5587 && data_shndx
== ppc_object
->opd_shndx()
5588 && r_type
== elfcpp::R_PPC64_ADDR64
)
5589 ppc_object
->set_opd_discard(reloc
.get_r_offset());
5593 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5594 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
5595 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(target
, object
, r_type
, true))
5597 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5598 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5599 r_type
, r_sym
, reloc
.get_r_addend());
5600 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
5605 case elfcpp::R_POWERPC_NONE
:
5606 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5607 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5608 case elfcpp::R_PPC64_TOCSAVE
:
5609 case elfcpp::R_POWERPC_TLS
:
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
:
5987 case elfcpp::R_PPC64_TOC
:
5989 Output_data_got_powerpc
<size
, big_endian
>* got
5990 = target
->got_section(symtab
, layout
);
5991 if (parameters
->options().output_is_position_independent())
5993 Address off
= reloc
.get_r_offset();
5995 && data_shndx
== ppc_object
->opd_shndx()
5996 && ppc_object
->get_opd_discard(off
- 8))
5999 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6000 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
6001 if (data_shndx
!= ppc_object
->opd_shndx())
6002 symobj
= static_cast
6003 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
6004 rela_dyn
->add_output_section_relative(got
->output_section(),
6005 elfcpp::R_POWERPC_RELATIVE
,
6007 object
, data_shndx
, off
,
6008 symobj
->toc_base_offset());
6013 case elfcpp::R_PPC64_ADDR64
:
6015 && target
->abiversion() < 2
6016 && data_shndx
== ppc_object
->opd_shndx()
6017 && (gsym
->is_defined_in_discarded_section()
6018 || gsym
->object() != object
))
6020 ppc_object
->set_opd_discard(reloc
.get_r_offset());
6024 case elfcpp::R_PPC64_UADDR64
:
6025 case elfcpp::R_POWERPC_ADDR32
:
6026 case elfcpp::R_POWERPC_UADDR32
:
6027 case elfcpp::R_POWERPC_ADDR24
:
6028 case elfcpp::R_POWERPC_ADDR16
:
6029 case elfcpp::R_POWERPC_ADDR16_LO
:
6030 case elfcpp::R_POWERPC_ADDR16_HI
:
6031 case elfcpp::R_POWERPC_ADDR16_HA
:
6032 case elfcpp::R_POWERPC_UADDR16
:
6033 case elfcpp::R_PPC64_ADDR16_HIGH
:
6034 case elfcpp::R_PPC64_ADDR16_HIGHA
:
6035 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6036 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6037 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6038 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6039 case elfcpp::R_PPC64_ADDR16_DS
:
6040 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6041 case elfcpp::R_POWERPC_ADDR14
:
6042 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6043 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6045 // Make a PLT entry if necessary.
6046 if (gsym
->needs_plt_entry())
6048 // Since this is not a PC-relative relocation, we may be
6049 // taking the address of a function. In that case we need to
6050 // set the entry in the dynamic symbol table to the address of
6051 // the PLT call stub.
6052 bool need_ifunc_plt
= false;
6053 if ((size
== 32 || target
->abiversion() >= 2)
6054 && gsym
->is_from_dynobj()
6055 && !parameters
->options().output_is_position_independent())
6057 gsym
->set_needs_dynsym_value();
6058 need_ifunc_plt
= true;
6060 if (!is_ifunc
|| (!pushed_ifunc
&& need_ifunc_plt
))
6062 target
->push_branch(ppc_object
, data_shndx
,
6063 reloc
.get_r_offset(), r_type
,
6064 elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
6065 reloc
.get_r_addend());
6066 target
->make_plt_entry(symtab
, layout
, gsym
);
6069 // Make a dynamic relocation if necessary.
6070 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
))
6071 || (size
== 64 && is_ifunc
&& target
->abiversion() < 2))
6073 if (!parameters
->options().output_is_position_independent()
6074 && gsym
->may_need_copy_reloc())
6076 target
->copy_reloc(symtab
, layout
, object
,
6077 data_shndx
, output_section
, gsym
, reloc
);
6079 else if ((((size
== 32
6080 && r_type
== elfcpp::R_POWERPC_ADDR32
)
6082 && r_type
== elfcpp::R_PPC64_ADDR64
6083 && target
->abiversion() >= 2))
6084 && gsym
->can_use_relative_reloc(false)
6085 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
6086 && parameters
->options().shared()))
6088 && r_type
== elfcpp::R_PPC64_ADDR64
6089 && target
->abiversion() < 2
6090 && (gsym
->can_use_relative_reloc(false)
6091 || data_shndx
== ppc_object
->opd_shndx())))
6093 Reloc_section
* rela_dyn
6094 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6095 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6096 : elfcpp::R_POWERPC_RELATIVE
);
6097 rela_dyn
->add_symbolless_global_addend(
6098 gsym
, dynrel
, output_section
, object
, data_shndx
,
6099 reloc
.get_r_offset(), reloc
.get_r_addend());
6103 Reloc_section
* rela_dyn
6104 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6105 check_non_pic(object
, r_type
);
6106 rela_dyn
->add_global(gsym
, r_type
, output_section
,
6108 reloc
.get_r_offset(),
6109 reloc
.get_r_addend());
6115 case elfcpp::R_PPC_PLTREL24
:
6116 case elfcpp::R_POWERPC_REL24
:
6119 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6121 elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
6122 reloc
.get_r_addend());
6123 if (gsym
->needs_plt_entry()
6124 || (!gsym
->final_value_is_known()
6125 && (gsym
->is_undefined()
6126 || gsym
->is_from_dynobj()
6127 || gsym
->is_preemptible())))
6128 target
->make_plt_entry(symtab
, layout
, gsym
);
6132 case elfcpp::R_PPC64_REL64
:
6133 case elfcpp::R_POWERPC_REL32
:
6134 // Make a dynamic relocation if necessary.
6135 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
, target
)))
6137 if (!parameters
->options().output_is_position_independent()
6138 && gsym
->may_need_copy_reloc())
6140 target
->copy_reloc(symtab
, layout
, object
,
6141 data_shndx
, output_section
, gsym
,
6146 Reloc_section
* rela_dyn
6147 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6148 check_non_pic(object
, r_type
);
6149 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
6150 data_shndx
, reloc
.get_r_offset(),
6151 reloc
.get_r_addend());
6156 case elfcpp::R_POWERPC_REL14
:
6157 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6158 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6160 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
6161 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
6162 reloc
.get_r_addend());
6165 case elfcpp::R_POWERPC_REL16
:
6166 case elfcpp::R_POWERPC_REL16_LO
:
6167 case elfcpp::R_POWERPC_REL16_HI
:
6168 case elfcpp::R_POWERPC_REL16_HA
:
6169 case elfcpp::R_POWERPC_REL16DX_HA
:
6170 case elfcpp::R_POWERPC_SECTOFF
:
6171 case elfcpp::R_POWERPC_SECTOFF_LO
:
6172 case elfcpp::R_POWERPC_SECTOFF_HI
:
6173 case elfcpp::R_POWERPC_SECTOFF_HA
:
6174 case elfcpp::R_PPC64_SECTOFF_DS
:
6175 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6176 case elfcpp::R_POWERPC_TPREL16
:
6177 case elfcpp::R_POWERPC_TPREL16_LO
:
6178 case elfcpp::R_POWERPC_TPREL16_HI
:
6179 case elfcpp::R_POWERPC_TPREL16_HA
:
6180 case elfcpp::R_PPC64_TPREL16_DS
:
6181 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6182 case elfcpp::R_PPC64_TPREL16_HIGH
:
6183 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6184 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6185 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6186 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6187 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6188 case elfcpp::R_POWERPC_DTPREL16
:
6189 case elfcpp::R_POWERPC_DTPREL16_LO
:
6190 case elfcpp::R_POWERPC_DTPREL16_HI
:
6191 case elfcpp::R_POWERPC_DTPREL16_HA
:
6192 case elfcpp::R_PPC64_DTPREL16_DS
:
6193 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6194 case elfcpp::R_PPC64_DTPREL16_HIGH
:
6195 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
6196 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6197 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6198 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6199 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6200 case elfcpp::R_PPC64_TLSGD
:
6201 case elfcpp::R_PPC64_TLSLD
:
6202 case elfcpp::R_PPC64_ADDR64_LOCAL
:
6205 case elfcpp::R_POWERPC_GOT16
:
6206 case elfcpp::R_POWERPC_GOT16_LO
:
6207 case elfcpp::R_POWERPC_GOT16_HI
:
6208 case elfcpp::R_POWERPC_GOT16_HA
:
6209 case elfcpp::R_PPC64_GOT16_DS
:
6210 case elfcpp::R_PPC64_GOT16_LO_DS
:
6212 // The symbol requires a GOT entry.
6213 Output_data_got_powerpc
<size
, big_endian
>* got
;
6215 got
= target
->got_section(symtab
, layout
);
6216 if (gsym
->final_value_is_known())
6219 && (size
== 32 || target
->abiversion() >= 2))
6220 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
6222 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
6224 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
6226 // If we are generating a shared object or a pie, this
6227 // symbol's GOT entry will be set by a dynamic relocation.
6228 unsigned int off
= got
->add_constant(0);
6229 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
6231 Reloc_section
* rela_dyn
6232 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
6234 if (gsym
->can_use_relative_reloc(false)
6236 || target
->abiversion() >= 2)
6237 && gsym
->visibility() == elfcpp::STV_PROTECTED
6238 && parameters
->options().shared()))
6240 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
6241 : elfcpp::R_POWERPC_RELATIVE
);
6242 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
6246 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
6247 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
6253 case elfcpp::R_PPC64_TOC16
:
6254 case elfcpp::R_PPC64_TOC16_LO
:
6255 case elfcpp::R_PPC64_TOC16_HI
:
6256 case elfcpp::R_PPC64_TOC16_HA
:
6257 case elfcpp::R_PPC64_TOC16_DS
:
6258 case elfcpp::R_PPC64_TOC16_LO_DS
:
6259 // We need a GOT section.
6260 target
->got_section(symtab
, layout
);
6263 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6264 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6265 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6266 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6268 const bool final
= gsym
->final_value_is_known();
6269 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6270 if (tls_type
== tls::TLSOPT_NONE
)
6272 Output_data_got_powerpc
<size
, big_endian
>* got
6273 = target
->got_section(symtab
, layout
);
6274 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6275 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
, rela_dyn
,
6276 elfcpp::R_POWERPC_DTPMOD
,
6277 elfcpp::R_POWERPC_DTPREL
);
6279 else if (tls_type
== tls::TLSOPT_TO_IE
)
6281 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
6283 Output_data_got_powerpc
<size
, big_endian
>* got
6284 = target
->got_section(symtab
, layout
);
6285 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6286 if (gsym
->is_undefined()
6287 || gsym
->is_from_dynobj())
6289 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
6290 elfcpp::R_POWERPC_TPREL
);
6294 unsigned int off
= got
->add_constant(0);
6295 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
6296 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
6297 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
6302 else if (tls_type
== tls::TLSOPT_TO_LE
)
6304 // no GOT relocs needed for Local Exec.
6311 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6312 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6313 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6314 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6316 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6317 if (tls_type
== tls::TLSOPT_NONE
)
6318 target
->tlsld_got_offset(symtab
, layout
, object
);
6319 else if (tls_type
== tls::TLSOPT_TO_LE
)
6321 // no GOT relocs needed for Local Exec.
6322 if (parameters
->options().emit_relocs())
6324 Output_section
* os
= layout
->tls_segment()->first_section();
6325 gold_assert(os
!= NULL
);
6326 os
->set_needs_symtab_index();
6334 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6335 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6336 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6337 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6339 Output_data_got_powerpc
<size
, big_endian
>* got
6340 = target
->got_section(symtab
, layout
);
6341 if (!gsym
->final_value_is_known()
6342 && (gsym
->is_from_dynobj()
6343 || gsym
->is_undefined()
6344 || gsym
->is_preemptible()))
6345 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
6346 target
->rela_dyn_section(layout
),
6347 elfcpp::R_POWERPC_DTPREL
);
6349 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
6353 case elfcpp::R_POWERPC_GOT_TPREL16
:
6354 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6355 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6356 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6358 const bool final
= gsym
->final_value_is_known();
6359 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
6360 if (tls_type
== tls::TLSOPT_NONE
)
6362 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
6364 Output_data_got_powerpc
<size
, big_endian
>* got
6365 = target
->got_section(symtab
, layout
);
6366 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
6367 if (gsym
->is_undefined()
6368 || gsym
->is_from_dynobj())
6370 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
6371 elfcpp::R_POWERPC_TPREL
);
6375 unsigned int off
= got
->add_constant(0);
6376 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
6377 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
6378 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
6383 else if (tls_type
== tls::TLSOPT_TO_LE
)
6385 // no GOT relocs needed for Local Exec.
6393 unsupported_reloc_global(object
, r_type
, gsym
);
6399 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6400 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6401 case elfcpp::R_POWERPC_GOT_TPREL16
:
6402 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6403 case elfcpp::R_POWERPC_GOT16
:
6404 case elfcpp::R_PPC64_GOT16_DS
:
6405 case elfcpp::R_PPC64_TOC16
:
6406 case elfcpp::R_PPC64_TOC16_DS
:
6407 ppc_object
->set_has_small_toc_reloc();
6413 // Process relocations for gc.
6415 template<int size
, bool big_endian
>
6417 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
6418 Symbol_table
* symtab
,
6420 Sized_relobj_file
<size
, big_endian
>* object
,
6421 unsigned int data_shndx
,
6423 const unsigned char* prelocs
,
6425 Output_section
* output_section
,
6426 bool needs_special_offset_handling
,
6427 size_t local_symbol_count
,
6428 const unsigned char* plocal_symbols
)
6430 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
6431 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
6432 Powerpc_relobj
<size
, big_endian
>* ppc_object
6433 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
6435 ppc_object
->set_opd_valid();
6436 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
6438 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
6439 for (p
= ppc_object
->access_from_map()->begin();
6440 p
!= ppc_object
->access_from_map()->end();
6443 Address dst_off
= p
->first
;
6444 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
6445 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
6446 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
6448 Relobj
* src_obj
= s
->first
;
6449 unsigned int src_indx
= s
->second
;
6450 symtab
->gc()->add_reference(src_obj
, src_indx
,
6451 ppc_object
, dst_indx
);
6455 ppc_object
->access_from_map()->clear();
6456 ppc_object
->process_gc_mark(symtab
);
6457 // Don't look at .opd relocs as .opd will reference everything.
6461 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
6462 typename
Target_powerpc::Relocatable_size_for_reloc
>(
6471 needs_special_offset_handling
,
6476 // Handle target specific gc actions when adding a gc reference from
6477 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
6478 // and DST_OFF. For powerpc64, this adds a referenc to the code
6479 // section of a function descriptor.
6481 template<int size
, bool big_endian
>
6483 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
6484 Symbol_table
* symtab
,
6486 unsigned int src_shndx
,
6488 unsigned int dst_shndx
,
6489 Address dst_off
) const
6491 if (size
!= 64 || dst_obj
->is_dynamic())
6494 Powerpc_relobj
<size
, big_endian
>* ppc_object
6495 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
6496 if (dst_shndx
!= 0 && dst_shndx
== ppc_object
->opd_shndx())
6498 if (ppc_object
->opd_valid())
6500 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
6501 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
6505 // If we haven't run scan_opd_relocs, we must delay
6506 // processing this function descriptor reference.
6507 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
6512 // Add any special sections for this symbol to the gc work list.
6513 // For powerpc64, this adds the code section of a function
6516 template<int size
, bool big_endian
>
6518 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
6519 Symbol_table
* symtab
,
6524 Powerpc_relobj
<size
, big_endian
>* ppc_object
6525 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
6527 unsigned int shndx
= sym
->shndx(&is_ordinary
);
6528 if (is_ordinary
&& shndx
!= 0 && shndx
== ppc_object
->opd_shndx())
6530 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
6531 Address dst_off
= gsym
->value();
6532 if (ppc_object
->opd_valid())
6534 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
6535 symtab
->gc()->worklist().push_back(Section_id(ppc_object
,
6539 ppc_object
->add_gc_mark(dst_off
);
6544 // For a symbol location in .opd, set LOC to the location of the
6547 template<int size
, bool big_endian
>
6549 Target_powerpc
<size
, big_endian
>::do_function_location(
6550 Symbol_location
* loc
) const
6552 if (size
== 64 && loc
->shndx
!= 0)
6554 if (loc
->object
->is_dynamic())
6556 Powerpc_dynobj
<size
, big_endian
>* ppc_object
6557 = static_cast<Powerpc_dynobj
<size
, big_endian
>*>(loc
->object
);
6558 if (loc
->shndx
== ppc_object
->opd_shndx())
6561 Address off
= loc
->offset
- ppc_object
->opd_address();
6562 loc
->shndx
= ppc_object
->get_opd_ent(off
, &dest_off
);
6563 loc
->offset
= dest_off
;
6568 const Powerpc_relobj
<size
, big_endian
>* ppc_object
6569 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(loc
->object
);
6570 if (loc
->shndx
== ppc_object
->opd_shndx())
6573 loc
->shndx
= ppc_object
->get_opd_ent(loc
->offset
, &dest_off
);
6574 loc
->offset
= dest_off
;
6580 // FNOFFSET in section SHNDX in OBJECT is the start of a function
6581 // compiled with -fsplit-stack. The function calls non-split-stack
6582 // code. Change the function to ensure it has enough stack space to
6583 // call some random function.
6585 template<int size
, bool big_endian
>
6587 Target_powerpc
<size
, big_endian
>::do_calls_non_split(
6590 section_offset_type fnoffset
,
6591 section_size_type fnsize
,
6592 unsigned char* view
,
6593 section_size_type view_size
,
6595 std::string
* to
) const
6597 // 32-bit not supported.
6601 Target::do_calls_non_split(object
, shndx
, fnoffset
, fnsize
,
6602 view
, view_size
, from
, to
);
6606 // The function always starts with
6607 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
6608 // addis %r12,%r1,-allocate@ha
6609 // addi %r12,%r12,-allocate@l
6611 // but note that the addis or addi may be replaced with a nop
6613 unsigned char *entry
= view
+ fnoffset
;
6614 uint32_t insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
6616 if ((insn
& 0xffff0000) == addis_2_12
)
6618 /* Skip ELFv2 global entry code. */
6620 insn
= elfcpp::Swap
<32, big_endian
>::readval(entry
);
6623 unsigned char *pinsn
= entry
;
6625 const uint32_t ld_private_ss
= 0xe80d8fc0;
6626 if (insn
== ld_private_ss
)
6628 int32_t allocate
= 0;
6632 insn
= elfcpp::Swap
<32, big_endian
>::readval(pinsn
);
6633 if ((insn
& 0xffff0000) == addis_12_1
)
6634 allocate
+= (insn
& 0xffff) << 16;
6635 else if ((insn
& 0xffff0000) == addi_12_1
6636 || (insn
& 0xffff0000) == addi_12_12
)
6637 allocate
+= ((insn
& 0xffff) ^ 0x8000) - 0x8000;
6638 else if (insn
!= nop
)
6641 if (insn
== cmpld_7_12_0
&& pinsn
== entry
+ 12)
6643 int extra
= parameters
->options().split_stack_adjust_size();
6645 if (allocate
>= 0 || extra
< 0)
6647 object
->error(_("split-stack stack size overflow at "
6648 "section %u offset %0zx"),
6649 shndx
, static_cast<size_t>(fnoffset
));
6653 insn
= addis_12_1
| (((allocate
+ 0x8000) >> 16) & 0xffff);
6654 if (insn
!= addis_12_1
)
6656 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
6658 insn
= addi_12_12
| (allocate
& 0xffff);
6659 if (insn
!= addi_12_12
)
6661 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
6667 insn
= addi_12_1
| (allocate
& 0xffff);
6668 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, insn
);
6671 if (pinsn
!= entry
+ 12)
6672 elfcpp::Swap
<32, big_endian
>::writeval(pinsn
, nop
);
6680 if (!object
->has_no_split_stack())
6681 object
->error(_("failed to match split-stack sequence at "
6682 "section %u offset %0zx"),
6683 shndx
, static_cast<size_t>(fnoffset
));
6687 // Scan relocations for a section.
6689 template<int size
, bool big_endian
>
6691 Target_powerpc
<size
, big_endian
>::scan_relocs(
6692 Symbol_table
* symtab
,
6694 Sized_relobj_file
<size
, big_endian
>* object
,
6695 unsigned int data_shndx
,
6696 unsigned int sh_type
,
6697 const unsigned char* prelocs
,
6699 Output_section
* output_section
,
6700 bool needs_special_offset_handling
,
6701 size_t local_symbol_count
,
6702 const unsigned char* plocal_symbols
)
6704 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
6705 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
6707 if (sh_type
== elfcpp::SHT_REL
)
6709 gold_error(_("%s: unsupported REL reloc section"),
6710 object
->name().c_str());
6714 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
6723 needs_special_offset_handling
,
6728 // Functor class for processing the global symbol table.
6729 // Removes symbols defined on discarded opd entries.
6731 template<bool big_endian
>
6732 class Global_symbol_visitor_opd
6735 Global_symbol_visitor_opd()
6739 operator()(Sized_symbol
<64>* sym
)
6741 if (sym
->has_symtab_index()
6742 || sym
->source() != Symbol::FROM_OBJECT
6743 || !sym
->in_real_elf())
6746 if (sym
->object()->is_dynamic())
6749 Powerpc_relobj
<64, big_endian
>* symobj
6750 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
6751 if (symobj
->opd_shndx() == 0)
6755 unsigned int shndx
= sym
->shndx(&is_ordinary
);
6756 if (shndx
== symobj
->opd_shndx()
6757 && symobj
->get_opd_discard(sym
->value()))
6759 sym
->set_undefined();
6760 sym
->set_visibility(elfcpp::STV_DEFAULT
);
6761 sym
->set_is_defined_in_discarded_section();
6762 sym
->set_symtab_index(-1U);
6767 template<int size
, bool big_endian
>
6769 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
6771 Symbol_table
* symtab
)
6775 Output_data_save_res
<size
, big_endian
>* savres
6776 = new Output_data_save_res
<size
, big_endian
>(symtab
);
6777 this->savres_section_
= savres
;
6778 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
6779 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
6780 savres
, ORDER_TEXT
, false);
6784 // Sort linker created .got section first (for the header), then input
6785 // sections belonging to files using small model code.
6787 template<bool big_endian
>
6788 class Sort_toc_sections
6792 operator()(const Output_section::Input_section
& is1
,
6793 const Output_section::Input_section
& is2
) const
6795 if (!is1
.is_input_section() && is2
.is_input_section())
6798 = (is1
.is_input_section()
6799 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
6800 ->has_small_toc_reloc()));
6802 = (is2
.is_input_section()
6803 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
6804 ->has_small_toc_reloc()));
6805 return small1
&& !small2
;
6809 // Finalize the sections.
6811 template<int size
, bool big_endian
>
6813 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
6815 const Input_objects
*,
6816 Symbol_table
* symtab
)
6818 if (parameters
->doing_static_link())
6820 // At least some versions of glibc elf-init.o have a strong
6821 // reference to __rela_iplt marker syms. A weak ref would be
6823 if (this->iplt_
!= NULL
)
6825 Reloc_section
* rel
= this->iplt_
->rel_plt();
6826 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
6827 Symbol_table::PREDEFINED
, rel
, 0, 0,
6828 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6829 elfcpp::STV_HIDDEN
, 0, false, true);
6830 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
6831 Symbol_table::PREDEFINED
, rel
, 0, 0,
6832 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6833 elfcpp::STV_HIDDEN
, 0, true, true);
6837 symtab
->define_as_constant("__rela_iplt_start", NULL
,
6838 Symbol_table::PREDEFINED
, 0, 0,
6839 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6840 elfcpp::STV_HIDDEN
, 0, true, false);
6841 symtab
->define_as_constant("__rela_iplt_end", NULL
,
6842 Symbol_table::PREDEFINED
, 0, 0,
6843 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6844 elfcpp::STV_HIDDEN
, 0, true, false);
6850 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
6851 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
6853 if (!parameters
->options().relocatable())
6855 this->define_save_restore_funcs(layout
, symtab
);
6857 // Annoyingly, we need to make these sections now whether or
6858 // not we need them. If we delay until do_relax then we
6859 // need to mess with the relaxation machinery checkpointing.
6860 this->got_section(symtab
, layout
);
6861 this->make_brlt_section(layout
);
6863 if (parameters
->options().toc_sort())
6865 Output_section
* os
= this->got_
->output_section();
6866 if (os
!= NULL
&& os
->input_sections().size() > 1)
6867 std::stable_sort(os
->input_sections().begin(),
6868 os
->input_sections().end(),
6869 Sort_toc_sections
<big_endian
>());
6874 // Fill in some more dynamic tags.
6875 Output_data_dynamic
* odyn
= layout
->dynamic_data();
6878 const Reloc_section
* rel_plt
= (this->plt_
== NULL
6880 : this->plt_
->rel_plt());
6881 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
6882 this->rela_dyn_
, true, size
== 32);
6886 if (this->got_
!= NULL
)
6888 this->got_
->finalize_data_size();
6889 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
6890 this->got_
, this->got_
->g_o_t());
6895 if (this->glink_
!= NULL
)
6897 this->glink_
->finalize_data_size();
6898 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
6900 (this->glink_
->pltresolve_size
6906 // Emit any relocs we saved in an attempt to avoid generating COPY
6908 if (this->copy_relocs_
.any_saved_relocs())
6909 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
6912 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6916 ok_lo_toc_insn(uint32_t insn
)
6918 return ((insn
& (0x3f << 26)) == 14u << 26 /* addi */
6919 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
6920 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
6921 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
6922 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
6923 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
6924 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
6925 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
6926 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
6927 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
6928 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
6929 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
6930 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
6931 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
6932 || ((insn
& (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6934 || ((insn
& (0x3f << 26)) == 62u << 26 /* std, stmd */
6935 && ((insn
& 3) == 0 || (insn
& 3) == 3))
6936 || (insn
& (0x3f << 26)) == 12u << 26 /* addic */);
6939 // Return the value to use for a branch relocation.
6941 template<int size
, bool big_endian
>
6943 Target_powerpc
<size
, big_endian
>::symval_for_branch(
6944 const Symbol_table
* symtab
,
6945 const Sized_symbol
<size
>* gsym
,
6946 Powerpc_relobj
<size
, big_endian
>* object
,
6948 unsigned int *dest_shndx
)
6950 if (size
== 32 || this->abiversion() >= 2)
6954 // If the symbol is defined in an opd section, ie. is a function
6955 // descriptor, use the function descriptor code entry address
6956 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
6958 && gsym
->source() != Symbol::FROM_OBJECT
)
6961 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
6962 unsigned int shndx
= symobj
->opd_shndx();
6965 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
6966 if (opd_addr
== invalid_address
)
6968 opd_addr
+= symobj
->output_section_address(shndx
);
6969 if (*value
>= opd_addr
&& *value
< opd_addr
+ symobj
->section_size(shndx
))
6972 *dest_shndx
= symobj
->get_opd_ent(*value
- opd_addr
, &sec_off
);
6973 if (symtab
->is_section_folded(symobj
, *dest_shndx
))
6976 = symtab
->icf()->get_folded_section(symobj
, *dest_shndx
);
6977 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(folded
.first
);
6978 *dest_shndx
= folded
.second
;
6980 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
6981 if (sec_addr
== invalid_address
)
6984 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
6985 *value
= sec_addr
+ sec_off
;
6990 // Perform a relocation.
6992 template<int size
, bool big_endian
>
6994 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
6995 const Relocate_info
<size
, big_endian
>* relinfo
,
6996 Target_powerpc
* target
,
6999 const elfcpp::Rela
<size
, big_endian
>& rela
,
7000 unsigned int r_type
,
7001 const Sized_symbol
<size
>* gsym
,
7002 const Symbol_value
<size
>* psymval
,
7003 unsigned char* view
,
7005 section_size_type view_size
)
7010 switch (this->maybe_skip_tls_get_addr_call(r_type
, gsym
))
7012 case Track_tls::NOT_EXPECTED
:
7013 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7014 _("__tls_get_addr call lacks marker reloc"));
7016 case Track_tls::EXPECTED
:
7017 // We have already complained.
7019 case Track_tls::SKIP
:
7021 case Track_tls::NORMAL
:
7025 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
7026 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
7027 Powerpc_relobj
<size
, big_endian
>* const object
7028 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
7030 bool has_stub_value
= false;
7031 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7033 ? gsym
->use_plt_offset(Scan::get_reference_flags(r_type
, target
))
7034 : object
->local_has_plt_offset(r_sym
))
7035 && (!psymval
->is_ifunc_symbol()
7036 || Scan::reloc_needs_plt_for_ifunc(target
, object
, r_type
, false)))
7040 && target
->abiversion() >= 2
7041 && !parameters
->options().output_is_position_independent()
7042 && !is_branch_reloc(r_type
))
7044 Address off
= target
->glink_section()->find_global_entry(gsym
);
7045 if (off
!= invalid_address
)
7047 value
= target
->glink_section()->global_entry_address() + off
;
7048 has_stub_value
= true;
7053 Stub_table
<size
, big_endian
>* stub_table
7054 = object
->stub_table(relinfo
->data_shndx
);
7055 if (stub_table
== NULL
)
7057 // This is a ref from a data section to an ifunc symbol.
7058 if (target
->stub_tables().size() != 0)
7059 stub_table
= target
->stub_tables()[0];
7061 if (stub_table
!= NULL
)
7065 off
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
7066 rela
.get_r_addend());
7068 off
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
7069 rela
.get_r_addend());
7070 if (off
!= invalid_address
)
7072 value
= stub_table
->stub_address() + off
;
7073 has_stub_value
= true;
7077 // We don't care too much about bogus debug references to
7078 // non-local functions, but otherwise there had better be a plt
7079 // call stub or global entry stub as appropriate.
7080 gold_assert(has_stub_value
|| !(os
->flags() & elfcpp::SHF_ALLOC
));
7083 if (r_type
== elfcpp::R_POWERPC_GOT16
7084 || r_type
== elfcpp::R_POWERPC_GOT16_LO
7085 || r_type
== elfcpp::R_POWERPC_GOT16_HI
7086 || r_type
== elfcpp::R_POWERPC_GOT16_HA
7087 || r_type
== elfcpp::R_PPC64_GOT16_DS
7088 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
7092 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
7093 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
7097 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7098 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
7099 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
7101 value
-= target
->got_section()->got_base_offset(object
);
7103 else if (r_type
== elfcpp::R_PPC64_TOC
)
7105 value
= (target
->got_section()->output_section()->address()
7106 + object
->toc_base_offset());
7108 else if (gsym
!= NULL
7109 && (r_type
== elfcpp::R_POWERPC_REL24
7110 || r_type
== elfcpp::R_PPC_PLTREL24
)
7115 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
7116 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
7117 bool can_plt_call
= false;
7118 if (rela
.get_r_offset() + 8 <= view_size
)
7120 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
7121 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
7124 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
7126 elfcpp::Swap
<32, big_endian
>::
7127 writeval(wv
+ 1, ld_2_1
+ target
->stk_toc());
7128 can_plt_call
= true;
7133 // If we don't have a branch and link followed by a nop,
7134 // we can't go via the plt because there is no place to
7135 // put a toc restoring instruction.
7136 // Unless we know we won't be returning.
7137 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
7138 can_plt_call
= true;
7142 // g++ as of 20130507 emits self-calls without a
7143 // following nop. This is arguably wrong since we have
7144 // conflicting information. On the one hand a global
7145 // symbol and on the other a local call sequence, but
7146 // don't error for this special case.
7147 // It isn't possible to cheaply verify we have exactly
7148 // such a call. Allow all calls to the same section.
7150 Address code
= value
;
7151 if (gsym
->source() == Symbol::FROM_OBJECT
7152 && gsym
->object() == object
)
7154 unsigned int dest_shndx
= 0;
7155 if (target
->abiversion() < 2)
7157 Address addend
= rela
.get_r_addend();
7158 code
= psymval
->value(object
, addend
);
7159 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
7160 &code
, &dest_shndx
);
7163 if (dest_shndx
== 0)
7164 dest_shndx
= gsym
->shndx(&is_ordinary
);
7165 ok
= dest_shndx
== relinfo
->data_shndx
;
7169 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7170 _("call lacks nop, can't restore toc; "
7171 "recompile with -fPIC"));
7177 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7178 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
7179 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
7180 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
7182 // First instruction of a global dynamic sequence, arg setup insn.
7183 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7184 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7185 enum Got_type got_type
= GOT_TYPE_STANDARD
;
7186 if (tls_type
== tls::TLSOPT_NONE
)
7187 got_type
= GOT_TYPE_TLSGD
;
7188 else if (tls_type
== tls::TLSOPT_TO_IE
)
7189 got_type
= GOT_TYPE_TPREL
;
7190 if (got_type
!= GOT_TYPE_STANDARD
)
7194 gold_assert(gsym
->has_got_offset(got_type
));
7195 value
= gsym
->got_offset(got_type
);
7199 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7200 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
7201 value
= object
->local_got_offset(r_sym
, got_type
);
7203 value
-= target
->got_section()->got_base_offset(object
);
7205 if (tls_type
== tls::TLSOPT_TO_IE
)
7207 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7208 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
7210 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7211 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7212 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
7214 insn
|= 32 << 26; // lwz
7216 insn
|= 58 << 26; // ld
7217 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7219 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
7220 - elfcpp::R_POWERPC_GOT_TLSGD16
);
7222 else if (tls_type
== tls::TLSOPT_TO_LE
)
7224 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7225 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
7227 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7228 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7229 insn
&= (1 << 26) - (1 << 21); // extract rt
7234 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7235 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7236 value
= psymval
->value(object
, rela
.get_r_addend());
7240 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7242 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7243 r_type
= elfcpp::R_POWERPC_NONE
;
7247 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7248 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
7249 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
7250 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
7252 // First instruction of a local dynamic sequence, arg setup insn.
7253 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7254 if (tls_type
== tls::TLSOPT_NONE
)
7256 value
= target
->tlsld_got_offset();
7257 value
-= target
->got_section()->got_base_offset(object
);
7261 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
7262 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7263 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
7265 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7266 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7267 insn
&= (1 << 26) - (1 << 21); // extract rt
7272 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7273 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7278 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7280 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7281 r_type
= elfcpp::R_POWERPC_NONE
;
7285 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
7286 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
7287 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
7288 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
7290 // Accesses relative to a local dynamic sequence address,
7291 // no optimisation here.
7294 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
7295 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
7299 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7300 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
7301 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
7303 value
-= target
->got_section()->got_base_offset(object
);
7305 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
7306 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
7307 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
7308 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
7310 // First instruction of initial exec sequence.
7311 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7312 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
7313 if (tls_type
== tls::TLSOPT_NONE
)
7317 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
7318 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
7322 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
7323 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
7324 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
7326 value
-= target
->got_section()->got_base_offset(object
);
7330 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
7331 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
7332 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
7334 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7335 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7336 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
7341 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7342 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7343 value
= psymval
->value(object
, rela
.get_r_addend());
7347 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7349 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7350 r_type
= elfcpp::R_POWERPC_NONE
;
7354 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
7355 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
7357 // Second instruction of a global dynamic sequence,
7358 // the __tls_get_addr call
7359 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
7360 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7361 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
7362 if (tls_type
!= tls::TLSOPT_NONE
)
7364 if (tls_type
== tls::TLSOPT_TO_IE
)
7366 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
7367 Insn insn
= add_3_3_13
;
7370 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7371 r_type
= elfcpp::R_POWERPC_NONE
;
7375 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
7376 Insn insn
= addi_3_3
;
7377 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7378 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7379 view
+= 2 * big_endian
;
7380 value
= psymval
->value(object
, rela
.get_r_addend());
7382 this->skip_next_tls_get_addr_call();
7385 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
7386 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
7388 // Second instruction of a local dynamic sequence,
7389 // the __tls_get_addr call
7390 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
7391 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
7392 if (tls_type
== tls::TLSOPT_TO_LE
)
7394 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
7395 Insn insn
= addi_3_3
;
7396 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7397 this->skip_next_tls_get_addr_call();
7398 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7399 view
+= 2 * big_endian
;
7403 else if (r_type
== elfcpp::R_POWERPC_TLS
)
7405 // Second instruction of an initial exec sequence
7406 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7407 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
7408 if (tls_type
== tls::TLSOPT_TO_LE
)
7410 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
7411 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7412 unsigned int reg
= size
== 32 ? 2 : 13;
7413 insn
= at_tls_transform(insn
, reg
);
7414 gold_assert(insn
!= 0);
7415 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7416 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7417 view
+= 2 * big_endian
;
7418 value
= psymval
->value(object
, rela
.get_r_addend());
7421 else if (!has_stub_value
)
7424 if (!(size
== 32 && r_type
== elfcpp::R_PPC_PLTREL24
))
7425 addend
= rela
.get_r_addend();
7426 value
= psymval
->value(object
, addend
);
7427 if (size
== 64 && is_branch_reloc(r_type
))
7429 if (target
->abiversion() >= 2)
7432 value
+= object
->ppc64_local_entry_offset(gsym
);
7434 value
+= object
->ppc64_local_entry_offset(r_sym
);
7438 unsigned int dest_shndx
;
7439 target
->symval_for_branch(relinfo
->symtab
, gsym
, object
,
7440 &value
, &dest_shndx
);
7443 Address max_branch_offset
= max_branch_delta(r_type
);
7444 if (max_branch_offset
!= 0
7445 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
7447 Stub_table
<size
, big_endian
>* stub_table
7448 = object
->stub_table(relinfo
->data_shndx
);
7449 if (stub_table
!= NULL
)
7451 Address off
= stub_table
->find_long_branch_entry(object
, value
);
7452 if (off
!= invalid_address
)
7454 value
= (stub_table
->stub_address() + stub_table
->plt_size()
7456 has_stub_value
= true;
7464 case elfcpp::R_PPC64_REL64
:
7465 case elfcpp::R_POWERPC_REL32
:
7466 case elfcpp::R_POWERPC_REL24
:
7467 case elfcpp::R_PPC_PLTREL24
:
7468 case elfcpp::R_PPC_LOCAL24PC
:
7469 case elfcpp::R_POWERPC_REL16
:
7470 case elfcpp::R_POWERPC_REL16_LO
:
7471 case elfcpp::R_POWERPC_REL16_HI
:
7472 case elfcpp::R_POWERPC_REL16_HA
:
7473 case elfcpp::R_POWERPC_REL16DX_HA
:
7474 case elfcpp::R_POWERPC_REL14
:
7475 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7476 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7480 case elfcpp::R_PPC64_TOC16
:
7481 case elfcpp::R_PPC64_TOC16_LO
:
7482 case elfcpp::R_PPC64_TOC16_HI
:
7483 case elfcpp::R_PPC64_TOC16_HA
:
7484 case elfcpp::R_PPC64_TOC16_DS
:
7485 case elfcpp::R_PPC64_TOC16_LO_DS
:
7486 // Subtract the TOC base address.
7487 value
-= (target
->got_section()->output_section()->address()
7488 + object
->toc_base_offset());
7491 case elfcpp::R_POWERPC_SECTOFF
:
7492 case elfcpp::R_POWERPC_SECTOFF_LO
:
7493 case elfcpp::R_POWERPC_SECTOFF_HI
:
7494 case elfcpp::R_POWERPC_SECTOFF_HA
:
7495 case elfcpp::R_PPC64_SECTOFF_DS
:
7496 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
7498 value
-= os
->address();
7501 case elfcpp::R_PPC64_TPREL16_DS
:
7502 case elfcpp::R_PPC64_TPREL16_LO_DS
:
7503 case elfcpp::R_PPC64_TPREL16_HIGH
:
7504 case elfcpp::R_PPC64_TPREL16_HIGHA
:
7506 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
7508 case elfcpp::R_POWERPC_TPREL16
:
7509 case elfcpp::R_POWERPC_TPREL16_LO
:
7510 case elfcpp::R_POWERPC_TPREL16_HI
:
7511 case elfcpp::R_POWERPC_TPREL16_HA
:
7512 case elfcpp::R_POWERPC_TPREL
:
7513 case elfcpp::R_PPC64_TPREL16_HIGHER
:
7514 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
7515 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
7516 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
7517 // tls symbol values are relative to tls_segment()->vaddr()
7521 case elfcpp::R_PPC64_DTPREL16_DS
:
7522 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
7523 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
7524 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
7525 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
7526 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
7528 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
7529 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
7531 case elfcpp::R_POWERPC_DTPREL16
:
7532 case elfcpp::R_POWERPC_DTPREL16_LO
:
7533 case elfcpp::R_POWERPC_DTPREL16_HI
:
7534 case elfcpp::R_POWERPC_DTPREL16_HA
:
7535 case elfcpp::R_POWERPC_DTPREL
:
7536 case elfcpp::R_PPC64_DTPREL16_HIGH
:
7537 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
7538 // tls symbol values are relative to tls_segment()->vaddr()
7539 value
-= dtp_offset
;
7542 case elfcpp::R_PPC64_ADDR64_LOCAL
:
7544 value
+= object
->ppc64_local_entry_offset(gsym
);
7546 value
+= object
->ppc64_local_entry_offset(r_sym
);
7553 Insn branch_bit
= 0;
7556 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
7557 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7558 branch_bit
= 1 << 21;
7559 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
7560 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7562 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
7563 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7566 if (this->is_isa_v2
)
7568 // Set 'a' bit. This is 0b00010 in BO field for branch
7569 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
7570 // for branch on CTR insns (BO == 1a00t or 1a01t).
7571 if ((insn
& (0x14 << 21)) == (0x04 << 21))
7573 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
7580 // Invert 'y' bit if not the default.
7581 if (static_cast<Signed_address
>(value
) < 0)
7584 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7594 // Multi-instruction sequences that access the TOC can be
7595 // optimized, eg. addis ra,r2,0; addi rb,ra,x;
7596 // to nop; addi rb,r2,x;
7602 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7603 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7604 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7605 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7606 case elfcpp::R_POWERPC_GOT16_HA
:
7607 case elfcpp::R_PPC64_TOC16_HA
:
7608 if (parameters
->options().toc_optimize())
7610 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7611 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7612 if ((insn
& ((0x3f << 26) | 0x1f << 16))
7613 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
7614 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7615 _("toc optimization is not supported "
7616 "for %#08x instruction"), insn
);
7617 else if (value
+ 0x8000 < 0x10000)
7619 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
7625 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7626 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7627 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7628 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7629 case elfcpp::R_POWERPC_GOT16_LO
:
7630 case elfcpp::R_PPC64_GOT16_LO_DS
:
7631 case elfcpp::R_PPC64_TOC16_LO
:
7632 case elfcpp::R_PPC64_TOC16_LO_DS
:
7633 if (parameters
->options().toc_optimize())
7635 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7636 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7637 if (!ok_lo_toc_insn(insn
))
7638 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7639 _("toc optimization is not supported "
7640 "for %#08x instruction"), insn
);
7641 else if (value
+ 0x8000 < 0x10000)
7643 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
7645 // Transform addic to addi when we change reg.
7646 insn
&= ~((0x3f << 26) | (0x1f << 16));
7647 insn
|= (14u << 26) | (2 << 16);
7651 insn
&= ~(0x1f << 16);
7654 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
7661 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
7662 elfcpp::Shdr
<size
, big_endian
> shdr(relinfo
->data_shdr
);
7665 case elfcpp::R_POWERPC_ADDR32
:
7666 case elfcpp::R_POWERPC_UADDR32
:
7668 overflow
= Reloc::CHECK_BITFIELD
;
7671 case elfcpp::R_POWERPC_REL32
:
7672 case elfcpp::R_POWERPC_REL16DX_HA
:
7674 overflow
= Reloc::CHECK_SIGNED
;
7677 case elfcpp::R_POWERPC_UADDR16
:
7678 overflow
= Reloc::CHECK_BITFIELD
;
7681 case elfcpp::R_POWERPC_ADDR16
:
7682 // We really should have three separate relocations,
7683 // one for 16-bit data, one for insns with 16-bit signed fields,
7684 // and one for insns with 16-bit unsigned fields.
7685 overflow
= Reloc::CHECK_BITFIELD
;
7686 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
7687 overflow
= Reloc::CHECK_LOW_INSN
;
7690 case elfcpp::R_POWERPC_ADDR16_HI
:
7691 case elfcpp::R_POWERPC_ADDR16_HA
:
7692 case elfcpp::R_POWERPC_GOT16_HI
:
7693 case elfcpp::R_POWERPC_GOT16_HA
:
7694 case elfcpp::R_POWERPC_PLT16_HI
:
7695 case elfcpp::R_POWERPC_PLT16_HA
:
7696 case elfcpp::R_POWERPC_SECTOFF_HI
:
7697 case elfcpp::R_POWERPC_SECTOFF_HA
:
7698 case elfcpp::R_PPC64_TOC16_HI
:
7699 case elfcpp::R_PPC64_TOC16_HA
:
7700 case elfcpp::R_PPC64_PLTGOT16_HI
:
7701 case elfcpp::R_PPC64_PLTGOT16_HA
:
7702 case elfcpp::R_POWERPC_TPREL16_HI
:
7703 case elfcpp::R_POWERPC_TPREL16_HA
:
7704 case elfcpp::R_POWERPC_DTPREL16_HI
:
7705 case elfcpp::R_POWERPC_DTPREL16_HA
:
7706 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
7707 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7708 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
7709 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7710 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7711 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7712 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
7713 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7714 case elfcpp::R_POWERPC_REL16_HI
:
7715 case elfcpp::R_POWERPC_REL16_HA
:
7717 overflow
= Reloc::CHECK_HIGH_INSN
;
7720 case elfcpp::R_POWERPC_REL16
:
7721 case elfcpp::R_PPC64_TOC16
:
7722 case elfcpp::R_POWERPC_GOT16
:
7723 case elfcpp::R_POWERPC_SECTOFF
:
7724 case elfcpp::R_POWERPC_TPREL16
:
7725 case elfcpp::R_POWERPC_DTPREL16
:
7726 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7727 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7728 case elfcpp::R_POWERPC_GOT_TPREL16
:
7729 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7730 overflow
= Reloc::CHECK_LOW_INSN
;
7733 case elfcpp::R_POWERPC_ADDR24
:
7734 case elfcpp::R_POWERPC_ADDR14
:
7735 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
7736 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
7737 case elfcpp::R_PPC64_ADDR16_DS
:
7738 case elfcpp::R_POWERPC_REL24
:
7739 case elfcpp::R_PPC_PLTREL24
:
7740 case elfcpp::R_PPC_LOCAL24PC
:
7741 case elfcpp::R_PPC64_TPREL16_DS
:
7742 case elfcpp::R_PPC64_DTPREL16_DS
:
7743 case elfcpp::R_PPC64_TOC16_DS
:
7744 case elfcpp::R_PPC64_GOT16_DS
:
7745 case elfcpp::R_PPC64_SECTOFF_DS
:
7746 case elfcpp::R_POWERPC_REL14
:
7747 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7748 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7749 overflow
= Reloc::CHECK_SIGNED
;
7753 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
7756 if (overflow
== Reloc::CHECK_LOW_INSN
7757 || overflow
== Reloc::CHECK_HIGH_INSN
)
7759 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
7761 if ((insn
& (0x3f << 26)) == 10u << 26 /* cmpli */)
7762 overflow
= Reloc::CHECK_BITFIELD
;
7763 else if (overflow
== Reloc::CHECK_LOW_INSN
7764 ? ((insn
& (0x3f << 26)) == 28u << 26 /* andi */
7765 || (insn
& (0x3f << 26)) == 24u << 26 /* ori */
7766 || (insn
& (0x3f << 26)) == 26u << 26 /* xori */)
7767 : ((insn
& (0x3f << 26)) == 29u << 26 /* andis */
7768 || (insn
& (0x3f << 26)) == 25u << 26 /* oris */
7769 || (insn
& (0x3f << 26)) == 27u << 26 /* xoris */))
7770 overflow
= Reloc::CHECK_UNSIGNED
;
7772 overflow
= Reloc::CHECK_SIGNED
;
7775 bool maybe_dq_reloc
= false;
7776 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
7777 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
7780 case elfcpp::R_POWERPC_NONE
:
7781 case elfcpp::R_POWERPC_TLS
:
7782 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
7783 case elfcpp::R_POWERPC_GNU_VTENTRY
:
7786 case elfcpp::R_PPC64_ADDR64
:
7787 case elfcpp::R_PPC64_REL64
:
7788 case elfcpp::R_PPC64_TOC
:
7789 case elfcpp::R_PPC64_ADDR64_LOCAL
:
7790 Reloc::addr64(view
, value
);
7793 case elfcpp::R_POWERPC_TPREL
:
7794 case elfcpp::R_POWERPC_DTPREL
:
7796 Reloc::addr64(view
, value
);
7798 status
= Reloc::addr32(view
, value
, overflow
);
7801 case elfcpp::R_PPC64_UADDR64
:
7802 Reloc::addr64_u(view
, value
);
7805 case elfcpp::R_POWERPC_ADDR32
:
7806 status
= Reloc::addr32(view
, value
, overflow
);
7809 case elfcpp::R_POWERPC_REL32
:
7810 case elfcpp::R_POWERPC_UADDR32
:
7811 status
= Reloc::addr32_u(view
, value
, overflow
);
7814 case elfcpp::R_POWERPC_ADDR24
:
7815 case elfcpp::R_POWERPC_REL24
:
7816 case elfcpp::R_PPC_PLTREL24
:
7817 case elfcpp::R_PPC_LOCAL24PC
:
7818 status
= Reloc::addr24(view
, value
, overflow
);
7821 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7822 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7823 case elfcpp::R_POWERPC_GOT_TPREL16
:
7824 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7827 // On ppc64 these are all ds form
7828 maybe_dq_reloc
= true;
7831 case elfcpp::R_POWERPC_ADDR16
:
7832 case elfcpp::R_POWERPC_REL16
:
7833 case elfcpp::R_PPC64_TOC16
:
7834 case elfcpp::R_POWERPC_GOT16
:
7835 case elfcpp::R_POWERPC_SECTOFF
:
7836 case elfcpp::R_POWERPC_TPREL16
:
7837 case elfcpp::R_POWERPC_DTPREL16
:
7838 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7839 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7840 case elfcpp::R_POWERPC_ADDR16_LO
:
7841 case elfcpp::R_POWERPC_REL16_LO
:
7842 case elfcpp::R_PPC64_TOC16_LO
:
7843 case elfcpp::R_POWERPC_GOT16_LO
:
7844 case elfcpp::R_POWERPC_SECTOFF_LO
:
7845 case elfcpp::R_POWERPC_TPREL16_LO
:
7846 case elfcpp::R_POWERPC_DTPREL16_LO
:
7847 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7848 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7850 status
= Reloc::addr16(view
, value
, overflow
);
7852 maybe_dq_reloc
= true;
7855 case elfcpp::R_POWERPC_UADDR16
:
7856 status
= Reloc::addr16_u(view
, value
, overflow
);
7859 case elfcpp::R_PPC64_ADDR16_HIGH
:
7860 case elfcpp::R_PPC64_TPREL16_HIGH
:
7861 case elfcpp::R_PPC64_DTPREL16_HIGH
:
7863 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
7865 case elfcpp::R_POWERPC_ADDR16_HI
:
7866 case elfcpp::R_POWERPC_REL16_HI
:
7867 case elfcpp::R_PPC64_TOC16_HI
:
7868 case elfcpp::R_POWERPC_GOT16_HI
:
7869 case elfcpp::R_POWERPC_SECTOFF_HI
:
7870 case elfcpp::R_POWERPC_TPREL16_HI
:
7871 case elfcpp::R_POWERPC_DTPREL16_HI
:
7872 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
7873 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
7874 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7875 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
7876 Reloc::addr16_hi(view
, value
);
7879 case elfcpp::R_PPC64_ADDR16_HIGHA
:
7880 case elfcpp::R_PPC64_TPREL16_HIGHA
:
7881 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
7883 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
7885 case elfcpp::R_POWERPC_ADDR16_HA
:
7886 case elfcpp::R_POWERPC_REL16_HA
:
7887 case elfcpp::R_PPC64_TOC16_HA
:
7888 case elfcpp::R_POWERPC_GOT16_HA
:
7889 case elfcpp::R_POWERPC_SECTOFF_HA
:
7890 case elfcpp::R_POWERPC_TPREL16_HA
:
7891 case elfcpp::R_POWERPC_DTPREL16_HA
:
7892 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7893 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7894 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7895 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7896 Reloc::addr16_ha(view
, value
);
7899 case elfcpp::R_POWERPC_REL16DX_HA
:
7900 status
= Reloc::addr16dx_ha(view
, value
, overflow
);
7903 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
7905 // R_PPC_EMB_NADDR16_LO
7907 case elfcpp::R_PPC64_ADDR16_HIGHER
:
7908 case elfcpp::R_PPC64_TPREL16_HIGHER
:
7909 Reloc::addr16_hi2(view
, value
);
7912 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
7914 // R_PPC_EMB_NADDR16_HI
7916 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
7917 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
7918 Reloc::addr16_ha2(view
, value
);
7921 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
7923 // R_PPC_EMB_NADDR16_HA
7925 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
7926 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
7927 Reloc::addr16_hi3(view
, value
);
7930 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
7934 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
7935 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
7936 Reloc::addr16_ha3(view
, value
);
7939 case elfcpp::R_PPC64_DTPREL16_DS
:
7940 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
7942 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
7944 case elfcpp::R_PPC64_TPREL16_DS
:
7945 case elfcpp::R_PPC64_TPREL16_LO_DS
:
7947 // R_PPC_TLSGD, R_PPC_TLSLD
7949 case elfcpp::R_PPC64_ADDR16_DS
:
7950 case elfcpp::R_PPC64_ADDR16_LO_DS
:
7951 case elfcpp::R_PPC64_TOC16_DS
:
7952 case elfcpp::R_PPC64_TOC16_LO_DS
:
7953 case elfcpp::R_PPC64_GOT16_DS
:
7954 case elfcpp::R_PPC64_GOT16_LO_DS
:
7955 case elfcpp::R_PPC64_SECTOFF_DS
:
7956 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
7957 maybe_dq_reloc
= true;
7960 case elfcpp::R_POWERPC_ADDR14
:
7961 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
7962 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
7963 case elfcpp::R_POWERPC_REL14
:
7964 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7965 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7966 status
= Reloc::addr14(view
, value
, overflow
);
7969 case elfcpp::R_POWERPC_COPY
:
7970 case elfcpp::R_POWERPC_GLOB_DAT
:
7971 case elfcpp::R_POWERPC_JMP_SLOT
:
7972 case elfcpp::R_POWERPC_RELATIVE
:
7973 case elfcpp::R_POWERPC_DTPMOD
:
7974 case elfcpp::R_PPC64_JMP_IREL
:
7975 case elfcpp::R_POWERPC_IRELATIVE
:
7976 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7977 _("unexpected reloc %u in object file"),
7981 case elfcpp::R_PPC_EMB_SDA21
:
7986 // R_PPC64_TOCSAVE. For the time being this can be ignored.
7990 case elfcpp::R_PPC_EMB_SDA2I16
:
7991 case elfcpp::R_PPC_EMB_SDA2REL
:
7994 // R_PPC64_TLSGD, R_PPC64_TLSLD
7997 case elfcpp::R_POWERPC_PLT32
:
7998 case elfcpp::R_POWERPC_PLTREL32
:
7999 case elfcpp::R_POWERPC_PLT16_LO
:
8000 case elfcpp::R_POWERPC_PLT16_HI
:
8001 case elfcpp::R_POWERPC_PLT16_HA
:
8002 case elfcpp::R_PPC_SDAREL16
:
8003 case elfcpp::R_POWERPC_ADDR30
:
8004 case elfcpp::R_PPC64_PLT64
:
8005 case elfcpp::R_PPC64_PLTREL64
:
8006 case elfcpp::R_PPC64_PLTGOT16
:
8007 case elfcpp::R_PPC64_PLTGOT16_LO
:
8008 case elfcpp::R_PPC64_PLTGOT16_HI
:
8009 case elfcpp::R_PPC64_PLTGOT16_HA
:
8010 case elfcpp::R_PPC64_PLT16_LO_DS
:
8011 case elfcpp::R_PPC64_PLTGOT16_DS
:
8012 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
8013 case elfcpp::R_PPC_EMB_RELSDA
:
8014 case elfcpp::R_PPC_TOC16
:
8017 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8018 _("unsupported reloc %u"),
8026 insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
8028 if ((insn
& (0x3f << 26)) == 56u << 26 /* lq */
8029 || ((insn
& (0x3f << 26)) == (61u << 26) /* lxv, stxv */
8030 && (insn
& 3) == 1))
8031 status
= Reloc::addr16_dq(view
, value
, overflow
);
8033 || (insn
& (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
8034 || (insn
& (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
8035 || (insn
& (0x3f << 26)) == 57u << 26 /* lfdp */
8036 || (insn
& (0x3f << 26)) == 61u << 26 /* stfdp */)
8037 status
= Reloc::addr16_ds(view
, value
, overflow
);
8039 status
= Reloc::addr16(view
, value
, overflow
);
8042 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
8045 && gsym
->is_undefined()
8046 && is_branch_reloc(r_type
))))
8048 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
8049 _("relocation overflow"));
8051 gold_info(_("try relinking with a smaller --stub-group-size"));
8057 // Relocate section data.
8059 template<int size
, bool big_endian
>
8061 Target_powerpc
<size
, big_endian
>::relocate_section(
8062 const Relocate_info
<size
, big_endian
>* relinfo
,
8063 unsigned int sh_type
,
8064 const unsigned char* prelocs
,
8066 Output_section
* output_section
,
8067 bool needs_special_offset_handling
,
8068 unsigned char* view
,
8070 section_size_type view_size
,
8071 const Reloc_symbol_changes
* reloc_symbol_changes
)
8073 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
8074 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
8075 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
8076 Powerpc_comdat_behavior
;
8078 gold_assert(sh_type
== elfcpp::SHT_RELA
);
8080 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
8081 Powerpc_relocate
, Powerpc_comdat_behavior
>(
8087 needs_special_offset_handling
,
8091 reloc_symbol_changes
);
8094 class Powerpc_scan_relocatable_reloc
8097 // Return the strategy to use for a local symbol which is not a
8098 // section symbol, given the relocation type.
8099 inline Relocatable_relocs::Reloc_strategy
8100 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
8102 if (r_type
== 0 && r_sym
== 0)
8103 return Relocatable_relocs::RELOC_DISCARD
;
8104 return Relocatable_relocs::RELOC_COPY
;
8107 // Return the strategy to use for a local symbol which is a section
8108 // symbol, given the relocation type.
8109 inline Relocatable_relocs::Reloc_strategy
8110 local_section_strategy(unsigned int, Relobj
*)
8112 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
8115 // Return the strategy to use for a global symbol, given the
8116 // relocation type, the object, and the symbol index.
8117 inline Relocatable_relocs::Reloc_strategy
8118 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
8120 if (r_type
== elfcpp::R_PPC_PLTREL24
)
8121 return Relocatable_relocs::RELOC_SPECIAL
;
8122 return Relocatable_relocs::RELOC_COPY
;
8126 // Scan the relocs during a relocatable link.
8128 template<int size
, bool big_endian
>
8130 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
8131 Symbol_table
* symtab
,
8133 Sized_relobj_file
<size
, big_endian
>* object
,
8134 unsigned int data_shndx
,
8135 unsigned int sh_type
,
8136 const unsigned char* prelocs
,
8138 Output_section
* output_section
,
8139 bool needs_special_offset_handling
,
8140 size_t local_symbol_count
,
8141 const unsigned char* plocal_symbols
,
8142 Relocatable_relocs
* rr
)
8144 gold_assert(sh_type
== elfcpp::SHT_RELA
);
8146 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
8147 Powerpc_scan_relocatable_reloc
>(
8155 needs_special_offset_handling
,
8161 // Emit relocations for a section.
8162 // This is a modified version of the function by the same name in
8163 // target-reloc.h. Using relocate_special_relocatable for
8164 // R_PPC_PLTREL24 would require duplication of the entire body of the
8165 // loop, so we may as well duplicate the whole thing.
8167 template<int size
, bool big_endian
>
8169 Target_powerpc
<size
, big_endian
>::relocate_relocs(
8170 const Relocate_info
<size
, big_endian
>* relinfo
,
8171 unsigned int sh_type
,
8172 const unsigned char* prelocs
,
8174 Output_section
* output_section
,
8175 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
8176 const Relocatable_relocs
* rr
,
8178 Address view_address
,
8180 unsigned char* reloc_view
,
8181 section_size_type reloc_view_size
)
8183 gold_assert(sh_type
== elfcpp::SHT_RELA
);
8185 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
8187 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc_write
8189 const int reloc_size
8190 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
8192 Powerpc_relobj
<size
, big_endian
>* const object
8193 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
8194 const unsigned int local_count
= object
->local_symbol_count();
8195 unsigned int got2_shndx
= object
->got2_shndx();
8196 Address got2_addend
= 0;
8197 if (got2_shndx
!= 0)
8199 got2_addend
= object
->get_output_section_offset(got2_shndx
);
8200 gold_assert(got2_addend
!= invalid_address
);
8203 unsigned char* pwrite
= reloc_view
;
8204 bool zap_next
= false;
8205 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
8207 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
8208 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
8211 Reltype
reloc(prelocs
);
8212 Reltype_write
reloc_write(pwrite
);
8214 Address offset
= reloc
.get_r_offset();
8215 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
8216 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
8217 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
8218 const unsigned int orig_r_sym
= r_sym
;
8219 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
8220 = reloc
.get_r_addend();
8221 const Symbol
* gsym
= NULL
;
8225 // We could arrange to discard these and other relocs for
8226 // tls optimised sequences in the strategy methods, but for
8227 // now do as BFD ld does.
8228 r_type
= elfcpp::R_POWERPC_NONE
;
8232 // Get the new symbol index.
8233 Output_section
* os
= NULL
;
8234 if (r_sym
< local_count
)
8238 case Relocatable_relocs::RELOC_COPY
:
8239 case Relocatable_relocs::RELOC_SPECIAL
:
8242 r_sym
= object
->symtab_index(r_sym
);
8243 gold_assert(r_sym
!= -1U);
8247 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
8249 // We are adjusting a section symbol. We need to find
8250 // the symbol table index of the section symbol for
8251 // the output section corresponding to input section
8252 // in which this symbol is defined.
8253 gold_assert(r_sym
< local_count
);
8255 unsigned int shndx
=
8256 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
8257 gold_assert(is_ordinary
);
8258 os
= object
->output_section(shndx
);
8259 gold_assert(os
!= NULL
);
8260 gold_assert(os
->needs_symtab_index());
8261 r_sym
= os
->symtab_index();
8271 gsym
= object
->global_symbol(r_sym
);
8272 gold_assert(gsym
!= NULL
);
8273 if (gsym
->is_forwarder())
8274 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
8276 gold_assert(gsym
->has_symtab_index());
8277 r_sym
= gsym
->symtab_index();
8280 // Get the new offset--the location in the output section where
8281 // this relocation should be applied.
8282 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
8283 offset
+= offset_in_output_section
;
8286 section_offset_type sot_offset
=
8287 convert_types
<section_offset_type
, Address
>(offset
);
8288 section_offset_type new_sot_offset
=
8289 output_section
->output_offset(object
, relinfo
->data_shndx
,
8291 gold_assert(new_sot_offset
!= -1);
8292 offset
= new_sot_offset
;
8295 // In an object file, r_offset is an offset within the section.
8296 // In an executable or dynamic object, generated by
8297 // --emit-relocs, r_offset is an absolute address.
8298 if (!parameters
->options().relocatable())
8300 offset
+= view_address
;
8301 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
8302 offset
-= offset_in_output_section
;
8305 // Handle the reloc addend based on the strategy.
8306 if (strategy
== Relocatable_relocs::RELOC_COPY
)
8308 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
8310 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
8311 gold_assert(os
!= NULL
);
8312 addend
= psymval
->value(object
, addend
) - os
->address();
8314 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
8316 if (addend
>= 32768)
8317 addend
+= got2_addend
;
8322 if (!parameters
->options().relocatable())
8324 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8325 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
8326 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
8327 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
8329 // First instruction of a global dynamic sequence,
8331 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8332 switch (this->optimize_tls_gd(final
))
8334 case tls::TLSOPT_TO_IE
:
8335 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
8336 - elfcpp::R_POWERPC_GOT_TLSGD16
);
8338 case tls::TLSOPT_TO_LE
:
8339 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
8340 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
8341 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8344 r_type
= elfcpp::R_POWERPC_NONE
;
8345 offset
-= 2 * big_endian
;
8352 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
8353 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
8354 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
8355 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
8357 // First instruction of a local dynamic sequence,
8359 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
8361 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
8362 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
8364 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8365 const Output_section
* os
= relinfo
->layout
->tls_segment()
8367 gold_assert(os
!= NULL
);
8368 gold_assert(os
->needs_symtab_index());
8369 r_sym
= os
->symtab_index();
8370 addend
= dtp_offset
;
8374 r_type
= elfcpp::R_POWERPC_NONE
;
8375 offset
-= 2 * big_endian
;
8379 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8380 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
8381 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
8382 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
8384 // First instruction of initial exec sequence.
8385 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8386 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
8388 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
8389 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
8390 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
8393 r_type
= elfcpp::R_POWERPC_NONE
;
8394 offset
-= 2 * big_endian
;
8398 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
8399 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
8401 // Second instruction of a global dynamic sequence,
8402 // the __tls_get_addr call
8403 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8404 switch (this->optimize_tls_gd(final
))
8406 case tls::TLSOPT_TO_IE
:
8407 r_type
= elfcpp::R_POWERPC_NONE
;
8410 case tls::TLSOPT_TO_LE
:
8411 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8412 offset
+= 2 * big_endian
;
8419 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
8420 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
8422 // Second instruction of a local dynamic sequence,
8423 // the __tls_get_addr call
8424 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
8426 const Output_section
* os
= relinfo
->layout
->tls_segment()
8428 gold_assert(os
!= NULL
);
8429 gold_assert(os
->needs_symtab_index());
8430 r_sym
= os
->symtab_index();
8431 addend
= dtp_offset
;
8432 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8433 offset
+= 2 * big_endian
;
8437 else if (r_type
== elfcpp::R_POWERPC_TLS
)
8439 // Second instruction of an initial exec sequence
8440 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
8441 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
8443 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
8444 offset
+= 2 * big_endian
;
8449 reloc_write
.put_r_offset(offset
);
8450 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
8451 reloc_write
.put_r_addend(addend
);
8453 pwrite
+= reloc_size
;
8456 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
8457 == reloc_view_size
);
8460 // Return the value to use for a dynamic symbol which requires special
8461 // treatment. This is how we support equality comparisons of function
8462 // pointers across shared library boundaries, as described in the
8463 // processor specific ABI supplement.
8465 template<int size
, bool big_endian
>
8467 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
8471 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
8472 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
8473 p
!= this->stub_tables_
.end();
8476 Address off
= (*p
)->find_plt_call_entry(gsym
);
8477 if (off
!= invalid_address
)
8478 return (*p
)->stub_address() + off
;
8481 else if (this->abiversion() >= 2)
8483 Address off
= this->glink_section()->find_global_entry(gsym
);
8484 if (off
!= invalid_address
)
8485 return this->glink_section()->global_entry_address() + off
;
8490 // Return the PLT address to use for a local symbol.
8491 template<int size
, bool big_endian
>
8493 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
8494 const Relobj
* object
,
8495 unsigned int symndx
) const
8499 const Sized_relobj
<size
, big_endian
>* relobj
8500 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
8501 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
8502 p
!= this->stub_tables_
.end();
8505 Address off
= (*p
)->find_plt_call_entry(relobj
->sized_relobj(),
8507 if (off
!= invalid_address
)
8508 return (*p
)->stub_address() + off
;
8514 // Return the PLT address to use for a global symbol.
8515 template<int size
, bool big_endian
>
8517 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
8518 const Symbol
* gsym
) const
8522 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
8523 p
!= this->stub_tables_
.end();
8526 Address off
= (*p
)->find_plt_call_entry(gsym
);
8527 if (off
!= invalid_address
)
8528 return (*p
)->stub_address() + off
;
8531 else if (this->abiversion() >= 2)
8533 Address off
= this->glink_section()->find_global_entry(gsym
);
8534 if (off
!= invalid_address
)
8535 return this->glink_section()->global_entry_address() + off
;
8540 // Return the offset to use for the GOT_INDX'th got entry which is
8541 // for a local tls symbol specified by OBJECT, SYMNDX.
8542 template<int size
, bool big_endian
>
8544 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
8545 const Relobj
* object
,
8546 unsigned int symndx
,
8547 unsigned int got_indx
) const
8549 const Powerpc_relobj
<size
, big_endian
>* ppc_object
8550 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
8551 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
8553 for (Got_type got_type
= GOT_TYPE_TLSGD
;
8554 got_type
<= GOT_TYPE_TPREL
;
8555 got_type
= Got_type(got_type
+ 1))
8556 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
8558 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
8559 if (got_type
== GOT_TYPE_TLSGD
)
8561 if (off
== got_indx
* (size
/ 8))
8563 if (got_type
== GOT_TYPE_TPREL
)
8573 // Return the offset to use for the GOT_INDX'th got entry which is
8574 // for global tls symbol GSYM.
8575 template<int size
, bool big_endian
>
8577 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
8579 unsigned int got_indx
) const
8581 if (gsym
->type() == elfcpp::STT_TLS
)
8583 for (Got_type got_type
= GOT_TYPE_TLSGD
;
8584 got_type
<= GOT_TYPE_TPREL
;
8585 got_type
= Got_type(got_type
+ 1))
8586 if (gsym
->has_got_offset(got_type
))
8588 unsigned int off
= gsym
->got_offset(got_type
);
8589 if (got_type
== GOT_TYPE_TLSGD
)
8591 if (off
== got_indx
* (size
/ 8))
8593 if (got_type
== GOT_TYPE_TPREL
)
8603 // The selector for powerpc object files.
8605 template<int size
, bool big_endian
>
8606 class Target_selector_powerpc
: public Target_selector
8609 Target_selector_powerpc()
8610 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
8613 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
8614 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
8616 ? (big_endian
? "elf64ppc" : "elf64lppc")
8617 : (big_endian
? "elf32ppc" : "elf32lppc")))
8621 do_instantiate_target()
8622 { return new Target_powerpc
<size
, big_endian
>(); }
8625 Target_selector_powerpc
<32, true> target_selector_ppc32
;
8626 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
8627 Target_selector_powerpc
<64, true> target_selector_ppc64
;
8628 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
8630 // Instantiate these constants for -O0
8631 template<int size
, bool big_endian
>
8632 const int Output_data_glink
<size
, big_endian
>::pltresolve_size
;
8633 template<int size
, bool big_endian
>
8634 const typename Output_data_glink
<size
, big_endian
>::Address
8635 Output_data_glink
<size
, big_endian
>::invalid_address
;
8636 template<int size
, bool big_endian
>
8637 const typename Stub_table
<size
, big_endian
>::Address
8638 Stub_table
<size
, big_endian
>::invalid_address
;
8639 template<int size
, bool big_endian
>
8640 const typename Target_powerpc
<size
, big_endian
>::Address
8641 Target_powerpc
<size
, big_endian
>::invalid_address
;
8643 } // End anonymous namespace.