1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
30 #include "parameters.h"
37 #include "copy-relocs.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
50 template<int size
, bool big_endian
>
51 class Output_data_plt_powerpc
;
53 template<int size
, bool big_endian
>
54 class Output_data_brlt_powerpc
;
56 template<int size
, bool big_endian
>
57 class Output_data_got_powerpc
;
59 template<int size
, bool big_endian
>
60 class Output_data_glink
;
62 template<int size
, bool big_endian
>
66 is_branch_reloc(unsigned int r_type
);
68 template<int size
, bool big_endian
>
69 class Powerpc_relobj
: public Sized_relobj_file
<size
, big_endian
>
72 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
73 typedef Unordered_set
<Section_id
, Section_id_hash
> Section_refs
;
74 typedef Unordered_map
<Address
, Section_refs
> Access_from
;
76 Powerpc_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
77 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
78 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
79 special_(0), has_small_toc_reloc_(false), opd_valid_(false),
80 opd_ent_(), access_from_map_(), has14_(), stub_table_()
86 // The .got2 section shndx.
91 return this->special_
;
96 // The .opd section shndx.
103 return this->special_
;
106 // Init OPD entry arrays.
108 init_opd(size_t opd_size
)
110 size_t count
= this->opd_ent_ndx(opd_size
);
111 this->opd_ent_
.resize(count
);
114 // Return section and offset of function entry for .opd + R_OFF.
116 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
118 size_t ndx
= this->opd_ent_ndx(r_off
);
119 gold_assert(ndx
< this->opd_ent_
.size());
120 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
122 *value
= this->opd_ent_
[ndx
].off
;
123 return this->opd_ent_
[ndx
].shndx
;
126 // Set section and offset of function entry for .opd + R_OFF.
128 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
130 size_t ndx
= this->opd_ent_ndx(r_off
);
131 gold_assert(ndx
< this->opd_ent_
.size());
132 this->opd_ent_
[ndx
].shndx
= shndx
;
133 this->opd_ent_
[ndx
].off
= value
;
136 // Return discard flag for .opd + R_OFF.
138 get_opd_discard(Address r_off
) const
140 size_t ndx
= this->opd_ent_ndx(r_off
);
141 gold_assert(ndx
< this->opd_ent_
.size());
142 return this->opd_ent_
[ndx
].discard
;
145 // Set discard flag for .opd + R_OFF.
147 set_opd_discard(Address r_off
)
149 size_t ndx
= this->opd_ent_ndx(r_off
);
150 gold_assert(ndx
< this->opd_ent_
.size());
151 this->opd_ent_
[ndx
].discard
= true;
156 { return this->opd_valid_
; }
160 { this->opd_valid_
= true; }
162 // Examine .rela.opd to build info about function entry points.
164 scan_opd_relocs(size_t reloc_count
,
165 const unsigned char* prelocs
,
166 const unsigned char* plocal_syms
);
168 // Perform the Sized_relobj_file method, then set up opd info from
171 do_read_relocs(Read_relocs_data
*);
174 do_find_special_sections(Read_symbols_data
* sd
);
176 // Adjust this local symbol value. Return false if the symbol
177 // should be discarded from the output file.
179 do_adjust_local_symbol(Symbol_value
<size
>* lv
) const
181 if (size
== 64 && this->opd_shndx() != 0)
184 if (lv
->input_shndx(&is_ordinary
) != this->opd_shndx())
186 if (this->get_opd_discard(lv
->input_value()))
194 { return &this->access_from_map_
; }
196 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
197 // section at DST_OFF.
199 add_reference(Object
* src_obj
,
200 unsigned int src_indx
,
201 typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
203 Section_id
src_id(src_obj
, src_indx
);
204 this->access_from_map_
[dst_off
].insert(src_id
);
207 // Add a reference to the code section specified by the .opd entry
210 add_gc_mark(typename
elfcpp::Elf_types
<size
>::Elf_Addr dst_off
)
212 size_t ndx
= this->opd_ent_ndx(dst_off
);
213 if (ndx
>= this->opd_ent_
.size())
214 this->opd_ent_
.resize(ndx
+ 1);
215 this->opd_ent_
[ndx
].gc_mark
= true;
219 process_gc_mark(Symbol_table
* symtab
)
221 for (size_t i
= 0; i
< this->opd_ent_
.size(); i
++)
222 if (this->opd_ent_
[i
].gc_mark
)
224 unsigned int shndx
= this->opd_ent_
[i
].shndx
;
225 symtab
->gc()->worklist().push(Section_id(this, shndx
));
229 // Return offset in output GOT section that this object will use
230 // as a TOC pointer. Won't be just a constant with multi-toc support.
232 toc_base_offset() const
236 set_has_small_toc_reloc()
237 { has_small_toc_reloc_
= true; }
240 has_small_toc_reloc() const
241 { return has_small_toc_reloc_
; }
244 set_has_14bit_branch(unsigned int shndx
)
246 if (shndx
>= this->has14_
.size())
247 this->has14_
.resize(shndx
+ 1);
248 this->has14_
[shndx
] = true;
252 has_14bit_branch(unsigned int shndx
) const
253 { return shndx
< this->has14_
.size() && this->has14_
[shndx
]; }
256 set_stub_table(unsigned int shndx
, Stub_table
<size
, big_endian
>* stub_table
)
258 if (shndx
>= this->stub_table_
.size())
259 this->stub_table_
.resize(shndx
+ 1);
260 this->stub_table_
[shndx
] = stub_table
;
263 Stub_table
<size
, big_endian
>*
264 stub_table(unsigned int shndx
)
266 if (shndx
< this->stub_table_
.size())
267 return this->stub_table_
[shndx
];
280 // Return index into opd_ent_ array for .opd entry at OFF.
281 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
282 // apart when the language doesn't use the last 8-byte word, the
283 // environment pointer. Thus dividing the entry section offset by
284 // 16 will give an index into opd_ent_ that works for either layout
285 // of .opd. (It leaves some elements of the vector unused when .opd
286 // entries are spaced 24 bytes apart, but we don't know the spacing
287 // until relocations are processed, and in any case it is possible
288 // for an object to have some entries spaced 16 bytes apart and
289 // others 24 bytes apart.)
291 opd_ent_ndx(size_t off
) const
294 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
295 unsigned int special_
;
297 // For 64-bit, whether this object uses small model relocs to access
299 bool has_small_toc_reloc_
;
301 // Set at the start of gc_process_relocs, when we know opd_ent_
302 // vector is valid. The flag could be made atomic and set in
303 // do_read_relocs with memory_order_release and then tested with
304 // memory_order_acquire, potentially resulting in fewer entries in
308 // The first 8-byte word of an OPD entry gives the address of the
309 // entry point of the function. Relocatable object files have a
310 // relocation on this word. The following vector records the
311 // section and offset specified by these relocations.
312 std::vector
<Opd_ent
> opd_ent_
;
314 // References made to this object's .opd section when running
315 // gc_process_relocs for another object, before the opd_ent_ vector
316 // is valid for this object.
317 Access_from access_from_map_
;
319 // Whether input section has a 14-bit branch reloc.
320 std::vector
<bool> has14_
;
322 // The stub table to use for a given input section.
323 std::vector
<Stub_table
<size
, big_endian
>*> stub_table_
;
326 template<int size
, bool big_endian
>
327 class Powerpc_dynobj
: public Sized_dynobj
<size
, big_endian
>
330 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
332 Powerpc_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
333 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
334 : Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
335 opd_shndx_(0), opd_ent_()
341 // Call Sized_dynobj::do_read_symbols to read the symbols then
342 // read .opd from a dynamic object, filling in opd_ent_ vector,
344 do_read_symbols(Read_symbols_data
*);
346 // The .opd section shndx.
350 return this->opd_shndx_
;
353 // The .opd section address.
357 return this->opd_address_
;
360 // Init OPD entry arrays.
362 init_opd(size_t opd_size
)
364 size_t count
= this->opd_ent_ndx(opd_size
);
365 this->opd_ent_
.resize(count
);
368 // Return section and offset of function entry for .opd + R_OFF.
370 get_opd_ent(Address r_off
, Address
* value
= NULL
) const
372 size_t ndx
= this->opd_ent_ndx(r_off
);
373 gold_assert(ndx
< this->opd_ent_
.size());
374 gold_assert(this->opd_ent_
[ndx
].shndx
!= 0);
376 *value
= this->opd_ent_
[ndx
].off
;
377 return this->opd_ent_
[ndx
].shndx
;
380 // Set section and offset of function entry for .opd + R_OFF.
382 set_opd_ent(Address r_off
, unsigned int shndx
, Address value
)
384 size_t ndx
= this->opd_ent_ndx(r_off
);
385 gold_assert(ndx
< this->opd_ent_
.size());
386 this->opd_ent_
[ndx
].shndx
= shndx
;
387 this->opd_ent_
[ndx
].off
= value
;
391 // Used to specify extent of executable sections.
394 Sec_info(Address start_
, Address len_
, unsigned int shndx_
)
395 : start(start_
), len(len_
), shndx(shndx_
)
399 operator<(const Sec_info
& that
) const
400 { return this->start
< that
.start
; }
413 // Return index into opd_ent_ array for .opd entry at OFF.
415 opd_ent_ndx(size_t off
) const
418 // For 64-bit the .opd section shndx and address.
419 unsigned int opd_shndx_
;
420 Address opd_address_
;
422 // The first 8-byte word of an OPD entry gives the address of the
423 // entry point of the function. Records the section and offset
424 // corresponding to the address. Note that in dynamic objects,
425 // offset is *not* relative to the section.
426 std::vector
<Opd_ent
> opd_ent_
;
429 template<int size
, bool big_endian
>
430 class Target_powerpc
: public Sized_target
<size
, big_endian
>
434 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
435 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
436 typedef typename
elfcpp::Elf_types
<size
>::Elf_Swxword Signed_address
;
437 static const Address invalid_address
= static_cast<Address
>(0) - 1;
438 // Offset of tp and dtp pointers from start of TLS block.
439 static const Address tp_offset
= 0x7000;
440 static const Address dtp_offset
= 0x8000;
443 : Sized_target
<size
, big_endian
>(&powerpc_info
),
444 got_(NULL
), plt_(NULL
), iplt_(NULL
), brlt_section_(NULL
),
445 glink_(NULL
), rela_dyn_(NULL
), copy_relocs_(elfcpp::R_POWERPC_COPY
),
446 tlsld_got_offset_(-1U),
447 stub_tables_(), branch_lookup_table_(), branch_info_(),
448 plt_thread_safe_(false)
452 // Process the relocations to determine unreferenced sections for
453 // garbage collection.
455 gc_process_relocs(Symbol_table
* symtab
,
457 Sized_relobj_file
<size
, big_endian
>* object
,
458 unsigned int data_shndx
,
459 unsigned int sh_type
,
460 const unsigned char* prelocs
,
462 Output_section
* output_section
,
463 bool needs_special_offset_handling
,
464 size_t local_symbol_count
,
465 const unsigned char* plocal_symbols
);
467 // Scan the relocations to look for symbol adjustments.
469 scan_relocs(Symbol_table
* symtab
,
471 Sized_relobj_file
<size
, big_endian
>* object
,
472 unsigned int data_shndx
,
473 unsigned int sh_type
,
474 const unsigned char* prelocs
,
476 Output_section
* output_section
,
477 bool needs_special_offset_handling
,
478 size_t local_symbol_count
,
479 const unsigned char* plocal_symbols
);
481 // Map input .toc section to output .got section.
483 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
485 if (size
== 64 && strcmp(name
, ".toc") == 0)
493 // Provide linker defined save/restore functions.
495 define_save_restore_funcs(Layout
*, Symbol_table
*);
497 // No stubs unless a final link.
500 { return !parameters
->options().relocatable(); }
503 do_relax(int, const Input_objects
*, Symbol_table
*, Layout
*, const Task
*);
506 do_plt_fde_location(const Output_data
*, unsigned char*,
507 uint64_t*, off_t
*) const;
509 // Stash info about branches, for stub generation.
511 push_branch(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
512 unsigned int data_shndx
, Address r_offset
,
513 unsigned int r_type
, unsigned int r_sym
, Address addend
)
515 Branch_info
info(ppc_object
, data_shndx
, r_offset
, r_type
, r_sym
, addend
);
516 this->branch_info_
.push_back(info
);
517 if (r_type
== elfcpp::R_POWERPC_REL14
518 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
519 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
520 ppc_object
->set_has_14bit_branch(data_shndx
);
523 Stub_table
<size
, big_endian
>*
527 do_define_standard_symbols(Symbol_table
*, Layout
*);
529 // Finalize the sections.
531 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
533 // Return the value to use for a dynamic which requires special
536 do_dynsym_value(const Symbol
*) const;
538 // Return the PLT address to use for a local symbol.
540 do_plt_address_for_local(const Relobj
*, unsigned int) const;
542 // Return the PLT address to use for a global symbol.
544 do_plt_address_for_global(const Symbol
*) const;
546 // Return the offset to use for the GOT_INDX'th got entry which is
547 // for a local tls symbol specified by OBJECT, SYMNDX.
549 do_tls_offset_for_local(const Relobj
* object
,
551 unsigned int got_indx
) const;
553 // Return the offset to use for the GOT_INDX'th got entry which is
554 // for global tls symbol GSYM.
556 do_tls_offset_for_global(Symbol
* gsym
, unsigned int got_indx
) const;
559 do_function_location(Symbol_location
*) const;
562 do_can_check_for_function_pointers() const
565 // Relocate a section.
567 relocate_section(const Relocate_info
<size
, big_endian
>*,
568 unsigned int sh_type
,
569 const unsigned char* prelocs
,
571 Output_section
* output_section
,
572 bool needs_special_offset_handling
,
574 Address view_address
,
575 section_size_type view_size
,
576 const Reloc_symbol_changes
*);
578 // Scan the relocs during a relocatable link.
580 scan_relocatable_relocs(Symbol_table
* symtab
,
582 Sized_relobj_file
<size
, big_endian
>* object
,
583 unsigned int data_shndx
,
584 unsigned int sh_type
,
585 const unsigned char* prelocs
,
587 Output_section
* output_section
,
588 bool needs_special_offset_handling
,
589 size_t local_symbol_count
,
590 const unsigned char* plocal_symbols
,
591 Relocatable_relocs
*);
593 // Emit relocations for a section.
595 relocate_relocs(const Relocate_info
<size
, big_endian
>*,
596 unsigned int sh_type
,
597 const unsigned char* prelocs
,
599 Output_section
* output_section
,
600 typename
elfcpp::Elf_types
<size
>::Elf_Off
601 offset_in_output_section
,
602 const Relocatable_relocs
*,
604 Address view_address
,
606 unsigned char* reloc_view
,
607 section_size_type reloc_view_size
);
609 // Return whether SYM is defined by the ABI.
611 do_is_defined_by_abi(const Symbol
* sym
) const
613 return strcmp(sym
->name(), "__tls_get_addr") == 0;
616 // Return the size of the GOT section.
620 gold_assert(this->got_
!= NULL
);
621 return this->got_
->data_size();
624 // Get the PLT section.
625 const Output_data_plt_powerpc
<size
, big_endian
>*
628 gold_assert(this->plt_
!= NULL
);
632 // Get the IPLT section.
633 const Output_data_plt_powerpc
<size
, big_endian
>*
636 gold_assert(this->iplt_
!= NULL
);
640 // Get the .glink section.
641 const Output_data_glink
<size
, big_endian
>*
642 glink_section() const
644 gold_assert(this->glink_
!= NULL
);
648 bool has_glink() const
649 { return this->glink_
!= NULL
; }
651 // Get the GOT section.
652 const Output_data_got_powerpc
<size
, big_endian
>*
655 gold_assert(this->got_
!= NULL
);
659 // Get the GOT section, creating it if necessary.
660 Output_data_got_powerpc
<size
, big_endian
>*
661 got_section(Symbol_table
*, Layout
*);
664 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
665 const elfcpp::Ehdr
<size
, big_endian
>&);
667 // Return the number of entries in the GOT.
669 got_entry_count() const
671 if (this->got_
== NULL
)
673 return this->got_size() / (size
/ 8);
676 // Return the number of entries in the PLT.
678 plt_entry_count() const;
680 // Return the offset of the first non-reserved PLT entry.
682 first_plt_entry_offset() const;
684 // Return the size of each PLT entry.
686 plt_entry_size() const;
688 // Add any special sections for this symbol to the gc work list.
689 // For powerpc64, this adds the code section of a function
692 do_gc_mark_symbol(Symbol_table
* symtab
, Symbol
* sym
) const;
694 // Handle target specific gc actions when adding a gc reference from
695 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
696 // and DST_OFF. For powerpc64, this adds a referenc to the code
697 // section of a function descriptor.
699 do_gc_add_reference(Symbol_table
* symtab
,
701 unsigned int src_shndx
,
703 unsigned int dst_shndx
,
704 Address dst_off
) const;
706 typedef std::vector
<Stub_table
<size
, big_endian
>*> Stub_tables
;
709 { return this->stub_tables_
; }
711 const Output_data_brlt_powerpc
<size
, big_endian
>*
713 { return this->brlt_section_
; }
716 add_branch_lookup_table(Address to
)
718 unsigned int off
= this->branch_lookup_table_
.size() * (size
/ 8);
719 this->branch_lookup_table_
.insert(std::make_pair(to
, off
));
723 find_branch_lookup_table(Address to
)
725 typename
Branch_lookup_table::const_iterator p
726 = this->branch_lookup_table_
.find(to
);
727 return p
== this->branch_lookup_table_
.end() ? invalid_address
: p
->second
;
731 write_branch_lookup_table(unsigned char *oview
)
733 for (typename
Branch_lookup_table::const_iterator p
734 = this->branch_lookup_table_
.begin();
735 p
!= this->branch_lookup_table_
.end();
738 elfcpp::Swap
<size
, big_endian
>::writeval(oview
+ p
->second
, p
->first
);
743 plt_thread_safe() const
744 { return this->plt_thread_safe_
; }
760 : tls_get_addr_(NOT_EXPECTED
),
761 relinfo_(NULL
), relnum_(0), r_offset_(0)
766 if (this->tls_get_addr_
!= NOT_EXPECTED
)
773 if (this->relinfo_
!= NULL
)
774 gold_error_at_location(this->relinfo_
, this->relnum_
, this->r_offset_
,
775 _("missing expected __tls_get_addr call"));
779 expect_tls_get_addr_call(
780 const Relocate_info
<size
, big_endian
>* relinfo
,
784 this->tls_get_addr_
= EXPECTED
;
785 this->relinfo_
= relinfo
;
786 this->relnum_
= relnum
;
787 this->r_offset_
= r_offset
;
791 expect_tls_get_addr_call()
792 { this->tls_get_addr_
= EXPECTED
; }
795 skip_next_tls_get_addr_call()
796 {this->tls_get_addr_
= SKIP
; }
799 maybe_skip_tls_get_addr_call(unsigned int r_type
, const Symbol
* gsym
)
801 bool is_tls_call
= ((r_type
== elfcpp::R_POWERPC_REL24
802 || r_type
== elfcpp::R_PPC_PLTREL24
)
804 && strcmp(gsym
->name(), "__tls_get_addr") == 0);
805 Tls_get_addr last_tls
= this->tls_get_addr_
;
806 this->tls_get_addr_
= NOT_EXPECTED
;
807 if (is_tls_call
&& last_tls
!= EXPECTED
)
809 else if (!is_tls_call
&& last_tls
!= NOT_EXPECTED
)
818 // What we're up to regarding calls to __tls_get_addr.
819 // On powerpc, the branch and link insn making a call to
820 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
821 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
822 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
823 // The marker relocation always comes first, and has the same
824 // symbol as the reloc on the insn setting up the __tls_get_addr
825 // argument. This ties the arg setup insn with the call insn,
826 // allowing ld to safely optimize away the call. We check that
827 // every call to __tls_get_addr has a marker relocation, and that
828 // every marker relocation is on a call to __tls_get_addr.
829 Tls_get_addr tls_get_addr_
;
830 // Info about the last reloc for error message.
831 const Relocate_info
<size
, big_endian
>* relinfo_
;
836 // The class which scans relocations.
837 class Scan
: protected Track_tls
840 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
843 : Track_tls(), issued_non_pic_error_(false)
847 get_reference_flags(unsigned int r_type
);
850 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
851 Sized_relobj_file
<size
, big_endian
>* object
,
852 unsigned int data_shndx
,
853 Output_section
* output_section
,
854 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
855 const elfcpp::Sym
<size
, big_endian
>& lsym
,
859 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
860 Sized_relobj_file
<size
, big_endian
>* object
,
861 unsigned int data_shndx
,
862 Output_section
* output_section
,
863 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
867 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
869 Sized_relobj_file
<size
, big_endian
>* ,
872 const elfcpp::Rela
<size
, big_endian
>& ,
874 const elfcpp::Sym
<size
, big_endian
>&)
876 // PowerPC64 .opd is not folded, so any identical function text
877 // may be folded and we'll still keep function addresses distinct.
878 // That means no reloc is of concern here.
881 // For 32-bit, conservatively assume anything but calls to
882 // function code might be taking the address of the function.
883 return !is_branch_reloc(r_type
);
887 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
889 Sized_relobj_file
<size
, big_endian
>* ,
892 const elfcpp::Rela
<size
, big_endian
>& ,
899 return !is_branch_reloc(r_type
);
903 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, big_endian
>* object
,
904 unsigned int r_type
, bool report_err
);
908 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
909 unsigned int r_type
);
912 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
913 unsigned int r_type
, Symbol
*);
916 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
917 Target_powerpc
* target
);
920 check_non_pic(Relobj
*, unsigned int r_type
);
922 // Whether we have issued an error about a non-PIC compilation.
923 bool issued_non_pic_error_
;
927 symval_for_branch(const Symbol_table
* symtab
, Address value
,
928 const Sized_symbol
<size
>* gsym
,
929 Powerpc_relobj
<size
, big_endian
>* object
,
930 unsigned int *dest_shndx
);
932 // The class which implements relocation.
933 class Relocate
: protected Track_tls
936 // Use 'at' branch hints when true, 'y' when false.
937 // FIXME maybe: set this with an option.
938 static const bool is_isa_v2
= true;
944 // Do a relocation. Return false if the caller should not issue
945 // any warnings about this relocation.
947 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
948 Output_section
*, size_t relnum
,
949 const elfcpp::Rela
<size
, big_endian
>&,
950 unsigned int r_type
, const Sized_symbol
<size
>*,
951 const Symbol_value
<size
>*,
953 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
957 class Relocate_comdat_behavior
960 // Decide what the linker should do for relocations that refer to
961 // discarded comdat sections.
962 inline Comdat_behavior
963 get(const char* name
)
965 gold::Default_comdat_behavior default_behavior
;
966 Comdat_behavior ret
= default_behavior
.get(name
);
967 if (ret
== CB_WARNING
)
970 && (strcmp(name
, ".fixup") == 0
971 || strcmp(name
, ".got2") == 0))
974 && (strcmp(name
, ".opd") == 0
975 || strcmp(name
, ".toc") == 0
976 || strcmp(name
, ".toc1") == 0))
983 // A class which returns the size required for a relocation type,
984 // used while scanning relocs during a relocatable link.
985 class Relocatable_size_for_reloc
989 get_size_for_reloc(unsigned int, Relobj
*)
996 // Optimize the TLS relocation type based on what we know about the
997 // symbol. IS_FINAL is true if the final address of this symbol is
998 // known at link time.
1000 tls::Tls_optimization
1001 optimize_tls_gd(bool is_final
)
1003 // If we are generating a shared library, then we can't do anything
1005 if (parameters
->options().shared())
1006 return tls::TLSOPT_NONE
;
1009 return tls::TLSOPT_TO_IE
;
1010 return tls::TLSOPT_TO_LE
;
1013 tls::Tls_optimization
1016 if (parameters
->options().shared())
1017 return tls::TLSOPT_NONE
;
1019 return tls::TLSOPT_TO_LE
;
1022 tls::Tls_optimization
1023 optimize_tls_ie(bool is_final
)
1025 if (!is_final
|| parameters
->options().shared())
1026 return tls::TLSOPT_NONE
;
1028 return tls::TLSOPT_TO_LE
;
1033 make_glink_section(Layout
*);
1035 // Create the PLT section.
1037 make_plt_section(Symbol_table
*, Layout
*);
1040 make_iplt_section(Symbol_table
*, Layout
*);
1043 make_brlt_section(Layout
*);
1045 // Create a PLT entry for a global symbol.
1047 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1049 // Create a PLT entry for a local IFUNC symbol.
1051 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1052 Sized_relobj_file
<size
, big_endian
>*,
1056 // Create a GOT entry for local dynamic __tls_get_addr.
1058 tlsld_got_offset(Symbol_table
* symtab
, Layout
* layout
,
1059 Sized_relobj_file
<size
, big_endian
>* object
);
1062 tlsld_got_offset() const
1064 return this->tlsld_got_offset_
;
1067 // Get the dynamic reloc section, creating it if necessary.
1069 rela_dyn_section(Layout
*);
1071 // Similarly, but for ifunc symbols get the one for ifunc.
1073 rela_dyn_section(Symbol_table
*, Layout
*, bool for_ifunc
);
1075 // Copy a relocation against a global symbol.
1077 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1078 Sized_relobj_file
<size
, big_endian
>* object
,
1079 unsigned int shndx
, Output_section
* output_section
,
1080 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
1082 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1083 symtab
->get_sized_symbol
<size
>(sym
),
1084 object
, shndx
, output_section
,
1085 reloc
, this->rela_dyn_section(layout
));
1088 // Look over all the input sections, deciding where to place stubs.
1090 group_sections(Layout
*, const Task
*);
1092 // Sort output sections by address.
1093 struct Sort_sections
1096 operator()(const Output_section
* sec1
, const Output_section
* sec2
)
1097 { return sec1
->address() < sec2
->address(); }
1103 Branch_info(Powerpc_relobj
<size
, big_endian
>* ppc_object
,
1104 unsigned int data_shndx
,
1106 unsigned int r_type
,
1109 : object_(ppc_object
), shndx_(data_shndx
), offset_(r_offset
),
1110 r_type_(r_type
), r_sym_(r_sym
), addend_(addend
)
1116 // If this branch needs a plt call stub, or a long branch stub, make one.
1118 make_stub(Stub_table
<size
, big_endian
>*,
1119 Stub_table
<size
, big_endian
>*,
1120 Symbol_table
*) const;
1123 // The branch location..
1124 Powerpc_relobj
<size
, big_endian
>* object_
;
1125 unsigned int shndx_
;
1127 // ..and the branch type and destination.
1128 unsigned int r_type_
;
1129 unsigned int r_sym_
;
1133 // Information about this specific target which we pass to the
1134 // general Target structure.
1135 static Target::Target_info powerpc_info
;
1137 // The types of GOT entries needed for this platform.
1138 // These values are exposed to the ABI in an incremental link.
1139 // Do not renumber existing values without changing the version
1140 // number of the .gnu_incremental_inputs section.
1144 GOT_TYPE_TLSGD
, // double entry for @got@tlsgd
1145 GOT_TYPE_DTPREL
, // entry for @got@dtprel
1146 GOT_TYPE_TPREL
// entry for @got@tprel
1150 Output_data_got_powerpc
<size
, big_endian
>* got_
;
1151 // The PLT section. This is a container for a table of addresses,
1152 // and their relocations. Each address in the PLT has a dynamic
1153 // relocation (R_*_JMP_SLOT) and each address will have a
1154 // corresponding entry in .glink for lazy resolution of the PLT.
1155 // ppc32 initialises the PLT to point at the .glink entry, while
1156 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1157 // linker adds a stub that loads the PLT entry into ctr then
1158 // branches to ctr. There may be more than one stub for each PLT
1159 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1160 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1161 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
1162 // The IPLT section. Like plt_, this is a container for a table of
1163 // addresses and their relocations, specifically for STT_GNU_IFUNC
1164 // functions that resolve locally (STT_GNU_IFUNC functions that
1165 // don't resolve locally go in PLT). Unlike plt_, these have no
1166 // entry in .glink for lazy resolution, and the relocation section
1167 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1168 // the relocation section may contain relocations against
1169 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1170 // relocation section will appear at the end of other dynamic
1171 // relocations, so that ld.so applies these relocations after other
1172 // dynamic relocations. In a static executable, the relocation
1173 // section is emitted and marked with __rela_iplt_start and
1174 // __rela_iplt_end symbols.
1175 Output_data_plt_powerpc
<size
, big_endian
>* iplt_
;
1176 // Section holding long branch destinations.
1177 Output_data_brlt_powerpc
<size
, big_endian
>* brlt_section_
;
1178 // The .glink section.
1179 Output_data_glink
<size
, big_endian
>* glink_
;
1180 // The dynamic reloc section.
1181 Reloc_section
* rela_dyn_
;
1182 // Relocs saved to avoid a COPY reloc.
1183 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
1184 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1185 unsigned int tlsld_got_offset_
;
1187 Stub_tables stub_tables_
;
1188 typedef Unordered_map
<Address
, unsigned int> Branch_lookup_table
;
1189 Branch_lookup_table branch_lookup_table_
;
1191 typedef std::vector
<Branch_info
> Branches
;
1192 Branches branch_info_
;
1194 bool plt_thread_safe_
;
1198 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
1201 true, // is_big_endian
1202 elfcpp::EM_PPC
, // machine_code
1203 false, // has_make_symbol
1204 false, // has_resolve
1205 false, // has_code_fill
1206 true, // is_default_stack_executable
1207 false, // can_icf_inline_merge_sections
1209 "/usr/lib/ld.so.1", // dynamic_linker
1210 0x10000000, // default_text_segment_address
1211 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1212 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1213 false, // isolate_execinstr
1215 elfcpp::SHN_UNDEF
, // small_common_shndx
1216 elfcpp::SHN_UNDEF
, // large_common_shndx
1217 0, // small_common_section_flags
1218 0, // large_common_section_flags
1219 NULL
, // attributes_section
1220 NULL
, // attributes_vendor
1221 "_start" // entry_symbol_name
1225 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
1228 false, // is_big_endian
1229 elfcpp::EM_PPC
, // machine_code
1230 false, // has_make_symbol
1231 false, // has_resolve
1232 false, // has_code_fill
1233 true, // is_default_stack_executable
1234 false, // can_icf_inline_merge_sections
1236 "/usr/lib/ld.so.1", // dynamic_linker
1237 0x10000000, // default_text_segment_address
1238 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1239 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1240 false, // isolate_execinstr
1242 elfcpp::SHN_UNDEF
, // small_common_shndx
1243 elfcpp::SHN_UNDEF
, // large_common_shndx
1244 0, // small_common_section_flags
1245 0, // large_common_section_flags
1246 NULL
, // attributes_section
1247 NULL
, // attributes_vendor
1248 "_start" // entry_symbol_name
1252 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
1255 true, // is_big_endian
1256 elfcpp::EM_PPC64
, // machine_code
1257 false, // has_make_symbol
1258 false, // has_resolve
1259 false, // has_code_fill
1260 true, // is_default_stack_executable
1261 false, // can_icf_inline_merge_sections
1263 "/usr/lib/ld.so.1", // dynamic_linker
1264 0x10000000, // default_text_segment_address
1265 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1266 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1267 false, // isolate_execinstr
1269 elfcpp::SHN_UNDEF
, // small_common_shndx
1270 elfcpp::SHN_UNDEF
, // large_common_shndx
1271 0, // small_common_section_flags
1272 0, // large_common_section_flags
1273 NULL
, // attributes_section
1274 NULL
, // attributes_vendor
1275 "_start" // entry_symbol_name
1279 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
1282 false, // is_big_endian
1283 elfcpp::EM_PPC64
, // machine_code
1284 false, // has_make_symbol
1285 false, // has_resolve
1286 false, // has_code_fill
1287 true, // is_default_stack_executable
1288 false, // can_icf_inline_merge_sections
1290 "/usr/lib/ld.so.1", // dynamic_linker
1291 0x10000000, // default_text_segment_address
1292 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1293 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1294 false, // isolate_execinstr
1296 elfcpp::SHN_UNDEF
, // small_common_shndx
1297 elfcpp::SHN_UNDEF
, // large_common_shndx
1298 0, // small_common_section_flags
1299 0, // large_common_section_flags
1300 NULL
, // attributes_section
1301 NULL
, // attributes_vendor
1302 "_start" // entry_symbol_name
1306 is_branch_reloc(unsigned int r_type
)
1308 return (r_type
== elfcpp::R_POWERPC_REL24
1309 || r_type
== elfcpp::R_PPC_PLTREL24
1310 || r_type
== elfcpp::R_PPC_LOCAL24PC
1311 || r_type
== elfcpp::R_POWERPC_REL14
1312 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
1313 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
1314 || r_type
== elfcpp::R_POWERPC_ADDR24
1315 || r_type
== elfcpp::R_POWERPC_ADDR14
1316 || r_type
== elfcpp::R_POWERPC_ADDR14_BRTAKEN
1317 || r_type
== elfcpp::R_POWERPC_ADDR14_BRNTAKEN
);
1320 // If INSN is an opcode that may be used with an @tls operand, return
1321 // the transformed insn for TLS optimisation, otherwise return 0. If
1322 // REG is non-zero only match an insn with RB or RA equal to REG.
1324 at_tls_transform(uint32_t insn
, unsigned int reg
)
1326 if ((insn
& (0x3f << 26)) != 31 << 26)
1330 if (reg
== 0 || ((insn
>> 11) & 0x1f) == reg
)
1331 rtra
= insn
& ((1 << 26) - (1 << 16));
1332 else if (((insn
>> 16) & 0x1f) == reg
)
1333 rtra
= (insn
& (0x1f << 21)) | ((insn
& (0x1f << 11)) << 5);
1337 if ((insn
& (0x3ff << 1)) == 266 << 1)
1340 else if ((insn
& (0x1f << 1)) == 23 << 1
1341 && ((insn
& (0x1f << 6)) < 14 << 6
1342 || ((insn
& (0x1f << 6)) >= 16 << 6
1343 && (insn
& (0x1f << 6)) < 24 << 6)))
1344 // load and store indexed -> dform
1345 insn
= (32 | ((insn
>> 6) & 0x1f)) << 26;
1346 else if ((insn
& (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1347 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1348 insn
= ((58 | ((insn
>> 6) & 4)) << 26) | ((insn
>> 6) & 1);
1349 else if ((insn
& (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1351 insn
= (58 << 26) | 2;
1358 // Modified version of symtab.h class Symbol member
1359 // Given a direct absolute or pc-relative static relocation against
1360 // the global symbol, this function returns whether a dynamic relocation
1365 needs_dynamic_reloc(const Symbol
* gsym
, int flags
)
1367 // No dynamic relocations in a static link!
1368 if (parameters
->doing_static_link())
1371 // A reference to an undefined symbol from an executable should be
1372 // statically resolved to 0, and does not need a dynamic relocation.
1373 // This matches gnu ld behavior.
1374 if (gsym
->is_undefined() && !parameters
->options().shared())
1377 // A reference to an absolute symbol does not need a dynamic relocation.
1378 if (gsym
->is_absolute())
1381 // An absolute reference within a position-independent output file
1382 // will need a dynamic relocation.
1383 if ((flags
& Symbol::ABSOLUTE_REF
)
1384 && parameters
->options().output_is_position_independent())
1387 // A function call that can branch to a local PLT entry does not need
1388 // a dynamic relocation.
1389 if ((flags
& Symbol::FUNCTION_CALL
) && gsym
->has_plt_offset())
1392 // A reference to any PLT entry in a non-position-independent executable
1393 // does not need a dynamic relocation.
1394 // Except due to having function descriptors on powerpc64 we don't define
1395 // functions to their plt code in an executable, so this doesn't apply.
1397 && !parameters
->options().output_is_position_independent()
1398 && gsym
->has_plt_offset())
1401 // A reference to a symbol defined in a dynamic object or to a
1402 // symbol that is preemptible will need a dynamic relocation.
1403 if (gsym
->is_from_dynobj()
1404 || gsym
->is_undefined()
1405 || gsym
->is_preemptible())
1408 // For all other cases, return FALSE.
1412 // Modified version of symtab.h class Symbol member
1413 // Whether we should use the PLT offset associated with a symbol for
1414 // a relocation. FLAGS is a set of Reference_flags.
1418 use_plt_offset(const Symbol
* gsym
, int flags
)
1420 // If the symbol doesn't have a PLT offset, then naturally we
1421 // don't want to use it.
1422 if (!gsym
->has_plt_offset())
1425 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
1426 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1429 // If we are going to generate a dynamic relocation, then we will
1430 // wind up using that, so no need to use the PLT entry.
1431 if (needs_dynamic_reloc
<size
>(gsym
, flags
))
1434 // If the symbol is from a dynamic object, we need to use the PLT
1436 if (gsym
->is_from_dynobj())
1439 // If we are generating a shared object, and this symbol is
1440 // undefined or preemptible, we need to use the PLT entry.
1441 if (parameters
->options().shared()
1442 && (gsym
->is_undefined() || gsym
->is_preemptible()))
1445 // If this is a call to a weak undefined symbol, we need to use
1446 // the PLT entry; the symbol may be defined by a library loaded
1448 if ((flags
& Symbol::FUNCTION_CALL
) && gsym
->is_weak_undefined())
1451 // Otherwise we can use the regular definition.
1455 template<int size
, bool big_endian
>
1456 class Powerpc_relocate_functions
1473 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
1474 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
1476 template<int valsize
>
1478 has_overflow_signed(Address value
)
1480 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1481 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1482 limit
<<= ((valsize
- 1) >> 1);
1483 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1484 return value
+ limit
> (limit
<< 1) - 1;
1487 template<int valsize
>
1489 has_overflow_bitfield(Address value
)
1491 Address limit
= static_cast<Address
>(1) << ((valsize
- 1) >> 1);
1492 limit
<<= ((valsize
- 1) >> 1);
1493 limit
<<= ((valsize
- 1) - 2 * ((valsize
- 1) >> 1));
1494 return value
> (limit
<< 1) - 1 && value
+ limit
> (limit
<< 1) - 1;
1497 template<int valsize
>
1498 static inline Status
1499 overflowed(Address value
, Overflow_check overflow
)
1501 if (overflow
== CHECK_SIGNED
)
1503 if (has_overflow_signed
<valsize
>(value
))
1504 return STATUS_OVERFLOW
;
1506 else if (overflow
== CHECK_BITFIELD
)
1508 if (has_overflow_bitfield
<valsize
>(value
))
1509 return STATUS_OVERFLOW
;
1514 // Do a simple RELA relocation
1515 template<int valsize
>
1516 static inline Status
1517 rela(unsigned char* view
, Address value
, Overflow_check overflow
)
1519 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1520 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1521 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, value
);
1522 return overflowed
<valsize
>(value
, overflow
);
1525 template<int valsize
>
1526 static inline Status
1527 rela(unsigned char* view
,
1528 unsigned int right_shift
,
1529 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
1531 Overflow_check overflow
)
1533 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
1534 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
1535 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
1536 Valtype reloc
= value
>> right_shift
;
1539 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
1540 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1543 // Do a simple RELA relocation, unaligned.
1544 template<int valsize
>
1545 static inline Status
1546 rela_ua(unsigned char* view
, Address value
, Overflow_check overflow
)
1548 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
, value
);
1549 return overflowed
<valsize
>(value
, overflow
);
1552 template<int valsize
>
1553 static inline Status
1554 rela_ua(unsigned char* view
,
1555 unsigned int right_shift
,
1556 typename
elfcpp::Valtype_base
<valsize
>::Valtype dst_mask
,
1558 Overflow_check overflow
)
1560 typedef typename
elfcpp::Swap_unaligned
<valsize
, big_endian
>::Valtype
1562 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(view
);
1563 Valtype reloc
= value
>> right_shift
;
1566 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(view
, val
| reloc
);
1567 return overflowed
<valsize
>(value
>> right_shift
, overflow
);
1571 // R_PPC64_ADDR64: (Symbol + Addend)
1573 addr64(unsigned char* view
, Address value
)
1574 { This::template rela
<64>(view
, value
, CHECK_NONE
); }
1576 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1578 addr64_u(unsigned char* view
, Address value
)
1579 { This::template rela_ua
<64>(view
, value
, CHECK_NONE
); }
1581 // R_POWERPC_ADDR32: (Symbol + Addend)
1582 static inline Status
1583 addr32(unsigned char* view
, Address value
, Overflow_check overflow
)
1584 { return This::template rela
<32>(view
, value
, overflow
); }
1586 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1587 static inline Status
1588 addr32_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1589 { return This::template rela_ua
<32>(view
, value
, overflow
); }
1591 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1592 static inline Status
1593 addr24(unsigned char* view
, Address value
, Overflow_check overflow
)
1595 Status stat
= This::template rela
<32>(view
, 0, 0x03fffffc, value
, overflow
);
1596 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1597 stat
= STATUS_OVERFLOW
;
1601 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1602 static inline Status
1603 addr16(unsigned char* view
, Address value
, Overflow_check overflow
)
1604 { return This::template rela
<16>(view
, value
, overflow
); }
1606 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1607 static inline Status
1608 addr16_u(unsigned char* view
, Address value
, Overflow_check overflow
)
1609 { return This::template rela_ua
<16>(view
, value
, overflow
); }
1611 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1612 static inline Status
1613 addr16_ds(unsigned char* view
, Address value
, Overflow_check overflow
)
1615 Status stat
= This::template rela
<16>(view
, 0, 0xfffc, value
, overflow
);
1616 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1617 stat
= STATUS_OVERFLOW
;
1621 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1623 addr16_hi(unsigned char* view
, Address value
)
1624 { This::template rela
<16>(view
, 16, 0xffff, value
, CHECK_NONE
); }
1626 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1628 addr16_ha(unsigned char* view
, Address value
)
1629 { This::addr16_hi(view
, value
+ 0x8000); }
1631 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1633 addr16_hi2(unsigned char* view
, Address value
)
1634 { This::template rela
<16>(view
, 32, 0xffff, value
, CHECK_NONE
); }
1636 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1638 addr16_ha2(unsigned char* view
, Address value
)
1639 { This::addr16_hi2(view
, value
+ 0x8000); }
1641 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1643 addr16_hi3(unsigned char* view
, Address value
)
1644 { This::template rela
<16>(view
, 48, 0xffff, value
, CHECK_NONE
); }
1646 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1648 addr16_ha3(unsigned char* view
, Address value
)
1649 { This::addr16_hi3(view
, value
+ 0x8000); }
1651 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1652 static inline Status
1653 addr14(unsigned char* view
, Address value
, Overflow_check overflow
)
1655 Status stat
= This::template rela
<32>(view
, 0, 0xfffc, value
, overflow
);
1656 if (overflow
!= CHECK_NONE
&& (value
& 3) != 0)
1657 stat
= STATUS_OVERFLOW
;
1662 // Stash away the index of .got2 or .opd in a relocatable object, if
1663 // such a section exists.
1665 template<int size
, bool big_endian
>
1667 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
1668 Read_symbols_data
* sd
)
1670 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1671 const unsigned char* namesu
= sd
->section_names
->data();
1672 const char* names
= reinterpret_cast<const char*>(namesu
);
1673 section_size_type names_size
= sd
->section_names_size
;
1674 const unsigned char* s
;
1676 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
,
1677 size
== 32 ? ".got2" : ".opd",
1678 names
, names_size
, NULL
);
1681 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
1682 this->special_
= ndx
;
1684 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
1687 // Examine .rela.opd to build info about function entry points.
1689 template<int size
, bool big_endian
>
1691 Powerpc_relobj
<size
, big_endian
>::scan_opd_relocs(
1693 const unsigned char* prelocs
,
1694 const unsigned char* plocal_syms
)
1698 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
1700 const int reloc_size
1701 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
1702 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1703 Address expected_off
= 0;
1704 bool regular
= true;
1705 unsigned int opd_ent_size
= 0;
1707 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
1709 Reltype
reloc(prelocs
);
1710 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
1711 = reloc
.get_r_info();
1712 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
1713 if (r_type
== elfcpp::R_PPC64_ADDR64
)
1715 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
1716 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
1719 if (r_sym
< this->local_symbol_count())
1721 typename
elfcpp::Sym
<size
, big_endian
>
1722 lsym(plocal_syms
+ r_sym
* sym_size
);
1723 shndx
= lsym
.get_st_shndx();
1724 shndx
= this->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1725 value
= lsym
.get_st_value();
1728 shndx
= this->symbol_section_and_value(r_sym
, &value
,
1730 this->set_opd_ent(reloc
.get_r_offset(), shndx
,
1731 value
+ reloc
.get_r_addend());
1734 expected_off
= reloc
.get_r_offset();
1735 opd_ent_size
= expected_off
;
1737 else if (expected_off
!= reloc
.get_r_offset())
1739 expected_off
+= opd_ent_size
;
1741 else if (r_type
== elfcpp::R_PPC64_TOC
)
1743 if (expected_off
- opd_ent_size
+ 8 != reloc
.get_r_offset())
1748 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1749 this->name().c_str(), r_type
);
1753 if (reloc_count
<= 2)
1754 opd_ent_size
= this->section_size(this->opd_shndx());
1755 if (opd_ent_size
!= 24 && opd_ent_size
!= 16)
1759 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1760 this->name().c_str());
1766 template<int size
, bool big_endian
>
1768 Powerpc_relobj
<size
, big_endian
>::do_read_relocs(Read_relocs_data
* rd
)
1770 Sized_relobj_file
<size
, big_endian
>::do_read_relocs(rd
);
1773 for (Read_relocs_data::Relocs_list::iterator p
= rd
->relocs
.begin();
1774 p
!= rd
->relocs
.end();
1777 if (p
->data_shndx
== this->opd_shndx())
1779 uint64_t opd_size
= this->section_size(this->opd_shndx());
1780 gold_assert(opd_size
== static_cast<size_t>(opd_size
));
1783 this->init_opd(opd_size
);
1784 this->scan_opd_relocs(p
->reloc_count
, p
->contents
->data(),
1785 rd
->local_symbols
->data());
1793 // Call Sized_dynobj::do_read_symbols to read the symbols then
1794 // read .opd from a dynamic object, filling in opd_ent_ vector,
1796 template<int size
, bool big_endian
>
1798 Powerpc_dynobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
1800 Sized_dynobj
<size
, big_endian
>::do_read_symbols(sd
);
1803 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
1804 const unsigned char* const pshdrs
= sd
->section_headers
->data();
1805 const unsigned char* namesu
= sd
->section_names
->data();
1806 const char* names
= reinterpret_cast<const char*>(namesu
);
1807 const unsigned char* s
= NULL
;
1808 const unsigned char* opd
;
1809 section_size_type opd_size
;
1811 // Find and read .opd section.
1814 s
= this->template find_shdr
<size
, big_endian
>(pshdrs
, ".opd", names
,
1815 sd
->section_names_size
,
1820 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
1821 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
1822 && (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
1824 this->opd_shndx_
= (s
- pshdrs
) / shdr_size
;
1825 this->opd_address_
= shdr
.get_sh_addr();
1826 opd_size
= convert_to_section_size_type(shdr
.get_sh_size());
1827 opd
= this->get_view(shdr
.get_sh_offset(), opd_size
,
1833 // Build set of executable sections.
1834 // Using a set is probably overkill. There is likely to be only
1835 // a few executable sections, typically .init, .text and .fini,
1836 // and they are generally grouped together.
1837 typedef std::set
<Sec_info
> Exec_sections
;
1838 Exec_sections exec_sections
;
1840 for (unsigned int i
= 1; i
< this->shnum(); ++i
, s
+= shdr_size
)
1842 typename
elfcpp::Shdr
<size
, big_endian
> shdr(s
);
1843 if (shdr
.get_sh_type() == elfcpp::SHT_PROGBITS
1844 && ((shdr
.get_sh_flags()
1845 & (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
1846 == (elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
))
1847 && shdr
.get_sh_size() != 0)
1849 exec_sections
.insert(Sec_info(shdr
.get_sh_addr(),
1850 shdr
.get_sh_size(), i
));
1853 if (exec_sections
.empty())
1856 // Look over the OPD entries. This is complicated by the fact
1857 // that some binaries will use two-word entries while others
1858 // will use the standard three-word entries. In most cases
1859 // the third word (the environment pointer for languages like
1860 // Pascal) is unused and will be zero. If the third word is
1861 // used it should not be pointing into executable sections,
1863 this->init_opd(opd_size
);
1864 for (const unsigned char* p
= opd
; p
< opd
+ opd_size
; p
+= 8)
1866 typedef typename
elfcpp::Swap
<64, big_endian
>::Valtype Valtype
;
1867 const Valtype
* valp
= reinterpret_cast<const Valtype
*>(p
);
1868 Valtype val
= elfcpp::Swap
<64, big_endian
>::readval(valp
);
1870 // Chances are that this is the third word of an OPD entry.
1872 typename
Exec_sections::const_iterator e
1873 = exec_sections
.upper_bound(Sec_info(val
, 0, 0));
1874 if (e
!= exec_sections
.begin())
1877 if (e
->start
<= val
&& val
< e
->start
+ e
->len
)
1879 // We have an address in an executable section.
1880 // VAL ought to be the function entry, set it up.
1881 this->set_opd_ent(p
- opd
, e
->shndx
, val
);
1882 // Skip second word of OPD entry, the TOC pointer.
1886 // If we didn't match any executable sections, we likely
1887 // have a non-zero third word in the OPD entry.
1892 // Set up some symbols.
1894 template<int size
, bool big_endian
>
1896 Target_powerpc
<size
, big_endian
>::do_define_standard_symbols(
1897 Symbol_table
* symtab
,
1902 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
1903 // undefined when scanning relocs (and thus requires
1904 // non-relative dynamic relocs). The proper value will be
1906 Symbol
*gotsym
= symtab
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
1907 if (gotsym
!= NULL
&& gotsym
->is_undefined())
1909 Target_powerpc
<size
, big_endian
>* target
=
1910 static_cast<Target_powerpc
<size
, big_endian
>*>(
1911 parameters
->sized_target
<size
, big_endian
>());
1912 Output_data_got_powerpc
<size
, big_endian
>* got
1913 = target
->got_section(symtab
, layout
);
1914 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1915 Symbol_table::PREDEFINED
,
1919 elfcpp::STV_HIDDEN
, 0,
1923 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
1924 Symbol
*sdasym
= symtab
->lookup("_SDA_BASE_", NULL
);
1925 if (sdasym
!= NULL
&& sdasym
->is_undefined())
1927 Output_data_space
* sdata
= new Output_data_space(4, "** sdata");
1929 = layout
->add_output_section_data(".sdata", 0,
1931 | elfcpp::SHF_WRITE
,
1932 sdata
, ORDER_SMALL_DATA
, false);
1933 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
1934 Symbol_table::PREDEFINED
,
1935 os
, 32768, 0, elfcpp::STT_OBJECT
,
1936 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
1942 // Set up PowerPC target specific relobj.
1944 template<int size
, bool big_endian
>
1946 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
1947 const std::string
& name
,
1948 Input_file
* input_file
,
1949 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
1951 int et
= ehdr
.get_e_type();
1952 // ET_EXEC files are valid input for --just-symbols/-R,
1953 // and we treat them as relocatable objects.
1954 if (et
== elfcpp::ET_REL
1955 || (et
== elfcpp::ET_EXEC
&& input_file
->just_symbols()))
1957 Powerpc_relobj
<size
, big_endian
>* obj
=
1958 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1962 else if (et
== elfcpp::ET_DYN
)
1964 Powerpc_dynobj
<size
, big_endian
>* obj
=
1965 new Powerpc_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1971 gold_error(_("%s: unsupported ELF file type %d"), name
.c_str(), et
);
1976 template<int size
, bool big_endian
>
1977 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
1980 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
1981 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
1983 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
1984 : Output_data_got
<size
, big_endian
>(),
1985 symtab_(symtab
), layout_(layout
),
1986 header_ent_cnt_(size
== 32 ? 3 : 1),
1987 header_index_(size
== 32 ? 0x2000 : 0)
1990 // Override all the Output_data_got methods we use so as to first call
1993 add_global(Symbol
* gsym
, unsigned int got_type
)
1995 this->reserve_ent();
1996 return Output_data_got
<size
, big_endian
>::add_global(gsym
, got_type
);
2000 add_global_plt(Symbol
* gsym
, unsigned int got_type
)
2002 this->reserve_ent();
2003 return Output_data_got
<size
, big_endian
>::add_global_plt(gsym
, got_type
);
2007 add_global_tls(Symbol
* gsym
, unsigned int got_type
)
2008 { return this->add_global_plt(gsym
, got_type
); }
2011 add_global_with_rel(Symbol
* gsym
, unsigned int got_type
,
2012 Output_data_reloc_generic
* rel_dyn
, unsigned int r_type
)
2014 this->reserve_ent();
2015 Output_data_got
<size
, big_endian
>::
2016 add_global_with_rel(gsym
, got_type
, rel_dyn
, r_type
);
2020 add_global_pair_with_rel(Symbol
* gsym
, unsigned int got_type
,
2021 Output_data_reloc_generic
* rel_dyn
,
2022 unsigned int r_type_1
, unsigned int r_type_2
)
2024 this->reserve_ent(2);
2025 Output_data_got
<size
, big_endian
>::
2026 add_global_pair_with_rel(gsym
, got_type
, rel_dyn
, r_type_1
, r_type_2
);
2030 add_local(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2032 this->reserve_ent();
2033 return Output_data_got
<size
, big_endian
>::add_local(object
, sym_index
,
2038 add_local_plt(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2040 this->reserve_ent();
2041 return Output_data_got
<size
, big_endian
>::add_local_plt(object
, sym_index
,
2046 add_local_tls(Relobj
* object
, unsigned int sym_index
, unsigned int got_type
)
2047 { return this->add_local_plt(object
, sym_index
, got_type
); }
2050 add_local_tls_pair(Relobj
* object
, unsigned int sym_index
,
2051 unsigned int got_type
,
2052 Output_data_reloc_generic
* rel_dyn
,
2053 unsigned int r_type
)
2055 this->reserve_ent(2);
2056 Output_data_got
<size
, big_endian
>::
2057 add_local_tls_pair(object
, sym_index
, got_type
, rel_dyn
, r_type
);
2061 add_constant(Valtype constant
)
2063 this->reserve_ent();
2064 return Output_data_got
<size
, big_endian
>::add_constant(constant
);
2068 add_constant_pair(Valtype c1
, Valtype c2
)
2070 this->reserve_ent(2);
2071 return Output_data_got
<size
, big_endian
>::add_constant_pair(c1
, c2
);
2074 // Offset of _GLOBAL_OFFSET_TABLE_.
2078 return this->got_offset(this->header_index_
);
2081 // Offset of base used to access the GOT/TOC.
2082 // The got/toc pointer reg will be set to this value.
2084 got_base_offset(const Powerpc_relobj
<size
, big_endian
>* object
) const
2087 return this->g_o_t();
2089 return (this->output_section()->address()
2090 + object
->toc_base_offset()
2094 // Ensure our GOT has a header.
2096 set_final_data_size()
2098 if (this->header_ent_cnt_
!= 0)
2099 this->make_header();
2100 Output_data_got
<size
, big_endian
>::set_final_data_size();
2103 // First word of GOT header needs some values that are not
2104 // handled by Output_data_got so poke them in here.
2105 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2107 do_write(Output_file
* of
)
2110 if (size
== 32 && this->layout_
->dynamic_data() != NULL
)
2111 val
= this->layout_
->dynamic_section()->address();
2113 val
= this->output_section()->address() + 0x8000;
2114 this->replace_constant(this->header_index_
, val
);
2115 Output_data_got
<size
, big_endian
>::do_write(of
);
2120 reserve_ent(unsigned int cnt
= 1)
2122 if (this->header_ent_cnt_
== 0)
2124 if (this->num_entries() + cnt
> this->header_index_
)
2125 this->make_header();
2131 this->header_ent_cnt_
= 0;
2132 this->header_index_
= this->num_entries();
2135 Output_data_got
<size
, big_endian
>::add_constant(0);
2136 Output_data_got
<size
, big_endian
>::add_constant(0);
2137 Output_data_got
<size
, big_endian
>::add_constant(0);
2139 // Define _GLOBAL_OFFSET_TABLE_ at the header
2140 Symbol
*gotsym
= this->symtab_
->lookup("_GLOBAL_OFFSET_TABLE_", NULL
);
2143 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(gotsym
);
2144 sym
->set_value(this->g_o_t());
2147 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2148 Symbol_table::PREDEFINED
,
2149 this, this->g_o_t(), 0,
2152 elfcpp::STV_HIDDEN
, 0,
2156 Output_data_got
<size
, big_endian
>::add_constant(0);
2159 // Stashed pointers.
2160 Symbol_table
* symtab_
;
2164 unsigned int header_ent_cnt_
;
2165 // GOT header index.
2166 unsigned int header_index_
;
2169 // Get the GOT section, creating it if necessary.
2171 template<int size
, bool big_endian
>
2172 Output_data_got_powerpc
<size
, big_endian
>*
2173 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
2176 if (this->got_
== NULL
)
2178 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
2181 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
2183 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2184 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2185 this->got_
, ORDER_DATA
, false);
2191 // Get the dynamic reloc section, creating it if necessary.
2193 template<int size
, bool big_endian
>
2194 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2195 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
2197 if (this->rela_dyn_
== NULL
)
2199 gold_assert(layout
!= NULL
);
2200 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
2201 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
2202 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
2203 ORDER_DYNAMIC_RELOCS
, false);
2205 return this->rela_dyn_
;
2208 // Similarly, but for ifunc symbols get the one for ifunc.
2210 template<int size
, bool big_endian
>
2211 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
2212 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Symbol_table
* symtab
,
2217 return this->rela_dyn_section(layout
);
2219 if (this->iplt_
== NULL
)
2220 this->make_iplt_section(symtab
, layout
);
2221 return this->iplt_
->rel_plt();
2227 // Determine the stub group size. The group size is the absolute
2228 // value of the parameter --stub-group-size. If --stub-group-size
2229 // is passed a negative value, we restrict stubs to be always before
2230 // the stubbed branches.
2231 Stub_control(int32_t size
)
2232 : state_(NO_GROUP
), stub_group_size_(abs(size
)),
2233 stub14_group_size_(abs(size
)),
2234 stubs_always_before_branch_(size
< 0), suppress_size_errors_(false),
2235 group_end_addr_(0), owner_(NULL
), output_section_(NULL
)
2237 if (stub_group_size_
== 1)
2240 if (stubs_always_before_branch_
)
2242 stub_group_size_
= 0x1e00000;
2243 stub14_group_size_
= 0x7800;
2247 stub_group_size_
= 0x1c00000;
2248 stub14_group_size_
= 0x7000;
2250 suppress_size_errors_
= true;
2254 // Return true iff input section can be handled by current stub
2257 can_add_to_stub_group(Output_section
* o
,
2258 const Output_section::Input_section
* i
,
2261 const Output_section::Input_section
*
2267 { return output_section_
; }
2273 FINDING_STUB_SECTION
,
2278 uint32_t stub_group_size_
;
2279 uint32_t stub14_group_size_
;
2280 bool stubs_always_before_branch_
;
2281 bool suppress_size_errors_
;
2282 uint64_t group_end_addr_
;
2283 const Output_section::Input_section
* owner_
;
2284 Output_section
* output_section_
;
2287 // Return true iff input section can be handled by current stub
2291 Stub_control::can_add_to_stub_group(Output_section
* o
,
2292 const Output_section::Input_section
* i
,
2296 = has14
? this->stub14_group_size_
: this->stub_group_size_
;
2297 bool whole_sec
= o
->order() == ORDER_INIT
|| o
->order() == ORDER_FINI
;
2299 uint64_t start_addr
= o
->address();
2302 // .init and .fini sections are pasted together to form a single
2303 // function. We can't be adding stubs in the middle of the function.
2304 this_size
= o
->data_size();
2307 start_addr
+= i
->relobj()->output_section_offset(i
->shndx());
2308 this_size
= i
->data_size();
2310 uint64_t end_addr
= start_addr
+ this_size
;
2311 bool toobig
= this_size
> group_size
;
2313 if (toobig
&& !this->suppress_size_errors_
)
2314 gold_warning(_("%s:%s exceeds group size"),
2315 i
->relobj()->name().c_str(),
2316 i
->relobj()->section_name(i
->shndx()).c_str());
2318 if (this->state_
!= HAS_STUB_SECTION
2319 && (!whole_sec
|| this->output_section_
!= o
)
2320 && (this->state_
== NO_GROUP
2321 || this->group_end_addr_
- end_addr
< group_size
))
2324 this->output_section_
= o
;
2327 if (this->state_
== NO_GROUP
)
2329 this->state_
= FINDING_STUB_SECTION
;
2330 this->group_end_addr_
= end_addr
;
2332 else if (this->group_end_addr_
- start_addr
< group_size
)
2334 // Adding this section would make the group larger than GROUP_SIZE.
2335 else if (this->state_
== FINDING_STUB_SECTION
2336 && !this->stubs_always_before_branch_
2339 // But wait, there's more! Input sections up to GROUP_SIZE
2340 // bytes before the stub table can be handled by it too.
2341 this->state_
= HAS_STUB_SECTION
;
2342 this->group_end_addr_
= end_addr
;
2346 this->state_
= NO_GROUP
;
2352 // Look over all the input sections, deciding where to place stubs.
2354 template<int size
, bool big_endian
>
2356 Target_powerpc
<size
, big_endian
>::group_sections(Layout
* layout
,
2359 Stub_control
stub_control(parameters
->options().stub_group_size());
2361 // Group input sections and insert stub table
2362 Stub_table
<size
, big_endian
>* stub_table
= NULL
;
2363 Layout::Section_list section_list
;
2364 layout
->get_executable_sections(§ion_list
);
2365 std::stable_sort(section_list
.begin(), section_list
.end(), Sort_sections());
2366 for (Layout::Section_list::reverse_iterator o
= section_list
.rbegin();
2367 o
!= section_list
.rend();
2370 typedef Output_section::Input_section_list Input_section_list
;
2371 for (Input_section_list::const_reverse_iterator i
2372 = (*o
)->input_sections().rbegin();
2373 i
!= (*o
)->input_sections().rend();
2376 if (i
->is_input_section())
2378 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2379 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2380 bool has14
= ppcobj
->has_14bit_branch(i
->shndx());
2381 if (!stub_control
.can_add_to_stub_group(*o
, &*i
, has14
))
2383 stub_table
->init(stub_control
.owner(),
2384 stub_control
.output_section());
2387 if (stub_table
== NULL
)
2388 stub_table
= this->new_stub_table();
2389 ppcobj
->set_stub_table(i
->shndx(), stub_table
);
2393 if (stub_table
!= NULL
)
2395 const Output_section::Input_section
* i
= stub_control
.owner();
2396 if (!i
->is_input_section())
2398 // Corner case. A new stub group was made for the first
2399 // section (last one looked at here) for some reason, but
2400 // the first section is already being used as the owner for
2401 // a stub table for following sections. Force it into that
2403 gold_assert(this->stub_tables_
.size() >= 2);
2404 this->stub_tables_
.pop_back();
2406 Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
2407 <Powerpc_relobj
<size
, big_endian
>*>(i
->relobj());
2408 ppcobj
->set_stub_table(i
->shndx(), this->stub_tables_
.back());
2411 stub_table
->init(i
, stub_control
.output_section());
2415 // If this branch needs a plt call stub, or a long branch stub, make one.
2417 template<int size
, bool big_endian
>
2419 Target_powerpc
<size
, big_endian
>::Branch_info::make_stub(
2420 Stub_table
<size
, big_endian
>* stub_table
,
2421 Stub_table
<size
, big_endian
>* ifunc_stub_table
,
2422 Symbol_table
* symtab
) const
2424 Symbol
* sym
= this->object_
->global_symbol(this->r_sym_
);
2425 if (sym
!= NULL
&& sym
->is_forwarder())
2426 sym
= symtab
->resolve_forwards(sym
);
2427 const Sized_symbol
<size
>* gsym
= static_cast<const Sized_symbol
<size
>*>(sym
);
2429 ? use_plt_offset
<size
>(gsym
, Scan::get_reference_flags(this->r_type_
))
2430 : this->object_
->local_has_plt_offset(this->r_sym_
))
2432 if (stub_table
== NULL
)
2433 stub_table
= this->object_
->stub_table(this->shndx_
);
2434 if (stub_table
== NULL
)
2436 // This is a ref from a data section to an ifunc symbol.
2437 stub_table
= ifunc_stub_table
;
2439 gold_assert(stub_table
!= NULL
);
2441 stub_table
->add_plt_call_entry(this->object_
, gsym
,
2442 this->r_type_
, this->addend_
);
2444 stub_table
->add_plt_call_entry(this->object_
, this->r_sym_
,
2445 this->r_type_
, this->addend_
);
2449 unsigned int max_branch_offset
;
2450 if (this->r_type_
== elfcpp::R_POWERPC_REL14
2451 || this->r_type_
== elfcpp::R_POWERPC_REL14_BRTAKEN
2452 || this->r_type_
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
2453 max_branch_offset
= 1 << 15;
2454 else if (this->r_type_
== elfcpp::R_POWERPC_REL24
2455 || this->r_type_
== elfcpp::R_PPC_PLTREL24
2456 || this->r_type_
== elfcpp::R_PPC_LOCAL24PC
)
2457 max_branch_offset
= 1 << 25;
2460 Address from
= this->object_
->get_output_section_offset(this->shndx_
);
2461 gold_assert(from
!= invalid_address
);
2462 from
+= (this->object_
->output_section(this->shndx_
)->address()
2467 switch (gsym
->source())
2469 case Symbol::FROM_OBJECT
:
2471 Object
* symobj
= gsym
->object();
2472 if (symobj
->is_dynamic()
2473 || symobj
->pluginobj() != NULL
)
2476 unsigned int shndx
= gsym
->shndx(&is_ordinary
);
2477 if (shndx
== elfcpp::SHN_UNDEF
)
2482 case Symbol::IS_UNDEFINED
:
2488 Symbol_table::Compute_final_value_status status
;
2489 to
= symtab
->compute_final_value
<size
>(gsym
, &status
);
2490 if (status
!= Symbol_table::CFVS_OK
)
2495 const Symbol_value
<size
>* psymval
2496 = this->object_
->local_symbol(this->r_sym_
);
2497 Symbol_value
<size
> symval
;
2498 typedef Sized_relobj_file
<size
, big_endian
> ObjType
;
2499 typename
ObjType::Compute_final_local_value_status status
2500 = this->object_
->compute_final_local_value(this->r_sym_
, psymval
,
2502 if (status
!= ObjType::CFLV_OK
2503 || !symval
.has_output_value())
2505 to
= symval
.value(this->object_
, 0);
2507 to
+= this->addend_
;
2508 if (stub_table
== NULL
)
2509 stub_table
= this->object_
->stub_table(this->shndx_
);
2510 if (size
== 64 && is_branch_reloc(this->r_type_
))
2512 unsigned int dest_shndx
;
2513 Target_powerpc
<size
, big_endian
>* target
=
2514 static_cast<Target_powerpc
<size
, big_endian
>*>(
2515 parameters
->sized_target
<size
, big_endian
>());
2516 to
= target
->symval_for_branch(symtab
, to
, gsym
,
2517 this->object_
, &dest_shndx
);
2519 Address delta
= to
- from
;
2520 if (delta
+ max_branch_offset
>= 2 * max_branch_offset
)
2522 if (stub_table
== NULL
)
2524 gold_warning(_("%s:%s: branch in non-executable section,"
2525 " no long branch stub for you"),
2526 this->object_
->name().c_str(),
2527 this->object_
->section_name(this->shndx_
).c_str());
2530 stub_table
->add_long_branch_entry(this->object_
, to
);
2535 // Relaxation hook. This is where we do stub generation.
2537 template<int size
, bool big_endian
>
2539 Target_powerpc
<size
, big_endian
>::do_relax(int pass
,
2540 const Input_objects
*,
2541 Symbol_table
* symtab
,
2545 unsigned int prev_brlt_size
= 0;
2548 bool thread_safe
= parameters
->options().plt_thread_safe();
2549 if (size
== 64 && !parameters
->options().user_set_plt_thread_safe())
2551 static const char* const thread_starter
[] =
2555 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2557 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2558 "mq_notify", "create_timer",
2562 "GOMP_parallel_start",
2563 "GOMP_parallel_loop_static_start",
2564 "GOMP_parallel_loop_dynamic_start",
2565 "GOMP_parallel_loop_guided_start",
2566 "GOMP_parallel_loop_runtime_start",
2567 "GOMP_parallel_sections_start",
2570 if (parameters
->options().shared())
2574 for (unsigned int i
= 0;
2575 i
< sizeof(thread_starter
) / sizeof(thread_starter
[0]);
2578 Symbol
* sym
= symtab
->lookup(thread_starter
[i
], NULL
);
2579 thread_safe
= (sym
!= NULL
2581 && sym
->in_real_elf());
2587 this->plt_thread_safe_
= thread_safe
;
2588 this->group_sections(layout
, task
);
2591 // We need address of stub tables valid for make_stub.
2592 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2593 p
!= this->stub_tables_
.end();
2596 const Powerpc_relobj
<size
, big_endian
>* object
2597 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>((*p
)->relobj());
2598 Address off
= object
->get_output_section_offset((*p
)->shndx());
2599 gold_assert(off
!= invalid_address
);
2600 Output_section
* os
= (*p
)->output_section();
2601 (*p
)->set_address_and_size(os
, off
);
2606 // Clear plt call stubs, long branch stubs and branch lookup table.
2607 prev_brlt_size
= this->branch_lookup_table_
.size();
2608 this->branch_lookup_table_
.clear();
2609 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2610 p
!= this->stub_tables_
.end();
2613 (*p
)->clear_stubs();
2617 // Build all the stubs.
2618 Stub_table
<size
, big_endian
>* ifunc_stub_table
2619 = this->stub_tables_
.size() == 0 ? NULL
: this->stub_tables_
[0];
2620 Stub_table
<size
, big_endian
>* one_stub_table
2621 = this->stub_tables_
.size() != 1 ? NULL
: ifunc_stub_table
;
2622 for (typename
Branches::const_iterator b
= this->branch_info_
.begin();
2623 b
!= this->branch_info_
.end();
2626 b
->make_stub(one_stub_table
, ifunc_stub_table
, symtab
);
2629 // Did anything change size?
2630 unsigned int num_huge_branches
= this->branch_lookup_table_
.size();
2631 bool again
= num_huge_branches
!= prev_brlt_size
;
2632 if (size
== 64 && num_huge_branches
!= 0)
2633 this->make_brlt_section(layout
);
2634 if (size
== 64 && again
)
2635 this->brlt_section_
->set_current_size(num_huge_branches
);
2637 typedef Unordered_set
<Output_section
*> Output_sections
;
2638 Output_sections os_need_update
;
2639 for (typename
Stub_tables::iterator p
= this->stub_tables_
.begin();
2640 p
!= this->stub_tables_
.end();
2643 if ((*p
)->size_update())
2646 (*p
)->add_eh_frame(layout
);
2647 os_need_update
.insert((*p
)->output_section());
2651 // Set output section offsets for all input sections in an output
2652 // section that just changed size. Anything past the stubs will
2654 for (typename
Output_sections::iterator p
= os_need_update
.begin();
2655 p
!= os_need_update
.end();
2658 Output_section
* os
= *p
;
2660 typedef Output_section::Input_section_list Input_section_list
;
2661 for (Input_section_list::const_iterator i
= os
->input_sections().begin();
2662 i
!= os
->input_sections().end();
2665 off
= align_address(off
, i
->addralign());
2666 if (i
->is_input_section() || i
->is_relaxed_input_section())
2667 i
->relobj()->set_section_offset(i
->shndx(), off
);
2668 if (i
->is_relaxed_input_section())
2670 Stub_table
<size
, big_endian
>* stub_table
2671 = static_cast<Stub_table
<size
, big_endian
>*>(
2672 i
->relaxed_input_section());
2673 off
+= stub_table
->set_address_and_size(os
, off
);
2676 off
+= i
->data_size();
2678 // If .branch_lt is part of this output section, then we have
2679 // just done the offset adjustment.
2680 os
->clear_section_offsets_need_adjustment();
2685 && num_huge_branches
!= 0
2686 && parameters
->options().output_is_position_independent())
2688 // Fill in the BRLT relocs.
2689 this->brlt_section_
->reset_brlt_sizes();
2690 for (typename
Branch_lookup_table::const_iterator p
2691 = this->branch_lookup_table_
.begin();
2692 p
!= this->branch_lookup_table_
.end();
2695 this->brlt_section_
->add_reloc(p
->first
, p
->second
);
2697 this->brlt_section_
->finalize_brlt_sizes();
2702 template<int size
, bool big_endian
>
2704 Target_powerpc
<size
, big_endian
>::do_plt_fde_location(const Output_data
* plt
,
2705 unsigned char* oview
,
2709 uint64_t address
= plt
->address();
2710 off_t len
= plt
->data_size();
2712 if (plt
== this->glink_
)
2714 // See Output_data_glink::do_write() for glink contents.
2717 // There is one word before __glink_PLTresolve
2721 else if (parameters
->options().output_is_position_independent())
2723 // There are two FDEs for a position independent glink.
2724 // The first covers the branch table, the second
2725 // __glink_PLTresolve at the end of glink.
2726 off_t resolve_size
= this->glink_
->pltresolve_size
;
2728 len
-= resolve_size
;
2731 address
+= len
- resolve_size
;
2738 // Must be a stub table.
2739 const Stub_table
<size
, big_endian
>* stub_table
2740 = static_cast<const Stub_table
<size
, big_endian
>*>(plt
);
2741 uint64_t stub_address
= stub_table
->stub_address();
2742 len
-= stub_address
- address
;
2743 address
= stub_address
;
2746 *paddress
= address
;
2750 // A class to handle the PLT data.
2752 template<int size
, bool big_endian
>
2753 class Output_data_plt_powerpc
: public Output_section_data_build
2756 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
2757 size
, big_endian
> Reloc_section
;
2759 Output_data_plt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
2760 Reloc_section
* plt_rel
,
2761 unsigned int reserved_size
,
2763 : Output_section_data_build(size
== 32 ? 4 : 8),
2766 initial_plt_entry_size_(reserved_size
),
2770 // Add an entry to the PLT.
2775 add_ifunc_entry(Symbol
*);
2778 add_local_ifunc_entry(Sized_relobj_file
<size
, big_endian
>*, unsigned int);
2780 // Return the .rela.plt section data.
2787 // Return the number of PLT entries.
2791 if (this->current_data_size() == 0)
2793 return ((this->current_data_size() - this->initial_plt_entry_size_
)
2797 // Return the offset of the first non-reserved PLT entry.
2799 first_plt_entry_offset()
2800 { return this->initial_plt_entry_size_
; }
2802 // Return the size of a PLT entry.
2804 get_plt_entry_size()
2805 { return plt_entry_size
; }
2809 do_adjust_output_section(Output_section
* os
)
2814 // Write to a map file.
2816 do_print_to_mapfile(Mapfile
* mapfile
) const
2817 { mapfile
->print_output_data(this, this->name_
); }
2820 // The size of an entry in the PLT.
2821 static const int plt_entry_size
= size
== 32 ? 4 : 24;
2823 // Write out the PLT data.
2825 do_write(Output_file
*);
2827 // The reloc section.
2828 Reloc_section
* rel_
;
2829 // Allows access to .glink for do_write.
2830 Target_powerpc
<size
, big_endian
>* targ_
;
2831 // The size of the first reserved entry.
2832 int initial_plt_entry_size_
;
2833 // What to report in map file.
2837 // Add an entry to the PLT.
2839 template<int size
, bool big_endian
>
2841 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
2843 if (!gsym
->has_plt_offset())
2845 section_size_type off
= this->current_data_size();
2847 off
+= this->first_plt_entry_offset();
2848 gsym
->set_plt_offset(off
);
2849 gsym
->set_needs_dynsym_entry();
2850 unsigned int dynrel
= elfcpp::R_POWERPC_JMP_SLOT
;
2851 this->rel_
->add_global(gsym
, dynrel
, this, off
, 0);
2852 off
+= plt_entry_size
;
2853 this->set_current_data_size(off
);
2857 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
2859 template<int size
, bool big_endian
>
2861 Output_data_plt_powerpc
<size
, big_endian
>::add_ifunc_entry(Symbol
* gsym
)
2863 if (!gsym
->has_plt_offset())
2865 section_size_type off
= this->current_data_size();
2866 gsym
->set_plt_offset(off
);
2867 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
2869 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
2870 this->rel_
->add_symbolless_global_addend(gsym
, dynrel
, this, off
, 0);
2871 off
+= plt_entry_size
;
2872 this->set_current_data_size(off
);
2876 // Add an entry for a local ifunc symbol to the IPLT.
2878 template<int size
, bool big_endian
>
2880 Output_data_plt_powerpc
<size
, big_endian
>::add_local_ifunc_entry(
2881 Sized_relobj_file
<size
, big_endian
>* relobj
,
2882 unsigned int local_sym_index
)
2884 if (!relobj
->local_has_plt_offset(local_sym_index
))
2886 section_size_type off
= this->current_data_size();
2887 relobj
->set_local_plt_offset(local_sym_index
, off
);
2888 unsigned int dynrel
= elfcpp::R_POWERPC_IRELATIVE
;
2890 dynrel
= elfcpp::R_PPC64_JMP_IREL
;
2891 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
, dynrel
,
2893 off
+= plt_entry_size
;
2894 this->set_current_data_size(off
);
2898 static const uint32_t add_0_11_11
= 0x7c0b5a14;
2899 static const uint32_t add_2_2_11
= 0x7c425a14;
2900 static const uint32_t add_3_3_2
= 0x7c631214;
2901 static const uint32_t add_3_3_13
= 0x7c636a14;
2902 static const uint32_t add_11_0_11
= 0x7d605a14;
2903 static const uint32_t add_12_2_11
= 0x7d825a14;
2904 static const uint32_t add_12_12_11
= 0x7d8c5a14;
2905 static const uint32_t addi_11_11
= 0x396b0000;
2906 static const uint32_t addi_12_12
= 0x398c0000;
2907 static const uint32_t addi_2_2
= 0x38420000;
2908 static const uint32_t addi_3_2
= 0x38620000;
2909 static const uint32_t addi_3_3
= 0x38630000;
2910 static const uint32_t addis_0_2
= 0x3c020000;
2911 static const uint32_t addis_0_13
= 0x3c0d0000;
2912 static const uint32_t addis_11_11
= 0x3d6b0000;
2913 static const uint32_t addis_11_30
= 0x3d7e0000;
2914 static const uint32_t addis_12_12
= 0x3d8c0000;
2915 static const uint32_t addis_12_2
= 0x3d820000;
2916 static const uint32_t addis_3_2
= 0x3c620000;
2917 static const uint32_t addis_3_13
= 0x3c6d0000;
2918 static const uint32_t b
= 0x48000000;
2919 static const uint32_t bcl_20_31
= 0x429f0005;
2920 static const uint32_t bctr
= 0x4e800420;
2921 static const uint32_t blr
= 0x4e800020;
2922 static const uint32_t blrl
= 0x4e800021;
2923 static const uint32_t bnectr_p4
= 0x4ce20420;
2924 static const uint32_t cmpldi_2_0
= 0x28220000;
2925 static const uint32_t cror_15_15_15
= 0x4def7b82;
2926 static const uint32_t cror_31_31_31
= 0x4ffffb82;
2927 static const uint32_t ld_0_1
= 0xe8010000;
2928 static const uint32_t ld_0_12
= 0xe80c0000;
2929 static const uint32_t ld_11_12
= 0xe96c0000;
2930 static const uint32_t ld_11_2
= 0xe9620000;
2931 static const uint32_t ld_2_1
= 0xe8410000;
2932 static const uint32_t ld_2_11
= 0xe84b0000;
2933 static const uint32_t ld_2_12
= 0xe84c0000;
2934 static const uint32_t ld_2_2
= 0xe8420000;
2935 static const uint32_t lfd_0_1
= 0xc8010000;
2936 static const uint32_t li_0_0
= 0x38000000;
2937 static const uint32_t li_12_0
= 0x39800000;
2938 static const uint32_t lis_0_0
= 0x3c000000;
2939 static const uint32_t lis_11
= 0x3d600000;
2940 static const uint32_t lis_12
= 0x3d800000;
2941 static const uint32_t lwz_0_12
= 0x800c0000;
2942 static const uint32_t lwz_11_11
= 0x816b0000;
2943 static const uint32_t lwz_11_30
= 0x817e0000;
2944 static const uint32_t lwz_12_12
= 0x818c0000;
2945 static const uint32_t lwzu_0_12
= 0x840c0000;
2946 static const uint32_t lvx_0_12_0
= 0x7c0c00ce;
2947 static const uint32_t mflr_0
= 0x7c0802a6;
2948 static const uint32_t mflr_11
= 0x7d6802a6;
2949 static const uint32_t mflr_12
= 0x7d8802a6;
2950 static const uint32_t mtctr_0
= 0x7c0903a6;
2951 static const uint32_t mtctr_11
= 0x7d6903a6;
2952 static const uint32_t mtctr_12
= 0x7d8903a6;
2953 static const uint32_t mtlr_0
= 0x7c0803a6;
2954 static const uint32_t mtlr_12
= 0x7d8803a6;
2955 static const uint32_t nop
= 0x60000000;
2956 static const uint32_t ori_0_0_0
= 0x60000000;
2957 static const uint32_t std_0_1
= 0xf8010000;
2958 static const uint32_t std_0_12
= 0xf80c0000;
2959 static const uint32_t std_2_1
= 0xf8410000;
2960 static const uint32_t stfd_0_1
= 0xd8010000;
2961 static const uint32_t stvx_0_12_0
= 0x7c0c01ce;
2962 static const uint32_t sub_11_11_12
= 0x7d6c5850;
2963 static const uint32_t xor_11_11_11
= 0x7d6b5a78;
2965 // Write out the PLT.
2967 template<int size
, bool big_endian
>
2969 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
2971 if (size
== 32 && this->name_
[3] != 'I')
2973 const section_size_type offset
= this->offset();
2974 const section_size_type oview_size
2975 = convert_to_section_size_type(this->data_size());
2976 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2977 unsigned char* pov
= oview
;
2978 unsigned char* endpov
= oview
+ oview_size
;
2980 // The address of the .glink branch table
2981 const Output_data_glink
<size
, big_endian
>* glink
2982 = this->targ_
->glink_section();
2983 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
= glink
->address();
2985 while (pov
< endpov
)
2987 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
2992 of
->write_output_view(offset
, oview_size
, oview
);
2996 // Create the PLT section.
2998 template<int size
, bool big_endian
>
3000 Target_powerpc
<size
, big_endian
>::make_plt_section(Symbol_table
* symtab
,
3003 if (this->plt_
== NULL
)
3005 if (this->got_
== NULL
)
3006 this->got_section(symtab
, layout
);
3008 if (this->glink_
== NULL
)
3009 make_glink_section(layout
);
3011 // Ensure that .rela.dyn always appears before .rela.plt This is
3012 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3013 // needs to include .rela.plt in its range.
3014 this->rela_dyn_section(layout
);
3016 Reloc_section
* plt_rel
= new Reloc_section(false);
3017 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
3018 elfcpp::SHF_ALLOC
, plt_rel
,
3019 ORDER_DYNAMIC_PLT_RELOCS
, false);
3021 = new Output_data_plt_powerpc
<size
, big_endian
>(this, plt_rel
,
3022 size
== 32 ? 0 : 24,
3024 layout
->add_output_section_data(".plt",
3026 ? elfcpp::SHT_PROGBITS
3027 : elfcpp::SHT_NOBITS
),
3028 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3037 // Create the IPLT section.
3039 template<int size
, bool big_endian
>
3041 Target_powerpc
<size
, big_endian
>::make_iplt_section(Symbol_table
* symtab
,
3044 if (this->iplt_
== NULL
)
3046 this->make_plt_section(symtab
, layout
);
3048 Reloc_section
* iplt_rel
= new Reloc_section(false);
3049 this->rela_dyn_
->output_section()->add_output_section_data(iplt_rel
);
3051 = new Output_data_plt_powerpc
<size
, big_endian
>(this, iplt_rel
,
3053 this->plt_
->output_section()->add_output_section_data(this->iplt_
);
3057 // A section for huge long branch addresses, similar to plt section.
3059 template<int size
, bool big_endian
>
3060 class Output_data_brlt_powerpc
: public Output_section_data_build
3063 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3064 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
3065 size
, big_endian
> Reloc_section
;
3067 Output_data_brlt_powerpc(Target_powerpc
<size
, big_endian
>* targ
,
3068 Reloc_section
* brlt_rel
)
3069 : Output_section_data_build(size
== 32 ? 4 : 8),
3077 this->reset_data_size();
3078 this->rel_
->reset_data_size();
3082 finalize_brlt_sizes()
3084 this->finalize_data_size();
3085 this->rel_
->finalize_data_size();
3088 // Add a reloc for an entry in the BRLT.
3090 add_reloc(Address to
, unsigned int off
)
3091 { this->rel_
->add_relative(elfcpp::R_POWERPC_RELATIVE
, this, off
, to
); }
3093 // Update section and reloc section size.
3095 set_current_size(unsigned int num_branches
)
3097 this->reset_address_and_file_offset();
3098 this->set_current_data_size(num_branches
* 16);
3099 this->finalize_data_size();
3100 Output_section
* os
= this->output_section();
3101 os
->set_section_offsets_need_adjustment();
3102 if (this->rel_
!= NULL
)
3104 unsigned int reloc_size
3105 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
3106 this->rel_
->reset_address_and_file_offset();
3107 this->rel_
->set_current_data_size(num_branches
* reloc_size
);
3108 this->rel_
->finalize_data_size();
3109 Output_section
* os
= this->rel_
->output_section();
3110 os
->set_section_offsets_need_adjustment();
3116 do_adjust_output_section(Output_section
* os
)
3121 // Write to a map file.
3123 do_print_to_mapfile(Mapfile
* mapfile
) const
3124 { mapfile
->print_output_data(this, "** BRLT"); }
3127 // Write out the BRLT data.
3129 do_write(Output_file
*);
3131 // The reloc section.
3132 Reloc_section
* rel_
;
3133 Target_powerpc
<size
, big_endian
>* targ_
;
3136 // Make the branch lookup table section.
3138 template<int size
, bool big_endian
>
3140 Target_powerpc
<size
, big_endian
>::make_brlt_section(Layout
* layout
)
3142 if (size
== 64 && this->brlt_section_
== NULL
)
3144 Reloc_section
* brlt_rel
= NULL
;
3145 bool is_pic
= parameters
->options().output_is_position_independent();
3148 // When PIC we can't fill in .branch_lt (like .plt it can be
3149 // a bss style section) but must initialise at runtime via
3150 // dynamic relocats.
3151 this->rela_dyn_section(layout
);
3152 brlt_rel
= new Reloc_section(false);
3153 this->rela_dyn_
->output_section()->add_output_section_data(brlt_rel
);
3156 = new Output_data_brlt_powerpc
<size
, big_endian
>(this, brlt_rel
);
3157 if (this->plt_
&& is_pic
)
3158 this->plt_
->output_section()
3159 ->add_output_section_data(this->brlt_section_
);
3161 layout
->add_output_section_data(".branch_lt",
3162 (is_pic
? elfcpp::SHT_NOBITS
3163 : elfcpp::SHT_PROGBITS
),
3164 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3165 this->brlt_section_
,
3166 (is_pic
? ORDER_SMALL_BSS
3167 : ORDER_SMALL_DATA
),
3172 // Write out .branch_lt when non-PIC.
3174 template<int size
, bool big_endian
>
3176 Output_data_brlt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
3178 if (size
== 64 && !parameters
->options().output_is_position_independent())
3180 const section_size_type offset
= this->offset();
3181 const section_size_type oview_size
3182 = convert_to_section_size_type(this->data_size());
3183 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
3185 this->targ_
->write_branch_lookup_table(oview
);
3186 of
->write_output_view(offset
, oview_size
, oview
);
3190 static inline uint32_t
3196 static inline uint32_t
3202 static inline uint32_t
3205 return hi(a
+ 0x8000);
3211 static const unsigned char eh_frame_cie
[12];
3215 const unsigned char Eh_cie
<size
>::eh_frame_cie
[] =
3218 'z', 'R', 0, // Augmentation string.
3219 4, // Code alignment.
3220 0x80 - size
/ 8 , // Data alignment.
3222 1, // Augmentation size.
3223 (elfcpp::DW_EH_PE_pcrel
3224 | elfcpp::DW_EH_PE_sdata4
), // FDE encoding.
3225 elfcpp::DW_CFA_def_cfa
, 1, 0 // def_cfa: r1 offset 0.
3228 // Describe __glink_PLTresolve use of LR, 64-bit version.
3229 static const unsigned char glink_eh_frame_fde_64
[] =
3231 0, 0, 0, 0, // Replaced with offset to .glink.
3232 0, 0, 0, 0, // Replaced with size of .glink.
3233 0, // Augmentation size.
3234 elfcpp::DW_CFA_advance_loc
+ 1,
3235 elfcpp::DW_CFA_register
, 65, 12,
3236 elfcpp::DW_CFA_advance_loc
+ 4,
3237 elfcpp::DW_CFA_restore_extended
, 65
3240 // Describe __glink_PLTresolve use of LR, 32-bit version.
3241 static const unsigned char glink_eh_frame_fde_32
[] =
3243 0, 0, 0, 0, // Replaced with offset to .glink.
3244 0, 0, 0, 0, // Replaced with size of .glink.
3245 0, // Augmentation size.
3246 elfcpp::DW_CFA_advance_loc
+ 2,
3247 elfcpp::DW_CFA_register
, 65, 0,
3248 elfcpp::DW_CFA_advance_loc
+ 4,
3249 elfcpp::DW_CFA_restore_extended
, 65
3252 static const unsigned char default_fde
[] =
3254 0, 0, 0, 0, // Replaced with offset to stubs.
3255 0, 0, 0, 0, // Replaced with size of stubs.
3256 0, // Augmentation size.
3257 elfcpp::DW_CFA_nop
, // Pad.
3262 template<bool big_endian
>
3264 write_insn(unsigned char* p
, uint32_t v
)
3266 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
3269 // Stub_table holds information about plt and long branch stubs.
3270 // Stubs are built in an area following some input section determined
3271 // by group_sections(). This input section is converted to a relaxed
3272 // input section allowing it to be resized to accommodate the stubs
3274 template<int size
, bool big_endian
>
3275 class Stub_table
: public Output_relaxed_input_section
3278 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
3279 static const Address invalid_address
= static_cast<Address
>(0) - 1;
3281 Stub_table(Target_powerpc
<size
, big_endian
>* targ
)
3282 : Output_relaxed_input_section(NULL
, 0, 0),
3283 targ_(targ
), plt_call_stubs_(), long_branch_stubs_(),
3284 orig_data_size_(0), plt_size_(0), last_plt_size_(0),
3285 branch_size_(0), last_branch_size_(0), eh_frame_added_(false)
3288 // Delayed Output_relaxed_input_section init.
3290 init(const Output_section::Input_section
*, Output_section
*);
3292 // Add a plt call stub.
3294 add_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3300 add_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3305 // Find a given plt call stub.
3307 find_plt_call_entry(const Symbol
*) const;
3310 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3311 unsigned int) const;
3314 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3320 find_plt_call_entry(const Sized_relobj_file
<size
, big_endian
>*,
3325 // Add a long branch stub.
3327 add_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*, Address
);
3330 find_long_branch_entry(const Powerpc_relobj
<size
, big_endian
>*,
3336 this->plt_call_stubs_
.clear();
3337 this->plt_size_
= 0;
3338 this->long_branch_stubs_
.clear();
3339 this->branch_size_
= 0;
3343 set_address_and_size(const Output_section
* os
, Address off
)
3345 Address start_off
= off
;
3346 off
+= this->orig_data_size_
;
3347 Address my_size
= this->plt_size_
+ this->branch_size_
;
3349 off
= align_address(off
, this->stub_align());
3350 // Include original section size and alignment padding in size
3351 my_size
+= off
- start_off
;
3352 this->reset_address_and_file_offset();
3353 this->set_current_data_size(my_size
);
3354 this->set_address_and_file_offset(os
->address() + start_off
,
3355 os
->offset() + start_off
);
3360 stub_address() const
3362 return align_address(this->address() + this->orig_data_size_
,
3363 this->stub_align());
3369 return align_address(this->offset() + this->orig_data_size_
,
3370 this->stub_align());
3375 { return this->plt_size_
; }
3380 Output_section
* os
= this->output_section();
3381 if (os
->addralign() < this->stub_align())
3383 os
->set_addralign(this->stub_align());
3384 // FIXME: get rid of the insane checkpointing.
3385 // We can't increase alignment of the input section to which
3386 // stubs are attached; The input section may be .init which
3387 // is pasted together with other .init sections to form a
3388 // function. Aligning might insert zero padding resulting in
3389 // sigill. However we do need to increase alignment of the
3390 // output section so that the align_address() on offset in
3391 // set_address_and_size() adds the same padding as the
3392 // align_address() on address in stub_address().
3393 // What's more, we need this alignment for the layout done in
3394 // relaxation_loop_body() so that the output section starts at
3395 // a suitably aligned address.
3396 os
->checkpoint_set_addralign(this->stub_align());
3398 if (this->last_plt_size_
!= this->plt_size_
3399 || this->last_branch_size_
!= this->branch_size_
)
3401 this->last_plt_size_
= this->plt_size_
;
3402 this->last_branch_size_
= this->branch_size_
;
3408 // Add .eh_frame info for this stub section. Unlike other linker
3409 // generated .eh_frame this is added late in the link, because we
3410 // only want the .eh_frame info if this particular stub section is
3413 add_eh_frame(Layout
* layout
)
3415 if (!this->eh_frame_added_
)
3417 if (!parameters
->options().ld_generated_unwind_info())
3420 // Since we add stub .eh_frame info late, it must be placed
3421 // after all other linker generated .eh_frame info so that
3422 // merge mapping need not be updated for input sections.
3423 // There is no provision to use a different CIE to that used
3425 if (!this->targ_
->has_glink())
3428 layout
->add_eh_frame_for_plt(this,
3429 Eh_cie
<size
>::eh_frame_cie
,
3430 sizeof (Eh_cie
<size
>::eh_frame_cie
),
3432 sizeof (default_fde
));
3433 this->eh_frame_added_
= true;
3437 Target_powerpc
<size
, big_endian
>*
3443 class Plt_stub_ent_hash
;
3444 typedef Unordered_map
<Plt_stub_ent
, unsigned int,
3445 Plt_stub_ent_hash
> Plt_stub_entries
;
3447 // Alignment of stub section.
3453 unsigned int min_align
= 32;
3454 unsigned int user_align
= 1 << parameters
->options().plt_align();
3455 return std::max(user_align
, min_align
);
3458 // Return the plt offset for the given call stub.
3460 plt_off(typename
Plt_stub_entries::const_iterator p
, bool* is_iplt
) const
3462 const Symbol
* gsym
= p
->first
.sym_
;
3465 *is_iplt
= (gsym
->type() == elfcpp::STT_GNU_IFUNC
3466 && gsym
->can_use_relative_reloc(false));
3467 return gsym
->plt_offset();
3472 const Sized_relobj_file
<size
, big_endian
>* relobj
= p
->first
.object_
;
3473 unsigned int local_sym_index
= p
->first
.locsym_
;
3474 return relobj
->local_plt_offset(local_sym_index
);
3478 // Size of a given plt call stub.
3480 plt_call_size(typename
Plt_stub_entries::const_iterator p
) const
3486 Address plt_addr
= this->plt_off(p
, &is_iplt
);
3488 plt_addr
+= this->targ_
->iplt_section()->address();
3490 plt_addr
+= this->targ_
->plt_section()->address();
3491 Address got_addr
= this->targ_
->got_section()->output_section()->address();
3492 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3493 <const Powerpc_relobj
<size
, big_endian
>*>(p
->first
.object_
);
3494 got_addr
+= ppcobj
->toc_base_offset();
3495 Address off
= plt_addr
- got_addr
;
3496 bool static_chain
= parameters
->options().plt_static_chain();
3497 bool thread_safe
= this->targ_
->plt_thread_safe();
3498 unsigned int bytes
= (4 * 5
3501 + 4 * (ha(off
) != 0)
3502 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
)));
3503 unsigned int align
= 1 << parameters
->options().plt_align();
3505 bytes
= (bytes
+ align
- 1) & -align
;
3509 // Return long branch stub size.
3511 branch_stub_size(Address to
)
3514 = this->stub_address() + this->last_plt_size_
+ this->branch_size_
;
3515 if (to
- loc
+ (1 << 25) < 2 << 25)
3517 if (size
== 64 || !parameters
->options().output_is_position_independent())
3524 do_write(Output_file
*);
3526 // Plt call stub keys.
3530 Plt_stub_ent(const Symbol
* sym
)
3531 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3534 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3535 unsigned int locsym_index
)
3536 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3539 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3541 unsigned int r_type
,
3543 : sym_(sym
), object_(0), addend_(0), locsym_(0)
3546 this->addend_
= addend
;
3547 else if (parameters
->options().output_is_position_independent()
3548 && r_type
== elfcpp::R_PPC_PLTREL24
)
3550 this->addend_
= addend
;
3551 if (this->addend_
>= 32768)
3552 this->object_
= object
;
3556 Plt_stub_ent(const Sized_relobj_file
<size
, big_endian
>* object
,
3557 unsigned int locsym_index
,
3558 unsigned int r_type
,
3560 : sym_(NULL
), object_(object
), addend_(0), locsym_(locsym_index
)
3563 this->addend_
= addend
;
3564 else if (parameters
->options().output_is_position_independent()
3565 && r_type
== elfcpp::R_PPC_PLTREL24
)
3566 this->addend_
= addend
;
3569 bool operator==(const Plt_stub_ent
& that
) const
3571 return (this->sym_
== that
.sym_
3572 && this->object_
== that
.object_
3573 && this->addend_
== that
.addend_
3574 && this->locsym_
== that
.locsym_
);
3578 const Sized_relobj_file
<size
, big_endian
>* object_
;
3579 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend_
;
3580 unsigned int locsym_
;
3583 class Plt_stub_ent_hash
3586 size_t operator()(const Plt_stub_ent
& ent
) const
3588 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
3589 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
3595 // Long branch stub keys.
3596 class Branch_stub_ent
3599 Branch_stub_ent(const Powerpc_relobj
<size
, big_endian
>* obj
, Address to
)
3600 : dest_(to
), toc_base_off_(0)
3603 toc_base_off_
= obj
->toc_base_offset();
3606 bool operator==(const Branch_stub_ent
& that
) const
3608 return (this->dest_
== that
.dest_
3610 || this->toc_base_off_
== that
.toc_base_off_
));
3614 unsigned int toc_base_off_
;
3617 class Branch_stub_ent_hash
3620 size_t operator()(const Branch_stub_ent
& ent
) const
3621 { return ent
.dest_
^ ent
.toc_base_off_
; }
3624 // In a sane world this would be a global.
3625 Target_powerpc
<size
, big_endian
>* targ_
;
3626 // Map sym/object/addend to stub offset.
3627 Plt_stub_entries plt_call_stubs_
;
3628 // Map destination address to stub offset.
3629 typedef Unordered_map
<Branch_stub_ent
, unsigned int,
3630 Branch_stub_ent_hash
> Branch_stub_entries
;
3631 Branch_stub_entries long_branch_stubs_
;
3632 // size of input section
3633 section_size_type orig_data_size_
;
3635 section_size_type plt_size_
, last_plt_size_
, branch_size_
, last_branch_size_
;
3636 // Whether .eh_frame info has been created for this stub section.
3637 bool eh_frame_added_
;
3640 // Make a new stub table, and record.
3642 template<int size
, bool big_endian
>
3643 Stub_table
<size
, big_endian
>*
3644 Target_powerpc
<size
, big_endian
>::new_stub_table()
3646 Stub_table
<size
, big_endian
>* stub_table
3647 = new Stub_table
<size
, big_endian
>(this);
3648 this->stub_tables_
.push_back(stub_table
);
3652 // Delayed stub table initialisation, because we create the stub table
3653 // before we know to which section it will be attached.
3655 template<int size
, bool big_endian
>
3657 Stub_table
<size
, big_endian
>::init(
3658 const Output_section::Input_section
* owner
,
3659 Output_section
* output_section
)
3661 this->set_relobj(owner
->relobj());
3662 this->set_shndx(owner
->shndx());
3663 this->set_addralign(this->relobj()->section_addralign(this->shndx()));
3664 this->set_output_section(output_section
);
3665 this->orig_data_size_
= owner
->current_data_size();
3667 std::vector
<Output_relaxed_input_section
*> new_relaxed
;
3668 new_relaxed
.push_back(this);
3669 output_section
->convert_input_sections_to_relaxed_sections(new_relaxed
);
3672 // Add a plt call stub, if we do not already have one for this
3673 // sym/object/addend combo.
3675 template<int size
, bool big_endian
>
3677 Stub_table
<size
, big_endian
>::add_plt_call_entry(
3678 const Sized_relobj_file
<size
, big_endian
>* object
,
3680 unsigned int r_type
,
3683 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
3684 Address off
= this->plt_size_
;
3685 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
3686 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
3688 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
3691 template<int size
, bool big_endian
>
3693 Stub_table
<size
, big_endian
>::add_plt_call_entry(
3694 const Sized_relobj_file
<size
, big_endian
>* object
,
3695 unsigned int locsym_index
,
3696 unsigned int r_type
,
3699 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
3700 Address off
= this->plt_size_
;
3701 std::pair
<typename
Plt_stub_entries::iterator
, bool> p
3702 = this->plt_call_stubs_
.insert(std::make_pair(ent
, off
));
3704 this->plt_size_
= off
+ this->plt_call_size(p
.first
);
3707 // Find a plt call stub.
3709 template<int size
, bool big_endian
>
3710 typename Stub_table
<size
, big_endian
>::Address
3711 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3712 const Sized_relobj_file
<size
, big_endian
>* object
,
3714 unsigned int r_type
,
3715 Address addend
) const
3717 Plt_stub_ent
ent(object
, gsym
, r_type
, addend
);
3718 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3719 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3722 template<int size
, bool big_endian
>
3723 typename Stub_table
<size
, big_endian
>::Address
3724 Stub_table
<size
, big_endian
>::find_plt_call_entry(const Symbol
* gsym
) const
3726 Plt_stub_ent
ent(gsym
);
3727 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3728 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3731 template<int size
, bool big_endian
>
3732 typename Stub_table
<size
, big_endian
>::Address
3733 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3734 const Sized_relobj_file
<size
, big_endian
>* object
,
3735 unsigned int locsym_index
,
3736 unsigned int r_type
,
3737 Address addend
) const
3739 Plt_stub_ent
ent(object
, locsym_index
, r_type
, addend
);
3740 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3741 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3744 template<int size
, bool big_endian
>
3745 typename Stub_table
<size
, big_endian
>::Address
3746 Stub_table
<size
, big_endian
>::find_plt_call_entry(
3747 const Sized_relobj_file
<size
, big_endian
>* object
,
3748 unsigned int locsym_index
) const
3750 Plt_stub_ent
ent(object
, locsym_index
);
3751 typename
Plt_stub_entries::const_iterator p
= this->plt_call_stubs_
.find(ent
);
3752 return p
== this->plt_call_stubs_
.end() ? invalid_address
: p
->second
;
3755 // Add a long branch stub if we don't already have one to given
3758 template<int size
, bool big_endian
>
3760 Stub_table
<size
, big_endian
>::add_long_branch_entry(
3761 const Powerpc_relobj
<size
, big_endian
>* object
,
3764 Branch_stub_ent
ent(object
, to
);
3765 Address off
= this->branch_size_
;
3766 if (this->long_branch_stubs_
.insert(std::make_pair(ent
, off
)).second
)
3768 unsigned int stub_size
= this->branch_stub_size(to
);
3769 this->branch_size_
= off
+ stub_size
;
3770 if (size
== 64 && stub_size
!= 4)
3771 this->targ_
->add_branch_lookup_table(to
);
3775 // Find long branch stub.
3777 template<int size
, bool big_endian
>
3778 typename Stub_table
<size
, big_endian
>::Address
3779 Stub_table
<size
, big_endian
>::find_long_branch_entry(
3780 const Powerpc_relobj
<size
, big_endian
>* object
,
3783 Branch_stub_ent
ent(object
, to
);
3784 typename
Branch_stub_entries::const_iterator p
3785 = this->long_branch_stubs_
.find(ent
);
3786 return p
== this->long_branch_stubs_
.end() ? invalid_address
: p
->second
;
3789 // A class to handle .glink.
3791 template<int size
, bool big_endian
>
3792 class Output_data_glink
: public Output_section_data
3795 static const int pltresolve_size
= 16*4;
3797 Output_data_glink(Target_powerpc
<size
, big_endian
>* targ
)
3798 : Output_section_data(16), targ_(targ
)
3802 add_eh_frame(Layout
* layout
)
3804 if (!parameters
->options().ld_generated_unwind_info())
3808 layout
->add_eh_frame_for_plt(this,
3809 Eh_cie
<64>::eh_frame_cie
,
3810 sizeof (Eh_cie
<64>::eh_frame_cie
),
3811 glink_eh_frame_fde_64
,
3812 sizeof (glink_eh_frame_fde_64
));
3815 // 32-bit .glink can use the default since the CIE return
3816 // address reg, LR, is valid.
3817 layout
->add_eh_frame_for_plt(this,
3818 Eh_cie
<32>::eh_frame_cie
,
3819 sizeof (Eh_cie
<32>::eh_frame_cie
),
3821 sizeof (default_fde
));
3822 // Except where LR is used in a PIC __glink_PLTresolve.
3823 if (parameters
->options().output_is_position_independent())
3824 layout
->add_eh_frame_for_plt(this,
3825 Eh_cie
<32>::eh_frame_cie
,
3826 sizeof (Eh_cie
<32>::eh_frame_cie
),
3827 glink_eh_frame_fde_32
,
3828 sizeof (glink_eh_frame_fde_32
));
3833 // Write to a map file.
3835 do_print_to_mapfile(Mapfile
* mapfile
) const
3836 { mapfile
->print_output_data(this, _("** glink")); }
3840 set_final_data_size();
3844 do_write(Output_file
*);
3846 // Allows access to .got and .plt for do_write.
3847 Target_powerpc
<size
, big_endian
>* targ_
;
3850 template<int size
, bool big_endian
>
3852 Output_data_glink
<size
, big_endian
>::set_final_data_size()
3854 unsigned int count
= this->targ_
->plt_entry_count();
3855 section_size_type total
= 0;
3861 // space for branch table
3862 total
+= 4 * (count
- 1);
3864 total
+= -total
& 15;
3865 total
+= this->pltresolve_size
;
3869 total
+= this->pltresolve_size
;
3871 // space for branch table
3874 total
+= 4 * (count
- 0x8000);
3878 this->set_data_size(total
);
3881 // Write out plt and long branch stub code.
3883 template<int size
, bool big_endian
>
3885 Stub_table
<size
, big_endian
>::do_write(Output_file
* of
)
3887 if (this->plt_call_stubs_
.empty()
3888 && this->long_branch_stubs_
.empty())
3891 const section_size_type start_off
= this->offset();
3892 const section_size_type off
= this->stub_offset();
3893 const section_size_type oview_size
=
3894 convert_to_section_size_type(this->data_size() - (off
- start_off
));
3895 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
3900 const Output_data_got_powerpc
<size
, big_endian
>* got
3901 = this->targ_
->got_section();
3902 Address got_os_addr
= got
->output_section()->address();
3904 if (!this->plt_call_stubs_
.empty())
3906 // The base address of the .plt section.
3907 Address plt_base
= this->targ_
->plt_section()->address();
3908 Address iplt_base
= invalid_address
;
3910 // Write out plt call stubs.
3911 typename
Plt_stub_entries::const_iterator cs
;
3912 for (cs
= this->plt_call_stubs_
.begin();
3913 cs
!= this->plt_call_stubs_
.end();
3917 Address pltoff
= this->plt_off(cs
, &is_iplt
);
3918 Address plt_addr
= pltoff
;
3921 if (iplt_base
== invalid_address
)
3922 iplt_base
= this->targ_
->iplt_section()->address();
3923 plt_addr
+= iplt_base
;
3926 plt_addr
+= plt_base
;
3927 const Powerpc_relobj
<size
, big_endian
>* ppcobj
= static_cast
3928 <const Powerpc_relobj
<size
, big_endian
>*>(cs
->first
.object_
);
3929 Address got_addr
= got_os_addr
+ ppcobj
->toc_base_offset();
3930 Address off
= plt_addr
- got_addr
;
3932 if (off
+ 0x80008000 > 0xffffffff || (off
& 7) != 0)
3933 gold_error(_("%s: linkage table error against `%s'"),
3934 cs
->first
.object_
->name().c_str(),
3935 cs
->first
.sym_
->demangled_name().c_str());
3937 bool static_chain
= parameters
->options().plt_static_chain();
3938 bool thread_safe
= this->targ_
->plt_thread_safe();
3939 bool use_fake_dep
= false;
3940 Address cmp_branch_off
= 0;
3943 unsigned int pltindex
3944 = ((pltoff
- this->targ_
->first_plt_entry_offset())
3945 / this->targ_
->plt_entry_size());
3947 = (this->targ_
->glink_section()->pltresolve_size
3949 if (pltindex
> 32768)
3950 glinkoff
+= (pltindex
- 32768) * 4;
3952 = this->targ_
->glink_section()->address() + glinkoff
;
3954 = (this->stub_address() + cs
->second
+ 24
3955 + 4 * (ha(off
) != 0)
3956 + 4 * (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
3957 + 4 * static_chain
);
3958 cmp_branch_off
= to
- from
;
3959 use_fake_dep
= cmp_branch_off
+ (1 << 25) >= (1 << 26);
3962 p
= oview
+ cs
->second
;
3965 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
3966 write_insn
<big_endian
>(p
, addis_12_2
+ ha(off
)), p
+= 4;
3967 write_insn
<big_endian
>(p
, ld_11_12
+ l(off
)), p
+= 4;
3968 if (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
3970 write_insn
<big_endian
>(p
, addi_12_12
+ l(off
)), p
+= 4;
3973 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3976 write_insn
<big_endian
>(p
, xor_11_11_11
), p
+= 4;
3977 write_insn
<big_endian
>(p
, add_12_12_11
), p
+= 4;
3979 write_insn
<big_endian
>(p
, ld_2_12
+ l(off
+ 8)), p
+= 4;
3981 write_insn
<big_endian
>(p
, ld_11_12
+ l(off
+ 16)), p
+= 4;
3985 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
3986 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
)), p
+= 4;
3987 if (ha(off
+ 8 + 8 * static_chain
) != ha(off
))
3989 write_insn
<big_endian
>(p
, addi_2_2
+ l(off
)), p
+= 4;
3992 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
3995 write_insn
<big_endian
>(p
, xor_11_11_11
), p
+= 4;
3996 write_insn
<big_endian
>(p
, add_2_2_11
), p
+= 4;
3999 write_insn
<big_endian
>(p
, ld_11_2
+ l(off
+ 16)), p
+= 4;
4000 write_insn
<big_endian
>(p
, ld_2_2
+ l(off
+ 8)), p
+= 4;
4002 if (thread_safe
&& !use_fake_dep
)
4004 write_insn
<big_endian
>(p
, cmpldi_2_0
), p
+= 4;
4005 write_insn
<big_endian
>(p
, bnectr_p4
), p
+= 4;
4006 write_insn
<big_endian
>(p
, b
| (cmp_branch_off
& 0x3fffffc));
4009 write_insn
<big_endian
>(p
, bctr
);
4013 // Write out long branch stubs.
4014 typename
Branch_stub_entries::const_iterator bs
;
4015 for (bs
= this->long_branch_stubs_
.begin();
4016 bs
!= this->long_branch_stubs_
.end();
4019 p
= oview
+ this->plt_size_
+ bs
->second
;
4020 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
4021 Address delta
= bs
->first
.dest_
- loc
;
4022 if (delta
+ (1 << 25) < 2 << 25)
4023 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
4027 = this->targ_
->find_branch_lookup_table(bs
->first
.dest_
);
4028 gold_assert(brlt_addr
!= invalid_address
);
4029 brlt_addr
+= this->targ_
->brlt_section()->address();
4030 Address got_addr
= got_os_addr
+ bs
->first
.toc_base_off_
;
4031 Address brltoff
= brlt_addr
- got_addr
;
4032 if (ha(brltoff
) == 0)
4034 write_insn
<big_endian
>(p
, ld_11_2
+ l(brltoff
)), p
+= 4;
4038 write_insn
<big_endian
>(p
, addis_12_2
+ ha(brltoff
)), p
+= 4;
4039 write_insn
<big_endian
>(p
, ld_11_12
+ l(brltoff
)), p
+= 4;
4041 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
4042 write_insn
<big_endian
>(p
, bctr
);
4048 if (!this->plt_call_stubs_
.empty())
4050 // The base address of the .plt section.
4051 Address plt_base
= this->targ_
->plt_section()->address();
4052 Address iplt_base
= invalid_address
;
4053 // The address of _GLOBAL_OFFSET_TABLE_.
4054 Address g_o_t
= invalid_address
;
4056 // Write out plt call stubs.
4057 typename
Plt_stub_entries::const_iterator cs
;
4058 for (cs
= this->plt_call_stubs_
.begin();
4059 cs
!= this->plt_call_stubs_
.end();
4063 Address plt_addr
= this->plt_off(cs
, &is_iplt
);
4066 if (iplt_base
== invalid_address
)
4067 iplt_base
= this->targ_
->iplt_section()->address();
4068 plt_addr
+= iplt_base
;
4071 plt_addr
+= plt_base
;
4073 p
= oview
+ cs
->second
;
4074 if (parameters
->options().output_is_position_independent())
4077 const Powerpc_relobj
<size
, big_endian
>* ppcobj
4078 = (static_cast<const Powerpc_relobj
<size
, big_endian
>*>
4079 (cs
->first
.object_
));
4080 if (ppcobj
!= NULL
&& cs
->first
.addend_
>= 32768)
4082 unsigned int got2
= ppcobj
->got2_shndx();
4083 got_addr
= ppcobj
->get_output_section_offset(got2
);
4084 gold_assert(got_addr
!= invalid_address
);
4085 got_addr
+= (ppcobj
->output_section(got2
)->address()
4086 + cs
->first
.addend_
);
4090 if (g_o_t
== invalid_address
)
4092 const Output_data_got_powerpc
<size
, big_endian
>* got
4093 = this->targ_
->got_section();
4094 g_o_t
= got
->address() + got
->g_o_t();
4099 Address off
= plt_addr
- got_addr
;
4102 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(off
));
4103 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
4104 write_insn
<big_endian
>(p
+ 8, bctr
);
4108 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(off
));
4109 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(off
));
4110 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
4111 write_insn
<big_endian
>(p
+ 12, bctr
);
4116 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
4117 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
4118 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
4119 write_insn
<big_endian
>(p
+ 12, bctr
);
4124 // Write out long branch stubs.
4125 typename
Branch_stub_entries::const_iterator bs
;
4126 for (bs
= this->long_branch_stubs_
.begin();
4127 bs
!= this->long_branch_stubs_
.end();
4130 p
= oview
+ this->plt_size_
+ bs
->second
;
4131 Address loc
= this->stub_address() + this->plt_size_
+ bs
->second
;
4132 Address delta
= bs
->first
.dest_
- loc
;
4133 if (delta
+ (1 << 25) < 2 << 25)
4134 write_insn
<big_endian
>(p
, b
| (delta
& 0x3fffffc));
4135 else if (!parameters
->options().output_is_position_independent())
4137 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(bs
->first
.dest_
));
4138 write_insn
<big_endian
>(p
+ 4, addi_12_12
+ l(bs
->first
.dest_
));
4139 write_insn
<big_endian
>(p
+ 8, mtctr_12
);
4140 write_insn
<big_endian
>(p
+ 12, bctr
);
4145 write_insn
<big_endian
>(p
+ 0, mflr_0
);
4146 write_insn
<big_endian
>(p
+ 4, bcl_20_31
);
4147 write_insn
<big_endian
>(p
+ 8, mflr_12
);
4148 write_insn
<big_endian
>(p
+ 12, addis_12_12
+ ha(delta
));
4149 write_insn
<big_endian
>(p
+ 16, addi_12_12
+ l(delta
));
4150 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
4151 write_insn
<big_endian
>(p
+ 24, mtctr_12
);
4152 write_insn
<big_endian
>(p
+ 28, bctr
);
4158 // Write out .glink.
4160 template<int size
, bool big_endian
>
4162 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
4164 const section_size_type off
= this->offset();
4165 const section_size_type oview_size
=
4166 convert_to_section_size_type(this->data_size());
4167 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4170 // The base address of the .plt section.
4171 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
4172 Address plt_base
= this->targ_
->plt_section()->address();
4176 // Write pltresolve stub.
4178 Address after_bcl
= this->address() + 16;
4179 Address pltoff
= plt_base
- after_bcl
;
4181 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
4183 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
4184 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
4185 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
4186 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
4187 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
4188 write_insn
<big_endian
>(p
, add_12_2_11
), p
+= 4;
4189 write_insn
<big_endian
>(p
, ld_11_12
+ 0), p
+= 4;
4190 write_insn
<big_endian
>(p
, ld_2_12
+ 8), p
+= 4;
4191 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
4192 write_insn
<big_endian
>(p
, ld_11_12
+ 16), p
+= 4;
4193 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
4194 while (p
< oview
+ this->pltresolve_size
)
4195 write_insn
<big_endian
>(p
, nop
), p
+= 4;
4197 // Write lazy link call stubs.
4199 while (p
< oview
+ oview_size
)
4203 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
4207 write_insn
<big_endian
>(p
, lis_0_0
+ hi(indx
)), p
+= 4;
4208 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
4210 uint32_t branch_off
= 8 - (p
- oview
);
4211 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
4217 const Output_data_got_powerpc
<size
, big_endian
>* got
4218 = this->targ_
->got_section();
4219 // The address of _GLOBAL_OFFSET_TABLE_.
4220 Address g_o_t
= got
->address() + got
->g_o_t();
4222 // Write out pltresolve branch table.
4224 unsigned int the_end
= oview_size
- this->pltresolve_size
;
4225 unsigned char* end_p
= oview
+ the_end
;
4226 while (p
< end_p
- 8 * 4)
4227 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
4229 write_insn
<big_endian
>(p
, nop
), p
+= 4;
4231 // Write out pltresolve call stub.
4232 if (parameters
->options().output_is_position_independent())
4234 Address res0_off
= 0;
4235 Address after_bcl_off
= the_end
+ 12;
4236 Address bcl_res0
= after_bcl_off
- res0_off
;
4238 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
4239 write_insn
<big_endian
>(p
+ 4, mflr_0
);
4240 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
4241 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
4242 write_insn
<big_endian
>(p
+ 16, mflr_12
);
4243 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
4244 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
4246 Address got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
4248 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
4249 if (ha(got_bcl
) == ha(got_bcl
+ 4))
4251 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
4252 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
4256 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
4257 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
4259 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
4260 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
4261 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
4262 write_insn
<big_endian
>(p
+ 52, bctr
);
4263 write_insn
<big_endian
>(p
+ 56, nop
);
4264 write_insn
<big_endian
>(p
+ 60, nop
);
4268 Address res0
= this->address();
4270 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
4271 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
4272 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
4273 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
4275 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
4276 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
4277 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
4278 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
4279 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
4280 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
4282 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
4283 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
4284 write_insn
<big_endian
>(p
+ 32, bctr
);
4285 write_insn
<big_endian
>(p
+ 36, nop
);
4286 write_insn
<big_endian
>(p
+ 40, nop
);
4287 write_insn
<big_endian
>(p
+ 44, nop
);
4288 write_insn
<big_endian
>(p
+ 48, nop
);
4289 write_insn
<big_endian
>(p
+ 52, nop
);
4290 write_insn
<big_endian
>(p
+ 56, nop
);
4291 write_insn
<big_endian
>(p
+ 60, nop
);
4296 of
->write_output_view(off
, oview_size
, oview
);
4300 // A class to handle linker generated save/restore functions.
4302 template<int size
, bool big_endian
>
4303 class Output_data_save_res
: public Output_section_data_build
4306 Output_data_save_res(Symbol_table
* symtab
);
4309 // Write to a map file.
4311 do_print_to_mapfile(Mapfile
* mapfile
) const
4312 { mapfile
->print_output_data(this, _("** save/restore")); }
4315 do_write(Output_file
*);
4318 // The maximum size of save/restore contents.
4319 static const unsigned int savres_max
= 218*4;
4322 savres_define(Symbol_table
* symtab
,
4324 unsigned int lo
, unsigned int hi
,
4325 unsigned char* write_ent(unsigned char*, int),
4326 unsigned char* write_tail(unsigned char*, int));
4328 unsigned char *contents_
;
4331 template<bool big_endian
>
4332 static unsigned char*
4333 savegpr0(unsigned char* p
, int r
)
4335 uint32_t insn
= std_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4336 write_insn
<big_endian
>(p
, insn
);
4340 template<bool big_endian
>
4341 static unsigned char*
4342 savegpr0_tail(unsigned char* p
, int r
)
4344 p
= savegpr0
<big_endian
>(p
, r
);
4345 uint32_t insn
= std_0_1
+ 16;
4346 write_insn
<big_endian
>(p
, insn
);
4348 write_insn
<big_endian
>(p
, blr
);
4352 template<bool big_endian
>
4353 static unsigned char*
4354 restgpr0(unsigned char* p
, int r
)
4356 uint32_t insn
= ld_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4357 write_insn
<big_endian
>(p
, insn
);
4361 template<bool big_endian
>
4362 static unsigned char*
4363 restgpr0_tail(unsigned char* p
, int r
)
4365 uint32_t insn
= ld_0_1
+ 16;
4366 write_insn
<big_endian
>(p
, insn
);
4368 p
= restgpr0
<big_endian
>(p
, r
);
4369 write_insn
<big_endian
>(p
, mtlr_0
);
4373 p
= restgpr0
<big_endian
>(p
, 30);
4374 p
= restgpr0
<big_endian
>(p
, 31);
4376 write_insn
<big_endian
>(p
, blr
);
4380 template<bool big_endian
>
4381 static unsigned char*
4382 savegpr1(unsigned char* p
, int r
)
4384 uint32_t insn
= std_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4385 write_insn
<big_endian
>(p
, insn
);
4389 template<bool big_endian
>
4390 static unsigned char*
4391 savegpr1_tail(unsigned char* p
, int r
)
4393 p
= savegpr1
<big_endian
>(p
, r
);
4394 write_insn
<big_endian
>(p
, blr
);
4398 template<bool big_endian
>
4399 static unsigned char*
4400 restgpr1(unsigned char* p
, int r
)
4402 uint32_t insn
= ld_0_12
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4403 write_insn
<big_endian
>(p
, insn
);
4407 template<bool big_endian
>
4408 static unsigned char*
4409 restgpr1_tail(unsigned char* p
, int r
)
4411 p
= restgpr1
<big_endian
>(p
, r
);
4412 write_insn
<big_endian
>(p
, blr
);
4416 template<bool big_endian
>
4417 static unsigned char*
4418 savefpr(unsigned char* p
, int r
)
4420 uint32_t insn
= stfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4421 write_insn
<big_endian
>(p
, insn
);
4425 template<bool big_endian
>
4426 static unsigned char*
4427 savefpr0_tail(unsigned char* p
, int r
)
4429 p
= savefpr
<big_endian
>(p
, r
);
4430 write_insn
<big_endian
>(p
, std_0_1
+ 16);
4432 write_insn
<big_endian
>(p
, blr
);
4436 template<bool big_endian
>
4437 static unsigned char*
4438 restfpr(unsigned char* p
, int r
)
4440 uint32_t insn
= lfd_0_1
+ (r
<< 21) + (1 << 16) - (32 - r
) * 8;
4441 write_insn
<big_endian
>(p
, insn
);
4445 template<bool big_endian
>
4446 static unsigned char*
4447 restfpr0_tail(unsigned char* p
, int r
)
4449 write_insn
<big_endian
>(p
, ld_0_1
+ 16);
4451 p
= restfpr
<big_endian
>(p
, r
);
4452 write_insn
<big_endian
>(p
, mtlr_0
);
4456 p
= restfpr
<big_endian
>(p
, 30);
4457 p
= restfpr
<big_endian
>(p
, 31);
4459 write_insn
<big_endian
>(p
, blr
);
4463 template<bool big_endian
>
4464 static unsigned char*
4465 savefpr1_tail(unsigned char* p
, int r
)
4467 p
= savefpr
<big_endian
>(p
, r
);
4468 write_insn
<big_endian
>(p
, blr
);
4472 template<bool big_endian
>
4473 static unsigned char*
4474 restfpr1_tail(unsigned char* p
, int r
)
4476 p
= restfpr
<big_endian
>(p
, r
);
4477 write_insn
<big_endian
>(p
, blr
);
4481 template<bool big_endian
>
4482 static unsigned char*
4483 savevr(unsigned char* p
, int r
)
4485 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
4486 write_insn
<big_endian
>(p
, insn
);
4488 insn
= stvx_0_12_0
+ (r
<< 21);
4489 write_insn
<big_endian
>(p
, insn
);
4493 template<bool big_endian
>
4494 static unsigned char*
4495 savevr_tail(unsigned char* p
, int r
)
4497 p
= savevr
<big_endian
>(p
, r
);
4498 write_insn
<big_endian
>(p
, blr
);
4502 template<bool big_endian
>
4503 static unsigned char*
4504 restvr(unsigned char* p
, int r
)
4506 uint32_t insn
= li_12_0
+ (1 << 16) - (32 - r
) * 16;
4507 write_insn
<big_endian
>(p
, insn
);
4509 insn
= lvx_0_12_0
+ (r
<< 21);
4510 write_insn
<big_endian
>(p
, insn
);
4514 template<bool big_endian
>
4515 static unsigned char*
4516 restvr_tail(unsigned char* p
, int r
)
4518 p
= restvr
<big_endian
>(p
, r
);
4519 write_insn
<big_endian
>(p
, blr
);
4524 template<int size
, bool big_endian
>
4525 Output_data_save_res
<size
, big_endian
>::Output_data_save_res(
4526 Symbol_table
* symtab
)
4527 : Output_section_data_build(4),
4530 this->savres_define(symtab
,
4531 "_savegpr0_", 14, 31,
4532 savegpr0
<big_endian
>, savegpr0_tail
<big_endian
>);
4533 this->savres_define(symtab
,
4534 "_restgpr0_", 14, 29,
4535 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
4536 this->savres_define(symtab
,
4537 "_restgpr0_", 30, 31,
4538 restgpr0
<big_endian
>, restgpr0_tail
<big_endian
>);
4539 this->savres_define(symtab
,
4540 "_savegpr1_", 14, 31,
4541 savegpr1
<big_endian
>, savegpr1_tail
<big_endian
>);
4542 this->savres_define(symtab
,
4543 "_restgpr1_", 14, 31,
4544 restgpr1
<big_endian
>, restgpr1_tail
<big_endian
>);
4545 this->savres_define(symtab
,
4546 "_savefpr_", 14, 31,
4547 savefpr
<big_endian
>, savefpr0_tail
<big_endian
>);
4548 this->savres_define(symtab
,
4549 "_restfpr_", 14, 29,
4550 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
4551 this->savres_define(symtab
,
4552 "_restfpr_", 30, 31,
4553 restfpr
<big_endian
>, restfpr0_tail
<big_endian
>);
4554 this->savres_define(symtab
,
4556 savefpr
<big_endian
>, savefpr1_tail
<big_endian
>);
4557 this->savres_define(symtab
,
4559 restfpr
<big_endian
>, restfpr1_tail
<big_endian
>);
4560 this->savres_define(symtab
,
4562 savevr
<big_endian
>, savevr_tail
<big_endian
>);
4563 this->savres_define(symtab
,
4565 restvr
<big_endian
>, restvr_tail
<big_endian
>);
4568 template<int size
, bool big_endian
>
4570 Output_data_save_res
<size
, big_endian
>::savres_define(
4571 Symbol_table
* symtab
,
4573 unsigned int lo
, unsigned int hi
,
4574 unsigned char* write_ent(unsigned char*, int),
4575 unsigned char* write_tail(unsigned char*, int))
4577 size_t len
= strlen(name
);
4578 bool writing
= false;
4581 memcpy(sym
, name
, len
);
4584 for (unsigned int i
= lo
; i
<= hi
; i
++)
4586 sym
[len
+ 0] = i
/ 10 + '0';
4587 sym
[len
+ 1] = i
% 10 + '0';
4588 Symbol
* gsym
= symtab
->lookup(sym
);
4589 bool refd
= gsym
!= NULL
&& gsym
->is_undefined();
4590 writing
= writing
|| refd
;
4593 if (this->contents_
== NULL
)
4594 this->contents_
= new unsigned char[this->savres_max
];
4596 section_size_type value
= this->current_data_size();
4597 unsigned char* p
= this->contents_
+ value
;
4599 p
= write_ent(p
, i
);
4601 p
= write_tail(p
, i
);
4602 section_size_type cur_size
= p
- this->contents_
;
4603 this->set_current_data_size(cur_size
);
4605 symtab
->define_in_output_data(sym
, NULL
, Symbol_table::PREDEFINED
,
4606 this, value
, cur_size
- value
,
4607 elfcpp::STT_FUNC
, elfcpp::STB_GLOBAL
,
4608 elfcpp::STV_HIDDEN
, 0, false, false);
4613 // Write out save/restore.
4615 template<int size
, bool big_endian
>
4617 Output_data_save_res
<size
, big_endian
>::do_write(Output_file
* of
)
4619 const section_size_type off
= this->offset();
4620 const section_size_type oview_size
=
4621 convert_to_section_size_type(this->data_size());
4622 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
4623 memcpy(oview
, this->contents_
, oview_size
);
4624 of
->write_output_view(off
, oview_size
, oview
);
4628 // Create the glink section.
4630 template<int size
, bool big_endian
>
4632 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
4634 if (this->glink_
== NULL
)
4636 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
4637 this->glink_
->add_eh_frame(layout
);
4638 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
4639 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
4640 this->glink_
, ORDER_TEXT
, false);
4644 // Create a PLT entry for a global symbol.
4646 template<int size
, bool big_endian
>
4648 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
4652 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4653 && gsym
->can_use_relative_reloc(false))
4655 if (this->iplt_
== NULL
)
4656 this->make_iplt_section(symtab
, layout
);
4657 this->iplt_
->add_ifunc_entry(gsym
);
4661 if (this->plt_
== NULL
)
4662 this->make_plt_section(symtab
, layout
);
4663 this->plt_
->add_entry(gsym
);
4667 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
4669 template<int size
, bool big_endian
>
4671 Target_powerpc
<size
, big_endian
>::make_local_ifunc_plt_entry(
4672 Symbol_table
* symtab
,
4674 Sized_relobj_file
<size
, big_endian
>* relobj
,
4677 if (this->iplt_
== NULL
)
4678 this->make_iplt_section(symtab
, layout
);
4679 this->iplt_
->add_local_ifunc_entry(relobj
, r_sym
);
4682 // Return the number of entries in the PLT.
4684 template<int size
, bool big_endian
>
4686 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
4688 if (this->plt_
== NULL
)
4690 return this->plt_
->entry_count();
4693 // Return the offset of the first non-reserved PLT entry.
4695 template<int size
, bool big_endian
>
4697 Target_powerpc
<size
, big_endian
>::first_plt_entry_offset() const
4699 return this->plt_
->first_plt_entry_offset();
4702 // Return the size of each PLT entry.
4704 template<int size
, bool big_endian
>
4706 Target_powerpc
<size
, big_endian
>::plt_entry_size() const
4708 return Output_data_plt_powerpc
<size
, big_endian
>::get_plt_entry_size();
4711 // Create a GOT entry for local dynamic __tls_get_addr calls.
4713 template<int size
, bool big_endian
>
4715 Target_powerpc
<size
, big_endian
>::tlsld_got_offset(
4716 Symbol_table
* symtab
,
4718 Sized_relobj_file
<size
, big_endian
>* object
)
4720 if (this->tlsld_got_offset_
== -1U)
4722 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
4723 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
4724 Output_data_got_powerpc
<size
, big_endian
>* got
4725 = this->got_section(symtab
, layout
);
4726 unsigned int got_offset
= got
->add_constant_pair(0, 0);
4727 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
4729 this->tlsld_got_offset_
= got_offset
;
4731 return this->tlsld_got_offset_
;
4734 // Get the Reference_flags for a particular relocation.
4736 template<int size
, bool big_endian
>
4738 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(unsigned int r_type
)
4742 case elfcpp::R_POWERPC_NONE
:
4743 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
4744 case elfcpp::R_POWERPC_GNU_VTENTRY
:
4745 case elfcpp::R_PPC64_TOC
:
4746 // No symbol reference.
4749 case elfcpp::R_PPC64_ADDR64
:
4750 case elfcpp::R_PPC64_UADDR64
:
4751 case elfcpp::R_POWERPC_ADDR32
:
4752 case elfcpp::R_POWERPC_UADDR32
:
4753 case elfcpp::R_POWERPC_ADDR16
:
4754 case elfcpp::R_POWERPC_UADDR16
:
4755 case elfcpp::R_POWERPC_ADDR16_LO
:
4756 case elfcpp::R_POWERPC_ADDR16_HI
:
4757 case elfcpp::R_POWERPC_ADDR16_HA
:
4758 return Symbol::ABSOLUTE_REF
;
4760 case elfcpp::R_POWERPC_ADDR24
:
4761 case elfcpp::R_POWERPC_ADDR14
:
4762 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4763 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4764 return Symbol::FUNCTION_CALL
| Symbol::ABSOLUTE_REF
;
4766 case elfcpp::R_PPC64_REL64
:
4767 case elfcpp::R_POWERPC_REL32
:
4768 case elfcpp::R_PPC_LOCAL24PC
:
4769 case elfcpp::R_POWERPC_REL16
:
4770 case elfcpp::R_POWERPC_REL16_LO
:
4771 case elfcpp::R_POWERPC_REL16_HI
:
4772 case elfcpp::R_POWERPC_REL16_HA
:
4773 return Symbol::RELATIVE_REF
;
4775 case elfcpp::R_POWERPC_REL24
:
4776 case elfcpp::R_PPC_PLTREL24
:
4777 case elfcpp::R_POWERPC_REL14
:
4778 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4779 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4780 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
4782 case elfcpp::R_POWERPC_GOT16
:
4783 case elfcpp::R_POWERPC_GOT16_LO
:
4784 case elfcpp::R_POWERPC_GOT16_HI
:
4785 case elfcpp::R_POWERPC_GOT16_HA
:
4786 case elfcpp::R_PPC64_GOT16_DS
:
4787 case elfcpp::R_PPC64_GOT16_LO_DS
:
4788 case elfcpp::R_PPC64_TOC16
:
4789 case elfcpp::R_PPC64_TOC16_LO
:
4790 case elfcpp::R_PPC64_TOC16_HI
:
4791 case elfcpp::R_PPC64_TOC16_HA
:
4792 case elfcpp::R_PPC64_TOC16_DS
:
4793 case elfcpp::R_PPC64_TOC16_LO_DS
:
4795 return Symbol::ABSOLUTE_REF
;
4797 case elfcpp::R_POWERPC_GOT_TPREL16
:
4798 case elfcpp::R_POWERPC_TLS
:
4799 return Symbol::TLS_REF
;
4801 case elfcpp::R_POWERPC_COPY
:
4802 case elfcpp::R_POWERPC_GLOB_DAT
:
4803 case elfcpp::R_POWERPC_JMP_SLOT
:
4804 case elfcpp::R_POWERPC_RELATIVE
:
4805 case elfcpp::R_POWERPC_DTPMOD
:
4807 // Not expected. We will give an error later.
4812 // Report an unsupported relocation against a local symbol.
4814 template<int size
, bool big_endian
>
4816 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
4817 Sized_relobj_file
<size
, big_endian
>* object
,
4818 unsigned int r_type
)
4820 gold_error(_("%s: unsupported reloc %u against local symbol"),
4821 object
->name().c_str(), r_type
);
4824 // We are about to emit a dynamic relocation of type R_TYPE. If the
4825 // dynamic linker does not support it, issue an error.
4827 template<int size
, bool big_endian
>
4829 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
4830 unsigned int r_type
)
4832 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
4834 // These are the relocation types supported by glibc for both 32-bit
4835 // and 64-bit powerpc.
4838 case elfcpp::R_POWERPC_NONE
:
4839 case elfcpp::R_POWERPC_RELATIVE
:
4840 case elfcpp::R_POWERPC_GLOB_DAT
:
4841 case elfcpp::R_POWERPC_DTPMOD
:
4842 case elfcpp::R_POWERPC_DTPREL
:
4843 case elfcpp::R_POWERPC_TPREL
:
4844 case elfcpp::R_POWERPC_JMP_SLOT
:
4845 case elfcpp::R_POWERPC_COPY
:
4846 case elfcpp::R_POWERPC_IRELATIVE
:
4847 case elfcpp::R_POWERPC_ADDR32
:
4848 case elfcpp::R_POWERPC_UADDR32
:
4849 case elfcpp::R_POWERPC_ADDR24
:
4850 case elfcpp::R_POWERPC_ADDR16
:
4851 case elfcpp::R_POWERPC_UADDR16
:
4852 case elfcpp::R_POWERPC_ADDR16_LO
:
4853 case elfcpp::R_POWERPC_ADDR16_HI
:
4854 case elfcpp::R_POWERPC_ADDR16_HA
:
4855 case elfcpp::R_POWERPC_ADDR14
:
4856 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4857 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4858 case elfcpp::R_POWERPC_REL32
:
4859 case elfcpp::R_POWERPC_REL24
:
4860 case elfcpp::R_POWERPC_TPREL16
:
4861 case elfcpp::R_POWERPC_TPREL16_LO
:
4862 case elfcpp::R_POWERPC_TPREL16_HI
:
4863 case elfcpp::R_POWERPC_TPREL16_HA
:
4874 // These are the relocation types supported only on 64-bit.
4875 case elfcpp::R_PPC64_ADDR64
:
4876 case elfcpp::R_PPC64_UADDR64
:
4877 case elfcpp::R_PPC64_JMP_IREL
:
4878 case elfcpp::R_PPC64_ADDR16_DS
:
4879 case elfcpp::R_PPC64_ADDR16_LO_DS
:
4880 case elfcpp::R_PPC64_ADDR16_HIGH
:
4881 case elfcpp::R_PPC64_ADDR16_HIGHA
:
4882 case elfcpp::R_PPC64_ADDR16_HIGHER
:
4883 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
4884 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
4885 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
4886 case elfcpp::R_PPC64_REL64
:
4887 case elfcpp::R_POWERPC_ADDR30
:
4888 case elfcpp::R_PPC64_TPREL16_DS
:
4889 case elfcpp::R_PPC64_TPREL16_LO_DS
:
4890 case elfcpp::R_PPC64_TPREL16_HIGH
:
4891 case elfcpp::R_PPC64_TPREL16_HIGHA
:
4892 case elfcpp::R_PPC64_TPREL16_HIGHER
:
4893 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
4894 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
4895 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
4906 // These are the relocation types supported only on 32-bit.
4907 // ??? glibc ld.so doesn't need to support these.
4908 case elfcpp::R_POWERPC_DTPREL16
:
4909 case elfcpp::R_POWERPC_DTPREL16_LO
:
4910 case elfcpp::R_POWERPC_DTPREL16_HI
:
4911 case elfcpp::R_POWERPC_DTPREL16_HA
:
4919 // This prevents us from issuing more than one error per reloc
4920 // section. But we can still wind up issuing more than one
4921 // error per object file.
4922 if (this->issued_non_pic_error_
)
4924 gold_assert(parameters
->options().output_is_position_independent());
4925 object
->error(_("requires unsupported dynamic reloc; "
4926 "recompile with -fPIC"));
4927 this->issued_non_pic_error_
= true;
4931 // Return whether we need to make a PLT entry for a relocation of the
4932 // given type against a STT_GNU_IFUNC symbol.
4934 template<int size
, bool big_endian
>
4936 Target_powerpc
<size
, big_endian
>::Scan::reloc_needs_plt_for_ifunc(
4937 Sized_relobj_file
<size
, big_endian
>* object
,
4938 unsigned int r_type
,
4941 // In non-pic code any reference will resolve to the plt call stub
4942 // for the ifunc symbol.
4943 if (size
== 32 && !parameters
->options().output_is_position_independent())
4948 // Word size refs from data sections are OK, but don't need a PLT entry.
4949 case elfcpp::R_POWERPC_ADDR32
:
4950 case elfcpp::R_POWERPC_UADDR32
:
4955 case elfcpp::R_PPC64_ADDR64
:
4956 case elfcpp::R_PPC64_UADDR64
:
4961 // GOT refs are good, but also don't need a PLT entry.
4962 case elfcpp::R_POWERPC_GOT16
:
4963 case elfcpp::R_POWERPC_GOT16_LO
:
4964 case elfcpp::R_POWERPC_GOT16_HI
:
4965 case elfcpp::R_POWERPC_GOT16_HA
:
4966 case elfcpp::R_PPC64_GOT16_DS
:
4967 case elfcpp::R_PPC64_GOT16_LO_DS
:
4970 // Function calls are good, and these do need a PLT entry.
4971 case elfcpp::R_POWERPC_ADDR24
:
4972 case elfcpp::R_POWERPC_ADDR14
:
4973 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
4974 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
4975 case elfcpp::R_POWERPC_REL24
:
4976 case elfcpp::R_PPC_PLTREL24
:
4977 case elfcpp::R_POWERPC_REL14
:
4978 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
4979 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
4986 // Anything else is a problem.
4987 // If we are building a static executable, the libc startup function
4988 // responsible for applying indirect function relocations is going
4989 // to complain about the reloc type.
4990 // If we are building a dynamic executable, we will have a text
4991 // relocation. The dynamic loader will set the text segment
4992 // writable and non-executable to apply text relocations. So we'll
4993 // segfault when trying to run the indirection function to resolve
4996 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
4997 object
->name().c_str(), r_type
);
5001 // Scan a relocation for a local symbol.
5003 template<int size
, bool big_endian
>
5005 Target_powerpc
<size
, big_endian
>::Scan::local(
5006 Symbol_table
* symtab
,
5008 Target_powerpc
<size
, big_endian
>* target
,
5009 Sized_relobj_file
<size
, big_endian
>* object
,
5010 unsigned int data_shndx
,
5011 Output_section
* output_section
,
5012 const elfcpp::Rela
<size
, big_endian
>& reloc
,
5013 unsigned int r_type
,
5014 const elfcpp::Sym
<size
, big_endian
>& lsym
,
5017 this->maybe_skip_tls_get_addr_call(r_type
, NULL
);
5019 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
5020 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
5022 this->expect_tls_get_addr_call();
5023 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
5024 if (tls_type
!= tls::TLSOPT_NONE
)
5025 this->skip_next_tls_get_addr_call();
5027 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
5028 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
5030 this->expect_tls_get_addr_call();
5031 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5032 if (tls_type
!= tls::TLSOPT_NONE
)
5033 this->skip_next_tls_get_addr_call();
5036 Powerpc_relobj
<size
, big_endian
>* ppc_object
5037 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5042 && data_shndx
== ppc_object
->opd_shndx()
5043 && r_type
== elfcpp::R_PPC64_ADDR64
)
5044 ppc_object
->set_opd_discard(reloc
.get_r_offset());
5048 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5049 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
5050 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
, true))
5052 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5053 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5054 r_type
, r_sym
, reloc
.get_r_addend());
5055 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
5060 case elfcpp::R_POWERPC_NONE
:
5061 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5062 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5063 case elfcpp::R_PPC64_TOCSAVE
:
5064 case elfcpp::R_POWERPC_TLS
:
5067 case elfcpp::R_PPC64_TOC
:
5069 Output_data_got_powerpc
<size
, big_endian
>* got
5070 = target
->got_section(symtab
, layout
);
5071 if (parameters
->options().output_is_position_independent())
5073 Address off
= reloc
.get_r_offset();
5075 && data_shndx
== ppc_object
->opd_shndx()
5076 && ppc_object
->get_opd_discard(off
- 8))
5079 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5080 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
5081 rela_dyn
->add_output_section_relative(got
->output_section(),
5082 elfcpp::R_POWERPC_RELATIVE
,
5084 object
, data_shndx
, off
,
5085 symobj
->toc_base_offset());
5090 case elfcpp::R_PPC64_ADDR64
:
5091 case elfcpp::R_PPC64_UADDR64
:
5092 case elfcpp::R_POWERPC_ADDR32
:
5093 case elfcpp::R_POWERPC_UADDR32
:
5094 case elfcpp::R_POWERPC_ADDR24
:
5095 case elfcpp::R_POWERPC_ADDR16
:
5096 case elfcpp::R_POWERPC_ADDR16_LO
:
5097 case elfcpp::R_POWERPC_ADDR16_HI
:
5098 case elfcpp::R_POWERPC_ADDR16_HA
:
5099 case elfcpp::R_POWERPC_UADDR16
:
5100 case elfcpp::R_PPC64_ADDR16_HIGH
:
5101 case elfcpp::R_PPC64_ADDR16_HIGHA
:
5102 case elfcpp::R_PPC64_ADDR16_HIGHER
:
5103 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
5104 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
5105 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
5106 case elfcpp::R_PPC64_ADDR16_DS
:
5107 case elfcpp::R_PPC64_ADDR16_LO_DS
:
5108 case elfcpp::R_POWERPC_ADDR14
:
5109 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5110 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5111 // If building a shared library (or a position-independent
5112 // executable), we need to create a dynamic relocation for
5114 if (parameters
->options().output_is_position_independent()
5115 || (size
== 64 && is_ifunc
))
5117 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
5119 if ((size
== 32 && r_type
== elfcpp::R_POWERPC_ADDR32
)
5120 || (size
== 64 && r_type
== elfcpp::R_PPC64_ADDR64
))
5122 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5123 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
5124 : elfcpp::R_POWERPC_RELATIVE
);
5125 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
5126 output_section
, data_shndx
,
5127 reloc
.get_r_offset(),
5128 reloc
.get_r_addend(), false);
5132 check_non_pic(object
, r_type
);
5133 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5134 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
5135 data_shndx
, reloc
.get_r_offset(),
5136 reloc
.get_r_addend());
5141 case elfcpp::R_POWERPC_REL24
:
5142 case elfcpp::R_PPC_PLTREL24
:
5143 case elfcpp::R_PPC_LOCAL24PC
:
5144 case elfcpp::R_POWERPC_REL14
:
5145 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5146 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5148 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5149 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5150 reloc
.get_r_addend());
5153 case elfcpp::R_PPC64_REL64
:
5154 case elfcpp::R_POWERPC_REL32
:
5155 case elfcpp::R_POWERPC_REL16
:
5156 case elfcpp::R_POWERPC_REL16_LO
:
5157 case elfcpp::R_POWERPC_REL16_HI
:
5158 case elfcpp::R_POWERPC_REL16_HA
:
5159 case elfcpp::R_POWERPC_SECTOFF
:
5160 case elfcpp::R_POWERPC_SECTOFF_LO
:
5161 case elfcpp::R_POWERPC_SECTOFF_HI
:
5162 case elfcpp::R_POWERPC_SECTOFF_HA
:
5163 case elfcpp::R_PPC64_SECTOFF_DS
:
5164 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
5165 case elfcpp::R_POWERPC_TPREL16
:
5166 case elfcpp::R_POWERPC_TPREL16_LO
:
5167 case elfcpp::R_POWERPC_TPREL16_HI
:
5168 case elfcpp::R_POWERPC_TPREL16_HA
:
5169 case elfcpp::R_PPC64_TPREL16_DS
:
5170 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5171 case elfcpp::R_PPC64_TPREL16_HIGH
:
5172 case elfcpp::R_PPC64_TPREL16_HIGHA
:
5173 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5174 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5175 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5176 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5177 case elfcpp::R_POWERPC_DTPREL16
:
5178 case elfcpp::R_POWERPC_DTPREL16_LO
:
5179 case elfcpp::R_POWERPC_DTPREL16_HI
:
5180 case elfcpp::R_POWERPC_DTPREL16_HA
:
5181 case elfcpp::R_PPC64_DTPREL16_DS
:
5182 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
5183 case elfcpp::R_PPC64_DTPREL16_HIGH
:
5184 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
5185 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
5186 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
5187 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
5188 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
5189 case elfcpp::R_PPC64_TLSGD
:
5190 case elfcpp::R_PPC64_TLSLD
:
5193 case elfcpp::R_POWERPC_GOT16
:
5194 case elfcpp::R_POWERPC_GOT16_LO
:
5195 case elfcpp::R_POWERPC_GOT16_HI
:
5196 case elfcpp::R_POWERPC_GOT16_HA
:
5197 case elfcpp::R_PPC64_GOT16_DS
:
5198 case elfcpp::R_PPC64_GOT16_LO_DS
:
5200 // The symbol requires a GOT entry.
5201 Output_data_got_powerpc
<size
, big_endian
>* got
5202 = target
->got_section(symtab
, layout
);
5203 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5205 if (!parameters
->options().output_is_position_independent())
5207 if (size
== 32 && is_ifunc
)
5208 got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
5210 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
5212 else if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
5214 // If we are generating a shared object or a pie, this
5215 // symbol's GOT entry will be set by a dynamic relocation.
5217 off
= got
->add_constant(0);
5218 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
5220 Reloc_section
* rela_dyn
= target
->rela_dyn_section(symtab
, layout
,
5222 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
5223 : elfcpp::R_POWERPC_RELATIVE
);
5224 rela_dyn
->add_local_relative(object
, r_sym
, dynrel
,
5225 got
, off
, 0, false);
5230 case elfcpp::R_PPC64_TOC16
:
5231 case elfcpp::R_PPC64_TOC16_LO
:
5232 case elfcpp::R_PPC64_TOC16_HI
:
5233 case elfcpp::R_PPC64_TOC16_HA
:
5234 case elfcpp::R_PPC64_TOC16_DS
:
5235 case elfcpp::R_PPC64_TOC16_LO_DS
:
5236 // We need a GOT section.
5237 target
->got_section(symtab
, layout
);
5240 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5241 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
5242 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
5243 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
5245 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(true);
5246 if (tls_type
== tls::TLSOPT_NONE
)
5248 Output_data_got_powerpc
<size
, big_endian
>* got
5249 = target
->got_section(symtab
, layout
);
5250 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5251 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5252 got
->add_local_tls_pair(object
, r_sym
, GOT_TYPE_TLSGD
,
5253 rela_dyn
, elfcpp::R_POWERPC_DTPMOD
);
5255 else if (tls_type
== tls::TLSOPT_TO_LE
)
5257 // no GOT relocs needed for Local Exec.
5264 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5265 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
5266 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
5267 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
5269 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5270 if (tls_type
== tls::TLSOPT_NONE
)
5271 target
->tlsld_got_offset(symtab
, layout
, object
);
5272 else if (tls_type
== tls::TLSOPT_TO_LE
)
5274 // no GOT relocs needed for Local Exec.
5275 if (parameters
->options().emit_relocs())
5277 Output_section
* os
= layout
->tls_segment()->first_section();
5278 gold_assert(os
!= NULL
);
5279 os
->set_needs_symtab_index();
5287 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5288 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
5289 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
5290 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
5292 Output_data_got_powerpc
<size
, big_endian
>* got
5293 = target
->got_section(symtab
, layout
);
5294 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5295 got
->add_local_tls(object
, r_sym
, GOT_TYPE_DTPREL
);
5299 case elfcpp::R_POWERPC_GOT_TPREL16
:
5300 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
5301 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
5302 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
5304 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(true);
5305 if (tls_type
== tls::TLSOPT_NONE
)
5307 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
5308 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
))
5310 Output_data_got_powerpc
<size
, big_endian
>* got
5311 = target
->got_section(symtab
, layout
);
5312 unsigned int off
= got
->add_constant(0);
5313 object
->set_local_got_offset(r_sym
, GOT_TYPE_TPREL
, off
);
5315 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5316 rela_dyn
->add_symbolless_local_addend(object
, r_sym
,
5317 elfcpp::R_POWERPC_TPREL
,
5321 else if (tls_type
== tls::TLSOPT_TO_LE
)
5323 // no GOT relocs needed for Local Exec.
5331 unsupported_reloc_local(object
, r_type
);
5337 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5338 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5339 case elfcpp::R_POWERPC_GOT_TPREL16
:
5340 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5341 case elfcpp::R_POWERPC_GOT16
:
5342 case elfcpp::R_PPC64_GOT16_DS
:
5343 case elfcpp::R_PPC64_TOC16
:
5344 case elfcpp::R_PPC64_TOC16_DS
:
5345 ppc_object
->set_has_small_toc_reloc();
5351 // Report an unsupported relocation against a global symbol.
5353 template<int size
, bool big_endian
>
5355 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
5356 Sized_relobj_file
<size
, big_endian
>* object
,
5357 unsigned int r_type
,
5360 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5361 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
5364 // Scan a relocation for a global symbol.
5366 template<int size
, bool big_endian
>
5368 Target_powerpc
<size
, big_endian
>::Scan::global(
5369 Symbol_table
* symtab
,
5371 Target_powerpc
<size
, big_endian
>* target
,
5372 Sized_relobj_file
<size
, big_endian
>* object
,
5373 unsigned int data_shndx
,
5374 Output_section
* output_section
,
5375 const elfcpp::Rela
<size
, big_endian
>& reloc
,
5376 unsigned int r_type
,
5379 if (this->maybe_skip_tls_get_addr_call(r_type
, gsym
) == Track_tls::SKIP
)
5382 if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
5383 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
5385 this->expect_tls_get_addr_call();
5386 const bool final
= gsym
->final_value_is_known();
5387 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
5388 if (tls_type
!= tls::TLSOPT_NONE
)
5389 this->skip_next_tls_get_addr_call();
5391 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
5392 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
5394 this->expect_tls_get_addr_call();
5395 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5396 if (tls_type
!= tls::TLSOPT_NONE
)
5397 this->skip_next_tls_get_addr_call();
5400 Powerpc_relobj
<size
, big_endian
>* ppc_object
5401 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5403 // A STT_GNU_IFUNC symbol may require a PLT entry.
5404 bool is_ifunc
= gsym
->type() == elfcpp::STT_GNU_IFUNC
;
5405 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
, true))
5407 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5408 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5409 reloc
.get_r_addend());
5410 target
->make_plt_entry(symtab
, layout
, gsym
);
5415 case elfcpp::R_POWERPC_NONE
:
5416 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
5417 case elfcpp::R_POWERPC_GNU_VTENTRY
:
5418 case elfcpp::R_PPC_LOCAL24PC
:
5419 case elfcpp::R_POWERPC_TLS
:
5422 case elfcpp::R_PPC64_TOC
:
5424 Output_data_got_powerpc
<size
, big_endian
>* got
5425 = target
->got_section(symtab
, layout
);
5426 if (parameters
->options().output_is_position_independent())
5428 Address off
= reloc
.get_r_offset();
5430 && data_shndx
== ppc_object
->opd_shndx()
5431 && ppc_object
->get_opd_discard(off
- 8))
5434 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5435 Powerpc_relobj
<size
, big_endian
>* symobj
= ppc_object
;
5436 if (data_shndx
!= ppc_object
->opd_shndx())
5437 symobj
= static_cast
5438 <Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
5439 rela_dyn
->add_output_section_relative(got
->output_section(),
5440 elfcpp::R_POWERPC_RELATIVE
,
5442 object
, data_shndx
, off
,
5443 symobj
->toc_base_offset());
5448 case elfcpp::R_PPC64_ADDR64
:
5450 && data_shndx
== ppc_object
->opd_shndx()
5451 && (gsym
->is_defined_in_discarded_section()
5452 || gsym
->object() != object
))
5454 ppc_object
->set_opd_discard(reloc
.get_r_offset());
5458 case elfcpp::R_PPC64_UADDR64
:
5459 case elfcpp::R_POWERPC_ADDR32
:
5460 case elfcpp::R_POWERPC_UADDR32
:
5461 case elfcpp::R_POWERPC_ADDR24
:
5462 case elfcpp::R_POWERPC_ADDR16
:
5463 case elfcpp::R_POWERPC_ADDR16_LO
:
5464 case elfcpp::R_POWERPC_ADDR16_HI
:
5465 case elfcpp::R_POWERPC_ADDR16_HA
:
5466 case elfcpp::R_POWERPC_UADDR16
:
5467 case elfcpp::R_PPC64_ADDR16_HIGH
:
5468 case elfcpp::R_PPC64_ADDR16_HIGHA
:
5469 case elfcpp::R_PPC64_ADDR16_HIGHER
:
5470 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
5471 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
5472 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
5473 case elfcpp::R_PPC64_ADDR16_DS
:
5474 case elfcpp::R_PPC64_ADDR16_LO_DS
:
5475 case elfcpp::R_POWERPC_ADDR14
:
5476 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
5477 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
5479 // Make a PLT entry if necessary.
5480 if (gsym
->needs_plt_entry())
5484 target
->push_branch(ppc_object
, data_shndx
,
5485 reloc
.get_r_offset(), r_type
,
5486 elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5487 reloc
.get_r_addend());
5488 target
->make_plt_entry(symtab
, layout
, gsym
);
5490 // Since this is not a PC-relative relocation, we may be
5491 // taking the address of a function. In that case we need to
5492 // set the entry in the dynamic symbol table to the address of
5493 // the PLT call stub.
5495 && gsym
->is_from_dynobj()
5496 && !parameters
->options().output_is_position_independent())
5497 gsym
->set_needs_dynsym_value();
5499 // Make a dynamic relocation if necessary.
5500 if (needs_dynamic_reloc
<size
>(gsym
, Scan::get_reference_flags(r_type
))
5501 || (size
== 64 && is_ifunc
))
5503 if (gsym
->may_need_copy_reloc())
5505 target
->copy_reloc(symtab
, layout
, object
,
5506 data_shndx
, output_section
, gsym
, reloc
);
5508 else if ((size
== 32
5509 && r_type
== elfcpp::R_POWERPC_ADDR32
5510 && gsym
->can_use_relative_reloc(false)
5511 && !(gsym
->visibility() == elfcpp::STV_PROTECTED
5512 && parameters
->options().shared()))
5514 && r_type
== elfcpp::R_PPC64_ADDR64
5515 && (gsym
->can_use_relative_reloc(false)
5516 || data_shndx
== ppc_object
->opd_shndx())))
5518 Reloc_section
* rela_dyn
5519 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
5520 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
5521 : elfcpp::R_POWERPC_RELATIVE
);
5522 rela_dyn
->add_symbolless_global_addend(
5523 gsym
, dynrel
, output_section
, object
, data_shndx
,
5524 reloc
.get_r_offset(), reloc
.get_r_addend());
5528 Reloc_section
* rela_dyn
5529 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
5530 check_non_pic(object
, r_type
);
5531 rela_dyn
->add_global(gsym
, r_type
, output_section
,
5533 reloc
.get_r_offset(),
5534 reloc
.get_r_addend());
5540 case elfcpp::R_PPC_PLTREL24
:
5541 case elfcpp::R_POWERPC_REL24
:
5544 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5546 elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5547 reloc
.get_r_addend());
5548 if (gsym
->needs_plt_entry()
5549 || (!gsym
->final_value_is_known()
5550 && (gsym
->is_undefined()
5551 || gsym
->is_from_dynobj()
5552 || gsym
->is_preemptible())))
5553 target
->make_plt_entry(symtab
, layout
, gsym
);
5557 case elfcpp::R_PPC64_REL64
:
5558 case elfcpp::R_POWERPC_REL32
:
5559 // Make a dynamic relocation if necessary.
5560 if (needs_dynamic_reloc
<size
>(gsym
, Scan::get_reference_flags(r_type
)))
5562 if (gsym
->may_need_copy_reloc())
5564 target
->copy_reloc(symtab
, layout
, object
,
5565 data_shndx
, output_section
, gsym
,
5570 Reloc_section
* rela_dyn
5571 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
5572 check_non_pic(object
, r_type
);
5573 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
5574 data_shndx
, reloc
.get_r_offset(),
5575 reloc
.get_r_addend());
5580 case elfcpp::R_POWERPC_REL14
:
5581 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
5582 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
5584 target
->push_branch(ppc_object
, data_shndx
, reloc
.get_r_offset(),
5585 r_type
, elfcpp::elf_r_sym
<size
>(reloc
.get_r_info()),
5586 reloc
.get_r_addend());
5589 case elfcpp::R_POWERPC_REL16
:
5590 case elfcpp::R_POWERPC_REL16_LO
:
5591 case elfcpp::R_POWERPC_REL16_HI
:
5592 case elfcpp::R_POWERPC_REL16_HA
:
5593 case elfcpp::R_POWERPC_SECTOFF
:
5594 case elfcpp::R_POWERPC_SECTOFF_LO
:
5595 case elfcpp::R_POWERPC_SECTOFF_HI
:
5596 case elfcpp::R_POWERPC_SECTOFF_HA
:
5597 case elfcpp::R_PPC64_SECTOFF_DS
:
5598 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
5599 case elfcpp::R_POWERPC_TPREL16
:
5600 case elfcpp::R_POWERPC_TPREL16_LO
:
5601 case elfcpp::R_POWERPC_TPREL16_HI
:
5602 case elfcpp::R_POWERPC_TPREL16_HA
:
5603 case elfcpp::R_PPC64_TPREL16_DS
:
5604 case elfcpp::R_PPC64_TPREL16_LO_DS
:
5605 case elfcpp::R_PPC64_TPREL16_HIGH
:
5606 case elfcpp::R_PPC64_TPREL16_HIGHA
:
5607 case elfcpp::R_PPC64_TPREL16_HIGHER
:
5608 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
5609 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
5610 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
5611 case elfcpp::R_POWERPC_DTPREL16
:
5612 case elfcpp::R_POWERPC_DTPREL16_LO
:
5613 case elfcpp::R_POWERPC_DTPREL16_HI
:
5614 case elfcpp::R_POWERPC_DTPREL16_HA
:
5615 case elfcpp::R_PPC64_DTPREL16_DS
:
5616 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
5617 case elfcpp::R_PPC64_DTPREL16_HIGH
:
5618 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
5619 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
5620 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
5621 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
5622 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
5623 case elfcpp::R_PPC64_TLSGD
:
5624 case elfcpp::R_PPC64_TLSLD
:
5627 case elfcpp::R_POWERPC_GOT16
:
5628 case elfcpp::R_POWERPC_GOT16_LO
:
5629 case elfcpp::R_POWERPC_GOT16_HI
:
5630 case elfcpp::R_POWERPC_GOT16_HA
:
5631 case elfcpp::R_PPC64_GOT16_DS
:
5632 case elfcpp::R_PPC64_GOT16_LO_DS
:
5634 // The symbol requires a GOT entry.
5635 Output_data_got_powerpc
<size
, big_endian
>* got
;
5637 got
= target
->got_section(symtab
, layout
);
5638 if (gsym
->final_value_is_known())
5640 if (size
== 32 && is_ifunc
)
5641 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
5643 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
5645 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
5647 // If we are generating a shared object or a pie, this
5648 // symbol's GOT entry will be set by a dynamic relocation.
5649 unsigned int off
= got
->add_constant(0);
5650 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
5652 Reloc_section
* rela_dyn
5653 = target
->rela_dyn_section(symtab
, layout
, is_ifunc
);
5655 if (gsym
->can_use_relative_reloc(false)
5657 && gsym
->visibility() == elfcpp::STV_PROTECTED
5658 && parameters
->options().shared()))
5660 unsigned int dynrel
= (is_ifunc
? elfcpp::R_POWERPC_IRELATIVE
5661 : elfcpp::R_POWERPC_RELATIVE
);
5662 rela_dyn
->add_global_relative(gsym
, dynrel
, got
, off
, 0, false);
5666 unsigned int dynrel
= elfcpp::R_POWERPC_GLOB_DAT
;
5667 rela_dyn
->add_global(gsym
, dynrel
, got
, off
, 0);
5673 case elfcpp::R_PPC64_TOC16
:
5674 case elfcpp::R_PPC64_TOC16_LO
:
5675 case elfcpp::R_PPC64_TOC16_HI
:
5676 case elfcpp::R_PPC64_TOC16_HA
:
5677 case elfcpp::R_PPC64_TOC16_DS
:
5678 case elfcpp::R_PPC64_TOC16_LO_DS
:
5679 // We need a GOT section.
5680 target
->got_section(symtab
, layout
);
5683 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5684 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
5685 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
5686 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
5688 const bool final
= gsym
->final_value_is_known();
5689 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
5690 if (tls_type
== tls::TLSOPT_NONE
)
5692 Output_data_got_powerpc
<size
, big_endian
>* got
5693 = target
->got_section(symtab
, layout
);
5694 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5695 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLSGD
, rela_dyn
,
5696 elfcpp::R_POWERPC_DTPMOD
,
5697 elfcpp::R_POWERPC_DTPREL
);
5699 else if (tls_type
== tls::TLSOPT_TO_IE
)
5701 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
5703 Output_data_got_powerpc
<size
, big_endian
>* got
5704 = target
->got_section(symtab
, layout
);
5705 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5706 if (gsym
->is_undefined()
5707 || gsym
->is_from_dynobj())
5709 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
5710 elfcpp::R_POWERPC_TPREL
);
5714 unsigned int off
= got
->add_constant(0);
5715 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
5716 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
5717 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
5722 else if (tls_type
== tls::TLSOPT_TO_LE
)
5724 // no GOT relocs needed for Local Exec.
5731 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5732 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
5733 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
5734 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
5736 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
5737 if (tls_type
== tls::TLSOPT_NONE
)
5738 target
->tlsld_got_offset(symtab
, layout
, object
);
5739 else if (tls_type
== tls::TLSOPT_TO_LE
)
5741 // no GOT relocs needed for Local Exec.
5742 if (parameters
->options().emit_relocs())
5744 Output_section
* os
= layout
->tls_segment()->first_section();
5745 gold_assert(os
!= NULL
);
5746 os
->set_needs_symtab_index();
5754 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5755 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
5756 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
5757 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
5759 Output_data_got_powerpc
<size
, big_endian
>* got
5760 = target
->got_section(symtab
, layout
);
5761 if (!gsym
->final_value_is_known()
5762 && (gsym
->is_from_dynobj()
5763 || gsym
->is_undefined()
5764 || gsym
->is_preemptible()))
5765 got
->add_global_with_rel(gsym
, GOT_TYPE_DTPREL
,
5766 target
->rela_dyn_section(layout
),
5767 elfcpp::R_POWERPC_DTPREL
);
5769 got
->add_global_tls(gsym
, GOT_TYPE_DTPREL
);
5773 case elfcpp::R_POWERPC_GOT_TPREL16
:
5774 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
5775 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
5776 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
5778 const bool final
= gsym
->final_value_is_known();
5779 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
5780 if (tls_type
== tls::TLSOPT_NONE
)
5782 if (!gsym
->has_got_offset(GOT_TYPE_TPREL
))
5784 Output_data_got_powerpc
<size
, big_endian
>* got
5785 = target
->got_section(symtab
, layout
);
5786 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
5787 if (gsym
->is_undefined()
5788 || gsym
->is_from_dynobj())
5790 got
->add_global_with_rel(gsym
, GOT_TYPE_TPREL
, rela_dyn
,
5791 elfcpp::R_POWERPC_TPREL
);
5795 unsigned int off
= got
->add_constant(0);
5796 gsym
->set_got_offset(GOT_TYPE_TPREL
, off
);
5797 unsigned int dynrel
= elfcpp::R_POWERPC_TPREL
;
5798 rela_dyn
->add_symbolless_global_addend(gsym
, dynrel
,
5803 else if (tls_type
== tls::TLSOPT_TO_LE
)
5805 // no GOT relocs needed for Local Exec.
5813 unsupported_reloc_global(object
, r_type
, gsym
);
5819 case elfcpp::R_POWERPC_GOT_TLSLD16
:
5820 case elfcpp::R_POWERPC_GOT_TLSGD16
:
5821 case elfcpp::R_POWERPC_GOT_TPREL16
:
5822 case elfcpp::R_POWERPC_GOT_DTPREL16
:
5823 case elfcpp::R_POWERPC_GOT16
:
5824 case elfcpp::R_PPC64_GOT16_DS
:
5825 case elfcpp::R_PPC64_TOC16
:
5826 case elfcpp::R_PPC64_TOC16_DS
:
5827 ppc_object
->set_has_small_toc_reloc();
5833 // Process relocations for gc.
5835 template<int size
, bool big_endian
>
5837 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
5838 Symbol_table
* symtab
,
5840 Sized_relobj_file
<size
, big_endian
>* object
,
5841 unsigned int data_shndx
,
5843 const unsigned char* prelocs
,
5845 Output_section
* output_section
,
5846 bool needs_special_offset_handling
,
5847 size_t local_symbol_count
,
5848 const unsigned char* plocal_symbols
)
5850 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
5851 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
5852 Powerpc_relobj
<size
, big_endian
>* ppc_object
5853 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(object
);
5855 ppc_object
->set_opd_valid();
5856 if (size
== 64 && data_shndx
== ppc_object
->opd_shndx())
5858 typename Powerpc_relobj
<size
, big_endian
>::Access_from::iterator p
;
5859 for (p
= ppc_object
->access_from_map()->begin();
5860 p
!= ppc_object
->access_from_map()->end();
5863 Address dst_off
= p
->first
;
5864 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
5865 typename Powerpc_relobj
<size
, big_endian
>::Section_refs::iterator s
;
5866 for (s
= p
->second
.begin(); s
!= p
->second
.end(); ++s
)
5868 Object
* src_obj
= s
->first
;
5869 unsigned int src_indx
= s
->second
;
5870 symtab
->gc()->add_reference(src_obj
, src_indx
,
5871 ppc_object
, dst_indx
);
5875 ppc_object
->access_from_map()->clear();
5876 ppc_object
->process_gc_mark(symtab
);
5877 // Don't look at .opd relocs as .opd will reference everything.
5881 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
5882 typename
Target_powerpc::Relocatable_size_for_reloc
>(
5891 needs_special_offset_handling
,
5896 // Handle target specific gc actions when adding a gc reference from
5897 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
5898 // and DST_OFF. For powerpc64, this adds a referenc to the code
5899 // section of a function descriptor.
5901 template<int size
, bool big_endian
>
5903 Target_powerpc
<size
, big_endian
>::do_gc_add_reference(
5904 Symbol_table
* symtab
,
5906 unsigned int src_shndx
,
5908 unsigned int dst_shndx
,
5909 Address dst_off
) const
5911 if (size
!= 64 || dst_obj
->is_dynamic())
5914 Powerpc_relobj
<size
, big_endian
>* ppc_object
5915 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(dst_obj
);
5916 if (dst_shndx
!= 0 && dst_shndx
== ppc_object
->opd_shndx())
5918 if (ppc_object
->opd_valid())
5920 dst_shndx
= ppc_object
->get_opd_ent(dst_off
);
5921 symtab
->gc()->add_reference(src_obj
, src_shndx
, dst_obj
, dst_shndx
);
5925 // If we haven't run scan_opd_relocs, we must delay
5926 // processing this function descriptor reference.
5927 ppc_object
->add_reference(src_obj
, src_shndx
, dst_off
);
5932 // Add any special sections for this symbol to the gc work list.
5933 // For powerpc64, this adds the code section of a function
5936 template<int size
, bool big_endian
>
5938 Target_powerpc
<size
, big_endian
>::do_gc_mark_symbol(
5939 Symbol_table
* symtab
,
5944 Powerpc_relobj
<size
, big_endian
>* ppc_object
5945 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(sym
->object());
5947 unsigned int shndx
= sym
->shndx(&is_ordinary
);
5948 if (is_ordinary
&& shndx
!= 0 && shndx
== ppc_object
->opd_shndx())
5950 Sized_symbol
<size
>* gsym
= symtab
->get_sized_symbol
<size
>(sym
);
5951 Address dst_off
= gsym
->value();
5952 if (ppc_object
->opd_valid())
5954 unsigned int dst_indx
= ppc_object
->get_opd_ent(dst_off
);
5955 symtab
->gc()->worklist().push(Section_id(ppc_object
, dst_indx
));
5958 ppc_object
->add_gc_mark(dst_off
);
5963 // For a symbol location in .opd, set LOC to the location of the
5966 template<int size
, bool big_endian
>
5968 Target_powerpc
<size
, big_endian
>::do_function_location(
5969 Symbol_location
* loc
) const
5971 if (size
== 64 && loc
->shndx
!= 0)
5973 if (loc
->object
->is_dynamic())
5975 Powerpc_dynobj
<size
, big_endian
>* ppc_object
5976 = static_cast<Powerpc_dynobj
<size
, big_endian
>*>(loc
->object
);
5977 if (loc
->shndx
== ppc_object
->opd_shndx())
5980 Address off
= loc
->offset
- ppc_object
->opd_address();
5981 loc
->shndx
= ppc_object
->get_opd_ent(off
, &dest_off
);
5982 loc
->offset
= dest_off
;
5987 const Powerpc_relobj
<size
, big_endian
>* ppc_object
5988 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(loc
->object
);
5989 if (loc
->shndx
== ppc_object
->opd_shndx())
5992 loc
->shndx
= ppc_object
->get_opd_ent(loc
->offset
, &dest_off
);
5993 loc
->offset
= dest_off
;
5999 // Scan relocations for a section.
6001 template<int size
, bool big_endian
>
6003 Target_powerpc
<size
, big_endian
>::scan_relocs(
6004 Symbol_table
* symtab
,
6006 Sized_relobj_file
<size
, big_endian
>* object
,
6007 unsigned int data_shndx
,
6008 unsigned int sh_type
,
6009 const unsigned char* prelocs
,
6011 Output_section
* output_section
,
6012 bool needs_special_offset_handling
,
6013 size_t local_symbol_count
,
6014 const unsigned char* plocal_symbols
)
6016 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
6017 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
6019 if (sh_type
== elfcpp::SHT_REL
)
6021 gold_error(_("%s: unsupported REL reloc section"),
6022 object
->name().c_str());
6026 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
6035 needs_special_offset_handling
,
6040 // Functor class for processing the global symbol table.
6041 // Removes symbols defined on discarded opd entries.
6043 template<bool big_endian
>
6044 class Global_symbol_visitor_opd
6047 Global_symbol_visitor_opd()
6051 operator()(Sized_symbol
<64>* sym
)
6053 if (sym
->has_symtab_index()
6054 || sym
->source() != Symbol::FROM_OBJECT
6055 || !sym
->in_real_elf())
6058 if (sym
->object()->is_dynamic())
6061 Powerpc_relobj
<64, big_endian
>* symobj
6062 = static_cast<Powerpc_relobj
<64, big_endian
>*>(sym
->object());
6063 if (symobj
->opd_shndx() == 0)
6067 unsigned int shndx
= sym
->shndx(&is_ordinary
);
6068 if (shndx
== symobj
->opd_shndx()
6069 && symobj
->get_opd_discard(sym
->value()))
6070 sym
->set_symtab_index(-1U);
6074 template<int size
, bool big_endian
>
6076 Target_powerpc
<size
, big_endian
>::define_save_restore_funcs(
6078 Symbol_table
* symtab
)
6082 Output_data_save_res
<64, big_endian
>* savres
6083 = new Output_data_save_res
<64, big_endian
>(symtab
);
6084 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
6085 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
6086 savres
, ORDER_TEXT
, false);
6090 // Sort linker created .got section first (for the header), then input
6091 // sections belonging to files using small model code.
6093 template<bool big_endian
>
6094 class Sort_toc_sections
6098 operator()(const Output_section::Input_section
& is1
,
6099 const Output_section::Input_section
& is2
) const
6101 if (!is1
.is_input_section() && is2
.is_input_section())
6104 = (is1
.is_input_section()
6105 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is1
.relobj())
6106 ->has_small_toc_reloc()));
6108 = (is2
.is_input_section()
6109 && (static_cast<const Powerpc_relobj
<64, big_endian
>*>(is2
.relobj())
6110 ->has_small_toc_reloc()));
6111 return small1
&& !small2
;
6115 // Finalize the sections.
6117 template<int size
, bool big_endian
>
6119 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
6121 const Input_objects
*,
6122 Symbol_table
* symtab
)
6124 if (parameters
->doing_static_link())
6126 // At least some versions of glibc elf-init.o have a strong
6127 // reference to __rela_iplt marker syms. A weak ref would be
6129 if (this->iplt_
!= NULL
)
6131 Reloc_section
* rel
= this->iplt_
->rel_plt();
6132 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
6133 Symbol_table::PREDEFINED
, rel
, 0, 0,
6134 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6135 elfcpp::STV_HIDDEN
, 0, false, true);
6136 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
6137 Symbol_table::PREDEFINED
, rel
, 0, 0,
6138 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6139 elfcpp::STV_HIDDEN
, 0, true, true);
6143 symtab
->define_as_constant("__rela_iplt_start", NULL
,
6144 Symbol_table::PREDEFINED
, 0, 0,
6145 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6146 elfcpp::STV_HIDDEN
, 0, true, false);
6147 symtab
->define_as_constant("__rela_iplt_end", NULL
,
6148 Symbol_table::PREDEFINED
, 0, 0,
6149 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
6150 elfcpp::STV_HIDDEN
, 0, true, false);
6156 typedef Global_symbol_visitor_opd
<big_endian
> Symbol_visitor
;
6157 symtab
->for_all_symbols
<64, Symbol_visitor
>(Symbol_visitor());
6159 if (!parameters
->options().relocatable())
6161 this->define_save_restore_funcs(layout
, symtab
);
6163 // Annoyingly, we need to make these sections now whether or
6164 // not we need them. If we delay until do_relax then we
6165 // need to mess with the relaxation machinery checkpointing.
6166 this->got_section(symtab
, layout
);
6167 this->make_brlt_section(layout
);
6169 if (parameters
->options().toc_sort())
6171 Output_section
* os
= this->got_
->output_section();
6172 if (os
!= NULL
&& os
->input_sections().size() > 1)
6173 std::stable_sort(os
->input_sections().begin(),
6174 os
->input_sections().end(),
6175 Sort_toc_sections
<big_endian
>());
6180 // Fill in some more dynamic tags.
6181 Output_data_dynamic
* odyn
= layout
->dynamic_data();
6184 const Reloc_section
* rel_plt
= (this->plt_
== NULL
6186 : this->plt_
->rel_plt());
6187 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
6188 this->rela_dyn_
, true, size
== 32);
6192 if (this->got_
!= NULL
)
6194 this->got_
->finalize_data_size();
6195 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
6196 this->got_
, this->got_
->g_o_t());
6201 if (this->glink_
!= NULL
)
6203 this->glink_
->finalize_data_size();
6204 odyn
->add_section_plus_offset(elfcpp::DT_PPC64_GLINK
,
6206 (this->glink_
->pltresolve_size
6212 // Emit any relocs we saved in an attempt to avoid generating COPY
6214 if (this->copy_relocs_
.any_saved_relocs())
6215 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
6218 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6222 ok_lo_toc_insn(uint32_t insn
)
6224 return ((insn
& (0x3f << 26)) == 14u << 26 /* addi */
6225 || (insn
& (0x3f << 26)) == 32u << 26 /* lwz */
6226 || (insn
& (0x3f << 26)) == 34u << 26 /* lbz */
6227 || (insn
& (0x3f << 26)) == 36u << 26 /* stw */
6228 || (insn
& (0x3f << 26)) == 38u << 26 /* stb */
6229 || (insn
& (0x3f << 26)) == 40u << 26 /* lhz */
6230 || (insn
& (0x3f << 26)) == 42u << 26 /* lha */
6231 || (insn
& (0x3f << 26)) == 44u << 26 /* sth */
6232 || (insn
& (0x3f << 26)) == 46u << 26 /* lmw */
6233 || (insn
& (0x3f << 26)) == 47u << 26 /* stmw */
6234 || (insn
& (0x3f << 26)) == 48u << 26 /* lfs */
6235 || (insn
& (0x3f << 26)) == 50u << 26 /* lfd */
6236 || (insn
& (0x3f << 26)) == 52u << 26 /* stfs */
6237 || (insn
& (0x3f << 26)) == 54u << 26 /* stfd */
6238 || ((insn
& (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6240 || ((insn
& (0x3f << 26)) == 62u << 26 /* std, stmd */
6241 && ((insn
& 3) == 0 || (insn
& 3) == 3))
6242 || (insn
& (0x3f << 26)) == 12u << 26 /* addic */);
6245 // Return the value to use for a branch relocation.
6247 template<int size
, bool big_endian
>
6248 typename Target_powerpc
<size
, big_endian
>::Address
6249 Target_powerpc
<size
, big_endian
>::symval_for_branch(
6250 const Symbol_table
* symtab
,
6252 const Sized_symbol
<size
>* gsym
,
6253 Powerpc_relobj
<size
, big_endian
>* object
,
6254 unsigned int *dest_shndx
)
6260 // If the symbol is defined in an opd section, ie. is a function
6261 // descriptor, use the function descriptor code entry address
6262 Powerpc_relobj
<size
, big_endian
>* symobj
= object
;
6264 && gsym
->source() != Symbol::FROM_OBJECT
)
6267 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(gsym
->object());
6268 unsigned int shndx
= symobj
->opd_shndx();
6271 Address opd_addr
= symobj
->get_output_section_offset(shndx
);
6272 if (opd_addr
== invalid_address
)
6274 opd_addr
+= symobj
->output_section_address(shndx
);
6275 if (value
>= opd_addr
&& value
< opd_addr
+ symobj
->section_size(shndx
))
6278 *dest_shndx
= symobj
->get_opd_ent(value
- opd_addr
, &sec_off
);
6279 if (symtab
->is_section_folded(symobj
, *dest_shndx
))
6282 = symtab
->icf()->get_folded_section(symobj
, *dest_shndx
);
6283 symobj
= static_cast<Powerpc_relobj
<size
, big_endian
>*>(folded
.first
);
6284 *dest_shndx
= folded
.second
;
6286 Address sec_addr
= symobj
->get_output_section_offset(*dest_shndx
);
6287 gold_assert(sec_addr
!= invalid_address
);
6288 sec_addr
+= symobj
->output_section(*dest_shndx
)->address();
6289 value
= sec_addr
+ sec_off
;
6294 // Perform a relocation.
6296 template<int size
, bool big_endian
>
6298 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
6299 const Relocate_info
<size
, big_endian
>* relinfo
,
6300 Target_powerpc
* target
,
6303 const elfcpp::Rela
<size
, big_endian
>& rela
,
6304 unsigned int r_type
,
6305 const Sized_symbol
<size
>* gsym
,
6306 const Symbol_value
<size
>* psymval
,
6307 unsigned char* view
,
6309 section_size_type view_size
)
6314 switch (this->maybe_skip_tls_get_addr_call(r_type
, gsym
))
6316 case Track_tls::NOT_EXPECTED
:
6317 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6318 _("__tls_get_addr call lacks marker reloc"));
6320 case Track_tls::EXPECTED
:
6321 // We have already complained.
6323 case Track_tls::SKIP
:
6325 case Track_tls::NORMAL
:
6329 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
6330 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Insn
;
6331 Powerpc_relobj
<size
, big_endian
>* const object
6332 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
6334 bool has_plt_value
= false;
6335 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6337 ? use_plt_offset
<size
>(gsym
, Scan::get_reference_flags(r_type
))
6338 : object
->local_has_plt_offset(r_sym
))
6339 && (!psymval
->is_ifunc_symbol()
6340 || Scan::reloc_needs_plt_for_ifunc(object
, r_type
, false)))
6342 Stub_table
<size
, big_endian
>* stub_table
6343 = object
->stub_table(relinfo
->data_shndx
);
6344 if (stub_table
== NULL
)
6346 // This is a ref from a data section to an ifunc symbol.
6347 if (target
->stub_tables().size() != 0)
6348 stub_table
= target
->stub_tables()[0];
6350 gold_assert(stub_table
!= NULL
);
6353 off
= stub_table
->find_plt_call_entry(object
, gsym
, r_type
,
6354 rela
.get_r_addend());
6356 off
= stub_table
->find_plt_call_entry(object
, r_sym
, r_type
,
6357 rela
.get_r_addend());
6358 gold_assert(off
!= invalid_address
);
6359 value
= stub_table
->stub_address() + off
;
6360 has_plt_value
= true;
6363 if (r_type
== elfcpp::R_POWERPC_GOT16
6364 || r_type
== elfcpp::R_POWERPC_GOT16_LO
6365 || r_type
== elfcpp::R_POWERPC_GOT16_HI
6366 || r_type
== elfcpp::R_POWERPC_GOT16_HA
6367 || r_type
== elfcpp::R_PPC64_GOT16_DS
6368 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
6372 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
6373 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
6377 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6378 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
6379 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
6381 value
-= target
->got_section()->got_base_offset(object
);
6383 else if (r_type
== elfcpp::R_PPC64_TOC
)
6385 value
= (target
->got_section()->output_section()->address()
6386 + object
->toc_base_offset());
6388 else if (gsym
!= NULL
6389 && (r_type
== elfcpp::R_POWERPC_REL24
6390 || r_type
== elfcpp::R_PPC_PLTREL24
)
6395 typedef typename
elfcpp::Swap
<32, big_endian
>::Valtype Valtype
;
6396 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
6397 bool can_plt_call
= false;
6398 if (rela
.get_r_offset() + 8 <= view_size
)
6400 Valtype insn
= elfcpp::Swap
<32, big_endian
>::readval(wv
);
6401 Valtype insn2
= elfcpp::Swap
<32, big_endian
>::readval(wv
+ 1);
6404 || insn2
== cror_15_15_15
|| insn2
== cror_31_31_31
))
6406 elfcpp::Swap
<32, big_endian
>::writeval(wv
+ 1, ld_2_1
+ 40);
6407 can_plt_call
= true;
6412 // If we don't have a branch and link followed by a nop,
6413 // we can't go via the plt because there is no place to
6414 // put a toc restoring instruction.
6415 // Unless we know we won't be returning.
6416 if (strcmp(gsym
->name(), "__libc_start_main") == 0)
6417 can_plt_call
= true;
6421 // g++ as of 20130507 emits self-calls without a
6422 // following nop. This is arguably wrong since we have
6423 // conflicting information. On the one hand a global
6424 // symbol and on the other a local call sequence, but
6425 // don't error for this special case.
6426 // It isn't possible to cheaply verify we have exactly
6427 // such a call. Allow all calls to the same section.
6429 Address code
= value
;
6430 if (gsym
->source() == Symbol::FROM_OBJECT
6431 && gsym
->object() == object
)
6433 Address addend
= rela
.get_r_addend();
6434 unsigned int dest_shndx
;
6435 Address opdent
= psymval
->value(object
, addend
);
6436 code
= target
->symval_for_branch(relinfo
->symtab
, opdent
,
6437 gsym
, object
, &dest_shndx
);
6439 if (dest_shndx
== 0)
6440 dest_shndx
= gsym
->shndx(&is_ordinary
);
6441 ok
= dest_shndx
== relinfo
->data_shndx
;
6445 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6446 _("call lacks nop, can't restore toc; "
6447 "recompile with -fPIC"));
6453 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6454 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
6455 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
6456 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
6458 // First instruction of a global dynamic sequence, arg setup insn.
6459 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6460 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6461 enum Got_type got_type
= GOT_TYPE_STANDARD
;
6462 if (tls_type
== tls::TLSOPT_NONE
)
6463 got_type
= GOT_TYPE_TLSGD
;
6464 else if (tls_type
== tls::TLSOPT_TO_IE
)
6465 got_type
= GOT_TYPE_TPREL
;
6466 if (got_type
!= GOT_TYPE_STANDARD
)
6470 gold_assert(gsym
->has_got_offset(got_type
));
6471 value
= gsym
->got_offset(got_type
);
6475 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6476 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
6477 value
= object
->local_got_offset(r_sym
, got_type
);
6479 value
-= target
->got_section()->got_base_offset(object
);
6481 if (tls_type
== tls::TLSOPT_TO_IE
)
6483 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6484 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
6486 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6487 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6488 insn
&= (1 << 26) - (1 << 16); // extract rt,ra from addi
6490 insn
|= 32 << 26; // lwz
6492 insn
|= 58 << 26; // ld
6493 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6495 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
6496 - elfcpp::R_POWERPC_GOT_TLSGD16
);
6498 else if (tls_type
== tls::TLSOPT_TO_LE
)
6500 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
6501 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
6503 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6504 Insn insn
= addis_3_13
;
6507 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6508 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6509 value
= psymval
->value(object
, rela
.get_r_addend());
6513 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6515 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6516 r_type
= elfcpp::R_POWERPC_NONE
;
6520 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
6521 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
6522 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
6523 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
6525 // First instruction of a local dynamic sequence, arg setup insn.
6526 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6527 if (tls_type
== tls::TLSOPT_NONE
)
6529 value
= target
->tlsld_got_offset();
6530 value
-= target
->got_section()->got_base_offset(object
);
6534 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
6535 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
6536 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
6538 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6539 Insn insn
= addis_3_13
;
6542 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6543 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6548 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6550 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6551 r_type
= elfcpp::R_POWERPC_NONE
;
6555 else if (r_type
== elfcpp::R_POWERPC_GOT_DTPREL16
6556 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_LO
6557 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HI
6558 || r_type
== elfcpp::R_POWERPC_GOT_DTPREL16_HA
)
6560 // Accesses relative to a local dynamic sequence address,
6561 // no optimisation here.
6564 gold_assert(gsym
->has_got_offset(GOT_TYPE_DTPREL
));
6565 value
= gsym
->got_offset(GOT_TYPE_DTPREL
);
6569 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6570 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_DTPREL
));
6571 value
= object
->local_got_offset(r_sym
, GOT_TYPE_DTPREL
);
6573 value
-= target
->got_section()->got_base_offset(object
);
6575 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
6576 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
6577 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
6578 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
6580 // First instruction of initial exec sequence.
6581 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6582 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
6583 if (tls_type
== tls::TLSOPT_NONE
)
6587 gold_assert(gsym
->has_got_offset(GOT_TYPE_TPREL
));
6588 value
= gsym
->got_offset(GOT_TYPE_TPREL
);
6592 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
6593 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_TPREL
));
6594 value
= object
->local_got_offset(r_sym
, GOT_TYPE_TPREL
);
6596 value
-= target
->got_section()->got_base_offset(object
);
6600 gold_assert(tls_type
== tls::TLSOPT_TO_LE
);
6601 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
6602 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
6604 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6605 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6606 insn
&= (1 << 26) - (1 << 21); // extract rt from ld
6611 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6612 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
6613 value
= psymval
->value(object
, rela
.get_r_addend());
6617 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6619 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6620 r_type
= elfcpp::R_POWERPC_NONE
;
6624 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
6625 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
6627 // Second instruction of a global dynamic sequence,
6628 // the __tls_get_addr call
6629 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
6630 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6631 const tls::Tls_optimization tls_type
= target
->optimize_tls_gd(final
);
6632 if (tls_type
!= tls::TLSOPT_NONE
)
6634 if (tls_type
== tls::TLSOPT_TO_IE
)
6636 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6637 Insn insn
= add_3_3_13
;
6640 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6641 r_type
= elfcpp::R_POWERPC_NONE
;
6645 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6646 Insn insn
= addi_3_3
;
6647 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6648 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6649 view
+= 2 * big_endian
;
6650 value
= psymval
->value(object
, rela
.get_r_addend());
6652 this->skip_next_tls_get_addr_call();
6655 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
6656 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
6658 // Second instruction of a local dynamic sequence,
6659 // the __tls_get_addr call
6660 this->expect_tls_get_addr_call(relinfo
, relnum
, rela
.get_r_offset());
6661 const tls::Tls_optimization tls_type
= target
->optimize_tls_ld();
6662 if (tls_type
== tls::TLSOPT_TO_LE
)
6664 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6665 Insn insn
= addi_3_3
;
6666 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6667 this->skip_next_tls_get_addr_call();
6668 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6669 view
+= 2 * big_endian
;
6673 else if (r_type
== elfcpp::R_POWERPC_TLS
)
6675 // Second instruction of an initial exec sequence
6676 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
6677 const tls::Tls_optimization tls_type
= target
->optimize_tls_ie(final
);
6678 if (tls_type
== tls::TLSOPT_TO_LE
)
6680 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6681 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6682 unsigned int reg
= size
== 32 ? 2 : 13;
6683 insn
= at_tls_transform(insn
, reg
);
6684 gold_assert(insn
!= 0);
6685 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6686 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
6687 view
+= 2 * big_endian
;
6688 value
= psymval
->value(object
, rela
.get_r_addend());
6691 else if (!has_plt_value
)
6694 unsigned int dest_shndx
;
6695 if (r_type
!= elfcpp::R_PPC_PLTREL24
)
6696 addend
= rela
.get_r_addend();
6697 value
= psymval
->value(object
, addend
);
6698 if (size
== 64 && is_branch_reloc(r_type
))
6699 value
= target
->symval_for_branch(relinfo
->symtab
, value
,
6700 gsym
, object
, &dest_shndx
);
6701 unsigned int max_branch_offset
= 0;
6702 if (r_type
== elfcpp::R_POWERPC_REL24
6703 || r_type
== elfcpp::R_PPC_PLTREL24
6704 || r_type
== elfcpp::R_PPC_LOCAL24PC
)
6705 max_branch_offset
= 1 << 25;
6706 else if (r_type
== elfcpp::R_POWERPC_REL14
6707 || r_type
== elfcpp::R_POWERPC_REL14_BRTAKEN
6708 || r_type
== elfcpp::R_POWERPC_REL14_BRNTAKEN
)
6709 max_branch_offset
= 1 << 15;
6710 if (max_branch_offset
!= 0
6711 && value
- address
+ max_branch_offset
>= 2 * max_branch_offset
)
6713 Stub_table
<size
, big_endian
>* stub_table
6714 = object
->stub_table(relinfo
->data_shndx
);
6715 if (stub_table
!= NULL
)
6717 Address off
= stub_table
->find_long_branch_entry(object
, value
);
6718 if (off
!= invalid_address
)
6719 value
= (stub_table
->stub_address() + stub_table
->plt_size()
6727 case elfcpp::R_PPC64_REL64
:
6728 case elfcpp::R_POWERPC_REL32
:
6729 case elfcpp::R_POWERPC_REL24
:
6730 case elfcpp::R_PPC_PLTREL24
:
6731 case elfcpp::R_PPC_LOCAL24PC
:
6732 case elfcpp::R_POWERPC_REL16
:
6733 case elfcpp::R_POWERPC_REL16_LO
:
6734 case elfcpp::R_POWERPC_REL16_HI
:
6735 case elfcpp::R_POWERPC_REL16_HA
:
6736 case elfcpp::R_POWERPC_REL14
:
6737 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6738 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6742 case elfcpp::R_PPC64_TOC16
:
6743 case elfcpp::R_PPC64_TOC16_LO
:
6744 case elfcpp::R_PPC64_TOC16_HI
:
6745 case elfcpp::R_PPC64_TOC16_HA
:
6746 case elfcpp::R_PPC64_TOC16_DS
:
6747 case elfcpp::R_PPC64_TOC16_LO_DS
:
6748 // Subtract the TOC base address.
6749 value
-= (target
->got_section()->output_section()->address()
6750 + object
->toc_base_offset());
6753 case elfcpp::R_POWERPC_SECTOFF
:
6754 case elfcpp::R_POWERPC_SECTOFF_LO
:
6755 case elfcpp::R_POWERPC_SECTOFF_HI
:
6756 case elfcpp::R_POWERPC_SECTOFF_HA
:
6757 case elfcpp::R_PPC64_SECTOFF_DS
:
6758 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
6760 value
-= os
->address();
6763 case elfcpp::R_PPC64_TPREL16_DS
:
6764 case elfcpp::R_PPC64_TPREL16_LO_DS
:
6765 case elfcpp::R_PPC64_TPREL16_HIGH
:
6766 case elfcpp::R_PPC64_TPREL16_HIGHA
:
6768 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
6770 case elfcpp::R_POWERPC_TPREL16
:
6771 case elfcpp::R_POWERPC_TPREL16_LO
:
6772 case elfcpp::R_POWERPC_TPREL16_HI
:
6773 case elfcpp::R_POWERPC_TPREL16_HA
:
6774 case elfcpp::R_POWERPC_TPREL
:
6775 case elfcpp::R_PPC64_TPREL16_HIGHER
:
6776 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
6777 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
6778 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
6779 // tls symbol values are relative to tls_segment()->vaddr()
6783 case elfcpp::R_PPC64_DTPREL16_DS
:
6784 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
6785 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
6786 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
6787 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
6788 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
6790 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
6791 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
6793 case elfcpp::R_POWERPC_DTPREL16
:
6794 case elfcpp::R_POWERPC_DTPREL16_LO
:
6795 case elfcpp::R_POWERPC_DTPREL16_HI
:
6796 case elfcpp::R_POWERPC_DTPREL16_HA
:
6797 case elfcpp::R_POWERPC_DTPREL
:
6798 case elfcpp::R_PPC64_DTPREL16_HIGH
:
6799 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
6800 // tls symbol values are relative to tls_segment()->vaddr()
6801 value
-= dtp_offset
;
6808 Insn branch_bit
= 0;
6811 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6812 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6813 branch_bit
= 1 << 21;
6814 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6815 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6817 Insn
* iview
= reinterpret_cast<Insn
*>(view
);
6818 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6821 if (this->is_isa_v2
)
6823 // Set 'a' bit. This is 0b00010 in BO field for branch
6824 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
6825 // for branch on CTR insns (BO == 1a00t or 1a01t).
6826 if ((insn
& (0x14 << 21)) == (0x04 << 21))
6828 else if ((insn
& (0x14 << 21)) == (0x10 << 21))
6835 // Invert 'y' bit if not the default.
6836 if (static_cast<Signed_address
>(value
) < 0)
6839 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6849 // Multi-instruction sequences that access the TOC can be
6850 // optimized, eg. addis ra,r2,0; addi rb,ra,x;
6851 // to nop; addi rb,r2,x;
6857 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6858 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6859 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6860 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6861 case elfcpp::R_POWERPC_GOT16_HA
:
6862 case elfcpp::R_PPC64_TOC16_HA
:
6863 if (parameters
->options().toc_optimize())
6865 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6866 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6867 if ((insn
& ((0x3f << 26) | 0x1f << 16))
6868 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
6869 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6870 _("toc optimization is not supported "
6871 "for %#08x instruction"), insn
);
6872 else if (value
+ 0x8000 < 0x10000)
6874 elfcpp::Swap
<32, big_endian
>::writeval(iview
, nop
);
6880 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
6881 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
6882 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
6883 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
6884 case elfcpp::R_POWERPC_GOT16_LO
:
6885 case elfcpp::R_PPC64_GOT16_LO_DS
:
6886 case elfcpp::R_PPC64_TOC16_LO
:
6887 case elfcpp::R_PPC64_TOC16_LO_DS
:
6888 if (parameters
->options().toc_optimize())
6890 Insn
* iview
= reinterpret_cast<Insn
*>(view
- 2 * big_endian
);
6891 Insn insn
= elfcpp::Swap
<32, big_endian
>::readval(iview
);
6892 if (!ok_lo_toc_insn(insn
))
6893 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
6894 _("toc optimization is not supported "
6895 "for %#08x instruction"), insn
);
6896 else if (value
+ 0x8000 < 0x10000)
6898 if ((insn
& (0x3f << 26)) == 12u << 26 /* addic */)
6900 // Transform addic to addi when we change reg.
6901 insn
&= ~((0x3f << 26) | (0x1f << 16));
6902 insn
|= (14u << 26) | (2 << 16);
6906 insn
&= ~(0x1f << 16);
6909 elfcpp::Swap
<32, big_endian
>::writeval(iview
, insn
);
6916 typename
Reloc::Overflow_check overflow
= Reloc::CHECK_NONE
;
6919 case elfcpp::R_POWERPC_ADDR32
:
6920 case elfcpp::R_POWERPC_UADDR32
:
6922 overflow
= Reloc::CHECK_BITFIELD
;
6925 case elfcpp::R_POWERPC_REL32
:
6927 overflow
= Reloc::CHECK_SIGNED
;
6930 case elfcpp::R_POWERPC_ADDR24
:
6931 case elfcpp::R_POWERPC_ADDR16
:
6932 case elfcpp::R_POWERPC_UADDR16
:
6933 case elfcpp::R_PPC64_ADDR16_DS
:
6934 case elfcpp::R_POWERPC_ADDR14
:
6935 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
6936 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
6937 overflow
= Reloc::CHECK_BITFIELD
;
6940 case elfcpp::R_POWERPC_ADDR16_HI
:
6941 case elfcpp::R_POWERPC_ADDR16_HA
:
6942 case elfcpp::R_POWERPC_GOT16_HI
:
6943 case elfcpp::R_POWERPC_GOT16_HA
:
6944 case elfcpp::R_POWERPC_PLT16_HI
:
6945 case elfcpp::R_POWERPC_PLT16_HA
:
6946 case elfcpp::R_POWERPC_SECTOFF_HI
:
6947 case elfcpp::R_POWERPC_SECTOFF_HA
:
6948 case elfcpp::R_PPC64_TOC16_HI
:
6949 case elfcpp::R_PPC64_TOC16_HA
:
6950 case elfcpp::R_PPC64_PLTGOT16_HI
:
6951 case elfcpp::R_PPC64_PLTGOT16_HA
:
6952 case elfcpp::R_POWERPC_TPREL16_HI
:
6953 case elfcpp::R_POWERPC_TPREL16_HA
:
6954 case elfcpp::R_POWERPC_DTPREL16_HI
:
6955 case elfcpp::R_POWERPC_DTPREL16_HA
:
6956 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
6957 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
6958 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
6959 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
6960 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
6961 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
6962 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
6963 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
6964 case elfcpp::R_POWERPC_REL16_HI
:
6965 case elfcpp::R_POWERPC_REL16_HA
:
6968 case elfcpp::R_POWERPC_REL24
:
6969 case elfcpp::R_PPC_PLTREL24
:
6970 case elfcpp::R_PPC_LOCAL24PC
:
6971 case elfcpp::R_POWERPC_REL16
:
6972 case elfcpp::R_PPC64_TOC16
:
6973 case elfcpp::R_POWERPC_GOT16
:
6974 case elfcpp::R_POWERPC_SECTOFF
:
6975 case elfcpp::R_POWERPC_TPREL16
:
6976 case elfcpp::R_POWERPC_DTPREL16
:
6977 case elfcpp::R_PPC64_TPREL16_DS
:
6978 case elfcpp::R_PPC64_DTPREL16_DS
:
6979 case elfcpp::R_PPC64_TOC16_DS
:
6980 case elfcpp::R_PPC64_GOT16_DS
:
6981 case elfcpp::R_PPC64_SECTOFF_DS
:
6982 case elfcpp::R_POWERPC_REL14
:
6983 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
6984 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
6985 case elfcpp::R_POWERPC_GOT_TLSGD16
:
6986 case elfcpp::R_POWERPC_GOT_TLSLD16
:
6987 case elfcpp::R_POWERPC_GOT_TPREL16
:
6988 case elfcpp::R_POWERPC_GOT_DTPREL16
:
6989 overflow
= Reloc::CHECK_SIGNED
;
6993 typename Powerpc_relocate_functions
<size
, big_endian
>::Status status
6994 = Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
;
6997 case elfcpp::R_POWERPC_NONE
:
6998 case elfcpp::R_POWERPC_TLS
:
6999 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
7000 case elfcpp::R_POWERPC_GNU_VTENTRY
:
7003 case elfcpp::R_PPC64_ADDR64
:
7004 case elfcpp::R_PPC64_REL64
:
7005 case elfcpp::R_PPC64_TOC
:
7006 Reloc::addr64(view
, value
);
7009 case elfcpp::R_POWERPC_TPREL
:
7010 case elfcpp::R_POWERPC_DTPREL
:
7012 Reloc::addr64(view
, value
);
7014 status
= Reloc::addr32(view
, value
, overflow
);
7017 case elfcpp::R_PPC64_UADDR64
:
7018 Reloc::addr64_u(view
, value
);
7021 case elfcpp::R_POWERPC_ADDR32
:
7022 status
= Reloc::addr32(view
, value
, overflow
);
7025 case elfcpp::R_POWERPC_REL32
:
7026 case elfcpp::R_POWERPC_UADDR32
:
7027 status
= Reloc::addr32_u(view
, value
, overflow
);
7030 case elfcpp::R_POWERPC_ADDR24
:
7031 case elfcpp::R_POWERPC_REL24
:
7032 case elfcpp::R_PPC_PLTREL24
:
7033 case elfcpp::R_PPC_LOCAL24PC
:
7034 status
= Reloc::addr24(view
, value
, overflow
);
7037 case elfcpp::R_POWERPC_GOT_DTPREL16
:
7038 case elfcpp::R_POWERPC_GOT_DTPREL16_LO
:
7041 status
= Reloc::addr16_ds(view
, value
, overflow
);
7044 case elfcpp::R_POWERPC_ADDR16
:
7045 case elfcpp::R_POWERPC_REL16
:
7046 case elfcpp::R_PPC64_TOC16
:
7047 case elfcpp::R_POWERPC_GOT16
:
7048 case elfcpp::R_POWERPC_SECTOFF
:
7049 case elfcpp::R_POWERPC_TPREL16
:
7050 case elfcpp::R_POWERPC_DTPREL16
:
7051 case elfcpp::R_POWERPC_GOT_TLSGD16
:
7052 case elfcpp::R_POWERPC_GOT_TLSLD16
:
7053 case elfcpp::R_POWERPC_GOT_TPREL16
:
7054 case elfcpp::R_POWERPC_ADDR16_LO
:
7055 case elfcpp::R_POWERPC_REL16_LO
:
7056 case elfcpp::R_PPC64_TOC16_LO
:
7057 case elfcpp::R_POWERPC_GOT16_LO
:
7058 case elfcpp::R_POWERPC_SECTOFF_LO
:
7059 case elfcpp::R_POWERPC_TPREL16_LO
:
7060 case elfcpp::R_POWERPC_DTPREL16_LO
:
7061 case elfcpp::R_POWERPC_GOT_TLSGD16_LO
:
7062 case elfcpp::R_POWERPC_GOT_TLSLD16_LO
:
7063 case elfcpp::R_POWERPC_GOT_TPREL16_LO
:
7064 status
= Reloc::addr16(view
, value
, overflow
);
7067 case elfcpp::R_POWERPC_UADDR16
:
7068 status
= Reloc::addr16_u(view
, value
, overflow
);
7071 case elfcpp::R_PPC64_ADDR16_HIGH
:
7072 case elfcpp::R_PPC64_TPREL16_HIGH
:
7073 case elfcpp::R_PPC64_DTPREL16_HIGH
:
7075 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
7077 case elfcpp::R_POWERPC_ADDR16_HI
:
7078 case elfcpp::R_POWERPC_REL16_HI
:
7079 case elfcpp::R_PPC64_TOC16_HI
:
7080 case elfcpp::R_POWERPC_GOT16_HI
:
7081 case elfcpp::R_POWERPC_SECTOFF_HI
:
7082 case elfcpp::R_POWERPC_TPREL16_HI
:
7083 case elfcpp::R_POWERPC_DTPREL16_HI
:
7084 case elfcpp::R_POWERPC_GOT_TLSGD16_HI
:
7085 case elfcpp::R_POWERPC_GOT_TLSLD16_HI
:
7086 case elfcpp::R_POWERPC_GOT_TPREL16_HI
:
7087 case elfcpp::R_POWERPC_GOT_DTPREL16_HI
:
7088 Reloc::addr16_hi(view
, value
);
7091 case elfcpp::R_PPC64_ADDR16_HIGHA
:
7092 case elfcpp::R_PPC64_TPREL16_HIGHA
:
7093 case elfcpp::R_PPC64_DTPREL16_HIGHA
:
7095 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
7097 case elfcpp::R_POWERPC_ADDR16_HA
:
7098 case elfcpp::R_POWERPC_REL16_HA
:
7099 case elfcpp::R_PPC64_TOC16_HA
:
7100 case elfcpp::R_POWERPC_GOT16_HA
:
7101 case elfcpp::R_POWERPC_SECTOFF_HA
:
7102 case elfcpp::R_POWERPC_TPREL16_HA
:
7103 case elfcpp::R_POWERPC_DTPREL16_HA
:
7104 case elfcpp::R_POWERPC_GOT_TLSGD16_HA
:
7105 case elfcpp::R_POWERPC_GOT_TLSLD16_HA
:
7106 case elfcpp::R_POWERPC_GOT_TPREL16_HA
:
7107 case elfcpp::R_POWERPC_GOT_DTPREL16_HA
:
7108 Reloc::addr16_ha(view
, value
);
7111 case elfcpp::R_PPC64_DTPREL16_HIGHER
:
7113 // R_PPC_EMB_NADDR16_LO
7115 case elfcpp::R_PPC64_ADDR16_HIGHER
:
7116 case elfcpp::R_PPC64_TPREL16_HIGHER
:
7117 Reloc::addr16_hi2(view
, value
);
7120 case elfcpp::R_PPC64_DTPREL16_HIGHERA
:
7122 // R_PPC_EMB_NADDR16_HI
7124 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
7125 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
7126 Reloc::addr16_ha2(view
, value
);
7129 case elfcpp::R_PPC64_DTPREL16_HIGHEST
:
7131 // R_PPC_EMB_NADDR16_HA
7133 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
7134 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
7135 Reloc::addr16_hi3(view
, value
);
7138 case elfcpp::R_PPC64_DTPREL16_HIGHESTA
:
7142 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
7143 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
7144 Reloc::addr16_ha3(view
, value
);
7147 case elfcpp::R_PPC64_DTPREL16_DS
:
7148 case elfcpp::R_PPC64_DTPREL16_LO_DS
:
7150 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
7152 case elfcpp::R_PPC64_TPREL16_DS
:
7153 case elfcpp::R_PPC64_TPREL16_LO_DS
:
7155 // R_PPC_TLSGD, R_PPC_TLSLD
7157 case elfcpp::R_PPC64_ADDR16_DS
:
7158 case elfcpp::R_PPC64_ADDR16_LO_DS
:
7159 case elfcpp::R_PPC64_TOC16_DS
:
7160 case elfcpp::R_PPC64_TOC16_LO_DS
:
7161 case elfcpp::R_PPC64_GOT16_DS
:
7162 case elfcpp::R_PPC64_GOT16_LO_DS
:
7163 case elfcpp::R_PPC64_SECTOFF_DS
:
7164 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
7165 status
= Reloc::addr16_ds(view
, value
, overflow
);
7168 case elfcpp::R_POWERPC_ADDR14
:
7169 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
7170 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
7171 case elfcpp::R_POWERPC_REL14
:
7172 case elfcpp::R_POWERPC_REL14_BRTAKEN
:
7173 case elfcpp::R_POWERPC_REL14_BRNTAKEN
:
7174 status
= Reloc::addr14(view
, value
, overflow
);
7177 case elfcpp::R_POWERPC_COPY
:
7178 case elfcpp::R_POWERPC_GLOB_DAT
:
7179 case elfcpp::R_POWERPC_JMP_SLOT
:
7180 case elfcpp::R_POWERPC_RELATIVE
:
7181 case elfcpp::R_POWERPC_DTPMOD
:
7182 case elfcpp::R_PPC64_JMP_IREL
:
7183 case elfcpp::R_POWERPC_IRELATIVE
:
7184 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7185 _("unexpected reloc %u in object file"),
7189 case elfcpp::R_PPC_EMB_SDA21
:
7194 // R_PPC64_TOCSAVE. For the time being this can be ignored.
7198 case elfcpp::R_PPC_EMB_SDA2I16
:
7199 case elfcpp::R_PPC_EMB_SDA2REL
:
7202 // R_PPC64_TLSGD, R_PPC64_TLSLD
7205 case elfcpp::R_POWERPC_PLT32
:
7206 case elfcpp::R_POWERPC_PLTREL32
:
7207 case elfcpp::R_POWERPC_PLT16_LO
:
7208 case elfcpp::R_POWERPC_PLT16_HI
:
7209 case elfcpp::R_POWERPC_PLT16_HA
:
7210 case elfcpp::R_PPC_SDAREL16
:
7211 case elfcpp::R_POWERPC_ADDR30
:
7212 case elfcpp::R_PPC64_PLT64
:
7213 case elfcpp::R_PPC64_PLTREL64
:
7214 case elfcpp::R_PPC64_PLTGOT16
:
7215 case elfcpp::R_PPC64_PLTGOT16_LO
:
7216 case elfcpp::R_PPC64_PLTGOT16_HI
:
7217 case elfcpp::R_PPC64_PLTGOT16_HA
:
7218 case elfcpp::R_PPC64_PLT16_LO_DS
:
7219 case elfcpp::R_PPC64_PLTGOT16_DS
:
7220 case elfcpp::R_PPC64_PLTGOT16_LO_DS
:
7221 case elfcpp::R_PPC_EMB_RELSDA
:
7222 case elfcpp::R_PPC_TOC16
:
7225 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7226 _("unsupported reloc %u"),
7230 if (status
!= Powerpc_relocate_functions
<size
, big_endian
>::STATUS_OK
)
7231 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
7232 _("relocation overflow"));
7237 // Relocate section data.
7239 template<int size
, bool big_endian
>
7241 Target_powerpc
<size
, big_endian
>::relocate_section(
7242 const Relocate_info
<size
, big_endian
>* relinfo
,
7243 unsigned int sh_type
,
7244 const unsigned char* prelocs
,
7246 Output_section
* output_section
,
7247 bool needs_special_offset_handling
,
7248 unsigned char* view
,
7250 section_size_type view_size
,
7251 const Reloc_symbol_changes
* reloc_symbol_changes
)
7253 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
7254 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
7255 typedef typename Target_powerpc
<size
, big_endian
>::Relocate_comdat_behavior
7256 Powerpc_comdat_behavior
;
7258 gold_assert(sh_type
== elfcpp::SHT_RELA
);
7260 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
7261 Powerpc_relocate
, Powerpc_comdat_behavior
>(
7267 needs_special_offset_handling
,
7271 reloc_symbol_changes
);
7274 class Powerpc_scan_relocatable_reloc
7277 // Return the strategy to use for a local symbol which is not a
7278 // section symbol, given the relocation type.
7279 inline Relocatable_relocs::Reloc_strategy
7280 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
7282 if (r_type
== 0 && r_sym
== 0)
7283 return Relocatable_relocs::RELOC_DISCARD
;
7284 return Relocatable_relocs::RELOC_COPY
;
7287 // Return the strategy to use for a local symbol which is a section
7288 // symbol, given the relocation type.
7289 inline Relocatable_relocs::Reloc_strategy
7290 local_section_strategy(unsigned int, Relobj
*)
7292 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
7295 // Return the strategy to use for a global symbol, given the
7296 // relocation type, the object, and the symbol index.
7297 inline Relocatable_relocs::Reloc_strategy
7298 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
7300 if (r_type
== elfcpp::R_PPC_PLTREL24
)
7301 return Relocatable_relocs::RELOC_SPECIAL
;
7302 return Relocatable_relocs::RELOC_COPY
;
7306 // Scan the relocs during a relocatable link.
7308 template<int size
, bool big_endian
>
7310 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
7311 Symbol_table
* symtab
,
7313 Sized_relobj_file
<size
, big_endian
>* object
,
7314 unsigned int data_shndx
,
7315 unsigned int sh_type
,
7316 const unsigned char* prelocs
,
7318 Output_section
* output_section
,
7319 bool needs_special_offset_handling
,
7320 size_t local_symbol_count
,
7321 const unsigned char* plocal_symbols
,
7322 Relocatable_relocs
* rr
)
7324 gold_assert(sh_type
== elfcpp::SHT_RELA
);
7326 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
7327 Powerpc_scan_relocatable_reloc
>(
7335 needs_special_offset_handling
,
7341 // Emit relocations for a section.
7342 // This is a modified version of the function by the same name in
7343 // target-reloc.h. Using relocate_special_relocatable for
7344 // R_PPC_PLTREL24 would require duplication of the entire body of the
7345 // loop, so we may as well duplicate the whole thing.
7347 template<int size
, bool big_endian
>
7349 Target_powerpc
<size
, big_endian
>::relocate_relocs(
7350 const Relocate_info
<size
, big_endian
>* relinfo
,
7351 unsigned int sh_type
,
7352 const unsigned char* prelocs
,
7354 Output_section
* output_section
,
7355 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
7356 const Relocatable_relocs
* rr
,
7358 Address view_address
,
7360 unsigned char* reloc_view
,
7361 section_size_type reloc_view_size
)
7363 gold_assert(sh_type
== elfcpp::SHT_RELA
);
7365 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
7367 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc_write
7369 const int reloc_size
7370 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
7372 Powerpc_relobj
<size
, big_endian
>* const object
7373 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
7374 const unsigned int local_count
= object
->local_symbol_count();
7375 unsigned int got2_shndx
= object
->got2_shndx();
7376 Address got2_addend
= 0;
7377 if (got2_shndx
!= 0)
7379 got2_addend
= object
->get_output_section_offset(got2_shndx
);
7380 gold_assert(got2_addend
!= invalid_address
);
7383 unsigned char* pwrite
= reloc_view
;
7384 bool zap_next
= false;
7385 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
7387 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
7388 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
7391 Reltype
reloc(prelocs
);
7392 Reltype_write
reloc_write(pwrite
);
7394 Address offset
= reloc
.get_r_offset();
7395 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
7396 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
7397 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
7398 const unsigned int orig_r_sym
= r_sym
;
7399 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
7400 = reloc
.get_r_addend();
7401 const Symbol
* gsym
= NULL
;
7405 // We could arrange to discard these and other relocs for
7406 // tls optimised sequences in the strategy methods, but for
7407 // now do as BFD ld does.
7408 r_type
= elfcpp::R_POWERPC_NONE
;
7412 // Get the new symbol index.
7413 if (r_sym
< local_count
)
7417 case Relocatable_relocs::RELOC_COPY
:
7418 case Relocatable_relocs::RELOC_SPECIAL
:
7421 r_sym
= object
->symtab_index(r_sym
);
7422 gold_assert(r_sym
!= -1U);
7426 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
7428 // We are adjusting a section symbol. We need to find
7429 // the symbol table index of the section symbol for
7430 // the output section corresponding to input section
7431 // in which this symbol is defined.
7432 gold_assert(r_sym
< local_count
);
7434 unsigned int shndx
=
7435 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
7436 gold_assert(is_ordinary
);
7437 Output_section
* os
= object
->output_section(shndx
);
7438 gold_assert(os
!= NULL
);
7439 gold_assert(os
->needs_symtab_index());
7440 r_sym
= os
->symtab_index();
7450 gsym
= object
->global_symbol(r_sym
);
7451 gold_assert(gsym
!= NULL
);
7452 if (gsym
->is_forwarder())
7453 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
7455 gold_assert(gsym
->has_symtab_index());
7456 r_sym
= gsym
->symtab_index();
7459 // Get the new offset--the location in the output section where
7460 // this relocation should be applied.
7461 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
7462 offset
+= offset_in_output_section
;
7465 section_offset_type sot_offset
=
7466 convert_types
<section_offset_type
, Address
>(offset
);
7467 section_offset_type new_sot_offset
=
7468 output_section
->output_offset(object
, relinfo
->data_shndx
,
7470 gold_assert(new_sot_offset
!= -1);
7471 offset
= new_sot_offset
;
7474 // In an object file, r_offset is an offset within the section.
7475 // In an executable or dynamic object, generated by
7476 // --emit-relocs, r_offset is an absolute address.
7477 if (!parameters
->options().relocatable())
7479 offset
+= view_address
;
7480 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
7481 offset
-= offset_in_output_section
;
7484 // Handle the reloc addend based on the strategy.
7485 if (strategy
== Relocatable_relocs::RELOC_COPY
)
7487 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
7489 const Symbol_value
<size
>* psymval
= object
->local_symbol(orig_r_sym
);
7490 addend
= psymval
->value(object
, addend
);
7492 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
7494 if (addend
>= 32768)
7495 addend
+= got2_addend
;
7500 if (!parameters
->options().relocatable())
7502 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7503 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
7504 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HI
7505 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_HA
)
7507 // First instruction of a global dynamic sequence,
7509 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7510 switch (this->optimize_tls_gd(final
))
7512 case tls::TLSOPT_TO_IE
:
7513 r_type
+= (elfcpp::R_POWERPC_GOT_TPREL16
7514 - elfcpp::R_POWERPC_GOT_TLSGD16
);
7516 case tls::TLSOPT_TO_LE
:
7517 if (r_type
== elfcpp::R_POWERPC_GOT_TLSGD16
7518 || r_type
== elfcpp::R_POWERPC_GOT_TLSGD16_LO
)
7519 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7522 r_type
= elfcpp::R_POWERPC_NONE
;
7523 offset
-= 2 * big_endian
;
7530 else if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7531 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
7532 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HI
7533 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_HA
)
7535 // First instruction of a local dynamic sequence,
7537 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
7539 if (r_type
== elfcpp::R_POWERPC_GOT_TLSLD16
7540 || r_type
== elfcpp::R_POWERPC_GOT_TLSLD16_LO
)
7542 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7543 const Output_section
* os
= relinfo
->layout
->tls_segment()
7545 gold_assert(os
!= NULL
);
7546 gold_assert(os
->needs_symtab_index());
7547 r_sym
= os
->symtab_index();
7548 addend
= dtp_offset
;
7552 r_type
= elfcpp::R_POWERPC_NONE
;
7553 offset
-= 2 * big_endian
;
7557 else if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
7558 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
7559 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HI
7560 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_HA
)
7562 // First instruction of initial exec sequence.
7563 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7564 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
7566 if (r_type
== elfcpp::R_POWERPC_GOT_TPREL16
7567 || r_type
== elfcpp::R_POWERPC_GOT_TPREL16_LO
)
7568 r_type
= elfcpp::R_POWERPC_TPREL16_HA
;
7571 r_type
= elfcpp::R_POWERPC_NONE
;
7572 offset
-= 2 * big_endian
;
7576 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSGD
)
7577 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSGD
))
7579 // Second instruction of a global dynamic sequence,
7580 // the __tls_get_addr call
7581 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7582 switch (this->optimize_tls_gd(final
))
7584 case tls::TLSOPT_TO_IE
:
7585 r_type
= elfcpp::R_POWERPC_NONE
;
7588 case tls::TLSOPT_TO_LE
:
7589 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7590 offset
+= 2 * big_endian
;
7597 else if ((size
== 64 && r_type
== elfcpp::R_PPC64_TLSLD
)
7598 || (size
== 32 && r_type
== elfcpp::R_PPC_TLSLD
))
7600 // Second instruction of a local dynamic sequence,
7601 // the __tls_get_addr call
7602 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE
)
7604 const Output_section
* os
= relinfo
->layout
->tls_segment()
7606 gold_assert(os
!= NULL
);
7607 gold_assert(os
->needs_symtab_index());
7608 r_sym
= os
->symtab_index();
7609 addend
= dtp_offset
;
7610 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7611 offset
+= 2 * big_endian
;
7615 else if (r_type
== elfcpp::R_POWERPC_TLS
)
7617 // Second instruction of an initial exec sequence
7618 const bool final
= gsym
== NULL
|| gsym
->final_value_is_known();
7619 if (this->optimize_tls_ie(final
) == tls::TLSOPT_TO_LE
)
7621 r_type
= elfcpp::R_POWERPC_TPREL16_LO
;
7622 offset
+= 2 * big_endian
;
7627 reloc_write
.put_r_offset(offset
);
7628 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(r_sym
, r_type
));
7629 reloc_write
.put_r_addend(addend
);
7631 pwrite
+= reloc_size
;
7634 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
7635 == reloc_view_size
);
7638 // Return the value to use for a dynamic symbol which requires special
7639 // treatment. This is how we support equality comparisons of function
7640 // pointers across shared library boundaries, as described in the
7641 // processor specific ABI supplement.
7643 template<int size
, bool big_endian
>
7645 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
7649 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
7650 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7651 p
!= this->stub_tables_
.end();
7654 Address off
= (*p
)->find_plt_call_entry(gsym
);
7655 if (off
!= invalid_address
)
7656 return (*p
)->stub_address() + off
;
7662 // Return the PLT address to use for a local symbol.
7663 template<int size
, bool big_endian
>
7665 Target_powerpc
<size
, big_endian
>::do_plt_address_for_local(
7666 const Relobj
* object
,
7667 unsigned int symndx
) const
7671 const Sized_relobj
<size
, big_endian
>* relobj
7672 = static_cast<const Sized_relobj
<size
, big_endian
>*>(object
);
7673 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7674 p
!= this->stub_tables_
.end();
7677 Address off
= (*p
)->find_plt_call_entry(relobj
->sized_relobj(),
7679 if (off
!= invalid_address
)
7680 return (*p
)->stub_address() + off
;
7686 // Return the PLT address to use for a global symbol.
7687 template<int size
, bool big_endian
>
7689 Target_powerpc
<size
, big_endian
>::do_plt_address_for_global(
7690 const Symbol
* gsym
) const
7694 for (typename
Stub_tables::const_iterator p
= this->stub_tables_
.begin();
7695 p
!= this->stub_tables_
.end();
7698 Address off
= (*p
)->find_plt_call_entry(gsym
);
7699 if (off
!= invalid_address
)
7700 return (*p
)->stub_address() + off
;
7706 // Return the offset to use for the GOT_INDX'th got entry which is
7707 // for a local tls symbol specified by OBJECT, SYMNDX.
7708 template<int size
, bool big_endian
>
7710 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_local(
7711 const Relobj
* object
,
7712 unsigned int symndx
,
7713 unsigned int got_indx
) const
7715 const Powerpc_relobj
<size
, big_endian
>* ppc_object
7716 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
7717 if (ppc_object
->local_symbol(symndx
)->is_tls_symbol())
7719 for (Got_type got_type
= GOT_TYPE_TLSGD
;
7720 got_type
<= GOT_TYPE_TPREL
;
7721 got_type
= Got_type(got_type
+ 1))
7722 if (ppc_object
->local_has_got_offset(symndx
, got_type
))
7724 unsigned int off
= ppc_object
->local_got_offset(symndx
, got_type
);
7725 if (got_type
== GOT_TYPE_TLSGD
)
7727 if (off
== got_indx
* (size
/ 8))
7729 if (got_type
== GOT_TYPE_TPREL
)
7739 // Return the offset to use for the GOT_INDX'th got entry which is
7740 // for global tls symbol GSYM.
7741 template<int size
, bool big_endian
>
7743 Target_powerpc
<size
, big_endian
>::do_tls_offset_for_global(
7745 unsigned int got_indx
) const
7747 if (gsym
->type() == elfcpp::STT_TLS
)
7749 for (Got_type got_type
= GOT_TYPE_TLSGD
;
7750 got_type
<= GOT_TYPE_TPREL
;
7751 got_type
= Got_type(got_type
+ 1))
7752 if (gsym
->has_got_offset(got_type
))
7754 unsigned int off
= gsym
->got_offset(got_type
);
7755 if (got_type
== GOT_TYPE_TLSGD
)
7757 if (off
== got_indx
* (size
/ 8))
7759 if (got_type
== GOT_TYPE_TPREL
)
7769 // The selector for powerpc object files.
7771 template<int size
, bool big_endian
>
7772 class Target_selector_powerpc
: public Target_selector
7775 Target_selector_powerpc()
7776 : Target_selector(size
== 64 ? elfcpp::EM_PPC64
: elfcpp::EM_PPC
,
7779 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
7780 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
7782 ? (big_endian
? "elf64ppc" : "elf64lppc")
7783 : (big_endian
? "elf32ppc" : "elf32lppc")))
7787 do_instantiate_target()
7788 { return new Target_powerpc
<size
, big_endian
>(); }
7791 Target_selector_powerpc
<32, true> target_selector_ppc32
;
7792 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
7793 Target_selector_powerpc
<64, true> target_selector_ppc64
;
7794 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
7796 // Instantiate these constants for -O0
7797 template<int size
, bool big_endian
>
7798 const int Output_data_glink
<size
, big_endian
>::pltresolve_size
;
7799 template<int size
, bool big_endian
>
7800 const typename Stub_table
<size
, big_endian
>::Address
7801 Stub_table
<size
, big_endian
>::invalid_address
;
7802 template<int size
, bool big_endian
>
7803 const typename Target_powerpc
<size
, big_endian
>::Address
7804 Target_powerpc
<size
, big_endian
>::invalid_address
;
7806 } // End anonymous namespace.