1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009, 2010, 2011, 2012 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.
29 #include "parameters.h"
36 #include "copy-relocs.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
49 template<int size
, bool big_endian
>
50 class Output_data_plt_powerpc
;
52 template<int size
, bool big_endian
>
53 class Output_data_brlt_powerpc
;
55 template<int size
, bool big_endian
>
56 class Output_data_got_powerpc
;
58 template<int size
, bool big_endian
>
59 class Output_data_glink
;
61 template<int size
, bool big_endian
>
64 template<int size
, bool big_endian
>
65 class Powerpc_relobj
: public Sized_relobj_file
<size
, big_endian
>
68 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
69 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_refs
;
70 typedef Unordered_map
<Address
, Section_refs
> Access_from
;
72 Powerpc_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
73 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
74 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
75 special_(0), has_small_toc_reloc_(false), opd_valid_(false),
76 opd_ent_(), access_from_map_(), has14_(), stub_table_()
82 // The .got2 section shndx.
87 return this->special_
;
92 // The .opd section shndx.
99 return this->special_
;
102 // Init OPD entry arrays.
104 init_opd(size_t opd_size
)
106 size_t count
= this->opd_ent_ndx(opd_size
);
107 this->opd_ent_
.resize(count
);
110 // Return section and offset of function entry for .opd + R_OFF.
112 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
114 size_t ndx
= this->opd_ent_ndx(r_off
);
115 gold_assert(ndx
< this->opd_ent_
.size());
116 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
118 *value
= this->opd_ent_
[ndx
].off
;
119 return this->opd_ent_
[ndx
].shndx
;
122 // Set section and offset of function entry for .opd + R_OFF.
124 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
126 size_t ndx
= this->opd_ent_ndx(r_off
);
127 gold_assert(ndx
< this->opd_ent_
.size());
128 this->opd_ent_
[ndx
].shndx
= shndx
;
129 this->opd_ent_
[ndx
].off
= value
;
132 // Return discard flag for .opd + R_OFF.
134 get_opd_discard(Address r_off
) const
136 size_t ndx
= this->opd_ent_ndx(r_off
);
137 gold_assert(ndx
< this->opd_ent_
.size());
138 return this->opd_ent_
[ndx
].discard
;
141 // Set discard flag for .opd + R_OFF.
143 set_opd_discard(Address r_off
)
145 size_t ndx
= this->opd_ent_ndx(r_off
);
146 gold_assert(ndx
< this->opd_ent_
.size());
147 this->opd_ent_
[ndx
].discard
= true;
152 { return &this->access_from_map_
; }
154 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
155 // section at DST_OFF.
157 add_reference(Object
* src_obj
,
158 unsigned int src_indx
,
159 typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
161 Section_id
src_id(src_obj
, src_indx
);
162 this->access_from_map_
[dst_off
].insert(src_id
);
165 // Add a reference to the code section specified by the .opd entry
168 add_gc_mark(typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
170 size_t ndx
= this->opd_ent_ndx(dst_off
);
171 if (ndx
>= this->opd_ent_
.size())
172 this->opd_ent_
.resize(ndx
+ 1);
173 this->opd_ent_
[ndx
].gc_mark
= true;
177 process_gc_mark(Symbol_table
* symtab
)
179 for (size_t i
= 0; i
< this->opd_ent_
.size(); i
++)
180 if (this->opd_ent_
[i
].gc_mark
)
182 unsigned int shndx
= this->opd_ent_
[i
].shndx
;
183 symtab
->gc()->worklist().push(Section_id(this, shndx
));
189 { return this->opd_valid_
; }
193 { this->opd_valid_
= true; }
195 // Examine .rela.opd to build info about function entry points.
197 scan_opd_relocs(size_t reloc_count
,
198 const unsigned char* prelocs
,
199 const unsigned char* plocal_syms
);
201 // Perform the Sized_relobj_file method, then set up opd info from
204 do_read_relocs(Read_relocs_data
*);
207 do_find_special_sections(Read_symbols_data
* sd
);
209 // Adjust this local symbol value. Return false if the symbol
210 // should be discarded from the output file.
212 do_adjust_local_symbol(Symbol_value
<size
>* lv
) const
214 if (size
== 64 && this->opd_shndx() != 0)
217 if (lv
->input_shndx(&is_ordinary
) != this->opd_shndx())
219 if (this->get_opd_discard(lv
->input_value()))
225 // Return offset in output GOT section that this object will use
226 // as a TOC pointer. Won't be just a constant with multi-toc support.
228 toc_base_offset() const
232 set_has_small_toc_reloc()
233 { has_small_toc_reloc_
= true; }
236 has_small_toc_reloc() const
237 { return has_small_toc_reloc_
; }
240 set_has_14bit_branch(unsigned int shndx
)
242 if (shndx
>= this->has14_
.size())
243 this->has14_
.resize(shndx
+ 1);
244 this->has14_
[shndx
] = true;
248 has_14bit_branch(unsigned int shndx
) const
249 { return shndx
< this->has14_
.size() && this->has14_
[shndx
]; }
252 set_stub_table(unsigned int shndx
, Stub_table
<size
, big_endian
>* stub_table
)
254 if (shndx
>= this->stub_table_
.size())
255 this->stub_table_
.resize(shndx
+ 1);
256 this->stub_table_
[shndx
] = stub_table
;
259 Stub_table
<size
, big_endian
>*
260 stub_table(unsigned int shndx
)
262 if (shndx
< this->stub_table_
.size())
263 return this->stub_table_
[shndx
];
276 // Return index into opd_ent_ array for .opd entry at OFF.
277 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
278 // apart when the language doesn't use the last 8-byte word, the
279 // environment pointer. Thus dividing the entry section offset by
280 // 16 will give an index into opd_ent_ that works for either layout
281 // of .opd. (It leaves some elements of the vector unused when .opd
282 // entries are spaced 24 bytes apart, but we don't know the spacing
283 // until relocations are processed, and in any case it is possible
284 // for an object to have some entries spaced 16 bytes apart and
285 // others 24 bytes apart.)
287 opd_ent_ndx(size_t off
) const
290 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
291 unsigned int special_
;
293 // For 64-bit, whether this object uses small model relocs to access
295 bool has_small_toc_reloc_
;
297 // Set at the start of gc_process_relocs, when we know opd_ent_
298 // vector is valid. The flag could be made atomic and set in
299 // do_read_relocs with memory_order_release and then tested with
300 // memory_order_acquire, potentially resulting in fewer entries in
304 // The first 8-byte word of an OPD entry gives the address of the
305 // entry point of the function. Relocatable object files have a
306 // relocation on this word. The following vector records the
307 // section and offset specified by these relocations.
308 std::vector
<Opd_ent
> opd_ent_
;
310 // References made to this object's .opd section when running
311 // gc_process_relocs for another object, before the opd_ent_ vector
312 // is valid for this object.
313 Access_from access_from_map_
;
315 // Whether input section has a 14-bit branch reloc.
316 std::vector
<bool> has14_
;
318 // The stub table to use for a given input section.
319 std::vector
<Stub_table
<size
, big_endian
>*> stub_table_
;
322 template<int size
, bool big_endian
>
323 class Target_powerpc
: public Sized_target
<size
, big_endian
>
327 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
328 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
329 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Signed_address
;
330 static const Address invalid_address
= static_cast<Address
>(0) - 1;
331 // Offset of tp and dtp pointers from start of TLS block.
332 static const Address tp_offset
= 0x7000;
333 static const Address dtp_offset
= 0x8000;
336 : Sized_target
<size
, big_endian
>(&powerpc_info
),
337 got_(NULL
), plt_(NULL
), iplt_(NULL
), brlt_section_(NULL
),
338 glink_(NULL
), rela_dyn_(NULL
), copy_relocs_(elfcpp::R_POWERPC_COPY
),
339 dynbss_(NULL
), tlsld_got_offset_(-1U),
340 stub_tables_(), branch_lookup_table_(), branch_info_(),
341 plt_thread_safe_(false)
345 // Process the relocations to determine unreferenced sections for
346 // garbage collection.
348 gc_process_relocs(Symbol_table
* symtab
,
350 Sized_relobj_file
<size
, big_endian
>* object
,
351 unsigned int data_shndx
,
352 unsigned int sh_type
,
353 const unsigned char* prelocs
,
355 Output_section
* output_section
,
356 bool needs_special_offset_handling
,
357 size_t local_symbol_count
,
358 const unsigned char* plocal_symbols
);
360 // Scan the relocations to look for symbol adjustments.
362 scan_relocs(Symbol_table
* symtab
,
364 Sized_relobj_file
<size
, big_endian
>* object
,
365 unsigned int data_shndx
,
366 unsigned int sh_type
,
367 const unsigned char* prelocs
,
369 Output_section
* output_section
,
370 bool needs_special_offset_handling
,
371 size_t local_symbol_count
,
372 const unsigned char* plocal_symbols
);
374 // Map input .toc section to output .got section.
376 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
378 if (size
== 64 && strcmp(name
, ".toc") == 0)
386 // Provide linker defined save/restore functions.
388 define_save_restore_funcs(Layout
*, Symbol_table
*);
390 // No stubs unless a final link.
393 { return !parameters
->options().relocatable(); }
396 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
399 do_plt_fde_location(const Output_data
*, unsigned char*,
400 uint64_t*, off_t
*) const;
402 // Stash info about branches, for stub generation.
404 push_branch(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
405 unsigned int data_shndx
, Address r_offset
,
406 unsigned int r_type
, unsigned int r_sym
, Address addend
)
408 Branch_info
info(ppc_object
, data_shndx
, r_offset
, r_type
, r_sym
, addend
);
409 this->branch_info_
.push_back(info
);
410 if (r_type
== elfcpp::R_POWERPC_REL14
411 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
412 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
413 ppc_object
->set_has_14bit_branch(data_shndx
);
416 Stub_table
<size
, big_endian
>*
420 do_define_standard_symbols(Symbol_table
*, Layout
*);
422 // Finalize the sections.
424 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
426 // Return the value to use for a dynamic which requires special
429 do_dynsym_value(const Symbol
*) const;
431 // Return the PLT address to use for a local symbol.
433 do_plt_address_for_local(const Relobj
*, unsigned int) const;
435 // Return the PLT address to use for a global symbol.
437 do_plt_address_for_global(const Symbol
*) const;
439 // Return the offset to use for the GOT_INDX'th got entry which is
440 // for a local tls symbol specified by OBJECT, SYMNDX.
442 do_tls_offset_for_local(const Relobj
* object
,
444 unsigned int got_indx
) const;
446 // Return the offset to use for the GOT_INDX'th got entry which is
447 // for global tls symbol GSYM.
449 do_tls_offset_for_global(Symbol
* gsym
, unsigned int got_indx
) const;
451 // Relocate a section.
453 relocate_section(const Relocate_info
<size
, big_endian
>*,
454 unsigned int sh_type
,
455 const unsigned char* prelocs
,
457 Output_section
* output_section
,
458 bool needs_special_offset_handling
,
460 Address view_address
,
461 section_size_type view_size
,
462 const Reloc_symbol_changes
*);
464 // Scan the relocs during a relocatable link.
466 scan_relocatable_relocs(Symbol_table
* symtab
,
468 Sized_relobj_file
<size
, big_endian
>* object
,
469 unsigned int data_shndx
,
470 unsigned int sh_type
,
471 const unsigned char* prelocs
,
473 Output_section
* output_section
,
474 bool needs_special_offset_handling
,
475 size_t local_symbol_count
,
476 const unsigned char* plocal_symbols
,
477 Relocatable_relocs
*);
479 // Emit relocations for a section.
481 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
482 unsigned int sh_type
,
483 const unsigned char* prelocs
,
485 Output_section
* output_section
,
486 typename
elfcpp::Elf_types
<size
>::Elf_Off
487 offset_in_output_section
,
488 const Relocatable_relocs
*,
490 Address view_address
,
492 unsigned char* reloc_view
,
493 section_size_type reloc_view_size
);
495 // Return whether SYM is defined by the ABI.
497 do_is_defined_by_abi(const Symbol
* sym
) const
499 return strcmp(sym
->name(), "__tls_get_addr") == 0;
502 // Return the size of the GOT section.
506 gold_assert(this->got_
!= NULL
);
507 return this->got_
->data_size();
510 // Get the PLT section.
511 const Output_data_plt_powerpc
<size
, big_endian
>*
514 gold_assert(this->plt_
!= NULL
);
518 // Get the IPLT section.
519 const Output_data_plt_powerpc
<size
, big_endian
>*
522 gold_assert(this->iplt_
!= NULL
);
526 // Get the .glink section.
527 const Output_data_glink
<size
, big_endian
>*
528 glink_section() const
530 gold_assert(this->glink_
!= NULL
);
534 bool has_glink() const
535 { return this->glink_
!= NULL
; }
537 // Get the GOT section.
538 const Output_data_got_powerpc
<size
, big_endian
>*
541 gold_assert(this->got_
!= NULL
);
545 // Get the GOT section, creating it if necessary.
546 Output_data_got_powerpc
<size
, big_endian
>*
547 got_section(Symbol_table
*, Layout
*);
550 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
551 const elfcpp::Ehdr
<size
, big_endian
>&);
553 // Return the number of entries in the GOT.
555 got_entry_count() const
557 if (this->got_
== NULL
)
559 return this->got_size() / (size
/ 8);
562 // Return the number of entries in the PLT.
564 plt_entry_count() const;
566 // Return the offset of the first non-reserved PLT entry.
568 first_plt_entry_offset() const;
570 // Return the size of each PLT entry.
572 plt_entry_size() const;
574 // Add any special sections for this symbol to the gc work list.
575 // For powerpc64, this adds the code section of a function
578 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
580 // Handle target specific gc actions when adding a gc reference from
581 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
582 // and DST_OFF. For powerpc64, this adds a referenc to the code
583 // section of a function descriptor.
585 do_gc_add_reference(Symbol_table
* symtab
,
587 unsigned int src_shndx
,
589 unsigned int dst_shndx
,
590 Address dst_off
) const;
592 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
595 { return this->stub_tables_
; }
597 const Output_data_brlt_powerpc
<size
, big_endian
>*
599 { return this->brlt_section_
; }
602 add_branch_lookup_table(Address to
)
604 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
605 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
609 find_branch_lookup_table(Address to
)
611 typename
Branch_lookup_table::const_iterator p
612 = this->branch_lookup_table_
.find(to
);
613 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
617 write_branch_lookup_table(unsigned char *oview
)
619 for (typename
Branch_lookup_table::const_iterator p
620 = this->branch_lookup_table_
.begin();
621 p
!= this->branch_lookup_table_
.end();
624 elfcpp::Swap
<32, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
629 plt_thread_safe() const
630 { return this->plt_thread_safe_
; }
646 : tls_get_addr_(NOT_EXPECTED
),
647 relinfo_(NULL
), relnum_(0), r_offset_(0)
652 if (this->tls_get_addr_
!= NOT_EXPECTED
)
659 if (this->relinfo_
!= NULL
)
660 gold_error_at_location(this->relinfo_
, this->relnum_
, this->r_offset_
,
661 _("missing expected __tls_get_addr call"));
665 expect_tls_get_addr_call(
666 const Relocate_info
<size
, big_endian
>* relinfo
,
670 this->tls_get_addr_
= EXPECTED
;
671 this->relinfo_
= relinfo
;
672 this->relnum_
= relnum
;
673 this->r_offset_
= r_offset
;
677 expect_tls_get_addr_call()
678 { this->tls_get_addr_
= EXPECTED
; }
681 skip_next_tls_get_addr_call()
682 {this->tls_get_addr_
= SKIP
; }
685 maybe_skip_tls_get_addr_call(unsigned int r_type
, const Symbol
* gsym
)
687 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
688 || r_type
== elfcpp::R_PPC_PLTREL24
)
690 && strcmp(gsym
->name(), "__tls_get_addr") == 0);
691 Tls_get_addr last_tls
= this->tls_get_addr_
;
692 this->tls_get_addr_
= NOT_EXPECTED
;
693 if (is_tls_call
&& last_tls
!= EXPECTED
)
695 else if (!is_tls_call
&& last_tls
!= NOT_EXPECTED
)
704 // What we're up to regarding calls to __tls_get_addr.
705 // On powerpc, the branch and link insn making a call to
706 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
707 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
708 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
709 // The marker relocation always comes first, and has the same
710 // symbol as the reloc on the insn setting up the __tls_get_addr
711 // argument. This ties the arg setup insn with the call insn,
712 // allowing ld to safely optimize away the call. We check that
713 // every call to __tls_get_addr has a marker relocation, and that
714 // every marker relocation is on a call to __tls_get_addr.
715 Tls_get_addr tls_get_addr_
;
716 // Info about the last reloc for error message.
717 const Relocate_info
<size
, big_endian
>* relinfo_
;
722 // The class which scans relocations.
723 class Scan
: protected Track_tls
726 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
729 : Track_tls(), issued_non_pic_error_(false)
733 get_reference_flags(unsigned int r_type
);
736 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
737 Sized_relobj_file
<size
, big_endian
>* object
,
738 unsigned int data_shndx
,
739 Output_section
* output_section
,
740 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
741 const elfcpp::Sym
<size
, big_endian
>& lsym
,
745 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
746 Sized_relobj_file
<size
, big_endian
>* object
,
747 unsigned int data_shndx
,
748 Output_section
* output_section
,
749 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
753 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
755 Sized_relobj_file
<size
, big_endian
>* ,
758 const elfcpp::Rela
<size
, big_endian
>& ,
760 const elfcpp::Sym
<size
, big_endian
>&)
764 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
766 Sized_relobj_file
<size
, big_endian
>* ,
769 const elfcpp::Rela
<size
,
771 unsigned int , Symbol
*)
776 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
777 unsigned int r_type
);
780 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
781 unsigned int r_type
, Symbol
*);
784 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
785 Target_powerpc
* target
);
788 check_non_pic(Relobj
*, unsigned int r_type
);
791 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, big_endian
>* object
,
792 unsigned int r_type
);
794 // Whether we have issued an error about a non-PIC compilation.
795 bool issued_non_pic_error_
;
799 symval_for_branch(Address value
, const Sized_symbol
<size
>* gsym
,
800 Powerpc_relobj
<size
, big_endian
>* object
,
801 unsigned int *dest_shndx
);
803 // The class which implements relocation.
804 class Relocate
: protected Track_tls
807 // Use 'at' branch hints when true, 'y' when false.
808 // FIXME maybe: set this with an option.
809 static const bool is_isa_v2
= true;
815 // Do a relocation. Return false if the caller should not issue
816 // any warnings about this relocation.
818 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
819 Output_section
*, size_t relnum
,
820 const elfcpp::Rela
<size
, big_endian
>&,
821 unsigned int r_type
, const Sized_symbol
<size
>*,
822 const Symbol_value
<size
>*,
824 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
828 class Relocate_comdat_behavior
831 // Decide what the linker should do for relocations that refer to
832 // discarded comdat sections.
833 inline Comdat_behavior
834 get(const char* name
)
836 gold::Default_comdat_behavior default_behavior
;
837 Comdat_behavior ret
= default_behavior
.get(name
);
838 if (ret
== CB_WARNING
)
841 && (strcmp(name
, ".fixup") == 0
842 || strcmp(name
, ".got2") == 0))
845 && (strcmp(name
, ".opd") == 0
846 || strcmp(name
, ".toc") == 0
847 || strcmp(name
, ".toc1") == 0))
854 // A class which returns the size required for a relocation type,
855 // used while scanning relocs during a relocatable link.
856 class Relocatable_size_for_reloc
860 get_size_for_reloc(unsigned int, Relobj
*)
867 // Optimize the TLS relocation type based on what we know about the
868 // symbol. IS_FINAL is true if the final address of this symbol is
869 // known at link time.
871 tls::Tls_optimization
872 optimize_tls_gd(bool is_final
)
874 // If we are generating a shared library, then we can't do anything
876 if (parameters
->options().shared())
877 return tls::TLSOPT_NONE
;
880 return tls::TLSOPT_TO_IE
;
881 return tls::TLSOPT_TO_LE
;
884 tls::Tls_optimization
887 if (parameters
->options().shared())
888 return tls::TLSOPT_NONE
;
890 return tls::TLSOPT_TO_LE
;
893 tls::Tls_optimization
894 optimize_tls_ie(bool is_final
)
896 if (!is_final
|| parameters
->options().shared())
897 return tls::TLSOPT_NONE
;
899 return tls::TLSOPT_TO_LE
;
904 make_glink_section(Layout
*);
906 // Create the PLT section.
908 make_plt_section(Symbol_table
*, Layout
*);
911 make_iplt_section(Symbol_table
*, Layout
*);
914 make_brlt_section(Layout
*);
916 // Create a PLT entry for a global symbol.
918 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
920 // Create a PLT entry for a local IFUNC symbol.
922 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
923 Sized_relobj_file
<size
, big_endian
>*,
927 // Create a GOT entry for local dynamic __tls_get_addr.
929 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
930 Sized_relobj_file
<size
, big_endian
>* object
);
933 tlsld_got_offset() const
935 return this->tlsld_got_offset_
;
938 // Get the dynamic reloc section, creating it if necessary.
940 rela_dyn_section(Layout
*);
942 // Copy a relocation against a global symbol.
944 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
945 Sized_relobj_file
<size
, big_endian
>* object
,
946 unsigned int shndx
, Output_section
* output_section
,
947 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
949 this->copy_relocs_
.copy_reloc(symtab
, layout
,
950 symtab
->get_sized_symbol
<size
>(sym
),
951 object
, shndx
, output_section
,
952 reloc
, this->rela_dyn_section(layout
));
955 // Look over all the input sections, deciding where to place stub.
957 group_sections(Layout
*, const Task
*);
959 // Sort output sections by address.
963 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
964 { return sec1
->address() < sec2
->address(); }
970 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
971 unsigned int data_shndx
,
976 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
977 r_type_(r_type
), r_sym_(r_sym
), addend_(addend
)
983 // If this branch needs a plt call stub, or a long branch stub, make one.
985 make_stub(Stub_table
<size
, big_endian
>*,
986 Stub_table
<size
, big_endian
>*,
987 Symbol_table
*) const;
990 // The branch location..
991 Powerpc_relobj
<size
, big_endian
>* object_
;
994 // ..and the branch type and destination.
995 unsigned int r_type_
;
1000 // Information about this specific target which we pass to the
1001 // general Target structure.
1002 static Target::Target_info powerpc_info
;
1004 // The types of GOT entries needed for this platform.
1005 // These values are exposed to the ABI in an incremental link.
1006 // Do not renumber existing values without changing the version
1007 // number of the .gnu_incremental_inputs section.
1011 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
1012 GOT_TYPE_DTPREL
, // entry for @got@dtprel
1013 GOT_TYPE_TPREL
// entry for @got@tprel
1017 Output_data_got_powerpc
<size
, big_endian
>* got_
;
1019 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
1020 // The IPLT section.
1021 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
1022 // Section holding long branch destinations.
1023 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
1024 // The .glink section.
1025 Output_data_glink
<size
, big_endian
>* glink_
;
1026 // The dynamic reloc section.
1027 Reloc_section
* rela_dyn_
;
1028 // Relocs saved to avoid a COPY reloc.
1029 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
1030 // Space for variables copied with a COPY reloc.
1031 Output_data_space
* dynbss_
;
1032 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1033 unsigned int tlsld_got_offset_
;
1035 Stub_tables stub_tables_
;
1036 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
1037 Branch_lookup_table branch_lookup_table_
;
1039 typedef std::vector
<Branch_info
> Branches
;
1040 Branches branch_info_
;
1042 bool plt_thread_safe_
;
1046 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
1049 true, // is_big_endian
1050 elfcpp::EM_PPC
, // machine_code
1051 false, // has_make_symbol
1052 false, // has_resolve
1053 false, // has_code_fill
1054 true, // is_default_stack_executable
1055 false, // can_icf_inline_merge_sections
1057 "/usr/lib/ld.so.1", // dynamic_linker
1058 0x10000000, // default_text_segment_address
1059 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1060 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1061 false, // isolate_execinstr
1063 elfcpp::SHN_UNDEF
, // small_common_shndx
1064 elfcpp::SHN_UNDEF
, // large_common_shndx
1065 0, // small_common_section_flags
1066 0, // large_common_section_flags
1067 NULL
, // attributes_section
1068 NULL
// attributes_vendor
1072 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
1075 false, // is_big_endian
1076 elfcpp::EM_PPC
, // machine_code
1077 false, // has_make_symbol
1078 false, // has_resolve
1079 false, // has_code_fill
1080 true, // is_default_stack_executable
1081 false, // can_icf_inline_merge_sections
1083 "/usr/lib/ld.so.1", // dynamic_linker
1084 0x10000000, // default_text_segment_address
1085 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1086 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1087 false, // isolate_execinstr
1089 elfcpp::SHN_UNDEF
, // small_common_shndx
1090 elfcpp::SHN_UNDEF
, // large_common_shndx
1091 0, // small_common_section_flags
1092 0, // large_common_section_flags
1093 NULL
, // attributes_section
1094 NULL
// attributes_vendor
1098 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1101 true, // is_big_endian
1102 elfcpp::EM_PPC64
, // machine_code
1103 false, // has_make_symbol
1104 false, // has_resolve
1105 false, // has_code_fill
1106 true, // is_default_stack_executable
1107 false, // can_icf_inline_merge_sections
1109 "/usr/lib/ld.so.1", // dynamic_linker
1110 0x10000000, // default_text_segment_address
1111 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1112 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1113 false, // isolate_execinstr
1115 elfcpp::SHN_UNDEF
, // small_common_shndx
1116 elfcpp::SHN_UNDEF
, // large_common_shndx
1117 0, // small_common_section_flags
1118 0, // large_common_section_flags
1119 NULL
, // attributes_section
1120 NULL
// attributes_vendor
1124 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1127 false, // is_big_endian
1128 elfcpp::EM_PPC64
, // machine_code
1129 false, // has_make_symbol
1130 false, // has_resolve
1131 false, // has_code_fill
1132 true, // is_default_stack_executable
1133 false, // can_icf_inline_merge_sections
1135 "/usr/lib/ld.so.1", // dynamic_linker
1136 0x10000000, // default_text_segment_address
1137 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1138 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1139 false, // isolate_execinstr
1141 elfcpp::SHN_UNDEF
, // small_common_shndx
1142 elfcpp::SHN_UNDEF
, // large_common_shndx
1143 0, // small_common_section_flags
1144 0, // large_common_section_flags
1145 NULL
, // attributes_section
1146 NULL
// attributes_vendor
1150 is_branch_reloc(unsigned int r_type
)
1152 return (r_type
== elfcpp::R_POWERPC_REL24
1153 || r_type
== elfcpp::R_PPC_PLTREL24
1154 || r_type
== elfcpp::R_PPC_LOCAL24PC
1155 || r_type
== elfcpp::R_POWERPC_REL14
1156 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1157 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1158 || r_type
== elfcpp::R_POWERPC_ADDR24
1159 || r_type
== elfcpp::R_POWERPC_ADDR14
1160 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1161 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1164 // If INSN is an opcode that may be used with an @tls operand, return
1165 // the transformed insn for TLS optimisation, otherwise return 0. If
1166 // REG is non-zero only match an insn with RB or RA equal to REG.
1168 at_tls_transform(uint32_t insn
, unsigned int reg
)
1170 if ((insn
& (0x3f << 26)) != 31 << 26)
1174 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1175 rtra
= insn
& ((1 << 26) - (1 << 16));
1176 else if (((insn
>> 16) & 0x1f) == reg
)
1177 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1181 if ((insn
& (0x3ff << 1)) == 266 << 1)
1184 else if ((insn
& (0x1f << 1)) == 23 << 1
1185 && ((insn
& (0x1f << 6)) < 14 << 6
1186 || ((insn
& (0x1f << 6)) >= 16 << 6
1187 && (insn
& (0x1f << 6)) < 24 << 6)))
1188 // load and store indexed -> dform
1189 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1190 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1191 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1192 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1193 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1195 insn
= (58 << 26) | 2;
1202 // Modified version of symtab.h class Symbol member
1203 // Given a direct absolute or pc-relative static relocation against
1204 // the global symbol, this function returns whether a dynamic relocation
1209 needs_dynamic_reloc(const Symbol
* gsym
, int flags
)
1211 // No dynamic relocations in a static link!
1212 if (parameters
->doing_static_link())
1215 // A reference to an undefined symbol from an executable should be
1216 // statically resolved to 0, and does not need a dynamic relocation.
1217 // This matches gnu ld behavior.
1218 if (gsym
->is_undefined() && !parameters
->options().shared())
1221 // A reference to an absolute symbol does not need a dynamic relocation.
1222 if (gsym
->is_absolute())
1225 // An absolute reference within a position-independent output file
1226 // will need a dynamic relocation.
1227 if ((flags
& Symbol::ABSOLUTE_REF
)
1228 && parameters
->options().output_is_position_independent())
1231 // A function call that can branch to a local PLT entry does not need
1232 // a dynamic relocation.
1233 if ((flags
& Symbol::FUNCTION_CALL
) && gsym
->has_plt_offset())
1236 // A reference to any PLT entry in a non-position-independent executable
1237 // does not need a dynamic relocation.
1238 // Except due to having function descriptors on powerpc64 we don't define
1239 // functions to their plt code in an executable, so this doesn't apply.
1241 && !parameters
->options().output_is_position_independent()
1242 && gsym
->has_plt_offset())
1245 // A reference to a symbol defined in a dynamic object or to a
1246 // symbol that is preemptible will need a dynamic relocation.
1247 if (gsym
->is_from_dynobj()
1248 || gsym
->is_undefined()
1249 || gsym
->is_preemptible())
1252 // For all other cases, return FALSE.
1256 // Modified version of symtab.h class Symbol member
1257 // Whether we should use the PLT offset associated with a symbol for
1258 // a relocation. FLAGS is a set of Reference_flags.
1262 use_plt_offset(const Symbol
* gsym
, int flags
)
1264 // If the symbol doesn't have a PLT offset, then naturally we
1265 // don't want to use it.
1266 if (!gsym
->has_plt_offset())
1269 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
1270 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1273 // If we are going to generate a dynamic relocation, then we will
1274 // wind up using that, so no need to use the PLT entry.
1275 if (needs_dynamic_reloc
<size
>(gsym
, flags
))
1278 // If the symbol is from a dynamic object, we need to use the PLT
1280 if (gsym
->is_from_dynobj())
1283 // If we are generating a shared object, and this symbol is
1284 // undefined or preemptible, we need to use the PLT entry.
1285 if (parameters
->options().shared()
1286 && (gsym
->is_undefined() || gsym
->is_preemptible()))
1289 // If this is a call to a weak undefined symbol, we need to use
1290 // the PLT entry; the symbol may be defined by a library loaded
1292 if ((flags
& Symbol::FUNCTION_CALL
) && gsym
->is_weak_undefined())
1295 // Otherwise we can use the regular definition.
1299 template<int size
, bool big_endian
>
1300 class Powerpc_relocate_functions
1317 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1318 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1320 template<int valsize
>
1322 has_overflow_signed(Address value
)
1324 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1325 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1326 limit
<<= ((valsize
- 1) >> 1);
1327 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1328 return value
+ limit
> (limit
<< 1) - 1;
1331 template<int valsize
>
1333 has_overflow_bitfield(Address value
)
1335 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1336 limit
<<= ((valsize
- 1) >> 1);
1337 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1338 return value
> (limit
<< 1) - 1 && value
+ limit
> (limit
<< 1) - 1;
1341 template<int valsize
>
1342 static inline Status
1343 overflowed(Address value
, Overflow_check overflow
)
1345 if (overflow
== CHECK_SIGNED
)
1347 if (has_overflow_signed
<valsize
>(value
))
1348 return STATUS_OVERFLOW
;
1350 else if (overflow
== CHECK_BITFIELD
)
1352 if (has_overflow_bitfield
<valsize
>(value
))
1353 return STATUS_OVERFLOW
;
1358 // Do a simple RELA relocation
1359 template<int valsize
>
1360 static inline Status
1361 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1363 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1364 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1365 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
);
1366 return overflowed
<valsize
>(value
, overflow
);
1369 template<int valsize
>
1370 static inline Status
1371 rela(unsigned char* view
,
1372 unsigned int right_shift
,
1373 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
1375 Overflow_check overflow
)
1377 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1378 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1379 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
1380 Valtype reloc
= value
>> right_shift
;
1383 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
1384 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1387 // Do a simple RELA relocation, unaligned.
1388 template<int valsize
>
1389 static inline Status
1390 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1392 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
, value
);
1393 return overflowed
<valsize
>(value
, overflow
);
1396 template<int valsize
>
1397 static inline Status
1398 rela_ua(unsigned char* view
,
1399 unsigned int right_shift
,
1400 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
1402 Overflow_check overflow
)
1404 typedef typename
elfcpp::Swap_unaligned
<valsize
, big_endian
>::Valtype
1406 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(view
);
1407 Valtype reloc
= value
>> right_shift
;
1410 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
, val
| reloc
);
1411 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1415 // R_PPC64_ADDR64: (Symbol + Addend)
1417 addr64(unsigned char* view
, Address value
)
1418 { This::template rela
<64>(view
, value
, CHECK_NONE
); }
1420 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1422 addr64_u(unsigned char* view
, Address value
)
1423 { This::template rela_ua
<64>(view
, value
, CHECK_NONE
); }
1425 // R_POWERPC_ADDR32: (Symbol + Addend)
1426 static inline Status
1427 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
1428 { return This::template rela
<32>(view
, value
, overflow
); }
1430 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1431 static inline Status
1432 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1433 { return This::template rela_ua
<32>(view
, value
, overflow
); }
1435 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1436 static inline Status
1437 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
1439 Status stat
= This::template rela
<32>(view
, 0, 0x03fffffc, value
, overflow
);
1440 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1441 stat
= STATUS_OVERFLOW
;
1445 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1446 static inline Status
1447 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
1448 { return This::template rela
<16>(view
, value
, overflow
); }
1450 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1451 static inline Status
1452 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1453 { return This::template rela_ua
<16>(view
, value
, overflow
); }
1455 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1456 static inline Status
1457 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
1459 Status stat
= This::template rela
<16>(view
, 0, 0xfffc, value
, overflow
);
1460 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1461 stat
= STATUS_OVERFLOW
;
1465 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1467 addr16_hi(unsigned char* view
, Address value
)
1468 { This::template rela
<16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
1470 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1472 addr16_ha(unsigned char* view
, Address value
)
1473 { This::addr16_hi(view
, value
+ 0x8000); }
1475 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1477 addr16_hi2(unsigned char* view
, Address value
)
1478 { This::template rela
<16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
1480 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1482 addr16_ha2(unsigned char* view
, Address value
)
1483 { This::addr16_hi2(view
, value
+ 0x8000); }
1485 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1487 addr16_hi3(unsigned char* view
, Address value
)
1488 { This::template rela
<16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
1490 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1492 addr16_ha3(unsigned char* view
, Address value
)
1493 { This::addr16_hi3(view
, value
+ 0x8000); }
1495 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1496 static inline Status
1497 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
1499 Status stat
= This::template rela
<32>(view
, 0, 0xfffc, value
, overflow
);
1500 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1501 stat
= STATUS_OVERFLOW
;
1506 // Stash away the index of .got2 or .opd in a relocatable object, if
1507 // such a section exists.
1509 template<int size
, bool big_endian
>
1511 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
1512 Read_symbols_data
* sd
)
1514 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1515 const unsigned char* namesu
= sd
->section_names
->data();
1516 const char* names
= reinterpret_cast<const char*>(namesu
);
1517 section_size_type names_size
= sd
->section_names_size
;
1518 const unsigned char* s
;
1520 s
= this->find_shdr(pshdrs
, size
== 32 ? ".got2" : ".opd",
1521 names
, names_size
, NULL
);
1524 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
1525 this->special_
= ndx
;
1527 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
1530 // Examine .rela.opd to build info about function entry points.
1532 template<int size
, bool big_endian
>
1534 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
1536 const unsigned char* prelocs
,
1537 const unsigned char* plocal_syms
)
1541 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
1543 const int reloc_size
1544 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
1545 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1546 Address expected_off
= 0;
1547 bool regular
= true;
1548 unsigned int opd_ent_size
= 0;
1550 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
1552 Reltype
reloc(prelocs
);
1553 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
1554 = reloc
.get_r_info();
1555 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
1556 if (r_type
== elfcpp::R_PPC64_ADDR64
)
1558 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1559 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
1562 if (r_sym
< this->local_symbol_count())
1564 typename
elfcpp::Sym
<size
, big_endian
>
1565 lsym(plocal_syms
+ r_sym
* sym_size
);
1566 shndx
= lsym
.get_st_shndx();
1567 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1568 value
= lsym
.get_st_value();
1571 shndx
= this->symbol_section_and_value(r_sym
, &value
,
1573 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
1574 value
+ reloc
.get_r_addend());
1577 expected_off
= reloc
.get_r_offset();
1578 opd_ent_size
= expected_off
;
1580 else if (expected_off
!= reloc
.get_r_offset())
1582 expected_off
+= opd_ent_size
;
1584 else if (r_type
== elfcpp::R_PPC64_TOC
)
1586 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
1591 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1592 this->name().c_str(), r_type
);
1596 if (reloc_count
<= 2)
1597 opd_ent_size
= this->section_size(this->opd_shndx());
1598 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
1602 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1603 this->name().c_str());
1609 template<int size
, bool big_endian
>
1611 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
1613 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
1616 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
1617 p
!= rd
->relocs
.end();
1620 if (p
->data_shndx
== this->opd_shndx())
1622 uint64_t opd_size
= this->section_size(this->opd_shndx());
1623 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
1626 this->init_opd(opd_size
);
1627 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
1628 rd
->local_symbols
->data());
1636 // Set up some symbols.
1638 template<int size
, bool big_endian
>
1640 Target_powerpc
<size
, big_endian
>::do_define_standard_symbols(
1641 Symbol_table
* symtab
,
1646 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
1647 // undefined when scanning relocs (and thus requires
1648 // non-relative dynamic relocs). The proper value will be
1650 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
1651 if (gotsym
!= NULL
&& gotsym
->is_undefined())
1653 Target_powerpc
<size
, big_endian
>* target
=
1654 static_cast<Target_powerpc
<size
, big_endian
>*>(
1655 parameters
->sized_target
<size
, big_endian
>());
1656 Output_data_got_powerpc
<size
, big_endian
>* got
1657 = target
->got_section(symtab
, layout
);
1658 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1659 Symbol_table::PREDEFINED
,
1663 elfcpp::STV_HIDDEN
, 0,
1667 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
1668 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
1669 if (sdasym
!= NULL
&& sdasym
->is_undefined())
1671 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
1673 = layout
->add_output_section_data(".sdata", 0,
1675 | elfcpp::SHF_WRITE
,
1676 sdata
, ORDER_SMALL_DATA
, false);
1677 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
1678 Symbol_table::PREDEFINED
,
1679 os
, 32768, 0, elfcpp::STT_OBJECT
,
1680 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
1686 // Set up PowerPC target specific relobj.
1688 template<int size
, bool big_endian
>
1690 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
1691 const std::string
& name
,
1692 Input_file
* input_file
,
1693 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
1695 int et
= ehdr
.get_e_type();
1696 // ET_EXEC files are valid input for --just-symbols/-R,
1697 // and we treat them as relocatable objects.
1698 if (et
== elfcpp::ET_REL
1699 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
1701 Powerpc_relobj
<size
, big_endian
>* obj
=
1702 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1706 else if (et
== elfcpp::ET_DYN
)
1708 Sized_dynobj
<size
, big_endian
>* obj
=
1709 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1715 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
1720 template<int size
, bool big_endian
>
1721 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
1724 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1725 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1727 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
1728 : Output_data_got
<size
, big_endian
>(),
1729 symtab_(symtab
), layout_(layout
),
1730 header_ent_cnt_(size
== 32 ? 3 : 1),
1731 header_index_(size
== 32 ? 0x2000 : 0)
1736 // Create a new GOT entry and return its offset.
1738 add_got_entry(Got_entry got_entry
)
1740 this->reserve_ent();
1741 return Output_data_got
<size
, big_endian
>::add_got_entry(got_entry
);
1744 // Create a pair of new GOT entries and return the offset of the first.
1746 add_got_entry_pair(Got_entry got_entry_1
, Got_entry got_entry_2
)
1748 this->reserve_ent(2);
1749 return Output_data_got
<size
, big_endian
>::add_got_entry_pair(got_entry_1
,
1754 add_constant_pair(Valtype c1
, Valtype c2
)
1756 this->reserve_ent(2);
1757 unsigned int got_offset
= this->add_constant(c1
);
1758 this->add_constant(c2
);
1762 // Offset of _GLOBAL_OFFSET_TABLE_.
1766 return this->got_offset(this->header_index_
);
1769 // Offset of base used to access the GOT/TOC.
1770 // The got/toc pointer reg will be set to this value.
1772 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
1775 return this->g_o_t();
1777 return (this->output_section()->address()
1778 + object
->toc_base_offset()
1782 // Ensure our GOT has a header.
1784 set_final_data_size()
1786 if (this->header_ent_cnt_
!= 0)
1787 this->make_header();
1788 Output_data_got
<size
, big_endian
>::set_final_data_size();
1791 // First word of GOT header needs some values that are not
1792 // handled by Output_data_got so poke them in here.
1793 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1795 do_write(Output_file
* of
)
1798 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
1799 val
= this->layout_
->dynamic_section()->address();
1801 val
= this->output_section()->address() + 0x8000;
1802 this->replace_constant(this->header_index_
, val
);
1803 Output_data_got
<size
, big_endian
>::do_write(of
);
1808 reserve_ent(unsigned int cnt
= 1)
1810 if (this->header_ent_cnt_
== 0)
1812 if (this->num_entries() + cnt
> this->header_index_
)
1813 this->make_header();
1819 this->header_ent_cnt_
= 0;
1820 this->header_index_
= this->num_entries();
1823 Output_data_got
<size
, big_endian
>::add_constant(0);
1824 Output_data_got
<size
, big_endian
>::add_constant(0);
1825 Output_data_got
<size
, big_endian
>::add_constant(0);
1827 // Define _GLOBAL_OFFSET_TABLE_ at the header
1828 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
1831 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
1832 sym
->set_value(this->g_o_t());
1835 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1836 Symbol_table::PREDEFINED
,
1837 this, this->g_o_t(), 0,
1840 elfcpp::STV_HIDDEN
, 0,
1844 Output_data_got
<size
, big_endian
>::add_constant(0);
1847 // Stashed pointers.
1848 Symbol_table
* symtab_
;
1852 unsigned int header_ent_cnt_
;
1853 // GOT header index.
1854 unsigned int header_index_
;
1857 // Get the GOT section, creating it if necessary.
1859 template<int size
, bool big_endian
>
1860 Output_data_got_powerpc
<size
, big_endian
>*
1861 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
1864 if (this->got_
== NULL
)
1866 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1869 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
1871 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1872 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1873 this->got_
, ORDER_DATA
, false);
1879 // Get the dynamic reloc section, creating it if necessary.
1881 template<int size
, bool big_endian
>
1882 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
1883 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
1885 if (this->rela_dyn_
== NULL
)
1887 gold_assert(layout
!= NULL
);
1888 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1889 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1890 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1891 ORDER_DYNAMIC_RELOCS
, false);
1893 return this->rela_dyn_
;
1899 // Determine the stub group size. The group size is the absolute
1900 // value of the parameter --stub-group-size. If --stub-group-size
1901 // is passed a negative value, we restrict stubs to be always before
1902 // the stubbed branches.
1903 Stub_control(int32_t size
)
1904 : state_(NO_GROUP
), stub_group_size_(abs(size
)),
1905 stub14_group_size_(abs(size
)),
1906 stubs_always_before_branch_(size
< 0), suppress_size_errors_(false),
1907 group_end_addr_(0), owner_(NULL
), output_section_(NULL
)
1909 if (stub_group_size_
== 1)
1912 if (stubs_always_before_branch_
)
1914 stub_group_size_
= 0x1e00000;
1915 stub14_group_size_
= 0x7800;
1919 stub_group_size_
= 0x1c00000;
1920 stub14_group_size_
= 0x7000;
1922 suppress_size_errors_
= true;
1926 // Return true iff input section can be handled by current stub
1929 can_add_to_stub_group(Output_section
* o
,
1930 const Output_section::Input_section
* i
,
1933 const Output_section::Input_section
*
1939 { return output_section_
; }
1945 FINDING_STUB_SECTION
,
1950 uint32_t stub_group_size_
;
1951 uint32_t stub14_group_size_
;
1952 bool stubs_always_before_branch_
;
1953 bool suppress_size_errors_
;
1954 uint64_t group_end_addr_
;
1955 const Output_section::Input_section
* owner_
;
1956 Output_section
* output_section_
;
1959 // Return true iff input section can be handled by current stub/
1963 Stub_control::can_add_to_stub_group(Output_section
* o
,
1964 const Output_section::Input_section
* i
,
1968 = has14
? this->stub14_group_size_
: this->stub_group_size_
;
1969 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
1971 uint64_t start_addr
= o
->address();
1974 // .init and .fini sections are pasted together to form a single
1975 // function. We can't be adding stubs in the middle of the function.
1976 this_size
= o
->data_size();
1979 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
1980 this_size
= i
->data_size();
1982 uint64_t end_addr
= start_addr
+ this_size
;
1983 bool toobig
= this_size
> group_size
;
1985 if (toobig
&& !this->suppress_size_errors_
)
1986 gold_warning(_("%s:%s exceeds group size"),
1987 i
->relobj()->name().c_str(),
1988 i
->relobj()->section_name(i
->shndx()).c_str());
1990 if (this->state_
!= HAS_STUB_SECTION
1991 && (!whole_sec
|| this->output_section_
!= o
))
1994 this->output_section_
= o
;
1997 if (this->state_
== NO_GROUP
)
1999 this->state_
= FINDING_STUB_SECTION
;
2000 this->group_end_addr_
= end_addr
;
2002 else if (this->group_end_addr_
- start_addr
< group_size
)
2004 // Adding this section would make the group larger than GROUP_SIZE.
2005 else if (this->state_
== FINDING_STUB_SECTION
2006 && !this->stubs_always_before_branch_
2009 // But wait, there's more! Input sections up to GROUP_SIZE
2010 // bytes before the stub table can be handled by it too.
2011 this->state_
= HAS_STUB_SECTION
;
2012 this->group_end_addr_
= end_addr
;
2016 this->state_
= NO_GROUP
;
2022 // Look over all the input sections, deciding where to place stubs.
2024 template<int size
, bool big_endian
>
2026 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
2029 Stub_control
stub_control(parameters
->options().stub_group_size());
2031 // Group input sections and insert stub table
2032 Stub_table
<size
, big_endian
>* stub_table
= NULL
;
2033 Layout::Section_list section_list
;
2034 layout
->get_executable_sections(§ion_list
);
2035 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
2036 for (Layout::Section_list::reverse_iterator o
= section_list
.rbegin();
2037 o
!= section_list
.rend();
2040 typedef Output_section::Input_section_list Input_section_list
;
2041 for (Input_section_list::const_reverse_iterator i
2042 = (*o
)->input_sections().rbegin();
2043 i
!= (*o
)->input_sections().rend();
2046 if (i
->is_input_section())
2048 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2049 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2050 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
2051 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
2053 stub_table
->init(stub_control
.owner(),
2054 stub_control
.output_section());
2057 if (stub_table
== NULL
)
2058 stub_table
= this->new_stub_table();
2059 ppcobj
->set_stub_table(i
->shndx(), stub_table
);
2063 if (stub_table
!= NULL
)
2064 stub_table
->init(stub_control
.owner(), stub_control
.output_section());
2067 // If this branch needs a plt call stub, or a long branch stub, make one.
2069 template<int size
, bool big_endian
>
2071 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
2072 Stub_table
<size
, big_endian
>* stub_table
,
2073 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
2074 Symbol_table
* symtab
) const
2076 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
2077 if (sym
!= NULL
&& sym
->is_forwarder())
2078 sym
= symtab
->resolve_forwards(sym
);
2079 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
2081 ? use_plt_offset
<size
>(gsym
, Scan::get_reference_flags(this->r_type_
))
2082 : this->object_
->local_has_plt_offset(this->r_sym_
))
2084 if (stub_table
== NULL
)
2085 stub_table
= this->object_
->stub_table(this->shndx_
);
2086 if (stub_table
== NULL
)
2088 // This is a ref from a data section to an ifunc symbol.
2089 stub_table
= ifunc_stub_table
;
2091 gold_assert(stub_table
!= NULL
);
2093 stub_table
->add_plt_call_entry(this->object_
, gsym
,
2094 this->r_type_
, this->addend_
);
2096 stub_table
->add_plt_call_entry(this->object_
, this->r_sym_
,
2097 this->r_type_
, this->addend_
);
2101 unsigned int max_branch_offset
;
2102 if (this->r_type_
== elfcpp::R_POWERPC_REL14
2103 || this->r_type_
== elfcpp::R_POWERPC_REL14_BRTAKEN
2104 || this->r_type_
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
2105 max_branch_offset
= 1 << 15;
2106 else if (this->r_type_
== elfcpp::R_POWERPC_REL24
2107 || this->r_type_
== elfcpp::R_PPC_PLTREL24
2108 || this->r_type_
== elfcpp::R_PPC_LOCAL24PC
)
2109 max_branch_offset
= 1 << 25;
2112 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
2113 gold_assert(from
!= invalid_address
);
2114 from
+= (this->object_
->output_section(this->shndx_
)->address()
2119 switch (gsym
->source())
2121 case Symbol::FROM_OBJECT
:
2123 Object
* symobj
= gsym
->object();
2124 if (symobj
->is_dynamic()
2125 || symobj
->pluginobj() != NULL
)
2128 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
2129 if (shndx
== elfcpp::SHN_UNDEF
)
2134 case Symbol::IS_UNDEFINED
:
2140 Symbol_table::Compute_final_value_status status
;
2141 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
2142 if (status
!= Symbol_table::CFVS_OK
)
2147 const Symbol_value
<size
>* psymval
2148 = this->object_
->local_symbol(this->r_sym_
);
2149 Symbol_value
<size
> symval
;
2150 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
2151 typename
ObjType::Compute_final_local_value_status status
2152 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
2154 if (status
!= ObjType::CFLV_OK
2155 || !symval
.has_output_value())
2157 to
= symval
.value(this->object_
, 0);
2159 to
+= this->addend_
;
2160 if (stub_table
== NULL
)
2161 stub_table
= this->object_
->stub_table(this->shndx_
);
2162 gold_assert(stub_table
!= NULL
);
2163 if (size
== 64 && is_branch_reloc(this->r_type_
))
2165 unsigned int dest_shndx
;
2166 to
= stub_table
->targ()->symval_for_branch(to
, gsym
, this->object_
,
2169 Address delta
= to
- from
;
2170 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
2172 stub_table
->add_long_branch_entry(this->object_
, to
);
2177 // Relaxation hook. This is where we do stub generation.
2179 template<int size
, bool big_endian
>
2181 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
2182 const Input_objects
*,
2183 Symbol_table
* symtab
,
2187 unsigned int prev_brlt_size
= 0;
2190 bool thread_safe
= parameters
->options().plt_thread_safe();
2191 if (size
== 64 && !parameters
->options().user_set_plt_thread_safe())
2193 static const char* const thread_starter
[] =
2197 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2199 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2200 "mq_notify", "create_timer",
2204 "GOMP_parallel_start",
2205 "GOMP_parallel_loop_static_start",
2206 "GOMP_parallel_loop_dynamic_start",
2207 "GOMP_parallel_loop_guided_start",
2208 "GOMP_parallel_loop_runtime_start",
2209 "GOMP_parallel_sections_start",
2212 if (parameters
->options().shared())
2216 for (unsigned int i
= 0;
2217 i
< sizeof(thread_starter
) / sizeof(thread_starter
[0]);
2220 Symbol
* sym
= symtab
->lookup(thread_starter
[i
], NULL
);
2221 thread_safe
= (sym
!= NULL
2223 && sym
->in_real_elf());
2229 this->plt_thread_safe_
= thread_safe
;
2230 this->group_sections(layout
, task
);
2233 // We need address of stub tables valid for make_stub.
2234 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2235 p
!= this->stub_tables_
.end();
2238 const Powerpc_relobj
<size
, big_endian
>* object
2239 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
2240 Address off
= object
->get_output_section_offset((*p
)->shndx());
2241 gold_assert(off
!= invalid_address
);
2242 Output_section
* os
= (*p
)->output_section();
2243 (*p
)->set_address_and_size(os
, off
);
2248 // Clear plt call stubs, long branch stubs and branch lookup table.
2249 prev_brlt_size
= this->branch_lookup_table_
.size();
2250 this->branch_lookup_table_
.clear();
2251 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2252 p
!= this->stub_tables_
.end();
2255 (*p
)->clear_stubs();
2259 // Build all the stubs.
2260 Stub_table
<size
, big_endian
>* ifunc_stub_table
2261 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
2262 Stub_table
<size
, big_endian
>* one_stub_table
2263 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
2264 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
2265 b
!= this->branch_info_
.end();
2268 b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
);
2271 // Did anything change size?
2272 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
2273 bool again
= num_huge_branches
!= prev_brlt_size
;
2274 if (size
== 64 && num_huge_branches
!= 0)
2275 this->make_brlt_section(layout
);
2276 if (size
== 64 && again
)
2277 this->brlt_section_
->set_current_size(num_huge_branches
);
2279 typedef Unordered_set
<Output_section
*> Output_sections
;
2280 Output_sections os_need_update
;
2281 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2282 p
!= this->stub_tables_
.end();
2285 if ((*p
)->size_update())
2288 (*p
)->add_eh_frame(layout
);
2289 os_need_update
.insert((*p
)->output_section());
2293 // Set output section offsets for all input sections in an output
2294 // section that just changed size. Anything past the stubs will
2296 for (typename
Output_sections::iterator p
= os_need_update
.begin();
2297 p
!= os_need_update
.end();
2300 Output_section
* os
= *p
;
2302 typedef Output_section::Input_section_list Input_section_list
;
2303 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
2304 i
!= os
->input_sections().end();
2307 off
= align_address(off
, i
->addralign());
2308 if (i
->is_input_section() || i
->is_relaxed_input_section())
2309 i
->relobj()->set_section_offset(i
->shndx(), off
);
2310 if (i
->is_relaxed_input_section())
2312 Stub_table
<size
, big_endian
>* stub_table
2313 = static_cast<Stub_table
<size
, big_endian
>*>(
2314 i
->relaxed_input_section());
2315 off
+= stub_table
->set_address_and_size(os
, off
);
2318 off
+= i
->data_size();
2320 // If .brlt is part of this output section, then we have just
2321 // done the offset adjustment.
2322 os
->clear_section_offsets_need_adjustment();
2327 && num_huge_branches
!= 0
2328 && parameters
->options().output_is_position_independent())
2330 // Fill in the BRLT relocs.
2331 this->brlt_section_
->reset_data_size();
2332 for (typename
Branch_lookup_table::const_iterator p
2333 = this->branch_lookup_table_
.begin();
2334 p
!= this->branch_lookup_table_
.end();
2337 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
2339 this->brlt_section_
->finalize_data_size();
2344 template<int size
, bool big_endian
>
2346 Target_powerpc
<size
, big_endian
>::do_plt_fde_location(const Output_data
* plt
,
2347 unsigned char* oview
,
2351 uint64_t address
= plt
->address();
2352 off_t len
= plt
->data_size();
2354 if (plt
== this->glink_
)
2356 // See Output_data_glink::do_write() for glink contents.
2359 // There is one word before __glink_PLTresolve
2363 else if (parameters
->options().output_is_position_independent())
2365 // There are two FDEs for a position independent glink.
2366 // The first covers the branch table, the second
2367 // __glink_PLTresolve at the end of glink.
2368 off_t resolve_size
= this->glink_
->pltresolve_size
;
2370 len
-= resolve_size
;
2373 address
+= len
- resolve_size
;
2380 // Must be a stub table.
2381 const Stub_table
<size
, big_endian
>* stub_table
2382 = static_cast<const Stub_table
<size
, big_endian
>*>(plt
);
2383 uint64_t stub_address
= stub_table
->stub_address();
2384 len
-= stub_address
- address
;
2385 address
= stub_address
;
2388 *paddress
= address
;
2392 // A class to handle the PLT data.
2394 template<int size
, bool big_endian
>
2395 class Output_data_plt_powerpc
: public Output_section_data_build
2398 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
2399 size
, big_endian
> Reloc_section
;
2401 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
2402 Reloc_section
* plt_rel
,
2403 unsigned int reserved_size
,
2405 : Output_section_data_build(size
== 32 ? 4 : 8),
2408 initial_plt_entry_size_(reserved_size
),
2412 // Add an entry to the PLT.
2417 add_ifunc_entry(Symbol
*);
2420 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
2422 // Return the .rela.plt section data.
2429 // Return the number of PLT entries.
2433 return ((this->current_data_size() - this->initial_plt_entry_size_
)
2437 // Return the offset of the first non-reserved PLT entry.
2439 first_plt_entry_offset()
2440 { return this->initial_plt_entry_size_
; }
2442 // Return the size of a PLT entry.
2444 get_plt_entry_size()
2445 { return plt_entry_size
; }
2449 do_adjust_output_section(Output_section
* os
)
2454 // Write to a map file.
2456 do_print_to_mapfile(Mapfile
* mapfile
) const
2457 { mapfile
->print_output_data(this, this->name_
); }
2460 // The size of an entry in the PLT.
2461 static const int plt_entry_size
= size
== 32 ? 4 : 24;
2463 // Write out the PLT data.
2465 do_write(Output_file
*);
2467 // The reloc section.
2468 Reloc_section
* rel_
;
2469 // Allows access to .glink for do_write.
2470 Target_powerpc
<size
, big_endian
>* targ_
;
2471 // The size of the first reserved entry.
2472 int initial_plt_entry_size_
;
2473 // What to report in map file.
2477 // Add an entry to the PLT.
2479 template<int size
, bool big_endian
>
2481 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
2483 if (!gsym
->has_plt_offset())
2485 section_size_type off
= this->current_data_size();
2487 off
+= this->first_plt_entry_offset();
2488 gsym
->set_plt_offset(off
);
2489 gsym
->set_needs_dynsym_entry();
2490 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
2491 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
2492 off
+= plt_entry_size
;
2493 this->set_current_data_size(off
);
2497 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
2499 template<int size
, bool big_endian
>
2501 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
2503 if (!gsym
->has_plt_offset())
2505 section_size_type off
= this->current_data_size();
2506 gsym
->set_plt_offset(off
);
2507 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
2509 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
2510 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
2511 off
+= plt_entry_size
;
2512 this->set_current_data_size(off
);
2516 // Add an entry for a local ifunc symbol to the IPLT.
2518 template<int size
, bool big_endian
>
2520 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
2521 Sized_relobj_file
<size
, big_endian
>* relobj
,
2522 unsigned int local_sym_index
)
2524 if (!relobj
->local_has_plt_offset(local_sym_index
))
2526 section_size_type off
= this->current_data_size();
2527 relobj
->set_local_plt_offset(local_sym_index
, off
);
2528 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
2530 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
2531 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
2533 off
+= plt_entry_size
;
2534 this->set_current_data_size(off
);
2538 static const uint32_t add_0_11_11
= 0x7c0b5a14;
2539 static const uint32_t add_2_2_11
= 0x7c425a14;
2540 static const uint32_t add_3_3_2
= 0x7c631214;
2541 static const uint32_t add_3_3_13
= 0x7c636a14;
2542 static const uint32_t add_11_0_11
= 0x7d605a14;
2543 static const uint32_t add_12_2_11
= 0x7d825a14;
2544 static const uint32_t add_12_12_11
= 0x7d8c5a14;
2545 static const uint32_t addi_11_11
= 0x396b0000;
2546 static const uint32_t addi_12_12
= 0x398c0000;
2547 static const uint32_t addi_2_2
= 0x38420000;
2548 static const uint32_t addi_3_2
= 0x38620000;
2549 static const uint32_t addi_3_3
= 0x38630000;
2550 static const uint32_t addis_0_2
= 0x3c020000;
2551 static const uint32_t addis_0_13
= 0x3c0d0000;
2552 static const uint32_t addis_11_11
= 0x3d6b0000;
2553 static const uint32_t addis_11_30
= 0x3d7e0000;
2554 static const uint32_t addis_12_12
= 0x3d8c0000;
2555 static const uint32_t addis_12_2
= 0x3d820000;
2556 static const uint32_t addis_3_2
= 0x3c620000;
2557 static const uint32_t addis_3_13
= 0x3c6d0000;
2558 static const uint32_t b
= 0x48000000;
2559 static const uint32_t bcl_20_31
= 0x429f0005;
2560 static const uint32_t bctr
= 0x4e800420;
2561 static const uint32_t blr
= 0x4e800020;
2562 static const uint32_t blrl
= 0x4e800021;
2563 static const uint32_t bnectr_p4
= 0x4ce20420;
2564 static const uint32_t cmpldi_2_0
= 0x28220000;
2565 static const uint32_t cror_15_15_15
= 0x4def7b82;
2566 static const uint32_t cror_31_31_31
= 0x4ffffb82;
2567 static const uint32_t ld_0_1
= 0xe8010000;
2568 static const uint32_t ld_0_12
= 0xe80c0000;
2569 static const uint32_t ld_11_12
= 0xe96c0000;
2570 static const uint32_t ld_11_2
= 0xe9620000;
2571 static const uint32_t ld_2_1
= 0xe8410000;
2572 static const uint32_t ld_2_11
= 0xe84b0000;
2573 static const uint32_t ld_2_12
= 0xe84c0000;
2574 static const uint32_t ld_2_2
= 0xe8420000;
2575 static const uint32_t lfd_0_1
= 0xc8010000;
2576 static const uint32_t li_0_0
= 0x38000000;
2577 static const uint32_t li_12_0
= 0x39800000;
2578 static const uint32_t lis_0_0
= 0x3c000000;
2579 static const uint32_t lis_11
= 0x3d600000;
2580 static const uint32_t lis_12
= 0x3d800000;
2581 static const uint32_t lwz_0_12
= 0x800c0000;
2582 static const uint32_t lwz_11_11
= 0x816b0000;
2583 static const uint32_t lwz_11_30
= 0x817e0000;
2584 static const uint32_t lwz_12_12
= 0x818c0000;
2585 static const uint32_t lwzu_0_12
= 0x840c0000;
2586 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
2587 static const uint32_t mflr_0
= 0x7c0802a6;
2588 static const uint32_t mflr_11
= 0x7d6802a6;
2589 static const uint32_t mflr_12
= 0x7d8802a6;
2590 static const uint32_t mtctr_0
= 0x7c0903a6;
2591 static const uint32_t mtctr_11
= 0x7d6903a6;
2592 static const uint32_t mtctr_12
= 0x7d8903a6;
2593 static const uint32_t mtlr_0
= 0x7c0803a6;
2594 static const uint32_t mtlr_12
= 0x7d8803a6;
2595 static const uint32_t nop
= 0x60000000;
2596 static const uint32_t ori_0_0_0
= 0x60000000;
2597 static const uint32_t std_0_1
= 0xf8010000;
2598 static const uint32_t std_0_12
= 0xf80c0000;
2599 static const uint32_t std_2_1
= 0xf8410000;
2600 static const uint32_t stfd_0_1
= 0xd8010000;
2601 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
2602 static const uint32_t sub_11_11_12
= 0x7d6c5850;
2603 static const uint32_t xor_11_11_11
= 0x7d6b5a78;
2605 // Write out the PLT.
2607 template<int size
, bool big_endian
>
2609 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
2613 const section_size_type offset
= this->offset();
2614 const section_size_type oview_size
2615 = convert_to_section_size_type(this->data_size());
2616 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2617 unsigned char* pov
= oview
;
2618 unsigned char* endpov
= oview
+ oview_size
;
2620 // The address of the .glink branch table
2621 const Output_data_glink
<size
, big_endian
>* glink
2622 = this->targ_
->glink_section();
2623 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
2625 while (pov
< endpov
)
2627 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
2632 of
->write_output_view(offset
, oview_size
, oview
);
2636 // Create the PLT section.
2638 template<int size
, bool big_endian
>
2640 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
2643 if (this->plt_
== NULL
)
2645 if (this->got_
== NULL
)
2646 this->got_section(symtab
, layout
);
2648 if (this->glink_
== NULL
)
2649 make_glink_section(layout
);
2651 // Ensure that .rela.dyn always appears before .rela.plt This is
2652 // necessary due to how, on PowerPC and some other targets, .rela.dyn
2653 // needs to include .rela.plt in it's range.
2654 this->rela_dyn_section(layout
);
2656 Reloc_section
* plt_rel
= new Reloc_section(false);
2657 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
2658 elfcpp::SHF_ALLOC
, plt_rel
,
2659 ORDER_DYNAMIC_PLT_RELOCS
, false);
2661 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
2662 size
== 32 ? 0 : 24,
2664 layout
->add_output_section_data(".plt",
2666 ? elfcpp::SHT_PROGBITS
2667 : elfcpp::SHT_NOBITS
),
2668 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2677 // Create the IPLT section.
2679 template<int size
, bool big_endian
>
2681 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
2684 if (this->iplt_
== NULL
)
2686 this->make_plt_section(symtab
, layout
);
2688 Reloc_section
* iplt_rel
= new Reloc_section(false);
2689 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
2691 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
2693 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
2697 // A section for huge long branch addresses, similar to plt section.
2699 template<int size
, bool big_endian
>
2700 class Output_data_brlt_powerpc
: public Output_section_data_build
2703 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2704 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
2705 size
, big_endian
> Reloc_section
;
2707 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
2708 Reloc_section
* brlt_rel
)
2709 : Output_section_data_build(size
== 32 ? 4 : 8),
2714 // Add a reloc for an entry in the BRLT.
2716 add_reloc(Address to
, unsigned int off
)
2717 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
2719 // Update section and reloc section size.
2721 set_current_size(unsigned int num_branches
)
2723 this->reset_address_and_file_offset();
2724 this->set_current_data_size(num_branches
* 16);
2725 this->finalize_data_size();
2726 Output_section
* os
= this->output_section();
2727 os
->set_section_offsets_need_adjustment();
2728 if (this->rel_
!= NULL
)
2730 unsigned int reloc_size
2731 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
2732 this->rel_
->reset_address_and_file_offset();
2733 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
2734 this->rel_
->finalize_data_size();
2735 Output_section
* os
= this->rel_
->output_section();
2736 os
->set_section_offsets_need_adjustment();
2742 do_adjust_output_section(Output_section
* os
)
2747 // Write to a map file.
2749 do_print_to_mapfile(Mapfile
* mapfile
) const
2750 { mapfile
->print_output_data(this, "** BRLT"); }
2753 // Write out the BRLT data.
2755 do_write(Output_file
*);
2757 // The reloc section.
2758 Reloc_section
* rel_
;
2759 Target_powerpc
<size
, big_endian
>* targ_
;
2762 // Make the branch lookup table section.
2764 template<int size
, bool big_endian
>
2766 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
2768 if (size
== 64 && this->brlt_section_
== NULL
)
2770 Reloc_section
* brlt_rel
= NULL
;
2771 bool is_pic
= parameters
->options().output_is_position_independent();
2774 // When PIC we can't fill in .brlt (like .plt it can be a
2775 // bss style section) but must initialise at runtime via
2776 // dynamic relocats.
2777 this->rela_dyn_section(layout
);
2778 brlt_rel
= new Reloc_section(false);
2779 this->rela_dyn_
->output_section()->add_output_section_data(brlt_rel
);
2782 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
2783 if (this->plt_
&& is_pic
)
2784 this->plt_
->output_section()
2785 ->add_output_section_data(this->brlt_section_
);
2787 layout
->add_output_section_data(".brlt",
2788 (is_pic
? elfcpp::SHT_NOBITS
2789 : elfcpp::SHT_PROGBITS
),
2790 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2791 this->brlt_section_
,
2792 (is_pic
? ORDER_SMALL_BSS
2793 : ORDER_SMALL_DATA
),
2798 // Write out .brlt when non-PIC.
2800 template<int size
, bool big_endian
>
2802 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
2804 if (size
== 64 && !parameters
->options().output_is_position_independent())
2806 const section_size_type offset
= this->offset();
2807 const section_size_type oview_size
2808 = convert_to_section_size_type(this->data_size());
2809 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2811 this->targ_
->write_branch_lookup_table(oview
);
2812 of
->write_output_view(offset
, oview_size
, oview
);
2816 static inline uint32_t
2822 static inline uint32_t
2828 static inline uint32_t
2831 return hi(a
+ 0x8000);
2837 static const unsigned char eh_frame_cie
[12];
2841 const unsigned char Eh_cie
<size
>::eh_frame_cie
[] =
2844 'z', 'R', 0, // Augmentation string.
2845 4, // Code alignment.
2846 0x80 - size
/ 8 , // Data alignment.
2848 1, // Augmentation size.
2849 (elfcpp::DW_EH_PE_pcrel
2850 | elfcpp::DW_EH_PE_sdata4
), // FDE encoding.
2851 elfcpp::DW_CFA_def_cfa
, 1, 0 // def_cfa: r1 offset 0.
2854 // Describe __glink_PLTresolve use of LR, 64-bit version.
2855 static const unsigned char glink_eh_frame_fde_64
[] =
2857 0, 0, 0, 0, // Replaced with offset to .glink.
2858 0, 0, 0, 0, // Replaced with size of .glink.
2859 0, // Augmentation size.
2860 elfcpp::DW_CFA_advance_loc
+ 1,
2861 elfcpp::DW_CFA_register
, 65, 12,
2862 elfcpp::DW_CFA_advance_loc
+ 4,
2863 elfcpp::DW_CFA_restore_extended
, 65
2866 // Describe __glink_PLTresolve use of LR, 32-bit version.
2867 static const unsigned char glink_eh_frame_fde_32
[] =
2869 0, 0, 0, 0, // Replaced with offset to .glink.
2870 0, 0, 0, 0, // Replaced with size of .glink.
2871 0, // Augmentation size.
2872 elfcpp::DW_CFA_advance_loc
+ 2,
2873 elfcpp::DW_CFA_register
, 65, 0,
2874 elfcpp::DW_CFA_advance_loc
+ 4,
2875 elfcpp::DW_CFA_restore_extended
, 65
2878 static const unsigned char default_fde
[] =
2880 0, 0, 0, 0, // Replaced with offset to stubs.
2881 0, 0, 0, 0, // Replaced with size of stubs.
2882 0, // Augmentation size.
2883 elfcpp::DW_CFA_nop
, // Pad.
2888 template<bool big_endian
>
2890 write_insn(unsigned char* p
, uint32_t v
)
2892 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
2895 // Stub_table holds information about plt and long branch stubs.
2896 // Stubs are built in an area following some input section determined
2897 // by group_sections(). This input section is converted to a relaxed
2898 // input section allowing it to be resized to accommodate the stubs
2900 template<int size
, bool big_endian
>
2901 class Stub_table
: public Output_relaxed_input_section
2904 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2905 static const Address invalid_address
= static_cast<Address
>(0) - 1;
2907 Stub_table(Target_powerpc
<size
, big_endian
>* targ
)
2908 : Output_relaxed_input_section(NULL
, 0, 0),
2909 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
2910 orig_data_size_(0), plt_size_(0), last_plt_size_(0),
2911 branch_size_(0), last_branch_size_(0), eh_frame_added_(false)
2914 // Delayed Output_relaxed_input_section init.
2916 init(const Output_section::Input_section
*, Output_section
*);
2918 // Add a plt call stub.
2920 add_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2926 add_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2931 // Find a given plt call stub.
2933 find_plt_call_entry(const Symbol
*) const;
2936 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2937 unsigned int) const;
2940 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2946 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
2951 // Add a long branch stub.
2953 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*, Address
);
2956 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
2962 this->plt_call_stubs_
.clear();
2963 this->plt_size_
= 0;
2964 this->long_branch_stubs_
.clear();
2965 this->branch_size_
= 0;
2969 set_address_and_size(const Output_section
* os
, Address off
)
2971 Address start_off
= off
;
2972 off
+= this->orig_data_size_
;
2973 Address my_size
= this->plt_size_
+ this->branch_size_
;
2975 off
= align_address(off
, this->stub_align());
2976 // Include original section size and alignment padding in size
2977 my_size
+= off
- start_off
;
2978 this->reset_address_and_file_offset();
2979 this->set_current_data_size(my_size
);
2980 this->set_address_and_file_offset(os
->address() + start_off
,
2981 os
->offset() + start_off
);
2986 stub_address() const
2988 return align_address(this->address() + this->orig_data_size_
,
2989 this->stub_align());
2995 return align_address(this->offset() + this->orig_data_size_
,
2996 this->stub_align());
3001 { return this->plt_size_
; }
3006 Output_section
* os
= this->output_section();
3007 if (os
->addralign() < this->stub_align())
3009 os
->set_addralign(this->stub_align());
3010 // FIXME: get rid of the insane checkpointing.
3011 // We can't increase alignment of the input section to which
3012 // stubs are attached; The input section may be .init which
3013 // is pasted together with other .init sections to form a
3014 // function. Aligning might insert zero padding resulting in
3015 // sigill. However we do need to increase alignment of the
3016 // output section so that the align_address() on offset in
3017 // set_address_and_size() adds the same padding as the
3018 // align_address() on address in stub_address().
3019 // What's more, we need this alignment for the layout done in
3020 // relaxation_loop_body() so that the output section starts at
3021 // a suitably aligned address.
3022 os
->checkpoint_set_addralign(this->stub_align());
3024 if (this->last_plt_size_
!= this->plt_size_
3025 || this->last_branch_size_
!= this->branch_size_
)
3027 this->last_plt_size_
= this->plt_size_
;
3028 this->last_branch_size_
= this->branch_size_
;
3034 // Add .eh_frame info for this stub section. Unlike other linker
3035 // generated .eh_frame this is added late in the link, because we
3036 // only want the .eh_frame info if this particular stub section is
3039 add_eh_frame(Layout
* layout
)
3041 if (!this->eh_frame_added_
)
3043 if (!parameters
->options().ld_generated_unwind_info())
3046 // Since we add stub .eh_frame info late, it must be placed
3047 // after all other linker generated .eh_frame info so that
3048 // merge mapping need not be updated for input sections.
3049 // There is no provision to use a different CIE to that used
3051 if (!this->targ_
->has_glink())
3054 layout
->add_eh_frame_for_plt(this,
3055 Eh_cie
<size
>::eh_frame_cie
,
3056 sizeof (Eh_cie
<size
>::eh_frame_cie
),
3058 sizeof (default_fde
));
3059 this->eh_frame_added_
= true;
3063 Target_powerpc
<size
, big_endian
>*
3069 class Plt_stub_ent_hash
;
3070 typedef Unordered_map
<Plt_stub_ent
, unsigned int,
3071 Plt_stub_ent_hash
> Plt_stub_entries
;
3073 // Alignment of stub section.
3079 unsigned int min_align
= 32;
3080 unsigned int user_align
= 1 << parameters
->options().plt_align();
3081 return std::max(user_align
, min_align
);
3084 // Return the plt offset for the given call stub.
3086 plt_off(typename
Plt_stub_entries::const_iterator p
, bool* is_iplt
) const
3088 const Symbol
* gsym
= p
->first
.sym_
;
3091 *is_iplt
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
3092 && gsym
->can_use_relative_reloc(false));
3093 return gsym
->plt_offset();
3098 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
3099 unsigned int local_sym_index
= p
->first
.locsym_
;
3100 return relobj
->local_plt_offset(local_sym_index
);
3104 // Size of a given plt call stub.
3106 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
3112 Address plt_addr
= this->plt_off(p
, &is_iplt
);
3114 plt_addr
+= this->targ_
->iplt_section()->address();
3116 plt_addr
+= this->targ_
->plt_section()->address();
3117 Address got_addr
= this->targ_
->got_section()->output_section()->address();
3118 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3119 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
3120 got_addr
+= ppcobj
->toc_base_offset();
3121 Address off
= plt_addr
- got_addr
;
3122 bool static_chain
= parameters
->options().plt_static_chain();
3123 bool thread_safe
= this->targ_
->plt_thread_safe();
3124 unsigned int bytes
= (4 * 5
3127 + 4 * (ha(off
) != 0)
3128 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
3129 unsigned int align
= 1 << parameters
->options().plt_align();
3131 bytes
= (bytes
+ align
- 1) & -align
;
3135 // Return long branch stub size.
3137 branch_stub_size(Address to
)
3140 = this->stub_address() + this->last_plt_size_
+ this->branch_size_
;
3141 if (to
- loc
+ (1 << 25) < 2 << 25)
3143 if (size
== 64 || !parameters
->options().output_is_position_independent())
3150 do_write(Output_file
*);
3152 // Plt call stub keys.
3156 Plt_stub_ent(const Symbol
* sym
)
3157 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3160 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3161 unsigned int locsym_index
)
3162 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3165 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3167 unsigned int r_type
,
3169 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3172 this->addend_
= addend
;
3173 else if (parameters
->options().output_is_position_independent()
3174 && r_type
== elfcpp::R_PPC_PLTREL24
)
3176 this->addend_
= addend
;
3177 if (this->addend_
>= 32768)
3178 this->object_
= object
;
3182 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3183 unsigned int locsym_index
,
3184 unsigned int r_type
,
3186 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3189 this->addend_
= addend
;
3190 else if (parameters
->options().output_is_position_independent()
3191 && r_type
== elfcpp::R_PPC_PLTREL24
)
3192 this->addend_
= addend
;
3195 bool operator==(const Plt_stub_ent
& that
) const
3197 return (this->sym_
== that
.sym_
3198 && this->object_
== that
.object_
3199 && this->addend_
== that
.addend_
3200 && this->locsym_
== that
.locsym_
);
3204 const Sized_relobj_file
<size
, big_endian
>* object_
;
3205 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
3206 unsigned int locsym_
;
3209 class Plt_stub_ent_hash
3212 size_t operator()(const Plt_stub_ent
& ent
) const
3214 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
3215 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
3221 // Long branch stub keys.
3222 class Branch_stub_ent
3225 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
, Address to
)
3226 : dest_(to
), toc_base_off_(0)
3229 toc_base_off_
= obj
->toc_base_offset();
3232 bool operator==(const Branch_stub_ent
& that
) const
3234 return (this->dest_
== that
.dest_
3236 || this->toc_base_off_
== that
.toc_base_off_
));
3240 unsigned int toc_base_off_
;
3243 class Branch_stub_ent_hash
3246 size_t operator()(const Branch_stub_ent
& ent
) const
3247 { return ent
.dest_
^ ent
.toc_base_off_
; }
3250 // In a sane world this would be a global.
3251 Target_powerpc
<size
, big_endian
>* targ_
;
3252 // Map sym/object/addend to stub offset.
3253 Plt_stub_entries plt_call_stubs_
;
3254 // Map destination address to stub offset.
3255 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
3256 Branch_stub_ent_hash
> Branch_stub_entries
;
3257 Branch_stub_entries long_branch_stubs_
;
3258 // size of input section
3259 section_size_type orig_data_size_
;
3261 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
3262 // Whether .eh_frame info has been created for this stub section.
3263 bool eh_frame_added_
;
3266 // Make a new stub table, and record.
3268 template<int size
, bool big_endian
>
3269 Stub_table
<size
, big_endian
>*
3270 Target_powerpc
<size
, big_endian
>::new_stub_table()
3272 Stub_table
<size
, big_endian
>* stub_table
3273 = new Stub_table
<size
, big_endian
>(this);
3274 this->stub_tables_
.push_back(stub_table
);
3278 // Delayed stub table initialisation, because we create the stub table
3279 // before we know to which section it will be attached.
3281 template<int size
, bool big_endian
>
3283 Stub_table
<size
, big_endian
>::init(
3284 const Output_section::Input_section
* owner
,
3285 Output_section
* output_section
)
3287 this->set_relobj(owner
->relobj());
3288 this->set_shndx(owner
->shndx());
3289 this->set_addralign(this->relobj()->section_addralign(this->shndx()));
3290 this->set_output_section(output_section
);
3291 this->orig_data_size_
= owner
->current_data_size();
3293 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
3294 new_relaxed
.push_back(this);
3295 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
3298 // Add a plt call stub, if we do not already have one for this
3299 // sym/object/addend combo.
3301 template<int size
, bool big_endian
>
3303 Stub_table
<size
, big_endian
>::add_plt_call_entry(
3304 const Sized_relobj_file
<size
, big_endian
>* object
,
3306 unsigned int r_type
,
3309 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
3310 Address off
= this->plt_size_
;
3311 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
3312 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
3314 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
3317 template<int size
, bool big_endian
>
3319 Stub_table
<size
, big_endian
>::add_plt_call_entry(
3320 const Sized_relobj_file
<size
, big_endian
>* object
,
3321 unsigned int locsym_index
,
3322 unsigned int r_type
,
3325 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
3326 Address off
= this->plt_size_
;
3327 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
3328 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
3330 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
3333 // Find a plt call stub.
3335 template<int size
, bool big_endian
>
3336 typename Stub_table
<size
, big_endian
>::Address
3337 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3338 const Sized_relobj_file
<size
, big_endian
>* object
,
3340 unsigned int r_type
,
3341 Address addend
) const
3343 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
3344 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3345 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3348 template<int size
, bool big_endian
>
3349 typename Stub_table
<size
, big_endian
>::Address
3350 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
3352 Plt_stub_ent
ent(gsym
);
3353 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3354 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3357 template<int size
, bool big_endian
>
3358 typename Stub_table
<size
, big_endian
>::Address
3359 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3360 const Sized_relobj_file
<size
, big_endian
>* object
,
3361 unsigned int locsym_index
,
3362 unsigned int r_type
,
3363 Address addend
) const
3365 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
3366 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3367 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3370 template<int size
, bool big_endian
>
3371 typename Stub_table
<size
, big_endian
>::Address
3372 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3373 const Sized_relobj_file
<size
, big_endian
>* object
,
3374 unsigned int locsym_index
) const
3376 Plt_stub_ent
ent(object
, locsym_index
);
3377 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3378 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3381 // Add a long branch stub if we don't already have one to given
3384 template<int size
, bool big_endian
>
3386 Stub_table
<size
, big_endian
>::add_long_branch_entry(
3387 const Powerpc_relobj
<size
, big_endian
>* object
,
3390 Branch_stub_ent
ent(object
, to
);
3391 Address off
= this->branch_size_
;
3392 if (this->long_branch_stubs_
.insert(std::make_pair(ent
, off
)).second
)
3394 unsigned int stub_size
= this->branch_stub_size(to
);
3395 this->branch_size_
= off
+ stub_size
;
3396 if (size
== 64 && stub_size
!= 4)
3397 this->targ_
->add_branch_lookup_table(to
);
3401 // Find long branch stub.
3403 template<int size
, bool big_endian
>
3404 typename Stub_table
<size
, big_endian
>::Address
3405 Stub_table
<size
, big_endian
>::find_long_branch_entry(
3406 const Powerpc_relobj
<size
, big_endian
>* object
,
3409 Branch_stub_ent
ent(object
, to
);
3410 typename
Branch_stub_entries::const_iterator p
3411 = this->long_branch_stubs_
.find(ent
);
3412 return p
== this->long_branch_stubs_
.end() ? invalid_address
: p
->second
;
3415 // A class to handle .glink.
3417 template<int size
, bool big_endian
>
3418 class Output_data_glink
: public Output_section_data
3421 static const int pltresolve_size
= 16*4;
3423 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
3424 : Output_section_data(16), targ_(targ
)
3428 add_eh_frame(Layout
* layout
)
3430 if (!parameters
->options().ld_generated_unwind_info())
3434 layout
->add_eh_frame_for_plt(this,
3435 Eh_cie
<64>::eh_frame_cie
,
3436 sizeof (Eh_cie
<64>::eh_frame_cie
),
3437 glink_eh_frame_fde_64
,
3438 sizeof (glink_eh_frame_fde_64
));
3441 // 32-bit .glink can use the default since the CIE return
3442 // address reg, LR, is valid.
3443 layout
->add_eh_frame_for_plt(this,
3444 Eh_cie
<32>::eh_frame_cie
,
3445 sizeof (Eh_cie
<32>::eh_frame_cie
),
3447 sizeof (default_fde
));
3448 // Except where LR is used in a PIC __glink_PLTresolve.
3449 if (parameters
->options().output_is_position_independent())
3450 layout
->add_eh_frame_for_plt(this,
3451 Eh_cie
<32>::eh_frame_cie
,
3452 sizeof (Eh_cie
<32>::eh_frame_cie
),
3453 glink_eh_frame_fde_32
,
3454 sizeof (glink_eh_frame_fde_32
));
3459 // Write to a map file.
3461 do_print_to_mapfile(Mapfile
* mapfile
) const
3462 { mapfile
->print_output_data(this, _("** glink")); }
3466 set_final_data_size();
3470 do_write(Output_file
*);
3472 // Allows access to .got and .plt for do_write.
3473 Target_powerpc
<size
, big_endian
>* targ_
;
3476 template<int size
, bool big_endian
>
3478 Output_data_glink
<size
, big_endian
>::set_final_data_size()
3480 unsigned int count
= this->targ_
->plt_entry_count();
3481 section_size_type total
= 0;
3487 // space for branch table
3488 total
+= 4 * (count
- 1);
3490 total
+= -total
& 15;
3491 total
+= this->pltresolve_size
;
3495 total
+= this->pltresolve_size
;
3497 // space for branch table
3500 total
+= 4 * (count
- 0x8000);
3504 this->set_data_size(total
);
3507 // Write out plt and long branch stub code.
3509 template<int size
, bool big_endian
>
3511 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
3513 if (this->plt_call_stubs_
.empty()
3514 && this->long_branch_stubs_
.empty())
3517 const section_size_type start_off
= this->offset();
3518 const section_size_type off
= this->stub_offset();
3519 const section_size_type oview_size
=
3520 convert_to_section_size_type(this->data_size() - (off
- start_off
));
3521 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
3526 const Output_data_got_powerpc
<size
, big_endian
>* got
3527 = this->targ_
->got_section();
3528 Address got_os_addr
= got
->output_section()->address();
3530 if (!this->plt_call_stubs_
.empty())
3532 // The base address of the .plt section.
3533 Address plt_base
= this->targ_
->plt_section()->address();
3534 Address iplt_base
= invalid_address
;
3536 // Write out plt call stubs.
3537 typename
Plt_stub_entries::const_iterator cs
;
3538 for (cs
= this->plt_call_stubs_
.begin();
3539 cs
!= this->plt_call_stubs_
.end();
3543 Address pltoff
= this->plt_off(cs
, &is_iplt
);
3544 Address plt_addr
= pltoff
;
3547 if (iplt_base
== invalid_address
)
3548 iplt_base
= this->targ_
->iplt_section()->address();
3549 plt_addr
+= iplt_base
;
3552 plt_addr
+= plt_base
;
3553 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3554 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
3555 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
3556 Address off
= plt_addr
- got_addr
;
3558 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
3559 gold_error(_("%s: linkage table error against `%s'"),
3560 cs
->first
.object_
->name().c_str(),
3561 cs
->first
.sym_
->demangled_name().c_str());
3563 bool static_chain
= parameters
->options().plt_static_chain();
3564 bool thread_safe
= this->targ_
->plt_thread_safe();
3565 bool use_fake_dep
= false;
3566 Address cmp_branch_off
= 0;
3569 unsigned int pltindex
3570 = ((pltoff
- this->targ_
->first_plt_entry_offset())
3571 / this->targ_
->plt_entry_size());
3573 = (this->targ_
->glink_section()->pltresolve_size
3575 if (pltindex
> 32768)
3576 glinkoff
+= (pltindex
- 32768) * 4;
3578 = this->targ_
->glink_section()->address() + glinkoff
;
3580 = (this->stub_address() + cs
->second
+ 24
3581 + 4 * (ha(off
) != 0)
3582 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
3583 + 4 * static_chain
);
3584 cmp_branch_off
= to
- from
;
3585 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
3588 p
= oview
+ cs
->second
;
3591 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
3592 write_insn
<big_endian
>(p
, addis_12_2
+ ha(off
)), p
+= 4;
3593 write_insn
<big_endian
>(p
, ld_11_12
+ l(off
)), p
+= 4;
3594 if (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
3596 write_insn
<big_endian
>(p
, addi_12_12
+ l(off
)), p
+= 4;
3599 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3602 write_insn
<big_endian
>(p
, xor_11_11_11
), p
+= 4;
3603 write_insn
<big_endian
>(p
, add_12_12_11
), p
+= 4;
3605 write_insn
<big_endian
>(p
, ld_2_12
+ l(off
+ 8)), p
+= 4;
3607 write_insn
<big_endian
>(p
, ld_11_12
+ l(off
+ 16)), p
+= 4;
3611 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
3612 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
)), p
+= 4;
3613 if (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
3615 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
)), p
+= 4;
3618 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3621 write_insn
<big_endian
>(p
, xor_11_11_11
), p
+= 4;
3622 write_insn
<big_endian
>(p
, add_2_2_11
), p
+= 4;
3625 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16)), p
+= 4;
3626 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8)), p
+= 4;
3628 if (thread_safe
&& !use_fake_dep
)
3630 write_insn
<big_endian
>(p
, cmpldi_2_0
), p
+= 4;
3631 write_insn
<big_endian
>(p
, bnectr_p4
), p
+= 4;
3632 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
3635 write_insn
<big_endian
>(p
, bctr
);
3639 // Write out long branch stubs.
3640 typename
Branch_stub_entries::const_iterator bs
;
3641 for (bs
= this->long_branch_stubs_
.begin();
3642 bs
!= this->long_branch_stubs_
.end();
3645 p
= oview
+ this->plt_size_
+ bs
->second
;
3646 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
3647 Address delta
= bs
->first
.dest_
- loc
;
3648 if (delta
+ (1 << 25) < 2 << 25)
3649 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
3653 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
3654 gold_assert(brlt_addr
!= invalid_address
);
3655 brlt_addr
+= this->targ_
->brlt_section()->address();
3656 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
3657 Address brltoff
= brlt_addr
- got_addr
;
3658 if (ha(brltoff
) == 0)
3660 write_insn
<big_endian
>(p
, ld_11_2
+ l(brltoff
)), p
+= 4;
3664 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
3665 write_insn
<big_endian
>(p
, ld_11_12
+ l(brltoff
)), p
+= 4;
3667 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3668 write_insn
<big_endian
>(p
, bctr
);
3674 if (!this->plt_call_stubs_
.empty())
3676 // The base address of the .plt section.
3677 Address plt_base
= this->targ_
->plt_section()->address();
3678 Address iplt_base
= invalid_address
;
3679 // The address of _GLOBAL_OFFSET_TABLE_.
3680 Address g_o_t
= invalid_address
;
3682 // Write out plt call stubs.
3683 typename
Plt_stub_entries::const_iterator cs
;
3684 for (cs
= this->plt_call_stubs_
.begin();
3685 cs
!= this->plt_call_stubs_
.end();
3689 Address plt_addr
= this->plt_off(cs
, &is_iplt
);
3692 if (iplt_base
== invalid_address
)
3693 iplt_base
= this->targ_
->iplt_section()->address();
3694 plt_addr
+= iplt_base
;
3697 plt_addr
+= plt_base
;
3699 p
= oview
+ cs
->second
;
3700 if (parameters
->options().output_is_position_independent())
3703 const Powerpc_relobj
<size
, big_endian
>* ppcobj
3704 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
3705 (cs
->first
.object_
));
3706 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
3708 unsigned int got2
= ppcobj
->got2_shndx();
3709 got_addr
= ppcobj
->get_output_section_offset(got2
);
3710 gold_assert(got_addr
!= invalid_address
);
3711 got_addr
+= (ppcobj
->output_section(got2
)->address()
3712 + cs
->first
.addend_
);
3716 if (g_o_t
== invalid_address
)
3718 const Output_data_got_powerpc
<size
, big_endian
>* got
3719 = this->targ_
->got_section();
3720 g_o_t
= got
->address() + got
->g_o_t();
3725 Address off
= plt_addr
- got_addr
;
3728 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(off
));
3729 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
3730 write_insn
<big_endian
>(p
+ 8, bctr
);
3734 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(off
));
3735 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(off
));
3736 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
3737 write_insn
<big_endian
>(p
+ 12, bctr
);
3742 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
3743 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
3744 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
3745 write_insn
<big_endian
>(p
+ 12, bctr
);
3750 // Write out long branch stubs.
3751 typename
Branch_stub_entries::const_iterator bs
;
3752 for (bs
= this->long_branch_stubs_
.begin();
3753 bs
!= this->long_branch_stubs_
.end();
3756 p
= oview
+ this->plt_size_
+ bs
->second
;
3757 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
3758 Address delta
= bs
->first
.dest_
- loc
;
3759 if (delta
+ (1 << 25) < 2 << 25)
3760 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
3761 else if (!parameters
->options().output_is_position_independent())
3763 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(bs
->first
.dest_
));
3764 write_insn
<big_endian
>(p
+ 4, addi_12_12
+ l(bs
->first
.dest_
));
3765 write_insn
<big_endian
>(p
+ 8, mtctr_12
);
3766 write_insn
<big_endian
>(p
+ 12, bctr
);
3771 write_insn
<big_endian
>(p
+ 0, mflr_0
);
3772 write_insn
<big_endian
>(p
+ 4, bcl_20_31
);
3773 write_insn
<big_endian
>(p
+ 8, mflr_12
);
3774 write_insn
<big_endian
>(p
+ 12, addis_12_12
+ ha(delta
));
3775 write_insn
<big_endian
>(p
+ 16, addi_12_12
+ l(delta
));
3776 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
3777 write_insn
<big_endian
>(p
+ 24, mtctr_12
);
3778 write_insn
<big_endian
>(p
+ 28, bctr
);
3784 // Write out .glink.
3786 template<int size
, bool big_endian
>
3788 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
3790 const section_size_type off
= this->offset();
3791 const section_size_type oview_size
=
3792 convert_to_section_size_type(this->data_size());
3793 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
3796 // The base address of the .plt section.
3797 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3798 Address plt_base
= this->targ_
->plt_section()->address();
3802 // Write pltresolve stub.
3804 Address after_bcl
= this->address() + 16;
3805 Address pltoff
= plt_base
- after_bcl
;
3807 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
3809 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
3810 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
3811 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
3812 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
3813 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
3814 write_insn
<big_endian
>(p
, add_12_2_11
), p
+= 4;
3815 write_insn
<big_endian
>(p
, ld_11_12
+ 0), p
+= 4;
3816 write_insn
<big_endian
>(p
, ld_2_12
+ 8), p
+= 4;
3817 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3818 write_insn
<big_endian
>(p
, ld_11_12
+ 16), p
+= 4;
3819 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
3820 while (p
< oview
+ this->pltresolve_size
)
3821 write_insn
<big_endian
>(p
, nop
), p
+= 4;
3823 // Write lazy link call stubs.
3825 while (p
< oview
+ oview_size
)
3829 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
3833 write_insn
<big_endian
>(p
, lis_0_0
+ hi(indx
)), p
+= 4;
3834 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
3836 uint32_t branch_off
= 8 - (p
- oview
);
3837 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
3843 const Output_data_got_powerpc
<size
, big_endian
>* got
3844 = this->targ_
->got_section();
3845 // The address of _GLOBAL_OFFSET_TABLE_.
3846 Address g_o_t
= got
->address() + got
->g_o_t();
3848 // Write out pltresolve branch table.
3850 unsigned int the_end
= oview_size
- this->pltresolve_size
;
3851 unsigned char* end_p
= oview
+ the_end
;
3852 while (p
< end_p
- 8 * 4)
3853 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
3855 write_insn
<big_endian
>(p
, nop
), p
+= 4;
3857 // Write out pltresolve call stub.
3858 if (parameters
->options().output_is_position_independent())
3860 Address res0_off
= 0;
3861 Address after_bcl_off
= the_end
+ 12;
3862 Address bcl_res0
= after_bcl_off
- res0_off
;
3864 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
3865 write_insn
<big_endian
>(p
+ 4, mflr_0
);
3866 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
3867 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
3868 write_insn
<big_endian
>(p
+ 16, mflr_12
);
3869 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
3870 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
3872 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
3874 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
3875 if (ha(got_bcl
) == ha(got_bcl
+ 4))
3877 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
3878 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
3882 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
3883 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
3885 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
3886 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
3887 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
3888 write_insn
<big_endian
>(p
+ 52, bctr
);
3889 write_insn
<big_endian
>(p
+ 56, nop
);
3890 write_insn
<big_endian
>(p
+ 60, nop
);
3894 Address res0
= this->address();
3896 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
3897 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
3898 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
3899 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
3901 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
3902 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
3903 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
3904 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
3905 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
3906 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
3908 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
3909 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
3910 write_insn
<big_endian
>(p
+ 32, bctr
);
3911 write_insn
<big_endian
>(p
+ 36, nop
);
3912 write_insn
<big_endian
>(p
+ 40, nop
);
3913 write_insn
<big_endian
>(p
+ 44, nop
);
3914 write_insn
<big_endian
>(p
+ 48, nop
);
3915 write_insn
<big_endian
>(p
+ 52, nop
);
3916 write_insn
<big_endian
>(p
+ 56, nop
);
3917 write_insn
<big_endian
>(p
+ 60, nop
);
3922 of
->write_output_view(off
, oview_size
, oview
);
3926 // A class to handle linker generated save/restore functions.
3928 template<int size
, bool big_endian
>
3929 class Output_data_save_res
: public Output_section_data_build
3932 Output_data_save_res(Symbol_table
* symtab
);
3935 // Write to a map file.
3937 do_print_to_mapfile(Mapfile
* mapfile
) const
3938 { mapfile
->print_output_data(this, _("** save/restore")); }
3941 do_write(Output_file
*);
3944 // The maximum size of save/restore contents.
3945 static const unsigned int savres_max
= 218*4;
3948 savres_define(Symbol_table
* symtab
,
3950 unsigned int lo
, unsigned int hi
,
3951 unsigned char* write_ent(unsigned char*, int),
3952 unsigned char* write_tail(unsigned char*, int));
3954 unsigned char *contents_
;
3957 template<bool big_endian
>
3958 static unsigned char*
3959 savegpr0(unsigned char* p
, int r
)
3961 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3962 write_insn
<big_endian
>(p
, insn
);
3966 template<bool big_endian
>
3967 static unsigned char*
3968 savegpr0_tail(unsigned char* p
, int r
)
3970 p
= savegpr0
<big_endian
>(p
, r
);
3971 uint32_t insn
= std_0_1
+ 16;
3972 write_insn
<big_endian
>(p
, insn
);
3974 write_insn
<big_endian
>(p
, blr
);
3978 template<bool big_endian
>
3979 static unsigned char*
3980 restgpr0(unsigned char* p
, int r
)
3982 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
3983 write_insn
<big_endian
>(p
, insn
);
3987 template<bool big_endian
>
3988 static unsigned char*
3989 restgpr0_tail(unsigned char* p
, int r
)
3991 uint32_t insn
= ld_0_1
+ 16;
3992 write_insn
<big_endian
>(p
, insn
);
3994 p
= restgpr0
<big_endian
>(p
, r
);
3995 write_insn
<big_endian
>(p
, mtlr_0
);
3999 p
= restgpr0
<big_endian
>(p
, 30);
4000 p
= restgpr0
<big_endian
>(p
, 31);
4002 write_insn
<big_endian
>(p
, blr
);
4006 template<bool big_endian
>
4007 static unsigned char*
4008 savegpr1(unsigned char* p
, int r
)
4010 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4011 write_insn
<big_endian
>(p
, insn
);
4015 template<bool big_endian
>
4016 static unsigned char*
4017 savegpr1_tail(unsigned char* p
, int r
)
4019 p
= savegpr1
<big_endian
>(p
, r
);
4020 write_insn
<big_endian
>(p
, blr
);
4024 template<bool big_endian
>
4025 static unsigned char*
4026 restgpr1(unsigned char* p
, int r
)
4028 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4029 write_insn
<big_endian
>(p
, insn
);
4033 template<bool big_endian
>
4034 static unsigned char*
4035 restgpr1_tail(unsigned char* p
, int r
)
4037 p
= restgpr1
<big_endian
>(p
, r
);
4038 write_insn
<big_endian
>(p
, blr
);
4042 template<bool big_endian
>
4043 static unsigned char*
4044 savefpr(unsigned char* p
, int r
)
4046 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4047 write_insn
<big_endian
>(p
, insn
);
4051 template<bool big_endian
>
4052 static unsigned char*
4053 savefpr0_tail(unsigned char* p
, int r
)
4055 p
= savefpr
<big_endian
>(p
, r
);
4056 write_insn
<big_endian
>(p
, std_0_1
+ 16);
4058 write_insn
<big_endian
>(p
, blr
);
4062 template<bool big_endian
>
4063 static unsigned char*
4064 restfpr(unsigned char* p
, int r
)
4066 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4067 write_insn
<big_endian
>(p
, insn
);
4071 template<bool big_endian
>
4072 static unsigned char*
4073 restfpr0_tail(unsigned char* p
, int r
)
4075 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
4077 p
= restfpr
<big_endian
>(p
, r
);
4078 write_insn
<big_endian
>(p
, mtlr_0
);
4082 p
= restfpr
<big_endian
>(p
, 30);
4083 p
= restfpr
<big_endian
>(p
, 31);
4085 write_insn
<big_endian
>(p
, blr
);
4089 template<bool big_endian
>
4090 static unsigned char*
4091 savefpr1_tail(unsigned char* p
, int r
)
4093 p
= savefpr
<big_endian
>(p
, r
);
4094 write_insn
<big_endian
>(p
, blr
);
4098 template<bool big_endian
>
4099 static unsigned char*
4100 restfpr1_tail(unsigned char* p
, int r
)
4102 p
= restfpr
<big_endian
>(p
, r
);
4103 write_insn
<big_endian
>(p
, blr
);
4107 template<bool big_endian
>
4108 static unsigned char*
4109 savevr(unsigned char* p
, int r
)
4111 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
4112 write_insn
<big_endian
>(p
, insn
);
4114 insn
= stvx_0_12_0
+ (r
<< 21);
4115 write_insn
<big_endian
>(p
, insn
);
4119 template<bool big_endian
>
4120 static unsigned char*
4121 savevr_tail(unsigned char* p
, int r
)
4123 p
= savevr
<big_endian
>(p
, r
);
4124 write_insn
<big_endian
>(p
, blr
);
4128 template<bool big_endian
>
4129 static unsigned char*
4130 restvr(unsigned char* p
, int r
)
4132 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
4133 write_insn
<big_endian
>(p
, insn
);
4135 insn
= lvx_0_12_0
+ (r
<< 21);
4136 write_insn
<big_endian
>(p
, insn
);
4140 template<bool big_endian
>
4141 static unsigned char*
4142 restvr_tail(unsigned char* p
, int r
)
4144 p
= restvr
<big_endian
>(p
, r
);
4145 write_insn
<big_endian
>(p
, blr
);
4150 template<int size
, bool big_endian
>
4151 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
4152 Symbol_table
* symtab
)
4153 : Output_section_data_build(4),
4156 this->savres_define(symtab
,
4157 "_savegpr0_", 14, 31,
4158 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
4159 this->savres_define(symtab
,
4160 "_restgpr0_", 14, 29,
4161 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
4162 this->savres_define(symtab
,
4163 "_restgpr0_", 30, 31,
4164 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
4165 this->savres_define(symtab
,
4166 "_savegpr1_", 14, 31,
4167 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
4168 this->savres_define(symtab
,
4169 "_restgpr1_", 14, 31,
4170 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
4171 this->savres_define(symtab
,
4172 "_savefpr_", 14, 31,
4173 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
4174 this->savres_define(symtab
,
4175 "_restfpr_", 14, 29,
4176 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
4177 this->savres_define(symtab
,
4178 "_restfpr_", 30, 31,
4179 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
4180 this->savres_define(symtab
,
4182 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
4183 this->savres_define(symtab
,
4185 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
4186 this->savres_define(symtab
,
4188 savevr
<big_endian
>, savevr_tail
<big_endian
>);
4189 this->savres_define(symtab
,
4191 restvr
<big_endian
>, restvr_tail
<big_endian
>);
4194 template<int size
, bool big_endian
>
4196 Output_data_save_res
<size
, big_endian
>::savres_define(
4197 Symbol_table
* symtab
,
4199 unsigned int lo
, unsigned int hi
,
4200 unsigned char* write_ent(unsigned char*, int),
4201 unsigned char* write_tail(unsigned char*, int))
4203 size_t len
= strlen(name
);
4204 bool writing
= false;
4207 memcpy(sym
, name
, len
);
4210 for (unsigned int i
= lo
; i
<= hi
; i
++)
4212 sym
[len
+ 0] = i
/ 10 + '0';
4213 sym
[len
+ 1] = i
% 10 + '0';
4214 Symbol
* gsym
= symtab
->lookup(sym
);
4215 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
4216 writing
= writing
|| refd
;
4219 if (this->contents_
== NULL
)
4220 this->contents_
= new unsigned char[this->savres_max
];
4222 section_size_type value
= this->current_data_size();
4223 unsigned char* p
= this->contents_
+ value
;
4225 p
= write_ent(p
, i
);
4227 p
= write_tail(p
, i
);
4228 section_size_type cur_size
= p
- this->contents_
;
4229 this->set_current_data_size(cur_size
);
4231 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
4232 this, value
, cur_size
- value
,
4233 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
4234 elfcpp::STV_HIDDEN
, 0, false, false);
4239 // Write out save/restore.
4241 template<int size
, bool big_endian
>
4243 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
4245 const section_size_type off
= this->offset();
4246 const section_size_type oview_size
=
4247 convert_to_section_size_type(this->data_size());
4248 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4249 memcpy(oview
, this->contents_
, oview_size
);
4250 of
->write_output_view(off
, oview_size
, oview
);
4254 // Create the glink section.
4256 template<int size
, bool big_endian
>
4258 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
4260 if (this->glink_
== NULL
)
4262 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
4263 this->glink_
->add_eh_frame(layout
);
4264 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
4265 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
4266 this->glink_
, ORDER_TEXT
, false);
4270 // Create a PLT entry for a global symbol.
4272 template<int size
, bool big_endian
>
4274 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
4278 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4279 && gsym
->can_use_relative_reloc(false))
4281 if (this->iplt_
== NULL
)
4282 this->make_iplt_section(symtab
, layout
);
4283 this->iplt_
->add_ifunc_entry(gsym
);
4287 if (this->plt_
== NULL
)
4288 this->make_plt_section(symtab
, layout
);
4289 this->plt_
->add_entry(gsym
);
4293 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
4295 template<int size
, bool big_endian
>
4297 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
4298 Symbol_table
* symtab
,
4300 Sized_relobj_file
<size
, big_endian
>* relobj
,
4303 if (this->iplt_
== NULL
)
4304 this->make_iplt_section(symtab
, layout
);
4305 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
4308 // Return the number of entries in the PLT.
4310 template<int size
, bool big_endian
>
4312 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
4314 if (this->plt_
== NULL
)
4316 unsigned int count
= this->plt_
->entry_count();
4317 if (this->iplt_
!= NULL
)
4318 count
+= this->iplt_
->entry_count();
4322 // Return the offset of the first non-reserved PLT entry.
4324 template<int size
, bool big_endian
>
4326 Target_powerpc
<size
, big_endian
>::first_plt_entry_offset() const
4328 return this->plt_
->first_plt_entry_offset();
4331 // Return the size of each PLT entry.
4333 template<int size
, bool big_endian
>
4335 Target_powerpc
<size
, big_endian
>::plt_entry_size() const
4337 return Output_data_plt_powerpc
<size
, big_endian
>::get_plt_entry_size();
4340 // Create a GOT entry for local dynamic __tls_get_addr calls.
4342 template<int size
, bool big_endian
>
4344 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
4345 Symbol_table
* symtab
,
4347 Sized_relobj_file
<size
, big_endian
>* object
)
4349 if (this->tlsld_got_offset_
== -1U)
4351 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
4352 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
4353 Output_data_got_powerpc
<size
, big_endian
>* got
4354 = this->got_section(symtab
, layout
);
4355 unsigned int got_offset
= got
->add_constant_pair(0, 0);
4356 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
4358 this->tlsld_got_offset_
= got_offset
;
4360 return this->tlsld_got_offset_
;
4363 // Get the Reference_flags for a particular relocation.
4365 template<int size
, bool big_endian
>
4367 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(unsigned int r_type
)
4371 case elfcpp::R_POWERPC_NONE
:
4372 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
4373 case elfcpp::R_POWERPC_GNU_VTENTRY
:
4374 case elfcpp::R_PPC64_TOC
:
4375 // No symbol reference.
4378 case elfcpp::R_PPC64_ADDR64
:
4379 case elfcpp::R_PPC64_UADDR64
:
4380 case elfcpp::R_POWERPC_ADDR32
:
4381 case elfcpp::R_POWERPC_UADDR32
:
4382 case elfcpp::R_POWERPC_ADDR16
:
4383 case elfcpp::R_POWERPC_UADDR16
:
4384 case elfcpp::R_POWERPC_ADDR16_LO
:
4385 case elfcpp::R_POWERPC_ADDR16_HI
:
4386 case elfcpp::R_POWERPC_ADDR16_HA
:
4387 return Symbol::ABSOLUTE_REF
;
4389 case elfcpp::R_POWERPC_ADDR24
:
4390 case elfcpp::R_POWERPC_ADDR14
:
4391 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4392 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4393 return Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
4395 case elfcpp::R_PPC64_REL64
:
4396 case elfcpp::R_POWERPC_REL32
:
4397 case elfcpp::R_PPC_LOCAL24PC
:
4398 case elfcpp::R_POWERPC_REL16
:
4399 case elfcpp::R_POWERPC_REL16_LO
:
4400 case elfcpp::R_POWERPC_REL16_HI
:
4401 case elfcpp::R_POWERPC_REL16_HA
:
4402 return Symbol::RELATIVE_REF
;
4404 case elfcpp::R_POWERPC_REL24
:
4405 case elfcpp::R_PPC_PLTREL24
:
4406 case elfcpp::R_POWERPC_REL14
:
4407 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4408 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4409 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
4411 case elfcpp::R_POWERPC_GOT16
:
4412 case elfcpp::R_POWERPC_GOT16_LO
:
4413 case elfcpp::R_POWERPC_GOT16_HI
:
4414 case elfcpp::R_POWERPC_GOT16_HA
:
4415 case elfcpp::R_PPC64_GOT16_DS
:
4416 case elfcpp::R_PPC64_GOT16_LO_DS
:
4417 case elfcpp::R_PPC64_TOC16
:
4418 case elfcpp::R_PPC64_TOC16_LO
:
4419 case elfcpp::R_PPC64_TOC16_HI
:
4420 case elfcpp::R_PPC64_TOC16_HA
:
4421 case elfcpp::R_PPC64_TOC16_DS
:
4422 case elfcpp::R_PPC64_TOC16_LO_DS
:
4424 return Symbol::ABSOLUTE_REF
;
4426 case elfcpp::R_POWERPC_GOT_TPREL16
:
4427 case elfcpp::R_POWERPC_TLS
:
4428 return Symbol::TLS_REF
;
4430 case elfcpp::R_POWERPC_COPY
:
4431 case elfcpp::R_POWERPC_GLOB_DAT
:
4432 case elfcpp::R_POWERPC_JMP_SLOT
:
4433 case elfcpp::R_POWERPC_RELATIVE
:
4434 case elfcpp::R_POWERPC_DTPMOD
:
4436 // Not expected. We will give an error later.
4441 // Report an unsupported relocation against a local symbol.
4443 template<int size
, bool big_endian
>
4445 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
4446 Sized_relobj_file
<size
, big_endian
>* object
,
4447 unsigned int r_type
)
4449 gold_error(_("%s: unsupported reloc %u against local symbol"),
4450 object
->name().c_str(), r_type
);
4453 // We are about to emit a dynamic relocation of type R_TYPE. If the
4454 // dynamic linker does not support it, issue an error.
4456 template<int size
, bool big_endian
>
4458 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
4459 unsigned int r_type
)
4461 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
4463 // These are the relocation types supported by glibc for both 32-bit
4464 // and 64-bit powerpc.
4467 case elfcpp::R_POWERPC_NONE
:
4468 case elfcpp::R_POWERPC_RELATIVE
:
4469 case elfcpp::R_POWERPC_GLOB_DAT
:
4470 case elfcpp::R_POWERPC_DTPMOD
:
4471 case elfcpp::R_POWERPC_DTPREL
:
4472 case elfcpp::R_POWERPC_TPREL
:
4473 case elfcpp::R_POWERPC_JMP_SLOT
:
4474 case elfcpp::R_POWERPC_COPY
:
4475 case elfcpp::R_POWERPC_IRELATIVE
:
4476 case elfcpp::R_POWERPC_ADDR32
:
4477 case elfcpp::R_POWERPC_UADDR32
:
4478 case elfcpp::R_POWERPC_ADDR24
:
4479 case elfcpp::R_POWERPC_ADDR16
:
4480 case elfcpp::R_POWERPC_UADDR16
:
4481 case elfcpp::R_POWERPC_ADDR16_LO
:
4482 case elfcpp::R_POWERPC_ADDR16_HI
:
4483 case elfcpp::R_POWERPC_ADDR16_HA
:
4484 case elfcpp::R_POWERPC_ADDR14
:
4485 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4486 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4487 case elfcpp::R_POWERPC_REL32
:
4488 case elfcpp::R_POWERPC_REL24
:
4489 case elfcpp::R_POWERPC_TPREL16
:
4490 case elfcpp::R_POWERPC_TPREL16_LO
:
4491 case elfcpp::R_POWERPC_TPREL16_HI
:
4492 case elfcpp::R_POWERPC_TPREL16_HA
:
4503 // These are the relocation types supported only on 64-bit.
4504 case elfcpp::R_PPC64_ADDR64
:
4505 case elfcpp::R_PPC64_UADDR64
:
4506 case elfcpp::R_PPC64_JMP_IREL
:
4507 case elfcpp::R_PPC64_ADDR16_DS
:
4508 case elfcpp::R_PPC64_ADDR16_LO_DS
:
4509 case elfcpp::R_PPC64_ADDR16_HIGHER
:
4510 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
4511 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
4512 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
4513 case elfcpp::R_PPC64_REL64
:
4514 case elfcpp::R_POWERPC_ADDR30
:
4515 case elfcpp::R_PPC64_TPREL16_DS
:
4516 case elfcpp::R_PPC64_TPREL16_LO_DS
:
4517 case elfcpp::R_PPC64_TPREL16_HIGHER
:
4518 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
4519 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
4520 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
4531 // These are the relocation types supported only on 32-bit.
4532 // ??? glibc ld.so doesn't need to support these.
4533 case elfcpp::R_POWERPC_DTPREL16
:
4534 case elfcpp::R_POWERPC_DTPREL16_LO
:
4535 case elfcpp::R_POWERPC_DTPREL16_HI
:
4536 case elfcpp::R_POWERPC_DTPREL16_HA
:
4544 // This prevents us from issuing more than one error per reloc
4545 // section. But we can still wind up issuing more than one
4546 // error per object file.
4547 if (this->issued_non_pic_error_
)
4549 gold_assert(parameters
->options().output_is_position_independent());
4550 object
->error(_("requires unsupported dynamic reloc; "
4551 "recompile with -fPIC"));
4552 this->issued_non_pic_error_
= true;
4556 // Return whether we need to make a PLT entry for a relocation of the
4557 // given type against a STT_GNU_IFUNC symbol.
4559 template<int size
, bool big_endian
>
4561 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
4562 Sized_relobj_file
<size
, big_endian
>* object
,
4563 unsigned int r_type
)
4565 // In non-pic code any reference will resolve to the plt call stub
4566 // for the ifunc symbol.
4567 if (size
== 32 && !parameters
->options().output_is_position_independent())
4572 // Word size refs from data sections are OK.
4573 case elfcpp::R_POWERPC_ADDR32
:
4574 case elfcpp::R_POWERPC_UADDR32
:
4579 case elfcpp::R_PPC64_ADDR64
:
4580 case elfcpp::R_PPC64_UADDR64
:
4585 // GOT refs are good.
4586 case elfcpp::R_POWERPC_GOT16
:
4587 case elfcpp::R_POWERPC_GOT16_LO
:
4588 case elfcpp::R_POWERPC_GOT16_HI
:
4589 case elfcpp::R_POWERPC_GOT16_HA
:
4590 case elfcpp::R_PPC64_GOT16_DS
:
4591 case elfcpp::R_PPC64_GOT16_LO_DS
:
4594 // So are function calls.
4595 case elfcpp::R_POWERPC_ADDR24
:
4596 case elfcpp::R_POWERPC_ADDR14
:
4597 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4598 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4599 case elfcpp::R_POWERPC_REL24
:
4600 case elfcpp::R_PPC_PLTREL24
:
4601 case elfcpp::R_POWERPC_REL14
:
4602 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4603 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4610 // Anything else is a problem.
4611 // If we are building a static executable, the libc startup function
4612 // responsible for applying indirect function relocations is going
4613 // to complain about the reloc type.
4614 // If we are building a dynamic executable, we will have a text
4615 // relocation. The dynamic loader will set the text segment
4616 // writable and non-executable to apply text relocations. So we'll
4617 // segfault when trying to run the indirection function to resolve
4619 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
4620 object
->name().c_str(), r_type
);
4624 // Scan a relocation for a local symbol.
4626 template<int size
, bool big_endian
>
4628 Target_powerpc
<size
, big_endian
>::Scan::local(
4629 Symbol_table
* symtab
,
4631 Target_powerpc
<size
, big_endian
>* target
,
4632 Sized_relobj_file
<size
, big_endian
>* object
,
4633 unsigned int data_shndx
,
4634 Output_section
* output_section
,
4635 const elfcpp::Rela
<size
, big_endian
>& reloc
,
4636 unsigned int r_type
,
4637 const elfcpp::Sym
<size
, big_endian
>& lsym
,
4640 this->maybe_skip_tls_get_addr_call(r_type
, NULL
);
4642 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
4643 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
4645 this->expect_tls_get_addr_call();
4646 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
4647 if (tls_type
!= tls::TLSOPT_NONE
)
4648 this->skip_next_tls_get_addr_call();
4650 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
4651 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
4653 this->expect_tls_get_addr_call();
4654 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
4655 if (tls_type
!= tls::TLSOPT_NONE
)
4656 this->skip_next_tls_get_addr_call();
4659 Powerpc_relobj
<size
, big_endian
>* ppc_object
4660 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
4665 && data_shndx
== ppc_object
->opd_shndx()
4666 && r_type
== elfcpp::R_PPC64_ADDR64
)
4667 ppc_object
->set_opd_discard(reloc
.get_r_offset());
4671 // A local STT_GNU_IFUNC symbol may require a PLT entry.
4672 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
4673 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
4675 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4676 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4677 r_type
, r_sym
, reloc
.get_r_addend());
4678 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
4683 case elfcpp::R_POWERPC_NONE
:
4684 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
4685 case elfcpp::R_POWERPC_GNU_VTENTRY
:
4686 case elfcpp::R_PPC64_TOCSAVE
:
4687 case elfcpp::R_PPC_EMB_MRKREF
:
4688 case elfcpp::R_POWERPC_TLS
:
4691 case elfcpp::R_PPC64_TOC
:
4693 Output_data_got_powerpc
<size
, big_endian
>* got
4694 = target
->got_section(symtab
, layout
);
4695 if (parameters
->options().output_is_position_independent())
4697 Address off
= reloc
.get_r_offset();
4699 && data_shndx
== ppc_object
->opd_shndx()
4700 && ppc_object
->get_opd_discard(off
- 8))
4703 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4704 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
4705 rela_dyn
->add_output_section_relative(got
->output_section(),
4706 elfcpp::R_POWERPC_RELATIVE
,
4708 object
, data_shndx
, off
,
4709 symobj
->toc_base_offset());
4714 case elfcpp::R_PPC64_ADDR64
:
4715 case elfcpp::R_PPC64_UADDR64
:
4716 case elfcpp::R_POWERPC_ADDR32
:
4717 case elfcpp::R_POWERPC_UADDR32
:
4718 case elfcpp::R_POWERPC_ADDR24
:
4719 case elfcpp::R_POWERPC_ADDR16
:
4720 case elfcpp::R_POWERPC_ADDR16_LO
:
4721 case elfcpp::R_POWERPC_ADDR16_HI
:
4722 case elfcpp::R_POWERPC_ADDR16_HA
:
4723 case elfcpp::R_POWERPC_UADDR16
:
4724 case elfcpp::R_PPC64_ADDR16_HIGHER
:
4725 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
4726 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
4727 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
4728 case elfcpp::R_PPC64_ADDR16_DS
:
4729 case elfcpp::R_PPC64_ADDR16_LO_DS
:
4730 case elfcpp::R_POWERPC_ADDR14
:
4731 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4732 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4733 // If building a shared library (or a position-independent
4734 // executable), we need to create a dynamic relocation for
4736 if (parameters
->options().output_is_position_independent()
4737 || (size
== 64 && is_ifunc
))
4739 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4741 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
4742 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
4744 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4745 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
4748 rela_dyn
= target
->iplt_section()->rel_plt();
4749 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
4751 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
4752 output_section
, data_shndx
,
4753 reloc
.get_r_offset(),
4754 reloc
.get_r_addend(), false);
4758 check_non_pic(object
, r_type
);
4759 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4760 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
4761 data_shndx
, reloc
.get_r_offset(),
4762 reloc
.get_r_addend());
4767 case elfcpp::R_POWERPC_REL24
:
4768 case elfcpp::R_PPC_PLTREL24
:
4769 case elfcpp::R_PPC_LOCAL24PC
:
4770 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4771 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4772 reloc
.get_r_addend());
4775 case elfcpp::R_POWERPC_REL14
:
4776 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4777 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4778 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
4779 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
4780 reloc
.get_r_addend());
4783 case elfcpp::R_PPC64_REL64
:
4784 case elfcpp::R_POWERPC_REL32
:
4785 case elfcpp::R_POWERPC_REL16
:
4786 case elfcpp::R_POWERPC_REL16_LO
:
4787 case elfcpp::R_POWERPC_REL16_HI
:
4788 case elfcpp::R_POWERPC_REL16_HA
:
4789 case elfcpp::R_POWERPC_SECTOFF
:
4790 case elfcpp::R_POWERPC_TPREL16
:
4791 case elfcpp::R_POWERPC_DTPREL16
:
4792 case elfcpp::R_POWERPC_SECTOFF_LO
:
4793 case elfcpp::R_POWERPC_TPREL16_LO
:
4794 case elfcpp::R_POWERPC_DTPREL16_LO
:
4795 case elfcpp::R_POWERPC_SECTOFF_HI
:
4796 case elfcpp::R_POWERPC_TPREL16_HI
:
4797 case elfcpp::R_POWERPC_DTPREL16_HI
:
4798 case elfcpp::R_POWERPC_SECTOFF_HA
:
4799 case elfcpp::R_POWERPC_TPREL16_HA
:
4800 case elfcpp::R_POWERPC_DTPREL16_HA
:
4801 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
4802 case elfcpp::R_PPC64_TPREL16_HIGHER
:
4803 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
4804 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
4805 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
4806 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
4807 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
4808 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
4809 case elfcpp::R_PPC64_TPREL16_DS
:
4810 case elfcpp::R_PPC64_TPREL16_LO_DS
:
4811 case elfcpp::R_PPC64_DTPREL16_DS
:
4812 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
4813 case elfcpp::R_PPC64_SECTOFF_DS
:
4814 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
4815 case elfcpp::R_PPC64_TLSGD
:
4816 case elfcpp::R_PPC64_TLSLD
:
4819 case elfcpp::R_POWERPC_GOT16
:
4820 case elfcpp::R_POWERPC_GOT16_LO
:
4821 case elfcpp::R_POWERPC_GOT16_HI
:
4822 case elfcpp::R_POWERPC_GOT16_HA
:
4823 case elfcpp::R_PPC64_GOT16_DS
:
4824 case elfcpp::R_PPC64_GOT16_LO_DS
:
4826 // The symbol requires a GOT entry.
4827 Output_data_got_powerpc
<size
, big_endian
>* got
4828 = target
->got_section(symtab
, layout
);
4829 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4831 if (!parameters
->options().output_is_position_independent())
4833 if (size
== 32 && is_ifunc
)
4834 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
4836 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
4838 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
4840 // If we are generating a shared object or a pie, this
4841 // symbol's GOT entry will be set by a dynamic relocation.
4843 off
= got
->add_constant(0);
4844 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
4846 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4847 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
4850 rela_dyn
= target
->iplt_section()->rel_plt();
4851 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
4853 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
4854 got
, off
, 0, false);
4859 case elfcpp::R_PPC64_TOC16
:
4860 case elfcpp::R_PPC64_TOC16_LO
:
4861 case elfcpp::R_PPC64_TOC16_HI
:
4862 case elfcpp::R_PPC64_TOC16_HA
:
4863 case elfcpp::R_PPC64_TOC16_DS
:
4864 case elfcpp::R_PPC64_TOC16_LO_DS
:
4865 // We need a GOT section.
4866 target
->got_section(symtab
, layout
);
4869 case elfcpp::R_POWERPC_GOT_TLSGD16
:
4870 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
4871 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
4872 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
4874 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
4875 if (tls_type
== tls::TLSOPT_NONE
)
4877 Output_data_got_powerpc
<size
, big_endian
>* got
4878 = target
->got_section(symtab
, layout
);
4879 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4880 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4881 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
4882 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
4884 else if (tls_type
== tls::TLSOPT_TO_LE
)
4886 // no GOT relocs needed for Local Exec.
4893 case elfcpp::R_POWERPC_GOT_TLSLD16
:
4894 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
4895 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
4896 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
4898 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
4899 if (tls_type
== tls::TLSOPT_NONE
)
4900 target
->tlsld_got_offset(symtab
, layout
, object
);
4901 else if (tls_type
== tls::TLSOPT_TO_LE
)
4903 // no GOT relocs needed for Local Exec.
4904 if (parameters
->options().emit_relocs())
4906 Output_section
* os
= layout
->tls_segment()->first_section();
4907 gold_assert(os
!= NULL
);
4908 os
->set_needs_symtab_index();
4916 case elfcpp::R_POWERPC_GOT_DTPREL16
:
4917 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
4918 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
4919 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
4921 Output_data_got_powerpc
<size
, big_endian
>* got
4922 = target
->got_section(symtab
, layout
);
4923 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4924 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
4928 case elfcpp::R_POWERPC_GOT_TPREL16
:
4929 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
4930 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
4931 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
4933 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
4934 if (tls_type
== tls::TLSOPT_NONE
)
4936 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
4937 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
4939 Output_data_got_powerpc
<size
, big_endian
>* got
4940 = target
->got_section(symtab
, layout
);
4941 unsigned int off
= got
->add_constant(0);
4942 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
4944 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4945 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
4946 elfcpp::R_POWERPC_TPREL
,
4950 else if (tls_type
== tls::TLSOPT_TO_LE
)
4952 // no GOT relocs needed for Local Exec.
4960 unsupported_reloc_local(object
, r_type
);
4966 case elfcpp::R_POWERPC_GOT_TLSLD16
:
4967 case elfcpp::R_POWERPC_GOT_TLSGD16
:
4968 case elfcpp::R_POWERPC_GOT_TPREL16
:
4969 case elfcpp::R_POWERPC_GOT_DTPREL16
:
4970 case elfcpp::R_POWERPC_GOT16
:
4971 case elfcpp::R_PPC64_GOT16_DS
:
4972 case elfcpp::R_PPC64_TOC16
:
4973 case elfcpp::R_PPC64_TOC16_DS
:
4974 ppc_object
->set_has_small_toc_reloc();
4980 // Report an unsupported relocation against a global symbol.
4982 template<int size
, bool big_endian
>
4984 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
4985 Sized_relobj_file
<size
, big_endian
>* object
,
4986 unsigned int r_type
,
4989 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4990 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
4993 // Scan a relocation for a global symbol.
4995 template<int size
, bool big_endian
>
4997 Target_powerpc
<size
, big_endian
>::Scan::global(
4998 Symbol_table
* symtab
,
5000 Target_powerpc
<size
, big_endian
>* target
,
5001 Sized_relobj_file
<size
, big_endian
>* object
,
5002 unsigned int data_shndx
,
5003 Output_section
* output_section
,
5004 const elfcpp::Rela
<size
, big_endian
>& reloc
,
5005 unsigned int r_type
,
5008 if (this->maybe_skip_tls_get_addr_call(r_type
, gsym
) == Track_tls::SKIP
)
5011 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
5012 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
5014 this->expect_tls_get_addr_call();
5015 const bool final
= gsym
->final_value_is_known();
5016 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
5017 if (tls_type
!= tls::TLSOPT_NONE
)
5018 this->skip_next_tls_get_addr_call();
5020 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
5021 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
5023 this->expect_tls_get_addr_call();
5024 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5025 if (tls_type
!= tls::TLSOPT_NONE
)
5026 this->skip_next_tls_get_addr_call();
5029 Powerpc_relobj
<size
, big_endian
>* ppc_object
5030 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5032 // A STT_GNU_IFUNC symbol may require a PLT entry.
5033 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
5034 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
5036 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5037 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5038 reloc
.get_r_addend());
5039 target
->make_plt_entry(symtab
, layout
, gsym
);
5044 case elfcpp::R_POWERPC_NONE
:
5045 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5046 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5047 case elfcpp::R_PPC_LOCAL24PC
:
5048 case elfcpp::R_PPC_EMB_MRKREF
:
5049 case elfcpp::R_POWERPC_TLS
:
5052 case elfcpp::R_PPC64_TOC
:
5054 Output_data_got_powerpc
<size
, big_endian
>* got
5055 = target
->got_section(symtab
, layout
);
5056 if (parameters
->options().output_is_position_independent())
5058 Address off
= reloc
.get_r_offset();
5060 && data_shndx
== ppc_object
->opd_shndx()
5061 && ppc_object
->get_opd_discard(off
- 8))
5064 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5065 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
5066 if (data_shndx
!= ppc_object
->opd_shndx())
5067 symobj
= static_cast
5068 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
5069 rela_dyn
->add_output_section_relative(got
->output_section(),
5070 elfcpp::R_POWERPC_RELATIVE
,
5072 object
, data_shndx
, off
,
5073 symobj
->toc_base_offset());
5078 case elfcpp::R_PPC64_ADDR64
:
5080 && data_shndx
== ppc_object
->opd_shndx()
5081 && (gsym
->is_defined_in_discarded_section()
5082 || gsym
->object() != object
))
5084 ppc_object
->set_opd_discard(reloc
.get_r_offset());
5088 case elfcpp::R_PPC64_UADDR64
:
5089 case elfcpp::R_POWERPC_ADDR32
:
5090 case elfcpp::R_POWERPC_UADDR32
:
5091 case elfcpp::R_POWERPC_ADDR24
:
5092 case elfcpp::R_POWERPC_ADDR16
:
5093 case elfcpp::R_POWERPC_ADDR16_LO
:
5094 case elfcpp::R_POWERPC_ADDR16_HI
:
5095 case elfcpp::R_POWERPC_ADDR16_HA
:
5096 case elfcpp::R_POWERPC_UADDR16
:
5097 case elfcpp::R_PPC64_ADDR16_HIGHER
:
5098 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
5099 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
5100 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
5101 case elfcpp::R_PPC64_ADDR16_DS
:
5102 case elfcpp::R_PPC64_ADDR16_LO_DS
:
5103 case elfcpp::R_POWERPC_ADDR14
:
5104 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5105 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5107 // Make a PLT entry if necessary.
5108 if (gsym
->needs_plt_entry())
5110 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5112 elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5113 reloc
.get_r_addend());
5114 target
->make_plt_entry(symtab
, layout
, gsym
);
5115 // Since this is not a PC-relative relocation, we may be
5116 // taking the address of a function. In that case we need to
5117 // set the entry in the dynamic symbol table to the address of
5118 // the PLT call stub.
5120 && gsym
->is_from_dynobj()
5121 && !parameters
->options().output_is_position_independent())
5122 gsym
->set_needs_dynsym_value();
5124 // Make a dynamic relocation if necessary.
5125 if (needs_dynamic_reloc
<size
>(gsym
, Scan::get_reference_flags(r_type
))
5126 || (size
== 64 && gsym
->type() == elfcpp::STT_GNU_IFUNC
))
5128 if (gsym
->may_need_copy_reloc())
5130 target
->copy_reloc(symtab
, layout
, object
,
5131 data_shndx
, output_section
, gsym
, reloc
);
5133 else if ((size
== 32
5134 && r_type
== elfcpp::R_POWERPC_ADDR32
5135 && gsym
->can_use_relative_reloc(false)
5136 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
5137 && parameters
->options().shared()))
5139 && r_type
== elfcpp::R_PPC64_ADDR64
5140 && (gsym
->can_use_relative_reloc(false)
5141 || data_shndx
== ppc_object
->opd_shndx())))
5143 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5144 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
5145 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
5147 rela_dyn
= target
->iplt_section()->rel_plt();
5148 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
5150 rela_dyn
->add_symbolless_global_addend(
5151 gsym
, dynrel
, output_section
, object
, data_shndx
,
5152 reloc
.get_r_offset(), reloc
.get_r_addend());
5156 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5157 check_non_pic(object
, r_type
);
5158 rela_dyn
->add_global(gsym
, r_type
, output_section
,
5160 reloc
.get_r_offset(),
5161 reloc
.get_r_addend());
5167 case elfcpp::R_PPC_PLTREL24
:
5168 case elfcpp::R_POWERPC_REL24
:
5169 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5170 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5171 reloc
.get_r_addend());
5172 if (gsym
->needs_plt_entry()
5173 || (!gsym
->final_value_is_known()
5174 && (gsym
->is_undefined()
5175 || gsym
->is_from_dynobj()
5176 || gsym
->is_preemptible())))
5177 target
->make_plt_entry(symtab
, layout
, gsym
);
5180 case elfcpp::R_PPC64_REL64
:
5181 case elfcpp::R_POWERPC_REL32
:
5182 // Make a dynamic relocation if necessary.
5183 if (needs_dynamic_reloc
<size
>(gsym
, Scan::get_reference_flags(r_type
)))
5185 if (gsym
->may_need_copy_reloc())
5187 target
->copy_reloc(symtab
, layout
, object
,
5188 data_shndx
, output_section
, gsym
,
5193 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5194 check_non_pic(object
, r_type
);
5195 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
5196 data_shndx
, reloc
.get_r_offset(),
5197 reloc
.get_r_addend());
5202 case elfcpp::R_POWERPC_REL14
:
5203 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5204 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5205 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5206 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5207 reloc
.get_r_addend());
5210 case elfcpp::R_POWERPC_REL16
:
5211 case elfcpp::R_POWERPC_REL16_LO
:
5212 case elfcpp::R_POWERPC_REL16_HI
:
5213 case elfcpp::R_POWERPC_REL16_HA
:
5214 case elfcpp::R_POWERPC_SECTOFF
:
5215 case elfcpp::R_POWERPC_TPREL16
:
5216 case elfcpp::R_POWERPC_DTPREL16
:
5217 case elfcpp::R_POWERPC_SECTOFF_LO
:
5218 case elfcpp::R_POWERPC_TPREL16_LO
:
5219 case elfcpp::R_POWERPC_DTPREL16_LO
:
5220 case elfcpp::R_POWERPC_SECTOFF_HI
:
5221 case elfcpp::R_POWERPC_TPREL16_HI
:
5222 case elfcpp::R_POWERPC_DTPREL16_HI
:
5223 case elfcpp::R_POWERPC_SECTOFF_HA
:
5224 case elfcpp::R_POWERPC_TPREL16_HA
:
5225 case elfcpp::R_POWERPC_DTPREL16_HA
:
5226 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
5227 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5228 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
5229 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5230 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
5231 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5232 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
5233 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5234 case elfcpp::R_PPC64_TPREL16_DS
:
5235 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5236 case elfcpp::R_PPC64_DTPREL16_DS
:
5237 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
5238 case elfcpp::R_PPC64_SECTOFF_DS
:
5239 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
5240 case elfcpp::R_PPC64_TLSGD
:
5241 case elfcpp::R_PPC64_TLSLD
:
5244 case elfcpp::R_POWERPC_GOT16
:
5245 case elfcpp::R_POWERPC_GOT16_LO
:
5246 case elfcpp::R_POWERPC_GOT16_HI
:
5247 case elfcpp::R_POWERPC_GOT16_HA
:
5248 case elfcpp::R_PPC64_GOT16_DS
:
5249 case elfcpp::R_PPC64_GOT16_LO_DS
:
5251 // The symbol requires a GOT entry.
5252 Output_data_got_powerpc
<size
, big_endian
>* got
;
5254 got
= target
->got_section(symtab
, layout
);
5255 if (gsym
->final_value_is_known())
5257 if (size
== 32 && gsym
->type() == elfcpp::STT_GNU_IFUNC
)
5258 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
5260 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
5262 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
5264 // If we are generating a shared object or a pie, this
5265 // symbol's GOT entry will be set by a dynamic relocation.
5266 unsigned int off
= got
->add_constant(0);
5267 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
5269 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5270 if (gsym
->can_use_relative_reloc(false)
5272 && gsym
->visibility() == elfcpp::STV_PROTECTED
5273 && parameters
->options().shared()))
5275 unsigned int dynrel
= elfcpp::R_POWERPC_RELATIVE
;
5276 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
5278 rela_dyn
= target
->iplt_section()->rel_plt();
5279 dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
5281 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
5285 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
5286 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
5292 case elfcpp::R_PPC64_TOC16
:
5293 case elfcpp::R_PPC64_TOC16_LO
:
5294 case elfcpp::R_PPC64_TOC16_HI
:
5295 case elfcpp::R_PPC64_TOC16_HA
:
5296 case elfcpp::R_PPC64_TOC16_DS
:
5297 case elfcpp::R_PPC64_TOC16_LO_DS
:
5298 // We need a GOT section.
5299 target
->got_section(symtab
, layout
);
5302 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5303 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
5304 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
5305 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
5307 const bool final
= gsym
->final_value_is_known();
5308 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
5309 if (tls_type
== tls::TLSOPT_NONE
)
5311 Output_data_got_powerpc
<size
, big_endian
>* got
5312 = target
->got_section(symtab
, layout
);
5313 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
,
5314 target
->rela_dyn_section(layout
),
5315 elfcpp::R_POWERPC_DTPMOD
,
5316 elfcpp::R_POWERPC_DTPREL
);
5318 else if (tls_type
== tls::TLSOPT_TO_IE
)
5320 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
5322 Output_data_got_powerpc
<size
, big_endian
>* got
5323 = target
->got_section(symtab
, layout
);
5324 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5325 if (gsym
->is_undefined()
5326 || gsym
->is_from_dynobj())
5328 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
5329 elfcpp::R_POWERPC_TPREL
);
5333 unsigned int off
= got
->add_constant(0);
5334 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
5335 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
5336 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
5341 else if (tls_type
== tls::TLSOPT_TO_LE
)
5343 // no GOT relocs needed for Local Exec.
5350 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5351 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
5352 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
5353 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
5355 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5356 if (tls_type
== tls::TLSOPT_NONE
)
5357 target
->tlsld_got_offset(symtab
, layout
, object
);
5358 else if (tls_type
== tls::TLSOPT_TO_LE
)
5360 // no GOT relocs needed for Local Exec.
5361 if (parameters
->options().emit_relocs())
5363 Output_section
* os
= layout
->tls_segment()->first_section();
5364 gold_assert(os
!= NULL
);
5365 os
->set_needs_symtab_index();
5373 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5374 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
5375 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
5376 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
5378 Output_data_got_powerpc
<size
, big_endian
>* got
5379 = target
->got_section(symtab
, layout
);
5380 if (!gsym
->final_value_is_known()
5381 && (gsym
->is_from_dynobj()
5382 || gsym
->is_undefined()
5383 || gsym
->is_preemptible()))
5384 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
5385 target
->rela_dyn_section(layout
),
5386 elfcpp::R_POWERPC_DTPREL
);
5388 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
5392 case elfcpp::R_POWERPC_GOT_TPREL16
:
5393 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
5394 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
5395 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
5397 const bool final
= gsym
->final_value_is_known();
5398 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
5399 if (tls_type
== tls::TLSOPT_NONE
)
5401 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
5403 Output_data_got_powerpc
<size
, big_endian
>* got
5404 = target
->got_section(symtab
, layout
);
5405 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5406 if (gsym
->is_undefined()
5407 || gsym
->is_from_dynobj())
5409 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
5410 elfcpp::R_POWERPC_TPREL
);
5414 unsigned int off
= got
->add_constant(0);
5415 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
5416 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
5417 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
5422 else if (tls_type
== tls::TLSOPT_TO_LE
)
5424 // no GOT relocs needed for Local Exec.
5432 unsupported_reloc_global(object
, r_type
, gsym
);
5438 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5439 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5440 case elfcpp::R_POWERPC_GOT_TPREL16
:
5441 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5442 case elfcpp::R_POWERPC_GOT16
:
5443 case elfcpp::R_PPC64_GOT16_DS
:
5444 case elfcpp::R_PPC64_TOC16
:
5445 case elfcpp::R_PPC64_TOC16_DS
:
5446 ppc_object
->set_has_small_toc_reloc();
5452 // Process relocations for gc.
5454 template<int size
, bool big_endian
>
5456 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
5457 Symbol_table
* symtab
,
5459 Sized_relobj_file
<size
, big_endian
>* object
,
5460 unsigned int data_shndx
,
5462 const unsigned char* prelocs
,
5464 Output_section
* output_section
,
5465 bool needs_special_offset_handling
,
5466 size_t local_symbol_count
,
5467 const unsigned char* plocal_symbols
)
5469 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
5470 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
5471 Powerpc_relobj
<size
, big_endian
>* ppc_object
5472 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5474 ppc_object
->set_opd_valid();
5475 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
5477 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
5478 for (p
= ppc_object
->access_from_map()->begin();
5479 p
!= ppc_object
->access_from_map()->end();
5482 Address dst_off
= p
->first
;
5483 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
5484 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
5485 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
5487 Object
* src_obj
= s
->first
;
5488 unsigned int src_indx
= s
->second
;
5489 symtab
->gc()->add_reference(src_obj
, src_indx
,
5490 ppc_object
, dst_indx
);
5494 ppc_object
->access_from_map()->clear();
5495 ppc_object
->process_gc_mark(symtab
);
5496 // Don't look at .opd relocs as .opd will reference everything.
5500 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
5501 typename
Target_powerpc::Relocatable_size_for_reloc
>(
5510 needs_special_offset_handling
,
5515 // Handle target specific gc actions when adding a gc reference from
5516 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
5517 // and DST_OFF. For powerpc64, this adds a referenc to the code
5518 // section of a function descriptor.
5520 template<int size
, bool big_endian
>
5522 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
5523 Symbol_table
* symtab
,
5525 unsigned int src_shndx
,
5527 unsigned int dst_shndx
,
5528 Address dst_off
) const
5530 Powerpc_relobj
<size
, big_endian
>* ppc_object
5531 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
5533 && !ppc_object
->is_dynamic()
5534 && dst_shndx
== ppc_object
->opd_shndx())
5536 if (ppc_object
->opd_valid())
5538 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
5539 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
5543 // If we haven't run scan_opd_relocs, we must delay
5544 // processing this function descriptor reference.
5545 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
5550 // Add any special sections for this symbol to the gc work list.
5551 // For powerpc64, this adds the code section of a function
5554 template<int size
, bool big_endian
>
5556 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
5557 Symbol_table
* symtab
,
5562 Powerpc_relobj
<size
, big_endian
>* ppc_object
5563 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
5565 unsigned int shndx
= sym
->shndx(&is_ordinary
);
5566 if (is_ordinary
&& shndx
== ppc_object
->opd_shndx())
5568 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
5569 Address dst_off
= gsym
->value();
5570 if (ppc_object
->opd_valid())
5572 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
5573 symtab
->gc()->worklist().push(Section_id(ppc_object
, dst_indx
));
5576 ppc_object
->add_gc_mark(dst_off
);
5581 // Scan relocations for a section.
5583 template<int size
, bool big_endian
>
5585 Target_powerpc
<size
, big_endian
>::scan_relocs(
5586 Symbol_table
* symtab
,
5588 Sized_relobj_file
<size
, big_endian
>* object
,
5589 unsigned int data_shndx
,
5590 unsigned int sh_type
,
5591 const unsigned char* prelocs
,
5593 Output_section
* output_section
,
5594 bool needs_special_offset_handling
,
5595 size_t local_symbol_count
,
5596 const unsigned char* plocal_symbols
)
5598 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
5599 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
5601 if (sh_type
== elfcpp::SHT_REL
)
5603 gold_error(_("%s: unsupported REL reloc section"),
5604 object
->name().c_str());
5608 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
5617 needs_special_offset_handling
,
5622 // Functor class for processing the global symbol table.
5623 // Removes symbols defined on discarded opd entries.
5625 template<bool big_endian
>
5626 class Global_symbol_visitor_opd
5629 Global_symbol_visitor_opd()
5633 operator()(Sized_symbol
<64>* sym
)
5635 if (sym
->has_symtab_index()
5636 || sym
->source() != Symbol::FROM_OBJECT
5637 || !sym
->in_real_elf())
5640 Powerpc_relobj
<64, big_endian
>* symobj
5641 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
5642 if (symobj
->is_dynamic()
5643 || symobj
->opd_shndx() == 0)
5647 unsigned int shndx
= sym
->shndx(&is_ordinary
);
5648 if (shndx
== symobj
->opd_shndx()
5649 && symobj
->get_opd_discard(sym
->value()))
5650 sym
->set_symtab_index(-1U);
5654 template<int size
, bool big_endian
>
5656 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
5658 Symbol_table
* symtab
)
5662 Output_data_save_res
<64, big_endian
>* savres
5663 = new Output_data_save_res
<64, big_endian
>(symtab
);
5664 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
5665 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
5666 savres
, ORDER_TEXT
, false);
5670 // Sort linker created .got section first (for the header), then input
5671 // sections belonging to files using small model code.
5673 template<bool big_endian
>
5674 class Sort_toc_sections
5678 operator()(const Output_section::Input_section
& is1
,
5679 const Output_section::Input_section
& is2
) const
5681 if (!is1
.is_input_section() && is2
.is_input_section())
5684 = (is1
.is_input_section()
5685 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
5686 ->has_small_toc_reloc()));
5688 = (is2
.is_input_section()
5689 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
5690 ->has_small_toc_reloc()));
5691 return small1
&& !small2
;
5695 // Finalize the sections.
5697 template<int size
, bool big_endian
>
5699 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
5701 const Input_objects
*,
5702 Symbol_table
* symtab
)
5704 if (parameters
->doing_static_link())
5706 // At least some versions of glibc elf-init.o have a strong
5707 // reference to __rela_iplt marker syms. A weak ref would be
5709 if (this->iplt_
!= NULL
)
5711 Reloc_section
* rel
= this->iplt_
->rel_plt();
5712 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
5713 Symbol_table::PREDEFINED
, rel
, 0, 0,
5714 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5715 elfcpp::STV_HIDDEN
, 0, false, true);
5716 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
5717 Symbol_table::PREDEFINED
, rel
, 0, 0,
5718 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5719 elfcpp::STV_HIDDEN
, 0, true, true);
5723 symtab
->define_as_constant("__rela_iplt_start", NULL
,
5724 Symbol_table::PREDEFINED
, 0, 0,
5725 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5726 elfcpp::STV_HIDDEN
, 0, true, false);
5727 symtab
->define_as_constant("__rela_iplt_end", NULL
,
5728 Symbol_table::PREDEFINED
, 0, 0,
5729 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
5730 elfcpp::STV_HIDDEN
, 0, true, false);
5736 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
5737 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
5739 if (!parameters
->options().relocatable())
5741 this->define_save_restore_funcs(layout
, symtab
);
5743 // Annoyingly, we need to make these sections now whether or
5744 // not we need them. If we delay until do_relax then we
5745 // need to mess with the relaxation machinery checkpointing.
5746 this->got_section(symtab
, layout
);
5747 this->make_brlt_section(layout
);
5749 if (parameters
->options().toc_sort())
5751 Output_section
* os
= this->got_
->output_section();
5752 if (os
!= NULL
&& os
->input_sections().size() > 1)
5753 std::stable_sort(os
->input_sections().begin(),
5754 os
->input_sections().end(),
5755 Sort_toc_sections
<big_endian
>());
5760 // Fill in some more dynamic tags.
5761 Output_data_dynamic
* odyn
= layout
->dynamic_data();
5764 const Reloc_section
* rel_plt
= (this->plt_
== NULL
5766 : this->plt_
->rel_plt());
5767 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
5768 this->rela_dyn_
, true, size
== 32);
5772 if (this->got_
!= NULL
)
5774 this->got_
->finalize_data_size();
5775 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
5776 this->got_
, this->got_
->g_o_t());
5781 if (this->glink_
!= NULL
)
5783 this->glink_
->finalize_data_size();
5784 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
5786 (this->glink_
->pltresolve_size
5792 // Emit any relocs we saved in an attempt to avoid generating COPY
5794 if (this->copy_relocs_
.any_saved_relocs())
5795 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
5798 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
5802 ok_lo_toc_insn(uint32_t insn
)
5804 return ((insn
& (0x3f << 26)) == 14u << 26 /* addi */
5805 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
5806 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
5807 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
5808 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
5809 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
5810 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
5811 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
5812 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
5813 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
5814 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
5815 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
5816 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
5817 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
5818 || ((insn
& (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
5820 || ((insn
& (0x3f << 26)) == 62u << 26 /* std, stmd */
5821 && ((insn
& 3) == 0 || (insn
& 3) == 3))
5822 || (insn
& (0x3f << 26)) == 12u << 26 /* addic */);
5825 // Return the value to use for a branch relocation.
5827 template<int size
, bool big_endian
>
5828 typename Target_powerpc
<size
, big_endian
>::Address
5829 Target_powerpc
<size
, big_endian
>::symval_for_branch(
5831 const Sized_symbol
<size
>* gsym
,
5832 Powerpc_relobj
<size
, big_endian
>* object
,
5833 unsigned int *dest_shndx
)
5839 // If the symbol is defined in an opd section, ie. is a function
5840 // descriptor, use the function descriptor code entry address
5841 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
5843 && gsym
->source() != Symbol::FROM_OBJECT
)
5846 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
5847 unsigned int shndx
= symobj
->opd_shndx();
5850 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
5851 gold_assert(opd_addr
!= invalid_address
);
5852 opd_addr
+= symobj
->output_section(shndx
)->address();
5853 if (value
>= opd_addr
&& value
< opd_addr
+ symobj
->section_size(shndx
))
5856 *dest_shndx
= symobj
->get_opd_ent(value
- opd_addr
, &sec_off
);
5857 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
5858 gold_assert(sec_addr
!= invalid_address
);
5859 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
5860 value
= sec_addr
+ sec_off
;
5865 // Perform a relocation.
5867 template<int size
, bool big_endian
>
5869 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
5870 const Relocate_info
<size
, big_endian
>* relinfo
,
5871 Target_powerpc
* target
,
5874 const elfcpp::Rela
<size
, big_endian
>& rela
,
5875 unsigned int r_type
,
5876 const Sized_symbol
<size
>* gsym
,
5877 const Symbol_value
<size
>* psymval
,
5878 unsigned char* view
,
5880 section_size_type view_size
)
5882 switch (this->maybe_skip_tls_get_addr_call(r_type
, gsym
))
5884 case Track_tls::NOT_EXPECTED
:
5885 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5886 _("__tls_get_addr call lacks marker reloc"));
5888 case Track_tls::EXPECTED
:
5889 // We have already complained.
5891 case Track_tls::SKIP
:
5893 case Track_tls::NORMAL
:
5897 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
5898 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
5899 Powerpc_relobj
<size
, big_endian
>* const object
5900 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
5902 bool has_plt_value
= false;
5903 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5905 ? use_plt_offset
<size
>(gsym
, Scan::get_reference_flags(r_type
))
5906 : object
->local_has_plt_offset(r_sym
))
5908 Stub_table
<size
, big_endian
>* stub_table
5909 = object
->stub_table(relinfo
->data_shndx
);
5910 if (stub_table
== NULL
)
5912 // This is a ref from a data section to an ifunc symbol.
5913 if (target
->stub_tables().size() != 0)
5914 stub_table
= target
->stub_tables()[0];
5916 gold_assert(stub_table
!= NULL
);
5919 off
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
5920 rela
.get_r_addend());
5922 off
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
5923 rela
.get_r_addend());
5924 gold_assert(off
!= invalid_address
);
5925 value
= stub_table
->stub_address() + off
;
5926 has_plt_value
= true;
5929 if (r_type
== elfcpp::R_POWERPC_GOT16
5930 || r_type
== elfcpp::R_POWERPC_GOT16_LO
5931 || r_type
== elfcpp::R_POWERPC_GOT16_HI
5932 || r_type
== elfcpp::R_POWERPC_GOT16_HA
5933 || r_type
== elfcpp::R_PPC64_GOT16_DS
5934 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
5938 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
5939 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
5943 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5944 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
5945 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
5947 value
-= target
->got_section()->got_base_offset(object
);
5949 else if (r_type
== elfcpp::R_PPC64_TOC
)
5951 value
= (target
->got_section()->output_section()->address()
5952 + object
->toc_base_offset());
5954 else if (gsym
!= NULL
5955 && (r_type
== elfcpp::R_POWERPC_REL24
5956 || r_type
== elfcpp::R_PPC_PLTREL24
)
5961 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
5962 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
5963 bool can_plt_call
= false;
5964 if (rela
.get_r_offset() + 8 <= view_size
)
5966 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
5967 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
5970 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
5972 elfcpp::Swap
<32, big_endian
>::writeval(wv
+ 1, ld_2_1
+ 40);
5973 can_plt_call
= true;
5978 // If we don't have a branch and link followed by a nop,
5979 // we can't go via the plt because there is no place to
5980 // put a toc restoring instruction.
5981 // Unless we know we won't be returning.
5982 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
5983 can_plt_call
= true;
5987 // This is not an error in one special case: A self
5988 // call. It isn't possible to cheaply verify we have
5989 // such a call so just check for a call to the same
5992 Address code
= value
;
5993 if (gsym
->source() == Symbol::FROM_OBJECT
5994 && gsym
->object() == object
)
5996 Address addend
= rela
.get_r_addend();
5997 unsigned int dest_shndx
;
5998 Address opdent
= psymval
->value(object
, addend
);
5999 code
= target
->symval_for_branch(opdent
, gsym
, object
,
6002 if (dest_shndx
== 0)
6003 dest_shndx
= gsym
->shndx(&is_ordinary
);
6004 ok
= dest_shndx
== relinfo
->data_shndx
;
6008 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6009 _("call lacks nop, can't restore toc; "
6010 "recompile with -fPIC"));
6016 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6017 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
6018 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
6019 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
6021 // First instruction of a global dynamic sequence, arg setup insn.
6022 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6023 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6024 enum Got_type got_type
= GOT_TYPE_STANDARD
;
6025 if (tls_type
== tls::TLSOPT_NONE
)
6026 got_type
= GOT_TYPE_TLSGD
;
6027 else if (tls_type
== tls::TLSOPT_TO_IE
)
6028 got_type
= GOT_TYPE_TPREL
;
6029 if (got_type
!= GOT_TYPE_STANDARD
)
6033 gold_assert(gsym
->has_got_offset(got_type
));
6034 value
= gsym
->got_offset(got_type
);
6038 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6039 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
6040 value
= object
->local_got_offset(r_sym
, got_type
);
6042 value
-= target
->got_section()->got_base_offset(object
);
6044 if (tls_type
== tls::TLSOPT_TO_IE
)
6046 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6047 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
6049 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6050 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6051 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
6053 insn
|= 32 << 26; // lwz
6055 insn
|= 58 << 26; // ld
6056 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6058 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
6059 - elfcpp::R_POWERPC_GOT_TLSGD16
);
6061 else if (tls_type
== tls::TLSOPT_TO_LE
)
6063 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6064 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
6066 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6067 Insn insn
= addis_3_13
;
6070 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6071 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6072 value
= psymval
->value(object
, rela
.get_r_addend());
6076 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6078 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6079 r_type
= elfcpp::R_POWERPC_NONE
;
6083 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
6084 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
6085 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
6086 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
6088 // First instruction of a local dynamic sequence, arg setup insn.
6089 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6090 if (tls_type
== tls::TLSOPT_NONE
)
6092 value
= target
->tlsld_got_offset();
6093 value
-= target
->got_section()->got_base_offset(object
);
6097 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
6098 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
6099 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
6101 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6102 Insn insn
= addis_3_13
;
6105 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6106 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6111 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6113 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6114 r_type
= elfcpp::R_POWERPC_NONE
;
6118 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
6119 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
6120 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
6121 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
6123 // Accesses relative to a local dynamic sequence address,
6124 // no optimisation here.
6127 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
6128 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
6132 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6133 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
6134 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
6136 value
-= target
->got_section()->got_base_offset(object
);
6138 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
6139 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
6140 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
6141 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
6143 // First instruction of initial exec sequence.
6144 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6145 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
6146 if (tls_type
== tls::TLSOPT_NONE
)
6150 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
6151 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
6155 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6156 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
6157 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
6159 value
-= target
->got_section()->got_base_offset(object
);
6163 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
6164 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
6165 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
6167 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6168 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6169 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
6174 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6175 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6176 value
= psymval
->value(object
, rela
.get_r_addend());
6180 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6182 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6183 r_type
= elfcpp::R_POWERPC_NONE
;
6187 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6188 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6190 // Second instruction of a global dynamic sequence,
6191 // the __tls_get_addr call
6192 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
6193 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6194 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6195 if (tls_type
!= tls::TLSOPT_NONE
)
6197 if (tls_type
== tls::TLSOPT_TO_IE
)
6199 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6200 Insn insn
= add_3_3_13
;
6203 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6204 r_type
= elfcpp::R_POWERPC_NONE
;
6208 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6209 Insn insn
= addi_3_3
;
6210 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6211 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6212 view
+= 2 * big_endian
;
6213 value
= psymval
->value(object
, rela
.get_r_addend());
6215 this->skip_next_tls_get_addr_call();
6218 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6219 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6221 // Second instruction of a local dynamic sequence,
6222 // the __tls_get_addr call
6223 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
6224 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6225 if (tls_type
== tls::TLSOPT_TO_LE
)
6227 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6228 Insn insn
= addi_3_3
;
6229 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6230 this->skip_next_tls_get_addr_call();
6231 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6232 view
+= 2 * big_endian
;
6236 else if (r_type
== elfcpp::R_POWERPC_TLS
)
6238 // Second instruction of an initial exec sequence
6239 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6240 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
6241 if (tls_type
== tls::TLSOPT_TO_LE
)
6243 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6244 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6245 unsigned int reg
= size
== 32 ? 2 : 13;
6246 insn
= at_tls_transform(insn
, reg
);
6247 gold_assert(insn
!= 0);
6248 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6249 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6250 view
+= 2 * big_endian
;
6251 value
= psymval
->value(object
, rela
.get_r_addend());
6254 else if (!has_plt_value
)
6257 unsigned int dest_shndx
;
6258 if (r_type
!= elfcpp::R_PPC_PLTREL24
)
6259 addend
= rela
.get_r_addend();
6260 value
= psymval
->value(object
, addend
);
6261 if (size
== 64 && is_branch_reloc(r_type
))
6262 value
= target
->symval_for_branch(value
, gsym
, object
, &dest_shndx
);
6263 unsigned int max_branch_offset
= 0;
6264 if (r_type
== elfcpp::R_POWERPC_REL24
6265 || r_type
== elfcpp::R_PPC_PLTREL24
6266 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
6267 max_branch_offset
= 1 << 25;
6268 else if (r_type
== elfcpp::R_POWERPC_REL14
6269 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
6270 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
6271 max_branch_offset
= 1 << 15;
6272 if (max_branch_offset
!= 0
6273 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
6275 Stub_table
<size
, big_endian
>* stub_table
6276 = object
->stub_table(relinfo
->data_shndx
);
6277 gold_assert(stub_table
!= NULL
);
6278 Address off
= stub_table
->find_long_branch_entry(object
, value
);
6279 if (off
!= invalid_address
)
6280 value
= stub_table
->stub_address() + stub_table
->plt_size() + off
;
6286 case elfcpp::R_PPC64_REL64
:
6287 case elfcpp::R_POWERPC_REL32
:
6288 case elfcpp::R_POWERPC_REL24
:
6289 case elfcpp::R_PPC_PLTREL24
:
6290 case elfcpp::R_PPC_LOCAL24PC
:
6291 case elfcpp::R_POWERPC_REL16
:
6292 case elfcpp::R_POWERPC_REL16_LO
:
6293 case elfcpp::R_POWERPC_REL16_HI
:
6294 case elfcpp::R_POWERPC_REL16_HA
:
6295 case elfcpp::R_POWERPC_REL14
:
6296 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6297 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6301 case elfcpp::R_PPC64_TOC16
:
6302 case elfcpp::R_PPC64_TOC16_LO
:
6303 case elfcpp::R_PPC64_TOC16_HI
:
6304 case elfcpp::R_PPC64_TOC16_HA
:
6305 case elfcpp::R_PPC64_TOC16_DS
:
6306 case elfcpp::R_PPC64_TOC16_LO_DS
:
6307 // Subtract the TOC base address.
6308 value
-= (target
->got_section()->output_section()->address()
6309 + object
->toc_base_offset());
6312 case elfcpp::R_POWERPC_SECTOFF
:
6313 case elfcpp::R_POWERPC_SECTOFF_LO
:
6314 case elfcpp::R_POWERPC_SECTOFF_HI
:
6315 case elfcpp::R_POWERPC_SECTOFF_HA
:
6316 case elfcpp::R_PPC64_SECTOFF_DS
:
6317 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6319 value
-= os
->address();
6322 case elfcpp::R_PPC64_TPREL16_DS
:
6323 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6325 // R_PPC_TLSGD and R_PPC_TLSLD
6327 case elfcpp::R_POWERPC_TPREL16
:
6328 case elfcpp::R_POWERPC_TPREL16_LO
:
6329 case elfcpp::R_POWERPC_TPREL16_HI
:
6330 case elfcpp::R_POWERPC_TPREL16_HA
:
6331 case elfcpp::R_POWERPC_TPREL
:
6332 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6333 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6334 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6335 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6336 // tls symbol values are relative to tls_segment()->vaddr()
6340 case elfcpp::R_PPC64_DTPREL16_DS
:
6341 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6342 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6343 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6344 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6345 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6347 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
6348 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
6350 case elfcpp::R_POWERPC_DTPREL16
:
6351 case elfcpp::R_POWERPC_DTPREL16_LO
:
6352 case elfcpp::R_POWERPC_DTPREL16_HI
:
6353 case elfcpp::R_POWERPC_DTPREL16_HA
:
6354 case elfcpp::R_POWERPC_DTPREL
:
6355 // tls symbol values are relative to tls_segment()->vaddr()
6356 value
-= dtp_offset
;
6363 Insn branch_bit
= 0;
6366 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6367 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6368 branch_bit
= 1 << 21;
6369 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6370 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6372 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6373 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6376 if (this->is_isa_v2
)
6378 // Set 'a' bit. This is 0b00010 in BO field for branch
6379 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
6380 // for branch on CTR insns (BO == 1a00t or 1a01t).
6381 if ((insn
& (0x14 << 21)) == (0x04 << 21))
6383 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
6390 // Invert 'y' bit if not the default.
6391 if (static_cast<Signed_address
>(value
) < 0)
6394 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6404 // Multi-instruction sequences that access the TOC can be
6405 // optimized, eg. addis ra,r2,0; addi rb,ra,x;
6406 // to nop; addi rb,r2,x;
6412 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6413 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6414 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6415 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6416 case elfcpp::R_POWERPC_GOT16_HA
:
6417 case elfcpp::R_PPC64_TOC16_HA
:
6418 if (parameters
->options().toc_optimize())
6420 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6421 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6422 if ((insn
& ((0x3f << 26) | 0x1f << 16))
6423 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
6424 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6425 _("toc optimization is not supported "
6426 "for %#08x instruction"), insn
);
6427 else if (value
+ 0x8000 < 0x10000)
6429 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
6435 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6436 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6437 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6438 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6439 case elfcpp::R_POWERPC_GOT16_LO
:
6440 case elfcpp::R_PPC64_GOT16_LO_DS
:
6441 case elfcpp::R_PPC64_TOC16_LO
:
6442 case elfcpp::R_PPC64_TOC16_LO_DS
:
6443 if (parameters
->options().toc_optimize())
6445 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6446 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6447 if (!ok_lo_toc_insn(insn
))
6448 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6449 _("toc optimization is not supported "
6450 "for %#08x instruction"), insn
);
6451 else if (value
+ 0x8000 < 0x10000)
6453 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
6455 // Transform addic to addi when we change reg.
6456 insn
&= ~((0x3f << 26) | (0x1f << 16));
6457 insn
|= (14u << 26) | (2 << 16);
6461 insn
&= ~(0x1f << 16);
6464 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6471 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
6474 case elfcpp::R_POWERPC_ADDR32
:
6475 case elfcpp::R_POWERPC_UADDR32
:
6477 overflow
= Reloc::CHECK_BITFIELD
;
6480 case elfcpp::R_POWERPC_REL32
:
6482 overflow
= Reloc::CHECK_SIGNED
;
6485 case elfcpp::R_POWERPC_ADDR24
:
6486 case elfcpp::R_POWERPC_ADDR16
:
6487 case elfcpp::R_POWERPC_UADDR16
:
6488 case elfcpp::R_PPC64_ADDR16_DS
:
6489 case elfcpp::R_POWERPC_ADDR14
:
6490 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6491 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6492 overflow
= Reloc::CHECK_BITFIELD
;
6495 case elfcpp::R_POWERPC_REL24
:
6496 case elfcpp::R_PPC_PLTREL24
:
6497 case elfcpp::R_PPC_LOCAL24PC
:
6498 case elfcpp::R_POWERPC_REL16
:
6499 case elfcpp::R_PPC64_TOC16
:
6500 case elfcpp::R_POWERPC_GOT16
:
6501 case elfcpp::R_POWERPC_SECTOFF
:
6502 case elfcpp::R_POWERPC_TPREL16
:
6503 case elfcpp::R_POWERPC_DTPREL16
:
6504 case elfcpp::R_PPC64_TPREL16_DS
:
6505 case elfcpp::R_PPC64_DTPREL16_DS
:
6506 case elfcpp::R_PPC64_TOC16_DS
:
6507 case elfcpp::R_PPC64_GOT16_DS
:
6508 case elfcpp::R_PPC64_SECTOFF_DS
:
6509 case elfcpp::R_POWERPC_REL14
:
6510 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6511 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6512 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6513 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6514 case elfcpp::R_POWERPC_GOT_TPREL16
:
6515 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6516 overflow
= Reloc::CHECK_SIGNED
;
6520 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
6521 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
6524 case elfcpp::R_POWERPC_NONE
:
6525 case elfcpp::R_POWERPC_TLS
:
6526 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
6527 case elfcpp::R_POWERPC_GNU_VTENTRY
:
6528 case elfcpp::R_PPC_EMB_MRKREF
:
6531 case elfcpp::R_PPC64_ADDR64
:
6532 case elfcpp::R_PPC64_REL64
:
6533 case elfcpp::R_PPC64_TOC
:
6534 Reloc::addr64(view
, value
);
6537 case elfcpp::R_POWERPC_TPREL
:
6538 case elfcpp::R_POWERPC_DTPREL
:
6540 Reloc::addr64(view
, value
);
6542 status
= Reloc::addr32(view
, value
, overflow
);
6545 case elfcpp::R_PPC64_UADDR64
:
6546 Reloc::addr64_u(view
, value
);
6549 case elfcpp::R_POWERPC_ADDR32
:
6550 status
= Reloc::addr32(view
, value
, overflow
);
6553 case elfcpp::R_POWERPC_REL32
:
6554 case elfcpp::R_POWERPC_UADDR32
:
6555 status
= Reloc::addr32_u(view
, value
, overflow
);
6558 case elfcpp::R_POWERPC_ADDR24
:
6559 case elfcpp::R_POWERPC_REL24
:
6560 case elfcpp::R_PPC_PLTREL24
:
6561 case elfcpp::R_PPC_LOCAL24PC
:
6562 status
= Reloc::addr24(view
, value
, overflow
);
6565 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6566 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6569 status
= Reloc::addr16_ds(view
, value
, overflow
);
6572 case elfcpp::R_POWERPC_ADDR16
:
6573 case elfcpp::R_POWERPC_REL16
:
6574 case elfcpp::R_PPC64_TOC16
:
6575 case elfcpp::R_POWERPC_GOT16
:
6576 case elfcpp::R_POWERPC_SECTOFF
:
6577 case elfcpp::R_POWERPC_TPREL16
:
6578 case elfcpp::R_POWERPC_DTPREL16
:
6579 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6580 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6581 case elfcpp::R_POWERPC_GOT_TPREL16
:
6582 case elfcpp::R_POWERPC_ADDR16_LO
:
6583 case elfcpp::R_POWERPC_REL16_LO
:
6584 case elfcpp::R_PPC64_TOC16_LO
:
6585 case elfcpp::R_POWERPC_GOT16_LO
:
6586 case elfcpp::R_POWERPC_SECTOFF_LO
:
6587 case elfcpp::R_POWERPC_TPREL16_LO
:
6588 case elfcpp::R_POWERPC_DTPREL16_LO
:
6589 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6590 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6591 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6592 status
= Reloc::addr16(view
, value
, overflow
);
6595 case elfcpp::R_POWERPC_UADDR16
:
6596 status
= Reloc::addr16_u(view
, value
, overflow
);
6599 case elfcpp::R_POWERPC_ADDR16_HI
:
6600 case elfcpp::R_POWERPC_REL16_HI
:
6601 case elfcpp::R_PPC64_TOC16_HI
:
6602 case elfcpp::R_POWERPC_GOT16_HI
:
6603 case elfcpp::R_POWERPC_SECTOFF_HI
:
6604 case elfcpp::R_POWERPC_TPREL16_HI
:
6605 case elfcpp::R_POWERPC_DTPREL16_HI
:
6606 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6607 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6608 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6609 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6610 Reloc::addr16_hi(view
, value
);
6613 case elfcpp::R_POWERPC_ADDR16_HA
:
6614 case elfcpp::R_POWERPC_REL16_HA
:
6615 case elfcpp::R_PPC64_TOC16_HA
:
6616 case elfcpp::R_POWERPC_GOT16_HA
:
6617 case elfcpp::R_POWERPC_SECTOFF_HA
:
6618 case elfcpp::R_POWERPC_TPREL16_HA
:
6619 case elfcpp::R_POWERPC_DTPREL16_HA
:
6620 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6621 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6622 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6623 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6624 Reloc::addr16_ha(view
, value
);
6627 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6629 // R_PPC_EMB_NADDR16_LO
6631 case elfcpp::R_PPC64_ADDR16_HIGHER
:
6632 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6633 Reloc::addr16_hi2(view
, value
);
6636 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6638 // R_PPC_EMB_NADDR16_HI
6640 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
6641 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6642 Reloc::addr16_ha2(view
, value
);
6645 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6647 // R_PPC_EMB_NADDR16_HA
6649 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
6650 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6651 Reloc::addr16_hi3(view
, value
);
6654 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6658 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
6659 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6660 Reloc::addr16_ha3(view
, value
);
6663 case elfcpp::R_PPC64_DTPREL16_DS
:
6664 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6666 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
6668 case elfcpp::R_PPC64_TPREL16_DS
:
6669 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6671 // R_PPC_TLSGD, R_PPC_TLSLD
6673 case elfcpp::R_PPC64_ADDR16_DS
:
6674 case elfcpp::R_PPC64_ADDR16_LO_DS
:
6675 case elfcpp::R_PPC64_TOC16_DS
:
6676 case elfcpp::R_PPC64_TOC16_LO_DS
:
6677 case elfcpp::R_PPC64_GOT16_DS
:
6678 case elfcpp::R_PPC64_GOT16_LO_DS
:
6679 case elfcpp::R_PPC64_SECTOFF_DS
:
6680 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6681 status
= Reloc::addr16_ds(view
, value
, overflow
);
6684 case elfcpp::R_POWERPC_ADDR14
:
6685 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6686 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6687 case elfcpp::R_POWERPC_REL14
:
6688 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6689 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6690 status
= Reloc::addr14(view
, value
, overflow
);
6693 case elfcpp::R_POWERPC_COPY
:
6694 case elfcpp::R_POWERPC_GLOB_DAT
:
6695 case elfcpp::R_POWERPC_JMP_SLOT
:
6696 case elfcpp::R_POWERPC_RELATIVE
:
6697 case elfcpp::R_POWERPC_DTPMOD
:
6698 case elfcpp::R_PPC64_JMP_IREL
:
6699 case elfcpp::R_POWERPC_IRELATIVE
:
6700 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6701 _("unexpected reloc %u in object file"),
6705 case elfcpp::R_PPC_EMB_SDA21
:
6710 // R_PPC64_TOCSAVE. For the time being this can be ignored.
6714 case elfcpp::R_PPC_EMB_SDA2I16
:
6715 case elfcpp::R_PPC_EMB_SDA2REL
:
6718 // R_PPC64_TLSGD, R_PPC64_TLSLD
6721 case elfcpp::R_POWERPC_PLT32
:
6722 case elfcpp::R_POWERPC_PLTREL32
:
6723 case elfcpp::R_POWERPC_PLT16_LO
:
6724 case elfcpp::R_POWERPC_PLT16_HI
:
6725 case elfcpp::R_POWERPC_PLT16_HA
:
6726 case elfcpp::R_PPC_SDAREL16
:
6727 case elfcpp::R_POWERPC_ADDR30
:
6728 case elfcpp::R_PPC64_PLT64
:
6729 case elfcpp::R_PPC64_PLTREL64
:
6730 case elfcpp::R_PPC64_PLTGOT16
:
6731 case elfcpp::R_PPC64_PLTGOT16_LO
:
6732 case elfcpp::R_PPC64_PLTGOT16_HI
:
6733 case elfcpp::R_PPC64_PLTGOT16_HA
:
6734 case elfcpp::R_PPC64_PLT16_LO_DS
:
6735 case elfcpp::R_PPC64_PLTGOT16_DS
:
6736 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
6737 case elfcpp::R_PPC_EMB_RELSEC16
:
6738 case elfcpp::R_PPC_EMB_RELST_LO
:
6739 case elfcpp::R_PPC_EMB_RELST_HI
:
6740 case elfcpp::R_PPC_EMB_RELST_HA
:
6741 case elfcpp::R_PPC_EMB_BIT_FLD
:
6742 case elfcpp::R_PPC_EMB_RELSDA
:
6743 case elfcpp::R_PPC_TOC16
:
6746 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6747 _("unsupported reloc %u"),
6751 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
)
6752 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6753 _("relocation overflow"));
6758 // Relocate section data.
6760 template<int size
, bool big_endian
>
6762 Target_powerpc
<size
, big_endian
>::relocate_section(
6763 const Relocate_info
<size
, big_endian
>* relinfo
,
6764 unsigned int sh_type
,
6765 const unsigned char* prelocs
,
6767 Output_section
* output_section
,
6768 bool needs_special_offset_handling
,
6769 unsigned char* view
,
6771 section_size_type view_size
,
6772 const Reloc_symbol_changes
* reloc_symbol_changes
)
6774 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
6775 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
6776 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
6777 Powerpc_comdat_behavior
;
6779 gold_assert(sh_type
== elfcpp::SHT_RELA
);
6781 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
6782 Powerpc_relocate
, Powerpc_comdat_behavior
>(
6788 needs_special_offset_handling
,
6792 reloc_symbol_changes
);
6795 class Powerpc_scan_relocatable_reloc
6798 // Return the strategy to use for a local symbol which is not a
6799 // section symbol, given the relocation type.
6800 inline Relocatable_relocs::Reloc_strategy
6801 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
6803 if (r_type
== 0 && r_sym
== 0)
6804 return Relocatable_relocs::RELOC_DISCARD
;
6805 return Relocatable_relocs::RELOC_COPY
;
6808 // Return the strategy to use for a local symbol which is a section
6809 // symbol, given the relocation type.
6810 inline Relocatable_relocs::Reloc_strategy
6811 local_section_strategy(unsigned int, Relobj
*)
6813 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
6816 // Return the strategy to use for a global symbol, given the
6817 // relocation type, the object, and the symbol index.
6818 inline Relocatable_relocs::Reloc_strategy
6819 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
6821 if (r_type
== elfcpp::R_PPC_PLTREL24
)
6822 return Relocatable_relocs::RELOC_SPECIAL
;
6823 return Relocatable_relocs::RELOC_COPY
;
6827 // Scan the relocs during a relocatable link.
6829 template<int size
, bool big_endian
>
6831 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
6832 Symbol_table
* symtab
,
6834 Sized_relobj_file
<size
, big_endian
>* object
,
6835 unsigned int data_shndx
,
6836 unsigned int sh_type
,
6837 const unsigned char* prelocs
,
6839 Output_section
* output_section
,
6840 bool needs_special_offset_handling
,
6841 size_t local_symbol_count
,
6842 const unsigned char* plocal_symbols
,
6843 Relocatable_relocs
* rr
)
6845 gold_assert(sh_type
== elfcpp::SHT_RELA
);
6847 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
6848 Powerpc_scan_relocatable_reloc
>(
6856 needs_special_offset_handling
,
6862 // Emit relocations for a section.
6863 // This is a modified version of the function by the same name in
6864 // target-reloc.h. Using relocate_special_relocatable for
6865 // R_PPC_PLTREL24 would require duplication of the entire body of the
6866 // loop, so we may as well duplicate the whole thing.
6868 template<int size
, bool big_endian
>
6870 Target_powerpc
<size
, big_endian
>::relocate_relocs(
6871 const Relocate_info
<size
, big_endian
>* relinfo
,
6872 unsigned int sh_type
,
6873 const unsigned char* prelocs
,
6875 Output_section
* output_section
,
6876 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
6877 const Relocatable_relocs
* rr
,
6879 Address view_address
,
6881 unsigned char* reloc_view
,
6882 section_size_type reloc_view_size
)
6884 gold_assert(sh_type
== elfcpp::SHT_RELA
);
6886 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
6888 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc_write
6890 const int reloc_size
6891 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
6893 Powerpc_relobj
<size
, big_endian
>* const object
6894 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
6895 const unsigned int local_count
= object
->local_symbol_count();
6896 unsigned int got2_shndx
= object
->got2_shndx();
6897 Address got2_addend
= 0;
6898 if (got2_shndx
!= 0)
6900 got2_addend
= object
->get_output_section_offset(got2_shndx
);
6901 gold_assert(got2_addend
!= invalid_address
);
6904 unsigned char* pwrite
= reloc_view
;
6905 bool zap_next
= false;
6906 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
6908 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
6909 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
6912 Reltype
reloc(prelocs
);
6913 Reltype_write
reloc_write(pwrite
);
6915 Address offset
= reloc
.get_r_offset();
6916 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
6917 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
6918 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
6919 const unsigned int orig_r_sym
= r_sym
;
6920 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
6921 = reloc
.get_r_addend();
6922 const Symbol
* gsym
= NULL
;
6926 // We could arrange to discard these and other relocs for
6927 // tls optimised sequences in the strategy methods, but for
6928 // now do as BFD ld does.
6929 r_type
= elfcpp::R_POWERPC_NONE
;
6933 // Get the new symbol index.
6934 if (r_sym
< local_count
)
6938 case Relocatable_relocs::RELOC_COPY
:
6939 case Relocatable_relocs::RELOC_SPECIAL
:
6942 r_sym
= object
->symtab_index(r_sym
);
6943 gold_assert(r_sym
!= -1U);
6947 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
6949 // We are adjusting a section symbol. We need to find
6950 // the symbol table index of the section symbol for
6951 // the output section corresponding to input section
6952 // in which this symbol is defined.
6953 gold_assert(r_sym
< local_count
);
6955 unsigned int shndx
=
6956 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
6957 gold_assert(is_ordinary
);
6958 Output_section
* os
= object
->output_section(shndx
);
6959 gold_assert(os
!= NULL
);
6960 gold_assert(os
->needs_symtab_index());
6961 r_sym
= os
->symtab_index();
6971 gsym
= object
->global_symbol(r_sym
);
6972 gold_assert(gsym
!= NULL
);
6973 if (gsym
->is_forwarder())
6974 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
6976 gold_assert(gsym
->has_symtab_index());
6977 r_sym
= gsym
->symtab_index();
6980 // Get the new offset--the location in the output section where
6981 // this relocation should be applied.
6982 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
6983 offset
+= offset_in_output_section
;
6986 section_offset_type sot_offset
=
6987 convert_types
<section_offset_type
, Address
>(offset
);
6988 section_offset_type new_sot_offset
=
6989 output_section
->output_offset(object
, relinfo
->data_shndx
,
6991 gold_assert(new_sot_offset
!= -1);
6992 offset
= new_sot_offset
;
6995 // In an object file, r_offset is an offset within the section.
6996 // In an executable or dynamic object, generated by
6997 // --emit-relocs, r_offset is an absolute address.
6998 if (!parameters
->options().relocatable())
7000 offset
+= view_address
;
7001 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
7002 offset
-= offset_in_output_section
;
7005 // Handle the reloc addend based on the strategy.
7006 if (strategy
== Relocatable_relocs::RELOC_COPY
)
7008 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
7010 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
7011 addend
= psymval
->value(object
, addend
);
7013 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
7015 if (addend
>= 32768)
7016 addend
+= got2_addend
;
7021 if (!parameters
->options().relocatable())
7023 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7024 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
7025 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
7026 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
7028 // First instruction of a global dynamic sequence,
7030 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7031 switch (this->optimize_tls_gd(final
))
7033 case tls::TLSOPT_TO_IE
:
7034 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
7035 - elfcpp::R_POWERPC_GOT_TLSGD16
);
7037 case tls::TLSOPT_TO_LE
:
7038 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7039 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
7040 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7043 r_type
= elfcpp::R_POWERPC_NONE
;
7044 offset
-= 2 * big_endian
;
7051 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7052 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
7053 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
7054 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
7056 // First instruction of a local dynamic sequence,
7058 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
7060 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7061 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
7063 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7064 const Output_section
* os
= relinfo
->layout
->tls_segment()
7066 gold_assert(os
!= NULL
);
7067 gold_assert(os
->needs_symtab_index());
7068 r_sym
= os
->symtab_index();
7069 addend
= dtp_offset
;
7073 r_type
= elfcpp::R_POWERPC_NONE
;
7074 offset
-= 2 * big_endian
;
7078 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
7079 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
7080 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
7081 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
7083 // First instruction of initial exec sequence.
7084 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7085 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
7087 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
7088 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
7089 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7092 r_type
= elfcpp::R_POWERPC_NONE
;
7093 offset
-= 2 * big_endian
;
7097 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
7098 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
7100 // Second instruction of a global dynamic sequence,
7101 // the __tls_get_addr call
7102 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7103 switch (this->optimize_tls_gd(final
))
7105 case tls::TLSOPT_TO_IE
:
7106 r_type
= elfcpp::R_POWERPC_NONE
;
7109 case tls::TLSOPT_TO_LE
:
7110 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7111 offset
+= 2 * big_endian
;
7118 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
7119 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
7121 // Second instruction of a local dynamic sequence,
7122 // the __tls_get_addr call
7123 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
7125 const Output_section
* os
= relinfo
->layout
->tls_segment()
7127 gold_assert(os
!= NULL
);
7128 gold_assert(os
->needs_symtab_index());
7129 r_sym
= os
->symtab_index();
7130 addend
= dtp_offset
;
7131 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7132 offset
+= 2 * big_endian
;
7136 else if (r_type
== elfcpp::R_POWERPC_TLS
)
7138 // Second instruction of an initial exec sequence
7139 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7140 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
7142 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7143 offset
+= 2 * big_endian
;
7148 reloc_write
.put_r_offset(offset
);
7149 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
7150 reloc_write
.put_r_addend(addend
);
7152 pwrite
+= reloc_size
;
7155 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
7156 == reloc_view_size
);
7159 // Return the value to use for a dynamic symbol which requires special
7160 // treatment. This is how we support equality comparisons of function
7161 // pointers across shared library boundaries, as described in the
7162 // processor specific ABI supplement.
7164 template<int size
, bool big_endian
>
7166 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
7170 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
7171 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7172 p
!= this->stub_tables_
.end();
7175 Address off
= (*p
)->find_plt_call_entry(gsym
);
7176 if (off
!= invalid_address
)
7177 return (*p
)->stub_address() + off
;
7183 // Return the PLT address to use for a local symbol.
7184 template<int size
, bool big_endian
>
7186 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
7187 const Relobj
* object
,
7188 unsigned int symndx
) const
7192 const Sized_relobj
<size
, big_endian
>* relobj
7193 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
7194 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7195 p
!= this->stub_tables_
.end();
7198 Address off
= (*p
)->find_plt_call_entry(relobj
->sized_relobj(),
7200 if (off
!= invalid_address
)
7201 return (*p
)->stub_address() + off
;
7207 // Return the PLT address to use for a global symbol.
7208 template<int size
, bool big_endian
>
7210 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
7211 const Symbol
* gsym
) const
7215 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7216 p
!= this->stub_tables_
.end();
7219 Address off
= (*p
)->find_plt_call_entry(gsym
);
7220 if (off
!= invalid_address
)
7221 return (*p
)->stub_address() + off
;
7227 // Return the offset to use for the GOT_INDX'th got entry which is
7228 // for a local tls symbol specified by OBJECT, SYMNDX.
7229 template<int size
, bool big_endian
>
7231 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
7232 const Relobj
* object
,
7233 unsigned int symndx
,
7234 unsigned int got_indx
) const
7236 const Powerpc_relobj
<size
, big_endian
>* ppc_object
7237 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
7238 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
7240 for (Got_type got_type
= GOT_TYPE_TLSGD
;
7241 got_type
<= GOT_TYPE_TPREL
;
7242 got_type
= Got_type(got_type
+ 1))
7243 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
7245 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
7246 if (got_type
== GOT_TYPE_TLSGD
)
7248 if (off
== got_indx
* (size
/ 8))
7250 if (got_type
== GOT_TYPE_TPREL
)
7260 // Return the offset to use for the GOT_INDX'th got entry which is
7261 // for global tls symbol GSYM.
7262 template<int size
, bool big_endian
>
7264 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
7266 unsigned int got_indx
) const
7268 if (gsym
->type() == elfcpp::STT_TLS
)
7270 for (Got_type got_type
= GOT_TYPE_TLSGD
;
7271 got_type
<= GOT_TYPE_TPREL
;
7272 got_type
= Got_type(got_type
+ 1))
7273 if (gsym
->has_got_offset(got_type
))
7275 unsigned int off
= gsym
->got_offset(got_type
);
7276 if (got_type
== GOT_TYPE_TLSGD
)
7278 if (off
== got_indx
* (size
/ 8))
7280 if (got_type
== GOT_TYPE_TPREL
)
7290 // The selector for powerpc object files.
7292 template<int size
, bool big_endian
>
7293 class Target_selector_powerpc
: public Target_selector
7296 Target_selector_powerpc()
7297 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
7300 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
7301 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
7303 ? (big_endian
? "elf64ppc" : "elf64lppc")
7304 : (big_endian
? "elf32ppc" : "elf32lppc")))
7308 do_instantiate_target()
7309 { return new Target_powerpc
<size
, big_endian
>(); }
7312 Target_selector_powerpc
<32, true> target_selector_ppc32
;
7313 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
7314 Target_selector_powerpc
<64, true> target_selector_ppc64
;
7315 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
7317 // Instantiate these constants for -O0
7318 template<int size
, bool big_endian
>
7319 const int Output_data_glink
<size
, big_endian
>::pltresolve_size
;
7320 template<int size
, bool big_endian
>
7321 const typename Stub_table
<size
, big_endian
>::Address
7322 Stub_table
<size
, big_endian
>::invalid_address
;
7323 template<int size
, bool big_endian
>
7324 const typename Target_powerpc
<size
, big_endian
>::Address
7325 Target_powerpc
<size
, big_endian
>::invalid_address
;
7327 } // End anonymous namespace.